compile-cplus.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* Header file for GDB compile C++ language support.
  2. Copyright (C) 2016-2022 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #ifndef COMPILE_COMPILE_CPLUS_H
  14. #define COMPILE_COMPILE_CPLUS_H
  15. #include "gdbsupport/enum-flags.h"
  16. #include "gcc-cp-plugin.h"
  17. #include "symtab.h"
  18. struct type;
  19. struct block;
  20. /* enum-flags wrapper */
  21. DEF_ENUM_FLAGS_TYPE (enum gcc_cp_qualifiers, gcc_cp_qualifiers_flags);
  22. DEF_ENUM_FLAGS_TYPE (enum gcc_cp_ref_qualifiers, gcc_cp_ref_qualifiers_flags);
  23. DEF_ENUM_FLAGS_TYPE (enum gcc_cp_symbol_kind, gcc_cp_symbol_kind_flags);
  24. class compile_cplus_instance;
  25. /* A single component of a type's scope. Type names are broken into
  26. "components," a series of unqualified names comprising the type name,
  27. e.g., "namespace1", "namespace2", "myclass". */
  28. struct scope_component
  29. {
  30. /* The unqualified name of this scope. */
  31. std::string name;
  32. /* The block symbol for this type/scope. */
  33. struct block_symbol bsymbol;
  34. };
  35. /* Comparison operators for scope_components. */
  36. bool operator== (const scope_component &lhs, const scope_component &rhs);
  37. bool operator!= (const scope_component &lhs, const scope_component &rhs);
  38. /* A single compiler scope used to define a type.
  39. A compile_scope is a list of scope_components, where all leading
  40. scope_components are namespaces, followed by a single non-namespace
  41. type component (the actual type we are converting). */
  42. class compile_scope : private std::vector<scope_component>
  43. {
  44. public:
  45. using std::vector<scope_component>::push_back;
  46. using std::vector<scope_component>::pop_back;
  47. using std::vector<scope_component>::back;
  48. using std::vector<scope_component>::empty;
  49. using std::vector<scope_component>::size;
  50. using std::vector<scope_component>::begin;
  51. using std::vector<scope_component>::end;
  52. using std::vector<scope_component>::operator[];
  53. compile_scope ()
  54. : m_nested_type (GCC_TYPE_NONE), m_pushed (false)
  55. {
  56. }
  57. /* Return the gcc_type of the type if it is a nested definition.
  58. Returns GCC_TYPE_NONE if this type was not nested. */
  59. gcc_type nested_type ()
  60. {
  61. return m_nested_type;
  62. }
  63. private:
  64. /* compile_cplus_instance is a friend class so that it can set the
  65. following private members when compile_scopes are created. */
  66. friend compile_cplus_instance;
  67. /* If the type was actually a nested type, this will hold that nested
  68. type after the scope is pushed. */
  69. gcc_type m_nested_type;
  70. /* If true, this scope was pushed to the compiler and all namespaces
  71. must be popped when leaving the scope. */
  72. bool m_pushed;
  73. };
  74. /* Comparison operators for compile_scopes. */
  75. bool operator== (const compile_scope &lhs, const compile_scope &rhs);
  76. bool operator!= (const compile_scope &lhs, const compile_scope &rhs);
  77. /* Convert TYPENAME into a vector of namespace and top-most/super
  78. composite scopes.
  79. For example, for the input "Namespace::classB::classInner", the
  80. resultant vector will contain the tokens "Namespace" and
  81. "classB". */
  82. compile_scope type_name_to_scope (const char *type_name,
  83. const struct block *block);
  84. /* A callback suitable for use as the GCC C++ symbol oracle. */
  85. extern gcc_cp_oracle_function gcc_cplus_convert_symbol;
  86. /* A callback suitable for use as the GCC C++ address oracle. */
  87. extern gcc_cp_symbol_address_function gcc_cplus_symbol_address;
  88. /* A subclass of compile_instance that is specific to the C++ front
  89. end. */
  90. class compile_cplus_instance : public compile_instance
  91. {
  92. public:
  93. explicit compile_cplus_instance (struct gcc_cp_context *gcc_cp)
  94. : compile_instance (&gcc_cp->base, m_default_cflags),
  95. m_plugin (gcc_cp)
  96. {
  97. m_plugin.set_callbacks (gcc_cplus_convert_symbol,
  98. gcc_cplus_symbol_address,
  99. gcc_cplus_enter_scope, gcc_cplus_leave_scope,
  100. this);
  101. }
  102. /* Convert a gdb type, TYPE, to a GCC type.
  103. If this type was defined in another type, NESTED_ACCESS should indicate
  104. the accessibility of this type (or GCC_CP_ACCESS_NONE if not a nested
  105. type). GCC_CP_ACCESS_NONE is the default nested access.
  106. The new GCC type is returned. */
  107. gcc_type convert_type
  108. (struct type *type,
  109. enum gcc_cp_symbol_kind nested_access = GCC_CP_ACCESS_NONE);
  110. /* Return a handle for the GCC plug-in. */
  111. gcc_cp_plugin &plugin () { return m_plugin; }
  112. /* Factory method to create a new scope based on TYPE with name TYPE_NAME.
  113. [TYPE_NAME could be TYPE_NAME or SYMBOL_NATURAL_NAME.]
  114. If TYPE is a nested or local definition, nested_type () will return
  115. the gcc_type of the conversion.
  116. Otherwise, nested_type () is GCC_TYPE_NONE. */
  117. compile_scope new_scope (const char *type_name, struct type *type);
  118. /* Enter the given NEW_SCOPE. */
  119. void enter_scope (compile_scope &&scope);
  120. /* Leave the current scope. */
  121. void leave_scope ();
  122. /* Add the qualifiers given by QUALS to BASE. */
  123. gcc_type convert_qualified_base (gcc_type base,
  124. gcc_cp_qualifiers_flags quals);
  125. /* Convert TARGET into a pointer type. */
  126. gcc_type convert_pointer_base (gcc_type target);
  127. /* Convert BASE into a reference type. RQUALS describes the reference. */
  128. gcc_type convert_reference_base (gcc_type base,
  129. enum gcc_cp_ref_qualifiers rquals);
  130. /* Return the declaration name of the symbol named NATURAL.
  131. This returns a name with no function arguments or template parameters,
  132. suitable for passing to the compiler plug-in. */
  133. static gdb::unique_xmalloc_ptr<char> decl_name (const char *natural);
  134. private:
  135. /* Callbacks suitable for use as the GCC C++ enter/leave scope requests. */
  136. static gcc_cp_enter_leave_user_expr_scope_function gcc_cplus_enter_scope;
  137. static gcc_cp_enter_leave_user_expr_scope_function gcc_cplus_leave_scope;
  138. /* Default compiler flags for C++. */
  139. static const char *m_default_cflags;
  140. /* The GCC plug-in. */
  141. gcc_cp_plugin m_plugin;
  142. /* A list of scopes we are processing. */
  143. std::vector<compile_scope> m_scopes;
  144. };
  145. /* Get the access flag for the NUM'th method of TYPE's FNI'th
  146. fieldlist. */
  147. enum gcc_cp_symbol_kind get_method_access_flag (const struct type *type,
  148. int fni, int num);
  149. #endif /* COMPILE_COMPILE_CPLUS_H */