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