1c03b94e9Schristos /* Code dealing with "using" directives for GDB.
2*1424dfb3Schristos Copyright (C) 2003-2020 Free Software Foundation, Inc.
3c03b94e9Schristos
4c03b94e9Schristos This file is part of GDB.
5c03b94e9Schristos
6c03b94e9Schristos This program is free software; you can redistribute it and/or modify
7c03b94e9Schristos it under the terms of the GNU General Public License as published by
8c03b94e9Schristos the Free Software Foundation; either version 3 of the License, or
9c03b94e9Schristos (at your option) any later version.
10c03b94e9Schristos
11c03b94e9Schristos This program is distributed in the hope that it will be useful,
12c03b94e9Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
13c03b94e9Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14c03b94e9Schristos GNU General Public License for more details.
15c03b94e9Schristos
16c03b94e9Schristos You should have received a copy of the GNU General Public License
17c03b94e9Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */
18c03b94e9Schristos
19c03b94e9Schristos #include "defs.h"
20c03b94e9Schristos #include "namespace.h"
21c03b94e9Schristos
22c03b94e9Schristos /* Add a using directive to USING_DIRECTIVES. If the using directive
23c03b94e9Schristos in question has already been added, don't add it twice.
24c03b94e9Schristos
25c03b94e9Schristos Create a new struct using_direct which imports the namespace SRC
26c03b94e9Schristos into the scope DEST. ALIAS is the name of the imported namespace
27c03b94e9Schristos in the current scope. If ALIAS is NULL then the namespace is known
28c03b94e9Schristos by its original name. DECLARATION is the name if the imported
29*1424dfb3Schristos variable if this is a declaration import (Eg. using A::x), otherwise
30c03b94e9Schristos it is NULL. EXCLUDES is a list of names not to import from an
31c03b94e9Schristos imported module or NULL. If COPY_NAMES is non-zero, then the
32c03b94e9Schristos arguments are copied into newly allocated memory so they can be
33*1424dfb3Schristos temporaries. For EXCLUDES the contents of the vector are copied,
34*1424dfb3Schristos but the pointed to characters are not copied. */
35c03b94e9Schristos
36c03b94e9Schristos void
add_using_directive(struct using_direct ** using_directives,const char * dest,const char * src,const char * alias,const char * declaration,const std::vector<const char * > & excludes,int copy_names,struct obstack * obstack)37c03b94e9Schristos add_using_directive (struct using_direct **using_directives,
38c03b94e9Schristos const char *dest,
39c03b94e9Schristos const char *src,
40c03b94e9Schristos const char *alias,
41c03b94e9Schristos const char *declaration,
4207163879Schristos const std::vector<const char *> &excludes,
43c03b94e9Schristos int copy_names,
44c03b94e9Schristos struct obstack *obstack)
45c03b94e9Schristos {
46c03b94e9Schristos struct using_direct *current;
47c03b94e9Schristos struct using_direct *newobj;
48c03b94e9Schristos int alloc_len;
49c03b94e9Schristos
50c03b94e9Schristos /* Has it already been added? */
51c03b94e9Schristos
52c03b94e9Schristos for (current = *using_directives; current != NULL; current = current->next)
53c03b94e9Schristos {
54c03b94e9Schristos int ix;
55c03b94e9Schristos
56c03b94e9Schristos if (strcmp (current->import_src, src) != 0)
57c03b94e9Schristos continue;
58c03b94e9Schristos if (strcmp (current->import_dest, dest) != 0)
59c03b94e9Schristos continue;
60c03b94e9Schristos if ((alias == NULL && current->alias != NULL)
61c03b94e9Schristos || (alias != NULL && current->alias == NULL)
62c03b94e9Schristos || (alias != NULL && current->alias != NULL
63c03b94e9Schristos && strcmp (alias, current->alias) != 0))
64c03b94e9Schristos continue;
65c03b94e9Schristos if ((declaration == NULL && current->declaration != NULL)
66c03b94e9Schristos || (declaration != NULL && current->declaration == NULL)
67c03b94e9Schristos || (declaration != NULL && current->declaration != NULL
68c03b94e9Schristos && strcmp (declaration, current->declaration) != 0))
69c03b94e9Schristos continue;
70c03b94e9Schristos
71c03b94e9Schristos /* Compare the contents of EXCLUDES. */
7207163879Schristos for (ix = 0; ix < excludes.size (); ++ix)
73c03b94e9Schristos if (current->excludes[ix] == NULL
7407163879Schristos || strcmp (excludes[ix], current->excludes[ix]) != 0)
75c03b94e9Schristos break;
7607163879Schristos if (ix < excludes.size () || current->excludes[ix] != NULL)
77c03b94e9Schristos continue;
78c03b94e9Schristos
79c03b94e9Schristos /* Parameters exactly match CURRENT. */
80c03b94e9Schristos return;
81c03b94e9Schristos }
82c03b94e9Schristos
83c03b94e9Schristos alloc_len = (sizeof(*newobj)
8407163879Schristos + (excludes.size () * sizeof(*newobj->excludes)));
85c03b94e9Schristos newobj = (struct using_direct *) obstack_alloc (obstack, alloc_len);
86c03b94e9Schristos memset (newobj, 0, sizeof (*newobj));
87c03b94e9Schristos
88c03b94e9Schristos if (copy_names)
89c03b94e9Schristos {
90*1424dfb3Schristos newobj->import_src = obstack_strdup (obstack, src);
91*1424dfb3Schristos newobj->import_dest = obstack_strdup (obstack, dest);
92c03b94e9Schristos }
93c03b94e9Schristos else
94c03b94e9Schristos {
95c03b94e9Schristos newobj->import_src = src;
96c03b94e9Schristos newobj->import_dest = dest;
97c03b94e9Schristos }
98c03b94e9Schristos
99c03b94e9Schristos if (alias != NULL && copy_names)
100*1424dfb3Schristos newobj->alias = obstack_strdup (obstack, alias);
101c03b94e9Schristos else
102c03b94e9Schristos newobj->alias = alias;
103c03b94e9Schristos
104c03b94e9Schristos if (declaration != NULL && copy_names)
105*1424dfb3Schristos newobj->declaration = obstack_strdup (obstack, declaration);
106c03b94e9Schristos else
107c03b94e9Schristos newobj->declaration = declaration;
108c03b94e9Schristos
10907163879Schristos if (!excludes.empty ())
11007163879Schristos memcpy (newobj->excludes, excludes.data (),
11107163879Schristos excludes.size () * sizeof (*newobj->excludes));
11207163879Schristos newobj->excludes[excludes.size ()] = NULL;
113c03b94e9Schristos
114c03b94e9Schristos newobj->next = *using_directives;
115c03b94e9Schristos *using_directives = newobj;
116c03b94e9Schristos }
117