name-lookup.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /* Declarations for C++ name lookup routines.
  2. Copyright (C) 2003-2015 Free Software Foundation, Inc.
  3. Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifndef GCC_CP_NAME_LOOKUP_H
  17. #define GCC_CP_NAME_LOOKUP_H
  18. #include "c-family/c-common.h"
  19. /* The type of dictionary used to map names to types declared at
  20. a given scope. */
  21. typedef struct binding_table_s *binding_table;
  22. typedef struct binding_entry_s *binding_entry;
  23. /* The type of a routine repeatedly called by binding_table_foreach. */
  24. typedef void (*bt_foreach_proc) (binding_entry, void *);
  25. struct GTY(()) binding_entry_s {
  26. binding_entry chain;
  27. tree name;
  28. tree type;
  29. };
  30. /* These macros indicate the initial chains count for binding_table. */
  31. #define SCOPE_DEFAULT_HT_SIZE (1 << 3)
  32. #define CLASS_SCOPE_HT_SIZE (1 << 3)
  33. #define NAMESPACE_ORDINARY_HT_SIZE (1 << 5)
  34. #define NAMESPACE_STD_HT_SIZE (1 << 8)
  35. #define GLOBAL_SCOPE_HT_SIZE (1 << 8)
  36. extern void binding_table_foreach (binding_table, bt_foreach_proc, void *);
  37. extern binding_entry binding_table_find (binding_table, tree);
  38. /* Datatype that represents binding established by a declaration between
  39. a name and a C++ entity. */
  40. typedef struct cxx_binding cxx_binding;
  41. /* The datatype used to implement C++ scope. */
  42. typedef struct cp_binding_level cp_binding_level;
  43. /* Nonzero if this binding is for a local scope, as opposed to a class
  44. or namespace scope. */
  45. #define LOCAL_BINDING_P(NODE) ((NODE)->is_local)
  46. /* True if NODE->value is from a base class of the class which is
  47. currently being defined. */
  48. #define INHERITED_VALUE_BINDING_P(NODE) ((NODE)->value_is_inherited)
  49. struct GTY(()) cxx_binding {
  50. /* Link to chain together various bindings for this name. */
  51. cxx_binding *previous;
  52. /* The non-type entity this name is bound to. */
  53. tree value;
  54. /* The type entity this name is bound to. */
  55. tree type;
  56. /* The scope at which this binding was made. */
  57. cp_binding_level *scope;
  58. unsigned value_is_inherited : 1;
  59. unsigned is_local : 1;
  60. };
  61. /* Datatype used to temporarily save C++ bindings (for implicit
  62. instantiations purposes and like). Implemented in decl.c. */
  63. typedef struct GTY(()) cxx_saved_binding {
  64. /* The name of the current binding. */
  65. tree identifier;
  66. /* The binding we're saving. */
  67. cxx_binding *binding;
  68. tree real_type_value;
  69. } cxx_saved_binding;
  70. extern tree identifier_type_value (tree);
  71. extern void set_identifier_type_value (tree, tree);
  72. extern void pop_binding (tree, tree);
  73. extern void pop_bindings_and_leave_scope (void);
  74. extern tree constructor_name (tree);
  75. extern bool constructor_name_p (tree, tree);
  76. /* The kinds of scopes we recognize. */
  77. typedef enum scope_kind {
  78. sk_block = 0, /* An ordinary block scope. This enumerator must
  79. have the value zero because "cp_binding_level"
  80. is initialized by using "memset" to set the
  81. contents to zero, and the default scope kind
  82. is "sk_block". */
  83. sk_cleanup, /* A scope for (pseudo-)scope for cleanup. It is
  84. pseudo in that it is transparent to name lookup
  85. activities. */
  86. sk_try, /* A try-block. */
  87. sk_catch, /* A catch-block. */
  88. sk_for, /* The scope of the variable declared in a
  89. for-init-statement. */
  90. sk_cond, /* The scope of the variable declared in the condition
  91. of an if or switch statement. */
  92. sk_function_parms, /* The scope containing function parameters. */
  93. sk_class, /* The scope containing the members of a class. */
  94. sk_scoped_enum, /* The scope containing the enumertors of a C++0x
  95. scoped enumeration. */
  96. sk_namespace, /* The scope containing the members of a
  97. namespace, including the global scope. */
  98. sk_template_parms, /* A scope for template parameters. */
  99. sk_template_spec, /* Like sk_template_parms, but for an explicit
  100. specialization. Since, by definition, an
  101. explicit specialization is introduced by
  102. "template <>", this scope is always empty. */
  103. sk_omp /* An OpenMP structured block. */
  104. } scope_kind;
  105. /* The scope where the class/struct/union/enum tag applies. */
  106. typedef enum tag_scope {
  107. ts_current = 0, /* Current scope only. This is for the
  108. class-key identifier;
  109. case mentioned in [basic.lookup.elab]/2,
  110. or the class/enum definition
  111. class-key identifier { ... }; */
  112. ts_global = 1, /* All scopes. This is the 3.4.1
  113. [basic.lookup.unqual] lookup mentioned
  114. in [basic.lookup.elab]/2. */
  115. ts_within_enclosing_non_class = 2, /* Search within enclosing non-class
  116. only, for friend class lookup
  117. according to [namespace.memdef]/3
  118. and [class.friend]/9. */
  119. ts_lambda = 3 /* Declaring a lambda closure. */
  120. } tag_scope;
  121. typedef struct GTY(()) cp_class_binding {
  122. cxx_binding *base;
  123. /* The bound name. */
  124. tree identifier;
  125. } cp_class_binding;
  126. typedef struct GTY(()) cp_label_binding {
  127. /* The bound LABEL_DECL. */
  128. tree label;
  129. /* The previous IDENTIFIER_LABEL_VALUE. */
  130. tree prev_value;
  131. } cp_label_binding;
  132. /* For each binding contour we allocate a binding_level structure
  133. which records the names defined in that contour.
  134. Contours include:
  135. 0) the global one
  136. 1) one for each function definition,
  137. where internal declarations of the parameters appear.
  138. 2) one for each compound statement,
  139. to record its declarations.
  140. The current meaning of a name can be found by searching the levels
  141. from the current one out to the global one.
  142. Off to the side, may be the class_binding_level. This exists only
  143. to catch class-local declarations. It is otherwise nonexistent.
  144. Also there may be binding levels that catch cleanups that must be
  145. run when exceptions occur. Thus, to see whether a name is bound in
  146. the current scope, it is not enough to look in the
  147. CURRENT_BINDING_LEVEL. You should use lookup_name_current_level
  148. instead. */
  149. /* Note that the information in the `names' component of the global contour
  150. is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
  151. struct GTY(()) cp_binding_level {
  152. /* A chain of _DECL nodes for all variables, constants, functions,
  153. and typedef types. These are in the reverse of the order
  154. supplied. There may be OVERLOADs on this list, too, but they
  155. are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD. */
  156. tree names;
  157. /* A chain of NAMESPACE_DECL nodes. */
  158. tree namespaces;
  159. /* An array of static functions and variables (for namespaces only) */
  160. vec<tree, va_gc> *static_decls;
  161. /* A list of USING_DECL nodes. */
  162. tree usings;
  163. /* A list of used namespaces. PURPOSE is the namespace,
  164. VALUE the common ancestor with this binding_level's namespace. */
  165. tree using_directives;
  166. /* For the binding level corresponding to a class, the entities
  167. declared in the class or its base classes. */
  168. vec<cp_class_binding, va_gc> *class_shadowed;
  169. /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
  170. is used for all binding levels. The TREE_PURPOSE is the name of
  171. the entity, the TREE_TYPE is the associated type. In addition
  172. the TREE_VALUE is the IDENTIFIER_TYPE_VALUE before we entered
  173. the class. */
  174. tree type_shadowed;
  175. /* Similar to class_shadowed, but for IDENTIFIER_LABEL_VALUE, and
  176. used for all binding levels. */
  177. vec<cp_label_binding, va_gc> *shadowed_labels;
  178. /* For each level (except not the global one),
  179. a chain of BLOCK nodes for all the levels
  180. that were entered and exited one level down. */
  181. tree blocks;
  182. /* The entity (namespace, class, function) the scope of which this
  183. binding contour corresponds to. Otherwise NULL. */
  184. tree this_entity;
  185. /* The binding level which this one is contained in (inherits from). */
  186. cp_binding_level *level_chain;
  187. /* List of VAR_DECLS saved from a previous for statement.
  188. These would be dead in ISO-conforming code, but might
  189. be referenced in ARM-era code. */
  190. vec<tree, va_gc> *dead_vars_from_for;
  191. /* STATEMENT_LIST for statements in this binding contour.
  192. Only used at present for SK_CLEANUP temporary bindings. */
  193. tree statement_list;
  194. /* Binding depth at which this level began. */
  195. int binding_depth;
  196. /* The kind of scope that this object represents. However, a
  197. SK_TEMPLATE_SPEC scope is represented with KIND set to
  198. SK_TEMPLATE_PARMS and EXPLICIT_SPEC_P set to true. */
  199. ENUM_BITFIELD (scope_kind) kind : 4;
  200. /* True if this scope is an SK_TEMPLATE_SPEC scope. This field is
  201. only valid if KIND == SK_TEMPLATE_PARMS. */
  202. BOOL_BITFIELD explicit_spec_p : 1;
  203. /* true means make a BLOCK for this level regardless of all else. */
  204. unsigned keep : 1;
  205. /* Nonzero if this level can safely have additional
  206. cleanup-needing variables added to it. */
  207. unsigned more_cleanups_ok : 1;
  208. unsigned have_cleanups : 1;
  209. /* Transient state set if this scope is of sk_class kind
  210. and is in the process of defining 'this_entity'. Reset
  211. on leaving the class definition to allow for the scope
  212. to be subsequently re-used as a non-defining scope for
  213. 'this_entity'. */
  214. unsigned defining_class_p : 1;
  215. /* 23 bits left to fill a 32-bit word. */
  216. };
  217. /* The binding level currently in effect. */
  218. #define current_binding_level \
  219. (*(cfun && cp_function_chain && cp_function_chain->bindings \
  220. ? &cp_function_chain->bindings \
  221. : &scope_chain->bindings))
  222. /* The binding level of the current class, if any. */
  223. #define class_binding_level scope_chain->class_bindings
  224. /* The tree node representing the global scope. */
  225. extern GTY(()) tree global_namespace;
  226. extern GTY(()) tree global_scope_name;
  227. /* Indicates that there is a type value in some namespace, although
  228. that is not necessarily in scope at the moment. */
  229. extern GTY(()) tree global_type_node;
  230. /* True if SCOPE designates the global scope binding contour. */
  231. #define global_scope_p(SCOPE) \
  232. ((SCOPE) == NAMESPACE_LEVEL (global_namespace))
  233. extern cp_binding_level *leave_scope (void);
  234. extern bool kept_level_p (void);
  235. extern bool global_bindings_p (void);
  236. extern bool toplevel_bindings_p (void);
  237. extern bool namespace_bindings_p (void);
  238. extern bool local_bindings_p (void);
  239. extern bool template_parm_scope_p (void);
  240. extern scope_kind innermost_scope_kind (void);
  241. extern cp_binding_level *begin_scope (scope_kind, tree);
  242. extern void print_binding_stack (void);
  243. extern void push_to_top_level (void);
  244. extern void pop_from_top_level (void);
  245. extern void pop_everything (void);
  246. extern void keep_next_level (bool);
  247. extern bool is_ancestor (tree, tree);
  248. extern tree push_scope (tree);
  249. extern void pop_scope (tree);
  250. extern tree push_inner_scope (tree);
  251. extern void pop_inner_scope (tree, tree);
  252. extern void push_binding_level (cp_binding_level *);
  253. extern void push_namespace (tree);
  254. extern void pop_namespace (void);
  255. extern void push_nested_namespace (tree);
  256. extern void pop_nested_namespace (tree);
  257. extern bool handle_namespace_attrs (tree, tree);
  258. extern void pushlevel_class (void);
  259. extern void poplevel_class (void);
  260. extern tree pushdecl_with_scope (tree, cp_binding_level *, bool);
  261. extern tree lookup_name_prefer_type (tree, int);
  262. extern tree lookup_name_real (tree, int, int, bool, int, int);
  263. extern tree lookup_type_scope (tree, tag_scope);
  264. extern tree namespace_binding (tree, tree);
  265. extern void set_namespace_binding (tree, tree, tree);
  266. extern bool hidden_name_p (tree);
  267. extern tree remove_hidden_names (tree);
  268. extern tree lookup_qualified_name (tree, tree, bool, bool);
  269. extern tree lookup_name_nonclass (tree);
  270. extern tree lookup_name_innermost_nonclass_level (tree);
  271. extern bool is_local_extern (tree);
  272. extern tree lookup_function_nonclass (tree, vec<tree, va_gc> *, bool);
  273. extern void push_local_binding (tree, tree, int);
  274. extern bool pushdecl_class_level (tree);
  275. extern tree pushdecl_namespace_level (tree, bool);
  276. extern bool push_class_level_binding (tree, tree);
  277. extern tree getdecls (void);
  278. extern int function_parm_depth (void);
  279. extern tree cp_namespace_decls (tree);
  280. extern void set_decl_namespace (tree, tree, bool);
  281. extern void push_decl_namespace (tree);
  282. extern void pop_decl_namespace (void);
  283. extern void do_namespace_alias (tree, tree);
  284. extern void do_toplevel_using_decl (tree, tree, tree);
  285. extern void do_local_using_decl (tree, tree, tree);
  286. extern tree do_class_using_decl (tree, tree);
  287. extern void do_using_directive (tree);
  288. extern tree lookup_arg_dependent (tree, tree, vec<tree, va_gc> *);
  289. extern bool is_associated_namespace (tree, tree);
  290. extern void parse_using_directive (tree, tree);
  291. extern tree innermost_non_namespace_value (tree);
  292. extern cxx_binding *outer_binding (tree, cxx_binding *, bool);
  293. extern void cp_emit_debug_info_for_using (tree, tree);
  294. /* Set *DECL to the (non-hidden) declaration for ID at global scope,
  295. if present and return true; otherwise return false. */
  296. inline bool
  297. get_global_value_if_present (tree id, tree *decl)
  298. {
  299. tree global_value = namespace_binding (id, global_namespace);
  300. if (global_value)
  301. *decl = global_value;
  302. return global_value != NULL;
  303. }
  304. /* True is the binding of IDENTIFIER at global scope names a type. */
  305. inline bool
  306. is_typename_at_global_scope (tree id)
  307. {
  308. tree global_value = namespace_binding (id, global_namespace);
  309. return global_value && TREE_CODE (global_value) == TYPE_DECL;
  310. }
  311. #endif /* GCC_CP_NAME_LOOKUP_H */