1//===-- CompilerDeclContext.h -----------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_SYMBOL_COMPILERDECLCONTEXT_H
10#define LLDB_SYMBOL_COMPILERDECLCONTEXT_H
11
12#include <vector>
13
14#include "lldb/Utility/ConstString.h"
15#include "lldb/lldb-private.h"
16
17namespace lldb_private {
18
19/// Represents a generic declaration context in a program. A declaration context
20/// is data structure that contains declarations (e.g. namespaces).
21///
22/// This class serves as an abstraction for a declaration context inside one of
23/// the TypeSystems implemented by the language plugins. It does not have any
24/// actual logic in it but only stores an opaque pointer and a pointer to the
25/// TypeSystem that gives meaning to this opaque pointer. All methods of this
26/// class should call their respective method in the TypeSystem interface and
27/// pass the opaque pointer along.
28///
29/// \see lldb_private::TypeSystem
30class CompilerDeclContext {
31public:
32  /// Constructs an invalid CompilerDeclContext.
33  CompilerDeclContext() = default;
34
35  /// Constructs a CompilerDeclContext with the given opaque decl context
36  /// and its respective TypeSystem instance.
37  ///
38  /// This constructor should only be called from the respective TypeSystem
39  /// implementation.
40  ///
41  /// \see lldb_private::TypeSystemClang::CreateDeclContext(clang::DeclContext*)
42  CompilerDeclContext(TypeSystem *type_system, void *decl_ctx)
43      : m_type_system(type_system), m_opaque_decl_ctx(decl_ctx) {}
44
45  // Tests
46
47  explicit operator bool() const { return IsValid(); }
48
49  bool operator<(const CompilerDeclContext &rhs) const {
50    if (m_type_system == rhs.m_type_system)
51      return m_opaque_decl_ctx < rhs.m_opaque_decl_ctx;
52    return m_type_system < rhs.m_type_system;
53  }
54
55  bool IsValid() const {
56    return m_type_system != nullptr && m_opaque_decl_ctx != nullptr;
57  }
58
59  std::vector<CompilerDecl> FindDeclByName(ConstString name,
60                                           const bool ignore_using_decls);
61
62  /// Checks if this decl context represents a method of a class.
63  ///
64  /// \param[out] language_ptr
65  ///     If non NULL and \b true is returned from this function,
66  ///     this will indicate if the language that respresents the method.
67  ///
68  /// \param[out] is_instance_method_ptr
69  ///     If non NULL and \b true is returned from this function,
70  ///     this will indicate if the method is an instance function (true)
71  ///     or a class method (false indicating the function is static, or
72  ///     doesn't require an instance of the class to be called).
73  ///
74  /// \param[out] language_object_name_ptr
75  ///     If non NULL and \b true is returned from this function,
76  ///     this will indicate if implicit object name for the language
77  ///     like "this" for C++, and "self" for Objective C.
78  ///
79  /// \return
80  ///     Returns true if this is a decl context that represents a method
81  ///     in a struct, union or class.
82  bool IsClassMethod(lldb::LanguageType *language_ptr,
83                     bool *is_instance_method_ptr,
84                     ConstString *language_object_name_ptr);
85
86  /// Check if the given other decl context is contained in the lookup
87  /// of this decl context (for example because the other context is a nested
88  /// inline namespace).
89  ///
90  /// @param[in] other
91  ///     The other decl context for which we should check if it is contained
92  ///     in the lookoup of this context.
93  ///
94  /// @return
95  ///     Returns true iff the other decl context is contained in the lookup
96  ///     of this decl context.
97  bool IsContainedInLookup(CompilerDeclContext other) const;
98
99  // Accessors
100
101  TypeSystem *GetTypeSystem() const { return m_type_system; }
102
103  void *GetOpaqueDeclContext() const { return m_opaque_decl_ctx; }
104
105  void SetDeclContext(TypeSystem *type_system, void *decl_ctx) {
106    m_type_system = type_system;
107    m_opaque_decl_ctx = decl_ctx;
108  }
109
110  void Clear() {
111    m_type_system = nullptr;
112    m_opaque_decl_ctx = nullptr;
113  }
114
115  ConstString GetName() const;
116
117  ConstString GetScopeQualifiedName() const;
118
119private:
120  TypeSystem *m_type_system = nullptr;
121  void *m_opaque_decl_ctx = nullptr;
122};
123
124bool operator==(const CompilerDeclContext &lhs, const CompilerDeclContext &rhs);
125bool operator!=(const CompilerDeclContext &lhs, const CompilerDeclContext &rhs);
126
127} // namespace lldb_private
128
129#endif // LLDB_SYMBOL_COMPILERDECLCONTEXT_H
130