OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [gcc/] [fortran/] [primary.c] - Blame information for rev 20

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.