OSDN Git Service

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