1 //===-- Language.h ---------------------------------------------------*- C++
4 // The LLVM Compiler Infrastructure
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef liblldb_Language_h_
12 #define liblldb_Language_h_
19 #include "lldb/Core/Highlighter.h"
20 #include "lldb/Core/PluginInterface.h"
21 #include "lldb/DataFormatters/DumpValueObjectOptions.h"
22 #include "lldb/DataFormatters/FormatClasses.h"
23 #include "lldb/DataFormatters/StringPrinter.h"
24 #include "lldb/lldb-private.h"
25 #include "lldb/lldb-public.h"
27 namespace lldb_private {
29 class Language : public PluginInterface {
35 virtual bool IsValid() = 0;
37 virtual bool DumpToStream(Stream &stream,
38 bool print_help_if_available) = 0;
40 virtual ~Result() = default;
43 typedef std::set<std::unique_ptr<Result>> ResultSet;
45 virtual ~TypeScavenger() = default;
47 size_t Find(ExecutionContextScope *exe_scope, const char *key,
48 ResultSet &results, bool append = true);
51 TypeScavenger() = default;
53 virtual bool Find_Impl(ExecutionContextScope *exe_scope, const char *key,
54 ResultSet &results) = 0;
57 class ImageListTypeScavenger : public TypeScavenger {
58 class Result : public Language::TypeScavenger::Result {
60 Result(CompilerType type)
61 : Language::TypeScavenger::Result(), m_compiler_type(type) {}
63 bool IsValid() override { return m_compiler_type.IsValid(); }
65 bool DumpToStream(Stream &stream, bool print_help_if_available) override {
67 m_compiler_type.DumpTypeDescription(&stream);
74 ~Result() override = default;
77 CompilerType m_compiler_type;
81 ImageListTypeScavenger() = default;
83 ~ImageListTypeScavenger() override = default;
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;
91 bool Find_Impl(ExecutionContextScope *exe_scope, const char *key,
92 ResultSet &results) override;
95 template <typename... ScavengerTypes>
96 class EitherTypeScavenger : public TypeScavenger {
98 EitherTypeScavenger() : TypeScavenger(), m_scavengers() {
99 for (std::shared_ptr<TypeScavenger> scavenger : { std::shared_ptr<TypeScavenger>(new ScavengerTypes())... }) {
101 m_scavengers.push_back(scavenger);
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))
115 std::vector<std::shared_ptr<TypeScavenger>> m_scavengers;
118 template <typename... ScavengerTypes>
119 class UnionTypeScavenger : public TypeScavenger {
121 UnionTypeScavenger() : TypeScavenger(), m_scavengers() {
122 for (std::shared_ptr<TypeScavenger> scavenger : { std::shared_ptr<TypeScavenger>(new ScavengerTypes())... }) {
124 m_scavengers.push_back(scavenger);
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) {
134 success = scavenger->Find(exe_scope, key, results, append) || success;
139 std::vector<std::shared_ptr<TypeScavenger>> m_scavengers;
142 enum class FunctionNameRepresentation {
148 ~Language() override;
150 static Language *FindPlugin(lldb::LanguageType language);
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);
156 static Language *FindPlugin(lldb::LanguageType language,
157 llvm::StringRef file_path);
159 // return false from callback to stop iterating
160 static void ForEach(std::function<bool(Language *)> callback);
162 virtual lldb::LanguageType GetLanguageType() const = 0;
164 virtual bool IsTopLevelFunction(Function &function);
166 virtual bool IsSourceFile(llvm::StringRef file_path) const = 0;
168 virtual const Highlighter *GetHighlighter() const { return nullptr; }
170 virtual lldb::TypeCategoryImplSP GetFormatters();
172 virtual HardcodedFormatters::HardcodedFormatFinder GetHardcodedFormats();
174 virtual HardcodedFormatters::HardcodedSummaryFinder GetHardcodedSummaries();
176 virtual HardcodedFormatters::HardcodedSyntheticFinder
177 GetHardcodedSynthetics();
179 virtual HardcodedFormatters::HardcodedValidatorFinder
180 GetHardcodedValidators();
182 virtual std::vector<ConstString>
183 GetPossibleFormattersMatches(ValueObject &valobj,
184 lldb::DynamicValueType use_dynamic);
186 virtual lldb_private::formatters::StringPrinter::EscapingHelper
187 GetStringPrinterEscapingHelper(
188 lldb_private::formatters::StringPrinter::GetPrintableElementType);
190 virtual std::unique_ptr<TypeScavenger> GetTypeScavenger();
192 virtual const char *GetLanguageSpecificTypeLookupHelp();
194 // if an individual data formatter can apply to several types and cross a
195 // language boundary it makes sense for individual languages to want to
196 // customize the printing of values of that type by appending proper
197 // prefix/suffix information in language-specific ways
198 virtual bool GetFormatterPrefixSuffix(ValueObject &valobj,
199 ConstString type_hint,
201 std::string &suffix);
203 // if a language has a custom format for printing variable declarations that
204 // it wants LLDB to honor it should return an appropriate closure here
205 virtual DumpValueObjectOptions::DeclPrintingHelper GetDeclPrintingHelper();
207 virtual LazyBool IsLogicalTrue(ValueObject &valobj, Status &error);
209 // for a ValueObject of some "reference type", if the value points to the
210 // nil/null object, this method returns true
211 virtual bool IsNilReference(ValueObject &valobj);
213 // for a ValueObject of some "reference type", if the language provides a
214 // technique to decide whether the reference has ever been assigned to some
215 // object, this method will return true if such detection is possible, and if
216 // the reference has never been assigned
217 virtual bool IsUninitializedReference(ValueObject &valobj);
219 virtual bool GetFunctionDisplayName(const SymbolContext *sc,
220 const ExecutionContext *exe_ctx,
221 FunctionNameRepresentation representation,
224 virtual void GetExceptionResolverDescription(bool catch_on, bool throw_on,
227 static void GetDefaultExceptionResolverDescription(bool catch_on,
228 bool throw_on, Stream &s);
230 // These are accessors for general information about the Languages lldb knows
233 static lldb::LanguageType
234 GetLanguageTypeFromString(const char *string) = delete;
235 static lldb::LanguageType GetLanguageTypeFromString(llvm::StringRef string);
237 static const char *GetNameForLanguageType(lldb::LanguageType language);
239 static void PrintAllLanguages(Stream &s, const char *prefix,
242 // return false from callback to stop iterating
243 static void ForAllLanguages(std::function<bool(lldb::LanguageType)> callback);
245 static bool LanguageIsCPlusPlus(lldb::LanguageType language);
247 static bool LanguageIsObjC(lldb::LanguageType language);
249 static bool LanguageIsC(lldb::LanguageType language);
251 static bool LanguageIsPascal(lldb::LanguageType language);
253 // return the primary language, so if LanguageIsC(l), return eLanguageTypeC,
255 static lldb::LanguageType GetPrimaryLanguage(lldb::LanguageType language);
257 static void GetLanguagesSupportingTypeSystems(
258 std::set<lldb::LanguageType> &languages,
259 std::set<lldb::LanguageType> &languages_for_expressions);
262 GetLanguagesSupportingREPLs(std::set<lldb::LanguageType> &languages);
265 //------------------------------------------------------------------
266 // Classes that inherit from Language can see and modify these
267 //------------------------------------------------------------------
272 DISALLOW_COPY_AND_ASSIGN(Language);
275 } // namespace lldb_private
277 #endif // liblldb_Language_h_