1 //===-- CPlusPlusLanguage.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_CPlusPlusLanguage_h_
11 #define liblldb_CPlusPlusLanguage_h_
12 
13 // C Includes
14 // C++ Includes
15 #include <set>
16 #include <vector>
17 
18 // Other libraries and framework includes
19 #include "llvm/ADT/StringRef.h"
20 
21 // Project includes
22 #include "lldb/Target/Language.h"
23 #include "lldb/Utility/ConstString.h"
24 #include "lldb/lldb-private.h"
25 
26 namespace lldb_private {
27 
28 class CPlusPlusLanguage : public Language {
29 public:
30   class MethodName {
31   public:
32     MethodName()
33         : m_full(), m_basename(), m_context(), m_arguments(), m_qualifiers(),
34           m_parsed(false), m_parse_error(false) {}
35 
36     MethodName(const ConstString &s)
37         : m_full(s), m_basename(), m_context(), m_arguments(), m_qualifiers(),
38           m_parsed(false), m_parse_error(false) {}
39 
40     void Clear();
41 
42     bool IsValid() {
43       if (!m_parsed)
44         Parse();
45       if (m_parse_error)
46         return false;
47       return (bool)m_full;
48     }
49 
50     const ConstString &GetFullName() const { return m_full; }
51 
52     std::string GetScopeQualifiedName();
53 
54     llvm::StringRef GetBasename();
55 
56     llvm::StringRef GetContext();
57 
58     llvm::StringRef GetArguments();
59 
60     llvm::StringRef GetQualifiers();
61 
62   protected:
63     void Parse();
64     bool TrySimplifiedParse();
65 
66     ConstString m_full; // Full name:
67                         // "lldb::SBTarget::GetBreakpointAtIndex(unsigned int)
68                         // const"
69     llvm::StringRef m_basename;   // Basename:     "GetBreakpointAtIndex"
70     llvm::StringRef m_context;    // Decl context: "lldb::SBTarget"
71     llvm::StringRef m_arguments;  // Arguments:    "(unsigned int)"
72     llvm::StringRef m_qualifiers; // Qualifiers:   "const"
73     bool m_parsed;
74     bool m_parse_error;
75   };
76 
77   CPlusPlusLanguage() = default;
78 
79   ~CPlusPlusLanguage() override = default;
80 
81   lldb::LanguageType GetLanguageType() const override {
82     return lldb::eLanguageTypeC_plus_plus;
83   }
84 
85   std::unique_ptr<TypeScavenger> GetTypeScavenger() override;
86   lldb::TypeCategoryImplSP GetFormatters() override;
87 
88   HardcodedFormatters::HardcodedSummaryFinder GetHardcodedSummaries() override;
89 
90   HardcodedFormatters::HardcodedSyntheticFinder
91   GetHardcodedSynthetics() override;
92 
93   //------------------------------------------------------------------
94   // Static Functions
95   //------------------------------------------------------------------
96   static void Initialize();
97 
98   static void Terminate();
99 
100   static lldb_private::Language *CreateInstance(lldb::LanguageType language);
101 
102   static lldb_private::ConstString GetPluginNameStatic();
103 
104   static bool IsCPPMangledName(const char *name);
105 
106   // Extract C++ context and identifier from a string using heuristic matching
107   // (as opposed to
108   // CPlusPlusLanguage::MethodName which has to have a fully qualified C++ name
109   // with parens and arguments.
110   // If the name is a lone C identifier (e.g. C) or a qualified C identifier
111   // (e.g. A::B::C) it will return true,
112   // and identifier will be the identifier (C and C respectively) and the
113   // context will be "" and "A::B" respectively.
114   // If the name fails the heuristic matching for a qualified or unqualified
115   // C/C++ identifier, then it will return false
116   // and identifier and context will be unchanged.
117 
118   static bool ExtractContextAndIdentifier(const char *name,
119                                           llvm::StringRef &context,
120                                           llvm::StringRef &identifier);
121 
122   // in some cases, compilers will output different names for one same type.
123   // when that happens, it might be impossible
124   // to construct SBType objects for a valid type, because the name that is
125   // available is not the same as the name that
126   // can be used as a search key in FindTypes(). the equivalents map here is
127   // meant to return possible alternative names
128   // for a type through which a search can be conducted. Currently, this is only
129   // enabled for C++ but can be extended
130   // to ObjC or other languages if necessary
131   static uint32_t FindEquivalentNames(ConstString type_name,
132                                       std::vector<ConstString> &equivalents);
133 
134   // Given a mangled function name, calculates some alternative manglings since
135   // the compiler mangling may not line up with the symbol we are expecting
136   static uint32_t
137   FindAlternateFunctionManglings(const ConstString mangled,
138                                  std::set<ConstString> &candidates);
139 
140   //------------------------------------------------------------------
141   // PluginInterface protocol
142   //------------------------------------------------------------------
143   ConstString GetPluginName() override;
144 
145   uint32_t GetPluginVersion() override;
146 };
147 
148 } // namespace lldb_private
149 
150 #endif // liblldb_CPlusPlusLanguage_h_
151