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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [cp/] [name-lookup.c] - Blame information for rev 729

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

Line No. Rev Author Line
1 710 jeremybenn
/* Definitions for C++ name lookup routines.
2
   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
3
   Free Software Foundation, Inc.
4
   Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
5
 
6
This file is part of GCC.
7
 
8
GCC is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 3, or (at your option)
11
any later version.
12
 
13
GCC is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
GNU General Public License 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 "coretypes.h"
25
#include "tm.h"
26
#include "flags.h"
27
#include "tree.h"
28
#include "cp-tree.h"
29
#include "name-lookup.h"
30
#include "timevar.h"
31
#include "diagnostic-core.h"
32
#include "intl.h"
33
#include "debug.h"
34
#include "c-family/c-pragma.h"
35
#include "params.h"
36
#include "pointer-set.h"
37
 
38
/* The bindings for a particular name in a particular scope.  */
39
 
40
struct scope_binding {
41
  tree value;
42
  tree type;
43
};
44
#define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
45
 
46
static cp_binding_level *innermost_nonclass_level (void);
47
static cxx_binding *binding_for_name (cp_binding_level *, tree);
48
static tree push_overloaded_decl (tree, int, bool);
49
static bool lookup_using_namespace (tree, struct scope_binding *, tree,
50
                                    tree, int);
51
static bool qualified_lookup_using_namespace (tree, tree,
52
                                              struct scope_binding *, int);
53
static tree lookup_type_current_level (tree);
54
static tree push_using_directive (tree);
55
static tree lookup_extern_c_fun_in_all_ns (tree);
56
static void diagnose_name_conflict (tree, tree);
57
 
58
/* The :: namespace.  */
59
 
60
tree global_namespace;
61
 
62
/* The name of the anonymous namespace, throughout this translation
63
   unit.  */
64
static GTY(()) tree anonymous_namespace_name;
65
 
66
/* Initialize anonymous_namespace_name if necessary, and return it.  */
67
 
68
static tree
69
get_anonymous_namespace_name (void)
70
{
71
  if (!anonymous_namespace_name)
72
    {
73
      /* The anonymous namespace has to have a unique name
74
         if typeinfo objects are being compared by name.  */
75
      if (! flag_weak || ! SUPPORTS_ONE_ONLY)
76
       anonymous_namespace_name = get_file_function_name ("N");
77
      else
78
       /* The demangler expects anonymous namespaces to be called
79
          something starting with '_GLOBAL__N_'.  */
80
       anonymous_namespace_name = get_identifier ("_GLOBAL__N_1");
81
    }
82
  return anonymous_namespace_name;
83
}
84
 
85
/* Compute the chain index of a binding_entry given the HASH value of its
86
   name and the total COUNT of chains.  COUNT is assumed to be a power
87
   of 2.  */
88
 
89
#define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
90
 
91
/* A free list of "binding_entry"s awaiting for re-use.  */
92
 
93
static GTY((deletable)) binding_entry free_binding_entry = NULL;
94
 
95
/* Create a binding_entry object for (NAME, TYPE).  */
96
 
97
static inline binding_entry
98
binding_entry_make (tree name, tree type)
99
{
100
  binding_entry entry;
101
 
102
  if (free_binding_entry)
103
    {
104
      entry = free_binding_entry;
105
      free_binding_entry = entry->chain;
106
    }
107
  else
108
    entry = ggc_alloc_binding_entry_s ();
109
 
110
  entry->name = name;
111
  entry->type = type;
112
  entry->chain = NULL;
113
 
114
  return entry;
115
}
116
 
117
/* Put ENTRY back on the free list.  */
118
#if 0
119
static inline void
120
binding_entry_free (binding_entry entry)
121
{
122
  entry->name = NULL;
123
  entry->type = NULL;
124
  entry->chain = free_binding_entry;
125
  free_binding_entry = entry;
126
}
127
#endif
128
 
129
/* The datatype used to implement the mapping from names to types at
130
   a given scope.  */
131
struct GTY(()) binding_table_s {
132
  /* Array of chains of "binding_entry"s  */
133
  binding_entry * GTY((length ("%h.chain_count"))) chain;
134
 
135
  /* The number of chains in this table.  This is the length of the
136
     member "chain" considered as an array.  */
137
  size_t chain_count;
138
 
139
  /* Number of "binding_entry"s in this table.  */
140
  size_t entry_count;
141
};
142
 
143
/* Construct TABLE with an initial CHAIN_COUNT.  */
144
 
145
static inline void
146
binding_table_construct (binding_table table, size_t chain_count)
147
{
148
  table->chain_count = chain_count;
149
  table->entry_count = 0;
150
  table->chain = ggc_alloc_cleared_vec_binding_entry (table->chain_count);
151
}
152
 
153
/* Make TABLE's entries ready for reuse.  */
154
#if 0
155
static void
156
binding_table_free (binding_table table)
157
{
158
  size_t i;
159
  size_t count;
160
 
161
  if (table == NULL)
162
    return;
163
 
164
  for (i = 0, count = table->chain_count; i < count; ++i)
165
    {
166
      binding_entry temp = table->chain[i];
167
      while (temp != NULL)
168
        {
169
          binding_entry entry = temp;
170
          temp = entry->chain;
171
          binding_entry_free (entry);
172
        }
173
      table->chain[i] = NULL;
174
    }
175
  table->entry_count = 0;
176
}
177
#endif
178
 
179
/* Allocate a table with CHAIN_COUNT, assumed to be a power of two.  */
180
 
181
static inline binding_table
182
binding_table_new (size_t chain_count)
183
{
184
  binding_table table = ggc_alloc_binding_table_s ();
185
  table->chain = NULL;
186
  binding_table_construct (table, chain_count);
187
  return table;
188
}
189
 
190
/* Expand TABLE to twice its current chain_count.  */
191
 
192
static void
193
binding_table_expand (binding_table table)
194
{
195
  const size_t old_chain_count = table->chain_count;
196
  const size_t old_entry_count = table->entry_count;
197
  const size_t new_chain_count = 2 * old_chain_count;
198
  binding_entry *old_chains = table->chain;
199
  size_t i;
200
 
201
  binding_table_construct (table, new_chain_count);
202
  for (i = 0; i < old_chain_count; ++i)
203
    {
204
      binding_entry entry = old_chains[i];
205
      for (; entry != NULL; entry = old_chains[i])
206
        {
207
          const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
208
          const size_t j = ENTRY_INDEX (hash, new_chain_count);
209
 
210
          old_chains[i] = entry->chain;
211
          entry->chain = table->chain[j];
212
          table->chain[j] = entry;
213
        }
214
    }
215
  table->entry_count = old_entry_count;
216
}
217
 
218
/* Insert a binding for NAME to TYPE into TABLE.  */
219
 
220
static void
221
binding_table_insert (binding_table table, tree name, tree type)
222
{
223
  const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
224
  const size_t i = ENTRY_INDEX (hash, table->chain_count);
225
  binding_entry entry = binding_entry_make (name, type);
226
 
227
  entry->chain = table->chain[i];
228
  table->chain[i] = entry;
229
  ++table->entry_count;
230
 
231
  if (3 * table->chain_count < 5 * table->entry_count)
232
    binding_table_expand (table);
233
}
234
 
235
/* Return the binding_entry, if any, that maps NAME.  */
236
 
237
binding_entry
238
binding_table_find (binding_table table, tree name)
239
{
240
  const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
241
  binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
242
 
243
  while (entry != NULL && entry->name != name)
244
    entry = entry->chain;
245
 
246
  return entry;
247
}
248
 
249
/* Apply PROC -- with DATA -- to all entries in TABLE.  */
250
 
251
void
252
binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
253
{
254
  const size_t chain_count = table->chain_count;
255
  size_t i;
256
 
257
  for (i = 0; i < chain_count; ++i)
258
    {
259
      binding_entry entry = table->chain[i];
260
      for (; entry != NULL; entry = entry->chain)
261
        proc (entry, data);
262
    }
263
}
264
 
265
#ifndef ENABLE_SCOPE_CHECKING
266
#  define ENABLE_SCOPE_CHECKING 0
267
#else
268
#  define ENABLE_SCOPE_CHECKING 1
269
#endif
270
 
271
/* A free list of "cxx_binding"s, connected by their PREVIOUS.  */
272
 
273
static GTY((deletable)) cxx_binding *free_bindings;
274
 
275
/* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
276
   field to NULL.  */
277
 
278
static inline void
279
cxx_binding_init (cxx_binding *binding, tree value, tree type)
280
{
281
  binding->value = value;
282
  binding->type = type;
283
  binding->previous = NULL;
284
}
285
 
286
/* (GC)-allocate a binding object with VALUE and TYPE member initialized.  */
287
 
288
static cxx_binding *
289
cxx_binding_make (tree value, tree type)
290
{
291
  cxx_binding *binding;
292
  if (free_bindings)
293
    {
294
      binding = free_bindings;
295
      free_bindings = binding->previous;
296
    }
297
  else
298
    binding = ggc_alloc_cxx_binding ();
299
 
300
  cxx_binding_init (binding, value, type);
301
 
302
  return binding;
303
}
304
 
305
/* Put BINDING back on the free list.  */
306
 
307
static inline void
308
cxx_binding_free (cxx_binding *binding)
309
{
310
  binding->scope = NULL;
311
  binding->previous = free_bindings;
312
  free_bindings = binding;
313
}
314
 
315
/* Create a new binding for NAME (with the indicated VALUE and TYPE
316
   bindings) in the class scope indicated by SCOPE.  */
317
 
318
static cxx_binding *
319
new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
320
{
321
  cp_class_binding *cb;
322
  cxx_binding *binding;
323
 
324
    cb = VEC_safe_push (cp_class_binding, gc, scope->class_shadowed, NULL);
325
 
326
  cb->identifier = name;
327
  cb->base = binding = cxx_binding_make (value, type);
328
  binding->scope = scope;
329
  return binding;
330
}
331
 
332
/* Make DECL the innermost binding for ID.  The LEVEL is the binding
333
   level at which this declaration is being bound.  */
334
 
335
static void
336
push_binding (tree id, tree decl, cp_binding_level* level)
337
{
338
  cxx_binding *binding;
339
 
340
  if (level != class_binding_level)
341
    {
342
      binding = cxx_binding_make (decl, NULL_TREE);
343
      binding->scope = level;
344
    }
345
  else
346
    binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
347
 
348
  /* Now, fill in the binding information.  */
349
  binding->previous = IDENTIFIER_BINDING (id);
350
  INHERITED_VALUE_BINDING_P (binding) = 0;
351
  LOCAL_BINDING_P (binding) = (level != class_binding_level);
352
 
353
  /* And put it on the front of the list of bindings for ID.  */
354
  IDENTIFIER_BINDING (id) = binding;
355
}
356
 
357
/* Remove the binding for DECL which should be the innermost binding
358
   for ID.  */
359
 
360
void
361
pop_binding (tree id, tree decl)
362
{
363
  cxx_binding *binding;
364
 
365
  if (id == NULL_TREE)
366
    /* It's easiest to write the loops that call this function without
367
       checking whether or not the entities involved have names.  We
368
       get here for such an entity.  */
369
    return;
370
 
371
  /* Get the innermost binding for ID.  */
372
  binding = IDENTIFIER_BINDING (id);
373
 
374
  /* The name should be bound.  */
375
  gcc_assert (binding != NULL);
376
 
377
  /* The DECL will be either the ordinary binding or the type
378
     binding for this identifier.  Remove that binding.  */
379
  if (binding->value == decl)
380
    binding->value = NULL_TREE;
381
  else
382
    {
383
      gcc_assert (binding->type == decl);
384
      binding->type = NULL_TREE;
385
    }
386
 
387
  if (!binding->value && !binding->type)
388
    {
389
      /* We're completely done with the innermost binding for this
390
         identifier.  Unhook it from the list of bindings.  */
391
      IDENTIFIER_BINDING (id) = binding->previous;
392
 
393
      /* Add it to the free list.  */
394
      cxx_binding_free (binding);
395
    }
396
}
397
 
398
/* Strip non dependent using declarations.  */
399
 
400
tree
401
strip_using_decl (tree decl)
402
{
403
  while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
404
    decl = USING_DECL_DECLS (decl);
405
  return decl;
406
}
407
 
408
/* BINDING records an existing declaration for a name in the current scope.
409
   But, DECL is another declaration for that same identifier in the
410
   same scope.  This is the `struct stat' hack whereby a non-typedef
411
   class name or enum-name can be bound at the same level as some other
412
   kind of entity.
413
   3.3.7/1
414
 
415
     A class name (9.1) or enumeration name (7.2) can be hidden by the
416
     name of an object, function, or enumerator declared in the same scope.
417
     If a class or enumeration name and an object, function, or enumerator
418
     are declared in the same scope (in any order) with the same name, the
419
     class or enumeration name is hidden wherever the object, function, or
420
     enumerator name is visible.
421
 
422
   It's the responsibility of the caller to check that
423
   inserting this name is valid here.  Returns nonzero if the new binding
424
   was successful.  */
425
 
426
static bool
427
supplement_binding_1 (cxx_binding *binding, tree decl)
428
{
429
  tree bval = binding->value;
430
  bool ok = true;
431
  tree target_bval = strip_using_decl (bval);
432
  tree target_decl = strip_using_decl (decl);
433
 
434
  if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl)
435
      && target_decl != target_bval
436
      && (TREE_CODE (target_bval) != TYPE_DECL
437
          /* We allow pushing an enum multiple times in a class
438
             template in order to handle late matching of underlying
439
             type on an opaque-enum-declaration followed by an
440
             enum-specifier.  */
441
          || (TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE
442
              && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE
443
              && (dependent_type_p (ENUM_UNDERLYING_TYPE
444
                                    (TREE_TYPE (target_decl)))
445
                  || dependent_type_p (ENUM_UNDERLYING_TYPE
446
                                       (TREE_TYPE (target_bval)))))))
447
    /* The new name is the type name.  */
448
    binding->type = decl;
449
  else if (/* TARGET_BVAL is null when push_class_level_binding moves
450
              an inherited type-binding out of the way to make room
451
              for a new value binding.  */
452
           !target_bval
453
           /* TARGET_BVAL is error_mark_node when TARGET_DECL's name
454
              has been used in a non-class scope prior declaration.
455
              In that case, we should have already issued a
456
              diagnostic; for graceful error recovery purpose, pretend
457
              this was the intended declaration for that name.  */
458
           || target_bval == error_mark_node
459
           /* If TARGET_BVAL is anticipated but has not yet been
460
              declared, pretend it is not there at all.  */
461
           || (TREE_CODE (target_bval) == FUNCTION_DECL
462
               && DECL_ANTICIPATED (target_bval)
463
               && !DECL_HIDDEN_FRIEND_P (target_bval)))
464
    binding->value = decl;
465
  else if (TREE_CODE (target_bval) == TYPE_DECL
466
           && DECL_ARTIFICIAL (target_bval)
467
           && target_decl != target_bval
468
           && (TREE_CODE (target_decl) != TYPE_DECL
469
               || same_type_p (TREE_TYPE (target_decl),
470
                               TREE_TYPE (target_bval))))
471
    {
472
      /* The old binding was a type name.  It was placed in
473
         VALUE field because it was thought, at the point it was
474
         declared, to be the only entity with such a name.  Move the
475
         type name into the type slot; it is now hidden by the new
476
         binding.  */
477
      binding->type = bval;
478
      binding->value = decl;
479
      binding->value_is_inherited = false;
480
    }
481
  else if (TREE_CODE (target_bval) == TYPE_DECL
482
           && TREE_CODE (target_decl) == TYPE_DECL
483
           && DECL_NAME (target_decl) == DECL_NAME (target_bval)
484
           && binding->scope->kind != sk_class
485
           && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval))
486
               /* If either type involves template parameters, we must
487
                  wait until instantiation.  */
488
               || uses_template_parms (TREE_TYPE (target_decl))
489
               || uses_template_parms (TREE_TYPE (target_bval))))
490
    /* We have two typedef-names, both naming the same type to have
491
       the same name.  In general, this is OK because of:
492
 
493
         [dcl.typedef]
494
 
495
         In a given scope, a typedef specifier can be used to redefine
496
         the name of any type declared in that scope to refer to the
497
         type to which it already refers.
498
 
499
       However, in class scopes, this rule does not apply due to the
500
       stricter language in [class.mem] prohibiting redeclarations of
501
       members.  */
502
    ok = false;
503
  /* There can be two block-scope declarations of the same variable,
504
     so long as they are `extern' declarations.  However, there cannot
505
     be two declarations of the same static data member:
506
 
507
       [class.mem]
508
 
509
       A member shall not be declared twice in the
510
       member-specification.  */
511
  else if (TREE_CODE (target_decl) == VAR_DECL
512
           && TREE_CODE (target_bval) == VAR_DECL
513
           && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval)
514
           && !DECL_CLASS_SCOPE_P (target_decl))
515
    {
516
      duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false);
517
      ok = false;
518
    }
519
  else if (TREE_CODE (decl) == NAMESPACE_DECL
520
           && TREE_CODE (bval) == NAMESPACE_DECL
521
           && DECL_NAMESPACE_ALIAS (decl)
522
           && DECL_NAMESPACE_ALIAS (bval)
523
           && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
524
    /* [namespace.alias]
525
 
526
      In a declarative region, a namespace-alias-definition can be
527
      used to redefine a namespace-alias declared in that declarative
528
      region to refer only to the namespace to which it already
529
      refers.  */
530
    ok = false;
531
  else
532
    {
533
      diagnose_name_conflict (decl, bval);
534
      ok = false;
535
    }
536
 
537
  return ok;
538
}
539
 
540
/* Diagnose a name conflict between DECL and BVAL.  */
541
 
542
static void
543
diagnose_name_conflict (tree decl, tree bval)
544
{
545
  if (TREE_CODE (decl) == TREE_CODE (bval)
546
      && (TREE_CODE (decl) != TYPE_DECL
547
          || (DECL_ARTIFICIAL (decl) && DECL_ARTIFICIAL (bval))
548
          || (!DECL_ARTIFICIAL (decl) && !DECL_ARTIFICIAL (bval)))
549
      && !is_overloaded_fn (decl))
550
    error ("redeclaration of %q#D", decl);
551
  else
552
    error ("%q#D conflicts with a previous declaration", decl);
553
 
554
  inform (input_location, "previous declaration %q+#D", bval);
555
}
556
 
557
/* Wrapper for supplement_binding_1.  */
558
 
559
static bool
560
supplement_binding (cxx_binding *binding, tree decl)
561
{
562
  bool ret;
563
  bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
564
  ret = supplement_binding_1 (binding, decl);
565
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
566
  return ret;
567
}
568
 
569
/* Add DECL to the list of things declared in B.  */
570
 
571
static void
572
add_decl_to_level (tree decl, cp_binding_level *b)
573
{
574
  /* We used to record virtual tables as if they were ordinary
575
     variables, but no longer do so.  */
576
  gcc_assert (!(TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl)));
577
 
578
  if (TREE_CODE (decl) == NAMESPACE_DECL
579
      && !DECL_NAMESPACE_ALIAS (decl))
580
    {
581
      DECL_CHAIN (decl) = b->namespaces;
582
      b->namespaces = decl;
583
    }
584
  else
585
    {
586
      /* We build up the list in reverse order, and reverse it later if
587
         necessary.  */
588
      TREE_CHAIN (decl) = b->names;
589
      b->names = decl;
590
 
591
      /* If appropriate, add decl to separate list of statics.  We
592
         include extern variables because they might turn out to be
593
         static later.  It's OK for this list to contain a few false
594
         positives.  */
595
      if (b->kind == sk_namespace)
596
        if ((TREE_CODE (decl) == VAR_DECL
597
             && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
598
            || (TREE_CODE (decl) == FUNCTION_DECL
599
                && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
600
          VEC_safe_push (tree, gc, b->static_decls, decl);
601
    }
602
}
603
 
604
/* Record a decl-node X as belonging to the current lexical scope.
605
   Check for errors (such as an incompatible declaration for the same
606
   name already seen in the same scope).  IS_FRIEND is true if X is
607
   declared as a friend.
608
 
609
   Returns either X or an old decl for the same name.
610
   If an old decl is returned, it may have been smashed
611
   to agree with what X says.  */
612
 
613
static tree
614
pushdecl_maybe_friend_1 (tree x, bool is_friend)
615
{
616
  tree t;
617
  tree name;
618
  int need_new_binding;
619
 
620
  if (x == error_mark_node)
621
    return error_mark_node;
622
 
623
  need_new_binding = 1;
624
 
625
  if (DECL_TEMPLATE_PARM_P (x))
626
    /* Template parameters have no context; they are not X::T even
627
       when declared within a class or namespace.  */
628
    ;
629
  else
630
    {
631
      if (current_function_decl && x != current_function_decl
632
          /* A local declaration for a function doesn't constitute
633
             nesting.  */
634
          && TREE_CODE (x) != FUNCTION_DECL
635
          /* A local declaration for an `extern' variable is in the
636
             scope of the current namespace, not the current
637
             function.  */
638
          && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
639
          /* When parsing the parameter list of a function declarator,
640
             don't set DECL_CONTEXT to an enclosing function.  When we
641
             push the PARM_DECLs in order to process the function body,
642
             current_binding_level->this_entity will be set.  */
643
          && !(TREE_CODE (x) == PARM_DECL
644
               && current_binding_level->kind == sk_function_parms
645
               && current_binding_level->this_entity == NULL)
646
          && !DECL_CONTEXT (x))
647
        DECL_CONTEXT (x) = current_function_decl;
648
 
649
      /* If this is the declaration for a namespace-scope function,
650
         but the declaration itself is in a local scope, mark the
651
         declaration.  */
652
      if (TREE_CODE (x) == FUNCTION_DECL
653
          && DECL_NAMESPACE_SCOPE_P (x)
654
          && current_function_decl
655
          && x != current_function_decl)
656
        DECL_LOCAL_FUNCTION_P (x) = 1;
657
    }
658
 
659
  name = DECL_NAME (x);
660
  if (name)
661
    {
662
      int different_binding_level = 0;
663
 
664
      if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
665
        name = TREE_OPERAND (name, 0);
666
 
667
      /* In case this decl was explicitly namespace-qualified, look it
668
         up in its namespace context.  */
669
      if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
670
        t = namespace_binding (name, DECL_CONTEXT (x));
671
      else
672
        t = lookup_name_innermost_nonclass_level (name);
673
 
674
      /* [basic.link] If there is a visible declaration of an entity
675
         with linkage having the same name and type, ignoring entities
676
         declared outside the innermost enclosing namespace scope, the
677
         block scope declaration declares that same entity and
678
         receives the linkage of the previous declaration.  */
679
      if (! t && current_function_decl && x != current_function_decl
680
          && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
681
          && DECL_EXTERNAL (x))
682
        {
683
          /* Look in block scope.  */
684
          t = innermost_non_namespace_value (name);
685
          /* Or in the innermost namespace.  */
686
          if (! t)
687
            t = namespace_binding (name, DECL_CONTEXT (x));
688
          /* Does it have linkage?  Note that if this isn't a DECL, it's an
689
             OVERLOAD, which is OK.  */
690
          if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
691
            t = NULL_TREE;
692
          if (t)
693
            different_binding_level = 1;
694
        }
695
 
696
      /* If we are declaring a function, and the result of name-lookup
697
         was an OVERLOAD, look for an overloaded instance that is
698
         actually the same as the function we are declaring.  (If
699
         there is one, we have to merge our declaration with the
700
         previous declaration.)  */
701
      if (t && TREE_CODE (t) == OVERLOAD)
702
        {
703
          tree match;
704
 
705
          if (TREE_CODE (x) == FUNCTION_DECL)
706
            for (match = t; match; match = OVL_NEXT (match))
707
              {
708
                if (decls_match (OVL_CURRENT (match), x))
709
                  break;
710
              }
711
          else
712
            /* Just choose one.  */
713
            match = t;
714
 
715
          if (match)
716
            t = OVL_CURRENT (match);
717
          else
718
            t = NULL_TREE;
719
        }
720
 
721
      if (t && t != error_mark_node)
722
        {
723
          if (different_binding_level)
724
            {
725
              if (decls_match (x, t))
726
                /* The standard only says that the local extern
727
                   inherits linkage from the previous decl; in
728
                   particular, default args are not shared.  Add
729
                   the decl into a hash table to make sure only
730
                   the previous decl in this case is seen by the
731
                   middle end.  */
732
                {
733
                  struct cxx_int_tree_map *h;
734
                  void **loc;
735
 
736
                  TREE_PUBLIC (x) = TREE_PUBLIC (t);
737
 
738
                  if (cp_function_chain->extern_decl_map == NULL)
739
                    cp_function_chain->extern_decl_map
740
                      = htab_create_ggc (20, cxx_int_tree_map_hash,
741
                                         cxx_int_tree_map_eq, NULL);
742
 
743
                  h = ggc_alloc_cxx_int_tree_map ();
744
                  h->uid = DECL_UID (x);
745
                  h->to = t;
746
                  loc = htab_find_slot_with_hash
747
                          (cp_function_chain->extern_decl_map, h,
748
                           h->uid, INSERT);
749
                  *(struct cxx_int_tree_map **) loc = h;
750
                }
751
            }
752
          else if (TREE_CODE (t) == PARM_DECL)
753
            {
754
              /* Check for duplicate params.  */
755
              tree d = duplicate_decls (x, t, is_friend);
756
              if (d)
757
                return d;
758
            }
759
          else if ((DECL_EXTERN_C_FUNCTION_P (x)
760
                    || DECL_FUNCTION_TEMPLATE_P (x))
761
                   && is_overloaded_fn (t))
762
            /* Don't do anything just yet.  */;
763
          else if (t == wchar_decl_node)
764
            {
765
              if (! DECL_IN_SYSTEM_HEADER (x))
766
                pedwarn (input_location, OPT_pedantic, "redeclaration of %<wchar_t%> as %qT",
767
                         TREE_TYPE (x));
768
 
769
              /* Throw away the redeclaration.  */
770
              return t;
771
            }
772
          else
773
            {
774
              tree olddecl = duplicate_decls (x, t, is_friend);
775
 
776
              /* If the redeclaration failed, we can stop at this
777
                 point.  */
778
              if (olddecl == error_mark_node)
779
                return error_mark_node;
780
 
781
              if (olddecl)
782
                {
783
                  if (TREE_CODE (t) == TYPE_DECL)
784
                    SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
785
 
786
                  return t;
787
                }
788
              else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
789
                {
790
                  /* A redeclaration of main, but not a duplicate of the
791
                     previous one.
792
 
793
                     [basic.start.main]
794
 
795
                     This function shall not be overloaded.  */
796
                  error ("invalid redeclaration of %q+D", t);
797
                  error ("as %qD", x);
798
                  /* We don't try to push this declaration since that
799
                     causes a crash.  */
800
                  return x;
801
                }
802
            }
803
        }
804
 
805
      /* If x has C linkage-specification, (extern "C"),
806
         lookup its binding, in case it's already bound to an object.
807
         The lookup is done in all namespaces.
808
         If we find an existing binding, make sure it has the same
809
         exception specification as x, otherwise, bail in error [7.5, 7.6].  */
810
      if ((TREE_CODE (x) == FUNCTION_DECL)
811
          && DECL_EXTERN_C_P (x)
812
          /* We should ignore declarations happening in system headers.  */
813
          && !DECL_ARTIFICIAL (x)
814
          && !DECL_IN_SYSTEM_HEADER (x))
815
        {
816
          tree previous = lookup_extern_c_fun_in_all_ns (x);
817
          if (previous
818
              && !DECL_ARTIFICIAL (previous)
819
              && !DECL_IN_SYSTEM_HEADER (previous)
820
              && DECL_CONTEXT (previous) != DECL_CONTEXT (x))
821
            {
822
              /* In case either x or previous is declared to throw an exception,
823
                 make sure both exception specifications are equal.  */
824
              if (decls_match (x, previous))
825
                {
826
                  tree x_exception_spec = NULL_TREE;
827
                  tree previous_exception_spec = NULL_TREE;
828
 
829
                  x_exception_spec =
830
                                TYPE_RAISES_EXCEPTIONS (TREE_TYPE (x));
831
                  previous_exception_spec =
832
                                TYPE_RAISES_EXCEPTIONS (TREE_TYPE (previous));
833
                  if (!comp_except_specs (previous_exception_spec,
834
                                          x_exception_spec,
835
                                          ce_normal))
836
                    {
837
                      pedwarn (input_location, 0,
838
                               "declaration of %q#D with C language linkage",
839
                               x);
840
                      pedwarn (input_location, 0,
841
                               "conflicts with previous declaration %q+#D",
842
                               previous);
843
                      pedwarn (input_location, 0,
844
                               "due to different exception specifications");
845
                      return error_mark_node;
846
                    }
847
                  if (DECL_ASSEMBLER_NAME_SET_P (previous))
848
                    SET_DECL_ASSEMBLER_NAME (x,
849
                                             DECL_ASSEMBLER_NAME (previous));
850
                }
851
              else
852
                {
853
                  pedwarn (input_location, 0,
854
                           "declaration of %q#D with C language linkage", x);
855
                  pedwarn (input_location, 0,
856
                           "conflicts with previous declaration %q+#D",
857
                           previous);
858
                }
859
            }
860
        }
861
 
862
      check_template_shadow (x);
863
 
864
      /* If this is a function conjured up by the back end, massage it
865
         so it looks friendly.  */
866
      if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
867
        {
868
          retrofit_lang_decl (x);
869
          SET_DECL_LANGUAGE (x, lang_c);
870
        }
871
 
872
      t = x;
873
      if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
874
        {
875
          t = push_overloaded_decl (x, PUSH_LOCAL, is_friend);
876
          if (!namespace_bindings_p ())
877
            /* We do not need to create a binding for this name;
878
               push_overloaded_decl will have already done so if
879
               necessary.  */
880
            need_new_binding = 0;
881
        }
882
      else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
883
        {
884
          t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend);
885
          if (t == x)
886
            add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
887
        }
888
 
889
      if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t))
890
        check_default_args (t);
891
 
892
      if (t != x || DECL_FUNCTION_TEMPLATE_P (t))
893
        return t;
894
 
895
      /* If declaring a type as a typedef, copy the type (unless we're
896
         at line 0), and install this TYPE_DECL as the new type's typedef
897
         name.  See the extensive comment of set_underlying_type ().  */
898
      if (TREE_CODE (x) == TYPE_DECL)
899
        {
900
          tree type = TREE_TYPE (x);
901
 
902
          if (DECL_IS_BUILTIN (x)
903
              || (TREE_TYPE (x) != error_mark_node
904
                  && TYPE_NAME (type) != x
905
                  /* We don't want to copy the type when all we're
906
                     doing is making a TYPE_DECL for the purposes of
907
                     inlining.  */
908
                  && (!TYPE_NAME (type)
909
                      || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x))))
910
            set_underlying_type (x);
911
 
912
          if (type != error_mark_node
913
              && TYPE_NAME (type)
914
              && TYPE_IDENTIFIER (type))
915
            set_identifier_type_value (DECL_NAME (x), x);
916
 
917
          /* If this is a locally defined typedef in a function that
918
             is not a template instantation, record it to implement
919
             -Wunused-local-typedefs.  */
920
          if (current_instantiation () == NULL
921
              || (current_instantiation ()->decl != current_function_decl))
922
          record_locally_defined_typedef (x);
923
        }
924
 
925
      /* Multiple external decls of the same identifier ought to match.
926
 
927
         We get warnings about inline functions where they are defined.
928
         We get warnings about other functions from push_overloaded_decl.
929
 
930
         Avoid duplicate warnings where they are used.  */
931
      if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
932
        {
933
          tree decl;
934
 
935
          decl = IDENTIFIER_NAMESPACE_VALUE (name);
936
          if (decl && TREE_CODE (decl) == OVERLOAD)
937
            decl = OVL_FUNCTION (decl);
938
 
939
          if (decl && decl != error_mark_node
940
              && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
941
              /* If different sort of thing, we already gave an error.  */
942
              && TREE_CODE (decl) == TREE_CODE (x)
943
              && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
944
            {
945
              permerror (input_location, "type mismatch with previous external decl of %q#D", x);
946
              permerror (input_location, "previous external decl of %q+#D", decl);
947
            }
948
        }
949
 
950
      if (TREE_CODE (x) == FUNCTION_DECL
951
          && is_friend
952
          && !flag_friend_injection)
953
        {
954
          /* This is a new declaration of a friend function, so hide
955
             it from ordinary function lookup.  */
956
          DECL_ANTICIPATED (x) = 1;
957
          DECL_HIDDEN_FRIEND_P (x) = 1;
958
        }
959
 
960
      /* This name is new in its binding level.
961
         Install the new declaration and return it.  */
962
      if (namespace_bindings_p ())
963
        {
964
          /* Install a global value.  */
965
 
966
          /* If the first global decl has external linkage,
967
             warn if we later see static one.  */
968
          if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
969
            TREE_PUBLIC (name) = 1;
970
 
971
          /* Bind the name for the entity.  */
972
          if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
973
                && t != NULL_TREE)
974
              && (TREE_CODE (x) == TYPE_DECL
975
                  || TREE_CODE (x) == VAR_DECL
976
                  || TREE_CODE (x) == NAMESPACE_DECL
977
                  || TREE_CODE (x) == CONST_DECL
978
                  || TREE_CODE (x) == TEMPLATE_DECL))
979
            SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
980
 
981
          /* If new decl is `static' and an `extern' was seen previously,
982
             warn about it.  */
983
          if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
984
            warn_extern_redeclared_static (x, t);
985
        }
986
      else
987
        {
988
          /* Here to install a non-global value.  */
989
          tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
990
          tree oldlocal = NULL_TREE;
991
          cp_binding_level *oldscope = NULL;
992
          cxx_binding *oldbinding = outer_binding (name, NULL, true);
993
          if (oldbinding)
994
            {
995
              oldlocal = oldbinding->value;
996
              oldscope = oldbinding->scope;
997
            }
998
 
999
          if (need_new_binding)
1000
            {
1001
              push_local_binding (name, x, 0);
1002
              /* Because push_local_binding will hook X on to the
1003
                 current_binding_level's name list, we don't want to
1004
                 do that again below.  */
1005
              need_new_binding = 0;
1006
            }
1007
 
1008
          /* If this is a TYPE_DECL, push it into the type value slot.  */
1009
          if (TREE_CODE (x) == TYPE_DECL)
1010
            set_identifier_type_value (name, x);
1011
 
1012
          /* Clear out any TYPE_DECL shadowed by a namespace so that
1013
             we won't think this is a type.  The C struct hack doesn't
1014
             go through namespaces.  */
1015
          if (TREE_CODE (x) == NAMESPACE_DECL)
1016
            set_identifier_type_value (name, NULL_TREE);
1017
 
1018
          if (oldlocal)
1019
            {
1020
              tree d = oldlocal;
1021
 
1022
              while (oldlocal
1023
                     && TREE_CODE (oldlocal) == VAR_DECL
1024
                     && DECL_DEAD_FOR_LOCAL (oldlocal))
1025
                oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
1026
 
1027
              if (oldlocal == NULL_TREE)
1028
                oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
1029
            }
1030
 
1031
          /* If this is an extern function declaration, see if we
1032
             have a global definition or declaration for the function.  */
1033
          if (oldlocal == NULL_TREE
1034
              && DECL_EXTERNAL (x)
1035
              && oldglobal != NULL_TREE
1036
              && TREE_CODE (x) == FUNCTION_DECL
1037
              && TREE_CODE (oldglobal) == FUNCTION_DECL)
1038
            {
1039
              /* We have one.  Their types must agree.  */
1040
              if (decls_match (x, oldglobal))
1041
                /* OK */;
1042
              else
1043
                {
1044
                  warning (0, "extern declaration of %q#D doesn%'t match", x);
1045
                  warning (0, "global declaration %q+#D", oldglobal);
1046
                }
1047
            }
1048
          /* If we have a local external declaration,
1049
             and no file-scope declaration has yet been seen,
1050
             then if we later have a file-scope decl it must not be static.  */
1051
          if (oldlocal == NULL_TREE
1052
              && oldglobal == NULL_TREE
1053
              && DECL_EXTERNAL (x)
1054
              && TREE_PUBLIC (x))
1055
            TREE_PUBLIC (name) = 1;
1056
 
1057
          /* Don't complain about the parms we push and then pop
1058
             while tentatively parsing a function declarator.  */
1059
          if (TREE_CODE (x) == PARM_DECL && DECL_CONTEXT (x) == NULL_TREE)
1060
            /* Ignore.  */;
1061
 
1062
          /* Warn if shadowing an argument at the top level of the body.  */
1063
          else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
1064
                   /* Inline decls shadow nothing.  */
1065
                   && !DECL_FROM_INLINE (x)
1066
                   && (TREE_CODE (oldlocal) == PARM_DECL
1067
                       || TREE_CODE (oldlocal) == VAR_DECL
1068
                       /* If the old decl is a type decl, only warn if the
1069
                          old decl is an explicit typedef or if both the old
1070
                          and new decls are type decls.  */
1071
                       || (TREE_CODE (oldlocal) == TYPE_DECL
1072
                           && (!DECL_ARTIFICIAL (oldlocal)
1073
                               || TREE_CODE (x) == TYPE_DECL)))
1074
                   /* Don't check for internally generated vars unless
1075
                      it's an implicit typedef (see create_implicit_typedef
1076
                      in decl.c).  */
1077
                   && (!DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x)))
1078
            {
1079
              bool nowarn = false;
1080
 
1081
              /* Don't complain if it's from an enclosing function.  */
1082
              if (DECL_CONTEXT (oldlocal) == current_function_decl
1083
                  && TREE_CODE (x) != PARM_DECL
1084
                  && TREE_CODE (oldlocal) == PARM_DECL)
1085
                {
1086
                  /* Go to where the parms should be and see if we find
1087
                     them there.  */
1088
                  cp_binding_level *b = current_binding_level->level_chain;
1089
 
1090
                  if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
1091
                    /* Skip the ctor/dtor cleanup level.  */
1092
                    b = b->level_chain;
1093
 
1094
                  /* ARM $8.3 */
1095
                  if (b->kind == sk_function_parms)
1096
                    {
1097
                      error ("declaration of %q#D shadows a parameter", x);
1098
                      nowarn = true;
1099
                    }
1100
                }
1101
 
1102
              /* The local structure or class can't use parameters of
1103
                 the containing function anyway.  */
1104
              if (DECL_CONTEXT (oldlocal) != current_function_decl)
1105
                {
1106
                  cp_binding_level *scope = current_binding_level;
1107
                  tree context = DECL_CONTEXT (oldlocal);
1108
                  for (; scope; scope = scope->level_chain)
1109
                   {
1110
                     if (scope->kind == sk_function_parms
1111
                         && scope->this_entity == context)
1112
                      break;
1113
                     if (scope->kind == sk_class
1114
                         && !LAMBDA_TYPE_P (scope->this_entity))
1115
                       {
1116
                         nowarn = true;
1117
                         break;
1118
                       }
1119
                   }
1120
                }
1121
              /* Error if redeclaring a local declared in a
1122
                 for-init-statement or in the condition of an if or
1123
                 switch statement when the new declaration is in the
1124
                 outermost block of the controlled statement.
1125
                 Redeclaring a variable from a for or while condition is
1126
                 detected elsewhere.  */
1127
              else if (TREE_CODE (oldlocal) == VAR_DECL
1128
                       && oldscope == current_binding_level->level_chain
1129
                       && (oldscope->kind == sk_cond
1130
                           || oldscope->kind == sk_for))
1131
                {
1132
                  error ("redeclaration of %q#D", x);
1133
                  error ("%q+#D previously declared here", oldlocal);
1134
                }
1135
 
1136
              if (warn_shadow && !nowarn)
1137
                {
1138
                  if (TREE_CODE (oldlocal) == PARM_DECL)
1139
                    warning_at (input_location, OPT_Wshadow,
1140
                                "declaration of %q#D shadows a parameter", x);
1141
                  else if (is_capture_proxy (oldlocal))
1142
                    warning_at (input_location, OPT_Wshadow,
1143
                                "declaration of %qD shadows a lambda capture",
1144
                                x);
1145
                  else
1146
                    warning_at (input_location, OPT_Wshadow,
1147
                                "declaration of %qD shadows a previous local",
1148
                                x);
1149
                   warning_at (DECL_SOURCE_LOCATION (oldlocal), OPT_Wshadow,
1150
                               "shadowed declaration is here");
1151
                }
1152
            }
1153
 
1154
          /* Maybe warn if shadowing something else.  */
1155
          else if (warn_shadow && !DECL_EXTERNAL (x)
1156
                   /* No shadow warnings for internally generated vars unless
1157
                      it's an implicit typedef (see create_implicit_typedef
1158
                      in decl.c).  */
1159
                   && (! DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x))
1160
                   /* No shadow warnings for vars made for inlining.  */
1161
                   && ! DECL_FROM_INLINE (x))
1162
            {
1163
              tree member;
1164
 
1165
              if (current_class_ptr)
1166
                member = lookup_member (current_class_type,
1167
                                        name,
1168
                                        /*protect=*/0,
1169
                                        /*want_type=*/false,
1170
                                        tf_warning_or_error);
1171
              else
1172
                member = NULL_TREE;
1173
 
1174
              if (member && !TREE_STATIC (member))
1175
                {
1176
                  /* Location of previous decl is not useful in this case.  */
1177
                  warning (OPT_Wshadow, "declaration of %qD shadows a member of 'this'",
1178
                           x);
1179
                }
1180
              else if (oldglobal != NULL_TREE
1181
                       && (TREE_CODE (oldglobal) == VAR_DECL
1182
                           /* If the old decl is a type decl, only warn if the
1183
                              old decl is an explicit typedef or if both the
1184
                              old and new decls are type decls.  */
1185
                           || (TREE_CODE (oldglobal) == TYPE_DECL
1186
                               && (!DECL_ARTIFICIAL (oldglobal)
1187
                                   || TREE_CODE (x) == TYPE_DECL))))
1188
                /* XXX shadow warnings in outer-more namespaces */
1189
                {
1190
                  warning_at (input_location, OPT_Wshadow,
1191
                              "declaration of %qD shadows a global declaration", x);
1192
                  warning_at (DECL_SOURCE_LOCATION (oldglobal), OPT_Wshadow,
1193
                              "shadowed declaration is here");
1194
                }
1195
            }
1196
        }
1197
 
1198
      if (TREE_CODE (x) == VAR_DECL)
1199
        maybe_register_incomplete_var (x);
1200
    }
1201
 
1202
  if (need_new_binding)
1203
    add_decl_to_level (x,
1204
                       DECL_NAMESPACE_SCOPE_P (x)
1205
                       ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
1206
                       : current_binding_level);
1207
 
1208
  return x;
1209
}
1210
 
1211
/* Wrapper for pushdecl_maybe_friend_1.  */
1212
 
1213
tree
1214
pushdecl_maybe_friend (tree x, bool is_friend)
1215
{
1216
  tree ret;
1217
  bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
1218
  ret = pushdecl_maybe_friend_1 (x, is_friend);
1219
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
1220
  return ret;
1221
}
1222
 
1223
/* Record a decl-node X as belonging to the current lexical scope.  */
1224
 
1225
tree
1226
pushdecl (tree x)
1227
{
1228
  return pushdecl_maybe_friend (x, false);
1229
}
1230
 
1231
/* Enter DECL into the symbol table, if that's appropriate.  Returns
1232
   DECL, or a modified version thereof.  */
1233
 
1234
tree
1235
maybe_push_decl (tree decl)
1236
{
1237
  tree type = TREE_TYPE (decl);
1238
 
1239
  /* Add this decl to the current binding level, but not if it comes
1240
     from another scope, e.g. a static member variable.  TEM may equal
1241
     DECL or it may be a previous decl of the same name.  */
1242
  if (decl == error_mark_node
1243
      || (TREE_CODE (decl) != PARM_DECL
1244
          && DECL_CONTEXT (decl) != NULL_TREE
1245
          /* Definitions of namespace members outside their namespace are
1246
             possible.  */
1247
          && !DECL_NAMESPACE_SCOPE_P (decl))
1248
      || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
1249
      || type == unknown_type_node
1250
      /* The declaration of a template specialization does not affect
1251
         the functions available for overload resolution, so we do not
1252
         call pushdecl.  */
1253
      || (TREE_CODE (decl) == FUNCTION_DECL
1254
          && DECL_TEMPLATE_SPECIALIZATION (decl)))
1255
    return decl;
1256
  else
1257
    return pushdecl (decl);
1258
}
1259
 
1260
/* Bind DECL to ID in the current_binding_level, assumed to be a local
1261
   binding level.  If PUSH_USING is set in FLAGS, we know that DECL
1262
   doesn't really belong to this binding level, that it got here
1263
   through a using-declaration.  */
1264
 
1265
void
1266
push_local_binding (tree id, tree decl, int flags)
1267
{
1268
  cp_binding_level *b;
1269
 
1270
  /* Skip over any local classes.  This makes sense if we call
1271
     push_local_binding with a friend decl of a local class.  */
1272
  b = innermost_nonclass_level ();
1273
 
1274
  if (lookup_name_innermost_nonclass_level (id))
1275
    {
1276
      /* Supplement the existing binding.  */
1277
      if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
1278
        /* It didn't work.  Something else must be bound at this
1279
           level.  Do not add DECL to the list of things to pop
1280
           later.  */
1281
        return;
1282
    }
1283
  else
1284
    /* Create a new binding.  */
1285
    push_binding (id, decl, b);
1286
 
1287
  if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1288
    /* We must put the OVERLOAD into a TREE_LIST since the
1289
       TREE_CHAIN of an OVERLOAD is already used.  Similarly for
1290
       decls that got here through a using-declaration.  */
1291
    decl = build_tree_list (NULL_TREE, decl);
1292
 
1293
  /* And put DECL on the list of things declared by the current
1294
     binding level.  */
1295
  add_decl_to_level (decl, b);
1296
}
1297
 
1298
/* Check to see whether or not DECL is a variable that would have been
1299
   in scope under the ARM, but is not in scope under the ANSI/ISO
1300
   standard.  If so, issue an error message.  If name lookup would
1301
   work in both cases, but return a different result, this function
1302
   returns the result of ANSI/ISO lookup.  Otherwise, it returns
1303
   DECL.  */
1304
 
1305
tree
1306
check_for_out_of_scope_variable (tree decl)
1307
{
1308
  tree shadowed;
1309
 
1310
  /* We only care about out of scope variables.  */
1311
  if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
1312
    return decl;
1313
 
1314
  shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl)
1315
    ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ;
1316
  while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
1317
         && DECL_DEAD_FOR_LOCAL (shadowed))
1318
    shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed)
1319
      ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE;
1320
  if (!shadowed)
1321
    shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
1322
  if (shadowed)
1323
    {
1324
      if (!DECL_ERROR_REPORTED (decl))
1325
        {
1326
          warning (0, "name lookup of %qD changed", DECL_NAME (decl));
1327
          warning (0, "  matches this %q+D under ISO standard rules",
1328
                   shadowed);
1329
          warning (0, "  matches this %q+D under old rules", decl);
1330
          DECL_ERROR_REPORTED (decl) = 1;
1331
        }
1332
      return shadowed;
1333
    }
1334
 
1335
  /* If we have already complained about this declaration, there's no
1336
     need to do it again.  */
1337
  if (DECL_ERROR_REPORTED (decl))
1338
    return decl;
1339
 
1340
  DECL_ERROR_REPORTED (decl) = 1;
1341
 
1342
  if (TREE_TYPE (decl) == error_mark_node)
1343
    return decl;
1344
 
1345
  if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
1346
    {
1347
      error ("name lookup of %qD changed for ISO %<for%> scoping",
1348
             DECL_NAME (decl));
1349
      error ("  cannot use obsolete binding at %q+D because "
1350
             "it has a destructor", decl);
1351
      return error_mark_node;
1352
    }
1353
  else
1354
    {
1355
      permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
1356
                 DECL_NAME (decl));
1357
      if (flag_permissive)
1358
        permerror (input_location, "  using obsolete binding at %q+D", decl);
1359
      else
1360
        {
1361
          static bool hint;
1362
          if (!hint)
1363
            {
1364
              inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
1365
              hint = true;
1366
            }
1367
        }
1368
    }
1369
 
1370
  return decl;
1371
}
1372
 
1373
/* true means unconditionally make a BLOCK for the next level pushed.  */
1374
 
1375
static bool keep_next_level_flag;
1376
 
1377
static int binding_depth = 0;
1378
 
1379
static void
1380
indent (int depth)
1381
{
1382
  int i;
1383
 
1384
  for (i = 0; i < depth * 2; i++)
1385
    putc (' ', stderr);
1386
}
1387
 
1388
/* Return a string describing the kind of SCOPE we have.  */
1389
static const char *
1390
cp_binding_level_descriptor (cp_binding_level *scope)
1391
{
1392
  /* The order of this table must match the "scope_kind"
1393
     enumerators.  */
1394
  static const char* scope_kind_names[] = {
1395
    "block-scope",
1396
    "cleanup-scope",
1397
    "try-scope",
1398
    "catch-scope",
1399
    "for-scope",
1400
    "function-parameter-scope",
1401
    "class-scope",
1402
    "namespace-scope",
1403
    "template-parameter-scope",
1404
    "template-explicit-spec-scope"
1405
  };
1406
  const scope_kind kind = scope->explicit_spec_p
1407
    ? sk_template_spec : scope->kind;
1408
 
1409
  return scope_kind_names[kind];
1410
}
1411
 
1412
/* Output a debugging information about SCOPE when performing
1413
   ACTION at LINE.  */
1414
static void
1415
cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
1416
{
1417
  const char *desc = cp_binding_level_descriptor (scope);
1418
  if (scope->this_entity)
1419
    verbatim ("%s %s(%E) %p %d\n", action, desc,
1420
              scope->this_entity, (void *) scope, line);
1421
  else
1422
    verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
1423
}
1424
 
1425
/* Return the estimated initial size of the hashtable of a NAMESPACE
1426
   scope.  */
1427
 
1428
static inline size_t
1429
namespace_scope_ht_size (tree ns)
1430
{
1431
  tree name = DECL_NAME (ns);
1432
 
1433
  return name == std_identifier
1434
    ? NAMESPACE_STD_HT_SIZE
1435
    : (name == global_scope_name
1436
       ? GLOBAL_SCOPE_HT_SIZE
1437
       : NAMESPACE_ORDINARY_HT_SIZE);
1438
}
1439
 
1440
/* A chain of binding_level structures awaiting reuse.  */
1441
 
1442
static GTY((deletable)) cp_binding_level *free_binding_level;
1443
 
1444
/* Insert SCOPE as the innermost binding level.  */
1445
 
1446
void
1447
push_binding_level (cp_binding_level *scope)
1448
{
1449
  /* Add it to the front of currently active scopes stack.  */
1450
  scope->level_chain = current_binding_level;
1451
  current_binding_level = scope;
1452
  keep_next_level_flag = false;
1453
 
1454
  if (ENABLE_SCOPE_CHECKING)
1455
    {
1456
      scope->binding_depth = binding_depth;
1457
      indent (binding_depth);
1458
      cp_binding_level_debug (scope, input_line, "push");
1459
      binding_depth++;
1460
    }
1461
}
1462
 
1463
/* Create a new KIND scope and make it the top of the active scopes stack.
1464
   ENTITY is the scope of the associated C++ entity (namespace, class,
1465
   function, C++0x enumeration); it is NULL otherwise.  */
1466
 
1467
cp_binding_level *
1468
begin_scope (scope_kind kind, tree entity)
1469
{
1470
  cp_binding_level *scope;
1471
 
1472
  /* Reuse or create a struct for this binding level.  */
1473
  if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1474
    {
1475
      scope = free_binding_level;
1476
      memset (scope, 0, sizeof (cp_binding_level));
1477
      free_binding_level = scope->level_chain;
1478
    }
1479
  else
1480
    scope = ggc_alloc_cleared_cp_binding_level ();
1481
 
1482
  scope->this_entity = entity;
1483
  scope->more_cleanups_ok = true;
1484
  switch (kind)
1485
    {
1486
    case sk_cleanup:
1487
      scope->keep = true;
1488
      break;
1489
 
1490
    case sk_template_spec:
1491
      scope->explicit_spec_p = true;
1492
      kind = sk_template_parms;
1493
      /* Fall through.  */
1494
    case sk_template_parms:
1495
    case sk_block:
1496
    case sk_try:
1497
    case sk_catch:
1498
    case sk_for:
1499
    case sk_cond:
1500
    case sk_class:
1501
    case sk_scoped_enum:
1502
    case sk_function_parms:
1503
    case sk_omp:
1504
      scope->keep = keep_next_level_flag;
1505
      break;
1506
 
1507
    case sk_namespace:
1508
      NAMESPACE_LEVEL (entity) = scope;
1509
      scope->static_decls =
1510
        VEC_alloc (tree, gc,
1511
                   DECL_NAME (entity) == std_identifier
1512
                   || DECL_NAME (entity) == global_scope_name
1513
                   ? 200 : 10);
1514
      break;
1515
 
1516
    default:
1517
      /* Should not happen.  */
1518
      gcc_unreachable ();
1519
      break;
1520
    }
1521
  scope->kind = kind;
1522
 
1523
  push_binding_level (scope);
1524
 
1525
  return scope;
1526
}
1527
 
1528
/* We're about to leave current scope.  Pop the top of the stack of
1529
   currently active scopes.  Return the enclosing scope, now active.  */
1530
 
1531
cp_binding_level *
1532
leave_scope (void)
1533
{
1534
  cp_binding_level *scope = current_binding_level;
1535
 
1536
  if (scope->kind == sk_namespace && class_binding_level)
1537
    current_binding_level = class_binding_level;
1538
 
1539
  /* We cannot leave a scope, if there are none left.  */
1540
  if (NAMESPACE_LEVEL (global_namespace))
1541
    gcc_assert (!global_scope_p (scope));
1542
 
1543
  if (ENABLE_SCOPE_CHECKING)
1544
    {
1545
      indent (--binding_depth);
1546
      cp_binding_level_debug (scope, input_line, "leave");
1547
    }
1548
 
1549
  /* Move one nesting level up.  */
1550
  current_binding_level = scope->level_chain;
1551
 
1552
  /* Namespace-scopes are left most probably temporarily, not
1553
     completely; they can be reopened later, e.g. in namespace-extension
1554
     or any name binding activity that requires us to resume a
1555
     namespace.  For classes, we cache some binding levels.  For other
1556
     scopes, we just make the structure available for reuse.  */
1557
  if (scope->kind != sk_namespace
1558
      && scope->kind != sk_class)
1559
    {
1560
      scope->level_chain = free_binding_level;
1561
      gcc_assert (!ENABLE_SCOPE_CHECKING
1562
                  || scope->binding_depth == binding_depth);
1563
      free_binding_level = scope;
1564
    }
1565
 
1566
  /* Find the innermost enclosing class scope, and reset
1567
     CLASS_BINDING_LEVEL appropriately.  */
1568
  if (scope->kind == sk_class)
1569
    {
1570
      class_binding_level = NULL;
1571
      for (scope = current_binding_level; scope; scope = scope->level_chain)
1572
        if (scope->kind == sk_class)
1573
          {
1574
            class_binding_level = scope;
1575
            break;
1576
          }
1577
    }
1578
 
1579
  return current_binding_level;
1580
}
1581
 
1582
static void
1583
resume_scope (cp_binding_level* b)
1584
{
1585
  /* Resuming binding levels is meant only for namespaces,
1586
     and those cannot nest into classes.  */
1587
  gcc_assert (!class_binding_level);
1588
  /* Also, resuming a non-directly nested namespace is a no-no.  */
1589
  gcc_assert (b->level_chain == current_binding_level);
1590
  current_binding_level = b;
1591
  if (ENABLE_SCOPE_CHECKING)
1592
    {
1593
      b->binding_depth = binding_depth;
1594
      indent (binding_depth);
1595
      cp_binding_level_debug (b, input_line, "resume");
1596
      binding_depth++;
1597
    }
1598
}
1599
 
1600
/* Return the innermost binding level that is not for a class scope.  */
1601
 
1602
static cp_binding_level *
1603
innermost_nonclass_level (void)
1604
{
1605
  cp_binding_level *b;
1606
 
1607
  b = current_binding_level;
1608
  while (b->kind == sk_class)
1609
    b = b->level_chain;
1610
 
1611
  return b;
1612
}
1613
 
1614
/* We're defining an object of type TYPE.  If it needs a cleanup, but
1615
   we're not allowed to add any more objects with cleanups to the current
1616
   scope, create a new binding level.  */
1617
 
1618
void
1619
maybe_push_cleanup_level (tree type)
1620
{
1621
  if (type != error_mark_node
1622
      && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1623
      && current_binding_level->more_cleanups_ok == 0)
1624
    {
1625
      begin_scope (sk_cleanup, NULL);
1626
      current_binding_level->statement_list = push_stmt_list ();
1627
    }
1628
}
1629
 
1630
/* Return true if we are in the global binding level.  */
1631
 
1632
bool
1633
global_bindings_p (void)
1634
{
1635
  return global_scope_p (current_binding_level);
1636
}
1637
 
1638
/* True if we are currently in a toplevel binding level.  This
1639
   means either the global binding level or a namespace in a toplevel
1640
   binding level.  Since there are no non-toplevel namespace levels,
1641
   this really means any namespace or template parameter level.  We
1642
   also include a class whose context is toplevel.  */
1643
 
1644
bool
1645
toplevel_bindings_p (void)
1646
{
1647
  cp_binding_level *b = innermost_nonclass_level ();
1648
 
1649
  return b->kind == sk_namespace || b->kind == sk_template_parms;
1650
}
1651
 
1652
/* True if this is a namespace scope, or if we are defining a class
1653
   which is itself at namespace scope, or whose enclosing class is
1654
   such a class, etc.  */
1655
 
1656
bool
1657
namespace_bindings_p (void)
1658
{
1659
  cp_binding_level *b = innermost_nonclass_level ();
1660
 
1661
  return b->kind == sk_namespace;
1662
}
1663
 
1664
/* True if the innermost non-class scope is a block scope.  */
1665
 
1666
bool
1667
local_bindings_p (void)
1668
{
1669
  cp_binding_level *b = innermost_nonclass_level ();
1670
  return b->kind < sk_function_parms || b->kind == sk_omp;
1671
}
1672
 
1673
/* True if the current level needs to have a BLOCK made.  */
1674
 
1675
bool
1676
kept_level_p (void)
1677
{
1678
  return (current_binding_level->blocks != NULL_TREE
1679
          || current_binding_level->keep
1680
          || current_binding_level->kind == sk_cleanup
1681
          || current_binding_level->names != NULL_TREE
1682
          || current_binding_level->using_directives);
1683
}
1684
 
1685
/* Returns the kind of the innermost scope.  */
1686
 
1687
scope_kind
1688
innermost_scope_kind (void)
1689
{
1690
  return current_binding_level->kind;
1691
}
1692
 
1693
/* Returns true if this scope was created to store template parameters.  */
1694
 
1695
bool
1696
template_parm_scope_p (void)
1697
{
1698
  return innermost_scope_kind () == sk_template_parms;
1699
}
1700
 
1701
/* If KEEP is true, make a BLOCK node for the next binding level,
1702
   unconditionally.  Otherwise, use the normal logic to decide whether
1703
   or not to create a BLOCK.  */
1704
 
1705
void
1706
keep_next_level (bool keep)
1707
{
1708
  keep_next_level_flag = keep;
1709
}
1710
 
1711
/* Return the list of declarations of the current level.
1712
   Note that this list is in reverse order unless/until
1713
   you nreverse it; and when you do nreverse it, you must
1714
   store the result back using `storedecls' or you will lose.  */
1715
 
1716
tree
1717
getdecls (void)
1718
{
1719
  return current_binding_level->names;
1720
}
1721
 
1722
/* Return how many function prototypes we are currently nested inside.  */
1723
 
1724
int
1725
function_parm_depth (void)
1726
{
1727
  int level = 0;
1728
  cp_binding_level *b;
1729
 
1730
  for (b = current_binding_level;
1731
       b->kind == sk_function_parms;
1732
       b = b->level_chain)
1733
    ++level;
1734
 
1735
  return level;
1736
}
1737
 
1738
/* For debugging.  */
1739
static int no_print_functions = 0;
1740
static int no_print_builtins = 0;
1741
 
1742
static void
1743
print_binding_level (cp_binding_level* lvl)
1744
{
1745
  tree t;
1746
  int i = 0, len;
1747
  fprintf (stderr, " blocks=%p", (void *) lvl->blocks);
1748
  if (lvl->more_cleanups_ok)
1749
    fprintf (stderr, " more-cleanups-ok");
1750
  if (lvl->have_cleanups)
1751
    fprintf (stderr, " have-cleanups");
1752
  fprintf (stderr, "\n");
1753
  if (lvl->names)
1754
    {
1755
      fprintf (stderr, " names:\t");
1756
      /* We can probably fit 3 names to a line?  */
1757
      for (t = lvl->names; t; t = TREE_CHAIN (t))
1758
        {
1759
          if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1760
            continue;
1761
          if (no_print_builtins
1762
              && (TREE_CODE (t) == TYPE_DECL)
1763
              && DECL_IS_BUILTIN (t))
1764
            continue;
1765
 
1766
          /* Function decls tend to have longer names.  */
1767
          if (TREE_CODE (t) == FUNCTION_DECL)
1768
            len = 3;
1769
          else
1770
            len = 2;
1771
          i += len;
1772
          if (i > 6)
1773
            {
1774
              fprintf (stderr, "\n\t");
1775
              i = len;
1776
            }
1777
          print_node_brief (stderr, "", t, 0);
1778
          if (t == error_mark_node)
1779
            break;
1780
        }
1781
      if (i)
1782
        fprintf (stderr, "\n");
1783
    }
1784
  if (VEC_length (cp_class_binding, lvl->class_shadowed))
1785
    {
1786
      size_t i;
1787
      cp_class_binding *b;
1788
      fprintf (stderr, " class-shadowed:");
1789
      FOR_EACH_VEC_ELT (cp_class_binding, lvl->class_shadowed, i, b)
1790
        fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
1791
      fprintf (stderr, "\n");
1792
    }
1793
  if (lvl->type_shadowed)
1794
    {
1795
      fprintf (stderr, " type-shadowed:");
1796
      for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1797
        {
1798
          fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1799
        }
1800
      fprintf (stderr, "\n");
1801
    }
1802
}
1803
 
1804
void
1805
print_other_binding_stack (cp_binding_level *stack)
1806
{
1807
  cp_binding_level *level;
1808
  for (level = stack; !global_scope_p (level); level = level->level_chain)
1809
    {
1810
      fprintf (stderr, "binding level %p\n", (void *) level);
1811
      print_binding_level (level);
1812
    }
1813
}
1814
 
1815
void
1816
print_binding_stack (void)
1817
{
1818
  cp_binding_level *b;
1819
  fprintf (stderr, "current_binding_level=%p\n"
1820
           "class_binding_level=%p\n"
1821
           "NAMESPACE_LEVEL (global_namespace)=%p\n",
1822
           (void *) current_binding_level, (void *) class_binding_level,
1823
           (void *) NAMESPACE_LEVEL (global_namespace));
1824
  if (class_binding_level)
1825
    {
1826
      for (b = class_binding_level; b; b = b->level_chain)
1827
        if (b == current_binding_level)
1828
          break;
1829
      if (b)
1830
        b = class_binding_level;
1831
      else
1832
        b = current_binding_level;
1833
    }
1834
  else
1835
    b = current_binding_level;
1836
  print_other_binding_stack (b);
1837
  fprintf (stderr, "global:\n");
1838
  print_binding_level (NAMESPACE_LEVEL (global_namespace));
1839
}
1840
 
1841
/* Return the type associated with ID.  */
1842
 
1843
static tree
1844
identifier_type_value_1 (tree id)
1845
{
1846
  /* There is no type with that name, anywhere.  */
1847
  if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
1848
    return NULL_TREE;
1849
  /* This is not the type marker, but the real thing.  */
1850
  if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
1851
    return REAL_IDENTIFIER_TYPE_VALUE (id);
1852
  /* Have to search for it. It must be on the global level, now.
1853
     Ask lookup_name not to return non-types.  */
1854
  id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
1855
  if (id)
1856
    return TREE_TYPE (id);
1857
  return NULL_TREE;
1858
}
1859
 
1860
/* Wrapper for identifier_type_value_1.  */
1861
 
1862
tree
1863
identifier_type_value (tree id)
1864
{
1865
  tree ret;
1866
  timevar_start (TV_NAME_LOOKUP);
1867
  ret = identifier_type_value_1 (id);
1868
  timevar_stop (TV_NAME_LOOKUP);
1869
  return ret;
1870
}
1871
 
1872
 
1873
/* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
1874
   the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++.  */
1875
 
1876
tree
1877
identifier_global_value (tree t)
1878
{
1879
  return IDENTIFIER_GLOBAL_VALUE (t);
1880
}
1881
 
1882
/* Push a definition of struct, union or enum tag named ID.  into
1883
   binding_level B.  DECL is a TYPE_DECL for the type.  We assume that
1884
   the tag ID is not already defined.  */
1885
 
1886
static void
1887
set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
1888
{
1889
  tree type;
1890
 
1891
  if (b->kind != sk_namespace)
1892
    {
1893
      /* Shadow the marker, not the real thing, so that the marker
1894
         gets restored later.  */
1895
      tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
1896
      b->type_shadowed
1897
        = tree_cons (id, old_type_value, b->type_shadowed);
1898
      type = decl ? TREE_TYPE (decl) : NULL_TREE;
1899
      TREE_TYPE (b->type_shadowed) = type;
1900
    }
1901
  else
1902
    {
1903
      cxx_binding *binding =
1904
        binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
1905
      gcc_assert (decl);
1906
      if (binding->value)
1907
        supplement_binding (binding, decl);
1908
      else
1909
        binding->value = decl;
1910
 
1911
      /* Store marker instead of real type.  */
1912
      type = global_type_node;
1913
    }
1914
  SET_IDENTIFIER_TYPE_VALUE (id, type);
1915
}
1916
 
1917
/* As set_identifier_type_value_with_scope, but using
1918
   current_binding_level.  */
1919
 
1920
void
1921
set_identifier_type_value (tree id, tree decl)
1922
{
1923
  set_identifier_type_value_with_scope (id, decl, current_binding_level);
1924
}
1925
 
1926
/* Return the name for the constructor (or destructor) for the
1927
   specified class TYPE.  When given a template, this routine doesn't
1928
   lose the specialization.  */
1929
 
1930
static inline tree
1931
constructor_name_full (tree type)
1932
{
1933
  return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
1934
}
1935
 
1936
/* Return the name for the constructor (or destructor) for the
1937
   specified class.  When given a template, return the plain
1938
   unspecialized name.  */
1939
 
1940
tree
1941
constructor_name (tree type)
1942
{
1943
  tree name;
1944
  name = constructor_name_full (type);
1945
  if (IDENTIFIER_TEMPLATE (name))
1946
    name = IDENTIFIER_TEMPLATE (name);
1947
  return name;
1948
}
1949
 
1950
/* Returns TRUE if NAME is the name for the constructor for TYPE,
1951
   which must be a class type.  */
1952
 
1953
bool
1954
constructor_name_p (tree name, tree type)
1955
{
1956
  tree ctor_name;
1957
 
1958
  gcc_assert (MAYBE_CLASS_TYPE_P (type));
1959
 
1960
  if (!name)
1961
    return false;
1962
 
1963
  if (TREE_CODE (name) != IDENTIFIER_NODE)
1964
    return false;
1965
 
1966
  ctor_name = constructor_name_full (type);
1967
  if (name == ctor_name)
1968
    return true;
1969
  if (IDENTIFIER_TEMPLATE (ctor_name)
1970
      && name == IDENTIFIER_TEMPLATE (ctor_name))
1971
    return true;
1972
  return false;
1973
}
1974
 
1975
/* Counter used to create anonymous type names.  */
1976
 
1977
static GTY(()) int anon_cnt;
1978
 
1979
/* Return an IDENTIFIER which can be used as a name for
1980
   anonymous structs and unions.  */
1981
 
1982
tree
1983
make_anon_name (void)
1984
{
1985
  char buf[32];
1986
 
1987
  sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
1988
  return get_identifier (buf);
1989
}
1990
 
1991
/* This code is practically identical to that for creating
1992
   anonymous names, but is just used for lambdas instead.  This is necessary
1993
   because anonymous names are recognized and cannot be passed to template
1994
   functions.  */
1995
/* FIXME is this still necessary? */
1996
 
1997
static GTY(()) int lambda_cnt = 0;
1998
 
1999
tree
2000
make_lambda_name (void)
2001
{
2002
  char buf[32];
2003
 
2004
  sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
2005
  return get_identifier (buf);
2006
}
2007
 
2008
/* Return (from the stack of) the BINDING, if any, established at SCOPE.  */
2009
 
2010
static inline cxx_binding *
2011
find_binding (cp_binding_level *scope, cxx_binding *binding)
2012
{
2013
  for (; binding != NULL; binding = binding->previous)
2014
    if (binding->scope == scope)
2015
      return binding;
2016
 
2017
  return (cxx_binding *)0;
2018
}
2019
 
2020
/* Return the binding for NAME in SCOPE, if any.  Otherwise, return NULL.  */
2021
 
2022
static inline cxx_binding *
2023
cp_binding_level_find_binding_for_name (cp_binding_level *scope, tree name)
2024
{
2025
  cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2026
  if (b)
2027
    {
2028
      /* Fold-in case where NAME is used only once.  */
2029
      if (scope == b->scope && b->previous == NULL)
2030
        return b;
2031
      return find_binding (scope, b);
2032
    }
2033
  return NULL;
2034
}
2035
 
2036
/* Always returns a binding for name in scope.  If no binding is
2037
   found, make a new one.  */
2038
 
2039
static cxx_binding *
2040
binding_for_name (cp_binding_level *scope, tree name)
2041
{
2042
  cxx_binding *result;
2043
 
2044
  result = cp_binding_level_find_binding_for_name (scope, name);
2045
  if (result)
2046
    return result;
2047
  /* Not found, make a new one.  */
2048
  result = cxx_binding_make (NULL, NULL);
2049
  result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
2050
  result->scope = scope;
2051
  result->is_local = false;
2052
  result->value_is_inherited = false;
2053
  IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2054
  return result;
2055
}
2056
 
2057
/* Walk through the bindings associated to the name of FUNCTION,
2058
   and return the first declaration of a function with a
2059
   "C" linkage specification, a.k.a 'extern "C"'.
2060
   This function looks for the binding, regardless of which scope it
2061
   has been defined in. It basically looks in all the known scopes.
2062
   Note that this function does not lookup for bindings of builtin functions
2063
   or for functions declared in system headers.  */
2064
static tree
2065
lookup_extern_c_fun_in_all_ns (tree function)
2066
{
2067
  tree name;
2068
  cxx_binding *iter;
2069
 
2070
  gcc_assert (function && TREE_CODE (function) == FUNCTION_DECL);
2071
 
2072
  name = DECL_NAME (function);
2073
  gcc_assert (name && TREE_CODE (name) == IDENTIFIER_NODE);
2074
 
2075
  for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
2076
       iter;
2077
       iter = iter->previous)
2078
    {
2079
      tree ovl;
2080
      for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl))
2081
        {
2082
          tree decl = OVL_CURRENT (ovl);
2083
          if (decl
2084
              && TREE_CODE (decl) == FUNCTION_DECL
2085
              && DECL_EXTERN_C_P (decl)
2086
              && !DECL_ARTIFICIAL (decl))
2087
            {
2088
              return decl;
2089
            }
2090
        }
2091
    }
2092
  return NULL;
2093
}
2094
 
2095
/* Returns a list of C-linkage decls with the name NAME.  */
2096
 
2097
tree
2098
c_linkage_bindings (tree name)
2099
{
2100
  tree decls = NULL_TREE;
2101
  cxx_binding *iter;
2102
 
2103
  for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
2104
       iter;
2105
       iter = iter->previous)
2106
    {
2107
      tree ovl;
2108
      for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl))
2109
        {
2110
          tree decl = OVL_CURRENT (ovl);
2111
          if (decl
2112
              && DECL_EXTERN_C_P (decl)
2113
              && !DECL_ARTIFICIAL (decl))
2114
            {
2115
              if (decls == NULL_TREE)
2116
                decls = decl;
2117
              else
2118
                decls = tree_cons (NULL_TREE, decl, decls);
2119
            }
2120
        }
2121
    }
2122
  return decls;
2123
}
2124
 
2125
/* Insert another USING_DECL into the current binding level, returning
2126
   this declaration. If this is a redeclaration, do nothing, and
2127
   return NULL_TREE if this not in namespace scope (in namespace
2128
   scope, a using decl might extend any previous bindings).  */
2129
 
2130
static tree
2131
push_using_decl_1 (tree scope, tree name)
2132
{
2133
  tree decl;
2134
 
2135
  gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
2136
  gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
2137
  for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
2138
    if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
2139
      break;
2140
  if (decl)
2141
    return namespace_bindings_p () ? decl : NULL_TREE;
2142
  decl = build_lang_decl (USING_DECL, name, NULL_TREE);
2143
  USING_DECL_SCOPE (decl) = scope;
2144
  DECL_CHAIN (decl) = current_binding_level->usings;
2145
  current_binding_level->usings = decl;
2146
  return decl;
2147
}
2148
 
2149
/* Wrapper for push_using_decl_1.  */
2150
 
2151
static tree
2152
push_using_decl (tree scope, tree name)
2153
{
2154
  tree ret;
2155
  timevar_start (TV_NAME_LOOKUP);
2156
  ret = push_using_decl_1 (scope, name);
2157
  timevar_stop (TV_NAME_LOOKUP);
2158
  return ret;
2159
}
2160
 
2161
/* Same as pushdecl, but define X in binding-level LEVEL.  We rely on the
2162
   caller to set DECL_CONTEXT properly.
2163
 
2164
   Note that this must only be used when X will be the new innermost
2165
   binding for its name, as we tack it onto the front of IDENTIFIER_BINDING
2166
   without checking to see if the current IDENTIFIER_BINDING comes from a
2167
   closer binding level than LEVEL.  */
2168
 
2169
static tree
2170
pushdecl_with_scope_1 (tree x, cp_binding_level *level, bool is_friend)
2171
{
2172
  cp_binding_level *b;
2173
  tree function_decl = current_function_decl;
2174
 
2175
  current_function_decl = NULL_TREE;
2176
  if (level->kind == sk_class)
2177
    {
2178
      b = class_binding_level;
2179
      class_binding_level = level;
2180
      pushdecl_class_level (x);
2181
      class_binding_level = b;
2182
    }
2183
  else
2184
    {
2185
      b = current_binding_level;
2186
      current_binding_level = level;
2187
      x = pushdecl_maybe_friend (x, is_friend);
2188
      current_binding_level = b;
2189
    }
2190
  current_function_decl = function_decl;
2191
  return x;
2192
}
2193
 
2194
/* Wrapper for pushdecl_with_scope_1.  */
2195
 
2196
tree
2197
pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
2198
{
2199
  tree ret;
2200
  bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2201
  ret = pushdecl_with_scope_1 (x, level, is_friend);
2202
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2203
  return ret;
2204
}
2205
 
2206
 
2207
/* DECL is a FUNCTION_DECL for a non-member function, which may have
2208
   other definitions already in place.  We get around this by making
2209
   the value of the identifier point to a list of all the things that
2210
   want to be referenced by that name.  It is then up to the users of
2211
   that name to decide what to do with that list.
2212
 
2213
   DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
2214
   DECL_TEMPLATE_RESULT.  It is dealt with the same way.
2215
 
2216
   FLAGS is a bitwise-or of the following values:
2217
     PUSH_LOCAL: Bind DECL in the current scope, rather than at
2218
                 namespace scope.
2219
     PUSH_USING: DECL is being pushed as the result of a using
2220
                 declaration.
2221
 
2222
   IS_FRIEND is true if this is a friend declaration.
2223
 
2224
   The value returned may be a previous declaration if we guessed wrong
2225
   about what language DECL should belong to (C or C++).  Otherwise,
2226
   it's always DECL (and never something that's not a _DECL).  */
2227
 
2228
static tree
2229
push_overloaded_decl_1 (tree decl, int flags, bool is_friend)
2230
{
2231
  tree name = DECL_NAME (decl);
2232
  tree old;
2233
  tree new_binding;
2234
  int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
2235
 
2236
  if (doing_global)
2237
    old = namespace_binding (name, DECL_CONTEXT (decl));
2238
  else
2239
    old = lookup_name_innermost_nonclass_level (name);
2240
 
2241
  if (old)
2242
    {
2243
      if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
2244
        {
2245
          tree t = TREE_TYPE (old);
2246
          if (MAYBE_CLASS_TYPE_P (t) && warn_shadow
2247
              && (! DECL_IN_SYSTEM_HEADER (decl)
2248
                  || ! DECL_IN_SYSTEM_HEADER (old)))
2249
            warning (OPT_Wshadow, "%q#D hides constructor for %q#T", decl, t);
2250
          old = NULL_TREE;
2251
        }
2252
      else if (is_overloaded_fn (old))
2253
        {
2254
          tree tmp;
2255
 
2256
          for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
2257
            {
2258
              tree fn = OVL_CURRENT (tmp);
2259
              tree dup;
2260
 
2261
              if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
2262
                  && !(flags & PUSH_USING)
2263
                  && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2264
                                TYPE_ARG_TYPES (TREE_TYPE (decl)))
2265
                  && ! decls_match (fn, decl))
2266
                error ("%q#D conflicts with previous using declaration %q#D",
2267
                       decl, fn);
2268
 
2269
              dup = duplicate_decls (decl, fn, is_friend);
2270
              /* If DECL was a redeclaration of FN -- even an invalid
2271
                 one -- pass that information along to our caller.  */
2272
              if (dup == fn || dup == error_mark_node)
2273
                return dup;
2274
            }
2275
 
2276
          /* We don't overload implicit built-ins.  duplicate_decls()
2277
             may fail to merge the decls if the new decl is e.g. a
2278
             template function.  */
2279
          if (TREE_CODE (old) == FUNCTION_DECL
2280
              && DECL_ANTICIPATED (old)
2281
              && !DECL_HIDDEN_FRIEND_P (old))
2282
            old = NULL;
2283
        }
2284
      else if (old == error_mark_node)
2285
        /* Ignore the undefined symbol marker.  */
2286
        old = NULL_TREE;
2287
      else
2288
        {
2289
          error ("previous non-function declaration %q+#D", old);
2290
          error ("conflicts with function declaration %q#D", decl);
2291
          return decl;
2292
        }
2293
    }
2294
 
2295
  if (old || TREE_CODE (decl) == TEMPLATE_DECL
2296
      /* If it's a using declaration, we always need to build an OVERLOAD,
2297
         because it's the only way to remember that the declaration comes
2298
         from 'using', and have the lookup behave correctly.  */
2299
      || (flags & PUSH_USING))
2300
    {
2301
      if (old && TREE_CODE (old) != OVERLOAD)
2302
        new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
2303
      else
2304
        new_binding = ovl_cons (decl, old);
2305
      if (flags & PUSH_USING)
2306
        OVL_USED (new_binding) = 1;
2307
    }
2308
  else
2309
    /* NAME is not ambiguous.  */
2310
    new_binding = decl;
2311
 
2312
  if (doing_global)
2313
    set_namespace_binding (name, current_namespace, new_binding);
2314
  else
2315
    {
2316
      /* We only create an OVERLOAD if there was a previous binding at
2317
         this level, or if decl is a template. In the former case, we
2318
         need to remove the old binding and replace it with the new
2319
         binding.  We must also run through the NAMES on the binding
2320
         level where the name was bound to update the chain.  */
2321
 
2322
      if (TREE_CODE (new_binding) == OVERLOAD && old)
2323
        {
2324
          tree *d;
2325
 
2326
          for (d = &IDENTIFIER_BINDING (name)->scope->names;
2327
               *d;
2328
               d = &TREE_CHAIN (*d))
2329
            if (*d == old
2330
                || (TREE_CODE (*d) == TREE_LIST
2331
                    && TREE_VALUE (*d) == old))
2332
              {
2333
                if (TREE_CODE (*d) == TREE_LIST)
2334
                  /* Just replace the old binding with the new.  */
2335
                  TREE_VALUE (*d) = new_binding;
2336
                else
2337
                  /* Build a TREE_LIST to wrap the OVERLOAD.  */
2338
                  *d = tree_cons (NULL_TREE, new_binding,
2339
                                  TREE_CHAIN (*d));
2340
 
2341
                /* And update the cxx_binding node.  */
2342
                IDENTIFIER_BINDING (name)->value = new_binding;
2343
                return decl;
2344
              }
2345
 
2346
          /* We should always find a previous binding in this case.  */
2347
          gcc_unreachable ();
2348
        }
2349
 
2350
      /* Install the new binding.  */
2351
      push_local_binding (name, new_binding, flags);
2352
    }
2353
 
2354
  return decl;
2355
}
2356
 
2357
/* Wrapper for push_overloaded_decl_1.  */
2358
 
2359
static tree
2360
push_overloaded_decl (tree decl, int flags, bool is_friend)
2361
{
2362
  tree ret;
2363
  bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2364
  ret = push_overloaded_decl_1 (decl, flags, is_friend);
2365
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2366
  return ret;
2367
}
2368
 
2369
/* Check a non-member using-declaration. Return the name and scope
2370
   being used, and the USING_DECL, or NULL_TREE on failure.  */
2371
 
2372
static tree
2373
validate_nonmember_using_decl (tree decl, tree scope, tree name)
2374
{
2375
  /* [namespace.udecl]
2376
       A using-declaration for a class member shall be a
2377
       member-declaration.  */
2378
  if (TYPE_P (scope))
2379
    {
2380
      error ("%qT is not a namespace", scope);
2381
      return NULL_TREE;
2382
    }
2383
  else if (scope == error_mark_node)
2384
    return NULL_TREE;
2385
 
2386
  if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
2387
    {
2388
      /* 7.3.3/5
2389
           A using-declaration shall not name a template-id.  */
2390
      error ("a using-declaration cannot specify a template-id.  "
2391
             "Try %<using %D%>", name);
2392
      return NULL_TREE;
2393
    }
2394
 
2395
  if (TREE_CODE (decl) == NAMESPACE_DECL)
2396
    {
2397
      error ("namespace %qD not allowed in using-declaration", decl);
2398
      return NULL_TREE;
2399
    }
2400
 
2401
  if (TREE_CODE (decl) == SCOPE_REF)
2402
    {
2403
      /* It's a nested name with template parameter dependent scope.
2404
         This can only be using-declaration for class member.  */
2405
      error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
2406
      return NULL_TREE;
2407
    }
2408
 
2409
  if (is_overloaded_fn (decl))
2410
    decl = get_first_fn (decl);
2411
 
2412
  gcc_assert (DECL_P (decl));
2413
 
2414
  /* Make a USING_DECL.  */
2415
  return push_using_decl (scope, name);
2416
}
2417
 
2418
/* Process local and global using-declarations.  */
2419
 
2420
static void
2421
do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
2422
                         tree *newval, tree *newtype)
2423
{
2424
  struct scope_binding decls = EMPTY_SCOPE_BINDING;
2425
 
2426
  *newval = *newtype = NULL_TREE;
2427
  if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
2428
    /* Lookup error */
2429
    return;
2430
 
2431
  if (!decls.value && !decls.type)
2432
    {
2433
      error ("%qD not declared", name);
2434
      return;
2435
    }
2436
 
2437
  /* Shift the old and new bindings around so we're comparing class and
2438
     enumeration names to each other.  */
2439
  if (oldval && DECL_IMPLICIT_TYPEDEF_P (oldval))
2440
    {
2441
      oldtype = oldval;
2442
      oldval = NULL_TREE;
2443
    }
2444
 
2445
  if (decls.value && DECL_IMPLICIT_TYPEDEF_P (decls.value))
2446
    {
2447
      decls.type = decls.value;
2448
      decls.value = NULL_TREE;
2449
    }
2450
 
2451
  /* It is impossible to overload a built-in function; any explicit
2452
     declaration eliminates the built-in declaration.  So, if OLDVAL
2453
     is a built-in, then we can just pretend it isn't there.  */
2454
  if (oldval
2455
      && TREE_CODE (oldval) == FUNCTION_DECL
2456
      && DECL_ANTICIPATED (oldval)
2457
      && !DECL_HIDDEN_FRIEND_P (oldval))
2458
    oldval = NULL_TREE;
2459
 
2460
  if (decls.value)
2461
    {
2462
      /* Check for using functions.  */
2463
      if (is_overloaded_fn (decls.value))
2464
        {
2465
          tree tmp, tmp1;
2466
 
2467
          if (oldval && !is_overloaded_fn (oldval))
2468
            {
2469
              error ("%qD is already declared in this scope", name);
2470
              oldval = NULL_TREE;
2471
            }
2472
 
2473
          *newval = oldval;
2474
          for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
2475
            {
2476
              tree new_fn = OVL_CURRENT (tmp);
2477
 
2478
              /* [namespace.udecl]
2479
 
2480
                 If a function declaration in namespace scope or block
2481
                 scope has the same name and the same parameter types as a
2482
                 function introduced by a using declaration the program is
2483
                 ill-formed.  */
2484
              for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
2485
                {
2486
                  tree old_fn = OVL_CURRENT (tmp1);
2487
 
2488
                  if (new_fn == old_fn)
2489
                    /* The function already exists in the current namespace.  */
2490
                    break;
2491
                  else if (OVL_USED (tmp1))
2492
                    continue; /* this is a using decl */
2493
                  else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
2494
                                      TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
2495
                    {
2496
                      gcc_assert (!DECL_ANTICIPATED (old_fn)
2497
                                  || DECL_HIDDEN_FRIEND_P (old_fn));
2498
 
2499
                      /* There was already a non-using declaration in
2500
                         this scope with the same parameter types. If both
2501
                         are the same extern "C" functions, that's ok.  */
2502
                      if (decls_match (new_fn, old_fn))
2503
                        break;
2504
                      else
2505
                        {
2506
                          error ("%qD is already declared in this scope", name);
2507
                          break;
2508
                        }
2509
                    }
2510
                }
2511
 
2512
              /* If we broke out of the loop, there's no reason to add
2513
                 this function to the using declarations for this
2514
                 scope.  */
2515
              if (tmp1)
2516
                continue;
2517
 
2518
              /* If we are adding to an existing OVERLOAD, then we no
2519
                 longer know the type of the set of functions.  */
2520
              if (*newval && TREE_CODE (*newval) == OVERLOAD)
2521
                TREE_TYPE (*newval) = unknown_type_node;
2522
              /* Add this new function to the set.  */
2523
              *newval = build_overload (OVL_CURRENT (tmp), *newval);
2524
              /* If there is only one function, then we use its type.  (A
2525
                 using-declaration naming a single function can be used in
2526
                 contexts where overload resolution cannot be
2527
                 performed.)  */
2528
              if (TREE_CODE (*newval) != OVERLOAD)
2529
                {
2530
                  *newval = ovl_cons (*newval, NULL_TREE);
2531
                  TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
2532
                }
2533
              OVL_USED (*newval) = 1;
2534
            }
2535
        }
2536
      else
2537
        {
2538
          *newval = decls.value;
2539
          if (oldval && !decls_match (*newval, oldval))
2540
            error ("%qD is already declared in this scope", name);
2541
        }
2542
    }
2543
  else
2544
    *newval = oldval;
2545
 
2546
  if (decls.type && TREE_CODE (decls.type) == TREE_LIST)
2547
    {
2548
      error ("reference to %qD is ambiguous", name);
2549
      print_candidates (decls.type);
2550
    }
2551
  else
2552
    {
2553
      *newtype = decls.type;
2554
      if (oldtype && *newtype && !decls_match (oldtype, *newtype))
2555
        error ("%qD is already declared in this scope", name);
2556
    }
2557
 
2558
    /* If *newval is empty, shift any class or enumeration name down.  */
2559
    if (!*newval)
2560
      {
2561
        *newval = *newtype;
2562
        *newtype = NULL_TREE;
2563
      }
2564
}
2565
 
2566
/* Process a using-declaration at function scope.  */
2567
 
2568
void
2569
do_local_using_decl (tree decl, tree scope, tree name)
2570
{
2571
  tree oldval, oldtype, newval, newtype;
2572
  tree orig_decl = decl;
2573
 
2574
  decl = validate_nonmember_using_decl (decl, scope, name);
2575
  if (decl == NULL_TREE)
2576
    return;
2577
 
2578
  if (building_stmt_list_p ()
2579
      && at_function_scope_p ())
2580
    add_decl_expr (decl);
2581
 
2582
  oldval = lookup_name_innermost_nonclass_level (name);
2583
  oldtype = lookup_type_current_level (name);
2584
 
2585
  do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
2586
 
2587
  if (newval)
2588
    {
2589
      if (is_overloaded_fn (newval))
2590
        {
2591
          tree fn, term;
2592
 
2593
          /* We only need to push declarations for those functions
2594
             that were not already bound in the current level.
2595
             The old value might be NULL_TREE, it might be a single
2596
             function, or an OVERLOAD.  */
2597
          if (oldval && TREE_CODE (oldval) == OVERLOAD)
2598
            term = OVL_FUNCTION (oldval);
2599
          else
2600
            term = oldval;
2601
          for (fn = newval; fn && OVL_CURRENT (fn) != term;
2602
               fn = OVL_NEXT (fn))
2603
            push_overloaded_decl (OVL_CURRENT (fn),
2604
                                  PUSH_LOCAL | PUSH_USING,
2605
                                  false);
2606
        }
2607
      else
2608
        push_local_binding (name, newval, PUSH_USING);
2609
    }
2610
  if (newtype)
2611
    {
2612
      push_local_binding (name, newtype, PUSH_USING);
2613
      set_identifier_type_value (name, newtype);
2614
    }
2615
 
2616
  /* Emit debug info.  */
2617
  if (!processing_template_decl)
2618
    cp_emit_debug_info_for_using (orig_decl, current_scope());
2619
}
2620
 
2621
/* Returns true if ROOT (a namespace, class, or function) encloses
2622
   CHILD.  CHILD may be either a class type or a namespace.  */
2623
 
2624
bool
2625
is_ancestor (tree root, tree child)
2626
{
2627
  gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
2628
               || TREE_CODE (root) == FUNCTION_DECL
2629
               || CLASS_TYPE_P (root)));
2630
  gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
2631
               || CLASS_TYPE_P (child)));
2632
 
2633
  /* The global namespace encloses everything.  */
2634
  if (root == global_namespace)
2635
    return true;
2636
 
2637
  while (true)
2638
    {
2639
      /* If we've run out of scopes, stop.  */
2640
      if (!child)
2641
        return false;
2642
      /* If we've reached the ROOT, it encloses CHILD.  */
2643
      if (root == child)
2644
        return true;
2645
      /* Go out one level.  */
2646
      if (TYPE_P (child))
2647
        child = TYPE_NAME (child);
2648
      child = DECL_CONTEXT (child);
2649
    }
2650
}
2651
 
2652
/* Enter the class or namespace scope indicated by T suitable for name
2653
   lookup.  T can be arbitrary scope, not necessary nested inside the
2654
   current scope.  Returns a non-null scope to pop iff pop_scope
2655
   should be called later to exit this scope.  */
2656
 
2657
tree
2658
push_scope (tree t)
2659
{
2660
  if (TREE_CODE (t) == NAMESPACE_DECL)
2661
    push_decl_namespace (t);
2662
  else if (CLASS_TYPE_P (t))
2663
    {
2664
      if (!at_class_scope_p ()
2665
          || !same_type_p (current_class_type, t))
2666
        push_nested_class (t);
2667
      else
2668
        /* T is the same as the current scope.  There is therefore no
2669
           need to re-enter the scope.  Since we are not actually
2670
           pushing a new scope, our caller should not call
2671
           pop_scope.  */
2672
        t = NULL_TREE;
2673
    }
2674
 
2675
  return t;
2676
}
2677
 
2678
/* Leave scope pushed by push_scope.  */
2679
 
2680
void
2681
pop_scope (tree t)
2682
{
2683
  if (t == NULL_TREE)
2684
    return;
2685
  if (TREE_CODE (t) == NAMESPACE_DECL)
2686
    pop_decl_namespace ();
2687
  else if CLASS_TYPE_P (t)
2688
    pop_nested_class ();
2689
}
2690
 
2691
/* Subroutine of push_inner_scope.  */
2692
 
2693
static void
2694
push_inner_scope_r (tree outer, tree inner)
2695
{
2696
  tree prev;
2697
 
2698
  if (outer == inner
2699
      || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2700
    return;
2701
 
2702
  prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2703
  if (outer != prev)
2704
    push_inner_scope_r (outer, prev);
2705
  if (TREE_CODE (inner) == NAMESPACE_DECL)
2706
    {
2707
      cp_binding_level *save_template_parm = 0;
2708
      /* Temporary take out template parameter scopes.  They are saved
2709
         in reversed order in save_template_parm.  */
2710
      while (current_binding_level->kind == sk_template_parms)
2711
        {
2712
          cp_binding_level *b = current_binding_level;
2713
          current_binding_level = b->level_chain;
2714
          b->level_chain = save_template_parm;
2715
          save_template_parm = b;
2716
        }
2717
 
2718
      resume_scope (NAMESPACE_LEVEL (inner));
2719
      current_namespace = inner;
2720
 
2721
      /* Restore template parameter scopes.  */
2722
      while (save_template_parm)
2723
        {
2724
          cp_binding_level *b = save_template_parm;
2725
          save_template_parm = b->level_chain;
2726
          b->level_chain = current_binding_level;
2727
          current_binding_level = b;
2728
        }
2729
    }
2730
  else
2731
    pushclass (inner);
2732
}
2733
 
2734
/* Enter the scope INNER from current scope.  INNER must be a scope
2735
   nested inside current scope.  This works with both name lookup and
2736
   pushing name into scope.  In case a template parameter scope is present,
2737
   namespace is pushed under the template parameter scope according to
2738
   name lookup rule in 14.6.1/6.
2739
 
2740
   Return the former current scope suitable for pop_inner_scope.  */
2741
 
2742
tree
2743
push_inner_scope (tree inner)
2744
{
2745
  tree outer = current_scope ();
2746
  if (!outer)
2747
    outer = current_namespace;
2748
 
2749
  push_inner_scope_r (outer, inner);
2750
  return outer;
2751
}
2752
 
2753
/* Exit the current scope INNER back to scope OUTER.  */
2754
 
2755
void
2756
pop_inner_scope (tree outer, tree inner)
2757
{
2758
  if (outer == inner
2759
      || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
2760
    return;
2761
 
2762
  while (outer != inner)
2763
    {
2764
      if (TREE_CODE (inner) == NAMESPACE_DECL)
2765
        {
2766
          cp_binding_level *save_template_parm = 0;
2767
          /* Temporary take out template parameter scopes.  They are saved
2768
             in reversed order in save_template_parm.  */
2769
          while (current_binding_level->kind == sk_template_parms)
2770
            {
2771
              cp_binding_level *b = current_binding_level;
2772
              current_binding_level = b->level_chain;
2773
              b->level_chain = save_template_parm;
2774
              save_template_parm = b;
2775
            }
2776
 
2777
          pop_namespace ();
2778
 
2779
          /* Restore template parameter scopes.  */
2780
          while (save_template_parm)
2781
            {
2782
              cp_binding_level *b = save_template_parm;
2783
              save_template_parm = b->level_chain;
2784
              b->level_chain = current_binding_level;
2785
              current_binding_level = b;
2786
            }
2787
        }
2788
      else
2789
        popclass ();
2790
 
2791
      inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
2792
    }
2793
}
2794
 
2795
/* Do a pushlevel for class declarations.  */
2796
 
2797
void
2798
pushlevel_class (void)
2799
{
2800
  class_binding_level = begin_scope (sk_class, current_class_type);
2801
}
2802
 
2803
/* ...and a poplevel for class declarations.  */
2804
 
2805
void
2806
poplevel_class (void)
2807
{
2808
  cp_binding_level *level = class_binding_level;
2809
  cp_class_binding *cb;
2810
  size_t i;
2811
  tree shadowed;
2812
 
2813
  bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
2814
  gcc_assert (level != 0);
2815
 
2816
  /* If we're leaving a toplevel class, cache its binding level.  */
2817
  if (current_class_depth == 1)
2818
    previous_class_level = level;
2819
  for (shadowed = level->type_shadowed;
2820
       shadowed;
2821
       shadowed = TREE_CHAIN (shadowed))
2822
    SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
2823
 
2824
  /* Remove the bindings for all of the class-level declarations.  */
2825
  if (level->class_shadowed)
2826
    {
2827
      FOR_EACH_VEC_ELT (cp_class_binding, level->class_shadowed, i, cb)
2828
        {
2829
          IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
2830
          cxx_binding_free (cb->base);
2831
        }
2832
      ggc_free (level->class_shadowed);
2833
      level->class_shadowed = NULL;
2834
    }
2835
 
2836
  /* Now, pop out of the binding level which we created up in the
2837
     `pushlevel_class' routine.  */
2838
  gcc_assert (current_binding_level == level);
2839
  leave_scope ();
2840
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2841
}
2842
 
2843
/* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
2844
   appropriate.  DECL is the value to which a name has just been
2845
   bound.  CLASS_TYPE is the class in which the lookup occurred.  */
2846
 
2847
static void
2848
set_inherited_value_binding_p (cxx_binding *binding, tree decl,
2849
                               tree class_type)
2850
{
2851
  if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
2852
    {
2853
      tree context;
2854
 
2855
      if (TREE_CODE (decl) == OVERLOAD)
2856
        context = ovl_scope (decl);
2857
      else
2858
        {
2859
          gcc_assert (DECL_P (decl));
2860
          context = context_for_name_lookup (decl);
2861
        }
2862
 
2863
      if (is_properly_derived_from (class_type, context))
2864
        INHERITED_VALUE_BINDING_P (binding) = 1;
2865
      else
2866
        INHERITED_VALUE_BINDING_P (binding) = 0;
2867
    }
2868
  else if (binding->value == decl)
2869
    /* We only encounter a TREE_LIST when there is an ambiguity in the
2870
       base classes.  Such an ambiguity can be overridden by a
2871
       definition in this class.  */
2872
    INHERITED_VALUE_BINDING_P (binding) = 1;
2873
  else
2874
    INHERITED_VALUE_BINDING_P (binding) = 0;
2875
}
2876
 
2877
/* Make the declaration of X appear in CLASS scope.  */
2878
 
2879
bool
2880
pushdecl_class_level (tree x)
2881
{
2882
  tree name;
2883
  bool is_valid = true;
2884
  bool subtime;
2885
 
2886
  /* Do nothing if we're adding to an outer lambda closure type,
2887
     outer_binding will add it later if it's needed.  */
2888
  if (current_class_type != class_binding_level->this_entity)
2889
    return true;
2890
 
2891
  subtime = timevar_cond_start (TV_NAME_LOOKUP);
2892
  /* Get the name of X.  */
2893
  if (TREE_CODE (x) == OVERLOAD)
2894
    name = DECL_NAME (get_first_fn (x));
2895
  else
2896
    name = DECL_NAME (x);
2897
 
2898
  if (name)
2899
    {
2900
      is_valid = push_class_level_binding (name, x);
2901
      if (TREE_CODE (x) == TYPE_DECL)
2902
        set_identifier_type_value (name, x);
2903
    }
2904
  else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2905
    {
2906
      /* If X is an anonymous aggregate, all of its members are
2907
         treated as if they were members of the class containing the
2908
         aggregate, for naming purposes.  */
2909
      tree f;
2910
 
2911
      for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
2912
        {
2913
          location_t save_location = input_location;
2914
          input_location = DECL_SOURCE_LOCATION (f);
2915
          if (!pushdecl_class_level (f))
2916
            is_valid = false;
2917
          input_location = save_location;
2918
        }
2919
    }
2920
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
2921
  return is_valid;
2922
}
2923
 
2924
/* Return the BINDING (if any) for NAME in SCOPE, which is a class
2925
   scope.  If the value returned is non-NULL, and the PREVIOUS field
2926
   is not set, callers must set the PREVIOUS field explicitly.  */
2927
 
2928
static cxx_binding *
2929
get_class_binding (tree name, cp_binding_level *scope)
2930
{
2931
  tree class_type;
2932
  tree type_binding;
2933
  tree value_binding;
2934
  cxx_binding *binding;
2935
 
2936
  class_type = scope->this_entity;
2937
 
2938
  /* Get the type binding.  */
2939
  type_binding = lookup_member (class_type, name,
2940
                                /*protect=*/2, /*want_type=*/true,
2941
                                tf_warning_or_error);
2942
  /* Get the value binding.  */
2943
  value_binding = lookup_member (class_type, name,
2944
                                 /*protect=*/2, /*want_type=*/false,
2945
                                 tf_warning_or_error);
2946
 
2947
  if (value_binding
2948
      && (TREE_CODE (value_binding) == TYPE_DECL
2949
          || DECL_CLASS_TEMPLATE_P (value_binding)
2950
          || (TREE_CODE (value_binding) == TREE_LIST
2951
              && TREE_TYPE (value_binding) == error_mark_node
2952
              && (TREE_CODE (TREE_VALUE (value_binding))
2953
                  == TYPE_DECL))))
2954
    /* We found a type binding, even when looking for a non-type
2955
       binding.  This means that we already processed this binding
2956
       above.  */
2957
    ;
2958
  else if (value_binding)
2959
    {
2960
      if (TREE_CODE (value_binding) == TREE_LIST
2961
          && TREE_TYPE (value_binding) == error_mark_node)
2962
        /* NAME is ambiguous.  */
2963
        ;
2964
      else if (BASELINK_P (value_binding))
2965
        /* NAME is some overloaded functions.  */
2966
        value_binding = BASELINK_FUNCTIONS (value_binding);
2967
    }
2968
 
2969
  /* If we found either a type binding or a value binding, create a
2970
     new binding object.  */
2971
  if (type_binding || value_binding)
2972
    {
2973
      binding = new_class_binding (name,
2974
                                   value_binding,
2975
                                   type_binding,
2976
                                   scope);
2977
      /* This is a class-scope binding, not a block-scope binding.  */
2978
      LOCAL_BINDING_P (binding) = 0;
2979
      set_inherited_value_binding_p (binding, value_binding, class_type);
2980
    }
2981
  else
2982
    binding = NULL;
2983
 
2984
  return binding;
2985
}
2986
 
2987
/* Make the declaration(s) of X appear in CLASS scope under the name
2988
   NAME.  Returns true if the binding is valid.  */
2989
 
2990
static bool
2991
push_class_level_binding_1 (tree name, tree x)
2992
{
2993
  cxx_binding *binding;
2994
  tree decl = x;
2995
  bool ok;
2996
 
2997
  /* The class_binding_level will be NULL if x is a template
2998
     parameter name in a member template.  */
2999
  if (!class_binding_level)
3000
    return true;
3001
 
3002
  if (name == error_mark_node)
3003
    return false;
3004
 
3005
  /* Check for invalid member names.  */
3006
  gcc_assert (TYPE_BEING_DEFINED (current_class_type));
3007
  /* Check that we're pushing into the right binding level.  */
3008
  gcc_assert (current_class_type == class_binding_level->this_entity);
3009
 
3010
  /* We could have been passed a tree list if this is an ambiguous
3011
     declaration. If so, pull the declaration out because
3012
     check_template_shadow will not handle a TREE_LIST.  */
3013
  if (TREE_CODE (decl) == TREE_LIST
3014
      && TREE_TYPE (decl) == error_mark_node)
3015
    decl = TREE_VALUE (decl);
3016
 
3017
  if (!check_template_shadow (decl))
3018
    return false;
3019
 
3020
  /* [class.mem]
3021
 
3022
     If T is the name of a class, then each of the following shall
3023
     have a name different from T:
3024
 
3025
     -- every static data member of class T;
3026
 
3027
     -- every member of class T that is itself a type;
3028
 
3029
     -- every enumerator of every member of class T that is an
3030
        enumerated type;
3031
 
3032
     -- every member of every anonymous union that is a member of
3033
        class T.
3034
 
3035
     (Non-static data members were also forbidden to have the same
3036
     name as T until TC1.)  */
3037
  if ((TREE_CODE (x) == VAR_DECL
3038
       || TREE_CODE (x) == CONST_DECL
3039
       || (TREE_CODE (x) == TYPE_DECL
3040
           && !DECL_SELF_REFERENCE_P (x))
3041
       /* A data member of an anonymous union.  */
3042
       || (TREE_CODE (x) == FIELD_DECL
3043
           && DECL_CONTEXT (x) != current_class_type))
3044
      && DECL_NAME (x) == constructor_name (current_class_type))
3045
    {
3046
      tree scope = context_for_name_lookup (x);
3047
      if (TYPE_P (scope) && same_type_p (scope, current_class_type))
3048
        {
3049
          error ("%qD has the same name as the class in which it is "
3050
                 "declared",
3051
                 x);
3052
          return false;
3053
        }
3054
    }
3055
 
3056
  /* Get the current binding for NAME in this class, if any.  */
3057
  binding = IDENTIFIER_BINDING (name);
3058
  if (!binding || binding->scope != class_binding_level)
3059
    {
3060
      binding = get_class_binding (name, class_binding_level);
3061
      /* If a new binding was created, put it at the front of the
3062
         IDENTIFIER_BINDING list.  */
3063
      if (binding)
3064
        {
3065
          binding->previous = IDENTIFIER_BINDING (name);
3066
          IDENTIFIER_BINDING (name) = binding;
3067
        }
3068
    }
3069
 
3070
  /* If there is already a binding, then we may need to update the
3071
     current value.  */
3072
  if (binding && binding->value)
3073
    {
3074
      tree bval = binding->value;
3075
      tree old_decl = NULL_TREE;
3076
      tree target_decl = strip_using_decl (decl);
3077
      tree target_bval = strip_using_decl (bval);
3078
 
3079
      if (INHERITED_VALUE_BINDING_P (binding))
3080
        {
3081
          /* If the old binding was from a base class, and was for a
3082
             tag name, slide it over to make room for the new binding.
3083
             The old binding is still visible if explicitly qualified
3084
             with a class-key.  */
3085
          if (TREE_CODE (target_bval) == TYPE_DECL
3086
              && DECL_ARTIFICIAL (target_bval)
3087
              && !(TREE_CODE (target_decl) == TYPE_DECL
3088
                   && DECL_ARTIFICIAL (target_decl)))
3089
            {
3090
              old_decl = binding->type;
3091
              binding->type = bval;
3092
              binding->value = NULL_TREE;
3093
              INHERITED_VALUE_BINDING_P (binding) = 0;
3094
            }
3095
          else
3096
            {
3097
              old_decl = bval;
3098
              /* Any inherited type declaration is hidden by the type
3099
                 declaration in the derived class.  */
3100
              if (TREE_CODE (target_decl) == TYPE_DECL
3101
                  && DECL_ARTIFICIAL (target_decl))
3102
                binding->type = NULL_TREE;
3103
            }
3104
        }
3105
      else if (TREE_CODE (target_decl) == OVERLOAD
3106
               && is_overloaded_fn (target_bval))
3107
        old_decl = bval;
3108
      else if (TREE_CODE (decl) == USING_DECL
3109
               && TREE_CODE (bval) == USING_DECL
3110
               && same_type_p (USING_DECL_SCOPE (decl),
3111
                               USING_DECL_SCOPE (bval)))
3112
        /* This is a using redeclaration that will be diagnosed later
3113
           in supplement_binding */
3114
        ;
3115
      else if (TREE_CODE (decl) == USING_DECL
3116
               && TREE_CODE (bval) == USING_DECL
3117
               && DECL_DEPENDENT_P (decl)
3118
               && DECL_DEPENDENT_P (bval))
3119
        return true;
3120
      else if (TREE_CODE (decl) == USING_DECL
3121
               && is_overloaded_fn (target_bval))
3122
        old_decl = bval;
3123
      else if (TREE_CODE (bval) == USING_DECL
3124
               && is_overloaded_fn (target_decl))
3125
        return true;
3126
 
3127
      if (old_decl && binding->scope == class_binding_level)
3128
        {
3129
          binding->value = x;
3130
          /* It is always safe to clear INHERITED_VALUE_BINDING_P
3131
             here.  This function is only used to register bindings
3132
             from with the class definition itself.  */
3133
          INHERITED_VALUE_BINDING_P (binding) = 0;
3134
          return true;
3135
        }
3136
    }
3137
 
3138
  /* Note that we declared this value so that we can issue an error if
3139
     this is an invalid redeclaration of a name already used for some
3140
     other purpose.  */
3141
  note_name_declared_in_class (name, decl);
3142
 
3143
  /* If we didn't replace an existing binding, put the binding on the
3144
     stack of bindings for the identifier, and update the shadowed
3145
     list.  */
3146
  if (binding && binding->scope == class_binding_level)
3147
    /* Supplement the existing binding.  */
3148
    ok = supplement_binding (binding, decl);
3149
  else
3150
    {
3151
      /* Create a new binding.  */
3152
      push_binding (name, decl, class_binding_level);
3153
      ok = true;
3154
    }
3155
 
3156
  return ok;
3157
}
3158
 
3159
/* Wrapper for push_class_level_binding_1.  */
3160
 
3161
bool
3162
push_class_level_binding (tree name, tree x)
3163
{
3164
  bool ret;
3165
  bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3166
  ret = push_class_level_binding_1 (name, x);
3167
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3168
  return ret;
3169
}
3170
 
3171
/* Process "using SCOPE::NAME" in a class scope.  Return the
3172
   USING_DECL created.  */
3173
 
3174
tree
3175
do_class_using_decl (tree scope, tree name)
3176
{
3177
  /* The USING_DECL returned by this function.  */
3178
  tree value;
3179
  /* The declaration (or declarations) name by this using
3180
     declaration.  NULL if we are in a template and cannot figure out
3181
     what has been named.  */
3182
  tree decl;
3183
  /* True if SCOPE is a dependent type.  */
3184
  bool scope_dependent_p;
3185
  /* True if SCOPE::NAME is dependent.  */
3186
  bool name_dependent_p;
3187
  /* True if any of the bases of CURRENT_CLASS_TYPE are dependent.  */
3188
  bool bases_dependent_p;
3189
  tree binfo;
3190
  tree base_binfo;
3191
  int i;
3192
 
3193
  if (name == error_mark_node)
3194
    return NULL_TREE;
3195
 
3196
  if (!scope || !TYPE_P (scope))
3197
    {
3198
      error ("using-declaration for non-member at class scope");
3199
      return NULL_TREE;
3200
    }
3201
 
3202
  /* Make sure the name is not invalid */
3203
  if (TREE_CODE (name) == BIT_NOT_EXPR)
3204
    {
3205
      error ("%<%T::%D%> names destructor", scope, name);
3206
      return NULL_TREE;
3207
    }
3208
  if (MAYBE_CLASS_TYPE_P (scope) && constructor_name_p (name, scope))
3209
    {
3210
      error ("%<%T::%D%> names constructor", scope, name);
3211
      return NULL_TREE;
3212
    }
3213
  if (constructor_name_p (name, current_class_type))
3214
    {
3215
      error ("%<%T::%D%> names constructor in %qT",
3216
             scope, name, current_class_type);
3217
      return NULL_TREE;
3218
    }
3219
 
3220
  scope_dependent_p = dependent_scope_p (scope);
3221
  name_dependent_p = (scope_dependent_p
3222
                      || (IDENTIFIER_TYPENAME_P (name)
3223
                          && dependent_type_p (TREE_TYPE (name))));
3224
 
3225
  bases_dependent_p = false;
3226
  if (processing_template_decl)
3227
    for (binfo = TYPE_BINFO (current_class_type), i = 0;
3228
         BINFO_BASE_ITERATE (binfo, i, base_binfo);
3229
         i++)
3230
      if (dependent_type_p (TREE_TYPE (base_binfo)))
3231
        {
3232
          bases_dependent_p = true;
3233
          break;
3234
        }
3235
 
3236
  decl = NULL_TREE;
3237
 
3238
  /* From [namespace.udecl]:
3239
 
3240
       A using-declaration used as a member-declaration shall refer to a
3241
       member of a base class of the class being defined.
3242
 
3243
     In general, we cannot check this constraint in a template because
3244
     we do not know the entire set of base classes of the current
3245
     class type. Morover, if SCOPE is dependent, it might match a
3246
     non-dependent base.  */
3247
 
3248
  if (!scope_dependent_p)
3249
    {
3250
      base_kind b_kind;
3251
      binfo = lookup_base (current_class_type, scope, ba_any, &b_kind);
3252
      if (b_kind < bk_proper_base)
3253
        {
3254
          if (!bases_dependent_p)
3255
            {
3256
              error_not_base_type (scope, current_class_type);
3257
              return NULL_TREE;
3258
            }
3259
        }
3260
      else if (!name_dependent_p)
3261
        {
3262
          decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
3263
          if (!decl)
3264
            {
3265
              error ("no members matching %<%T::%D%> in %q#T", scope, name,
3266
                     scope);
3267
              return NULL_TREE;
3268
            }
3269
          /* The binfo from which the functions came does not matter.  */
3270
          if (BASELINK_P (decl))
3271
            decl = BASELINK_FUNCTIONS (decl);
3272
        }
3273
    }
3274
 
3275
  value = build_lang_decl (USING_DECL, name, NULL_TREE);
3276
  USING_DECL_DECLS (value) = decl;
3277
  USING_DECL_SCOPE (value) = scope;
3278
  DECL_DEPENDENT_P (value) = !decl;
3279
 
3280
  return value;
3281
}
3282
 
3283
 
3284
/* Return the binding value for name in scope.  */
3285
 
3286
 
3287
static tree
3288
namespace_binding_1 (tree name, tree scope)
3289
{
3290
  cxx_binding *binding;
3291
 
3292
  if (SCOPE_FILE_SCOPE_P (scope))
3293
    scope = global_namespace;
3294
  else
3295
    /* Unnecessary for the global namespace because it can't be an alias. */
3296
    scope = ORIGINAL_NAMESPACE (scope);
3297
 
3298
  binding = cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
3299
 
3300
  return binding ? binding->value : NULL_TREE;
3301
}
3302
 
3303
tree
3304
namespace_binding (tree name, tree scope)
3305
{
3306
  tree ret;
3307
  bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3308
  ret = namespace_binding_1 (name, scope);
3309
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3310
  return ret;
3311
}
3312
 
3313
/* Set the binding value for name in scope.  */
3314
 
3315
static void
3316
set_namespace_binding_1 (tree name, tree scope, tree val)
3317
{
3318
  cxx_binding *b;
3319
 
3320
  if (scope == NULL_TREE)
3321
    scope = global_namespace;
3322
  b = binding_for_name (NAMESPACE_LEVEL (scope), name);
3323
  if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
3324
    b->value = val;
3325
  else
3326
    supplement_binding (b, val);
3327
}
3328
 
3329
/* Wrapper for set_namespace_binding_1.  */
3330
 
3331
void
3332
set_namespace_binding (tree name, tree scope, tree val)
3333
{
3334
  bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3335
  set_namespace_binding_1 (name, scope, val);
3336
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3337
}
3338
 
3339
/* Set the context of a declaration to scope. Complain if we are not
3340
   outside scope.  */
3341
 
3342
void
3343
set_decl_namespace (tree decl, tree scope, bool friendp)
3344
{
3345
  tree old;
3346
 
3347
  /* Get rid of namespace aliases.  */
3348
  scope = ORIGINAL_NAMESPACE (scope);
3349
 
3350
  /* It is ok for friends to be qualified in parallel space.  */
3351
  if (!friendp && !is_ancestor (current_namespace, scope))
3352
    error ("declaration of %qD not in a namespace surrounding %qD",
3353
           decl, scope);
3354
  DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3355
 
3356
  /* Writing "int N::i" to declare a variable within "N" is invalid.  */
3357
  if (scope == current_namespace)
3358
    {
3359
      if (at_namespace_scope_p ())
3360
        error ("explicit qualification in declaration of %qD",
3361
               decl);
3362
      return;
3363
    }
3364
 
3365
  /* See whether this has been declared in the namespace.  */
3366
  old = lookup_qualified_name (scope, DECL_NAME (decl), false, true);
3367
  if (old == error_mark_node)
3368
    /* No old declaration at all.  */
3369
    goto complain;
3370
  /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
3371
  if (TREE_CODE (old) == TREE_LIST)
3372
    {
3373
      error ("reference to %qD is ambiguous", decl);
3374
      print_candidates (old);
3375
      return;
3376
    }
3377
  if (!is_overloaded_fn (decl))
3378
    {
3379
      /* We might have found OLD in an inline namespace inside SCOPE.  */
3380
      if (TREE_CODE (decl) == TREE_CODE (old))
3381
        DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3382
      /* Don't compare non-function decls with decls_match here, since
3383
         it can't check for the correct constness at this
3384
         point. pushdecl will find those errors later.  */
3385
      return;
3386
    }
3387
  /* Since decl is a function, old should contain a function decl.  */
3388
  if (!is_overloaded_fn (old))
3389
    goto complain;
3390
  /* A template can be explicitly specialized in any namespace.  */
3391
  if (processing_explicit_instantiation)
3392
    return;
3393
  if (processing_template_decl || processing_specialization)
3394
    /* We have not yet called push_template_decl to turn a
3395
       FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
3396
       match.  But, we'll check later, when we construct the
3397
       template.  */
3398
    return;
3399
  /* Instantiations or specializations of templates may be declared as
3400
     friends in any namespace.  */
3401
  if (friendp && DECL_USE_TEMPLATE (decl))
3402
    return;
3403
  if (is_overloaded_fn (old))
3404
    {
3405
      tree found = NULL_TREE;
3406
      tree elt = old;
3407
      for (; elt; elt = OVL_NEXT (elt))
3408
        {
3409
          tree ofn = OVL_CURRENT (elt);
3410
          /* Adjust DECL_CONTEXT first so decls_match will return true
3411
             if DECL will match a declaration in an inline namespace.  */
3412
          DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
3413
          if (decls_match (decl, ofn))
3414
            {
3415
              if (found && !decls_match (found, ofn))
3416
                {
3417
                  DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3418
                  error ("reference to %qD is ambiguous", decl);
3419
                  print_candidates (old);
3420
                  return;
3421
                }
3422
              found = ofn;
3423
            }
3424
        }
3425
      if (found)
3426
        {
3427
          if (!is_associated_namespace (scope, CP_DECL_CONTEXT (found)))
3428
            goto complain;
3429
          DECL_CONTEXT (decl) = DECL_CONTEXT (found);
3430
          return;
3431
        }
3432
    }
3433
  else
3434
    {
3435
      DECL_CONTEXT (decl) = DECL_CONTEXT (old);
3436
      if (decls_match (decl, old))
3437
        return;
3438
    }
3439
 
3440
  /* It didn't work, go back to the explicit scope.  */
3441
  DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
3442
 complain:
3443
  error ("%qD should have been declared inside %qD", decl, scope);
3444
}
3445
 
3446
/* Return the namespace where the current declaration is declared.  */
3447
 
3448
tree
3449
current_decl_namespace (void)
3450
{
3451
  tree result;
3452
  /* If we have been pushed into a different namespace, use it.  */
3453
  if (!VEC_empty (tree, decl_namespace_list))
3454
    return VEC_last (tree, decl_namespace_list);
3455
 
3456
  if (current_class_type)
3457
    result = decl_namespace_context (current_class_type);
3458
  else if (current_function_decl)
3459
    result = decl_namespace_context (current_function_decl);
3460
  else
3461
    result = current_namespace;
3462
  return result;
3463
}
3464
 
3465
/* Process any ATTRIBUTES on a namespace definition.  Currently only
3466
   attribute visibility is meaningful, which is a property of the syntactic
3467
   block rather than the namespace as a whole, so we don't touch the
3468
   NAMESPACE_DECL at all.  Returns true if attribute visibility is seen.  */
3469
 
3470
bool
3471
handle_namespace_attrs (tree ns, tree attributes)
3472
{
3473
  tree d;
3474
  bool saw_vis = false;
3475
 
3476
  for (d = attributes; d; d = TREE_CHAIN (d))
3477
    {
3478
      tree name = TREE_PURPOSE (d);
3479
      tree args = TREE_VALUE (d);
3480
 
3481
      if (is_attribute_p ("visibility", name))
3482
        {
3483
          tree x = args ? TREE_VALUE (args) : NULL_TREE;
3484
          if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
3485
            {
3486
              warning (OPT_Wattributes,
3487
                       "%qD attribute requires a single NTBS argument",
3488
                       name);
3489
              continue;
3490
            }
3491
 
3492
          if (!TREE_PUBLIC (ns))
3493
            warning (OPT_Wattributes,
3494
                     "%qD attribute is meaningless since members of the "
3495
                     "anonymous namespace get local symbols", name);
3496
 
3497
          push_visibility (TREE_STRING_POINTER (x), 1);
3498
          saw_vis = true;
3499
        }
3500
      else
3501
        {
3502
          warning (OPT_Wattributes, "%qD attribute directive ignored",
3503
                   name);
3504
          continue;
3505
        }
3506
    }
3507
 
3508
  return saw_vis;
3509
}
3510
 
3511
/* Push into the scope of the NAME namespace.  If NAME is NULL_TREE, then we
3512
   select a name that is unique to this compilation unit.  */
3513
 
3514
void
3515
push_namespace (tree name)
3516
{
3517
  tree d = NULL_TREE;
3518
  int need_new = 1;
3519
  int implicit_use = 0;
3520
  bool anon = !name;
3521
 
3522
  bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3523
 
3524
  /* We should not get here if the global_namespace is not yet constructed
3525
     nor if NAME designates the global namespace:  The global scope is
3526
     constructed elsewhere.  */
3527
  gcc_assert (global_namespace != NULL && name != global_scope_name);
3528
 
3529
  if (anon)
3530
    {
3531
      name = get_anonymous_namespace_name();
3532
      d = IDENTIFIER_NAMESPACE_VALUE (name);
3533
      if (d)
3534
        /* Reopening anonymous namespace.  */
3535
        need_new = 0;
3536
      implicit_use = 1;
3537
    }
3538
  else
3539
    {
3540
      /* Check whether this is an extended namespace definition.  */
3541
      d = IDENTIFIER_NAMESPACE_VALUE (name);
3542
      if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
3543
        {
3544
          need_new = 0;
3545
          if (DECL_NAMESPACE_ALIAS (d))
3546
            {
3547
              error ("namespace alias %qD not allowed here, assuming %qD",
3548
                     d, DECL_NAMESPACE_ALIAS (d));
3549
              d = DECL_NAMESPACE_ALIAS (d);
3550
            }
3551
        }
3552
    }
3553
 
3554
  if (need_new)
3555
    {
3556
      /* Make a new namespace, binding the name to it.  */
3557
      d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
3558
      DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
3559
      /* The name of this namespace is not visible to other translation
3560
         units if it is an anonymous namespace or member thereof.  */
3561
      if (anon || decl_anon_ns_mem_p (current_namespace))
3562
        TREE_PUBLIC (d) = 0;
3563
      else
3564
        TREE_PUBLIC (d) = 1;
3565
      pushdecl (d);
3566
      if (anon)
3567
        {
3568
          /* Clear DECL_NAME for the benefit of debugging back ends.  */
3569
          SET_DECL_ASSEMBLER_NAME (d, name);
3570
          DECL_NAME (d) = NULL_TREE;
3571
        }
3572
      begin_scope (sk_namespace, d);
3573
    }
3574
  else
3575
    resume_scope (NAMESPACE_LEVEL (d));
3576
 
3577
  if (implicit_use)
3578
    do_using_directive (d);
3579
  /* Enter the name space.  */
3580
  current_namespace = d;
3581
 
3582
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3583
}
3584
 
3585
/* Pop from the scope of the current namespace.  */
3586
 
3587
void
3588
pop_namespace (void)
3589
{
3590
  gcc_assert (current_namespace != global_namespace);
3591
  current_namespace = CP_DECL_CONTEXT (current_namespace);
3592
  /* The binding level is not popped, as it might be re-opened later.  */
3593
  leave_scope ();
3594
}
3595
 
3596
/* Push into the scope of the namespace NS, even if it is deeply
3597
   nested within another namespace.  */
3598
 
3599
void
3600
push_nested_namespace (tree ns)
3601
{
3602
  if (ns == global_namespace)
3603
    push_to_top_level ();
3604
  else
3605
    {
3606
      push_nested_namespace (CP_DECL_CONTEXT (ns));
3607
      push_namespace (DECL_NAME (ns));
3608
    }
3609
}
3610
 
3611
/* Pop back from the scope of the namespace NS, which was previously
3612
   entered with push_nested_namespace.  */
3613
 
3614
void
3615
pop_nested_namespace (tree ns)
3616
{
3617
  bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3618
  gcc_assert (current_namespace == ns);
3619
  while (ns != global_namespace)
3620
    {
3621
      pop_namespace ();
3622
      ns = CP_DECL_CONTEXT (ns);
3623
    }
3624
 
3625
  pop_from_top_level ();
3626
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3627
}
3628
 
3629
/* Temporarily set the namespace for the current declaration.  */
3630
 
3631
void
3632
push_decl_namespace (tree decl)
3633
{
3634
  if (TREE_CODE (decl) != NAMESPACE_DECL)
3635
    decl = decl_namespace_context (decl);
3636
  VEC_safe_push (tree, gc, decl_namespace_list, ORIGINAL_NAMESPACE (decl));
3637
}
3638
 
3639
/* [namespace.memdef]/2 */
3640
 
3641
void
3642
pop_decl_namespace (void)
3643
{
3644
  VEC_pop (tree, decl_namespace_list);
3645
}
3646
 
3647
/* Return the namespace that is the common ancestor
3648
   of two given namespaces.  */
3649
 
3650
static tree
3651
namespace_ancestor_1 (tree ns1, tree ns2)
3652
{
3653
  tree nsr;
3654
  if (is_ancestor (ns1, ns2))
3655
    nsr = ns1;
3656
  else
3657
    nsr = namespace_ancestor_1 (CP_DECL_CONTEXT (ns1), ns2);
3658
  return nsr;
3659
}
3660
 
3661
/* Wrapper for namespace_ancestor_1.  */
3662
 
3663
static tree
3664
namespace_ancestor (tree ns1, tree ns2)
3665
{
3666
  tree nsr;
3667
  bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3668
  nsr = namespace_ancestor_1 (ns1, ns2);
3669
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3670
  return nsr;
3671
}
3672
 
3673
/* Process a namespace-alias declaration.  */
3674
 
3675
void
3676
do_namespace_alias (tree alias, tree name_space)
3677
{
3678
  if (name_space == error_mark_node)
3679
    return;
3680
 
3681
  gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
3682
 
3683
  name_space = ORIGINAL_NAMESPACE (name_space);
3684
 
3685
  /* Build the alias.  */
3686
  alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
3687
  DECL_NAMESPACE_ALIAS (alias) = name_space;
3688
  DECL_EXTERNAL (alias) = 1;
3689
  DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
3690
  pushdecl (alias);
3691
 
3692
  /* Emit debug info for namespace alias.  */
3693
  if (!building_stmt_list_p ())
3694
    (*debug_hooks->global_decl) (alias);
3695
}
3696
 
3697
/* Like pushdecl, only it places X in the current namespace,
3698
   if appropriate.  */
3699
 
3700
tree
3701
pushdecl_namespace_level (tree x, bool is_friend)
3702
{
3703
  cp_binding_level *b = current_binding_level;
3704
  tree t;
3705
 
3706
  bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3707
  t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend);
3708
 
3709
  /* Now, the type_shadowed stack may screw us.  Munge it so it does
3710
     what we want.  */
3711
  if (TREE_CODE (t) == TYPE_DECL)
3712
    {
3713
      tree name = DECL_NAME (t);
3714
      tree newval;
3715
      tree *ptr = (tree *)0;
3716
      for (; !global_scope_p (b); b = b->level_chain)
3717
        {
3718
          tree shadowed = b->type_shadowed;
3719
          for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3720
            if (TREE_PURPOSE (shadowed) == name)
3721
              {
3722
                ptr = &TREE_VALUE (shadowed);
3723
                /* Can't break out of the loop here because sometimes
3724
                   a binding level will have duplicate bindings for
3725
                   PT names.  It's gross, but I haven't time to fix it.  */
3726
              }
3727
        }
3728
      newval = TREE_TYPE (t);
3729
      if (ptr == (tree *)0)
3730
        {
3731
          /* @@ This shouldn't be needed.  My test case "zstring.cc" trips
3732
             up here if this is changed to an assertion.  --KR  */
3733
          SET_IDENTIFIER_TYPE_VALUE (name, t);
3734
        }
3735
      else
3736
        {
3737
          *ptr = newval;
3738
        }
3739
    }
3740
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3741
  return t;
3742
}
3743
 
3744
/* Insert USED into the using list of USER. Set INDIRECT_flag if this
3745
   directive is not directly from the source. Also find the common
3746
   ancestor and let our users know about the new namespace */
3747
 
3748
static void
3749
add_using_namespace_1 (tree user, tree used, bool indirect)
3750
{
3751
  tree t;
3752
  /* Using oneself is a no-op.  */
3753
  if (user == used)
3754
    return;
3755
  gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
3756
  gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
3757
  /* Check if we already have this.  */
3758
  t = purpose_member (used, DECL_NAMESPACE_USING (user));
3759
  if (t != NULL_TREE)
3760
    {
3761
      if (!indirect)
3762
        /* Promote to direct usage.  */
3763
        TREE_INDIRECT_USING (t) = 0;
3764
      return;
3765
    }
3766
 
3767
  /* Add used to the user's using list.  */
3768
  DECL_NAMESPACE_USING (user)
3769
    = tree_cons (used, namespace_ancestor (user, used),
3770
                 DECL_NAMESPACE_USING (user));
3771
 
3772
  TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
3773
 
3774
  /* Add user to the used's users list.  */
3775
  DECL_NAMESPACE_USERS (used)
3776
    = tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
3777
 
3778
  /* Recursively add all namespaces used.  */
3779
  for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
3780
    /* indirect usage */
3781
    add_using_namespace_1 (user, TREE_PURPOSE (t), 1);
3782
 
3783
  /* Tell everyone using us about the new used namespaces.  */
3784
  for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
3785
    add_using_namespace_1 (TREE_PURPOSE (t), used, 1);
3786
}
3787
 
3788
/* Wrapper for add_using_namespace_1.  */
3789
 
3790
static void
3791
add_using_namespace (tree user, tree used, bool indirect)
3792
{
3793
  bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3794
  add_using_namespace_1 (user, used, indirect);
3795
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3796
}
3797
 
3798
/* Process a using-declaration not appearing in class or local scope.  */
3799
 
3800
void
3801
do_toplevel_using_decl (tree decl, tree scope, tree name)
3802
{
3803
  tree oldval, oldtype, newval, newtype;
3804
  tree orig_decl = decl;
3805
  cxx_binding *binding;
3806
 
3807
  decl = validate_nonmember_using_decl (decl, scope, name);
3808
  if (decl == NULL_TREE)
3809
    return;
3810
 
3811
  binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
3812
 
3813
  oldval = binding->value;
3814
  oldtype = binding->type;
3815
 
3816
  do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
3817
 
3818
  /* Emit debug info.  */
3819
  if (!processing_template_decl)
3820
    cp_emit_debug_info_for_using (orig_decl, current_namespace);
3821
 
3822
  /* Copy declarations found.  */
3823
  if (newval)
3824
    binding->value = newval;
3825
  if (newtype)
3826
    binding->type = newtype;
3827
}
3828
 
3829
/* Process a using-directive.  */
3830
 
3831
void
3832
do_using_directive (tree name_space)
3833
{
3834
  tree context = NULL_TREE;
3835
 
3836
  if (name_space == error_mark_node)
3837
    return;
3838
 
3839
  gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
3840
 
3841
  if (building_stmt_list_p ())
3842
    add_stmt (build_stmt (input_location, USING_STMT, name_space));
3843
  name_space = ORIGINAL_NAMESPACE (name_space);
3844
 
3845
  if (!toplevel_bindings_p ())
3846
    {
3847
      push_using_directive (name_space);
3848
    }
3849
  else
3850
    {
3851
      /* direct usage */
3852
      add_using_namespace (current_namespace, name_space, 0);
3853
      if (current_namespace != global_namespace)
3854
        context = current_namespace;
3855
 
3856
      /* Emit debugging info.  */
3857
      if (!processing_template_decl)
3858
        (*debug_hooks->imported_module_or_decl) (name_space, NULL_TREE,
3859
                                                 context, false);
3860
    }
3861
}
3862
 
3863
/* Deal with a using-directive seen by the parser.  Currently we only
3864
   handle attributes here, since they cannot appear inside a template.  */
3865
 
3866
void
3867
parse_using_directive (tree name_space, tree attribs)
3868
{
3869
  tree a;
3870
 
3871
  do_using_directive (name_space);
3872
 
3873
  for (a = attribs; a; a = TREE_CHAIN (a))
3874
    {
3875
      tree name = TREE_PURPOSE (a);
3876
      if (is_attribute_p ("strong", name))
3877
        {
3878
          if (!toplevel_bindings_p ())
3879
            error ("strong using only meaningful at namespace scope");
3880
          else if (name_space != error_mark_node)
3881
            {
3882
              if (!is_ancestor (current_namespace, name_space))
3883
                error ("current namespace %qD does not enclose strongly used namespace %qD",
3884
                       current_namespace, name_space);
3885
              DECL_NAMESPACE_ASSOCIATIONS (name_space)
3886
                = tree_cons (current_namespace, 0,
3887
                             DECL_NAMESPACE_ASSOCIATIONS (name_space));
3888
            }
3889
        }
3890
      else
3891
        warning (OPT_Wattributes, "%qD attribute directive ignored", name);
3892
    }
3893
}
3894
 
3895
/* Like pushdecl, only it places X in the global scope if appropriate.
3896
   Calls cp_finish_decl to register the variable, initializing it with
3897
   *INIT, if INIT is non-NULL.  */
3898
 
3899
static tree
3900
pushdecl_top_level_1 (tree x, tree *init, bool is_friend)
3901
{
3902
  bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
3903
  push_to_top_level ();
3904
  x = pushdecl_namespace_level (x, is_friend);
3905
  if (init)
3906
    cp_finish_decl (x, *init, false, NULL_TREE, 0);
3907
  pop_from_top_level ();
3908
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
3909
  return x;
3910
}
3911
 
3912
/* Like pushdecl, only it places X in the global scope if appropriate.  */
3913
 
3914
tree
3915
pushdecl_top_level (tree x)
3916
{
3917
  return pushdecl_top_level_1 (x, NULL, false);
3918
}
3919
 
3920
/* Like pushdecl_top_level, but adding the IS_FRIEND parameter.  */
3921
 
3922
tree
3923
pushdecl_top_level_maybe_friend (tree x, bool is_friend)
3924
{
3925
  return pushdecl_top_level_1 (x, NULL, is_friend);
3926
}
3927
 
3928
/* Like pushdecl, only it places X in the global scope if
3929
   appropriate.  Calls cp_finish_decl to register the variable,
3930
   initializing it with INIT.  */
3931
 
3932
tree
3933
pushdecl_top_level_and_finish (tree x, tree init)
3934
{
3935
  return pushdecl_top_level_1 (x, &init, false);
3936
}
3937
 
3938
/* Combines two sets of overloaded functions into an OVERLOAD chain, removing
3939
   duplicates.  The first list becomes the tail of the result.
3940
 
3941
   The algorithm is O(n^2).  We could get this down to O(n log n) by
3942
   doing a sort on the addresses of the functions, if that becomes
3943
   necessary.  */
3944
 
3945
static tree
3946
merge_functions (tree s1, tree s2)
3947
{
3948
  for (; s2; s2 = OVL_NEXT (s2))
3949
    {
3950
      tree fn2 = OVL_CURRENT (s2);
3951
      tree fns1;
3952
 
3953
      for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
3954
        {
3955
          tree fn1 = OVL_CURRENT (fns1);
3956
 
3957
          /* If the function from S2 is already in S1, there is no
3958
             need to add it again.  For `extern "C"' functions, we
3959
             might have two FUNCTION_DECLs for the same function, in
3960
             different namespaces, but let's leave them in in case
3961
             they have different default arguments.  */
3962
          if (fn1 == fn2)
3963
            break;
3964
        }
3965
 
3966
      /* If we exhausted all of the functions in S1, FN2 is new.  */
3967
      if (!fns1)
3968
        s1 = build_overload (fn2, s1);
3969
    }
3970
  return s1;
3971
}
3972
 
3973
/* Returns TRUE iff OLD and NEW are the same entity.
3974
 
3975
   3 [basic]/3: An entity is a value, object, reference, function,
3976
   enumerator, type, class member, template, template specialization,
3977
   namespace, parameter pack, or this.
3978
 
3979
   7.3.4 [namespace.udir]/4: If name lookup finds a declaration for a name
3980
   in two different namespaces, and the declarations do not declare the
3981
   same entity and do not declare functions, the use of the name is
3982
   ill-formed.  */
3983
 
3984
static bool
3985
same_entity_p (tree one, tree two)
3986
{
3987
  if (one == two)
3988
    return true;
3989
  if (!one || !two)
3990
    return false;
3991
  if (TREE_CODE (one) == TYPE_DECL
3992
      && TREE_CODE (two) == TYPE_DECL
3993
      && same_type_p (TREE_TYPE (one), TREE_TYPE (two)))
3994
    return true;
3995
  return false;
3996
}
3997
 
3998
/* This should return an error not all definitions define functions.
3999
   It is not an error if we find two functions with exactly the
4000
   same signature, only if these are selected in overload resolution.
4001
   old is the current set of bindings, new_binding the freshly-found binding.
4002
   XXX Do we want to give *all* candidates in case of ambiguity?
4003
   XXX In what way should I treat extern declarations?
4004
   XXX I don't want to repeat the entire duplicate_decls here */
4005
 
4006
static void
4007
ambiguous_decl (struct scope_binding *old, cxx_binding *new_binding, int flags)
4008
{
4009
  tree val, type;
4010
  gcc_assert (old != NULL);
4011
 
4012
  /* Copy the type.  */
4013
  type = new_binding->type;
4014
  if (LOOKUP_NAMESPACES_ONLY (flags)
4015
      || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN)))
4016
    type = NULL_TREE;
4017
 
4018
  /* Copy the value.  */
4019
  val = new_binding->value;
4020
  if (val)
4021
    {
4022
      if (hidden_name_p (val) && !(flags & LOOKUP_HIDDEN))
4023
        val = NULL_TREE;
4024
      else
4025
        switch (TREE_CODE (val))
4026
          {
4027
          case TEMPLATE_DECL:
4028
            /* If we expect types or namespaces, and not templates,
4029
               or this is not a template class.  */
4030
            if ((LOOKUP_QUALIFIERS_ONLY (flags)
4031
                 && !DECL_CLASS_TEMPLATE_P (val)))
4032
              val = NULL_TREE;
4033
            break;
4034
          case TYPE_DECL:
4035
            if (LOOKUP_NAMESPACES_ONLY (flags)
4036
                || (type && (flags & LOOKUP_PREFER_TYPES)))
4037
              val = NULL_TREE;
4038
            break;
4039
          case NAMESPACE_DECL:
4040
            if (LOOKUP_TYPES_ONLY (flags))
4041
              val = NULL_TREE;
4042
            break;
4043
          case FUNCTION_DECL:
4044
            /* Ignore built-in functions that are still anticipated.  */
4045
            if (LOOKUP_QUALIFIERS_ONLY (flags))
4046
              val = NULL_TREE;
4047
            break;
4048
          default:
4049
            if (LOOKUP_QUALIFIERS_ONLY (flags))
4050
              val = NULL_TREE;
4051
          }
4052
    }
4053
 
4054
  /* If val is hidden, shift down any class or enumeration name.  */
4055
  if (!val)
4056
    {
4057
      val = type;
4058
      type = NULL_TREE;
4059
    }
4060
 
4061
  if (!old->value)
4062
    old->value = val;
4063
  else if (val && !same_entity_p (val, old->value))
4064
    {
4065
      if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
4066
        old->value = merge_functions (old->value, val);
4067
      else
4068
        {
4069
          old->value = tree_cons (NULL_TREE, old->value,
4070
                                  build_tree_list (NULL_TREE, val));
4071
          TREE_TYPE (old->value) = error_mark_node;
4072
        }
4073
    }
4074
 
4075
  if (!old->type)
4076
    old->type = type;
4077
  else if (type && old->type != type)
4078
    {
4079
      old->type = tree_cons (NULL_TREE, old->type,
4080
                             build_tree_list (NULL_TREE, type));
4081
      TREE_TYPE (old->type) = error_mark_node;
4082
    }
4083
}
4084
 
4085
/* Return the declarations that are members of the namespace NS.  */
4086
 
4087
tree
4088
cp_namespace_decls (tree ns)
4089
{
4090
  return NAMESPACE_LEVEL (ns)->names;
4091
}
4092
 
4093
/* Combine prefer_type and namespaces_only into flags.  */
4094
 
4095
static int
4096
lookup_flags (int prefer_type, int namespaces_only)
4097
{
4098
  if (namespaces_only)
4099
    return LOOKUP_PREFER_NAMESPACES;
4100
  if (prefer_type > 1)
4101
    return LOOKUP_PREFER_TYPES;
4102
  if (prefer_type > 0)
4103
    return LOOKUP_PREFER_BOTH;
4104
  return 0;
4105
}
4106
 
4107
/* Given a lookup that returned VAL, use FLAGS to decide if we want to
4108
   ignore it or not.  Subroutine of lookup_name_real and
4109
   lookup_type_scope.  */
4110
 
4111
static bool
4112
qualify_lookup (tree val, int flags)
4113
{
4114
  if (val == NULL_TREE)
4115
    return false;
4116
  if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
4117
    return true;
4118
  if ((flags & LOOKUP_PREFER_TYPES)
4119
      && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL))
4120
    return true;
4121
  if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
4122
    return false;
4123
  /* Look through lambda things that we shouldn't be able to see.  */
4124
  if (is_lambda_ignored_entity (val))
4125
    return false;
4126
  return true;
4127
}
4128
 
4129
/* Given a lookup that returned VAL, decide if we want to ignore it or
4130
   not based on DECL_ANTICIPATED.  */
4131
 
4132
bool
4133
hidden_name_p (tree val)
4134
{
4135
  if (DECL_P (val)
4136
      && DECL_LANG_SPECIFIC (val)
4137
      && DECL_ANTICIPATED (val))
4138
    return true;
4139
  return false;
4140
}
4141
 
4142
/* Remove any hidden friend functions from a possibly overloaded set
4143
   of functions.  */
4144
 
4145
tree
4146
remove_hidden_names (tree fns)
4147
{
4148
  if (!fns)
4149
    return fns;
4150
 
4151
  if (TREE_CODE (fns) == FUNCTION_DECL && hidden_name_p (fns))
4152
    fns = NULL_TREE;
4153
  else if (TREE_CODE (fns) == OVERLOAD)
4154
    {
4155
      tree o;
4156
 
4157
      for (o = fns; o; o = OVL_NEXT (o))
4158
        if (hidden_name_p (OVL_CURRENT (o)))
4159
          break;
4160
      if (o)
4161
        {
4162
          tree n = NULL_TREE;
4163
 
4164
          for (o = fns; o; o = OVL_NEXT (o))
4165
            if (!hidden_name_p (OVL_CURRENT (o)))
4166
              n = build_overload (OVL_CURRENT (o), n);
4167
          fns = n;
4168
        }
4169
    }
4170
 
4171
  return fns;
4172
}
4173
 
4174
/* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name
4175
   lookup failed.  Search through all available namespaces and print out
4176
   possible candidates.  */
4177
 
4178
void
4179
suggest_alternatives_for (location_t location, tree name)
4180
{
4181
  VEC(tree,heap) *candidates = NULL;
4182
  VEC(tree,heap) *namespaces_to_search = NULL;
4183
  int max_to_search = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP);
4184
  int n_searched = 0;
4185
  tree t;
4186
  unsigned ix;
4187
 
4188
  VEC_safe_push (tree, heap, namespaces_to_search, global_namespace);
4189
 
4190
  while (!VEC_empty (tree, namespaces_to_search)
4191
         && n_searched < max_to_search)
4192
    {
4193
      tree scope = VEC_pop (tree, namespaces_to_search);
4194
      struct scope_binding binding = EMPTY_SCOPE_BINDING;
4195
      cp_binding_level *level = NAMESPACE_LEVEL (scope);
4196
 
4197
      /* Look in this namespace.  */
4198
      qualified_lookup_using_namespace (name, scope, &binding, 0);
4199
 
4200
      n_searched++;
4201
 
4202
      if (binding.value)
4203
        VEC_safe_push (tree, heap, candidates, binding.value);
4204
 
4205
      /* Add child namespaces.  */
4206
      for (t = level->namespaces; t; t = DECL_CHAIN (t))
4207
        VEC_safe_push (tree, heap, namespaces_to_search, t);
4208
    }
4209
 
4210
  /* If we stopped before we could examine all namespaces, inform the
4211
     user.  Do this even if we don't have any candidates, since there
4212
     might be more candidates further down that we weren't able to
4213
     find.  */
4214
  if (n_searched >= max_to_search
4215
      && !VEC_empty (tree, namespaces_to_search))
4216
    inform (location,
4217
            "maximum limit of %d namespaces searched for %qE",
4218
            max_to_search, name);
4219
 
4220
  VEC_free (tree, heap, namespaces_to_search);
4221
 
4222
  /* Nothing useful to report.  */
4223
  if (VEC_empty (tree, candidates))
4224
    return;
4225
 
4226
  inform_n (location, VEC_length (tree, candidates),
4227
            "suggested alternative:",
4228
            "suggested alternatives:");
4229
 
4230
  FOR_EACH_VEC_ELT (tree, candidates, ix, t)
4231
    inform (location_of (t), "  %qE", t);
4232
 
4233
  VEC_free (tree, heap, candidates);
4234
}
4235
 
4236
/* Unscoped lookup of a global: iterate over current namespaces,
4237
   considering using-directives.  */
4238
 
4239
static tree
4240
unqualified_namespace_lookup_1 (tree name, int flags)
4241
{
4242
  tree initial = current_decl_namespace ();
4243
  tree scope = initial;
4244
  tree siter;
4245
  cp_binding_level *level;
4246
  tree val = NULL_TREE;
4247
 
4248
  for (; !val; scope = CP_DECL_CONTEXT (scope))
4249
    {
4250
      struct scope_binding binding = EMPTY_SCOPE_BINDING;
4251
      cxx_binding *b =
4252
         cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4253
 
4254
      if (b)
4255
        ambiguous_decl (&binding, b, flags);
4256
 
4257
      /* Add all _DECLs seen through local using-directives.  */
4258
      for (level = current_binding_level;
4259
           level->kind != sk_namespace;
4260
           level = level->level_chain)
4261
        if (!lookup_using_namespace (name, &binding, level->using_directives,
4262
                                     scope, flags))
4263
          /* Give up because of error.  */
4264
          return error_mark_node;
4265
 
4266
      /* Add all _DECLs seen through global using-directives.  */
4267
      /* XXX local and global using lists should work equally.  */
4268
      siter = initial;
4269
      while (1)
4270
        {
4271
          if (!lookup_using_namespace (name, &binding,
4272
                                       DECL_NAMESPACE_USING (siter),
4273
                                       scope, flags))
4274
            /* Give up because of error.  */
4275
            return error_mark_node;
4276
          if (siter == scope) break;
4277
          siter = CP_DECL_CONTEXT (siter);
4278
        }
4279
 
4280
      val = binding.value;
4281
      if (scope == global_namespace)
4282
        break;
4283
    }
4284
  return val;
4285
}
4286
 
4287
/* Wrapper for unqualified_namespace_lookup_1.  */
4288
 
4289
static tree
4290
unqualified_namespace_lookup (tree name, int flags)
4291
{
4292
  tree ret;
4293
  bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4294
  ret = unqualified_namespace_lookup_1 (name, flags);
4295
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4296
  return ret;
4297
}
4298
 
4299
/* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
4300
   or a class TYPE).  If IS_TYPE_P is TRUE, then ignore non-type
4301
   bindings.
4302
 
4303
   Returns a DECL (or OVERLOAD, or BASELINK) representing the
4304
   declaration found.  If no suitable declaration can be found,
4305
   ERROR_MARK_NODE is returned.  If COMPLAIN is true and SCOPE is
4306
   neither a class-type nor a namespace a diagnostic is issued.  */
4307
 
4308
tree
4309
lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
4310
{
4311
  int flags = 0;
4312
  tree t = NULL_TREE;
4313
 
4314
  if (TREE_CODE (scope) == NAMESPACE_DECL)
4315
    {
4316
      struct scope_binding binding = EMPTY_SCOPE_BINDING;
4317
 
4318
      flags |= LOOKUP_COMPLAIN;
4319
      if (is_type_p)
4320
        flags |= LOOKUP_PREFER_TYPES;
4321
      if (qualified_lookup_using_namespace (name, scope, &binding, flags))
4322
        t = binding.value;
4323
    }
4324
  else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE)
4325
    t = lookup_enumerator (scope, name);
4326
  else if (is_class_type (scope, complain))
4327
    t = lookup_member (scope, name, 2, is_type_p, tf_warning_or_error);
4328
 
4329
  if (!t)
4330
    return error_mark_node;
4331
  return t;
4332
}
4333
 
4334
/* Subroutine of unqualified_namespace_lookup:
4335
   Add the bindings of NAME in used namespaces to VAL.
4336
   We are currently looking for names in namespace SCOPE, so we
4337
   look through USINGS for using-directives of namespaces
4338
   which have SCOPE as a common ancestor with the current scope.
4339
   Returns false on errors.  */
4340
 
4341
static bool
4342
lookup_using_namespace (tree name, struct scope_binding *val,
4343
                        tree usings, tree scope, int flags)
4344
{
4345
  tree iter;
4346
  bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4347
  /* Iterate over all used namespaces in current, searching for using
4348
     directives of scope.  */
4349
  for (iter = usings; iter; iter = TREE_CHAIN (iter))
4350
    if (TREE_VALUE (iter) == scope)
4351
      {
4352
        tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
4353
        cxx_binding *val1 =
4354
          cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (used), name);
4355
        /* Resolve ambiguities.  */
4356
        if (val1)
4357
          ambiguous_decl (val, val1, flags);
4358
      }
4359
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4360
  return val->value != error_mark_node;
4361
}
4362
 
4363
/* Returns true iff VEC contains TARGET.  */
4364
 
4365
static bool
4366
tree_vec_contains (VEC(tree,gc)* vec, tree target)
4367
{
4368
  unsigned int i;
4369
  tree elt;
4370
  FOR_EACH_VEC_ELT (tree,vec,i,elt)
4371
    if (elt == target)
4372
      return true;
4373
  return false;
4374
}
4375
 
4376
/* [namespace.qual]
4377
   Accepts the NAME to lookup and its qualifying SCOPE.
4378
   Returns the name/type pair found into the cxx_binding *RESULT,
4379
   or false on error.  */
4380
 
4381
static bool
4382
qualified_lookup_using_namespace (tree name, tree scope,
4383
                                  struct scope_binding *result, int flags)
4384
{
4385
  /* Maintain a list of namespaces visited...  */
4386
  VEC(tree,gc) *seen = NULL;
4387
  VEC(tree,gc) *seen_inline = NULL;
4388
  /* ... and a list of namespace yet to see.  */
4389
  VEC(tree,gc) *todo = NULL;
4390
  VEC(tree,gc) *todo_maybe = NULL;
4391
  VEC(tree,gc) *todo_inline = NULL;
4392
  tree usings;
4393
  timevar_start (TV_NAME_LOOKUP);
4394
  /* Look through namespace aliases.  */
4395
  scope = ORIGINAL_NAMESPACE (scope);
4396
 
4397
  /* Algorithm: Starting with SCOPE, walk through the set of used
4398
     namespaces.  For each used namespace, look through its inline
4399
     namespace set for any bindings and usings.  If no bindings are
4400
     found, add any usings seen to the set of used namespaces.  */
4401
  VEC_safe_push (tree, gc, todo, scope);
4402
 
4403
  while (VEC_length (tree, todo))
4404
    {
4405
      bool found_here;
4406
      scope = VEC_pop (tree, todo);
4407
      if (tree_vec_contains (seen, scope))
4408
        continue;
4409
      VEC_safe_push (tree, gc, seen, scope);
4410
      VEC_safe_push (tree, gc, todo_inline, scope);
4411
 
4412
      found_here = false;
4413
      while (VEC_length (tree, todo_inline))
4414
        {
4415
          cxx_binding *binding;
4416
 
4417
          scope = VEC_pop (tree, todo_inline);
4418
          if (tree_vec_contains (seen_inline, scope))
4419
            continue;
4420
          VEC_safe_push (tree, gc, seen_inline, scope);
4421
 
4422
          binding =
4423
            cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
4424
          if (binding)
4425
            {
4426
              found_here = true;
4427
              ambiguous_decl (result, binding, flags);
4428
            }
4429
 
4430
          for (usings = DECL_NAMESPACE_USING (scope); usings;
4431
               usings = TREE_CHAIN (usings))
4432
            if (!TREE_INDIRECT_USING (usings))
4433
              {
4434
                if (is_associated_namespace (scope, TREE_PURPOSE (usings)))
4435
                  VEC_safe_push (tree, gc, todo_inline, TREE_PURPOSE (usings));
4436
                else
4437
                  VEC_safe_push (tree, gc, todo_maybe, TREE_PURPOSE (usings));
4438
              }
4439
        }
4440
 
4441
      if (found_here)
4442
        VEC_truncate (tree, todo_maybe, 0);
4443
      else
4444
        while (VEC_length (tree, todo_maybe))
4445
          VEC_safe_push (tree, gc, todo, VEC_pop (tree, todo_maybe));
4446
    }
4447
  VEC_free (tree,gc,todo);
4448
  VEC_free (tree,gc,todo_maybe);
4449
  VEC_free (tree,gc,todo_inline);
4450
  VEC_free (tree,gc,seen);
4451
  VEC_free (tree,gc,seen_inline);
4452
  timevar_stop (TV_NAME_LOOKUP);
4453
  return result->value != error_mark_node;
4454
}
4455
 
4456
/* Subroutine of outer_binding.
4457
 
4458
   Returns TRUE if BINDING is a binding to a template parameter of
4459
   SCOPE.  In that case SCOPE is the scope of a primary template
4460
   parameter -- in the sense of G++, i.e, a template that has its own
4461
   template header.
4462
 
4463
   Returns FALSE otherwise.  */
4464
 
4465
static bool
4466
binding_to_template_parms_of_scope_p (cxx_binding *binding,
4467
                                      cp_binding_level *scope)
4468
{
4469
  tree binding_value;
4470
 
4471
  if (!binding || !scope)
4472
    return false;
4473
 
4474
  binding_value = binding->value ?  binding->value : binding->type;
4475
 
4476
  return (scope
4477
          && scope->this_entity
4478
          && get_template_info (scope->this_entity)
4479
          && PRIMARY_TEMPLATE_P (TI_TEMPLATE
4480
                                 (get_template_info (scope->this_entity)))
4481
          && parameter_of_template_p (binding_value,
4482
                                      TI_TEMPLATE (get_template_info \
4483
                                                    (scope->this_entity))));
4484
}
4485
 
4486
/* Return the innermost non-namespace binding for NAME from a scope
4487
   containing BINDING, or, if BINDING is NULL, the current scope.
4488
   Please note that for a given template, the template parameters are
4489
   considered to be in the scope containing the current scope.
4490
   If CLASS_P is false, then class bindings are ignored.  */
4491
 
4492
cxx_binding *
4493
outer_binding (tree name,
4494
               cxx_binding *binding,
4495
               bool class_p)
4496
{
4497
  cxx_binding *outer;
4498
  cp_binding_level *scope;
4499
  cp_binding_level *outer_scope;
4500
 
4501
  if (binding)
4502
    {
4503
      scope = binding->scope->level_chain;
4504
      outer = binding->previous;
4505
    }
4506
  else
4507
    {
4508
      scope = current_binding_level;
4509
      outer = IDENTIFIER_BINDING (name);
4510
    }
4511
  outer_scope = outer ? outer->scope : NULL;
4512
 
4513
  /* Because we create class bindings lazily, we might be missing a
4514
     class binding for NAME.  If there are any class binding levels
4515
     between the LAST_BINDING_LEVEL and the scope in which OUTER was
4516
     declared, we must lookup NAME in those class scopes.  */
4517
  if (class_p)
4518
    while (scope && scope != outer_scope && scope->kind != sk_namespace)
4519
      {
4520
        if (scope->kind == sk_class)
4521
          {
4522
            cxx_binding *class_binding;
4523
 
4524
            class_binding = get_class_binding (name, scope);
4525
            if (class_binding)
4526
              {
4527
                /* Thread this new class-scope binding onto the
4528
                   IDENTIFIER_BINDING list so that future lookups
4529
                   find it quickly.  */
4530
                class_binding->previous = outer;
4531
                if (binding)
4532
                  binding->previous = class_binding;
4533
                else
4534
                  IDENTIFIER_BINDING (name) = class_binding;
4535
                return class_binding;
4536
              }
4537
          }
4538
        /* If we are in a member template, the template parms of the member
4539
           template are considered to be inside the scope of the containing
4540
           class, but within G++ the class bindings are all pushed between the
4541
           template parms and the function body.  So if the outer binding is
4542
           a template parm for the current scope, return it now rather than
4543
           look for a class binding.  */
4544
        if (outer_scope && outer_scope->kind == sk_template_parms
4545
            && binding_to_template_parms_of_scope_p (outer, scope))
4546
          return outer;
4547
 
4548
        scope = scope->level_chain;
4549
      }
4550
 
4551
  return outer;
4552
}
4553
 
4554
/* Return the innermost block-scope or class-scope value binding for
4555
   NAME, or NULL_TREE if there is no such binding.  */
4556
 
4557
tree
4558
innermost_non_namespace_value (tree name)
4559
{
4560
  cxx_binding *binding;
4561
  binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
4562
  return binding ? binding->value : NULL_TREE;
4563
}
4564
 
4565
/* Look up NAME in the current binding level and its superiors in the
4566
   namespace of variables, functions and typedefs.  Return a ..._DECL
4567
   node of some kind representing its definition if there is only one
4568
   such declaration, or return a TREE_LIST with all the overloaded
4569
   definitions if there are many, or return 0 if it is undefined.
4570
   Hidden name, either friend declaration or built-in function, are
4571
   not ignored.
4572
 
4573
   If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
4574
   If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
4575
   Otherwise we prefer non-TYPE_DECLs.
4576
 
4577
   If NONCLASS is nonzero, bindings in class scopes are ignored.  If
4578
   BLOCK_P is false, bindings in block scopes are ignored.  */
4579
 
4580
static tree
4581
lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
4582
                    int namespaces_only, int flags)
4583
{
4584
  cxx_binding *iter;
4585
  tree val = NULL_TREE;
4586
 
4587
  /* Conversion operators are handled specially because ordinary
4588
     unqualified name lookup will not find template conversion
4589
     operators.  */
4590
  if (IDENTIFIER_TYPENAME_P (name))
4591
    {
4592
      cp_binding_level *level;
4593
 
4594
      for (level = current_binding_level;
4595
           level && level->kind != sk_namespace;
4596
           level = level->level_chain)
4597
        {
4598
          tree class_type;
4599
          tree operators;
4600
 
4601
          /* A conversion operator can only be declared in a class
4602
             scope.  */
4603
          if (level->kind != sk_class)
4604
            continue;
4605
 
4606
          /* Lookup the conversion operator in the class.  */
4607
          class_type = level->this_entity;
4608
          operators = lookup_fnfields (class_type, name, /*protect=*/0);
4609
          if (operators)
4610
            return operators;
4611
        }
4612
 
4613
      return NULL_TREE;
4614
    }
4615
 
4616
  flags |= lookup_flags (prefer_type, namespaces_only);
4617
 
4618
  /* First, look in non-namespace scopes.  */
4619
 
4620
  if (current_class_type == NULL_TREE)
4621
    nonclass = 1;
4622
 
4623
  if (block_p || !nonclass)
4624
    for (iter = outer_binding (name, NULL, !nonclass);
4625
         iter;
4626
         iter = outer_binding (name, iter, !nonclass))
4627
      {
4628
        tree binding;
4629
 
4630
        /* Skip entities we don't want.  */
4631
        if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
4632
          continue;
4633
 
4634
        /* If this is the kind of thing we're looking for, we're done.  */
4635
        if (qualify_lookup (iter->value, flags))
4636
          binding = iter->value;
4637
        else if ((flags & LOOKUP_PREFER_TYPES)
4638
                 && qualify_lookup (iter->type, flags))
4639
          binding = iter->type;
4640
        else
4641
          binding = NULL_TREE;
4642
 
4643
        if (binding)
4644
          {
4645
            if (hidden_name_p (binding))
4646
              {
4647
                /* A non namespace-scope binding can only be hidden in the
4648
                   presence of a local class, due to friend declarations.
4649
 
4650
                   In particular, consider:
4651
 
4652
                   struct C;
4653
                   void f() {
4654
                     struct A {
4655
                       friend struct B;
4656
                       friend struct C;
4657
                       void g() {
4658
                         B* b; // error: B is hidden
4659
                         C* c; // OK, finds ::C
4660
                       }
4661
                     };
4662
                     B *b;  // error: B is hidden
4663
                     C *c;  // OK, finds ::C
4664
                     struct B {};
4665
                     B *bb; // OK
4666
                   }
4667
 
4668
                   The standard says that "B" is a local class in "f"
4669
                   (but not nested within "A") -- but that name lookup
4670
                   for "B" does not find this declaration until it is
4671
                   declared directly with "f".
4672
 
4673
                   In particular:
4674
 
4675
                   [class.friend]
4676
 
4677
                   If a friend declaration appears in a local class and
4678
                   the name specified is an unqualified name, a prior
4679
                   declaration is looked up without considering scopes
4680
                   that are outside the innermost enclosing non-class
4681
                   scope. For a friend function declaration, if there is
4682
                   no prior declaration, the program is ill-formed. For a
4683
                   friend class declaration, if there is no prior
4684
                   declaration, the class that is specified belongs to the
4685
                   innermost enclosing non-class scope, but if it is
4686
                   subsequently referenced, its name is not found by name
4687
                   lookup until a matching declaration is provided in the
4688
                   innermost enclosing nonclass scope.
4689
 
4690
                   So just keep looking for a non-hidden binding.
4691
                */
4692
                gcc_assert (TREE_CODE (binding) == TYPE_DECL);
4693
                continue;
4694
              }
4695
            val = binding;
4696
            break;
4697
          }
4698
      }
4699
 
4700
  /* Now lookup in namespace scopes.  */
4701
  if (!val)
4702
    val = unqualified_namespace_lookup (name, flags);
4703
 
4704
  /* If we have a single function from a using decl, pull it out.  */
4705
  if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val))
4706
    val = OVL_FUNCTION (val);
4707
 
4708
  return val;
4709
}
4710
 
4711
/* Wrapper for lookup_name_real_1.  */
4712
 
4713
tree
4714
lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
4715
                  int namespaces_only, int flags)
4716
{
4717
  tree ret;
4718
  bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4719
  ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p,
4720
                            namespaces_only, flags);
4721
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4722
  return ret;
4723
}
4724
 
4725
tree
4726
lookup_name_nonclass (tree name)
4727
{
4728
  return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
4729
}
4730
 
4731
tree
4732
lookup_function_nonclass (tree name, VEC(tree,gc) *args, bool block_p)
4733
{
4734
  return
4735
    lookup_arg_dependent (name,
4736
                          lookup_name_real (name, 0, 1, block_p, 0,
4737
                                            LOOKUP_COMPLAIN),
4738
                          args, false);
4739
}
4740
 
4741
tree
4742
lookup_name (tree name)
4743
{
4744
  return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
4745
}
4746
 
4747
tree
4748
lookup_name_prefer_type (tree name, int prefer_type)
4749
{
4750
  return lookup_name_real (name, prefer_type, 0, /*block_p=*/true,
4751
                           0, LOOKUP_COMPLAIN);
4752
}
4753
 
4754
/* Look up NAME for type used in elaborated name specifier in
4755
   the scopes given by SCOPE.  SCOPE can be either TS_CURRENT or
4756
   TS_WITHIN_ENCLOSING_NON_CLASS.  Although not implied by the
4757
   name, more scopes are checked if cleanup or template parameter
4758
   scope is encountered.
4759
 
4760
   Unlike lookup_name_real, we make sure that NAME is actually
4761
   declared in the desired scope, not from inheritance, nor using
4762
   directive.  For using declaration, there is DR138 still waiting
4763
   to be resolved.  Hidden name coming from an earlier friend
4764
   declaration is also returned.
4765
 
4766
   A TYPE_DECL best matching the NAME is returned.  Catching error
4767
   and issuing diagnostics are caller's responsibility.  */
4768
 
4769
static tree
4770
lookup_type_scope_1 (tree name, tag_scope scope)
4771
{
4772
  cxx_binding *iter = NULL;
4773
  tree val = NULL_TREE;
4774
 
4775
  /* Look in non-namespace scope first.  */
4776
  if (current_binding_level->kind != sk_namespace)
4777
    iter = outer_binding (name, NULL, /*class_p=*/ true);
4778
  for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
4779
    {
4780
      /* Check if this is the kind of thing we're looking for.
4781
         If SCOPE is TS_CURRENT, also make sure it doesn't come from
4782
         base class.  For ITER->VALUE, we can simply use
4783
         INHERITED_VALUE_BINDING_P.  For ITER->TYPE, we have to use
4784
         our own check.
4785
 
4786
         We check ITER->TYPE before ITER->VALUE in order to handle
4787
           typedef struct C {} C;
4788
         correctly.  */
4789
 
4790
      if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
4791
          && (scope != ts_current
4792
              || LOCAL_BINDING_P (iter)
4793
              || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
4794
        val = iter->type;
4795
      else if ((scope != ts_current
4796
                || !INHERITED_VALUE_BINDING_P (iter))
4797
               && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4798
        val = iter->value;
4799
 
4800
      if (val)
4801
        break;
4802
    }
4803
 
4804
  /* Look in namespace scope.  */
4805
  if (!val)
4806
    {
4807
      iter = cp_binding_level_find_binding_for_name
4808
               (NAMESPACE_LEVEL (current_decl_namespace ()), name);
4809
 
4810
      if (iter)
4811
        {
4812
          /* If this is the kind of thing we're looking for, we're done.  */
4813
          if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
4814
            val = iter->type;
4815
          else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
4816
            val = iter->value;
4817
        }
4818
 
4819
    }
4820
 
4821
  /* Type found, check if it is in the allowed scopes, ignoring cleanup
4822
     and template parameter scopes.  */
4823
  if (val)
4824
    {
4825
      cp_binding_level *b = current_binding_level;
4826
      while (b)
4827
        {
4828
          if (iter->scope == b)
4829
            return val;
4830
 
4831
          if (b->kind == sk_cleanup || b->kind == sk_template_parms
4832
              || b->kind == sk_function_parms)
4833
            b = b->level_chain;
4834
          else if (b->kind == sk_class
4835
                   && scope == ts_within_enclosing_non_class)
4836
            b = b->level_chain;
4837
          else
4838
            break;
4839
        }
4840
    }
4841
 
4842
  return NULL_TREE;
4843
}
4844
 
4845
/* Wrapper for lookup_type_scope_1.  */
4846
 
4847
tree
4848
lookup_type_scope (tree name, tag_scope scope)
4849
{
4850
  tree ret;
4851
  bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4852
  ret = lookup_type_scope_1 (name, scope);
4853
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4854
  return ret;
4855
}
4856
 
4857
 
4858
/* Similar to `lookup_name' but look only in the innermost non-class
4859
   binding level.  */
4860
 
4861
static tree
4862
lookup_name_innermost_nonclass_level_1 (tree name)
4863
{
4864
  cp_binding_level *b;
4865
  tree t = NULL_TREE;
4866
 
4867
  b = innermost_nonclass_level ();
4868
 
4869
  if (b->kind == sk_namespace)
4870
    {
4871
      t = IDENTIFIER_NAMESPACE_VALUE (name);
4872
 
4873
      /* extern "C" function() */
4874
      if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
4875
        t = TREE_VALUE (t);
4876
    }
4877
  else if (IDENTIFIER_BINDING (name)
4878
           && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
4879
    {
4880
      cxx_binding *binding;
4881
      binding = IDENTIFIER_BINDING (name);
4882
      while (1)
4883
        {
4884
          if (binding->scope == b
4885
              && !(TREE_CODE (binding->value) == VAR_DECL
4886
                   && DECL_DEAD_FOR_LOCAL (binding->value)))
4887
            return binding->value;
4888
 
4889
          if (b->kind == sk_cleanup)
4890
            b = b->level_chain;
4891
          else
4892
            break;
4893
        }
4894
    }
4895
 
4896
  return t;
4897
}
4898
 
4899
/* Wrapper for lookup_name_innermost_nonclass_level_1.  */
4900
 
4901
tree
4902
lookup_name_innermost_nonclass_level (tree name)
4903
{
4904
  tree ret;
4905
  bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
4906
  ret = lookup_name_innermost_nonclass_level_1 (name);
4907
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
4908
  return ret;
4909
}
4910
 
4911
 
4912
/* Returns true iff DECL is a block-scope extern declaration of a function
4913
   or variable.  */
4914
 
4915
bool
4916
is_local_extern (tree decl)
4917
{
4918
  cxx_binding *binding;
4919
 
4920
  /* For functions, this is easy.  */
4921
  if (TREE_CODE (decl) == FUNCTION_DECL)
4922
    return DECL_LOCAL_FUNCTION_P (decl);
4923
 
4924
  if (TREE_CODE (decl) != VAR_DECL)
4925
    return false;
4926
  if (!current_function_decl)
4927
    return false;
4928
 
4929
  /* For variables, this is not easy.  We need to look at the binding stack
4930
     for the identifier to see whether the decl we have is a local.  */
4931
  for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
4932
       binding && binding->scope->kind != sk_namespace;
4933
       binding = binding->previous)
4934
    if (binding->value == decl)
4935
      return LOCAL_BINDING_P (binding);
4936
 
4937
  return false;
4938
}
4939
 
4940
/* Like lookup_name_innermost_nonclass_level, but for types.  */
4941
 
4942
static tree
4943
lookup_type_current_level (tree name)
4944
{
4945
  tree t = NULL_TREE;
4946
 
4947
  timevar_start (TV_NAME_LOOKUP);
4948
  gcc_assert (current_binding_level->kind != sk_namespace);
4949
 
4950
  if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
4951
      && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
4952
    {
4953
      cp_binding_level *b = current_binding_level;
4954
      while (1)
4955
        {
4956
          if (purpose_member (name, b->type_shadowed))
4957
            {
4958
              t = REAL_IDENTIFIER_TYPE_VALUE (name);
4959
              break;
4960
            }
4961
          if (b->kind == sk_cleanup)
4962
            b = b->level_chain;
4963
          else
4964
            break;
4965
        }
4966
    }
4967
 
4968
  timevar_stop (TV_NAME_LOOKUP);
4969
  return t;
4970
}
4971
 
4972
/* [basic.lookup.koenig] */
4973
/* A nonzero return value in the functions below indicates an error.  */
4974
 
4975
struct arg_lookup
4976
{
4977
  tree name;
4978
  VEC(tree,gc) *args;
4979
  VEC(tree,gc) *namespaces;
4980
  VEC(tree,gc) *classes;
4981
  tree functions;
4982
  struct pointer_set_t *fn_set;
4983
};
4984
 
4985
static bool arg_assoc (struct arg_lookup*, tree);
4986
static bool arg_assoc_args (struct arg_lookup*, tree);
4987
static bool arg_assoc_args_vec (struct arg_lookup*, VEC(tree,gc) *);
4988
static bool arg_assoc_type (struct arg_lookup*, tree);
4989
static bool add_function (struct arg_lookup *, tree);
4990
static bool arg_assoc_namespace (struct arg_lookup *, tree);
4991
static bool arg_assoc_class_only (struct arg_lookup *, tree);
4992
static bool arg_assoc_bases (struct arg_lookup *, tree);
4993
static bool arg_assoc_class (struct arg_lookup *, tree);
4994
static bool arg_assoc_template_arg (struct arg_lookup*, tree);
4995
 
4996
/* Add a function to the lookup structure.
4997
   Returns true on error.  */
4998
 
4999
static bool
5000
add_function (struct arg_lookup *k, tree fn)
5001
{
5002
  if (!is_overloaded_fn (fn))
5003
    /* All names except those of (possibly overloaded) functions and
5004
       function templates are ignored.  */;
5005
  else if (k->fn_set && pointer_set_insert (k->fn_set, fn))
5006
    /* It's already in the list.  */;
5007
  else if (!k->functions)
5008
    k->functions = fn;
5009
  else if (fn == k->functions)
5010
    ;
5011
  else
5012
    {
5013
      k->functions = build_overload (fn, k->functions);
5014
      if (TREE_CODE (k->functions) == OVERLOAD)
5015
        OVL_ARG_DEPENDENT (k->functions) = true;
5016
    }
5017
 
5018
  return false;
5019
}
5020
 
5021
/* Returns true iff CURRENT has declared itself to be an associated
5022
   namespace of SCOPE via a strong using-directive (or transitive chain
5023
   thereof).  Both are namespaces.  */
5024
 
5025
bool
5026
is_associated_namespace (tree current, tree scope)
5027
{
5028
  VEC(tree,gc) *seen = make_tree_vector ();
5029
  VEC(tree,gc) *todo = make_tree_vector ();
5030
  tree t;
5031
  bool ret;
5032
 
5033
  while (1)
5034
    {
5035
      if (scope == current)
5036
        {
5037
          ret = true;
5038
          break;
5039
        }
5040
      VEC_safe_push (tree, gc, seen, scope);
5041
      for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
5042
        if (!vec_member (TREE_PURPOSE (t), seen))
5043
          VEC_safe_push (tree, gc, todo, TREE_PURPOSE (t));
5044
      if (!VEC_empty (tree, todo))
5045
        {
5046
          scope = VEC_last (tree, todo);
5047
          VEC_pop (tree, todo);
5048
        }
5049
      else
5050
        {
5051
          ret = false;
5052
          break;
5053
        }
5054
    }
5055
 
5056
  release_tree_vector (seen);
5057
  release_tree_vector (todo);
5058
 
5059
  return ret;
5060
}
5061
 
5062
/* Add functions of a namespace to the lookup structure.
5063
   Returns true on error.  */
5064
 
5065
static bool
5066
arg_assoc_namespace (struct arg_lookup *k, tree scope)
5067
{
5068
  tree value;
5069
 
5070
  if (vec_member (scope, k->namespaces))
5071
    return false;
5072
  VEC_safe_push (tree, gc, k->namespaces, scope);
5073
 
5074
  /* Check out our super-users.  */
5075
  for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
5076
       value = TREE_CHAIN (value))
5077
    if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
5078
      return true;
5079
 
5080
  /* Also look down into inline namespaces.  */
5081
  for (value = DECL_NAMESPACE_USING (scope); value;
5082
       value = TREE_CHAIN (value))
5083
    if (is_associated_namespace (scope, TREE_PURPOSE (value)))
5084
      if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
5085
        return true;
5086
 
5087
  value = namespace_binding (k->name, scope);
5088
  if (!value)
5089
    return false;
5090
 
5091
  for (; value; value = OVL_NEXT (value))
5092
    {
5093
      /* We don't want to find arbitrary hidden functions via argument
5094
         dependent lookup.  We only want to find friends of associated
5095
         classes, which we'll do via arg_assoc_class.  */
5096
      if (hidden_name_p (OVL_CURRENT (value)))
5097
        continue;
5098
 
5099
      if (add_function (k, OVL_CURRENT (value)))
5100
        return true;
5101
    }
5102
 
5103
  return false;
5104
}
5105
 
5106
/* Adds everything associated with a template argument to the lookup
5107
   structure.  Returns true on error.  */
5108
 
5109
static bool
5110
arg_assoc_template_arg (struct arg_lookup *k, tree arg)
5111
{
5112
  /* [basic.lookup.koenig]
5113
 
5114
     If T is a template-id, its associated namespaces and classes are
5115
     ... the namespaces and classes associated with the types of the
5116
     template arguments provided for template type parameters
5117
     (excluding template template parameters); the namespaces in which
5118
     any template template arguments are defined; and the classes in
5119
     which any member templates used as template template arguments
5120
     are defined.  [Note: non-type template arguments do not
5121
     contribute to the set of associated namespaces.  ]  */
5122
 
5123
  /* Consider first template template arguments.  */
5124
  if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5125
      || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
5126
    return false;
5127
  else if (TREE_CODE (arg) == TEMPLATE_DECL)
5128
    {
5129
      tree ctx = CP_DECL_CONTEXT (arg);
5130
 
5131
      /* It's not a member template.  */
5132
      if (TREE_CODE (ctx) == NAMESPACE_DECL)
5133
        return arg_assoc_namespace (k, ctx);
5134
      /* Otherwise, it must be member template.  */
5135
      else
5136
        return arg_assoc_class_only (k, ctx);
5137
    }
5138
  /* It's an argument pack; handle it recursively.  */
5139
  else if (ARGUMENT_PACK_P (arg))
5140
    {
5141
      tree args = ARGUMENT_PACK_ARGS (arg);
5142
      int i, len = TREE_VEC_LENGTH (args);
5143
      for (i = 0; i < len; ++i)
5144
        if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, i)))
5145
          return true;
5146
 
5147
      return false;
5148
    }
5149
  /* It's not a template template argument, but it is a type template
5150
     argument.  */
5151
  else if (TYPE_P (arg))
5152
    return arg_assoc_type (k, arg);
5153
  /* It's a non-type template argument.  */
5154
  else
5155
    return false;
5156
}
5157
 
5158
/* Adds the class and its friends to the lookup structure.
5159
   Returns true on error.  */
5160
 
5161
static bool
5162
arg_assoc_class_only (struct arg_lookup *k, tree type)
5163
{
5164
  tree list, friends, context;
5165
 
5166
  /* Backend-built structures, such as __builtin_va_list, aren't
5167
     affected by all this.  */
5168
  if (!CLASS_TYPE_P (type))
5169
    return false;
5170
 
5171
  context = decl_namespace_context (type);
5172
  if (arg_assoc_namespace (k, context))
5173
    return true;
5174
 
5175
  complete_type (type);
5176
 
5177
  /* Process friends.  */
5178
  for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
5179
       list = TREE_CHAIN (list))
5180
    if (k->name == FRIEND_NAME (list))
5181
      for (friends = FRIEND_DECLS (list); friends;
5182
           friends = TREE_CHAIN (friends))
5183
        {
5184
          tree fn = TREE_VALUE (friends);
5185
 
5186
          /* Only interested in global functions with potentially hidden
5187
             (i.e. unqualified) declarations.  */
5188
          if (CP_DECL_CONTEXT (fn) != context)
5189
            continue;
5190
          /* Template specializations are never found by name lookup.
5191
             (Templates themselves can be found, but not template
5192
             specializations.)  */
5193
          if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
5194
            continue;
5195
          if (add_function (k, fn))
5196
            return true;
5197
        }
5198
 
5199
  return false;
5200
}
5201
 
5202
/* Adds the class and its bases to the lookup structure.
5203
   Returns true on error.  */
5204
 
5205
static bool
5206
arg_assoc_bases (struct arg_lookup *k, tree type)
5207
{
5208
  if (arg_assoc_class_only (k, type))
5209
    return true;
5210
 
5211
  if (TYPE_BINFO (type))
5212
    {
5213
      /* Process baseclasses.  */
5214
      tree binfo, base_binfo;
5215
      int i;
5216
 
5217
      for (binfo = TYPE_BINFO (type), i = 0;
5218
           BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
5219
        if (arg_assoc_bases (k, BINFO_TYPE (base_binfo)))
5220
          return true;
5221
    }
5222
 
5223
  return false;
5224
}
5225
 
5226
/* Adds everything associated with a class argument type to the lookup
5227
   structure.  Returns true on error.
5228
 
5229
   If T is a class type (including unions), its associated classes are: the
5230
   class itself; the class of which it is a member, if any; and its direct
5231
   and indirect base classes. Its associated namespaces are the namespaces
5232
   of which its associated classes are members. Furthermore, if T is a
5233
   class template specialization, its associated namespaces and classes
5234
   also include: the namespaces and classes associated with the types of
5235
   the template arguments provided for template type parameters (excluding
5236
   template template parameters); the namespaces of which any template
5237
   template arguments are members; and the classes of which any member
5238
   templates used as template template arguments are members. [ Note:
5239
   non-type template arguments do not contribute to the set of associated
5240
   namespaces.  --end note] */
5241
 
5242
static bool
5243
arg_assoc_class (struct arg_lookup *k, tree type)
5244
{
5245
  tree list;
5246
  int i;
5247
 
5248
  /* Backend build structures, such as __builtin_va_list, aren't
5249
     affected by all this.  */
5250
  if (!CLASS_TYPE_P (type))
5251
    return false;
5252
 
5253
  if (vec_member (type, k->classes))
5254
    return false;
5255
  VEC_safe_push (tree, gc, k->classes, type);
5256
 
5257
  if (TYPE_CLASS_SCOPE_P (type)
5258
      && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5259
    return true;
5260
 
5261
  if (arg_assoc_bases (k, type))
5262
    return true;
5263
 
5264
  /* Process template arguments.  */
5265
  if (CLASSTYPE_TEMPLATE_INFO (type)
5266
      && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
5267
    {
5268
      list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
5269
      for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
5270
        if (arg_assoc_template_arg (k, TREE_VEC_ELT (list, i)))
5271
          return true;
5272
    }
5273
 
5274
  return false;
5275
}
5276
 
5277
/* Adds everything associated with a given type.
5278
   Returns 1 on error.  */
5279
 
5280
static bool
5281
arg_assoc_type (struct arg_lookup *k, tree type)
5282
{
5283
  /* As we do not get the type of non-type dependent expressions
5284
     right, we can end up with such things without a type.  */
5285
  if (!type)
5286
    return false;
5287
 
5288
  if (TYPE_PTRMEM_P (type))
5289
    {
5290
      /* Pointer to member: associate class type and value type.  */
5291
      if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
5292
        return true;
5293
      return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
5294
    }
5295
  else switch (TREE_CODE (type))
5296
    {
5297
    case ERROR_MARK:
5298
      return false;
5299
    case VOID_TYPE:
5300
    case INTEGER_TYPE:
5301
    case REAL_TYPE:
5302
    case COMPLEX_TYPE:
5303
    case VECTOR_TYPE:
5304
    case BOOLEAN_TYPE:
5305
    case FIXED_POINT_TYPE:
5306
    case DECLTYPE_TYPE:
5307
    case NULLPTR_TYPE:
5308
      return false;
5309
    case RECORD_TYPE:
5310
      if (TYPE_PTRMEMFUNC_P (type))
5311
        return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
5312
    case UNION_TYPE:
5313
      return arg_assoc_class (k, type);
5314
    case POINTER_TYPE:
5315
    case REFERENCE_TYPE:
5316
    case ARRAY_TYPE:
5317
      return arg_assoc_type (k, TREE_TYPE (type));
5318
    case ENUMERAL_TYPE:
5319
      if (TYPE_CLASS_SCOPE_P (type)
5320
          && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
5321
        return true;
5322
      return arg_assoc_namespace (k, decl_namespace_context (type));
5323
    case METHOD_TYPE:
5324
      /* The basetype is referenced in the first arg type, so just
5325
         fall through.  */
5326
    case FUNCTION_TYPE:
5327
      /* Associate the parameter types.  */
5328
      if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
5329
        return true;
5330
      /* Associate the return type.  */
5331
      return arg_assoc_type (k, TREE_TYPE (type));
5332
    case TEMPLATE_TYPE_PARM:
5333
    case BOUND_TEMPLATE_TEMPLATE_PARM:
5334
      return false;
5335
    case TYPENAME_TYPE:
5336
      return false;
5337
    case LANG_TYPE:
5338
      gcc_assert (type == unknown_type_node
5339
                  || type == init_list_type_node);
5340
      return false;
5341
    case TYPE_PACK_EXPANSION:
5342
      return arg_assoc_type (k, PACK_EXPANSION_PATTERN (type));
5343
 
5344
    default:
5345
      gcc_unreachable ();
5346
    }
5347
  return false;
5348
}
5349
 
5350
/* Adds everything associated with arguments.  Returns true on error.  */
5351
 
5352
static bool
5353
arg_assoc_args (struct arg_lookup *k, tree args)
5354
{
5355
  for (; args; args = TREE_CHAIN (args))
5356
    if (arg_assoc (k, TREE_VALUE (args)))
5357
      return true;
5358
  return false;
5359
}
5360
 
5361
/* Adds everything associated with an argument vector.  Returns true
5362
   on error.  */
5363
 
5364
static bool
5365
arg_assoc_args_vec (struct arg_lookup *k, VEC(tree,gc) *args)
5366
{
5367
  unsigned int ix;
5368
  tree arg;
5369
 
5370
  FOR_EACH_VEC_ELT (tree, args, ix, arg)
5371
    if (arg_assoc (k, arg))
5372
      return true;
5373
  return false;
5374
}
5375
 
5376
/* Adds everything associated with a given tree_node.  Returns 1 on error.  */
5377
 
5378
static bool
5379
arg_assoc (struct arg_lookup *k, tree n)
5380
{
5381
  if (n == error_mark_node)
5382
    return false;
5383
 
5384
  if (TYPE_P (n))
5385
    return arg_assoc_type (k, n);
5386
 
5387
  if (! type_unknown_p (n))
5388
    return arg_assoc_type (k, TREE_TYPE (n));
5389
 
5390
  if (TREE_CODE (n) == ADDR_EXPR)
5391
    n = TREE_OPERAND (n, 0);
5392
  if (TREE_CODE (n) == COMPONENT_REF)
5393
    n = TREE_OPERAND (n, 1);
5394
  if (TREE_CODE (n) == OFFSET_REF)
5395
    n = TREE_OPERAND (n, 1);
5396
  while (TREE_CODE (n) == TREE_LIST)
5397
    n = TREE_VALUE (n);
5398
  if (BASELINK_P (n))
5399
    n = BASELINK_FUNCTIONS (n);
5400
 
5401
  if (TREE_CODE (n) == FUNCTION_DECL)
5402
    return arg_assoc_type (k, TREE_TYPE (n));
5403
  if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
5404
    {
5405
      /* The working paper doesn't currently say how to handle template-id
5406
         arguments.  The sensible thing would seem to be to handle the list
5407
         of template candidates like a normal overload set, and handle the
5408
         template arguments like we do for class template
5409
         specializations.  */
5410
      tree templ = TREE_OPERAND (n, 0);
5411
      tree args = TREE_OPERAND (n, 1);
5412
      int ix;
5413
 
5414
      /* First the templates.  */
5415
      if (arg_assoc (k, templ))
5416
        return true;
5417
 
5418
      /* Now the arguments.  */
5419
      if (args)
5420
        for (ix = TREE_VEC_LENGTH (args); ix--;)
5421
          if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
5422
            return true;
5423
    }
5424
  else if (TREE_CODE (n) == OVERLOAD)
5425
    {
5426
      for (; n; n = OVL_NEXT (n))
5427
        if (arg_assoc_type (k, TREE_TYPE (OVL_CURRENT (n))))
5428
          return true;
5429
    }
5430
 
5431
  return false;
5432
}
5433
 
5434
/* Performs Koenig lookup depending on arguments, where fns
5435
   are the functions found in normal lookup.  */
5436
 
5437
static tree
5438
lookup_arg_dependent_1 (tree name, tree fns, VEC(tree,gc) *args,
5439
                        bool include_std)
5440
{
5441
  struct arg_lookup k;
5442
 
5443
  /* Remove any hidden friend functions from the list of functions
5444
     found so far.  They will be added back by arg_assoc_class as
5445
     appropriate.  */
5446
  fns = remove_hidden_names (fns);
5447
 
5448
  k.name = name;
5449
  k.args = args;
5450
  k.functions = fns;
5451
  k.classes = make_tree_vector ();
5452
 
5453
  /* We previously performed an optimization here by setting
5454
     NAMESPACES to the current namespace when it was safe. However, DR
5455
     164 says that namespaces that were already searched in the first
5456
     stage of template processing are searched again (potentially
5457
     picking up later definitions) in the second stage. */
5458
  k.namespaces = make_tree_vector ();
5459
 
5460
  /* We used to allow duplicates and let joust discard them, but
5461
     since the above change for DR 164 we end up with duplicates of
5462
     all the functions found by unqualified lookup.  So keep track
5463
     of which ones we've seen.  */
5464
  if (fns)
5465
    {
5466
      tree ovl;
5467
      /* We shouldn't be here if lookup found something other than
5468
         namespace-scope functions.  */
5469
      gcc_assert (DECL_NAMESPACE_SCOPE_P (OVL_CURRENT (fns)));
5470
      k.fn_set = pointer_set_create ();
5471
      for (ovl = fns; ovl; ovl = OVL_NEXT (ovl))
5472
        pointer_set_insert (k.fn_set, OVL_CURRENT (ovl));
5473
    }
5474
  else
5475
    k.fn_set = NULL;
5476
 
5477
  if (include_std)
5478
    arg_assoc_namespace (&k, std_node);
5479
  arg_assoc_args_vec (&k, args);
5480
 
5481
  fns = k.functions;
5482
 
5483
  if (fns
5484
      && TREE_CODE (fns) != VAR_DECL
5485
      && !is_overloaded_fn (fns))
5486
    {
5487
      error ("argument dependent lookup finds %q+D", fns);
5488
      error ("  in call to %qD", name);
5489
      fns = error_mark_node;
5490
    }
5491
 
5492
  release_tree_vector (k.classes);
5493
  release_tree_vector (k.namespaces);
5494
  if (k.fn_set)
5495
    pointer_set_destroy (k.fn_set);
5496
 
5497
  return fns;
5498
}
5499
 
5500
/* Wrapper for lookup_arg_dependent_1.  */
5501
 
5502
tree
5503
lookup_arg_dependent (tree name, tree fns, VEC(tree,gc) *args,
5504
                      bool include_std)
5505
{
5506
  tree ret;
5507
  bool subtime;
5508
  subtime = timevar_cond_start (TV_NAME_LOOKUP);
5509
  ret = lookup_arg_dependent_1 (name, fns, args, include_std);
5510
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5511
  return ret;
5512
}
5513
 
5514
 
5515
/* Add namespace to using_directives. Return NULL_TREE if nothing was
5516
   changed (i.e. there was already a directive), or the fresh
5517
   TREE_LIST otherwise.  */
5518
 
5519
static tree
5520
push_using_directive_1 (tree used)
5521
{
5522
  tree ud = current_binding_level->using_directives;
5523
  tree iter, ancestor;
5524
 
5525
  /* Check if we already have this.  */
5526
  if (purpose_member (used, ud) != NULL_TREE)
5527
    return NULL_TREE;
5528
 
5529
  ancestor = namespace_ancestor (current_decl_namespace (), used);
5530
  ud = current_binding_level->using_directives;
5531
  ud = tree_cons (used, ancestor, ud);
5532
  current_binding_level->using_directives = ud;
5533
 
5534
  /* Recursively add all namespaces used.  */
5535
  for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
5536
    push_using_directive (TREE_PURPOSE (iter));
5537
 
5538
  return ud;
5539
}
5540
 
5541
/* Wrapper for push_using_directive_1.  */
5542
 
5543
static tree
5544
push_using_directive (tree used)
5545
{
5546
  tree ret;
5547
  timevar_start (TV_NAME_LOOKUP);
5548
  ret = push_using_directive_1 (used);
5549
  timevar_stop (TV_NAME_LOOKUP);
5550
  return ret;
5551
}
5552
 
5553
/* The type TYPE is being declared.  If it is a class template, or a
5554
   specialization of a class template, do any processing required and
5555
   perform error-checking.  If IS_FRIEND is nonzero, this TYPE is
5556
   being declared a friend.  B is the binding level at which this TYPE
5557
   should be bound.
5558
 
5559
   Returns the TYPE_DECL for TYPE, which may have been altered by this
5560
   processing.  */
5561
 
5562
static tree
5563
maybe_process_template_type_declaration (tree type, int is_friend,
5564
                                         cp_binding_level *b)
5565
{
5566
  tree decl = TYPE_NAME (type);
5567
 
5568
  if (processing_template_parmlist)
5569
    /* You can't declare a new template type in a template parameter
5570
       list.  But, you can declare a non-template type:
5571
 
5572
         template <class A*> struct S;
5573
 
5574
       is a forward-declaration of `A'.  */
5575
    ;
5576
  else if (b->kind == sk_namespace
5577
           && current_binding_level->kind != sk_namespace)
5578
    /* If this new type is being injected into a containing scope,
5579
       then it's not a template type.  */
5580
    ;
5581
  else
5582
    {
5583
      gcc_assert (MAYBE_CLASS_TYPE_P (type)
5584
                  || TREE_CODE (type) == ENUMERAL_TYPE);
5585
 
5586
      if (processing_template_decl)
5587
        {
5588
          /* This may change after the call to
5589
             push_template_decl_real, but we want the original value.  */
5590
          tree name = DECL_NAME (decl);
5591
 
5592
          decl = push_template_decl_real (decl, is_friend);
5593
          if (decl == error_mark_node)
5594
            return error_mark_node;
5595
 
5596
          /* If the current binding level is the binding level for the
5597
             template parameters (see the comment in
5598
             begin_template_parm_list) and the enclosing level is a class
5599
             scope, and we're not looking at a friend, push the
5600
             declaration of the member class into the class scope.  In the
5601
             friend case, push_template_decl will already have put the
5602
             friend into global scope, if appropriate.  */
5603
          if (TREE_CODE (type) != ENUMERAL_TYPE
5604
              && !is_friend && b->kind == sk_template_parms
5605
              && b->level_chain->kind == sk_class)
5606
            {
5607
              finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
5608
 
5609
              if (!COMPLETE_TYPE_P (current_class_type))
5610
                {
5611
                  maybe_add_class_template_decl_list (current_class_type,
5612
                                                      type, /*friend_p=*/0);
5613
                  /* Put this UTD in the table of UTDs for the class.  */
5614
                  if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5615
                    CLASSTYPE_NESTED_UTDS (current_class_type) =
5616
                      binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5617
 
5618
                  binding_table_insert
5619
                    (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5620
                }
5621
            }
5622
        }
5623
    }
5624
 
5625
  return decl;
5626
}
5627
 
5628
/* Push a tag name NAME for struct/class/union/enum type TYPE.  In case
5629
   that the NAME is a class template, the tag is processed but not pushed.
5630
 
5631
   The pushed scope depend on the SCOPE parameter:
5632
   - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup
5633
     scope.
5634
   - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and
5635
     non-template-parameter scope.  This case is needed for forward
5636
     declarations.
5637
   - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to
5638
     TS_GLOBAL case except that names within template-parameter scopes
5639
     are not pushed at all.
5640
 
5641
   Returns TYPE upon success and ERROR_MARK_NODE otherwise.  */
5642
 
5643
static tree
5644
pushtag_1 (tree name, tree type, tag_scope scope)
5645
{
5646
  cp_binding_level *b;
5647
  tree decl;
5648
 
5649
  b = current_binding_level;
5650
  while (/* Cleanup scopes are not scopes from the point of view of
5651
            the language.  */
5652
         b->kind == sk_cleanup
5653
         /* Neither are function parameter scopes.  */
5654
         || b->kind == sk_function_parms
5655
         /* Neither are the scopes used to hold template parameters
5656
            for an explicit specialization.  For an ordinary template
5657
            declaration, these scopes are not scopes from the point of
5658
            view of the language.  */
5659
         || (b->kind == sk_template_parms
5660
             && (b->explicit_spec_p || scope == ts_global))
5661
         || (b->kind == sk_class
5662
             && (scope != ts_current
5663
                 /* We may be defining a new type in the initializer
5664
                    of a static member variable. We allow this when
5665
                    not pedantic, and it is particularly useful for
5666
                    type punning via an anonymous union.  */
5667
                 || COMPLETE_TYPE_P (b->this_entity))))
5668
    b = b->level_chain;
5669
 
5670
  gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
5671
 
5672
  /* Do C++ gratuitous typedefing.  */
5673
  if (identifier_type_value_1 (name) != type)
5674
    {
5675
      tree tdef;
5676
      int in_class = 0;
5677
      tree context = TYPE_CONTEXT (type);
5678
 
5679
      if (! context)
5680
        {
5681
          tree cs = current_scope ();
5682
 
5683
          if (scope == ts_current
5684
              || (cs && TREE_CODE (cs) == FUNCTION_DECL))
5685
            context = cs;
5686
          else if (cs != NULL_TREE && TYPE_P (cs))
5687
            /* When declaring a friend class of a local class, we want
5688
               to inject the newly named class into the scope
5689
               containing the local class, not the namespace
5690
               scope.  */
5691
            context = decl_function_context (get_type_decl (cs));
5692
        }
5693
      if (!context)
5694
        context = current_namespace;
5695
 
5696
      if (b->kind == sk_class
5697
          || (b->kind == sk_template_parms
5698
              && b->level_chain->kind == sk_class))
5699
        in_class = 1;
5700
 
5701
      if (current_lang_name == lang_name_java)
5702
        TYPE_FOR_JAVA (type) = 1;
5703
 
5704
      tdef = create_implicit_typedef (name, type);
5705
      DECL_CONTEXT (tdef) = FROB_CONTEXT (context);
5706
      if (scope == ts_within_enclosing_non_class)
5707
        {
5708
          /* This is a friend.  Make this TYPE_DECL node hidden from
5709
             ordinary name lookup.  Its corresponding TEMPLATE_DECL
5710
             will be marked in push_template_decl_real.  */
5711
          retrofit_lang_decl (tdef);
5712
          DECL_ANTICIPATED (tdef) = 1;
5713
          DECL_FRIEND_P (tdef) = 1;
5714
        }
5715
 
5716
      decl = maybe_process_template_type_declaration
5717
        (type, scope == ts_within_enclosing_non_class, b);
5718
      if (decl == error_mark_node)
5719
        return decl;
5720
 
5721
      if (b->kind == sk_class)
5722
        {
5723
          if (!TYPE_BEING_DEFINED (current_class_type))
5724
            return error_mark_node;
5725
 
5726
          if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
5727
            /* Put this TYPE_DECL on the TYPE_FIELDS list for the
5728
               class.  But if it's a member template class, we want
5729
               the TEMPLATE_DECL, not the TYPE_DECL, so this is done
5730
               later.  */
5731
            finish_member_declaration (decl);
5732
          else
5733
            pushdecl_class_level (decl);
5734
        }
5735
      else if (b->kind != sk_template_parms)
5736
        {
5737
          decl = pushdecl_with_scope_1 (decl, b, /*is_friend=*/false);
5738
          if (decl == error_mark_node)
5739
            return decl;
5740
        }
5741
 
5742
      if (! in_class)
5743
        set_identifier_type_value_with_scope (name, tdef, b);
5744
 
5745
      TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
5746
 
5747
      /* If this is a local class, keep track of it.  We need this
5748
         information for name-mangling, and so that it is possible to
5749
         find all function definitions in a translation unit in a
5750
         convenient way.  (It's otherwise tricky to find a member
5751
         function definition it's only pointed to from within a local
5752
         class.)  */
5753
      if (TYPE_CONTEXT (type)
5754
          && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL)
5755
        VEC_safe_push (tree, gc, local_classes, type);
5756
    }
5757
  if (b->kind == sk_class
5758
      && !COMPLETE_TYPE_P (current_class_type))
5759
    {
5760
      maybe_add_class_template_decl_list (current_class_type,
5761
                                          type, /*friend_p=*/0);
5762
 
5763
      if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
5764
        CLASSTYPE_NESTED_UTDS (current_class_type)
5765
          = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
5766
 
5767
      binding_table_insert
5768
        (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
5769
    }
5770
 
5771
  decl = TYPE_NAME (type);
5772
  gcc_assert (TREE_CODE (decl) == TYPE_DECL);
5773
 
5774
  /* Set type visibility now if this is a forward declaration.  */
5775
  TREE_PUBLIC (decl) = 1;
5776
  determine_visibility (decl);
5777
 
5778
  return type;
5779
}
5780
 
5781
/* Wrapper for pushtag_1.  */
5782
 
5783
tree
5784
pushtag (tree name, tree type, tag_scope scope)
5785
{
5786
  tree ret;
5787
  bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5788
  ret = pushtag_1 (name, type, scope);
5789
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5790
  return ret;
5791
}
5792
 
5793
/* Subroutines for reverting temporarily to top-level for instantiation
5794
   of templates and such.  We actually need to clear out the class- and
5795
   local-value slots of all identifiers, so that only the global values
5796
   are at all visible.  Simply setting current_binding_level to the global
5797
   scope isn't enough, because more binding levels may be pushed.  */
5798
struct saved_scope *scope_chain;
5799
 
5800
/* If ID has not already been marked, add an appropriate binding to
5801
   *OLD_BINDINGS.  */
5802
 
5803
static void
5804
store_binding (tree id, VEC(cxx_saved_binding,gc) **old_bindings)
5805
{
5806
  cxx_saved_binding *saved;
5807
 
5808
  if (!id || !IDENTIFIER_BINDING (id))
5809
    return;
5810
 
5811
  if (IDENTIFIER_MARKED (id))
5812
    return;
5813
 
5814
  IDENTIFIER_MARKED (id) = 1;
5815
 
5816
  saved = VEC_safe_push (cxx_saved_binding, gc, *old_bindings, NULL);
5817
  saved->identifier = id;
5818
  saved->binding = IDENTIFIER_BINDING (id);
5819
  saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
5820
  IDENTIFIER_BINDING (id) = NULL;
5821
}
5822
 
5823
static void
5824
store_bindings (tree names, VEC(cxx_saved_binding,gc) **old_bindings)
5825
{
5826
  tree t;
5827
 
5828
  bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5829
  for (t = names; t; t = TREE_CHAIN (t))
5830
    {
5831
      tree id;
5832
 
5833
      if (TREE_CODE (t) == TREE_LIST)
5834
        id = TREE_PURPOSE (t);
5835
      else
5836
        id = DECL_NAME (t);
5837
 
5838
      store_binding (id, old_bindings);
5839
    }
5840
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5841
}
5842
 
5843
/* Like store_bindings, but NAMES is a vector of cp_class_binding
5844
   objects, rather than a TREE_LIST.  */
5845
 
5846
static void
5847
store_class_bindings (VEC(cp_class_binding,gc) *names,
5848
                      VEC(cxx_saved_binding,gc) **old_bindings)
5849
{
5850
  size_t i;
5851
  cp_class_binding *cb;
5852
 
5853
  bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5854
  for (i = 0; VEC_iterate(cp_class_binding, names, i, cb); ++i)
5855
    store_binding (cb->identifier, old_bindings);
5856
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5857
}
5858
 
5859
void
5860
push_to_top_level (void)
5861
{
5862
  struct saved_scope *s;
5863
  cp_binding_level *b;
5864
  cxx_saved_binding *sb;
5865
  size_t i;
5866
  bool need_pop;
5867
 
5868
  bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5869
  s = ggc_alloc_cleared_saved_scope ();
5870
 
5871
  b = scope_chain ? current_binding_level : 0;
5872
 
5873
  /* If we're in the middle of some function, save our state.  */
5874
  if (cfun)
5875
    {
5876
      need_pop = true;
5877
      push_function_context ();
5878
    }
5879
  else
5880
    need_pop = false;
5881
 
5882
  if (scope_chain && previous_class_level)
5883
    store_class_bindings (previous_class_level->class_shadowed,
5884
                          &s->old_bindings);
5885
 
5886
  /* Have to include the global scope, because class-scope decls
5887
     aren't listed anywhere useful.  */
5888
  for (; b; b = b->level_chain)
5889
    {
5890
      tree t;
5891
 
5892
      /* Template IDs are inserted into the global level. If they were
5893
         inserted into namespace level, finish_file wouldn't find them
5894
         when doing pending instantiations. Therefore, don't stop at
5895
         namespace level, but continue until :: .  */
5896
      if (global_scope_p (b))
5897
        break;
5898
 
5899
      store_bindings (b->names, &s->old_bindings);
5900
      /* We also need to check class_shadowed to save class-level type
5901
         bindings, since pushclass doesn't fill in b->names.  */
5902
      if (b->kind == sk_class)
5903
        store_class_bindings (b->class_shadowed, &s->old_bindings);
5904
 
5905
      /* Unwind type-value slots back to top level.  */
5906
      for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
5907
        SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
5908
    }
5909
 
5910
  FOR_EACH_VEC_ELT (cxx_saved_binding, s->old_bindings, i, sb)
5911
    IDENTIFIER_MARKED (sb->identifier) = 0;
5912
 
5913
  s->prev = scope_chain;
5914
  s->bindings = b;
5915
  s->need_pop_function_context = need_pop;
5916
  s->function_decl = current_function_decl;
5917
  s->unevaluated_operand = cp_unevaluated_operand;
5918
  s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
5919
  s->x_stmt_tree.stmts_are_full_exprs_p = true;
5920
 
5921
  scope_chain = s;
5922
  current_function_decl = NULL_TREE;
5923
  current_lang_base = VEC_alloc (tree, gc, 10);
5924
  current_lang_name = lang_name_cplusplus;
5925
  current_namespace = global_namespace;
5926
  push_class_stack ();
5927
  cp_unevaluated_operand = 0;
5928
  c_inhibit_evaluation_warnings = 0;
5929
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5930
}
5931
 
5932
static void
5933
pop_from_top_level_1 (void)
5934
{
5935
  struct saved_scope *s = scope_chain;
5936
  cxx_saved_binding *saved;
5937
  size_t i;
5938
 
5939
  /* Clear out class-level bindings cache.  */
5940
  if (previous_class_level)
5941
    invalidate_class_lookup_cache ();
5942
  pop_class_stack ();
5943
 
5944
  current_lang_base = 0;
5945
 
5946
  scope_chain = s->prev;
5947
  FOR_EACH_VEC_ELT (cxx_saved_binding, s->old_bindings, i, saved)
5948
    {
5949
      tree id = saved->identifier;
5950
 
5951
      IDENTIFIER_BINDING (id) = saved->binding;
5952
      SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
5953
    }
5954
 
5955
  /* If we were in the middle of compiling a function, restore our
5956
     state.  */
5957
  if (s->need_pop_function_context)
5958
    pop_function_context ();
5959
  current_function_decl = s->function_decl;
5960
  cp_unevaluated_operand = s->unevaluated_operand;
5961
  c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
5962
}
5963
 
5964
/* Wrapper for pop_from_top_level_1.  */
5965
 
5966
void
5967
pop_from_top_level (void)
5968
{
5969
  bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
5970
  pop_from_top_level_1 ();
5971
  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
5972
}
5973
 
5974
 
5975
/* Pop off extraneous binding levels left over due to syntax errors.
5976
 
5977
   We don't pop past namespaces, as they might be valid.  */
5978
 
5979
void
5980
pop_everything (void)
5981
{
5982
  if (ENABLE_SCOPE_CHECKING)
5983
    verbatim ("XXX entering pop_everything ()\n");
5984
  while (!toplevel_bindings_p ())
5985
    {
5986
      if (current_binding_level->kind == sk_class)
5987
        pop_nested_class ();
5988
      else
5989
        poplevel (0, 0, 0);
5990
    }
5991
  if (ENABLE_SCOPE_CHECKING)
5992
    verbatim ("XXX leaving pop_everything ()\n");
5993
}
5994
 
5995
/* Emit debugging information for using declarations and directives.
5996
   If input tree is overloaded fn then emit debug info for all
5997
   candidates.  */
5998
 
5999
void
6000
cp_emit_debug_info_for_using (tree t, tree context)
6001
{
6002
  /* Don't try to emit any debug information if we have errors.  */
6003
  if (seen_error ())
6004
    return;
6005
 
6006
  /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
6007
     of a builtin function.  */
6008
  if (TREE_CODE (t) == FUNCTION_DECL
6009
      && DECL_EXTERNAL (t)
6010
      && DECL_BUILT_IN (t))
6011
    return;
6012
 
6013
  /* Do not supply context to imported_module_or_decl, if
6014
     it is a global namespace.  */
6015
  if (context == global_namespace)
6016
    context = NULL_TREE;
6017
 
6018
  if (BASELINK_P (t))
6019
    t = BASELINK_FUNCTIONS (t);
6020
 
6021
  /* FIXME: Handle TEMPLATE_DECLs.  */
6022
  for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
6023
    if (TREE_CODE (t) != TEMPLATE_DECL)
6024
      {
6025
        if (building_stmt_list_p ())
6026
          add_stmt (build_stmt (input_location, USING_STMT, t));
6027
        else
6028
          (*debug_hooks->imported_module_or_decl) (t, NULL_TREE, context, false);
6029
      }
6030
}
6031
 
6032
#include "gt-cp-name-lookup.h"

powered by: WebSVN 2.1.0

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