Imported Upstream version 4.8.1
[platform/upstream/gcc48.git] / gcc / fortran / primary.c
1 /* Primary expression subroutines
2    Copyright (C) 2000-2013 Free Software Foundation, Inc.
3    Contributed by Andy Vaught
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "flags.h"
25 #include "gfortran.h"
26 #include "arith.h"
27 #include "match.h"
28 #include "parse.h"
29 #include "constructor.h"
30
31 int matching_actual_arglist = 0;
32
33 /* Matches a kind-parameter expression, which is either a named
34    symbolic constant or a nonnegative integer constant.  If
35    successful, sets the kind value to the correct integer.
36    The argument 'is_iso_c' signals whether the kind is an ISO_C_BINDING
37    symbol like e.g. 'c_int'.  */
38
39 static match
40 match_kind_param (int *kind, int *is_iso_c)
41 {
42   char name[GFC_MAX_SYMBOL_LEN + 1];
43   gfc_symbol *sym;
44   const char *p;
45   match m;
46
47   *is_iso_c = 0;
48
49   m = gfc_match_small_literal_int (kind, NULL);
50   if (m != MATCH_NO)
51     return m;
52
53   m = gfc_match_name (name);
54   if (m != MATCH_YES)
55     return m;
56
57   if (gfc_find_symbol (name, NULL, 1, &sym))
58     return MATCH_ERROR;
59
60   if (sym == NULL)
61     return MATCH_NO;
62
63   *is_iso_c = sym->attr.is_iso_c;
64
65   if (sym->attr.flavor != FL_PARAMETER)
66     return MATCH_NO;
67
68   if (sym->value == NULL)
69     return MATCH_NO;
70
71   p = gfc_extract_int (sym->value, kind);
72   if (p != NULL)
73     return MATCH_NO;
74
75   gfc_set_sym_referenced (sym);
76
77   if (*kind < 0)
78     return MATCH_NO;
79
80   return MATCH_YES;
81 }
82
83
84 /* Get a trailing kind-specification for non-character variables.
85    Returns:
86      * the integer kind value or
87      * -1 if an error was generated,
88      * -2 if no kind was found.
89    The argument 'is_iso_c' signals whether the kind is an ISO_C_BINDING
90    symbol like e.g. 'c_int'.  */
91
92 static int
93 get_kind (int *is_iso_c)
94 {
95   int kind;
96   match m;
97
98   *is_iso_c = 0;
99
100   if (gfc_match_char ('_') != MATCH_YES)
101     return -2;
102
103   m = match_kind_param (&kind, is_iso_c);
104   if (m == MATCH_NO)
105     gfc_error ("Missing kind-parameter at %C");
106
107   return (m == MATCH_YES) ? kind : -1;
108 }
109
110
111 /* Given a character and a radix, see if the character is a valid
112    digit in that radix.  */
113
114 int
115 gfc_check_digit (char c, int radix)
116 {
117   int r;
118
119   switch (radix)
120     {
121     case 2:
122       r = ('0' <= c && c <= '1');
123       break;
124
125     case 8:
126       r = ('0' <= c && c <= '7');
127       break;
128
129     case 10:
130       r = ('0' <= c && c <= '9');
131       break;
132
133     case 16:
134       r = ISXDIGIT (c);
135       break;
136
137     default:
138       gfc_internal_error ("gfc_check_digit(): bad radix");
139     }
140
141   return r;
142 }
143
144
145 /* Match the digit string part of an integer if signflag is not set,
146    the signed digit string part if signflag is set.  If the buffer 
147    is NULL, we just count characters for the resolution pass.  Returns 
148    the number of characters matched, -1 for no match.  */
149
150 static int
151 match_digits (int signflag, int radix, char *buffer)
152 {
153   locus old_loc;
154   int length;
155   char c;
156
157   length = 0;
158   c = gfc_next_ascii_char ();
159
160   if (signflag && (c == '+' || c == '-'))
161     {
162       if (buffer != NULL)
163         *buffer++ = c;
164       gfc_gobble_whitespace ();
165       c = gfc_next_ascii_char ();
166       length++;
167     }
168
169   if (!gfc_check_digit (c, radix))
170     return -1;
171
172   length++;
173   if (buffer != NULL)
174     *buffer++ = c;
175
176   for (;;)
177     {
178       old_loc = gfc_current_locus;
179       c = gfc_next_ascii_char ();
180
181       if (!gfc_check_digit (c, radix))
182         break;
183
184       if (buffer != NULL)
185         *buffer++ = c;
186       length++;
187     }
188
189   gfc_current_locus = old_loc;
190
191   return length;
192 }
193
194
195 /* Match an integer (digit string and optional kind).  
196    A sign will be accepted if signflag is set.  */
197
198 static match
199 match_integer_constant (gfc_expr **result, int signflag)
200 {
201   int length, kind, is_iso_c;
202   locus old_loc;
203   char *buffer;
204   gfc_expr *e;
205
206   old_loc = gfc_current_locus;
207   gfc_gobble_whitespace ();
208
209   length = match_digits (signflag, 10, NULL);
210   gfc_current_locus = old_loc;
211   if (length == -1)
212     return MATCH_NO;
213
214   buffer = (char *) alloca (length + 1);
215   memset (buffer, '\0', length + 1);
216
217   gfc_gobble_whitespace ();
218
219   match_digits (signflag, 10, buffer);
220
221   kind = get_kind (&is_iso_c);
222   if (kind == -2)
223     kind = gfc_default_integer_kind;
224   if (kind == -1)
225     return MATCH_ERROR;
226
227   if (kind == 4 && gfc_option.flag_integer4_kind == 8)
228     kind = 8;
229
230   if (gfc_validate_kind (BT_INTEGER, kind, true) < 0)
231     {
232       gfc_error ("Integer kind %d at %C not available", kind);
233       return MATCH_ERROR;
234     }
235
236   e = gfc_convert_integer (buffer, kind, 10, &gfc_current_locus);
237   e->ts.is_c_interop = is_iso_c;
238
239   if (gfc_range_check (e) != ARITH_OK)
240     {
241       gfc_error ("Integer too big for its kind at %C. This check can be "
242                  "disabled with the option -fno-range-check");
243
244       gfc_free_expr (e);
245       return MATCH_ERROR;
246     }
247
248   *result = e;
249   return MATCH_YES;
250 }
251
252
253 /* Match a Hollerith constant.  */
254
255 static match
256 match_hollerith_constant (gfc_expr **result)
257 {
258   locus old_loc;
259   gfc_expr *e = NULL;
260   const char *msg;
261   int num, pad;
262   int i;  
263
264   old_loc = gfc_current_locus;
265   gfc_gobble_whitespace ();
266
267   if (match_integer_constant (&e, 0) == MATCH_YES
268       && gfc_match_char ('h') == MATCH_YES)
269     {
270       if (gfc_notify_std (GFC_STD_LEGACY, "Hollerith constant "
271                           "at %C") == FAILURE)
272         goto cleanup;
273
274       msg = gfc_extract_int (e, &num);
275       if (msg != NULL)
276         {
277           gfc_error (msg);
278           goto cleanup;
279         }
280       if (num == 0)
281         {
282           gfc_error ("Invalid Hollerith constant: %L must contain at least "
283                      "one character", &old_loc);
284           goto cleanup;
285         }
286       if (e->ts.kind != gfc_default_integer_kind)
287         {
288           gfc_error ("Invalid Hollerith constant: Integer kind at %L "
289                      "should be default", &old_loc);
290           goto cleanup;
291         }
292       else
293         {
294           gfc_free_expr (e);
295           e = gfc_get_constant_expr (BT_HOLLERITH, gfc_default_character_kind,
296                                      &gfc_current_locus);
297
298           /* Calculate padding needed to fit default integer memory.  */
299           pad = gfc_default_integer_kind - (num % gfc_default_integer_kind);
300
301           e->representation.string = XCNEWVEC (char, num + pad + 1);
302
303           for (i = 0; i < num; i++)
304             {
305               gfc_char_t c = gfc_next_char_literal (INSTRING_WARN);
306               if (! gfc_wide_fits_in_byte (c))
307                 {
308                   gfc_error ("Invalid Hollerith constant at %L contains a "
309                              "wide character", &old_loc);
310                   goto cleanup;
311                 }
312
313               e->representation.string[i] = (unsigned char) c;
314             }
315
316           /* Now pad with blanks and end with a null char.  */
317           for (i = 0; i < pad; i++)
318             e->representation.string[num + i] = ' ';
319
320           e->representation.string[num + i] = '\0';
321           e->representation.length = num + pad;
322           e->ts.u.pad = pad;
323
324           *result = e;
325           return MATCH_YES;
326         }
327     }
328
329   gfc_free_expr (e);
330   gfc_current_locus = old_loc;
331   return MATCH_NO;
332
333 cleanup:
334   gfc_free_expr (e);
335   return MATCH_ERROR;
336 }
337
338
339 /* Match a binary, octal or hexadecimal constant that can be found in
340    a DATA statement.  The standard permits b'010...', o'73...', and
341    z'a1...' where b, o, and z can be capital letters.  This function
342    also accepts postfixed forms of the constants: '01...'b, '73...'o,
343    and 'a1...'z.  An additional extension is the use of x for z.  */
344
345 static match
346 match_boz_constant (gfc_expr **result)
347 {
348   int radix, length, x_hex, kind;
349   locus old_loc, start_loc;
350   char *buffer, post, delim;
351   gfc_expr *e;
352
353   start_loc = old_loc = gfc_current_locus;
354   gfc_gobble_whitespace ();
355
356   x_hex = 0;
357   switch (post = gfc_next_ascii_char ())
358     {
359     case 'b':
360       radix = 2;
361       post = 0;
362       break;
363     case 'o':
364       radix = 8;
365       post = 0;
366       break;
367     case 'x':
368       x_hex = 1;
369       /* Fall through.  */
370     case 'z':
371       radix = 16;
372       post = 0;
373       break;
374     case '\'':
375       /* Fall through.  */
376     case '\"':
377       delim = post;
378       post = 1;
379       radix = 16;  /* Set to accept any valid digit string.  */
380       break;
381     default:
382       goto backup;
383     }
384
385   /* No whitespace allowed here.  */
386
387   if (post == 0)
388     delim = gfc_next_ascii_char ();
389
390   if (delim != '\'' && delim != '\"')
391     goto backup;
392
393   if (x_hex
394       && (gfc_notify_std (GFC_STD_GNU, "Hexadecimal "
395                           "constant at %C uses non-standard syntax")
396           == FAILURE))
397       return MATCH_ERROR;
398
399   old_loc = gfc_current_locus;
400
401   length = match_digits (0, radix, NULL);
402   if (length == -1)
403     {
404       gfc_error ("Empty set of digits in BOZ constant at %C");
405       return MATCH_ERROR;
406     }
407
408   if (gfc_next_ascii_char () != delim)
409     {
410       gfc_error ("Illegal character in BOZ constant at %C");
411       return MATCH_ERROR;
412     }
413
414   if (post == 1)
415     {
416       switch (gfc_next_ascii_char ())
417         {
418         case 'b':
419           radix = 2;
420           break;
421         case 'o':
422           radix = 8;
423           break;
424         case 'x':
425           /* Fall through.  */
426         case 'z':
427           radix = 16;
428           break;
429         default:
430           goto backup;
431         }
432
433       if (gfc_notify_std (GFC_STD_GNU, "BOZ constant "
434                           "at %C uses non-standard postfix syntax")
435           == FAILURE)
436         return MATCH_ERROR;
437     }
438
439   gfc_current_locus = old_loc;
440
441   buffer = (char *) alloca (length + 1);
442   memset (buffer, '\0', length + 1);
443
444   match_digits (0, radix, buffer);
445   gfc_next_ascii_char ();    /* Eat delimiter.  */
446   if (post == 1)
447     gfc_next_ascii_char ();  /* Eat postfixed b, o, z, or x.  */
448
449   /* In section 5.2.5 and following C567 in the Fortran 2003 standard, we find
450      "If a data-stmt-constant is a boz-literal-constant, the corresponding
451      variable shall be of type integer.  The boz-literal-constant is treated
452      as if it were an int-literal-constant with a kind-param that specifies
453      the representation method with the largest decimal exponent range
454      supported by the processor."  */
455
456   kind = gfc_max_integer_kind;
457   e = gfc_convert_integer (buffer, kind, radix, &gfc_current_locus);
458
459   /* Mark as boz variable.  */
460   e->is_boz = 1;
461
462   if (gfc_range_check (e) != ARITH_OK)
463     {
464       gfc_error ("Integer too big for integer kind %i at %C", kind);
465       gfc_free_expr (e);
466       return MATCH_ERROR;
467     }
468
469   if (!gfc_in_match_data ()
470       && (gfc_notify_std (GFC_STD_F2003, "BOZ used outside a DATA "
471                           "statement at %C")
472           == FAILURE))
473       return MATCH_ERROR;
474
475   *result = e;
476   return MATCH_YES;
477
478 backup:
479   gfc_current_locus = start_loc;
480   return MATCH_NO;
481 }
482
483
484 /* Match a real constant of some sort.  Allow a signed constant if signflag
485    is nonzero.  */
486
487 static match
488 match_real_constant (gfc_expr **result, int signflag)
489 {
490   int kind, count, seen_dp, seen_digits, is_iso_c;
491   locus old_loc, temp_loc;
492   char *p, *buffer, c, exp_char;
493   gfc_expr *e;
494   bool negate;
495
496   old_loc = gfc_current_locus;
497   gfc_gobble_whitespace ();
498
499   e = NULL;
500
501   count = 0;
502   seen_dp = 0;
503   seen_digits = 0;
504   exp_char = ' ';
505   negate = FALSE;
506
507   c = gfc_next_ascii_char ();
508   if (signflag && (c == '+' || c == '-'))
509     {
510       if (c == '-')
511         negate = TRUE;
512
513       gfc_gobble_whitespace ();
514       c = gfc_next_ascii_char ();
515     }
516
517   /* Scan significand.  */
518   for (;; c = gfc_next_ascii_char (), count++)
519     {
520       if (c == '.')
521         {
522           if (seen_dp)
523             goto done;
524
525           /* Check to see if "." goes with a following operator like 
526              ".eq.".  */
527           temp_loc = gfc_current_locus;
528           c = gfc_next_ascii_char ();
529
530           if (c == 'e' || c == 'd' || c == 'q')
531             {
532               c = gfc_next_ascii_char ();
533               if (c == '.')
534                 goto done;      /* Operator named .e. or .d.  */
535             }
536
537           if (ISALPHA (c))
538             goto done;          /* Distinguish 1.e9 from 1.eq.2 */
539
540           gfc_current_locus = temp_loc;
541           seen_dp = 1;
542           continue;
543         }
544
545       if (ISDIGIT (c))
546         {
547           seen_digits = 1;
548           continue;
549         }
550
551       break;
552     }
553
554   if (!seen_digits || (c != 'e' && c != 'd' && c != 'q'))
555     goto done;
556   exp_char = c;
557
558
559   if (c == 'q')
560     {
561       if (gfc_notify_std (GFC_STD_GNU, "exponent-letter 'q' in "
562                          "real-literal-constant at %C") == FAILURE)
563         return MATCH_ERROR;
564       else if (gfc_option.warn_real_q_constant)
565         gfc_warning("Extension: exponent-letter 'q' in real-literal-constant "
566                     "at %C");
567     }
568
569   /* Scan exponent.  */
570   c = gfc_next_ascii_char ();
571   count++;
572
573   if (c == '+' || c == '-')
574     {                           /* optional sign */
575       c = gfc_next_ascii_char ();
576       count++;
577     }
578
579   if (!ISDIGIT (c))
580     {
581       gfc_error ("Missing exponent in real number at %C");
582       return MATCH_ERROR;
583     }
584
585   while (ISDIGIT (c))
586     {
587       c = gfc_next_ascii_char ();
588       count++;
589     }
590
591 done:
592   /* Check that we have a numeric constant.  */
593   if (!seen_digits || (!seen_dp && exp_char == ' '))
594     {
595       gfc_current_locus = old_loc;
596       return MATCH_NO;
597     }
598
599   /* Convert the number.  */
600   gfc_current_locus = old_loc;
601   gfc_gobble_whitespace ();
602
603   buffer = (char *) alloca (count + 1);
604   memset (buffer, '\0', count + 1);
605
606   p = buffer;
607   c = gfc_next_ascii_char ();
608   if (c == '+' || c == '-')
609     {
610       gfc_gobble_whitespace ();
611       c = gfc_next_ascii_char ();
612     }
613
614   /* Hack for mpfr_set_str().  */
615   for (;;)
616     {
617       if (c == 'd' || c == 'q')
618         *p = 'e';
619       else
620         *p = c;
621       p++;
622       if (--count == 0)
623         break;
624
625       c = gfc_next_ascii_char ();
626     }
627
628   kind = get_kind (&is_iso_c);
629   if (kind == -1)
630     goto cleanup;
631
632   switch (exp_char)
633     {
634     case 'd':
635       if (kind != -2)
636         {
637           gfc_error ("Real number at %C has a 'd' exponent and an explicit "
638                      "kind");
639           goto cleanup;
640         }
641       kind = gfc_default_double_kind;
642
643       if (kind == 4)
644         {
645           if (gfc_option.flag_real4_kind == 8)
646             kind = 8;
647           if (gfc_option.flag_real4_kind == 10)
648             kind = 10;
649           if (gfc_option.flag_real4_kind == 16)
650             kind = 16;
651         }
652
653       if (kind == 8)
654         {
655           if (gfc_option.flag_real8_kind == 4)
656             kind = 4;
657           if (gfc_option.flag_real8_kind == 10)
658             kind = 10;
659           if (gfc_option.flag_real8_kind == 16)
660             kind = 16;
661         }
662       break;
663
664     case 'q':
665       if (kind != -2)
666         {
667           gfc_error ("Real number at %C has a 'q' exponent and an explicit "
668                      "kind");
669           goto cleanup;
670         }
671
672       /* The maximum possible real kind type parameter is 16.  First, try
673          that for the kind, then fallback to trying kind=10 (Intel 80 bit)
674          extended precision.  If neither value works, just given up.  */
675       kind = 16;
676       if (gfc_validate_kind (BT_REAL, kind, true) < 0)
677         {
678           kind = 10;
679           if (gfc_validate_kind (BT_REAL, kind, true) < 0)
680             {
681               gfc_error ("Invalid exponent-letter 'q' in "
682                          "real-literal-constant at %C");
683               goto cleanup;
684             }
685         }
686       break;
687
688     default:
689       if (kind == -2)
690         kind = gfc_default_real_kind;
691
692       if (kind == 4)
693         {
694           if (gfc_option.flag_real4_kind == 8)
695             kind = 8;
696           if (gfc_option.flag_real4_kind == 10)
697             kind = 10;
698           if (gfc_option.flag_real4_kind == 16)
699             kind = 16;
700         }
701
702       if (kind == 8)
703         {
704           if (gfc_option.flag_real8_kind == 4)
705             kind = 4;
706           if (gfc_option.flag_real8_kind == 10)
707             kind = 10;
708           if (gfc_option.flag_real8_kind == 16)
709             kind = 16;
710         }
711
712       if (gfc_validate_kind (BT_REAL, kind, true) < 0)
713         {
714           gfc_error ("Invalid real kind %d at %C", kind);
715           goto cleanup;
716         }
717     }
718
719   e = gfc_convert_real (buffer, kind, &gfc_current_locus);
720   if (negate)
721     mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
722   e->ts.is_c_interop = is_iso_c;
723
724   switch (gfc_range_check (e))
725     {
726     case ARITH_OK:
727       break;
728     case ARITH_OVERFLOW:
729       gfc_error ("Real constant overflows its kind at %C");
730       goto cleanup;
731
732     case ARITH_UNDERFLOW:
733       if (gfc_option.warn_underflow)
734         gfc_warning ("Real constant underflows its kind at %C");
735       mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
736       break;
737
738     default:
739       gfc_internal_error ("gfc_range_check() returned bad value");
740     }
741
742   *result = e;
743   return MATCH_YES;
744
745 cleanup:
746   gfc_free_expr (e);
747   return MATCH_ERROR;
748 }
749
750
751 /* Match a substring reference.  */
752
753 static match
754 match_substring (gfc_charlen *cl, int init, gfc_ref **result)
755 {
756   gfc_expr *start, *end;
757   locus old_loc;
758   gfc_ref *ref;
759   match m;
760
761   start = NULL;
762   end = NULL;
763
764   old_loc = gfc_current_locus;
765
766   m = gfc_match_char ('(');
767   if (m != MATCH_YES)
768     return MATCH_NO;
769
770   if (gfc_match_char (':') != MATCH_YES)
771     {
772       if (init)
773         m = gfc_match_init_expr (&start);
774       else
775         m = gfc_match_expr (&start);
776
777       if (m != MATCH_YES)
778         {
779           m = MATCH_NO;
780           goto cleanup;
781         }
782
783       m = gfc_match_char (':');
784       if (m != MATCH_YES)
785         goto cleanup;
786     }
787
788   if (gfc_match_char (')') != MATCH_YES)
789     {
790       if (init)
791         m = gfc_match_init_expr (&end);
792       else
793         m = gfc_match_expr (&end);
794
795       if (m == MATCH_NO)
796         goto syntax;
797       if (m == MATCH_ERROR)
798         goto cleanup;
799
800       m = gfc_match_char (')');
801       if (m == MATCH_NO)
802         goto syntax;
803     }
804
805   /* Optimize away the (:) reference.  */
806   if (start == NULL && end == NULL)
807     ref = NULL;
808   else
809     {
810       ref = gfc_get_ref ();
811
812       ref->type = REF_SUBSTRING;
813       if (start == NULL)
814         start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
815       ref->u.ss.start = start;
816       if (end == NULL && cl)
817         end = gfc_copy_expr (cl->length);
818       ref->u.ss.end = end;
819       ref->u.ss.length = cl;
820     }
821
822   *result = ref;
823   return MATCH_YES;
824
825 syntax:
826   gfc_error ("Syntax error in SUBSTRING specification at %C");
827   m = MATCH_ERROR;
828
829 cleanup:
830   gfc_free_expr (start);
831   gfc_free_expr (end);
832
833   gfc_current_locus = old_loc;
834   return m;
835 }
836
837
838 /* Reads the next character of a string constant, taking care to
839    return doubled delimiters on the input as a single instance of
840    the delimiter.
841
842    Special return values for "ret" argument are:
843      -1   End of the string, as determined by the delimiter
844      -2   Unterminated string detected
845
846    Backslash codes are also expanded at this time.  */
847
848 static gfc_char_t
849 next_string_char (gfc_char_t delimiter, int *ret)
850 {
851   locus old_locus;
852   gfc_char_t c;
853
854   c = gfc_next_char_literal (INSTRING_WARN);
855   *ret = 0;
856
857   if (c == '\n')
858     {
859       *ret = -2;
860       return 0;
861     }
862
863   if (gfc_option.flag_backslash && c == '\\')
864     {
865       old_locus = gfc_current_locus;
866
867       if (gfc_match_special_char (&c) == MATCH_NO)
868         gfc_current_locus = old_locus;
869
870       if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
871         gfc_warning ("Extension: backslash character at %C");
872     }
873
874   if (c != delimiter)
875     return c;
876
877   old_locus = gfc_current_locus;
878   c = gfc_next_char_literal (NONSTRING);
879
880   if (c == delimiter)
881     return c;
882   gfc_current_locus = old_locus;
883
884   *ret = -1;
885   return 0;
886 }
887
888
889 /* Special case of gfc_match_name() that matches a parameter kind name
890    before a string constant.  This takes case of the weird but legal
891    case of:
892
893      kind_____'string'
894
895    where kind____ is a parameter. gfc_match_name() will happily slurp
896    up all the underscores, which leads to problems.  If we return
897    MATCH_YES, the parse pointer points to the final underscore, which
898    is not part of the name.  We never return MATCH_ERROR-- errors in
899    the name will be detected later.  */
900
901 static match
902 match_charkind_name (char *name)
903 {
904   locus old_loc;
905   char c, peek;
906   int len;
907
908   gfc_gobble_whitespace ();
909   c = gfc_next_ascii_char ();
910   if (!ISALPHA (c))
911     return MATCH_NO;
912
913   *name++ = c;
914   len = 1;
915
916   for (;;)
917     {
918       old_loc = gfc_current_locus;
919       c = gfc_next_ascii_char ();
920
921       if (c == '_')
922         {
923           peek = gfc_peek_ascii_char ();
924
925           if (peek == '\'' || peek == '\"')
926             {
927               gfc_current_locus = old_loc;
928               *name = '\0';
929               return MATCH_YES;
930             }
931         }
932
933       if (!ISALNUM (c)
934           && c != '_'
935           && (c != '$' || !gfc_option.flag_dollar_ok))
936         break;
937
938       *name++ = c;
939       if (++len > GFC_MAX_SYMBOL_LEN)
940         break;
941     }
942
943   return MATCH_NO;
944 }
945
946
947 /* See if the current input matches a character constant.  Lots of
948    contortions have to be done to match the kind parameter which comes
949    before the actual string.  The main consideration is that we don't
950    want to error out too quickly.  For example, we don't actually do
951    any validation of the kinds until we have actually seen a legal
952    delimiter.  Using match_kind_param() generates errors too quickly.  */
953
954 static match
955 match_string_constant (gfc_expr **result)
956 {
957   char name[GFC_MAX_SYMBOL_LEN + 1], peek;
958   int i, kind, length, warn_ampersand, ret;
959   locus old_locus, start_locus;
960   gfc_symbol *sym;
961   gfc_expr *e;
962   const char *q;
963   match m;
964   gfc_char_t c, delimiter, *p;
965
966   old_locus = gfc_current_locus;
967
968   gfc_gobble_whitespace ();
969
970   c = gfc_next_char ();
971   if (c == '\'' || c == '"')
972     {
973       kind = gfc_default_character_kind;
974       start_locus = gfc_current_locus;
975       goto got_delim;
976     }
977
978   if (gfc_wide_is_digit (c))
979     {
980       kind = 0;
981
982       while (gfc_wide_is_digit (c))
983         {
984           kind = kind * 10 + c - '0';
985           if (kind > 9999999)
986             goto no_match;
987           c = gfc_next_char ();
988         }
989
990     }
991   else
992     {
993       gfc_current_locus = old_locus;
994
995       m = match_charkind_name (name);
996       if (m != MATCH_YES)
997         goto no_match;
998
999       if (gfc_find_symbol (name, NULL, 1, &sym)
1000           || sym == NULL
1001           || sym->attr.flavor != FL_PARAMETER)
1002         goto no_match;
1003
1004       kind = -1;
1005       c = gfc_next_char ();
1006     }
1007
1008   if (c == ' ')
1009     {
1010       gfc_gobble_whitespace ();
1011       c = gfc_next_char ();
1012     }
1013
1014   if (c != '_')
1015     goto no_match;
1016
1017   gfc_gobble_whitespace ();
1018
1019   c = gfc_next_char ();
1020   if (c != '\'' && c != '"')
1021     goto no_match;
1022
1023   start_locus = gfc_current_locus;
1024
1025   if (kind == -1)
1026     {
1027       q = gfc_extract_int (sym->value, &kind);
1028       if (q != NULL)
1029         {
1030           gfc_error (q);
1031           return MATCH_ERROR;
1032         }
1033       gfc_set_sym_referenced (sym);
1034     }
1035
1036   if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1037     {
1038       gfc_error ("Invalid kind %d for CHARACTER constant at %C", kind);
1039       return MATCH_ERROR;
1040     }
1041
1042 got_delim:
1043   /* Scan the string into a block of memory by first figuring out how
1044      long it is, allocating the structure, then re-reading it.  This
1045      isn't particularly efficient, but string constants aren't that
1046      common in most code.  TODO: Use obstacks?  */
1047
1048   delimiter = c;
1049   length = 0;
1050
1051   for (;;)
1052     {
1053       c = next_string_char (delimiter, &ret);
1054       if (ret == -1)
1055         break;
1056       if (ret == -2)
1057         {
1058           gfc_current_locus = start_locus;
1059           gfc_error ("Unterminated character constant beginning at %C");
1060           return MATCH_ERROR;
1061         }
1062
1063       length++;
1064     }
1065
1066   /* Peek at the next character to see if it is a b, o, z, or x for the
1067      postfixed BOZ literal constants.  */
1068   peek = gfc_peek_ascii_char ();
1069   if (peek == 'b' || peek == 'o' || peek =='z' || peek == 'x')
1070     goto no_match;
1071
1072   e = gfc_get_character_expr (kind, &start_locus, NULL, length);
1073
1074   gfc_current_locus = start_locus;
1075
1076   /* We disable the warning for the following loop as the warning has already
1077      been printed in the loop above.  */
1078   warn_ampersand = gfc_option.warn_ampersand;
1079   gfc_option.warn_ampersand = 0;
1080
1081   p = e->value.character.string;
1082   for (i = 0; i < length; i++)
1083     {
1084       c = next_string_char (delimiter, &ret);
1085
1086       if (!gfc_check_character_range (c, kind))
1087         {
1088           gfc_free_expr (e);
1089           gfc_error ("Character '%s' in string at %C is not representable "
1090                      "in character kind %d", gfc_print_wide_char (c), kind);
1091           return MATCH_ERROR;
1092         }
1093
1094       *p++ = c;
1095     }
1096
1097   *p = '\0';    /* TODO: C-style string is for development/debug purposes.  */
1098   gfc_option.warn_ampersand = warn_ampersand;
1099
1100   next_string_char (delimiter, &ret);
1101   if (ret != -1)
1102     gfc_internal_error ("match_string_constant(): Delimiter not found");
1103
1104   if (match_substring (NULL, 0, &e->ref) != MATCH_NO)
1105     e->expr_type = EXPR_SUBSTRING;
1106
1107   *result = e;
1108
1109   return MATCH_YES;
1110
1111 no_match:
1112   gfc_current_locus = old_locus;
1113   return MATCH_NO;
1114 }
1115
1116
1117 /* Match a .true. or .false.  Returns 1 if a .true. was found,
1118    0 if a .false. was found, and -1 otherwise.  */
1119 static int
1120 match_logical_constant_string (void)
1121 {
1122   locus orig_loc = gfc_current_locus;
1123
1124   gfc_gobble_whitespace ();
1125   if (gfc_next_ascii_char () == '.')
1126     {
1127       char ch = gfc_next_ascii_char ();
1128       if (ch == 'f')
1129         {
1130           if (gfc_next_ascii_char () == 'a'
1131               && gfc_next_ascii_char () == 'l'
1132               && gfc_next_ascii_char () == 's'
1133               && gfc_next_ascii_char () == 'e'
1134               && gfc_next_ascii_char () == '.')
1135             /* Matched ".false.".  */
1136             return 0;
1137         }
1138       else if (ch == 't')
1139         {
1140           if (gfc_next_ascii_char () == 'r'
1141               && gfc_next_ascii_char () == 'u'
1142               && gfc_next_ascii_char () == 'e'
1143               && gfc_next_ascii_char () == '.')
1144             /* Matched ".true.".  */
1145             return 1;
1146         }
1147     }
1148   gfc_current_locus = orig_loc;
1149   return -1;
1150 }
1151
1152 /* Match a .true. or .false.  */
1153
1154 static match
1155 match_logical_constant (gfc_expr **result)
1156 {
1157   gfc_expr *e;
1158   int i, kind, is_iso_c;
1159
1160   i = match_logical_constant_string ();
1161   if (i == -1)
1162     return MATCH_NO;
1163
1164   kind = get_kind (&is_iso_c);
1165   if (kind == -1)
1166     return MATCH_ERROR;
1167   if (kind == -2)
1168     kind = gfc_default_logical_kind;
1169
1170   if (gfc_validate_kind (BT_LOGICAL, kind, true) < 0)
1171     {
1172       gfc_error ("Bad kind for logical constant at %C");
1173       return MATCH_ERROR;
1174     }
1175
1176   e = gfc_get_logical_expr (kind, &gfc_current_locus, i);
1177   e->ts.is_c_interop = is_iso_c;
1178
1179   *result = e;
1180   return MATCH_YES;
1181 }
1182
1183
1184 /* Match a real or imaginary part of a complex constant that is a
1185    symbolic constant.  */
1186
1187 static match
1188 match_sym_complex_part (gfc_expr **result)
1189 {
1190   char name[GFC_MAX_SYMBOL_LEN + 1];
1191   gfc_symbol *sym;
1192   gfc_expr *e;
1193   match m;
1194
1195   m = gfc_match_name (name);
1196   if (m != MATCH_YES)
1197     return m;
1198
1199   if (gfc_find_symbol (name, NULL, 1, &sym) || sym == NULL)
1200     return MATCH_NO;
1201
1202   if (sym->attr.flavor != FL_PARAMETER)
1203     {
1204       gfc_error ("Expected PARAMETER symbol in complex constant at %C");
1205       return MATCH_ERROR;
1206     }
1207
1208   if (!gfc_numeric_ts (&sym->value->ts))
1209     {
1210       gfc_error ("Numeric PARAMETER required in complex constant at %C");
1211       return MATCH_ERROR;
1212     }
1213
1214   if (sym->value->rank != 0)
1215     {
1216       gfc_error ("Scalar PARAMETER required in complex constant at %C");
1217       return MATCH_ERROR;
1218     }
1219
1220   if (gfc_notify_std (GFC_STD_F2003, "PARAMETER symbol in "
1221                       "complex constant at %C") == FAILURE)
1222     return MATCH_ERROR;
1223
1224   switch (sym->value->ts.type)
1225     {
1226     case BT_REAL:
1227       e = gfc_copy_expr (sym->value);
1228       break;
1229
1230     case BT_COMPLEX:
1231       e = gfc_complex2real (sym->value, sym->value->ts.kind);
1232       if (e == NULL)
1233         goto error;
1234       break;
1235
1236     case BT_INTEGER:
1237       e = gfc_int2real (sym->value, gfc_default_real_kind);
1238       if (e == NULL)
1239         goto error;
1240       break;
1241
1242     default:
1243       gfc_internal_error ("gfc_match_sym_complex_part(): Bad type");
1244     }
1245
1246   *result = e;          /* e is a scalar, real, constant expression.  */
1247   return MATCH_YES;
1248
1249 error:
1250   gfc_error ("Error converting PARAMETER constant in complex constant at %C");
1251   return MATCH_ERROR;
1252 }
1253
1254
1255 /* Match a real or imaginary part of a complex number.  */
1256
1257 static match
1258 match_complex_part (gfc_expr **result)
1259 {
1260   match m;
1261
1262   m = match_sym_complex_part (result);
1263   if (m != MATCH_NO)
1264     return m;
1265
1266   m = match_real_constant (result, 1);
1267   if (m != MATCH_NO)
1268     return m;
1269
1270   return match_integer_constant (result, 1);
1271 }
1272
1273
1274 /* Try to match a complex constant.  */
1275
1276 static match
1277 match_complex_constant (gfc_expr **result)
1278 {
1279   gfc_expr *e, *real, *imag;
1280   gfc_error_buf old_error;
1281   gfc_typespec target;
1282   locus old_loc;
1283   int kind;
1284   match m;
1285
1286   old_loc = gfc_current_locus;
1287   real = imag = e = NULL;
1288
1289   m = gfc_match_char ('(');
1290   if (m != MATCH_YES)
1291     return m;
1292
1293   gfc_push_error (&old_error);
1294
1295   m = match_complex_part (&real);
1296   if (m == MATCH_NO)
1297     {
1298       gfc_free_error (&old_error);
1299       goto cleanup;
1300     }
1301
1302   if (gfc_match_char (',') == MATCH_NO)
1303     {
1304       gfc_pop_error (&old_error);
1305       m = MATCH_NO;
1306       goto cleanup;
1307     }
1308
1309   /* If m is error, then something was wrong with the real part and we
1310      assume we have a complex constant because we've seen the ','.  An
1311      ambiguous case here is the start of an iterator list of some
1312      sort. These sort of lists are matched prior to coming here.  */
1313
1314   if (m == MATCH_ERROR)
1315     {
1316       gfc_free_error (&old_error);
1317       goto cleanup;
1318     }
1319   gfc_pop_error (&old_error);
1320
1321   m = match_complex_part (&imag);
1322   if (m == MATCH_NO)
1323     goto syntax;
1324   if (m == MATCH_ERROR)
1325     goto cleanup;
1326
1327   m = gfc_match_char (')');
1328   if (m == MATCH_NO)
1329     {
1330       /* Give the matcher for implied do-loops a chance to run.  This
1331          yields a much saner error message for (/ (i, 4=i, 6) /).  */
1332       if (gfc_peek_ascii_char () == '=')
1333         {
1334           m = MATCH_ERROR;
1335           goto cleanup;
1336         }
1337       else
1338     goto syntax;
1339     }
1340
1341   if (m == MATCH_ERROR)
1342     goto cleanup;
1343
1344   /* Decide on the kind of this complex number.  */
1345   if (real->ts.type == BT_REAL)
1346     {
1347       if (imag->ts.type == BT_REAL)
1348         kind = gfc_kind_max (real, imag);
1349       else
1350         kind = real->ts.kind;
1351     }
1352   else
1353     {
1354       if (imag->ts.type == BT_REAL)
1355         kind = imag->ts.kind;
1356       else
1357         kind = gfc_default_real_kind;
1358     }
1359   gfc_clear_ts (&target);
1360   target.type = BT_REAL;
1361   target.kind = kind;
1362
1363   if (real->ts.type != BT_REAL || kind != real->ts.kind)
1364     gfc_convert_type (real, &target, 2);
1365   if (imag->ts.type != BT_REAL || kind != imag->ts.kind)
1366     gfc_convert_type (imag, &target, 2);
1367
1368   e = gfc_convert_complex (real, imag, kind);
1369   e->where = gfc_current_locus;
1370
1371   gfc_free_expr (real);
1372   gfc_free_expr (imag);
1373
1374   *result = e;
1375   return MATCH_YES;
1376
1377 syntax:
1378   gfc_error ("Syntax error in COMPLEX constant at %C");
1379   m = MATCH_ERROR;
1380
1381 cleanup:
1382   gfc_free_expr (e);
1383   gfc_free_expr (real);
1384   gfc_free_expr (imag);
1385   gfc_current_locus = old_loc;
1386
1387   return m;
1388 }
1389
1390
1391 /* Match constants in any of several forms.  Returns nonzero for a
1392    match, zero for no match.  */
1393
1394 match
1395 gfc_match_literal_constant (gfc_expr **result, int signflag)
1396 {
1397   match m;
1398
1399   m = match_complex_constant (result);
1400   if (m != MATCH_NO)
1401     return m;
1402
1403   m = match_string_constant (result);
1404   if (m != MATCH_NO)
1405     return m;
1406
1407   m = match_boz_constant (result);
1408   if (m != MATCH_NO)
1409     return m;
1410
1411   m = match_real_constant (result, signflag);
1412   if (m != MATCH_NO)
1413     return m;
1414
1415   m = match_hollerith_constant (result);
1416   if (m != MATCH_NO)
1417     return m;
1418
1419   m = match_integer_constant (result, signflag);
1420   if (m != MATCH_NO)
1421     return m;
1422
1423   m = match_logical_constant (result);
1424   if (m != MATCH_NO)
1425     return m;
1426
1427   return MATCH_NO;
1428 }
1429
1430
1431 /* This checks if a symbol is the return value of an encompassing function.
1432    Function nesting can be maximally two levels deep, but we may have
1433    additional local namespaces like BLOCK etc.  */
1434
1435 bool
1436 gfc_is_function_return_value (gfc_symbol *sym, gfc_namespace *ns)
1437 {
1438   if (!sym->attr.function || (sym->result != sym))
1439     return false;
1440   while (ns)
1441     {
1442       if (ns->proc_name == sym)
1443         return true;
1444       ns = ns->parent;
1445     }
1446   return false;
1447 }
1448
1449
1450 /* Match a single actual argument value.  An actual argument is
1451    usually an expression, but can also be a procedure name.  If the
1452    argument is a single name, it is not always possible to tell
1453    whether the name is a dummy procedure or not.  We treat these cases
1454    by creating an argument that looks like a dummy procedure and
1455    fixing things later during resolution.  */
1456
1457 static match
1458 match_actual_arg (gfc_expr **result)
1459 {
1460   char name[GFC_MAX_SYMBOL_LEN + 1];
1461   gfc_symtree *symtree;
1462   locus where, w;
1463   gfc_expr *e;
1464   char c;
1465
1466   gfc_gobble_whitespace ();
1467   where = gfc_current_locus;
1468
1469   switch (gfc_match_name (name))
1470     {
1471     case MATCH_ERROR:
1472       return MATCH_ERROR;
1473
1474     case MATCH_NO:
1475       break;
1476
1477     case MATCH_YES:
1478       w = gfc_current_locus;
1479       gfc_gobble_whitespace ();
1480       c = gfc_next_ascii_char ();
1481       gfc_current_locus = w;
1482
1483       if (c != ',' && c != ')')
1484         break;
1485
1486       if (gfc_find_sym_tree (name, NULL, 1, &symtree))
1487         break;
1488       /* Handle error elsewhere.  */
1489
1490       /* Eliminate a couple of common cases where we know we don't
1491          have a function argument.  */
1492       if (symtree == NULL)
1493         {
1494           gfc_get_sym_tree (name, NULL, &symtree, false);
1495           gfc_set_sym_referenced (symtree->n.sym);
1496         }
1497       else
1498         {
1499           gfc_symbol *sym;
1500
1501           sym = symtree->n.sym;
1502           gfc_set_sym_referenced (sym);
1503           if (sym->attr.flavor != FL_PROCEDURE
1504               && sym->attr.flavor != FL_UNKNOWN)
1505             break;
1506
1507           if (sym->attr.in_common && !sym->attr.proc_pointer)
1508             {
1509               if (gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name,
1510                                   &sym->declared_at) == FAILURE)
1511                 return MATCH_ERROR;
1512               break;
1513             }
1514
1515           /* If the symbol is a function with itself as the result and
1516              is being defined, then we have a variable.  */
1517           if (sym->attr.function && sym->result == sym)
1518             {
1519               if (gfc_is_function_return_value (sym, gfc_current_ns))
1520                 break;
1521
1522               if (sym->attr.entry
1523                   && (sym->ns == gfc_current_ns
1524                       || sym->ns == gfc_current_ns->parent))
1525                 {
1526                   gfc_entry_list *el = NULL;
1527
1528                   for (el = sym->ns->entries; el; el = el->next)
1529                     if (sym == el->sym)
1530                       break;
1531
1532                   if (el)
1533                     break;
1534                 }
1535             }
1536         }
1537
1538       e = gfc_get_expr ();      /* Leave it unknown for now */
1539       e->symtree = symtree;
1540       e->expr_type = EXPR_VARIABLE;
1541       e->ts.type = BT_PROCEDURE;
1542       e->where = where;
1543
1544       *result = e;
1545       return MATCH_YES;
1546     }
1547
1548   gfc_current_locus = where;
1549   return gfc_match_expr (result);
1550 }
1551
1552
1553 /* Match a keyword argument.  */
1554
1555 static match
1556 match_keyword_arg (gfc_actual_arglist *actual, gfc_actual_arglist *base)
1557 {
1558   char name[GFC_MAX_SYMBOL_LEN + 1];
1559   gfc_actual_arglist *a;
1560   locus name_locus;
1561   match m;
1562
1563   name_locus = gfc_current_locus;
1564   m = gfc_match_name (name);
1565
1566   if (m != MATCH_YES)
1567     goto cleanup;
1568   if (gfc_match_char ('=') != MATCH_YES)
1569     {
1570       m = MATCH_NO;
1571       goto cleanup;
1572     }
1573
1574   m = match_actual_arg (&actual->expr);
1575   if (m != MATCH_YES)
1576     goto cleanup;
1577
1578   /* Make sure this name has not appeared yet.  */
1579
1580   if (name[0] != '\0')
1581     {
1582       for (a = base; a; a = a->next)
1583         if (a->name != NULL && strcmp (a->name, name) == 0)
1584           {
1585             gfc_error ("Keyword '%s' at %C has already appeared in the "
1586                        "current argument list", name);
1587             return MATCH_ERROR;
1588           }
1589     }
1590
1591   actual->name = gfc_get_string (name);
1592   return MATCH_YES;
1593
1594 cleanup:
1595   gfc_current_locus = name_locus;
1596   return m;
1597 }
1598
1599
1600 /* Match an argument list function, such as %VAL.  */
1601
1602 static match
1603 match_arg_list_function (gfc_actual_arglist *result)
1604 {
1605   char name[GFC_MAX_SYMBOL_LEN + 1];
1606   locus old_locus;
1607   match m;
1608
1609   old_locus = gfc_current_locus;
1610
1611   if (gfc_match_char ('%') != MATCH_YES)
1612     {
1613       m = MATCH_NO;
1614       goto cleanup;
1615     }
1616
1617   m = gfc_match ("%n (", name);
1618   if (m != MATCH_YES)
1619     goto cleanup;
1620
1621   if (name[0] != '\0')
1622     {
1623       switch (name[0])
1624         {
1625         case 'l':
1626           if (strncmp (name, "loc", 3) == 0)
1627             {
1628               result->name = "%LOC";
1629               break;
1630             }
1631         case 'r':
1632           if (strncmp (name, "ref", 3) == 0)
1633             {
1634               result->name = "%REF";
1635               break;
1636             }
1637         case 'v':
1638           if (strncmp (name, "val", 3) == 0)
1639             {
1640               result->name = "%VAL";
1641               break;
1642             }
1643         default:
1644           m = MATCH_ERROR;
1645           goto cleanup;
1646         }
1647     }
1648
1649   if (gfc_notify_std (GFC_STD_GNU, "argument list "
1650                       "function at %C") == FAILURE)
1651     {
1652       m = MATCH_ERROR;
1653       goto cleanup;
1654     }
1655
1656   m = match_actual_arg (&result->expr);
1657   if (m != MATCH_YES)
1658     goto cleanup;
1659
1660   if (gfc_match_char (')') != MATCH_YES)
1661     {
1662       m = MATCH_NO;
1663       goto cleanup;
1664     }
1665
1666   return MATCH_YES;
1667
1668 cleanup:
1669   gfc_current_locus = old_locus;
1670   return m;
1671 }
1672
1673
1674 /* Matches an actual argument list of a function or subroutine, from
1675    the opening parenthesis to the closing parenthesis.  The argument
1676    list is assumed to allow keyword arguments because we don't know if
1677    the symbol associated with the procedure has an implicit interface
1678    or not.  We make sure keywords are unique. If sub_flag is set,
1679    we're matching the argument list of a subroutine.  */
1680
1681 match
1682 gfc_match_actual_arglist (int sub_flag, gfc_actual_arglist **argp)
1683 {
1684   gfc_actual_arglist *head, *tail;
1685   int seen_keyword;
1686   gfc_st_label *label;
1687   locus old_loc;
1688   match m;
1689
1690   *argp = tail = NULL;
1691   old_loc = gfc_current_locus;
1692
1693   seen_keyword = 0;
1694
1695   if (gfc_match_char ('(') == MATCH_NO)
1696     return (sub_flag) ? MATCH_YES : MATCH_NO;
1697
1698   if (gfc_match_char (')') == MATCH_YES)
1699     return MATCH_YES;
1700   head = NULL;
1701
1702   matching_actual_arglist++;
1703
1704   for (;;)
1705     {
1706       if (head == NULL)
1707         head = tail = gfc_get_actual_arglist ();
1708       else
1709         {
1710           tail->next = gfc_get_actual_arglist ();
1711           tail = tail->next;
1712         }
1713
1714       if (sub_flag && gfc_match_char ('*') == MATCH_YES)
1715         {
1716           m = gfc_match_st_label (&label);
1717           if (m == MATCH_NO)
1718             gfc_error ("Expected alternate return label at %C");
1719           if (m != MATCH_YES)
1720             goto cleanup;
1721
1722           tail->label = label;
1723           goto next;
1724         }
1725
1726       /* After the first keyword argument is seen, the following
1727          arguments must also have keywords.  */
1728       if (seen_keyword)
1729         {
1730           m = match_keyword_arg (tail, head);
1731
1732           if (m == MATCH_ERROR)
1733             goto cleanup;
1734           if (m == MATCH_NO)
1735             {
1736               gfc_error ("Missing keyword name in actual argument list at %C");
1737               goto cleanup;
1738             }
1739
1740         }
1741       else
1742         {
1743           /* Try an argument list function, like %VAL.  */
1744           m = match_arg_list_function (tail);
1745           if (m == MATCH_ERROR)
1746             goto cleanup;
1747
1748           /* See if we have the first keyword argument.  */
1749           if (m == MATCH_NO)
1750             {
1751               m = match_keyword_arg (tail, head);
1752               if (m == MATCH_YES)
1753                 seen_keyword = 1;
1754               if (m == MATCH_ERROR)
1755                 goto cleanup;
1756             }
1757
1758           if (m == MATCH_NO)
1759             {
1760               /* Try for a non-keyword argument.  */
1761               m = match_actual_arg (&tail->expr);
1762               if (m == MATCH_ERROR)
1763                 goto cleanup;
1764               if (m == MATCH_NO)
1765                 goto syntax;
1766             }
1767         }
1768
1769
1770     next:
1771       if (gfc_match_char (')') == MATCH_YES)
1772         break;
1773       if (gfc_match_char (',') != MATCH_YES)
1774         goto syntax;
1775     }
1776
1777   *argp = head;
1778   matching_actual_arglist--;
1779   return MATCH_YES;
1780
1781 syntax:
1782   gfc_error ("Syntax error in argument list at %C");
1783
1784 cleanup:
1785   gfc_free_actual_arglist (head);
1786   gfc_current_locus = old_loc;
1787   matching_actual_arglist--;
1788   return MATCH_ERROR;
1789 }
1790
1791
1792 /* Used by gfc_match_varspec() to extend the reference list by one
1793    element.  */
1794
1795 static gfc_ref *
1796 extend_ref (gfc_expr *primary, gfc_ref *tail)
1797 {
1798   if (primary->ref == NULL)
1799     primary->ref = tail = gfc_get_ref ();
1800   else
1801     {
1802       if (tail == NULL)
1803         gfc_internal_error ("extend_ref(): Bad tail");
1804       tail->next = gfc_get_ref ();
1805       tail = tail->next;
1806     }
1807
1808   return tail;
1809 }
1810
1811
1812 /* Match any additional specifications associated with the current
1813    variable like member references or substrings.  If equiv_flag is
1814    set we only match stuff that is allowed inside an EQUIVALENCE
1815    statement.  sub_flag tells whether we expect a type-bound procedure found
1816    to be a subroutine as part of CALL or a FUNCTION. For procedure pointer
1817    components, 'ppc_arg' determines whether the PPC may be called (with an
1818    argument list), or whether it may just be referred to as a pointer.  */
1819
1820 match
1821 gfc_match_varspec (gfc_expr *primary, int equiv_flag, bool sub_flag,
1822                    bool ppc_arg)
1823 {
1824   char name[GFC_MAX_SYMBOL_LEN + 1];
1825   gfc_ref *substring, *tail;
1826   gfc_component *component;
1827   gfc_symbol *sym = primary->symtree->n.sym;
1828   match m;
1829   bool unknown;
1830
1831   tail = NULL;
1832
1833   gfc_gobble_whitespace ();
1834
1835   if (gfc_peek_ascii_char () == '[')
1836     {
1837       if ((sym->ts.type != BT_CLASS && sym->attr.dimension)
1838           || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
1839               && CLASS_DATA (sym)->attr.dimension))
1840         {
1841           gfc_error ("Array section designator, e.g. '(:)', is required "
1842                      "besides the coarray designator '[...]' at %C");
1843           return MATCH_ERROR;
1844         }
1845       if ((sym->ts.type != BT_CLASS && !sym->attr.codimension)
1846           || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
1847               && !CLASS_DATA (sym)->attr.codimension))
1848         {
1849           gfc_error ("Coarray designator at %C but '%s' is not a coarray",
1850                      sym->name);
1851           return MATCH_ERROR;
1852         }
1853     }
1854
1855   /* For associate names, we may not yet know whether they are arrays or not.
1856      Thus if we have one and parentheses follow, we have to assume that it
1857      actually is one for now.  The final decision will be made at
1858      resolution time, of course.  */
1859   if (sym->assoc && gfc_peek_ascii_char () == '(')
1860     sym->attr.dimension = 1;
1861
1862   if ((equiv_flag && gfc_peek_ascii_char () == '(')
1863       || gfc_peek_ascii_char () == '[' || sym->attr.codimension
1864       || (sym->attr.dimension && sym->ts.type != BT_CLASS
1865           && !sym->attr.proc_pointer && !gfc_is_proc_ptr_comp (primary)
1866           && !(gfc_matching_procptr_assignment
1867                && sym->attr.flavor == FL_PROCEDURE))
1868       || (sym->ts.type == BT_CLASS && sym->attr.class_ok
1869           && (CLASS_DATA (sym)->attr.dimension
1870               || CLASS_DATA (sym)->attr.codimension)))
1871     {
1872       gfc_array_spec *as;
1873
1874       tail = extend_ref (primary, tail);
1875       tail->type = REF_ARRAY;
1876
1877       /* In EQUIVALENCE, we don't know yet whether we are seeing
1878          an array, character variable or array of character
1879          variables.  We'll leave the decision till resolve time.  */
1880
1881       if (equiv_flag)
1882         as = NULL;
1883       else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
1884         as = CLASS_DATA (sym)->as;
1885       else
1886         as = sym->as;
1887
1888       m = gfc_match_array_ref (&tail->u.ar, as, equiv_flag,
1889                                as ? as->corank : 0);
1890       if (m != MATCH_YES)
1891         return m;
1892
1893       gfc_gobble_whitespace ();
1894       if (equiv_flag && gfc_peek_ascii_char () == '(')
1895         {
1896           tail = extend_ref (primary, tail);
1897           tail->type = REF_ARRAY;
1898
1899           m = gfc_match_array_ref (&tail->u.ar, NULL, equiv_flag, 0);
1900           if (m != MATCH_YES)
1901             return m;
1902         }
1903     }
1904
1905   primary->ts = sym->ts;
1906
1907   if (equiv_flag)
1908     return MATCH_YES;
1909
1910   if (sym->ts.type == BT_UNKNOWN && gfc_peek_ascii_char () == '%'
1911       && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
1912     gfc_set_default_type (sym, 0, sym->ns);
1913
1914   if (sym->ts.type == BT_UNKNOWN && gfc_match_char ('%') == MATCH_YES)
1915     {
1916       gfc_error ("Symbol '%s' at %C has no IMPLICIT type", sym->name);
1917       return MATCH_ERROR;
1918     }
1919   else if ((sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
1920            && gfc_match_char ('%') == MATCH_YES)
1921     {
1922       gfc_error ("Unexpected '%%' for nonderived-type variable '%s' at %C",
1923                  sym->name);
1924       return MATCH_ERROR;
1925     }
1926
1927   if ((sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
1928       || gfc_match_char ('%') != MATCH_YES)
1929     goto check_substring;
1930
1931   sym = sym->ts.u.derived;
1932
1933   for (;;)
1934     {
1935       gfc_try t;
1936       gfc_symtree *tbp;
1937
1938       m = gfc_match_name (name);
1939       if (m == MATCH_NO)
1940         gfc_error ("Expected structure component name at %C");
1941       if (m != MATCH_YES)
1942         return MATCH_ERROR;
1943
1944       if (sym->f2k_derived)
1945         tbp = gfc_find_typebound_proc (sym, &t, name, false, &gfc_current_locus);
1946       else
1947         tbp = NULL;
1948
1949       if (tbp)
1950         {
1951           gfc_symbol* tbp_sym;
1952
1953           if (t == FAILURE)
1954             return MATCH_ERROR;
1955
1956           gcc_assert (!tail || !tail->next);
1957           gcc_assert (primary->expr_type == EXPR_VARIABLE
1958                       || (primary->expr_type == EXPR_STRUCTURE
1959                           && primary->symtree && primary->symtree->n.sym
1960                           && primary->symtree->n.sym->attr.flavor));
1961
1962           if (tbp->n.tb->is_generic)
1963             tbp_sym = NULL;
1964           else
1965             tbp_sym = tbp->n.tb->u.specific->n.sym;
1966
1967           primary->expr_type = EXPR_COMPCALL;
1968           primary->value.compcall.tbp = tbp->n.tb;
1969           primary->value.compcall.name = tbp->name;
1970           primary->value.compcall.ignore_pass = 0;
1971           primary->value.compcall.assign = 0;
1972           primary->value.compcall.base_object = NULL;
1973           gcc_assert (primary->symtree->n.sym->attr.referenced);
1974           if (tbp_sym)
1975             primary->ts = tbp_sym->ts;
1976           else
1977             gfc_clear_ts (&primary->ts);
1978
1979           m = gfc_match_actual_arglist (tbp->n.tb->subroutine,
1980                                         &primary->value.compcall.actual);
1981           if (m == MATCH_ERROR)
1982             return MATCH_ERROR;
1983           if (m == MATCH_NO)
1984             {
1985               if (sub_flag)
1986                 primary->value.compcall.actual = NULL;
1987               else
1988                 {
1989                   gfc_error ("Expected argument list at %C");
1990                   return MATCH_ERROR;
1991                 }
1992             }
1993
1994           break;
1995         }
1996
1997       component = gfc_find_component (sym, name, false, false);
1998       if (component == NULL)
1999         return MATCH_ERROR;
2000
2001       tail = extend_ref (primary, tail);
2002       tail->type = REF_COMPONENT;
2003
2004       tail->u.c.component = component;
2005       tail->u.c.sym = sym;
2006
2007       primary->ts = component->ts;
2008
2009       if (component->attr.proc_pointer && ppc_arg)
2010         {
2011           /* Procedure pointer component call: Look for argument list.  */
2012           m = gfc_match_actual_arglist (sub_flag,
2013                                         &primary->value.compcall.actual);
2014           if (m == MATCH_ERROR)
2015             return MATCH_ERROR;
2016
2017           if (m == MATCH_NO && !gfc_matching_ptr_assignment
2018               && !gfc_matching_procptr_assignment && !matching_actual_arglist)
2019             {
2020               gfc_error ("Procedure pointer component '%s' requires an "
2021                          "argument list at %C", component->name);
2022               return MATCH_ERROR;
2023             }
2024
2025           if (m == MATCH_YES)
2026             primary->expr_type = EXPR_PPC;
2027
2028           break;
2029         }
2030
2031       if (component->as != NULL && !component->attr.proc_pointer)
2032         {
2033           tail = extend_ref (primary, tail);
2034           tail->type = REF_ARRAY;
2035
2036           m = gfc_match_array_ref (&tail->u.ar, component->as, equiv_flag,
2037                           component->as->corank);
2038           if (m != MATCH_YES)
2039             return m;
2040         }
2041       else if (component->ts.type == BT_CLASS
2042                && CLASS_DATA (component)->as != NULL
2043                && !component->attr.proc_pointer)
2044         {
2045           tail = extend_ref (primary, tail);
2046           tail->type = REF_ARRAY;
2047
2048           m = gfc_match_array_ref (&tail->u.ar, CLASS_DATA (component)->as,
2049                                    equiv_flag,
2050                                    CLASS_DATA (component)->as->corank);
2051           if (m != MATCH_YES)
2052             return m;
2053         }
2054
2055       if ((component->ts.type != BT_DERIVED && component->ts.type != BT_CLASS)
2056           || gfc_match_char ('%') != MATCH_YES)
2057         break;
2058
2059       sym = component->ts.u.derived;
2060     }
2061
2062 check_substring:
2063   unknown = false;
2064   if (primary->ts.type == BT_UNKNOWN && sym->attr.flavor != FL_DERIVED)
2065     {
2066       if (gfc_get_default_type (sym->name, sym->ns)->type == BT_CHARACTER)
2067        {
2068          gfc_set_default_type (sym, 0, sym->ns);
2069          primary->ts = sym->ts;
2070          unknown = true;
2071        }
2072     }
2073
2074   if (primary->ts.type == BT_CHARACTER)
2075     {
2076       switch (match_substring (primary->ts.u.cl, equiv_flag, &substring))
2077         {
2078         case MATCH_YES:
2079           if (tail == NULL)
2080             primary->ref = substring;
2081           else
2082             tail->next = substring;
2083
2084           if (primary->expr_type == EXPR_CONSTANT)
2085             primary->expr_type = EXPR_SUBSTRING;
2086
2087           if (substring)
2088             primary->ts.u.cl = NULL;
2089
2090           break;
2091
2092         case MATCH_NO:
2093           if (unknown)
2094             {
2095               gfc_clear_ts (&primary->ts);
2096               gfc_clear_ts (&sym->ts);
2097             }
2098           break;
2099
2100         case MATCH_ERROR:
2101           return MATCH_ERROR;
2102         }
2103     }
2104
2105   /* F2008, C727.  */
2106   if (primary->expr_type == EXPR_PPC && gfc_is_coindexed (primary))
2107     {
2108       gfc_error ("Coindexed procedure-pointer component at %C");
2109       return MATCH_ERROR;
2110     }
2111
2112   return MATCH_YES;
2113 }
2114
2115
2116 /* Given an expression that is a variable, figure out what the
2117    ultimate variable's type and attribute is, traversing the reference
2118    structures if necessary.
2119
2120    This subroutine is trickier than it looks.  We start at the base
2121    symbol and store the attribute.  Component references load a
2122    completely new attribute.
2123
2124    A couple of rules come into play.  Subobjects of targets are always
2125    targets themselves.  If we see a component that goes through a
2126    pointer, then the expression must also be a target, since the
2127    pointer is associated with something (if it isn't core will soon be
2128    dumped).  If we see a full part or section of an array, the
2129    expression is also an array.
2130
2131    We can have at most one full array reference.  */
2132
2133 symbol_attribute
2134 gfc_variable_attr (gfc_expr *expr, gfc_typespec *ts)
2135 {
2136   int dimension, pointer, allocatable, target;
2137   symbol_attribute attr;
2138   gfc_ref *ref;
2139   gfc_symbol *sym;
2140   gfc_component *comp;
2141
2142   if (expr->expr_type != EXPR_VARIABLE && expr->expr_type != EXPR_FUNCTION)
2143     gfc_internal_error ("gfc_variable_attr(): Expression isn't a variable");
2144
2145   sym = expr->symtree->n.sym;
2146   attr = sym->attr;
2147
2148   if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
2149     {
2150       dimension = CLASS_DATA (sym)->attr.dimension;
2151       pointer = CLASS_DATA (sym)->attr.class_pointer;
2152       allocatable = CLASS_DATA (sym)->attr.allocatable;
2153     }
2154   else
2155     {
2156       dimension = attr.dimension;
2157       pointer = attr.pointer;
2158       allocatable = attr.allocatable;
2159     }
2160
2161   target = attr.target;
2162   if (pointer || attr.proc_pointer)
2163     target = 1;
2164
2165   if (ts != NULL && expr->ts.type == BT_UNKNOWN)
2166     *ts = sym->ts;
2167
2168   for (ref = expr->ref; ref; ref = ref->next)
2169     switch (ref->type)
2170       {
2171       case REF_ARRAY:
2172
2173         switch (ref->u.ar.type)
2174           {
2175           case AR_FULL:
2176             dimension = 1;
2177             break;
2178
2179           case AR_SECTION:
2180             allocatable = pointer = 0;
2181             dimension = 1;
2182             break;
2183
2184           case AR_ELEMENT:
2185             /* Handle coarrays.  */
2186             if (ref->u.ar.dimen > 0)
2187               allocatable = pointer = 0;
2188             break;
2189
2190           case AR_UNKNOWN:
2191             gfc_internal_error ("gfc_variable_attr(): Bad array reference");
2192           }
2193
2194         break;
2195
2196       case REF_COMPONENT:
2197         comp = ref->u.c.component;
2198         attr = comp->attr;
2199         if (ts != NULL)
2200           {
2201             *ts = comp->ts;
2202             /* Don't set the string length if a substring reference
2203                follows.  */
2204             if (ts->type == BT_CHARACTER
2205                 && ref->next && ref->next->type == REF_SUBSTRING)
2206                 ts->u.cl = NULL;
2207           }
2208
2209         if (comp->ts.type == BT_CLASS)
2210           {
2211             pointer = CLASS_DATA (comp)->attr.class_pointer;
2212             allocatable = CLASS_DATA (comp)->attr.allocatable;
2213           }
2214         else
2215           {
2216             pointer = comp->attr.pointer;
2217             allocatable = comp->attr.allocatable;
2218           }
2219         if (pointer || attr.proc_pointer)
2220           target = 1;
2221
2222         break;
2223
2224       case REF_SUBSTRING:
2225         allocatable = pointer = 0;
2226         break;
2227       }
2228
2229   attr.dimension = dimension;
2230   attr.pointer = pointer;
2231   attr.allocatable = allocatable;
2232   attr.target = target;
2233   attr.save = sym->attr.save;
2234
2235   return attr;
2236 }
2237
2238
2239 /* Return the attribute from a general expression.  */
2240
2241 symbol_attribute
2242 gfc_expr_attr (gfc_expr *e)
2243 {
2244   symbol_attribute attr;
2245
2246   switch (e->expr_type)
2247     {
2248     case EXPR_VARIABLE:
2249       attr = gfc_variable_attr (e, NULL);
2250       break;
2251
2252     case EXPR_FUNCTION:
2253       gfc_clear_attr (&attr);
2254
2255       if (e->value.function.esym != NULL)
2256         {
2257           gfc_symbol *sym = e->value.function.esym->result;
2258           attr = sym->attr;
2259           if (sym->ts.type == BT_CLASS)
2260             {
2261               attr.dimension = CLASS_DATA (sym)->attr.dimension;
2262               attr.pointer = CLASS_DATA (sym)->attr.class_pointer;
2263               attr.allocatable = CLASS_DATA (sym)->attr.allocatable;
2264             }
2265         }
2266       else
2267         attr = gfc_variable_attr (e, NULL);
2268
2269       /* TODO: NULL() returns pointers.  May have to take care of this
2270          here.  */
2271
2272       break;
2273
2274     default:
2275       gfc_clear_attr (&attr);
2276       break;
2277     }
2278
2279   return attr;
2280 }
2281
2282
2283 /* Match a structure constructor.  The initial symbol has already been
2284    seen.  */
2285
2286 typedef struct gfc_structure_ctor_component
2287 {
2288   char* name;
2289   gfc_expr* val;
2290   locus where;
2291   struct gfc_structure_ctor_component* next;
2292 }
2293 gfc_structure_ctor_component;
2294
2295 #define gfc_get_structure_ctor_component() XCNEW (gfc_structure_ctor_component)
2296
2297 static void
2298 gfc_free_structure_ctor_component (gfc_structure_ctor_component *comp)
2299 {
2300   free (comp->name);
2301   gfc_free_expr (comp->val);
2302   free (comp);
2303 }
2304
2305
2306 /* Translate the component list into the actual constructor by sorting it in
2307    the order required; this also checks along the way that each and every
2308    component actually has an initializer and handles default initializers
2309    for components without explicit value given.  */
2310 static gfc_try
2311 build_actual_constructor (gfc_structure_ctor_component **comp_head,
2312                           gfc_constructor_base *ctor_head, gfc_symbol *sym)
2313 {
2314   gfc_structure_ctor_component *comp_iter;
2315   gfc_component *comp;
2316
2317   for (comp = sym->components; comp; comp = comp->next)
2318     {
2319       gfc_structure_ctor_component **next_ptr;
2320       gfc_expr *value = NULL;
2321
2322       /* Try to find the initializer for the current component by name.  */
2323       next_ptr = comp_head;
2324       for (comp_iter = *comp_head; comp_iter; comp_iter = comp_iter->next)
2325         {
2326           if (!strcmp (comp_iter->name, comp->name))
2327             break;
2328           next_ptr = &comp_iter->next;
2329         }
2330
2331       /* If an extension, try building the parent derived type by building
2332          a value expression for the parent derived type and calling self.  */
2333       if (!comp_iter && comp == sym->components && sym->attr.extension)
2334         {
2335           value = gfc_get_structure_constructor_expr (comp->ts.type,
2336                                                       comp->ts.kind,
2337                                                       &gfc_current_locus);
2338           value->ts = comp->ts;
2339
2340           if (build_actual_constructor (comp_head, &value->value.constructor,
2341                                         comp->ts.u.derived) == FAILURE)
2342             {
2343               gfc_free_expr (value);
2344               return FAILURE;
2345             }
2346
2347           gfc_constructor_append_expr (ctor_head, value, NULL);
2348           continue;
2349         }
2350
2351       /* If it was not found, try the default initializer if there's any;
2352          otherwise, it's an error.  */
2353       if (!comp_iter)
2354         {
2355           if (comp->initializer)
2356             {
2357               if (gfc_notify_std (GFC_STD_F2003, "Structure"
2358                                   " constructor with missing optional arguments"
2359                                   " at %C") == FAILURE)
2360                 return FAILURE;
2361               value = gfc_copy_expr (comp->initializer);
2362             }
2363           else
2364             {
2365               gfc_error ("No initializer for component '%s' given in the"
2366                          " structure constructor at %C!", comp->name);
2367               return FAILURE;
2368             }
2369         }
2370       else
2371         value = comp_iter->val;
2372
2373       /* Add the value to the constructor chain built.  */
2374       gfc_constructor_append_expr (ctor_head, value, NULL);
2375
2376       /* Remove the entry from the component list.  We don't want the expression
2377          value to be free'd, so set it to NULL.  */
2378       if (comp_iter)
2379         {
2380           *next_ptr = comp_iter->next;
2381           comp_iter->val = NULL;
2382           gfc_free_structure_ctor_component (comp_iter);
2383         }
2384     }
2385   return SUCCESS;
2386 }
2387
2388
2389 gfc_try
2390 gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **cexpr,
2391                                       gfc_actual_arglist **arglist,
2392                                       bool parent)
2393 {
2394   gfc_actual_arglist *actual;
2395   gfc_structure_ctor_component *comp_tail, *comp_head, *comp_iter;
2396   gfc_constructor_base ctor_head = NULL;
2397   gfc_component *comp; /* Is set NULL when named component is first seen */
2398   const char* last_name = NULL;
2399   locus old_locus;
2400   gfc_expr *expr;
2401
2402   expr = parent ? *cexpr : e;
2403   old_locus = gfc_current_locus;
2404   if (parent)
2405     ; /* gfc_current_locus = *arglist->expr ? ->where;*/
2406   else
2407     gfc_current_locus = expr->where;
2408
2409   comp_tail = comp_head = NULL;
2410
2411   if (!parent && sym->attr.abstract)
2412     {
2413       gfc_error ("Can't construct ABSTRACT type '%s' at %L",
2414                  sym->name, &expr->where);
2415       goto cleanup;
2416     }
2417
2418   comp = sym->components;
2419   actual = parent ? *arglist : expr->value.function.actual;
2420   for ( ; actual; )
2421     {
2422       gfc_component *this_comp = NULL;
2423
2424       if (!comp_head)
2425         comp_tail = comp_head = gfc_get_structure_ctor_component ();
2426       else
2427         {
2428           comp_tail->next = gfc_get_structure_ctor_component ();
2429           comp_tail = comp_tail->next;
2430         }
2431       if (actual->name)
2432         {
2433           if (gfc_notify_std (GFC_STD_F2003, "Structure"
2434                               " constructor with named arguments at %C")
2435               == FAILURE)
2436             goto cleanup;
2437
2438           comp_tail->name = xstrdup (actual->name);
2439           last_name = comp_tail->name;
2440           comp = NULL;
2441         }
2442       else
2443         {
2444           /* Components without name are not allowed after the first named
2445              component initializer!  */
2446           if (!comp)
2447             {
2448               if (last_name)
2449                 gfc_error ("Component initializer without name after component"
2450                            " named %s at %L!", last_name,
2451                            actual->expr ? &actual->expr->where
2452                                         : &gfc_current_locus);
2453               else
2454                 gfc_error ("Too many components in structure constructor at "
2455                            "%L!", actual->expr ? &actual->expr->where
2456                                                : &gfc_current_locus);
2457               goto cleanup;
2458             }
2459
2460           comp_tail->name = xstrdup (comp->name);
2461         }
2462
2463       /* Find the current component in the structure definition and check
2464              its access is not private.  */
2465       if (comp)
2466         this_comp = gfc_find_component (sym, comp->name, false, false);
2467       else
2468         {
2469           this_comp = gfc_find_component (sym, (const char *)comp_tail->name,
2470                                           false, false);
2471           comp = NULL; /* Reset needed!  */
2472         }
2473
2474       /* Here we can check if a component name is given which does not
2475          correspond to any component of the defined structure.  */
2476       if (!this_comp)
2477         goto cleanup;
2478
2479       comp_tail->val = actual->expr;
2480       if (actual->expr != NULL)
2481         comp_tail->where = actual->expr->where;
2482       actual->expr = NULL;
2483
2484       /* Check if this component is already given a value.  */
2485       for (comp_iter = comp_head; comp_iter != comp_tail; 
2486            comp_iter = comp_iter->next)
2487         {
2488           gcc_assert (comp_iter);
2489           if (!strcmp (comp_iter->name, comp_tail->name))
2490             {
2491               gfc_error ("Component '%s' is initialized twice in the structure"
2492                          " constructor at %L!", comp_tail->name,
2493                          comp_tail->val ? &comp_tail->where
2494                                         : &gfc_current_locus);
2495               goto cleanup;
2496             }
2497         }
2498
2499       /* F2008, R457/C725, for PURE C1283.  */
2500       if (this_comp->attr.pointer && comp_tail->val
2501           && gfc_is_coindexed (comp_tail->val))
2502         {
2503           gfc_error ("Coindexed expression to pointer component '%s' in "
2504                      "structure constructor at %L!", comp_tail->name,
2505                      &comp_tail->where);
2506           goto cleanup;
2507         }
2508
2509           /* If not explicitly a parent constructor, gather up the components
2510              and build one.  */
2511           if (comp && comp == sym->components
2512                 && sym->attr.extension
2513                 && comp_tail->val
2514                 && (comp_tail->val->ts.type != BT_DERIVED
2515                       ||
2516                     comp_tail->val->ts.u.derived != this_comp->ts.u.derived))
2517             {
2518               gfc_try m;
2519               gfc_actual_arglist *arg_null = NULL;
2520
2521               actual->expr = comp_tail->val;
2522               comp_tail->val = NULL;
2523
2524               m = gfc_convert_to_structure_constructor (NULL,
2525                                         comp->ts.u.derived, &comp_tail->val,
2526                                         comp->ts.u.derived->attr.zero_comp
2527                                           ? &arg_null : &actual, true);
2528               if (m == FAILURE)
2529                 goto cleanup;
2530
2531               if (comp->ts.u.derived->attr.zero_comp)
2532                 {
2533                   comp = comp->next;
2534                   continue;
2535                 }
2536             }
2537
2538       if (comp)
2539         comp = comp->next;
2540       if (parent && !comp)
2541         break;
2542
2543       actual = actual->next;
2544     }
2545
2546   if (build_actual_constructor (&comp_head, &ctor_head, sym) == FAILURE)
2547     goto cleanup;
2548
2549   /* No component should be left, as this should have caused an error in the
2550      loop constructing the component-list (name that does not correspond to any
2551      component in the structure definition).  */
2552   if (comp_head && sym->attr.extension)
2553     {
2554       for (comp_iter = comp_head; comp_iter; comp_iter = comp_iter->next)
2555         {
2556           gfc_error ("component '%s' at %L has already been set by a "
2557                      "parent derived type constructor", comp_iter->name,
2558                      &comp_iter->where);
2559         }
2560       goto cleanup;
2561     }
2562   else
2563     gcc_assert (!comp_head);
2564
2565   if (parent)
2566     {
2567       expr = gfc_get_structure_constructor_expr (BT_DERIVED, 0, &gfc_current_locus);
2568       expr->ts.u.derived = sym;
2569       expr->value.constructor = ctor_head;
2570       *cexpr = expr;
2571     }
2572   else
2573     {
2574       expr->ts.u.derived = sym;
2575       expr->ts.kind = 0;
2576       expr->ts.type = BT_DERIVED;
2577       expr->value.constructor = ctor_head;
2578       expr->expr_type = EXPR_STRUCTURE;
2579     }
2580
2581   gfc_current_locus = old_locus; 
2582   if (parent)
2583     *arglist = actual;
2584   return SUCCESS;
2585
2586   cleanup:
2587   gfc_current_locus = old_locus; 
2588
2589   for (comp_iter = comp_head; comp_iter; )
2590     {
2591       gfc_structure_ctor_component *next = comp_iter->next;
2592       gfc_free_structure_ctor_component (comp_iter);
2593       comp_iter = next;
2594     }
2595   gfc_constructor_free (ctor_head);
2596
2597   return FAILURE;
2598 }
2599
2600
2601 match
2602 gfc_match_structure_constructor (gfc_symbol *sym, gfc_expr **result)
2603 {
2604   match m;
2605   gfc_expr *e;
2606   gfc_symtree *symtree;
2607
2608   gfc_get_sym_tree (sym->name, NULL, &symtree, false);   /* Can't fail */
2609
2610   e = gfc_get_expr ();
2611   e->symtree = symtree;
2612   e->expr_type = EXPR_FUNCTION;
2613
2614   gcc_assert (sym->attr.flavor == FL_DERIVED
2615               && symtree->n.sym->attr.flavor == FL_PROCEDURE);
2616   e->value.function.esym = sym;
2617   e->symtree->n.sym->attr.generic = 1;
2618
2619    m = gfc_match_actual_arglist (0, &e->value.function.actual);
2620    if (m != MATCH_YES)
2621      {
2622        gfc_free_expr (e);
2623        return m;
2624      }
2625
2626    if (gfc_convert_to_structure_constructor (e, sym, NULL, NULL, false)
2627        != SUCCESS)
2628      {
2629        gfc_free_expr (e);
2630        return MATCH_ERROR;
2631      }
2632
2633    *result = e;
2634    return MATCH_YES;
2635 }
2636
2637
2638 /* If the symbol is an implicit do loop index and implicitly typed,
2639    it should not be host associated.  Provide a symtree from the
2640    current namespace.  */
2641 static match
2642 check_for_implicit_index (gfc_symtree **st, gfc_symbol **sym)
2643 {
2644   if ((*sym)->attr.flavor == FL_VARIABLE
2645       && (*sym)->ns != gfc_current_ns
2646       && (*sym)->attr.implied_index
2647       && (*sym)->attr.implicit_type
2648       && !(*sym)->attr.use_assoc)
2649     {
2650       int i;
2651       i = gfc_get_sym_tree ((*sym)->name, NULL, st, false);
2652       if (i)
2653         return MATCH_ERROR;
2654       *sym = (*st)->n.sym;
2655     }
2656   return MATCH_YES;
2657 }
2658
2659
2660 /* Procedure pointer as function result: Replace the function symbol by the
2661    auto-generated hidden result variable named "ppr@".  */
2662
2663 static gfc_try
2664 replace_hidden_procptr_result (gfc_symbol **sym, gfc_symtree **st)
2665 {
2666   /* Check for procedure pointer result variable.  */
2667   if ((*sym)->attr.function && !(*sym)->attr.external
2668       && (*sym)->result && (*sym)->result != *sym
2669       && (*sym)->result->attr.proc_pointer
2670       && (*sym) == gfc_current_ns->proc_name
2671       && (*sym) == (*sym)->result->ns->proc_name
2672       && strcmp ("ppr@", (*sym)->result->name) == 0)
2673     {
2674       /* Automatic replacement with "hidden" result variable.  */
2675       (*sym)->result->attr.referenced = (*sym)->attr.referenced;
2676       *sym = (*sym)->result;
2677       *st = gfc_find_symtree ((*sym)->ns->sym_root, (*sym)->name);
2678       return SUCCESS;
2679     }
2680   return FAILURE;
2681 }
2682
2683
2684 /* Matches a variable name followed by anything that might follow it--
2685    array reference, argument list of a function, etc.  */
2686
2687 match
2688 gfc_match_rvalue (gfc_expr **result)
2689 {
2690   gfc_actual_arglist *actual_arglist;
2691   char name[GFC_MAX_SYMBOL_LEN + 1], argname[GFC_MAX_SYMBOL_LEN + 1];
2692   gfc_state_data *st;
2693   gfc_symbol *sym;
2694   gfc_symtree *symtree;
2695   locus where, old_loc;
2696   gfc_expr *e;
2697   match m, m2;
2698   int i;
2699   gfc_typespec *ts;
2700   bool implicit_char;
2701   gfc_ref *ref;
2702
2703   m = gfc_match_name (name);
2704   if (m != MATCH_YES)
2705     return m;
2706
2707   if (gfc_find_state (COMP_INTERFACE) == SUCCESS
2708       && !gfc_current_ns->has_import_set)
2709     i = gfc_get_sym_tree (name, NULL, &symtree, false);
2710   else
2711     i = gfc_get_ha_sym_tree (name, &symtree);
2712
2713   if (i)
2714     return MATCH_ERROR;
2715
2716   sym = symtree->n.sym;
2717   e = NULL;
2718   where = gfc_current_locus;
2719
2720   replace_hidden_procptr_result (&sym, &symtree);
2721
2722   /* If this is an implicit do loop index and implicitly typed,
2723      it should not be host associated.  */
2724   m = check_for_implicit_index (&symtree, &sym);
2725   if (m != MATCH_YES)
2726     return m;
2727
2728   gfc_set_sym_referenced (sym);
2729   sym->attr.implied_index = 0;
2730
2731   if (sym->attr.function && sym->result == sym)
2732     {
2733       /* See if this is a directly recursive function call.  */
2734       gfc_gobble_whitespace ();
2735       if (sym->attr.recursive
2736           && gfc_peek_ascii_char () == '('
2737           && gfc_current_ns->proc_name == sym
2738           && !sym->attr.dimension)
2739         {
2740           gfc_error ("'%s' at %C is the name of a recursive function "
2741                      "and so refers to the result variable. Use an "
2742                      "explicit RESULT variable for direct recursion "
2743                      "(12.5.2.1)", sym->name);
2744           return MATCH_ERROR;
2745         }
2746
2747       if (gfc_is_function_return_value (sym, gfc_current_ns))
2748         goto variable;
2749
2750       if (sym->attr.entry
2751           && (sym->ns == gfc_current_ns
2752               || sym->ns == gfc_current_ns->parent))
2753         {
2754           gfc_entry_list *el = NULL;
2755           
2756           for (el = sym->ns->entries; el; el = el->next)
2757             if (sym == el->sym)
2758               goto variable;
2759         }
2760     }
2761
2762   if (gfc_matching_procptr_assignment)
2763     goto procptr0;
2764
2765   if (sym->attr.function || sym->attr.external || sym->attr.intrinsic)
2766     goto function0;
2767
2768   if (sym->attr.generic)
2769     goto generic_function;
2770
2771   switch (sym->attr.flavor)
2772     {
2773     case FL_VARIABLE:
2774     variable:
2775       e = gfc_get_expr ();
2776
2777       e->expr_type = EXPR_VARIABLE;
2778       e->symtree = symtree;
2779
2780       m = gfc_match_varspec (e, 0, false, true);
2781       break;
2782
2783     case FL_PARAMETER:
2784       /* A statement of the form "REAL, parameter :: a(0:10) = 1" will
2785          end up here.  Unfortunately, sym->value->expr_type is set to 
2786          EXPR_CONSTANT, and so the if () branch would be followed without
2787          the !sym->as check.  */
2788       if (sym->value && sym->value->expr_type != EXPR_ARRAY && !sym->as)
2789         e = gfc_copy_expr (sym->value);
2790       else
2791         {
2792           e = gfc_get_expr ();
2793           e->expr_type = EXPR_VARIABLE;
2794         }
2795
2796       e->symtree = symtree;
2797       m = gfc_match_varspec (e, 0, false, true);
2798
2799       if (sym->ts.is_c_interop || sym->ts.is_iso_c)
2800         break;
2801
2802       /* Variable array references to derived type parameters cause
2803          all sorts of headaches in simplification. Treating such
2804          expressions as variable works just fine for all array
2805          references.  */
2806       if (sym->value && sym->ts.type == BT_DERIVED && e->ref)
2807         {
2808           for (ref = e->ref; ref; ref = ref->next)
2809             if (ref->type == REF_ARRAY)
2810               break;
2811
2812           if (ref == NULL || ref->u.ar.type == AR_FULL)
2813             break;
2814
2815           ref = e->ref;
2816           e->ref = NULL;
2817           gfc_free_expr (e);
2818           e = gfc_get_expr ();
2819           e->expr_type = EXPR_VARIABLE;
2820           e->symtree = symtree;
2821           e->ref = ref;
2822         }
2823
2824       break;
2825
2826     case FL_DERIVED:
2827       sym = gfc_use_derived (sym);
2828       if (sym == NULL)
2829         m = MATCH_ERROR;
2830       else
2831         goto generic_function;
2832       break;
2833
2834     /* If we're here, then the name is known to be the name of a
2835        procedure, yet it is not sure to be the name of a function.  */
2836     case FL_PROCEDURE:
2837
2838     /* Procedure Pointer Assignments. */
2839     procptr0:
2840       if (gfc_matching_procptr_assignment)
2841         {
2842           gfc_gobble_whitespace ();
2843           if (!sym->attr.dimension && gfc_peek_ascii_char () == '(')
2844             /* Parse functions returning a procptr.  */
2845             goto function0;
2846
2847           e = gfc_get_expr ();
2848           e->expr_type = EXPR_VARIABLE;
2849           e->symtree = symtree;
2850           m = gfc_match_varspec (e, 0, false, true);
2851           if (!e->ref && sym->attr.flavor == FL_UNKNOWN
2852               && sym->ts.type == BT_UNKNOWN
2853               && gfc_add_flavor (&sym->attr, FL_PROCEDURE,
2854                                  sym->name, NULL) == FAILURE)
2855             {
2856               m = MATCH_ERROR;
2857               break;
2858             }
2859           break;
2860         }
2861
2862       if (sym->attr.subroutine)
2863         {
2864           gfc_error ("Unexpected use of subroutine name '%s' at %C",
2865                      sym->name);
2866           m = MATCH_ERROR;
2867           break;
2868         }
2869
2870       /* At this point, the name has to be a non-statement function.
2871          If the name is the same as the current function being
2872          compiled, then we have a variable reference (to the function
2873          result) if the name is non-recursive.  */
2874
2875       st = gfc_enclosing_unit (NULL);
2876
2877       if (st != NULL && st->state == COMP_FUNCTION
2878           && st->sym == sym
2879           && !sym->attr.recursive)
2880         {
2881           e = gfc_get_expr ();
2882           e->symtree = symtree;
2883           e->expr_type = EXPR_VARIABLE;
2884
2885           m = gfc_match_varspec (e, 0, false, true);
2886           break;
2887         }
2888
2889     /* Match a function reference.  */
2890     function0:
2891       m = gfc_match_actual_arglist (0, &actual_arglist);
2892       if (m == MATCH_NO)
2893         {
2894           if (sym->attr.proc == PROC_ST_FUNCTION)
2895             gfc_error ("Statement function '%s' requires argument list at %C",
2896                        sym->name);
2897           else
2898             gfc_error ("Function '%s' requires an argument list at %C",
2899                        sym->name);
2900
2901           m = MATCH_ERROR;
2902           break;
2903         }
2904
2905       if (m != MATCH_YES)
2906         {
2907           m = MATCH_ERROR;
2908           break;
2909         }
2910
2911       gfc_get_ha_sym_tree (name, &symtree);     /* Can't fail */
2912       sym = symtree->n.sym;
2913
2914       replace_hidden_procptr_result (&sym, &symtree);
2915
2916       e = gfc_get_expr ();
2917       e->symtree = symtree;
2918       e->expr_type = EXPR_FUNCTION;
2919       e->value.function.actual = actual_arglist;
2920       e->where = gfc_current_locus;
2921
2922       if (sym->ts.type == BT_CLASS && sym->attr.class_ok
2923           && CLASS_DATA (sym)->as)
2924         e->rank = CLASS_DATA (sym)->as->rank;
2925       else if (sym->as != NULL)
2926         e->rank = sym->as->rank;
2927
2928       if (!sym->attr.function
2929           && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2930         {
2931           m = MATCH_ERROR;
2932           break;
2933         }
2934
2935       /* Check here for the existence of at least one argument for the
2936          iso_c_binding functions C_LOC, C_FUNLOC, and C_ASSOCIATED.  The
2937          argument(s) given will be checked in gfc_iso_c_func_interface,
2938          during resolution of the function call.  */
2939       if (sym->attr.is_iso_c == 1
2940           && (sym->from_intmod == INTMOD_ISO_C_BINDING
2941               && (sym->intmod_sym_id == ISOCBINDING_LOC
2942                   || sym->intmod_sym_id == ISOCBINDING_FUNLOC
2943                   || sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)))
2944         {
2945           /* make sure we were given a param */
2946           if (actual_arglist == NULL)
2947             {
2948               gfc_error ("Missing argument to '%s' at %C", sym->name);
2949               m = MATCH_ERROR;
2950               break;
2951             }
2952         }
2953
2954       if (sym->result == NULL)
2955         sym->result = sym;
2956
2957       m = MATCH_YES;
2958       break;
2959
2960     case FL_UNKNOWN:
2961
2962       /* Special case for derived type variables that get their types
2963          via an IMPLICIT statement.  This can't wait for the
2964          resolution phase.  */
2965
2966       if (gfc_peek_ascii_char () == '%'
2967           && sym->ts.type == BT_UNKNOWN
2968           && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
2969         gfc_set_default_type (sym, 0, sym->ns);
2970
2971       /* If the symbol has a (co)dimension attribute, the expression is a
2972          variable.  */
2973
2974       if (sym->attr.dimension || sym->attr.codimension)
2975         {
2976           if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
2977                               sym->name, NULL) == FAILURE)
2978             {
2979               m = MATCH_ERROR;
2980               break;
2981             }
2982
2983           e = gfc_get_expr ();
2984           e->symtree = symtree;
2985           e->expr_type = EXPR_VARIABLE;
2986           m = gfc_match_varspec (e, 0, false, true);
2987           break;
2988         }
2989
2990       if (sym->ts.type == BT_CLASS && sym->attr.class_ok
2991           && (CLASS_DATA (sym)->attr.dimension
2992               || CLASS_DATA (sym)->attr.codimension))
2993         {
2994           if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
2995                               sym->name, NULL) == FAILURE)
2996             {
2997               m = MATCH_ERROR;
2998               break;
2999             }
3000
3001           e = gfc_get_expr ();
3002           e->symtree = symtree;
3003           e->expr_type = EXPR_VARIABLE;
3004           m = gfc_match_varspec (e, 0, false, true);
3005           break;
3006         }
3007
3008       /* Name is not an array, so we peek to see if a '(' implies a
3009          function call or a substring reference.  Otherwise the
3010          variable is just a scalar.  */
3011
3012       gfc_gobble_whitespace ();
3013       if (gfc_peek_ascii_char () != '(')
3014         {
3015           /* Assume a scalar variable */
3016           e = gfc_get_expr ();
3017           e->symtree = symtree;
3018           e->expr_type = EXPR_VARIABLE;
3019
3020           if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
3021                               sym->name, NULL) == FAILURE)
3022             {
3023               m = MATCH_ERROR;
3024               break;
3025             }
3026
3027           /*FIXME:??? gfc_match_varspec does set this for us: */
3028           e->ts = sym->ts;
3029           m = gfc_match_varspec (e, 0, false, true);
3030           break;
3031         }
3032
3033       /* See if this is a function reference with a keyword argument
3034          as first argument. We do this because otherwise a spurious
3035          symbol would end up in the symbol table.  */
3036
3037       old_loc = gfc_current_locus;
3038       m2 = gfc_match (" ( %n =", argname);
3039       gfc_current_locus = old_loc;
3040
3041       e = gfc_get_expr ();
3042       e->symtree = symtree;
3043
3044       if (m2 != MATCH_YES)
3045         {
3046           /* Try to figure out whether we're dealing with a character type.
3047              We're peeking ahead here, because we don't want to call 
3048              match_substring if we're dealing with an implicitly typed
3049              non-character variable.  */
3050           implicit_char = false;
3051           if (sym->ts.type == BT_UNKNOWN)
3052             {
3053               ts = gfc_get_default_type (sym->name, NULL);
3054               if (ts->type == BT_CHARACTER)
3055                 implicit_char = true;
3056             }
3057
3058           /* See if this could possibly be a substring reference of a name
3059              that we're not sure is a variable yet.  */
3060
3061           if ((implicit_char || sym->ts.type == BT_CHARACTER)
3062               && match_substring (sym->ts.u.cl, 0, &e->ref) == MATCH_YES)
3063             {
3064
3065               e->expr_type = EXPR_VARIABLE;
3066
3067               if (sym->attr.flavor != FL_VARIABLE
3068                   && gfc_add_flavor (&sym->attr, FL_VARIABLE,
3069                                      sym->name, NULL) == FAILURE)
3070                 {
3071                   m = MATCH_ERROR;
3072                   break;
3073                 }
3074
3075               if (sym->ts.type == BT_UNKNOWN
3076                   && gfc_set_default_type (sym, 1, NULL) == FAILURE)
3077                 {
3078                   m = MATCH_ERROR;
3079                   break;
3080                 }
3081
3082               e->ts = sym->ts;
3083               if (e->ref)
3084                 e->ts.u.cl = NULL;
3085               m = MATCH_YES;
3086               break;
3087             }
3088         }
3089
3090       /* Give up, assume we have a function.  */
3091
3092       gfc_get_sym_tree (name, NULL, &symtree, false);   /* Can't fail */
3093       sym = symtree->n.sym;
3094       e->expr_type = EXPR_FUNCTION;
3095
3096       if (!sym->attr.function
3097           && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
3098         {
3099           m = MATCH_ERROR;
3100           break;
3101         }
3102
3103       sym->result = sym;
3104
3105       m = gfc_match_actual_arglist (0, &e->value.function.actual);
3106       if (m == MATCH_NO)
3107         gfc_error ("Missing argument list in function '%s' at %C", sym->name);
3108
3109       if (m != MATCH_YES)
3110         {
3111           m = MATCH_ERROR;
3112           break;
3113         }
3114
3115       /* If our new function returns a character, array or structure
3116          type, it might have subsequent references.  */
3117
3118       m = gfc_match_varspec (e, 0, false, true);
3119       if (m == MATCH_NO)
3120         m = MATCH_YES;
3121
3122       break;
3123
3124     generic_function:
3125       gfc_get_sym_tree (name, NULL, &symtree, false);   /* Can't fail */
3126
3127       e = gfc_get_expr ();
3128       e->symtree = symtree;
3129       e->expr_type = EXPR_FUNCTION;
3130
3131       if (sym->attr.flavor == FL_DERIVED)
3132         {
3133           e->value.function.esym = sym;
3134           e->symtree->n.sym->attr.generic = 1;
3135         }
3136
3137       m = gfc_match_actual_arglist (0, &e->value.function.actual);
3138       break;
3139
3140     default:
3141       gfc_error ("Symbol at %C is not appropriate for an expression");
3142       return MATCH_ERROR;
3143     }
3144
3145   if (m == MATCH_YES)
3146     {
3147       e->where = where;
3148       *result = e;
3149     }
3150   else
3151     gfc_free_expr (e);
3152
3153   return m;
3154 }
3155
3156
3157 /* Match a variable, i.e. something that can be assigned to.  This
3158    starts as a symbol, can be a structure component or an array
3159    reference.  It can be a function if the function doesn't have a
3160    separate RESULT variable.  If the symbol has not been previously
3161    seen, we assume it is a variable.
3162
3163    This function is called by two interface functions:
3164    gfc_match_variable, which has host_flag = 1, and
3165    gfc_match_equiv_variable, with host_flag = 0, to restrict the
3166    match of the symbol to the local scope.  */
3167
3168 static match
3169 match_variable (gfc_expr **result, int equiv_flag, int host_flag)
3170 {
3171   gfc_symbol *sym;
3172   gfc_symtree *st;
3173   gfc_expr *expr;
3174   locus where;
3175   match m;
3176
3177   /* Since nothing has any business being an lvalue in a module
3178      specification block, an interface block or a contains section,
3179      we force the changed_symbols mechanism to work by setting
3180      host_flag to 0. This prevents valid symbols that have the name
3181      of keywords, such as 'end', being turned into variables by
3182      failed matching to assignments for, e.g., END INTERFACE.  */
3183   if (gfc_current_state () == COMP_MODULE
3184       || gfc_current_state () == COMP_INTERFACE
3185       || gfc_current_state () == COMP_CONTAINS)
3186     host_flag = 0;
3187
3188   where = gfc_current_locus;
3189   m = gfc_match_sym_tree (&st, host_flag);
3190   if (m != MATCH_YES)
3191     return m;
3192
3193   sym = st->n.sym;
3194
3195   /* If this is an implicit do loop index and implicitly typed,
3196      it should not be host associated.  */
3197   m = check_for_implicit_index (&st, &sym);
3198   if (m != MATCH_YES)
3199     return m;
3200
3201   sym->attr.implied_index = 0;
3202
3203   gfc_set_sym_referenced (sym);
3204   switch (sym->attr.flavor)
3205     {
3206     case FL_VARIABLE:
3207       /* Everything is alright.  */
3208       break;
3209
3210     case FL_UNKNOWN:
3211       {
3212         sym_flavor flavor = FL_UNKNOWN;
3213
3214         gfc_gobble_whitespace ();
3215
3216         if (sym->attr.external || sym->attr.procedure
3217             || sym->attr.function || sym->attr.subroutine)
3218           flavor = FL_PROCEDURE;
3219
3220         /* If it is not a procedure, is not typed and is host associated,
3221            we cannot give it a flavor yet.  */
3222         else if (sym->ns == gfc_current_ns->parent
3223                    && sym->ts.type == BT_UNKNOWN)
3224           break;
3225
3226         /* These are definitive indicators that this is a variable.  */
3227         else if (gfc_peek_ascii_char () != '(' || sym->ts.type != BT_UNKNOWN
3228                  || sym->attr.pointer || sym->as != NULL)
3229           flavor = FL_VARIABLE;
3230
3231         if (flavor != FL_UNKNOWN
3232             && gfc_add_flavor (&sym->attr, flavor, sym->name, NULL) == FAILURE)
3233           return MATCH_ERROR;
3234       }
3235       break;
3236
3237     case FL_PARAMETER:
3238       if (equiv_flag)
3239         {
3240           gfc_error ("Named constant at %C in an EQUIVALENCE");
3241           return MATCH_ERROR;
3242         }
3243       /* Otherwise this is checked for and an error given in the
3244          variable definition context checks.  */
3245       break;
3246
3247     case FL_PROCEDURE:
3248       /* Check for a nonrecursive function result variable.  */
3249       if (sym->attr.function
3250           && !sym->attr.external
3251           && sym->result == sym
3252           && (gfc_is_function_return_value (sym, gfc_current_ns)
3253               || (sym->attr.entry
3254                   && sym->ns == gfc_current_ns)
3255               || (sym->attr.entry
3256                   && sym->ns == gfc_current_ns->parent)))
3257         {
3258           /* If a function result is a derived type, then the derived
3259              type may still have to be resolved.  */
3260
3261           if (sym->ts.type == BT_DERIVED
3262               && gfc_use_derived (sym->ts.u.derived) == NULL)
3263             return MATCH_ERROR;
3264           break;
3265         }
3266
3267       if (sym->attr.proc_pointer
3268           || replace_hidden_procptr_result (&sym, &st) == SUCCESS)
3269         break;
3270
3271       /* Fall through to error */
3272
3273     default:
3274       gfc_error ("'%s' at %C is not a variable", sym->name);
3275       return MATCH_ERROR;
3276     }
3277
3278   /* Special case for derived type variables that get their types
3279      via an IMPLICIT statement.  This can't wait for the
3280      resolution phase.  */
3281
3282     {
3283       gfc_namespace * implicit_ns;
3284
3285       if (gfc_current_ns->proc_name == sym)
3286         implicit_ns = gfc_current_ns;
3287       else
3288         implicit_ns = sym->ns;
3289         
3290       if (gfc_peek_ascii_char () == '%'
3291           && sym->ts.type == BT_UNKNOWN
3292           && gfc_get_default_type (sym->name, implicit_ns)->type == BT_DERIVED)
3293         gfc_set_default_type (sym, 0, implicit_ns);
3294     }
3295
3296   expr = gfc_get_expr ();
3297
3298   expr->expr_type = EXPR_VARIABLE;
3299   expr->symtree = st;
3300   expr->ts = sym->ts;
3301   expr->where = where;
3302
3303   /* Now see if we have to do more.  */
3304   m = gfc_match_varspec (expr, equiv_flag, false, false);
3305   if (m != MATCH_YES)
3306     {
3307       gfc_free_expr (expr);
3308       return m;
3309     }
3310
3311   *result = expr;
3312   return MATCH_YES;
3313 }
3314
3315
3316 match
3317 gfc_match_variable (gfc_expr **result, int equiv_flag)
3318 {
3319   return match_variable (result, equiv_flag, 1);
3320 }
3321
3322
3323 match
3324 gfc_match_equiv_variable (gfc_expr **result)
3325 {
3326   return match_variable (result, 1, 0);
3327 }
3328