xref: /llvm-project/lldb/include/lldb/Core/Declaration.h (revision 7dbbd2b251412b7b0809aabe672f3f57f0805dbb)
1 //===-- Declaration.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_DECLARATION_H
10 #define LLDB_SYMBOL_DECLARATION_H
11 
12 #include "lldb/Utility/FileSpec.h"
13 #include "lldb/lldb-private.h"
14 
15 namespace lldb_private {
16 
17 /// \class Declaration Declaration.h "lldb/Core/Declaration.h"
18 /// A class that describes the declaration location of a
19 ///        lldb object.
20 ///
21 /// The declarations include the file specification, line number, and the
22 /// column info and can help track where functions, blocks, inlined functions,
23 /// types, variables, any many other debug core objects were declared.
24 class Declaration {
25 public:
26   /// Default constructor.
27   Declaration() = default;
28 
29   /// Construct with file specification, and optional line and column.
30   ///
31   /// \param[in] file_spec
32   ///     The file specification that describes where this was
33   ///     declared.
34   ///
35   /// \param[in] line
36   ///     The line number that describes where this was declared. Set
37   ///     to zero if there is no line number information.
38   ///
39   /// \param[in] column
40   ///     The column number that describes where this was declared.
41   ///     Set to zero if there is no column number information.
42   Declaration(const FileSpec &file_spec, uint32_t line = 0,
43               uint16_t column = LLDB_INVALID_COLUMN_NUMBER)
44       : m_file(file_spec), m_line(line), m_column(column) {}
45 
46   /// Construct with a pointer to another Declaration object.
47   Declaration(const Declaration *decl_ptr)
48       : m_line(0), m_column(LLDB_INVALID_COLUMN_NUMBER) {
49     if (decl_ptr)
50       *this = *decl_ptr;
51   }
52 
53   /// Clear the object's state.
54   ///
55   /// Sets the file specification to be empty, and the line and column to
56   /// zero.
57   void Clear() {
58     m_file.Clear();
59     m_line = 0;
60     m_column = 0;
61   }
62 
63   /// Compare two declaration objects.
64   ///
65   /// Compares the two file specifications from \a lhs and \a rhs. If the file
66   /// specifications are equal, then continue to compare the line number and
67   /// column numbers respectively.
68   ///
69   /// \param[in] lhs
70   ///     The Left Hand Side const Declaration object reference.
71   ///
72   /// \param[in] rhs
73   ///     The Right Hand Side const Declaration object reference.
74   ///
75   /// \return
76   ///     -1 if lhs < rhs
77   ///     0 if lhs == rhs
78   ///     1 if lhs > rhs
79   static int Compare(const Declaration &lhs, const Declaration &rhs);
80 
81   /// Checks if this object has the same file and line as another declaration
82   /// object.
83   ///
84   /// \param[in] declaration
85   ///     The const Declaration object to compare with.
86   ///
87   /// \param[in] full
88   ///     Same meaning as Full in FileSpec::Equal.  True means an empty
89   ///     directory is not equal to a specified one, false means it is equal.
90   ///
91   /// \return
92   ///     Returns \b true if \b declaration is at the same file and
93   ///     line, \b false otherwise.
94   bool FileAndLineEqual(const Declaration &declaration, bool full) const;
95 
96   /// Dump a description of this object to a Stream.
97   ///
98   /// Dump a description of the contents of this object to the supplied stream
99   /// \a s.
100   ///
101   /// \param[in] s
102   ///     The stream to which to dump the object description.
103   void Dump(Stream *s, bool show_fullpaths) const;
104 
105   bool DumpStopContext(Stream *s, bool show_fullpaths) const;
106 
107   /// Get accessor for file specification.
108   ///
109   /// \return
110   ///     A reference to the file specification object.
111   FileSpec &GetFile() { return m_file; }
112 
113   /// Get const accessor for file specification.
114   ///
115   /// \return
116   ///     A const reference to the file specification object.
117   const FileSpec &GetFile() const { return m_file; }
118 
119   /// Get accessor for the declaration line number.
120   ///
121   /// \return
122   ///     Non-zero indicates a valid line number, zero indicates no
123   ///     line information is available.
124   uint32_t GetLine() const { return m_line; }
125 
126   /// Get accessor for the declaration column number.
127   ///
128   /// \return
129   ///     Non-zero indicates a valid column number, zero indicates no
130   ///     column information is available.
131   uint16_t GetColumn() const { return m_column; }
132 
133   /// Convert to boolean operator.
134   ///
135   /// This allows code to check a Declaration object to see if it
136   /// contains anything valid using code such as:
137   ///
138   /// \code
139   /// Declaration decl(...);
140   /// if (decl)
141   /// { ...
142   /// \endcode
143   ///
144   /// \return
145   ///     A \b true if both the file_spec and the line are valid,
146   ///     \b false otherwise.
147   explicit operator bool() const { return IsValid(); }
148 
149   bool IsValid() const {
150     return m_file && m_line != 0 && m_line != LLDB_INVALID_LINE_NUMBER;
151   }
152 
153   /// Get the memory cost of this object.
154   ///
155   /// \return
156   ///     The number of bytes that this object occupies in memory.
157   ///     The returned value does not include the bytes for any
158   ///     shared string values.
159   size_t MemorySize() const;
160 
161   /// Set accessor for the declaration file specification.
162   ///
163   /// \param[in] file_spec
164   ///     The new declaration file specification.
165   void SetFile(const FileSpec &file_spec) { m_file = file_spec; }
166 
167   /// Set accessor for the declaration line number.
168   ///
169   /// \param[in] line
170   ///     Non-zero indicates a valid line number, zero indicates no
171   ///     line information is available.
172   void SetLine(uint32_t line) { m_line = line; }
173 
174   /// Set accessor for the declaration column number.
175   ///
176   /// \param[in] column
177   ///     Non-zero indicates a valid column number, zero indicates no
178   ///     column information is available.
179   void SetColumn(uint16_t column) { m_column = column; }
180 
181 protected:
182   /// The file specification that points to the source file where the
183   /// declaration occurred.
184   FileSpec m_file;
185   /// Non-zero values indicates a valid line number, zero indicates no line
186   /// number information is available.
187   uint32_t m_line = 0;
188   /// Non-zero values indicates a valid column number, zero indicates no column
189   /// information is available.
190   uint16_t m_column = LLDB_INVALID_COLUMN_NUMBER;
191 };
192 
193 bool operator==(const Declaration &lhs, const Declaration &rhs);
194 
195 } // namespace lldb_private
196 
197 #endif // LLDB_SYMBOL_DECLARATION_H
198