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 liblldb_Language_h_
11 #define liblldb_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/lldb-private.h"
24 #include "lldb/lldb-public.h"
25 
26 namespace lldb_private {
27 
28 class Language : public PluginInterface {
29 public:
30  class TypeScavenger {
31  public:
32  class Result {
33  public:
34  virtual bool IsValid() = 0;
35 
36  virtual bool DumpToStream(Stream &stream,
37  bool print_help_if_available) = 0;
38 
39  virtual ~Result() = default;
40  };
41 
42  typedef std::set<std::unique_ptr<Result>> ResultSet;
43 
44  virtual ~TypeScavenger() = default;
45 
46  size_t Find(ExecutionContextScope *exe_scope, const char *key,
47  ResultSet &results, bool append = true);
48 
49  protected:
50  TypeScavenger() = default;
51 
52  virtual bool Find_Impl(ExecutionContextScope *exe_scope, const char *key,
53  ResultSet &results) = 0;
54  };
55 
57  class Result : public Language::TypeScavenger::Result {
58  public:
59  Result(CompilerType type)
60  : Language::TypeScavenger::Result(), m_compiler_type(type) {}
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()) {
66  m_compiler_type.DumpTypeDescription(&stream);
67  stream.EOL();
68  return true;
69  }
70  return false;
71  }
72 
73  ~Result() override = default;
74 
75  private:
76  CompilerType m_compiler_type;
77  };
78 
79  protected:
80  ImageListTypeScavenger() = default;
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
88  virtual CompilerType AdjustForInclusion(CompilerType &candidate) = 0;
89 
90  bool Find_Impl(ExecutionContextScope *exe_scope, const char *key,
91  ResultSet &results) override;
92  };
93 
94  template <typename... ScavengerTypes>
96  public:
97  EitherTypeScavenger() : TypeScavenger(), m_scavengers() {
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:
120  UnionTypeScavenger() : TypeScavenger(), m_scavengers() {
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,
143  eNameWithArgs,
144  eNameWithNoArgs
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 
161  virtual lldb::LanguageType GetLanguageType() const = 0;
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 
180 
181  virtual std::vector<ConstString>
183  lldb::DynamicValueType use_dynamic);
184 
188 
189  virtual std::unique_ptr<TypeScavenger> GetTypeScavenger();
190 
191  virtual const char *GetLanguageSpecificTypeLookupHelp();
192 
193  // if an individual data formatter can apply to several types and cross a
194  // language boundary it makes sense for individual languages to want to
195  // customize the printing of values of that type by appending proper
196  // prefix/suffix information in language-specific ways
197  virtual bool GetFormatterPrefixSuffix(ValueObject &valobj,
198  ConstString type_hint,
199  std::string &prefix,
200  std::string &suffix);
201 
202  // if a language has a custom format for printing variable declarations that
203  // it wants LLDB to honor it should return an appropriate closure here
205 
206  virtual LazyBool IsLogicalTrue(ValueObject &valobj, Status &error);
207 
208  // for a ValueObject of some "reference type", if the value points to the
209  // nil/null object, this method returns true
210  virtual bool IsNilReference(ValueObject &valobj);
211 
212  // for a ValueObject of some "reference type", if the language provides a
213  // technique to decide whether the reference has ever been assigned to some
214  // object, this method will return true if such detection is possible, and if
215  // the reference has never been assigned
216  virtual bool IsUninitializedReference(ValueObject &valobj);
217 
218  virtual bool GetFunctionDisplayName(const SymbolContext *sc,
219  const ExecutionContext *exe_ctx,
220  FunctionNameRepresentation representation,
221  Stream &s);
222 
223  virtual void GetExceptionResolverDescription(bool catch_on, bool throw_on,
224  Stream &s);
225 
226  static void GetDefaultExceptionResolverDescription(bool catch_on,
227  bool throw_on, Stream &s);
228 
229  // These are accessors for general information about the Languages lldb knows
230  // about:
231 
232  static lldb::LanguageType
233  GetLanguageTypeFromString(const char *string) = delete;
234  static lldb::LanguageType GetLanguageTypeFromString(llvm::StringRef string);
235 
236  static const char *GetNameForLanguageType(lldb::LanguageType language);
237 
238  static void PrintAllLanguages(Stream &s, const char *prefix,
239  const char *suffix);
240 
241  // return false from callback to stop iterating
242  static void ForAllLanguages(std::function<bool(lldb::LanguageType)> callback);
243 
244  static bool LanguageIsCPlusPlus(lldb::LanguageType language);
245 
246  static bool LanguageIsObjC(lldb::LanguageType language);
247 
248  static bool LanguageIsC(lldb::LanguageType language);
249 
250  static bool LanguageIsPascal(lldb::LanguageType language);
251 
252  // return the primary language, so if LanguageIsC(l), return eLanguageTypeC,
253  // etc.
255 
257  std::set<lldb::LanguageType> &languages,
258  std::set<lldb::LanguageType> &languages_for_expressions);
259 
260  static void
261  GetLanguagesSupportingREPLs(std::set<lldb::LanguageType> &languages);
262 
263 protected:
264  // Classes that inherit from Language can see and modify these
265 
266  Language();
267 
268 private:
269  DISALLOW_COPY_AND_ASSIGN(Language);
270 };
271 
272 } // namespace lldb_private
273 
274 #endif // liblldb_Language_h_
virtual bool GetFunctionDisplayName(const SymbolContext *sc, const ExecutionContext *exe_ctx, FunctionNameRepresentation representation, Stream &s)
Definition: Language.cpp:428
virtual HardcodedFormatters::HardcodedValidatorFinder GetHardcodedValidators()
Definition: Language.cpp:138
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
virtual HardcodedFormatters::HardcodedSyntheticFinder GetHardcodedSynthetics()
Definition: Language.cpp:133
static lldb::LanguageType GetLanguageTypeFromString(const char *string)=delete
HardcodedFormatterFinders< SyntheticChildren > HardcodedSyntheticFinder
Definition: FormatClasses.h:42
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
virtual DumpValueObjectOptions::DeclPrintingHelper GetDeclPrintingHelper()
Definition: Language.cpp:416
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:33
static bool LanguageIsObjC(lldb::LanguageType language)
Definition: Language.cpp:254
virtual LazyBool IsLogicalTrue(ValueObject &valobj, Status &error)
Definition: Language.cpp:420
virtual bool IsSourceFile(llvm::StringRef file_path) const =0
virtual void GetExceptionResolverDescription(bool catch_on, bool throw_on, Stream &s)
Definition: Language.cpp:435
virtual bool DumpToStream(Stream &stream, bool print_help_if_available)=0
static void ForEach(std::function< bool(Language *)> callback)
Definition: Language.cpp:101
virtual lldb_private::formatters::StringPrinter::EscapingHelper GetStringPrinterEscapingHelper(lldb_private::formatters::StringPrinter::GetPrintableElementType)
Definition: Language.cpp:149
A class that describes a function.
Definition: Function.h:323
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
size_t Find(ExecutionContextScope *exe_scope, const char *key, ResultSet &results, bool append=true)
Definition: Language.cpp:361
static void PrintAllLanguages(Stream &s, const char *prefix, const char *suffix)
Definition: Language.cpp:226
bool Find_Impl(ExecutionContextScope *exe_scope, const char *key, ResultSet &results) override
Definition: Language.h:104
virtual bool Find_Impl(ExecutionContextScope *exe_scope, const char *key, ResultSet &results)=0
virtual bool IsUninitializedReference(ValueObject &valobj)
Definition: Language.cpp:426
HardcodedFormatterFinders< TypeFormatImpl > HardcodedFormatFinder
Definition: FormatClasses.h:40
HardcodedFormatterFinders< TypeSummaryImpl > HardcodedSummaryFinder
Definition: FormatClasses.h:41
static bool LanguageIsPascal(lldb::LanguageType language)
Definition: Language.cpp:276
static void GetLanguagesSupportingREPLs(std::set< lldb::LanguageType > &languages)
Definition: Language.cpp:344
static void GetDefaultExceptionResolverDescription(bool catch_on, bool throw_on, Stream &s)
Definition: Language.cpp:440
virtual std::unique_ptr< TypeScavenger > GetTypeScavenger()
Definition: Language.cpp:355
virtual lldb::LanguageType GetLanguageType() const =0
virtual const Highlighter * GetHighlighter() const
Definition: Language.h:167
LanguageType
Programming language type.
virtual HardcodedFormatters::HardcodedFormatFinder GetHardcodedFormats()
Definition: Language.cpp:124
virtual const char * GetLanguageSpecificTypeLookupHelp()
Definition: Language.cpp:359
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:127
static void ForAllLanguages(std::function< bool(lldb::LanguageType)> callback)
Definition: Language.cpp:233
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
static lldb::LanguageType GetPrimaryLanguage(lldb::LanguageType language)
Definition: Language.cpp:285
Annotates source code with color attributes.
Definition: Highlighter.h:92
virtual bool IsNilReference(ValueObject &valobj)
Definition: Language.cpp:424
virtual lldb::TypeCategoryImplSP GetFormatters()
Definition: Language.cpp:122
std::set< std::unique_ptr< Result > > ResultSet
Definition: Language.h:42
A uniqued constant string class.
Definition: ConstString.h:38
std::function< StringPrinter::StringPrinterBufferPointer< uint8_t, char, size_t >uint8_t *, uint8_t *, uint8_t *&)> EscapingHelper
static void GetLanguagesSupportingTypeSystems(std::set< lldb::LanguageType > &languages, std::set< lldb::LanguageType > &languages_for_expressions)
Definition: Language.cpp:333
virtual bool IsTopLevelFunction(Function &function)
Definition: Language.cpp:120
virtual std::vector< ConstString > GetPossibleFormattersMatches(ValueObject &valobj, lldb::DynamicValueType use_dynamic)
Definition: Language.cpp:143
static bool LanguageIsCPlusPlus(lldb::LanguageType language)
Definition: Language.cpp:241
bool Find_Impl(ExecutionContextScope *exe_scope, const char *key, ResultSet &results) override
Definition: Language.h:127
static bool LanguageIsC(lldb::LanguageType language)
Definition: Language.cpp:264
HardcodedFormatterFinders< TypeValidatorImpl > HardcodedValidatorFinder
Definition: FormatClasses.h:43
std::function< bool(ConstString, ConstString, const DumpValueObjectOptions &, Stream &)> DeclPrintingHelper
static Language * FindPlugin(lldb::LanguageType language)
Definition: Language.cpp:54
virtual HardcodedFormatters::HardcodedSummaryFinder GetHardcodedSummaries()
Definition: Language.cpp:128
static const char * GetNameForLanguageType(lldb::LanguageType language)
Definition: Language.cpp:219
virtual bool GetFormatterPrefixSuffix(ValueObject &valobj, ConstString type_hint, std::string &prefix, std::string &suffix)
Definition: Language.cpp:409
An error handling class.
Definition: Status.h:44