xref: /freebsd/contrib/llvm-project/lldb/include/lldb/Symbol/CompilerDecl.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 //===-- CompilerDecl.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_COMPILERDECL_H
10 #define LLDB_SYMBOL_COMPILERDECL_H
11 
12 #include "lldb/Symbol/CompilerType.h"
13 #include "lldb/Utility/ConstString.h"
14 #include "lldb/lldb-private.h"
15 
16 namespace lldb_private {
17 
18 /// Represents a generic declaration such as a function declaration.
19 ///
20 /// This class serves as an abstraction for a declaration inside one of the
21 /// TypeSystems implemented by the language plugins. It does not have any actual
22 /// logic in it but only stores an opaque pointer and a pointer to the
23 /// TypeSystem that gives meaning to this opaque pointer. All methods of this
24 /// class should call their respective method in the TypeSystem interface and
25 /// pass the opaque pointer along.
26 ///
27 /// \see lldb_private::TypeSystem
28 class CompilerDecl {
29 public:
30   // Constructors and Destructors
31   CompilerDecl() = default;
32 
33   /// Creates a CompilerDecl with the given TypeSystem and opaque pointer.
34   ///
35   /// This constructor should only be called from the respective TypeSystem
36   /// implementation.
CompilerDecl(TypeSystem * type_system,void * decl)37   CompilerDecl(TypeSystem *type_system, void *decl)
38       : m_type_system(type_system), m_opaque_decl(decl) {}
39 
40   // Tests
41 
42   explicit operator bool() const { return IsValid(); }
43 
44   bool operator<(const CompilerDecl &rhs) const {
45     if (m_type_system == rhs.m_type_system)
46       return m_opaque_decl < rhs.m_opaque_decl;
47     return m_type_system < rhs.m_type_system;
48   }
49 
IsValid()50   bool IsValid() const {
51     return m_type_system != nullptr && m_opaque_decl != nullptr;
52   }
53 
54   // Accessors
55 
GetTypeSystem()56   TypeSystem *GetTypeSystem() const { return m_type_system; }
57 
GetOpaqueDecl()58   void *GetOpaqueDecl() const { return m_opaque_decl; }
59 
SetDecl(TypeSystem * type_system,void * decl)60   void SetDecl(TypeSystem *type_system, void *decl) {
61     m_type_system = type_system;
62     m_opaque_decl = decl;
63   }
64 
Clear()65   void Clear() {
66     m_type_system = nullptr;
67     m_opaque_decl = nullptr;
68   }
69 
70   ConstString GetName() const;
71 
72   ConstString GetMangledName() const;
73 
74   CompilerDeclContext GetDeclContext() const;
75 
76   // If this decl has a type, return it.
77   CompilerType GetType() const;
78 
79   // If this decl represents a function, return the return type
80   CompilerType GetFunctionReturnType() const;
81 
82   // If this decl represents a function, return the number of arguments for the
83   // function
84   size_t GetNumFunctionArguments() const;
85 
86   // If this decl represents a function, return the argument type given a zero
87   // based argument index
88   CompilerType GetFunctionArgumentType(size_t arg_idx) const;
89 
90   /// Populate a valid compiler context from the current declaration.
91   ///
92   /// \returns A valid vector of CompilerContext entries that describes
93   /// this declaration. The first entry in the vector is the parent of
94   /// the subsequent entry, so the topmost entry is the global namespace.
95   std::vector<lldb_private::CompilerContext> GetCompilerContext() const;
96 
97   // If decl represents a constant value, return it. Otherwise, return an
98   // invalid/empty Scalar.
99   Scalar GetConstantValue() const;
100 
101 private:
102   TypeSystem *m_type_system = nullptr;
103   void *m_opaque_decl = nullptr;
104 };
105 
106 bool operator==(const CompilerDecl &lhs, const CompilerDecl &rhs);
107 bool operator!=(const CompilerDecl &lhs, const CompilerDecl &rhs);
108 
109 } // namespace lldb_private
110 
111 #endif // LLDB_SYMBOL_COMPILERDECL_H
112