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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [fortran/] [decl.c] - Blame information for rev 742

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

Line No. Rev Author Line
1 712 jeremybenn
/* Declaration statement matcher
2
   Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3
   Free Software Foundation, Inc.
4
   Contributed by Andy Vaught
5
 
6
This file is part of GCC.
7
 
8
GCC is free software; you can redistribute it and/or modify it under
9
the terms of the GNU General Public License as published by the Free
10
Software Foundation; either version 3, or (at your option) any later
11
version.
12
 
13
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14
WARRANTY; without even the implied warranty of MERCHANTABILITY or
15
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16
for more details.
17
 
18
You should have received a copy of the GNU General Public License
19
along with GCC; see the file COPYING3.  If not see
20
<http://www.gnu.org/licenses/>.  */
21
 
22
#include "config.h"
23
#include "system.h"
24
#include "gfortran.h"
25
#include "match.h"
26
#include "parse.h"
27
#include "flags.h"
28
#include "constructor.h"
29
#include "tree.h"
30
 
31
/* Macros to access allocate memory for gfc_data_variable,
32
   gfc_data_value and gfc_data.  */
33
#define gfc_get_data_variable() XCNEW (gfc_data_variable)
34
#define gfc_get_data_value() XCNEW (gfc_data_value)
35
#define gfc_get_data() XCNEW (gfc_data)
36
 
37
 
38
static gfc_try set_binding_label (const char **, const char *, int);
39
 
40
 
41
/* This flag is set if an old-style length selector is matched
42
   during a type-declaration statement.  */
43
 
44
static int old_char_selector;
45
 
46
/* When variables acquire types and attributes from a declaration
47
   statement, they get them from the following static variables.  The
48
   first part of a declaration sets these variables and the second
49
   part copies these into symbol structures.  */
50
 
51
static gfc_typespec current_ts;
52
 
53
static symbol_attribute current_attr;
54
static gfc_array_spec *current_as;
55
static int colon_seen;
56
 
57
/* The current binding label (if any).  */
58
static const char* curr_binding_label;
59
/* Need to know how many identifiers are on the current data declaration
60
   line in case we're given the BIND(C) attribute with a NAME= specifier.  */
61
static int num_idents_on_line;
62
/* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
63
   can supply a name if the curr_binding_label is nil and NAME= was not.  */
64
static int has_name_equals = 0;
65
 
66
/* Initializer of the previous enumerator.  */
67
 
68
static gfc_expr *last_initializer;
69
 
70
/* History of all the enumerators is maintained, so that
71
   kind values of all the enumerators could be updated depending
72
   upon the maximum initialized value.  */
73
 
74
typedef struct enumerator_history
75
{
76
  gfc_symbol *sym;
77
  gfc_expr *initializer;
78
  struct enumerator_history *next;
79
}
80
enumerator_history;
81
 
82
/* Header of enum history chain.  */
83
 
84
static enumerator_history *enum_history = NULL;
85
 
86
/* Pointer of enum history node containing largest initializer.  */
87
 
88
static enumerator_history *max_enum = NULL;
89
 
90
/* gfc_new_block points to the symbol of a newly matched block.  */
91
 
92
gfc_symbol *gfc_new_block;
93
 
94
bool gfc_matching_function;
95
 
96
 
97
/********************* DATA statement subroutines *********************/
98
 
99
static bool in_match_data = false;
100
 
101
bool
102
gfc_in_match_data (void)
103
{
104
  return in_match_data;
105
}
106
 
107
static void
108
set_in_match_data (bool set_value)
109
{
110
  in_match_data = set_value;
111
}
112
 
113
/* Free a gfc_data_variable structure and everything beneath it.  */
114
 
115
static void
116
free_variable (gfc_data_variable *p)
117
{
118
  gfc_data_variable *q;
119
 
120
  for (; p; p = q)
121
    {
122
      q = p->next;
123
      gfc_free_expr (p->expr);
124
      gfc_free_iterator (&p->iter, 0);
125
      free_variable (p->list);
126
      free (p);
127
    }
128
}
129
 
130
 
131
/* Free a gfc_data_value structure and everything beneath it.  */
132
 
133
static void
134
free_value (gfc_data_value *p)
135
{
136
  gfc_data_value *q;
137
 
138
  for (; p; p = q)
139
    {
140
      q = p->next;
141
      mpz_clear (p->repeat);
142
      gfc_free_expr (p->expr);
143
      free (p);
144
    }
145
}
146
 
147
 
148
/* Free a list of gfc_data structures.  */
149
 
150
void
151
gfc_free_data (gfc_data *p)
152
{
153
  gfc_data *q;
154
 
155
  for (; p; p = q)
156
    {
157
      q = p->next;
158
      free_variable (p->var);
159
      free_value (p->value);
160
      free (p);
161
    }
162
}
163
 
164
 
165
/* Free all data in a namespace.  */
166
 
167
static void
168
gfc_free_data_all (gfc_namespace *ns)
169
{
170
  gfc_data *d;
171
 
172
  for (;ns->data;)
173
    {
174
      d = ns->data->next;
175
      free (ns->data);
176
      ns->data = d;
177
    }
178
}
179
 
180
 
181
static match var_element (gfc_data_variable *);
182
 
183
/* Match a list of variables terminated by an iterator and a right
184
   parenthesis.  */
185
 
186
static match
187
var_list (gfc_data_variable *parent)
188
{
189
  gfc_data_variable *tail, var;
190
  match m;
191
 
192
  m = var_element (&var);
193
  if (m == MATCH_ERROR)
194
    return MATCH_ERROR;
195
  if (m == MATCH_NO)
196
    goto syntax;
197
 
198
  tail = gfc_get_data_variable ();
199
  *tail = var;
200
 
201
  parent->list = tail;
202
 
203
  for (;;)
204
    {
205
      if (gfc_match_char (',') != MATCH_YES)
206
        goto syntax;
207
 
208
      m = gfc_match_iterator (&parent->iter, 1);
209
      if (m == MATCH_YES)
210
        break;
211
      if (m == MATCH_ERROR)
212
        return MATCH_ERROR;
213
 
214
      m = var_element (&var);
215
      if (m == MATCH_ERROR)
216
        return MATCH_ERROR;
217
      if (m == MATCH_NO)
218
        goto syntax;
219
 
220
      tail->next = gfc_get_data_variable ();
221
      tail = tail->next;
222
 
223
      *tail = var;
224
    }
225
 
226
  if (gfc_match_char (')') != MATCH_YES)
227
    goto syntax;
228
  return MATCH_YES;
229
 
230
syntax:
231
  gfc_syntax_error (ST_DATA);
232
  return MATCH_ERROR;
233
}
234
 
235
 
236
/* Match a single element in a data variable list, which can be a
237
   variable-iterator list.  */
238
 
239
static match
240
var_element (gfc_data_variable *new_var)
241
{
242
  match m;
243
  gfc_symbol *sym;
244
 
245
  memset (new_var, 0, sizeof (gfc_data_variable));
246
 
247
  if (gfc_match_char ('(') == MATCH_YES)
248
    return var_list (new_var);
249
 
250
  m = gfc_match_variable (&new_var->expr, 0);
251
  if (m != MATCH_YES)
252
    return m;
253
 
254
  sym = new_var->expr->symtree->n.sym;
255
 
256
  /* Symbol should already have an associated type.  */
257
  if (gfc_check_symbol_typed (sym, gfc_current_ns,
258
                              false, gfc_current_locus) == FAILURE)
259
    return MATCH_ERROR;
260
 
261
  if (!sym->attr.function && gfc_current_ns->parent
262
      && gfc_current_ns->parent == sym->ns)
263
    {
264
      gfc_error ("Host associated variable '%s' may not be in the DATA "
265
                 "statement at %C", sym->name);
266
      return MATCH_ERROR;
267
    }
268
 
269
  if (gfc_current_state () != COMP_BLOCK_DATA
270
      && sym->attr.in_common
271
      && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
272
                         "common block variable '%s' in DATA statement at %C",
273
                         sym->name) == FAILURE)
274
    return MATCH_ERROR;
275
 
276
  if (gfc_add_data (&sym->attr, sym->name, &new_var->expr->where) == FAILURE)
277
    return MATCH_ERROR;
278
 
279
  return MATCH_YES;
280
}
281
 
282
 
283
/* Match the top-level list of data variables.  */
284
 
285
static match
286
top_var_list (gfc_data *d)
287
{
288
  gfc_data_variable var, *tail, *new_var;
289
  match m;
290
 
291
  tail = NULL;
292
 
293
  for (;;)
294
    {
295
      m = var_element (&var);
296
      if (m == MATCH_NO)
297
        goto syntax;
298
      if (m == MATCH_ERROR)
299
        return MATCH_ERROR;
300
 
301
      new_var = gfc_get_data_variable ();
302
      *new_var = var;
303
 
304
      if (tail == NULL)
305
        d->var = new_var;
306
      else
307
        tail->next = new_var;
308
 
309
      tail = new_var;
310
 
311
      if (gfc_match_char ('/') == MATCH_YES)
312
        break;
313
      if (gfc_match_char (',') != MATCH_YES)
314
        goto syntax;
315
    }
316
 
317
  return MATCH_YES;
318
 
319
syntax:
320
  gfc_syntax_error (ST_DATA);
321
  gfc_free_data_all (gfc_current_ns);
322
  return MATCH_ERROR;
323
}
324
 
325
 
326
static match
327
match_data_constant (gfc_expr **result)
328
{
329
  char name[GFC_MAX_SYMBOL_LEN + 1];
330
  gfc_symbol *sym, *dt_sym = NULL;
331
  gfc_expr *expr;
332
  match m;
333
  locus old_loc;
334
 
335
  m = gfc_match_literal_constant (&expr, 1);
336
  if (m == MATCH_YES)
337
    {
338
      *result = expr;
339
      return MATCH_YES;
340
    }
341
 
342
  if (m == MATCH_ERROR)
343
    return MATCH_ERROR;
344
 
345
  m = gfc_match_null (result);
346
  if (m != MATCH_NO)
347
    return m;
348
 
349
  old_loc = gfc_current_locus;
350
 
351
  /* Should this be a structure component, try to match it
352
     before matching a name.  */
353
  m = gfc_match_rvalue (result);
354
  if (m == MATCH_ERROR)
355
    return m;
356
 
357
  if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
358
    {
359
      if (gfc_simplify_expr (*result, 0) == FAILURE)
360
        m = MATCH_ERROR;
361
      return m;
362
    }
363
 
364
  gfc_current_locus = old_loc;
365
 
366
  m = gfc_match_name (name);
367
  if (m != MATCH_YES)
368
    return m;
369
 
370
  if (gfc_find_symbol (name, NULL, 1, &sym))
371
    return MATCH_ERROR;
372
 
373
  if (sym && sym->attr.generic)
374
    dt_sym = gfc_find_dt_in_generic (sym);
375
 
376
  if (sym == NULL
377
      || (sym->attr.flavor != FL_PARAMETER
378
          && (!dt_sym || dt_sym->attr.flavor != FL_DERIVED)))
379
    {
380
      gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
381
                 name);
382
      return MATCH_ERROR;
383
    }
384
  else if (dt_sym && dt_sym->attr.flavor == FL_DERIVED)
385
    return gfc_match_structure_constructor (dt_sym, result);
386
 
387
  /* Check to see if the value is an initialization array expression.  */
388
  if (sym->value->expr_type == EXPR_ARRAY)
389
    {
390
      gfc_current_locus = old_loc;
391
 
392
      m = gfc_match_init_expr (result);
393
      if (m == MATCH_ERROR)
394
        return m;
395
 
396
      if (m == MATCH_YES)
397
        {
398
          if (gfc_simplify_expr (*result, 0) == FAILURE)
399
            m = MATCH_ERROR;
400
 
401
          if ((*result)->expr_type == EXPR_CONSTANT)
402
            return m;
403
          else
404
            {
405
              gfc_error ("Invalid initializer %s in Data statement at %C", name);
406
              return MATCH_ERROR;
407
            }
408
        }
409
    }
410
 
411
  *result = gfc_copy_expr (sym->value);
412
  return MATCH_YES;
413
}
414
 
415
 
416
/* Match a list of values in a DATA statement.  The leading '/' has
417
   already been seen at this point.  */
418
 
419
static match
420
top_val_list (gfc_data *data)
421
{
422
  gfc_data_value *new_val, *tail;
423
  gfc_expr *expr;
424
  match m;
425
 
426
  tail = NULL;
427
 
428
  for (;;)
429
    {
430
      m = match_data_constant (&expr);
431
      if (m == MATCH_NO)
432
        goto syntax;
433
      if (m == MATCH_ERROR)
434
        return MATCH_ERROR;
435
 
436
      new_val = gfc_get_data_value ();
437
      mpz_init (new_val->repeat);
438
 
439
      if (tail == NULL)
440
        data->value = new_val;
441
      else
442
        tail->next = new_val;
443
 
444
      tail = new_val;
445
 
446
      if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
447
        {
448
          tail->expr = expr;
449
          mpz_set_ui (tail->repeat, 1);
450
        }
451
      else
452
        {
453
          if (expr->ts.type == BT_INTEGER)
454
            mpz_set (tail->repeat, expr->value.integer);
455
          gfc_free_expr (expr);
456
 
457
          m = match_data_constant (&tail->expr);
458
          if (m == MATCH_NO)
459
            goto syntax;
460
          if (m == MATCH_ERROR)
461
            return MATCH_ERROR;
462
        }
463
 
464
      if (gfc_match_char ('/') == MATCH_YES)
465
        break;
466
      if (gfc_match_char (',') == MATCH_NO)
467
        goto syntax;
468
    }
469
 
470
  return MATCH_YES;
471
 
472
syntax:
473
  gfc_syntax_error (ST_DATA);
474
  gfc_free_data_all (gfc_current_ns);
475
  return MATCH_ERROR;
476
}
477
 
478
 
479
/* Matches an old style initialization.  */
480
 
481
static match
482
match_old_style_init (const char *name)
483
{
484
  match m;
485
  gfc_symtree *st;
486
  gfc_symbol *sym;
487
  gfc_data *newdata;
488
 
489
  /* Set up data structure to hold initializers.  */
490
  gfc_find_sym_tree (name, NULL, 0, &st);
491
  sym = st->n.sym;
492
 
493
  newdata = gfc_get_data ();
494
  newdata->var = gfc_get_data_variable ();
495
  newdata->var->expr = gfc_get_variable_expr (st);
496
  newdata->where = gfc_current_locus;
497
 
498
  /* Match initial value list. This also eats the terminal '/'.  */
499
  m = top_val_list (newdata);
500
  if (m != MATCH_YES)
501
    {
502
      free (newdata);
503
      return m;
504
    }
505
 
506
  if (gfc_pure (NULL))
507
    {
508
      gfc_error ("Initialization at %C is not allowed in a PURE procedure");
509
      free (newdata);
510
      return MATCH_ERROR;
511
    }
512
 
513
  if (gfc_implicit_pure (NULL))
514
    gfc_current_ns->proc_name->attr.implicit_pure = 0;
515
 
516
  /* Mark the variable as having appeared in a data statement.  */
517
  if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
518
    {
519
      free (newdata);
520
      return MATCH_ERROR;
521
    }
522
 
523
  /* Chain in namespace list of DATA initializers.  */
524
  newdata->next = gfc_current_ns->data;
525
  gfc_current_ns->data = newdata;
526
 
527
  return m;
528
}
529
 
530
 
531
/* Match the stuff following a DATA statement. If ERROR_FLAG is set,
532
   we are matching a DATA statement and are therefore issuing an error
533
   if we encounter something unexpected, if not, we're trying to match
534
   an old-style initialization expression of the form INTEGER I /2/.  */
535
 
536
match
537
gfc_match_data (void)
538
{
539
  gfc_data *new_data;
540
  match m;
541
 
542
  set_in_match_data (true);
543
 
544
  for (;;)
545
    {
546
      new_data = gfc_get_data ();
547
      new_data->where = gfc_current_locus;
548
 
549
      m = top_var_list (new_data);
550
      if (m != MATCH_YES)
551
        goto cleanup;
552
 
553
      m = top_val_list (new_data);
554
      if (m != MATCH_YES)
555
        goto cleanup;
556
 
557
      new_data->next = gfc_current_ns->data;
558
      gfc_current_ns->data = new_data;
559
 
560
      if (gfc_match_eos () == MATCH_YES)
561
        break;
562
 
563
      gfc_match_char (',');     /* Optional comma */
564
    }
565
 
566
  set_in_match_data (false);
567
 
568
  if (gfc_pure (NULL))
569
    {
570
      gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
571
      return MATCH_ERROR;
572
    }
573
 
574
  if (gfc_implicit_pure (NULL))
575
    gfc_current_ns->proc_name->attr.implicit_pure = 0;
576
 
577
  return MATCH_YES;
578
 
579
cleanup:
580
  set_in_match_data (false);
581
  gfc_free_data (new_data);
582
  return MATCH_ERROR;
583
}
584
 
585
 
586
/************************ Declaration statements *********************/
587
 
588
 
589
/* Auxilliary function to merge DIMENSION and CODIMENSION array specs.  */
590
 
591
static void
592
merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
593
{
594
  int i;
595
 
596
  if (to->rank == 0 && from->rank > 0)
597
    {
598
      to->rank = from->rank;
599
      to->type = from->type;
600
      to->cray_pointee = from->cray_pointee;
601
      to->cp_was_assumed = from->cp_was_assumed;
602
 
603
      for (i = 0; i < to->corank; i++)
604
        {
605
          to->lower[from->rank + i] = to->lower[i];
606
          to->upper[from->rank + i] = to->upper[i];
607
        }
608
      for (i = 0; i < from->rank; i++)
609
        {
610
          if (copy)
611
            {
612
              to->lower[i] = gfc_copy_expr (from->lower[i]);
613
              to->upper[i] = gfc_copy_expr (from->upper[i]);
614
            }
615
          else
616
            {
617
              to->lower[i] = from->lower[i];
618
              to->upper[i] = from->upper[i];
619
            }
620
        }
621
    }
622
  else if (to->corank == 0 && from->corank > 0)
623
    {
624
      to->corank = from->corank;
625
      to->cotype = from->cotype;
626
 
627
      for (i = 0; i < from->corank; i++)
628
        {
629
          if (copy)
630
            {
631
              to->lower[to->rank + i] = gfc_copy_expr (from->lower[i]);
632
              to->upper[to->rank + i] = gfc_copy_expr (from->upper[i]);
633
            }
634
          else
635
            {
636
              to->lower[to->rank + i] = from->lower[i];
637
              to->upper[to->rank + i] = from->upper[i];
638
            }
639
        }
640
    }
641
}
642
 
643
 
644
/* Match an intent specification.  Since this can only happen after an
645
   INTENT word, a legal intent-spec must follow.  */
646
 
647
static sym_intent
648
match_intent_spec (void)
649
{
650
 
651
  if (gfc_match (" ( in out )") == MATCH_YES)
652
    return INTENT_INOUT;
653
  if (gfc_match (" ( in )") == MATCH_YES)
654
    return INTENT_IN;
655
  if (gfc_match (" ( out )") == MATCH_YES)
656
    return INTENT_OUT;
657
 
658
  gfc_error ("Bad INTENT specification at %C");
659
  return INTENT_UNKNOWN;
660
}
661
 
662
 
663
/* Matches a character length specification, which is either a
664
   specification expression, '*', or ':'.  */
665
 
666
static match
667
char_len_param_value (gfc_expr **expr, bool *deferred)
668
{
669
  match m;
670
 
671
  *expr = NULL;
672
  *deferred = false;
673
 
674
  if (gfc_match_char ('*') == MATCH_YES)
675
    return MATCH_YES;
676
 
677
  if (gfc_match_char (':') == MATCH_YES)
678
    {
679
      if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: deferred type "
680
                          "parameter at %C") == FAILURE)
681
        return MATCH_ERROR;
682
 
683
      *deferred = true;
684
 
685
      return MATCH_YES;
686
    }
687
 
688
  m = gfc_match_expr (expr);
689
 
690
  if (m == MATCH_YES
691
      && gfc_expr_check_typed (*expr, gfc_current_ns, false) == FAILURE)
692
    return MATCH_ERROR;
693
 
694
  if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
695
    {
696
      if ((*expr)->value.function.actual
697
          && (*expr)->value.function.actual->expr->symtree)
698
        {
699
          gfc_expr *e;
700
          e = (*expr)->value.function.actual->expr;
701
          if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
702
              && e->expr_type == EXPR_VARIABLE)
703
            {
704
              if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
705
                goto syntax;
706
              if (e->symtree->n.sym->ts.type == BT_CHARACTER
707
                  && e->symtree->n.sym->ts.u.cl
708
                  && e->symtree->n.sym->ts.u.cl->length->ts.type == BT_UNKNOWN)
709
                goto syntax;
710
            }
711
        }
712
    }
713
  return m;
714
 
715
syntax:
716
  gfc_error ("Conflict in attributes of function argument at %C");
717
  return MATCH_ERROR;
718
}
719
 
720
 
721
/* A character length is a '*' followed by a literal integer or a
722
   char_len_param_value in parenthesis.  */
723
 
724
static match
725
match_char_length (gfc_expr **expr, bool *deferred)
726
{
727
  int length;
728
  match m;
729
 
730
  *deferred = false;
731
  m = gfc_match_char ('*');
732
  if (m != MATCH_YES)
733
    return m;
734
 
735
  m = gfc_match_small_literal_int (&length, NULL);
736
  if (m == MATCH_ERROR)
737
    return m;
738
 
739
  if (m == MATCH_YES)
740
    {
741
      if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
742
                          "Old-style character length at %C") == FAILURE)
743
        return MATCH_ERROR;
744
      *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, length);
745
      return m;
746
    }
747
 
748
  if (gfc_match_char ('(') == MATCH_NO)
749
    goto syntax;
750
 
751
  m = char_len_param_value (expr, deferred);
752
  if (m != MATCH_YES && gfc_matching_function)
753
    {
754
      gfc_undo_symbols ();
755
      m = MATCH_YES;
756
    }
757
 
758
  if (m == MATCH_ERROR)
759
    return m;
760
  if (m == MATCH_NO)
761
    goto syntax;
762
 
763
  if (gfc_match_char (')') == MATCH_NO)
764
    {
765
      gfc_free_expr (*expr);
766
      *expr = NULL;
767
      goto syntax;
768
    }
769
 
770
  return MATCH_YES;
771
 
772
syntax:
773
  gfc_error ("Syntax error in character length specification at %C");
774
  return MATCH_ERROR;
775
}
776
 
777
 
778
/* Special subroutine for finding a symbol.  Check if the name is found
779
   in the current name space.  If not, and we're compiling a function or
780
   subroutine and the parent compilation unit is an interface, then check
781
   to see if the name we've been given is the name of the interface
782
   (located in another namespace).  */
783
 
784
static int
785
find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
786
{
787
  gfc_state_data *s;
788
  gfc_symtree *st;
789
  int i;
790
 
791
  i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
792
  if (i == 0)
793
    {
794
      *result = st ? st->n.sym : NULL;
795
      goto end;
796
    }
797
 
798
  if (gfc_current_state () != COMP_SUBROUTINE
799
      && gfc_current_state () != COMP_FUNCTION)
800
    goto end;
801
 
802
  s = gfc_state_stack->previous;
803
  if (s == NULL)
804
    goto end;
805
 
806
  if (s->state != COMP_INTERFACE)
807
    goto end;
808
  if (s->sym == NULL)
809
    goto end;             /* Nameless interface.  */
810
 
811
  if (strcmp (name, s->sym->name) == 0)
812
    {
813
      *result = s->sym;
814
      return 0;
815
    }
816
 
817
end:
818
  return i;
819
}
820
 
821
 
822
/* Special subroutine for getting a symbol node associated with a
823
   procedure name, used in SUBROUTINE and FUNCTION statements.  The
824
   symbol is created in the parent using with symtree node in the
825
   child unit pointing to the symbol.  If the current namespace has no
826
   parent, then the symbol is just created in the current unit.  */
827
 
828
static int
829
get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
830
{
831
  gfc_symtree *st;
832
  gfc_symbol *sym;
833
  int rc = 0;
834
 
835
  /* Module functions have to be left in their own namespace because
836
     they have potentially (almost certainly!) already been referenced.
837
     In this sense, they are rather like external functions.  This is
838
     fixed up in resolve.c(resolve_entries), where the symbol name-
839
     space is set to point to the master function, so that the fake
840
     result mechanism can work.  */
841
  if (module_fcn_entry)
842
    {
843
      /* Present if entry is declared to be a module procedure.  */
844
      rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
845
 
846
      if (*result == NULL)
847
        rc = gfc_get_symbol (name, NULL, result);
848
      else if (!gfc_get_symbol (name, NULL, &sym) && sym
849
                 && (*result)->ts.type == BT_UNKNOWN
850
                 && sym->attr.flavor == FL_UNKNOWN)
851
        /* Pick up the typespec for the entry, if declared in the function
852
           body.  Note that this symbol is FL_UNKNOWN because it will
853
           only have appeared in a type declaration.  The local symtree
854
           is set to point to the module symbol and a unique symtree
855
           to the local version.  This latter ensures a correct clearing
856
           of the symbols.  */
857
        {
858
          /* If the ENTRY proceeds its specification, we need to ensure
859
             that this does not raise a "has no IMPLICIT type" error.  */
860
          if (sym->ts.type == BT_UNKNOWN)
861
            sym->attr.untyped = 1;
862
 
863
          (*result)->ts = sym->ts;
864
 
865
          /* Put the symbol in the procedure namespace so that, should
866
             the ENTRY precede its specification, the specification
867
             can be applied.  */
868
          (*result)->ns = gfc_current_ns;
869
 
870
          gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
871
          st->n.sym = *result;
872
          st = gfc_get_unique_symtree (gfc_current_ns);
873
          st->n.sym = sym;
874
        }
875
    }
876
  else
877
    rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
878
 
879
  if (rc)
880
    return rc;
881
 
882
  sym = *result;
883
  gfc_current_ns->refs++;
884
 
885
  if (sym && !sym->gfc_new && gfc_current_state () != COMP_INTERFACE)
886
    {
887
      /* Trap another encompassed procedure with the same name.  All
888
         these conditions are necessary to avoid picking up an entry
889
         whose name clashes with that of the encompassing procedure;
890
         this is handled using gsymbols to register unique,globally
891
         accessible names.  */
892
      if (sym->attr.flavor != 0
893
          && sym->attr.proc != 0
894
          && (sym->attr.subroutine || sym->attr.function)
895
          && sym->attr.if_source != IFSRC_UNKNOWN)
896
        gfc_error_now ("Procedure '%s' at %C is already defined at %L",
897
                       name, &sym->declared_at);
898
 
899
      /* Trap a procedure with a name the same as interface in the
900
         encompassing scope.  */
901
      if (sym->attr.generic != 0
902
          && (sym->attr.subroutine || sym->attr.function)
903
          && !sym->attr.mod_proc)
904
        gfc_error_now ("Name '%s' at %C is already defined"
905
                       " as a generic interface at %L",
906
                       name, &sym->declared_at);
907
 
908
      /* Trap declarations of attributes in encompassing scope.  The
909
         signature for this is that ts.kind is set.  Legitimate
910
         references only set ts.type.  */
911
      if (sym->ts.kind != 0
912
          && !sym->attr.implicit_type
913
          && sym->attr.proc == 0
914
          && gfc_current_ns->parent != NULL
915
          && sym->attr.access == 0
916
          && !module_fcn_entry)
917
        gfc_error_now ("Procedure '%s' at %C has an explicit interface "
918
                       "and must not have attributes declared at %L",
919
                       name, &sym->declared_at);
920
    }
921
 
922
  if (gfc_current_ns->parent == NULL || *result == NULL)
923
    return rc;
924
 
925
  /* Module function entries will already have a symtree in
926
     the current namespace but will need one at module level.  */
927
  if (module_fcn_entry)
928
    {
929
      /* Present if entry is declared to be a module procedure.  */
930
      rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
931
      if (st == NULL)
932
        st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
933
    }
934
  else
935
    st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
936
 
937
  st->n.sym = sym;
938
  sym->refs++;
939
 
940
  /* See if the procedure should be a module procedure.  */
941
 
942
  if (((sym->ns->proc_name != NULL
943
                && sym->ns->proc_name->attr.flavor == FL_MODULE
944
                && sym->attr.proc != PROC_MODULE)
945
            || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
946
        && gfc_add_procedure (&sym->attr, PROC_MODULE,
947
                              sym->name, NULL) == FAILURE)
948
    rc = 2;
949
 
950
  return rc;
951
}
952
 
953
 
954
/* Verify that the given symbol representing a parameter is C
955
   interoperable, by checking to see if it was marked as such after
956
   its declaration.  If the given symbol is not interoperable, a
957
   warning is reported, thus removing the need to return the status to
958
   the calling function.  The standard does not require the user use
959
   one of the iso_c_binding named constants to declare an
960
   interoperable parameter, but we can't be sure if the param is C
961
   interop or not if the user doesn't.  For example, integer(4) may be
962
   legal Fortran, but doesn't have meaning in C.  It may interop with
963
   a number of the C types, which causes a problem because the
964
   compiler can't know which one.  This code is almost certainly not
965
   portable, and the user will get what they deserve if the C type
966
   across platforms isn't always interoperable with integer(4).  If
967
   the user had used something like integer(c_int) or integer(c_long),
968
   the compiler could have automatically handled the varying sizes
969
   across platforms.  */
970
 
971
gfc_try
972
gfc_verify_c_interop_param (gfc_symbol *sym)
973
{
974
  int is_c_interop = 0;
975
  gfc_try retval = SUCCESS;
976
 
977
  /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
978
     Don't repeat the checks here.  */
979
  if (sym->attr.implicit_type)
980
    return SUCCESS;
981
 
982
  /* For subroutines or functions that are passed to a BIND(C) procedure,
983
     they're interoperable if they're BIND(C) and their params are all
984
     interoperable.  */
985
  if (sym->attr.flavor == FL_PROCEDURE)
986
    {
987
      if (sym->attr.is_bind_c == 0)
988
        {
989
          gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
990
                         "attribute to be C interoperable", sym->name,
991
                         &(sym->declared_at));
992
 
993
          return FAILURE;
994
        }
995
      else
996
        {
997
          if (sym->attr.is_c_interop == 1)
998
            /* We've already checked this procedure; don't check it again.  */
999
            return SUCCESS;
1000
          else
1001
            return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1002
                                      sym->common_block);
1003
        }
1004
    }
1005
 
1006
  /* See if we've stored a reference to a procedure that owns sym.  */
1007
  if (sym->ns != NULL && sym->ns->proc_name != NULL)
1008
    {
1009
      if (sym->ns->proc_name->attr.is_bind_c == 1)
1010
        {
1011
          is_c_interop = (gfc_verify_c_interop (&(sym->ts)) == SUCCESS ? 1 : 0);
1012
 
1013
          if (is_c_interop != 1)
1014
            {
1015
              /* Make personalized messages to give better feedback.  */
1016
              if (sym->ts.type == BT_DERIVED)
1017
                gfc_error ("Variable '%s' at %L is a dummy argument to the "
1018
                           "BIND(C) procedure '%s' but is not C interoperable "
1019
                           "because derived type '%s' is not C interoperable",
1020
                           sym->name, &(sym->declared_at),
1021
                           sym->ns->proc_name->name,
1022
                           sym->ts.u.derived->name);
1023
              else if (sym->ts.type == BT_CLASS)
1024
                gfc_error ("Variable '%s' at %L is a dummy argument to the "
1025
                           "BIND(C) procedure '%s' but is not C interoperable "
1026
                           "because it is polymorphic",
1027
                           sym->name, &(sym->declared_at),
1028
                           sym->ns->proc_name->name);
1029
              else
1030
                gfc_warning ("Variable '%s' at %L is a parameter to the "
1031
                             "BIND(C) procedure '%s' but may not be C "
1032
                             "interoperable",
1033
                             sym->name, &(sym->declared_at),
1034
                             sym->ns->proc_name->name);
1035
            }
1036
 
1037
          /* Character strings are only C interoperable if they have a
1038
             length of 1.  */
1039
          if (sym->ts.type == BT_CHARACTER)
1040
            {
1041
              gfc_charlen *cl = sym->ts.u.cl;
1042
              if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1043
                  || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1044
                {
1045
                  gfc_error ("Character argument '%s' at %L "
1046
                             "must be length 1 because "
1047
                             "procedure '%s' is BIND(C)",
1048
                             sym->name, &sym->declared_at,
1049
                             sym->ns->proc_name->name);
1050
                  retval = FAILURE;
1051
                }
1052
            }
1053
 
1054
          /* We have to make sure that any param to a bind(c) routine does
1055
             not have the allocatable, pointer, or optional attributes,
1056
             according to J3/04-007, section 5.1.  */
1057
          if (sym->attr.allocatable == 1)
1058
            {
1059
              gfc_error ("Variable '%s' at %L cannot have the "
1060
                         "ALLOCATABLE attribute because procedure '%s'"
1061
                         " is BIND(C)", sym->name, &(sym->declared_at),
1062
                         sym->ns->proc_name->name);
1063
              retval = FAILURE;
1064
            }
1065
 
1066
          if (sym->attr.pointer == 1)
1067
            {
1068
              gfc_error ("Variable '%s' at %L cannot have the "
1069
                         "POINTER attribute because procedure '%s'"
1070
                         " is BIND(C)", sym->name, &(sym->declared_at),
1071
                         sym->ns->proc_name->name);
1072
              retval = FAILURE;
1073
            }
1074
 
1075
          if (sym->attr.optional == 1 && sym->attr.value)
1076
            {
1077
              gfc_error ("Variable '%s' at %L cannot have both the OPTIONAL "
1078
                         "and the VALUE attribute because procedure '%s' "
1079
                         "is BIND(C)", sym->name, &(sym->declared_at),
1080
                         sym->ns->proc_name->name);
1081
              retval = FAILURE;
1082
            }
1083
          else if (sym->attr.optional == 1
1084
                   && gfc_notify_std (GFC_STD_F2008_TS, "TS29113: Variable '%s' "
1085
                                      "at %L with OPTIONAL attribute in "
1086
                                      "procedure '%s' which is BIND(C)",
1087
                                      sym->name, &(sym->declared_at),
1088
                                      sym->ns->proc_name->name)
1089
                      == FAILURE)
1090
            retval = FAILURE;
1091
 
1092
          /* Make sure that if it has the dimension attribute, that it is
1093
             either assumed size or explicit shape.  */
1094
          if (sym->as != NULL)
1095
            {
1096
              if (sym->as->type == AS_ASSUMED_SHAPE)
1097
                {
1098
                  gfc_error ("Assumed-shape array '%s' at %L cannot be an "
1099
                             "argument to the procedure '%s' at %L because "
1100
                             "the procedure is BIND(C)", sym->name,
1101
                             &(sym->declared_at), sym->ns->proc_name->name,
1102
                             &(sym->ns->proc_name->declared_at));
1103
                  retval = FAILURE;
1104
                }
1105
 
1106
              if (sym->as->type == AS_DEFERRED)
1107
                {
1108
                  gfc_error ("Deferred-shape array '%s' at %L cannot be an "
1109
                             "argument to the procedure '%s' at %L because "
1110
                             "the procedure is BIND(C)", sym->name,
1111
                             &(sym->declared_at), sym->ns->proc_name->name,
1112
                             &(sym->ns->proc_name->declared_at));
1113
                  retval = FAILURE;
1114
                }
1115
          }
1116
        }
1117
    }
1118
 
1119
  return retval;
1120
}
1121
 
1122
 
1123
 
1124
/* Function called by variable_decl() that adds a name to the symbol table.  */
1125
 
1126
static gfc_try
1127
build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1128
           gfc_array_spec **as, locus *var_locus)
1129
{
1130
  symbol_attribute attr;
1131
  gfc_symbol *sym;
1132
 
1133
  if (gfc_get_symbol (name, NULL, &sym))
1134
    return FAILURE;
1135
 
1136
  /* Start updating the symbol table.  Add basic type attribute if present.  */
1137
  if (current_ts.type != BT_UNKNOWN
1138
      && (sym->attr.implicit_type == 0
1139
          || !gfc_compare_types (&sym->ts, &current_ts))
1140
      && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
1141
    return FAILURE;
1142
 
1143
  if (sym->ts.type == BT_CHARACTER)
1144
    {
1145
      sym->ts.u.cl = cl;
1146
      sym->ts.deferred = cl_deferred;
1147
    }
1148
 
1149
  /* Add dimension attribute if present.  */
1150
  if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
1151
    return FAILURE;
1152
  *as = NULL;
1153
 
1154
  /* Add attribute to symbol.  The copy is so that we can reset the
1155
     dimension attribute.  */
1156
  attr = current_attr;
1157
  attr.dimension = 0;
1158
  attr.codimension = 0;
1159
 
1160
  if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1161
    return FAILURE;
1162
 
1163
  /* Finish any work that may need to be done for the binding label,
1164
     if it's a bind(c).  The bind(c) attr is found before the symbol
1165
     is made, and before the symbol name (for data decls), so the
1166
     current_ts is holding the binding label, or nothing if the
1167
     name= attr wasn't given.  Therefore, test here if we're dealing
1168
     with a bind(c) and make sure the binding label is set correctly.  */
1169
  if (sym->attr.is_bind_c == 1)
1170
    {
1171
      if (!sym->binding_label)
1172
        {
1173
          /* Set the binding label and verify that if a NAME= was specified
1174
             then only one identifier was in the entity-decl-list.  */
1175
          if (set_binding_label (&sym->binding_label, sym->name,
1176
                                 num_idents_on_line) == FAILURE)
1177
            return FAILURE;
1178
        }
1179
    }
1180
 
1181
  /* See if we know we're in a common block, and if it's a bind(c)
1182
     common then we need to make sure we're an interoperable type.  */
1183
  if (sym->attr.in_common == 1)
1184
    {
1185
      /* Test the common block object.  */
1186
      if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1187
          && sym->ts.is_c_interop != 1)
1188
        {
1189
          gfc_error_now ("Variable '%s' in common block '%s' at %C "
1190
                         "must be declared with a C interoperable "
1191
                         "kind since common block '%s' is BIND(C)",
1192
                         sym->name, sym->common_block->name,
1193
                         sym->common_block->name);
1194
          gfc_clear_error ();
1195
        }
1196
    }
1197
 
1198
  sym->attr.implied_index = 0;
1199
 
1200
  if (sym->ts.type == BT_CLASS)
1201
    return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false);
1202
 
1203
  return SUCCESS;
1204
}
1205
 
1206
 
1207
/* Set character constant to the given length. The constant will be padded or
1208
   truncated.  If we're inside an array constructor without a typespec, we
1209
   additionally check that all elements have the same length; check_len -1
1210
   means no checking.  */
1211
 
1212
void
1213
gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1214
{
1215
  gfc_char_t *s;
1216
  int slen;
1217
 
1218
  gcc_assert (expr->expr_type == EXPR_CONSTANT);
1219
  gcc_assert (expr->ts.type == BT_CHARACTER);
1220
 
1221
  slen = expr->value.character.length;
1222
  if (len != slen)
1223
    {
1224
      s = gfc_get_wide_string (len + 1);
1225
      memcpy (s, expr->value.character.string,
1226
              MIN (len, slen) * sizeof (gfc_char_t));
1227
      if (len > slen)
1228
        gfc_wide_memset (&s[slen], ' ', len - slen);
1229
 
1230
      if (gfc_option.warn_character_truncation && slen > len)
1231
        gfc_warning_now ("CHARACTER expression at %L is being truncated "
1232
                         "(%d/%d)", &expr->where, slen, len);
1233
 
1234
      /* Apply the standard by 'hand' otherwise it gets cleared for
1235
         initializers.  */
1236
      if (check_len != -1 && slen != check_len
1237
          && !(gfc_option.allow_std & GFC_STD_GNU))
1238
        gfc_error_now ("The CHARACTER elements of the array constructor "
1239
                       "at %L must have the same length (%d/%d)",
1240
                        &expr->where, slen, check_len);
1241
 
1242
      s[len] = '\0';
1243
      free (expr->value.character.string);
1244
      expr->value.character.string = s;
1245
      expr->value.character.length = len;
1246
    }
1247
}
1248
 
1249
 
1250
/* Function to create and update the enumerator history
1251
   using the information passed as arguments.
1252
   Pointer "max_enum" is also updated, to point to
1253
   enum history node containing largest initializer.
1254
 
1255
   SYM points to the symbol node of enumerator.
1256
   INIT points to its enumerator value.  */
1257
 
1258
static void
1259
create_enum_history (gfc_symbol *sym, gfc_expr *init)
1260
{
1261
  enumerator_history *new_enum_history;
1262
  gcc_assert (sym != NULL && init != NULL);
1263
 
1264
  new_enum_history = XCNEW (enumerator_history);
1265
 
1266
  new_enum_history->sym = sym;
1267
  new_enum_history->initializer = init;
1268
  new_enum_history->next = NULL;
1269
 
1270
  if (enum_history == NULL)
1271
    {
1272
      enum_history = new_enum_history;
1273
      max_enum = enum_history;
1274
    }
1275
  else
1276
    {
1277
      new_enum_history->next = enum_history;
1278
      enum_history = new_enum_history;
1279
 
1280
      if (mpz_cmp (max_enum->initializer->value.integer,
1281
                   new_enum_history->initializer->value.integer) < 0)
1282
        max_enum = new_enum_history;
1283
    }
1284
}
1285
 
1286
 
1287
/* Function to free enum kind history.  */
1288
 
1289
void
1290
gfc_free_enum_history (void)
1291
{
1292
  enumerator_history *current = enum_history;
1293
  enumerator_history *next;
1294
 
1295
  while (current != NULL)
1296
    {
1297
      next = current->next;
1298
      free (current);
1299
      current = next;
1300
    }
1301
  max_enum = NULL;
1302
  enum_history = NULL;
1303
}
1304
 
1305
 
1306
/* Function called by variable_decl() that adds an initialization
1307
   expression to a symbol.  */
1308
 
1309
static gfc_try
1310
add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1311
{
1312
  symbol_attribute attr;
1313
  gfc_symbol *sym;
1314
  gfc_expr *init;
1315
 
1316
  init = *initp;
1317
  if (find_special (name, &sym, false))
1318
    return FAILURE;
1319
 
1320
  attr = sym->attr;
1321
 
1322
  /* If this symbol is confirming an implicit parameter type,
1323
     then an initialization expression is not allowed.  */
1324
  if (attr.flavor == FL_PARAMETER
1325
      && sym->value != NULL
1326
      && *initp != NULL)
1327
    {
1328
      gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1329
                 sym->name);
1330
      return FAILURE;
1331
    }
1332
 
1333
  if (init == NULL)
1334
    {
1335
      /* An initializer is required for PARAMETER declarations.  */
1336
      if (attr.flavor == FL_PARAMETER)
1337
        {
1338
          gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1339
          return FAILURE;
1340
        }
1341
    }
1342
  else
1343
    {
1344
      /* If a variable appears in a DATA block, it cannot have an
1345
         initializer.  */
1346
      if (sym->attr.data)
1347
        {
1348
          gfc_error ("Variable '%s' at %C with an initializer already "
1349
                     "appears in a DATA statement", sym->name);
1350
          return FAILURE;
1351
        }
1352
 
1353
      /* Check if the assignment can happen. This has to be put off
1354
         until later for derived type variables and procedure pointers.  */
1355
      if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1356
          && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1357
          && !sym->attr.proc_pointer
1358
          && gfc_check_assign_symbol (sym, init) == FAILURE)
1359
        return FAILURE;
1360
 
1361
      if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1362
            && init->ts.type == BT_CHARACTER)
1363
        {
1364
          /* Update symbol character length according initializer.  */
1365
          if (gfc_check_assign_symbol (sym, init) == FAILURE)
1366
            return FAILURE;
1367
 
1368
          if (sym->ts.u.cl->length == NULL)
1369
            {
1370
              int clen;
1371
              /* If there are multiple CHARACTER variables declared on the
1372
                 same line, we don't want them to share the same length.  */
1373
              sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1374
 
1375
              if (sym->attr.flavor == FL_PARAMETER)
1376
                {
1377
                  if (init->expr_type == EXPR_CONSTANT)
1378
                    {
1379
                      clen = init->value.character.length;
1380
                      sym->ts.u.cl->length
1381
                                = gfc_get_int_expr (gfc_default_integer_kind,
1382
                                                    NULL, clen);
1383
                    }
1384
                  else if (init->expr_type == EXPR_ARRAY)
1385
                    {
1386
                      gfc_constructor *c;
1387
                      c = gfc_constructor_first (init->value.constructor);
1388
                      clen = c->expr->value.character.length;
1389
                      sym->ts.u.cl->length
1390
                                = gfc_get_int_expr (gfc_default_integer_kind,
1391
                                                    NULL, clen);
1392
                    }
1393
                  else if (init->ts.u.cl && init->ts.u.cl->length)
1394
                    sym->ts.u.cl->length =
1395
                                gfc_copy_expr (sym->value->ts.u.cl->length);
1396
                }
1397
            }
1398
          /* Update initializer character length according symbol.  */
1399
          else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1400
            {
1401
              int len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1402
 
1403
              if (init->expr_type == EXPR_CONSTANT)
1404
                gfc_set_constant_character_len (len, init, -1);
1405
              else if (init->expr_type == EXPR_ARRAY)
1406
                {
1407
                  gfc_constructor *c;
1408
 
1409
                  /* Build a new charlen to prevent simplification from
1410
                     deleting the length before it is resolved.  */
1411
                  init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1412
                  init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1413
 
1414
                  for (c = gfc_constructor_first (init->value.constructor);
1415
                       c; c = gfc_constructor_next (c))
1416
                    gfc_set_constant_character_len (len, c->expr, -1);
1417
                }
1418
            }
1419
        }
1420
 
1421
      /* If sym is implied-shape, set its upper bounds from init.  */
1422
      if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1423
          && sym->as->type == AS_IMPLIED_SHAPE)
1424
        {
1425
          int dim;
1426
 
1427
          if (init->rank == 0)
1428
            {
1429
              gfc_error ("Can't initialize implied-shape array at %L"
1430
                         " with scalar", &sym->declared_at);
1431
              return FAILURE;
1432
            }
1433
          gcc_assert (sym->as->rank == init->rank);
1434
 
1435
          /* Shape should be present, we get an initialization expression.  */
1436
          gcc_assert (init->shape);
1437
 
1438
          for (dim = 0; dim < sym->as->rank; ++dim)
1439
            {
1440
              int k;
1441
              gfc_expr* lower;
1442
              gfc_expr* e;
1443
 
1444
              lower = sym->as->lower[dim];
1445
              if (lower->expr_type != EXPR_CONSTANT)
1446
                {
1447
                  gfc_error ("Non-constant lower bound in implied-shape"
1448
                             " declaration at %L", &lower->where);
1449
                  return FAILURE;
1450
                }
1451
 
1452
              /* All dimensions must be without upper bound.  */
1453
              gcc_assert (!sym->as->upper[dim]);
1454
 
1455
              k = lower->ts.kind;
1456
              e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1457
              mpz_add (e->value.integer,
1458
                       lower->value.integer, init->shape[dim]);
1459
              mpz_sub_ui (e->value.integer, e->value.integer, 1);
1460
              sym->as->upper[dim] = e;
1461
            }
1462
 
1463
          sym->as->type = AS_EXPLICIT;
1464
        }
1465
 
1466
      /* Need to check if the expression we initialized this
1467
         to was one of the iso_c_binding named constants.  If so,
1468
         and we're a parameter (constant), let it be iso_c.
1469
         For example:
1470
         integer(c_int), parameter :: my_int = c_int
1471
         integer(my_int) :: my_int_2
1472
         If we mark my_int as iso_c (since we can see it's value
1473
         is equal to one of the named constants), then my_int_2
1474
         will be considered C interoperable.  */
1475
      if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1476
        {
1477
          sym->ts.is_iso_c |= init->ts.is_iso_c;
1478
          sym->ts.is_c_interop |= init->ts.is_c_interop;
1479
          /* attr bits needed for module files.  */
1480
          sym->attr.is_iso_c |= init->ts.is_iso_c;
1481
          sym->attr.is_c_interop |= init->ts.is_c_interop;
1482
          if (init->ts.is_iso_c)
1483
            sym->ts.f90_type = init->ts.f90_type;
1484
        }
1485
 
1486
      /* Add initializer.  Make sure we keep the ranks sane.  */
1487
      if (sym->attr.dimension && init->rank == 0)
1488
        {
1489
          mpz_t size;
1490
          gfc_expr *array;
1491
          int n;
1492
          if (sym->attr.flavor == FL_PARAMETER
1493
                && init->expr_type == EXPR_CONSTANT
1494
                && spec_size (sym->as, &size) == SUCCESS
1495
                && mpz_cmp_si (size, 0) > 0)
1496
            {
1497
              array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1498
                                          &init->where);
1499
              for (n = 0; n < (int)mpz_get_si (size); n++)
1500
                gfc_constructor_append_expr (&array->value.constructor,
1501
                                             n == 0
1502
                                                ? init
1503
                                                : gfc_copy_expr (init),
1504
                                             &init->where);
1505
 
1506
              array->shape = gfc_get_shape (sym->as->rank);
1507
              for (n = 0; n < sym->as->rank; n++)
1508
                spec_dimen_size (sym->as, n, &array->shape[n]);
1509
 
1510
              init = array;
1511
              mpz_clear (size);
1512
            }
1513
          init->rank = sym->as->rank;
1514
        }
1515
 
1516
      sym->value = init;
1517
      if (sym->attr.save == SAVE_NONE)
1518
        sym->attr.save = SAVE_IMPLICIT;
1519
      *initp = NULL;
1520
    }
1521
 
1522
  return SUCCESS;
1523
}
1524
 
1525
 
1526
/* Function called by variable_decl() that adds a name to a structure
1527
   being built.  */
1528
 
1529
static gfc_try
1530
build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1531
              gfc_array_spec **as)
1532
{
1533
  gfc_component *c;
1534
  gfc_try t = SUCCESS;
1535
 
1536
  /* F03:C438/C439. If the current symbol is of the same derived type that we're
1537
     constructing, it must have the pointer attribute.  */
1538
  if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1539
      && current_ts.u.derived == gfc_current_block ()
1540
      && current_attr.pointer == 0)
1541
    {
1542
      gfc_error ("Component at %C must have the POINTER attribute");
1543
      return FAILURE;
1544
    }
1545
 
1546
  if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1547
    {
1548
      if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1549
        {
1550
          gfc_error ("Array component of structure at %C must have explicit "
1551
                     "or deferred shape");
1552
          return FAILURE;
1553
        }
1554
    }
1555
 
1556
  if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1557
    return FAILURE;
1558
 
1559
  c->ts = current_ts;
1560
  if (c->ts.type == BT_CHARACTER)
1561
    c->ts.u.cl = cl;
1562
  c->attr = current_attr;
1563
 
1564
  c->initializer = *init;
1565
  *init = NULL;
1566
 
1567
  c->as = *as;
1568
  if (c->as != NULL)
1569
    {
1570
      if (c->as->corank)
1571
        c->attr.codimension = 1;
1572
      if (c->as->rank)
1573
        c->attr.dimension = 1;
1574
    }
1575
  *as = NULL;
1576
 
1577
  /* Should this ever get more complicated, combine with similar section
1578
     in add_init_expr_to_sym into a separate function.  */
1579
  if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer
1580
      && c->ts.u.cl
1581
      && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1582
    {
1583
      int len;
1584
 
1585
      gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1586
      gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1587
      gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1588
 
1589
      len = mpz_get_si (c->ts.u.cl->length->value.integer);
1590
 
1591
      if (c->initializer->expr_type == EXPR_CONSTANT)
1592
        gfc_set_constant_character_len (len, c->initializer, -1);
1593
      else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1594
                        c->initializer->ts.u.cl->length->value.integer))
1595
        {
1596
          gfc_constructor *ctor;
1597
          ctor = gfc_constructor_first (c->initializer->value.constructor);
1598
 
1599
          if (ctor)
1600
            {
1601
              int first_len;
1602
              bool has_ts = (c->initializer->ts.u.cl
1603
                             && c->initializer->ts.u.cl->length_from_typespec);
1604
 
1605
              /* Remember the length of the first element for checking
1606
                 that all elements *in the constructor* have the same
1607
                 length.  This need not be the length of the LHS!  */
1608
              gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1609
              gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1610
              first_len = ctor->expr->value.character.length;
1611
 
1612
              for ( ; ctor; ctor = gfc_constructor_next (ctor))
1613
                if (ctor->expr->expr_type == EXPR_CONSTANT)
1614
                {
1615
                  gfc_set_constant_character_len (len, ctor->expr,
1616
                                                  has_ts ? -1 : first_len);
1617
                  ctor->expr->ts.u.cl->length = gfc_copy_expr (c->ts.u.cl->length);
1618
                }
1619
            }
1620
        }
1621
    }
1622
 
1623
  /* Check array components.  */
1624
  if (!c->attr.dimension)
1625
    goto scalar;
1626
 
1627
  if (c->attr.pointer)
1628
    {
1629
      if (c->as->type != AS_DEFERRED)
1630
        {
1631
          gfc_error ("Pointer array component of structure at %C must have a "
1632
                     "deferred shape");
1633
          t = FAILURE;
1634
        }
1635
    }
1636
  else if (c->attr.allocatable)
1637
    {
1638
      if (c->as->type != AS_DEFERRED)
1639
        {
1640
          gfc_error ("Allocatable component of structure at %C must have a "
1641
                     "deferred shape");
1642
          t = FAILURE;
1643
        }
1644
    }
1645
  else
1646
    {
1647
      if (c->as->type != AS_EXPLICIT)
1648
        {
1649
          gfc_error ("Array component of structure at %C must have an "
1650
                     "explicit shape");
1651
          t = FAILURE;
1652
        }
1653
    }
1654
 
1655
scalar:
1656
  if (c->ts.type == BT_CLASS)
1657
    {
1658
      bool delayed = (gfc_state_stack->sym == c->ts.u.derived)
1659
                     || (!c->ts.u.derived->components
1660
                         && !c->ts.u.derived->attr.zero_comp);
1661
      return gfc_build_class_symbol (&c->ts, &c->attr, &c->as, delayed);
1662
    }
1663
 
1664
  return t;
1665
}
1666
 
1667
 
1668
/* Match a 'NULL()', and possibly take care of some side effects.  */
1669
 
1670
match
1671
gfc_match_null (gfc_expr **result)
1672
{
1673
  gfc_symbol *sym;
1674
  match m;
1675
 
1676
  m = gfc_match (" null ( )");
1677
  if (m != MATCH_YES)
1678
    return m;
1679
 
1680
  /* The NULL symbol now has to be/become an intrinsic function.  */
1681
  if (gfc_get_symbol ("null", NULL, &sym))
1682
    {
1683
      gfc_error ("NULL() initialization at %C is ambiguous");
1684
      return MATCH_ERROR;
1685
    }
1686
 
1687
  gfc_intrinsic_symbol (sym);
1688
 
1689
  if (sym->attr.proc != PROC_INTRINSIC
1690
      && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1691
                             sym->name, NULL) == FAILURE
1692
          || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1693
    return MATCH_ERROR;
1694
 
1695
  *result = gfc_get_null_expr (&gfc_current_locus);
1696
 
1697
  return MATCH_YES;
1698
}
1699
 
1700
 
1701
/* Match the initialization expr for a data pointer or procedure pointer.  */
1702
 
1703
static match
1704
match_pointer_init (gfc_expr **init, int procptr)
1705
{
1706
  match m;
1707
 
1708
  if (gfc_pure (NULL) && gfc_state_stack->state != COMP_DERIVED)
1709
    {
1710
      gfc_error ("Initialization of pointer at %C is not allowed in "
1711
                 "a PURE procedure");
1712
      return MATCH_ERROR;
1713
    }
1714
 
1715
  /* Match NULL() initilization.  */
1716
  m = gfc_match_null (init);
1717
  if (m != MATCH_NO)
1718
    return m;
1719
 
1720
  /* Match non-NULL initialization.  */
1721
  gfc_matching_ptr_assignment = !procptr;
1722
  gfc_matching_procptr_assignment = procptr;
1723
  m = gfc_match_rvalue (init);
1724
  gfc_matching_ptr_assignment = 0;
1725
  gfc_matching_procptr_assignment = 0;
1726
  if (m == MATCH_ERROR)
1727
    return MATCH_ERROR;
1728
  else if (m == MATCH_NO)
1729
    {
1730
      gfc_error ("Error in pointer initialization at %C");
1731
      return MATCH_ERROR;
1732
    }
1733
 
1734
  if (!procptr)
1735
    gfc_resolve_expr (*init);
1736
 
1737
  if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: non-NULL pointer "
1738
                      "initialization at %C") == FAILURE)
1739
    return MATCH_ERROR;
1740
 
1741
  return MATCH_YES;
1742
}
1743
 
1744
 
1745
static gfc_try
1746
check_function_name (char *name)
1747
{
1748
  /* In functions that have a RESULT variable defined, the function name always
1749
     refers to function calls.  Therefore, the name is not allowed to appear in
1750
     specification statements. When checking this, be careful about
1751
     'hidden' procedure pointer results ('ppr@').  */
1752
 
1753
  if (gfc_current_state () == COMP_FUNCTION)
1754
    {
1755
      gfc_symbol *block = gfc_current_block ();
1756
      if (block && block->result && block->result != block
1757
          && strcmp (block->result->name, "ppr@") != 0
1758
          && strcmp (block->name, name) == 0)
1759
        {
1760
          gfc_error ("Function name '%s' not allowed at %C", name);
1761
          return FAILURE;
1762
        }
1763
    }
1764
 
1765
  return SUCCESS;
1766
}
1767
 
1768
 
1769
/* Match a variable name with an optional initializer.  When this
1770
   subroutine is called, a variable is expected to be parsed next.
1771
   Depending on what is happening at the moment, updates either the
1772
   symbol table or the current interface.  */
1773
 
1774
static match
1775
variable_decl (int elem)
1776
{
1777
  char name[GFC_MAX_SYMBOL_LEN + 1];
1778
  gfc_expr *initializer, *char_len;
1779
  gfc_array_spec *as;
1780
  gfc_array_spec *cp_as; /* Extra copy for Cray Pointees.  */
1781
  gfc_charlen *cl;
1782
  bool cl_deferred;
1783
  locus var_locus;
1784
  match m;
1785
  gfc_try t;
1786
  gfc_symbol *sym;
1787
 
1788
  initializer = NULL;
1789
  as = NULL;
1790
  cp_as = NULL;
1791
 
1792
  /* When we get here, we've just matched a list of attributes and
1793
     maybe a type and a double colon.  The next thing we expect to see
1794
     is the name of the symbol.  */
1795
  m = gfc_match_name (name);
1796
  if (m != MATCH_YES)
1797
    goto cleanup;
1798
 
1799
  var_locus = gfc_current_locus;
1800
 
1801
  /* Now we could see the optional array spec. or character length.  */
1802
  m = gfc_match_array_spec (&as, true, true);
1803
  if (m == MATCH_ERROR)
1804
    goto cleanup;
1805
 
1806
  if (m == MATCH_NO)
1807
    as = gfc_copy_array_spec (current_as);
1808
  else if (current_as)
1809
    merge_array_spec (current_as, as, true);
1810
 
1811
  if (gfc_option.flag_cray_pointer)
1812
    cp_as = gfc_copy_array_spec (as);
1813
 
1814
  /* At this point, we know for sure if the symbol is PARAMETER and can thus
1815
     determine (and check) whether it can be implied-shape.  If it
1816
     was parsed as assumed-size, change it because PARAMETERs can not
1817
     be assumed-size.  */
1818
  if (as)
1819
    {
1820
      if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
1821
        {
1822
          m = MATCH_ERROR;
1823
          gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1824
                     name, &var_locus);
1825
          goto cleanup;
1826
        }
1827
 
1828
      if (as->type == AS_ASSUMED_SIZE && as->rank == 1
1829
          && current_attr.flavor == FL_PARAMETER)
1830
        as->type = AS_IMPLIED_SHAPE;
1831
 
1832
      if (as->type == AS_IMPLIED_SHAPE
1833
          && gfc_notify_std (GFC_STD_F2008,
1834
                             "Fortran 2008: Implied-shape array at %L",
1835
                             &var_locus) == FAILURE)
1836
        {
1837
          m = MATCH_ERROR;
1838
          goto cleanup;
1839
        }
1840
    }
1841
 
1842
  char_len = NULL;
1843
  cl = NULL;
1844
  cl_deferred = false;
1845
 
1846
  if (current_ts.type == BT_CHARACTER)
1847
    {
1848
      switch (match_char_length (&char_len, &cl_deferred))
1849
        {
1850
        case MATCH_YES:
1851
          cl = gfc_new_charlen (gfc_current_ns, NULL);
1852
 
1853
          cl->length = char_len;
1854
          break;
1855
 
1856
        /* Non-constant lengths need to be copied after the first
1857
           element.  Also copy assumed lengths.  */
1858
        case MATCH_NO:
1859
          if (elem > 1
1860
              && (current_ts.u.cl->length == NULL
1861
                  || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
1862
            {
1863
              cl = gfc_new_charlen (gfc_current_ns, NULL);
1864
              cl->length = gfc_copy_expr (current_ts.u.cl->length);
1865
            }
1866
          else
1867
            cl = current_ts.u.cl;
1868
 
1869
          cl_deferred = current_ts.deferred;
1870
 
1871
          break;
1872
 
1873
        case MATCH_ERROR:
1874
          goto cleanup;
1875
        }
1876
    }
1877
 
1878
  /*  If this symbol has already shown up in a Cray Pointer declaration,
1879
      then we want to set the type & bail out.  */
1880
  if (gfc_option.flag_cray_pointer)
1881
    {
1882
      gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1883
      if (sym != NULL && sym->attr.cray_pointee)
1884
        {
1885
          sym->ts.type = current_ts.type;
1886
          sym->ts.kind = current_ts.kind;
1887
          sym->ts.u.cl = cl;
1888
          sym->ts.u.derived = current_ts.u.derived;
1889
          sym->ts.is_c_interop = current_ts.is_c_interop;
1890
          sym->ts.is_iso_c = current_ts.is_iso_c;
1891
          m = MATCH_YES;
1892
 
1893
          /* Check to see if we have an array specification.  */
1894
          if (cp_as != NULL)
1895
            {
1896
              if (sym->as != NULL)
1897
                {
1898
                  gfc_error ("Duplicate array spec for Cray pointee at %C");
1899
                  gfc_free_array_spec (cp_as);
1900
                  m = MATCH_ERROR;
1901
                  goto cleanup;
1902
                }
1903
              else
1904
                {
1905
                  if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1906
                    gfc_internal_error ("Couldn't set pointee array spec.");
1907
 
1908
                  /* Fix the array spec.  */
1909
                  m = gfc_mod_pointee_as (sym->as);
1910
                  if (m == MATCH_ERROR)
1911
                    goto cleanup;
1912
                }
1913
            }
1914
          goto cleanup;
1915
        }
1916
      else
1917
        {
1918
          gfc_free_array_spec (cp_as);
1919
        }
1920
    }
1921
 
1922
  /* Procedure pointer as function result.  */
1923
  if (gfc_current_state () == COMP_FUNCTION
1924
      && strcmp ("ppr@", gfc_current_block ()->name) == 0
1925
      && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
1926
    strcpy (name, "ppr@");
1927
 
1928
  if (gfc_current_state () == COMP_FUNCTION
1929
      && strcmp (name, gfc_current_block ()->name) == 0
1930
      && gfc_current_block ()->result
1931
      && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
1932
    strcpy (name, "ppr@");
1933
 
1934
  /* OK, we've successfully matched the declaration.  Now put the
1935
     symbol in the current namespace, because it might be used in the
1936
     optional initialization expression for this symbol, e.g. this is
1937
     perfectly legal:
1938
 
1939
     integer, parameter :: i = huge(i)
1940
 
1941
     This is only true for parameters or variables of a basic type.
1942
     For components of derived types, it is not true, so we don't
1943
     create a symbol for those yet.  If we fail to create the symbol,
1944
     bail out.  */
1945
  if (gfc_current_state () != COMP_DERIVED
1946
      && build_sym (name, cl, cl_deferred, &as, &var_locus) == FAILURE)
1947
    {
1948
      m = MATCH_ERROR;
1949
      goto cleanup;
1950
    }
1951
 
1952
  /* An interface body specifies all of the procedure's
1953
     characteristics and these shall be consistent with those
1954
     specified in the procedure definition, except that the interface
1955
     may specify a procedure that is not pure if the procedure is
1956
     defined to be pure(12.3.2).  */
1957
  if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1958
      && gfc_current_ns->proc_name
1959
      && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1960
      && current_ts.u.derived->ns != gfc_current_ns)
1961
    {
1962
      gfc_symtree *st;
1963
      st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.u.derived->name);
1964
      if (!(current_ts.u.derived->attr.imported
1965
                && st != NULL
1966
                && gfc_find_dt_in_generic (st->n.sym) == current_ts.u.derived)
1967
            && !gfc_current_ns->has_import_set)
1968
        {
1969
            gfc_error ("The type of '%s' at %C has not been declared within the "
1970
                       "interface", name);
1971
            m = MATCH_ERROR;
1972
            goto cleanup;
1973
        }
1974
    }
1975
 
1976
  if (check_function_name (name) == FAILURE)
1977
    {
1978
      m = MATCH_ERROR;
1979
      goto cleanup;
1980
    }
1981
 
1982
  /* We allow old-style initializations of the form
1983
       integer i /2/, j(4) /3*3, 1/
1984
     (if no colon has been seen). These are different from data
1985
     statements in that initializers are only allowed to apply to the
1986
     variable immediately preceding, i.e.
1987
       integer i, j /1, 2/
1988
     is not allowed. Therefore we have to do some work manually, that
1989
     could otherwise be left to the matchers for DATA statements.  */
1990
 
1991
  if (!colon_seen && gfc_match (" /") == MATCH_YES)
1992
    {
1993
      if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1994
                          "initialization at %C") == FAILURE)
1995
        return MATCH_ERROR;
1996
 
1997
      return match_old_style_init (name);
1998
    }
1999
 
2000
  /* The double colon must be present in order to have initializers.
2001
     Otherwise the statement is ambiguous with an assignment statement.  */
2002
  if (colon_seen)
2003
    {
2004
      if (gfc_match (" =>") == MATCH_YES)
2005
        {
2006
          if (!current_attr.pointer)
2007
            {
2008
              gfc_error ("Initialization at %C isn't for a pointer variable");
2009
              m = MATCH_ERROR;
2010
              goto cleanup;
2011
            }
2012
 
2013
          m = match_pointer_init (&initializer, 0);
2014
          if (m != MATCH_YES)
2015
            goto cleanup;
2016
        }
2017
      else if (gfc_match_char ('=') == MATCH_YES)
2018
        {
2019
          if (current_attr.pointer)
2020
            {
2021
              gfc_error ("Pointer initialization at %C requires '=>', "
2022
                         "not '='");
2023
              m = MATCH_ERROR;
2024
              goto cleanup;
2025
            }
2026
 
2027
          m = gfc_match_init_expr (&initializer);
2028
          if (m == MATCH_NO)
2029
            {
2030
              gfc_error ("Expected an initialization expression at %C");
2031
              m = MATCH_ERROR;
2032
            }
2033
 
2034
          if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2035
              && gfc_state_stack->state != COMP_DERIVED)
2036
            {
2037
              gfc_error ("Initialization of variable at %C is not allowed in "
2038
                         "a PURE procedure");
2039
              m = MATCH_ERROR;
2040
            }
2041
 
2042
          if (m != MATCH_YES)
2043
            goto cleanup;
2044
        }
2045
    }
2046
 
2047
  if (initializer != NULL && current_attr.allocatable
2048
        && gfc_current_state () == COMP_DERIVED)
2049
    {
2050
      gfc_error ("Initialization of allocatable component at %C is not "
2051
                 "allowed");
2052
      m = MATCH_ERROR;
2053
      goto cleanup;
2054
    }
2055
 
2056
  /* Add the initializer.  Note that it is fine if initializer is
2057
     NULL here, because we sometimes also need to check if a
2058
     declaration *must* have an initialization expression.  */
2059
  if (gfc_current_state () != COMP_DERIVED)
2060
    t = add_init_expr_to_sym (name, &initializer, &var_locus);
2061
  else
2062
    {
2063
      if (current_ts.type == BT_DERIVED
2064
          && !current_attr.pointer && !initializer)
2065
        initializer = gfc_default_initializer (&current_ts);
2066
      t = build_struct (name, cl, &initializer, &as);
2067
    }
2068
 
2069
  m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
2070
 
2071
cleanup:
2072
  /* Free stuff up and return.  */
2073
  gfc_free_expr (initializer);
2074
  gfc_free_array_spec (as);
2075
 
2076
  return m;
2077
}
2078
 
2079
 
2080
/* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2081
   This assumes that the byte size is equal to the kind number for
2082
   non-COMPLEX types, and equal to twice the kind number for COMPLEX.  */
2083
 
2084
match
2085
gfc_match_old_kind_spec (gfc_typespec *ts)
2086
{
2087
  match m;
2088
  int original_kind;
2089
 
2090
  if (gfc_match_char ('*') != MATCH_YES)
2091
    return MATCH_NO;
2092
 
2093
  m = gfc_match_small_literal_int (&ts->kind, NULL);
2094
  if (m != MATCH_YES)
2095
    return MATCH_ERROR;
2096
 
2097
  original_kind = ts->kind;
2098
 
2099
  /* Massage the kind numbers for complex types.  */
2100
  if (ts->type == BT_COMPLEX)
2101
    {
2102
      if (ts->kind % 2)
2103
        {
2104
          gfc_error ("Old-style type declaration %s*%d not supported at %C",
2105
                     gfc_basic_typename (ts->type), original_kind);
2106
          return MATCH_ERROR;
2107
        }
2108
      ts->kind /= 2;
2109
 
2110
    }
2111
 
2112
  if (ts->type == BT_INTEGER && ts->kind == 4 && gfc_option.flag_integer4_kind == 8)
2113
    ts->kind = 8;
2114
 
2115
  if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2116
    {
2117
      if (ts->kind == 4)
2118
        {
2119
          if (gfc_option.flag_real4_kind == 8)
2120
            ts->kind =  8;
2121
          if (gfc_option.flag_real4_kind == 10)
2122
            ts->kind = 10;
2123
          if (gfc_option.flag_real4_kind == 16)
2124
            ts->kind = 16;
2125
        }
2126
 
2127
      if (ts->kind == 8)
2128
        {
2129
          if (gfc_option.flag_real8_kind == 4)
2130
            ts->kind = 4;
2131
          if (gfc_option.flag_real8_kind == 10)
2132
            ts->kind = 10;
2133
          if (gfc_option.flag_real8_kind == 16)
2134
            ts->kind = 16;
2135
        }
2136
    }
2137
 
2138
  if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2139
    {
2140
      gfc_error ("Old-style type declaration %s*%d not supported at %C",
2141
                 gfc_basic_typename (ts->type), original_kind);
2142
      return MATCH_ERROR;
2143
    }
2144
 
2145
  if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
2146
                      gfc_basic_typename (ts->type), original_kind) == FAILURE)
2147
    return MATCH_ERROR;
2148
 
2149
  return MATCH_YES;
2150
}
2151
 
2152
 
2153
/* Match a kind specification.  Since kinds are generally optional, we
2154
   usually return MATCH_NO if something goes wrong.  If a "kind="
2155
   string is found, then we know we have an error.  */
2156
 
2157
match
2158
gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2159
{
2160
  locus where, loc;
2161
  gfc_expr *e;
2162
  match m, n;
2163
  char c;
2164
  const char *msg;
2165
 
2166
  m = MATCH_NO;
2167
  n = MATCH_YES;
2168
  e = NULL;
2169
 
2170
  where = loc = gfc_current_locus;
2171
 
2172
  if (kind_expr_only)
2173
    goto kind_expr;
2174
 
2175
  if (gfc_match_char ('(') == MATCH_NO)
2176
    return MATCH_NO;
2177
 
2178
  /* Also gobbles optional text.  */
2179
  if (gfc_match (" kind = ") == MATCH_YES)
2180
    m = MATCH_ERROR;
2181
 
2182
  loc = gfc_current_locus;
2183
 
2184
kind_expr:
2185
  n = gfc_match_init_expr (&e);
2186
 
2187
  if (n != MATCH_YES)
2188
    {
2189
      if (gfc_matching_function)
2190
        {
2191
          /* The function kind expression might include use associated or
2192
             imported parameters and try again after the specification
2193
             expressions.....  */
2194
          if (gfc_match_char (')') != MATCH_YES)
2195
            {
2196
              gfc_error ("Missing right parenthesis at %C");
2197
              m = MATCH_ERROR;
2198
              goto no_match;
2199
            }
2200
 
2201
          gfc_free_expr (e);
2202
          gfc_undo_symbols ();
2203
          return MATCH_YES;
2204
        }
2205
      else
2206
        {
2207
          /* ....or else, the match is real.  */
2208
          if (n == MATCH_NO)
2209
            gfc_error ("Expected initialization expression at %C");
2210
          if (n != MATCH_YES)
2211
            return MATCH_ERROR;
2212
        }
2213
    }
2214
 
2215
  if (e->rank != 0)
2216
    {
2217
      gfc_error ("Expected scalar initialization expression at %C");
2218
      m = MATCH_ERROR;
2219
      goto no_match;
2220
    }
2221
 
2222
  msg = gfc_extract_int (e, &ts->kind);
2223
 
2224
  if (msg != NULL)
2225
    {
2226
      gfc_error (msg);
2227
      m = MATCH_ERROR;
2228
      goto no_match;
2229
    }
2230
 
2231
  /* Before throwing away the expression, let's see if we had a
2232
     C interoperable kind (and store the fact).  */
2233
  if (e->ts.is_c_interop == 1)
2234
    {
2235
      /* Mark this as c interoperable if being declared with one
2236
         of the named constants from iso_c_binding.  */
2237
      ts->is_c_interop = e->ts.is_iso_c;
2238
      ts->f90_type = e->ts.f90_type;
2239
    }
2240
 
2241
  gfc_free_expr (e);
2242
  e = NULL;
2243
 
2244
  /* Ignore errors to this point, if we've gotten here.  This means
2245
     we ignore the m=MATCH_ERROR from above.  */
2246
  if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2247
    {
2248
      gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2249
                 gfc_basic_typename (ts->type));
2250
      gfc_current_locus = where;
2251
      return MATCH_ERROR;
2252
    }
2253
 
2254
  /* Warn if, e.g., c_int is used for a REAL variable, but not
2255
     if, e.g., c_double is used for COMPLEX as the standard
2256
     explicitly says that the kind type parameter for complex and real
2257
     variable is the same, i.e. c_float == c_float_complex.  */
2258
  if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2259
      && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2260
           || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2261
    gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2262
                     "is %s", gfc_basic_typename (ts->f90_type), &where,
2263
                     gfc_basic_typename (ts->type));
2264
 
2265
  gfc_gobble_whitespace ();
2266
  if ((c = gfc_next_ascii_char ()) != ')'
2267
      && (ts->type != BT_CHARACTER || c != ','))
2268
    {
2269
      if (ts->type == BT_CHARACTER)
2270
        gfc_error ("Missing right parenthesis or comma at %C");
2271
      else
2272
        gfc_error ("Missing right parenthesis at %C");
2273
      m = MATCH_ERROR;
2274
    }
2275
  else
2276
     /* All tests passed.  */
2277
     m = MATCH_YES;
2278
 
2279
  if(m == MATCH_ERROR)
2280
     gfc_current_locus = where;
2281
 
2282
  if (ts->type == BT_INTEGER && ts->kind == 4 && gfc_option.flag_integer4_kind == 8)
2283
    ts->kind =  8;
2284
 
2285
  if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2286
    {
2287
      if (ts->kind == 4)
2288
        {
2289
          if (gfc_option.flag_real4_kind == 8)
2290
            ts->kind =  8;
2291
          if (gfc_option.flag_real4_kind == 10)
2292
            ts->kind = 10;
2293
          if (gfc_option.flag_real4_kind == 16)
2294
            ts->kind = 16;
2295
        }
2296
 
2297
      if (ts->kind == 8)
2298
        {
2299
          if (gfc_option.flag_real8_kind == 4)
2300
            ts->kind = 4;
2301
          if (gfc_option.flag_real8_kind == 10)
2302
            ts->kind = 10;
2303
          if (gfc_option.flag_real8_kind == 16)
2304
            ts->kind = 16;
2305
        }
2306
    }
2307
 
2308
  /* Return what we know from the test(s).  */
2309
  return m;
2310
 
2311
no_match:
2312
  gfc_free_expr (e);
2313
  gfc_current_locus = where;
2314
  return m;
2315
}
2316
 
2317
 
2318
static match
2319
match_char_kind (int * kind, int * is_iso_c)
2320
{
2321
  locus where;
2322
  gfc_expr *e;
2323
  match m, n;
2324
  const char *msg;
2325
 
2326
  m = MATCH_NO;
2327
  e = NULL;
2328
  where = gfc_current_locus;
2329
 
2330
  n = gfc_match_init_expr (&e);
2331
 
2332
  if (n != MATCH_YES && gfc_matching_function)
2333
    {
2334
      /* The expression might include use-associated or imported
2335
         parameters and try again after the specification
2336
         expressions.  */
2337
      gfc_free_expr (e);
2338
      gfc_undo_symbols ();
2339
      return MATCH_YES;
2340
    }
2341
 
2342
  if (n == MATCH_NO)
2343
    gfc_error ("Expected initialization expression at %C");
2344
  if (n != MATCH_YES)
2345
    return MATCH_ERROR;
2346
 
2347
  if (e->rank != 0)
2348
    {
2349
      gfc_error ("Expected scalar initialization expression at %C");
2350
      m = MATCH_ERROR;
2351
      goto no_match;
2352
    }
2353
 
2354
  msg = gfc_extract_int (e, kind);
2355
  *is_iso_c = e->ts.is_iso_c;
2356
  if (msg != NULL)
2357
    {
2358
      gfc_error (msg);
2359
      m = MATCH_ERROR;
2360
      goto no_match;
2361
    }
2362
 
2363
  gfc_free_expr (e);
2364
 
2365
  /* Ignore errors to this point, if we've gotten here.  This means
2366
     we ignore the m=MATCH_ERROR from above.  */
2367
  if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2368
    {
2369
      gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2370
      m = MATCH_ERROR;
2371
    }
2372
  else
2373
     /* All tests passed.  */
2374
     m = MATCH_YES;
2375
 
2376
  if (m == MATCH_ERROR)
2377
     gfc_current_locus = where;
2378
 
2379
  /* Return what we know from the test(s).  */
2380
  return m;
2381
 
2382
no_match:
2383
  gfc_free_expr (e);
2384
  gfc_current_locus = where;
2385
  return m;
2386
}
2387
 
2388
 
2389
/* Match the various kind/length specifications in a CHARACTER
2390
   declaration.  We don't return MATCH_NO.  */
2391
 
2392
match
2393
gfc_match_char_spec (gfc_typespec *ts)
2394
{
2395
  int kind, seen_length, is_iso_c;
2396
  gfc_charlen *cl;
2397
  gfc_expr *len;
2398
  match m;
2399
  bool deferred;
2400
 
2401
  len = NULL;
2402
  seen_length = 0;
2403
  kind = 0;
2404
  is_iso_c = 0;
2405
  deferred = false;
2406
 
2407
  /* Try the old-style specification first.  */
2408
  old_char_selector = 0;
2409
 
2410
  m = match_char_length (&len, &deferred);
2411
  if (m != MATCH_NO)
2412
    {
2413
      if (m == MATCH_YES)
2414
        old_char_selector = 1;
2415
      seen_length = 1;
2416
      goto done;
2417
    }
2418
 
2419
  m = gfc_match_char ('(');
2420
  if (m != MATCH_YES)
2421
    {
2422
      m = MATCH_YES;    /* Character without length is a single char.  */
2423
      goto done;
2424
    }
2425
 
2426
  /* Try the weird case:  ( KIND = <int> [ , LEN = <len-param> ] ).  */
2427
  if (gfc_match (" kind =") == MATCH_YES)
2428
    {
2429
      m = match_char_kind (&kind, &is_iso_c);
2430
 
2431
      if (m == MATCH_ERROR)
2432
        goto done;
2433
      if (m == MATCH_NO)
2434
        goto syntax;
2435
 
2436
      if (gfc_match (" , len =") == MATCH_NO)
2437
        goto rparen;
2438
 
2439
      m = char_len_param_value (&len, &deferred);
2440
      if (m == MATCH_NO)
2441
        goto syntax;
2442
      if (m == MATCH_ERROR)
2443
        goto done;
2444
      seen_length = 1;
2445
 
2446
      goto rparen;
2447
    }
2448
 
2449
  /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>".  */
2450
  if (gfc_match (" len =") == MATCH_YES)
2451
    {
2452
      m = char_len_param_value (&len, &deferred);
2453
      if (m == MATCH_NO)
2454
        goto syntax;
2455
      if (m == MATCH_ERROR)
2456
        goto done;
2457
      seen_length = 1;
2458
 
2459
      if (gfc_match_char (')') == MATCH_YES)
2460
        goto done;
2461
 
2462
      if (gfc_match (" , kind =") != MATCH_YES)
2463
        goto syntax;
2464
 
2465
      if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2466
        goto done;
2467
 
2468
      goto rparen;
2469
    }
2470
 
2471
  /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ).  */
2472
  m = char_len_param_value (&len, &deferred);
2473
  if (m == MATCH_NO)
2474
    goto syntax;
2475
  if (m == MATCH_ERROR)
2476
    goto done;
2477
  seen_length = 1;
2478
 
2479
  m = gfc_match_char (')');
2480
  if (m == MATCH_YES)
2481
    goto done;
2482
 
2483
  if (gfc_match_char (',') != MATCH_YES)
2484
    goto syntax;
2485
 
2486
  gfc_match (" kind =");        /* Gobble optional text.  */
2487
 
2488
  m = match_char_kind (&kind, &is_iso_c);
2489
  if (m == MATCH_ERROR)
2490
    goto done;
2491
  if (m == MATCH_NO)
2492
    goto syntax;
2493
 
2494
rparen:
2495
  /* Require a right-paren at this point.  */
2496
  m = gfc_match_char (')');
2497
  if (m == MATCH_YES)
2498
    goto done;
2499
 
2500
syntax:
2501
  gfc_error ("Syntax error in CHARACTER declaration at %C");
2502
  m = MATCH_ERROR;
2503
  gfc_free_expr (len);
2504
  return m;
2505
 
2506
done:
2507
  /* Deal with character functions after USE and IMPORT statements.  */
2508
  if (gfc_matching_function)
2509
    {
2510
      gfc_free_expr (len);
2511
      gfc_undo_symbols ();
2512
      return MATCH_YES;
2513
    }
2514
 
2515
  if (m != MATCH_YES)
2516
    {
2517
      gfc_free_expr (len);
2518
      return m;
2519
    }
2520
 
2521
  /* Do some final massaging of the length values.  */
2522
  cl = gfc_new_charlen (gfc_current_ns, NULL);
2523
 
2524
  if (seen_length == 0)
2525
    cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2526
  else
2527
    cl->length = len;
2528
 
2529
  ts->u.cl = cl;
2530
  ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2531
  ts->deferred = deferred;
2532
 
2533
  /* We have to know if it was a c interoperable kind so we can
2534
     do accurate type checking of bind(c) procs, etc.  */
2535
  if (kind != 0)
2536
    /* Mark this as c interoperable if being declared with one
2537
       of the named constants from iso_c_binding.  */
2538
    ts->is_c_interop = is_iso_c;
2539
  else if (len != NULL)
2540
    /* Here, we might have parsed something such as: character(c_char)
2541
       In this case, the parsing code above grabs the c_char when
2542
       looking for the length (line 1690, roughly).  it's the last
2543
       testcase for parsing the kind params of a character variable.
2544
       However, it's not actually the length.    this seems like it
2545
       could be an error.
2546
       To see if the user used a C interop kind, test the expr
2547
       of the so called length, and see if it's C interoperable.  */
2548
    ts->is_c_interop = len->ts.is_iso_c;
2549
 
2550
  return MATCH_YES;
2551
}
2552
 
2553
 
2554
/* Matches a declaration-type-spec (F03:R502).  If successful, sets the ts
2555
   structure to the matched specification.  This is necessary for FUNCTION and
2556
   IMPLICIT statements.
2557
 
2558
   If implicit_flag is nonzero, then we don't check for the optional
2559
   kind specification.  Not doing so is needed for matching an IMPLICIT
2560
   statement correctly.  */
2561
 
2562
match
2563
gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2564
{
2565
  char name[GFC_MAX_SYMBOL_LEN + 1];
2566
  gfc_symbol *sym, *dt_sym;
2567
  match m;
2568
  char c;
2569
  bool seen_deferred_kind, matched_type;
2570
  const char *dt_name;
2571
 
2572
  /* A belt and braces check that the typespec is correctly being treated
2573
     as a deferred characteristic association.  */
2574
  seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2575
                          && (gfc_current_block ()->result->ts.kind == -1)
2576
                          && (ts->kind == -1);
2577
  gfc_clear_ts (ts);
2578
  if (seen_deferred_kind)
2579
    ts->kind = -1;
2580
 
2581
  /* Clear the current binding label, in case one is given.  */
2582
  curr_binding_label = NULL;
2583
 
2584
  if (gfc_match (" byte") == MATCH_YES)
2585
    {
2586
      if (gfc_notify_std (GFC_STD_GNU, "Extension: BYTE type at %C")
2587
          == FAILURE)
2588
        return MATCH_ERROR;
2589
 
2590
      if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2591
        {
2592
          gfc_error ("BYTE type used at %C "
2593
                     "is not available on the target machine");
2594
          return MATCH_ERROR;
2595
        }
2596
 
2597
      ts->type = BT_INTEGER;
2598
      ts->kind = 1;
2599
      return MATCH_YES;
2600
    }
2601
 
2602
 
2603
  m = gfc_match (" type ( %n", name);
2604
  matched_type = (m == MATCH_YES);
2605
 
2606
  if ((matched_type && strcmp ("integer", name) == 0)
2607
      || (!matched_type && gfc_match (" integer") == MATCH_YES))
2608
    {
2609
      ts->type = BT_INTEGER;
2610
      ts->kind = gfc_default_integer_kind;
2611
      goto get_kind;
2612
    }
2613
 
2614
  if ((matched_type && strcmp ("character", name) == 0)
2615
      || (!matched_type && gfc_match (" character") == MATCH_YES))
2616
    {
2617
      if (matched_type
2618
          && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2619
                          "intrinsic-type-spec at %C") == FAILURE)
2620
        return MATCH_ERROR;
2621
 
2622
      ts->type = BT_CHARACTER;
2623
      if (implicit_flag == 0)
2624
        m = gfc_match_char_spec (ts);
2625
      else
2626
        m = MATCH_YES;
2627
 
2628
      if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
2629
        m = MATCH_ERROR;
2630
 
2631
      return m;
2632
    }
2633
 
2634
  if ((matched_type && strcmp ("real", name) == 0)
2635
      || (!matched_type && gfc_match (" real") == MATCH_YES))
2636
    {
2637
      ts->type = BT_REAL;
2638
      ts->kind = gfc_default_real_kind;
2639
      goto get_kind;
2640
    }
2641
 
2642
  if ((matched_type
2643
       && (strcmp ("doubleprecision", name) == 0
2644
           || (strcmp ("double", name) == 0
2645
               && gfc_match (" precision") == MATCH_YES)))
2646
      || (!matched_type && gfc_match (" double precision") == MATCH_YES))
2647
    {
2648
      if (matched_type
2649
          && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2650
                          "intrinsic-type-spec at %C") == FAILURE)
2651
        return MATCH_ERROR;
2652
      if (matched_type && gfc_match_char (')') != MATCH_YES)
2653
        return MATCH_ERROR;
2654
 
2655
      ts->type = BT_REAL;
2656
      ts->kind = gfc_default_double_kind;
2657
      return MATCH_YES;
2658
    }
2659
 
2660
  if ((matched_type && strcmp ("complex", name) == 0)
2661
      || (!matched_type && gfc_match (" complex") == MATCH_YES))
2662
    {
2663
      ts->type = BT_COMPLEX;
2664
      ts->kind = gfc_default_complex_kind;
2665
      goto get_kind;
2666
    }
2667
 
2668
  if ((matched_type
2669
       && (strcmp ("doublecomplex", name) == 0
2670
           || (strcmp ("double", name) == 0
2671
               && gfc_match (" complex") == MATCH_YES)))
2672
      || (!matched_type && gfc_match (" double complex") == MATCH_YES))
2673
    {
2674
      if (gfc_notify_std (GFC_STD_GNU, "Extension: DOUBLE COMPLEX at %C")
2675
          == FAILURE)
2676
        return MATCH_ERROR;
2677
 
2678
      if (matched_type
2679
          && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2680
                          "intrinsic-type-spec at %C") == FAILURE)
2681
        return MATCH_ERROR;
2682
 
2683
      if (matched_type && gfc_match_char (')') != MATCH_YES)
2684
        return MATCH_ERROR;
2685
 
2686
      ts->type = BT_COMPLEX;
2687
      ts->kind = gfc_default_double_kind;
2688
      return MATCH_YES;
2689
    }
2690
 
2691
  if ((matched_type && strcmp ("logical", name) == 0)
2692
      || (!matched_type && gfc_match (" logical") == MATCH_YES))
2693
    {
2694
      ts->type = BT_LOGICAL;
2695
      ts->kind = gfc_default_logical_kind;
2696
      goto get_kind;
2697
    }
2698
 
2699
  if (matched_type)
2700
    m = gfc_match_char (')');
2701
 
2702
  if (m == MATCH_YES)
2703
    ts->type = BT_DERIVED;
2704
  else
2705
    {
2706
      /* Match CLASS declarations.  */
2707
      m = gfc_match (" class ( * )");
2708
      if (m == MATCH_ERROR)
2709
        return MATCH_ERROR;
2710
      else if (m == MATCH_YES)
2711
        {
2712
          gfc_fatal_error ("Unlimited polymorphism at %C not yet supported");
2713
          return MATCH_ERROR;
2714
        }
2715
 
2716
      m = gfc_match (" class ( %n )", name);
2717
      if (m != MATCH_YES)
2718
        return m;
2719
      ts->type = BT_CLASS;
2720
 
2721
      if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: CLASS statement at %C")
2722
                          == FAILURE)
2723
        return MATCH_ERROR;
2724
    }
2725
 
2726
  /* Defer association of the derived type until the end of the
2727
     specification block.  However, if the derived type can be
2728
     found, add it to the typespec.  */
2729
  if (gfc_matching_function)
2730
    {
2731
      ts->u.derived = NULL;
2732
      if (gfc_current_state () != COMP_INTERFACE
2733
            && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2734
        {
2735
          sym = gfc_find_dt_in_generic (sym);
2736
          ts->u.derived = sym;
2737
        }
2738
      return MATCH_YES;
2739
    }
2740
 
2741
  /* Search for the name but allow the components to be defined later.  If
2742
     type = -1, this typespec has been seen in a function declaration but
2743
     the type could not be accessed at that point.  The actual derived type is
2744
     stored in a symtree with the first letter of the name captialized; the
2745
     symtree with the all lower-case name contains the associated
2746
     generic function.  */
2747
  dt_name = gfc_get_string ("%c%s",
2748
                            (char) TOUPPER ((unsigned char) name[0]),
2749
                            (const char*)&name[1]);
2750
  sym = NULL;
2751
  dt_sym = NULL;
2752
  if (ts->kind != -1)
2753
    {
2754
      gfc_get_ha_symbol (name, &sym);
2755
      if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
2756
        {
2757
          gfc_error ("Type name '%s' at %C is ambiguous", name);
2758
          return MATCH_ERROR;
2759
        }
2760
      if (sym->generic && !dt_sym)
2761
        dt_sym = gfc_find_dt_in_generic (sym);
2762
    }
2763
  else if (ts->kind == -1)
2764
    {
2765
      int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2766
                    || gfc_current_ns->has_import_set;
2767
      gfc_find_symbol (name, NULL, iface, &sym);
2768
      if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
2769
        {
2770
          gfc_error ("Type name '%s' at %C is ambiguous", name);
2771
          return MATCH_ERROR;
2772
        }
2773
      if (sym && sym->generic && !dt_sym)
2774
        dt_sym = gfc_find_dt_in_generic (sym);
2775
 
2776
      ts->kind = 0;
2777
      if (sym == NULL)
2778
        return MATCH_NO;
2779
    }
2780
 
2781
  if ((sym->attr.flavor != FL_UNKNOWN
2782
       && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
2783
      || sym->attr.subroutine)
2784
    {
2785
      gfc_error ("Type name '%s' at %C conflicts with previously declared "
2786
                 "entity at %L, which has the same name", name,
2787
                 &sym->declared_at);
2788
      return MATCH_ERROR;
2789
    }
2790
 
2791
  gfc_set_sym_referenced (sym);
2792
  if (!sym->attr.generic
2793
      && gfc_add_generic (&sym->attr, sym->name, NULL) == FAILURE)
2794
    return MATCH_ERROR;
2795
 
2796
  if (!sym->attr.function
2797
      && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2798
    return MATCH_ERROR;
2799
 
2800
  if (!dt_sym)
2801
    {
2802
      gfc_interface *intr, *head;
2803
 
2804
      /* Use upper case to save the actual derived-type symbol.  */
2805
      gfc_get_symbol (dt_name, NULL, &dt_sym);
2806
      dt_sym->name = gfc_get_string (sym->name);
2807
      head = sym->generic;
2808
      intr = gfc_get_interface ();
2809
      intr->sym = dt_sym;
2810
      intr->where = gfc_current_locus;
2811
      intr->next = head;
2812
      sym->generic = intr;
2813
      sym->attr.if_source = IFSRC_DECL;
2814
    }
2815
 
2816
  gfc_set_sym_referenced (dt_sym);
2817
 
2818
  if (dt_sym->attr.flavor != FL_DERIVED
2819
      && gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL)
2820
                         == FAILURE)
2821
    return MATCH_ERROR;
2822
 
2823
  ts->u.derived = dt_sym;
2824
 
2825
  return MATCH_YES;
2826
 
2827
get_kind:
2828
  if (matched_type
2829
      && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2830
                         "intrinsic-type-spec at %C") == FAILURE)
2831
    return MATCH_ERROR;
2832
 
2833
  /* For all types except double, derived and character, look for an
2834
     optional kind specifier.  MATCH_NO is actually OK at this point.  */
2835
  if (implicit_flag == 1)
2836
    {
2837
        if (matched_type && gfc_match_char (')') != MATCH_YES)
2838
          return MATCH_ERROR;
2839
 
2840
        return MATCH_YES;
2841
    }
2842
 
2843
  if (gfc_current_form == FORM_FREE)
2844
    {
2845
      c = gfc_peek_ascii_char ();
2846
      if (!gfc_is_whitespace (c) && c != '*' && c != '('
2847
          && c != ':' && c != ',')
2848
        {
2849
          if (matched_type && c == ')')
2850
            {
2851
              gfc_next_ascii_char ();
2852
              return MATCH_YES;
2853
            }
2854
          return MATCH_NO;
2855
        }
2856
    }
2857
 
2858
  m = gfc_match_kind_spec (ts, false);
2859
  if (m == MATCH_NO && ts->type != BT_CHARACTER)
2860
    m = gfc_match_old_kind_spec (ts);
2861
 
2862
  if (matched_type && gfc_match_char (')') != MATCH_YES)
2863
    return MATCH_ERROR;
2864
 
2865
  /* Defer association of the KIND expression of function results
2866
     until after USE and IMPORT statements.  */
2867
  if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2868
         || gfc_matching_function)
2869
    return MATCH_YES;
2870
 
2871
  if (m == MATCH_NO)
2872
    m = MATCH_YES;              /* No kind specifier found.  */
2873
 
2874
  return m;
2875
}
2876
 
2877
 
2878
/* Match an IMPLICIT NONE statement.  Actually, this statement is
2879
   already matched in parse.c, or we would not end up here in the
2880
   first place.  So the only thing we need to check, is if there is
2881
   trailing garbage.  If not, the match is successful.  */
2882
 
2883
match
2884
gfc_match_implicit_none (void)
2885
{
2886
  return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2887
}
2888
 
2889
 
2890
/* Match the letter range(s) of an IMPLICIT statement.  */
2891
 
2892
static match
2893
match_implicit_range (void)
2894
{
2895
  char c, c1, c2;
2896
  int inner;
2897
  locus cur_loc;
2898
 
2899
  cur_loc = gfc_current_locus;
2900
 
2901
  gfc_gobble_whitespace ();
2902
  c = gfc_next_ascii_char ();
2903
  if (c != '(')
2904
    {
2905
      gfc_error ("Missing character range in IMPLICIT at %C");
2906
      goto bad;
2907
    }
2908
 
2909
  inner = 1;
2910
  while (inner)
2911
    {
2912
      gfc_gobble_whitespace ();
2913
      c1 = gfc_next_ascii_char ();
2914
      if (!ISALPHA (c1))
2915
        goto bad;
2916
 
2917
      gfc_gobble_whitespace ();
2918
      c = gfc_next_ascii_char ();
2919
 
2920
      switch (c)
2921
        {
2922
        case ')':
2923
          inner = 0;             /* Fall through.  */
2924
 
2925
        case ',':
2926
          c2 = c1;
2927
          break;
2928
 
2929
        case '-':
2930
          gfc_gobble_whitespace ();
2931
          c2 = gfc_next_ascii_char ();
2932
          if (!ISALPHA (c2))
2933
            goto bad;
2934
 
2935
          gfc_gobble_whitespace ();
2936
          c = gfc_next_ascii_char ();
2937
 
2938
          if ((c != ',') && (c != ')'))
2939
            goto bad;
2940
          if (c == ')')
2941
            inner = 0;
2942
 
2943
          break;
2944
 
2945
        default:
2946
          goto bad;
2947
        }
2948
 
2949
      if (c1 > c2)
2950
        {
2951
          gfc_error ("Letters must be in alphabetic order in "
2952
                     "IMPLICIT statement at %C");
2953
          goto bad;
2954
        }
2955
 
2956
      /* See if we can add the newly matched range to the pending
2957
         implicits from this IMPLICIT statement.  We do not check for
2958
         conflicts with whatever earlier IMPLICIT statements may have
2959
         set.  This is done when we've successfully finished matching
2960
         the current one.  */
2961
      if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2962
        goto bad;
2963
    }
2964
 
2965
  return MATCH_YES;
2966
 
2967
bad:
2968
  gfc_syntax_error (ST_IMPLICIT);
2969
 
2970
  gfc_current_locus = cur_loc;
2971
  return MATCH_ERROR;
2972
}
2973
 
2974
 
2975
/* Match an IMPLICIT statement, storing the types for
2976
   gfc_set_implicit() if the statement is accepted by the parser.
2977
   There is a strange looking, but legal syntactic construction
2978
   possible.  It looks like:
2979
 
2980
     IMPLICIT INTEGER (a-b) (c-d)
2981
 
2982
   This is legal if "a-b" is a constant expression that happens to
2983
   equal one of the legal kinds for integers.  The real problem
2984
   happens with an implicit specification that looks like:
2985
 
2986
     IMPLICIT INTEGER (a-b)
2987
 
2988
   In this case, a typespec matcher that is "greedy" (as most of the
2989
   matchers are) gobbles the character range as a kindspec, leaving
2990
   nothing left.  We therefore have to go a bit more slowly in the
2991
   matching process by inhibiting the kindspec checking during
2992
   typespec matching and checking for a kind later.  */
2993
 
2994
match
2995
gfc_match_implicit (void)
2996
{
2997
  gfc_typespec ts;
2998
  locus cur_loc;
2999
  char c;
3000
  match m;
3001
 
3002
  gfc_clear_ts (&ts);
3003
 
3004
  /* We don't allow empty implicit statements.  */
3005
  if (gfc_match_eos () == MATCH_YES)
3006
    {
3007
      gfc_error ("Empty IMPLICIT statement at %C");
3008
      return MATCH_ERROR;
3009
    }
3010
 
3011
  do
3012
    {
3013
      /* First cleanup.  */
3014
      gfc_clear_new_implicit ();
3015
 
3016
      /* A basic type is mandatory here.  */
3017
      m = gfc_match_decl_type_spec (&ts, 1);
3018
      if (m == MATCH_ERROR)
3019
        goto error;
3020
      if (m == MATCH_NO)
3021
        goto syntax;
3022
 
3023
      cur_loc = gfc_current_locus;
3024
      m = match_implicit_range ();
3025
 
3026
      if (m == MATCH_YES)
3027
        {
3028
          /* We may have <TYPE> (<RANGE>).  */
3029
          gfc_gobble_whitespace ();
3030
          c = gfc_next_ascii_char ();
3031
          if ((c == '\n') || (c == ','))
3032
            {
3033
              /* Check for CHARACTER with no length parameter.  */
3034
              if (ts.type == BT_CHARACTER && !ts.u.cl)
3035
                {
3036
                  ts.kind = gfc_default_character_kind;
3037
                  ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3038
                  ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
3039
                                                      NULL, 1);
3040
                }
3041
 
3042
              /* Record the Successful match.  */
3043
              if (gfc_merge_new_implicit (&ts) != SUCCESS)
3044
                return MATCH_ERROR;
3045
              continue;
3046
            }
3047
 
3048
          gfc_current_locus = cur_loc;
3049
        }
3050
 
3051
      /* Discard the (incorrectly) matched range.  */
3052
      gfc_clear_new_implicit ();
3053
 
3054
      /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>).  */
3055
      if (ts.type == BT_CHARACTER)
3056
        m = gfc_match_char_spec (&ts);
3057
      else
3058
        {
3059
          m = gfc_match_kind_spec (&ts, false);
3060
          if (m == MATCH_NO)
3061
            {
3062
              m = gfc_match_old_kind_spec (&ts);
3063
              if (m == MATCH_ERROR)
3064
                goto error;
3065
              if (m == MATCH_NO)
3066
                goto syntax;
3067
            }
3068
        }
3069
      if (m == MATCH_ERROR)
3070
        goto error;
3071
 
3072
      m = match_implicit_range ();
3073
      if (m == MATCH_ERROR)
3074
        goto error;
3075
      if (m == MATCH_NO)
3076
        goto syntax;
3077
 
3078
      gfc_gobble_whitespace ();
3079
      c = gfc_next_ascii_char ();
3080
      if ((c != '\n') && (c != ','))
3081
        goto syntax;
3082
 
3083
      if (gfc_merge_new_implicit (&ts) != SUCCESS)
3084
        return MATCH_ERROR;
3085
    }
3086
  while (c == ',');
3087
 
3088
  return MATCH_YES;
3089
 
3090
syntax:
3091
  gfc_syntax_error (ST_IMPLICIT);
3092
 
3093
error:
3094
  return MATCH_ERROR;
3095
}
3096
 
3097
 
3098
match
3099
gfc_match_import (void)
3100
{
3101
  char name[GFC_MAX_SYMBOL_LEN + 1];
3102
  match m;
3103
  gfc_symbol *sym;
3104
  gfc_symtree *st;
3105
 
3106
  if (gfc_current_ns->proc_name == NULL
3107
      || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
3108
    {
3109
      gfc_error ("IMPORT statement at %C only permitted in "
3110
                 "an INTERFACE body");
3111
      return MATCH_ERROR;
3112
    }
3113
 
3114
  if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
3115
      == FAILURE)
3116
    return MATCH_ERROR;
3117
 
3118
  if (gfc_match_eos () == MATCH_YES)
3119
    {
3120
      /* All host variables should be imported.  */
3121
      gfc_current_ns->has_import_set = 1;
3122
      return MATCH_YES;
3123
    }
3124
 
3125
  if (gfc_match (" ::") == MATCH_YES)
3126
    {
3127
      if (gfc_match_eos () == MATCH_YES)
3128
        {
3129
           gfc_error ("Expecting list of named entities at %C");
3130
           return MATCH_ERROR;
3131
        }
3132
    }
3133
 
3134
  for(;;)
3135
    {
3136
      sym = NULL;
3137
      m = gfc_match (" %n", name);
3138
      switch (m)
3139
        {
3140
        case MATCH_YES:
3141
          if (gfc_current_ns->parent !=  NULL
3142
              && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
3143
            {
3144
               gfc_error ("Type name '%s' at %C is ambiguous", name);
3145
               return MATCH_ERROR;
3146
            }
3147
          else if (!sym && gfc_current_ns->proc_name->ns->parent !=  NULL
3148
                   && gfc_find_symbol (name,
3149
                                       gfc_current_ns->proc_name->ns->parent,
3150
                                       1, &sym))
3151
            {
3152
               gfc_error ("Type name '%s' at %C is ambiguous", name);
3153
               return MATCH_ERROR;
3154
            }
3155
 
3156
          if (sym == NULL)
3157
            {
3158
              gfc_error ("Cannot IMPORT '%s' from host scoping unit "
3159
                         "at %C - does not exist.", name);
3160
              return MATCH_ERROR;
3161
            }
3162
 
3163
          if (gfc_find_symtree (gfc_current_ns->sym_root,name))
3164
            {
3165
              gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3166
                           "at %C.", name);
3167
              goto next_item;
3168
            }
3169
 
3170
          st = gfc_new_symtree (&gfc_current_ns->sym_root, sym->name);
3171
          st->n.sym = sym;
3172
          sym->refs++;
3173
          sym->attr.imported = 1;
3174
 
3175
          if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
3176
            {
3177
              /* The actual derived type is stored in a symtree with the first
3178
                 letter of the name captialized; the symtree with the all
3179
                 lower-case name contains the associated generic function. */
3180
              st = gfc_new_symtree (&gfc_current_ns->sym_root,
3181
                        gfc_get_string ("%c%s",
3182
                                (char) TOUPPER ((unsigned char) sym->name[0]),
3183
                                &sym->name[1]));
3184
              st->n.sym = sym;
3185
              sym->refs++;
3186
              sym->attr.imported = 1;
3187
            }
3188
 
3189
          goto next_item;
3190
 
3191
        case MATCH_NO:
3192
          break;
3193
 
3194
        case MATCH_ERROR:
3195
          return MATCH_ERROR;
3196
        }
3197
 
3198
    next_item:
3199
      if (gfc_match_eos () == MATCH_YES)
3200
        break;
3201
      if (gfc_match_char (',') != MATCH_YES)
3202
        goto syntax;
3203
    }
3204
 
3205
  return MATCH_YES;
3206
 
3207
syntax:
3208
  gfc_error ("Syntax error in IMPORT statement at %C");
3209
  return MATCH_ERROR;
3210
}
3211
 
3212
 
3213
/* A minimal implementation of gfc_match without whitespace, escape
3214
   characters or variable arguments.  Returns true if the next
3215
   characters match the TARGET template exactly.  */
3216
 
3217
static bool
3218
match_string_p (const char *target)
3219
{
3220
  const char *p;
3221
 
3222
  for (p = target; *p; p++)
3223
    if ((char) gfc_next_ascii_char () != *p)
3224
      return false;
3225
  return true;
3226
}
3227
 
3228
/* Matches an attribute specification including array specs.  If
3229
   successful, leaves the variables current_attr and current_as
3230
   holding the specification.  Also sets the colon_seen variable for
3231
   later use by matchers associated with initializations.
3232
 
3233
   This subroutine is a little tricky in the sense that we don't know
3234
   if we really have an attr-spec until we hit the double colon.
3235
   Until that time, we can only return MATCH_NO.  This forces us to
3236
   check for duplicate specification at this level.  */
3237
 
3238
static match
3239
match_attr_spec (void)
3240
{
3241
  /* Modifiers that can exist in a type statement.  */
3242
  typedef enum
3243
  { GFC_DECL_BEGIN = 0,
3244
    DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3245
    DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3246
    DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3247
    DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3248
    DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3249
    DECL_NONE, GFC_DECL_END /* Sentinel */
3250
  }
3251
  decl_types;
3252
 
3253
/* GFC_DECL_END is the sentinel, index starts at 0.  */
3254
#define NUM_DECL GFC_DECL_END
3255
 
3256
  locus start, seen_at[NUM_DECL];
3257
  int seen[NUM_DECL];
3258
  unsigned int d;
3259
  const char *attr;
3260
  match m;
3261
  gfc_try t;
3262
 
3263
  gfc_clear_attr (&current_attr);
3264
  start = gfc_current_locus;
3265
 
3266
  current_as = NULL;
3267
  colon_seen = 0;
3268
 
3269
  /* See if we get all of the keywords up to the final double colon.  */
3270
  for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3271
    seen[d] = 0;
3272
 
3273
  for (;;)
3274
    {
3275
      char ch;
3276
 
3277
      d = DECL_NONE;
3278
      gfc_gobble_whitespace ();
3279
 
3280
      ch = gfc_next_ascii_char ();
3281
      if (ch == ':')
3282
        {
3283
          /* This is the successful exit condition for the loop.  */
3284
          if (gfc_next_ascii_char () == ':')
3285
            break;
3286
        }
3287
      else if (ch == ',')
3288
        {
3289
          gfc_gobble_whitespace ();
3290
          switch (gfc_peek_ascii_char ())
3291
            {
3292
            case 'a':
3293
              gfc_next_ascii_char ();
3294
              switch (gfc_next_ascii_char ())
3295
                {
3296
                case 'l':
3297
                  if (match_string_p ("locatable"))
3298
                    {
3299
                      /* Matched "allocatable".  */
3300
                      d = DECL_ALLOCATABLE;
3301
                    }
3302
                  break;
3303
 
3304
                case 's':
3305
                  if (match_string_p ("ynchronous"))
3306
                    {
3307
                      /* Matched "asynchronous".  */
3308
                      d = DECL_ASYNCHRONOUS;
3309
                    }
3310
                  break;
3311
                }
3312
              break;
3313
 
3314
            case 'b':
3315
              /* Try and match the bind(c).  */
3316
              m = gfc_match_bind_c (NULL, true);
3317
              if (m == MATCH_YES)
3318
                d = DECL_IS_BIND_C;
3319
              else if (m == MATCH_ERROR)
3320
                goto cleanup;
3321
              break;
3322
 
3323
            case 'c':
3324
              gfc_next_ascii_char ();
3325
              if ('o' != gfc_next_ascii_char ())
3326
                break;
3327
              switch (gfc_next_ascii_char ())
3328
                {
3329
                case 'd':
3330
                  if (match_string_p ("imension"))
3331
                    {
3332
                      d = DECL_CODIMENSION;
3333
                      break;
3334
                    }
3335
                case 'n':
3336
                  if (match_string_p ("tiguous"))
3337
                    {
3338
                      d = DECL_CONTIGUOUS;
3339
                      break;
3340
                    }
3341
                }
3342
              break;
3343
 
3344
            case 'd':
3345
              if (match_string_p ("dimension"))
3346
                d = DECL_DIMENSION;
3347
              break;
3348
 
3349
            case 'e':
3350
              if (match_string_p ("external"))
3351
                d = DECL_EXTERNAL;
3352
              break;
3353
 
3354
            case 'i':
3355
              if (match_string_p ("int"))
3356
                {
3357
                  ch = gfc_next_ascii_char ();
3358
                  if (ch == 'e')
3359
                    {
3360
                      if (match_string_p ("nt"))
3361
                        {
3362
                          /* Matched "intent".  */
3363
                          /* TODO: Call match_intent_spec from here.  */
3364
                          if (gfc_match (" ( in out )") == MATCH_YES)
3365
                            d = DECL_INOUT;
3366
                          else if (gfc_match (" ( in )") == MATCH_YES)
3367
                            d = DECL_IN;
3368
                          else if (gfc_match (" ( out )") == MATCH_YES)
3369
                            d = DECL_OUT;
3370
                        }
3371
                    }
3372
                  else if (ch == 'r')
3373
                    {
3374
                      if (match_string_p ("insic"))
3375
                        {
3376
                          /* Matched "intrinsic".  */
3377
                          d = DECL_INTRINSIC;
3378
                        }
3379
                    }
3380
                }
3381
              break;
3382
 
3383
            case 'o':
3384
              if (match_string_p ("optional"))
3385
                d = DECL_OPTIONAL;
3386
              break;
3387
 
3388
            case 'p':
3389
              gfc_next_ascii_char ();
3390
              switch (gfc_next_ascii_char ())
3391
                {
3392
                case 'a':
3393
                  if (match_string_p ("rameter"))
3394
                    {
3395
                      /* Matched "parameter".  */
3396
                      d = DECL_PARAMETER;
3397
                    }
3398
                  break;
3399
 
3400
                case 'o':
3401
                  if (match_string_p ("inter"))
3402
                    {
3403
                      /* Matched "pointer".  */
3404
                      d = DECL_POINTER;
3405
                    }
3406
                  break;
3407
 
3408
                case 'r':
3409
                  ch = gfc_next_ascii_char ();
3410
                  if (ch == 'i')
3411
                    {
3412
                      if (match_string_p ("vate"))
3413
                        {
3414
                          /* Matched "private".  */
3415
                          d = DECL_PRIVATE;
3416
                        }
3417
                    }
3418
                  else if (ch == 'o')
3419
                    {
3420
                      if (match_string_p ("tected"))
3421
                        {
3422
                          /* Matched "protected".  */
3423
                          d = DECL_PROTECTED;
3424
                        }
3425
                    }
3426
                  break;
3427
 
3428
                case 'u':
3429
                  if (match_string_p ("blic"))
3430
                    {
3431
                      /* Matched "public".  */
3432
                      d = DECL_PUBLIC;
3433
                    }
3434
                  break;
3435
                }
3436
              break;
3437
 
3438
            case 's':
3439
              if (match_string_p ("save"))
3440
                d = DECL_SAVE;
3441
              break;
3442
 
3443
            case 't':
3444
              if (match_string_p ("target"))
3445
                d = DECL_TARGET;
3446
              break;
3447
 
3448
            case 'v':
3449
              gfc_next_ascii_char ();
3450
              ch = gfc_next_ascii_char ();
3451
              if (ch == 'a')
3452
                {
3453
                  if (match_string_p ("lue"))
3454
                    {
3455
                      /* Matched "value".  */
3456
                      d = DECL_VALUE;
3457
                    }
3458
                }
3459
              else if (ch == 'o')
3460
                {
3461
                  if (match_string_p ("latile"))
3462
                    {
3463
                      /* Matched "volatile".  */
3464
                      d = DECL_VOLATILE;
3465
                    }
3466
                }
3467
              break;
3468
            }
3469
        }
3470
 
3471
      /* No double colon and no recognizable decl_type, so assume that
3472
         we've been looking at something else the whole time.  */
3473
      if (d == DECL_NONE)
3474
        {
3475
          m = MATCH_NO;
3476
          goto cleanup;
3477
        }
3478
 
3479
      /* Check to make sure any parens are paired up correctly.  */
3480
      if (gfc_match_parens () == MATCH_ERROR)
3481
        {
3482
          m = MATCH_ERROR;
3483
          goto cleanup;
3484
        }
3485
 
3486
      seen[d]++;
3487
      seen_at[d] = gfc_current_locus;
3488
 
3489
      if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
3490
        {
3491
          gfc_array_spec *as = NULL;
3492
 
3493
          m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
3494
                                    d == DECL_CODIMENSION);
3495
 
3496
          if (current_as == NULL)
3497
            current_as = as;
3498
          else if (m == MATCH_YES)
3499
            {
3500
              merge_array_spec (as, current_as, false);
3501
              free (as);
3502
            }
3503
 
3504
          if (m == MATCH_NO)
3505
            {
3506
              if (d == DECL_CODIMENSION)
3507
                gfc_error ("Missing codimension specification at %C");
3508
              else
3509
                gfc_error ("Missing dimension specification at %C");
3510
              m = MATCH_ERROR;
3511
            }
3512
 
3513
          if (m == MATCH_ERROR)
3514
            goto cleanup;
3515
        }
3516
    }
3517
 
3518
  /* Since we've seen a double colon, we have to be looking at an
3519
     attr-spec.  This means that we can now issue errors.  */
3520
  for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3521
    if (seen[d] > 1)
3522
      {
3523
        switch (d)
3524
          {
3525
          case DECL_ALLOCATABLE:
3526
            attr = "ALLOCATABLE";
3527
            break;
3528
          case DECL_ASYNCHRONOUS:
3529
            attr = "ASYNCHRONOUS";
3530
            break;
3531
          case DECL_CODIMENSION:
3532
            attr = "CODIMENSION";
3533
            break;
3534
          case DECL_CONTIGUOUS:
3535
            attr = "CONTIGUOUS";
3536
            break;
3537
          case DECL_DIMENSION:
3538
            attr = "DIMENSION";
3539
            break;
3540
          case DECL_EXTERNAL:
3541
            attr = "EXTERNAL";
3542
            break;
3543
          case DECL_IN:
3544
            attr = "INTENT (IN)";
3545
            break;
3546
          case DECL_OUT:
3547
            attr = "INTENT (OUT)";
3548
            break;
3549
          case DECL_INOUT:
3550
            attr = "INTENT (IN OUT)";
3551
            break;
3552
          case DECL_INTRINSIC:
3553
            attr = "INTRINSIC";
3554
            break;
3555
          case DECL_OPTIONAL:
3556
            attr = "OPTIONAL";
3557
            break;
3558
          case DECL_PARAMETER:
3559
            attr = "PARAMETER";
3560
            break;
3561
          case DECL_POINTER:
3562
            attr = "POINTER";
3563
            break;
3564
          case DECL_PROTECTED:
3565
            attr = "PROTECTED";
3566
            break;
3567
          case DECL_PRIVATE:
3568
            attr = "PRIVATE";
3569
            break;
3570
          case DECL_PUBLIC:
3571
            attr = "PUBLIC";
3572
            break;
3573
          case DECL_SAVE:
3574
            attr = "SAVE";
3575
            break;
3576
          case DECL_TARGET:
3577
            attr = "TARGET";
3578
            break;
3579
          case DECL_IS_BIND_C:
3580
            attr = "IS_BIND_C";
3581
            break;
3582
          case DECL_VALUE:
3583
            attr = "VALUE";
3584
            break;
3585
          case DECL_VOLATILE:
3586
            attr = "VOLATILE";
3587
            break;
3588
          default:
3589
            attr = NULL;        /* This shouldn't happen.  */
3590
          }
3591
 
3592
        gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3593
        m = MATCH_ERROR;
3594
        goto cleanup;
3595
      }
3596
 
3597
  /* Now that we've dealt with duplicate attributes, add the attributes
3598
     to the current attribute.  */
3599
  for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3600
    {
3601
      if (seen[d] == 0)
3602
        continue;
3603
 
3604
      if (gfc_current_state () == COMP_DERIVED
3605
          && d != DECL_DIMENSION && d != DECL_CODIMENSION
3606
          && d != DECL_POINTER   && d != DECL_PRIVATE
3607
          && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
3608
        {
3609
          if (d == DECL_ALLOCATABLE)
3610
            {
3611
              if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
3612
                                  "attribute at %C in a TYPE definition")
3613
                  == FAILURE)
3614
                {
3615
                  m = MATCH_ERROR;
3616
                  goto cleanup;
3617
                }
3618
            }
3619
          else
3620
            {
3621
              gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3622
                         &seen_at[d]);
3623
              m = MATCH_ERROR;
3624
              goto cleanup;
3625
            }
3626
        }
3627
 
3628
      if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3629
          && gfc_current_state () != COMP_MODULE)
3630
        {
3631
          if (d == DECL_PRIVATE)
3632
            attr = "PRIVATE";
3633
          else
3634
            attr = "PUBLIC";
3635
          if (gfc_current_state () == COMP_DERIVED
3636
              && gfc_state_stack->previous
3637
              && gfc_state_stack->previous->state == COMP_MODULE)
3638
            {
3639
              if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
3640
                                  "at %L in a TYPE definition", attr,
3641
                                  &seen_at[d])
3642
                  == FAILURE)
3643
                {
3644
                  m = MATCH_ERROR;
3645
                  goto cleanup;
3646
                }
3647
            }
3648
          else
3649
            {
3650
              gfc_error ("%s attribute at %L is not allowed outside of the "
3651
                         "specification part of a module", attr, &seen_at[d]);
3652
              m = MATCH_ERROR;
3653
              goto cleanup;
3654
            }
3655
        }
3656
 
3657
      switch (d)
3658
        {
3659
        case DECL_ALLOCATABLE:
3660
          t = gfc_add_allocatable (&current_attr, &seen_at[d]);
3661
          break;
3662
 
3663
        case DECL_ASYNCHRONOUS:
3664
          if (gfc_notify_std (GFC_STD_F2003,
3665
                              "Fortran 2003: ASYNCHRONOUS attribute at %C")
3666
              == FAILURE)
3667
            t = FAILURE;
3668
          else
3669
            t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
3670
          break;
3671
 
3672
        case DECL_CODIMENSION:
3673
          t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
3674
          break;
3675
 
3676
        case DECL_CONTIGUOUS:
3677
          if (gfc_notify_std (GFC_STD_F2008,
3678
                              "Fortran 2008: CONTIGUOUS attribute at %C")
3679
              == FAILURE)
3680
            t = FAILURE;
3681
          else
3682
            t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
3683
          break;
3684
 
3685
        case DECL_DIMENSION:
3686
          t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
3687
          break;
3688
 
3689
        case DECL_EXTERNAL:
3690
          t = gfc_add_external (&current_attr, &seen_at[d]);
3691
          break;
3692
 
3693
        case DECL_IN:
3694
          t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
3695
          break;
3696
 
3697
        case DECL_OUT:
3698
          t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
3699
          break;
3700
 
3701
        case DECL_INOUT:
3702
          t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
3703
          break;
3704
 
3705
        case DECL_INTRINSIC:
3706
          t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
3707
          break;
3708
 
3709
        case DECL_OPTIONAL:
3710
          t = gfc_add_optional (&current_attr, &seen_at[d]);
3711
          break;
3712
 
3713
        case DECL_PARAMETER:
3714
          t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
3715
          break;
3716
 
3717
        case DECL_POINTER:
3718
          t = gfc_add_pointer (&current_attr, &seen_at[d]);
3719
          break;
3720
 
3721
        case DECL_PROTECTED:
3722
          if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3723
            {
3724
               gfc_error ("PROTECTED at %C only allowed in specification "
3725
                          "part of a module");
3726
               t = FAILURE;
3727
               break;
3728
            }
3729
 
3730
          if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3731
                              "attribute at %C")
3732
              == FAILURE)
3733
            t = FAILURE;
3734
          else
3735
            t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
3736
          break;
3737
 
3738
        case DECL_PRIVATE:
3739
          t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
3740
                              &seen_at[d]);
3741
          break;
3742
 
3743
        case DECL_PUBLIC:
3744
          t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
3745
                              &seen_at[d]);
3746
          break;
3747
 
3748
        case DECL_SAVE:
3749
          t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
3750
          break;
3751
 
3752
        case DECL_TARGET:
3753
          t = gfc_add_target (&current_attr, &seen_at[d]);
3754
          break;
3755
 
3756
        case DECL_IS_BIND_C:
3757
           t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
3758
           break;
3759
 
3760
        case DECL_VALUE:
3761
          if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3762
                              "at %C")
3763
              == FAILURE)
3764
            t = FAILURE;
3765
          else
3766
            t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
3767
          break;
3768
 
3769
        case DECL_VOLATILE:
3770
          if (gfc_notify_std (GFC_STD_F2003,
3771
                              "Fortran 2003: VOLATILE attribute at %C")
3772
              == FAILURE)
3773
            t = FAILURE;
3774
          else
3775
            t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
3776
          break;
3777
 
3778
        default:
3779
          gfc_internal_error ("match_attr_spec(): Bad attribute");
3780
        }
3781
 
3782
      if (t == FAILURE)
3783
        {
3784
          m = MATCH_ERROR;
3785
          goto cleanup;
3786
        }
3787
    }
3788
 
3789
  /* Module variables implicitly have the SAVE attribute.  */
3790
  if (gfc_current_state () == COMP_MODULE && !current_attr.save)
3791
    current_attr.save = SAVE_IMPLICIT;
3792
 
3793
  colon_seen = 1;
3794
  return MATCH_YES;
3795
 
3796
cleanup:
3797
  gfc_current_locus = start;
3798
  gfc_free_array_spec (current_as);
3799
  current_as = NULL;
3800
  return m;
3801
}
3802
 
3803
 
3804
/* Set the binding label, dest_label, either with the binding label
3805
   stored in the given gfc_typespec, ts, or if none was provided, it
3806
   will be the symbol name in all lower case, as required by the draft
3807
   (J3/04-007, section 15.4.1).  If a binding label was given and
3808
   there is more than one argument (num_idents), it is an error.  */
3809
 
3810
static gfc_try
3811
set_binding_label (const char **dest_label, const char *sym_name,
3812
                   int num_idents)
3813
{
3814
  if (num_idents > 1 && has_name_equals)
3815
    {
3816
      gfc_error ("Multiple identifiers provided with "
3817
                 "single NAME= specifier at %C");
3818
      return FAILURE;
3819
    }
3820
 
3821
  if (curr_binding_label)
3822
    /* Binding label given; store in temp holder til have sym.  */
3823
    *dest_label = curr_binding_label;
3824
  else
3825
    {
3826
      /* No binding label given, and the NAME= specifier did not exist,
3827
         which means there was no NAME="".  */
3828
      if (sym_name != NULL && has_name_equals == 0)
3829
        *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
3830
    }
3831
 
3832
  return SUCCESS;
3833
}
3834
 
3835
 
3836
/* Set the status of the given common block as being BIND(C) or not,
3837
   depending on the given parameter, is_bind_c.  */
3838
 
3839
void
3840
set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3841
{
3842
  com_block->is_bind_c = is_bind_c;
3843
  return;
3844
}
3845
 
3846
 
3847
/* Verify that the given gfc_typespec is for a C interoperable type.  */
3848
 
3849
gfc_try
3850
gfc_verify_c_interop (gfc_typespec *ts)
3851
{
3852
  if (ts->type == BT_DERIVED && ts->u.derived != NULL)
3853
    return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
3854
           ? SUCCESS : FAILURE;
3855
  else if (ts->type == BT_CLASS)
3856
    return FAILURE;
3857
  else if (ts->is_c_interop != 1)
3858
    return FAILURE;
3859
 
3860
  return SUCCESS;
3861
}
3862
 
3863
 
3864
/* Verify that the variables of a given common block, which has been
3865
   defined with the attribute specifier bind(c), to be of a C
3866
   interoperable type.  Errors will be reported here, if
3867
   encountered.  */
3868
 
3869
gfc_try
3870
verify_com_block_vars_c_interop (gfc_common_head *com_block)
3871
{
3872
  gfc_symbol *curr_sym = NULL;
3873
  gfc_try retval = SUCCESS;
3874
 
3875
  curr_sym = com_block->head;
3876
 
3877
  /* Make sure we have at least one symbol.  */
3878
  if (curr_sym == NULL)
3879
    return retval;
3880
 
3881
  /* Here we know we have a symbol, so we'll execute this loop
3882
     at least once.  */
3883
  do
3884
    {
3885
      /* The second to last param, 1, says this is in a common block.  */
3886
      retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3887
      curr_sym = curr_sym->common_next;
3888
    } while (curr_sym != NULL);
3889
 
3890
  return retval;
3891
}
3892
 
3893
 
3894
/* Verify that a given BIND(C) symbol is C interoperable.  If it is not,
3895
   an appropriate error message is reported.  */
3896
 
3897
gfc_try
3898
verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3899
                   int is_in_common, gfc_common_head *com_block)
3900
{
3901
  bool bind_c_function = false;
3902
  gfc_try retval = SUCCESS;
3903
 
3904
  if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
3905
    bind_c_function = true;
3906
 
3907
  if (tmp_sym->attr.function && tmp_sym->result != NULL)
3908
    {
3909
      tmp_sym = tmp_sym->result;
3910
      /* Make sure it wasn't an implicitly typed result.  */
3911
      if (tmp_sym->attr.implicit_type)
3912
        {
3913
          gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3914
                       "%L may not be C interoperable", tmp_sym->name,
3915
                       &tmp_sym->declared_at);
3916
          tmp_sym->ts.f90_type = tmp_sym->ts.type;
3917
          /* Mark it as C interoperable to prevent duplicate warnings.  */
3918
          tmp_sym->ts.is_c_interop = 1;
3919
          tmp_sym->attr.is_c_interop = 1;
3920
        }
3921
    }
3922
 
3923
  /* Here, we know we have the bind(c) attribute, so if we have
3924
     enough type info, then verify that it's a C interop kind.
3925
     The info could be in the symbol already, or possibly still in
3926
     the given ts (current_ts), so look in both.  */
3927
  if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3928
    {
3929
      if (gfc_verify_c_interop (&(tmp_sym->ts)) != SUCCESS)
3930
        {
3931
          /* See if we're dealing with a sym in a common block or not.  */
3932
          if (is_in_common == 1)
3933
            {
3934
              gfc_warning ("Variable '%s' in common block '%s' at %L "
3935
                           "may not be a C interoperable "
3936
                           "kind though common block '%s' is BIND(C)",
3937
                           tmp_sym->name, com_block->name,
3938
                           &(tmp_sym->declared_at), com_block->name);
3939
            }
3940
          else
3941
            {
3942
              if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3943
                gfc_error ("Type declaration '%s' at %L is not C "
3944
                           "interoperable but it is BIND(C)",
3945
                           tmp_sym->name, &(tmp_sym->declared_at));
3946
              else
3947
                gfc_warning ("Variable '%s' at %L "
3948
                             "may not be a C interoperable "
3949
                             "kind but it is bind(c)",
3950
                             tmp_sym->name, &(tmp_sym->declared_at));
3951
            }
3952
        }
3953
 
3954
      /* Variables declared w/in a common block can't be bind(c)
3955
         since there's no way for C to see these variables, so there's
3956
         semantically no reason for the attribute.  */
3957
      if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3958
        {
3959
          gfc_error ("Variable '%s' in common block '%s' at "
3960
                     "%L cannot be declared with BIND(C) "
3961
                     "since it is not a global",
3962
                     tmp_sym->name, com_block->name,
3963
                     &(tmp_sym->declared_at));
3964
          retval = FAILURE;
3965
        }
3966
 
3967
      /* Scalar variables that are bind(c) can not have the pointer
3968
         or allocatable attributes.  */
3969
      if (tmp_sym->attr.is_bind_c == 1)
3970
        {
3971
          if (tmp_sym->attr.pointer == 1)
3972
            {
3973
              gfc_error ("Variable '%s' at %L cannot have both the "
3974
                         "POINTER and BIND(C) attributes",
3975
                         tmp_sym->name, &(tmp_sym->declared_at));
3976
              retval = FAILURE;
3977
            }
3978
 
3979
          if (tmp_sym->attr.allocatable == 1)
3980
            {
3981
              gfc_error ("Variable '%s' at %L cannot have both the "
3982
                         "ALLOCATABLE and BIND(C) attributes",
3983
                         tmp_sym->name, &(tmp_sym->declared_at));
3984
              retval = FAILURE;
3985
            }
3986
 
3987
        }
3988
 
3989
      /* If it is a BIND(C) function, make sure the return value is a
3990
         scalar value.  The previous tests in this function made sure
3991
         the type is interoperable.  */
3992
      if (bind_c_function && tmp_sym->as != NULL)
3993
        gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3994
                   "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3995
 
3996
      /* BIND(C) functions can not return a character string.  */
3997
      if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
3998
        if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
3999
            || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
4000
            || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
4001
          gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
4002
                         "be a character string", tmp_sym->name,
4003
                         &(tmp_sym->declared_at));
4004
    }
4005
 
4006
  /* See if the symbol has been marked as private.  If it has, make sure
4007
     there is no binding label and warn the user if there is one.  */
4008
  if (tmp_sym->attr.access == ACCESS_PRIVATE
4009
      && tmp_sym->binding_label)
4010
      /* Use gfc_warning_now because we won't say that the symbol fails
4011
         just because of this.  */
4012
      gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
4013
                       "given the binding label '%s'", tmp_sym->name,
4014
                       &(tmp_sym->declared_at), tmp_sym->binding_label);
4015
 
4016
  return retval;
4017
}
4018
 
4019
 
4020
/* Set the appropriate fields for a symbol that's been declared as
4021
   BIND(C) (the is_bind_c flag and the binding label), and verify that
4022
   the type is C interoperable.  Errors are reported by the functions
4023
   used to set/test these fields.  */
4024
 
4025
gfc_try
4026
set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
4027
{
4028
  gfc_try retval = SUCCESS;
4029
 
4030
  /* TODO: Do we need to make sure the vars aren't marked private?  */
4031
 
4032
  /* Set the is_bind_c bit in symbol_attribute.  */
4033
  gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
4034
 
4035
  if (set_binding_label (&tmp_sym->binding_label, tmp_sym->name,
4036
                         num_idents) != SUCCESS)
4037
    return FAILURE;
4038
 
4039
  return retval;
4040
}
4041
 
4042
 
4043
/* Set the fields marking the given common block as BIND(C), including
4044
   a binding label, and report any errors encountered.  */
4045
 
4046
gfc_try
4047
set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
4048
{
4049
  gfc_try retval = SUCCESS;
4050
 
4051
  /* destLabel, common name, typespec (which may have binding label).  */
4052
  if (set_binding_label (&com_block->binding_label, com_block->name,
4053
                         num_idents)
4054
      != SUCCESS)
4055
    return FAILURE;
4056
 
4057
  /* Set the given common block (com_block) to being bind(c) (1).  */
4058
  set_com_block_bind_c (com_block, 1);
4059
 
4060
  return retval;
4061
}
4062
 
4063
 
4064
/* Retrieve the list of one or more identifiers that the given bind(c)
4065
   attribute applies to.  */
4066
 
4067
gfc_try
4068
get_bind_c_idents (void)
4069
{
4070
  char name[GFC_MAX_SYMBOL_LEN + 1];
4071
  int num_idents = 0;
4072
  gfc_symbol *tmp_sym = NULL;
4073
  match found_id;
4074
  gfc_common_head *com_block = NULL;
4075
 
4076
  if (gfc_match_name (name) == MATCH_YES)
4077
    {
4078
      found_id = MATCH_YES;
4079
      gfc_get_ha_symbol (name, &tmp_sym);
4080
    }
4081
  else if (match_common_name (name) == MATCH_YES)
4082
    {
4083
      found_id = MATCH_YES;
4084
      com_block = gfc_get_common (name, 0);
4085
    }
4086
  else
4087
    {
4088
      gfc_error ("Need either entity or common block name for "
4089
                 "attribute specification statement at %C");
4090
      return FAILURE;
4091
    }
4092
 
4093
  /* Save the current identifier and look for more.  */
4094
  do
4095
    {
4096
      /* Increment the number of identifiers found for this spec stmt.  */
4097
      num_idents++;
4098
 
4099
      /* Make sure we have a sym or com block, and verify that it can
4100
         be bind(c).  Set the appropriate field(s) and look for more
4101
         identifiers.  */
4102
      if (tmp_sym != NULL || com_block != NULL)
4103
        {
4104
          if (tmp_sym != NULL)
4105
            {
4106
              if (set_verify_bind_c_sym (tmp_sym, num_idents)
4107
                  != SUCCESS)
4108
                return FAILURE;
4109
            }
4110
          else
4111
            {
4112
              if (set_verify_bind_c_com_block(com_block, num_idents)
4113
                  != SUCCESS)
4114
                return FAILURE;
4115
            }
4116
 
4117
          /* Look to see if we have another identifier.  */
4118
          tmp_sym = NULL;
4119
          if (gfc_match_eos () == MATCH_YES)
4120
            found_id = MATCH_NO;
4121
          else if (gfc_match_char (',') != MATCH_YES)
4122
            found_id = MATCH_NO;
4123
          else if (gfc_match_name (name) == MATCH_YES)
4124
            {
4125
              found_id = MATCH_YES;
4126
              gfc_get_ha_symbol (name, &tmp_sym);
4127
            }
4128
          else if (match_common_name (name) == MATCH_YES)
4129
            {
4130
              found_id = MATCH_YES;
4131
              com_block = gfc_get_common (name, 0);
4132
            }
4133
          else
4134
            {
4135
              gfc_error ("Missing entity or common block name for "
4136
                         "attribute specification statement at %C");
4137
              return FAILURE;
4138
            }
4139
        }
4140
      else
4141
        {
4142
          gfc_internal_error ("Missing symbol");
4143
        }
4144
    } while (found_id == MATCH_YES);
4145
 
4146
  /* if we get here we were successful */
4147
  return SUCCESS;
4148
}
4149
 
4150
 
4151
/* Try and match a BIND(C) attribute specification statement.  */
4152
 
4153
match
4154
gfc_match_bind_c_stmt (void)
4155
{
4156
  match found_match = MATCH_NO;
4157
  gfc_typespec *ts;
4158
 
4159
  ts = &current_ts;
4160
 
4161
  /* This may not be necessary.  */
4162
  gfc_clear_ts (ts);
4163
  /* Clear the temporary binding label holder.  */
4164
  curr_binding_label = NULL;
4165
 
4166
  /* Look for the bind(c).  */
4167
  found_match = gfc_match_bind_c (NULL, true);
4168
 
4169
  if (found_match == MATCH_YES)
4170
    {
4171
      /* Look for the :: now, but it is not required.  */
4172
      gfc_match (" :: ");
4173
 
4174
      /* Get the identifier(s) that needs to be updated.  This may need to
4175
         change to hand the flag(s) for the attr specified so all identifiers
4176
         found can have all appropriate parts updated (assuming that the same
4177
         spec stmt can have multiple attrs, such as both bind(c) and
4178
         allocatable...).  */
4179
      if (get_bind_c_idents () != SUCCESS)
4180
        /* Error message should have printed already.  */
4181
        return MATCH_ERROR;
4182
    }
4183
 
4184
  return found_match;
4185
}
4186
 
4187
 
4188
/* Match a data declaration statement.  */
4189
 
4190
match
4191
gfc_match_data_decl (void)
4192
{
4193
  gfc_symbol *sym;
4194
  match m;
4195
  int elem;
4196
 
4197
  num_idents_on_line = 0;
4198
 
4199
  m = gfc_match_decl_type_spec (&current_ts, 0);
4200
  if (m != MATCH_YES)
4201
    return m;
4202
 
4203
  if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4204
        && gfc_current_state () != COMP_DERIVED)
4205
    {
4206
      sym = gfc_use_derived (current_ts.u.derived);
4207
 
4208
      if (sym == NULL)
4209
        {
4210
          m = MATCH_ERROR;
4211
          goto cleanup;
4212
        }
4213
 
4214
      current_ts.u.derived = sym;
4215
    }
4216
 
4217
  m = match_attr_spec ();
4218
  if (m == MATCH_ERROR)
4219
    {
4220
      m = MATCH_NO;
4221
      goto cleanup;
4222
    }
4223
 
4224
  if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4225
      && current_ts.u.derived->components == NULL
4226
      && !current_ts.u.derived->attr.zero_comp)
4227
    {
4228
 
4229
      if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
4230
        goto ok;
4231
 
4232
      gfc_find_symbol (current_ts.u.derived->name,
4233
                       current_ts.u.derived->ns->parent, 1, &sym);
4234
 
4235
      /* Any symbol that we find had better be a type definition
4236
         which has its components defined.  */
4237
      if (sym != NULL && sym->attr.flavor == FL_DERIVED
4238
          && (current_ts.u.derived->components != NULL
4239
              || current_ts.u.derived->attr.zero_comp))
4240
        goto ok;
4241
 
4242
      /* Now we have an error, which we signal, and then fix up
4243
         because the knock-on is plain and simple confusing.  */
4244
      gfc_error_now ("Derived type at %C has not been previously defined "
4245
                     "and so cannot appear in a derived type definition");
4246
      current_attr.pointer = 1;
4247
      goto ok;
4248
    }
4249
 
4250
ok:
4251
  /* If we have an old-style character declaration, and no new-style
4252
     attribute specifications, then there a comma is optional between
4253
     the type specification and the variable list.  */
4254
  if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
4255
    gfc_match_char (',');
4256
 
4257
  /* Give the types/attributes to symbols that follow. Give the element
4258
     a number so that repeat character length expressions can be copied.  */
4259
  elem = 1;
4260
  for (;;)
4261
    {
4262
      num_idents_on_line++;
4263
      m = variable_decl (elem++);
4264
      if (m == MATCH_ERROR)
4265
        goto cleanup;
4266
      if (m == MATCH_NO)
4267
        break;
4268
 
4269
      if (gfc_match_eos () == MATCH_YES)
4270
        goto cleanup;
4271
      if (gfc_match_char (',') != MATCH_YES)
4272
        break;
4273
    }
4274
 
4275
  if (gfc_error_flag_test () == 0)
4276
    gfc_error ("Syntax error in data declaration at %C");
4277
  m = MATCH_ERROR;
4278
 
4279
  gfc_free_data_all (gfc_current_ns);
4280
 
4281
cleanup:
4282
  gfc_free_array_spec (current_as);
4283
  current_as = NULL;
4284
  return m;
4285
}
4286
 
4287
 
4288
/* Match a prefix associated with a function or subroutine
4289
   declaration.  If the typespec pointer is nonnull, then a typespec
4290
   can be matched.  Note that if nothing matches, MATCH_YES is
4291
   returned (the null string was matched).  */
4292
 
4293
match
4294
gfc_match_prefix (gfc_typespec *ts)
4295
{
4296
  bool seen_type;
4297
  bool seen_impure;
4298
  bool found_prefix;
4299
 
4300
  gfc_clear_attr (&current_attr);
4301
  seen_type = false;
4302
  seen_impure = false;
4303
 
4304
  gcc_assert (!gfc_matching_prefix);
4305
  gfc_matching_prefix = true;
4306
 
4307
  do
4308
    {
4309
      found_prefix = false;
4310
 
4311
      if (!seen_type && ts != NULL
4312
          && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
4313
          && gfc_match_space () == MATCH_YES)
4314
        {
4315
 
4316
          seen_type = true;
4317
          found_prefix = true;
4318
        }
4319
 
4320
      if (gfc_match ("elemental% ") == MATCH_YES)
4321
        {
4322
          if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
4323
            goto error;
4324
 
4325
          found_prefix = true;
4326
        }
4327
 
4328
      if (gfc_match ("pure% ") == MATCH_YES)
4329
        {
4330
          if (gfc_add_pure (&current_attr, NULL) == FAILURE)
4331
            goto error;
4332
 
4333
          found_prefix = true;
4334
        }
4335
 
4336
      if (gfc_match ("recursive% ") == MATCH_YES)
4337
        {
4338
          if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
4339
            goto error;
4340
 
4341
          found_prefix = true;
4342
        }
4343
 
4344
      /* IMPURE is a somewhat special case, as it needs not set an actual
4345
         attribute but rather only prevents ELEMENTAL routines from being
4346
         automatically PURE.  */
4347
      if (gfc_match ("impure% ") == MATCH_YES)
4348
        {
4349
          if (gfc_notify_std (GFC_STD_F2008,
4350
                              "Fortran 2008: IMPURE procedure at %C")
4351
                == FAILURE)
4352
            goto error;
4353
 
4354
          seen_impure = true;
4355
          found_prefix = true;
4356
        }
4357
    }
4358
  while (found_prefix);
4359
 
4360
  /* IMPURE and PURE must not both appear, of course.  */
4361
  if (seen_impure && current_attr.pure)
4362
    {
4363
      gfc_error ("PURE and IMPURE must not appear both at %C");
4364
      goto error;
4365
    }
4366
 
4367
  /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE.  */
4368
  if (!seen_impure && current_attr.elemental && !current_attr.pure)
4369
    {
4370
      if (gfc_add_pure (&current_attr, NULL) == FAILURE)
4371
        goto error;
4372
    }
4373
 
4374
  /* At this point, the next item is not a prefix.  */
4375
  gcc_assert (gfc_matching_prefix);
4376
  gfc_matching_prefix = false;
4377
  return MATCH_YES;
4378
 
4379
error:
4380
  gcc_assert (gfc_matching_prefix);
4381
  gfc_matching_prefix = false;
4382
  return MATCH_ERROR;
4383
}
4384
 
4385
 
4386
/* Copy attributes matched by gfc_match_prefix() to attributes on a symbol.  */
4387
 
4388
static gfc_try
4389
copy_prefix (symbol_attribute *dest, locus *where)
4390
{
4391
  if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
4392
    return FAILURE;
4393
 
4394
  if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
4395
    return FAILURE;
4396
 
4397
  if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
4398
    return FAILURE;
4399
 
4400
  return SUCCESS;
4401
}
4402
 
4403
 
4404
/* Match a formal argument list.  */
4405
 
4406
match
4407
gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
4408
{
4409
  gfc_formal_arglist *head, *tail, *p, *q;
4410
  char name[GFC_MAX_SYMBOL_LEN + 1];
4411
  gfc_symbol *sym;
4412
  match m;
4413
 
4414
  head = tail = NULL;
4415
 
4416
  if (gfc_match_char ('(') != MATCH_YES)
4417
    {
4418
      if (null_flag)
4419
        goto ok;
4420
      return MATCH_NO;
4421
    }
4422
 
4423
  if (gfc_match_char (')') == MATCH_YES)
4424
    goto ok;
4425
 
4426
  for (;;)
4427
    {
4428
      if (gfc_match_char ('*') == MATCH_YES)
4429
        sym = NULL;
4430
      else
4431
        {
4432
          m = gfc_match_name (name);
4433
          if (m != MATCH_YES)
4434
            goto cleanup;
4435
 
4436
          if (gfc_get_symbol (name, NULL, &sym))
4437
            goto cleanup;
4438
        }
4439
 
4440
      p = gfc_get_formal_arglist ();
4441
 
4442
      if (head == NULL)
4443
        head = tail = p;
4444
      else
4445
        {
4446
          tail->next = p;
4447
          tail = p;
4448
        }
4449
 
4450
      tail->sym = sym;
4451
 
4452
      /* We don't add the VARIABLE flavor because the name could be a
4453
         dummy procedure.  We don't apply these attributes to formal
4454
         arguments of statement functions.  */
4455
      if (sym != NULL && !st_flag
4456
          && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
4457
              || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
4458
        {
4459
          m = MATCH_ERROR;
4460
          goto cleanup;
4461
        }
4462
 
4463
      /* The name of a program unit can be in a different namespace,
4464
         so check for it explicitly.  After the statement is accepted,
4465
         the name is checked for especially in gfc_get_symbol().  */
4466
      if (gfc_new_block != NULL && sym != NULL
4467
          && strcmp (sym->name, gfc_new_block->name) == 0)
4468
        {
4469
          gfc_error ("Name '%s' at %C is the name of the procedure",
4470
                     sym->name);
4471
          m = MATCH_ERROR;
4472
          goto cleanup;
4473
        }
4474
 
4475
      if (gfc_match_char (')') == MATCH_YES)
4476
        goto ok;
4477
 
4478
      m = gfc_match_char (',');
4479
      if (m != MATCH_YES)
4480
        {
4481
          gfc_error ("Unexpected junk in formal argument list at %C");
4482
          goto cleanup;
4483
        }
4484
    }
4485
 
4486
ok:
4487
  /* Check for duplicate symbols in the formal argument list.  */
4488
  if (head != NULL)
4489
    {
4490
      for (p = head; p->next; p = p->next)
4491
        {
4492
          if (p->sym == NULL)
4493
            continue;
4494
 
4495
          for (q = p->next; q; q = q->next)
4496
            if (p->sym == q->sym)
4497
              {
4498
                gfc_error ("Duplicate symbol '%s' in formal argument list "
4499
                           "at %C", p->sym->name);
4500
 
4501
                m = MATCH_ERROR;
4502
                goto cleanup;
4503
              }
4504
        }
4505
    }
4506
 
4507
  if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
4508
      == FAILURE)
4509
    {
4510
      m = MATCH_ERROR;
4511
      goto cleanup;
4512
    }
4513
 
4514
  return MATCH_YES;
4515
 
4516
cleanup:
4517
  gfc_free_formal_arglist (head);
4518
  return m;
4519
}
4520
 
4521
 
4522
/* Match a RESULT specification following a function declaration or
4523
   ENTRY statement.  Also matches the end-of-statement.  */
4524
 
4525
static match
4526
match_result (gfc_symbol *function, gfc_symbol **result)
4527
{
4528
  char name[GFC_MAX_SYMBOL_LEN + 1];
4529
  gfc_symbol *r;
4530
  match m;
4531
 
4532
  if (gfc_match (" result (") != MATCH_YES)
4533
    return MATCH_NO;
4534
 
4535
  m = gfc_match_name (name);
4536
  if (m != MATCH_YES)
4537
    return m;
4538
 
4539
  /* Get the right paren, and that's it because there could be the
4540
     bind(c) attribute after the result clause.  */
4541
  if (gfc_match_char(')') != MATCH_YES)
4542
    {
4543
     /* TODO: should report the missing right paren here.  */
4544
      return MATCH_ERROR;
4545
    }
4546
 
4547
  if (strcmp (function->name, name) == 0)
4548
    {
4549
      gfc_error ("RESULT variable at %C must be different than function name");
4550
      return MATCH_ERROR;
4551
    }
4552
 
4553
  if (gfc_get_symbol (name, NULL, &r))
4554
    return MATCH_ERROR;
4555
 
4556
  if (gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
4557
    return MATCH_ERROR;
4558
 
4559
  *result = r;
4560
 
4561
  return MATCH_YES;
4562
}
4563
 
4564
 
4565
/* Match a function suffix, which could be a combination of a result
4566
   clause and BIND(C), either one, or neither.  The draft does not
4567
   require them to come in a specific order.  */
4568
 
4569
match
4570
gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4571
{
4572
  match is_bind_c;   /* Found bind(c).  */
4573
  match is_result;   /* Found result clause.  */
4574
  match found_match; /* Status of whether we've found a good match.  */
4575
  char peek_char;    /* Character we're going to peek at.  */
4576
  bool allow_binding_name;
4577
 
4578
  /* Initialize to having found nothing.  */
4579
  found_match = MATCH_NO;
4580
  is_bind_c = MATCH_NO;
4581
  is_result = MATCH_NO;
4582
 
4583
  /* Get the next char to narrow between result and bind(c).  */
4584
  gfc_gobble_whitespace ();
4585
  peek_char = gfc_peek_ascii_char ();
4586
 
4587
  /* C binding names are not allowed for internal procedures.  */
4588
  if (gfc_current_state () == COMP_CONTAINS
4589
      && sym->ns->proc_name->attr.flavor != FL_MODULE)
4590
    allow_binding_name = false;
4591
  else
4592
    allow_binding_name = true;
4593
 
4594
  switch (peek_char)
4595
    {
4596
    case 'r':
4597
      /* Look for result clause.  */
4598
      is_result = match_result (sym, result);
4599
      if (is_result == MATCH_YES)
4600
        {
4601
          /* Now see if there is a bind(c) after it.  */
4602
          is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4603
          /* We've found the result clause and possibly bind(c).  */
4604
          found_match = MATCH_YES;
4605
        }
4606
      else
4607
        /* This should only be MATCH_ERROR.  */
4608
        found_match = is_result;
4609
      break;
4610
    case 'b':
4611
      /* Look for bind(c) first.  */
4612
      is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4613
      if (is_bind_c == MATCH_YES)
4614
        {
4615
          /* Now see if a result clause followed it.  */
4616
          is_result = match_result (sym, result);
4617
          found_match = MATCH_YES;
4618
        }
4619
      else
4620
        {
4621
          /* Should only be a MATCH_ERROR if we get here after seeing 'b'.  */
4622
          found_match = MATCH_ERROR;
4623
        }
4624
      break;
4625
    default:
4626
      gfc_error ("Unexpected junk after function declaration at %C");
4627
      found_match = MATCH_ERROR;
4628
      break;
4629
    }
4630
 
4631
  if (is_bind_c == MATCH_YES)
4632
    {
4633
      /* Fortran 2008 draft allows BIND(C) for internal procedures.  */
4634
      if (gfc_current_state () == COMP_CONTAINS
4635
          && sym->ns->proc_name->attr.flavor != FL_MODULE
4636
          && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
4637
                             "at %L may not be specified for an internal "
4638
                             "procedure", &gfc_current_locus)
4639
             == FAILURE)
4640
        return MATCH_ERROR;
4641
 
4642
      if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
4643
          == FAILURE)
4644
        return MATCH_ERROR;
4645
    }
4646
 
4647
  return found_match;
4648
}
4649
 
4650
 
4651
/* Procedure pointer return value without RESULT statement:
4652
   Add "hidden" result variable named "ppr@".  */
4653
 
4654
static gfc_try
4655
add_hidden_procptr_result (gfc_symbol *sym)
4656
{
4657
  bool case1,case2;
4658
 
4659
  if (gfc_notification_std (GFC_STD_F2003) == ERROR)
4660
    return FAILURE;
4661
 
4662
  /* First usage case: PROCEDURE and EXTERNAL statements.  */
4663
  case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
4664
          && strcmp (gfc_current_block ()->name, sym->name) == 0
4665
          && sym->attr.external;
4666
  /* Second usage case: INTERFACE statements.  */
4667
  case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
4668
          && gfc_state_stack->previous->state == COMP_FUNCTION
4669
          && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
4670
 
4671
  if (case1 || case2)
4672
    {
4673
      gfc_symtree *stree;
4674
      if (case1)
4675
        gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
4676
      else if (case2)
4677
        {
4678
          gfc_symtree *st2;
4679
          gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
4680
          st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
4681
          st2->n.sym = stree->n.sym;
4682
        }
4683
      sym->result = stree->n.sym;
4684
 
4685
      sym->result->attr.proc_pointer = sym->attr.proc_pointer;
4686
      sym->result->attr.pointer = sym->attr.pointer;
4687
      sym->result->attr.external = sym->attr.external;
4688
      sym->result->attr.referenced = sym->attr.referenced;
4689
      sym->result->ts = sym->ts;
4690
      sym->attr.proc_pointer = 0;
4691
      sym->attr.pointer = 0;
4692
      sym->attr.external = 0;
4693
      if (sym->result->attr.external && sym->result->attr.pointer)
4694
        {
4695
          sym->result->attr.pointer = 0;
4696
          sym->result->attr.proc_pointer = 1;
4697
        }
4698
 
4699
      return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
4700
    }
4701
  /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement.  */
4702
  else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
4703
           && sym->result && sym->result != sym && sym->result->attr.external
4704
           && sym == gfc_current_ns->proc_name
4705
           && sym == sym->result->ns->proc_name
4706
           && strcmp ("ppr@", sym->result->name) == 0)
4707
    {
4708
      sym->result->attr.proc_pointer = 1;
4709
      sym->attr.pointer = 0;
4710
      return SUCCESS;
4711
    }
4712
  else
4713
    return FAILURE;
4714
}
4715
 
4716
 
4717
/* Match the interface for a PROCEDURE declaration,
4718
   including brackets (R1212).  */
4719
 
4720
static match
4721
match_procedure_interface (gfc_symbol **proc_if)
4722
{
4723
  match m;
4724
  gfc_symtree *st;
4725
  locus old_loc, entry_loc;
4726
  gfc_namespace *old_ns = gfc_current_ns;
4727
  char name[GFC_MAX_SYMBOL_LEN + 1];
4728
 
4729
  old_loc = entry_loc = gfc_current_locus;
4730
  gfc_clear_ts (&current_ts);
4731
 
4732
  if (gfc_match (" (") != MATCH_YES)
4733
    {
4734
      gfc_current_locus = entry_loc;
4735
      return MATCH_NO;
4736
    }
4737
 
4738
  /* Get the type spec. for the procedure interface.  */
4739
  old_loc = gfc_current_locus;
4740
  m = gfc_match_decl_type_spec (&current_ts, 0);
4741
  gfc_gobble_whitespace ();
4742
  if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
4743
    goto got_ts;
4744
 
4745
  if (m == MATCH_ERROR)
4746
    return m;
4747
 
4748
  /* Procedure interface is itself a procedure.  */
4749
  gfc_current_locus = old_loc;
4750
  m = gfc_match_name (name);
4751
 
4752
  /* First look to see if it is already accessible in the current
4753
     namespace because it is use associated or contained.  */
4754
  st = NULL;
4755
  if (gfc_find_sym_tree (name, NULL, 0, &st))
4756
    return MATCH_ERROR;
4757
 
4758
  /* If it is still not found, then try the parent namespace, if it
4759
     exists and create the symbol there if it is still not found.  */
4760
  if (gfc_current_ns->parent)
4761
    gfc_current_ns = gfc_current_ns->parent;
4762
  if (st == NULL && gfc_get_ha_sym_tree (name, &st))
4763
    return MATCH_ERROR;
4764
 
4765
  gfc_current_ns = old_ns;
4766
  *proc_if = st->n.sym;
4767
 
4768
  /* Various interface checks.  */
4769
  if (*proc_if)
4770
    {
4771
      (*proc_if)->refs++;
4772
      /* Resolve interface if possible. That way, attr.procedure is only set
4773
         if it is declared by a later procedure-declaration-stmt, which is
4774
         invalid per C1212.  */
4775
      while ((*proc_if)->ts.interface)
4776
        *proc_if = (*proc_if)->ts.interface;
4777
 
4778
      if ((*proc_if)->generic)
4779
        {
4780
          gfc_error ("Interface '%s' at %C may not be generic",
4781
                     (*proc_if)->name);
4782
          return MATCH_ERROR;
4783
        }
4784
      if ((*proc_if)->attr.proc == PROC_ST_FUNCTION)
4785
        {
4786
          gfc_error ("Interface '%s' at %C may not be a statement function",
4787
                     (*proc_if)->name);
4788
          return MATCH_ERROR;
4789
        }
4790
      /* Handle intrinsic procedures.  */
4791
      if (!((*proc_if)->attr.external || (*proc_if)->attr.use_assoc
4792
            || (*proc_if)->attr.if_source == IFSRC_IFBODY)
4793
          && (gfc_is_intrinsic ((*proc_if), 0, gfc_current_locus)
4794
              || gfc_is_intrinsic ((*proc_if), 1, gfc_current_locus)))
4795
        (*proc_if)->attr.intrinsic = 1;
4796
      if ((*proc_if)->attr.intrinsic
4797
          && !gfc_intrinsic_actual_ok ((*proc_if)->name, 0))
4798
        {
4799
          gfc_error ("Intrinsic procedure '%s' not allowed "
4800
                    "in PROCEDURE statement at %C", (*proc_if)->name);
4801
          return MATCH_ERROR;
4802
        }
4803
    }
4804
 
4805
got_ts:
4806
  if (gfc_match (" )") != MATCH_YES)
4807
    {
4808
      gfc_current_locus = entry_loc;
4809
      return MATCH_NO;
4810
    }
4811
 
4812
  return MATCH_YES;
4813
}
4814
 
4815
 
4816
/* Match a PROCEDURE declaration (R1211).  */
4817
 
4818
static match
4819
match_procedure_decl (void)
4820
{
4821
  match m;
4822
  gfc_symbol *sym, *proc_if = NULL;
4823
  int num;
4824
  gfc_expr *initializer = NULL;
4825
 
4826
  /* Parse interface (with brackets). */
4827
  m = match_procedure_interface (&proc_if);
4828
  if (m != MATCH_YES)
4829
    return m;
4830
 
4831
  /* Parse attributes (with colons).  */
4832
  m = match_attr_spec();
4833
  if (m == MATCH_ERROR)
4834
    return MATCH_ERROR;
4835
 
4836
  /* Get procedure symbols.  */
4837
  for(num=1;;num++)
4838
    {
4839
      m = gfc_match_symbol (&sym, 0);
4840
      if (m == MATCH_NO)
4841
        goto syntax;
4842
      else if (m == MATCH_ERROR)
4843
        return m;
4844
 
4845
      /* Add current_attr to the symbol attributes.  */
4846
      if (gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
4847
        return MATCH_ERROR;
4848
 
4849
      if (sym->attr.is_bind_c)
4850
        {
4851
          /* Check for C1218.  */
4852
          if (!proc_if || !proc_if->attr.is_bind_c)
4853
            {
4854
              gfc_error ("BIND(C) attribute at %C requires "
4855
                        "an interface with BIND(C)");
4856
              return MATCH_ERROR;
4857
            }
4858
          /* Check for C1217.  */
4859
          if (has_name_equals && sym->attr.pointer)
4860
            {
4861
              gfc_error ("BIND(C) procedure with NAME may not have "
4862
                        "POINTER attribute at %C");
4863
              return MATCH_ERROR;
4864
            }
4865
          if (has_name_equals && sym->attr.dummy)
4866
            {
4867
              gfc_error ("Dummy procedure at %C may not have "
4868
                        "BIND(C) attribute with NAME");
4869
              return MATCH_ERROR;
4870
            }
4871
          /* Set binding label for BIND(C).  */
4872
          if (set_binding_label (&sym->binding_label, sym->name, num)
4873
              != SUCCESS)
4874
            return MATCH_ERROR;
4875
        }
4876
 
4877
      if (gfc_add_external (&sym->attr, NULL) == FAILURE)
4878
        return MATCH_ERROR;
4879
 
4880
      if (add_hidden_procptr_result (sym) == SUCCESS)
4881
        sym = sym->result;
4882
 
4883
      if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4884
        return MATCH_ERROR;
4885
 
4886
      /* Set interface.  */
4887
      if (proc_if != NULL)
4888
        {
4889
          if (sym->ts.type != BT_UNKNOWN)
4890
            {
4891
              gfc_error ("Procedure '%s' at %L already has basic type of %s",
4892
                         sym->name, &gfc_current_locus,
4893
                         gfc_basic_typename (sym->ts.type));
4894
              return MATCH_ERROR;
4895
            }
4896
          sym->ts.interface = proc_if;
4897
          sym->attr.untyped = 1;
4898
          sym->attr.if_source = IFSRC_IFBODY;
4899
        }
4900
      else if (current_ts.type != BT_UNKNOWN)
4901
        {
4902
          if (gfc_add_type (sym, &current_ts, &gfc_current_locus) == FAILURE)
4903
            return MATCH_ERROR;
4904
          sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4905
          sym->ts.interface->ts = current_ts;
4906
          sym->ts.interface->attr.flavor = FL_PROCEDURE;
4907
          sym->ts.interface->attr.function = 1;
4908
          sym->attr.function = 1;
4909
          sym->attr.if_source = IFSRC_UNKNOWN;
4910
        }
4911
 
4912
      if (gfc_match (" =>") == MATCH_YES)
4913
        {
4914
          if (!current_attr.pointer)
4915
            {
4916
              gfc_error ("Initialization at %C isn't for a pointer variable");
4917
              m = MATCH_ERROR;
4918
              goto cleanup;
4919
            }
4920
 
4921
          m = match_pointer_init (&initializer, 1);
4922
          if (m != MATCH_YES)
4923
            goto cleanup;
4924
 
4925
          if (add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus)
4926
              != SUCCESS)
4927
            goto cleanup;
4928
 
4929
        }
4930
 
4931
      gfc_set_sym_referenced (sym);
4932
 
4933
      if (gfc_match_eos () == MATCH_YES)
4934
        return MATCH_YES;
4935
      if (gfc_match_char (',') != MATCH_YES)
4936
        goto syntax;
4937
    }
4938
 
4939
syntax:
4940
  gfc_error ("Syntax error in PROCEDURE statement at %C");
4941
  return MATCH_ERROR;
4942
 
4943
cleanup:
4944
  /* Free stuff up and return.  */
4945
  gfc_free_expr (initializer);
4946
  return m;
4947
}
4948
 
4949
 
4950
static match
4951
match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
4952
 
4953
 
4954
/* Match a procedure pointer component declaration (R445).  */
4955
 
4956
static match
4957
match_ppc_decl (void)
4958
{
4959
  match m;
4960
  gfc_symbol *proc_if = NULL;
4961
  gfc_typespec ts;
4962
  int num;
4963
  gfc_component *c;
4964
  gfc_expr *initializer = NULL;
4965
  gfc_typebound_proc* tb;
4966
  char name[GFC_MAX_SYMBOL_LEN + 1];
4967
 
4968
  /* Parse interface (with brackets).  */
4969
  m = match_procedure_interface (&proc_if);
4970
  if (m != MATCH_YES)
4971
    goto syntax;
4972
 
4973
  /* Parse attributes.  */
4974
  tb = XCNEW (gfc_typebound_proc);
4975
  tb->where = gfc_current_locus;
4976
  m = match_binding_attributes (tb, false, true);
4977
  if (m == MATCH_ERROR)
4978
    return m;
4979
 
4980
  gfc_clear_attr (&current_attr);
4981
  current_attr.procedure = 1;
4982
  current_attr.proc_pointer = 1;
4983
  current_attr.access = tb->access;
4984
  current_attr.flavor = FL_PROCEDURE;
4985
 
4986
  /* Match the colons (required).  */
4987
  if (gfc_match (" ::") != MATCH_YES)
4988
    {
4989
      gfc_error ("Expected '::' after binding-attributes at %C");
4990
      return MATCH_ERROR;
4991
    }
4992
 
4993
  /* Check for C450.  */
4994
  if (!tb->nopass && proc_if == NULL)
4995
    {
4996
      gfc_error("NOPASS or explicit interface required at %C");
4997
      return MATCH_ERROR;
4998
    }
4999
 
5000
  if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure pointer "
5001
                     "component at %C") == FAILURE)
5002
    return MATCH_ERROR;
5003
 
5004
  /* Match PPC names.  */
5005
  ts = current_ts;
5006
  for(num=1;;num++)
5007
    {
5008
      m = gfc_match_name (name);
5009
      if (m == MATCH_NO)
5010
        goto syntax;
5011
      else if (m == MATCH_ERROR)
5012
        return m;
5013
 
5014
      if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
5015
        return MATCH_ERROR;
5016
 
5017
      /* Add current_attr to the symbol attributes.  */
5018
      if (gfc_copy_attr (&c->attr, &current_attr, NULL) == FAILURE)
5019
        return MATCH_ERROR;
5020
 
5021
      if (gfc_add_external (&c->attr, NULL) == FAILURE)
5022
        return MATCH_ERROR;
5023
 
5024
      if (gfc_add_proc (&c->attr, name, NULL) == FAILURE)
5025
        return MATCH_ERROR;
5026
 
5027
      c->tb = tb;
5028
 
5029
      /* Set interface.  */
5030
      if (proc_if != NULL)
5031
        {
5032
          c->ts.interface = proc_if;
5033
          c->attr.untyped = 1;
5034
          c->attr.if_source = IFSRC_IFBODY;
5035
        }
5036
      else if (ts.type != BT_UNKNOWN)
5037
        {
5038
          c->ts = ts;
5039
          c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
5040
          c->ts.interface->ts = ts;
5041
          c->ts.interface->attr.flavor = FL_PROCEDURE;
5042
          c->ts.interface->attr.function = 1;
5043
          c->attr.function = 1;
5044
          c->attr.if_source = IFSRC_UNKNOWN;
5045
        }
5046
 
5047
      if (gfc_match (" =>") == MATCH_YES)
5048
        {
5049
          m = match_pointer_init (&initializer, 1);
5050
          if (m != MATCH_YES)
5051
            {
5052
              gfc_free_expr (initializer);
5053
              return m;
5054
            }
5055
          c->initializer = initializer;
5056
        }
5057
 
5058
      if (gfc_match_eos () == MATCH_YES)
5059
        return MATCH_YES;
5060
      if (gfc_match_char (',') != MATCH_YES)
5061
        goto syntax;
5062
    }
5063
 
5064
syntax:
5065
  gfc_error ("Syntax error in procedure pointer component at %C");
5066
  return MATCH_ERROR;
5067
}
5068
 
5069
 
5070
/* Match a PROCEDURE declaration inside an interface (R1206).  */
5071
 
5072
static match
5073
match_procedure_in_interface (void)
5074
{
5075
  match m;
5076
  gfc_symbol *sym;
5077
  char name[GFC_MAX_SYMBOL_LEN + 1];
5078
 
5079
  if (current_interface.type == INTERFACE_NAMELESS
5080
      || current_interface.type == INTERFACE_ABSTRACT)
5081
    {
5082
      gfc_error ("PROCEDURE at %C must be in a generic interface");
5083
      return MATCH_ERROR;
5084
    }
5085
 
5086
  for(;;)
5087
    {
5088
      m = gfc_match_name (name);
5089
      if (m == MATCH_NO)
5090
        goto syntax;
5091
      else if (m == MATCH_ERROR)
5092
        return m;
5093
      if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
5094
        return MATCH_ERROR;
5095
 
5096
      if (gfc_add_interface (sym) == FAILURE)
5097
        return MATCH_ERROR;
5098
 
5099
      if (gfc_match_eos () == MATCH_YES)
5100
        break;
5101
      if (gfc_match_char (',') != MATCH_YES)
5102
        goto syntax;
5103
    }
5104
 
5105
  return MATCH_YES;
5106
 
5107
syntax:
5108
  gfc_error ("Syntax error in PROCEDURE statement at %C");
5109
  return MATCH_ERROR;
5110
}
5111
 
5112
 
5113
/* General matcher for PROCEDURE declarations.  */
5114
 
5115
static match match_procedure_in_type (void);
5116
 
5117
match
5118
gfc_match_procedure (void)
5119
{
5120
  match m;
5121
 
5122
  switch (gfc_current_state ())
5123
    {
5124
    case COMP_NONE:
5125
    case COMP_PROGRAM:
5126
    case COMP_MODULE:
5127
    case COMP_SUBROUTINE:
5128
    case COMP_FUNCTION:
5129
    case COMP_BLOCK:
5130
      m = match_procedure_decl ();
5131
      break;
5132
    case COMP_INTERFACE:
5133
      m = match_procedure_in_interface ();
5134
      break;
5135
    case COMP_DERIVED:
5136
      m = match_ppc_decl ();
5137
      break;
5138
    case COMP_DERIVED_CONTAINS:
5139
      m = match_procedure_in_type ();
5140
      break;
5141
    default:
5142
      return MATCH_NO;
5143
    }
5144
 
5145
  if (m != MATCH_YES)
5146
    return m;
5147
 
5148
  if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
5149
      == FAILURE)
5150
    return MATCH_ERROR;
5151
 
5152
  return m;
5153
}
5154
 
5155
 
5156
/* Warn if a matched procedure has the same name as an intrinsic; this is
5157
   simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5158
   parser-state-stack to find out whether we're in a module.  */
5159
 
5160
static void
5161
warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
5162
{
5163
  bool in_module;
5164
 
5165
  in_module = (gfc_state_stack->previous
5166
               && gfc_state_stack->previous->state == COMP_MODULE);
5167
 
5168
  gfc_warn_intrinsic_shadow (sym, in_module, func);
5169
}
5170
 
5171
 
5172
/* Match a function declaration.  */
5173
 
5174
match
5175
gfc_match_function_decl (void)
5176
{
5177
  char name[GFC_MAX_SYMBOL_LEN + 1];
5178
  gfc_symbol *sym, *result;
5179
  locus old_loc;
5180
  match m;
5181
  match suffix_match;
5182
  match found_match; /* Status returned by match func.  */
5183
 
5184
  if (gfc_current_state () != COMP_NONE
5185
      && gfc_current_state () != COMP_INTERFACE
5186
      && gfc_current_state () != COMP_CONTAINS)
5187
    return MATCH_NO;
5188
 
5189
  gfc_clear_ts (&current_ts);
5190
 
5191
  old_loc = gfc_current_locus;
5192
 
5193
  m = gfc_match_prefix (&current_ts);
5194
  if (m != MATCH_YES)
5195
    {
5196
      gfc_current_locus = old_loc;
5197
      return m;
5198
    }
5199
 
5200
  if (gfc_match ("function% %n", name) != MATCH_YES)
5201
    {
5202
      gfc_current_locus = old_loc;
5203
      return MATCH_NO;
5204
    }
5205
  if (get_proc_name (name, &sym, false))
5206
    return MATCH_ERROR;
5207
 
5208
  if (add_hidden_procptr_result (sym) == SUCCESS)
5209
    sym = sym->result;
5210
 
5211
  gfc_new_block = sym;
5212
 
5213
  m = gfc_match_formal_arglist (sym, 0, 0);
5214
  if (m == MATCH_NO)
5215
    {
5216
      gfc_error ("Expected formal argument list in function "
5217
                 "definition at %C");
5218
      m = MATCH_ERROR;
5219
      goto cleanup;
5220
    }
5221
  else if (m == MATCH_ERROR)
5222
    goto cleanup;
5223
 
5224
  result = NULL;
5225
 
5226
  /* According to the draft, the bind(c) and result clause can
5227
     come in either order after the formal_arg_list (i.e., either
5228
     can be first, both can exist together or by themselves or neither
5229
     one).  Therefore, the match_result can't match the end of the
5230
     string, and check for the bind(c) or result clause in either order.  */
5231
  found_match = gfc_match_eos ();
5232
 
5233
  /* Make sure that it isn't already declared as BIND(C).  If it is, it
5234
     must have been marked BIND(C) with a BIND(C) attribute and that is
5235
     not allowed for procedures.  */
5236
  if (sym->attr.is_bind_c == 1)
5237
    {
5238
      sym->attr.is_bind_c = 0;
5239
      if (sym->old_symbol != NULL)
5240
        gfc_error_now ("BIND(C) attribute at %L can only be used for "
5241
                       "variables or common blocks",
5242
                       &(sym->old_symbol->declared_at));
5243
      else
5244
        gfc_error_now ("BIND(C) attribute at %L can only be used for "
5245
                       "variables or common blocks", &gfc_current_locus);
5246
    }
5247
 
5248
  if (found_match != MATCH_YES)
5249
    {
5250
      /* If we haven't found the end-of-statement, look for a suffix.  */
5251
      suffix_match = gfc_match_suffix (sym, &result);
5252
      if (suffix_match == MATCH_YES)
5253
        /* Need to get the eos now.  */
5254
        found_match = gfc_match_eos ();
5255
      else
5256
        found_match = suffix_match;
5257
    }
5258
 
5259
  if(found_match != MATCH_YES)
5260
    m = MATCH_ERROR;
5261
  else
5262
    {
5263
      /* Make changes to the symbol.  */
5264
      m = MATCH_ERROR;
5265
 
5266
      if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
5267
        goto cleanup;
5268
 
5269
      if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
5270
          || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5271
        goto cleanup;
5272
 
5273
      /* Delay matching the function characteristics until after the
5274
         specification block by signalling kind=-1.  */
5275
      sym->declared_at = old_loc;
5276
      if (current_ts.type != BT_UNKNOWN)
5277
        current_ts.kind = -1;
5278
      else
5279
        current_ts.kind = 0;
5280
 
5281
      if (result == NULL)
5282
        {
5283
          if (current_ts.type != BT_UNKNOWN
5284
              && gfc_add_type (sym, &current_ts, &gfc_current_locus) == FAILURE)
5285
            goto cleanup;
5286
          sym->result = sym;
5287
        }
5288
      else
5289
        {
5290
          if (current_ts.type != BT_UNKNOWN
5291
              && gfc_add_type (result, &current_ts, &gfc_current_locus)
5292
                 == FAILURE)
5293
            goto cleanup;
5294
          sym->result = result;
5295
        }
5296
 
5297
      /* Warn if this procedure has the same name as an intrinsic.  */
5298
      warn_intrinsic_shadow (sym, true);
5299
 
5300
      return MATCH_YES;
5301
    }
5302
 
5303
cleanup:
5304
  gfc_current_locus = old_loc;
5305
  return m;
5306
}
5307
 
5308
 
5309
/* This is mostly a copy of parse.c(add_global_procedure) but modified to
5310
   pass the name of the entry, rather than the gfc_current_block name, and
5311
   to return false upon finding an existing global entry.  */
5312
 
5313
static bool
5314
add_global_entry (const char *name, int sub)
5315
{
5316
  gfc_gsymbol *s;
5317
  enum gfc_symbol_type type;
5318
 
5319
  s = gfc_get_gsymbol(name);
5320
  type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5321
 
5322
  if (s->defined
5323
      || (s->type != GSYM_UNKNOWN
5324
          && s->type != type))
5325
    gfc_global_used(s, NULL);
5326
  else
5327
    {
5328
      s->type = type;
5329
      s->where = gfc_current_locus;
5330
      s->defined = 1;
5331
      s->ns = gfc_current_ns;
5332
      return true;
5333
    }
5334
  return false;
5335
}
5336
 
5337
 
5338
/* Match an ENTRY statement.  */
5339
 
5340
match
5341
gfc_match_entry (void)
5342
{
5343
  gfc_symbol *proc;
5344
  gfc_symbol *result;
5345
  gfc_symbol *entry;
5346
  char name[GFC_MAX_SYMBOL_LEN + 1];
5347
  gfc_compile_state state;
5348
  match m;
5349
  gfc_entry_list *el;
5350
  locus old_loc;
5351
  bool module_procedure;
5352
  char peek_char;
5353
  match is_bind_c;
5354
 
5355
  m = gfc_match_name (name);
5356
  if (m != MATCH_YES)
5357
    return m;
5358
 
5359
  if (gfc_notify_std (GFC_STD_F2008_OBS, "Fortran 2008 obsolescent feature: "
5360
                      "ENTRY statement at %C") == FAILURE)
5361
    return MATCH_ERROR;
5362
 
5363
  state = gfc_current_state ();
5364
  if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
5365
    {
5366
      switch (state)
5367
        {
5368
          case COMP_PROGRAM:
5369
            gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5370
            break;
5371
          case COMP_MODULE:
5372
            gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5373
            break;
5374
          case COMP_BLOCK_DATA:
5375
            gfc_error ("ENTRY statement at %C cannot appear within "
5376
                       "a BLOCK DATA");
5377
            break;
5378
          case COMP_INTERFACE:
5379
            gfc_error ("ENTRY statement at %C cannot appear within "
5380
                       "an INTERFACE");
5381
            break;
5382
          case COMP_DERIVED:
5383
            gfc_error ("ENTRY statement at %C cannot appear within "
5384
                       "a DERIVED TYPE block");
5385
            break;
5386
          case COMP_IF:
5387
            gfc_error ("ENTRY statement at %C cannot appear within "
5388
                       "an IF-THEN block");
5389
            break;
5390
          case COMP_DO:
5391
          case COMP_DO_CONCURRENT:
5392
            gfc_error ("ENTRY statement at %C cannot appear within "
5393
                       "a DO block");
5394
            break;
5395
          case COMP_SELECT:
5396
            gfc_error ("ENTRY statement at %C cannot appear within "
5397
                       "a SELECT block");
5398
            break;
5399
          case COMP_FORALL:
5400
            gfc_error ("ENTRY statement at %C cannot appear within "
5401
                       "a FORALL block");
5402
            break;
5403
          case COMP_WHERE:
5404
            gfc_error ("ENTRY statement at %C cannot appear within "
5405
                       "a WHERE block");
5406
            break;
5407
          case COMP_CONTAINS:
5408
            gfc_error ("ENTRY statement at %C cannot appear within "
5409
                       "a contained subprogram");
5410
            break;
5411
          default:
5412
            gfc_internal_error ("gfc_match_entry(): Bad state");
5413
        }
5414
      return MATCH_ERROR;
5415
    }
5416
 
5417
  module_procedure = gfc_current_ns->parent != NULL
5418
                   && gfc_current_ns->parent->proc_name
5419
                   && gfc_current_ns->parent->proc_name->attr.flavor
5420
                      == FL_MODULE;
5421
 
5422
  if (gfc_current_ns->parent != NULL
5423
      && gfc_current_ns->parent->proc_name
5424
      && !module_procedure)
5425
    {
5426
      gfc_error("ENTRY statement at %C cannot appear in a "
5427
                "contained procedure");
5428
      return MATCH_ERROR;
5429
    }
5430
 
5431
  /* Module function entries need special care in get_proc_name
5432
     because previous references within the function will have
5433
     created symbols attached to the current namespace.  */
5434
  if (get_proc_name (name, &entry,
5435
                     gfc_current_ns->parent != NULL
5436
                     && module_procedure))
5437
    return MATCH_ERROR;
5438
 
5439
  proc = gfc_current_block ();
5440
 
5441
  /* Make sure that it isn't already declared as BIND(C).  If it is, it
5442
     must have been marked BIND(C) with a BIND(C) attribute and that is
5443
     not allowed for procedures.  */
5444
  if (entry->attr.is_bind_c == 1)
5445
    {
5446
      entry->attr.is_bind_c = 0;
5447
      if (entry->old_symbol != NULL)
5448
        gfc_error_now ("BIND(C) attribute at %L can only be used for "
5449
                       "variables or common blocks",
5450
                       &(entry->old_symbol->declared_at));
5451
      else
5452
        gfc_error_now ("BIND(C) attribute at %L can only be used for "
5453
                       "variables or common blocks", &gfc_current_locus);
5454
    }
5455
 
5456
  /* Check what next non-whitespace character is so we can tell if there
5457
     is the required parens if we have a BIND(C).  */
5458
  gfc_gobble_whitespace ();
5459
  peek_char = gfc_peek_ascii_char ();
5460
 
5461
  if (state == COMP_SUBROUTINE)
5462
    {
5463
      /* An entry in a subroutine.  */
5464
      if (!gfc_current_ns->parent && !add_global_entry (name, 1))
5465
        return MATCH_ERROR;
5466
 
5467
      m = gfc_match_formal_arglist (entry, 0, 1);
5468
      if (m != MATCH_YES)
5469
        return MATCH_ERROR;
5470
 
5471
      /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5472
         never be an internal procedure.  */
5473
      is_bind_c = gfc_match_bind_c (entry, true);
5474
      if (is_bind_c == MATCH_ERROR)
5475
        return MATCH_ERROR;
5476
      if (is_bind_c == MATCH_YES)
5477
        {
5478
          if (peek_char != '(')
5479
            {
5480
              gfc_error ("Missing required parentheses before BIND(C) at %C");
5481
              return MATCH_ERROR;
5482
            }
5483
            if (gfc_add_is_bind_c (&(entry->attr), entry->name, &(entry->declared_at), 1)
5484
                == FAILURE)
5485
              return MATCH_ERROR;
5486
        }
5487
 
5488
      if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5489
          || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
5490
        return MATCH_ERROR;
5491
    }
5492
  else
5493
    {
5494
      /* An entry in a function.
5495
         We need to take special care because writing
5496
            ENTRY f()
5497
         as
5498
            ENTRY f
5499
         is allowed, whereas
5500
            ENTRY f() RESULT (r)
5501
         can't be written as
5502
            ENTRY f RESULT (r).  */
5503
      if (!gfc_current_ns->parent && !add_global_entry (name, 0))
5504
        return MATCH_ERROR;
5505
 
5506
      old_loc = gfc_current_locus;
5507
      if (gfc_match_eos () == MATCH_YES)
5508
        {
5509
          gfc_current_locus = old_loc;
5510
          /* Match the empty argument list, and add the interface to
5511
             the symbol.  */
5512
          m = gfc_match_formal_arglist (entry, 0, 1);
5513
        }
5514
      else
5515
        m = gfc_match_formal_arglist (entry, 0, 0);
5516
 
5517
      if (m != MATCH_YES)
5518
        return MATCH_ERROR;
5519
 
5520
      result = NULL;
5521
 
5522
      if (gfc_match_eos () == MATCH_YES)
5523
        {
5524
          if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5525
              || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5526
            return MATCH_ERROR;
5527
 
5528
          entry->result = entry;
5529
        }
5530
      else
5531
        {
5532
          m = gfc_match_suffix (entry, &result);
5533
          if (m == MATCH_NO)
5534
            gfc_syntax_error (ST_ENTRY);
5535
          if (m != MATCH_YES)
5536
            return MATCH_ERROR;
5537
 
5538
          if (result)
5539
            {
5540
              if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
5541
                  || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
5542
                  || gfc_add_function (&entry->attr, result->name, NULL)
5543
                  == FAILURE)
5544
                return MATCH_ERROR;
5545
              entry->result = result;
5546
            }
5547
          else
5548
            {
5549
              if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5550
                  || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5551
                return MATCH_ERROR;
5552
              entry->result = entry;
5553
            }
5554
        }
5555
    }
5556
 
5557
  if (gfc_match_eos () != MATCH_YES)
5558
    {
5559
      gfc_syntax_error (ST_ENTRY);
5560
      return MATCH_ERROR;
5561
    }
5562
 
5563
  entry->attr.recursive = proc->attr.recursive;
5564
  entry->attr.elemental = proc->attr.elemental;
5565
  entry->attr.pure = proc->attr.pure;
5566
 
5567
  el = gfc_get_entry_list ();
5568
  el->sym = entry;
5569
  el->next = gfc_current_ns->entries;
5570
  gfc_current_ns->entries = el;
5571
  if (el->next)
5572
    el->id = el->next->id + 1;
5573
  else
5574
    el->id = 1;
5575
 
5576
  new_st.op = EXEC_ENTRY;
5577
  new_st.ext.entry = el;
5578
 
5579
  return MATCH_YES;
5580
}
5581
 
5582
 
5583
/* Match a subroutine statement, including optional prefixes.  */
5584
 
5585
match
5586
gfc_match_subroutine (void)
5587
{
5588
  char name[GFC_MAX_SYMBOL_LEN + 1];
5589
  gfc_symbol *sym;
5590
  match m;
5591
  match is_bind_c;
5592
  char peek_char;
5593
  bool allow_binding_name;
5594
 
5595
  if (gfc_current_state () != COMP_NONE
5596
      && gfc_current_state () != COMP_INTERFACE
5597
      && gfc_current_state () != COMP_CONTAINS)
5598
    return MATCH_NO;
5599
 
5600
  m = gfc_match_prefix (NULL);
5601
  if (m != MATCH_YES)
5602
    return m;
5603
 
5604
  m = gfc_match ("subroutine% %n", name);
5605
  if (m != MATCH_YES)
5606
    return m;
5607
 
5608
  if (get_proc_name (name, &sym, false))
5609
    return MATCH_ERROR;
5610
 
5611
  /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5612
     the symbol existed before. */
5613
  sym->declared_at = gfc_current_locus;
5614
 
5615
  if (add_hidden_procptr_result (sym) == SUCCESS)
5616
    sym = sym->result;
5617
 
5618
  gfc_new_block = sym;
5619
 
5620
  /* Check what next non-whitespace character is so we can tell if there
5621
     is the required parens if we have a BIND(C).  */
5622
  gfc_gobble_whitespace ();
5623
  peek_char = gfc_peek_ascii_char ();
5624
 
5625
  if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
5626
    return MATCH_ERROR;
5627
 
5628
  if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
5629
    return MATCH_ERROR;
5630
 
5631
  /* Make sure that it isn't already declared as BIND(C).  If it is, it
5632
     must have been marked BIND(C) with a BIND(C) attribute and that is
5633
     not allowed for procedures.  */
5634
  if (sym->attr.is_bind_c == 1)
5635
    {
5636
      sym->attr.is_bind_c = 0;
5637
      if (sym->old_symbol != NULL)
5638
        gfc_error_now ("BIND(C) attribute at %L can only be used for "
5639
                       "variables or common blocks",
5640
                       &(sym->old_symbol->declared_at));
5641
      else
5642
        gfc_error_now ("BIND(C) attribute at %L can only be used for "
5643
                       "variables or common blocks", &gfc_current_locus);
5644
    }
5645
 
5646
  /* C binding names are not allowed for internal procedures.  */
5647
  if (gfc_current_state () == COMP_CONTAINS
5648
      && sym->ns->proc_name->attr.flavor != FL_MODULE)
5649
    allow_binding_name = false;
5650
  else
5651
    allow_binding_name = true;
5652
 
5653
  /* Here, we are just checking if it has the bind(c) attribute, and if
5654
     so, then we need to make sure it's all correct.  If it doesn't,
5655
     we still need to continue matching the rest of the subroutine line.  */
5656
  is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5657
  if (is_bind_c == MATCH_ERROR)
5658
    {
5659
      /* There was an attempt at the bind(c), but it was wrong.  An
5660
         error message should have been printed w/in the gfc_match_bind_c
5661
         so here we'll just return the MATCH_ERROR.  */
5662
      return MATCH_ERROR;
5663
    }
5664
 
5665
  if (is_bind_c == MATCH_YES)
5666
    {
5667
      /* The following is allowed in the Fortran 2008 draft.  */
5668
      if (gfc_current_state () == COMP_CONTAINS
5669
          && sym->ns->proc_name->attr.flavor != FL_MODULE
5670
          && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
5671
                             "at %L may not be specified for an internal "
5672
                             "procedure", &gfc_current_locus)
5673
             == FAILURE)
5674
        return MATCH_ERROR;
5675
 
5676
      if (peek_char != '(')
5677
        {
5678
          gfc_error ("Missing required parentheses before BIND(C) at %C");
5679
          return MATCH_ERROR;
5680
        }
5681
      if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
5682
          == FAILURE)
5683
        return MATCH_ERROR;
5684
    }
5685
 
5686
  if (gfc_match_eos () != MATCH_YES)
5687
    {
5688
      gfc_syntax_error (ST_SUBROUTINE);
5689
      return MATCH_ERROR;
5690
    }
5691
 
5692
  if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5693
    return MATCH_ERROR;
5694
 
5695
  /* Warn if it has the same name as an intrinsic.  */
5696
  warn_intrinsic_shadow (sym, false);
5697
 
5698
  return MATCH_YES;
5699
}
5700
 
5701
 
5702
/* Match a BIND(C) specifier, with the optional 'name=' specifier if
5703
   given, and set the binding label in either the given symbol (if not
5704
   NULL), or in the current_ts.  The symbol may be NULL because we may
5705
   encounter the BIND(C) before the declaration itself.  Return
5706
   MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5707
   MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5708
   or MATCH_YES if the specifier was correct and the binding label and
5709
   bind(c) fields were set correctly for the given symbol or the
5710
   current_ts. If allow_binding_name is false, no binding name may be
5711
   given.  */
5712
 
5713
match
5714
gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
5715
{
5716
  /* binding label, if exists */
5717
  const char* binding_label = NULL;
5718
  match double_quote;
5719
  match single_quote;
5720
 
5721
  /* Initialize the flag that specifies whether we encountered a NAME=
5722
     specifier or not.  */
5723
  has_name_equals = 0;
5724
 
5725
  /* This much we have to be able to match, in this order, if
5726
     there is a bind(c) label.  */
5727
  if (gfc_match (" bind ( c ") != MATCH_YES)
5728
    return MATCH_NO;
5729
 
5730
  /* Now see if there is a binding label, or if we've reached the
5731
     end of the bind(c) attribute without one.  */
5732
  if (gfc_match_char (',') == MATCH_YES)
5733
    {
5734
      if (gfc_match (" name = ") != MATCH_YES)
5735
        {
5736
          gfc_error ("Syntax error in NAME= specifier for binding label "
5737
                     "at %C");
5738
          /* should give an error message here */
5739
          return MATCH_ERROR;
5740
        }
5741
 
5742
      has_name_equals = 1;
5743
 
5744
      /* Get the opening quote.  */
5745
      double_quote = MATCH_YES;
5746
      single_quote = MATCH_YES;
5747
      double_quote = gfc_match_char ('"');
5748
      if (double_quote != MATCH_YES)
5749
        single_quote = gfc_match_char ('\'');
5750
      if (double_quote != MATCH_YES && single_quote != MATCH_YES)
5751
        {
5752
          gfc_error ("Syntax error in NAME= specifier for binding label "
5753
                     "at %C");
5754
          return MATCH_ERROR;
5755
        }
5756
 
5757
      /* Grab the binding label, using functions that will not lower
5758
         case the names automatically.  */
5759
      if (gfc_match_name_C (&binding_label) != MATCH_YES)
5760
         return MATCH_ERROR;
5761
 
5762
      /* Get the closing quotation.  */
5763
      if (double_quote == MATCH_YES)
5764
        {
5765
          if (gfc_match_char ('"') != MATCH_YES)
5766
            {
5767
              gfc_error ("Missing closing quote '\"' for binding label at %C");
5768
              /* User started string with '"' so looked to match it.  */
5769
              return MATCH_ERROR;
5770
            }
5771
        }
5772
      else
5773
        {
5774
          if (gfc_match_char ('\'') != MATCH_YES)
5775
            {
5776
              gfc_error ("Missing closing quote '\'' for binding label at %C");
5777
              /* User started string with "'" char.  */
5778
              return MATCH_ERROR;
5779
            }
5780
        }
5781
   }
5782
 
5783
  /* Get the required right paren.  */
5784
  if (gfc_match_char (')') != MATCH_YES)
5785
    {
5786
      gfc_error ("Missing closing paren for binding label at %C");
5787
      return MATCH_ERROR;
5788
    }
5789
 
5790
  if (has_name_equals && !allow_binding_name)
5791
    {
5792
      gfc_error ("No binding name is allowed in BIND(C) at %C");
5793
      return MATCH_ERROR;
5794
    }
5795
 
5796
  if (has_name_equals && sym != NULL && sym->attr.dummy)
5797
    {
5798
      gfc_error ("For dummy procedure %s, no binding name is "
5799
                 "allowed in BIND(C) at %C", sym->name);
5800
      return MATCH_ERROR;
5801
    }
5802
 
5803
 
5804
  /* Save the binding label to the symbol.  If sym is null, we're
5805
     probably matching the typespec attributes of a declaration and
5806
     haven't gotten the name yet, and therefore, no symbol yet.  */
5807
  if (binding_label)
5808
    {
5809
      if (sym != NULL)
5810
        sym->binding_label = binding_label;
5811
      else
5812
        curr_binding_label = binding_label;
5813
    }
5814
  else if (allow_binding_name)
5815
    {
5816
      /* No binding label, but if symbol isn't null, we
5817
         can set the label for it here.
5818
         If name="" or allow_binding_name is false, no C binding name is
5819
         created. */
5820
      if (sym != NULL && sym->name != NULL && has_name_equals == 0)
5821
        sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
5822
    }
5823
 
5824
  if (has_name_equals && gfc_current_state () == COMP_INTERFACE
5825
      && current_interface.type == INTERFACE_ABSTRACT)
5826
    {
5827
      gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5828
      return MATCH_ERROR;
5829
    }
5830
 
5831
  return MATCH_YES;
5832
}
5833
 
5834
 
5835
/* Return nonzero if we're currently compiling a contained procedure.  */
5836
 
5837
static int
5838
contained_procedure (void)
5839
{
5840
  gfc_state_data *s = gfc_state_stack;
5841
 
5842
  if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
5843
      && s->previous != NULL && s->previous->state == COMP_CONTAINS)
5844
    return 1;
5845
 
5846
  return 0;
5847
}
5848
 
5849
/* Set the kind of each enumerator.  The kind is selected such that it is
5850
   interoperable with the corresponding C enumeration type, making
5851
   sure that -fshort-enums is honored.  */
5852
 
5853
static void
5854
set_enum_kind(void)
5855
{
5856
  enumerator_history *current_history = NULL;
5857
  int kind;
5858
  int i;
5859
 
5860
  if (max_enum == NULL || enum_history == NULL)
5861
    return;
5862
 
5863
  if (!flag_short_enums)
5864
    return;
5865
 
5866
  i = 0;
5867
  do
5868
    {
5869
      kind = gfc_integer_kinds[i++].kind;
5870
    }
5871
  while (kind < gfc_c_int_kind
5872
         && gfc_check_integer_range (max_enum->initializer->value.integer,
5873
                                     kind) != ARITH_OK);
5874
 
5875
  current_history = enum_history;
5876
  while (current_history != NULL)
5877
    {
5878
      current_history->sym->ts.kind = kind;
5879
      current_history = current_history->next;
5880
    }
5881
}
5882
 
5883
 
5884
/* Match any of the various end-block statements.  Returns the type of
5885
   END to the caller.  The END INTERFACE, END IF, END DO, END SELECT
5886
   and END BLOCK statements cannot be replaced by a single END statement.  */
5887
 
5888
match
5889
gfc_match_end (gfc_statement *st)
5890
{
5891
  char name[GFC_MAX_SYMBOL_LEN + 1];
5892
  gfc_compile_state state;
5893
  locus old_loc;
5894
  const char *block_name;
5895
  const char *target;
5896
  int eos_ok;
5897
  match m;
5898
 
5899
  old_loc = gfc_current_locus;
5900
  if (gfc_match ("end") != MATCH_YES)
5901
    return MATCH_NO;
5902
 
5903
  state = gfc_current_state ();
5904
  block_name = gfc_current_block () == NULL
5905
             ? NULL : gfc_current_block ()->name;
5906
 
5907
  switch (state)
5908
    {
5909
    case COMP_ASSOCIATE:
5910
    case COMP_BLOCK:
5911
      if (!strncmp (block_name, "block@", strlen("block@")))
5912
        block_name = NULL;
5913
      break;
5914
 
5915
    case COMP_CONTAINS:
5916
    case COMP_DERIVED_CONTAINS:
5917
      state = gfc_state_stack->previous->state;
5918
      block_name = gfc_state_stack->previous->sym == NULL
5919
                 ? NULL : gfc_state_stack->previous->sym->name;
5920
      break;
5921
 
5922
    default:
5923
      break;
5924
    }
5925
 
5926
  switch (state)
5927
    {
5928
    case COMP_NONE:
5929
    case COMP_PROGRAM:
5930
      *st = ST_END_PROGRAM;
5931
      target = " program";
5932
      eos_ok = 1;
5933
      break;
5934
 
5935
    case COMP_SUBROUTINE:
5936
      *st = ST_END_SUBROUTINE;
5937
      target = " subroutine";
5938
      eos_ok = !contained_procedure ();
5939
      break;
5940
 
5941
    case COMP_FUNCTION:
5942
      *st = ST_END_FUNCTION;
5943
      target = " function";
5944
      eos_ok = !contained_procedure ();
5945
      break;
5946
 
5947
    case COMP_BLOCK_DATA:
5948
      *st = ST_END_BLOCK_DATA;
5949
      target = " block data";
5950
      eos_ok = 1;
5951
      break;
5952
 
5953
    case COMP_MODULE:
5954
      *st = ST_END_MODULE;
5955
      target = " module";
5956
      eos_ok = 1;
5957
      break;
5958
 
5959
    case COMP_INTERFACE:
5960
      *st = ST_END_INTERFACE;
5961
      target = " interface";
5962
      eos_ok = 0;
5963
      break;
5964
 
5965
    case COMP_DERIVED:
5966
    case COMP_DERIVED_CONTAINS:
5967
      *st = ST_END_TYPE;
5968
      target = " type";
5969
      eos_ok = 0;
5970
      break;
5971
 
5972
    case COMP_ASSOCIATE:
5973
      *st = ST_END_ASSOCIATE;
5974
      target = " associate";
5975
      eos_ok = 0;
5976
      break;
5977
 
5978
    case COMP_BLOCK:
5979
      *st = ST_END_BLOCK;
5980
      target = " block";
5981
      eos_ok = 0;
5982
      break;
5983
 
5984
    case COMP_IF:
5985
      *st = ST_ENDIF;
5986
      target = " if";
5987
      eos_ok = 0;
5988
      break;
5989
 
5990
    case COMP_DO:
5991
    case COMP_DO_CONCURRENT:
5992
      *st = ST_ENDDO;
5993
      target = " do";
5994
      eos_ok = 0;
5995
      break;
5996
 
5997
    case COMP_CRITICAL:
5998
      *st = ST_END_CRITICAL;
5999
      target = " critical";
6000
      eos_ok = 0;
6001
      break;
6002
 
6003
    case COMP_SELECT:
6004
    case COMP_SELECT_TYPE:
6005
      *st = ST_END_SELECT;
6006
      target = " select";
6007
      eos_ok = 0;
6008
      break;
6009
 
6010
    case COMP_FORALL:
6011
      *st = ST_END_FORALL;
6012
      target = " forall";
6013
      eos_ok = 0;
6014
      break;
6015
 
6016
    case COMP_WHERE:
6017
      *st = ST_END_WHERE;
6018
      target = " where";
6019
      eos_ok = 0;
6020
      break;
6021
 
6022
    case COMP_ENUM:
6023
      *st = ST_END_ENUM;
6024
      target = " enum";
6025
      eos_ok = 0;
6026
      last_initializer = NULL;
6027
      set_enum_kind ();
6028
      gfc_free_enum_history ();
6029
      break;
6030
 
6031
    default:
6032
      gfc_error ("Unexpected END statement at %C");
6033
      goto cleanup;
6034
    }
6035
 
6036
  if (gfc_match_eos () == MATCH_YES)
6037
    {
6038
      if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
6039
        {
6040
          if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: END statement "
6041
                              "instead of %s statement at %L",
6042
                              gfc_ascii_statement (*st), &old_loc) == FAILURE)
6043
            goto cleanup;
6044
        }
6045
      else if (!eos_ok)
6046
        {
6047
          /* We would have required END [something].  */
6048
          gfc_error ("%s statement expected at %L",
6049
                     gfc_ascii_statement (*st), &old_loc);
6050
          goto cleanup;
6051
        }
6052
 
6053
      return MATCH_YES;
6054
    }
6055
 
6056
  /* Verify that we've got the sort of end-block that we're expecting.  */
6057
  if (gfc_match (target) != MATCH_YES)
6058
    {
6059
      gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
6060
      goto cleanup;
6061
    }
6062
 
6063
  /* If we're at the end, make sure a block name wasn't required.  */
6064
  if (gfc_match_eos () == MATCH_YES)
6065
    {
6066
 
6067
      if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
6068
          && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
6069
          && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
6070
        return MATCH_YES;
6071
 
6072
      if (!block_name)
6073
        return MATCH_YES;
6074
 
6075
      gfc_error ("Expected block name of '%s' in %s statement at %C",
6076
                 block_name, gfc_ascii_statement (*st));
6077
 
6078
      return MATCH_ERROR;
6079
    }
6080
 
6081
  /* END INTERFACE has a special handler for its several possible endings.  */
6082
  if (*st == ST_END_INTERFACE)
6083
    return gfc_match_end_interface ();
6084
 
6085
  /* We haven't hit the end of statement, so what is left must be an
6086
     end-name.  */
6087
  m = gfc_match_space ();
6088
  if (m == MATCH_YES)
6089
    m = gfc_match_name (name);
6090
 
6091
  if (m == MATCH_NO)
6092
    gfc_error ("Expected terminating name at %C");
6093
  if (m != MATCH_YES)
6094
    goto cleanup;
6095
 
6096
  if (block_name == NULL)
6097
    goto syntax;
6098
 
6099
  if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
6100
    {
6101
      gfc_error ("Expected label '%s' for %s statement at %C", block_name,
6102
                 gfc_ascii_statement (*st));
6103
      goto cleanup;
6104
    }
6105
  /* Procedure pointer as function result.  */
6106
  else if (strcmp (block_name, "ppr@") == 0
6107
           && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
6108
    {
6109
      gfc_error ("Expected label '%s' for %s statement at %C",
6110
                 gfc_current_block ()->ns->proc_name->name,
6111
                 gfc_ascii_statement (*st));
6112
      goto cleanup;
6113
    }
6114
 
6115
  if (gfc_match_eos () == MATCH_YES)
6116
    return MATCH_YES;
6117
 
6118
syntax:
6119
  gfc_syntax_error (*st);
6120
 
6121
cleanup:
6122
  gfc_current_locus = old_loc;
6123
  return MATCH_ERROR;
6124
}
6125
 
6126
 
6127
 
6128
/***************** Attribute declaration statements ****************/
6129
 
6130
/* Set the attribute of a single variable.  */
6131
 
6132
static match
6133
attr_decl1 (void)
6134
{
6135
  char name[GFC_MAX_SYMBOL_LEN + 1];
6136
  gfc_array_spec *as;
6137
  gfc_symbol *sym;
6138
  locus var_locus;
6139
  match m;
6140
 
6141
  as = NULL;
6142
 
6143
  m = gfc_match_name (name);
6144
  if (m != MATCH_YES)
6145
    goto cleanup;
6146
 
6147
  if (find_special (name, &sym, false))
6148
    return MATCH_ERROR;
6149
 
6150
  if (check_function_name (name) == FAILURE)
6151
    {
6152
      m = MATCH_ERROR;
6153
      goto cleanup;
6154
    }
6155
 
6156
  var_locus = gfc_current_locus;
6157
 
6158
  /* Deal with possible array specification for certain attributes.  */
6159
  if (current_attr.dimension
6160
      || current_attr.codimension
6161
      || current_attr.allocatable
6162
      || current_attr.pointer
6163
      || current_attr.target)
6164
    {
6165
      m = gfc_match_array_spec (&as, !current_attr.codimension,
6166
                                !current_attr.dimension
6167
                                && !current_attr.pointer
6168
                                && !current_attr.target);
6169
      if (m == MATCH_ERROR)
6170
        goto cleanup;
6171
 
6172
      if (current_attr.dimension && m == MATCH_NO)
6173
        {
6174
          gfc_error ("Missing array specification at %L in DIMENSION "
6175
                     "statement", &var_locus);
6176
          m = MATCH_ERROR;
6177
          goto cleanup;
6178
        }
6179
 
6180
      if (current_attr.dimension && sym->value)
6181
        {
6182
          gfc_error ("Dimensions specified for %s at %L after its "
6183
                     "initialisation", sym->name, &var_locus);
6184
          m = MATCH_ERROR;
6185
          goto cleanup;
6186
        }
6187
 
6188
      if (current_attr.codimension && m == MATCH_NO)
6189
        {
6190
          gfc_error ("Missing array specification at %L in CODIMENSION "
6191
                     "statement", &var_locus);
6192
          m = MATCH_ERROR;
6193
          goto cleanup;
6194
        }
6195
 
6196
      if ((current_attr.allocatable || current_attr.pointer)
6197
          && (m == MATCH_YES) && (as->type != AS_DEFERRED))
6198
        {
6199
          gfc_error ("Array specification must be deferred at %L", &var_locus);
6200
          m = MATCH_ERROR;
6201
          goto cleanup;
6202
        }
6203
    }
6204
 
6205
  /* Update symbol table.  DIMENSION attribute is set in
6206
     gfc_set_array_spec().  For CLASS variables, this must be applied
6207
     to the first component, or '_data' field.  */
6208
  if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
6209
    {
6210
      if (gfc_copy_attr (&CLASS_DATA (sym)->attr, &current_attr, &var_locus)
6211
          == FAILURE)
6212
        {
6213
          m = MATCH_ERROR;
6214
          goto cleanup;
6215
        }
6216
    }
6217
  else
6218
    {
6219
      if (current_attr.dimension == 0 && current_attr.codimension == 0
6220
          && gfc_copy_attr (&sym->attr, &current_attr, &var_locus) == FAILURE)
6221
        {
6222
          m = MATCH_ERROR;
6223
          goto cleanup;
6224
        }
6225
    }
6226
 
6227
  if (sym->ts.type == BT_CLASS
6228
      && gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false) == FAILURE)
6229
    {
6230
      m = MATCH_ERROR;
6231
      goto cleanup;
6232
    }
6233
 
6234
  if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
6235
    {
6236
      m = MATCH_ERROR;
6237
      goto cleanup;
6238
    }
6239
 
6240
  if (sym->attr.cray_pointee && sym->as != NULL)
6241
    {
6242
      /* Fix the array spec.  */
6243
      m = gfc_mod_pointee_as (sym->as);
6244
      if (m == MATCH_ERROR)
6245
        goto cleanup;
6246
    }
6247
 
6248
  if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
6249
    {
6250
      m = MATCH_ERROR;
6251
      goto cleanup;
6252
    }
6253
 
6254
  if ((current_attr.external || current_attr.intrinsic)
6255
      && sym->attr.flavor != FL_PROCEDURE
6256
      && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
6257
    {
6258
      m = MATCH_ERROR;
6259
      goto cleanup;
6260
    }
6261
 
6262
  add_hidden_procptr_result (sym);
6263
 
6264
  return MATCH_YES;
6265
 
6266
cleanup:
6267
  gfc_free_array_spec (as);
6268
  return m;
6269
}
6270
 
6271
 
6272
/* Generic attribute declaration subroutine.  Used for attributes that
6273
   just have a list of names.  */
6274
 
6275
static match
6276
attr_decl (void)
6277
{
6278
  match m;
6279
 
6280
  /* Gobble the optional double colon, by simply ignoring the result
6281
     of gfc_match().  */
6282
  gfc_match (" ::");
6283
 
6284
  for (;;)
6285
    {
6286
      m = attr_decl1 ();
6287
      if (m != MATCH_YES)
6288
        break;
6289
 
6290
      if (gfc_match_eos () == MATCH_YES)
6291
        {
6292
          m = MATCH_YES;
6293
          break;
6294
        }
6295
 
6296
      if (gfc_match_char (',') != MATCH_YES)
6297
        {
6298
          gfc_error ("Unexpected character in variable list at %C");
6299
          m = MATCH_ERROR;
6300
          break;
6301
        }
6302
    }
6303
 
6304
  return m;
6305
}
6306
 
6307
 
6308
/* This routine matches Cray Pointer declarations of the form:
6309
   pointer ( <pointer>, <pointee> )
6310
   or
6311
   pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6312
   The pointer, if already declared, should be an integer.  Otherwise, we
6313
   set it as BT_INTEGER with kind gfc_index_integer_kind.  The pointee may
6314
   be either a scalar, or an array declaration.  No space is allocated for
6315
   the pointee.  For the statement
6316
   pointer (ipt, ar(10))
6317
   any subsequent uses of ar will be translated (in C-notation) as
6318
   ar(i) => ((<type> *) ipt)(i)
6319
   After gimplification, pointee variable will disappear in the code.  */
6320
 
6321
static match
6322
cray_pointer_decl (void)
6323
{
6324
  match m;
6325
  gfc_array_spec *as = NULL;
6326
  gfc_symbol *cptr; /* Pointer symbol.  */
6327
  gfc_symbol *cpte; /* Pointee symbol.  */
6328
  locus var_locus;
6329
  bool done = false;
6330
 
6331
  while (!done)
6332
    {
6333
      if (gfc_match_char ('(') != MATCH_YES)
6334
        {
6335
          gfc_error ("Expected '(' at %C");
6336
          return MATCH_ERROR;
6337
        }
6338
 
6339
      /* Match pointer.  */
6340
      var_locus = gfc_current_locus;
6341
      gfc_clear_attr (&current_attr);
6342
      gfc_add_cray_pointer (&current_attr, &var_locus);
6343
      current_ts.type = BT_INTEGER;
6344
      current_ts.kind = gfc_index_integer_kind;
6345
 
6346
      m = gfc_match_symbol (&cptr, 0);
6347
      if (m != MATCH_YES)
6348
        {
6349
          gfc_error ("Expected variable name at %C");
6350
          return m;
6351
        }
6352
 
6353
      if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
6354
        return MATCH_ERROR;
6355
 
6356
      gfc_set_sym_referenced (cptr);
6357
 
6358
      if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary.  */
6359
        {
6360
          cptr->ts.type = BT_INTEGER;
6361
          cptr->ts.kind = gfc_index_integer_kind;
6362
        }
6363
      else if (cptr->ts.type != BT_INTEGER)
6364
        {
6365
          gfc_error ("Cray pointer at %C must be an integer");
6366
          return MATCH_ERROR;
6367
        }
6368
      else if (cptr->ts.kind < gfc_index_integer_kind)
6369
        gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6370
                     " memory addresses require %d bytes",
6371
                     cptr->ts.kind, gfc_index_integer_kind);
6372
 
6373
      if (gfc_match_char (',') != MATCH_YES)
6374
        {
6375
          gfc_error ("Expected \",\" at %C");
6376
          return MATCH_ERROR;
6377
        }
6378
 
6379
      /* Match Pointee.  */
6380
      var_locus = gfc_current_locus;
6381
      gfc_clear_attr (&current_attr);
6382
      gfc_add_cray_pointee (&current_attr, &var_locus);
6383
      current_ts.type = BT_UNKNOWN;
6384
      current_ts.kind = 0;
6385
 
6386
      m = gfc_match_symbol (&cpte, 0);
6387
      if (m != MATCH_YES)
6388
        {
6389
          gfc_error ("Expected variable name at %C");
6390
          return m;
6391
        }
6392
 
6393
      /* Check for an optional array spec.  */
6394
      m = gfc_match_array_spec (&as, true, false);
6395
      if (m == MATCH_ERROR)
6396
        {
6397
          gfc_free_array_spec (as);
6398
          return m;
6399
        }
6400
      else if (m == MATCH_NO)
6401
        {
6402
          gfc_free_array_spec (as);
6403
          as = NULL;
6404
        }
6405
 
6406
      if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
6407
        return MATCH_ERROR;
6408
 
6409
      gfc_set_sym_referenced (cpte);
6410
 
6411
      if (cpte->as == NULL)
6412
        {
6413
          if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
6414
            gfc_internal_error ("Couldn't set Cray pointee array spec.");
6415
        }
6416
      else if (as != NULL)
6417
        {
6418
          gfc_error ("Duplicate array spec for Cray pointee at %C");
6419
          gfc_free_array_spec (as);
6420
          return MATCH_ERROR;
6421
        }
6422
 
6423
      as = NULL;
6424
 
6425
      if (cpte->as != NULL)
6426
        {
6427
          /* Fix array spec.  */
6428
          m = gfc_mod_pointee_as (cpte->as);
6429
          if (m == MATCH_ERROR)
6430
            return m;
6431
        }
6432
 
6433
      /* Point the Pointee at the Pointer.  */
6434
      cpte->cp_pointer = cptr;
6435
 
6436
      if (gfc_match_char (')') != MATCH_YES)
6437
        {
6438
          gfc_error ("Expected \")\" at %C");
6439
          return MATCH_ERROR;
6440
        }
6441
      m = gfc_match_char (',');
6442
      if (m != MATCH_YES)
6443
        done = true; /* Stop searching for more declarations.  */
6444
 
6445
    }
6446
 
6447
  if (m == MATCH_ERROR /* Failed when trying to find ',' above.  */
6448
      || gfc_match_eos () != MATCH_YES)
6449
    {
6450
      gfc_error ("Expected \",\" or end of statement at %C");
6451
      return MATCH_ERROR;
6452
    }
6453
  return MATCH_YES;
6454
}
6455
 
6456
 
6457
match
6458
gfc_match_external (void)
6459
{
6460
 
6461
  gfc_clear_attr (&current_attr);
6462
  current_attr.external = 1;
6463
 
6464
  return attr_decl ();
6465
}
6466
 
6467
 
6468
match
6469
gfc_match_intent (void)
6470
{
6471
  sym_intent intent;
6472
 
6473
  /* This is not allowed within a BLOCK construct!  */
6474
  if (gfc_current_state () == COMP_BLOCK)
6475
    {
6476
      gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6477
      return MATCH_ERROR;
6478
    }
6479
 
6480
  intent = match_intent_spec ();
6481
  if (intent == INTENT_UNKNOWN)
6482
    return MATCH_ERROR;
6483
 
6484
  gfc_clear_attr (&current_attr);
6485
  current_attr.intent = intent;
6486
 
6487
  return attr_decl ();
6488
}
6489
 
6490
 
6491
match
6492
gfc_match_intrinsic (void)
6493
{
6494
 
6495
  gfc_clear_attr (&current_attr);
6496
  current_attr.intrinsic = 1;
6497
 
6498
  return attr_decl ();
6499
}
6500
 
6501
 
6502
match
6503
gfc_match_optional (void)
6504
{
6505
  /* This is not allowed within a BLOCK construct!  */
6506
  if (gfc_current_state () == COMP_BLOCK)
6507
    {
6508
      gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6509
      return MATCH_ERROR;
6510
    }
6511
 
6512
  gfc_clear_attr (&current_attr);
6513
  current_attr.optional = 1;
6514
 
6515
  return attr_decl ();
6516
}
6517
 
6518
 
6519
match
6520
gfc_match_pointer (void)
6521
{
6522
  gfc_gobble_whitespace ();
6523
  if (gfc_peek_ascii_char () == '(')
6524
    {
6525
      if (!gfc_option.flag_cray_pointer)
6526
        {
6527
          gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6528
                     "flag");
6529
          return MATCH_ERROR;
6530
        }
6531
      return cray_pointer_decl ();
6532
    }
6533
  else
6534
    {
6535
      gfc_clear_attr (&current_attr);
6536
      current_attr.pointer = 1;
6537
 
6538
      return attr_decl ();
6539
    }
6540
}
6541
 
6542
 
6543
match
6544
gfc_match_allocatable (void)
6545
{
6546
  gfc_clear_attr (&current_attr);
6547
  current_attr.allocatable = 1;
6548
 
6549
  return attr_decl ();
6550
}
6551
 
6552
 
6553
match
6554
gfc_match_codimension (void)
6555
{
6556
  gfc_clear_attr (&current_attr);
6557
  current_attr.codimension = 1;
6558
 
6559
  return attr_decl ();
6560
}
6561
 
6562
 
6563
match
6564
gfc_match_contiguous (void)
6565
{
6566
  if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: CONTIGUOUS statement at %C")
6567
      == FAILURE)
6568
    return MATCH_ERROR;
6569
 
6570
  gfc_clear_attr (&current_attr);
6571
  current_attr.contiguous = 1;
6572
 
6573
  return attr_decl ();
6574
}
6575
 
6576
 
6577
match
6578
gfc_match_dimension (void)
6579
{
6580
  gfc_clear_attr (&current_attr);
6581
  current_attr.dimension = 1;
6582
 
6583
  return attr_decl ();
6584
}
6585
 
6586
 
6587
match
6588
gfc_match_target (void)
6589
{
6590
  gfc_clear_attr (&current_attr);
6591
  current_attr.target = 1;
6592
 
6593
  return attr_decl ();
6594
}
6595
 
6596
 
6597
/* Match the list of entities being specified in a PUBLIC or PRIVATE
6598
   statement.  */
6599
 
6600
static match
6601
access_attr_decl (gfc_statement st)
6602
{
6603
  char name[GFC_MAX_SYMBOL_LEN + 1];
6604
  interface_type type;
6605
  gfc_user_op *uop;
6606
  gfc_symbol *sym, *dt_sym;
6607
  gfc_intrinsic_op op;
6608
  match m;
6609
 
6610
  if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6611
    goto done;
6612
 
6613
  for (;;)
6614
    {
6615
      m = gfc_match_generic_spec (&type, name, &op);
6616
      if (m == MATCH_NO)
6617
        goto syntax;
6618
      if (m == MATCH_ERROR)
6619
        return MATCH_ERROR;
6620
 
6621
      switch (type)
6622
        {
6623
        case INTERFACE_NAMELESS:
6624
        case INTERFACE_ABSTRACT:
6625
          goto syntax;
6626
 
6627
        case INTERFACE_GENERIC:
6628
          if (gfc_get_symbol (name, NULL, &sym))
6629
            goto done;
6630
 
6631
          if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
6632
                                          ? ACCESS_PUBLIC : ACCESS_PRIVATE,
6633
                              sym->name, NULL) == FAILURE)
6634
            return MATCH_ERROR;
6635
 
6636
          if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
6637
              && gfc_add_access (&dt_sym->attr,
6638
                                 (st == ST_PUBLIC) ? ACCESS_PUBLIC
6639
                                                   : ACCESS_PRIVATE,
6640
                                 sym->name, NULL) == FAILURE)
6641
            return MATCH_ERROR;
6642
 
6643
          break;
6644
 
6645
        case INTERFACE_INTRINSIC_OP:
6646
          if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
6647
            {
6648
              gfc_intrinsic_op other_op;
6649
 
6650
              gfc_current_ns->operator_access[op] =
6651
                (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6652
 
6653
              /* Handle the case if there is another op with the same
6654
                 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on.  */
6655
              other_op = gfc_equivalent_op (op);
6656
 
6657
              if (other_op != INTRINSIC_NONE)
6658
                gfc_current_ns->operator_access[other_op] =
6659
                  (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6660
 
6661
            }
6662
          else
6663
            {
6664
              gfc_error ("Access specification of the %s operator at %C has "
6665
                         "already been specified", gfc_op2string (op));
6666
              goto done;
6667
            }
6668
 
6669
          break;
6670
 
6671
        case INTERFACE_USER_OP:
6672
          uop = gfc_get_uop (name);
6673
 
6674
          if (uop->access == ACCESS_UNKNOWN)
6675
            {
6676
              uop->access = (st == ST_PUBLIC)
6677
                          ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6678
            }
6679
          else
6680
            {
6681
              gfc_error ("Access specification of the .%s. operator at %C "
6682
                         "has already been specified", sym->name);
6683
              goto done;
6684
            }
6685
 
6686
          break;
6687
        }
6688
 
6689
      if (gfc_match_char (',') == MATCH_NO)
6690
        break;
6691
    }
6692
 
6693
  if (gfc_match_eos () != MATCH_YES)
6694
    goto syntax;
6695
  return MATCH_YES;
6696
 
6697
syntax:
6698
  gfc_syntax_error (st);
6699
 
6700
done:
6701
  return MATCH_ERROR;
6702
}
6703
 
6704
 
6705
match
6706
gfc_match_protected (void)
6707
{
6708
  gfc_symbol *sym;
6709
  match m;
6710
 
6711
  if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6712
    {
6713
       gfc_error ("PROTECTED at %C only allowed in specification "
6714
                  "part of a module");
6715
       return MATCH_ERROR;
6716
 
6717
    }
6718
 
6719
  if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
6720
      == FAILURE)
6721
    return MATCH_ERROR;
6722
 
6723
  if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6724
    {
6725
      return MATCH_ERROR;
6726
    }
6727
 
6728
  if (gfc_match_eos () == MATCH_YES)
6729
    goto syntax;
6730
 
6731
  for(;;)
6732
    {
6733
      m = gfc_match_symbol (&sym, 0);
6734
      switch (m)
6735
        {
6736
        case MATCH_YES:
6737
          if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
6738
              == FAILURE)
6739
            return MATCH_ERROR;
6740
          goto next_item;
6741
 
6742
        case MATCH_NO:
6743
          break;
6744
 
6745
        case MATCH_ERROR:
6746
          return MATCH_ERROR;
6747
        }
6748
 
6749
    next_item:
6750
      if (gfc_match_eos () == MATCH_YES)
6751
        break;
6752
      if (gfc_match_char (',') != MATCH_YES)
6753
        goto syntax;
6754
    }
6755
 
6756
  return MATCH_YES;
6757
 
6758
syntax:
6759
  gfc_error ("Syntax error in PROTECTED statement at %C");
6760
  return MATCH_ERROR;
6761
}
6762
 
6763
 
6764
/* The PRIVATE statement is a bit weird in that it can be an attribute
6765
   declaration, but also works as a standalone statement inside of a
6766
   type declaration or a module.  */
6767
 
6768
match
6769
gfc_match_private (gfc_statement *st)
6770
{
6771
 
6772
  if (gfc_match ("private") != MATCH_YES)
6773
    return MATCH_NO;
6774
 
6775
  if (gfc_current_state () != COMP_MODULE
6776
      && !(gfc_current_state () == COMP_DERIVED
6777
           && gfc_state_stack->previous
6778
           && gfc_state_stack->previous->state == COMP_MODULE)
6779
      && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6780
           && gfc_state_stack->previous && gfc_state_stack->previous->previous
6781
           && gfc_state_stack->previous->previous->state == COMP_MODULE))
6782
    {
6783
      gfc_error ("PRIVATE statement at %C is only allowed in the "
6784
                 "specification part of a module");
6785
      return MATCH_ERROR;
6786
    }
6787
 
6788
  if (gfc_current_state () == COMP_DERIVED)
6789
    {
6790
      if (gfc_match_eos () == MATCH_YES)
6791
        {
6792
          *st = ST_PRIVATE;
6793
          return MATCH_YES;
6794
        }
6795
 
6796
      gfc_syntax_error (ST_PRIVATE);
6797
      return MATCH_ERROR;
6798
    }
6799
 
6800
  if (gfc_match_eos () == MATCH_YES)
6801
    {
6802
      *st = ST_PRIVATE;
6803
      return MATCH_YES;
6804
    }
6805
 
6806
  *st = ST_ATTR_DECL;
6807
  return access_attr_decl (ST_PRIVATE);
6808
}
6809
 
6810
 
6811
match
6812
gfc_match_public (gfc_statement *st)
6813
{
6814
 
6815
  if (gfc_match ("public") != MATCH_YES)
6816
    return MATCH_NO;
6817
 
6818
  if (gfc_current_state () != COMP_MODULE)
6819
    {
6820
      gfc_error ("PUBLIC statement at %C is only allowed in the "
6821
                 "specification part of a module");
6822
      return MATCH_ERROR;
6823
    }
6824
 
6825
  if (gfc_match_eos () == MATCH_YES)
6826
    {
6827
      *st = ST_PUBLIC;
6828
      return MATCH_YES;
6829
    }
6830
 
6831
  *st = ST_ATTR_DECL;
6832
  return access_attr_decl (ST_PUBLIC);
6833
}
6834
 
6835
 
6836
/* Workhorse for gfc_match_parameter.  */
6837
 
6838
static match
6839
do_parm (void)
6840
{
6841
  gfc_symbol *sym;
6842
  gfc_expr *init;
6843
  match m;
6844
  gfc_try t;
6845
 
6846
  m = gfc_match_symbol (&sym, 0);
6847
  if (m == MATCH_NO)
6848
    gfc_error ("Expected variable name at %C in PARAMETER statement");
6849
 
6850
  if (m != MATCH_YES)
6851
    return m;
6852
 
6853
  if (gfc_match_char ('=') == MATCH_NO)
6854
    {
6855
      gfc_error ("Expected = sign in PARAMETER statement at %C");
6856
      return MATCH_ERROR;
6857
    }
6858
 
6859
  m = gfc_match_init_expr (&init);
6860
  if (m == MATCH_NO)
6861
    gfc_error ("Expected expression at %C in PARAMETER statement");
6862
  if (m != MATCH_YES)
6863
    return m;
6864
 
6865
  if (sym->ts.type == BT_UNKNOWN
6866
      && gfc_set_default_type (sym, 1, NULL) == FAILURE)
6867
    {
6868
      m = MATCH_ERROR;
6869
      goto cleanup;
6870
    }
6871
 
6872
  if (gfc_check_assign_symbol (sym, init) == FAILURE
6873
      || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
6874
    {
6875
      m = MATCH_ERROR;
6876
      goto cleanup;
6877
    }
6878
 
6879
  if (sym->value)
6880
    {
6881
      gfc_error ("Initializing already initialized variable at %C");
6882
      m = MATCH_ERROR;
6883
      goto cleanup;
6884
    }
6885
 
6886
  t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
6887
  return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6888
 
6889
cleanup:
6890
  gfc_free_expr (init);
6891
  return m;
6892
}
6893
 
6894
 
6895
/* Match a parameter statement, with the weird syntax that these have.  */
6896
 
6897
match
6898
gfc_match_parameter (void)
6899
{
6900
  match m;
6901
 
6902
  if (gfc_match_char ('(') == MATCH_NO)
6903
    return MATCH_NO;
6904
 
6905
  for (;;)
6906
    {
6907
      m = do_parm ();
6908
      if (m != MATCH_YES)
6909
        break;
6910
 
6911
      if (gfc_match (" )%t") == MATCH_YES)
6912
        break;
6913
 
6914
      if (gfc_match_char (',') != MATCH_YES)
6915
        {
6916
          gfc_error ("Unexpected characters in PARAMETER statement at %C");
6917
          m = MATCH_ERROR;
6918
          break;
6919
        }
6920
    }
6921
 
6922
  return m;
6923
}
6924
 
6925
 
6926
/* Save statements have a special syntax.  */
6927
 
6928
match
6929
gfc_match_save (void)
6930
{
6931
  char n[GFC_MAX_SYMBOL_LEN+1];
6932
  gfc_common_head *c;
6933
  gfc_symbol *sym;
6934
  match m;
6935
 
6936
  if (gfc_match_eos () == MATCH_YES)
6937
    {
6938
      if (gfc_current_ns->seen_save)
6939
        {
6940
          if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
6941
                              "follows previous SAVE statement")
6942
              == FAILURE)
6943
            return MATCH_ERROR;
6944
        }
6945
 
6946
      gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
6947
      return MATCH_YES;
6948
    }
6949
 
6950
  if (gfc_current_ns->save_all)
6951
    {
6952
      if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
6953
                          "blanket SAVE statement")
6954
          == FAILURE)
6955
        return MATCH_ERROR;
6956
    }
6957
 
6958
  gfc_match (" ::");
6959
 
6960
  for (;;)
6961
    {
6962
      m = gfc_match_symbol (&sym, 0);
6963
      switch (m)
6964
        {
6965
        case MATCH_YES:
6966
          if (gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
6967
                            &gfc_current_locus) == FAILURE)
6968
            return MATCH_ERROR;
6969
          goto next_item;
6970
 
6971
        case MATCH_NO:
6972
          break;
6973
 
6974
        case MATCH_ERROR:
6975
          return MATCH_ERROR;
6976
        }
6977
 
6978
      m = gfc_match (" / %n /", &n);
6979
      if (m == MATCH_ERROR)
6980
        return MATCH_ERROR;
6981
      if (m == MATCH_NO)
6982
        goto syntax;
6983
 
6984
      c = gfc_get_common (n, 0);
6985
      c->saved = 1;
6986
 
6987
      gfc_current_ns->seen_save = 1;
6988
 
6989
    next_item:
6990
      if (gfc_match_eos () == MATCH_YES)
6991
        break;
6992
      if (gfc_match_char (',') != MATCH_YES)
6993
        goto syntax;
6994
    }
6995
 
6996
  return MATCH_YES;
6997
 
6998
syntax:
6999
  gfc_error ("Syntax error in SAVE statement at %C");
7000
  return MATCH_ERROR;
7001
}
7002
 
7003
 
7004
match
7005
gfc_match_value (void)
7006
{
7007
  gfc_symbol *sym;
7008
  match m;
7009
 
7010
  /* This is not allowed within a BLOCK construct!  */
7011
  if (gfc_current_state () == COMP_BLOCK)
7012
    {
7013
      gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7014
      return MATCH_ERROR;
7015
    }
7016
 
7017
  if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
7018
      == FAILURE)
7019
    return MATCH_ERROR;
7020
 
7021
  if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7022
    {
7023
      return MATCH_ERROR;
7024
    }
7025
 
7026
  if (gfc_match_eos () == MATCH_YES)
7027
    goto syntax;
7028
 
7029
  for(;;)
7030
    {
7031
      m = gfc_match_symbol (&sym, 0);
7032
      switch (m)
7033
        {
7034
        case MATCH_YES:
7035
          if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
7036
              == FAILURE)
7037
            return MATCH_ERROR;
7038
          goto next_item;
7039
 
7040
        case MATCH_NO:
7041
          break;
7042
 
7043
        case MATCH_ERROR:
7044
          return MATCH_ERROR;
7045
        }
7046
 
7047
    next_item:
7048
      if (gfc_match_eos () == MATCH_YES)
7049
        break;
7050
      if (gfc_match_char (',') != MATCH_YES)
7051
        goto syntax;
7052
    }
7053
 
7054
  return MATCH_YES;
7055
 
7056
syntax:
7057
  gfc_error ("Syntax error in VALUE statement at %C");
7058
  return MATCH_ERROR;
7059
}
7060
 
7061
 
7062
match
7063
gfc_match_volatile (void)
7064
{
7065
  gfc_symbol *sym;
7066
  match m;
7067
 
7068
  if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
7069
      == FAILURE)
7070
    return MATCH_ERROR;
7071
 
7072
  if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7073
    {
7074
      return MATCH_ERROR;
7075
    }
7076
 
7077
  if (gfc_match_eos () == MATCH_YES)
7078
    goto syntax;
7079
 
7080
  for(;;)
7081
    {
7082
      /* VOLATILE is special because it can be added to host-associated
7083
         symbols locally. Except for coarrays. */
7084
      m = gfc_match_symbol (&sym, 1);
7085
      switch (m)
7086
        {
7087
        case MATCH_YES:
7088
          /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7089
             for variable in a BLOCK which is defined outside of the BLOCK.  */
7090
          if (sym->ns != gfc_current_ns && sym->attr.codimension)
7091
            {
7092
              gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
7093
                         "%C, which is use-/host-associated", sym->name);
7094
              return MATCH_ERROR;
7095
            }
7096
          if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
7097
              == FAILURE)
7098
            return MATCH_ERROR;
7099
          goto next_item;
7100
 
7101
        case MATCH_NO:
7102
          break;
7103
 
7104
        case MATCH_ERROR:
7105
          return MATCH_ERROR;
7106
        }
7107
 
7108
    next_item:
7109
      if (gfc_match_eos () == MATCH_YES)
7110
        break;
7111
      if (gfc_match_char (',') != MATCH_YES)
7112
        goto syntax;
7113
    }
7114
 
7115
  return MATCH_YES;
7116
 
7117
syntax:
7118
  gfc_error ("Syntax error in VOLATILE statement at %C");
7119
  return MATCH_ERROR;
7120
}
7121
 
7122
 
7123
match
7124
gfc_match_asynchronous (void)
7125
{
7126
  gfc_symbol *sym;
7127
  match m;
7128
 
7129
  if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ASYNCHRONOUS statement at %C")
7130
      == FAILURE)
7131
    return MATCH_ERROR;
7132
 
7133
  if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7134
    {
7135
      return MATCH_ERROR;
7136
    }
7137
 
7138
  if (gfc_match_eos () == MATCH_YES)
7139
    goto syntax;
7140
 
7141
  for(;;)
7142
    {
7143
      /* ASYNCHRONOUS is special because it can be added to host-associated
7144
         symbols locally.  */
7145
      m = gfc_match_symbol (&sym, 1);
7146
      switch (m)
7147
        {
7148
        case MATCH_YES:
7149
          if (gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus)
7150
              == FAILURE)
7151
            return MATCH_ERROR;
7152
          goto next_item;
7153
 
7154
        case MATCH_NO:
7155
          break;
7156
 
7157
        case MATCH_ERROR:
7158
          return MATCH_ERROR;
7159
        }
7160
 
7161
    next_item:
7162
      if (gfc_match_eos () == MATCH_YES)
7163
        break;
7164
      if (gfc_match_char (',') != MATCH_YES)
7165
        goto syntax;
7166
    }
7167
 
7168
  return MATCH_YES;
7169
 
7170
syntax:
7171
  gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7172
  return MATCH_ERROR;
7173
}
7174
 
7175
 
7176
/* Match a module procedure statement.  Note that we have to modify
7177
   symbols in the parent's namespace because the current one was there
7178
   to receive symbols that are in an interface's formal argument list.  */
7179
 
7180
match
7181
gfc_match_modproc (void)
7182
{
7183
  char name[GFC_MAX_SYMBOL_LEN + 1];
7184
  gfc_symbol *sym;
7185
  match m;
7186
  locus old_locus;
7187
  gfc_namespace *module_ns;
7188
  gfc_interface *old_interface_head, *interface;
7189
 
7190
  if (gfc_state_stack->state != COMP_INTERFACE
7191
      || gfc_state_stack->previous == NULL
7192
      || current_interface.type == INTERFACE_NAMELESS
7193
      || current_interface.type == INTERFACE_ABSTRACT)
7194
    {
7195
      gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7196
                 "interface");
7197
      return MATCH_ERROR;
7198
    }
7199
 
7200
  module_ns = gfc_current_ns->parent;
7201
  for (; module_ns; module_ns = module_ns->parent)
7202
    if (module_ns->proc_name->attr.flavor == FL_MODULE
7203
        || module_ns->proc_name->attr.flavor == FL_PROGRAM
7204
        || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
7205
            && !module_ns->proc_name->attr.contained))
7206
      break;
7207
 
7208
  if (module_ns == NULL)
7209
    return MATCH_ERROR;
7210
 
7211
  /* Store the current state of the interface. We will need it if we
7212
     end up with a syntax error and need to recover.  */
7213
  old_interface_head = gfc_current_interface_head ();
7214
 
7215
  /* Check if the F2008 optional double colon appears.  */
7216
  gfc_gobble_whitespace ();
7217
  old_locus = gfc_current_locus;
7218
  if (gfc_match ("::") == MATCH_YES)
7219
    {
7220
      if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: double colon in "
7221
                         "MODULE PROCEDURE statement at %L", &old_locus)
7222
          == FAILURE)
7223
        return MATCH_ERROR;
7224
    }
7225
  else
7226
    gfc_current_locus = old_locus;
7227
 
7228
  for (;;)
7229
    {
7230
      bool last = false;
7231
      old_locus = gfc_current_locus;
7232
 
7233
      m = gfc_match_name (name);
7234
      if (m == MATCH_NO)
7235
        goto syntax;
7236
      if (m != MATCH_YES)
7237
        return MATCH_ERROR;
7238
 
7239
      /* Check for syntax error before starting to add symbols to the
7240
         current namespace.  */
7241
      if (gfc_match_eos () == MATCH_YES)
7242
        last = true;
7243
 
7244
      if (!last && gfc_match_char (',') != MATCH_YES)
7245
        goto syntax;
7246
 
7247
      /* Now we're sure the syntax is valid, we process this item
7248
         further.  */
7249
      if (gfc_get_symbol (name, module_ns, &sym))
7250
        return MATCH_ERROR;
7251
 
7252
      if (sym->attr.intrinsic)
7253
        {
7254
          gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7255
                     "PROCEDURE", &old_locus);
7256
          return MATCH_ERROR;
7257
        }
7258
 
7259
      if (sym->attr.proc != PROC_MODULE
7260
          && gfc_add_procedure (&sym->attr, PROC_MODULE,
7261
                                sym->name, NULL) == FAILURE)
7262
        return MATCH_ERROR;
7263
 
7264
      if (gfc_add_interface (sym) == FAILURE)
7265
        return MATCH_ERROR;
7266
 
7267
      sym->attr.mod_proc = 1;
7268
      sym->declared_at = old_locus;
7269
 
7270
      if (last)
7271
        break;
7272
    }
7273
 
7274
  return MATCH_YES;
7275
 
7276
syntax:
7277
  /* Restore the previous state of the interface.  */
7278
  interface = gfc_current_interface_head ();
7279
  gfc_set_current_interface_head (old_interface_head);
7280
 
7281
  /* Free the new interfaces.  */
7282
  while (interface != old_interface_head)
7283
  {
7284
    gfc_interface *i = interface->next;
7285
    free (interface);
7286
    interface = i;
7287
  }
7288
 
7289
  /* And issue a syntax error.  */
7290
  gfc_syntax_error (ST_MODULE_PROC);
7291
  return MATCH_ERROR;
7292
}
7293
 
7294
 
7295
/* Check a derived type that is being extended.  */
7296
static gfc_symbol*
7297
check_extended_derived_type (char *name)
7298
{
7299
  gfc_symbol *extended;
7300
 
7301
  if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
7302
    {
7303
      gfc_error ("Ambiguous symbol in TYPE definition at %C");
7304
      return NULL;
7305
    }
7306
 
7307
  if (!extended)
7308
    {
7309
      gfc_error ("No such symbol in TYPE definition at %C");
7310
      return NULL;
7311
    }
7312
 
7313
  extended = gfc_find_dt_in_generic (extended);
7314
 
7315
  if (extended->attr.flavor != FL_DERIVED)
7316
    {
7317
      gfc_error ("'%s' in EXTENDS expression at %C is not a "
7318
                 "derived type", name);
7319
      return NULL;
7320
    }
7321
 
7322
  if (extended->attr.is_bind_c)
7323
    {
7324
      gfc_error ("'%s' cannot be extended at %C because it "
7325
                 "is BIND(C)", extended->name);
7326
      return NULL;
7327
    }
7328
 
7329
  if (extended->attr.sequence)
7330
    {
7331
      gfc_error ("'%s' cannot be extended at %C because it "
7332
                 "is a SEQUENCE type", extended->name);
7333
      return NULL;
7334
    }
7335
 
7336
  return extended;
7337
}
7338
 
7339
 
7340
/* Match the optional attribute specifiers for a type declaration.
7341
   Return MATCH_ERROR if an error is encountered in one of the handled
7342
   attributes (public, private, bind(c)), MATCH_NO if what's found is
7343
   not a handled attribute, and MATCH_YES otherwise.  TODO: More error
7344
   checking on attribute conflicts needs to be done.  */
7345
 
7346
match
7347
gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
7348
{
7349
  /* See if the derived type is marked as private.  */
7350
  if (gfc_match (" , private") == MATCH_YES)
7351
    {
7352
      if (gfc_current_state () != COMP_MODULE)
7353
        {
7354
          gfc_error ("Derived type at %C can only be PRIVATE in the "
7355
                     "specification part of a module");
7356
          return MATCH_ERROR;
7357
        }
7358
 
7359
      if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
7360
        return MATCH_ERROR;
7361
    }
7362
  else if (gfc_match (" , public") == MATCH_YES)
7363
    {
7364
      if (gfc_current_state () != COMP_MODULE)
7365
        {
7366
          gfc_error ("Derived type at %C can only be PUBLIC in the "
7367
                     "specification part of a module");
7368
          return MATCH_ERROR;
7369
        }
7370
 
7371
      if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
7372
        return MATCH_ERROR;
7373
    }
7374
  else if (gfc_match (" , bind ( c )") == MATCH_YES)
7375
    {
7376
      /* If the type is defined to be bind(c) it then needs to make
7377
         sure that all fields are interoperable.  This will
7378
         need to be a semantic check on the finished derived type.
7379
         See 15.2.3 (lines 9-12) of F2003 draft.  */
7380
      if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
7381
        return MATCH_ERROR;
7382
 
7383
      /* TODO: attr conflicts need to be checked, probably in symbol.c.  */
7384
    }
7385
  else if (gfc_match (" , abstract") == MATCH_YES)
7386
    {
7387
      if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ABSTRACT type at %C")
7388
            == FAILURE)
7389
        return MATCH_ERROR;
7390
 
7391
      if (gfc_add_abstract (attr, &gfc_current_locus) == FAILURE)
7392
        return MATCH_ERROR;
7393
    }
7394
  else if (name && gfc_match(" , extends ( %n )", name) == MATCH_YES)
7395
    {
7396
      if (gfc_add_extension (attr, &gfc_current_locus) == FAILURE)
7397
        return MATCH_ERROR;
7398
    }
7399
  else
7400
    return MATCH_NO;
7401
 
7402
  /* If we get here, something matched.  */
7403
  return MATCH_YES;
7404
}
7405
 
7406
 
7407
/* Match the beginning of a derived type declaration.  If a type name
7408
   was the result of a function, then it is possible to have a symbol
7409
   already to be known as a derived type yet have no components.  */
7410
 
7411
match
7412
gfc_match_derived_decl (void)
7413
{
7414
  char name[GFC_MAX_SYMBOL_LEN + 1];
7415
  char parent[GFC_MAX_SYMBOL_LEN + 1];
7416
  symbol_attribute attr;
7417
  gfc_symbol *sym, *gensym;
7418
  gfc_symbol *extended;
7419
  match m;
7420
  match is_type_attr_spec = MATCH_NO;
7421
  bool seen_attr = false;
7422
  gfc_interface *intr = NULL, *head;
7423
 
7424
  if (gfc_current_state () == COMP_DERIVED)
7425
    return MATCH_NO;
7426
 
7427
  name[0] = '\0';
7428
  parent[0] = '\0';
7429
  gfc_clear_attr (&attr);
7430
  extended = NULL;
7431
 
7432
  do
7433
    {
7434
      is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
7435
      if (is_type_attr_spec == MATCH_ERROR)
7436
        return MATCH_ERROR;
7437
      if (is_type_attr_spec == MATCH_YES)
7438
        seen_attr = true;
7439
    } while (is_type_attr_spec == MATCH_YES);
7440
 
7441
  /* Deal with derived type extensions.  The extension attribute has
7442
     been added to 'attr' but now the parent type must be found and
7443
     checked.  */
7444
  if (parent[0])
7445
    extended = check_extended_derived_type (parent);
7446
 
7447
  if (parent[0] && !extended)
7448
    return MATCH_ERROR;
7449
 
7450
  if (gfc_match (" ::") != MATCH_YES && seen_attr)
7451
    {
7452
      gfc_error ("Expected :: in TYPE definition at %C");
7453
      return MATCH_ERROR;
7454
    }
7455
 
7456
  m = gfc_match (" %n%t", name);
7457
  if (m != MATCH_YES)
7458
    return m;
7459
 
7460
  /* Make sure the name is not the name of an intrinsic type.  */
7461
  if (gfc_is_intrinsic_typename (name))
7462
    {
7463
      gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7464
                 "type", name);
7465
      return MATCH_ERROR;
7466
    }
7467
 
7468
  if (gfc_get_symbol (name, NULL, &gensym))
7469
    return MATCH_ERROR;
7470
 
7471
  if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
7472
    {
7473
      gfc_error ("Derived type name '%s' at %C already has a basic type "
7474
                 "of %s", gensym->name, gfc_typename (&gensym->ts));
7475
      return MATCH_ERROR;
7476
    }
7477
 
7478
  if (!gensym->attr.generic
7479
      && gfc_add_generic (&gensym->attr, gensym->name, NULL) == FAILURE)
7480
    return MATCH_ERROR;
7481
 
7482
  if (!gensym->attr.function
7483
      && gfc_add_function (&gensym->attr, gensym->name, NULL) == FAILURE)
7484
    return MATCH_ERROR;
7485
 
7486
  sym = gfc_find_dt_in_generic (gensym);
7487
 
7488
  if (sym && (sym->components != NULL || sym->attr.zero_comp))
7489
    {
7490
      gfc_error ("Derived type definition of '%s' at %C has already been "
7491
                 "defined", sym->name);
7492
      return MATCH_ERROR;
7493
    }
7494
 
7495
  if (!sym)
7496
    {
7497
      /* Use upper case to save the actual derived-type symbol.  */
7498
      gfc_get_symbol (gfc_get_string ("%c%s",
7499
                        (char) TOUPPER ((unsigned char) gensym->name[0]),
7500
                        &gensym->name[1]), NULL, &sym);
7501
      sym->name = gfc_get_string (gensym->name);
7502
      head = gensym->generic;
7503
      intr = gfc_get_interface ();
7504
      intr->sym = sym;
7505
      intr->where = gfc_current_locus;
7506
      intr->sym->declared_at = gfc_current_locus;
7507
      intr->next = head;
7508
      gensym->generic = intr;
7509
      gensym->attr.if_source = IFSRC_DECL;
7510
    }
7511
 
7512
  /* The symbol may already have the derived attribute without the
7513
     components.  The ways this can happen is via a function
7514
     definition, an INTRINSIC statement or a subtype in another
7515
     derived type that is a pointer.  The first part of the AND clause
7516
     is true if the symbol is not the return value of a function.  */
7517
  if (sym->attr.flavor != FL_DERIVED
7518
      && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
7519
    return MATCH_ERROR;
7520
 
7521
  if (attr.access != ACCESS_UNKNOWN
7522
      && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
7523
    return MATCH_ERROR;
7524
  else if (sym->attr.access == ACCESS_UNKNOWN
7525
           && gensym->attr.access != ACCESS_UNKNOWN
7526
           && gfc_add_access (&sym->attr, gensym->attr.access, sym->name, NULL)
7527
              == FAILURE)
7528
    return MATCH_ERROR;
7529
 
7530
  if (sym->attr.access != ACCESS_UNKNOWN
7531
      && gensym->attr.access == ACCESS_UNKNOWN)
7532
    gensym->attr.access = sym->attr.access;
7533
 
7534
  /* See if the derived type was labeled as bind(c).  */
7535
  if (attr.is_bind_c != 0)
7536
    sym->attr.is_bind_c = attr.is_bind_c;
7537
 
7538
  /* Construct the f2k_derived namespace if it is not yet there.  */
7539
  if (!sym->f2k_derived)
7540
    sym->f2k_derived = gfc_get_namespace (NULL, 0);
7541
 
7542
  if (extended && !sym->components)
7543
    {
7544
      gfc_component *p;
7545
      gfc_symtree *st;
7546
 
7547
      /* Add the extended derived type as the first component.  */
7548
      gfc_add_component (sym, parent, &p);
7549
      extended->refs++;
7550
      gfc_set_sym_referenced (extended);
7551
 
7552
      p->ts.type = BT_DERIVED;
7553
      p->ts.u.derived = extended;
7554
      p->initializer = gfc_default_initializer (&p->ts);
7555
 
7556
      /* Set extension level.  */
7557
      if (extended->attr.extension == 255)
7558
        {
7559
          /* Since the extension field is 8 bit wide, we can only have
7560
             up to 255 extension levels.  */
7561
          gfc_error ("Maximum extension level reached with type '%s' at %L",
7562
                     extended->name, &extended->declared_at);
7563
          return MATCH_ERROR;
7564
        }
7565
      sym->attr.extension = extended->attr.extension + 1;
7566
 
7567
      /* Provide the links between the extended type and its extension.  */
7568
      if (!extended->f2k_derived)
7569
        extended->f2k_derived = gfc_get_namespace (NULL, 0);
7570
      st = gfc_new_symtree (&extended->f2k_derived->sym_root, sym->name);
7571
      st->n.sym = sym;
7572
    }
7573
 
7574
  if (!sym->hash_value)
7575
    /* Set the hash for the compound name for this type.  */
7576
    sym->hash_value = gfc_hash_value (sym);
7577
 
7578
  /* Take over the ABSTRACT attribute.  */
7579
  sym->attr.abstract = attr.abstract;
7580
 
7581
  gfc_new_block = sym;
7582
 
7583
  return MATCH_YES;
7584
}
7585
 
7586
 
7587
/* Cray Pointees can be declared as:
7588
      pointer (ipt, a (n,m,...,*))  */
7589
 
7590
match
7591
gfc_mod_pointee_as (gfc_array_spec *as)
7592
{
7593
  as->cray_pointee = true; /* This will be useful to know later.  */
7594
  if (as->type == AS_ASSUMED_SIZE)
7595
    as->cp_was_assumed = true;
7596
  else if (as->type == AS_ASSUMED_SHAPE)
7597
    {
7598
      gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7599
      return MATCH_ERROR;
7600
    }
7601
  return MATCH_YES;
7602
}
7603
 
7604
 
7605
/* Match the enum definition statement, here we are trying to match
7606
   the first line of enum definition statement.
7607
   Returns MATCH_YES if match is found.  */
7608
 
7609
match
7610
gfc_match_enum (void)
7611
{
7612
  match m;
7613
 
7614
  m = gfc_match_eos ();
7615
  if (m != MATCH_YES)
7616
    return m;
7617
 
7618
  if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
7619
      == FAILURE)
7620
    return MATCH_ERROR;
7621
 
7622
  return MATCH_YES;
7623
}
7624
 
7625
 
7626
/* Returns an initializer whose value is one higher than the value of the
7627
   LAST_INITIALIZER argument.  If the argument is NULL, the
7628
   initializers value will be set to zero.  The initializer's kind
7629
   will be set to gfc_c_int_kind.
7630
 
7631
   If -fshort-enums is given, the appropriate kind will be selected
7632
   later after all enumerators have been parsed.  A warning is issued
7633
   here if an initializer exceeds gfc_c_int_kind.  */
7634
 
7635
static gfc_expr *
7636
enum_initializer (gfc_expr *last_initializer, locus where)
7637
{
7638
  gfc_expr *result;
7639
  result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
7640
 
7641
  mpz_init (result->value.integer);
7642
 
7643
  if (last_initializer != NULL)
7644
    {
7645
      mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
7646
      result->where = last_initializer->where;
7647
 
7648
      if (gfc_check_integer_range (result->value.integer,
7649
             gfc_c_int_kind) != ARITH_OK)
7650
        {
7651
          gfc_error ("Enumerator exceeds the C integer type at %C");
7652
          return NULL;
7653
        }
7654
    }
7655
  else
7656
    {
7657
      /* Control comes here, if it's the very first enumerator and no
7658
         initializer has been given.  It will be initialized to zero.  */
7659
      mpz_set_si (result->value.integer, 0);
7660
    }
7661
 
7662
  return result;
7663
}
7664
 
7665
 
7666
/* Match a variable name with an optional initializer.  When this
7667
   subroutine is called, a variable is expected to be parsed next.
7668
   Depending on what is happening at the moment, updates either the
7669
   symbol table or the current interface.  */
7670
 
7671
static match
7672
enumerator_decl (void)
7673
{
7674
  char name[GFC_MAX_SYMBOL_LEN + 1];
7675
  gfc_expr *initializer;
7676
  gfc_array_spec *as = NULL;
7677
  gfc_symbol *sym;
7678
  locus var_locus;
7679
  match m;
7680
  gfc_try t;
7681
  locus old_locus;
7682
 
7683
  initializer = NULL;
7684
  old_locus = gfc_current_locus;
7685
 
7686
  /* When we get here, we've just matched a list of attributes and
7687
     maybe a type and a double colon.  The next thing we expect to see
7688
     is the name of the symbol.  */
7689
  m = gfc_match_name (name);
7690
  if (m != MATCH_YES)
7691
    goto cleanup;
7692
 
7693
  var_locus = gfc_current_locus;
7694
 
7695
  /* OK, we've successfully matched the declaration.  Now put the
7696
     symbol in the current namespace. If we fail to create the symbol,
7697
     bail out.  */
7698
  if (build_sym (name, NULL, false, &as, &var_locus) == FAILURE)
7699
    {
7700
      m = MATCH_ERROR;
7701
      goto cleanup;
7702
    }
7703
 
7704
  /* The double colon must be present in order to have initializers.
7705
     Otherwise the statement is ambiguous with an assignment statement.  */
7706
  if (colon_seen)
7707
    {
7708
      if (gfc_match_char ('=') == MATCH_YES)
7709
        {
7710
          m = gfc_match_init_expr (&initializer);
7711
          if (m == MATCH_NO)
7712
            {
7713
              gfc_error ("Expected an initialization expression at %C");
7714
              m = MATCH_ERROR;
7715
            }
7716
 
7717
          if (m != MATCH_YES)
7718
            goto cleanup;
7719
        }
7720
    }
7721
 
7722
  /* If we do not have an initializer, the initialization value of the
7723
     previous enumerator (stored in last_initializer) is incremented
7724
     by 1 and is used to initialize the current enumerator.  */
7725
  if (initializer == NULL)
7726
    initializer = enum_initializer (last_initializer, old_locus);
7727
 
7728
  if (initializer == NULL || initializer->ts.type != BT_INTEGER)
7729
    {
7730
      gfc_error ("ENUMERATOR %L not initialized with integer expression",
7731
                 &var_locus);
7732
      m = MATCH_ERROR;
7733
      goto cleanup;
7734
    }
7735
 
7736
  /* Store this current initializer, for the next enumerator variable
7737
     to be parsed.  add_init_expr_to_sym() zeros initializer, so we
7738
     use last_initializer below.  */
7739
  last_initializer = initializer;
7740
  t = add_init_expr_to_sym (name, &initializer, &var_locus);
7741
 
7742
  /* Maintain enumerator history.  */
7743
  gfc_find_symbol (name, NULL, 0, &sym);
7744
  create_enum_history (sym, last_initializer);
7745
 
7746
  return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
7747
 
7748
cleanup:
7749
  /* Free stuff up and return.  */
7750
  gfc_free_expr (initializer);
7751
 
7752
  return m;
7753
}
7754
 
7755
 
7756
/* Match the enumerator definition statement.  */
7757
 
7758
match
7759
gfc_match_enumerator_def (void)
7760
{
7761
  match m;
7762
  gfc_try t;
7763
 
7764
  gfc_clear_ts (&current_ts);
7765
 
7766
  m = gfc_match (" enumerator");
7767
  if (m != MATCH_YES)
7768
    return m;
7769
 
7770
  m = gfc_match (" :: ");
7771
  if (m == MATCH_ERROR)
7772
    return m;
7773
 
7774
  colon_seen = (m == MATCH_YES);
7775
 
7776
  if (gfc_current_state () != COMP_ENUM)
7777
    {
7778
      gfc_error ("ENUM definition statement expected before %C");
7779
      gfc_free_enum_history ();
7780
      return MATCH_ERROR;
7781
    }
7782
 
7783
  (&current_ts)->type = BT_INTEGER;
7784
  (&current_ts)->kind = gfc_c_int_kind;
7785
 
7786
  gfc_clear_attr (&current_attr);
7787
  t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
7788
  if (t == FAILURE)
7789
    {
7790
      m = MATCH_ERROR;
7791
      goto cleanup;
7792
    }
7793
 
7794
  for (;;)
7795
    {
7796
      m = enumerator_decl ();
7797
      if (m == MATCH_ERROR)
7798
        {
7799
          gfc_free_enum_history ();
7800
          goto cleanup;
7801
        }
7802
      if (m == MATCH_NO)
7803
        break;
7804
 
7805
      if (gfc_match_eos () == MATCH_YES)
7806
        goto cleanup;
7807
      if (gfc_match_char (',') != MATCH_YES)
7808
        break;
7809
    }
7810
 
7811
  if (gfc_current_state () == COMP_ENUM)
7812
    {
7813
      gfc_free_enum_history ();
7814
      gfc_error ("Syntax error in ENUMERATOR definition at %C");
7815
      m = MATCH_ERROR;
7816
    }
7817
 
7818
cleanup:
7819
  gfc_free_array_spec (current_as);
7820
  current_as = NULL;
7821
  return m;
7822
 
7823
}
7824
 
7825
 
7826
/* Match binding attributes.  */
7827
 
7828
static match
7829
match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
7830
{
7831
  bool found_passing = false;
7832
  bool seen_ptr = false;
7833
  match m = MATCH_YES;
7834
 
7835
  /* Intialize to defaults.  Do so even before the MATCH_NO check so that in
7836
     this case the defaults are in there.  */
7837
  ba->access = ACCESS_UNKNOWN;
7838
  ba->pass_arg = NULL;
7839
  ba->pass_arg_num = 0;
7840
  ba->nopass = 0;
7841
  ba->non_overridable = 0;
7842
  ba->deferred = 0;
7843
  ba->ppc = ppc;
7844
 
7845
  /* If we find a comma, we believe there are binding attributes.  */
7846
  m = gfc_match_char (',');
7847
  if (m == MATCH_NO)
7848
    goto done;
7849
 
7850
  do
7851
    {
7852
      /* Access specifier.  */
7853
 
7854
      m = gfc_match (" public");
7855
      if (m == MATCH_ERROR)
7856
        goto error;
7857
      if (m == MATCH_YES)
7858
        {
7859
          if (ba->access != ACCESS_UNKNOWN)
7860
            {
7861
              gfc_error ("Duplicate access-specifier at %C");
7862
              goto error;
7863
            }
7864
 
7865
          ba->access = ACCESS_PUBLIC;
7866
          continue;
7867
        }
7868
 
7869
      m = gfc_match (" private");
7870
      if (m == MATCH_ERROR)
7871
        goto error;
7872
      if (m == MATCH_YES)
7873
        {
7874
          if (ba->access != ACCESS_UNKNOWN)
7875
            {
7876
              gfc_error ("Duplicate access-specifier at %C");
7877
              goto error;
7878
            }
7879
 
7880
          ba->access = ACCESS_PRIVATE;
7881
          continue;
7882
        }
7883
 
7884
      /* If inside GENERIC, the following is not allowed.  */
7885
      if (!generic)
7886
        {
7887
 
7888
          /* NOPASS flag.  */
7889
          m = gfc_match (" nopass");
7890
          if (m == MATCH_ERROR)
7891
            goto error;
7892
          if (m == MATCH_YES)
7893
            {
7894
              if (found_passing)
7895
                {
7896
                  gfc_error ("Binding attributes already specify passing,"
7897
                             " illegal NOPASS at %C");
7898
                  goto error;
7899
                }
7900
 
7901
              found_passing = true;
7902
              ba->nopass = 1;
7903
              continue;
7904
            }
7905
 
7906
          /* PASS possibly including argument.  */
7907
          m = gfc_match (" pass");
7908
          if (m == MATCH_ERROR)
7909
            goto error;
7910
          if (m == MATCH_YES)
7911
            {
7912
              char arg[GFC_MAX_SYMBOL_LEN + 1];
7913
 
7914
              if (found_passing)
7915
                {
7916
                  gfc_error ("Binding attributes already specify passing,"
7917
                             " illegal PASS at %C");
7918
                  goto error;
7919
                }
7920
 
7921
              m = gfc_match (" ( %n )", arg);
7922
              if (m == MATCH_ERROR)
7923
                goto error;
7924
              if (m == MATCH_YES)
7925
                ba->pass_arg = gfc_get_string (arg);
7926
              gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
7927
 
7928
              found_passing = true;
7929
              ba->nopass = 0;
7930
              continue;
7931
            }
7932
 
7933
          if (ppc)
7934
            {
7935
              /* POINTER flag.  */
7936
              m = gfc_match (" pointer");
7937
              if (m == MATCH_ERROR)
7938
                goto error;
7939
              if (m == MATCH_YES)
7940
                {
7941
                  if (seen_ptr)
7942
                    {
7943
                      gfc_error ("Duplicate POINTER attribute at %C");
7944
                      goto error;
7945
                    }
7946
 
7947
                  seen_ptr = true;
7948
                  continue;
7949
                }
7950
            }
7951
          else
7952
            {
7953
              /* NON_OVERRIDABLE flag.  */
7954
              m = gfc_match (" non_overridable");
7955
              if (m == MATCH_ERROR)
7956
                goto error;
7957
              if (m == MATCH_YES)
7958
                {
7959
                  if (ba->non_overridable)
7960
                    {
7961
                      gfc_error ("Duplicate NON_OVERRIDABLE at %C");
7962
                      goto error;
7963
                    }
7964
 
7965
                  ba->non_overridable = 1;
7966
                  continue;
7967
                }
7968
 
7969
              /* DEFERRED flag.  */
7970
              m = gfc_match (" deferred");
7971
              if (m == MATCH_ERROR)
7972
                goto error;
7973
              if (m == MATCH_YES)
7974
                {
7975
                  if (ba->deferred)
7976
                    {
7977
                      gfc_error ("Duplicate DEFERRED at %C");
7978
                      goto error;
7979
                    }
7980
 
7981
                  ba->deferred = 1;
7982
                  continue;
7983
                }
7984
            }
7985
 
7986
        }
7987
 
7988
      /* Nothing matching found.  */
7989
      if (generic)
7990
        gfc_error ("Expected access-specifier at %C");
7991
      else
7992
        gfc_error ("Expected binding attribute at %C");
7993
      goto error;
7994
    }
7995
  while (gfc_match_char (',') == MATCH_YES);
7996
 
7997
  /* NON_OVERRIDABLE and DEFERRED exclude themselves.  */
7998
  if (ba->non_overridable && ba->deferred)
7999
    {
8000
      gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
8001
      goto error;
8002
    }
8003
 
8004
  m = MATCH_YES;
8005
 
8006
done:
8007
  if (ba->access == ACCESS_UNKNOWN)
8008
    ba->access = gfc_typebound_default_access;
8009
 
8010
  if (ppc && !seen_ptr)
8011
    {
8012
      gfc_error ("POINTER attribute is required for procedure pointer component"
8013
                 " at %C");
8014
      goto error;
8015
    }
8016
 
8017
  return m;
8018
 
8019
error:
8020
  return MATCH_ERROR;
8021
}
8022
 
8023
 
8024
/* Match a PROCEDURE specific binding inside a derived type.  */
8025
 
8026
static match
8027
match_procedure_in_type (void)
8028
{
8029
  char name[GFC_MAX_SYMBOL_LEN + 1];
8030
  char target_buf[GFC_MAX_SYMBOL_LEN + 1];
8031
  char* target = NULL, *ifc = NULL;
8032
  gfc_typebound_proc tb;
8033
  bool seen_colons;
8034
  bool seen_attrs;
8035
  match m;
8036
  gfc_symtree* stree;
8037
  gfc_namespace* ns;
8038
  gfc_symbol* block;
8039
  int num;
8040
 
8041
  /* Check current state.  */
8042
  gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
8043
  block = gfc_state_stack->previous->sym;
8044
  gcc_assert (block);
8045
 
8046
  /* Try to match PROCEDURE(interface).  */
8047
  if (gfc_match (" (") == MATCH_YES)
8048
    {
8049
      m = gfc_match_name (target_buf);
8050
      if (m == MATCH_ERROR)
8051
        return m;
8052
      if (m != MATCH_YES)
8053
        {
8054
          gfc_error ("Interface-name expected after '(' at %C");
8055
          return MATCH_ERROR;
8056
        }
8057
 
8058
      if (gfc_match (" )") != MATCH_YES)
8059
        {
8060
          gfc_error ("')' expected at %C");
8061
          return MATCH_ERROR;
8062
        }
8063
 
8064
      ifc = target_buf;
8065
    }
8066
 
8067
  /* Construct the data structure.  */
8068
  memset (&tb, 0, sizeof (tb));
8069
  tb.where = gfc_current_locus;
8070
 
8071
  /* Match binding attributes.  */
8072
  m = match_binding_attributes (&tb, false, false);
8073
  if (m == MATCH_ERROR)
8074
    return m;
8075
  seen_attrs = (m == MATCH_YES);
8076
 
8077
  /* Check that attribute DEFERRED is given if an interface is specified.  */
8078
  if (tb.deferred && !ifc)
8079
    {
8080
      gfc_error ("Interface must be specified for DEFERRED binding at %C");
8081
      return MATCH_ERROR;
8082
    }
8083
  if (ifc && !tb.deferred)
8084
    {
8085
      gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
8086
      return MATCH_ERROR;
8087
    }
8088
 
8089
  /* Match the colons.  */
8090
  m = gfc_match (" ::");
8091
  if (m == MATCH_ERROR)
8092
    return m;
8093
  seen_colons = (m == MATCH_YES);
8094
  if (seen_attrs && !seen_colons)
8095
    {
8096
      gfc_error ("Expected '::' after binding-attributes at %C");
8097
      return MATCH_ERROR;
8098
    }
8099
 
8100
  /* Match the binding names.  */
8101
  for(num=1;;num++)
8102
    {
8103
      m = gfc_match_name (name);
8104
      if (m == MATCH_ERROR)
8105
        return m;
8106
      if (m == MATCH_NO)
8107
        {
8108
          gfc_error ("Expected binding name at %C");
8109
          return MATCH_ERROR;
8110
        }
8111
 
8112
      if (num>1 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: PROCEDURE list"
8113
                                   " at %C") == FAILURE)
8114
        return MATCH_ERROR;
8115
 
8116
      /* Try to match the '=> target', if it's there.  */
8117
      target = ifc;
8118
      m = gfc_match (" =>");
8119
      if (m == MATCH_ERROR)
8120
        return m;
8121
      if (m == MATCH_YES)
8122
        {
8123
          if (tb.deferred)
8124
            {
8125
              gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
8126
              return MATCH_ERROR;
8127
            }
8128
 
8129
          if (!seen_colons)
8130
            {
8131
              gfc_error ("'::' needed in PROCEDURE binding with explicit target"
8132
                         " at %C");
8133
              return MATCH_ERROR;
8134
            }
8135
 
8136
          m = gfc_match_name (target_buf);
8137
          if (m == MATCH_ERROR)
8138
            return m;
8139
          if (m == MATCH_NO)
8140
            {
8141
              gfc_error ("Expected binding target after '=>' at %C");
8142
              return MATCH_ERROR;
8143
            }
8144
          target = target_buf;
8145
        }
8146
 
8147
      /* If no target was found, it has the same name as the binding.  */
8148
      if (!target)
8149
        target = name;
8150
 
8151
      /* Get the namespace to insert the symbols into.  */
8152
      ns = block->f2k_derived;
8153
      gcc_assert (ns);
8154
 
8155
      /* If the binding is DEFERRED, check that the containing type is ABSTRACT.  */
8156
      if (tb.deferred && !block->attr.abstract)
8157
        {
8158
          gfc_error ("Type '%s' containing DEFERRED binding at %C "
8159
                     "is not ABSTRACT", block->name);
8160
          return MATCH_ERROR;
8161
        }
8162
 
8163
      /* See if we already have a binding with this name in the symtree which
8164
         would be an error.  If a GENERIC already targetted this binding, it may
8165
         be already there but then typebound is still NULL.  */
8166
      stree = gfc_find_symtree (ns->tb_sym_root, name);
8167
      if (stree && stree->n.tb)
8168
        {
8169
          gfc_error ("There is already a procedure with binding name '%s' for "
8170
                     "the derived type '%s' at %C", name, block->name);
8171
          return MATCH_ERROR;
8172
        }
8173
 
8174
      /* Insert it and set attributes.  */
8175
 
8176
      if (!stree)
8177
        {
8178
          stree = gfc_new_symtree (&ns->tb_sym_root, name);
8179
          gcc_assert (stree);
8180
        }
8181
      stree->n.tb = gfc_get_typebound_proc (&tb);
8182
 
8183
      if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
8184
                            false))
8185
        return MATCH_ERROR;
8186
      gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
8187
 
8188
      if (gfc_match_eos () == MATCH_YES)
8189
        return MATCH_YES;
8190
      if (gfc_match_char (',') != MATCH_YES)
8191
        goto syntax;
8192
    }
8193
 
8194
syntax:
8195
  gfc_error ("Syntax error in PROCEDURE statement at %C");
8196
  return MATCH_ERROR;
8197
}
8198
 
8199
 
8200
/* Match a GENERIC procedure binding inside a derived type.  */
8201
 
8202
match
8203
gfc_match_generic (void)
8204
{
8205
  char name[GFC_MAX_SYMBOL_LEN + 1];
8206
  char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...).  */
8207
  gfc_symbol* block;
8208
  gfc_typebound_proc tbattr; /* Used for match_binding_attributes.  */
8209
  gfc_typebound_proc* tb;
8210
  gfc_namespace* ns;
8211
  interface_type op_type;
8212
  gfc_intrinsic_op op;
8213
  match m;
8214
 
8215
  /* Check current state.  */
8216
  if (gfc_current_state () == COMP_DERIVED)
8217
    {
8218
      gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8219
      return MATCH_ERROR;
8220
    }
8221
  if (gfc_current_state () != COMP_DERIVED_CONTAINS)
8222
    return MATCH_NO;
8223
  block = gfc_state_stack->previous->sym;
8224
  ns = block->f2k_derived;
8225
  gcc_assert (block && ns);
8226
 
8227
  memset (&tbattr, 0, sizeof (tbattr));
8228
  tbattr.where = gfc_current_locus;
8229
 
8230
  /* See if we get an access-specifier.  */
8231
  m = match_binding_attributes (&tbattr, true, false);
8232
  if (m == MATCH_ERROR)
8233
    goto error;
8234
 
8235
  /* Now the colons, those are required.  */
8236
  if (gfc_match (" ::") != MATCH_YES)
8237
    {
8238
      gfc_error ("Expected '::' at %C");
8239
      goto error;
8240
    }
8241
 
8242
  /* Match the binding name; depending on type (operator / generic) format
8243
     it for future error messages into bind_name.  */
8244
 
8245
  m = gfc_match_generic_spec (&op_type, name, &op);
8246
  if (m == MATCH_ERROR)
8247
    return MATCH_ERROR;
8248
  if (m == MATCH_NO)
8249
    {
8250
      gfc_error ("Expected generic name or operator descriptor at %C");
8251
      goto error;
8252
    }
8253
 
8254
  switch (op_type)
8255
    {
8256
    case INTERFACE_GENERIC:
8257
      snprintf (bind_name, sizeof (bind_name), "%s", name);
8258
      break;
8259
 
8260
    case INTERFACE_USER_OP:
8261
      snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
8262
      break;
8263
 
8264
    case INTERFACE_INTRINSIC_OP:
8265
      snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
8266
                gfc_op2string (op));
8267
      break;
8268
 
8269
    default:
8270
      gcc_unreachable ();
8271
    }
8272
 
8273
  /* Match the required =>.  */
8274
  if (gfc_match (" =>") != MATCH_YES)
8275
    {
8276
      gfc_error ("Expected '=>' at %C");
8277
      goto error;
8278
    }
8279
 
8280
  /* Try to find existing GENERIC binding with this name / for this operator;
8281
     if there is something, check that it is another GENERIC and then extend
8282
     it rather than building a new node.  Otherwise, create it and put it
8283
     at the right position.  */
8284
 
8285
  switch (op_type)
8286
    {
8287
    case INTERFACE_USER_OP:
8288
    case INTERFACE_GENERIC:
8289
      {
8290
        const bool is_op = (op_type == INTERFACE_USER_OP);
8291
        gfc_symtree* st;
8292
 
8293
        st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
8294
        if (st)
8295
          {
8296
            tb = st->n.tb;
8297
            gcc_assert (tb);
8298
          }
8299
        else
8300
          tb = NULL;
8301
 
8302
        break;
8303
      }
8304
 
8305
    case INTERFACE_INTRINSIC_OP:
8306
      tb = ns->tb_op[op];
8307
      break;
8308
 
8309
    default:
8310
      gcc_unreachable ();
8311
    }
8312
 
8313
  if (tb)
8314
    {
8315
      if (!tb->is_generic)
8316
        {
8317
          gcc_assert (op_type == INTERFACE_GENERIC);
8318
          gfc_error ("There's already a non-generic procedure with binding name"
8319
                     " '%s' for the derived type '%s' at %C",
8320
                     bind_name, block->name);
8321
          goto error;
8322
        }
8323
 
8324
      if (tb->access != tbattr.access)
8325
        {
8326
          gfc_error ("Binding at %C must have the same access as already"
8327
                     " defined binding '%s'", bind_name);
8328
          goto error;
8329
        }
8330
    }
8331
  else
8332
    {
8333
      tb = gfc_get_typebound_proc (NULL);
8334
      tb->where = gfc_current_locus;
8335
      tb->access = tbattr.access;
8336
      tb->is_generic = 1;
8337
      tb->u.generic = NULL;
8338
 
8339
      switch (op_type)
8340
        {
8341
        case INTERFACE_GENERIC:
8342
        case INTERFACE_USER_OP:
8343
          {
8344
            const bool is_op = (op_type == INTERFACE_USER_OP);
8345
            gfc_symtree* st;
8346
 
8347
            st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
8348
                                  name);
8349
            gcc_assert (st);
8350
            st->n.tb = tb;
8351
 
8352
            break;
8353
          }
8354
 
8355
        case INTERFACE_INTRINSIC_OP:
8356
          ns->tb_op[op] = tb;
8357
          break;
8358
 
8359
        default:
8360
          gcc_unreachable ();
8361
        }
8362
    }
8363
 
8364
  /* Now, match all following names as specific targets.  */
8365
  do
8366
    {
8367
      gfc_symtree* target_st;
8368
      gfc_tbp_generic* target;
8369
 
8370
      m = gfc_match_name (name);
8371
      if (m == MATCH_ERROR)
8372
        goto error;
8373
      if (m == MATCH_NO)
8374
        {
8375
          gfc_error ("Expected specific binding name at %C");
8376
          goto error;
8377
        }
8378
 
8379
      target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
8380
 
8381
      /* See if this is a duplicate specification.  */
8382
      for (target = tb->u.generic; target; target = target->next)
8383
        if (target_st == target->specific_st)
8384
          {
8385
            gfc_error ("'%s' already defined as specific binding for the"
8386
                       " generic '%s' at %C", name, bind_name);
8387
            goto error;
8388
          }
8389
 
8390
      target = gfc_get_tbp_generic ();
8391
      target->specific_st = target_st;
8392
      target->specific = NULL;
8393
      target->next = tb->u.generic;
8394
      target->is_operator = ((op_type == INTERFACE_USER_OP)
8395
                             || (op_type == INTERFACE_INTRINSIC_OP));
8396
      tb->u.generic = target;
8397
    }
8398
  while (gfc_match (" ,") == MATCH_YES);
8399
 
8400
  /* Here should be the end.  */
8401
  if (gfc_match_eos () != MATCH_YES)
8402
    {
8403
      gfc_error ("Junk after GENERIC binding at %C");
8404
      goto error;
8405
    }
8406
 
8407
  return MATCH_YES;
8408
 
8409
error:
8410
  return MATCH_ERROR;
8411
}
8412
 
8413
 
8414
/* Match a FINAL declaration inside a derived type.  */
8415
 
8416
match
8417
gfc_match_final_decl (void)
8418
{
8419
  char name[GFC_MAX_SYMBOL_LEN + 1];
8420
  gfc_symbol* sym;
8421
  match m;
8422
  gfc_namespace* module_ns;
8423
  bool first, last;
8424
  gfc_symbol* block;
8425
 
8426
  if (gfc_current_form == FORM_FREE)
8427
    {
8428
      char c = gfc_peek_ascii_char ();
8429
      if (!gfc_is_whitespace (c) && c != ':')
8430
        return MATCH_NO;
8431
    }
8432
 
8433
  if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
8434
    {
8435
      if (gfc_current_form == FORM_FIXED)
8436
        return MATCH_NO;
8437
 
8438
      gfc_error ("FINAL declaration at %C must be inside a derived type "
8439
                 "CONTAINS section");
8440
      return MATCH_ERROR;
8441
    }
8442
 
8443
  block = gfc_state_stack->previous->sym;
8444
  gcc_assert (block);
8445
 
8446
  if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
8447
      || gfc_state_stack->previous->previous->state != COMP_MODULE)
8448
    {
8449
      gfc_error ("Derived type declaration with FINAL at %C must be in the"
8450
                 " specification part of a MODULE");
8451
      return MATCH_ERROR;
8452
    }
8453
 
8454
  module_ns = gfc_current_ns;
8455
  gcc_assert (module_ns);
8456
  gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
8457
 
8458
  /* Match optional ::, don't care about MATCH_YES or MATCH_NO.  */
8459
  if (gfc_match (" ::") == MATCH_ERROR)
8460
    return MATCH_ERROR;
8461
 
8462
  /* Match the sequence of procedure names.  */
8463
  first = true;
8464
  last = false;
8465
  do
8466
    {
8467
      gfc_finalizer* f;
8468
 
8469
      if (first && gfc_match_eos () == MATCH_YES)
8470
        {
8471
          gfc_error ("Empty FINAL at %C");
8472
          return MATCH_ERROR;
8473
        }
8474
 
8475
      m = gfc_match_name (name);
8476
      if (m == MATCH_NO)
8477
        {
8478
          gfc_error ("Expected module procedure name at %C");
8479
          return MATCH_ERROR;
8480
        }
8481
      else if (m != MATCH_YES)
8482
        return MATCH_ERROR;
8483
 
8484
      if (gfc_match_eos () == MATCH_YES)
8485
        last = true;
8486
      if (!last && gfc_match_char (',') != MATCH_YES)
8487
        {
8488
          gfc_error ("Expected ',' at %C");
8489
          return MATCH_ERROR;
8490
        }
8491
 
8492
      if (gfc_get_symbol (name, module_ns, &sym))
8493
        {
8494
          gfc_error ("Unknown procedure name \"%s\" at %C", name);
8495
          return MATCH_ERROR;
8496
        }
8497
 
8498
      /* Mark the symbol as module procedure.  */
8499
      if (sym->attr.proc != PROC_MODULE
8500
          && gfc_add_procedure (&sym->attr, PROC_MODULE,
8501
                                sym->name, NULL) == FAILURE)
8502
        return MATCH_ERROR;
8503
 
8504
      /* Check if we already have this symbol in the list, this is an error.  */
8505
      for (f = block->f2k_derived->finalizers; f; f = f->next)
8506
        if (f->proc_sym == sym)
8507
          {
8508
            gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8509
                       name);
8510
            return MATCH_ERROR;
8511
          }
8512
 
8513
      /* Add this symbol to the list of finalizers.  */
8514
      gcc_assert (block->f2k_derived);
8515
      ++sym->refs;
8516
      f = XCNEW (gfc_finalizer);
8517
      f->proc_sym = sym;
8518
      f->proc_tree = NULL;
8519
      f->where = gfc_current_locus;
8520
      f->next = block->f2k_derived->finalizers;
8521
      block->f2k_derived->finalizers = f;
8522
 
8523
      first = false;
8524
    }
8525
  while (!last);
8526
 
8527
  return MATCH_YES;
8528
}
8529
 
8530
 
8531
const ext_attr_t ext_attr_list[] = {
8532
  { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
8533
  { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
8534
  { "cdecl",     EXT_ATTR_CDECL,     "cdecl"     },
8535
  { "stdcall",   EXT_ATTR_STDCALL,   "stdcall"   },
8536
  { "fastcall",  EXT_ATTR_FASTCALL,  "fastcall"  },
8537
  { NULL,        EXT_ATTR_LAST,      NULL        }
8538
};
8539
 
8540
/* Match a !GCC$ ATTRIBUTES statement of the form:
8541
      !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8542
   When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8543
 
8544
   TODO: We should support all GCC attributes using the same syntax for
8545
   the attribute list, i.e. the list in C
8546
      __attributes(( attribute-list ))
8547
   matches then
8548
      !GCC$ ATTRIBUTES attribute-list ::
8549
   Cf. c-parser.c's c_parser_attributes; the data can then directly be
8550
   saved into a TREE.
8551
 
8552
   As there is absolutely no risk of confusion, we should never return
8553
   MATCH_NO.  */
8554
match
8555
gfc_match_gcc_attributes (void)
8556
{
8557
  symbol_attribute attr;
8558
  char name[GFC_MAX_SYMBOL_LEN + 1];
8559
  unsigned id;
8560
  gfc_symbol *sym;
8561
  match m;
8562
 
8563
  gfc_clear_attr (&attr);
8564
  for(;;)
8565
    {
8566
      char ch;
8567
 
8568
      if (gfc_match_name (name) != MATCH_YES)
8569
        return MATCH_ERROR;
8570
 
8571
      for (id = 0; id < EXT_ATTR_LAST; id++)
8572
        if (strcmp (name, ext_attr_list[id].name) == 0)
8573
          break;
8574
 
8575
      if (id == EXT_ATTR_LAST)
8576
        {
8577
          gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8578
          return MATCH_ERROR;
8579
        }
8580
 
8581
      if (gfc_add_ext_attribute (&attr, (ext_attr_id_t) id, &gfc_current_locus)
8582
          == FAILURE)
8583
        return MATCH_ERROR;
8584
 
8585
      gfc_gobble_whitespace ();
8586
      ch = gfc_next_ascii_char ();
8587
      if (ch == ':')
8588
        {
8589
          /* This is the successful exit condition for the loop.  */
8590
          if (gfc_next_ascii_char () == ':')
8591
            break;
8592
        }
8593
 
8594
      if (ch == ',')
8595
        continue;
8596
 
8597
      goto syntax;
8598
    }
8599
 
8600
  if (gfc_match_eos () == MATCH_YES)
8601
    goto syntax;
8602
 
8603
  for(;;)
8604
    {
8605
      m = gfc_match_name (name);
8606
      if (m != MATCH_YES)
8607
        return m;
8608
 
8609
      if (find_special (name, &sym, true))
8610
        return MATCH_ERROR;
8611
 
8612
      sym->attr.ext_attr |= attr.ext_attr;
8613
 
8614
      if (gfc_match_eos () == MATCH_YES)
8615
        break;
8616
 
8617
      if (gfc_match_char (',') != MATCH_YES)
8618
        goto syntax;
8619
    }
8620
 
8621
  return MATCH_YES;
8622
 
8623
syntax:
8624
  gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
8625
  return MATCH_ERROR;
8626
}

powered by: WebSVN 2.1.0

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