LLDB mainline
Language.h
Go to the documentation of this file.
1//===-- Language.h ---------------------------------------------------*- C++
2//-*-===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLDB_TARGET_LANGUAGE_H
11#define LLDB_TARGET_LANGUAGE_H
12
13#include <functional>
14#include <memory>
15#include <set>
16#include <vector>
17
24#include "lldb/lldb-private.h"
25#include "lldb/lldb-public.h"
26
27namespace lldb_private {
28
29class Language : public PluginInterface {
30public:
32 public:
33 class Result {
34 public:
35 virtual bool IsValid() = 0;
36
37 virtual bool DumpToStream(Stream &stream,
38 bool print_help_if_available) = 0;
39
40 virtual ~Result() = default;
41 };
42
43 typedef std::set<std::unique_ptr<Result>> ResultSet;
44
45 virtual ~TypeScavenger() = default;
46
47 size_t Find(ExecutionContextScope *exe_scope, const char *key,
48 ResultSet &results, bool append = true);
49
50 protected:
51 TypeScavenger() = default;
52
53 virtual bool Find_Impl(ExecutionContextScope *exe_scope, const char *key,
54 ResultSet &results) = 0;
55 };
56
59 public:
61
62 bool IsValid() override { return m_compiler_type.IsValid(); }
63
64 bool DumpToStream(Stream &stream, bool print_help_if_available) override {
65 if (IsValid()) {
67 stream.EOL();
68 return true;
69 }
70 return false;
71 }
72
73 ~Result() override = default;
74
75 private:
77 };
78
79 protected:
81
82 ~ImageListTypeScavenger() override = default;
83
84 // is this type something we should accept? it's usually going to be a
85 // filter by language + maybe some sugar tweaking
86 // returning an empty type means rejecting this candidate entirely;
87 // any other result will be accepted as a valid match
89
90 bool Find_Impl(ExecutionContextScope *exe_scope, const char *key,
91 ResultSet &results) override;
92 };
93
94 template <typename... ScavengerTypes>
96 public:
98 for (std::shared_ptr<TypeScavenger> scavenger : { std::shared_ptr<TypeScavenger>(new ScavengerTypes())... }) {
99 if (scavenger)
100 m_scavengers.push_back(scavenger);
101 }
102 }
103 protected:
104 bool Find_Impl(ExecutionContextScope *exe_scope, const char *key,
105 ResultSet &results) override {
106 const bool append = false;
107 for (auto& scavenger : m_scavengers) {
108 if (scavenger && scavenger->Find(exe_scope, key, results, append))
109 return true;
110 }
111 return false;
112 }
113 private:
114 std::vector<std::shared_ptr<TypeScavenger>> m_scavengers;
115 };
116
117 template <typename... ScavengerTypes>
119 public:
121 for (std::shared_ptr<TypeScavenger> scavenger : { std::shared_ptr<TypeScavenger>(new ScavengerTypes())... }) {
122 if (scavenger)
123 m_scavengers.push_back(scavenger);
124 }
125 }
126 protected:
127 bool Find_Impl(ExecutionContextScope *exe_scope, const char *key,
128 ResultSet &results) override {
129 const bool append = true;
130 bool success = false;
131 for (auto& scavenger : m_scavengers) {
132 if (scavenger)
133 success = scavenger->Find(exe_scope, key, results, append) || success;
134 }
135 return success;
136 }
137 private:
138 std::vector<std::shared_ptr<TypeScavenger>> m_scavengers;
139 };
140
142 eName,
145 };
146
147 ~Language() override;
148
149 static Language *FindPlugin(lldb::LanguageType language);
150
151 /// Returns the Language associated with the given file path or a nullptr
152 /// if there is no known language.
153 static Language *FindPlugin(llvm::StringRef file_path);
154
155 static Language *FindPlugin(lldb::LanguageType language,
156 llvm::StringRef file_path);
157
158 // return false from callback to stop iterating
159 static void ForEach(std::function<bool(Language *)> callback);
160
162
163 virtual bool IsTopLevelFunction(Function &function);
164
165 virtual bool IsSourceFile(llvm::StringRef file_path) const = 0;
166
167 virtual const Highlighter *GetHighlighter() const { return nullptr; }
168
169 virtual lldb::TypeCategoryImplSP GetFormatters();
170
172
174
177
178 virtual std::vector<FormattersMatchCandidate>
180 lldb::DynamicValueType use_dynamic);
181
182 virtual std::unique_ptr<TypeScavenger> GetTypeScavenger();
183
184 virtual const char *GetLanguageSpecificTypeLookupHelp();
185
188 lldb::FunctionNameType m_type;
189
190 public:
191 MethodNameVariant(ConstString name, lldb::FunctionNameType type)
192 : m_name(name), m_type(type) {}
193 ConstString GetName() const { return m_name; }
194 lldb::FunctionNameType GetType() const { return m_type; }
195 };
196 // If a language can have more than one possible name for a method, this
197 // function can be used to enumerate them. This is useful when doing name
198 // lookups.
199 virtual std::vector<Language::MethodNameVariant>
201 return std::vector<Language::MethodNameVariant>();
202 };
203
204 /// Returns true iff the given symbol name is compatible with the mangling
205 /// scheme of this language.
206 ///
207 /// This function should only return true if there is a high confidence
208 /// that the name actually belongs to this language.
209 virtual bool SymbolNameFitsToLanguage(Mangled name) const { return false; }
210
211 /// An individual data formatter may apply to several types and cross language
212 /// boundaries. Each of those languages may want to customize the display of
213 /// values of said types by appending proper prefix/suffix information in
214 /// language-specific ways. This function returns that prefix and suffix.
215 ///
216 /// \param[in] type_hint
217 /// A StringRef used to determine what the prefix and suffix should be. It
218 /// is called a hint because some types may have multiple variants for which
219 /// the prefix and/or suffix may vary.
220 ///
221 /// \return
222 /// A std::pair<StringRef, StringRef>, the first being the prefix and the
223 /// second being the suffix. They may be empty.
224 virtual std::pair<llvm::StringRef, llvm::StringRef>
225 GetFormatterPrefixSuffix(llvm::StringRef type_hint);
226
227 // When looking up functions, we take a user provided string which may be a
228 // partial match to the full demangled name and compare it to the actual
229 // demangled name to see if it matches as much as the user specified. An
230 // example of this is if the user provided A::my_function, but the
231 // symbol was really B::A::my_function. We want that to be
232 // a match. But we wouldn't want this to match AnotherA::my_function. The
233 // user is specifying a truncated path, not a truncated set of characters.
234 // This function does a language-aware comparison for those purposes.
235 virtual bool DemangledNameContainsPath(llvm::StringRef path,
236 ConstString demangled) const;
237
238 // if a language has a custom format for printing variable declarations that
239 // it wants LLDB to honor it should return an appropriate closure here
241
242 virtual LazyBool IsLogicalTrue(ValueObject &valobj, Status &error);
243
244 // for a ValueObject of some "reference type", if the value points to the
245 // nil/null object, this method returns true
246 virtual bool IsNilReference(ValueObject &valobj);
247
248 /// Returns the summary string for ValueObjects for which IsNilReference() is
249 /// true.
250 virtual llvm::StringRef GetNilReferenceSummaryString() { return {}; }
251
252 // for a ValueObject of some "reference type", if the language provides a
253 // technique to decide whether the reference has ever been assigned to some
254 // object, this method will return true if such detection is possible, and if
255 // the reference has never been assigned
256 virtual bool IsUninitializedReference(ValueObject &valobj);
257
258 virtual bool GetFunctionDisplayName(const SymbolContext *sc,
259 const ExecutionContext *exe_ctx,
260 FunctionNameRepresentation representation,
261 Stream &s);
262
263 virtual ConstString
265 if (ConstString demangled = mangled.GetDemangledName())
266 return demangled;
267
268 return mangled.GetMangledName();
269 }
270
271 virtual void GetExceptionResolverDescription(bool catch_on, bool throw_on,
272 Stream &s);
273
274 static void GetDefaultExceptionResolverDescription(bool catch_on,
275 bool throw_on, Stream &s);
276
277 // These are accessors for general information about the Languages lldb knows
278 // about:
279
280 static lldb::LanguageType
281 GetLanguageTypeFromString(const char *string) = delete;
282 static lldb::LanguageType GetLanguageTypeFromString(llvm::StringRef string);
283
284 static const char *GetNameForLanguageType(lldb::LanguageType language);
285
286 static void PrintAllLanguages(Stream &s, const char *prefix,
287 const char *suffix);
288
289 /// Prints to the specified stream 's' each language type that the
290 /// current target supports for expression evaluation.
291 ///
292 /// \param[out] s Stream to which the language types are written.
293 /// \param[in] prefix String that is prepended to the language type.
294 /// \param[in] suffix String that is appended to the language type.
296 llvm::StringRef prefix,
297 llvm::StringRef suffix);
298
299 // return false from callback to stop iterating
300 static void ForAllLanguages(std::function<bool(lldb::LanguageType)> callback);
301
302 static bool LanguageIsCPlusPlus(lldb::LanguageType language);
303
304 static bool LanguageIsObjC(lldb::LanguageType language);
305
306 static bool LanguageIsC(lldb::LanguageType language);
307
308 /// Equivalent to \c LanguageIsC||LanguageIsObjC||LanguageIsCPlusPlus.
309 static bool LanguageIsCFamily(lldb::LanguageType language);
310
311 static bool LanguageIsPascal(lldb::LanguageType language);
312
313 // return the primary language, so if LanguageIsC(l), return eLanguageTypeC,
314 // etc.
316
317 static std::set<lldb::LanguageType> GetSupportedLanguages();
318
322
323 // Given a mangled function name, calculates some alternative manglings since
324 // the compiler mangling may not line up with the symbol we are expecting.
325 virtual std::vector<ConstString>
327 return std::vector<ConstString>();
328 }
329
330 virtual ConstString
332 const SymbolContext &sym_ctx) const {
333 return ConstString();
334 }
335
336 virtual llvm::StringRef GetInstanceVariableName() { return {}; }
337
338protected:
339 // Classes that inherit from Language can see and modify these
340
342
343private:
344 Language(const Language &) = delete;
345 const Language &operator=(const Language &) = delete;
346};
347
348} // namespace lldb_private
349
350#endif // LLDB_TARGET_LANGUAGE_H
static llvm::raw_ostream & error(Stream &strm)
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
void DumpTypeDescription(lldb::DescriptionLevel level=lldb::eDescriptionLevelFull) const
Dump to stdout.
A uniqued constant string class.
Definition: ConstString.h:40
std::function< bool(ConstString, ConstString, const DumpValueObjectOptions &, Stream &)> DeclPrintingHelper
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A class that describes a function.
Definition: Function.h:409
HardcodedFormatterFinders< SyntheticChildren > HardcodedSyntheticFinder
Definition: FormatClasses.h:42
HardcodedFormatterFinders< TypeSummaryImpl > HardcodedSummaryFinder
Definition: FormatClasses.h:41
HardcodedFormatterFinders< TypeFormatImpl > HardcodedFormatFinder
Definition: FormatClasses.h:40
Annotates source code with color attributes.
Definition: Highlighter.h:91
bool Find_Impl(ExecutionContextScope *exe_scope, const char *key, ResultSet &results) override
Definition: Language.h:104
std::vector< std::shared_ptr< TypeScavenger > > m_scavengers
Definition: Language.h:114
bool DumpToStream(Stream &stream, bool print_help_if_available) override
Definition: Language.h:64
virtual CompilerType AdjustForInclusion(CompilerType &candidate)=0
bool Find_Impl(ExecutionContextScope *exe_scope, const char *key, ResultSet &results) override
Definition: Language.cpp:426
MethodNameVariant(ConstString name, lldb::FunctionNameType type)
Definition: Language.h:191
lldb::FunctionNameType GetType() const
Definition: Language.h:194
virtual bool DumpToStream(Stream &stream, bool print_help_if_available)=0
std::set< std::unique_ptr< Result > > ResultSet
Definition: Language.h:43
virtual bool Find_Impl(ExecutionContextScope *exe_scope, const char *key, ResultSet &results)=0
size_t Find(ExecutionContextScope *exe_scope, const char *key, ResultSet &results, bool append=true)
Definition: Language.cpp:407
std::vector< std::shared_ptr< TypeScavenger > > m_scavengers
Definition: Language.h:138
bool Find_Impl(ExecutionContextScope *exe_scope, const char *key, ResultSet &results) override
Definition: Language.h:127
virtual bool IsSourceFile(llvm::StringRef file_path) const =0
static void PrintSupportedLanguagesForExpressions(Stream &s, llvm::StringRef prefix, llvm::StringRef suffix)
Prints to the specified stream 's' each language type that the current target supports for expression...
Definition: Language.cpp:239
static LanguageSet GetLanguagesSupportingREPLs()
Definition: Language.cpp:397
static LanguageSet GetLanguagesSupportingTypeSystems()
Definition: Language.cpp:389
virtual std::vector< FormattersMatchCandidate > GetPossibleFormattersMatches(ValueObject &valobj, lldb::DynamicValueType use_dynamic)
Definition: Language.cpp:148
virtual lldb::TypeCategoryImplSP GetFormatters()
Definition: Language.cpp:132
virtual llvm::StringRef GetInstanceVariableName()
Definition: Language.h:336
static Language * FindPlugin(lldb::LanguageType language)
Definition: Language.cpp:53
static const char * GetNameForLanguageType(lldb::LanguageType language)
Definition: Language.cpp:232
virtual DumpValueObjectOptions::DeclPrintingHelper GetDeclPrintingHelper()
Definition: Language.cpp:468
virtual LazyBool IsLogicalTrue(ValueObject &valobj, Status &error)
Definition: Language.cpp:472
virtual ConstString GetDemangledFunctionNameWithoutArguments(Mangled mangled) const
Definition: Language.h:264
virtual const char * GetLanguageSpecificTypeLookupHelp()
Definition: Language.cpp:405
virtual std::pair< llvm::StringRef, llvm::StringRef > GetFormatterPrefixSuffix(llvm::StringRef type_hint)
An individual data formatter may apply to several types and cross language boundaries.
Definition: Language.cpp:456
static bool LanguageIsC(lldb::LanguageType language)
Definition: Language.cpp:290
virtual lldb::LanguageType GetLanguageType() const =0
virtual HardcodedFormatters::HardcodedSummaryFinder GetHardcodedSummaries()
Definition: Language.cpp:138
virtual std::vector< Language::MethodNameVariant > GetMethodNameVariants(ConstString method_name) const
Definition: Language.h:200
static void GetDefaultExceptionResolverDescription(bool catch_on, bool throw_on, Stream &s)
Definition: Language.cpp:492
virtual bool DemangledNameContainsPath(llvm::StringRef path, ConstString demangled) const
Definition: Language.cpp:460
virtual std::vector< ConstString > GenerateAlternateFunctionManglings(const ConstString mangled) const
Definition: Language.h:326
static bool LanguageIsCPlusPlus(lldb::LanguageType language)
Definition: Language.cpp:265
static lldb::LanguageType GetPrimaryLanguage(lldb::LanguageType language)
Definition: Language.cpp:331
const Language & operator=(const Language &)=delete
virtual ConstString FindBestAlternateFunctionMangledName(const Mangled mangled, const SymbolContext &sym_ctx) const
Definition: Language.h:331
static bool LanguageIsPascal(lldb::LanguageType language)
Definition: Language.cpp:322
virtual bool IsNilReference(ValueObject &valobj)
Definition: Language.cpp:476
static void ForAllLanguages(std::function< bool(lldb::LanguageType)> callback)
Definition: Language.cpp:257
virtual bool IsUninitializedReference(ValueObject &valobj)
Definition: Language.cpp:478
virtual bool GetFunctionDisplayName(const SymbolContext *sc, const ExecutionContext *exe_ctx, FunctionNameRepresentation representation, Stream &s)
Definition: Language.cpp:480
virtual llvm::StringRef GetNilReferenceSummaryString()
Returns the summary string for ValueObjects for which IsNilReference() is true.
Definition: Language.h:250
static void PrintAllLanguages(Stream &s, const char *prefix, const char *suffix)
Definition: Language.cpp:250
virtual HardcodedFormatters::HardcodedSyntheticFinder GetHardcodedSynthetics()
Definition: Language.cpp:143
virtual const Highlighter * GetHighlighter() const
Definition: Language.h:167
virtual std::unique_ptr< TypeScavenger > GetTypeScavenger()
Definition: Language.cpp:401
static LanguageSet GetLanguagesSupportingTypeSystemsForExpressions()
Definition: Language.cpp:393
virtual bool IsTopLevelFunction(Function &function)
Definition: Language.cpp:130
virtual void GetExceptionResolverDescription(bool catch_on, bool throw_on, Stream &s)
Definition: Language.cpp:487
static lldb::LanguageType GetLanguageTypeFromString(const char *string)=delete
static void ForEach(std::function< bool(Language *)> callback)
Definition: Language.cpp:100
Language(const Language &)=delete
static bool LanguageIsCFamily(lldb::LanguageType language)
Equivalent to LanguageIsC||LanguageIsObjC||LanguageIsCPlusPlus.
Definition: Language.cpp:302
static std::set< lldb::LanguageType > GetSupportedLanguages()
Definition: Language.cpp:380
virtual HardcodedFormatters::HardcodedFormatFinder GetHardcodedFormats()
Definition: Language.cpp:134
static bool LanguageIsObjC(lldb::LanguageType language)
Definition: Language.cpp:280
virtual bool SymbolNameFitsToLanguage(Mangled name) const
Returns true iff the given symbol name is compatible with the mangling scheme of this language.
Definition: Language.h:209
An error handling class.
Definition: Status.h:44
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:128
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:33
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
LanguageType
Programming language type.
A SmallBitVector that represents a set of source languages (lldb::LanguageType).
Definition: TypeSystem.h:45