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/Symbol/Type.h"
15 #include "lldb/Utility/ConstString.h"
16 #include "lldb/lldb-private.h"
17 
18 namespace lldb_private {
19 
20 /// Represents a generic declaration context in a program. A declaration context
21 /// is data structure that contains declarations (e.g. namespaces).
22 ///
23 /// This class serves as an abstraction for a declaration context inside one of
24 /// the TypeSystems implemented by the language plugins. It does not have any
25 /// actual logic in it but only stores an opaque pointer and a pointer to the
26 /// TypeSystem that gives meaning to this opaque pointer. All methods of this
27 /// class should call their respective method in the TypeSystem interface and
28 /// pass the opaque pointer along.
29 ///
30 /// \see lldb_private::TypeSystem
31 class CompilerDeclContext {
32 public:
33   /// Constructs an invalid CompilerDeclContext.
34   CompilerDeclContext() = default;
35 
36   /// Constructs a CompilerDeclContext with the given opaque decl context
37   /// and its respective TypeSystem instance.
38   ///
39   /// This constructor should only be called from the respective TypeSystem
40   /// implementation.
41   ///
42   /// \see lldb_private::TypeSystemClang::CreateDeclContext(clang::DeclContext*)
CompilerDeclContext(TypeSystem * type_system,void * decl_ctx)43   CompilerDeclContext(TypeSystem *type_system, void *decl_ctx)
44       : m_type_system(type_system), m_opaque_decl_ctx(decl_ctx) {}
45 
46   // Tests
47 
48   explicit operator bool() const { return IsValid(); }
49 
50   bool operator<(const CompilerDeclContext &rhs) const {
51     if (m_type_system == rhs.m_type_system)
52       return m_opaque_decl_ctx < rhs.m_opaque_decl_ctx;
53     return m_type_system < rhs.m_type_system;
54   }
55 
IsValid()56   bool IsValid() const {
57     return m_type_system != nullptr && m_opaque_decl_ctx != nullptr;
58   }
59 
60   /// Populate a valid compiler context from the current decl context.
61   ///
62   /// \returns A valid vector of CompilerContext entries that describes
63   /// this declaration context. The first entry in the vector is the parent of
64   /// the subsequent entry, so the topmost entry is the global namespace.
65   std::vector<lldb_private::CompilerContext> GetCompilerContext() const;
66 
67   std::vector<CompilerDecl> FindDeclByName(ConstString name,
68                                            const bool ignore_using_decls);
69 
70   /// Checks if this decl context represents a method of a class.
71   ///
72   /// \return
73   ///     Returns true if this is a decl context that represents a method
74   ///     in a struct, union or class.
75   bool IsClassMethod();
76 
77   /// Determines the original language of the decl context.
78   lldb::LanguageType GetLanguage();
79 
80   /// Check if the given other decl context is contained in the lookup
81   /// of this decl context (for example because the other context is a nested
82   /// inline namespace).
83   ///
84   /// @param[in] other
85   ///     The other decl context for which we should check if it is contained
86   ///     in the lookoup of this context.
87   ///
88   /// @return
89   ///     Returns true iff the other decl context is contained in the lookup
90   ///     of this decl context.
91   bool IsContainedInLookup(CompilerDeclContext other) const;
92 
93   // Accessors
94 
GetTypeSystem()95   TypeSystem *GetTypeSystem() const { return m_type_system; }
96 
GetOpaqueDeclContext()97   void *GetOpaqueDeclContext() const { return m_opaque_decl_ctx; }
98 
SetDeclContext(TypeSystem * type_system,void * decl_ctx)99   void SetDeclContext(TypeSystem *type_system, void *decl_ctx) {
100     m_type_system = type_system;
101     m_opaque_decl_ctx = decl_ctx;
102   }
103 
Clear()104   void Clear() {
105     m_type_system = nullptr;
106     m_opaque_decl_ctx = nullptr;
107   }
108 
109   ConstString GetName() const;
110 
111   ConstString GetScopeQualifiedName() const;
112 
113 private:
114   TypeSystem *m_type_system = nullptr;
115   void *m_opaque_decl_ctx = nullptr;
116 };
117 
118 bool operator==(const CompilerDeclContext &lhs, const CompilerDeclContext &rhs);
119 bool operator!=(const CompilerDeclContext &lhs, const CompilerDeclContext &rhs);
120 
121 } // namespace lldb_private
122 
123 #endif // LLDB_SYMBOL_COMPILERDECLCONTEXT_H
124