OSDN Git Service

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