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 // C Includes
14 // C++ Includes
15 #include <cstring>
16 #include <vector>
17 
18 // Other libraries and framework includes
19 // Project includes
20 #include "lldb/lldb-private.h"
21 #include "lldb/Core/ConstString.h"
22 #include "lldb/Target/Language.h"
23 
24 namespace lldb_private {
25 
26 class ObjCLanguage :
27     public Language
28 {
29 public:
30     class MethodName
31     {
32     public:
33         enum Type
34         {
35             eTypeUnspecified,
36             eTypeClassMethod,
37             eTypeInstanceMethod
38         };
39 
40         MethodName () :
41             m_full(),
42             m_class(),
43             m_category(),
44             m_selector(),
45             m_type (eTypeUnspecified),
46             m_category_is_valid (false)
47         {
48         }
49 
50         MethodName (const char *name, bool strict) :
51             m_full(),
52             m_class(),
53             m_category(),
54             m_selector(),
55             m_type (eTypeUnspecified),
56             m_category_is_valid (false)
57         {
58             SetName (name, strict);
59         }
60 
61         void
62         Clear();
63 
64         bool
65         IsValid (bool strict) const
66         {
67             // If "strict" is true, the name must have everything specified including
68             // the leading "+" or "-" on the method name
69             if (strict && m_type == eTypeUnspecified)
70                 return false;
71             // Other than that, m_full will only be filled in if the objective C
72             // name is valid.
73             return (bool)m_full;
74         }
75 
76         bool
77         HasCategory()
78         {
79             return !GetCategory().IsEmpty();
80         }
81 
82         Type
83         GetType () const
84         {
85             return m_type;
86         }
87 
88         const ConstString &
89         GetFullName () const
90         {
91             return m_full;
92         }
93 
94         ConstString
95         GetFullNameWithoutCategory (bool empty_if_no_category);
96 
97         bool
98         SetName (const char *name, bool strict);
99 
100         const ConstString &
101         GetClassName ();
102 
103         const ConstString &
104         GetClassNameWithCategory ();
105 
106         const ConstString &
107         GetCategory ();
108 
109         const ConstString &
110         GetSelector ();
111 
112         // Get all possible names for a method. Examples:
113         // If name is "+[NSString(my_additions) myStringWithCString:]"
114         //  names[0] => "+[NSString(my_additions) myStringWithCString:]"
115         //  names[1] => "+[NSString myStringWithCString:]"
116         // If name is specified without the leading '+' or '-' like "[NSString(my_additions) myStringWithCString:]"
117         //  names[0] => "+[NSString(my_additions) myStringWithCString:]"
118         //  names[1] => "-[NSString(my_additions) myStringWithCString:]"
119         //  names[2] => "+[NSString myStringWithCString:]"
120         //  names[3] => "-[NSString myStringWithCString:]"
121         size_t
122         GetFullNames (std::vector<ConstString> &names, bool append);
123 
124     protected:
125         ConstString m_full;     // Full name:   "+[NSString(my_additions) myStringWithCString:]"
126         ConstString m_class;    // Class name:  "NSString"
127         ConstString m_class_category; // Class with category: "NSString(my_additions)"
128         ConstString m_category; // Category:    "my_additions"
129         ConstString m_selector; // Selector:    "myStringWithCString:"
130         Type m_type;
131         bool m_category_is_valid;
132     };
133 
134     ObjCLanguage() = default;
135 
136     ~ObjCLanguage() override = default;
137 
138     lldb::LanguageType
139     GetLanguageType () const override
140     {
141         return lldb::eLanguageTypeObjC;
142     }
143 
144     lldb::TypeCategoryImplSP
145     GetFormatters () override;
146 
147     std::vector<ConstString>
148     GetPossibleFormattersMatches (ValueObject& valobj, lldb::DynamicValueType use_dynamic) override;
149 
150     std::unique_ptr<TypeScavenger>
151     GetTypeScavenger () override;
152 
153     bool
154     GetFormatterPrefixSuffix (ValueObject& valobj, ConstString type_hint,
155                               std::string& prefix, std::string& suffix) override;
156 
157     bool
158     IsNilReference (ValueObject& valobj) override;
159 
160     //------------------------------------------------------------------
161     // Static Functions
162     //------------------------------------------------------------------
163     static void
164     Initialize();
165 
166     static void
167     Terminate();
168 
169     static lldb_private::Language *
170     CreateInstance (lldb::LanguageType language);
171 
172     static lldb_private::ConstString
173     GetPluginNameStatic();
174 
175     static bool
176     IsPossibleObjCMethodName (const char *name)
177     {
178         if (!name)
179             return false;
180         bool starts_right = (name[0] == '+' || name[0] == '-') && name[1] == '[';
181         bool ends_right = (name[strlen(name) - 1] == ']');
182         return (starts_right && ends_right);
183     }
184 
185     static bool
186     IsPossibleObjCSelector (const char *name)
187     {
188         if (!name)
189             return false;
190 
191         if (strchr(name, ':') == nullptr)
192             return true;
193         else if (name[strlen(name) - 1] == ':')
194             return true;
195         else
196             return false;
197     }
198 
199     //------------------------------------------------------------------
200     // PluginInterface protocol
201     //------------------------------------------------------------------
202     ConstString
203     GetPluginName() override;
204 
205     uint32_t
206     GetPluginVersion() override;
207 };
208 
209 } // namespace lldb_private
210 
211 #endif // liblldb_ObjCLanguage_h_
212