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

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [gcc/] [fortran/] [decl.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
/* Declaration statement matcher
2
   Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
3
   Contributed by Andy Vaught
4
 
5
This file is part of GCC.
6
 
7
GCC is free software; you can redistribute it and/or modify it under
8
the terms of the GNU General Public License as published by the Free
9
Software Foundation; either version 2, or (at your option) any later
10
version.
11
 
12
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13
WARRANTY; without even the implied warranty of MERCHANTABILITY or
14
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15
for more details.
16
 
17
You should have received a copy of the GNU General Public License
18
along with GCC; see the file COPYING.  If not, write to the Free
19
Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20
02110-1301, USA.  */
21
 
22
 
23
#include "config.h"
24
#include "system.h"
25
#include "gfortran.h"
26
#include "match.h"
27
#include "parse.h"
28
 
29
 
30
/* This flag is set if an old-style length selector is matched
31
   during a type-declaration statement.  */
32
 
33
static int old_char_selector;
34
 
35
/* When variables acquire types and attributes from a declaration
36
   statement, they get them from the following static variables.  The
37
   first part of a declaration sets these variables and the second
38
   part copies these into symbol structures.  */
39
 
40
static gfc_typespec current_ts;
41
 
42
static symbol_attribute current_attr;
43
static gfc_array_spec *current_as;
44
static int colon_seen;
45
 
46
/* Initializer of the previous enumerator.  */
47
 
48
static gfc_expr *last_initializer;
49
 
50
/* History of all the enumerators is maintained, so that
51
   kind values of all the enumerators could be updated depending
52
   upon the maximum initialized value.  */
53
 
54
typedef struct enumerator_history
55
{
56
  gfc_symbol *sym;
57
  gfc_expr *initializer;
58
  struct enumerator_history *next;
59
}
60
enumerator_history;
61
 
62
/* Header of enum history chain.  */
63
 
64
static enumerator_history *enum_history = NULL;
65
 
66
/* Pointer of enum history node containing largest initializer.  */
67
 
68
static enumerator_history *max_enum = NULL;
69
 
70
/* gfc_new_block points to the symbol of a newly matched block.  */
71
 
72
gfc_symbol *gfc_new_block;
73
 
74
 
75
/********************* DATA statement subroutines *********************/
76
 
77
/* Free a gfc_data_variable structure and everything beneath it.  */
78
 
79
static void
80
free_variable (gfc_data_variable * p)
81
{
82
  gfc_data_variable *q;
83
 
84
  for (; p; p = q)
85
    {
86
      q = p->next;
87
      gfc_free_expr (p->expr);
88
      gfc_free_iterator (&p->iter, 0);
89
      free_variable (p->list);
90
 
91
      gfc_free (p);
92
    }
93
}
94
 
95
 
96
/* Free a gfc_data_value structure and everything beneath it.  */
97
 
98
static void
99
free_value (gfc_data_value * p)
100
{
101
  gfc_data_value *q;
102
 
103
  for (; p; p = q)
104
    {
105
      q = p->next;
106
      gfc_free_expr (p->expr);
107
      gfc_free (p);
108
    }
109
}
110
 
111
 
112
/* Free a list of gfc_data structures.  */
113
 
114
void
115
gfc_free_data (gfc_data * p)
116
{
117
  gfc_data *q;
118
 
119
  for (; p; p = q)
120
    {
121
      q = p->next;
122
 
123
      free_variable (p->var);
124
      free_value (p->value);
125
 
126
      gfc_free (p);
127
    }
128
}
129
 
130
 
131
static match var_element (gfc_data_variable *);
132
 
133
/* Match a list of variables terminated by an iterator and a right
134
   parenthesis.  */
135
 
136
static match
137
var_list (gfc_data_variable * parent)
138
{
139
  gfc_data_variable *tail, var;
140
  match m;
141
 
142
  m = var_element (&var);
143
  if (m == MATCH_ERROR)
144
    return MATCH_ERROR;
145
  if (m == MATCH_NO)
146
    goto syntax;
147
 
148
  tail = gfc_get_data_variable ();
149
  *tail = var;
150
 
151
  parent->list = tail;
152
 
153
  for (;;)
154
    {
155
      if (gfc_match_char (',') != MATCH_YES)
156
        goto syntax;
157
 
158
      m = gfc_match_iterator (&parent->iter, 1);
159
      if (m == MATCH_YES)
160
        break;
161
      if (m == MATCH_ERROR)
162
        return MATCH_ERROR;
163
 
164
      m = var_element (&var);
165
      if (m == MATCH_ERROR)
166
        return MATCH_ERROR;
167
      if (m == MATCH_NO)
168
        goto syntax;
169
 
170
      tail->next = gfc_get_data_variable ();
171
      tail = tail->next;
172
 
173
      *tail = var;
174
    }
175
 
176
  if (gfc_match_char (')') != MATCH_YES)
177
    goto syntax;
178
  return MATCH_YES;
179
 
180
syntax:
181
  gfc_syntax_error (ST_DATA);
182
  return MATCH_ERROR;
183
}
184
 
185
 
186
/* Match a single element in a data variable list, which can be a
187
   variable-iterator list.  */
188
 
189
static match
190
var_element (gfc_data_variable * new)
191
{
192
  match m;
193
  gfc_symbol *sym;
194
 
195
  memset (new, 0, sizeof (gfc_data_variable));
196
 
197
  if (gfc_match_char ('(') == MATCH_YES)
198
    return var_list (new);
199
 
200
  m = gfc_match_variable (&new->expr, 0);
201
  if (m != MATCH_YES)
202
    return m;
203
 
204
  sym = new->expr->symtree->n.sym;
205
 
206
  if (!sym->attr.function && gfc_current_ns->parent && gfc_current_ns->parent == sym->ns)
207
    {
208
      gfc_error ("Host associated variable '%s' may not be in the DATA "
209
                 "statement at %C.", sym->name);
210
      return MATCH_ERROR;
211
    }
212
 
213
  if (gfc_current_state () != COMP_BLOCK_DATA
214
        && sym->attr.in_common
215
        && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
216
                           "common block variable '%s' in DATA statement at %C",
217
                           sym->name) == FAILURE)
218
    return MATCH_ERROR;
219
 
220
  if (gfc_add_data (&sym->attr, sym->name, &new->expr->where) == FAILURE)
221
    return MATCH_ERROR;
222
 
223
  return MATCH_YES;
224
}
225
 
226
 
227
/* Match the top-level list of data variables.  */
228
 
229
static match
230
top_var_list (gfc_data * d)
231
{
232
  gfc_data_variable var, *tail, *new;
233
  match m;
234
 
235
  tail = NULL;
236
 
237
  for (;;)
238
    {
239
      m = var_element (&var);
240
      if (m == MATCH_NO)
241
        goto syntax;
242
      if (m == MATCH_ERROR)
243
        return MATCH_ERROR;
244
 
245
      new = gfc_get_data_variable ();
246
      *new = var;
247
 
248
      if (tail == NULL)
249
        d->var = new;
250
      else
251
        tail->next = new;
252
 
253
      tail = new;
254
 
255
      if (gfc_match_char ('/') == MATCH_YES)
256
        break;
257
      if (gfc_match_char (',') != MATCH_YES)
258
        goto syntax;
259
    }
260
 
261
  return MATCH_YES;
262
 
263
syntax:
264
  gfc_syntax_error (ST_DATA);
265
  return MATCH_ERROR;
266
}
267
 
268
 
269
static match
270
match_data_constant (gfc_expr ** result)
271
{
272
  char name[GFC_MAX_SYMBOL_LEN + 1];
273
  gfc_symbol *sym;
274
  gfc_expr *expr;
275
  match m;
276
 
277
  m = gfc_match_literal_constant (&expr, 1);
278
  if (m == MATCH_YES)
279
    {
280
      *result = expr;
281
      return MATCH_YES;
282
    }
283
 
284
  if (m == MATCH_ERROR)
285
    return MATCH_ERROR;
286
 
287
  m = gfc_match_null (result);
288
  if (m != MATCH_NO)
289
    return m;
290
 
291
  m = gfc_match_name (name);
292
  if (m != MATCH_YES)
293
    return m;
294
 
295
  if (gfc_find_symbol (name, NULL, 1, &sym))
296
    return MATCH_ERROR;
297
 
298
  if (sym == NULL
299
      || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
300
    {
301
      gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
302
                 name);
303
      return MATCH_ERROR;
304
    }
305
  else if (sym->attr.flavor == FL_DERIVED)
306
    return gfc_match_structure_constructor (sym, result);
307
 
308
  *result = gfc_copy_expr (sym->value);
309
  return MATCH_YES;
310
}
311
 
312
 
313
/* Match a list of values in a DATA statement.  The leading '/' has
314
   already been seen at this point.  */
315
 
316
static match
317
top_val_list (gfc_data * data)
318
{
319
  gfc_data_value *new, *tail;
320
  gfc_expr *expr;
321
  const char *msg;
322
  match m;
323
 
324
  tail = NULL;
325
 
326
  for (;;)
327
    {
328
      m = match_data_constant (&expr);
329
      if (m == MATCH_NO)
330
        goto syntax;
331
      if (m == MATCH_ERROR)
332
        return MATCH_ERROR;
333
 
334
      new = gfc_get_data_value ();
335
 
336
      if (tail == NULL)
337
        data->value = new;
338
      else
339
        tail->next = new;
340
 
341
      tail = new;
342
 
343
      if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
344
        {
345
          tail->expr = expr;
346
          tail->repeat = 1;
347
        }
348
      else
349
        {
350
          signed int tmp;
351
          msg = gfc_extract_int (expr, &tmp);
352
          gfc_free_expr (expr);
353
          if (msg != NULL)
354
            {
355
              gfc_error (msg);
356
              return MATCH_ERROR;
357
            }
358
          tail->repeat = tmp;
359
 
360
          m = match_data_constant (&tail->expr);
361
          if (m == MATCH_NO)
362
            goto syntax;
363
          if (m == MATCH_ERROR)
364
            return MATCH_ERROR;
365
        }
366
 
367
      if (gfc_match_char ('/') == MATCH_YES)
368
        break;
369
      if (gfc_match_char (',') == MATCH_NO)
370
        goto syntax;
371
    }
372
 
373
  return MATCH_YES;
374
 
375
syntax:
376
  gfc_syntax_error (ST_DATA);
377
  return MATCH_ERROR;
378
}
379
 
380
 
381
/* Matches an old style initialization.  */
382
 
383
static match
384
match_old_style_init (const char *name)
385
{
386
  match m;
387
  gfc_symtree *st;
388
  gfc_data *newdata;
389
 
390
  /* Set up data structure to hold initializers.  */
391
  gfc_find_sym_tree (name, NULL, 0, &st);
392
 
393
  newdata = gfc_get_data ();
394
  newdata->var = gfc_get_data_variable ();
395
  newdata->var->expr = gfc_get_variable_expr (st);
396
 
397
  /* Match initial value list. This also eats the terminal
398
     '/'.  */
399
  m = top_val_list (newdata);
400
  if (m != MATCH_YES)
401
    {
402
      gfc_free (newdata);
403
      return m;
404
    }
405
 
406
  if (gfc_pure (NULL))
407
    {
408
      gfc_error ("Initialization at %C is not allowed in a PURE procedure");
409
      gfc_free (newdata);
410
      return MATCH_ERROR;
411
    }
412
 
413
  /* Chain in namespace list of DATA initializers.  */
414
  newdata->next = gfc_current_ns->data;
415
  gfc_current_ns->data = newdata;
416
 
417
  return m;
418
}
419
 
420
/* Match the stuff following a DATA statement. If ERROR_FLAG is set,
421
   we are matching a DATA statement and are therefore issuing an error
422
   if we encounter something unexpected, if not, we're trying to match
423
   an old-style initialization expression of the form INTEGER I /2/.  */
424
 
425
match
426
gfc_match_data (void)
427
{
428
  gfc_data *new;
429
  match m;
430
 
431
  for (;;)
432
    {
433
      new = gfc_get_data ();
434
      new->where = gfc_current_locus;
435
 
436
      m = top_var_list (new);
437
      if (m != MATCH_YES)
438
        goto cleanup;
439
 
440
      m = top_val_list (new);
441
      if (m != MATCH_YES)
442
        goto cleanup;
443
 
444
      new->next = gfc_current_ns->data;
445
      gfc_current_ns->data = new;
446
 
447
      if (gfc_match_eos () == MATCH_YES)
448
        break;
449
 
450
      gfc_match_char (',');     /* Optional comma */
451
    }
452
 
453
  if (gfc_pure (NULL))
454
    {
455
      gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
456
      return MATCH_ERROR;
457
    }
458
 
459
  return MATCH_YES;
460
 
461
cleanup:
462
  gfc_free_data (new);
463
  return MATCH_ERROR;
464
}
465
 
466
 
467
/************************ Declaration statements *********************/
468
 
469
/* Match an intent specification.  Since this can only happen after an
470
   INTENT word, a legal intent-spec must follow.  */
471
 
472
static sym_intent
473
match_intent_spec (void)
474
{
475
 
476
  if (gfc_match (" ( in out )") == MATCH_YES)
477
    return INTENT_INOUT;
478
  if (gfc_match (" ( in )") == MATCH_YES)
479
    return INTENT_IN;
480
  if (gfc_match (" ( out )") == MATCH_YES)
481
    return INTENT_OUT;
482
 
483
  gfc_error ("Bad INTENT specification at %C");
484
  return INTENT_UNKNOWN;
485
}
486
 
487
 
488
/* Matches a character length specification, which is either a
489
   specification expression or a '*'.  */
490
 
491
static match
492
char_len_param_value (gfc_expr ** expr)
493
{
494
 
495
  if (gfc_match_char ('*') == MATCH_YES)
496
    {
497
      *expr = NULL;
498
      return MATCH_YES;
499
    }
500
 
501
  return gfc_match_expr (expr);
502
}
503
 
504
 
505
/* A character length is a '*' followed by a literal integer or a
506
   char_len_param_value in parenthesis.  */
507
 
508
static match
509
match_char_length (gfc_expr ** expr)
510
{
511
  int length, cnt;
512
  match m;
513
 
514
  m = gfc_match_char ('*');
515
  if (m != MATCH_YES)
516
    return m;
517
 
518
  /* cnt is unused, here.  */
519
  m = gfc_match_small_literal_int (&length, &cnt);
520
  if (m == MATCH_ERROR)
521
    return m;
522
 
523
  if (m == MATCH_YES)
524
    {
525
      *expr = gfc_int_expr (length);
526
      return m;
527
    }
528
 
529
  if (gfc_match_char ('(') == MATCH_NO)
530
    goto syntax;
531
 
532
  m = char_len_param_value (expr);
533
  if (m == MATCH_ERROR)
534
    return m;
535
  if (m == MATCH_NO)
536
    goto syntax;
537
 
538
  if (gfc_match_char (')') == MATCH_NO)
539
    {
540
      gfc_free_expr (*expr);
541
      *expr = NULL;
542
      goto syntax;
543
    }
544
 
545
  return MATCH_YES;
546
 
547
syntax:
548
  gfc_error ("Syntax error in character length specification at %C");
549
  return MATCH_ERROR;
550
}
551
 
552
 
553
/* Special subroutine for finding a symbol.  Check if the name is found
554
   in the current name space.  If not, and we're compiling a function or
555
   subroutine and the parent compilation unit is an interface, then check
556
   to see if the name we've been given is the name of the interface
557
   (located in another namespace).  */
558
 
559
static int
560
find_special (const char *name, gfc_symbol ** result)
561
{
562
  gfc_state_data *s;
563
  int i;
564
 
565
  i = gfc_get_symbol (name, NULL, result);
566
  if (i==0)
567
    goto end;
568
 
569
  if (gfc_current_state () != COMP_SUBROUTINE
570
      && gfc_current_state () != COMP_FUNCTION)
571
    goto end;
572
 
573
  s = gfc_state_stack->previous;
574
  if (s == NULL)
575
    goto end;
576
 
577
  if (s->state != COMP_INTERFACE)
578
    goto end;
579
  if (s->sym == NULL)
580
    goto end;                  /* Nameless interface */
581
 
582
  if (strcmp (name, s->sym->name) == 0)
583
    {
584
      *result = s->sym;
585
      return 0;
586
    }
587
 
588
end:
589
  return i;
590
}
591
 
592
 
593
/* Special subroutine for getting a symbol node associated with a
594
   procedure name, used in SUBROUTINE and FUNCTION statements.  The
595
   symbol is created in the parent using with symtree node in the
596
   child unit pointing to the symbol.  If the current namespace has no
597
   parent, then the symbol is just created in the current unit.  */
598
 
599
static int
600
get_proc_name (const char *name, gfc_symbol ** result)
601
{
602
  gfc_symtree *st;
603
  gfc_symbol *sym;
604
  int rc;
605
 
606
  if (gfc_current_ns->parent == NULL)
607
    rc = gfc_get_symbol (name, NULL, result);
608
  else
609
    rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
610
 
611
  sym = *result;
612
 
613
  if (sym && !sym->new && gfc_current_state () != COMP_INTERFACE)
614
    {
615
      /* Trap another encompassed procedure with the same name.  All
616
         these conditions are necessary to avoid picking up an entry
617
         whose name clashes with that of the encompassing procedure;
618
         this is handled using gsymbols to register unique,globally
619
         accessible names.  */
620
      if (sym->attr.flavor != 0
621
            && sym->attr.proc != 0
622
            && sym->formal)
623
        gfc_error_now ("Procedure '%s' at %C is already defined at %L",
624
                       name, &sym->declared_at);
625
 
626
      /* Trap declarations of attributes in encompassing scope.  The
627
         signature for this is that ts.kind is set.  Legitimate
628
         references only set ts.type.  */
629
      if (sym->ts.kind != 0
630
            && sym->attr.proc == 0
631
            && gfc_current_ns->parent != NULL
632
            && sym->attr.access == 0)
633
        gfc_error_now ("Procedure '%s' at %C has an explicit interface"
634
                       " and must not have attributes declared at %L",
635
                       name, &sym->declared_at);
636
    }
637
 
638
  if (gfc_current_ns->parent == NULL || *result == NULL)
639
    return rc;
640
 
641
  st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
642
 
643
  st->n.sym = sym;
644
  sym->refs++;
645
 
646
  /* See if the procedure should be a module procedure */
647
 
648
  if (sym->ns->proc_name != NULL
649
      && sym->ns->proc_name->attr.flavor == FL_MODULE
650
      && sym->attr.proc != PROC_MODULE
651
      && gfc_add_procedure (&sym->attr, PROC_MODULE,
652
                            sym->name, NULL) == FAILURE)
653
    rc = 2;
654
 
655
  return rc;
656
}
657
 
658
 
659
/* Function called by variable_decl() that adds a name to the symbol
660
   table.  */
661
 
662
static try
663
build_sym (const char *name, gfc_charlen * cl,
664
           gfc_array_spec ** as, locus * var_locus)
665
{
666
  symbol_attribute attr;
667
  gfc_symbol *sym;
668
 
669
  /* if (find_special (name, &sym)) */
670
  if (gfc_get_symbol (name, NULL, &sym))
671
    return FAILURE;
672
 
673
  /* Start updating the symbol table.  Add basic type attribute
674
     if present.  */
675
  if (current_ts.type != BT_UNKNOWN
676
      &&(sym->attr.implicit_type == 0
677
         || !gfc_compare_types (&sym->ts, &current_ts))
678
      && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
679
    return FAILURE;
680
 
681
  if (sym->ts.type == BT_CHARACTER)
682
    sym->ts.cl = cl;
683
 
684
  /* Add dimension attribute if present.  */
685
  if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
686
    return FAILURE;
687
  *as = NULL;
688
 
689
  /* Add attribute to symbol.  The copy is so that we can reset the
690
     dimension attribute.  */
691
  attr = current_attr;
692
  attr.dimension = 0;
693
 
694
  if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
695
    return FAILURE;
696
 
697
  return SUCCESS;
698
}
699
 
700
/* Set character constant to the given length. The constant will be padded or
701
   truncated.  */
702
 
703
void
704
gfc_set_constant_character_len (int len, gfc_expr * expr)
705
{
706
  char * s;
707
  int slen;
708
 
709
  gcc_assert (expr->expr_type == EXPR_CONSTANT);
710
  gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
711
 
712
  slen = expr->value.character.length;
713
  if (len != slen)
714
    {
715
      s = gfc_getmem (len);
716
      memcpy (s, expr->value.character.string, MIN (len, slen));
717
      if (len > slen)
718
        memset (&s[slen], ' ', len - slen);
719
      gfc_free (expr->value.character.string);
720
      expr->value.character.string = s;
721
      expr->value.character.length = len;
722
    }
723
}
724
 
725
 
726
/* Function to create and update the enumerator history
727
   using the information passed as arguments.
728
   Pointer "max_enum" is also updated, to point to
729
   enum history node containing largest initializer.
730
 
731
   SYM points to the symbol node of enumerator.
732
   INIT points to its enumerator value.   */
733
 
734
static void
735
create_enum_history(gfc_symbol *sym, gfc_expr *init)
736
{
737
  enumerator_history *new_enum_history;
738
  gcc_assert (sym != NULL && init != NULL);
739
 
740
  new_enum_history = gfc_getmem (sizeof (enumerator_history));
741
 
742
  new_enum_history->sym = sym;
743
  new_enum_history->initializer = init;
744
  new_enum_history->next = NULL;
745
 
746
  if (enum_history == NULL)
747
    {
748
      enum_history = new_enum_history;
749
      max_enum = enum_history;
750
    }
751
  else
752
    {
753
      new_enum_history->next = enum_history;
754
      enum_history = new_enum_history;
755
 
756
      if (mpz_cmp (max_enum->initializer->value.integer,
757
                   new_enum_history->initializer->value.integer) < 0)
758
        max_enum = new_enum_history;
759
    }
760
}
761
 
762
 
763
/* Function to free enum kind history.  */
764
 
765
void
766
gfc_free_enum_history(void)
767
{
768
  enumerator_history *current = enum_history;
769
  enumerator_history *next;
770
 
771
  while (current != NULL)
772
    {
773
      next = current->next;
774
      gfc_free (current);
775
      current = next;
776
    }
777
  max_enum = NULL;
778
  enum_history = NULL;
779
}
780
 
781
 
782
/* Function called by variable_decl() that adds an initialization
783
   expression to a symbol.  */
784
 
785
static try
786
add_init_expr_to_sym (const char *name, gfc_expr ** initp,
787
                      locus * var_locus)
788
{
789
  symbol_attribute attr;
790
  gfc_symbol *sym;
791
  gfc_expr *init;
792
 
793
  init = *initp;
794
  if (find_special (name, &sym))
795
    return FAILURE;
796
 
797
  attr = sym->attr;
798
 
799
  /* If this symbol is confirming an implicit parameter type,
800
     then an initialization expression is not allowed.  */
801
  if (attr.flavor == FL_PARAMETER
802
      && sym->value != NULL
803
      && *initp != NULL)
804
    {
805
      gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
806
                 sym->name);
807
      return FAILURE;
808
    }
809
 
810
  if (attr.in_common
811
      && !attr.data
812
      && *initp != NULL)
813
    {
814
      gfc_error ("Initializer not allowed for COMMON variable '%s' at %C",
815
                 sym->name);
816
      return FAILURE;
817
    }
818
 
819
  if (init == NULL)
820
    {
821
      /* An initializer is required for PARAMETER declarations.  */
822
      if (attr.flavor == FL_PARAMETER)
823
        {
824
          gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
825
          return FAILURE;
826
        }
827
    }
828
  else
829
    {
830
      /* If a variable appears in a DATA block, it cannot have an
831
         initializer.  */
832
      if (sym->attr.data)
833
        {
834
          gfc_error
835
            ("Variable '%s' at %C with an initializer already appears "
836
             "in a DATA statement", sym->name);
837
          return FAILURE;
838
        }
839
 
840
      /* Check if the assignment can happen. This has to be put off
841
         until later for a derived type variable.  */
842
      if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
843
          && gfc_check_assign_symbol (sym, init) == FAILURE)
844
        return FAILURE;
845
 
846
      if (sym->ts.type == BT_CHARACTER && sym->ts.cl)
847
        {
848
          /* Update symbol character length according initializer.  */
849
          if (sym->ts.cl->length == NULL)
850
            {
851
              /* If there are multiple CHARACTER variables declared on
852
                 the same line, we don't want them to share the same
853
                length.  */
854
              sym->ts.cl = gfc_get_charlen ();
855
              sym->ts.cl->next = gfc_current_ns->cl_list;
856
              gfc_current_ns->cl_list = sym->ts.cl;
857
 
858
              if (init->expr_type == EXPR_CONSTANT)
859
                sym->ts.cl->length =
860
                        gfc_int_expr (init->value.character.length);
861
              else if (init->expr_type == EXPR_ARRAY)
862
                sym->ts.cl->length = gfc_copy_expr (init->ts.cl->length);
863
            }
864
          /* Update initializer character length according symbol.  */
865
          else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
866
            {
867
              int len = mpz_get_si (sym->ts.cl->length->value.integer);
868
              gfc_constructor * p;
869
 
870
              if (init->expr_type == EXPR_CONSTANT)
871
                gfc_set_constant_character_len (len, init);
872
              else if (init->expr_type == EXPR_ARRAY)
873
                {
874
                  gfc_free_expr (init->ts.cl->length);
875
                  init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
876
                  for (p = init->value.constructor; p; p = p->next)
877
                    gfc_set_constant_character_len (len, p->expr);
878
                }
879
            }
880
        }
881
 
882
      /* Add initializer.  Make sure we keep the ranks sane.  */
883
      if (sym->attr.dimension && init->rank == 0)
884
        init->rank = sym->as->rank;
885
 
886
      sym->value = init;
887
      *initp = NULL;
888
    }
889
 
890
  /* Maintain enumerator history.  */
891
  if (gfc_current_state () == COMP_ENUM)
892
    create_enum_history (sym, init);
893
 
894
  return SUCCESS;
895
}
896
 
897
 
898
/* Function called by variable_decl() that adds a name to a structure
899
   being built.  */
900
 
901
static try
902
build_struct (const char *name, gfc_charlen * cl, gfc_expr ** init,
903
              gfc_array_spec ** as)
904
{
905
  gfc_component *c;
906
 
907
  /* If the current symbol is of the same derived type that we're
908
     constructing, it must have the pointer attribute.  */
909
  if (current_ts.type == BT_DERIVED
910
      && current_ts.derived == gfc_current_block ()
911
      && current_attr.pointer == 0)
912
    {
913
      gfc_error ("Component at %C must have the POINTER attribute");
914
      return FAILURE;
915
    }
916
 
917
  if (gfc_current_block ()->attr.pointer
918
      && (*as)->rank != 0)
919
    {
920
      if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
921
        {
922
          gfc_error ("Array component of structure at %C must have explicit "
923
                     "or deferred shape");
924
          return FAILURE;
925
        }
926
    }
927
 
928
  if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
929
    return FAILURE;
930
 
931
  c->ts = current_ts;
932
  c->ts.cl = cl;
933
  gfc_set_component_attr (c, &current_attr);
934
 
935
  c->initializer = *init;
936
  *init = NULL;
937
 
938
  c->as = *as;
939
  if (c->as != NULL)
940
    c->dimension = 1;
941
  *as = NULL;
942
 
943
  /* Check array components.  */
944
  if (!c->dimension)
945
    return SUCCESS;
946
 
947
  if (c->pointer)
948
    {
949
      if (c->as->type != AS_DEFERRED)
950
        {
951
          gfc_error ("Pointer array component of structure at %C "
952
                     "must have a deferred shape");
953
          return FAILURE;
954
        }
955
    }
956
  else
957
    {
958
      if (c->as->type != AS_EXPLICIT)
959
        {
960
          gfc_error
961
            ("Array component of structure at %C must have an explicit "
962
             "shape");
963
          return FAILURE;
964
        }
965
    }
966
 
967
  return SUCCESS;
968
}
969
 
970
 
971
/* Match a 'NULL()', and possibly take care of some side effects.  */
972
 
973
match
974
gfc_match_null (gfc_expr ** result)
975
{
976
  gfc_symbol *sym;
977
  gfc_expr *e;
978
  match m;
979
 
980
  m = gfc_match (" null ( )");
981
  if (m != MATCH_YES)
982
    return m;
983
 
984
  /* The NULL symbol now has to be/become an intrinsic function.  */
985
  if (gfc_get_symbol ("null", NULL, &sym))
986
    {
987
      gfc_error ("NULL() initialization at %C is ambiguous");
988
      return MATCH_ERROR;
989
    }
990
 
991
  gfc_intrinsic_symbol (sym);
992
 
993
  if (sym->attr.proc != PROC_INTRINSIC
994
      && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
995
                             sym->name, NULL) == FAILURE
996
          || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
997
    return MATCH_ERROR;
998
 
999
  e = gfc_get_expr ();
1000
  e->where = gfc_current_locus;
1001
  e->expr_type = EXPR_NULL;
1002
  e->ts.type = BT_UNKNOWN;
1003
 
1004
  *result = e;
1005
 
1006
  return MATCH_YES;
1007
}
1008
 
1009
 
1010
/* Match a variable name with an optional initializer.  When this
1011
   subroutine is called, a variable is expected to be parsed next.
1012
   Depending on what is happening at the moment, updates either the
1013
   symbol table or the current interface.  */
1014
 
1015
static match
1016
variable_decl (int elem)
1017
{
1018
  char name[GFC_MAX_SYMBOL_LEN + 1];
1019
  gfc_expr *initializer, *char_len;
1020
  gfc_array_spec *as;
1021
  gfc_array_spec *cp_as; /* Extra copy for Cray Pointees.  */
1022
  gfc_charlen *cl;
1023
  locus var_locus;
1024
  match m;
1025
  try t;
1026
  gfc_symbol *sym;
1027
  locus old_locus;
1028
 
1029
  initializer = NULL;
1030
  as = NULL;
1031
  cp_as = NULL;
1032
  old_locus = gfc_current_locus;
1033
 
1034
  /* When we get here, we've just matched a list of attributes and
1035
     maybe a type and a double colon.  The next thing we expect to see
1036
     is the name of the symbol.  */
1037
  m = gfc_match_name (name);
1038
  if (m != MATCH_YES)
1039
    goto cleanup;
1040
 
1041
  var_locus = gfc_current_locus;
1042
 
1043
  /* Now we could see the optional array spec. or character length.  */
1044
  m = gfc_match_array_spec (&as);
1045
  if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1046
    cp_as = gfc_copy_array_spec (as);
1047
  else if (m == MATCH_ERROR)
1048
    goto cleanup;
1049
 
1050
  if (m == MATCH_NO)
1051
    as = gfc_copy_array_spec (current_as);
1052
  else if (gfc_current_state () == COMP_ENUM)
1053
    {
1054
      gfc_error ("Enumerator cannot be array at %C");
1055
      gfc_free_enum_history ();
1056
      m = MATCH_ERROR;
1057
      goto cleanup;
1058
    }
1059
 
1060
 
1061
  char_len = NULL;
1062
  cl = NULL;
1063
 
1064
  if (current_ts.type == BT_CHARACTER)
1065
    {
1066
      switch (match_char_length (&char_len))
1067
        {
1068
        case MATCH_YES:
1069
          cl = gfc_get_charlen ();
1070
          cl->next = gfc_current_ns->cl_list;
1071
          gfc_current_ns->cl_list = cl;
1072
 
1073
          cl->length = char_len;
1074
          break;
1075
 
1076
        /* Non-constant lengths need to be copied after the first
1077
           element.  */
1078
        case MATCH_NO:
1079
          if (elem > 1 && current_ts.cl->length
1080
                && current_ts.cl->length->expr_type != EXPR_CONSTANT)
1081
            {
1082
              cl = gfc_get_charlen ();
1083
              cl->next = gfc_current_ns->cl_list;
1084
              gfc_current_ns->cl_list = cl;
1085
              cl->length = gfc_copy_expr (current_ts.cl->length);
1086
            }
1087
          else
1088
            cl = current_ts.cl;
1089
 
1090
          break;
1091
 
1092
        case MATCH_ERROR:
1093
          goto cleanup;
1094
        }
1095
    }
1096
 
1097
  /*  If this symbol has already shown up in a Cray Pointer declaration,
1098
      then we want to set the type & bail out. */
1099
  if (gfc_option.flag_cray_pointer)
1100
    {
1101
      gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1102
      if (sym != NULL && sym->attr.cray_pointee)
1103
        {
1104
          sym->ts.type = current_ts.type;
1105
          sym->ts.kind = current_ts.kind;
1106
          sym->ts.cl = cl;
1107
          sym->ts.derived = current_ts.derived;
1108
          m = MATCH_YES;
1109
 
1110
          /* Check to see if we have an array specification.  */
1111
          if (cp_as != NULL)
1112
            {
1113
              if (sym->as != NULL)
1114
                {
1115
                  gfc_error ("Duplicate array spec for Cray pointee at %C.");
1116
                  gfc_free_array_spec (cp_as);
1117
                  m = MATCH_ERROR;
1118
                  goto cleanup;
1119
                }
1120
              else
1121
                {
1122
                  if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1123
                    gfc_internal_error ("Couldn't set pointee array spec.");
1124
 
1125
                  /* Fix the array spec.  */
1126
                  m = gfc_mod_pointee_as (sym->as);
1127
                  if (m == MATCH_ERROR)
1128
                    goto cleanup;
1129
                }
1130
            }
1131
          goto cleanup;
1132
        }
1133
      else
1134
        {
1135
          gfc_free_array_spec (cp_as);
1136
        }
1137
    }
1138
 
1139
 
1140
  /* OK, we've successfully matched the declaration.  Now put the
1141
     symbol in the current namespace, because it might be used in the
1142
     optional initialization expression for this symbol, e.g. this is
1143
     perfectly legal:
1144
 
1145
     integer, parameter :: i = huge(i)
1146
 
1147
     This is only true for parameters or variables of a basic type.
1148
     For components of derived types, it is not true, so we don't
1149
     create a symbol for those yet.  If we fail to create the symbol,
1150
     bail out.  */
1151
  if (gfc_current_state () != COMP_DERIVED
1152
      && build_sym (name, cl, &as, &var_locus) == FAILURE)
1153
    {
1154
      m = MATCH_ERROR;
1155
      goto cleanup;
1156
    }
1157
 
1158
  /* In functions that have a RESULT variable defined, the function
1159
     name always refers to function calls.  Therefore, the name is
1160
     not allowed to appear in specification statements.  */
1161
  if (gfc_current_state () == COMP_FUNCTION
1162
      && gfc_current_block () != NULL
1163
      && gfc_current_block ()->result != NULL
1164
      && gfc_current_block ()->result != gfc_current_block ()
1165
      && strcmp (gfc_current_block ()->name, name) == 0)
1166
    {
1167
      gfc_error ("Function name '%s' not allowed at %C", name);
1168
      m = MATCH_ERROR;
1169
      goto cleanup;
1170
    }
1171
 
1172
  /* We allow old-style initializations of the form
1173
       integer i /2/, j(4) /3*3, 1/
1174
     (if no colon has been seen). These are different from data
1175
     statements in that initializers are only allowed to apply to the
1176
     variable immediately preceding, i.e.
1177
       integer i, j /1, 2/
1178
     is not allowed. Therefore we have to do some work manually, that
1179
     could otherwise be left to the matchers for DATA statements.  */
1180
 
1181
  if (!colon_seen && gfc_match (" /") == MATCH_YES)
1182
    {
1183
      if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1184
                          "initialization at %C") == FAILURE)
1185
        return MATCH_ERROR;
1186
 
1187
      return match_old_style_init (name);
1188
    }
1189
 
1190
  /* The double colon must be present in order to have initializers.
1191
     Otherwise the statement is ambiguous with an assignment statement.  */
1192
  if (colon_seen)
1193
    {
1194
      if (gfc_match (" =>") == MATCH_YES)
1195
        {
1196
 
1197
          if (!current_attr.pointer)
1198
            {
1199
              gfc_error ("Initialization at %C isn't for a pointer variable");
1200
              m = MATCH_ERROR;
1201
              goto cleanup;
1202
            }
1203
 
1204
          m = gfc_match_null (&initializer);
1205
          if (m == MATCH_NO)
1206
            {
1207
              gfc_error ("Pointer initialization requires a NULL() at %C");
1208
              m = MATCH_ERROR;
1209
            }
1210
 
1211
          if (gfc_pure (NULL))
1212
            {
1213
              gfc_error
1214
                ("Initialization of pointer at %C is not allowed in a "
1215
                 "PURE procedure");
1216
              m = MATCH_ERROR;
1217
            }
1218
 
1219
          if (m != MATCH_YES)
1220
            goto cleanup;
1221
 
1222
        }
1223
      else if (gfc_match_char ('=') == MATCH_YES)
1224
        {
1225
          if (current_attr.pointer)
1226
            {
1227
              gfc_error
1228
                ("Pointer initialization at %C requires '=>', not '='");
1229
              m = MATCH_ERROR;
1230
              goto cleanup;
1231
            }
1232
 
1233
          m = gfc_match_init_expr (&initializer);
1234
          if (m == MATCH_NO)
1235
            {
1236
              gfc_error ("Expected an initialization expression at %C");
1237
              m = MATCH_ERROR;
1238
            }
1239
 
1240
          if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1241
            {
1242
              gfc_error
1243
                ("Initialization of variable at %C is not allowed in a "
1244
                 "PURE procedure");
1245
              m = MATCH_ERROR;
1246
            }
1247
 
1248
          if (m != MATCH_YES)
1249
            goto cleanup;
1250
        }
1251
    }
1252
 
1253
  /* Check if we are parsing an enumeration and if the current enumerator
1254
     variable has an initializer or not. If it does not have an
1255
     initializer, the initialization value of the previous enumerator
1256
     (stored in last_initializer) is incremented by 1 and is used to
1257
     initialize the current enumerator.  */
1258
  if (gfc_current_state () == COMP_ENUM)
1259
    {
1260
      if (initializer == NULL)
1261
        initializer = gfc_enum_initializer (last_initializer, old_locus);
1262
 
1263
      if (initializer == NULL || initializer->ts.type != BT_INTEGER)
1264
        {
1265
          gfc_error("ENUMERATOR %L not initialized with integer expression",
1266
                    &var_locus);
1267
          m = MATCH_ERROR;
1268
          gfc_free_enum_history ();
1269
          goto cleanup;
1270
        }
1271
 
1272
      /* Store this current initializer, for the next enumerator
1273
         variable to be parsed.  */
1274
      last_initializer = initializer;
1275
    }
1276
 
1277
  /* Add the initializer.  Note that it is fine if initializer is
1278
     NULL here, because we sometimes also need to check if a
1279
     declaration *must* have an initialization expression.  */
1280
  if (gfc_current_state () != COMP_DERIVED)
1281
    t = add_init_expr_to_sym (name, &initializer, &var_locus);
1282
  else
1283
    {
1284
      if (current_ts.type == BT_DERIVED && !current_attr.pointer
1285
          && !initializer)
1286
        initializer = gfc_default_initializer (&current_ts);
1287
      t = build_struct (name, cl, &initializer, &as);
1288
    }
1289
 
1290
  m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1291
 
1292
cleanup:
1293
  /* Free stuff up and return.  */
1294
  gfc_free_expr (initializer);
1295
  gfc_free_array_spec (as);
1296
 
1297
  return m;
1298
}
1299
 
1300
 
1301
/* Match an extended-f77 kind specification.  */
1302
 
1303
match
1304
gfc_match_old_kind_spec (gfc_typespec * ts)
1305
{
1306
  match m;
1307
  int original_kind, cnt;
1308
 
1309
  if (gfc_match_char ('*') != MATCH_YES)
1310
    return MATCH_NO;
1311
 
1312
  /* cnt is unused, here.  */
1313
  m = gfc_match_small_literal_int (&ts->kind, &cnt);
1314
  if (m != MATCH_YES)
1315
    return MATCH_ERROR;
1316
 
1317
  original_kind = ts->kind;
1318
 
1319
  /* Massage the kind numbers for complex types.  */
1320
  if (ts->type == BT_COMPLEX)
1321
    {
1322
      if (ts->kind % 2)
1323
        {
1324
          gfc_error ("Old-style type declaration %s*%d not supported at %C",
1325
                     gfc_basic_typename (ts->type), original_kind);
1326
          return MATCH_ERROR;
1327
        }
1328
      ts->kind /= 2;
1329
    }
1330
 
1331
  if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1332
    {
1333
      gfc_error ("Old-style type declaration %s*%d not supported at %C",
1334
                 gfc_basic_typename (ts->type), original_kind);
1335
      return MATCH_ERROR;
1336
    }
1337
 
1338
  if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1339
                      gfc_basic_typename (ts->type), original_kind) == FAILURE)
1340
    return MATCH_ERROR;
1341
 
1342
  return MATCH_YES;
1343
}
1344
 
1345
 
1346
/* Match a kind specification.  Since kinds are generally optional, we
1347
   usually return MATCH_NO if something goes wrong.  If a "kind="
1348
   string is found, then we know we have an error.  */
1349
 
1350
match
1351
gfc_match_kind_spec (gfc_typespec * ts)
1352
{
1353
  locus where;
1354
  gfc_expr *e;
1355
  match m, n;
1356
  const char *msg;
1357
 
1358
  m = MATCH_NO;
1359
  e = NULL;
1360
 
1361
  where = gfc_current_locus;
1362
 
1363
  if (gfc_match_char ('(') == MATCH_NO)
1364
    return MATCH_NO;
1365
 
1366
  /* Also gobbles optional text.  */
1367
  if (gfc_match (" kind = ") == MATCH_YES)
1368
    m = MATCH_ERROR;
1369
 
1370
  n = gfc_match_init_expr (&e);
1371
  if (n == MATCH_NO)
1372
    gfc_error ("Expected initialization expression at %C");
1373
  if (n != MATCH_YES)
1374
    return MATCH_ERROR;
1375
 
1376
  if (e->rank != 0)
1377
    {
1378
      gfc_error ("Expected scalar initialization expression at %C");
1379
      m = MATCH_ERROR;
1380
      goto no_match;
1381
    }
1382
 
1383
  msg = gfc_extract_int (e, &ts->kind);
1384
  if (msg != NULL)
1385
    {
1386
      gfc_error (msg);
1387
      m = MATCH_ERROR;
1388
      goto no_match;
1389
    }
1390
 
1391
  gfc_free_expr (e);
1392
  e = NULL;
1393
 
1394
  if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1395
    {
1396
      gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1397
                 gfc_basic_typename (ts->type));
1398
 
1399
      m = MATCH_ERROR;
1400
      goto no_match;
1401
    }
1402
 
1403
  if (gfc_match_char (')') != MATCH_YES)
1404
    {
1405
      gfc_error ("Missing right paren at %C");
1406
      goto no_match;
1407
    }
1408
 
1409
  return MATCH_YES;
1410
 
1411
no_match:
1412
  gfc_free_expr (e);
1413
  gfc_current_locus = where;
1414
  return m;
1415
}
1416
 
1417
 
1418
/* Match the various kind/length specifications in a CHARACTER
1419
   declaration.  We don't return MATCH_NO.  */
1420
 
1421
static match
1422
match_char_spec (gfc_typespec * ts)
1423
{
1424
  int i, kind, seen_length;
1425
  gfc_charlen *cl;
1426
  gfc_expr *len;
1427
  match m;
1428
 
1429
  kind = gfc_default_character_kind;
1430
  len = NULL;
1431
  seen_length = 0;
1432
 
1433
  /* Try the old-style specification first.  */
1434
  old_char_selector = 0;
1435
 
1436
  m = match_char_length (&len);
1437
  if (m != MATCH_NO)
1438
    {
1439
      if (m == MATCH_YES)
1440
        old_char_selector = 1;
1441
      seen_length = 1;
1442
      goto done;
1443
    }
1444
 
1445
  m = gfc_match_char ('(');
1446
  if (m != MATCH_YES)
1447
    {
1448
      m = MATCH_YES;    /* character without length is a single char */
1449
      goto done;
1450
    }
1451
 
1452
  /* Try the weird case:  ( KIND = <int> [ , LEN = <len-param> ] )   */
1453
  if (gfc_match (" kind =") == MATCH_YES)
1454
    {
1455
      m = gfc_match_small_int (&kind);
1456
      if (m == MATCH_ERROR)
1457
        goto done;
1458
      if (m == MATCH_NO)
1459
        goto syntax;
1460
 
1461
      if (gfc_match (" , len =") == MATCH_NO)
1462
        goto rparen;
1463
 
1464
      m = char_len_param_value (&len);
1465
      if (m == MATCH_NO)
1466
        goto syntax;
1467
      if (m == MATCH_ERROR)
1468
        goto done;
1469
      seen_length = 1;
1470
 
1471
      goto rparen;
1472
    }
1473
 
1474
  /* Try to match ( LEN = <len-param> ) or ( LEN = <len-param>, KIND = <int> )  */
1475
  if (gfc_match (" len =") == MATCH_YES)
1476
    {
1477
      m = char_len_param_value (&len);
1478
      if (m == MATCH_NO)
1479
        goto syntax;
1480
      if (m == MATCH_ERROR)
1481
        goto done;
1482
      seen_length = 1;
1483
 
1484
      if (gfc_match_char (')') == MATCH_YES)
1485
        goto done;
1486
 
1487
      if (gfc_match (" , kind =") != MATCH_YES)
1488
        goto syntax;
1489
 
1490
      gfc_match_small_int (&kind);
1491
 
1492
      if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1493
        {
1494
          gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1495
          return MATCH_YES;
1496
        }
1497
 
1498
      goto rparen;
1499
    }
1500
 
1501
  /* Try to match   ( <len-param> ) or ( <len-param> , [ KIND = ] <int> )  */
1502
  m = char_len_param_value (&len);
1503
  if (m == MATCH_NO)
1504
    goto syntax;
1505
  if (m == MATCH_ERROR)
1506
    goto done;
1507
  seen_length = 1;
1508
 
1509
  m = gfc_match_char (')');
1510
  if (m == MATCH_YES)
1511
    goto done;
1512
 
1513
  if (gfc_match_char (',') != MATCH_YES)
1514
    goto syntax;
1515
 
1516
  gfc_match (" kind =");        /* Gobble optional text */
1517
 
1518
  m = gfc_match_small_int (&kind);
1519
  if (m == MATCH_ERROR)
1520
    goto done;
1521
  if (m == MATCH_NO)
1522
    goto syntax;
1523
 
1524
rparen:
1525
  /* Require a right-paren at this point.  */
1526
  m = gfc_match_char (')');
1527
  if (m == MATCH_YES)
1528
    goto done;
1529
 
1530
syntax:
1531
  gfc_error ("Syntax error in CHARACTER declaration at %C");
1532
  m = MATCH_ERROR;
1533
 
1534
done:
1535
  if (m == MATCH_YES && gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1536
    {
1537
      gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1538
      m = MATCH_ERROR;
1539
    }
1540
 
1541
  if (m != MATCH_YES)
1542
    {
1543
      gfc_free_expr (len);
1544
      return m;
1545
    }
1546
 
1547
  /* Do some final massaging of the length values.  */
1548
  cl = gfc_get_charlen ();
1549
  cl->next = gfc_current_ns->cl_list;
1550
  gfc_current_ns->cl_list = cl;
1551
 
1552
  if (seen_length == 0)
1553
    cl->length = gfc_int_expr (1);
1554
  else
1555
    {
1556
      if (len == NULL || gfc_extract_int (len, &i) != NULL || i >= 0)
1557
        cl->length = len;
1558
      else
1559
        {
1560
          gfc_free_expr (len);
1561
          cl->length = gfc_int_expr (0);
1562
        }
1563
    }
1564
 
1565
  ts->cl = cl;
1566
  ts->kind = kind;
1567
 
1568
  return MATCH_YES;
1569
}
1570
 
1571
 
1572
/* Matches a type specification.  If successful, sets the ts structure
1573
   to the matched specification.  This is necessary for FUNCTION and
1574
   IMPLICIT statements.
1575
 
1576
   If implicit_flag is nonzero, then we don't check for the optional
1577
   kind specification.  Not doing so is needed for matching an IMPLICIT
1578
   statement correctly.  */
1579
 
1580
static match
1581
match_type_spec (gfc_typespec * ts, int implicit_flag)
1582
{
1583
  char name[GFC_MAX_SYMBOL_LEN + 1];
1584
  gfc_symbol *sym;
1585
  match m;
1586
  int c;
1587
 
1588
  gfc_clear_ts (ts);
1589
 
1590
  if (gfc_match (" byte") == MATCH_YES)
1591
    {
1592
      if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C")
1593
          == FAILURE)
1594
        return MATCH_ERROR;
1595
 
1596
      if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
1597
        {
1598
          gfc_error ("BYTE type used at %C "
1599
                     "is not available on the target machine");
1600
          return MATCH_ERROR;
1601
        }
1602
 
1603
      ts->type = BT_INTEGER;
1604
      ts->kind = 1;
1605
      return MATCH_YES;
1606
    }
1607
 
1608
  if (gfc_match (" integer") == MATCH_YES)
1609
    {
1610
      ts->type = BT_INTEGER;
1611
      ts->kind = gfc_default_integer_kind;
1612
      goto get_kind;
1613
    }
1614
 
1615
  if (gfc_match (" character") == MATCH_YES)
1616
    {
1617
      ts->type = BT_CHARACTER;
1618
      if (implicit_flag == 0)
1619
        return match_char_spec (ts);
1620
      else
1621
        return MATCH_YES;
1622
    }
1623
 
1624
  if (gfc_match (" real") == MATCH_YES)
1625
    {
1626
      ts->type = BT_REAL;
1627
      ts->kind = gfc_default_real_kind;
1628
      goto get_kind;
1629
    }
1630
 
1631
  if (gfc_match (" double precision") == MATCH_YES)
1632
    {
1633
      ts->type = BT_REAL;
1634
      ts->kind = gfc_default_double_kind;
1635
      return MATCH_YES;
1636
    }
1637
 
1638
  if (gfc_match (" complex") == MATCH_YES)
1639
    {
1640
      ts->type = BT_COMPLEX;
1641
      ts->kind = gfc_default_complex_kind;
1642
      goto get_kind;
1643
    }
1644
 
1645
  if (gfc_match (" double complex") == MATCH_YES)
1646
    {
1647
      if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
1648
                          "conform to the Fortran 95 standard") == FAILURE)
1649
        return MATCH_ERROR;
1650
 
1651
      ts->type = BT_COMPLEX;
1652
      ts->kind = gfc_default_double_kind;
1653
      return MATCH_YES;
1654
    }
1655
 
1656
  if (gfc_match (" logical") == MATCH_YES)
1657
    {
1658
      ts->type = BT_LOGICAL;
1659
      ts->kind = gfc_default_logical_kind;
1660
      goto get_kind;
1661
    }
1662
 
1663
  m = gfc_match (" type ( %n )", name);
1664
  if (m != MATCH_YES)
1665
    return m;
1666
 
1667
  /* Search for the name but allow the components to be defined later.  */
1668
  if (gfc_get_ha_symbol (name, &sym))
1669
    {
1670
      gfc_error ("Type name '%s' at %C is ambiguous", name);
1671
      return MATCH_ERROR;
1672
    }
1673
 
1674
  if (sym->attr.flavor != FL_DERIVED
1675
      && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
1676
    return MATCH_ERROR;
1677
 
1678
  ts->type = BT_DERIVED;
1679
  ts->kind = 0;
1680
  ts->derived = sym;
1681
 
1682
  return MATCH_YES;
1683
 
1684
get_kind:
1685
  /* For all types except double, derived and character, look for an
1686
     optional kind specifier.  MATCH_NO is actually OK at this point.  */
1687
  if (implicit_flag == 1)
1688
    return MATCH_YES;
1689
 
1690
  if (gfc_current_form == FORM_FREE)
1691
    {
1692
      c = gfc_peek_char();
1693
      if (!gfc_is_whitespace(c) && c != '*' && c != '('
1694
         && c != ':' && c != ',')
1695
       return MATCH_NO;
1696
    }
1697
 
1698
  m = gfc_match_kind_spec (ts);
1699
  if (m == MATCH_NO && ts->type != BT_CHARACTER)
1700
    m = gfc_match_old_kind_spec (ts);
1701
 
1702
  if (m == MATCH_NO)
1703
    m = MATCH_YES;              /* No kind specifier found.  */
1704
 
1705
  return m;
1706
}
1707
 
1708
 
1709
/* Match an IMPLICIT NONE statement.  Actually, this statement is
1710
   already matched in parse.c, or we would not end up here in the
1711
   first place.  So the only thing we need to check, is if there is
1712
   trailing garbage.  If not, the match is successful.  */
1713
 
1714
match
1715
gfc_match_implicit_none (void)
1716
{
1717
 
1718
  return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
1719
}
1720
 
1721
 
1722
/* Match the letter range(s) of an IMPLICIT statement.  */
1723
 
1724
static match
1725
match_implicit_range (void)
1726
{
1727
  int c, c1, c2, inner;
1728
  locus cur_loc;
1729
 
1730
  cur_loc = gfc_current_locus;
1731
 
1732
  gfc_gobble_whitespace ();
1733
  c = gfc_next_char ();
1734
  if (c != '(')
1735
    {
1736
      gfc_error ("Missing character range in IMPLICIT at %C");
1737
      goto bad;
1738
    }
1739
 
1740
  inner = 1;
1741
  while (inner)
1742
    {
1743
      gfc_gobble_whitespace ();
1744
      c1 = gfc_next_char ();
1745
      if (!ISALPHA (c1))
1746
        goto bad;
1747
 
1748
      gfc_gobble_whitespace ();
1749
      c = gfc_next_char ();
1750
 
1751
      switch (c)
1752
        {
1753
        case ')':
1754
          inner = 0;             /* Fall through */
1755
 
1756
        case ',':
1757
          c2 = c1;
1758
          break;
1759
 
1760
        case '-':
1761
          gfc_gobble_whitespace ();
1762
          c2 = gfc_next_char ();
1763
          if (!ISALPHA (c2))
1764
            goto bad;
1765
 
1766
          gfc_gobble_whitespace ();
1767
          c = gfc_next_char ();
1768
 
1769
          if ((c != ',') && (c != ')'))
1770
            goto bad;
1771
          if (c == ')')
1772
            inner = 0;
1773
 
1774
          break;
1775
 
1776
        default:
1777
          goto bad;
1778
        }
1779
 
1780
      if (c1 > c2)
1781
        {
1782
          gfc_error ("Letters must be in alphabetic order in "
1783
                     "IMPLICIT statement at %C");
1784
          goto bad;
1785
        }
1786
 
1787
      /* See if we can add the newly matched range to the pending
1788
         implicits from this IMPLICIT statement.  We do not check for
1789
         conflicts with whatever earlier IMPLICIT statements may have
1790
         set.  This is done when we've successfully finished matching
1791
         the current one.  */
1792
      if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
1793
        goto bad;
1794
    }
1795
 
1796
  return MATCH_YES;
1797
 
1798
bad:
1799
  gfc_syntax_error (ST_IMPLICIT);
1800
 
1801
  gfc_current_locus = cur_loc;
1802
  return MATCH_ERROR;
1803
}
1804
 
1805
 
1806
/* Match an IMPLICIT statement, storing the types for
1807
   gfc_set_implicit() if the statement is accepted by the parser.
1808
   There is a strange looking, but legal syntactic construction
1809
   possible.  It looks like:
1810
 
1811
     IMPLICIT INTEGER (a-b) (c-d)
1812
 
1813
   This is legal if "a-b" is a constant expression that happens to
1814
   equal one of the legal kinds for integers.  The real problem
1815
   happens with an implicit specification that looks like:
1816
 
1817
     IMPLICIT INTEGER (a-b)
1818
 
1819
   In this case, a typespec matcher that is "greedy" (as most of the
1820
   matchers are) gobbles the character range as a kindspec, leaving
1821
   nothing left.  We therefore have to go a bit more slowly in the
1822
   matching process by inhibiting the kindspec checking during
1823
   typespec matching and checking for a kind later.  */
1824
 
1825
match
1826
gfc_match_implicit (void)
1827
{
1828
  gfc_typespec ts;
1829
  locus cur_loc;
1830
  int c;
1831
  match m;
1832
 
1833
  /* We don't allow empty implicit statements.  */
1834
  if (gfc_match_eos () == MATCH_YES)
1835
    {
1836
      gfc_error ("Empty IMPLICIT statement at %C");
1837
      return MATCH_ERROR;
1838
    }
1839
 
1840
  do
1841
    {
1842
      /* First cleanup.  */
1843
      gfc_clear_new_implicit ();
1844
 
1845
      /* A basic type is mandatory here.  */
1846
      m = match_type_spec (&ts, 1);
1847
      if (m == MATCH_ERROR)
1848
        goto error;
1849
      if (m == MATCH_NO)
1850
        goto syntax;
1851
 
1852
      cur_loc = gfc_current_locus;
1853
      m = match_implicit_range ();
1854
 
1855
      if (m == MATCH_YES)
1856
        {
1857
          /* We may have <TYPE> (<RANGE>).  */
1858
          gfc_gobble_whitespace ();
1859
          c = gfc_next_char ();
1860
          if ((c == '\n') || (c == ','))
1861
            {
1862
              /* Check for CHARACTER with no length parameter.  */
1863
              if (ts.type == BT_CHARACTER && !ts.cl)
1864
                {
1865
                  ts.kind = gfc_default_character_kind;
1866
                  ts.cl = gfc_get_charlen ();
1867
                  ts.cl->next = gfc_current_ns->cl_list;
1868
                  gfc_current_ns->cl_list = ts.cl;
1869
                  ts.cl->length = gfc_int_expr (1);
1870
                }
1871
 
1872
              /* Record the Successful match.  */
1873
              if (gfc_merge_new_implicit (&ts) != SUCCESS)
1874
                return MATCH_ERROR;
1875
              continue;
1876
            }
1877
 
1878
          gfc_current_locus = cur_loc;
1879
        }
1880
 
1881
      /* Discard the (incorrectly) matched range.  */
1882
      gfc_clear_new_implicit ();
1883
 
1884
      /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>).  */
1885
      if (ts.type == BT_CHARACTER)
1886
        m = match_char_spec (&ts);
1887
      else
1888
        {
1889
          m = gfc_match_kind_spec (&ts);
1890
          if (m == MATCH_NO)
1891
            {
1892
              m = gfc_match_old_kind_spec (&ts);
1893
              if (m == MATCH_ERROR)
1894
                goto error;
1895
              if (m == MATCH_NO)
1896
                goto syntax;
1897
            }
1898
        }
1899
      if (m == MATCH_ERROR)
1900
        goto error;
1901
 
1902
      m = match_implicit_range ();
1903
      if (m == MATCH_ERROR)
1904
        goto error;
1905
      if (m == MATCH_NO)
1906
        goto syntax;
1907
 
1908
      gfc_gobble_whitespace ();
1909
      c = gfc_next_char ();
1910
      if ((c != '\n') && (c != ','))
1911
        goto syntax;
1912
 
1913
      if (gfc_merge_new_implicit (&ts) != SUCCESS)
1914
        return MATCH_ERROR;
1915
    }
1916
  while (c == ',');
1917
 
1918
  return MATCH_YES;
1919
 
1920
syntax:
1921
  gfc_syntax_error (ST_IMPLICIT);
1922
 
1923
error:
1924
  return MATCH_ERROR;
1925
}
1926
 
1927
 
1928
/* Matches an attribute specification including array specs.  If
1929
   successful, leaves the variables current_attr and current_as
1930
   holding the specification.  Also sets the colon_seen variable for
1931
   later use by matchers associated with initializations.
1932
 
1933
   This subroutine is a little tricky in the sense that we don't know
1934
   if we really have an attr-spec until we hit the double colon.
1935
   Until that time, we can only return MATCH_NO.  This forces us to
1936
   check for duplicate specification at this level.  */
1937
 
1938
static match
1939
match_attr_spec (void)
1940
{
1941
 
1942
  /* Modifiers that can exist in a type statement.  */
1943
  typedef enum
1944
  { GFC_DECL_BEGIN = 0,
1945
    DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
1946
    DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
1947
    DECL_PARAMETER, DECL_POINTER, DECL_PRIVATE, DECL_PUBLIC, DECL_SAVE,
1948
    DECL_TARGET, DECL_COLON, DECL_NONE,
1949
    GFC_DECL_END /* Sentinel */
1950
  }
1951
  decl_types;
1952
 
1953
/* GFC_DECL_END is the sentinel, index starts at 0.  */
1954
#define NUM_DECL GFC_DECL_END
1955
 
1956
  static mstring decls[] = {
1957
    minit (", allocatable", DECL_ALLOCATABLE),
1958
    minit (", dimension", DECL_DIMENSION),
1959
    minit (", external", DECL_EXTERNAL),
1960
    minit (", intent ( in )", DECL_IN),
1961
    minit (", intent ( out )", DECL_OUT),
1962
    minit (", intent ( in out )", DECL_INOUT),
1963
    minit (", intrinsic", DECL_INTRINSIC),
1964
    minit (", optional", DECL_OPTIONAL),
1965
    minit (", parameter", DECL_PARAMETER),
1966
    minit (", pointer", DECL_POINTER),
1967
    minit (", private", DECL_PRIVATE),
1968
    minit (", public", DECL_PUBLIC),
1969
    minit (", save", DECL_SAVE),
1970
    minit (", target", DECL_TARGET),
1971
    minit ("::", DECL_COLON),
1972
    minit (NULL, DECL_NONE)
1973
  };
1974
 
1975
  locus start, seen_at[NUM_DECL];
1976
  int seen[NUM_DECL];
1977
  decl_types d;
1978
  const char *attr;
1979
  match m;
1980
  try t;
1981
 
1982
  gfc_clear_attr (&current_attr);
1983
  start = gfc_current_locus;
1984
 
1985
  current_as = NULL;
1986
  colon_seen = 0;
1987
 
1988
  /* See if we get all of the keywords up to the final double colon.  */
1989
  for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
1990
    seen[d] = 0;
1991
 
1992
  for (;;)
1993
    {
1994
      d = (decl_types) gfc_match_strings (decls);
1995
      if (d == DECL_NONE || d == DECL_COLON)
1996
        break;
1997
 
1998
      if (gfc_current_state () == COMP_ENUM)
1999
        {
2000
          gfc_error ("Enumerator cannot have attributes %C");
2001
          return MATCH_ERROR;
2002
        }
2003
 
2004
      seen[d]++;
2005
      seen_at[d] = gfc_current_locus;
2006
 
2007
      if (d == DECL_DIMENSION)
2008
        {
2009
          m = gfc_match_array_spec (&current_as);
2010
 
2011
          if (m == MATCH_NO)
2012
            {
2013
              gfc_error ("Missing dimension specification at %C");
2014
              m = MATCH_ERROR;
2015
            }
2016
 
2017
          if (m == MATCH_ERROR)
2018
            goto cleanup;
2019
        }
2020
    }
2021
 
2022
  /* If we are parsing an enumeration and have ensured that no other
2023
     attributes are present we can now set the parameter attribute.  */
2024
  if (gfc_current_state () == COMP_ENUM)
2025
    {
2026
      t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
2027
      if (t == FAILURE)
2028
        {
2029
          m = MATCH_ERROR;
2030
          goto cleanup;
2031
        }
2032
    }
2033
 
2034
  /* No double colon, so assume that we've been looking at something
2035
     else the whole time.  */
2036
  if (d == DECL_NONE)
2037
    {
2038
      m = MATCH_NO;
2039
      goto cleanup;
2040
    }
2041
 
2042
  /* Since we've seen a double colon, we have to be looking at an
2043
     attr-spec.  This means that we can now issue errors.  */
2044
  for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2045
    if (seen[d] > 1)
2046
      {
2047
        switch (d)
2048
          {
2049
          case DECL_ALLOCATABLE:
2050
            attr = "ALLOCATABLE";
2051
            break;
2052
          case DECL_DIMENSION:
2053
            attr = "DIMENSION";
2054
            break;
2055
          case DECL_EXTERNAL:
2056
            attr = "EXTERNAL";
2057
            break;
2058
          case DECL_IN:
2059
            attr = "INTENT (IN)";
2060
            break;
2061
          case DECL_OUT:
2062
            attr = "INTENT (OUT)";
2063
            break;
2064
          case DECL_INOUT:
2065
            attr = "INTENT (IN OUT)";
2066
            break;
2067
          case DECL_INTRINSIC:
2068
            attr = "INTRINSIC";
2069
            break;
2070
          case DECL_OPTIONAL:
2071
            attr = "OPTIONAL";
2072
            break;
2073
          case DECL_PARAMETER:
2074
            attr = "PARAMETER";
2075
            break;
2076
          case DECL_POINTER:
2077
            attr = "POINTER";
2078
            break;
2079
          case DECL_PRIVATE:
2080
            attr = "PRIVATE";
2081
            break;
2082
          case DECL_PUBLIC:
2083
            attr = "PUBLIC";
2084
            break;
2085
          case DECL_SAVE:
2086
            attr = "SAVE";
2087
            break;
2088
          case DECL_TARGET:
2089
            attr = "TARGET";
2090
            break;
2091
          default:
2092
            attr = NULL;        /* This shouldn't happen */
2093
          }
2094
 
2095
        gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
2096
        m = MATCH_ERROR;
2097
        goto cleanup;
2098
      }
2099
 
2100
  /* Now that we've dealt with duplicate attributes, add the attributes
2101
     to the current attribute.  */
2102
  for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2103
    {
2104
      if (seen[d] == 0)
2105
        continue;
2106
 
2107
      if (gfc_current_state () == COMP_DERIVED
2108
          && d != DECL_DIMENSION && d != DECL_POINTER
2109
          && d != DECL_COLON && d != DECL_NONE)
2110
        {
2111
 
2112
          gfc_error ("Attribute at %L is not allowed in a TYPE definition",
2113
                     &seen_at[d]);
2114
          m = MATCH_ERROR;
2115
          goto cleanup;
2116
        }
2117
 
2118
      if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
2119
             && gfc_current_state () != COMP_MODULE)
2120
        {
2121
          if (d == DECL_PRIVATE)
2122
            attr = "PRIVATE";
2123
          else
2124
            attr = "PUBLIC";
2125
 
2126
          gfc_error ("%s attribute at %L is not allowed outside of a MODULE",
2127
                     attr, &seen_at[d]);
2128
          m = MATCH_ERROR;
2129
          goto cleanup;
2130
        }
2131
 
2132
      switch (d)
2133
        {
2134
        case DECL_ALLOCATABLE:
2135
          t = gfc_add_allocatable (&current_attr, &seen_at[d]);
2136
          break;
2137
 
2138
        case DECL_DIMENSION:
2139
          t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
2140
          break;
2141
 
2142
        case DECL_EXTERNAL:
2143
          t = gfc_add_external (&current_attr, &seen_at[d]);
2144
          break;
2145
 
2146
        case DECL_IN:
2147
          t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
2148
          break;
2149
 
2150
        case DECL_OUT:
2151
          t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
2152
          break;
2153
 
2154
        case DECL_INOUT:
2155
          t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
2156
          break;
2157
 
2158
        case DECL_INTRINSIC:
2159
          t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
2160
          break;
2161
 
2162
        case DECL_OPTIONAL:
2163
          t = gfc_add_optional (&current_attr, &seen_at[d]);
2164
          break;
2165
 
2166
        case DECL_PARAMETER:
2167
          t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
2168
          break;
2169
 
2170
        case DECL_POINTER:
2171
          t = gfc_add_pointer (&current_attr, &seen_at[d]);
2172
          break;
2173
 
2174
        case DECL_PRIVATE:
2175
          t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
2176
                              &seen_at[d]);
2177
          break;
2178
 
2179
        case DECL_PUBLIC:
2180
          t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
2181
                              &seen_at[d]);
2182
          break;
2183
 
2184
        case DECL_SAVE:
2185
          t = gfc_add_save (&current_attr, NULL, &seen_at[d]);
2186
          break;
2187
 
2188
        case DECL_TARGET:
2189
          t = gfc_add_target (&current_attr, &seen_at[d]);
2190
          break;
2191
 
2192
        default:
2193
          gfc_internal_error ("match_attr_spec(): Bad attribute");
2194
        }
2195
 
2196
      if (t == FAILURE)
2197
        {
2198
          m = MATCH_ERROR;
2199
          goto cleanup;
2200
        }
2201
    }
2202
 
2203
  colon_seen = 1;
2204
  return MATCH_YES;
2205
 
2206
cleanup:
2207
  gfc_current_locus = start;
2208
  gfc_free_array_spec (current_as);
2209
  current_as = NULL;
2210
  return m;
2211
}
2212
 
2213
 
2214
/* Match a data declaration statement.  */
2215
 
2216
match
2217
gfc_match_data_decl (void)
2218
{
2219
  gfc_symbol *sym;
2220
  match m;
2221
  int elem;
2222
 
2223
  m = match_type_spec (&current_ts, 0);
2224
  if (m != MATCH_YES)
2225
    return m;
2226
 
2227
  if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
2228
    {
2229
      sym = gfc_use_derived (current_ts.derived);
2230
 
2231
      if (sym == NULL)
2232
        {
2233
          m = MATCH_ERROR;
2234
          goto cleanup;
2235
        }
2236
 
2237
      current_ts.derived = sym;
2238
    }
2239
 
2240
  m = match_attr_spec ();
2241
  if (m == MATCH_ERROR)
2242
    {
2243
      m = MATCH_NO;
2244
      goto cleanup;
2245
    }
2246
 
2247
  if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL)
2248
    {
2249
 
2250
      if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
2251
        goto ok;
2252
 
2253
      gfc_find_symbol (current_ts.derived->name,
2254
                         current_ts.derived->ns->parent, 1, &sym);
2255
 
2256
      /* Any symbol that we find had better be a type definition
2257
         which has its components defined.  */
2258
      if (sym != NULL && sym->attr.flavor == FL_DERIVED
2259
            && current_ts.derived->components != NULL)
2260
        goto ok;
2261
 
2262
      /* Now we have an error, which we signal, and then fix up
2263
         because the knock-on is plain and simple confusing.  */
2264
      gfc_error_now ("Derived type at %C has not been previously defined "
2265
                 "and so cannot appear in a derived type definition.");
2266
      current_attr.pointer = 1;
2267
      goto ok;
2268
    }
2269
 
2270
ok:
2271
  /* If we have an old-style character declaration, and no new-style
2272
     attribute specifications, then there a comma is optional between
2273
     the type specification and the variable list.  */
2274
  if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
2275
    gfc_match_char (',');
2276
 
2277
  /* Give the types/attributes to symbols that follow. Give the element
2278
     a number so that repeat character length expressions can be copied.  */
2279
  elem = 1;
2280
  for (;;)
2281
    {
2282
      m = variable_decl (elem++);
2283
      if (m == MATCH_ERROR)
2284
        goto cleanup;
2285
      if (m == MATCH_NO)
2286
        break;
2287
 
2288
      if (gfc_match_eos () == MATCH_YES)
2289
        goto cleanup;
2290
      if (gfc_match_char (',') != MATCH_YES)
2291
        break;
2292
    }
2293
 
2294
  gfc_error ("Syntax error in data declaration at %C");
2295
  m = MATCH_ERROR;
2296
 
2297
cleanup:
2298
  gfc_free_array_spec (current_as);
2299
  current_as = NULL;
2300
  return m;
2301
}
2302
 
2303
 
2304
/* Match a prefix associated with a function or subroutine
2305
   declaration.  If the typespec pointer is nonnull, then a typespec
2306
   can be matched.  Note that if nothing matches, MATCH_YES is
2307
   returned (the null string was matched).  */
2308
 
2309
static match
2310
match_prefix (gfc_typespec * ts)
2311
{
2312
  int seen_type;
2313
 
2314
  gfc_clear_attr (&current_attr);
2315
  seen_type = 0;
2316
 
2317
loop:
2318
  if (!seen_type && ts != NULL
2319
      && match_type_spec (ts, 0) == MATCH_YES
2320
      && gfc_match_space () == MATCH_YES)
2321
    {
2322
 
2323
      seen_type = 1;
2324
      goto loop;
2325
    }
2326
 
2327
  if (gfc_match ("elemental% ") == MATCH_YES)
2328
    {
2329
      if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
2330
        return MATCH_ERROR;
2331
 
2332
      goto loop;
2333
    }
2334
 
2335
  if (gfc_match ("pure% ") == MATCH_YES)
2336
    {
2337
      if (gfc_add_pure (&current_attr, NULL) == FAILURE)
2338
        return MATCH_ERROR;
2339
 
2340
      goto loop;
2341
    }
2342
 
2343
  if (gfc_match ("recursive% ") == MATCH_YES)
2344
    {
2345
      if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
2346
        return MATCH_ERROR;
2347
 
2348
      goto loop;
2349
    }
2350
 
2351
  /* At this point, the next item is not a prefix.  */
2352
  return MATCH_YES;
2353
}
2354
 
2355
 
2356
/* Copy attributes matched by match_prefix() to attributes on a symbol.  */
2357
 
2358
static try
2359
copy_prefix (symbol_attribute * dest, locus * where)
2360
{
2361
 
2362
  if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
2363
    return FAILURE;
2364
 
2365
  if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
2366
    return FAILURE;
2367
 
2368
  if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
2369
    return FAILURE;
2370
 
2371
  return SUCCESS;
2372
}
2373
 
2374
 
2375
/* Match a formal argument list.  */
2376
 
2377
match
2378
gfc_match_formal_arglist (gfc_symbol * progname, int st_flag, int null_flag)
2379
{
2380
  gfc_formal_arglist *head, *tail, *p, *q;
2381
  char name[GFC_MAX_SYMBOL_LEN + 1];
2382
  gfc_symbol *sym;
2383
  match m;
2384
 
2385
  head = tail = NULL;
2386
 
2387
  if (gfc_match_char ('(') != MATCH_YES)
2388
    {
2389
      if (null_flag)
2390
        goto ok;
2391
      return MATCH_NO;
2392
    }
2393
 
2394
  if (gfc_match_char (')') == MATCH_YES)
2395
    goto ok;
2396
 
2397
  for (;;)
2398
    {
2399
      if (gfc_match_char ('*') == MATCH_YES)
2400
        sym = NULL;
2401
      else
2402
        {
2403
          m = gfc_match_name (name);
2404
          if (m != MATCH_YES)
2405
            goto cleanup;
2406
 
2407
          if (gfc_get_symbol (name, NULL, &sym))
2408
            goto cleanup;
2409
        }
2410
 
2411
      p = gfc_get_formal_arglist ();
2412
 
2413
      if (head == NULL)
2414
        head = tail = p;
2415
      else
2416
        {
2417
          tail->next = p;
2418
          tail = p;
2419
        }
2420
 
2421
      tail->sym = sym;
2422
 
2423
      /* We don't add the VARIABLE flavor because the name could be a
2424
         dummy procedure.  We don't apply these attributes to formal
2425
         arguments of statement functions.  */
2426
      if (sym != NULL && !st_flag
2427
          && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
2428
              || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
2429
        {
2430
          m = MATCH_ERROR;
2431
          goto cleanup;
2432
        }
2433
 
2434
      /* The name of a program unit can be in a different namespace,
2435
         so check for it explicitly.  After the statement is accepted,
2436
         the name is checked for especially in gfc_get_symbol().  */
2437
      if (gfc_new_block != NULL && sym != NULL
2438
          && strcmp (sym->name, gfc_new_block->name) == 0)
2439
        {
2440
          gfc_error ("Name '%s' at %C is the name of the procedure",
2441
                     sym->name);
2442
          m = MATCH_ERROR;
2443
          goto cleanup;
2444
        }
2445
 
2446
      if (gfc_match_char (')') == MATCH_YES)
2447
        goto ok;
2448
 
2449
      m = gfc_match_char (',');
2450
      if (m != MATCH_YES)
2451
        {
2452
          gfc_error ("Unexpected junk in formal argument list at %C");
2453
          goto cleanup;
2454
        }
2455
    }
2456
 
2457
ok:
2458
  /* Check for duplicate symbols in the formal argument list.  */
2459
  if (head != NULL)
2460
    {
2461
      for (p = head; p->next; p = p->next)
2462
        {
2463
          if (p->sym == NULL)
2464
            continue;
2465
 
2466
          for (q = p->next; q; q = q->next)
2467
            if (p->sym == q->sym)
2468
              {
2469
                gfc_error
2470
                  ("Duplicate symbol '%s' in formal argument list at %C",
2471
                   p->sym->name);
2472
 
2473
                m = MATCH_ERROR;
2474
                goto cleanup;
2475
              }
2476
        }
2477
    }
2478
 
2479
  if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL) ==
2480
      FAILURE)
2481
    {
2482
      m = MATCH_ERROR;
2483
      goto cleanup;
2484
    }
2485
 
2486
  return MATCH_YES;
2487
 
2488
cleanup:
2489
  gfc_free_formal_arglist (head);
2490
  return m;
2491
}
2492
 
2493
 
2494
/* Match a RESULT specification following a function declaration or
2495
   ENTRY statement.  Also matches the end-of-statement.  */
2496
 
2497
static match
2498
match_result (gfc_symbol * function, gfc_symbol ** result)
2499
{
2500
  char name[GFC_MAX_SYMBOL_LEN + 1];
2501
  gfc_symbol *r;
2502
  match m;
2503
 
2504
  if (gfc_match (" result (") != MATCH_YES)
2505
    return MATCH_NO;
2506
 
2507
  m = gfc_match_name (name);
2508
  if (m != MATCH_YES)
2509
    return m;
2510
 
2511
  if (gfc_match (" )%t") != MATCH_YES)
2512
    {
2513
      gfc_error ("Unexpected junk following RESULT variable at %C");
2514
      return MATCH_ERROR;
2515
    }
2516
 
2517
  if (strcmp (function->name, name) == 0)
2518
    {
2519
      gfc_error
2520
        ("RESULT variable at %C must be different than function name");
2521
      return MATCH_ERROR;
2522
    }
2523
 
2524
  if (gfc_get_symbol (name, NULL, &r))
2525
    return MATCH_ERROR;
2526
 
2527
  if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
2528
      || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
2529
    return MATCH_ERROR;
2530
 
2531
  *result = r;
2532
 
2533
  return MATCH_YES;
2534
}
2535
 
2536
 
2537
/* Match a function declaration.  */
2538
 
2539
match
2540
gfc_match_function_decl (void)
2541
{
2542
  char name[GFC_MAX_SYMBOL_LEN + 1];
2543
  gfc_symbol *sym, *result;
2544
  locus old_loc;
2545
  match m;
2546
 
2547
  if (gfc_current_state () != COMP_NONE
2548
      && gfc_current_state () != COMP_INTERFACE
2549
      && gfc_current_state () != COMP_CONTAINS)
2550
    return MATCH_NO;
2551
 
2552
  gfc_clear_ts (&current_ts);
2553
 
2554
  old_loc = gfc_current_locus;
2555
 
2556
  m = match_prefix (&current_ts);
2557
  if (m != MATCH_YES)
2558
    {
2559
      gfc_current_locus = old_loc;
2560
      return m;
2561
    }
2562
 
2563
  if (gfc_match ("function% %n", name) != MATCH_YES)
2564
    {
2565
      gfc_current_locus = old_loc;
2566
      return MATCH_NO;
2567
    }
2568
 
2569
  if (get_proc_name (name, &sym))
2570
    return MATCH_ERROR;
2571
  gfc_new_block = sym;
2572
 
2573
  m = gfc_match_formal_arglist (sym, 0, 0);
2574
  if (m == MATCH_NO)
2575
    gfc_error ("Expected formal argument list in function definition at %C");
2576
  else if (m == MATCH_ERROR)
2577
    goto cleanup;
2578
 
2579
  result = NULL;
2580
 
2581
  if (gfc_match_eos () != MATCH_YES)
2582
    {
2583
      /* See if a result variable is present.  */
2584
      m = match_result (sym, &result);
2585
      if (m == MATCH_NO)
2586
        gfc_error ("Unexpected junk after function declaration at %C");
2587
 
2588
      if (m != MATCH_YES)
2589
        {
2590
          m = MATCH_ERROR;
2591
          goto cleanup;
2592
        }
2593
    }
2594
 
2595
  /* Make changes to the symbol.  */
2596
  m = MATCH_ERROR;
2597
 
2598
  if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2599
    goto cleanup;
2600
 
2601
  if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
2602
      || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
2603
    goto cleanup;
2604
 
2605
  if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN)
2606
    {
2607
      gfc_error ("Function '%s' at %C already has a type of %s", name,
2608
                 gfc_basic_typename (sym->ts.type));
2609
      goto cleanup;
2610
    }
2611
 
2612
  if (result == NULL)
2613
    {
2614
      sym->ts = current_ts;
2615
      sym->result = sym;
2616
    }
2617
  else
2618
    {
2619
      result->ts = current_ts;
2620
      sym->result = result;
2621
    }
2622
 
2623
  return MATCH_YES;
2624
 
2625
cleanup:
2626
  gfc_current_locus = old_loc;
2627
  return m;
2628
}
2629
 
2630
/* This is mostly a copy of parse.c(add_global_procedure) but modified to pass the
2631
   name of the entry, rather than the gfc_current_block name, and to return false
2632
   upon finding an existing global entry.  */
2633
 
2634
static bool
2635
add_global_entry (const char * name, int sub)
2636
{
2637
  gfc_gsymbol *s;
2638
 
2639
  s = gfc_get_gsymbol(name);
2640
 
2641
  if (s->defined
2642
        || (s->type != GSYM_UNKNOWN && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
2643
    global_used(s, NULL);
2644
  else
2645
    {
2646
      s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2647
      s->where = gfc_current_locus;
2648
      s->defined = 1;
2649
      return true;
2650
    }
2651
  return false;
2652
}
2653
 
2654
/* Match an ENTRY statement.  */
2655
 
2656
match
2657
gfc_match_entry (void)
2658
{
2659
  gfc_symbol *proc;
2660
  gfc_symbol *result;
2661
  gfc_symbol *entry;
2662
  char name[GFC_MAX_SYMBOL_LEN + 1];
2663
  gfc_compile_state state;
2664
  match m;
2665
  gfc_entry_list *el;
2666
  locus old_loc;
2667
 
2668
  m = gfc_match_name (name);
2669
  if (m != MATCH_YES)
2670
    return m;
2671
 
2672
  state = gfc_current_state ();
2673
  if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
2674
    {
2675
      switch (state)
2676
        {
2677
          case COMP_PROGRAM:
2678
            gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
2679
            break;
2680
          case COMP_MODULE:
2681
            gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
2682
            break;
2683
          case COMP_BLOCK_DATA:
2684
            gfc_error
2685
              ("ENTRY statement at %C cannot appear within a BLOCK DATA");
2686
            break;
2687
          case COMP_INTERFACE:
2688
            gfc_error
2689
              ("ENTRY statement at %C cannot appear within an INTERFACE");
2690
            break;
2691
          case COMP_DERIVED:
2692
            gfc_error
2693
              ("ENTRY statement at %C cannot appear "
2694
               "within a DERIVED TYPE block");
2695
            break;
2696
          case COMP_IF:
2697
            gfc_error
2698
              ("ENTRY statement at %C cannot appear within an IF-THEN block");
2699
            break;
2700
          case COMP_DO:
2701
            gfc_error
2702
              ("ENTRY statement at %C cannot appear within a DO block");
2703
            break;
2704
          case COMP_SELECT:
2705
            gfc_error
2706
              ("ENTRY statement at %C cannot appear within a SELECT block");
2707
            break;
2708
          case COMP_FORALL:
2709
            gfc_error
2710
              ("ENTRY statement at %C cannot appear within a FORALL block");
2711
            break;
2712
          case COMP_WHERE:
2713
            gfc_error
2714
              ("ENTRY statement at %C cannot appear within a WHERE block");
2715
            break;
2716
          case COMP_CONTAINS:
2717
            gfc_error
2718
              ("ENTRY statement at %C cannot appear "
2719
               "within a contained subprogram");
2720
            break;
2721
          default:
2722
            gfc_internal_error ("gfc_match_entry(): Bad state");
2723
        }
2724
      return MATCH_ERROR;
2725
    }
2726
 
2727
  if (gfc_current_ns->parent != NULL
2728
      && gfc_current_ns->parent->proc_name
2729
      && gfc_current_ns->parent->proc_name->attr.flavor != FL_MODULE)
2730
    {
2731
      gfc_error("ENTRY statement at %C cannot appear in a "
2732
                "contained procedure");
2733
      return MATCH_ERROR;
2734
    }
2735
 
2736
  if (get_proc_name (name, &entry))
2737
    return MATCH_ERROR;
2738
 
2739
  proc = gfc_current_block ();
2740
 
2741
  if (state == COMP_SUBROUTINE)
2742
    {
2743
      /* An entry in a subroutine.  */
2744
      if (!add_global_entry (name, 1))
2745
        return MATCH_ERROR;
2746
 
2747
      m = gfc_match_formal_arglist (entry, 0, 1);
2748
      if (m != MATCH_YES)
2749
        return MATCH_ERROR;
2750
 
2751
      if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
2752
          || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
2753
        return MATCH_ERROR;
2754
    }
2755
  else
2756
    {
2757
      /* An entry in a function.
2758
         We need to take special care because writing
2759
            ENTRY f()
2760
         as
2761
            ENTRY f
2762
         is allowed, whereas
2763
            ENTRY f() RESULT (r)
2764
         can't be written as
2765
            ENTRY f RESULT (r).  */
2766
      if (!add_global_entry (name, 0))
2767
        return MATCH_ERROR;
2768
 
2769
      old_loc = gfc_current_locus;
2770
      if (gfc_match_eos () == MATCH_YES)
2771
        {
2772
          gfc_current_locus = old_loc;
2773
          /* Match the empty argument list, and add the interface to
2774
             the symbol.  */
2775
          m = gfc_match_formal_arglist (entry, 0, 1);
2776
        }
2777
      else
2778
        m = gfc_match_formal_arglist (entry, 0, 0);
2779
 
2780
      if (m != MATCH_YES)
2781
        return MATCH_ERROR;
2782
 
2783
      result = NULL;
2784
 
2785
      if (gfc_match_eos () == MATCH_YES)
2786
        {
2787
          if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
2788
              || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
2789
            return MATCH_ERROR;
2790
 
2791
          entry->result = entry;
2792
        }
2793
      else
2794
        {
2795
          m = match_result (proc, &result);
2796
          if (m == MATCH_NO)
2797
            gfc_syntax_error (ST_ENTRY);
2798
          if (m != MATCH_YES)
2799
            return MATCH_ERROR;
2800
 
2801
          if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
2802
              || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
2803
              || gfc_add_function (&entry->attr, result->name,
2804
                                   NULL) == FAILURE)
2805
            return MATCH_ERROR;
2806
 
2807
          entry->result = result;
2808
        }
2809
 
2810
      if (proc->attr.recursive && result == NULL)
2811
        {
2812
          gfc_error ("RESULT attribute required in ENTRY statement at %C");
2813
          return MATCH_ERROR;
2814
        }
2815
    }
2816
 
2817
  if (gfc_match_eos () != MATCH_YES)
2818
    {
2819
      gfc_syntax_error (ST_ENTRY);
2820
      return MATCH_ERROR;
2821
    }
2822
 
2823
  entry->attr.recursive = proc->attr.recursive;
2824
  entry->attr.elemental = proc->attr.elemental;
2825
  entry->attr.pure = proc->attr.pure;
2826
 
2827
  el = gfc_get_entry_list ();
2828
  el->sym = entry;
2829
  el->next = gfc_current_ns->entries;
2830
  gfc_current_ns->entries = el;
2831
  if (el->next)
2832
    el->id = el->next->id + 1;
2833
  else
2834
    el->id = 1;
2835
 
2836
  new_st.op = EXEC_ENTRY;
2837
  new_st.ext.entry = el;
2838
 
2839
  return MATCH_YES;
2840
}
2841
 
2842
 
2843
/* Match a subroutine statement, including optional prefixes.  */
2844
 
2845
match
2846
gfc_match_subroutine (void)
2847
{
2848
  char name[GFC_MAX_SYMBOL_LEN + 1];
2849
  gfc_symbol *sym;
2850
  match m;
2851
 
2852
  if (gfc_current_state () != COMP_NONE
2853
      && gfc_current_state () != COMP_INTERFACE
2854
      && gfc_current_state () != COMP_CONTAINS)
2855
    return MATCH_NO;
2856
 
2857
  m = match_prefix (NULL);
2858
  if (m != MATCH_YES)
2859
    return m;
2860
 
2861
  m = gfc_match ("subroutine% %n", name);
2862
  if (m != MATCH_YES)
2863
    return m;
2864
 
2865
  if (get_proc_name (name, &sym))
2866
    return MATCH_ERROR;
2867
  gfc_new_block = sym;
2868
 
2869
  if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2870
    return MATCH_ERROR;
2871
 
2872
  if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
2873
    return MATCH_ERROR;
2874
 
2875
  if (gfc_match_eos () != MATCH_YES)
2876
    {
2877
      gfc_syntax_error (ST_SUBROUTINE);
2878
      return MATCH_ERROR;
2879
    }
2880
 
2881
  if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
2882
    return MATCH_ERROR;
2883
 
2884
  return MATCH_YES;
2885
}
2886
 
2887
 
2888
/* Return nonzero if we're currently compiling a contained procedure.  */
2889
 
2890
static int
2891
contained_procedure (void)
2892
{
2893
  gfc_state_data *s;
2894
 
2895
  for (s=gfc_state_stack; s; s=s->previous)
2896
    if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
2897
       && s->previous != NULL
2898
       && s->previous->state == COMP_CONTAINS)
2899
      return 1;
2900
 
2901
  return 0;
2902
}
2903
 
2904
/* Set the kind of each enumerator.  The kind is selected such that it is
2905
   interoperable with the corresponding C enumeration type, making
2906
   sure that -fshort-enums is honored.  */
2907
 
2908
static void
2909
set_enum_kind(void)
2910
{
2911
  enumerator_history *current_history = NULL;
2912
  int kind;
2913
  int i;
2914
 
2915
  if (max_enum == NULL || enum_history == NULL)
2916
    return;
2917
 
2918
  if (!gfc_option.fshort_enums)
2919
    return;
2920
 
2921
  i = 0;
2922
  do
2923
    {
2924
      kind = gfc_integer_kinds[i++].kind;
2925
    }
2926
  while (kind < gfc_c_int_kind
2927
         && gfc_check_integer_range (max_enum->initializer->value.integer,
2928
                                     kind) != ARITH_OK);
2929
 
2930
  current_history = enum_history;
2931
  while (current_history != NULL)
2932
    {
2933
      current_history->sym->ts.kind = kind;
2934
      current_history = current_history->next;
2935
    }
2936
}
2937
 
2938
/* Match any of the various end-block statements.  Returns the type of
2939
   END to the caller.  The END INTERFACE, END IF, END DO and END
2940
   SELECT statements cannot be replaced by a single END statement.  */
2941
 
2942
match
2943
gfc_match_end (gfc_statement * st)
2944
{
2945
  char name[GFC_MAX_SYMBOL_LEN + 1];
2946
  gfc_compile_state state;
2947
  locus old_loc;
2948
  const char *block_name;
2949
  const char *target;
2950
  int eos_ok;
2951
  match m;
2952
 
2953
  old_loc = gfc_current_locus;
2954
  if (gfc_match ("end") != MATCH_YES)
2955
    return MATCH_NO;
2956
 
2957
  state = gfc_current_state ();
2958
  block_name =
2959
    gfc_current_block () == NULL ? NULL : gfc_current_block ()->name;
2960
 
2961
  if (state == COMP_CONTAINS)
2962
    {
2963
      state = gfc_state_stack->previous->state;
2964
      block_name = gfc_state_stack->previous->sym == NULL ? NULL
2965
        : gfc_state_stack->previous->sym->name;
2966
    }
2967
 
2968
  switch (state)
2969
    {
2970
    case COMP_NONE:
2971
    case COMP_PROGRAM:
2972
      *st = ST_END_PROGRAM;
2973
      target = " program";
2974
      eos_ok = 1;
2975
      break;
2976
 
2977
    case COMP_SUBROUTINE:
2978
      *st = ST_END_SUBROUTINE;
2979
      target = " subroutine";
2980
      eos_ok = !contained_procedure ();
2981
      break;
2982
 
2983
    case COMP_FUNCTION:
2984
      *st = ST_END_FUNCTION;
2985
      target = " function";
2986
      eos_ok = !contained_procedure ();
2987
      break;
2988
 
2989
    case COMP_BLOCK_DATA:
2990
      *st = ST_END_BLOCK_DATA;
2991
      target = " block data";
2992
      eos_ok = 1;
2993
      break;
2994
 
2995
    case COMP_MODULE:
2996
      *st = ST_END_MODULE;
2997
      target = " module";
2998
      eos_ok = 1;
2999
      break;
3000
 
3001
    case COMP_INTERFACE:
3002
      *st = ST_END_INTERFACE;
3003
      target = " interface";
3004
      eos_ok = 0;
3005
      break;
3006
 
3007
    case COMP_DERIVED:
3008
      *st = ST_END_TYPE;
3009
      target = " type";
3010
      eos_ok = 0;
3011
      break;
3012
 
3013
    case COMP_IF:
3014
      *st = ST_ENDIF;
3015
      target = " if";
3016
      eos_ok = 0;
3017
      break;
3018
 
3019
    case COMP_DO:
3020
      *st = ST_ENDDO;
3021
      target = " do";
3022
      eos_ok = 0;
3023
      break;
3024
 
3025
    case COMP_SELECT:
3026
      *st = ST_END_SELECT;
3027
      target = " select";
3028
      eos_ok = 0;
3029
      break;
3030
 
3031
    case COMP_FORALL:
3032
      *st = ST_END_FORALL;
3033
      target = " forall";
3034
      eos_ok = 0;
3035
      break;
3036
 
3037
    case COMP_WHERE:
3038
      *st = ST_END_WHERE;
3039
      target = " where";
3040
      eos_ok = 0;
3041
      break;
3042
 
3043
    case COMP_ENUM:
3044
      *st = ST_END_ENUM;
3045
      target = " enum";
3046
      eos_ok = 0;
3047
      last_initializer = NULL;
3048
      set_enum_kind ();
3049
      gfc_free_enum_history ();
3050
      break;
3051
 
3052
    default:
3053
      gfc_error ("Unexpected END statement at %C");
3054
      goto cleanup;
3055
    }
3056
 
3057
  if (gfc_match_eos () == MATCH_YES)
3058
    {
3059
      if (!eos_ok)
3060
        {
3061
          /* We would have required END [something]  */
3062
          gfc_error ("%s statement expected at %L",
3063
                     gfc_ascii_statement (*st), &old_loc);
3064
          goto cleanup;
3065
        }
3066
 
3067
      return MATCH_YES;
3068
    }
3069
 
3070
  /* Verify that we've got the sort of end-block that we're expecting.  */
3071
  if (gfc_match (target) != MATCH_YES)
3072
    {
3073
      gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
3074
      goto cleanup;
3075
    }
3076
 
3077
  /* If we're at the end, make sure a block name wasn't required.  */
3078
  if (gfc_match_eos () == MATCH_YES)
3079
    {
3080
 
3081
      if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT)
3082
        return MATCH_YES;
3083
 
3084
      if (gfc_current_block () == NULL)
3085
        return MATCH_YES;
3086
 
3087
      gfc_error ("Expected block name of '%s' in %s statement at %C",
3088
                 block_name, gfc_ascii_statement (*st));
3089
 
3090
      return MATCH_ERROR;
3091
    }
3092
 
3093
  /* END INTERFACE has a special handler for its several possible endings.  */
3094
  if (*st == ST_END_INTERFACE)
3095
    return gfc_match_end_interface ();
3096
 
3097
  /* We haven't hit the end of statement, so what is left must be an end-name.  */
3098
  m = gfc_match_space ();
3099
  if (m == MATCH_YES)
3100
    m = gfc_match_name (name);
3101
 
3102
  if (m == MATCH_NO)
3103
    gfc_error ("Expected terminating name at %C");
3104
  if (m != MATCH_YES)
3105
    goto cleanup;
3106
 
3107
  if (block_name == NULL)
3108
    goto syntax;
3109
 
3110
  if (strcmp (name, block_name) != 0)
3111
    {
3112
      gfc_error ("Expected label '%s' for %s statement at %C", block_name,
3113
                 gfc_ascii_statement (*st));
3114
      goto cleanup;
3115
    }
3116
 
3117
  if (gfc_match_eos () == MATCH_YES)
3118
    return MATCH_YES;
3119
 
3120
syntax:
3121
  gfc_syntax_error (*st);
3122
 
3123
cleanup:
3124
  gfc_current_locus = old_loc;
3125
  return MATCH_ERROR;
3126
}
3127
 
3128
 
3129
 
3130
/***************** Attribute declaration statements ****************/
3131
 
3132
/* Set the attribute of a single variable.  */
3133
 
3134
static match
3135
attr_decl1 (void)
3136
{
3137
  char name[GFC_MAX_SYMBOL_LEN + 1];
3138
  gfc_array_spec *as;
3139
  gfc_symbol *sym;
3140
  locus var_locus;
3141
  match m;
3142
 
3143
  as = NULL;
3144
 
3145
  m = gfc_match_name (name);
3146
  if (m != MATCH_YES)
3147
    goto cleanup;
3148
 
3149
  if (find_special (name, &sym))
3150
    return MATCH_ERROR;
3151
 
3152
  var_locus = gfc_current_locus;
3153
 
3154
  /* Deal with possible array specification for certain attributes.  */
3155
  if (current_attr.dimension
3156
      || current_attr.allocatable
3157
      || current_attr.pointer
3158
      || current_attr.target)
3159
    {
3160
      m = gfc_match_array_spec (&as);
3161
      if (m == MATCH_ERROR)
3162
        goto cleanup;
3163
 
3164
      if (current_attr.dimension && m == MATCH_NO)
3165
        {
3166
          gfc_error
3167
            ("Missing array specification at %L in DIMENSION statement",
3168
             &var_locus);
3169
          m = MATCH_ERROR;
3170
          goto cleanup;
3171
        }
3172
 
3173
      if ((current_attr.allocatable || current_attr.pointer)
3174
          && (m == MATCH_YES) && (as->type != AS_DEFERRED))
3175
        {
3176
          gfc_error ("Array specification must be deferred at %L",
3177
                     &var_locus);
3178
          m = MATCH_ERROR;
3179
          goto cleanup;
3180
        }
3181
    }
3182
 
3183
  /* Update symbol table.  DIMENSION attribute is set in gfc_set_array_spec().  */
3184
  if (current_attr.dimension == 0
3185
      && gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
3186
    {
3187
      m = MATCH_ERROR;
3188
      goto cleanup;
3189
    }
3190
 
3191
  if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
3192
    {
3193
      m = MATCH_ERROR;
3194
      goto cleanup;
3195
    }
3196
 
3197
  if (sym->attr.cray_pointee && sym->as != NULL)
3198
    {
3199
      /* Fix the array spec.  */
3200
      m = gfc_mod_pointee_as (sym->as);
3201
      if (m == MATCH_ERROR)
3202
        goto cleanup;
3203
    }
3204
 
3205
  if (gfc_add_attribute (&sym->attr, &var_locus, current_attr.intent) == FAILURE)
3206
    {
3207
      m = MATCH_ERROR;
3208
      goto cleanup;
3209
    }
3210
 
3211
  if ((current_attr.external || current_attr.intrinsic)
3212
      && sym->attr.flavor != FL_PROCEDURE
3213
      && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
3214
    {
3215
      m = MATCH_ERROR;
3216
      goto cleanup;
3217
    }
3218
 
3219
  return MATCH_YES;
3220
 
3221
cleanup:
3222
  gfc_free_array_spec (as);
3223
  return m;
3224
}
3225
 
3226
 
3227
/* Generic attribute declaration subroutine.  Used for attributes that
3228
   just have a list of names.  */
3229
 
3230
static match
3231
attr_decl (void)
3232
{
3233
  match m;
3234
 
3235
  /* Gobble the optional double colon, by simply ignoring the result
3236
     of gfc_match().  */
3237
  gfc_match (" ::");
3238
 
3239
  for (;;)
3240
    {
3241
      m = attr_decl1 ();
3242
      if (m != MATCH_YES)
3243
        break;
3244
 
3245
      if (gfc_match_eos () == MATCH_YES)
3246
        {
3247
          m = MATCH_YES;
3248
          break;
3249
        }
3250
 
3251
      if (gfc_match_char (',') != MATCH_YES)
3252
        {
3253
          gfc_error ("Unexpected character in variable list at %C");
3254
          m = MATCH_ERROR;
3255
          break;
3256
        }
3257
    }
3258
 
3259
  return m;
3260
}
3261
 
3262
 
3263
/* This routine matches Cray Pointer declarations of the form:
3264
   pointer ( <pointer>, <pointee> )
3265
   or
3266
   pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
3267
   The pointer, if already declared, should be an integer.  Otherwise, we
3268
   set it as BT_INTEGER with kind gfc_index_integer_kind.  The pointee may
3269
   be either a scalar, or an array declaration.  No space is allocated for
3270
   the pointee.  For the statement
3271
   pointer (ipt, ar(10))
3272
   any subsequent uses of ar will be translated (in C-notation) as
3273
   ar(i) => ((<type> *) ipt)(i)
3274
   After gimplification, pointee variable will disappear in the code.  */
3275
 
3276
static match
3277
cray_pointer_decl (void)
3278
{
3279
  match m;
3280
  gfc_array_spec *as;
3281
  gfc_symbol *cptr; /* Pointer symbol.  */
3282
  gfc_symbol *cpte; /* Pointee symbol.  */
3283
  locus var_locus;
3284
  bool done = false;
3285
 
3286
  while (!done)
3287
    {
3288
      if (gfc_match_char ('(') != MATCH_YES)
3289
        {
3290
          gfc_error ("Expected '(' at %C");
3291
          return MATCH_ERROR;
3292
        }
3293
 
3294
      /* Match pointer.  */
3295
      var_locus = gfc_current_locus;
3296
      gfc_clear_attr (&current_attr);
3297
      gfc_add_cray_pointer (&current_attr, &var_locus);
3298
      current_ts.type = BT_INTEGER;
3299
      current_ts.kind = gfc_index_integer_kind;
3300
 
3301
      m = gfc_match_symbol (&cptr, 0);
3302
      if (m != MATCH_YES)
3303
        {
3304
          gfc_error ("Expected variable name at %C");
3305
          return m;
3306
        }
3307
 
3308
      if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
3309
        return MATCH_ERROR;
3310
 
3311
      gfc_set_sym_referenced (cptr);
3312
 
3313
      if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary.  */
3314
        {
3315
          cptr->ts.type = BT_INTEGER;
3316
          cptr->ts.kind = gfc_index_integer_kind;
3317
        }
3318
      else if (cptr->ts.type != BT_INTEGER)
3319
        {
3320
          gfc_error ("Cray pointer at %C must be an integer.");
3321
          return MATCH_ERROR;
3322
        }
3323
      else if (cptr->ts.kind < gfc_index_integer_kind)
3324
        gfc_warning ("Cray pointer at %C has %d bytes of precision;"
3325
                     " memory addresses require %d bytes.",
3326
                     cptr->ts.kind,
3327
                     gfc_index_integer_kind);
3328
 
3329
      if (gfc_match_char (',') != MATCH_YES)
3330
        {
3331
          gfc_error ("Expected \",\" at %C");
3332
          return MATCH_ERROR;
3333
        }
3334
 
3335
      /* Match Pointee.  */
3336
      var_locus = gfc_current_locus;
3337
      gfc_clear_attr (&current_attr);
3338
      gfc_add_cray_pointee (&current_attr, &var_locus);
3339
      current_ts.type = BT_UNKNOWN;
3340
      current_ts.kind = 0;
3341
 
3342
      m = gfc_match_symbol (&cpte, 0);
3343
      if (m != MATCH_YES)
3344
        {
3345
          gfc_error ("Expected variable name at %C");
3346
          return m;
3347
        }
3348
 
3349
      /* Check for an optional array spec.  */
3350
      m = gfc_match_array_spec (&as);
3351
      if (m == MATCH_ERROR)
3352
        {
3353
          gfc_free_array_spec (as);
3354
          return m;
3355
        }
3356
      else if (m == MATCH_NO)
3357
        {
3358
          gfc_free_array_spec (as);
3359
          as = NULL;
3360
        }
3361
 
3362
      if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
3363
        return MATCH_ERROR;
3364
 
3365
      gfc_set_sym_referenced (cpte);
3366
 
3367
      if (cpte->as == NULL)
3368
        {
3369
          if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
3370
            gfc_internal_error ("Couldn't set Cray pointee array spec.");
3371
        }
3372
      else if (as != NULL)
3373
        {
3374
          gfc_error ("Duplicate array spec for Cray pointee at %C.");
3375
          gfc_free_array_spec (as);
3376
          return MATCH_ERROR;
3377
        }
3378
 
3379
      as = NULL;
3380
 
3381
      if (cpte->as != NULL)
3382
        {
3383
          /* Fix array spec.  */
3384
          m = gfc_mod_pointee_as (cpte->as);
3385
          if (m == MATCH_ERROR)
3386
            return m;
3387
        }
3388
 
3389
      /* Point the Pointee at the Pointer.  */
3390
      cpte->cp_pointer = cptr;
3391
 
3392
      if (gfc_match_char (')') != MATCH_YES)
3393
        {
3394
          gfc_error ("Expected \")\" at %C");
3395
          return MATCH_ERROR;
3396
        }
3397
      m = gfc_match_char (',');
3398
      if (m != MATCH_YES)
3399
        done = true; /* Stop searching for more declarations.  */
3400
 
3401
    }
3402
 
3403
  if (m == MATCH_ERROR /* Failed when trying to find ',' above.  */
3404
      || gfc_match_eos () != MATCH_YES)
3405
    {
3406
      gfc_error ("Expected \",\" or end of statement at %C");
3407
      return MATCH_ERROR;
3408
    }
3409
  return MATCH_YES;
3410
}
3411
 
3412
 
3413
match
3414
gfc_match_external (void)
3415
{
3416
 
3417
  gfc_clear_attr (&current_attr);
3418
  current_attr.external = 1;
3419
 
3420
  return attr_decl ();
3421
}
3422
 
3423
 
3424
 
3425
match
3426
gfc_match_intent (void)
3427
{
3428
  sym_intent intent;
3429
 
3430
  intent = match_intent_spec ();
3431
  if (intent == INTENT_UNKNOWN)
3432
    return MATCH_ERROR;
3433
 
3434
  gfc_clear_attr (&current_attr);
3435
  current_attr.intent = intent;
3436
 
3437
  return attr_decl ();
3438
}
3439
 
3440
 
3441
match
3442
gfc_match_intrinsic (void)
3443
{
3444
 
3445
  gfc_clear_attr (&current_attr);
3446
  current_attr.intrinsic = 1;
3447
 
3448
  return attr_decl ();
3449
}
3450
 
3451
 
3452
match
3453
gfc_match_optional (void)
3454
{
3455
 
3456
  gfc_clear_attr (&current_attr);
3457
  current_attr.optional = 1;
3458
 
3459
  return attr_decl ();
3460
}
3461
 
3462
 
3463
match
3464
gfc_match_pointer (void)
3465
{
3466
  gfc_gobble_whitespace ();
3467
  if (gfc_peek_char () == '(')
3468
    {
3469
      if (!gfc_option.flag_cray_pointer)
3470
        {
3471
          gfc_error ("Cray pointer declaration at %C requires -fcray-pointer"
3472
                     " flag.");
3473
          return MATCH_ERROR;
3474
        }
3475
      return cray_pointer_decl ();
3476
    }
3477
  else
3478
    {
3479
      gfc_clear_attr (&current_attr);
3480
      current_attr.pointer = 1;
3481
 
3482
      return attr_decl ();
3483
    }
3484
}
3485
 
3486
 
3487
match
3488
gfc_match_allocatable (void)
3489
{
3490
 
3491
  gfc_clear_attr (&current_attr);
3492
  current_attr.allocatable = 1;
3493
 
3494
  return attr_decl ();
3495
}
3496
 
3497
 
3498
match
3499
gfc_match_dimension (void)
3500
{
3501
 
3502
  gfc_clear_attr (&current_attr);
3503
  current_attr.dimension = 1;
3504
 
3505
  return attr_decl ();
3506
}
3507
 
3508
 
3509
match
3510
gfc_match_target (void)
3511
{
3512
 
3513
  gfc_clear_attr (&current_attr);
3514
  current_attr.target = 1;
3515
 
3516
  return attr_decl ();
3517
}
3518
 
3519
 
3520
/* Match the list of entities being specified in a PUBLIC or PRIVATE
3521
   statement.  */
3522
 
3523
static match
3524
access_attr_decl (gfc_statement st)
3525
{
3526
  char name[GFC_MAX_SYMBOL_LEN + 1];
3527
  interface_type type;
3528
  gfc_user_op *uop;
3529
  gfc_symbol *sym;
3530
  gfc_intrinsic_op operator;
3531
  match m;
3532
 
3533
  if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
3534
    goto done;
3535
 
3536
  for (;;)
3537
    {
3538
      m = gfc_match_generic_spec (&type, name, &operator);
3539
      if (m == MATCH_NO)
3540
        goto syntax;
3541
      if (m == MATCH_ERROR)
3542
        return MATCH_ERROR;
3543
 
3544
      switch (type)
3545
        {
3546
        case INTERFACE_NAMELESS:
3547
          goto syntax;
3548
 
3549
        case INTERFACE_GENERIC:
3550
          if (gfc_get_symbol (name, NULL, &sym))
3551
            goto done;
3552
 
3553
          if (gfc_add_access (&sym->attr,
3554
                              (st ==
3555
                               ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE,
3556
                              sym->name, NULL) == FAILURE)
3557
            return MATCH_ERROR;
3558
 
3559
          break;
3560
 
3561
        case INTERFACE_INTRINSIC_OP:
3562
          if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
3563
            {
3564
              gfc_current_ns->operator_access[operator] =
3565
                (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3566
            }
3567
          else
3568
            {
3569
              gfc_error ("Access specification of the %s operator at %C has "
3570
                         "already been specified", gfc_op2string (operator));
3571
              goto done;
3572
            }
3573
 
3574
          break;
3575
 
3576
        case INTERFACE_USER_OP:
3577
          uop = gfc_get_uop (name);
3578
 
3579
          if (uop->access == ACCESS_UNKNOWN)
3580
            {
3581
              uop->access =
3582
                (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3583
            }
3584
          else
3585
            {
3586
              gfc_error
3587
                ("Access specification of the .%s. operator at %C has "
3588
                 "already been specified", sym->name);
3589
              goto done;
3590
            }
3591
 
3592
          break;
3593
        }
3594
 
3595
      if (gfc_match_char (',') == MATCH_NO)
3596
        break;
3597
    }
3598
 
3599
  if (gfc_match_eos () != MATCH_YES)
3600
    goto syntax;
3601
  return MATCH_YES;
3602
 
3603
syntax:
3604
  gfc_syntax_error (st);
3605
 
3606
done:
3607
  return MATCH_ERROR;
3608
}
3609
 
3610
 
3611
/* The PRIVATE statement is a bit weird in that it can be a attribute
3612
   declaration, but also works as a standlone statement inside of a
3613
   type declaration or a module.  */
3614
 
3615
match
3616
gfc_match_private (gfc_statement * st)
3617
{
3618
 
3619
  if (gfc_match ("private") != MATCH_YES)
3620
    return MATCH_NO;
3621
 
3622
  if (gfc_current_state () == COMP_DERIVED)
3623
    {
3624
      if (gfc_match_eos () == MATCH_YES)
3625
        {
3626
          *st = ST_PRIVATE;
3627
          return MATCH_YES;
3628
        }
3629
 
3630
      gfc_syntax_error (ST_PRIVATE);
3631
      return MATCH_ERROR;
3632
    }
3633
 
3634
  if (gfc_match_eos () == MATCH_YES)
3635
    {
3636
      *st = ST_PRIVATE;
3637
      return MATCH_YES;
3638
    }
3639
 
3640
  *st = ST_ATTR_DECL;
3641
  return access_attr_decl (ST_PRIVATE);
3642
}
3643
 
3644
 
3645
match
3646
gfc_match_public (gfc_statement * st)
3647
{
3648
 
3649
  if (gfc_match ("public") != MATCH_YES)
3650
    return MATCH_NO;
3651
 
3652
  if (gfc_match_eos () == MATCH_YES)
3653
    {
3654
      *st = ST_PUBLIC;
3655
      return MATCH_YES;
3656
    }
3657
 
3658
  *st = ST_ATTR_DECL;
3659
  return access_attr_decl (ST_PUBLIC);
3660
}
3661
 
3662
 
3663
/* Workhorse for gfc_match_parameter.  */
3664
 
3665
static match
3666
do_parm (void)
3667
{
3668
  gfc_symbol *sym;
3669
  gfc_expr *init;
3670
  match m;
3671
 
3672
  m = gfc_match_symbol (&sym, 0);
3673
  if (m == MATCH_NO)
3674
    gfc_error ("Expected variable name at %C in PARAMETER statement");
3675
 
3676
  if (m != MATCH_YES)
3677
    return m;
3678
 
3679
  if (gfc_match_char ('=') == MATCH_NO)
3680
    {
3681
      gfc_error ("Expected = sign in PARAMETER statement at %C");
3682
      return MATCH_ERROR;
3683
    }
3684
 
3685
  m = gfc_match_init_expr (&init);
3686
  if (m == MATCH_NO)
3687
    gfc_error ("Expected expression at %C in PARAMETER statement");
3688
  if (m != MATCH_YES)
3689
    return m;
3690
 
3691
  if (sym->ts.type == BT_UNKNOWN
3692
      && gfc_set_default_type (sym, 1, NULL) == FAILURE)
3693
    {
3694
      m = MATCH_ERROR;
3695
      goto cleanup;
3696
    }
3697
 
3698
  if (gfc_check_assign_symbol (sym, init) == FAILURE
3699
      || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
3700
    {
3701
      m = MATCH_ERROR;
3702
      goto cleanup;
3703
    }
3704
 
3705
  if (sym->ts.type == BT_CHARACTER
3706
      && sym->ts.cl != NULL
3707
      && sym->ts.cl->length != NULL
3708
      && sym->ts.cl->length->expr_type == EXPR_CONSTANT
3709
      && init->expr_type == EXPR_CONSTANT
3710
      && init->ts.type == BT_CHARACTER
3711
      && init->ts.kind == 1)
3712
    gfc_set_constant_character_len (
3713
      mpz_get_si (sym->ts.cl->length->value.integer), init);
3714
 
3715
  sym->value = init;
3716
  return MATCH_YES;
3717
 
3718
cleanup:
3719
  gfc_free_expr (init);
3720
  return m;
3721
}
3722
 
3723
 
3724
/* Match a parameter statement, with the weird syntax that these have.  */
3725
 
3726
match
3727
gfc_match_parameter (void)
3728
{
3729
  match m;
3730
 
3731
  if (gfc_match_char ('(') == MATCH_NO)
3732
    return MATCH_NO;
3733
 
3734
  for (;;)
3735
    {
3736
      m = do_parm ();
3737
      if (m != MATCH_YES)
3738
        break;
3739
 
3740
      if (gfc_match (" )%t") == MATCH_YES)
3741
        break;
3742
 
3743
      if (gfc_match_char (',') != MATCH_YES)
3744
        {
3745
          gfc_error ("Unexpected characters in PARAMETER statement at %C");
3746
          m = MATCH_ERROR;
3747
          break;
3748
        }
3749
    }
3750
 
3751
  return m;
3752
}
3753
 
3754
 
3755
/* Save statements have a special syntax.  */
3756
 
3757
match
3758
gfc_match_save (void)
3759
{
3760
  char n[GFC_MAX_SYMBOL_LEN+1];
3761
  gfc_common_head *c;
3762
  gfc_symbol *sym;
3763
  match m;
3764
 
3765
  if (gfc_match_eos () == MATCH_YES)
3766
    {
3767
      if (gfc_current_ns->seen_save)
3768
        {
3769
          if (gfc_notify_std (GFC_STD_LEGACY,
3770
                              "Blanket SAVE statement at %C follows previous "
3771
                              "SAVE statement")
3772
              == FAILURE)
3773
            return MATCH_ERROR;
3774
        }
3775
 
3776
      gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
3777
      return MATCH_YES;
3778
    }
3779
 
3780
  if (gfc_current_ns->save_all)
3781
    {
3782
      if (gfc_notify_std (GFC_STD_LEGACY,
3783
                          "SAVE statement at %C follows blanket SAVE statement")
3784
          == FAILURE)
3785
        return MATCH_ERROR;
3786
    }
3787
 
3788
  gfc_match (" ::");
3789
 
3790
  for (;;)
3791
    {
3792
      m = gfc_match_symbol (&sym, 0);
3793
      switch (m)
3794
        {
3795
        case MATCH_YES:
3796
          if (gfc_add_save (&sym->attr, sym->name,
3797
                            &gfc_current_locus) == FAILURE)
3798
            return MATCH_ERROR;
3799
          goto next_item;
3800
 
3801
        case MATCH_NO:
3802
          break;
3803
 
3804
        case MATCH_ERROR:
3805
          return MATCH_ERROR;
3806
        }
3807
 
3808
      m = gfc_match (" / %n /", &n);
3809
      if (m == MATCH_ERROR)
3810
        return MATCH_ERROR;
3811
      if (m == MATCH_NO)
3812
        goto syntax;
3813
 
3814
      c = gfc_get_common (n, 0);
3815
      c->saved = 1;
3816
 
3817
      gfc_current_ns->seen_save = 1;
3818
 
3819
    next_item:
3820
      if (gfc_match_eos () == MATCH_YES)
3821
        break;
3822
      if (gfc_match_char (',') != MATCH_YES)
3823
        goto syntax;
3824
    }
3825
 
3826
  return MATCH_YES;
3827
 
3828
syntax:
3829
  gfc_error ("Syntax error in SAVE statement at %C");
3830
  return MATCH_ERROR;
3831
}
3832
 
3833
 
3834
/* Match a module procedure statement.  Note that we have to modify
3835
   symbols in the parent's namespace because the current one was there
3836
   to receive symbols that are in an interface's formal argument list.  */
3837
 
3838
match
3839
gfc_match_modproc (void)
3840
{
3841
  char name[GFC_MAX_SYMBOL_LEN + 1];
3842
  gfc_symbol *sym;
3843
  match m;
3844
 
3845
  if (gfc_state_stack->state != COMP_INTERFACE
3846
      || gfc_state_stack->previous == NULL
3847
      || current_interface.type == INTERFACE_NAMELESS)
3848
    {
3849
      gfc_error
3850
        ("MODULE PROCEDURE at %C must be in a generic module interface");
3851
      return MATCH_ERROR;
3852
    }
3853
 
3854
  for (;;)
3855
    {
3856
      m = gfc_match_name (name);
3857
      if (m == MATCH_NO)
3858
        goto syntax;
3859
      if (m != MATCH_YES)
3860
        return MATCH_ERROR;
3861
 
3862
      if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
3863
        return MATCH_ERROR;
3864
 
3865
      if (sym->attr.proc != PROC_MODULE
3866
          && gfc_add_procedure (&sym->attr, PROC_MODULE,
3867
                                sym->name, NULL) == FAILURE)
3868
        return MATCH_ERROR;
3869
 
3870
      if (gfc_add_interface (sym) == FAILURE)
3871
        return MATCH_ERROR;
3872
 
3873
      if (gfc_match_eos () == MATCH_YES)
3874
        break;
3875
      if (gfc_match_char (',') != MATCH_YES)
3876
        goto syntax;
3877
    }
3878
 
3879
  return MATCH_YES;
3880
 
3881
syntax:
3882
  gfc_syntax_error (ST_MODULE_PROC);
3883
  return MATCH_ERROR;
3884
}
3885
 
3886
 
3887
/* Match the beginning of a derived type declaration.  If a type name
3888
   was the result of a function, then it is possible to have a symbol
3889
   already to be known as a derived type yet have no components.  */
3890
 
3891
match
3892
gfc_match_derived_decl (void)
3893
{
3894
  char name[GFC_MAX_SYMBOL_LEN + 1];
3895
  symbol_attribute attr;
3896
  gfc_symbol *sym;
3897
  match m;
3898
 
3899
  if (gfc_current_state () == COMP_DERIVED)
3900
    return MATCH_NO;
3901
 
3902
  gfc_clear_attr (&attr);
3903
 
3904
loop:
3905
  if (gfc_match (" , private") == MATCH_YES)
3906
    {
3907
      if (gfc_find_state (COMP_MODULE) == FAILURE)
3908
        {
3909
          gfc_error
3910
            ("Derived type at %C can only be PRIVATE within a MODULE");
3911
          return MATCH_ERROR;
3912
        }
3913
 
3914
      if (gfc_add_access (&attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
3915
        return MATCH_ERROR;
3916
      goto loop;
3917
    }
3918
 
3919
  if (gfc_match (" , public") == MATCH_YES)
3920
    {
3921
      if (gfc_find_state (COMP_MODULE) == FAILURE)
3922
        {
3923
          gfc_error ("Derived type at %C can only be PUBLIC within a MODULE");
3924
          return MATCH_ERROR;
3925
        }
3926
 
3927
      if (gfc_add_access (&attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
3928
        return MATCH_ERROR;
3929
      goto loop;
3930
    }
3931
 
3932
  if (gfc_match (" ::") != MATCH_YES && attr.access != ACCESS_UNKNOWN)
3933
    {
3934
      gfc_error ("Expected :: in TYPE definition at %C");
3935
      return MATCH_ERROR;
3936
    }
3937
 
3938
  m = gfc_match (" %n%t", name);
3939
  if (m != MATCH_YES)
3940
    return m;
3941
 
3942
  /* Make sure the name isn't the name of an intrinsic type.  The
3943
     'double precision' type doesn't get past the name matcher.  */
3944
  if (strcmp (name, "integer") == 0
3945
      || strcmp (name, "real") == 0
3946
      || strcmp (name, "character") == 0
3947
      || strcmp (name, "logical") == 0
3948
      || strcmp (name, "complex") == 0)
3949
    {
3950
      gfc_error
3951
        ("Type name '%s' at %C cannot be the same as an intrinsic type",
3952
         name);
3953
      return MATCH_ERROR;
3954
    }
3955
 
3956
  if (gfc_get_symbol (name, NULL, &sym))
3957
    return MATCH_ERROR;
3958
 
3959
  if (sym->ts.type != BT_UNKNOWN)
3960
    {
3961
      gfc_error ("Derived type name '%s' at %C already has a basic type "
3962
                 "of %s", sym->name, gfc_typename (&sym->ts));
3963
      return MATCH_ERROR;
3964
    }
3965
 
3966
  /* The symbol may already have the derived attribute without the
3967
     components.  The ways this can happen is via a function
3968
     definition, an INTRINSIC statement or a subtype in another
3969
     derived type that is a pointer.  The first part of the AND clause
3970
     is true if a the symbol is not the return value of a function.  */
3971
  if (sym->attr.flavor != FL_DERIVED
3972
      && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
3973
    return MATCH_ERROR;
3974
 
3975
  if (sym->components != NULL)
3976
    {
3977
      gfc_error
3978
        ("Derived type definition of '%s' at %C has already been defined",
3979
         sym->name);
3980
      return MATCH_ERROR;
3981
    }
3982
 
3983
  if (attr.access != ACCESS_UNKNOWN
3984
      && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
3985
    return MATCH_ERROR;
3986
 
3987
  gfc_new_block = sym;
3988
 
3989
  return MATCH_YES;
3990
}
3991
 
3992
 
3993
/* Cray Pointees can be declared as:
3994
      pointer (ipt, a (n,m,...,*))
3995
   By default, this is treated as an AS_ASSUMED_SIZE array.  We'll
3996
   cheat and set a constant bound of 1 for the last dimension, if this
3997
   is the case. Since there is no bounds-checking for Cray Pointees,
3998
   this will be okay.  */
3999
 
4000
try
4001
gfc_mod_pointee_as (gfc_array_spec *as)
4002
{
4003
  as->cray_pointee = true; /* This will be useful to know later.  */
4004
  if (as->type == AS_ASSUMED_SIZE)
4005
    {
4006
      as->type = AS_EXPLICIT;
4007
      as->upper[as->rank - 1] = gfc_int_expr (1);
4008
      as->cp_was_assumed = true;
4009
    }
4010
  else if (as->type == AS_ASSUMED_SHAPE)
4011
    {
4012
      gfc_error ("Cray Pointee at %C cannot be assumed shape array");
4013
      return MATCH_ERROR;
4014
    }
4015
  return MATCH_YES;
4016
}
4017
 
4018
 
4019
/* Match the enum definition statement, here we are trying to match
4020
   the first line of enum definition statement.
4021
   Returns MATCH_YES if match is found.  */
4022
 
4023
match
4024
gfc_match_enum (void)
4025
{
4026
  match m;
4027
 
4028
  m = gfc_match_eos ();
4029
  if (m != MATCH_YES)
4030
    return m;
4031
 
4032
  if (gfc_notify_std (GFC_STD_F2003,
4033
                      "New in Fortran 2003: ENUM AND ENUMERATOR at %C")
4034
      == FAILURE)
4035
    return MATCH_ERROR;
4036
 
4037
  return MATCH_YES;
4038
}
4039
 
4040
 
4041
/* Match the enumerator definition statement. */
4042
 
4043
match
4044
gfc_match_enumerator_def (void)
4045
{
4046
  match m;
4047
  int elem;
4048
 
4049
  gfc_clear_ts (&current_ts);
4050
 
4051
  m = gfc_match (" enumerator");
4052
  if (m != MATCH_YES)
4053
    return m;
4054
 
4055
  if (gfc_current_state () != COMP_ENUM)
4056
    {
4057
      gfc_error ("ENUM definition statement expected before %C");
4058
      gfc_free_enum_history ();
4059
      return MATCH_ERROR;
4060
    }
4061
 
4062
  (&current_ts)->type = BT_INTEGER;
4063
  (&current_ts)->kind = gfc_c_int_kind;
4064
 
4065
  m = match_attr_spec ();
4066
  if (m == MATCH_ERROR)
4067
    {
4068
      m = MATCH_NO;
4069
      goto cleanup;
4070
    }
4071
 
4072
  elem = 1;
4073
  for (;;)
4074
    {
4075
      m = variable_decl (elem++);
4076
      if (m == MATCH_ERROR)
4077
        goto cleanup;
4078
      if (m == MATCH_NO)
4079
        break;
4080
 
4081
      if (gfc_match_eos () == MATCH_YES)
4082
        goto cleanup;
4083
      if (gfc_match_char (',') != MATCH_YES)
4084
        break;
4085
    }
4086
 
4087
  if (gfc_current_state () == COMP_ENUM)
4088
    {
4089
      gfc_free_enum_history ();
4090
      gfc_error ("Syntax error in ENUMERATOR definition at %C");
4091
      m = MATCH_ERROR;
4092
    }
4093
 
4094
cleanup:
4095
  gfc_free_array_spec (current_as);
4096
  current_as = NULL;
4097
  return m;
4098
 
4099
}
4100
 

powered by: WebSVN 2.1.0

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