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

Subversion Repositories scarts

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

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

Line No. Rev Author Line
1 12 jlechner
/* Maintain binary trees of symbols.
2
   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation,
3
   Inc.
4
   Contributed by Andy Vaught
5
 
6
This file is part of GCC.
7
 
8
GCC is free software; you can redistribute it and/or modify it under
9
the terms of the GNU General Public License as published by the Free
10
Software Foundation; either version 2, or (at your option) any later
11
version.
12
 
13
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14
WARRANTY; without even the implied warranty of MERCHANTABILITY or
15
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16
for more details.
17
 
18
You should have received a copy of the GNU General Public License
19
along with GCC; see the file COPYING.  If not, write to the Free
20
Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21
02110-1301, USA.  */
22
 
23
 
24
#include "config.h"
25
#include "system.h"
26
#include "gfortran.h"
27
#include "parse.h"
28
 
29
/* Strings for all symbol attributes.  We use these for dumping the
30
   parse tree, in error messages, and also when reading and writing
31
   modules.  */
32
 
33
const mstring flavors[] =
34
{
35
  minit ("UNKNOWN-FL", FL_UNKNOWN), minit ("PROGRAM", FL_PROGRAM),
36
  minit ("BLOCK-DATA", FL_BLOCK_DATA), minit ("MODULE", FL_MODULE),
37
  minit ("VARIABLE", FL_VARIABLE), minit ("PARAMETER", FL_PARAMETER),
38
  minit ("LABEL", FL_LABEL), minit ("PROCEDURE", FL_PROCEDURE),
39
  minit ("DERIVED", FL_DERIVED), minit ("NAMELIST", FL_NAMELIST),
40
  minit (NULL, -1)
41
};
42
 
43
const mstring procedures[] =
44
{
45
    minit ("UNKNOWN-PROC", PROC_UNKNOWN),
46
    minit ("MODULE-PROC", PROC_MODULE),
47
    minit ("INTERNAL-PROC", PROC_INTERNAL),
48
    minit ("DUMMY-PROC", PROC_DUMMY),
49
    minit ("INTRINSIC-PROC", PROC_INTRINSIC),
50
    minit ("EXTERNAL-PROC", PROC_EXTERNAL),
51
    minit ("STATEMENT-PROC", PROC_ST_FUNCTION),
52
    minit (NULL, -1)
53
};
54
 
55
const mstring intents[] =
56
{
57
    minit ("UNKNOWN-INTENT", INTENT_UNKNOWN),
58
    minit ("IN", INTENT_IN),
59
    minit ("OUT", INTENT_OUT),
60
    minit ("INOUT", INTENT_INOUT),
61
    minit (NULL, -1)
62
};
63
 
64
const mstring access_types[] =
65
{
66
    minit ("UNKNOWN-ACCESS", ACCESS_UNKNOWN),
67
    minit ("PUBLIC", ACCESS_PUBLIC),
68
    minit ("PRIVATE", ACCESS_PRIVATE),
69
    minit (NULL, -1)
70
};
71
 
72
const mstring ifsrc_types[] =
73
{
74
    minit ("UNKNOWN", IFSRC_UNKNOWN),
75
    minit ("DECL", IFSRC_DECL),
76
    minit ("BODY", IFSRC_IFBODY),
77
    minit ("USAGE", IFSRC_USAGE)
78
};
79
 
80
 
81
/* This is to make sure the backend generates setup code in the correct
82
   order.  */
83
 
84
static int next_dummy_order = 1;
85
 
86
 
87
gfc_namespace *gfc_current_ns;
88
 
89
gfc_gsymbol *gfc_gsym_root = NULL;
90
 
91
static gfc_symbol *changed_syms = NULL;
92
 
93
 
94
/*********** IMPLICIT NONE and IMPLICIT statement handlers ***********/
95
 
96
/* The following static variable indicates whether a particular element has
97
   been explicitly set or not.  */
98
 
99
static int new_flag[GFC_LETTERS];
100
 
101
 
102
/* Handle a correctly parsed IMPLICIT NONE.  */
103
 
104
void
105
gfc_set_implicit_none (void)
106
{
107
  int i;
108
 
109
  if (gfc_current_ns->seen_implicit_none)
110
    {
111
      gfc_error ("Duplicate IMPLICIT NONE statement at %C");
112
      return;
113
    }
114
 
115
  gfc_current_ns->seen_implicit_none = 1;
116
 
117
  for (i = 0; i < GFC_LETTERS; i++)
118
    {
119
      gfc_clear_ts (&gfc_current_ns->default_type[i]);
120
      gfc_current_ns->set_flag[i] = 1;
121
    }
122
}
123
 
124
 
125
/* Reset the implicit range flags.  */
126
 
127
void
128
gfc_clear_new_implicit (void)
129
{
130
  int i;
131
 
132
  for (i = 0; i < GFC_LETTERS; i++)
133
    new_flag[i] = 0;
134
}
135
 
136
 
137
/* Prepare for a new implicit range.  Sets flags in new_flag[].  */
138
 
139
try
140
gfc_add_new_implicit_range (int c1, int c2)
141
{
142
  int i;
143
 
144
  c1 -= 'a';
145
  c2 -= 'a';
146
 
147
  for (i = c1; i <= c2; i++)
148
    {
149
      if (new_flag[i])
150
        {
151
          gfc_error ("Letter '%c' already set in IMPLICIT statement at %C",
152
                     i + 'A');
153
          return FAILURE;
154
        }
155
 
156
      new_flag[i] = 1;
157
    }
158
 
159
  return SUCCESS;
160
}
161
 
162
 
163
/* Add a matched implicit range for gfc_set_implicit().  Check if merging
164
   the new implicit types back into the existing types will work.  */
165
 
166
try
167
gfc_merge_new_implicit (gfc_typespec * ts)
168
{
169
  int i;
170
 
171
  if (gfc_current_ns->seen_implicit_none)
172
    {
173
      gfc_error ("Cannot specify IMPLICIT at %C after IMPLICIT NONE");
174
      return FAILURE;
175
    }
176
 
177
  for (i = 0; i < GFC_LETTERS; i++)
178
    {
179
      if (new_flag[i])
180
        {
181
 
182
          if (gfc_current_ns->set_flag[i])
183
            {
184
              gfc_error ("Letter %c already has an IMPLICIT type at %C",
185
                         i + 'A');
186
              return FAILURE;
187
            }
188
          gfc_current_ns->default_type[i] = *ts;
189
          gfc_current_ns->set_flag[i] = 1;
190
        }
191
    }
192
  return SUCCESS;
193
}
194
 
195
 
196
/* Given a symbol, return a pointer to the typespec for its default type.  */
197
 
198
gfc_typespec *
199
gfc_get_default_type (gfc_symbol * sym, gfc_namespace * ns)
200
{
201
  char letter;
202
 
203
  letter = sym->name[0];
204
  if (letter < 'a' || letter > 'z')
205
    gfc_internal_error ("gfc_get_default_type(): Bad symbol");
206
 
207
  if (ns == NULL)
208
    ns = gfc_current_ns;
209
 
210
  return &ns->default_type[letter - 'a'];
211
}
212
 
213
 
214
/* Given a pointer to a symbol, set its type according to the first
215
   letter of its name.  Fails if the letter in question has no default
216
   type.  */
217
 
218
try
219
gfc_set_default_type (gfc_symbol * sym, int error_flag, gfc_namespace * ns)
220
{
221
  gfc_typespec *ts;
222
 
223
  if (sym->ts.type != BT_UNKNOWN)
224
    gfc_internal_error ("gfc_set_default_type(): symbol already has a type");
225
 
226
  ts = gfc_get_default_type (sym, ns);
227
 
228
  if (ts->type == BT_UNKNOWN)
229
    {
230
      if (error_flag && !sym->attr.untyped)
231
        {
232
          gfc_error ("Symbol '%s' at %L has no IMPLICIT type",
233
                     sym->name, &sym->declared_at);
234
          sym->attr.untyped = 1; /* Ensure we only give an error once.  */
235
        }
236
 
237
      return FAILURE;
238
    }
239
 
240
  sym->ts = *ts;
241
  sym->attr.implicit_type = 1;
242
 
243
  return SUCCESS;
244
}
245
 
246
 
247
/******************** Symbol attribute stuff *********************/
248
 
249
/* This is a generic conflict-checker.  We do this to avoid having a
250
   single conflict in two places.  */
251
 
252
#define conf(a, b) if (attr->a && attr->b) { a1 = a; a2 = b; goto conflict; }
253
#define conf2(a) if (attr->a) { a2 = a; goto conflict; }
254
 
255
static try
256
check_conflict (symbol_attribute * attr, const char * name, locus * where)
257
{
258
  static const char *dummy = "DUMMY", *save = "SAVE", *pointer = "POINTER",
259
    *target = "TARGET", *external = "EXTERNAL", *intent = "INTENT",
260
    *intrinsic = "INTRINSIC", *allocatable = "ALLOCATABLE",
261
    *elemental = "ELEMENTAL", *private = "PRIVATE", *recursive = "RECURSIVE",
262
    *in_common = "COMMON", *result = "RESULT", *in_namelist = "NAMELIST",
263
    *public = "PUBLIC", *optional = "OPTIONAL", *entry = "ENTRY",
264
    *function = "FUNCTION", *subroutine = "SUBROUTINE",
265
    *dimension = "DIMENSION", *in_equivalence = "EQUIVALENCE",
266
    *use_assoc = "USE ASSOCIATED", *cray_pointer = "CRAY POINTER",
267
    *cray_pointee = "CRAY POINTEE", *data = "DATA";
268
 
269
  const char *a1, *a2;
270
 
271
  if (where == NULL)
272
    where = &gfc_current_locus;
273
 
274
  if (attr->pointer && attr->intent != INTENT_UNKNOWN)
275
    {
276
      a1 = pointer;
277
      a2 = intent;
278
      goto conflict;
279
    }
280
 
281
  /* Check for attributes not allowed in a BLOCK DATA.  */
282
  if (gfc_current_state () == COMP_BLOCK_DATA)
283
    {
284
      a1 = NULL;
285
 
286
      if (attr->in_namelist)
287
        a1 = in_namelist;
288
      if (attr->allocatable)
289
        a1 = allocatable;
290
      if (attr->external)
291
        a1 = external;
292
      if (attr->optional)
293
        a1 = optional;
294
      if (attr->access == ACCESS_PRIVATE)
295
        a1 = private;
296
      if (attr->access == ACCESS_PUBLIC)
297
        a1 = public;
298
      if (attr->intent != INTENT_UNKNOWN)
299
        a1 = intent;
300
 
301
      if (a1 != NULL)
302
        {
303
          gfc_error
304
            ("%s attribute not allowed in BLOCK DATA program unit at %L", a1,
305
             where);
306
          return FAILURE;
307
        }
308
    }
309
 
310
  conf (dummy, save);
311
  conf (pointer, target);
312
  conf (pointer, external);
313
  conf (pointer, intrinsic);
314
  conf (pointer, elemental);
315
 
316
  conf (target, external);
317
  conf (target, intrinsic);
318
  conf (external, dimension);   /* See Fortran 95's R504.  */
319
 
320
  conf (external, intrinsic);
321
 
322
  if (attr->if_source || attr->contained)
323
    {
324
      conf (external, subroutine);
325
      conf (external, function);
326
    }
327
 
328
  conf (allocatable, pointer);
329
  conf (allocatable, dummy);    /* TODO: Allowed in Fortran 200x.  */
330
  conf (allocatable, function); /* TODO: Allowed in Fortran 200x.  */
331
  conf (allocatable, result);   /* TODO: Allowed in Fortran 200x.  */
332
  conf (elemental, recursive);
333
 
334
  conf (in_common, dummy);
335
  conf (in_common, allocatable);
336
  conf (in_common, result);
337
  conf (in_common, save);
338
  conf (result, save);
339
 
340
  conf (dummy, result);
341
 
342
  conf (in_equivalence, use_assoc);
343
  conf (in_equivalence, dummy);
344
  conf (in_equivalence, target);
345
  conf (in_equivalence, pointer);
346
  conf (in_equivalence, function);
347
  conf (in_equivalence, result);
348
  conf (in_equivalence, entry);
349
  conf (in_equivalence, allocatable);
350
 
351
  conf (in_namelist, pointer);
352
  conf (in_namelist, allocatable);
353
 
354
  conf (entry, result);
355
 
356
  conf (function, subroutine);
357
 
358
  /* Cray pointer/pointee conflicts.  */
359
  conf (cray_pointer, cray_pointee);
360
  conf (cray_pointer, dimension);
361
  conf (cray_pointer, pointer);
362
  conf (cray_pointer, target);
363
  conf (cray_pointer, allocatable);
364
  conf (cray_pointer, external);
365
  conf (cray_pointer, intrinsic);
366
  conf (cray_pointer, in_namelist);
367
  conf (cray_pointer, function);
368
  conf (cray_pointer, subroutine);
369
  conf (cray_pointer, entry);
370
 
371
  conf (cray_pointee, allocatable);
372
  conf (cray_pointee, intent);
373
  conf (cray_pointee, optional);
374
  conf (cray_pointee, dummy);
375
  conf (cray_pointee, target);
376
  conf (cray_pointee, external);
377
  conf (cray_pointee, intrinsic);
378
  conf (cray_pointee, pointer);
379
  conf (cray_pointee, function);
380
  conf (cray_pointee, subroutine);
381
  conf (cray_pointee, entry);
382
  conf (cray_pointee, in_common);
383
  conf (cray_pointee, in_equivalence);
384
 
385
  conf (data, dummy);
386
  conf (data, function);
387
  conf (data, result);
388
  conf (data, allocatable);
389
  conf (data, use_assoc);
390
 
391
  a1 = gfc_code2string (flavors, attr->flavor);
392
 
393
  if (attr->in_namelist
394
      && attr->flavor != FL_VARIABLE
395
      && attr->flavor != FL_UNKNOWN)
396
    {
397
 
398
      a2 = in_namelist;
399
      goto conflict;
400
    }
401
 
402
  switch (attr->flavor)
403
    {
404
    case FL_PROGRAM:
405
    case FL_BLOCK_DATA:
406
    case FL_MODULE:
407
    case FL_LABEL:
408
      conf2 (dummy);
409
      conf2 (save);
410
      conf2 (pointer);
411
      conf2 (target);
412
      conf2 (external);
413
      conf2 (intrinsic);
414
      conf2 (allocatable);
415
      conf2 (result);
416
      conf2 (in_namelist);
417
      conf2 (optional);
418
      conf2 (function);
419
      conf2 (subroutine);
420
      break;
421
 
422
    case FL_VARIABLE:
423
    case FL_NAMELIST:
424
      break;
425
 
426
    case FL_PROCEDURE:
427
      conf2 (intent);
428
 
429
      if (attr->subroutine)
430
        {
431
          conf2(save);
432
          conf2(pointer);
433
          conf2(target);
434
          conf2(allocatable);
435
          conf2(result);
436
          conf2(in_namelist);
437
          conf2(function);
438
        }
439
 
440
      switch (attr->proc)
441
        {
442
        case PROC_ST_FUNCTION:
443
          conf2 (in_common);
444
          conf2 (dummy);
445
          break;
446
 
447
        case PROC_MODULE:
448
          conf2 (dummy);
449
          break;
450
 
451
        case PROC_DUMMY:
452
          conf2 (result);
453
          conf2 (in_common);
454
          conf2 (save);
455
          break;
456
 
457
        default:
458
          break;
459
        }
460
 
461
      break;
462
 
463
    case FL_DERIVED:
464
      conf2 (dummy);
465
      conf2 (save);
466
      conf2 (pointer);
467
      conf2 (target);
468
      conf2 (external);
469
      conf2 (intrinsic);
470
      conf2 (allocatable);
471
      conf2 (optional);
472
      conf2 (entry);
473
      conf2 (function);
474
      conf2 (subroutine);
475
 
476
      if (attr->intent != INTENT_UNKNOWN)
477
        {
478
          a2 = intent;
479
          goto conflict;
480
        }
481
      break;
482
 
483
    case FL_PARAMETER:
484
      conf2 (external);
485
      conf2 (intrinsic);
486
      conf2 (optional);
487
      conf2 (allocatable);
488
      conf2 (function);
489
      conf2 (subroutine);
490
      conf2 (entry);
491
      conf2 (pointer);
492
      conf2 (target);
493
      conf2 (dummy);
494
      conf2 (in_common);
495
      conf2 (save);
496
      break;
497
 
498
    default:
499
      break;
500
    }
501
 
502
  return SUCCESS;
503
 
504
conflict:
505
  if (name == NULL)
506
    gfc_error ("%s attribute conflicts with %s attribute at %L",
507
               a1, a2, where);
508
  else
509
    gfc_error ("%s attribute conflicts with %s attribute in '%s' at %L",
510
               a1, a2, name, where);
511
 
512
  return FAILURE;
513
}
514
 
515
#undef conf
516
#undef conf2
517
 
518
 
519
/* Mark a symbol as referenced.  */
520
 
521
void
522
gfc_set_sym_referenced (gfc_symbol * sym)
523
{
524
  if (sym->attr.referenced)
525
    return;
526
 
527
  sym->attr.referenced = 1;
528
 
529
  /* Remember which order dummy variables are accessed in.  */
530
  if (sym->attr.dummy)
531
    sym->dummy_order = next_dummy_order++;
532
}
533
 
534
 
535
/* Common subroutine called by attribute changing subroutines in order
536
   to prevent them from changing a symbol that has been
537
   use-associated.  Returns zero if it is OK to change the symbol,
538
   nonzero if not.  */
539
 
540
static int
541
check_used (symbol_attribute * attr, const char * name, locus * where)
542
{
543
 
544
  if (attr->use_assoc == 0)
545
    return 0;
546
 
547
  if (where == NULL)
548
    where = &gfc_current_locus;
549
 
550
  if (name == NULL)
551
    gfc_error ("Cannot change attributes of USE-associated symbol at %L",
552
               where);
553
  else
554
    gfc_error ("Cannot change attributes of USE-associated symbol %s at %L",
555
               name, where);
556
 
557
  return 1;
558
}
559
 
560
 
561
/* Used to prevent changing the attributes of a symbol after it has been
562
   used.  This check is only done for dummy variables as only these can be
563
   used in specification expressions.  Applying this to all symbols causes
564
   an error when we reach the body of a contained function.  */
565
 
566
static int
567
check_done (symbol_attribute * attr, locus * where)
568
{
569
 
570
  if (!(attr->dummy && attr->referenced))
571
    return 0;
572
 
573
  if (where == NULL)
574
    where = &gfc_current_locus;
575
 
576
  gfc_error ("Cannot change attributes of symbol at %L"
577
             " after it has been used", where);
578
 
579
  return 1;
580
}
581
 
582
 
583
/* Generate an error because of a duplicate attribute.  */
584
 
585
static void
586
duplicate_attr (const char *attr, locus * where)
587
{
588
 
589
  if (where == NULL)
590
    where = &gfc_current_locus;
591
 
592
  gfc_error ("Duplicate %s attribute specified at %L", attr, where);
593
}
594
 
595
/* Called from decl.c (attr_decl1) to check attributes, when declared separately.  */
596
 
597
try
598
gfc_add_attribute (symbol_attribute * attr, locus * where, uint attr_intent)
599
{
600
 
601
  if (check_used (attr, NULL, where)
602
        || (attr_intent == 0 && check_done (attr, where)))
603
    return FAILURE;
604
 
605
  return check_conflict (attr, NULL, where);
606
}
607
 
608
try
609
gfc_add_allocatable (symbol_attribute * attr, locus * where)
610
{
611
 
612
  if (check_used (attr, NULL, where) || check_done (attr, where))
613
    return FAILURE;
614
 
615
  if (attr->allocatable)
616
    {
617
      duplicate_attr ("ALLOCATABLE", where);
618
      return FAILURE;
619
    }
620
 
621
  attr->allocatable = 1;
622
  return check_conflict (attr, NULL, where);
623
}
624
 
625
 
626
try
627
gfc_add_dimension (symbol_attribute * attr, const char *name, locus * where)
628
{
629
 
630
  if (check_used (attr, name, where) || check_done (attr, where))
631
    return FAILURE;
632
 
633
  if (attr->dimension)
634
    {
635
      duplicate_attr ("DIMENSION", where);
636
      return FAILURE;
637
    }
638
 
639
  attr->dimension = 1;
640
  return check_conflict (attr, name, where);
641
}
642
 
643
 
644
try
645
gfc_add_external (symbol_attribute * attr, locus * where)
646
{
647
 
648
  if (check_used (attr, NULL, where) || check_done (attr, where))
649
    return FAILURE;
650
 
651
  if (attr->external)
652
    {
653
      duplicate_attr ("EXTERNAL", where);
654
      return FAILURE;
655
    }
656
 
657
  attr->external = 1;
658
 
659
  return check_conflict (attr, NULL, where);
660
}
661
 
662
 
663
try
664
gfc_add_intrinsic (symbol_attribute * attr, locus * where)
665
{
666
 
667
  if (check_used (attr, NULL, where) || check_done (attr, where))
668
    return FAILURE;
669
 
670
  if (attr->intrinsic)
671
    {
672
      duplicate_attr ("INTRINSIC", where);
673
      return FAILURE;
674
    }
675
 
676
  attr->intrinsic = 1;
677
 
678
  return check_conflict (attr, NULL, where);
679
}
680
 
681
 
682
try
683
gfc_add_optional (symbol_attribute * attr, locus * where)
684
{
685
 
686
  if (check_used (attr, NULL, where) || check_done (attr, where))
687
    return FAILURE;
688
 
689
  if (attr->optional)
690
    {
691
      duplicate_attr ("OPTIONAL", where);
692
      return FAILURE;
693
    }
694
 
695
  attr->optional = 1;
696
  return check_conflict (attr, NULL, where);
697
}
698
 
699
 
700
try
701
gfc_add_pointer (symbol_attribute * attr, locus * where)
702
{
703
 
704
  if (check_used (attr, NULL, where) || check_done (attr, where))
705
    return FAILURE;
706
 
707
  attr->pointer = 1;
708
  return check_conflict (attr, NULL, where);
709
}
710
 
711
 
712
try
713
gfc_add_cray_pointer (symbol_attribute * attr, locus * where)
714
{
715
 
716
  if (check_used (attr, NULL, where) || check_done (attr, where))
717
    return FAILURE;
718
 
719
  attr->cray_pointer = 1;
720
  return check_conflict (attr, NULL, where);
721
}
722
 
723
 
724
try
725
gfc_add_cray_pointee (symbol_attribute * attr, locus * where)
726
{
727
 
728
  if (check_used (attr, NULL, where) || check_done (attr, where))
729
    return FAILURE;
730
 
731
  if (attr->cray_pointee)
732
    {
733
      gfc_error ("Cray Pointee at %L appears in multiple pointer()"
734
                 " statements.", where);
735
      return FAILURE;
736
    }
737
 
738
  attr->cray_pointee = 1;
739
  return check_conflict (attr, NULL, where);
740
}
741
 
742
 
743
try
744
gfc_add_result (symbol_attribute * attr, const char *name, locus * where)
745
{
746
 
747
  if (check_used (attr, name, where) || check_done (attr, where))
748
    return FAILURE;
749
 
750
  attr->result = 1;
751
  return check_conflict (attr, name, where);
752
}
753
 
754
 
755
try
756
gfc_add_save (symbol_attribute * attr, const char *name, locus * where)
757
{
758
 
759
  if (check_used (attr, name, where))
760
    return FAILURE;
761
 
762
  if (gfc_pure (NULL))
763
    {
764
      gfc_error
765
        ("SAVE attribute at %L cannot be specified in a PURE procedure",
766
         where);
767
      return FAILURE;
768
    }
769
 
770
  if (attr->save)
771
    {
772
        if (gfc_notify_std (GFC_STD_LEGACY,
773
                            "Duplicate SAVE attribute specified at %L",
774
                            where)
775
            == FAILURE)
776
          return FAILURE;
777
    }
778
 
779
  attr->save = 1;
780
  return check_conflict (attr, name, where);
781
}
782
 
783
 
784
try
785
gfc_add_target (symbol_attribute * attr, locus * where)
786
{
787
 
788
  if (check_used (attr, NULL, where) || check_done (attr, where))
789
    return FAILURE;
790
 
791
  if (attr->target)
792
    {
793
      duplicate_attr ("TARGET", where);
794
      return FAILURE;
795
    }
796
 
797
  attr->target = 1;
798
  return check_conflict (attr, NULL, where);
799
}
800
 
801
 
802
try
803
gfc_add_dummy (symbol_attribute * attr, const char *name, locus * where)
804
{
805
 
806
  if (check_used (attr, name, where))
807
    return FAILURE;
808
 
809
  /* Duplicate dummy arguments are allowed due to ENTRY statements.  */
810
  attr->dummy = 1;
811
  return check_conflict (attr, name, where);
812
}
813
 
814
 
815
try
816
gfc_add_in_common (symbol_attribute * attr, const char *name, locus * where)
817
{
818
 
819
  if (check_used (attr, name, where) || check_done (attr, where))
820
    return FAILURE;
821
 
822
  /* Duplicate attribute already checked for.  */
823
  attr->in_common = 1;
824
  if (check_conflict (attr, name, where) == FAILURE)
825
    return FAILURE;
826
 
827
  if (attr->flavor == FL_VARIABLE)
828
    return SUCCESS;
829
 
830
  return gfc_add_flavor (attr, FL_VARIABLE, name, where);
831
}
832
 
833
try
834
gfc_add_in_equivalence (symbol_attribute * attr, const char *name, locus * where)
835
{
836
 
837
  /* Duplicate attribute already checked for.  */
838
  attr->in_equivalence = 1;
839
  if (check_conflict (attr, name, where) == FAILURE)
840
    return FAILURE;
841
 
842
  if (attr->flavor == FL_VARIABLE)
843
    return SUCCESS;
844
 
845
  return gfc_add_flavor (attr, FL_VARIABLE, name, where);
846
}
847
 
848
 
849
try
850
gfc_add_data (symbol_attribute *attr, const char *name, locus *where)
851
{
852
 
853
  if (check_used (attr, name, where))
854
    return FAILURE;
855
 
856
  attr->data = 1;
857
  return check_conflict (attr, name, where);
858
}
859
 
860
 
861
try
862
gfc_add_in_namelist (symbol_attribute * attr, const char *name,
863
                     locus * where)
864
{
865
 
866
  attr->in_namelist = 1;
867
  return check_conflict (attr, name, where);
868
}
869
 
870
 
871
try
872
gfc_add_sequence (symbol_attribute * attr, const char *name, locus * where)
873
{
874
 
875
  if (check_used (attr, name, where))
876
    return FAILURE;
877
 
878
  attr->sequence = 1;
879
  return check_conflict (attr, name, where);
880
}
881
 
882
 
883
try
884
gfc_add_elemental (symbol_attribute * attr, locus * where)
885
{
886
 
887
  if (check_used (attr, NULL, where) || check_done (attr, where))
888
    return FAILURE;
889
 
890
  attr->elemental = 1;
891
  return check_conflict (attr, NULL, where);
892
}
893
 
894
 
895
try
896
gfc_add_pure (symbol_attribute * attr, locus * where)
897
{
898
 
899
  if (check_used (attr, NULL, where) || check_done (attr, where))
900
    return FAILURE;
901
 
902
  attr->pure = 1;
903
  return check_conflict (attr, NULL, where);
904
}
905
 
906
 
907
try
908
gfc_add_recursive (symbol_attribute * attr, locus * where)
909
{
910
 
911
  if (check_used (attr, NULL, where) || check_done (attr, where))
912
    return FAILURE;
913
 
914
  attr->recursive = 1;
915
  return check_conflict (attr, NULL, where);
916
}
917
 
918
 
919
try
920
gfc_add_entry (symbol_attribute * attr, const char *name, locus * where)
921
{
922
 
923
  if (check_used (attr, name, where))
924
    return FAILURE;
925
 
926
  if (attr->entry)
927
    {
928
      duplicate_attr ("ENTRY", where);
929
      return FAILURE;
930
    }
931
 
932
  attr->entry = 1;
933
  return check_conflict (attr, name, where);
934
}
935
 
936
 
937
try
938
gfc_add_function (symbol_attribute * attr, const char *name, locus * where)
939
{
940
 
941
  if (attr->flavor != FL_PROCEDURE
942
      && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
943
    return FAILURE;
944
 
945
  attr->function = 1;
946
  return check_conflict (attr, name, where);
947
}
948
 
949
 
950
try
951
gfc_add_subroutine (symbol_attribute * attr, const char *name, locus * where)
952
{
953
 
954
  if (attr->flavor != FL_PROCEDURE
955
      && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
956
    return FAILURE;
957
 
958
  attr->subroutine = 1;
959
  return check_conflict (attr, name, where);
960
}
961
 
962
 
963
try
964
gfc_add_generic (symbol_attribute * attr, const char *name, locus * where)
965
{
966
 
967
  if (attr->flavor != FL_PROCEDURE
968
      && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
969
    return FAILURE;
970
 
971
  attr->generic = 1;
972
  return check_conflict (attr, name, where);
973
}
974
 
975
 
976
/* Flavors are special because some flavors are not what Fortran
977
   considers attributes and can be reaffirmed multiple times.  */
978
 
979
try
980
gfc_add_flavor (symbol_attribute * attr, sym_flavor f, const char *name,
981
                locus * where)
982
{
983
 
984
  if ((f == FL_PROGRAM || f == FL_BLOCK_DATA || f == FL_MODULE
985
       || f == FL_PARAMETER || f == FL_LABEL || f == FL_DERIVED
986
       || f == FL_NAMELIST) && check_used (attr, name, where))
987
    return FAILURE;
988
 
989
  if (attr->flavor == f && f == FL_VARIABLE)
990
    return SUCCESS;
991
 
992
  if (attr->flavor != FL_UNKNOWN)
993
    {
994
      if (where == NULL)
995
        where = &gfc_current_locus;
996
 
997
      gfc_error ("%s attribute conflicts with %s attribute at %L",
998
                 gfc_code2string (flavors, attr->flavor),
999
                 gfc_code2string (flavors, f), where);
1000
 
1001
      return FAILURE;
1002
    }
1003
 
1004
  attr->flavor = f;
1005
 
1006
  return check_conflict (attr, name, where);
1007
}
1008
 
1009
 
1010
try
1011
gfc_add_procedure (symbol_attribute * attr, procedure_type t,
1012
                   const char *name, locus * where)
1013
{
1014
 
1015
  if (check_used (attr, name, where) || check_done (attr, where))
1016
    return FAILURE;
1017
 
1018
  if (attr->flavor != FL_PROCEDURE
1019
      && gfc_add_flavor (attr, FL_PROCEDURE, name, where) == FAILURE)
1020
    return FAILURE;
1021
 
1022
  if (where == NULL)
1023
    where = &gfc_current_locus;
1024
 
1025
  if (attr->proc != PROC_UNKNOWN)
1026
    {
1027
      gfc_error ("%s procedure at %L is already declared as %s procedure",
1028
                 gfc_code2string (procedures, t), where,
1029
                 gfc_code2string (procedures, attr->proc));
1030
 
1031
      return FAILURE;
1032
    }
1033
 
1034
  attr->proc = t;
1035
 
1036
  /* Statement functions are always scalar and functions.  */
1037
  if (t == PROC_ST_FUNCTION
1038
      && ((!attr->function && gfc_add_function (attr, name, where) == FAILURE)
1039
          || attr->dimension))
1040
    return FAILURE;
1041
 
1042
  return check_conflict (attr, name, where);
1043
}
1044
 
1045
 
1046
try
1047
gfc_add_intent (symbol_attribute * attr, sym_intent intent, locus * where)
1048
{
1049
 
1050
  if (check_used (attr, NULL, where))
1051
    return FAILURE;
1052
 
1053
  if (attr->intent == INTENT_UNKNOWN)
1054
    {
1055
      attr->intent = intent;
1056
      return check_conflict (attr, NULL, where);
1057
    }
1058
 
1059
  if (where == NULL)
1060
    where = &gfc_current_locus;
1061
 
1062
  gfc_error ("INTENT (%s) conflicts with INTENT(%s) at %L",
1063
             gfc_intent_string (attr->intent),
1064
             gfc_intent_string (intent), where);
1065
 
1066
  return FAILURE;
1067
}
1068
 
1069
 
1070
/* No checks for use-association in public and private statements.  */
1071
 
1072
try
1073
gfc_add_access (symbol_attribute * attr, gfc_access access,
1074
                const char *name, locus * where)
1075
{
1076
 
1077
  if (attr->access == ACCESS_UNKNOWN)
1078
    {
1079
      attr->access = access;
1080
      return check_conflict (attr, name, where);
1081
    }
1082
 
1083
  if (where == NULL)
1084
    where = &gfc_current_locus;
1085
  gfc_error ("ACCESS specification at %L was already specified", where);
1086
 
1087
  return FAILURE;
1088
}
1089
 
1090
 
1091
try
1092
gfc_add_explicit_interface (gfc_symbol * sym, ifsrc source,
1093
                            gfc_formal_arglist * formal, locus * where)
1094
{
1095
 
1096
  if (check_used (&sym->attr, sym->name, where))
1097
    return FAILURE;
1098
 
1099
  if (where == NULL)
1100
    where = &gfc_current_locus;
1101
 
1102
  if (sym->attr.if_source != IFSRC_UNKNOWN
1103
      && sym->attr.if_source != IFSRC_DECL)
1104
    {
1105
      gfc_error ("Symbol '%s' at %L already has an explicit interface",
1106
                 sym->name, where);
1107
      return FAILURE;
1108
    }
1109
 
1110
  sym->formal = formal;
1111
  sym->attr.if_source = source;
1112
 
1113
  return SUCCESS;
1114
}
1115
 
1116
 
1117
/* Add a type to a symbol.  */
1118
 
1119
try
1120
gfc_add_type (gfc_symbol * sym, gfc_typespec * ts, locus * where)
1121
{
1122
  sym_flavor flavor;
1123
 
1124
/* TODO: This is legal if it is reaffirming an implicit type.
1125
  if (check_done (&sym->attr, where))
1126
    return FAILURE;*/
1127
 
1128
  if (where == NULL)
1129
    where = &gfc_current_locus;
1130
 
1131
  if (sym->ts.type != BT_UNKNOWN)
1132
    {
1133
      gfc_error ("Symbol '%s' at %L already has basic type of %s", sym->name,
1134
                 where, gfc_basic_typename (sym->ts.type));
1135
      return FAILURE;
1136
    }
1137
 
1138
  flavor = sym->attr.flavor;
1139
 
1140
  if (flavor == FL_PROGRAM || flavor == FL_BLOCK_DATA || flavor == FL_MODULE
1141
      || flavor == FL_LABEL || (flavor == FL_PROCEDURE
1142
                                && sym->attr.subroutine)
1143
      || flavor == FL_DERIVED || flavor == FL_NAMELIST)
1144
    {
1145
      gfc_error ("Symbol '%s' at %L cannot have a type", sym->name, where);
1146
      return FAILURE;
1147
    }
1148
 
1149
  sym->ts = *ts;
1150
  return SUCCESS;
1151
}
1152
 
1153
 
1154
/* Clears all attributes.  */
1155
 
1156
void
1157
gfc_clear_attr (symbol_attribute * attr)
1158
{
1159
  memset (attr, 0, sizeof(symbol_attribute));
1160
}
1161
 
1162
 
1163
/* Check for missing attributes in the new symbol.  Currently does
1164
   nothing, but it's not clear that it is unnecessary yet.  */
1165
 
1166
try
1167
gfc_missing_attr (symbol_attribute * attr ATTRIBUTE_UNUSED,
1168
                  locus * where ATTRIBUTE_UNUSED)
1169
{
1170
 
1171
  return SUCCESS;
1172
}
1173
 
1174
 
1175
/* Copy an attribute to a symbol attribute, bit by bit.  Some
1176
   attributes have a lot of side-effects but cannot be present given
1177
   where we are called from, so we ignore some bits.  */
1178
 
1179
try
1180
gfc_copy_attr (symbol_attribute * dest, symbol_attribute * src, locus * where)
1181
{
1182
 
1183
  if (src->allocatable && gfc_add_allocatable (dest, where) == FAILURE)
1184
    goto fail;
1185
 
1186
  if (src->dimension && gfc_add_dimension (dest, NULL, where) == FAILURE)
1187
    goto fail;
1188
  if (src->optional && gfc_add_optional (dest, where) == FAILURE)
1189
    goto fail;
1190
  if (src->pointer && gfc_add_pointer (dest, where) == FAILURE)
1191
    goto fail;
1192
  if (src->save && gfc_add_save (dest, NULL, where) == FAILURE)
1193
    goto fail;
1194
  if (src->target && gfc_add_target (dest, where) == FAILURE)
1195
    goto fail;
1196
  if (src->dummy && gfc_add_dummy (dest, NULL, where) == FAILURE)
1197
    goto fail;
1198
  if (src->result && gfc_add_result (dest, NULL, where) == FAILURE)
1199
    goto fail;
1200
  if (src->entry)
1201
    dest->entry = 1;
1202
 
1203
  if (src->in_namelist && gfc_add_in_namelist (dest, NULL, where) == FAILURE)
1204
    goto fail;
1205
 
1206
  if (src->in_common && gfc_add_in_common (dest, NULL, where) == FAILURE)
1207
    goto fail;
1208
 
1209
  if (src->generic && gfc_add_generic (dest, NULL, where) == FAILURE)
1210
    goto fail;
1211
  if (src->function && gfc_add_function (dest, NULL, where) == FAILURE)
1212
    goto fail;
1213
  if (src->subroutine && gfc_add_subroutine (dest, NULL, where) == FAILURE)
1214
    goto fail;
1215
 
1216
  if (src->sequence && gfc_add_sequence (dest, NULL, where) == FAILURE)
1217
    goto fail;
1218
  if (src->elemental && gfc_add_elemental (dest, where) == FAILURE)
1219
    goto fail;
1220
  if (src->pure && gfc_add_pure (dest, where) == FAILURE)
1221
    goto fail;
1222
  if (src->recursive && gfc_add_recursive (dest, where) == FAILURE)
1223
    goto fail;
1224
 
1225
  if (src->flavor != FL_UNKNOWN
1226
      && gfc_add_flavor (dest, src->flavor, NULL, where) == FAILURE)
1227
    goto fail;
1228
 
1229
  if (src->intent != INTENT_UNKNOWN
1230
      && gfc_add_intent (dest, src->intent, where) == FAILURE)
1231
    goto fail;
1232
 
1233
  if (src->access != ACCESS_UNKNOWN
1234
      && gfc_add_access (dest, src->access, NULL, where) == FAILURE)
1235
    goto fail;
1236
 
1237
  if (gfc_missing_attr (dest, where) == FAILURE)
1238
    goto fail;
1239
 
1240
  if (src->cray_pointer && gfc_add_cray_pointer (dest, where) == FAILURE)
1241
    goto fail;
1242
  if (src->cray_pointee && gfc_add_cray_pointee (dest, where) == FAILURE)
1243
    goto fail;
1244
 
1245
  /* The subroutines that set these bits also cause flavors to be set,
1246
     and that has already happened in the original, so don't let it
1247
     happen again.  */
1248
  if (src->external)
1249
    dest->external = 1;
1250
  if (src->intrinsic)
1251
    dest->intrinsic = 1;
1252
 
1253
  return SUCCESS;
1254
 
1255
fail:
1256
  return FAILURE;
1257
}
1258
 
1259
 
1260
/************** Component name management ************/
1261
 
1262
/* Component names of a derived type form their own little namespaces
1263
   that are separate from all other spaces.  The space is composed of
1264
   a singly linked list of gfc_component structures whose head is
1265
   located in the parent symbol.  */
1266
 
1267
 
1268
/* Add a component name to a symbol.  The call fails if the name is
1269
   already present.  On success, the component pointer is modified to
1270
   point to the additional component structure.  */
1271
 
1272
try
1273
gfc_add_component (gfc_symbol * sym, const char *name, gfc_component ** component)
1274
{
1275
  gfc_component *p, *tail;
1276
 
1277
  tail = NULL;
1278
 
1279
  for (p = sym->components; p; p = p->next)
1280
    {
1281
      if (strcmp (p->name, name) == 0)
1282
        {
1283
          gfc_error ("Component '%s' at %C already declared at %L",
1284
                     name, &p->loc);
1285
          return FAILURE;
1286
        }
1287
 
1288
      tail = p;
1289
    }
1290
 
1291
  /* Allocate a new component.  */
1292
  p = gfc_get_component ();
1293
 
1294
  if (tail == NULL)
1295
    sym->components = p;
1296
  else
1297
    tail->next = p;
1298
 
1299
  p->name = gfc_get_string (name);
1300
  p->loc = gfc_current_locus;
1301
 
1302
  *component = p;
1303
  return SUCCESS;
1304
}
1305
 
1306
 
1307
/* Recursive function to switch derived types of all symbol in a
1308
   namespace.  */
1309
 
1310
static void
1311
switch_types (gfc_symtree * st, gfc_symbol * from, gfc_symbol * to)
1312
{
1313
  gfc_symbol *sym;
1314
 
1315
  if (st == NULL)
1316
    return;
1317
 
1318
  sym = st->n.sym;
1319
  if (sym->ts.type == BT_DERIVED && sym->ts.derived == from)
1320
    sym->ts.derived = to;
1321
 
1322
  switch_types (st->left, from, to);
1323
  switch_types (st->right, from, to);
1324
}
1325
 
1326
 
1327
/* This subroutine is called when a derived type is used in order to
1328
   make the final determination about which version to use.  The
1329
   standard requires that a type be defined before it is 'used', but
1330
   such types can appear in IMPLICIT statements before the actual
1331
   definition.  'Using' in this context means declaring a variable to
1332
   be that type or using the type constructor.
1333
 
1334
   If a type is used and the components haven't been defined, then we
1335
   have to have a derived type in a parent unit.  We find the node in
1336
   the other namespace and point the symtree node in this namespace to
1337
   that node.  Further reference to this name point to the correct
1338
   node.  If we can't find the node in a parent namespace, then we have
1339
   an error.
1340
 
1341
   This subroutine takes a pointer to a symbol node and returns a
1342
   pointer to the translated node or NULL for an error.  Usually there
1343
   is no translation and we return the node we were passed.  */
1344
 
1345
gfc_symbol *
1346
gfc_use_derived (gfc_symbol * sym)
1347
{
1348
  gfc_symbol *s;
1349
  gfc_typespec *t;
1350
  gfc_symtree *st;
1351
  int i;
1352
 
1353
  if (sym->components != NULL)
1354
    return sym;               /* Already defined.  */
1355
 
1356
  if (sym->ns->parent == NULL)
1357
    goto bad;
1358
 
1359
  if (gfc_find_symbol (sym->name, sym->ns->parent, 1, &s))
1360
    {
1361
      gfc_error ("Symbol '%s' at %C is ambiguous", sym->name);
1362
      return NULL;
1363
    }
1364
 
1365
  if (s == NULL || s->attr.flavor != FL_DERIVED)
1366
    goto bad;
1367
 
1368
  /* Get rid of symbol sym, translating all references to s.  */
1369
  for (i = 0; i < GFC_LETTERS; i++)
1370
    {
1371
      t = &sym->ns->default_type[i];
1372
      if (t->derived == sym)
1373
        t->derived = s;
1374
    }
1375
 
1376
  st = gfc_find_symtree (sym->ns->sym_root, sym->name);
1377
  st->n.sym = s;
1378
 
1379
  s->refs++;
1380
 
1381
  /* Unlink from list of modified symbols.  */
1382
  gfc_commit_symbol (sym);
1383
 
1384
  switch_types (sym->ns->sym_root, sym, s);
1385
 
1386
  /* TODO: Also have to replace sym -> s in other lists like
1387
     namelists, common lists and interface lists.  */
1388
  gfc_free_symbol (sym);
1389
 
1390
  return s;
1391
 
1392
bad:
1393
  gfc_error ("Derived type '%s' at %C is being used before it is defined",
1394
             sym->name);
1395
  return NULL;
1396
}
1397
 
1398
 
1399
/* Given a derived type node and a component name, try to locate the
1400
   component structure.  Returns the NULL pointer if the component is
1401
   not found or the components are private.  */
1402
 
1403
gfc_component *
1404
gfc_find_component (gfc_symbol * sym, const char *name)
1405
{
1406
  gfc_component *p;
1407
 
1408
  if (name == NULL)
1409
    return NULL;
1410
 
1411
  sym = gfc_use_derived (sym);
1412
 
1413
  if (sym == NULL)
1414
    return NULL;
1415
 
1416
  for (p = sym->components; p; p = p->next)
1417
    if (strcmp (p->name, name) == 0)
1418
      break;
1419
 
1420
  if (p == NULL)
1421
    gfc_error ("'%s' at %C is not a member of the '%s' structure",
1422
               name, sym->name);
1423
  else
1424
    {
1425
      if (sym->attr.use_assoc && sym->component_access == ACCESS_PRIVATE)
1426
        {
1427
          gfc_error ("Component '%s' at %C is a PRIVATE component of '%s'",
1428
                     name, sym->name);
1429
          p = NULL;
1430
        }
1431
    }
1432
 
1433
  return p;
1434
}
1435
 
1436
 
1437
/* Given a symbol, free all of the component structures and everything
1438
   they point to.  */
1439
 
1440
static void
1441
free_components (gfc_component * p)
1442
{
1443
  gfc_component *q;
1444
 
1445
  for (; p; p = q)
1446
    {
1447
      q = p->next;
1448
 
1449
      gfc_free_array_spec (p->as);
1450
      gfc_free_expr (p->initializer);
1451
 
1452
      gfc_free (p);
1453
    }
1454
}
1455
 
1456
 
1457
/* Set component attributes from a standard symbol attribute
1458
   structure.  */
1459
 
1460
void
1461
gfc_set_component_attr (gfc_component * c, symbol_attribute * attr)
1462
{
1463
 
1464
  c->dimension = attr->dimension;
1465
  c->pointer = attr->pointer;
1466
}
1467
 
1468
 
1469
/* Get a standard symbol attribute structure given the component
1470
   structure.  */
1471
 
1472
void
1473
gfc_get_component_attr (symbol_attribute * attr, gfc_component * c)
1474
{
1475
 
1476
  gfc_clear_attr (attr);
1477
  attr->dimension = c->dimension;
1478
  attr->pointer = c->pointer;
1479
}
1480
 
1481
 
1482
/******************** Statement label management ********************/
1483
 
1484
/* Free a single gfc_st_label structure, making sure the list is not
1485
   messed up.  This function is called only when some parse error
1486
   occurs.  */
1487
 
1488
void
1489
gfc_free_st_label (gfc_st_label * label)
1490
{
1491
 
1492
  if (label == NULL)
1493
    return;
1494
 
1495
  if (label->prev)
1496
    label->prev->next = label->next;
1497
 
1498
  if (label->next)
1499
    label->next->prev = label->prev;
1500
 
1501
  if (gfc_current_ns->st_labels == label)
1502
    gfc_current_ns->st_labels = label->next;
1503
 
1504
  if (label->format != NULL)
1505
    gfc_free_expr (label->format);
1506
 
1507
  gfc_free (label);
1508
}
1509
 
1510
/* Free a whole list of gfc_st_label structures.  */
1511
 
1512
static void
1513
free_st_labels (gfc_st_label * l1)
1514
{
1515
  gfc_st_label *l2;
1516
 
1517
  for (; l1; l1 = l2)
1518
    {
1519
      l2 = l1->next;
1520
      if (l1->format != NULL)
1521
        gfc_free_expr (l1->format);
1522
      gfc_free (l1);
1523
    }
1524
}
1525
 
1526
 
1527
/* Given a label number, search for and return a pointer to the label
1528
   structure, creating it if it does not exist.  */
1529
 
1530
gfc_st_label *
1531
gfc_get_st_label (int labelno)
1532
{
1533
  gfc_st_label *lp;
1534
 
1535
  /* First see if the label is already in this namespace.  */
1536
  for (lp = gfc_current_ns->st_labels; lp; lp = lp->next)
1537
    if (lp->value == labelno)
1538
      break;
1539
  if (lp != NULL)
1540
    return lp;
1541
 
1542
  lp = gfc_getmem (sizeof (gfc_st_label));
1543
 
1544
  lp->value = labelno;
1545
  lp->defined = ST_LABEL_UNKNOWN;
1546
  lp->referenced = ST_LABEL_UNKNOWN;
1547
 
1548
  lp->prev = NULL;
1549
  lp->next = gfc_current_ns->st_labels;
1550
  if (gfc_current_ns->st_labels)
1551
    gfc_current_ns->st_labels->prev = lp;
1552
  gfc_current_ns->st_labels = lp;
1553
 
1554
  return lp;
1555
}
1556
 
1557
 
1558
/* Called when a statement with a statement label is about to be
1559
   accepted.  We add the label to the list of the current namespace,
1560
   making sure it hasn't been defined previously and referenced
1561
   correctly.  */
1562
 
1563
void
1564
gfc_define_st_label (gfc_st_label * lp, gfc_sl_type type, locus * label_locus)
1565
{
1566
  int labelno;
1567
 
1568
  labelno = lp->value;
1569
 
1570
  if (lp->defined != ST_LABEL_UNKNOWN)
1571
    gfc_error ("Duplicate statement label %d at %L and %L", labelno,
1572
               &lp->where, label_locus);
1573
  else
1574
    {
1575
      lp->where = *label_locus;
1576
 
1577
      switch (type)
1578
        {
1579
        case ST_LABEL_FORMAT:
1580
          if (lp->referenced == ST_LABEL_TARGET)
1581
            gfc_error ("Label %d at %C already referenced as branch target",
1582
                       labelno);
1583
          else
1584
            lp->defined = ST_LABEL_FORMAT;
1585
 
1586
          break;
1587
 
1588
        case ST_LABEL_TARGET:
1589
          if (lp->referenced == ST_LABEL_FORMAT)
1590
            gfc_error ("Label %d at %C already referenced as a format label",
1591
                       labelno);
1592
          else
1593
            lp->defined = ST_LABEL_TARGET;
1594
 
1595
          break;
1596
 
1597
        default:
1598
          lp->defined = ST_LABEL_BAD_TARGET;
1599
          lp->referenced = ST_LABEL_BAD_TARGET;
1600
        }
1601
    }
1602
}
1603
 
1604
 
1605
/* Reference a label.  Given a label and its type, see if that
1606
   reference is consistent with what is known about that label,
1607
   updating the unknown state.  Returns FAILURE if something goes
1608
   wrong.  */
1609
 
1610
try
1611
gfc_reference_st_label (gfc_st_label * lp, gfc_sl_type type)
1612
{
1613
  gfc_sl_type label_type;
1614
  int labelno;
1615
  try rc;
1616
 
1617
  if (lp == NULL)
1618
    return SUCCESS;
1619
 
1620
  labelno = lp->value;
1621
 
1622
  if (lp->defined != ST_LABEL_UNKNOWN)
1623
    label_type = lp->defined;
1624
  else
1625
    {
1626
      label_type = lp->referenced;
1627
      lp->where = gfc_current_locus;
1628
    }
1629
 
1630
  if (label_type == ST_LABEL_FORMAT && type == ST_LABEL_TARGET)
1631
    {
1632
      gfc_error ("Label %d at %C previously used as a FORMAT label", labelno);
1633
      rc = FAILURE;
1634
      goto done;
1635
    }
1636
 
1637
  if ((label_type == ST_LABEL_TARGET || label_type == ST_LABEL_BAD_TARGET)
1638
      && type == ST_LABEL_FORMAT)
1639
    {
1640
      gfc_error ("Label %d at %C previously used as branch target", labelno);
1641
      rc = FAILURE;
1642
      goto done;
1643
    }
1644
 
1645
  lp->referenced = type;
1646
  rc = SUCCESS;
1647
 
1648
done:
1649
  return rc;
1650
}
1651
 
1652
 
1653
/************** Symbol table management subroutines ****************/
1654
 
1655
/* Basic details: Fortran 95 requires a potentially unlimited number
1656
   of distinct namespaces when compiling a program unit.  This case
1657
   occurs during a compilation of internal subprograms because all of
1658
   the internal subprograms must be read before we can start
1659
   generating code for the host.
1660
 
1661
   Given the tricky nature of the Fortran grammar, we must be able to
1662
   undo changes made to a symbol table if the current interpretation
1663
   of a statement is found to be incorrect.  Whenever a symbol is
1664
   looked up, we make a copy of it and link to it.  All of these
1665
   symbols are kept in a singly linked list so that we can commit or
1666
   undo the changes at a later time.
1667
 
1668
   A symtree may point to a symbol node outside of its namespace.  In
1669
   this case, that symbol has been used as a host associated variable
1670
   at some previous time.  */
1671
 
1672
/* Allocate a new namespace structure.  Copies the implicit types from
1673
   PARENT if PARENT_TYPES is set.  */
1674
 
1675
gfc_namespace *
1676
gfc_get_namespace (gfc_namespace * parent, int parent_types)
1677
{
1678
  gfc_namespace *ns;
1679
  gfc_typespec *ts;
1680
  gfc_intrinsic_op in;
1681
  int i;
1682
 
1683
  ns = gfc_getmem (sizeof (gfc_namespace));
1684
  ns->sym_root = NULL;
1685
  ns->uop_root = NULL;
1686
  ns->default_access = ACCESS_UNKNOWN;
1687
  ns->parent = parent;
1688
 
1689
  for (in = GFC_INTRINSIC_BEGIN; in != GFC_INTRINSIC_END; in++)
1690
    ns->operator_access[in] = ACCESS_UNKNOWN;
1691
 
1692
  /* Initialize default implicit types.  */
1693
  for (i = 'a'; i <= 'z'; i++)
1694
    {
1695
      ns->set_flag[i - 'a'] = 0;
1696
      ts = &ns->default_type[i - 'a'];
1697
 
1698
      if (parent_types && ns->parent != NULL)
1699
        {
1700
          /* Copy parent settings */
1701
          *ts = ns->parent->default_type[i - 'a'];
1702
          continue;
1703
        }
1704
 
1705
      if (gfc_option.flag_implicit_none != 0)
1706
        {
1707
          gfc_clear_ts (ts);
1708
          continue;
1709
        }
1710
 
1711
      if ('i' <= i && i <= 'n')
1712
        {
1713
          ts->type = BT_INTEGER;
1714
          ts->kind = gfc_default_integer_kind;
1715
        }
1716
      else
1717
        {
1718
          ts->type = BT_REAL;
1719
          ts->kind = gfc_default_real_kind;
1720
        }
1721
    }
1722
 
1723
  ns->refs = 1;
1724
 
1725
  return ns;
1726
}
1727
 
1728
 
1729
/* Comparison function for symtree nodes.  */
1730
 
1731
static int
1732
compare_symtree (void * _st1, void * _st2)
1733
{
1734
  gfc_symtree *st1, *st2;
1735
 
1736
  st1 = (gfc_symtree *) _st1;
1737
  st2 = (gfc_symtree *) _st2;
1738
 
1739
  return strcmp (st1->name, st2->name);
1740
}
1741
 
1742
 
1743
/* Allocate a new symtree node and associate it with the new symbol.  */
1744
 
1745
gfc_symtree *
1746
gfc_new_symtree (gfc_symtree ** root, const char *name)
1747
{
1748
  gfc_symtree *st;
1749
 
1750
  st = gfc_getmem (sizeof (gfc_symtree));
1751
  st->name = gfc_get_string (name);
1752
 
1753
  gfc_insert_bbt (root, st, compare_symtree);
1754
  return st;
1755
}
1756
 
1757
 
1758
/* Delete a symbol from the tree.  Does not free the symbol itself!  */
1759
 
1760
static void
1761
delete_symtree (gfc_symtree ** root, const char *name)
1762
{
1763
  gfc_symtree st, *st0;
1764
 
1765
  st0 = gfc_find_symtree (*root, name);
1766
 
1767
  st.name = gfc_get_string (name);
1768
  gfc_delete_bbt (root, &st, compare_symtree);
1769
 
1770
  gfc_free (st0);
1771
}
1772
 
1773
 
1774
/* Given a root symtree node and a name, try to find the symbol within
1775
   the namespace.  Returns NULL if the symbol is not found.  */
1776
 
1777
gfc_symtree *
1778
gfc_find_symtree (gfc_symtree * st, const char *name)
1779
{
1780
  int c;
1781
 
1782
  while (st != NULL)
1783
    {
1784
      c = strcmp (name, st->name);
1785
      if (c == 0)
1786
        return st;
1787
 
1788
      st = (c < 0) ? st->left : st->right;
1789
    }
1790
 
1791
  return NULL;
1792
}
1793
 
1794
 
1795
/* Given a name find a user operator node, creating it if it doesn't
1796
   exist.  These are much simpler than symbols because they can't be
1797
   ambiguous with one another.  */
1798
 
1799
gfc_user_op *
1800
gfc_get_uop (const char *name)
1801
{
1802
  gfc_user_op *uop;
1803
  gfc_symtree *st;
1804
 
1805
  st = gfc_find_symtree (gfc_current_ns->uop_root, name);
1806
  if (st != NULL)
1807
    return st->n.uop;
1808
 
1809
  st = gfc_new_symtree (&gfc_current_ns->uop_root, name);
1810
 
1811
  uop = st->n.uop = gfc_getmem (sizeof (gfc_user_op));
1812
  uop->name = gfc_get_string (name);
1813
  uop->access = ACCESS_UNKNOWN;
1814
  uop->ns = gfc_current_ns;
1815
 
1816
  return uop;
1817
}
1818
 
1819
 
1820
/* Given a name find the user operator node.  Returns NULL if it does
1821
   not exist.  */
1822
 
1823
gfc_user_op *
1824
gfc_find_uop (const char *name, gfc_namespace * ns)
1825
{
1826
  gfc_symtree *st;
1827
 
1828
  if (ns == NULL)
1829
    ns = gfc_current_ns;
1830
 
1831
  st = gfc_find_symtree (ns->uop_root, name);
1832
  return (st == NULL) ? NULL : st->n.uop;
1833
}
1834
 
1835
 
1836
/* Remove a gfc_symbol structure and everything it points to.  */
1837
 
1838
void
1839
gfc_free_symbol (gfc_symbol * sym)
1840
{
1841
 
1842
  if (sym == NULL)
1843
    return;
1844
 
1845
  gfc_free_array_spec (sym->as);
1846
 
1847
  free_components (sym->components);
1848
 
1849
  gfc_free_expr (sym->value);
1850
 
1851
  gfc_free_namelist (sym->namelist);
1852
 
1853
  gfc_free_namespace (sym->formal_ns);
1854
 
1855
  gfc_free_interface (sym->generic);
1856
 
1857
  gfc_free_formal_arglist (sym->formal);
1858
 
1859
  gfc_free (sym);
1860
}
1861
 
1862
 
1863
/* Allocate and initialize a new symbol node.  */
1864
 
1865
gfc_symbol *
1866
gfc_new_symbol (const char *name, gfc_namespace * ns)
1867
{
1868
  gfc_symbol *p;
1869
 
1870
  p = gfc_getmem (sizeof (gfc_symbol));
1871
 
1872
  gfc_clear_ts (&p->ts);
1873
  gfc_clear_attr (&p->attr);
1874
  p->ns = ns;
1875
 
1876
  p->declared_at = gfc_current_locus;
1877
 
1878
  if (strlen (name) > GFC_MAX_SYMBOL_LEN)
1879
    gfc_internal_error ("new_symbol(): Symbol name too long");
1880
 
1881
  p->name = gfc_get_string (name);
1882
  return p;
1883
}
1884
 
1885
 
1886
/* Generate an error if a symbol is ambiguous.  */
1887
 
1888
static void
1889
ambiguous_symbol (const char *name, gfc_symtree * st)
1890
{
1891
 
1892
  if (st->n.sym->module)
1893
    gfc_error ("Name '%s' at %C is an ambiguous reference to '%s' "
1894
               "from module '%s'", name, st->n.sym->name, st->n.sym->module);
1895
  else
1896
    gfc_error ("Name '%s' at %C is an ambiguous reference to '%s' "
1897
               "from current program unit", name, st->n.sym->name);
1898
}
1899
 
1900
 
1901
/* Search for a symtree starting in the current namespace, resorting to
1902
   any parent namespaces if requested by a nonzero parent_flag.
1903
   Returns nonzero if the name is ambiguous.  */
1904
 
1905
int
1906
gfc_find_sym_tree (const char *name, gfc_namespace * ns, int parent_flag,
1907
                   gfc_symtree ** result)
1908
{
1909
  gfc_symtree *st;
1910
 
1911
  if (ns == NULL)
1912
    ns = gfc_current_ns;
1913
 
1914
  do
1915
    {
1916
      st = gfc_find_symtree (ns->sym_root, name);
1917
      if (st != NULL)
1918
        {
1919
          *result = st;
1920
          if (st->ambiguous)
1921
            {
1922
              ambiguous_symbol (name, st);
1923
              return 1;
1924
            }
1925
 
1926
          return 0;
1927
        }
1928
 
1929
      if (!parent_flag)
1930
        break;
1931
 
1932
      ns = ns->parent;
1933
    }
1934
  while (ns != NULL);
1935
 
1936
  *result = NULL;
1937
  return 0;
1938
}
1939
 
1940
 
1941
/* Same, but returns the symbol instead.  */
1942
 
1943
int
1944
gfc_find_symbol (const char *name, gfc_namespace * ns, int parent_flag,
1945
                 gfc_symbol ** result)
1946
{
1947
  gfc_symtree *st;
1948
  int i;
1949
 
1950
  i = gfc_find_sym_tree (name, ns, parent_flag, &st);
1951
 
1952
  if (st == NULL)
1953
    *result = NULL;
1954
  else
1955
    *result = st->n.sym;
1956
 
1957
  return i;
1958
}
1959
 
1960
 
1961
/* Save symbol with the information necessary to back it out.  */
1962
 
1963
static void
1964
save_symbol_data (gfc_symbol * sym)
1965
{
1966
 
1967
  if (sym->new || sym->old_symbol != NULL)
1968
    return;
1969
 
1970
  sym->old_symbol = gfc_getmem (sizeof (gfc_symbol));
1971
  *(sym->old_symbol) = *sym;
1972
 
1973
  sym->tlink = changed_syms;
1974
  changed_syms = sym;
1975
}
1976
 
1977
 
1978
/* Given a name, find a symbol, or create it if it does not exist yet
1979
   in the current namespace.  If the symbol is found we make sure that
1980
   it's OK.
1981
 
1982
   The integer return code indicates
1983
 
1984
     1   The symbol name was ambiguous
1985
     2   The name meant to be established was already host associated.
1986
 
1987
   So if the return value is nonzero, then an error was issued.  */
1988
 
1989
int
1990
gfc_get_sym_tree (const char *name, gfc_namespace * ns, gfc_symtree ** result)
1991
{
1992
  gfc_symtree *st;
1993
  gfc_symbol *p;
1994
 
1995
  /* This doesn't usually happen during resolution.  */
1996
  if (ns == NULL)
1997
    ns = gfc_current_ns;
1998
 
1999
  /* Try to find the symbol in ns.  */
2000
  st = gfc_find_symtree (ns->sym_root, name);
2001
 
2002
  if (st == NULL)
2003
    {
2004
      /* If not there, create a new symbol.  */
2005
      p = gfc_new_symbol (name, ns);
2006
 
2007
      /* Add to the list of tentative symbols.  */
2008
      p->old_symbol = NULL;
2009
      p->tlink = changed_syms;
2010
      p->mark = 1;
2011
      p->new = 1;
2012
      changed_syms = p;
2013
 
2014
      st = gfc_new_symtree (&ns->sym_root, name);
2015
      st->n.sym = p;
2016
      p->refs++;
2017
 
2018
    }
2019
  else
2020
    {
2021
      /* Make sure the existing symbol is OK.  */
2022
      if (st->ambiguous)
2023
        {
2024
          ambiguous_symbol (name, st);
2025
          return 1;
2026
        }
2027
 
2028
      p = st->n.sym;
2029
 
2030
      if (p->ns != ns && (!p->attr.function || ns->proc_name != p))
2031
        {
2032
          /* Symbol is from another namespace.  */
2033
          gfc_error ("Symbol '%s' at %C has already been host associated",
2034
                     name);
2035
          return 2;
2036
        }
2037
 
2038
      p->mark = 1;
2039
 
2040
      /* Copy in case this symbol is changed.  */
2041
      save_symbol_data (p);
2042
    }
2043
 
2044
  *result = st;
2045
  return 0;
2046
}
2047
 
2048
 
2049
int
2050
gfc_get_symbol (const char *name, gfc_namespace * ns, gfc_symbol ** result)
2051
{
2052
  gfc_symtree *st;
2053
  int i;
2054
 
2055
 
2056
  i = gfc_get_sym_tree (name, ns, &st);
2057
  if (i != 0)
2058
    return i;
2059
 
2060
  if (st)
2061
    *result = st->n.sym;
2062
  else
2063
    *result = NULL;
2064
  return i;
2065
}
2066
 
2067
 
2068
/* Subroutine that searches for a symbol, creating it if it doesn't
2069
   exist, but tries to host-associate the symbol if possible.  */
2070
 
2071
int
2072
gfc_get_ha_sym_tree (const char *name, gfc_symtree ** result)
2073
{
2074
  gfc_symtree *st;
2075
  int i;
2076
 
2077
  i = gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
2078
  if (st != NULL)
2079
    {
2080
      save_symbol_data (st->n.sym);
2081
 
2082
      *result = st;
2083
      return i;
2084
    }
2085
 
2086
  if (gfc_current_ns->parent != NULL)
2087
    {
2088
      i = gfc_find_sym_tree (name, gfc_current_ns->parent, 1, &st);
2089
      if (i)
2090
        return i;
2091
 
2092
      if (st != NULL)
2093
        {
2094
          *result = st;
2095
          return 0;
2096
        }
2097
    }
2098
 
2099
  return gfc_get_sym_tree (name, gfc_current_ns, result);
2100
}
2101
 
2102
 
2103
int
2104
gfc_get_ha_symbol (const char *name, gfc_symbol ** result)
2105
{
2106
  int i;
2107
  gfc_symtree *st;
2108
 
2109
  i = gfc_get_ha_sym_tree (name, &st);
2110
 
2111
  if (st)
2112
    *result = st->n.sym;
2113
  else
2114
    *result = NULL;
2115
 
2116
  return i;
2117
}
2118
 
2119
/* Return true if both symbols could refer to the same data object.  Does
2120
   not take account of aliasing due to equivalence statements.  */
2121
 
2122
int
2123
gfc_symbols_could_alias (gfc_symbol * lsym, gfc_symbol * rsym)
2124
{
2125
  /* Aliasing isn't possible if the symbols have different base types.  */
2126
  if (gfc_compare_types (&lsym->ts, &rsym->ts) == 0)
2127
    return 0;
2128
 
2129
  /* Pointers can point to other pointers, target objects and allocatable
2130
     objects.  Two allocatable objects cannot share the same storage.  */
2131
  if (lsym->attr.pointer
2132
      && (rsym->attr.pointer || rsym->attr.allocatable || rsym->attr.target))
2133
    return 1;
2134
  if (lsym->attr.target && rsym->attr.pointer)
2135
    return 1;
2136
  if (lsym->attr.allocatable && rsym->attr.pointer)
2137
    return 1;
2138
 
2139
  return 0;
2140
}
2141
 
2142
 
2143
/* Undoes all the changes made to symbols in the current statement.
2144
   This subroutine is made simpler due to the fact that attributes are
2145
   never removed once added.  */
2146
 
2147
void
2148
gfc_undo_symbols (void)
2149
{
2150
  gfc_symbol *p, *q, *old;
2151
 
2152
  for (p = changed_syms; p; p = q)
2153
    {
2154
      q = p->tlink;
2155
 
2156
      if (p->new)
2157
        {
2158
          /* Symbol was new.  */
2159
          delete_symtree (&p->ns->sym_root, p->name);
2160
 
2161
          p->refs--;
2162
          if (p->refs < 0)
2163
            gfc_internal_error ("gfc_undo_symbols(): Negative refs");
2164
          if (p->refs == 0)
2165
            gfc_free_symbol (p);
2166
          continue;
2167
        }
2168
 
2169
      /* Restore previous state of symbol.  Just copy simple stuff.  */
2170
      p->mark = 0;
2171
      old = p->old_symbol;
2172
 
2173
      p->ts.type = old->ts.type;
2174
      p->ts.kind = old->ts.kind;
2175
 
2176
      p->attr = old->attr;
2177
 
2178
      if (p->value != old->value)
2179
        {
2180
          gfc_free_expr (old->value);
2181
          p->value = NULL;
2182
        }
2183
 
2184
      if (p->as != old->as)
2185
        {
2186
          if (p->as)
2187
            gfc_free_array_spec (p->as);
2188
          p->as = old->as;
2189
        }
2190
 
2191
      p->generic = old->generic;
2192
      p->component_access = old->component_access;
2193
 
2194
      if (p->namelist != NULL && old->namelist == NULL)
2195
        {
2196
          gfc_free_namelist (p->namelist);
2197
          p->namelist = NULL;
2198
        }
2199
      else
2200
        {
2201
 
2202
          if (p->namelist_tail != old->namelist_tail)
2203
            {
2204
              gfc_free_namelist (old->namelist_tail);
2205
              old->namelist_tail->next = NULL;
2206
            }
2207
        }
2208
 
2209
      p->namelist_tail = old->namelist_tail;
2210
 
2211
      if (p->formal != old->formal)
2212
        {
2213
          gfc_free_formal_arglist (p->formal);
2214
          p->formal = old->formal;
2215
        }
2216
 
2217
      gfc_free (p->old_symbol);
2218
      p->old_symbol = NULL;
2219
      p->tlink = NULL;
2220
    }
2221
 
2222
  changed_syms = NULL;
2223
}
2224
 
2225
 
2226
/* Free sym->old_symbol. sym->old_symbol is mostly a shallow copy of sym; the
2227
   components of old_symbol that might need deallocation are the "allocatables"
2228
   that are restored in gfc_undo_symbols(), with two exceptions: namelist and
2229
   namelist_tail.  In case these differ between old_symbol and sym, it's just
2230
   because sym->namelist has gotten a few more items.  */
2231
 
2232
static void
2233
free_old_symbol (gfc_symbol * sym)
2234
{
2235
  if (sym->old_symbol == NULL)
2236
    return;
2237
 
2238
  if (sym->old_symbol->as != sym->as)
2239
    gfc_free_array_spec (sym->old_symbol->as);
2240
 
2241
  if (sym->old_symbol->value != sym->value)
2242
    gfc_free_expr (sym->old_symbol->value);
2243
 
2244
  if (sym->old_symbol->formal != sym->formal)
2245
    gfc_free_formal_arglist (sym->old_symbol->formal);
2246
 
2247
  gfc_free (sym->old_symbol);
2248
  sym->old_symbol = NULL;
2249
}
2250
 
2251
 
2252
/* Makes the changes made in the current statement permanent-- gets
2253
   rid of undo information.  */
2254
 
2255
void
2256
gfc_commit_symbols (void)
2257
{
2258
  gfc_symbol *p, *q;
2259
 
2260
  for (p = changed_syms; p; p = q)
2261
    {
2262
      q = p->tlink;
2263
      p->tlink = NULL;
2264
      p->mark = 0;
2265
      p->new = 0;
2266
 
2267
      free_old_symbol (p);
2268
    }
2269
  changed_syms = NULL;
2270
}
2271
 
2272
 
2273
/* Makes the changes made in one symbol permanent -- gets rid of undo
2274
   information.  */
2275
 
2276
void
2277
gfc_commit_symbol (gfc_symbol * sym)
2278
{
2279
  gfc_symbol *p;
2280
 
2281
  if (changed_syms == sym)
2282
    changed_syms = sym->tlink;
2283
  else
2284
    {
2285
      for (p = changed_syms; p; p = p->tlink)
2286
        if (p->tlink == sym)
2287
          {
2288
            p->tlink = sym->tlink;
2289
            break;
2290
          }
2291
    }
2292
 
2293
  sym->tlink = NULL;
2294
  sym->mark = 0;
2295
  sym->new = 0;
2296
 
2297
  free_old_symbol (sym);
2298
}
2299
 
2300
 
2301
/* Recursive function that deletes an entire tree and all the common
2302
   head structures it points to.  */
2303
 
2304
static void
2305
free_common_tree (gfc_symtree * common_tree)
2306
{
2307
  if (common_tree == NULL)
2308
    return;
2309
 
2310
  free_common_tree (common_tree->left);
2311
  free_common_tree (common_tree->right);
2312
 
2313
  gfc_free (common_tree);
2314
}
2315
 
2316
 
2317
/* Recursive function that deletes an entire tree and all the user
2318
   operator nodes that it contains.  */
2319
 
2320
static void
2321
free_uop_tree (gfc_symtree * uop_tree)
2322
{
2323
 
2324
  if (uop_tree == NULL)
2325
    return;
2326
 
2327
  free_uop_tree (uop_tree->left);
2328
  free_uop_tree (uop_tree->right);
2329
 
2330
  gfc_free_interface (uop_tree->n.uop->operator);
2331
 
2332
  gfc_free (uop_tree->n.uop);
2333
  gfc_free (uop_tree);
2334
}
2335
 
2336
 
2337
/* Recursive function that deletes an entire tree and all the symbols
2338
   that it contains.  */
2339
 
2340
static void
2341
free_sym_tree (gfc_symtree * sym_tree)
2342
{
2343
  gfc_namespace *ns;
2344
  gfc_symbol *sym;
2345
 
2346
  if (sym_tree == NULL)
2347
    return;
2348
 
2349
  free_sym_tree (sym_tree->left);
2350
  free_sym_tree (sym_tree->right);
2351
 
2352
  sym = sym_tree->n.sym;
2353
 
2354
  sym->refs--;
2355
  if (sym->refs < 0)
2356
    gfc_internal_error ("free_sym_tree(): Negative refs");
2357
 
2358
  if (sym->formal_ns != NULL && sym->refs == 1)
2359
    {
2360
      /* As formal_ns contains a reference to sym, delete formal_ns just
2361
         before the deletion of sym.  */
2362
      ns = sym->formal_ns;
2363
      sym->formal_ns = NULL;
2364
      gfc_free_namespace (ns);
2365
    }
2366
  else if (sym->refs == 0)
2367
    {
2368
      /* Go ahead and delete the symbol.  */
2369
      gfc_free_symbol (sym);
2370
    }
2371
 
2372
  gfc_free (sym_tree);
2373
}
2374
 
2375
 
2376
/* Free a derived type list.  */
2377
 
2378
static void
2379
gfc_free_dt_list (gfc_dt_list * dt)
2380
{
2381
  gfc_dt_list *n;
2382
 
2383
  for (; dt; dt = n)
2384
    {
2385
      n = dt->next;
2386
      gfc_free (dt);
2387
    }
2388
}
2389
 
2390
 
2391
/* Free the gfc_equiv_info's.  */
2392
 
2393
static void
2394
gfc_free_equiv_infos (gfc_equiv_info * s)
2395
{
2396
  if (s == NULL)
2397
    return;
2398
  gfc_free_equiv_infos (s->next);
2399
  gfc_free (s);
2400
}
2401
 
2402
 
2403
/* Free the gfc_equiv_lists.  */
2404
 
2405
static void
2406
gfc_free_equiv_lists (gfc_equiv_list * l)
2407
{
2408
  if (l == NULL)
2409
    return;
2410
  gfc_free_equiv_lists (l->next);
2411
  gfc_free_equiv_infos (l->equiv);
2412
  gfc_free (l);
2413
}
2414
 
2415
 
2416
/* Free a namespace structure and everything below it.  Interface
2417
   lists associated with intrinsic operators are not freed.  These are
2418
   taken care of when a specific name is freed.  */
2419
 
2420
void
2421
gfc_free_namespace (gfc_namespace * ns)
2422
{
2423
  gfc_charlen *cl, *cl2;
2424
  gfc_namespace *p, *q;
2425
  gfc_intrinsic_op i;
2426
 
2427
  if (ns == NULL)
2428
    return;
2429
 
2430
  ns->refs--;
2431
  if (ns->refs > 0)
2432
    return;
2433
  gcc_assert (ns->refs == 0);
2434
 
2435
  gfc_free_statements (ns->code);
2436
 
2437
  free_sym_tree (ns->sym_root);
2438
  free_uop_tree (ns->uop_root);
2439
  free_common_tree (ns->common_root);
2440
 
2441
  for (cl = ns->cl_list; cl; cl = cl2)
2442
    {
2443
      cl2 = cl->next;
2444
      gfc_free_expr (cl->length);
2445
      gfc_free (cl);
2446
    }
2447
 
2448
  free_st_labels (ns->st_labels);
2449
 
2450
  gfc_free_equiv (ns->equiv);
2451
  gfc_free_equiv_lists (ns->equiv_lists);
2452
 
2453
  gfc_free_dt_list (ns->derived_types);
2454
 
2455
  for (i = GFC_INTRINSIC_BEGIN; i != GFC_INTRINSIC_END; i++)
2456
    gfc_free_interface (ns->operator[i]);
2457
 
2458
  gfc_free_data (ns->data);
2459
  p = ns->contained;
2460
  gfc_free (ns);
2461
 
2462
  /* Recursively free any contained namespaces.  */
2463
  while (p != NULL)
2464
    {
2465
      q = p;
2466
      p = p->sibling;
2467
 
2468
      gfc_free_namespace (q);
2469
    }
2470
}
2471
 
2472
 
2473
void
2474
gfc_symbol_init_2 (void)
2475
{
2476
 
2477
  gfc_current_ns = gfc_get_namespace (NULL, 0);
2478
}
2479
 
2480
 
2481
void
2482
gfc_symbol_done_2 (void)
2483
{
2484
 
2485
  gfc_free_namespace (gfc_current_ns);
2486
  gfc_current_ns = NULL;
2487
}
2488
 
2489
 
2490
/* Clear mark bits from symbol nodes associated with a symtree node.  */
2491
 
2492
static void
2493
clear_sym_mark (gfc_symtree * st)
2494
{
2495
 
2496
  st->n.sym->mark = 0;
2497
}
2498
 
2499
 
2500
/* Recursively traverse the symtree nodes.  */
2501
 
2502
void
2503
gfc_traverse_symtree (gfc_symtree * st, void (*func) (gfc_symtree *))
2504
{
2505
  if (st != NULL)
2506
    {
2507
      (*func) (st);
2508
 
2509
      gfc_traverse_symtree (st->left, func);
2510
      gfc_traverse_symtree (st->right, func);
2511
    }
2512
}
2513
 
2514
 
2515
/* Recursive namespace traversal function.  */
2516
 
2517
static void
2518
traverse_ns (gfc_symtree * st, void (*func) (gfc_symbol *))
2519
{
2520
 
2521
  if (st == NULL)
2522
    return;
2523
 
2524
  if (st->n.sym->mark == 0)
2525
    (*func) (st->n.sym);
2526
  st->n.sym->mark = 1;
2527
 
2528
  traverse_ns (st->left, func);
2529
  traverse_ns (st->right, func);
2530
}
2531
 
2532
 
2533
/* Call a given function for all symbols in the namespace.  We take
2534
   care that each gfc_symbol node is called exactly once.  */
2535
 
2536
void
2537
gfc_traverse_ns (gfc_namespace * ns, void (*func) (gfc_symbol *))
2538
{
2539
 
2540
  gfc_traverse_symtree (ns->sym_root, clear_sym_mark);
2541
 
2542
  traverse_ns (ns->sym_root, func);
2543
}
2544
 
2545
 
2546
/* Return TRUE if the symbol is an automatic variable.  */
2547
static bool
2548
gfc_is_var_automatic (gfc_symbol * sym)
2549
{
2550
  /* Pointer and allocatable variables are never automatic.  */
2551
  if (sym->attr.pointer || sym->attr.allocatable)
2552
    return false;
2553
  /* Check for arrays with non-constant size.  */
2554
  if (sym->attr.dimension && sym->as
2555
      && !gfc_is_compile_time_shape (sym->as))
2556
    return true;
2557
  /* Check for non-constant length character variables.  */
2558
  if (sym->ts.type == BT_CHARACTER
2559
      && sym->ts.cl
2560
      && !gfc_is_constant_expr (sym->ts.cl->length))
2561
    return true;
2562
  return false;
2563
}
2564
 
2565
/* Given a symbol, mark it as SAVEd if it is allowed.  */
2566
 
2567
static void
2568
save_symbol (gfc_symbol * sym)
2569
{
2570
 
2571
  if (sym->attr.use_assoc)
2572
    return;
2573
 
2574
  if (sym->attr.in_common
2575
      || sym->attr.dummy
2576
      || sym->attr.flavor != FL_VARIABLE)
2577
    return;
2578
  /* Automatic objects are not saved.  */
2579
  if (gfc_is_var_automatic (sym))
2580
    return;
2581
  gfc_add_save (&sym->attr, sym->name, &sym->declared_at);
2582
}
2583
 
2584
 
2585
/* Mark those symbols which can be SAVEd as such.  */
2586
 
2587
void
2588
gfc_save_all (gfc_namespace * ns)
2589
{
2590
 
2591
  gfc_traverse_ns (ns, save_symbol);
2592
}
2593
 
2594
 
2595
#ifdef GFC_DEBUG
2596
/* Make sure that no changes to symbols are pending.  */
2597
 
2598
void
2599
gfc_symbol_state(void) {
2600
 
2601
  if (changed_syms != NULL)
2602
    gfc_internal_error("Symbol changes still pending!");
2603
}
2604
#endif
2605
 
2606
 
2607
/************** Global symbol handling ************/
2608
 
2609
 
2610
/* Search a tree for the global symbol.  */
2611
 
2612
gfc_gsymbol *
2613
gfc_find_gsymbol (gfc_gsymbol *symbol, const char *name)
2614
{
2615
  gfc_gsymbol *s;
2616
 
2617
  if (symbol == NULL)
2618
    return NULL;
2619
  if (strcmp (symbol->name, name) == 0)
2620
    return symbol;
2621
 
2622
  s = gfc_find_gsymbol (symbol->left, name);
2623
  if (s != NULL)
2624
    return s;
2625
 
2626
  s = gfc_find_gsymbol (symbol->right, name);
2627
  if (s != NULL)
2628
    return s;
2629
 
2630
  return NULL;
2631
}
2632
 
2633
 
2634
/* Compare two global symbols. Used for managing the BB tree.  */
2635
 
2636
static int
2637
gsym_compare (void * _s1, void * _s2)
2638
{
2639
  gfc_gsymbol *s1, *s2;
2640
 
2641
  s1 = (gfc_gsymbol *)_s1;
2642
  s2 = (gfc_gsymbol *)_s2;
2643
  return strcmp(s1->name, s2->name);
2644
}
2645
 
2646
 
2647
/* Get a global symbol, creating it if it doesn't exist.  */
2648
 
2649
gfc_gsymbol *
2650
gfc_get_gsymbol (const char *name)
2651
{
2652
  gfc_gsymbol *s;
2653
 
2654
  s = gfc_find_gsymbol (gfc_gsym_root, name);
2655
  if (s != NULL)
2656
    return s;
2657
 
2658
  s = gfc_getmem (sizeof (gfc_gsymbol));
2659
  s->type = GSYM_UNKNOWN;
2660
  s->name = gfc_get_string (name);
2661
 
2662
  gfc_insert_bbt (&gfc_gsym_root, s, gsym_compare);
2663
 
2664
  return s;
2665
}

powered by: WebSVN 2.1.0

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