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 
18 #include "lldb/Core/Highlighter.h"
23 #include "lldb/Symbol/TypeSystem.h"
24 #include "lldb/lldb-private.h"
25 #include "lldb/lldb-public.h"
26 
27 namespace lldb_private {
28 
29 class Language : public PluginInterface {
30 public:
31  class TypeScavenger {
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:
62 
63  bool IsValid() override { return m_compiler_type.IsValid(); }
64 
65  bool DumpToStream(Stream &stream, bool print_help_if_available) override {
66  if (IsValid()) {
68  stream.EOL();
69  return true;
70  }
71  return false;
72  }
73 
74  ~Result() override = default;
75 
76  private:
78  };
79 
80  protected:
81  ImageListTypeScavenger() = default;
82 
83  ~ImageListTypeScavenger() override = default;
84 
85  // is this type something we should accept? it's usually going to be a
86  // filter by language + maybe some sugar tweaking
87  // returning an empty type means rejecting this candidate entirely;
88  // any other result will be accepted as a valid match
89  virtual CompilerType AdjustForInclusion(CompilerType &candidate) = 0;
90 
91  bool Find_Impl(ExecutionContextScope *exe_scope, const char *key,
92  ResultSet &results) override;
93  };
94 
95  template <typename... ScavengerTypes>
97  public:
99  for (std::shared_ptr<TypeScavenger> scavenger : { std::shared_ptr<TypeScavenger>(new ScavengerTypes())... }) {
100  if (scavenger)
101  m_scavengers.push_back(scavenger);
102  }
103  }
104  protected:
105  bool Find_Impl(ExecutionContextScope *exe_scope, const char *key,
106  ResultSet &results) override {
107  const bool append = false;
108  for (auto& scavenger : m_scavengers) {
109  if (scavenger && scavenger->Find(exe_scope, key, results, append))
110  return true;
111  }
112  return false;
113  }
114  private:
115  std::vector<std::shared_ptr<TypeScavenger>> m_scavengers;
116  };
117 
118  template <typename... ScavengerTypes>
120  public:
122  for (std::shared_ptr<TypeScavenger> scavenger : { std::shared_ptr<TypeScavenger>(new ScavengerTypes())... }) {
123  if (scavenger)
124  m_scavengers.push_back(scavenger);
125  }
126  }
127  protected:
128  bool Find_Impl(ExecutionContextScope *exe_scope, const char *key,
129  ResultSet &results) override {
130  const bool append = true;
131  bool success = false;
132  for (auto& scavenger : m_scavengers) {
133  if (scavenger)
134  success = scavenger->Find(exe_scope, key, results, append) || success;
135  }
136  return success;
137  }
138  private:
139  std::vector<std::shared_ptr<TypeScavenger>> m_scavengers;
140  };
141 
143  eName,
146  };
147 
148  ~Language() override;
149 
150  static Language *FindPlugin(lldb::LanguageType language);
151 
152  /// Returns the Language associated with the given file path or a nullptr
153  /// if there is no known language.
154  static Language *FindPlugin(llvm::StringRef file_path);
155 
156  static Language *FindPlugin(lldb::LanguageType language,
157  llvm::StringRef file_path);
158 
159  // return false from callback to stop iterating
160  static void ForEach(std::function<bool(Language *)> callback);
161 
162  virtual lldb::LanguageType GetLanguageType() const = 0;
163 
164  virtual bool IsTopLevelFunction(Function &function);
165 
166  virtual bool IsSourceFile(llvm::StringRef file_path) const = 0;
167 
168  virtual const Highlighter *GetHighlighter() const { return nullptr; }
169 
170  virtual lldb::TypeCategoryImplSP GetFormatters();
171 
173 
175 
178 
179  virtual std::vector<ConstString>
181  lldb::DynamicValueType use_dynamic);
182 
183  virtual std::unique_ptr<TypeScavenger> GetTypeScavenger();
184 
185  virtual const char *GetLanguageSpecificTypeLookupHelp();
186 
189  lldb::FunctionNameType m_type;
190 
191  public:
192  MethodNameVariant(ConstString name, lldb::FunctionNameType type)
193  : m_name(name), m_type(type) {}
194  ConstString GetName() const { return m_name; }
195  lldb::FunctionNameType GetType() const { return m_type; }
196  };
197  // If a language can have more than one possible name for a method, this
198  // function can be used to enumerate them. This is useful when doing name
199  // lookups.
200  virtual std::vector<Language::MethodNameVariant>
201  GetMethodNameVariants(ConstString method_name) const {
202  return std::vector<Language::MethodNameVariant>();
203  };
204 
205  /// Returns true iff the given symbol name is compatible with the mangling
206  /// scheme of this language.
207  ///
208  /// This function should only return true if there is a high confidence
209  /// that the name actually belongs to this language.
210  virtual bool SymbolNameFitsToLanguage(Mangled name) const { return false; }
211 
212  // if an individual data formatter can apply to several types and cross a
213  // language boundary it makes sense for individual languages to want to
214  // customize the printing of values of that type by appending proper
215  // prefix/suffix information in language-specific ways
216  virtual bool GetFormatterPrefixSuffix(ValueObject &valobj,
217  ConstString type_hint,
218  std::string &prefix,
219  std::string &suffix);
220 
221  // if a language has a custom format for printing variable declarations that
222  // it wants LLDB to honor it should return an appropriate closure here
224 
225  virtual LazyBool IsLogicalTrue(ValueObject &valobj, Status &error);
226 
227  // for a ValueObject of some "reference type", if the value points to the
228  // nil/null object, this method returns true
229  virtual bool IsNilReference(ValueObject &valobj);
230 
231  /// Returns the summary string for ValueObjects for which IsNilReference() is
232  /// true.
233  virtual llvm::StringRef GetNilReferenceSummaryString() { return {}; }
234 
235  // for a ValueObject of some "reference type", if the language provides a
236  // technique to decide whether the reference has ever been assigned to some
237  // object, this method will return true if such detection is possible, and if
238  // the reference has never been assigned
239  virtual bool IsUninitializedReference(ValueObject &valobj);
240 
241  virtual bool GetFunctionDisplayName(const SymbolContext *sc,
242  const ExecutionContext *exe_ctx,
243  FunctionNameRepresentation representation,
244  Stream &s);
245 
246  virtual ConstString
248  if (ConstString demangled = mangled.GetDemangledName())
249  return demangled;
250 
251  return mangled.GetMangledName();
252  }
253 
254  virtual void GetExceptionResolverDescription(bool catch_on, bool throw_on,
255  Stream &s);
256 
257  static void GetDefaultExceptionResolverDescription(bool catch_on,
258  bool throw_on, Stream &s);
259 
260  // These are accessors for general information about the Languages lldb knows
261  // about:
262 
263  static lldb::LanguageType
264  GetLanguageTypeFromString(const char *string) = delete;
265  static lldb::LanguageType GetLanguageTypeFromString(llvm::StringRef string);
266 
267  static const char *GetNameForLanguageType(lldb::LanguageType language);
268 
269  static void PrintAllLanguages(Stream &s, const char *prefix,
270  const char *suffix);
271 
272  // return false from callback to stop iterating
273  static void ForAllLanguages(std::function<bool(lldb::LanguageType)> callback);
274 
275  static bool LanguageIsCPlusPlus(lldb::LanguageType language);
276 
277  static bool LanguageIsObjC(lldb::LanguageType language);
278 
279  static bool LanguageIsC(lldb::LanguageType language);
280 
281  /// Equivalent to \c LanguageIsC||LanguageIsObjC||LanguageIsCPlusPlus.
282  static bool LanguageIsCFamily(lldb::LanguageType language);
283 
284  static bool LanguageIsPascal(lldb::LanguageType language);
285 
286  // return the primary language, so if LanguageIsC(l), return eLanguageTypeC,
287  // etc.
289 
290  static std::set<lldb::LanguageType> GetSupportedLanguages();
291 
295 
296  // Given a mangled function name, calculates some alternative manglings since
297  // the compiler mangling may not line up with the symbol we are expecting.
298  virtual std::vector<ConstString>
300  return std::vector<ConstString>();
301  }
302 
303  virtual ConstString
305  const SymbolContext &sym_ctx) const {
306  return ConstString();
307  }
308 
309 protected:
310  // Classes that inherit from Language can see and modify these
311 
312  Language();
313 
314 private:
315  Language(const Language &) = delete;
316  const Language &operator=(const Language &) = delete;
317 };
318 
319 } // namespace lldb_private
320 
321 #endif // LLDB_TARGET_LANGUAGE_H
lldb_private::Language::ImageListTypeScavenger::AdjustForInclusion
virtual CompilerType AdjustForInclusion(CompilerType &candidate)=0
lldb_private::Language::FunctionNameRepresentation::eNameWithNoArgs
@ eNameWithNoArgs
lldb_private::ExecutionContext
Definition: ExecutionContext.h:292
lldb-public.h
lldb_private::Language::Language
Language()
lldb_private::Language::GetDemangledFunctionNameWithoutArguments
virtual ConstString GetDemangledFunctionNameWithoutArguments(Mangled mangled) const
Definition: Language.h:247
lldb_private::Language::ImageListTypeScavenger::Result::IsValid
bool IsValid() override
Definition: Language.h:63
lldb_private::DumpValueObjectOptions::DeclPrintingHelper
std::function< bool(ConstString, ConstString, const DumpValueObjectOptions &, Stream &)> DeclPrintingHelper
Definition: DumpValueObjectOptions.h:54
lldb_private::Language::ImageListTypeScavenger::Result::DumpToStream
bool DumpToStream(Stream &stream, bool print_help_if_available) override
Definition: Language.h:65
lldb_private::Language::MethodNameVariant::m_name
ConstString m_name
Definition: Language.h:188
FormatClasses.h
lldb_private::HardcodedFormatters::HardcodedFormatFinder
HardcodedFormatterFinders< TypeFormatImpl > HardcodedFormatFinder
Definition: FormatClasses.h:39
lldb_private::Language::TypeScavenger::Find
size_t Find(ExecutionContextScope *exe_scope, const char *key, ResultSet &results, bool append=true)
Definition: Language.cpp:376
lldb_private::PluginInterface
Definition: PluginInterface.h:16
lldb_private::Language::TypeScavenger::Result
Definition: Language.h:33
lldb_private::Language::ImageListTypeScavenger::Find_Impl
bool Find_Impl(ExecutionContextScope *exe_scope, const char *key, ResultSet &results) override
Definition: Language.cpp:395
lldb_private::Language::GetFunctionDisplayName
virtual bool GetFunctionDisplayName(const SymbolContext *sc, const ExecutionContext *exe_ctx, FunctionNameRepresentation representation, Stream &s)
Definition: Language.cpp:443
lldb_private::Language::GetExceptionResolverDescription
virtual void GetExceptionResolverDescription(bool catch_on, bool throw_on, Stream &s)
Definition: Language.cpp:450
lldb::LanguageType
LanguageType
Programming language type.
Definition: lldb-enumerations.h:436
lldb_private::Language::TypeScavenger::ResultSet
std::set< std::unique_ptr< Result > > ResultSet
Definition: Language.h:43
lldb_private::Language::UnionTypeScavenger::m_scavengers
std::vector< std::shared_ptr< TypeScavenger > > m_scavengers
Definition: Language.h:139
lldb_private::Language::ImageListTypeScavenger::Result
Definition: Language.h:58
lldb_private::Language::TypeScavenger::Result::DumpToStream
virtual bool DumpToStream(Stream &stream, bool print_help_if_available)=0
lldb_private::Stream
Definition: Stream.h:28
lldb_private::LazyBool
LazyBool
Definition: lldb-private-enumerations.h:115
lldb_private::SymbolContext
Definition: SymbolContext.h:33
lldb_private::Language::GetMethodNameVariants
virtual std::vector< Language::MethodNameVariant > GetMethodNameVariants(ConstString method_name) const
Definition: Language.h:201
lldb_private::Language::GetHardcodedFormats
virtual HardcodedFormatters::HardcodedFormatFinder GetHardcodedFormats()
Definition: Language.cpp:134
lldb_private::Language::LanguageIsCPlusPlus
static bool LanguageIsCPlusPlus(lldb::LanguageType language)
Definition: Language.cpp:239
lldb_private::Language::LanguageIsCFamily
static bool LanguageIsCFamily(lldb::LanguageType language)
Equivalent to LanguageIsC||LanguageIsObjC||LanguageIsCPlusPlus.
Definition: Language.cpp:274
lldb_private::Language::ImageListTypeScavenger::~ImageListTypeScavenger
~ImageListTypeScavenger() override=default
lldb_private::Highlighter
Annotates source code with color attributes.
Definition: Highlighter.h:90
lldb_private::Language::TypeScavenger::Result::IsValid
virtual bool IsValid()=0
lldb_private::Language::IsTopLevelFunction
virtual bool IsTopLevelFunction(Function &function)
Definition: Language.cpp:130
lldb_private::HardcodedFormatters::HardcodedSummaryFinder
HardcodedFormatterFinders< TypeSummaryImpl > HardcodedSummaryFinder
Definition: FormatClasses.h:40
lldb_private::Language::MethodNameVariant::GetType
lldb::FunctionNameType GetType() const
Definition: Language.h:195
lldb_private::Language::IsSourceFile
virtual bool IsSourceFile(llvm::StringRef file_path) const =0
lldb_private::Language::GetLanguageSpecificTypeLookupHelp
virtual const char * GetLanguageSpecificTypeLookupHelp()
Definition: Language.cpp:374
lldb_private::Language::PrintAllLanguages
static void PrintAllLanguages(Stream &s, const char *prefix, const char *suffix)
Definition: Language.cpp:224
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::Language::IsUninitializedReference
virtual bool IsUninitializedReference(ValueObject &valobj)
Definition: Language.cpp:441
lldb_private::Function
Definition: Function.h:413
lldb_private::Language::EitherTypeScavenger
Definition: Language.h:96
lldb_private::Language::GetLanguagesSupportingTypeSystems
static LanguageSet GetLanguagesSupportingTypeSystems()
Definition: Language.cpp:358
lldb_private::Language::LanguageIsPascal
static bool LanguageIsPascal(lldb::LanguageType language)
Definition: Language.cpp:292
lldb_private::ExecutionContextScope
Definition: ExecutionContextScope.h:32
lldb_private::Language::LanguageIsC
static bool LanguageIsC(lldb::LanguageType language)
Definition: Language.cpp:262
lldb_private::Language::TypeScavenger
Definition: Language.h:31
lldb_private::Language::EitherTypeScavenger::EitherTypeScavenger
EitherTypeScavenger()
Definition: Language.h:98
lldb_private::Language::FunctionNameRepresentation
FunctionNameRepresentation
Definition: Language.h:142
lldb_private::Language::ImageListTypeScavenger::Result::Result
Result(CompilerType type)
Definition: Language.h:60
lldb_private::Language::ImageListTypeScavenger::Result::m_compiler_type
CompilerType m_compiler_type
Definition: Language.h:77
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::Language::LanguageIsObjC
static bool LanguageIsObjC(lldb::LanguageType language)
Definition: Language.cpp:252
lldb_private::Language::FunctionNameRepresentation::eName
@ eName
lldb_private::Language::IsNilReference
virtual bool IsNilReference(ValueObject &valobj)
Definition: Language.cpp:439
PluginInterface.h
lldb_private::Language::GetTypeScavenger
virtual std::unique_ptr< TypeScavenger > GetTypeScavenger()
Definition: Language.cpp:370
lldb_private::Language::SymbolNameFitsToLanguage
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:210
lldb_private::Language::UnionTypeScavenger::UnionTypeScavenger
UnionTypeScavenger()
Definition: Language.h:121
Highlighter.h
lldb_private::Language::MethodNameVariant::m_type
lldb::FunctionNameType m_type
Definition: Language.h:189
lldb::DynamicValueType
DynamicValueType
Definition: lldb-enumerations.h:494
lldb_private::Language::GetHardcodedSynthetics
virtual HardcodedFormatters::HardcodedSyntheticFinder GetHardcodedSynthetics()
Definition: Language.cpp:143
lldb_private::Language::TypeScavenger::~TypeScavenger
virtual ~TypeScavenger()=default
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::Language::ForAllLanguages
static void ForAllLanguages(std::function< bool(lldb::LanguageType)> callback)
Definition: Language.cpp:231
TypeSystem.h
lldb_private::Language::~Language
~Language() override
lldb_private::CompilerType::IsValid
bool IsValid() const
Definition: CompilerType.h:72
lldb_private::Language::FindPlugin
static Language * FindPlugin(lldb::LanguageType language)
Definition: Language.cpp:53
lldb_private::Language::GenerateAlternateFunctionManglings
virtual std::vector< ConstString > GenerateAlternateFunctionManglings(const ConstString mangled) const
Definition: Language.h:299
lldb-private.h
lldb_private::Language::GetLanguageType
virtual lldb::LanguageType GetLanguageType() const =0
lldb_private::Language::GetLanguagesSupportingREPLs
static LanguageSet GetLanguagesSupportingREPLs()
Definition: Language.cpp:366
lldb_private::Language::TypeScavenger::Find_Impl
virtual bool Find_Impl(ExecutionContextScope *exe_scope, const char *key, ResultSet &results)=0
lldb_private::Language::GetLanguageTypeFromString
static lldb::LanguageType GetLanguageTypeFromString(const char *string)=delete
lldb_private::Language::GetDefaultExceptionResolverDescription
static void GetDefaultExceptionResolverDescription(bool catch_on, bool throw_on, Stream &s)
Definition: Language.cpp:455
lldb_private::Language::GetFormatterPrefixSuffix
virtual bool GetFormatterPrefixSuffix(ValueObject &valobj, ConstString type_hint, std::string &prefix, std::string &suffix)
Definition: Language.cpp:424
lldb_private::Status
Definition: Status.h:44
lldb_private::ValueObject
ValueObject:
Definition: ValueObject.h:105
lldb_private::Language::FindBestAlternateFunctionMangledName
virtual ConstString FindBestAlternateFunctionMangledName(const Mangled mangled, const SymbolContext &sym_ctx) const
Definition: Language.h:304
lldb_private::Language::GetLanguagesSupportingTypeSystemsForExpressions
static LanguageSet GetLanguagesSupportingTypeSystemsForExpressions()
Definition: Language.cpp:362
lldb_private::Language::MethodNameVariant::GetName
ConstString GetName() const
Definition: Language.h:194
lldb_private::Language::GetDeclPrintingHelper
virtual DumpValueObjectOptions::DeclPrintingHelper GetDeclPrintingHelper()
Definition: Language.cpp:431
lldb_private::Language
Definition: Language.h:29
DumpValueObjectOptions.h
lldb_private::Stream::EOL
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:128
lldb_private::Language::GetFormatters
virtual lldb::TypeCategoryImplSP GetFormatters()
Definition: Language.cpp:132
lldb_private::Language::IsLogicalTrue
virtual LazyBool IsLogicalTrue(ValueObject &valobj, Status &error)
Definition: Language.cpp:435
lldb_private::Language::GetNilReferenceSummaryString
virtual llvm::StringRef GetNilReferenceSummaryString()
Returns the summary string for ValueObjects for which IsNilReference() is true.
Definition: Language.h:233
lldb_private::Language::FunctionNameRepresentation::eNameWithArgs
@ eNameWithArgs
lldb_private::Language::GetPossibleFormattersMatches
virtual std::vector< ConstString > GetPossibleFormattersMatches(ValueObject &valobj, lldb::DynamicValueType use_dynamic)
Definition: Language.cpp:148
lldb_private::CompilerType
Generic representation of a type in a programming language.
Definition: CompilerType.h:33
lldb_private::Language::ImageListTypeScavenger::ImageListTypeScavenger
ImageListTypeScavenger()=default
lldb_private::Language::GetSupportedLanguages
static std::set< lldb::LanguageType > GetSupportedLanguages()
Definition: Language.cpp:349
lldb_private::Language::TypeScavenger::Result::~Result
virtual ~Result()=default
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::Language::GetHighlighter
virtual const Highlighter * GetHighlighter() const
Definition: Language.h:168
lldb_private::Language::UnionTypeScavenger::Find_Impl
bool Find_Impl(ExecutionContextScope *exe_scope, const char *key, ResultSet &results) override
Definition: Language.h:128
lldb_private::Language::ImageListTypeScavenger
Definition: Language.h:57
lldb_private::Language::MethodNameVariant
Definition: Language.h:187
lldb_private::Language::EitherTypeScavenger::Find_Impl
bool Find_Impl(ExecutionContextScope *exe_scope, const char *key, ResultSet &results) override
Definition: Language.h:105
StringPrinter.h
lldb_private::LanguageSet
A SmallBitVector that represents a set of source languages (lldb::LanguageType).
Definition: TypeSystem.h:40
lldb_private::Language::ForEach
static void ForEach(std::function< bool(Language *)> callback)
Definition: Language.cpp:100
lldb_private::Language::GetNameForLanguageType
static const char * GetNameForLanguageType(lldb::LanguageType language)
Definition: Language.cpp:217
lldb_private::Language::UnionTypeScavenger
Definition: Language.h:119
lldb_private::CompilerType::DumpTypeDescription
void DumpTypeDescription(lldb::DescriptionLevel level=lldb::eDescriptionLevelFull) const
Dump to stdout.
Definition: CompilerType.cpp:759
lldb_private::Language::TypeScavenger::TypeScavenger
TypeScavenger()=default
lldb_private::Language::MethodNameVariant::MethodNameVariant
MethodNameVariant(ConstString name, lldb::FunctionNameType type)
Definition: Language.h:192
lldb_private::Language::ImageListTypeScavenger::Result::~Result
~Result() override=default
lldb_private::Language::EitherTypeScavenger::m_scavengers
std::vector< std::shared_ptr< TypeScavenger > > m_scavengers
Definition: Language.h:115
lldb_private::Language::operator=
const Language & operator=(const Language &)=delete
lldb_private::Language::GetHardcodedSummaries
virtual HardcodedFormatters::HardcodedSummaryFinder GetHardcodedSummaries()
Definition: Language.cpp:138
lldb_private::HardcodedFormatters::HardcodedSyntheticFinder
HardcodedFormatterFinders< SyntheticChildren > HardcodedSyntheticFinder
Definition: FormatClasses.h:41
lldb_private::Language::GetPrimaryLanguage
static lldb::LanguageType GetPrimaryLanguage(lldb::LanguageType language)
Definition: Language.cpp:301