1 //===-- CompilerDeclContext.h -----------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef liblldb_CompilerDeclContext_h_
10 #define liblldb_CompilerDeclContext_h_
14 #include "lldb/Utility/ConstString.h"
15 #include "lldb/lldb-private.h"
17 namespace lldb_private {
19 /// Represents a generic declaration context in a program. A declaration context
20 /// is data structure that contains declarations (e.g. namespaces).
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.
29 /// \see lldb_private::TypeSystem
30 class CompilerDeclContext {
32 /// Constructs an invalid CompilerDeclContext.
33 CompilerDeclContext() = default;
35 /// Constructs a CompilerDeclContext with the given opaque decl context
36 /// and its respective TypeSystem instance.
38 /// This constructor should only be called from the respective TypeSystem
41 /// \see lldb_private::ClangASTContext::CreateDeclContext(clang::DeclContext*)
42 CompilerDeclContext(TypeSystem *type_system, void *decl_ctx)
43 : m_type_system(type_system), m_opaque_decl_ctx(decl_ctx) {}
47 explicit operator bool() const { return IsValid(); }
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;
55 bool IsValid() const {
56 return m_type_system != nullptr && m_opaque_decl_ctx != nullptr;
59 std::vector<CompilerDecl> FindDeclByName(ConstString name,
60 const bool ignore_using_decls);
62 /// Checks if this decl context represents a method of a class.
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.
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).
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.
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);
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).
91 /// The other decl context for which we should check if it is contained
92 /// in the lookoup of this context.
95 /// Returns true iff the other decl context is contained in the lookup
96 /// of this decl context.
97 bool IsContainedInLookup(CompilerDeclContext other) const;
101 TypeSystem *GetTypeSystem() const { return m_type_system; }
103 void *GetOpaqueDeclContext() const { return m_opaque_decl_ctx; }
105 void SetDeclContext(TypeSystem *type_system, void *decl_ctx) {
106 m_type_system = type_system;
107 m_opaque_decl_ctx = decl_ctx;
111 m_type_system = nullptr;
112 m_opaque_decl_ctx = nullptr;
115 ConstString GetName() const;
117 ConstString GetScopeQualifiedName() const;
120 TypeSystem *m_type_system = nullptr;
121 void *m_opaque_decl_ctx = nullptr;
124 bool operator==(const CompilerDeclContext &lhs, const CompilerDeclContext &rhs);
125 bool operator!=(const CompilerDeclContext &lhs, const CompilerDeclContext &rhs);
127 } // namespace lldb_private
129 #endif // #ifndef liblldb_CompilerDeclContext_h_