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 
17 namespace 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
30 class CompilerDeclContext {
31 public:
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   /// \return
65   ///     Returns true if this is a decl context that represents a method
66   ///     in a struct, union or class.
67   bool IsClassMethod();
68 
69   /// Determines the original language of the decl context.
70   lldb::LanguageType GetLanguage();
71 
72   /// Check if the given other decl context is contained in the lookup
73   /// of this decl context (for example because the other context is a nested
74   /// inline namespace).
75   ///
76   /// @param[in] other
77   ///     The other decl context for which we should check if it is contained
78   ///     in the lookoup of this context.
79   ///
80   /// @return
81   ///     Returns true iff the other decl context is contained in the lookup
82   ///     of this decl context.
83   bool IsContainedInLookup(CompilerDeclContext other) const;
84 
85   // Accessors
86 
87   TypeSystem *GetTypeSystem() const { return m_type_system; }
88 
89   void *GetOpaqueDeclContext() const { return m_opaque_decl_ctx; }
90 
91   void SetDeclContext(TypeSystem *type_system, void *decl_ctx) {
92     m_type_system = type_system;
93     m_opaque_decl_ctx = decl_ctx;
94   }
95 
96   void Clear() {
97     m_type_system = nullptr;
98     m_opaque_decl_ctx = nullptr;
99   }
100 
101   ConstString GetName() const;
102 
103   ConstString GetScopeQualifiedName() const;
104 
105 private:
106   TypeSystem *m_type_system = nullptr;
107   void *m_opaque_decl_ctx = nullptr;
108 };
109 
110 bool operator==(const CompilerDeclContext &lhs, const CompilerDeclContext &rhs);
111 bool operator!=(const CompilerDeclContext &lhs, const CompilerDeclContext &rhs);
112 
113 } // namespace lldb_private
114 
115 #endif // LLDB_SYMBOL_COMPILERDECLCONTEXT_H
116