1 //===-- ObjCLanguage.h ------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_SOURCE_PLUGINS_LANGUAGE_OBJC_OBJCLANGUAGE_H
10 #define LLDB_SOURCE_PLUGINS_LANGUAGE_OBJC_OBJCLANGUAGE_H
11 
12 #include <cstring>
13 #include <vector>
14 
15 #include "Plugins/Language/ClangCommon/ClangHighlighter.h"
16 #include "lldb/Target/Language.h"
17 #include "lldb/Utility/ConstString.h"
18 #include "lldb/lldb-private.h"
19 
20 namespace lldb_private {
21 
22 class ObjCLanguage : public Language {
23   ClangHighlighter m_highlighter;
24 
25 public:
26   class MethodName {
27   public:
28     enum Type { eTypeUnspecified, eTypeClassMethod, eTypeInstanceMethod };
29 
30     MethodName()
31         : m_full(), m_class(), m_category(), m_selector(),
32           m_type(eTypeUnspecified), m_category_is_valid(false) {}
33 
34     MethodName(const char *name, bool strict)
35         : m_full(), m_class(), m_category(), m_selector(),
36           m_type(eTypeUnspecified), m_category_is_valid(false) {
37       SetName(name, strict);
38     }
39     MethodName(llvm::StringRef name, bool strict)
40         : m_full(), m_class(), m_category(), m_selector(),
41           m_type(eTypeUnspecified), m_category_is_valid(false) {
42       SetName(name, strict);
43     }
44 
45     void Clear();
46 
47     bool IsValid(bool strict) const {
48       // If "strict" is true, the name must have everything specified including
49       // the leading "+" or "-" on the method name
50       if (strict && m_type == eTypeUnspecified)
51         return false;
52       // Other than that, m_full will only be filled in if the objective C
53       // name is valid.
54       return (bool)m_full;
55     }
56 
57     bool HasCategory() { return !GetCategory().IsEmpty(); }
58 
59     Type GetType() const { return m_type; }
60 
61     ConstString GetFullName() const { return m_full; }
62 
63     ConstString GetFullNameWithoutCategory(bool empty_if_no_category);
64 
65     bool SetName(const char *name, bool strict);
66     bool SetName(llvm::StringRef name, bool strict);
67 
68     ConstString GetClassName();
69 
70     ConstString GetClassNameWithCategory();
71 
72     ConstString GetCategory();
73 
74     ConstString GetSelector();
75 
76   protected:
77     ConstString
78         m_full; // Full name:   "+[NSString(my_additions) myStringWithCString:]"
79     ConstString m_class; // Class name:  "NSString"
80     ConstString
81         m_class_category;   // Class with category: "NSString(my_additions)"
82     ConstString m_category; // Category:    "my_additions"
83     ConstString m_selector; // Selector:    "myStringWithCString:"
84     Type m_type;
85     bool m_category_is_valid;
86   };
87 
88   ObjCLanguage() = default;
89 
90   ~ObjCLanguage() override = default;
91 
92   lldb::LanguageType GetLanguageType() const override {
93     return lldb::eLanguageTypeObjC;
94   }
95 
96   // Get all possible names for a method. Examples:
97   // If method_name is "+[NSString(my_additions) myStringWithCString:]"
98   //   variant_names[0] => "+[NSString myStringWithCString:]"
99   // If name is specified without the leading '+' or '-' like
100   // "[NSString(my_additions) myStringWithCString:]"
101   //  variant_names[0] => "+[NSString(my_additions) myStringWithCString:]"
102   //  variant_names[1] => "-[NSString(my_additions) myStringWithCString:]"
103   //  variant_names[2] => "+[NSString myStringWithCString:]"
104   //  variant_names[3] => "-[NSString myStringWithCString:]"
105   std::vector<ConstString>
106   GetMethodNameVariants(ConstString method_name) const override;
107 
108   lldb::TypeCategoryImplSP GetFormatters() override;
109 
110   std::vector<ConstString>
111   GetPossibleFormattersMatches(ValueObject &valobj,
112                                lldb::DynamicValueType use_dynamic) override;
113 
114   std::unique_ptr<TypeScavenger> GetTypeScavenger() override;
115 
116   bool GetFormatterPrefixSuffix(ValueObject &valobj, ConstString type_hint,
117                                 std::string &prefix,
118                                 std::string &suffix) override;
119 
120   bool IsNilReference(ValueObject &valobj) override;
121 
122   llvm::StringRef GetNilReferenceSummaryString() override { return "nil"; }
123 
124   bool IsSourceFile(llvm::StringRef file_path) const override;
125 
126   const Highlighter *GetHighlighter() const override { return &m_highlighter; }
127 
128   // Static Functions
129   static void Initialize();
130 
131   static void Terminate();
132 
133   static lldb_private::Language *CreateInstance(lldb::LanguageType language);
134 
135   static lldb_private::ConstString GetPluginNameStatic();
136 
137   static bool IsPossibleObjCMethodName(const char *name) {
138     if (!name)
139       return false;
140     bool starts_right = (name[0] == '+' || name[0] == '-') && name[1] == '[';
141     bool ends_right = (name[strlen(name) - 1] == ']');
142     return (starts_right && ends_right);
143   }
144 
145   static bool IsPossibleObjCSelector(const char *name) {
146     if (!name)
147       return false;
148 
149     if (strchr(name, ':') == nullptr)
150       return true;
151     else if (name[strlen(name) - 1] == ':')
152       return true;
153     else
154       return false;
155   }
156 
157   // PluginInterface protocol
158   ConstString GetPluginName() override;
159 
160   uint32_t GetPluginVersion() override;
161 };
162 
163 } // namespace lldb_private
164 
165 #endif // LLDB_SOURCE_PLUGINS_LANGUAGE_OBJC_OBJCLANGUAGE_H
166