namespace.c 3.9 KB

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