1 //===-- Language.h ---------------------------------------------------*- C++
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
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_Language_h_
11 #define liblldb_Language_h_
18 #include "lldb/Core/Highlighter.h"
19 #include "lldb/Core/PluginInterface.h"
20 #include "lldb/DataFormatters/DumpValueObjectOptions.h"
21 #include "lldb/DataFormatters/FormatClasses.h"
22 #include "lldb/DataFormatters/StringPrinter.h"
23 #include "lldb/lldb-private.h"
24 #include "lldb/lldb-public.h"
26 namespace lldb_private {
28 class Language : public PluginInterface {
34 virtual bool IsValid() = 0;
36 virtual bool DumpToStream(Stream &stream,
37 bool print_help_if_available) = 0;
39 virtual ~Result() = default;
42 typedef std::set<std::unique_ptr<Result>> ResultSet;
44 virtual ~TypeScavenger() = default;
46 size_t Find(ExecutionContextScope *exe_scope, const char *key,
47 ResultSet &results, bool append = true);
50 TypeScavenger() = default;
52 virtual bool Find_Impl(ExecutionContextScope *exe_scope, const char *key,
53 ResultSet &results) = 0;
56 class ImageListTypeScavenger : public TypeScavenger {
57 class Result : public Language::TypeScavenger::Result {
59 Result(CompilerType type)
60 : Language::TypeScavenger::Result(), m_compiler_type(type) {}
62 bool IsValid() override { return m_compiler_type.IsValid(); }
64 bool DumpToStream(Stream &stream, bool print_help_if_available) override {
66 m_compiler_type.DumpTypeDescription(&stream);
73 ~Result() override = default;
76 CompilerType m_compiler_type;
80 ImageListTypeScavenger() = default;
82 ~ImageListTypeScavenger() override = default;
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;
90 bool Find_Impl(ExecutionContextScope *exe_scope, const char *key,
91 ResultSet &results) override;
94 template <typename... ScavengerTypes>
95 class EitherTypeScavenger : public TypeScavenger {
97 EitherTypeScavenger() : TypeScavenger(), m_scavengers() {
98 for (std::shared_ptr<TypeScavenger> scavenger : { std::shared_ptr<TypeScavenger>(new ScavengerTypes())... }) {
100 m_scavengers.push_back(scavenger);
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))
114 std::vector<std::shared_ptr<TypeScavenger>> m_scavengers;
117 template <typename... ScavengerTypes>
118 class UnionTypeScavenger : public TypeScavenger {
120 UnionTypeScavenger() : TypeScavenger(), m_scavengers() {
121 for (std::shared_ptr<TypeScavenger> scavenger : { std::shared_ptr<TypeScavenger>(new ScavengerTypes())... }) {
123 m_scavengers.push_back(scavenger);
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) {
133 success = scavenger->Find(exe_scope, key, results, append) || success;
138 std::vector<std::shared_ptr<TypeScavenger>> m_scavengers;
141 enum class FunctionNameRepresentation {
147 ~Language() override;
149 static Language *FindPlugin(lldb::LanguageType language);
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);
155 static Language *FindPlugin(lldb::LanguageType language,
156 llvm::StringRef file_path);
158 // return false from callback to stop iterating
159 static void ForEach(std::function<bool(Language *)> callback);
161 virtual lldb::LanguageType GetLanguageType() const = 0;
163 virtual bool IsTopLevelFunction(Function &function);
165 virtual bool IsSourceFile(llvm::StringRef file_path) const = 0;
167 virtual const Highlighter *GetHighlighter() const { return nullptr; }
169 virtual lldb::TypeCategoryImplSP GetFormatters();
171 virtual HardcodedFormatters::HardcodedFormatFinder GetHardcodedFormats();
173 virtual HardcodedFormatters::HardcodedSummaryFinder GetHardcodedSummaries();
175 virtual HardcodedFormatters::HardcodedSyntheticFinder
176 GetHardcodedSynthetics();
178 virtual HardcodedFormatters::HardcodedValidatorFinder
179 GetHardcodedValidators();
181 virtual std::vector<ConstString>
182 GetPossibleFormattersMatches(ValueObject &valobj,
183 lldb::DynamicValueType use_dynamic);
185 virtual lldb_private::formatters::StringPrinter::EscapingHelper
186 GetStringPrinterEscapingHelper(
187 lldb_private::formatters::StringPrinter::GetPrintableElementType);
189 virtual std::unique_ptr<TypeScavenger> GetTypeScavenger();
191 virtual const char *GetLanguageSpecificTypeLookupHelp();
193 // If a language can have more than one possible name for a method, this
194 // function can be used to enumerate them. This is useful when doing name
196 virtual std::vector<ConstString>
197 GetMethodNameVariants(ConstString method_name) const {
198 return std::vector<ConstString>();
201 // if an individual data formatter can apply to several types and cross a
202 // language boundary it makes sense for individual languages to want to
203 // customize the printing of values of that type by appending proper
204 // prefix/suffix information in language-specific ways
205 virtual bool GetFormatterPrefixSuffix(ValueObject &valobj,
206 ConstString type_hint,
208 std::string &suffix);
210 // if a language has a custom format for printing variable declarations that
211 // it wants LLDB to honor it should return an appropriate closure here
212 virtual DumpValueObjectOptions::DeclPrintingHelper GetDeclPrintingHelper();
214 virtual LazyBool IsLogicalTrue(ValueObject &valobj, Status &error);
216 // for a ValueObject of some "reference type", if the value points to the
217 // nil/null object, this method returns true
218 virtual bool IsNilReference(ValueObject &valobj);
220 // for a ValueObject of some "reference type", if the language provides a
221 // technique to decide whether the reference has ever been assigned to some
222 // object, this method will return true if such detection is possible, and if
223 // the reference has never been assigned
224 virtual bool IsUninitializedReference(ValueObject &valobj);
226 virtual bool GetFunctionDisplayName(const SymbolContext *sc,
227 const ExecutionContext *exe_ctx,
228 FunctionNameRepresentation representation,
231 virtual void GetExceptionResolverDescription(bool catch_on, bool throw_on,
234 static void GetDefaultExceptionResolverDescription(bool catch_on,
235 bool throw_on, Stream &s);
237 // These are accessors for general information about the Languages lldb knows
240 static lldb::LanguageType
241 GetLanguageTypeFromString(const char *string) = delete;
242 static lldb::LanguageType GetLanguageTypeFromString(llvm::StringRef string);
244 static const char *GetNameForLanguageType(lldb::LanguageType language);
246 static void PrintAllLanguages(Stream &s, const char *prefix,
249 // return false from callback to stop iterating
250 static void ForAllLanguages(std::function<bool(lldb::LanguageType)> callback);
252 static bool LanguageIsCPlusPlus(lldb::LanguageType language);
254 static bool LanguageIsObjC(lldb::LanguageType language);
256 static bool LanguageIsC(lldb::LanguageType language);
258 /// Equivalent to \c LanguageIsC||LanguageIsObjC||LanguageIsCPlusPlus.
259 static bool LanguageIsCFamily(lldb::LanguageType language);
261 static bool LanguageIsPascal(lldb::LanguageType language);
263 // return the primary language, so if LanguageIsC(l), return eLanguageTypeC,
265 static lldb::LanguageType GetPrimaryLanguage(lldb::LanguageType language);
267 static std::set<lldb::LanguageType> GetSupportedLanguages();
269 static void GetLanguagesSupportingTypeSystems(
270 std::set<lldb::LanguageType> &languages,
271 std::set<lldb::LanguageType> &languages_for_expressions);
274 GetLanguagesSupportingREPLs(std::set<lldb::LanguageType> &languages);
277 // Classes that inherit from Language can see and modify these
282 DISALLOW_COPY_AND_ASSIGN(Language);
285 } // namespace lldb_private
287 #endif // liblldb_Language_h_