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 liblldb_ObjCLanguage_h_
10 #define liblldb_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   bool IsSourceFile(llvm::StringRef file_path) const override;
123 
124   const Highlighter *GetHighlighter() const override { return &m_highlighter; }
125 
126   // Static Functions
127   static void Initialize();
128 
129   static void Terminate();
130 
131   static lldb_private::Language *CreateInstance(lldb::LanguageType language);
132 
133   static lldb_private::ConstString GetPluginNameStatic();
134 
135   static bool IsPossibleObjCMethodName(const char *name) {
136     if (!name)
137       return false;
138     bool starts_right = (name[0] == '+' || name[0] == '-') && name[1] == '[';
139     bool ends_right = (name[strlen(name) - 1] == ']');
140     return (starts_right && ends_right);
141   }
142 
143   static bool IsPossibleObjCSelector(const char *name) {
144     if (!name)
145       return false;
146 
147     if (strchr(name, ':') == nullptr)
148       return true;
149     else if (name[strlen(name) - 1] == ':')
150       return true;
151     else
152       return false;
153   }
154 
155   // PluginInterface protocol
156   ConstString GetPluginName() override;
157 
158   uint32_t GetPluginVersion() override;
159 };
160 
161 } // namespace lldb_private
162 
163 #endif // liblldb_ObjCLanguage_h_
164