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->ts.is_c_interop = 0;
945   e->ts.is_iso_c = 0;
946   e->where = start_locus;
947
948   e->value.character.string = p = gfc_getmem (length + 1);
949   e->value.character.length = length;
950
951   gfc_current_locus = start_locus;
952   gfc_next_char ();             /* Skip delimiter */
953
954   /* We disable the warning for the following loop as the warning has already
955      been printed in the loop above.  */
956   warn_ampersand = gfc_option.warn_ampersand;
957   gfc_option.warn_ampersand = 0;
958
959   for (i = 0; i < length; i++)
960     *p++ = next_string_char (delimiter);
961
962   *p = '\0';    /* TODO: C-style string is for development/debug purposes.  */
963   gfc_option.warn_ampersand = warn_ampersand;
964
965   if (next_string_char (delimiter) != -1)
966     gfc_internal_error ("match_string_constant(): Delimiter not found");
967
968   if (match_substring (NULL, 0, &e->ref) != MATCH_NO)
969     e->expr_type = EXPR_SUBSTRING;
970
971   *result = e;
972
973   return MATCH_YES;
974
975 no_match:
976   gfc_current_locus = old_locus;
977   return MATCH_NO;
978 }
979
980
981 /* Match a .true. or .false.  */
982
983 static match
984 match_logical_constant (gfc_expr **result)
985 {
986   static mstring logical_ops[] = {
987     minit (".false.", 0),
988     minit (".true.", 1),
989     minit (NULL, -1)
990   };
991
992   gfc_expr *e;
993   int i, kind;
994
995   i = gfc_match_strings (logical_ops);
996   if (i == -1)
997     return MATCH_NO;
998
999   kind = get_kind ();
1000   if (kind == -1)
1001     return MATCH_ERROR;
1002   if (kind == -2)
1003     kind = gfc_default_logical_kind;
1004
1005   if (gfc_validate_kind (BT_LOGICAL, kind, true) < 0)
1006     {
1007       gfc_error ("Bad kind for logical constant at %C");
1008       return MATCH_ERROR;
1009     }
1010
1011   e = gfc_get_expr ();
1012
1013   e->expr_type = EXPR_CONSTANT;
1014   e->value.logical = i;
1015   e->ts.type = BT_LOGICAL;
1016   e->ts.kind = kind;
1017   e->ts.is_c_interop = 0;
1018   e->ts.is_iso_c = 0;
1019   e->where = gfc_current_locus;
1020
1021   *result = e;
1022   return MATCH_YES;
1023 }
1024
1025
1026 /* Match a real or imaginary part of a complex constant that is a
1027    symbolic constant.  */
1028
1029 static match
1030 match_sym_complex_part (gfc_expr **result)
1031 {
1032   char name[GFC_MAX_SYMBOL_LEN + 1];
1033   gfc_symbol *sym;
1034   gfc_expr *e;
1035   match m;
1036
1037   m = gfc_match_name (name);
1038   if (m != MATCH_YES)
1039     return m;
1040
1041   if (gfc_find_symbol (name, NULL, 1, &sym) || sym == NULL)
1042     return MATCH_NO;
1043
1044   if (sym->attr.flavor != FL_PARAMETER)
1045     {
1046       gfc_error ("Expected PARAMETER symbol in complex constant at %C");
1047       return MATCH_ERROR;
1048     }
1049
1050   if (!gfc_numeric_ts (&sym->value->ts))
1051     {
1052       gfc_error ("Numeric PARAMETER required in complex constant at %C");
1053       return MATCH_ERROR;
1054     }
1055
1056   if (sym->value->rank != 0)
1057     {
1058       gfc_error ("Scalar PARAMETER required in complex constant at %C");
1059       return MATCH_ERROR;
1060     }
1061
1062   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PARAMETER symbol in "
1063                       "complex constant at %C") == FAILURE)
1064     return MATCH_ERROR;
1065
1066   switch (sym->value->ts.type)
1067     {
1068     case BT_REAL:
1069       e = gfc_copy_expr (sym->value);
1070       break;
1071
1072     case BT_COMPLEX:
1073       e = gfc_complex2real (sym->value, sym->value->ts.kind);
1074       if (e == NULL)
1075         goto error;
1076       break;
1077
1078     case BT_INTEGER:
1079       e = gfc_int2real (sym->value, gfc_default_real_kind);
1080       if (e == NULL)
1081         goto error;
1082       break;
1083
1084     default:
1085       gfc_internal_error ("gfc_match_sym_complex_part(): Bad type");
1086     }
1087
1088   *result = e;          /* e is a scalar, real, constant expression.  */
1089   return MATCH_YES;
1090
1091 error:
1092   gfc_error ("Error converting PARAMETER constant in complex constant at %C");
1093   return MATCH_ERROR;
1094 }
1095
1096
1097 /* Match a real or imaginary part of a complex number.  */
1098
1099 static match
1100 match_complex_part (gfc_expr **result)
1101 {
1102   match m;
1103
1104   m = match_sym_complex_part (result);
1105   if (m != MATCH_NO)
1106     return m;
1107
1108   m = match_real_constant (result, 1);
1109   if (m != MATCH_NO)
1110     return m;
1111
1112   return match_integer_constant (result, 1);
1113 }
1114
1115
1116 /* Try to match a complex constant.  */
1117
1118 static match
1119 match_complex_constant (gfc_expr **result)
1120 {
1121   gfc_expr *e, *real, *imag;
1122   gfc_error_buf old_error;
1123   gfc_typespec target;
1124   locus old_loc;
1125   int kind;
1126   match m;
1127
1128   old_loc = gfc_current_locus;
1129   real = imag = e = NULL;
1130
1131   m = gfc_match_char ('(');
1132   if (m != MATCH_YES)
1133     return m;
1134
1135   gfc_push_error (&old_error);
1136
1137   m = match_complex_part (&real);
1138   if (m == MATCH_NO)
1139     {
1140       gfc_free_error (&old_error);
1141       goto cleanup;
1142     }
1143
1144   if (gfc_match_char (',') == MATCH_NO)
1145     {
1146       gfc_pop_error (&old_error);
1147       m = MATCH_NO;
1148       goto cleanup;
1149     }
1150
1151   /* If m is error, then something was wrong with the real part and we
1152      assume we have a complex constant because we've seen the ','.  An
1153      ambiguous case here is the start of an iterator list of some
1154      sort. These sort of lists are matched prior to coming here.  */
1155
1156   if (m == MATCH_ERROR)
1157     {
1158       gfc_free_error (&old_error);
1159       goto cleanup;
1160     }
1161   gfc_pop_error (&old_error);
1162
1163   m = match_complex_part (&imag);
1164   if (m == MATCH_NO)
1165     goto syntax;
1166   if (m == MATCH_ERROR)
1167     goto cleanup;
1168
1169   m = gfc_match_char (')');
1170   if (m == MATCH_NO)
1171     {
1172       /* Give the matcher for implied do-loops a chance to run.  This
1173          yields a much saner error message for (/ (i, 4=i, 6) /).  */
1174       if (gfc_peek_char () == '=')
1175         {
1176           m = MATCH_ERROR;
1177           goto cleanup;
1178         }
1179       else
1180     goto syntax;
1181     }
1182
1183   if (m == MATCH_ERROR)
1184     goto cleanup;
1185
1186   /* Decide on the kind of this complex number.  */
1187   if (real->ts.type == BT_REAL)
1188     {
1189       if (imag->ts.type == BT_REAL)
1190         kind = gfc_kind_max (real, imag);
1191       else
1192         kind = real->ts.kind;
1193     }
1194   else
1195     {
1196       if (imag->ts.type == BT_REAL)
1197         kind = imag->ts.kind;
1198       else
1199         kind = gfc_default_real_kind;
1200     }
1201   target.type = BT_REAL;
1202   target.kind = kind;
1203   target.is_c_interop = 0;
1204   target.is_iso_c = 0;
1205
1206   if (real->ts.type != BT_REAL || kind != real->ts.kind)
1207     gfc_convert_type (real, &target, 2);
1208   if (imag->ts.type != BT_REAL || kind != imag->ts.kind)
1209     gfc_convert_type (imag, &target, 2);
1210
1211   e = gfc_convert_complex (real, imag, kind);
1212   e->where = gfc_current_locus;
1213
1214   gfc_free_expr (real);
1215   gfc_free_expr (imag);
1216
1217   *result = e;
1218   return MATCH_YES;
1219
1220 syntax:
1221   gfc_error ("Syntax error in COMPLEX constant at %C");
1222   m = MATCH_ERROR;
1223
1224 cleanup:
1225   gfc_free_expr (e);
1226   gfc_free_expr (real);
1227   gfc_free_expr (imag);
1228   gfc_current_locus = old_loc;
1229
1230   return m;
1231 }
1232
1233
1234 /* Match constants in any of several forms.  Returns nonzero for a
1235    match, zero for no match.  */
1236
1237 match
1238 gfc_match_literal_constant (gfc_expr **result, int signflag)
1239 {
1240   match m;
1241
1242   m = match_complex_constant (result);
1243   if (m != MATCH_NO)
1244     return m;
1245
1246   m = match_string_constant (result);
1247   if (m != MATCH_NO)
1248     return m;
1249
1250   m = match_boz_constant (result);
1251   if (m != MATCH_NO)
1252     return m;
1253
1254   m = match_real_constant (result, signflag);
1255   if (m != MATCH_NO)
1256     return m;
1257
1258   m = match_hollerith_constant (result);
1259   if (m != MATCH_NO)
1260     return m;
1261
1262   m = match_integer_constant (result, signflag);
1263   if (m != MATCH_NO)
1264     return m;
1265
1266   m = match_logical_constant (result);
1267   if (m != MATCH_NO)
1268     return m;
1269
1270   return MATCH_NO;
1271 }
1272
1273
1274 /* Match a single actual argument value.  An actual argument is
1275    usually an expression, but can also be a procedure name.  If the
1276    argument is a single name, it is not always possible to tell
1277    whether the name is a dummy procedure or not.  We treat these cases
1278    by creating an argument that looks like a dummy procedure and
1279    fixing things later during resolution.  */
1280
1281 static match
1282 match_actual_arg (gfc_expr **result)
1283 {
1284   char name[GFC_MAX_SYMBOL_LEN + 1];
1285   gfc_symtree *symtree;
1286   locus where, w;
1287   gfc_expr *e;
1288   int c;
1289
1290   where = gfc_current_locus;
1291
1292   switch (gfc_match_name (name))
1293     {
1294     case MATCH_ERROR:
1295       return MATCH_ERROR;
1296
1297     case MATCH_NO:
1298       break;
1299
1300     case MATCH_YES:
1301       w = gfc_current_locus;
1302       gfc_gobble_whitespace ();
1303       c = gfc_next_char ();
1304       gfc_current_locus = w;
1305
1306       if (c != ',' && c != ')')
1307         break;
1308
1309       if (gfc_find_sym_tree (name, NULL, 1, &symtree))
1310         break;
1311       /* Handle error elsewhere.  */
1312
1313       /* Eliminate a couple of common cases where we know we don't
1314          have a function argument.  */
1315       if (symtree == NULL)
1316         {
1317           gfc_get_sym_tree (name, NULL, &symtree);
1318           gfc_set_sym_referenced (symtree->n.sym);
1319         }
1320       else
1321         {
1322           gfc_symbol *sym;
1323
1324           sym = symtree->n.sym;
1325           gfc_set_sym_referenced (sym);
1326           if (sym->attr.flavor != FL_PROCEDURE
1327               && sym->attr.flavor != FL_UNKNOWN)
1328             break;
1329
1330           /* If the symbol is a function with itself as the result and
1331              is being defined, then we have a variable.  */
1332           if (sym->attr.function && sym->result == sym)
1333             {
1334               if (gfc_current_ns->proc_name == sym
1335                   || (gfc_current_ns->parent != NULL
1336                       && gfc_current_ns->parent->proc_name == sym))
1337                 break;
1338
1339               if (sym->attr.entry
1340                   && (sym->ns == gfc_current_ns
1341                       || sym->ns == gfc_current_ns->parent))
1342                 {
1343                   gfc_entry_list *el = NULL;
1344
1345                   for (el = sym->ns->entries; el; el = el->next)
1346                     if (sym == el->sym)
1347                       break;
1348
1349                   if (el)
1350                     break;
1351                 }
1352             }
1353         }
1354
1355       e = gfc_get_expr ();      /* Leave it unknown for now */
1356       e->symtree = symtree;
1357       e->expr_type = EXPR_VARIABLE;
1358       e->ts.type = BT_PROCEDURE;
1359       e->where = where;
1360
1361       *result = e;
1362       return MATCH_YES;
1363     }
1364
1365   gfc_current_locus = where;
1366   return gfc_match_expr (result);
1367 }
1368
1369
1370 /* Match a keyword argument.  */
1371
1372 static match
1373 match_keyword_arg (gfc_actual_arglist *actual, gfc_actual_arglist *base)
1374 {
1375   char name[GFC_MAX_SYMBOL_LEN + 1];
1376   gfc_actual_arglist *a;
1377   locus name_locus;
1378   match m;
1379
1380   name_locus = gfc_current_locus;
1381   m = gfc_match_name (name);
1382
1383   if (m != MATCH_YES)
1384     goto cleanup;
1385   if (gfc_match_char ('=') != MATCH_YES)
1386     {
1387       m = MATCH_NO;
1388       goto cleanup;
1389     }
1390
1391   m = match_actual_arg (&actual->expr);
1392   if (m != MATCH_YES)
1393     goto cleanup;
1394
1395   /* Make sure this name has not appeared yet.  */
1396
1397   if (name[0] != '\0')
1398     {
1399       for (a = base; a; a = a->next)
1400         if (a->name != NULL && strcmp (a->name, name) == 0)
1401           {
1402             gfc_error ("Keyword '%s' at %C has already appeared in the "
1403                        "current argument list", name);
1404             return MATCH_ERROR;
1405           }
1406     }
1407
1408   actual->name = gfc_get_string (name);
1409   return MATCH_YES;
1410
1411 cleanup:
1412   gfc_current_locus = name_locus;
1413   return m;
1414 }
1415
1416
1417 /* Match an argument list function, such as %VAL.  */
1418
1419 static match
1420 match_arg_list_function (gfc_actual_arglist *result)
1421 {
1422   char name[GFC_MAX_SYMBOL_LEN + 1];
1423   locus old_locus;
1424   match m;
1425
1426   old_locus = gfc_current_locus;
1427
1428   if (gfc_match_char ('%') != MATCH_YES)
1429     {
1430       m = MATCH_NO;
1431       goto cleanup;
1432     }
1433
1434   m = gfc_match ("%n (", name);
1435   if (m != MATCH_YES)
1436     goto cleanup;
1437
1438   if (name[0] != '\0')
1439     {
1440       switch (name[0])
1441         {
1442         case 'l':
1443           if (strncmp (name, "loc", 3) == 0)
1444             {
1445               result->name = "%LOC";
1446               break;
1447             }
1448         case 'r':
1449           if (strncmp (name, "ref", 3) == 0)
1450             {
1451               result->name = "%REF";
1452               break;
1453             }
1454         case 'v':
1455           if (strncmp (name, "val", 3) == 0)
1456             {
1457               result->name = "%VAL";
1458               break;
1459             }
1460         default:
1461           m = MATCH_ERROR;
1462           goto cleanup;
1463         }
1464     }
1465
1466   if (gfc_notify_std (GFC_STD_GNU, "Extension: argument list "
1467                       "function at %C") == FAILURE)
1468     {
1469       m = MATCH_ERROR;
1470       goto cleanup;
1471     }
1472
1473   m = match_actual_arg (&result->expr);
1474   if (m != MATCH_YES)
1475     goto cleanup;
1476
1477   if (gfc_match_char (')') != MATCH_YES)
1478     {
1479       m = MATCH_NO;
1480       goto cleanup;
1481     }
1482
1483   return MATCH_YES;
1484
1485 cleanup:
1486   gfc_current_locus = old_locus;
1487   return m;
1488 }
1489
1490
1491 /* Matches an actual argument list of a function or subroutine, from
1492    the opening parenthesis to the closing parenthesis.  The argument
1493    list is assumed to allow keyword arguments because we don't know if
1494    the symbol associated with the procedure has an implicit interface
1495    or not.  We make sure keywords are unique. If sub_flag is set,
1496    we're matching the argument list of a subroutine.  */
1497
1498 match
1499 gfc_match_actual_arglist (int sub_flag, gfc_actual_arglist **argp)
1500 {
1501   gfc_actual_arglist *head, *tail;
1502   int seen_keyword;
1503   gfc_st_label *label;
1504   locus old_loc;
1505   match m;
1506
1507   *argp = tail = NULL;
1508   old_loc = gfc_current_locus;
1509
1510   seen_keyword = 0;
1511
1512   if (gfc_match_char ('(') == MATCH_NO)
1513     return (sub_flag) ? MATCH_YES : MATCH_NO;
1514
1515   if (gfc_match_char (')') == MATCH_YES)
1516     return MATCH_YES;
1517   head = NULL;
1518
1519   for (;;)
1520     {
1521       if (head == NULL)
1522         head = tail = gfc_get_actual_arglist ();
1523       else
1524         {
1525           tail->next = gfc_get_actual_arglist ();
1526           tail = tail->next;
1527         }
1528
1529       if (sub_flag && gfc_match_char ('*') == MATCH_YES)
1530         {
1531           m = gfc_match_st_label (&label);
1532           if (m == MATCH_NO)
1533             gfc_error ("Expected alternate return label at %C");
1534           if (m != MATCH_YES)
1535             goto cleanup;
1536
1537           tail->label = label;
1538           goto next;
1539         }
1540
1541       /* After the first keyword argument is seen, the following
1542          arguments must also have keywords.  */
1543       if (seen_keyword)
1544         {
1545           m = match_keyword_arg (tail, head);
1546
1547           if (m == MATCH_ERROR)
1548             goto cleanup;
1549           if (m == MATCH_NO)
1550             {
1551               gfc_error ("Missing keyword name in actual argument list at %C");
1552               goto cleanup;
1553             }
1554
1555         }
1556       else
1557         {
1558           /* Try an argument list function, like %VAL.  */
1559           m = match_arg_list_function (tail);
1560           if (m == MATCH_ERROR)
1561             goto cleanup;
1562
1563           /* See if we have the first keyword argument.  */
1564           if (m == MATCH_NO)
1565             {
1566               m = match_keyword_arg (tail, head);
1567               if (m == MATCH_YES)
1568                 seen_keyword = 1;
1569               if (m == MATCH_ERROR)
1570                 goto cleanup;
1571             }
1572
1573           if (m == MATCH_NO)
1574             {
1575               /* Try for a non-keyword argument.  */
1576               m = match_actual_arg (&tail->expr);
1577               if (m == MATCH_ERROR)
1578                 goto cleanup;
1579               if (m == MATCH_NO)
1580                 goto syntax;
1581             }
1582         }
1583
1584
1585     next:
1586       if (gfc_match_char (')') == MATCH_YES)
1587         break;
1588       if (gfc_match_char (',') != MATCH_YES)
1589         goto syntax;
1590     }
1591
1592   *argp = head;
1593   return MATCH_YES;
1594
1595 syntax:
1596   gfc_error ("Syntax error in argument list at %C");
1597
1598 cleanup:
1599   gfc_free_actual_arglist (head);
1600   gfc_current_locus = old_loc;
1601
1602   return MATCH_ERROR;
1603 }
1604
1605
1606 /* Used by match_varspec() to extend the reference list by one
1607    element.  */
1608
1609 static gfc_ref *
1610 extend_ref (gfc_expr *primary, gfc_ref *tail)
1611 {
1612   if (primary->ref == NULL)
1613     primary->ref = tail = gfc_get_ref ();
1614   else
1615     {
1616       if (tail == NULL)
1617         gfc_internal_error ("extend_ref(): Bad tail");
1618       tail->next = gfc_get_ref ();
1619       tail = tail->next;
1620     }
1621
1622   return tail;
1623 }
1624
1625
1626 /* Match any additional specifications associated with the current
1627    variable like member references or substrings.  If equiv_flag is
1628    set we only match stuff that is allowed inside an EQUIVALENCE
1629    statement.  */
1630
1631 static match
1632 match_varspec (gfc_expr *primary, int equiv_flag)
1633 {
1634   char name[GFC_MAX_SYMBOL_LEN + 1];
1635   gfc_ref *substring, *tail;
1636   gfc_component *component;
1637   gfc_symbol *sym = primary->symtree->n.sym;
1638   match m;
1639
1640   tail = NULL;
1641
1642   if ((equiv_flag && gfc_peek_char () == '(') || sym->attr.dimension)
1643     {
1644       /* In EQUIVALENCE, we don't know yet whether we are seeing
1645          an array, character variable or array of character
1646          variables.  We'll leave the decision till resolve time.  */
1647       tail = extend_ref (primary, tail);
1648       tail->type = REF_ARRAY;
1649
1650       m = gfc_match_array_ref (&tail->u.ar, equiv_flag ? NULL : sym->as,
1651                                equiv_flag);
1652       if (m != MATCH_YES)
1653         return m;
1654
1655       if (equiv_flag && gfc_peek_char () == '(')
1656         {
1657           tail = extend_ref (primary, tail);
1658           tail->type = REF_ARRAY;
1659
1660           m = gfc_match_array_ref (&tail->u.ar, NULL, equiv_flag);
1661           if (m != MATCH_YES)
1662             return m;
1663         }
1664     }
1665
1666   primary->ts = sym->ts;
1667
1668   if (equiv_flag)
1669     return MATCH_YES;
1670
1671   if (sym->ts.type != BT_DERIVED || gfc_match_char ('%') != MATCH_YES)
1672     goto check_substring;
1673
1674   sym = sym->ts.derived;
1675
1676   for (;;)
1677     {
1678       m = gfc_match_name (name);
1679       if (m == MATCH_NO)
1680         gfc_error ("Expected structure component name at %C");
1681       if (m != MATCH_YES)
1682         return MATCH_ERROR;
1683
1684       component = gfc_find_component (sym, name);
1685       if (component == NULL)
1686         return MATCH_ERROR;
1687
1688       tail = extend_ref (primary, tail);
1689       tail->type = REF_COMPONENT;
1690
1691       tail->u.c.component = component;
1692       tail->u.c.sym = sym;
1693
1694       primary->ts = component->ts;
1695
1696       if (component->as != NULL)
1697         {
1698           tail = extend_ref (primary, tail);
1699           tail->type = REF_ARRAY;
1700
1701           m = gfc_match_array_ref (&tail->u.ar, component->as, equiv_flag);
1702           if (m != MATCH_YES)
1703             return m;
1704         }
1705
1706       if (component->ts.type != BT_DERIVED
1707           || gfc_match_char ('%') != MATCH_YES)
1708         break;
1709
1710       sym = component->ts.derived;
1711     }
1712
1713 check_substring:
1714   if (primary->ts.type == BT_UNKNOWN)
1715     {
1716       if (gfc_get_default_type (sym, sym->ns)->type == BT_CHARACTER)
1717        {
1718          gfc_set_default_type (sym, 0, sym->ns);
1719          primary->ts = sym->ts;
1720        }
1721     }
1722
1723   if (primary->ts.type == BT_CHARACTER)
1724     {
1725       switch (match_substring (primary->ts.cl, equiv_flag, &substring))
1726         {
1727         case MATCH_YES:
1728           if (tail == NULL)
1729             primary->ref = substring;
1730           else
1731             tail->next = substring;
1732
1733           if (primary->expr_type == EXPR_CONSTANT)
1734             primary->expr_type = EXPR_SUBSTRING;
1735
1736           if (substring)
1737             primary->ts.cl = NULL;
1738
1739           break;
1740
1741         case MATCH_NO:
1742           break;
1743
1744         case MATCH_ERROR:
1745           return MATCH_ERROR;
1746         }
1747     }
1748
1749   return MATCH_YES;
1750 }
1751
1752
1753 /* Given an expression that is a variable, figure out what the
1754    ultimate variable's type and attribute is, traversing the reference
1755    structures if necessary.
1756
1757    This subroutine is trickier than it looks.  We start at the base
1758    symbol and store the attribute.  Component references load a
1759    completely new attribute.
1760
1761    A couple of rules come into play.  Subobjects of targets are always
1762    targets themselves.  If we see a component that goes through a
1763    pointer, then the expression must also be a target, since the
1764    pointer is associated with something (if it isn't core will soon be
1765    dumped).  If we see a full part or section of an array, the
1766    expression is also an array.
1767
1768    We can have at most one full array reference.  */
1769
1770 symbol_attribute
1771 gfc_variable_attr (gfc_expr *expr, gfc_typespec *ts)
1772 {
1773   int dimension, pointer, allocatable, target;
1774   symbol_attribute attr;
1775   gfc_ref *ref;
1776
1777   if (expr->expr_type != EXPR_VARIABLE)
1778     gfc_internal_error ("gfc_variable_attr(): Expression isn't a variable");
1779
1780   ref = expr->ref;
1781   attr = expr->symtree->n.sym->attr;
1782
1783   dimension = attr.dimension;
1784   pointer = attr.pointer;
1785   allocatable = attr.allocatable;
1786
1787   target = attr.target;
1788   if (pointer)
1789     target = 1;
1790
1791   if (ts != NULL && expr->ts.type == BT_UNKNOWN)
1792     *ts = expr->symtree->n.sym->ts;
1793
1794   for (; ref; ref = ref->next)
1795     switch (ref->type)
1796       {
1797       case REF_ARRAY:
1798
1799         switch (ref->u.ar.type)
1800           {
1801           case AR_FULL:
1802             dimension = 1;
1803             break;
1804
1805           case AR_SECTION:
1806             allocatable = pointer = 0;
1807             dimension = 1;
1808             break;
1809
1810           case AR_ELEMENT:
1811             allocatable = pointer = 0;
1812             break;
1813
1814           case AR_UNKNOWN:
1815             gfc_internal_error ("gfc_variable_attr(): Bad array reference");
1816           }
1817
1818         break;
1819
1820       case REF_COMPONENT:
1821         gfc_get_component_attr (&attr, ref->u.c.component);
1822         if (ts != NULL)
1823           {
1824             *ts = ref->u.c.component->ts;
1825             /* Don't set the string length if a substring reference
1826                follows.  */
1827             if (ts->type == BT_CHARACTER
1828                 && ref->next && ref->next->type == REF_SUBSTRING)
1829                 ts->cl = NULL;
1830           }
1831
1832         pointer = ref->u.c.component->pointer;
1833         allocatable = ref->u.c.component->allocatable;
1834         if (pointer)
1835           target = 1;
1836
1837         break;
1838
1839       case REF_SUBSTRING:
1840         allocatable = pointer = 0;
1841         break;
1842       }
1843
1844   attr.dimension = dimension;
1845   attr.pointer = pointer;
1846   attr.allocatable = allocatable;
1847   attr.target = target;
1848
1849   return attr;
1850 }
1851
1852
1853 /* Return the attribute from a general expression.  */
1854
1855 symbol_attribute
1856 gfc_expr_attr (gfc_expr *e)
1857 {
1858   symbol_attribute attr;
1859
1860   switch (e->expr_type)
1861     {
1862     case EXPR_VARIABLE:
1863       attr = gfc_variable_attr (e, NULL);
1864       break;
1865
1866     case EXPR_FUNCTION:
1867       gfc_clear_attr (&attr);
1868
1869       if (e->value.function.esym != NULL)
1870         attr = e->value.function.esym->result->attr;
1871
1872       /* TODO: NULL() returns pointers.  May have to take care of this
1873          here.  */
1874
1875       break;
1876
1877     default:
1878       gfc_clear_attr (&attr);
1879       break;
1880     }
1881
1882   return attr;
1883 }
1884
1885
1886 /* Match a structure constructor.  The initial symbol has already been
1887    seen.  */
1888
1889 match
1890 gfc_match_structure_constructor (gfc_symbol *sym, gfc_expr **result)
1891 {
1892   gfc_constructor *head, *tail;
1893   gfc_component *comp;
1894   gfc_expr *e;
1895   locus where;
1896   match m;
1897   bool private_comp = false;
1898
1899   head = tail = NULL;
1900
1901   if (gfc_match_char ('(') != MATCH_YES)
1902     goto syntax;
1903
1904   where = gfc_current_locus;
1905
1906   gfc_find_component (sym, NULL);
1907
1908   for (comp = sym->components; comp; comp = comp->next)
1909     {
1910       if (comp->access == ACCESS_PRIVATE)
1911         {
1912           private_comp = true;
1913           break;
1914         }
1915       if (head == NULL)
1916         tail = head = gfc_get_constructor ();
1917       else
1918         {
1919           tail->next = gfc_get_constructor ();
1920           tail = tail->next;
1921         }
1922
1923       m = gfc_match_expr (&tail->expr);
1924       if (m == MATCH_NO)
1925         goto syntax;
1926       if (m == MATCH_ERROR)
1927         goto cleanup;
1928
1929       if (gfc_match_char (',') == MATCH_YES)
1930         {
1931           if (comp->next == NULL)
1932             {
1933               gfc_error ("Too many components in structure constructor at %C");
1934               goto cleanup;
1935             }
1936
1937           continue;
1938         }
1939
1940       break;
1941     }
1942
1943   if (sym->attr.use_assoc
1944       && (sym->component_access == ACCESS_PRIVATE || private_comp))
1945     {
1946       gfc_error ("Structure constructor for '%s' at %C has PRIVATE "
1947                  "components", sym->name);
1948       goto cleanup;
1949     }
1950
1951   if (gfc_match_char (')') != MATCH_YES)
1952     goto syntax;
1953
1954   if (comp->next != NULL)
1955     {
1956       gfc_error ("Too few components in structure constructor at %C");
1957       goto cleanup;
1958     }
1959
1960   e = gfc_get_expr ();
1961
1962   e->expr_type = EXPR_STRUCTURE;
1963
1964   e->ts.type = BT_DERIVED;
1965   e->ts.derived = sym;
1966   e->where = where;
1967
1968   e->value.constructor = head;
1969
1970   *result = e;
1971   return MATCH_YES;
1972
1973 syntax:
1974   gfc_error ("Syntax error in structure constructor at %C");
1975
1976 cleanup:
1977   gfc_free_constructor (head);
1978   return MATCH_ERROR;
1979 }
1980
1981
1982 /* If the symbol is an implicit do loop index and implicitly typed,
1983    it should not be host associated.  Provide a symtree from the
1984    current namespace.  */
1985 static match
1986 check_for_implicit_index (gfc_symtree **st, gfc_symbol **sym)
1987 {
1988   if ((*sym)->attr.flavor == FL_VARIABLE
1989       && (*sym)->ns != gfc_current_ns
1990       && (*sym)->attr.implied_index
1991       && (*sym)->attr.implicit_type
1992       && !(*sym)->attr.use_assoc)
1993     {
1994       int i;
1995       i = gfc_get_sym_tree ((*sym)->name, NULL, st);
1996       if (i)
1997         return MATCH_ERROR;
1998       *sym = (*st)->n.sym;
1999     }
2000   return MATCH_YES;
2001 }
2002
2003
2004 /* Matches a variable name followed by anything that might follow it--
2005    array reference, argument list of a function, etc.  */
2006
2007 match
2008 gfc_match_rvalue (gfc_expr **result)
2009 {
2010   gfc_actual_arglist *actual_arglist;
2011   char name[GFC_MAX_SYMBOL_LEN + 1], argname[GFC_MAX_SYMBOL_LEN + 1];
2012   gfc_state_data *st;
2013   gfc_symbol *sym;
2014   gfc_symtree *symtree;
2015   locus where, old_loc;
2016   gfc_expr *e;
2017   match m, m2;
2018   int i;
2019   gfc_typespec *ts;
2020   bool implicit_char;
2021
2022   m = gfc_match_name (name);
2023   if (m != MATCH_YES)
2024     return m;
2025
2026   if (gfc_find_state (COMP_INTERFACE) == SUCCESS
2027       && !gfc_current_ns->has_import_set)
2028     i = gfc_get_sym_tree (name, NULL, &symtree);
2029   else
2030     i = gfc_get_ha_sym_tree (name, &symtree);
2031
2032   if (i)
2033     return MATCH_ERROR;
2034
2035   sym = symtree->n.sym;
2036   e = NULL;
2037   where = gfc_current_locus;
2038
2039   /* If this is an implicit do loop index and implicitly typed,
2040      it should not be host associated.  */
2041   m = check_for_implicit_index (&symtree, &sym);
2042   if (m != MATCH_YES)
2043     return m;
2044
2045   gfc_set_sym_referenced (sym);
2046   sym->attr.implied_index = 0;
2047
2048   if (sym->attr.function && sym->result == sym)
2049     {
2050       /* See if this is a directly recursive function call.  */
2051       gfc_gobble_whitespace ();
2052       if (sym->attr.recursive
2053           && gfc_peek_char () == '('
2054           && gfc_current_ns->proc_name == sym
2055           && !sym->attr.dimension)
2056         {
2057           gfc_error ("'%s' at %C is the name of a recursive function "
2058                      "and so refers to the result variable. Use an "
2059                      "explicit RESULT variable for direct recursion "
2060                      "(12.5.2.1)", sym->name);
2061           return MATCH_ERROR;
2062         }
2063
2064       if (gfc_current_ns->proc_name == sym
2065           || (gfc_current_ns->parent != NULL
2066               && gfc_current_ns->parent->proc_name == sym))
2067         goto variable;
2068
2069       if (sym->attr.entry
2070           && (sym->ns == gfc_current_ns
2071               || sym->ns == gfc_current_ns->parent))
2072         {
2073           gfc_entry_list *el = NULL;
2074           
2075           for (el = sym->ns->entries; el; el = el->next)
2076             if (sym == el->sym)
2077               goto variable;
2078         }
2079     }
2080
2081   if (sym->attr.function || sym->attr.external || sym->attr.intrinsic)
2082     goto function0;
2083
2084   if (sym->attr.generic)
2085     goto generic_function;
2086
2087   switch (sym->attr.flavor)
2088     {
2089     case FL_VARIABLE:
2090     variable:
2091       if (sym->ts.type == BT_UNKNOWN && gfc_peek_char () == '%'
2092           && gfc_get_default_type (sym, sym->ns)->type == BT_DERIVED)
2093         gfc_set_default_type (sym, 0, sym->ns);
2094
2095       e = gfc_get_expr ();
2096
2097       e->expr_type = EXPR_VARIABLE;
2098       e->symtree = symtree;
2099
2100       m = match_varspec (e, 0);
2101       break;
2102
2103     case FL_PARAMETER:
2104       /* A statement of the form "REAL, parameter :: a(0:10) = 1" will
2105          end up here.  Unfortunately, sym->value->expr_type is set to 
2106          EXPR_CONSTANT, and so the if () branch would be followed without
2107          the !sym->as check.  */
2108       if (sym->value && sym->value->expr_type != EXPR_ARRAY && !sym->as)
2109         e = gfc_copy_expr (sym->value);
2110       else
2111         {
2112           e = gfc_get_expr ();
2113           e->expr_type = EXPR_VARIABLE;
2114         }
2115
2116       e->symtree = symtree;
2117       m = match_varspec (e, 0);
2118       break;
2119
2120     case FL_DERIVED:
2121       sym = gfc_use_derived (sym);
2122       if (sym == NULL)
2123         m = MATCH_ERROR;
2124       else
2125         m = gfc_match_structure_constructor (sym, &e);
2126       break;
2127
2128     /* If we're here, then the name is known to be the name of a
2129        procedure, yet it is not sure to be the name of a function.  */
2130     case FL_PROCEDURE:
2131       if (sym->attr.subroutine)
2132         {
2133           gfc_error ("Unexpected use of subroutine name '%s' at %C",
2134                      sym->name);
2135           m = MATCH_ERROR;
2136           break;
2137         }
2138
2139       /* At this point, the name has to be a non-statement function.
2140          If the name is the same as the current function being
2141          compiled, then we have a variable reference (to the function
2142          result) if the name is non-recursive.  */
2143
2144       st = gfc_enclosing_unit (NULL);
2145
2146       if (st != NULL && st->state == COMP_FUNCTION
2147           && st->sym == sym
2148           && !sym->attr.recursive)
2149         {
2150           e = gfc_get_expr ();
2151           e->symtree = symtree;
2152           e->expr_type = EXPR_VARIABLE;
2153
2154           m = match_varspec (e, 0);
2155           break;
2156         }
2157
2158     /* Match a function reference.  */
2159     function0:
2160       m = gfc_match_actual_arglist (0, &actual_arglist);
2161       if (m == MATCH_NO)
2162         {
2163           if (sym->attr.proc == PROC_ST_FUNCTION)
2164             gfc_error ("Statement function '%s' requires argument list at %C",
2165                        sym->name);
2166           else
2167             gfc_error ("Function '%s' requires an argument list at %C",
2168                        sym->name);
2169
2170           m = MATCH_ERROR;
2171           break;
2172         }
2173
2174       if (m != MATCH_YES)
2175         {
2176           m = MATCH_ERROR;
2177           break;
2178         }
2179
2180       gfc_get_ha_sym_tree (name, &symtree);     /* Can't fail */
2181       sym = symtree->n.sym;
2182
2183       e = gfc_get_expr ();
2184       e->symtree = symtree;
2185       e->expr_type = EXPR_FUNCTION;
2186       e->value.function.actual = actual_arglist;
2187       e->where = gfc_current_locus;
2188
2189       if (sym->as != NULL)
2190         e->rank = sym->as->rank;
2191
2192       if (!sym->attr.function
2193           && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2194         {
2195           m = MATCH_ERROR;
2196           break;
2197         }
2198
2199       /* Check here for the existence of at least one argument for the
2200          iso_c_binding functions C_LOC, C_FUNLOC, and C_ASSOCIATED.  The
2201          argument(s) given will be checked in gfc_iso_c_func_interface,
2202          during resolution of the function call.  */
2203       if (sym->attr.is_iso_c == 1
2204           && (sym->from_intmod == INTMOD_ISO_C_BINDING
2205               && (sym->intmod_sym_id == ISOCBINDING_LOC
2206                   || sym->intmod_sym_id == ISOCBINDING_FUNLOC
2207                   || sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)))
2208         {
2209           /* make sure we were given a param */
2210           if (actual_arglist == NULL)
2211             {
2212               gfc_error ("Missing argument to '%s' at %C", sym->name);
2213               m = MATCH_ERROR;
2214               break;
2215             }
2216         }
2217
2218       if (sym->result == NULL)
2219         sym->result = sym;
2220
2221       m = MATCH_YES;
2222       break;
2223
2224     case FL_UNKNOWN:
2225
2226       /* Special case for derived type variables that get their types
2227          via an IMPLICIT statement.  This can't wait for the
2228          resolution phase.  */
2229
2230       if (gfc_peek_char () == '%'
2231           && sym->ts.type == BT_UNKNOWN
2232           && gfc_get_default_type (sym, sym->ns)->type == BT_DERIVED)
2233         gfc_set_default_type (sym, 0, sym->ns);
2234
2235       /* If the symbol has a dimension attribute, the expression is a
2236          variable.  */
2237
2238       if (sym->attr.dimension)
2239         {
2240           if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
2241                               sym->name, NULL) == FAILURE)
2242             {
2243               m = MATCH_ERROR;
2244               break;
2245             }
2246
2247           e = gfc_get_expr ();
2248           e->symtree = symtree;
2249           e->expr_type = EXPR_VARIABLE;
2250           m = match_varspec (e, 0);
2251           break;
2252         }
2253
2254       /* Name is not an array, so we peek to see if a '(' implies a
2255          function call or a substring reference.  Otherwise the
2256          variable is just a scalar.  */
2257
2258       gfc_gobble_whitespace ();
2259       if (gfc_peek_char () != '(')
2260         {
2261           /* Assume a scalar variable */
2262           e = gfc_get_expr ();
2263           e->symtree = symtree;
2264           e->expr_type = EXPR_VARIABLE;
2265
2266           if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
2267                               sym->name, NULL) == FAILURE)
2268             {
2269               m = MATCH_ERROR;
2270               break;
2271             }
2272
2273           /*FIXME:??? match_varspec does set this for us: */
2274           e->ts = sym->ts;
2275           m = match_varspec (e, 0);
2276           break;
2277         }
2278
2279       /* See if this is a function reference with a keyword argument
2280          as first argument. We do this because otherwise a spurious
2281          symbol would end up in the symbol table.  */
2282
2283       old_loc = gfc_current_locus;
2284       m2 = gfc_match (" ( %n =", argname);
2285       gfc_current_locus = old_loc;
2286
2287       e = gfc_get_expr ();
2288       e->symtree = symtree;
2289
2290       if (m2 != MATCH_YES)
2291         {
2292           /* Try to figure out whether we're dealing with a character type.
2293              We're peeking ahead here, because we don't want to call 
2294              match_substring if we're dealing with an implicitly typed
2295              non-character variable.  */
2296           implicit_char = false;
2297           if (sym->ts.type == BT_UNKNOWN)
2298             {
2299               ts = gfc_get_default_type (sym,NULL);
2300               if (ts->type == BT_CHARACTER)
2301                 implicit_char = true;
2302             }
2303
2304           /* See if this could possibly be a substring reference of a name
2305              that we're not sure is a variable yet.  */
2306
2307           if ((implicit_char || sym->ts.type == BT_CHARACTER)
2308               && match_substring (sym->ts.cl, 0, &e->ref) == MATCH_YES)
2309             {
2310
2311               e->expr_type = EXPR_VARIABLE;
2312
2313               if (sym->attr.flavor != FL_VARIABLE
2314                   && gfc_add_flavor (&sym->attr, FL_VARIABLE,
2315                                      sym->name, NULL) == FAILURE)
2316                 {
2317                   m = MATCH_ERROR;
2318                   break;
2319                 }
2320
2321               if (sym->ts.type == BT_UNKNOWN
2322                   && gfc_set_default_type (sym, 1, NULL) == FAILURE)
2323                 {
2324                   m = MATCH_ERROR;
2325                   break;
2326                 }
2327
2328               e->ts = sym->ts;
2329               if (e->ref)
2330                 e->ts.cl = NULL;
2331               m = MATCH_YES;
2332               break;
2333             }
2334         }
2335
2336       /* Give up, assume we have a function.  */
2337
2338       gfc_get_sym_tree (name, NULL, &symtree);  /* Can't fail */
2339       sym = symtree->n.sym;
2340       e->expr_type = EXPR_FUNCTION;
2341
2342       if (!sym->attr.function
2343           && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2344         {
2345           m = MATCH_ERROR;
2346           break;
2347         }
2348
2349       sym->result = sym;
2350
2351       m = gfc_match_actual_arglist (0, &e->value.function.actual);
2352       if (m == MATCH_NO)
2353         gfc_error ("Missing argument list in function '%s' at %C", sym->name);
2354
2355       if (m != MATCH_YES)
2356         {
2357           m = MATCH_ERROR;
2358           break;
2359         }
2360
2361       /* If our new function returns a character, array or structure
2362          type, it might have subsequent references.  */
2363
2364       m = match_varspec (e, 0);
2365       if (m == MATCH_NO)
2366         m = MATCH_YES;
2367
2368       break;
2369
2370     generic_function:
2371       gfc_get_sym_tree (name, NULL, &symtree);  /* Can't fail */
2372
2373       e = gfc_get_expr ();
2374       e->symtree = symtree;
2375       e->expr_type = EXPR_FUNCTION;
2376
2377       m = gfc_match_actual_arglist (0, &e->value.function.actual);
2378       break;
2379
2380     default:
2381       gfc_error ("Symbol at %C is not appropriate for an expression");
2382       return MATCH_ERROR;
2383     }
2384
2385   if (m == MATCH_YES)
2386     {
2387       e->where = where;
2388       *result = e;
2389     }
2390   else
2391     gfc_free_expr (e);
2392
2393   return m;
2394 }
2395
2396
2397 /* Match a variable, ie something that can be assigned to.  This
2398    starts as a symbol, can be a structure component or an array
2399    reference.  It can be a function if the function doesn't have a
2400    separate RESULT variable.  If the symbol has not been previously
2401    seen, we assume it is a variable.
2402
2403    This function is called by two interface functions:
2404    gfc_match_variable, which has host_flag = 1, and
2405    gfc_match_equiv_variable, with host_flag = 0, to restrict the
2406    match of the symbol to the local scope.  */
2407
2408 static match
2409 match_variable (gfc_expr **result, int equiv_flag, int host_flag)
2410 {
2411   gfc_symbol *sym;
2412   gfc_symtree *st;
2413   gfc_expr *expr;
2414   locus where;
2415   match m;
2416
2417   /* Since nothing has any business being an lvalue in a module
2418      specification block, an interface block or a contains section,
2419      we force the changed_symbols mechanism to work by setting
2420      host_flag to 0. This prevents valid symbols that have the name
2421      of keywords, such as 'end', being turned into variables by
2422      failed matching to assignments for, eg., END INTERFACE.  */
2423   if (gfc_current_state () == COMP_MODULE
2424       || gfc_current_state () == COMP_INTERFACE
2425       || gfc_current_state () == COMP_CONTAINS)
2426     host_flag = 0;
2427
2428   m = gfc_match_sym_tree (&st, host_flag);
2429   if (m != MATCH_YES)
2430     return m;
2431   where = gfc_current_locus;
2432
2433   sym = st->n.sym;
2434
2435   /* If this is an implicit do loop index and implicitly typed,
2436      it should not be host associated.  */
2437   m = check_for_implicit_index (&st, &sym);
2438   if (m != MATCH_YES)
2439     return m;
2440
2441   sym->attr.implied_index = 0;
2442
2443   gfc_set_sym_referenced (sym);
2444   switch (sym->attr.flavor)
2445     {
2446     case FL_VARIABLE:
2447       if (sym->attr.protected && sym->attr.use_assoc)
2448         {
2449           gfc_error ("Assigning to PROTECTED variable at %C");
2450           return MATCH_ERROR;
2451         }
2452       break;
2453
2454     case FL_UNKNOWN:
2455       if (gfc_add_flavor (&sym->attr, FL_VARIABLE,
2456                           sym->name, NULL) == FAILURE)
2457         return MATCH_ERROR;
2458       break;
2459
2460     case FL_PARAMETER:
2461       if (equiv_flag)
2462         gfc_error ("Named constant at %C in an EQUIVALENCE");
2463       else
2464         gfc_error ("Cannot assign to a named constant at %C");
2465       return MATCH_ERROR;
2466       break;
2467
2468     case FL_PROCEDURE:
2469       /* Check for a nonrecursive function result */
2470       if (sym->attr.function && (sym->result == sym || sym->attr.entry)
2471           && !sym->attr.external)
2472         {
2473           /* If a function result is a derived type, then the derived
2474              type may still have to be resolved.  */
2475
2476           if (sym->ts.type == BT_DERIVED
2477               && gfc_use_derived (sym->ts.derived) == NULL)
2478             return MATCH_ERROR;
2479           break;
2480         }
2481
2482       /* Fall through to error */
2483
2484     default:
2485       gfc_error ("Expected VARIABLE at %C");
2486       return MATCH_ERROR;
2487     }
2488
2489   /* Special case for derived type variables that get their types
2490      via an IMPLICIT statement.  This can't wait for the
2491      resolution phase.  */
2492
2493     {
2494       gfc_namespace * implicit_ns;
2495
2496       if (gfc_current_ns->proc_name == sym)
2497         implicit_ns = gfc_current_ns;
2498       else
2499         implicit_ns = sym->ns;
2500         
2501       if (gfc_peek_char () == '%'
2502           && sym->ts.type == BT_UNKNOWN
2503           && gfc_get_default_type (sym, implicit_ns)->type == BT_DERIVED)
2504         gfc_set_default_type (sym, 0, implicit_ns);
2505     }
2506
2507   expr = gfc_get_expr ();
2508
2509   expr->expr_type = EXPR_VARIABLE;
2510   expr->symtree = st;
2511   expr->ts = sym->ts;
2512   expr->where = where;
2513
2514   /* Now see if we have to do more.  */
2515   m = match_varspec (expr, equiv_flag);
2516   if (m != MATCH_YES)
2517     {
2518       gfc_free_expr (expr);
2519       return m;
2520     }
2521
2522   *result = expr;
2523   return MATCH_YES;
2524 }
2525
2526
2527 match
2528 gfc_match_variable (gfc_expr **result, int equiv_flag)
2529 {
2530   return match_variable (result, equiv_flag, 1);
2531 }
2532
2533
2534 match
2535 gfc_match_equiv_variable (gfc_expr **result)
2536 {
2537   return match_variable (result, 1, 0);
2538 }
2539