OSDN Git Service

* intrinsic.c (add_functions): Add SELECTED_CHAR_KIND intrinsic.
[pf3gnuchains/gcc-fork.git] / gcc / fortran / primary.c
1 /* Primary expression subroutines
2    Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008
3    Free Software Foundation, Inc.
4    Contributed by Andy Vaught
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "flags.h"
25 #include "gfortran.h"
26 #include "arith.h"
27 #include "match.h"
28 #include "parse.h"
29
30 /* Matches a kind-parameter expression, which is either a named
31    symbolic constant or a nonnegative integer constant.  If
32    successful, sets the kind value to the correct integer.  */
33
34 static match
35 match_kind_param (int *kind)
36 {
37   char name[GFC_MAX_SYMBOL_LEN + 1];
38   gfc_symbol *sym;
39   const char *p;
40   match m;
41
42   m = gfc_match_small_literal_int (kind, NULL);
43   if (m != MATCH_NO)
44     return m;
45
46   m = gfc_match_name (name);
47   if (m != MATCH_YES)
48     return m;
49
50   if (gfc_find_symbol (name, NULL, 1, &sym))
51     return MATCH_ERROR;
52
53   if (sym == NULL)
54     return MATCH_NO;
55
56   if (sym->attr.flavor != FL_PARAMETER)
57     return MATCH_NO;
58
59   p = gfc_extract_int (sym->value, kind);
60   if (p != NULL)
61     return MATCH_NO;
62
63   gfc_set_sym_referenced (sym);
64
65   if (*kind < 0)
66     return MATCH_NO;
67
68   return MATCH_YES;
69 }
70
71
72 /* Get a trailing kind-specification for non-character variables.
73    Returns:
74       the integer kind value or:
75       -1 if an error was generated
76       -2 if no kind was found */
77
78 static int
79 get_kind (void)
80 {
81   int kind;
82   match m;
83
84   if (gfc_match_char ('_') != MATCH_YES)
85     return -2;
86
87   m = match_kind_param (&kind);
88   if (m == MATCH_NO)
89     gfc_error ("Missing kind-parameter at %C");
90
91   return (m == MATCH_YES) ? kind : -1;
92 }
93
94
95 /* Given a character and a radix, see if the character is a valid
96    digit in that radix.  */
97
98 static int
99 check_digit (int c, int radix)
100 {
101   int r;
102
103   switch (radix)
104     {
105     case 2:
106       r = ('0' <= c && c <= '1');
107       break;
108
109     case 8:
110       r = ('0' <= c && c <= '7');
111       break;
112
113     case 10:
114       r = ('0' <= c && c <= '9');
115       break;
116
117     case 16:
118       r = ISXDIGIT (c);
119       break;
120
121     default:
122       gfc_internal_error ("check_digit(): bad radix");
123     }
124
125   return r;
126 }
127
128
129 /* Match the digit string part of an integer if signflag is not set,
130    the signed digit string part if signflag is set.  If the buffer 
131    is NULL, we just count characters for the resolution pass.  Returns 
132    the number of characters matched, -1 for no match.  */
133
134 static int
135 match_digits (int signflag, int radix, char *buffer)
136 {
137   locus old_loc;
138   int length, c;
139
140   length = 0;
141   c = gfc_next_char ();
142
143   if (signflag && (c == '+' || c == '-'))
144     {
145       if (buffer != NULL)
146         *buffer++ = c;
147       gfc_gobble_whitespace ();
148       c = gfc_next_char ();
149       length++;
150     }
151
152   if (!check_digit (c, radix))
153     return -1;
154
155   length++;
156   if (buffer != NULL)
157     *buffer++ = c;
158
159   for (;;)
160     {
161       old_loc = gfc_current_locus;
162       c = gfc_next_char ();
163
164       if (!check_digit (c, radix))
165         break;
166
167       if (buffer != NULL)
168         *buffer++ = c;
169       length++;
170     }
171
172   gfc_current_locus = old_loc;
173
174   return length;
175 }
176
177
178 /* Match an integer (digit string and optional kind).  
179    A sign will be accepted if signflag is set.  */
180
181 static match
182 match_integer_constant (gfc_expr **result, int signflag)
183 {
184   int length, kind;
185   locus old_loc;
186   char *buffer;
187   gfc_expr *e;
188
189   old_loc = gfc_current_locus;
190   gfc_gobble_whitespace ();
191
192   length = match_digits (signflag, 10, NULL);
193   gfc_current_locus = old_loc;
194   if (length == -1)
195     return MATCH_NO;
196
197   buffer = alloca (length + 1);
198   memset (buffer, '\0', length + 1);
199
200   gfc_gobble_whitespace ();
201
202   match_digits (signflag, 10, buffer);
203
204   kind = get_kind ();
205   if (kind == -2)
206     kind = gfc_default_integer_kind;
207   if (kind == -1)
208     return MATCH_ERROR;
209
210   if (gfc_validate_kind (BT_INTEGER, kind, true) < 0)
211     {
212       gfc_error ("Integer kind %d at %C not available", kind);
213       return MATCH_ERROR;
214     }
215
216   e = gfc_convert_integer (buffer, kind, 10, &gfc_current_locus);
217
218   if (gfc_range_check (e) != ARITH_OK)
219     {
220       gfc_error ("Integer too big for its kind at %C. This check can be "
221                  "disabled with the option -fno-range-check");
222
223       gfc_free_expr (e);
224       return MATCH_ERROR;
225     }
226
227   *result = e;
228   return MATCH_YES;
229 }
230
231
232 /* Match a Hollerith constant.  */
233
234 static match
235 match_hollerith_constant (gfc_expr **result)
236 {
237   locus old_loc;
238   gfc_expr *e = NULL;
239   const char *msg;
240   int num;
241   int i;  
242
243   old_loc = gfc_current_locus;
244   gfc_gobble_whitespace ();
245
246   if (match_integer_constant (&e, 0) == MATCH_YES
247       && gfc_match_char ('h') == MATCH_YES)
248     {
249       if (gfc_notify_std (GFC_STD_LEGACY, "Extension: Hollerith constant "
250                           "at %C") == FAILURE)
251         goto cleanup;
252
253       msg = gfc_extract_int (e, &num);
254       if (msg != NULL)
255         {
256           gfc_error (msg);
257           goto cleanup;
258         }
259       if (num == 0)
260         {
261           gfc_error ("Invalid Hollerith constant: %L must contain at least "
262                      "one character", &old_loc);
263           goto cleanup;
264         }
265       if (e->ts.kind != gfc_default_integer_kind)
266         {
267           gfc_error ("Invalid Hollerith constant: Integer kind at %L "
268                      "should be default", &old_loc);
269           goto cleanup;
270         }
271       else
272         {
273           gfc_free_expr (e);
274           e = gfc_constant_result (BT_HOLLERITH, gfc_default_character_kind,
275                                    &gfc_current_locus);
276
277           e->representation.string = gfc_getmem (num + 1);
278           for (i = 0; i < num; i++)
279             {
280               e->representation.string[i] = gfc_next_char_literal (1);
281             }
282           e->representation.string[num] = '\0';
283           e->representation.length = num;
284
285           *result = e;
286           return MATCH_YES;
287         }
288     }
289
290   gfc_free_expr (e);
291   gfc_current_locus = old_loc;
292   return MATCH_NO;
293
294 cleanup:
295   gfc_free_expr (e);
296   return MATCH_ERROR;
297 }
298
299
300 /* Match a binary, octal or hexadecimal constant that can be found in
301    a DATA statement.  The standard permits b'010...', o'73...', and
302    z'a1...' where b, o, and z can be capital letters.  This function
303    also accepts postfixed forms of the constants: '01...'b, '73...'o,
304    and 'a1...'z.  An additional extension is the use of x for z.  */
305
306 static match
307 match_boz_constant (gfc_expr **result)
308 {
309   int post, radix, delim, length, x_hex, kind;
310   locus old_loc, start_loc;
311   char *buffer;
312   gfc_expr *e;
313
314   start_loc = old_loc = gfc_current_locus;
315   gfc_gobble_whitespace ();
316
317   x_hex = 0;
318   switch (post = gfc_next_char ())
319     {
320     case 'b':
321       radix = 2;
322       post = 0;
323       break;
324     case 'o':
325       radix = 8;
326       post = 0;
327       break;
328     case 'x':
329       x_hex = 1;
330       /* Fall through.  */
331     case 'z':
332       radix = 16;
333       post = 0;
334       break;
335     case '\'':
336       /* Fall through.  */
337     case '\"':
338       delim = post;
339       post = 1;
340       radix = 16;  /* Set to accept any valid digit string.  */
341       break;
342     default:
343       goto backup;
344     }
345
346   /* No whitespace allowed here.  */
347
348   if (post == 0)
349     delim = gfc_next_char ();
350
351   if (delim != '\'' && delim != '\"')
352     goto backup;
353
354   if (x_hex
355       && (gfc_notify_std (GFC_STD_GNU, "Extension: Hexadecimal "
356                           "constant at %C uses non-standard syntax")
357           == FAILURE))
358       return MATCH_ERROR;
359
360   old_loc = gfc_current_locus;
361
362   length = match_digits (0, radix, NULL);
363   if (length == -1)
364     {
365       gfc_error ("Empty set of digits in BOZ constant at %C");
366       return MATCH_ERROR;
367     }
368
369   if (gfc_next_char () != delim)
370     {
371       gfc_error ("Illegal character in BOZ constant at %C");
372       return MATCH_ERROR;
373     }
374
375   if (post == 1)
376     {
377       switch (gfc_next_char ())
378         {
379         case 'b':
380           radix = 2;
381           break;
382         case 'o':
383           radix = 8;
384           break;
385         case 'x':
386           /* Fall through.  */
387         case 'z':
388           radix = 16;
389           break;
390         default:
391           goto backup;
392         }
393
394       if (gfc_notify_std (GFC_STD_GNU, "Extension: BOZ constant "
395                           "at %C uses non-standard postfix syntax")
396           == FAILURE)
397         return MATCH_ERROR;
398     }
399
400   gfc_current_locus = old_loc;
401
402   buffer = alloca (length + 1);
403   memset (buffer, '\0', length + 1);
404
405   match_digits (0, radix, buffer);
406   gfc_next_char ();    /* Eat delimiter.  */
407   if (post == 1)
408     gfc_next_char ();  /* Eat postfixed b, o, z, or x.  */
409
410   /* In section 5.2.5 and following C567 in the Fortran 2003 standard, we find
411      "If a data-stmt-constant is a boz-literal-constant, the corresponding
412      variable shall be of type integer.  The boz-literal-constant is treated
413      as if it were an int-literal-constant with a kind-param that specifies
414      the representation method with the largest decimal exponent range
415      supported by the processor."  */
416
417   kind = gfc_max_integer_kind;
418   e = gfc_convert_integer (buffer, kind, radix, &gfc_current_locus);
419
420   /* Mark as boz variable.  */
421   e->is_boz = 1;
422
423   if (gfc_range_check (e) != ARITH_OK)
424     {
425       gfc_error ("Integer too big for integer kind %i at %C", kind);
426       gfc_free_expr (e);
427       return MATCH_ERROR;
428     }
429
430   if (!gfc_in_match_data ()
431       && (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: BOZ used outside a DATA "
432                           "statement at %C")
433           == FAILURE))
434       return MATCH_ERROR;
435
436   *result = e;
437   return MATCH_YES;
438
439 backup:
440   gfc_current_locus = start_loc;
441   return MATCH_NO;
442 }
443
444
445 /* Match a real constant of some sort.  Allow a signed constant if signflag
446    is nonzero.  */
447
448 static match
449 match_real_constant (gfc_expr **result, int signflag)
450 {
451   int kind, c, count, seen_dp, seen_digits, exp_char;
452   locus old_loc, temp_loc;
453   char *p, *buffer;
454   gfc_expr *e;
455   bool negate;
456
457   old_loc = gfc_current_locus;
458   gfc_gobble_whitespace ();
459
460   e = NULL;
461
462   count = 0;
463   seen_dp = 0;
464   seen_digits = 0;
465   exp_char = ' ';
466   negate = FALSE;
467
468   c = gfc_next_char ();
469   if (signflag && (c == '+' || c == '-'))
470     {
471       if (c == '-')
472         negate = TRUE;
473
474       gfc_gobble_whitespace ();
475       c = gfc_next_char ();
476     }
477
478   /* Scan significand.  */
479   for (;; c = gfc_next_char (), count++)
480     {
481       if (c == '.')
482         {
483           if (seen_dp)
484             goto done;
485
486           /* Check to see if "." goes with a following operator like 
487              ".eq.".  */
488           temp_loc = gfc_current_locus;
489           c = gfc_next_char ();
490
491           if (c == 'e' || c == 'd' || c == 'q')
492             {
493               c = gfc_next_char ();
494               if (c == '.')
495                 goto done;      /* Operator named .e. or .d.  */
496             }
497
498           if (ISALPHA (c))
499             goto done;          /* Distinguish 1.e9 from 1.eq.2 */
500
501           gfc_current_locus = temp_loc;
502           seen_dp = 1;
503           continue;
504         }
505
506       if (ISDIGIT (c))
507         {
508           seen_digits = 1;
509           continue;
510         }
511
512       break;
513     }
514
515   if (!seen_digits || (c != 'e' && c != 'd' && c != 'q'))
516     goto done;
517   exp_char = c;
518
519   /* Scan exponent.  */
520   c = gfc_next_char ();
521   count++;
522
523   if (c == '+' || c == '-')
524     {                           /* optional sign */
525       c = gfc_next_char ();
526       count++;
527     }
528
529   if (!ISDIGIT (c))
530     {
531       gfc_error ("Missing exponent in real number at %C");
532       return MATCH_ERROR;
533     }
534
535   while (ISDIGIT (c))
536     {
537       c = gfc_next_char ();
538       count++;
539     }
540
541 done:
542   /* Check that we have a numeric constant.  */
543   if (!seen_digits || (!seen_dp && exp_char == ' '))
544     {
545       gfc_current_locus = old_loc;
546       return MATCH_NO;
547     }
548
549   /* Convert the number.  */
550   gfc_current_locus = old_loc;
551   gfc_gobble_whitespace ();
552
553   buffer = alloca (count + 1);
554   memset (buffer, '\0', count + 1);
555
556   p = buffer;
557   c = gfc_next_char ();
558   if (c == '+' || c == '-')
559     {
560       gfc_gobble_whitespace ();
561       c = gfc_next_char ();
562     }
563
564   /* Hack for mpfr_set_str().  */
565   for (;;)
566     {
567       if (c == 'd' || c == 'q')
568         *p = 'e';
569       else
570         *p = c;
571       p++;
572       if (--count == 0)
573         break;
574
575       c = gfc_next_char ();
576     }
577
578   kind = get_kind ();
579   if (kind == -1)
580     goto cleanup;
581
582   switch (exp_char)
583     {
584     case 'd':
585       if (kind != -2)
586         {
587           gfc_error ("Real number at %C has a 'd' exponent and an explicit "
588                      "kind");
589           goto cleanup;
590         }
591       kind = gfc_default_double_kind;
592       break;
593
594     default:
595       if (kind == -2)
596         kind = gfc_default_real_kind;
597
598       if (gfc_validate_kind (BT_REAL, kind, true) < 0)
599         {
600           gfc_error ("Invalid real kind %d at %C", kind);
601           goto cleanup;
602         }
603     }
604
605   e = gfc_convert_real (buffer, kind, &gfc_current_locus);
606   if (negate)
607     mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
608
609   switch (gfc_range_check (e))
610     {
611     case ARITH_OK:
612       break;
613     case ARITH_OVERFLOW:
614       gfc_error ("Real constant overflows its kind at %C");
615       goto cleanup;
616
617     case ARITH_UNDERFLOW:
618       if (gfc_option.warn_underflow)
619         gfc_warning ("Real constant underflows its kind at %C");
620       mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
621       break;
622
623     default:
624       gfc_internal_error ("gfc_range_check() returned bad value");
625     }
626
627   *result = e;
628   return MATCH_YES;
629
630 cleanup:
631   gfc_free_expr (e);
632   return MATCH_ERROR;
633 }
634
635
636 /* Match a substring reference.  */
637
638 static match
639 match_substring (gfc_charlen *cl, int init, gfc_ref **result)
640 {
641   gfc_expr *start, *end;
642   locus old_loc;
643   gfc_ref *ref;
644   match m;
645
646   start = NULL;
647   end = NULL;
648
649   old_loc = gfc_current_locus;
650
651   m = gfc_match_char ('(');
652   if (m != MATCH_YES)
653     return MATCH_NO;
654
655   if (gfc_match_char (':') != MATCH_YES)
656     {
657       if (init)
658         m = gfc_match_init_expr (&start);
659       else
660         m = gfc_match_expr (&start);
661
662       if (m != MATCH_YES)
663         {
664           m = MATCH_NO;
665           goto cleanup;
666         }
667
668       m = gfc_match_char (':');
669       if (m != MATCH_YES)
670         goto cleanup;
671     }
672
673   if (gfc_match_char (')') != MATCH_YES)
674     {
675       if (init)
676         m = gfc_match_init_expr (&end);
677       else
678         m = gfc_match_expr (&end);
679
680       if (m == MATCH_NO)
681         goto syntax;
682       if (m == MATCH_ERROR)
683         goto cleanup;
684
685       m = gfc_match_char (')');
686       if (m == MATCH_NO)
687         goto syntax;
688     }
689
690   /* Optimize away the (:) reference.  */
691   if (start == NULL && end == NULL)
692     ref = NULL;
693   else
694     {
695       ref = gfc_get_ref ();
696
697       ref->type = REF_SUBSTRING;
698       if (start == NULL)
699         start = gfc_int_expr (1);
700       ref->u.ss.start = start;
701       if (end == NULL && cl)
702         end = gfc_copy_expr (cl->length);
703       ref->u.ss.end = end;
704       ref->u.ss.length = cl;
705     }
706
707   *result = ref;
708   return MATCH_YES;
709
710 syntax:
711   gfc_error ("Syntax error in SUBSTRING specification at %C");
712   m = MATCH_ERROR;
713
714 cleanup:
715   gfc_free_expr (start);
716   gfc_free_expr (end);
717
718   gfc_current_locus = old_loc;
719   return m;
720 }
721
722
723 /* Reads the next character of a string constant, taking care to
724    return doubled delimiters on the input as a single instance of
725    the delimiter.
726
727    Special return values are:
728      -1   End of the string, as determined by the delimiter
729      -2   Unterminated string detected
730
731    Backslash codes are also expanded at this time.  */
732
733 static int
734 next_string_char (char delimiter)
735 {
736   locus old_locus;
737   int c;
738
739   c = gfc_next_char_literal (1);
740
741   if (c == '\n')
742     return -2;
743
744   if (gfc_option.flag_backslash && c == '\\')
745     {
746       old_locus = gfc_current_locus;
747
748       if (gfc_match_special_char (&c) == MATCH_NO)
749         gfc_current_locus = old_locus;
750
751       if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
752         gfc_warning ("Extension: backslash character at %C");
753     }
754
755   if (c != delimiter)
756     return c;
757
758   old_locus = gfc_current_locus;
759   c = gfc_next_char_literal (0);
760
761   if (c == delimiter)
762     return c;
763   gfc_current_locus = old_locus;
764
765   return -1;
766 }
767
768
769 /* Special case of gfc_match_name() that matches a parameter kind name
770    before a string constant.  This takes case of the weird but legal
771    case of:
772
773      kind_____'string'
774
775    where kind____ is a parameter. gfc_match_name() will happily slurp
776    up all the underscores, which leads to problems.  If we return
777    MATCH_YES, the parse pointer points to the final underscore, which
778    is not part of the name.  We never return MATCH_ERROR-- errors in
779    the name will be detected later.  */
780
781 static match
782 match_charkind_name (char *name)
783 {
784   locus old_loc;
785   char c, peek;
786   int len;
787
788   gfc_gobble_whitespace ();
789   c = gfc_next_char ();
790   if (!ISALPHA (c))
791     return MATCH_NO;
792
793   *name++ = c;
794   len = 1;
795
796   for (;;)
797     {
798       old_loc = gfc_current_locus;
799       c = gfc_next_char ();
800
801       if (c == '_')
802         {
803           peek = gfc_peek_char ();
804
805           if (peek == '\'' || peek == '\"')
806             {
807               gfc_current_locus = old_loc;
808               *name = '\0';
809               return MATCH_YES;
810             }
811         }
812
813       if (!ISALNUM (c)
814           && c != '_'
815           && (gfc_option.flag_dollar_ok && c != '$'))
816         break;
817
818       *name++ = c;
819       if (++len > GFC_MAX_SYMBOL_LEN)
820         break;
821     }
822
823   return MATCH_NO;
824 }
825
826
827 /* See if the current input matches a character constant.  Lots of
828    contortions have to be done to match the kind parameter which comes
829    before the actual string.  The main consideration is that we don't
830    want to error out too quickly.  For example, we don't actually do
831    any validation of the kinds until we have actually seen a legal
832    delimiter.  Using match_kind_param() generates errors too quickly.  */
833
834 static match
835 match_string_constant (gfc_expr **result)
836 {
837   char *p, name[GFC_MAX_SYMBOL_LEN + 1];
838   int i, c, kind, length, delimiter, warn_ampersand;
839   locus old_locus, start_locus;
840   gfc_symbol *sym;
841   gfc_expr *e;
842   const char *q;
843   match m;
844
845   old_locus = gfc_current_locus;
846
847   gfc_gobble_whitespace ();
848
849   start_locus = gfc_current_locus;
850
851   c = gfc_next_char ();
852   if (c == '\'' || c == '"')
853     {
854       kind = gfc_default_character_kind;
855       goto got_delim;
856     }
857
858   if (ISDIGIT (c))
859     {
860       kind = 0;
861
862       while (ISDIGIT (c))
863         {
864           kind = kind * 10 + c - '0';
865           if (kind > 9999999)
866             goto no_match;
867           c = gfc_next_char ();
868         }
869
870     }
871   else
872     {
873       gfc_current_locus = old_locus;
874
875       m = match_charkind_name (name);
876       if (m != MATCH_YES)
877         goto no_match;
878
879       if (gfc_find_symbol (name, NULL, 1, &sym)
880           || sym == NULL
881           || sym->attr.flavor != FL_PARAMETER)
882         goto no_match;
883
884       kind = -1;
885       c = gfc_next_char ();
886     }
887
888   if (c == ' ')
889     {
890       gfc_gobble_whitespace ();
891       c = gfc_next_char ();
892     }
893
894   if (c != '_')
895     goto no_match;
896
897   gfc_gobble_whitespace ();
898   start_locus = gfc_current_locus;
899
900   c = gfc_next_char ();
901   if (c != '\'' && c != '"')
902     goto no_match;
903
904   if (kind == -1)
905     {
906       q = gfc_extract_int (sym->value, &kind);
907       if (q != NULL)
908         {
909           gfc_error (q);
910           return MATCH_ERROR;
911         }
912       gfc_set_sym_referenced (sym);
913     }
914
915   if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
916     {
917       gfc_error ("Invalid kind %d for CHARACTER constant at %C", kind);
918       return MATCH_ERROR;
919     }
920
921 got_delim:
922   /* Scan the string into a block of memory by first figuring out how
923      long it is, allocating the structure, then re-reading it.  This
924      isn't particularly efficient, but string constants aren't that
925      common in most code.  TODO: Use obstacks?  */
926
927   delimiter = c;
928   length = 0;
929
930   for (;;)
931     {
932       c = next_string_char (delimiter);
933       if (c == -1)
934         break;
935       if (c == -2)
936         {
937           gfc_current_locus = start_locus;
938           gfc_error ("Unterminated character constant beginning at %C");
939           return MATCH_ERROR;
940         }
941
942       length++;
943     }
944
945   /* Peek at the next character to see if it is a b, o, z, or x for the
946      postfixed BOZ literal constants.  */
947   c = gfc_peek_char ();
948   if (c == 'b' || c == 'o' || c =='z' || c == 'x')
949     goto no_match;
950
951
952   e = gfc_get_expr ();
953
954   e->expr_type = EXPR_CONSTANT;
955   e->ref = NULL;
956   e->ts.type = BT_CHARACTER;
957   e->ts.kind = kind;
958   e->ts.is_c_interop = 0;
959   e->ts.is_iso_c = 0;
960   e->where = start_locus;
961
962   e->value.character.string = p = gfc_getmem (length + 1);
963   e->value.character.length = length;
964
965   gfc_current_locus = start_locus;
966   gfc_next_char ();             /* Skip delimiter */
967
968   /* We disable the warning for the following loop as the warning has already
969      been printed in the loop above.  */
970   warn_ampersand = gfc_option.warn_ampersand;
971   gfc_option.warn_ampersand = 0;
972
973   for (i = 0; i < length; i++)
974     *p++ = next_string_char (delimiter);
975
976   *p = '\0';    /* TODO: C-style string is for development/debug purposes.  */
977   gfc_option.warn_ampersand = warn_ampersand;
978
979   if (next_string_char (delimiter) != -1)
980     gfc_internal_error ("match_string_constant(): Delimiter not found");
981
982   if (match_substring (NULL, 0, &e->ref) != MATCH_NO)
983     e->expr_type = EXPR_SUBSTRING;
984
985   *result = e;
986
987   return MATCH_YES;
988
989 no_match:
990   gfc_current_locus = old_locus;
991   return MATCH_NO;
992 }
993
994
995 /* Match a .true. or .false.  Returns 1 if a .true. was found,
996    0 if a .false. was found, and -1 otherwise.  */
997 static int
998 match_logical_constant_string (void)
999 {
1000   locus orig_loc = gfc_current_locus;
1001
1002   gfc_gobble_whitespace ();
1003   if (gfc_next_char () == '.')
1004     {
1005       int ch = gfc_next_char();
1006       if (ch == 'f')
1007         {
1008           if (gfc_next_char () == 'a'
1009               && gfc_next_char () == 'l'
1010               && gfc_next_char () == 's'
1011               && gfc_next_char () == 'e'
1012               && gfc_next_char () == '.')
1013             /* Matched ".false.".  */
1014             return 0;
1015         }
1016       else if (ch == 't')
1017         {
1018           if (gfc_next_char () == 'r'
1019               && gfc_next_char () == 'u'
1020               && gfc_next_char () == 'e'
1021               && gfc_next_char () == '.')
1022             /* Matched ".true.".  */
1023             return 1;
1024         }
1025     }
1026   gfc_current_locus = orig_loc;
1027   return -1;
1028 }
1029
1030 /* Match a .true. or .false.  */
1031
1032 static match
1033 match_logical_constant (gfc_expr **result)
1034 {
1035   gfc_expr *e;
1036   int i, kind;
1037
1038   i = match_logical_constant_string ();
1039   if (i == -1)
1040     return MATCH_NO;
1041
1042   kind = get_kind ();
1043   if (kind == -1)
1044     return MATCH_ERROR;
1045   if (kind == -2)
1046     kind = gfc_default_logical_kind;
1047
1048   if (gfc_validate_kind (BT_LOGICAL, kind, true) < 0)
1049     {
1050       gfc_error ("Bad kind for logical constant at %C");
1051       return MATCH_ERROR;
1052     }
1053
1054   e = gfc_get_expr ();
1055
1056   e->expr_type = EXPR_CONSTANT;
1057   e->value.logical = i;
1058   e->ts.type = BT_LOGICAL;
1059   e->ts.kind = kind;
1060   e->ts.is_c_interop = 0;
1061   e->ts.is_iso_c = 0;
1062   e->where = gfc_current_locus;
1063
1064   *result = e;
1065   return MATCH_YES;
1066 }
1067
1068
1069 /* Match a real or imaginary part of a complex constant that is a
1070    symbolic constant.  */
1071
1072 static match
1073 match_sym_complex_part (gfc_expr **result)
1074 {
1075   char name[GFC_MAX_SYMBOL_LEN + 1];
1076   gfc_symbol *sym;
1077   gfc_expr *e;
1078   match m;
1079
1080   m = gfc_match_name (name);
1081   if (m != MATCH_YES)
1082     return m;
1083
1084   if (gfc_find_symbol (name, NULL, 1, &sym) || sym == NULL)
1085     return MATCH_NO;
1086
1087   if (sym->attr.flavor != FL_PARAMETER)
1088     {
1089       gfc_error ("Expected PARAMETER symbol in complex constant at %C");
1090       return MATCH_ERROR;
1091     }
1092
1093   if (!gfc_numeric_ts (&sym->value->ts))
1094     {
1095       gfc_error ("Numeric PARAMETER required in complex constant at %C");
1096       return MATCH_ERROR;
1097     }
1098
1099   if (sym->value->rank != 0)
1100     {
1101       gfc_error ("Scalar PARAMETER required in complex constant at %C");
1102       return MATCH_ERROR;
1103     }
1104
1105   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PARAMETER symbol in "
1106                       "complex constant at %C") == FAILURE)
1107     return MATCH_ERROR;
1108
1109   switch (sym->value->ts.type)
1110     {
1111     case BT_REAL:
1112       e = gfc_copy_expr (sym->value);
1113       break;
1114
1115     case BT_COMPLEX:
1116       e = gfc_complex2real (sym->value, sym->value->ts.kind);
1117       if (e == NULL)
1118         goto error;
1119       break;
1120
1121     case BT_INTEGER:
1122       e = gfc_int2real (sym->value, gfc_default_real_kind);
1123       if (e == NULL)
1124         goto error;
1125       break;
1126
1127     default:
1128       gfc_internal_error ("gfc_match_sym_complex_part(): Bad type");
1129     }
1130
1131   *result = e;          /* e is a scalar, real, constant expression.  */
1132   return MATCH_YES;
1133
1134 error:
1135   gfc_error ("Error converting PARAMETER constant in complex constant at %C");
1136   return MATCH_ERROR;
1137 }
1138
1139
1140 /* Match a real or imaginary part of a complex number.  */
1141
1142 static match
1143 match_complex_part (gfc_expr **result)
1144 {
1145   match m;
1146
1147   m = match_sym_complex_part (result);
1148   if (m != MATCH_NO)
1149     return m;
1150
1151   m = match_real_constant (result, 1);
1152   if (m != MATCH_NO)
1153     return m;
1154
1155   return match_integer_constant (result, 1);
1156 }
1157
1158
1159 /* Try to match a complex constant.  */
1160
1161 static match
1162 match_complex_constant (gfc_expr **result)
1163 {
1164   gfc_expr *e, *real, *imag;
1165   gfc_error_buf old_error;
1166   gfc_typespec target;
1167   locus old_loc;
1168   int kind;
1169   match m;
1170
1171   old_loc = gfc_current_locus;
1172   real = imag = e = NULL;
1173
1174   m = gfc_match_char ('(');
1175   if (m != MATCH_YES)
1176     return m;
1177
1178   gfc_push_error (&old_error);
1179
1180   m = match_complex_part (&real);
1181   if (m == MATCH_NO)
1182     {
1183       gfc_free_error (&old_error);
1184       goto cleanup;
1185     }
1186
1187   if (gfc_match_char (',') == MATCH_NO)
1188     {
1189       gfc_pop_error (&old_error);
1190       m = MATCH_NO;
1191       goto cleanup;
1192     }
1193
1194   /* If m is error, then something was wrong with the real part and we
1195      assume we have a complex constant because we've seen the ','.  An
1196      ambiguous case here is the start of an iterator list of some
1197      sort. These sort of lists are matched prior to coming here.  */
1198
1199   if (m == MATCH_ERROR)
1200     {
1201       gfc_free_error (&old_error);
1202       goto cleanup;
1203     }
1204   gfc_pop_error (&old_error);
1205
1206   m = match_complex_part (&imag);
1207   if (m == MATCH_NO)
1208     goto syntax;
1209   if (m == MATCH_ERROR)
1210     goto cleanup;
1211
1212   m = gfc_match_char (')');
1213   if (m == MATCH_NO)
1214     {
1215       /* Give the matcher for implied do-loops a chance to run.  This
1216          yields a much saner error message for (/ (i, 4=i, 6) /).  */
1217       if (gfc_peek_char () == '=')
1218         {
1219           m = MATCH_ERROR;
1220           goto cleanup;
1221         }
1222       else
1223     goto syntax;
1224     }
1225
1226   if (m == MATCH_ERROR)
1227     goto cleanup;
1228
1229   /* Decide on the kind of this complex number.  */
1230   if (real->ts.type == BT_REAL)
1231     {
1232       if (imag->ts.type == BT_REAL)
1233         kind = gfc_kind_max (real, imag);
1234       else
1235         kind = real->ts.kind;
1236     }
1237   else
1238     {
1239       if (imag->ts.type == BT_REAL)
1240         kind = imag->ts.kind;
1241       else
1242         kind = gfc_default_real_kind;
1243     }
1244   target.type = BT_REAL;
1245   target.kind = kind;
1246   target.is_c_interop = 0;
1247   target.is_iso_c = 0;
1248
1249   if (real->ts.type != BT_REAL || kind != real->ts.kind)
1250     gfc_convert_type (real, &target, 2);
1251   if (imag->ts.type != BT_REAL || kind != imag->ts.kind)
1252     gfc_convert_type (imag, &target, 2);
1253
1254   e = gfc_convert_complex (real, imag, kind);
1255   e->where = gfc_current_locus;
1256
1257   gfc_free_expr (real);
1258   gfc_free_expr (imag);
1259
1260   *result = e;
1261   return MATCH_YES;
1262
1263 syntax:
1264   gfc_error ("Syntax error in COMPLEX constant at %C");
1265   m = MATCH_ERROR;
1266
1267 cleanup:
1268   gfc_free_expr (e);
1269   gfc_free_expr (real);
1270   gfc_free_expr (imag);
1271   gfc_current_locus = old_loc;
1272
1273   return m;
1274 }
1275
1276
1277 /* Match constants in any of several forms.  Returns nonzero for a
1278    match, zero for no match.  */
1279
1280 match
1281 gfc_match_literal_constant (gfc_expr **result, int signflag)
1282 {
1283   match m;
1284
1285   m = match_complex_constant (result);
1286   if (m != MATCH_NO)
1287     return m;
1288
1289   m = match_string_constant (result);
1290   if (m != MATCH_NO)
1291     return m;
1292
1293   m = match_boz_constant (result);
1294   if (m != MATCH_NO)
1295     return m;
1296
1297   m = match_real_constant (result, signflag);
1298   if (m != MATCH_NO)
1299     return m;
1300
1301   m = match_hollerith_constant (result);
1302   if (m != MATCH_NO)
1303     return m;
1304
1305   m = match_integer_constant (result, signflag);
1306   if (m != MATCH_NO)
1307     return m;
1308
1309   m = match_logical_constant (result);
1310   if (m != MATCH_NO)
1311     return m;
1312
1313   return MATCH_NO;
1314 }
1315
1316
1317 /* Match a single actual argument value.  An actual argument is
1318    usually an expression, but can also be a procedure name.  If the
1319    argument is a single name, it is not always possible to tell
1320    whether the name is a dummy procedure or not.  We treat these cases
1321    by creating an argument that looks like a dummy procedure and
1322    fixing things later during resolution.  */
1323
1324 static match
1325 match_actual_arg (gfc_expr **result)
1326 {
1327   char name[GFC_MAX_SYMBOL_LEN + 1];
1328   gfc_symtree *symtree;
1329   locus where, w;
1330   gfc_expr *e;
1331   int c;
1332
1333   where = gfc_current_locus;
1334
1335   switch (gfc_match_name (name))
1336     {
1337     case MATCH_ERROR:
1338       return MATCH_ERROR;
1339
1340     case MATCH_NO:
1341       break;
1342
1343     case MATCH_YES:
1344       w = gfc_current_locus;
1345       gfc_gobble_whitespace ();
1346       c = gfc_next_char ();
1347       gfc_current_locus = w;
1348
1349       if (c != ',' && c != ')')
1350         break;
1351
1352       if (gfc_find_sym_tree (name, NULL, 1, &symtree))
1353         break;
1354       /* Handle error elsewhere.  */
1355
1356       /* Eliminate a couple of common cases where we know we don't
1357          have a function argument.  */
1358       if (symtree == NULL)
1359         {
1360           gfc_get_sym_tree (name, NULL, &symtree);
1361           gfc_set_sym_referenced (symtree->n.sym);
1362         }
1363       else
1364         {
1365           gfc_symbol *sym;
1366
1367           sym = symtree->n.sym;
1368           gfc_set_sym_referenced (sym);
1369           if (sym->attr.flavor != FL_PROCEDURE
1370               && sym->attr.flavor != FL_UNKNOWN)
1371             break;
1372
1373           /* If the symbol is a function with itself as the result and
1374              is being defined, then we have a variable.  */
1375           if (sym->attr.function && sym->result == sym)
1376             {
1377               if (gfc_current_ns->proc_name == sym
1378                   || (gfc_current_ns->parent != NULL
1379                       && gfc_current_ns->parent->proc_name == sym))
1380                 break;
1381
1382               if (sym->attr.entry
1383                   && (sym->ns == gfc_current_ns
1384                       || sym->ns == gfc_current_ns->parent))
1385                 {
1386                   gfc_entry_list *el = NULL;
1387
1388                   for (el = sym->ns->entries; el; el = el->next)
1389                     if (sym == el->sym)
1390                       break;
1391
1392                   if (el)
1393                     break;
1394                 }
1395             }
1396         }
1397
1398       e = gfc_get_expr ();      /* Leave it unknown for now */
1399       e->symtree = symtree;
1400       e->expr_type = EXPR_VARIABLE;
1401       e->ts.type = BT_PROCEDURE;
1402       e->where = where;
1403
1404       *result = e;
1405       return MATCH_YES;
1406     }
1407
1408   gfc_current_locus = where;
1409   return gfc_match_expr (result);
1410 }
1411
1412
1413 /* Match a keyword argument.  */
1414
1415 static match
1416 match_keyword_arg (gfc_actual_arglist *actual, gfc_actual_arglist *base)
1417 {
1418   char name[GFC_MAX_SYMBOL_LEN + 1];
1419   gfc_actual_arglist *a;
1420   locus name_locus;
1421   match m;
1422
1423   name_locus = gfc_current_locus;
1424   m = gfc_match_name (name);
1425
1426   if (m != MATCH_YES)
1427     goto cleanup;
1428   if (gfc_match_char ('=') != MATCH_YES)
1429     {
1430       m = MATCH_NO;
1431       goto cleanup;
1432     }
1433
1434   m = match_actual_arg (&actual->expr);
1435   if (m != MATCH_YES)
1436     goto cleanup;
1437
1438   /* Make sure this name has not appeared yet.  */
1439
1440   if (name[0] != '\0')
1441     {
1442       for (a = base; a; a = a->next)
1443         if (a->name != NULL && strcmp (a->name, name) == 0)
1444           {
1445             gfc_error ("Keyword '%s' at %C has already appeared in the "
1446                        "current argument list", name);
1447             return MATCH_ERROR;
1448           }
1449     }
1450
1451   actual->name = gfc_get_string (name);
1452   return MATCH_YES;
1453
1454 cleanup:
1455   gfc_current_locus = name_locus;
1456   return m;
1457 }
1458
1459
1460 /* Match an argument list function, such as %VAL.  */
1461
1462 static match
1463 match_arg_list_function (gfc_actual_arglist *result)
1464 {
1465   char name[GFC_MAX_SYMBOL_LEN + 1];
1466   locus old_locus;
1467   match m;
1468
1469   old_locus = gfc_current_locus;
1470
1471   if (gfc_match_char ('%') != MATCH_YES)
1472     {
1473       m = MATCH_NO;
1474       goto cleanup;
1475     }
1476
1477   m = gfc_match ("%n (", name);
1478   if (m != MATCH_YES)
1479     goto cleanup;
1480
1481   if (name[0] != '\0')
1482     {
1483       switch (name[0])
1484         {
1485         case 'l':
1486           if (strncmp (name, "loc", 3) == 0)
1487             {
1488               result->name = "%LOC";
1489               break;
1490             }
1491         case 'r':
1492           if (strncmp (name, "ref", 3) == 0)
1493             {
1494               result->name = "%REF";
1495               break;
1496             }
1497         case 'v':
1498           if (strncmp (name, "val", 3) == 0)
1499             {
1500               result->name = "%VAL";
1501               break;
1502             }
1503         default:
1504           m = MATCH_ERROR;
1505           goto cleanup;
1506         }
1507     }
1508
1509   if (gfc_notify_std (GFC_STD_GNU, "Extension: argument list "
1510                       "function at %C") == FAILURE)
1511     {
1512       m = MATCH_ERROR;
1513       goto cleanup;
1514     }
1515
1516   m = match_actual_arg (&result->expr);
1517   if (m != MATCH_YES)
1518     goto cleanup;
1519
1520   if (gfc_match_char (')') != MATCH_YES)
1521     {
1522       m = MATCH_NO;
1523       goto cleanup;
1524     }
1525
1526   return MATCH_YES;
1527
1528 cleanup:
1529   gfc_current_locus = old_locus;
1530   return m;
1531 }
1532
1533
1534 /* Matches an actual argument list of a function or subroutine, from
1535    the opening parenthesis to the closing parenthesis.  The argument
1536    list is assumed to allow keyword arguments because we don't know if
1537    the symbol associated with the procedure has an implicit interface
1538    or not.  We make sure keywords are unique. If sub_flag is set,
1539    we're matching the argument list of a subroutine.  */
1540
1541 match
1542 gfc_match_actual_arglist (int sub_flag, gfc_actual_arglist **argp)
1543 {
1544   gfc_actual_arglist *head, *tail;
1545   int seen_keyword;
1546   gfc_st_label *label;
1547   locus old_loc;
1548   match m;
1549
1550   *argp = tail = NULL;
1551   old_loc = gfc_current_locus;
1552
1553   seen_keyword = 0;
1554
1555   if (gfc_match_char ('(') == MATCH_NO)
1556     return (sub_flag) ? MATCH_YES : MATCH_NO;
1557
1558   if (gfc_match_char (')') == MATCH_YES)
1559     return MATCH_YES;
1560   head = NULL;
1561
1562   for (;;)
1563     {
1564       if (head == NULL)
1565         head = tail = gfc_get_actual_arglist ();
1566       else
1567         {
1568           tail->next = gfc_get_actual_arglist ();
1569           tail = tail->next;
1570         }
1571
1572       if (sub_flag && gfc_match_char ('*') == MATCH_YES)
1573         {
1574           m = gfc_match_st_label (&label);
1575           if (m == MATCH_NO)
1576             gfc_error ("Expected alternate return label at %C");
1577           if (m != MATCH_YES)
1578             goto cleanup;
1579
1580           tail->label = label;
1581           goto next;
1582         }
1583
1584       /* After the first keyword argument is seen, the following
1585          arguments must also have keywords.  */
1586       if (seen_keyword)
1587         {
1588           m = match_keyword_arg (tail, head);
1589
1590           if (m == MATCH_ERROR)
1591             goto cleanup;
1592           if (m == MATCH_NO)
1593             {
1594               gfc_error ("Missing keyword name in actual argument list at %C");
1595               goto cleanup;
1596             }
1597
1598         }
1599       else
1600         {
1601           /* Try an argument list function, like %VAL.  */
1602           m = match_arg_list_function (tail);
1603           if (m == MATCH_ERROR)
1604             goto cleanup;
1605
1606           /* See if we have the first keyword argument.  */
1607           if (m == MATCH_NO)
1608             {
1609               m = match_keyword_arg (tail, head);
1610               if (m == MATCH_YES)
1611                 seen_keyword = 1;
1612               if (m == MATCH_ERROR)
1613                 goto cleanup;
1614             }
1615
1616           if (m == MATCH_NO)
1617             {
1618               /* Try for a non-keyword argument.  */
1619               m = match_actual_arg (&tail->expr);
1620               if (m == MATCH_ERROR)
1621                 goto cleanup;
1622               if (m == MATCH_NO)
1623                 goto syntax;
1624             }
1625         }
1626
1627
1628     next:
1629       if (gfc_match_char (')') == MATCH_YES)
1630         break;
1631       if (gfc_match_char (',') != MATCH_YES)
1632         goto syntax;
1633     }
1634
1635   *argp = head;
1636   return MATCH_YES;
1637
1638 syntax:
1639   gfc_error ("Syntax error in argument list at %C");
1640
1641 cleanup:
1642   gfc_free_actual_arglist (head);
1643   gfc_current_locus = old_loc;
1644
1645   return MATCH_ERROR;
1646 }
1647
1648
1649 /* Used by match_varspec() to extend the reference list by one
1650    element.  */
1651
1652 static gfc_ref *
1653 extend_ref (gfc_expr *primary, gfc_ref *tail)
1654 {
1655   if (primary->ref == NULL)
1656     primary->ref = tail = gfc_get_ref ();
1657   else
1658     {
1659       if (tail == NULL)
1660         gfc_internal_error ("extend_ref(): Bad tail");
1661       tail->next = gfc_get_ref ();
1662       tail = tail->next;
1663     }
1664
1665   return tail;
1666 }
1667
1668
1669 /* Match any additional specifications associated with the current
1670    variable like member references or substrings.  If equiv_flag is
1671    set we only match stuff that is allowed inside an EQUIVALENCE
1672    statement.  */
1673
1674 static match
1675 match_varspec (gfc_expr *primary, int equiv_flag)
1676 {
1677   char name[GFC_MAX_SYMBOL_LEN + 1];
1678   gfc_ref *substring, *tail;
1679   gfc_component *component;
1680   gfc_symbol *sym = primary->symtree->n.sym;
1681   match m;
1682   bool unknown;
1683
1684   tail = NULL;
1685
1686   gfc_gobble_whitespace ();
1687   if ((equiv_flag && gfc_peek_char () == '(') || sym->attr.dimension)
1688     {
1689       /* In EQUIVALENCE, we don't know yet whether we are seeing
1690          an array, character variable or array of character
1691          variables.  We'll leave the decision till resolve time.  */
1692       tail = extend_ref (primary, tail);
1693       tail->type = REF_ARRAY;
1694
1695       m = gfc_match_array_ref (&tail->u.ar, equiv_flag ? NULL : sym->as,
1696                                equiv_flag);
1697       if (m != MATCH_YES)
1698         return m;
1699
1700       gfc_gobble_whitespace ();
1701       if (equiv_flag && gfc_peek_char () == '(')
1702         {
1703           tail = extend_ref (primary, tail);
1704           tail->type = REF_ARRAY;
1705
1706           m = gfc_match_array_ref (&tail->u.ar, NULL, equiv_flag);
1707           if (m != MATCH_YES)
1708             return m;
1709         }
1710     }
1711
1712   primary->ts = sym->ts;
1713
1714   if (equiv_flag)
1715     return MATCH_YES;
1716
1717   if (sym->ts.type != BT_DERIVED || gfc_match_char ('%') != MATCH_YES)
1718     goto check_substring;
1719
1720   sym = sym->ts.derived;
1721
1722   for (;;)
1723     {
1724       m = gfc_match_name (name);
1725       if (m == MATCH_NO)
1726         gfc_error ("Expected structure component name at %C");
1727       if (m != MATCH_YES)
1728         return MATCH_ERROR;
1729
1730       component = gfc_find_component (sym, name);
1731       if (component == NULL)
1732         return MATCH_ERROR;
1733
1734       tail = extend_ref (primary, tail);
1735       tail->type = REF_COMPONENT;
1736
1737       tail->u.c.component = component;
1738       tail->u.c.sym = sym;
1739
1740       primary->ts = component->ts;
1741
1742       if (component->as != NULL)
1743         {
1744           tail = extend_ref (primary, tail);
1745           tail->type = REF_ARRAY;
1746
1747           m = gfc_match_array_ref (&tail->u.ar, component->as, equiv_flag);
1748           if (m != MATCH_YES)
1749             return m;
1750         }
1751
1752       if (component->ts.type != BT_DERIVED
1753           || gfc_match_char ('%') != MATCH_YES)
1754         break;
1755
1756       sym = component->ts.derived;
1757     }
1758
1759 check_substring:
1760   unknown = false;
1761   if (primary->ts.type == BT_UNKNOWN)
1762     {
1763       if (gfc_get_default_type (sym, sym->ns)->type == BT_CHARACTER)
1764        {
1765          gfc_set_default_type (sym, 0, sym->ns);
1766          primary->ts = sym->ts;
1767          unknown = true;
1768        }
1769     }
1770
1771   if (primary->ts.type == BT_CHARACTER)
1772     {
1773       switch (match_substring (primary->ts.cl, equiv_flag, &substring))
1774         {
1775         case MATCH_YES:
1776           if (tail == NULL)
1777             primary->ref = substring;
1778           else
1779             tail->next = substring;
1780
1781           if (primary->expr_type == EXPR_CONSTANT)
1782             primary->expr_type = EXPR_SUBSTRING;
1783
1784           if (substring)
1785             primary->ts.cl = NULL;
1786
1787           break;
1788
1789         case MATCH_NO:
1790           if (unknown)
1791             gfc_clear_ts (&primary->ts);
1792           break;
1793
1794         case MATCH_ERROR:
1795           return MATCH_ERROR;
1796         }
1797     }
1798
1799   return MATCH_YES;
1800 }
1801
1802
1803 /* Given an expression that is a variable, figure out what the
1804    ultimate variable's type and attribute is, traversing the reference
1805    structures if necessary.
1806
1807    This subroutine is trickier than it looks.  We start at the base
1808    symbol and store the attribute.  Component references load a
1809    completely new attribute.
1810
1811    A couple of rules come into play.  Subobjects of targets are always
1812    targets themselves.  If we see a component that goes through a
1813    pointer, then the expression must also be a target, since the
1814    pointer is associated with something (if it isn't core will soon be
1815    dumped).  If we see a full part or section of an array, the
1816    expression is also an array.
1817
1818    We can have at most one full array reference.  */
1819
1820 symbol_attribute
1821 gfc_variable_attr (gfc_expr *expr, gfc_typespec *ts)
1822 {
1823   int dimension, pointer, allocatable, target;
1824   symbol_attribute attr;
1825   gfc_ref *ref;
1826
1827   if (expr->expr_type != EXPR_VARIABLE)
1828     gfc_internal_error ("gfc_variable_attr(): Expression isn't a variable");
1829
1830   ref = expr->ref;
1831   attr = expr->symtree->n.sym->attr;
1832
1833   dimension = attr.dimension;
1834   pointer = attr.pointer;
1835   allocatable = attr.allocatable;
1836
1837   target = attr.target;
1838   if (pointer)
1839     target = 1;
1840
1841   if (ts != NULL && expr->ts.type == BT_UNKNOWN)
1842     *ts = expr->symtree->n.sym->ts;
1843
1844   for (; ref; ref = ref->next)
1845     switch (ref->type)
1846       {
1847       case REF_ARRAY:
1848
1849         switch (ref->u.ar.type)
1850           {
1851           case AR_FULL:
1852             dimension = 1;
1853             break;
1854
1855           case AR_SECTION:
1856             allocatable = pointer = 0;
1857             dimension = 1;
1858             break;
1859
1860           case AR_ELEMENT:
1861             allocatable = pointer = 0;
1862             break;
1863
1864           case AR_UNKNOWN:
1865             gfc_internal_error ("gfc_variable_attr(): Bad array reference");
1866           }
1867
1868         break;
1869
1870       case REF_COMPONENT:
1871         gfc_get_component_attr (&attr, ref->u.c.component);
1872         if (ts != NULL)
1873           {
1874             *ts = ref->u.c.component->ts;
1875             /* Don't set the string length if a substring reference
1876                follows.  */
1877             if (ts->type == BT_CHARACTER
1878                 && ref->next && ref->next->type == REF_SUBSTRING)
1879                 ts->cl = NULL;
1880           }
1881
1882         pointer = ref->u.c.component->pointer;
1883         allocatable = ref->u.c.component->allocatable;
1884         if (pointer)
1885           target = 1;
1886
1887         break;
1888
1889       case REF_SUBSTRING:
1890         allocatable = pointer = 0;
1891         break;
1892       }
1893
1894   attr.dimension = dimension;
1895   attr.pointer = pointer;
1896   attr.allocatable = allocatable;
1897   attr.target = target;
1898
1899   return attr;
1900 }
1901
1902
1903 /* Return the attribute from a general expression.  */
1904
1905 symbol_attribute
1906 gfc_expr_attr (gfc_expr *e)
1907 {
1908   symbol_attribute attr;
1909
1910   switch (e->expr_type)
1911     {
1912     case EXPR_VARIABLE:
1913       attr = gfc_variable_attr (e, NULL);
1914       break;
1915
1916     case EXPR_FUNCTION:
1917       gfc_clear_attr (&attr);
1918
1919       if (e->value.function.esym != NULL)
1920         attr = e->value.function.esym->result->attr;
1921
1922       /* TODO: NULL() returns pointers.  May have to take care of this
1923          here.  */
1924
1925       break;
1926
1927     default:
1928       gfc_clear_attr (&attr);
1929       break;
1930     }
1931
1932   return attr;
1933 }
1934
1935
1936 /* Match a structure constructor.  The initial symbol has already been
1937    seen.  */
1938
1939 match
1940 gfc_match_structure_constructor (gfc_symbol *sym, gfc_expr **result)
1941 {
1942   gfc_constructor *head, *tail;
1943   gfc_component *comp;
1944   gfc_expr *e;
1945   locus where;
1946   match m;
1947   bool private_comp = false;
1948
1949   head = tail = NULL;
1950
1951   if (gfc_match_char ('(') != MATCH_YES)
1952     goto syntax;
1953
1954   where = gfc_current_locus;
1955
1956   gfc_find_component (sym, NULL);
1957
1958   for (comp = sym->components; comp; comp = comp->next)
1959     {
1960       if (comp->access == ACCESS_PRIVATE)
1961         {
1962           private_comp = true;
1963           break;
1964         }
1965       if (head == NULL)
1966         tail = head = gfc_get_constructor ();
1967       else
1968         {
1969           tail->next = gfc_get_constructor ();
1970           tail = tail->next;
1971         }
1972
1973       m = gfc_match_expr (&tail->expr);
1974       if (m == MATCH_NO)
1975         goto syntax;
1976       if (m == MATCH_ERROR)
1977         goto cleanup;
1978
1979       if (gfc_match_char (',') == MATCH_YES)
1980         {
1981           if (comp->next == NULL)
1982             {
1983               gfc_error ("Too many components in structure constructor at %C");
1984               goto cleanup;
1985             }
1986
1987           continue;
1988         }
1989
1990       break;
1991     }
1992
1993   if (sym->attr.use_assoc
1994       && (sym->component_access == ACCESS_PRIVATE || private_comp))
1995     {
1996       gfc_error ("Structure constructor for '%s' at %C has PRIVATE "
1997                  "components", sym->name);
1998       goto cleanup;
1999     }
2000
2001   if (gfc_match_char (')') != MATCH_YES)
2002     goto syntax;
2003
2004   if (comp && comp->next != NULL)
2005     {
2006       gfc_error ("Too few components in structure constructor at %C");
2007       goto cleanup;
2008     }
2009
2010   e = gfc_get_expr ();
2011
2012   e->expr_type = EXPR_STRUCTURE;
2013
2014   e->ts.type = BT_DERIVED;
2015   e->ts.derived = sym;
2016   e->where = where;
2017
2018   e->value.constructor = head;
2019
2020   *result = e;
2021   return MATCH_YES;
2022
2023 syntax:
2024   gfc_error ("Syntax error in structure constructor at %C");
2025
2026 cleanup:
2027   gfc_free_constructor (head);
2028   return MATCH_ERROR;
2029 }
2030
2031
2032 /* If the symbol is an implicit do loop index and implicitly typed,
2033    it should not be host associated.  Provide a symtree from the
2034    current namespace.  */
2035 static match
2036 check_for_implicit_index (gfc_symtree **st, gfc_symbol **sym)
2037 {
2038   if ((*sym)->attr.flavor == FL_VARIABLE
2039       && (*sym)->ns != gfc_current_ns
2040       && (*sym)->attr.implied_index
2041       && (*sym)->attr.implicit_type
2042       && !(*sym)->attr.use_assoc)
2043     {
2044       int i;
2045       i = gfc_get_sym_tree ((*sym)->name, NULL, st);
2046       if (i)
2047         return MATCH_ERROR;
2048       *sym = (*st)->n.sym;
2049     }
2050   return MATCH_YES;
2051 }
2052
2053
2054 /* Matches a variable name followed by anything that might follow it--
2055    array reference, argument list of a function, etc.  */
2056
2057 match
2058 gfc_match_rvalue (gfc_expr **result)
2059 {
2060   gfc_actual_arglist *actual_arglist;
2061   char name[GFC_MAX_SYMBOL_LEN + 1], argname[GFC_MAX_SYMBOL_LEN + 1];
2062   gfc_state_data *st;
2063   gfc_symbol *sym;
2064   gfc_symtree *symtree;
2065   locus where, old_loc;
2066   gfc_expr *e;
2067   match m, m2;
2068   int i;
2069   gfc_typespec *ts;
2070   bool implicit_char;
2071   gfc_ref *ref;
2072
2073   m = gfc_match_name (name);
2074   if (m != MATCH_YES)
2075     return m;
2076
2077   if (gfc_find_state (COMP_INTERFACE) == SUCCESS
2078       && !gfc_current_ns->has_import_set)
2079     i = gfc_get_sym_tree (name, NULL, &symtree);
2080   else
2081     i = gfc_get_ha_sym_tree (name, &symtree);
2082
2083   if (i)
2084     return MATCH_ERROR;
2085
2086   sym = symtree->n.sym;
2087   e = NULL;
2088   where = gfc_current_locus;
2089
2090   /* If this is an implicit do loop index and implicitly typed,
2091      it should not be host associated.  */
2092   m = check_for_implicit_index (&symtree, &sym);
2093   if (m != MATCH_YES)
2094     return m;
2095
2096   gfc_set_sym_referenced (sym);
2097   sym->attr.implied_index = 0;
2098
2099   if (sym->attr.function && sym->result == sym)
2100     {
2101       /* See if this is a directly recursive function call.  */
2102       gfc_gobble_whitespace ();
2103       if (sym->attr.recursive
2104           && gfc_peek_char () == '('
2105           && gfc_current_ns->proc_name == sym
2106           && !sym->attr.dimension)
2107         {
2108           gfc_error ("'%s' at %C is the name of a recursive function "
2109                      "and so refers to the result variable. Use an "
2110                      "explicit RESULT variable for direct recursion "
2111                      "(12.5.2.1)", sym->name);
2112           return MATCH_ERROR;
2113         }
2114
2115       if (gfc_current_ns->proc_name == sym
2116           || (gfc_current_ns->parent != NULL
2117               && gfc_current_ns->parent->proc_name == sym))
2118         goto variable;
2119
2120       if (sym->attr.entry
2121           && (sym->ns == gfc_current_ns
2122               || sym->ns == gfc_current_ns->parent))
2123         {
2124           gfc_entry_list *el = NULL;
2125           
2126           for (el = sym->ns->entries; el; el = el->next)
2127             if (sym == el->sym)
2128               goto variable;
2129         }
2130     }
2131
2132   if (sym->attr.function || sym->attr.external || sym->attr.intrinsic)
2133     goto function0;
2134
2135   if (sym->attr.generic)
2136     goto generic_function;
2137
2138   switch (sym->attr.flavor)
2139     {
2140     case FL_VARIABLE:
2141     variable:
2142       if (sym->ts.type == BT_UNKNOWN && gfc_peek_char () == '%'
2143           && gfc_get_default_type (sym, sym->ns)->type == BT_DERIVED)
2144         gfc_set_default_type (sym, 0, sym->ns);
2145
2146       e = gfc_get_expr ();
2147
2148       e->expr_type = EXPR_VARIABLE;
2149       e->symtree = symtree;
2150
2151       m = match_varspec (e, 0);
2152       break;
2153
2154     case FL_PARAMETER:
2155       /* A statement of the form "REAL, parameter :: a(0:10) = 1" will
2156          end up here.  Unfortunately, sym->value->expr_type is set to 
2157          EXPR_CONSTANT, and so the if () branch would be followed without
2158          the !sym->as check.  */
2159       if (sym->value && sym->value->expr_type != EXPR_ARRAY && !sym->as)
2160         e = gfc_copy_expr (sym->value);
2161       else
2162         {
2163           e = gfc_get_expr ();
2164           e->expr_type = EXPR_VARIABLE;
2165         }
2166
2167       e->symtree = symtree;
2168       m = match_varspec (e, 0);
2169
2170       if (sym->ts.is_c_interop || sym->ts.is_iso_c)
2171         break;
2172
2173       /* Variable array references to derived type parameters cause
2174          all sorts of headaches in simplification. Treating such
2175          expressions as variable works just fine for all array
2176          references.  */
2177       if (sym->value && sym->ts.type == BT_DERIVED && e->ref)
2178         {
2179           for (ref = e->ref; ref; ref = ref->next)
2180             if (ref->type == REF_ARRAY)
2181               break;
2182
2183           if (ref == NULL || ref->u.ar.type == AR_FULL)
2184             break;
2185
2186           ref = e->ref;
2187           e->ref = NULL;
2188           gfc_free_expr (e);
2189           e = gfc_get_expr ();
2190           e->expr_type = EXPR_VARIABLE;
2191           e->symtree = symtree;
2192           e->ref = ref;
2193         }
2194
2195       break;
2196
2197     case FL_DERIVED:
2198       sym = gfc_use_derived (sym);
2199       if (sym == NULL)
2200         m = MATCH_ERROR;
2201       else
2202         m = gfc_match_structure_constructor (sym, &e);
2203       break;
2204
2205     /* If we're here, then the name is known to be the name of a
2206        procedure, yet it is not sure to be the name of a function.  */
2207     case FL_PROCEDURE:
2208       if (sym->attr.subroutine)
2209         {
2210           gfc_error ("Unexpected use of subroutine name '%s' at %C",
2211                      sym->name);
2212           m = MATCH_ERROR;
2213           break;
2214         }
2215
2216       /* At this point, the name has to be a non-statement function.
2217          If the name is the same as the current function being
2218          compiled, then we have a variable reference (to the function
2219          result) if the name is non-recursive.  */
2220
2221       st = gfc_enclosing_unit (NULL);
2222
2223       if (st != NULL && st->state == COMP_FUNCTION
2224           && st->sym == sym
2225           && !sym->attr.recursive)
2226         {
2227           e = gfc_get_expr ();
2228           e->symtree = symtree;
2229           e->expr_type = EXPR_VARIABLE;
2230
2231           m = match_varspec (e, 0);
2232           break;
2233         }
2234
2235     /* Match a function reference.  */
2236     function0:
2237       m = gfc_match_actual_arglist (0, &actual_arglist);
2238       if (m == MATCH_NO)
2239         {
2240           if (sym->attr.proc == PROC_ST_FUNCTION)
2241             gfc_error ("Statement function '%s' requires argument list at %C",
2242                        sym->name);
2243           else
2244             gfc_error ("Function '%s' requires an argument list at %C",
2245                        sym->name);
2246
2247           m = MATCH_ERROR;
2248           break;
2249         }
2250
2251       if (m != MATCH_YES)
2252         {
2253           m = MATCH_ERROR;
2254           break;
2255         }
2256
2257       gfc_get_ha_sym_tree (name, &symtree);     /* Can't fail */
2258       sym = symtree->n.sym;
2259
2260       e = gfc_get_expr ();
2261       e->symtree = symtree;
2262       e->expr_type = EXPR_FUNCTION;
2263       e->value.function.actual = actual_arglist;
2264       e->where = gfc_current_locus;
2265
2266       if (sym->as != NULL)
2267         e->rank = sym->as->rank;
2268
2269       if (!sym->attr.function
2270           && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2271         {
2272           m = MATCH_ERROR;
2273           break;
2274         }
2275
2276       /* Check here for the existence of at least one argument for the
2277          iso_c_binding functions C_LOC, C_FUNLOC, and C_ASSOCIATED.  The
2278          argument(s) given will be checked in gfc_iso_c_func_interface,
2279          during resolution of the function call.  */
2280       if (sym->attr.is_iso_c == 1
2281           && (sym->from_intmod == INTMOD_ISO_C_BINDING
2282               && (sym->intmod_sym_id == ISOCBINDING_LOC
2283                   || sym->intmod_sym_id == ISOCBINDING_FUNLOC
2284                   || sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)))
2285         {
2286           /* make sure we were given a param */
2287           if (actual_arglist == NULL)
2288             {
2289               gfc_error ("Missing argument to '%s' at %C", sym->name);
2290               m = MATCH_ERROR;
2291               break;
2292             }
2293         }
2294
2295       if (sym->result == NULL)
2296         sym->result = sym;
2297
2298       m = MATCH_YES;
2299       break;
2300
2301     case FL_UNKNOWN:
2302
2303       /* Special case for derived type variables that get their types
2304          via an IMPLICIT statement.  This can't wait for the
2305          resolution phase.  */
2306
2307       if (gfc_peek_char () == '%'
2308           && sym->ts.type == BT_UNKNOWN
2309           && gfc_get_default_type (sym, sym->ns)->type == BT_DERIVED)
2310         gfc_set_default_type (sym, 0, sym->ns);
2311
2312       /* If the symbol has a dimension attribute, the expression is a
2313          variable.  */
2314
2315       if (sym->attr.dimension)
2316         {
2317           if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
2318                               sym->name, NULL) == FAILURE)
2319             {
2320               m = MATCH_ERROR;
2321               break;
2322             }
2323
2324           e = gfc_get_expr ();
2325           e->symtree = symtree;
2326           e->expr_type = EXPR_VARIABLE;
2327           m = match_varspec (e, 0);
2328           break;
2329         }
2330
2331       /* Name is not an array, so we peek to see if a '(' implies a
2332          function call or a substring reference.  Otherwise the
2333          variable is just a scalar.  */
2334
2335       gfc_gobble_whitespace ();
2336       if (gfc_peek_char () != '(')
2337         {
2338           /* Assume a scalar variable */
2339           e = gfc_get_expr ();
2340           e->symtree = symtree;
2341           e->expr_type = EXPR_VARIABLE;
2342
2343           if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
2344                               sym->name, NULL) == FAILURE)
2345             {
2346               m = MATCH_ERROR;
2347               break;
2348             }
2349
2350           /*FIXME:??? match_varspec does set this for us: */
2351           e->ts = sym->ts;
2352           m = match_varspec (e, 0);
2353           break;
2354         }
2355
2356       /* See if this is a function reference with a keyword argument
2357          as first argument. We do this because otherwise a spurious
2358          symbol would end up in the symbol table.  */
2359
2360       old_loc = gfc_current_locus;
2361       m2 = gfc_match (" ( %n =", argname);
2362       gfc_current_locus = old_loc;
2363
2364       e = gfc_get_expr ();
2365       e->symtree = symtree;
2366
2367       if (m2 != MATCH_YES)
2368         {
2369           /* Try to figure out whether we're dealing with a character type.
2370              We're peeking ahead here, because we don't want to call 
2371              match_substring if we're dealing with an implicitly typed
2372              non-character variable.  */
2373           implicit_char = false;
2374           if (sym->ts.type == BT_UNKNOWN)
2375             {
2376               ts = gfc_get_default_type (sym,NULL);
2377               if (ts->type == BT_CHARACTER)
2378                 implicit_char = true;
2379             }
2380
2381           /* See if this could possibly be a substring reference of a name
2382              that we're not sure is a variable yet.  */
2383
2384           if ((implicit_char || sym->ts.type == BT_CHARACTER)
2385               && match_substring (sym->ts.cl, 0, &e->ref) == MATCH_YES)
2386             {
2387
2388               e->expr_type = EXPR_VARIABLE;
2389
2390               if (sym->attr.flavor != FL_VARIABLE
2391                   && gfc_add_flavor (&sym->attr, FL_VARIABLE,
2392                                      sym->name, NULL) == FAILURE)
2393                 {
2394                   m = MATCH_ERROR;
2395                   break;
2396                 }
2397
2398               if (sym->ts.type == BT_UNKNOWN
2399                   && gfc_set_default_type (sym, 1, NULL) == FAILURE)
2400                 {
2401                   m = MATCH_ERROR;
2402                   break;
2403                 }
2404
2405               e->ts = sym->ts;
2406               if (e->ref)
2407                 e->ts.cl = NULL;
2408               m = MATCH_YES;
2409               break;
2410             }
2411         }
2412
2413       /* Give up, assume we have a function.  */
2414
2415       gfc_get_sym_tree (name, NULL, &symtree);  /* Can't fail */
2416       sym = symtree->n.sym;
2417       e->expr_type = EXPR_FUNCTION;
2418
2419       if (!sym->attr.function
2420           && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2421         {
2422           m = MATCH_ERROR;
2423           break;
2424         }
2425
2426       sym->result = sym;
2427
2428       m = gfc_match_actual_arglist (0, &e->value.function.actual);
2429       if (m == MATCH_NO)
2430         gfc_error ("Missing argument list in function '%s' at %C", sym->name);
2431
2432       if (m != MATCH_YES)
2433         {
2434           m = MATCH_ERROR;
2435           break;
2436         }
2437
2438       /* If our new function returns a character, array or structure
2439          type, it might have subsequent references.  */
2440
2441       m = match_varspec (e, 0);
2442       if (m == MATCH_NO)
2443         m = MATCH_YES;
2444
2445       break;
2446
2447     generic_function:
2448       gfc_get_sym_tree (name, NULL, &symtree);  /* Can't fail */
2449
2450       e = gfc_get_expr ();
2451       e->symtree = symtree;
2452       e->expr_type = EXPR_FUNCTION;
2453
2454       m = gfc_match_actual_arglist (0, &e->value.function.actual);
2455       break;
2456
2457     default:
2458       gfc_error ("Symbol at %C is not appropriate for an expression");
2459       return MATCH_ERROR;
2460     }
2461
2462   if (m == MATCH_YES)
2463     {
2464       e->where = where;
2465       *result = e;
2466     }
2467   else
2468     gfc_free_expr (e);
2469
2470   return m;
2471 }
2472
2473
2474 /* Match a variable, ie something that can be assigned to.  This
2475    starts as a symbol, can be a structure component or an array
2476    reference.  It can be a function if the function doesn't have a
2477    separate RESULT variable.  If the symbol has not been previously
2478    seen, we assume it is a variable.
2479
2480    This function is called by two interface functions:
2481    gfc_match_variable, which has host_flag = 1, and
2482    gfc_match_equiv_variable, with host_flag = 0, to restrict the
2483    match of the symbol to the local scope.  */
2484
2485 static match
2486 match_variable (gfc_expr **result, int equiv_flag, int host_flag)
2487 {
2488   gfc_symbol *sym;
2489   gfc_symtree *st;
2490   gfc_expr *expr;
2491   locus where;
2492   match m;
2493
2494   /* Since nothing has any business being an lvalue in a module
2495      specification block, an interface block or a contains section,
2496      we force the changed_symbols mechanism to work by setting
2497      host_flag to 0. This prevents valid symbols that have the name
2498      of keywords, such as 'end', being turned into variables by
2499      failed matching to assignments for, eg., END INTERFACE.  */
2500   if (gfc_current_state () == COMP_MODULE
2501       || gfc_current_state () == COMP_INTERFACE
2502       || gfc_current_state () == COMP_CONTAINS)
2503     host_flag = 0;
2504
2505   m = gfc_match_sym_tree (&st, host_flag);
2506   if (m != MATCH_YES)
2507     return m;
2508   where = gfc_current_locus;
2509
2510   sym = st->n.sym;
2511
2512   /* If this is an implicit do loop index and implicitly typed,
2513      it should not be host associated.  */
2514   m = check_for_implicit_index (&st, &sym);
2515   if (m != MATCH_YES)
2516     return m;
2517
2518   sym->attr.implied_index = 0;
2519
2520   gfc_set_sym_referenced (sym);
2521   switch (sym->attr.flavor)
2522     {
2523     case FL_VARIABLE:
2524       if (sym->attr.protected && sym->attr.use_assoc)
2525         {
2526           gfc_error ("Assigning to PROTECTED variable at %C");
2527           return MATCH_ERROR;
2528         }
2529       break;
2530
2531     case FL_UNKNOWN:
2532       {
2533         sym_flavor flavor = FL_UNKNOWN;
2534
2535         gfc_gobble_whitespace ();
2536
2537         if (sym->attr.external || sym->attr.procedure
2538             || sym->attr.function || sym->attr.subroutine)
2539           flavor = FL_PROCEDURE;
2540
2541         /* If it is not a procedure, is not typed and is host associated,
2542            we cannot give it a flavor yet.  */
2543         else if (sym->ns == gfc_current_ns->parent
2544                    && sym->ts.type == BT_UNKNOWN)
2545           break;
2546
2547         /* These are definitive indicators that this is a variable.  */
2548         else if (gfc_peek_char () != '(' || sym->ts.type != BT_UNKNOWN
2549                  || sym->attr.pointer || sym->as != NULL)
2550           flavor = FL_VARIABLE;
2551
2552         if (flavor != FL_UNKNOWN
2553             && gfc_add_flavor (&sym->attr, flavor, sym->name, NULL) == FAILURE)
2554           return MATCH_ERROR;
2555       }
2556       break;
2557
2558     case FL_PARAMETER:
2559       if (equiv_flag)
2560         gfc_error ("Named constant at %C in an EQUIVALENCE");
2561       else
2562         gfc_error ("Cannot assign to a named constant at %C");
2563       return MATCH_ERROR;
2564       break;
2565
2566     case FL_PROCEDURE:
2567       /* Check for a nonrecursive function result variable.  */
2568       if (sym->attr.function
2569           && !sym->attr.external
2570           && sym->result == sym
2571           && ((sym == gfc_current_ns->proc_name
2572                && sym == gfc_current_ns->proc_name->result)
2573               || (gfc_current_ns->parent
2574                   && sym == gfc_current_ns->parent->proc_name->result)
2575               || (sym->attr.entry
2576                   && sym->ns == gfc_current_ns)
2577               || (sym->attr.entry
2578                   && sym->ns == gfc_current_ns->parent)))
2579         {
2580           /* If a function result is a derived type, then the derived
2581              type may still have to be resolved.  */
2582
2583           if (sym->ts.type == BT_DERIVED
2584               && gfc_use_derived (sym->ts.derived) == NULL)
2585             return MATCH_ERROR;
2586           break;
2587         }
2588
2589       /* Fall through to error */
2590
2591     default:
2592       gfc_error ("'%s' at %C is not a variable", sym->name);
2593       return MATCH_ERROR;
2594     }
2595
2596   /* Special case for derived type variables that get their types
2597      via an IMPLICIT statement.  This can't wait for the
2598      resolution phase.  */
2599
2600     {
2601       gfc_namespace * implicit_ns;
2602
2603       if (gfc_current_ns->proc_name == sym)
2604         implicit_ns = gfc_current_ns;
2605       else
2606         implicit_ns = sym->ns;
2607         
2608       if (gfc_peek_char () == '%'
2609           && sym->ts.type == BT_UNKNOWN
2610           && gfc_get_default_type (sym, implicit_ns)->type == BT_DERIVED)
2611         gfc_set_default_type (sym, 0, implicit_ns);
2612     }
2613
2614   expr = gfc_get_expr ();
2615
2616   expr->expr_type = EXPR_VARIABLE;
2617   expr->symtree = st;
2618   expr->ts = sym->ts;
2619   expr->where = where;
2620
2621   /* Now see if we have to do more.  */
2622   m = match_varspec (expr, equiv_flag);
2623   if (m != MATCH_YES)
2624     {
2625       gfc_free_expr (expr);
2626       return m;
2627     }
2628
2629   *result = expr;
2630   return MATCH_YES;
2631 }
2632
2633
2634 match
2635 gfc_match_variable (gfc_expr **result, int equiv_flag)
2636 {
2637   return match_variable (result, equiv_flag, 1);
2638 }
2639
2640
2641 match
2642 gfc_match_equiv_variable (gfc_expr **result)
2643 {
2644   return match_variable (result, 1, 0);
2645 }
2646