Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ClangASTContext.cpp -------------------------------------*- 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 | |
Eli Friedman | 932197d | 2010-06-13 19:06:42 +0000 | [diff] [blame] | 10 | #include "lldb/Symbol/ClangASTContext.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 11 | |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 12 | #include "llvm/Support/FormatAdapters.h" |
| 13 | #include "llvm/Support/FormatVariadic.h" |
| 14 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | // C Includes |
| 16 | // C++ Includes |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 17 | #include <mutex> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | #include <string> |
Sean Callanan | fe38c85 | 2015-10-08 23:07:53 +0000 | [diff] [blame] | 19 | #include <vector> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | |
| 21 | // Other libraries and framework includes |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 22 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 23 | // Clang headers like to use NDEBUG inside of them to enable/disable debug |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 24 | // related features using "#ifndef NDEBUG" preprocessor blocks to do one thing |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 25 | // or another. This is bad because it means that if clang was built in release |
| 26 | // mode, it assumes that you are building in release mode which is not always |
| 27 | // the case. You can end up with functions that are defined as empty in header |
| 28 | // files when NDEBUG is not defined, and this can cause link errors with the |
| 29 | // clang .a files that you have since you might be missing functions in the .a |
| 30 | // file. So we have to define NDEBUG when including clang headers to avoid any |
| 31 | // mismatches. This is covered by rdar://problem/8691220 |
| 32 | |
Sean Callanan | 3b1d4f6 | 2011-10-26 17:46:51 +0000 | [diff] [blame] | 33 | #if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF) |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 34 | #define LLDB_DEFINED_NDEBUG_FOR_CLANG |
Sean Callanan | 246549c | 2010-07-08 18:16:16 +0000 | [diff] [blame] | 35 | #define NDEBUG |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 36 | // Need to include assert.h so it is as clang would expect it to be (disabled) |
| 37 | #include <assert.h> |
| 38 | #endif |
| 39 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | #include "clang/AST/ASTContext.h" |
| 41 | #include "clang/AST/ASTImporter.h" |
Greg Clayton | f74c403 | 2012-12-03 18:29:55 +0000 | [diff] [blame] | 42 | #include "clang/AST/Attr.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | #include "clang/AST/CXXInheritance.h" |
Greg Clayton | 8cf0593 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 44 | #include "clang/AST/DeclObjC.h" |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 45 | #include "clang/AST/DeclTemplate.h" |
Greg Clayton | fe68904 | 2015-11-10 17:47:04 +0000 | [diff] [blame] | 46 | #include "clang/AST/Mangle.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | #include "clang/AST/RecordLayout.h" |
| 48 | #include "clang/AST/Type.h" |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 49 | #include "clang/AST/VTableBuilder.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 50 | #include "clang/Basic/Builtins.h" |
Sean Callanan | 7e2863b | 2012-02-06 21:28:03 +0000 | [diff] [blame] | 51 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 52 | #include "clang/Basic/FileManager.h" |
Sean Callanan | 79439e8 | 2010-11-18 02:56:27 +0000 | [diff] [blame] | 53 | #include "clang/Basic/FileSystemOptions.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 54 | #include "clang/Basic/SourceManager.h" |
| 55 | #include "clang/Basic/TargetInfo.h" |
| 56 | #include "clang/Basic/TargetOptions.h" |
| 57 | #include "clang/Frontend/FrontendOptions.h" |
| 58 | #include "clang/Frontend/LangStandard.h" |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 59 | |
| 60 | #ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG |
Sean Callanan | 246549c | 2010-07-08 18:16:16 +0000 | [diff] [blame] | 61 | #undef NDEBUG |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 62 | #undef LLDB_DEFINED_NDEBUG_FOR_CLANG |
| 63 | // Need to re-include assert.h so it is as _we_ would expect it to be (enabled) |
| 64 | #include <assert.h> |
| 65 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 66 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 67 | #include "llvm/Support/Signals.h" |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 68 | #include "llvm/Support/Threading.h" |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 69 | |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 70 | #include "Plugins/ExpressionParser/Clang/ClangFunctionCaller.h" |
| 71 | #include "Plugins/ExpressionParser/Clang/ClangUserExpression.h" |
| 72 | #include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h" |
Pavel Labath | 5f19b90 | 2017-11-13 16:16:33 +0000 | [diff] [blame] | 73 | #include "lldb/Utility/ArchSpec.h" |
Zachary Turner | 01c3243 | 2017-02-14 19:06:07 +0000 | [diff] [blame] | 74 | #include "lldb/Utility/Flags.h" |
| 75 | |
Zachary Turner | 29cb868 | 2017-03-03 20:57:05 +0000 | [diff] [blame] | 76 | #include "lldb/Core/DumpDataExtractor.h" |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 77 | #include "lldb/Core/Module.h" |
| 78 | #include "lldb/Core/PluginManager.h" |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 79 | #include "lldb/Core/StreamFile.h" |
Enrico Granata | 2267ad4 | 2014-09-16 17:28:40 +0000 | [diff] [blame] | 80 | #include "lldb/Core/ThreadSafeDenseMap.h" |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 81 | #include "lldb/Core/UniqueCStringMap.h" |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 82 | #include "lldb/Symbol/ClangASTContext.h" |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 83 | #include "lldb/Symbol/ClangASTImporter.h" |
Greg Clayton | 6dc8d58 | 2015-08-18 22:32:36 +0000 | [diff] [blame] | 84 | #include "lldb/Symbol/ClangExternalASTSourceCallbacks.h" |
Sean Callanan | 3b107b1 | 2011-12-03 03:15:28 +0000 | [diff] [blame] | 85 | #include "lldb/Symbol/ClangExternalASTSourceCommon.h" |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 86 | #include "lldb/Symbol/ClangUtil.h" |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 87 | #include "lldb/Symbol/ObjectFile.h" |
Greg Clayton | 261ac3f | 2015-08-28 01:01:03 +0000 | [diff] [blame] | 88 | #include "lldb/Symbol/SymbolFile.h" |
Sean Callanan | 5e9e199 | 2011-10-26 01:06:27 +0000 | [diff] [blame] | 89 | #include "lldb/Symbol/VerifyDecl.h" |
Jim Ingham | d555bac | 2011-06-24 22:03:24 +0000 | [diff] [blame] | 90 | #include "lldb/Target/ExecutionContext.h" |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 91 | #include "lldb/Target/Language.h" |
Jim Ingham | d555bac | 2011-06-24 22:03:24 +0000 | [diff] [blame] | 92 | #include "lldb/Target/ObjCLanguageRuntime.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 93 | #include "lldb/Target/Process.h" |
| 94 | #include "lldb/Target/Target.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 95 | #include "lldb/Utility/DataExtractor.h" |
Sean Callanan | c530ba9 | 2016-05-02 21:15:31 +0000 | [diff] [blame] | 96 | #include "lldb/Utility/LLDBAssert.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 97 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 98 | #include "lldb/Utility/RegularExpression.h" |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 +0000 | [diff] [blame] | 99 | #include "lldb/Utility/Scalar.h" |
Jim Ingham | d555bac | 2011-06-24 22:03:24 +0000 | [diff] [blame] | 100 | |
Greg Clayton | 261ac3f | 2015-08-28 01:01:03 +0000 | [diff] [blame] | 101 | #include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h" |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 102 | #include "Plugins/SymbolFile/PDB/PDBASTParser.h" |
Greg Clayton | 261ac3f | 2015-08-28 01:01:03 +0000 | [diff] [blame] | 103 | |
Eli Friedman | 932197d | 2010-06-13 19:06:42 +0000 | [diff] [blame] | 104 | #include <stdio.h> |
| 105 | |
Greg Clayton | 1341baf | 2013-07-11 23:36:31 +0000 | [diff] [blame] | 106 | #include <mutex> |
| 107 | |
Greg Clayton | c86103d | 2010-08-05 01:57:25 +0000 | [diff] [blame] | 108 | using namespace lldb; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 109 | using namespace lldb_private; |
| 110 | using namespace llvm; |
| 111 | using namespace clang; |
| 112 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 113 | namespace { |
| 114 | static inline bool |
| 115 | ClangASTContextSupportsLanguage(lldb::LanguageType language) { |
| 116 | return language == eLanguageTypeUnknown || // Clang is the default type system |
| 117 | Language::LanguageIsC(language) || |
| 118 | Language::LanguageIsCPlusPlus(language) || |
| 119 | Language::LanguageIsObjC(language) || |
| 120 | Language::LanguageIsPascal(language) || |
| 121 | // Use Clang for Rust until there is a proper language plugin for it |
| 122 | language == eLanguageTypeRust || |
Johan Engelen | 0479957 | 2016-11-25 11:01:12 +0000 | [diff] [blame] | 123 | language == eLanguageTypeExtRenderScript || |
| 124 | // Use Clang for D until there is a proper language plugin for it |
| 125 | language == eLanguageTypeD; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 126 | } |
Aleksandr Urakov | 7d2a74f | 2018-08-14 07:57:44 +0000 | [diff] [blame^] | 127 | |
| 128 | // Checks whether m1 is an overload of m2 (as opposed to an override). This is |
| 129 | // called by addOverridesForMethod to distinguish overrides (which share a |
| 130 | // vtable entry) from overloads (which require distinct entries). |
| 131 | bool isOverload(clang::CXXMethodDecl *m1, clang::CXXMethodDecl *m2) { |
| 132 | // FIXME: This should detect covariant return types, but currently doesn't. |
| 133 | lldbassert(&m1->getASTContext() == &m2->getASTContext() && |
| 134 | "Methods should have the same AST context"); |
| 135 | clang::ASTContext &context = m1->getASTContext(); |
| 136 | |
| 137 | const auto *m1Type = llvm::cast<clang::FunctionProtoType>( |
| 138 | context.getCanonicalType(m1->getType())); |
| 139 | |
| 140 | const auto *m2Type = llvm::cast<clang::FunctionProtoType>( |
| 141 | context.getCanonicalType(m2->getType())); |
| 142 | |
| 143 | auto compareArgTypes = [&context](const clang::QualType &m1p, |
| 144 | const clang::QualType &m2p) { |
| 145 | return context.hasSameType(m1p.getUnqualifiedType(), |
| 146 | m2p.getUnqualifiedType()); |
| 147 | }; |
| 148 | |
| 149 | // FIXME: In C++14 and later, we can just pass m2Type->param_type_end() |
| 150 | // as a fourth parameter to std::equal(). |
| 151 | return (m1->getNumParams() != m2->getNumParams()) || |
| 152 | !std::equal(m1Type->param_type_begin(), m1Type->param_type_end(), |
| 153 | m2Type->param_type_begin(), compareArgTypes); |
| 154 | } |
| 155 | |
| 156 | // If decl is a virtual method, walk the base classes looking for methods that |
| 157 | // decl overrides. This table of overridden methods is used by IRGen to |
| 158 | // determine the vtable layout for decl's parent class. |
| 159 | void addOverridesForMethod(clang::CXXMethodDecl *decl) { |
| 160 | if (!decl->isVirtual()) |
| 161 | return; |
| 162 | |
| 163 | clang::CXXBasePaths paths; |
| 164 | |
| 165 | auto find_overridden_methods = |
| 166 | [decl](const clang::CXXBaseSpecifier *specifier, |
| 167 | clang::CXXBasePath &path) { |
| 168 | if (auto *base_record = llvm::dyn_cast<clang::CXXRecordDecl>( |
| 169 | specifier->getType()->getAs<clang::RecordType>()->getDecl())) { |
| 170 | |
| 171 | clang::DeclarationName name = decl->getDeclName(); |
| 172 | |
| 173 | // If this is a destructor, check whether the base class destructor is |
| 174 | // virtual. |
| 175 | if (name.getNameKind() == clang::DeclarationName::CXXDestructorName) |
| 176 | if (auto *baseDtorDecl = base_record->getDestructor()) { |
| 177 | if (baseDtorDecl->isVirtual()) { |
| 178 | path.Decls = baseDtorDecl; |
| 179 | return true; |
| 180 | } else |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | // Otherwise, search for name in the base class. |
| 185 | for (path.Decls = base_record->lookup(name); !path.Decls.empty(); |
| 186 | path.Decls = path.Decls.slice(1)) { |
| 187 | if (auto *method_decl = |
| 188 | llvm::dyn_cast<clang::CXXMethodDecl>(path.Decls.front())) |
| 189 | if (method_decl->isVirtual() && !isOverload(decl, method_decl)) { |
| 190 | path.Decls = method_decl; |
| 191 | return true; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return false; |
| 197 | }; |
| 198 | |
| 199 | if (decl->getParent()->lookupInBases(find_overridden_methods, paths)) { |
| 200 | for (auto *overridden_decl : paths.found_decls()) |
| 201 | decl->addOverriddenMethod( |
| 202 | llvm::cast<clang::CXXMethodDecl>(overridden_decl)); |
| 203 | } |
| 204 | } |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 207 | typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext *> |
| 208 | ClangASTMap; |
Enrico Granata | 5d84a69 | 2014-08-19 21:46:37 +0000 | [diff] [blame] | 209 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 210 | static ClangASTMap &GetASTMap() { |
| 211 | static ClangASTMap *g_map_ptr = nullptr; |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 212 | static llvm::once_flag g_once_flag; |
| 213 | llvm::call_once(g_once_flag, []() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 214 | g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins |
| 215 | }); |
| 216 | return *g_map_ptr; |
Enrico Granata | 5d84a69 | 2014-08-19 21:46:37 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Davide Italiano | 7e3ef4d | 2018-03-20 19:46:32 +0000 | [diff] [blame] | 219 | bool ClangASTContext::IsOperator(const char *name, |
| 220 | clang::OverloadedOperatorKind &op_kind) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 221 | if (name == nullptr || name[0] == '\0') |
| 222 | return false; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 223 | |
| 224 | #define OPERATOR_PREFIX "operator" |
| 225 | #define OPERATOR_PREFIX_LENGTH (sizeof(OPERATOR_PREFIX) - 1) |
| 226 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 227 | const char *post_op_name = nullptr; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 228 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 229 | bool no_space = true; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 230 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 231 | if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH)) |
| 232 | return false; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 233 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 234 | post_op_name = name + OPERATOR_PREFIX_LENGTH; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 235 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 236 | if (post_op_name[0] == ' ') { |
| 237 | post_op_name++; |
| 238 | no_space = false; |
| 239 | } |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 240 | |
| 241 | #undef OPERATOR_PREFIX |
| 242 | #undef OPERATOR_PREFIX_LENGTH |
| 243 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 244 | // This is an operator, set the overloaded operator kind to invalid in case |
| 245 | // this is a conversion operator... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 246 | op_kind = clang::NUM_OVERLOADED_OPERATORS; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 247 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 248 | switch (post_op_name[0]) { |
| 249 | default: |
| 250 | if (no_space) |
| 251 | return false; |
| 252 | break; |
| 253 | case 'n': |
| 254 | if (no_space) |
| 255 | return false; |
| 256 | if (strcmp(post_op_name, "new") == 0) |
| 257 | op_kind = clang::OO_New; |
| 258 | else if (strcmp(post_op_name, "new[]") == 0) |
| 259 | op_kind = clang::OO_Array_New; |
| 260 | break; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 261 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 262 | case 'd': |
| 263 | if (no_space) |
| 264 | return false; |
| 265 | if (strcmp(post_op_name, "delete") == 0) |
| 266 | op_kind = clang::OO_Delete; |
| 267 | else if (strcmp(post_op_name, "delete[]") == 0) |
| 268 | op_kind = clang::OO_Array_Delete; |
| 269 | break; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 270 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 271 | case '+': |
| 272 | if (post_op_name[1] == '\0') |
| 273 | op_kind = clang::OO_Plus; |
| 274 | else if (post_op_name[2] == '\0') { |
| 275 | if (post_op_name[1] == '=') |
| 276 | op_kind = clang::OO_PlusEqual; |
| 277 | else if (post_op_name[1] == '+') |
| 278 | op_kind = clang::OO_PlusPlus; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 279 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 280 | break; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 281 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 282 | case '-': |
| 283 | if (post_op_name[1] == '\0') |
| 284 | op_kind = clang::OO_Minus; |
| 285 | else if (post_op_name[2] == '\0') { |
| 286 | switch (post_op_name[1]) { |
| 287 | case '=': |
| 288 | op_kind = clang::OO_MinusEqual; |
| 289 | break; |
| 290 | case '-': |
| 291 | op_kind = clang::OO_MinusMinus; |
| 292 | break; |
| 293 | case '>': |
| 294 | op_kind = clang::OO_Arrow; |
| 295 | break; |
| 296 | } |
| 297 | } else if (post_op_name[3] == '\0') { |
| 298 | if (post_op_name[2] == '*') |
| 299 | op_kind = clang::OO_ArrowStar; |
| 300 | break; |
| 301 | } |
| 302 | break; |
| 303 | |
| 304 | case '*': |
| 305 | if (post_op_name[1] == '\0') |
| 306 | op_kind = clang::OO_Star; |
| 307 | else if (post_op_name[1] == '=' && post_op_name[2] == '\0') |
| 308 | op_kind = clang::OO_StarEqual; |
| 309 | break; |
| 310 | |
| 311 | case '/': |
| 312 | if (post_op_name[1] == '\0') |
| 313 | op_kind = clang::OO_Slash; |
| 314 | else if (post_op_name[1] == '=' && post_op_name[2] == '\0') |
| 315 | op_kind = clang::OO_SlashEqual; |
| 316 | break; |
| 317 | |
| 318 | case '%': |
| 319 | if (post_op_name[1] == '\0') |
| 320 | op_kind = clang::OO_Percent; |
| 321 | else if (post_op_name[1] == '=' && post_op_name[2] == '\0') |
| 322 | op_kind = clang::OO_PercentEqual; |
| 323 | break; |
| 324 | |
| 325 | case '^': |
| 326 | if (post_op_name[1] == '\0') |
| 327 | op_kind = clang::OO_Caret; |
| 328 | else if (post_op_name[1] == '=' && post_op_name[2] == '\0') |
| 329 | op_kind = clang::OO_CaretEqual; |
| 330 | break; |
| 331 | |
| 332 | case '&': |
| 333 | if (post_op_name[1] == '\0') |
| 334 | op_kind = clang::OO_Amp; |
| 335 | else if (post_op_name[2] == '\0') { |
| 336 | switch (post_op_name[1]) { |
| 337 | case '=': |
| 338 | op_kind = clang::OO_AmpEqual; |
| 339 | break; |
| 340 | case '&': |
| 341 | op_kind = clang::OO_AmpAmp; |
| 342 | break; |
| 343 | } |
| 344 | } |
| 345 | break; |
| 346 | |
| 347 | case '|': |
| 348 | if (post_op_name[1] == '\0') |
| 349 | op_kind = clang::OO_Pipe; |
| 350 | else if (post_op_name[2] == '\0') { |
| 351 | switch (post_op_name[1]) { |
| 352 | case '=': |
| 353 | op_kind = clang::OO_PipeEqual; |
| 354 | break; |
| 355 | case '|': |
| 356 | op_kind = clang::OO_PipePipe; |
| 357 | break; |
| 358 | } |
| 359 | } |
| 360 | break; |
| 361 | |
| 362 | case '~': |
| 363 | if (post_op_name[1] == '\0') |
| 364 | op_kind = clang::OO_Tilde; |
| 365 | break; |
| 366 | |
| 367 | case '!': |
| 368 | if (post_op_name[1] == '\0') |
| 369 | op_kind = clang::OO_Exclaim; |
| 370 | else if (post_op_name[1] == '=' && post_op_name[2] == '\0') |
| 371 | op_kind = clang::OO_ExclaimEqual; |
| 372 | break; |
| 373 | |
| 374 | case '=': |
| 375 | if (post_op_name[1] == '\0') |
| 376 | op_kind = clang::OO_Equal; |
| 377 | else if (post_op_name[1] == '=' && post_op_name[2] == '\0') |
| 378 | op_kind = clang::OO_EqualEqual; |
| 379 | break; |
| 380 | |
| 381 | case '<': |
| 382 | if (post_op_name[1] == '\0') |
| 383 | op_kind = clang::OO_Less; |
| 384 | else if (post_op_name[2] == '\0') { |
| 385 | switch (post_op_name[1]) { |
| 386 | case '<': |
| 387 | op_kind = clang::OO_LessLess; |
| 388 | break; |
| 389 | case '=': |
| 390 | op_kind = clang::OO_LessEqual; |
| 391 | break; |
| 392 | } |
| 393 | } else if (post_op_name[3] == '\0') { |
| 394 | if (post_op_name[2] == '=') |
| 395 | op_kind = clang::OO_LessLessEqual; |
| 396 | } |
| 397 | break; |
| 398 | |
| 399 | case '>': |
| 400 | if (post_op_name[1] == '\0') |
| 401 | op_kind = clang::OO_Greater; |
| 402 | else if (post_op_name[2] == '\0') { |
| 403 | switch (post_op_name[1]) { |
| 404 | case '>': |
| 405 | op_kind = clang::OO_GreaterGreater; |
| 406 | break; |
| 407 | case '=': |
| 408 | op_kind = clang::OO_GreaterEqual; |
| 409 | break; |
| 410 | } |
| 411 | } else if (post_op_name[1] == '>' && post_op_name[2] == '=' && |
| 412 | post_op_name[3] == '\0') { |
| 413 | op_kind = clang::OO_GreaterGreaterEqual; |
| 414 | } |
| 415 | break; |
| 416 | |
| 417 | case ',': |
| 418 | if (post_op_name[1] == '\0') |
| 419 | op_kind = clang::OO_Comma; |
| 420 | break; |
| 421 | |
| 422 | case '(': |
| 423 | if (post_op_name[1] == ')' && post_op_name[2] == '\0') |
| 424 | op_kind = clang::OO_Call; |
| 425 | break; |
| 426 | |
| 427 | case '[': |
| 428 | if (post_op_name[1] == ']' && post_op_name[2] == '\0') |
| 429 | op_kind = clang::OO_Subscript; |
| 430 | break; |
| 431 | } |
| 432 | |
| 433 | return true; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 434 | } |
Enrico Granata | 5d84a69 | 2014-08-19 21:46:37 +0000 | [diff] [blame] | 435 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 436 | clang::AccessSpecifier |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 437 | ClangASTContext::ConvertAccessTypeToAccessSpecifier(AccessType access) { |
| 438 | switch (access) { |
| 439 | default: |
| 440 | break; |
| 441 | case eAccessNone: |
Greg Clayton | 8cf0593 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 442 | return AS_none; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 443 | case eAccessPublic: |
| 444 | return AS_public; |
| 445 | case eAccessPrivate: |
| 446 | return AS_private; |
| 447 | case eAccessProtected: |
| 448 | return AS_protected; |
| 449 | } |
| 450 | return AS_none; |
Greg Clayton | 8cf0593 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 453 | static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) { |
| 454 | // FIXME: Cleanup per-file based stuff. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 455 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 456 | // Set some properties which depend solely on the input kind; it would be |
| 457 | // nice to move these to the language standard, and have the driver resolve |
| 458 | // the input kind + language standard. |
Richard Smith | 8186cd4 | 2017-04-26 22:10:53 +0000 | [diff] [blame] | 459 | if (IK.getLanguage() == InputKind::Asm) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 460 | Opts.AsmPreprocessor = 1; |
Richard Smith | 8186cd4 | 2017-04-26 22:10:53 +0000 | [diff] [blame] | 461 | } else if (IK.isObjectiveC()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 462 | Opts.ObjC1 = Opts.ObjC2 = 1; |
| 463 | } |
| 464 | |
| 465 | LangStandard::Kind LangStd = LangStandard::lang_unspecified; |
| 466 | |
| 467 | if (LangStd == LangStandard::lang_unspecified) { |
| 468 | // Based on the base language, pick one. |
Richard Smith | 8186cd4 | 2017-04-26 22:10:53 +0000 | [diff] [blame] | 469 | switch (IK.getLanguage()) { |
| 470 | case InputKind::Unknown: |
| 471 | case InputKind::LLVM_IR: |
| 472 | case InputKind::RenderScript: |
David Blaikie | a322f36 | 2017-01-06 00:38:06 +0000 | [diff] [blame] | 473 | llvm_unreachable("Invalid input kind!"); |
Richard Smith | 8186cd4 | 2017-04-26 22:10:53 +0000 | [diff] [blame] | 474 | case InputKind::OpenCL: |
Pavel Labath | 4716854 | 2017-04-27 08:49:19 +0000 | [diff] [blame] | 475 | LangStd = LangStandard::lang_opencl10; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 476 | break; |
Richard Smith | 8186cd4 | 2017-04-26 22:10:53 +0000 | [diff] [blame] | 477 | case InputKind::CUDA: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 478 | LangStd = LangStandard::lang_cuda; |
| 479 | break; |
Richard Smith | 8186cd4 | 2017-04-26 22:10:53 +0000 | [diff] [blame] | 480 | case InputKind::Asm: |
| 481 | case InputKind::C: |
| 482 | case InputKind::ObjC: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 483 | LangStd = LangStandard::lang_gnu99; |
| 484 | break; |
Richard Smith | 8186cd4 | 2017-04-26 22:10:53 +0000 | [diff] [blame] | 485 | case InputKind::CXX: |
| 486 | case InputKind::ObjCXX: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 487 | LangStd = LangStandard::lang_gnucxx98; |
| 488 | break; |
Benjamin Kramer | 0d97c22 | 2018-04-25 13:22:47 +0000 | [diff] [blame] | 489 | case InputKind::HIP: |
| 490 | LangStd = LangStandard::lang_hip; |
| 491 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 492 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 493 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 494 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 495 | const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd); |
| 496 | Opts.LineComment = Std.hasLineComments(); |
| 497 | Opts.C99 = Std.isC99(); |
| 498 | Opts.CPlusPlus = Std.isCPlusPlus(); |
| 499 | Opts.CPlusPlus11 = Std.isCPlusPlus11(); |
| 500 | Opts.Digraphs = Std.hasDigraphs(); |
| 501 | Opts.GNUMode = Std.isGNUMode(); |
| 502 | Opts.GNUInline = !Std.isC99(); |
| 503 | Opts.HexFloats = Std.hasHexFloats(); |
| 504 | Opts.ImplicitInt = Std.hasImplicitInt(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 505 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 506 | Opts.WChar = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 507 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 508 | // OpenCL has some additional defaults. |
Pavel Labath | 4716854 | 2017-04-27 08:49:19 +0000 | [diff] [blame] | 509 | if (LangStd == LangStandard::lang_opencl10) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 510 | Opts.OpenCL = 1; |
| 511 | Opts.AltiVec = 1; |
| 512 | Opts.CXXOperatorNames = 1; |
| 513 | Opts.LaxVectorConversions = 1; |
| 514 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 515 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 516 | // OpenCL and C++ both have bool, true, false keywords. |
| 517 | Opts.Bool = Opts.OpenCL || Opts.CPlusPlus; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 518 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 519 | Opts.setValueVisibilityMode(DefaultVisibility); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 520 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 521 | // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs is |
| 522 | // specified, or -std is set to a conforming mode. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 523 | Opts.Trigraphs = !Opts.GNUMode; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 524 | Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 525 | Opts.OptimizeSize = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 526 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 527 | // FIXME: Eliminate this dependency. |
| 528 | // unsigned Opt = |
| 529 | // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags); |
| 530 | // Opts.Optimize = Opt != 0; |
| 531 | unsigned Opt = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 532 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 533 | // This is the __NO_INLINE__ define, which just depends on things like the |
| 534 | // optimization level and -fno-inline, not actually whether the backend has |
| 535 | // inlining enabled. |
| 536 | // |
| 537 | // FIXME: This is affected by other options (-fno-inline). |
| 538 | Opts.NoInlineDefine = !Opt; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 541 | ClangASTContext::ClangASTContext(const char *target_triple) |
| 542 | : TypeSystem(TypeSystem::eKindClang), m_target_triple(), m_ast_ap(), |
| 543 | m_language_options_ap(), m_source_manager_ap(), m_diagnostics_engine_ap(), |
| 544 | m_target_options_rp(), m_target_info_ap(), m_identifier_table_ap(), |
| 545 | m_selector_table_ap(), m_builtins_ap(), m_callback_tag_decl(nullptr), |
| 546 | m_callback_objc_decl(nullptr), m_callback_baton(nullptr), |
| 547 | m_pointer_byte_size(0), m_ast_owned(false) { |
| 548 | if (target_triple && target_triple[0]) |
| 549 | SetTargetTriple(target_triple); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | //---------------------------------------------------------------------- |
| 553 | // Destructor |
| 554 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 555 | ClangASTContext::~ClangASTContext() { Finalize(); } |
| 556 | |
| 557 | ConstString ClangASTContext::GetPluginNameStatic() { |
| 558 | return ConstString("clang"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 561 | ConstString ClangASTContext::GetPluginName() { |
| 562 | return ClangASTContext::GetPluginNameStatic(); |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 565 | uint32_t ClangASTContext::GetPluginVersion() { return 1; } |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 566 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 567 | lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language, |
| 568 | lldb_private::Module *module, |
| 569 | Target *target) { |
| 570 | if (ClangASTContextSupportsLanguage(language)) { |
| 571 | ArchSpec arch; |
| 572 | if (module) |
| 573 | arch = module->GetArchitecture(); |
| 574 | else if (target) |
| 575 | arch = target->GetArchitecture(); |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 576 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 577 | if (arch.IsValid()) { |
| 578 | ArchSpec fixed_arch = arch; |
| 579 | // LLVM wants this to be set to iOS or MacOSX; if we're working on |
| 580 | // a bare-boards type image, change the triple for llvm's benefit. |
| 581 | if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple && |
| 582 | fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) { |
| 583 | if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm || |
| 584 | fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 || |
| 585 | fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) { |
| 586 | fixed_arch.GetTriple().setOS(llvm::Triple::IOS); |
| 587 | } else { |
| 588 | fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX); |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 589 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 590 | } |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 591 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 592 | if (module) { |
| 593 | std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext); |
| 594 | if (ast_sp) { |
| 595 | ast_sp->SetArchitecture(fixed_arch); |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 596 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 597 | return ast_sp; |
| 598 | } else if (target && target->IsValid()) { |
| 599 | std::shared_ptr<ClangASTContextForExpressions> ast_sp( |
| 600 | new ClangASTContextForExpressions(*target)); |
| 601 | if (ast_sp) { |
| 602 | ast_sp->SetArchitecture(fixed_arch); |
| 603 | ast_sp->m_scratch_ast_source_ap.reset( |
| 604 | new ClangASTSource(target->shared_from_this())); |
Sean Callanan | 68e4423 | 2017-09-28 20:20:25 +0000 | [diff] [blame] | 605 | lldbassert(ast_sp->getFileManager()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 606 | ast_sp->m_scratch_ast_source_ap->InstallASTContext( |
Sean Callanan | 68e4423 | 2017-09-28 20:20:25 +0000 | [diff] [blame] | 607 | *ast_sp->getASTContext(), *ast_sp->getFileManager(), true); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 608 | llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source( |
| 609 | ast_sp->m_scratch_ast_source_ap->CreateProxy()); |
| 610 | ast_sp->SetExternalSource(proxy_ast_source); |
| 611 | return ast_sp; |
| 612 | } |
| 613 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 614 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 615 | } |
| 616 | return lldb::TypeSystemSP(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 619 | void ClangASTContext::EnumerateSupportedLanguages( |
| 620 | std::set<lldb::LanguageType> &languages_for_types, |
| 621 | std::set<lldb::LanguageType> &languages_for_expressions) { |
| 622 | static std::vector<lldb::LanguageType> s_supported_languages_for_types( |
| 623 | {lldb::eLanguageTypeC89, lldb::eLanguageTypeC, lldb::eLanguageTypeC11, |
| 624 | lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeC99, |
| 625 | lldb::eLanguageTypeObjC, lldb::eLanguageTypeObjC_plus_plus, |
| 626 | lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11, |
| 627 | lldb::eLanguageTypeC11, lldb::eLanguageTypeC_plus_plus_14}); |
| 628 | |
| 629 | static std::vector<lldb::LanguageType> s_supported_languages_for_expressions( |
| 630 | {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC_plus_plus, |
| 631 | lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11, |
| 632 | lldb::eLanguageTypeC_plus_plus_14}); |
| 633 | |
| 634 | languages_for_types.insert(s_supported_languages_for_types.begin(), |
| 635 | s_supported_languages_for_types.end()); |
| 636 | languages_for_expressions.insert( |
| 637 | s_supported_languages_for_expressions.begin(), |
| 638 | s_supported_languages_for_expressions.end()); |
Enrico Granata | 5d84a69 | 2014-08-19 21:46:37 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 641 | void ClangASTContext::Initialize() { |
| 642 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 643 | "clang base AST context plug-in", |
| 644 | CreateInstance, EnumerateSupportedLanguages); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 647 | void ClangASTContext::Terminate() { |
| 648 | PluginManager::UnregisterPlugin(CreateInstance); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 651 | void ClangASTContext::Finalize() { |
| 652 | if (m_ast_ap.get()) { |
| 653 | GetASTMap().Erase(m_ast_ap.get()); |
| 654 | if (!m_ast_owned) |
| 655 | m_ast_ap.release(); |
| 656 | } |
| 657 | |
| 658 | m_builtins_ap.reset(); |
| 659 | m_selector_table_ap.reset(); |
| 660 | m_identifier_table_ap.reset(); |
| 661 | m_target_info_ap.reset(); |
| 662 | m_target_options_rp.reset(); |
| 663 | m_diagnostics_engine_ap.reset(); |
| 664 | m_source_manager_ap.reset(); |
| 665 | m_language_options_ap.reset(); |
| 666 | m_ast_ap.reset(); |
| 667 | m_scratch_ast_source_ap.reset(); |
| 668 | } |
| 669 | |
| 670 | void ClangASTContext::Clear() { |
| 671 | m_ast_ap.reset(); |
| 672 | m_language_options_ap.reset(); |
| 673 | m_source_manager_ap.reset(); |
| 674 | m_diagnostics_engine_ap.reset(); |
| 675 | m_target_options_rp.reset(); |
| 676 | m_target_info_ap.reset(); |
| 677 | m_identifier_table_ap.reset(); |
| 678 | m_selector_table_ap.reset(); |
| 679 | m_builtins_ap.reset(); |
| 680 | m_pointer_byte_size = 0; |
| 681 | } |
| 682 | |
| 683 | const char *ClangASTContext::GetTargetTriple() { |
| 684 | return m_target_triple.c_str(); |
| 685 | } |
| 686 | |
| 687 | void ClangASTContext::SetTargetTriple(const char *target_triple) { |
| 688 | Clear(); |
| 689 | m_target_triple.assign(target_triple); |
| 690 | } |
| 691 | |
| 692 | void ClangASTContext::SetArchitecture(const ArchSpec &arch) { |
| 693 | SetTargetTriple(arch.GetTriple().str().c_str()); |
| 694 | } |
| 695 | |
| 696 | bool ClangASTContext::HasExternalSource() { |
| 697 | ASTContext *ast = getASTContext(); |
| 698 | if (ast) |
| 699 | return ast->getExternalSource() != nullptr; |
| 700 | return false; |
| 701 | } |
| 702 | |
| 703 | void ClangASTContext::SetExternalSource( |
| 704 | llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap) { |
| 705 | ASTContext *ast = getASTContext(); |
| 706 | if (ast) { |
| 707 | ast->setExternalSource(ast_source_ap); |
| 708 | ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
| 712 | void ClangASTContext::RemoveExternalSource() { |
| 713 | ASTContext *ast = getASTContext(); |
| 714 | |
| 715 | if (ast) { |
| 716 | llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap; |
| 717 | ast->setExternalSource(empty_ast_source_ap); |
| 718 | ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | |
| 722 | void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) { |
| 723 | if (!m_ast_owned) { |
| 724 | m_ast_ap.release(); |
| 725 | } |
| 726 | m_ast_owned = false; |
| 727 | m_ast_ap.reset(ast_ctx); |
| 728 | GetASTMap().Insert(ast_ctx, this); |
| 729 | } |
| 730 | |
| 731 | ASTContext *ClangASTContext::getASTContext() { |
| 732 | if (m_ast_ap.get() == nullptr) { |
| 733 | m_ast_owned = true; |
| 734 | m_ast_ap.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(), |
| 735 | *getIdentifierTable(), *getSelectorTable(), |
| 736 | *getBuiltinContext())); |
| 737 | |
| 738 | m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false); |
| 739 | |
| 740 | // This can be NULL if we don't know anything about the architecture or if |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 741 | // the target for an architecture isn't enabled in the llvm/clang that we |
| 742 | // built |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 743 | TargetInfo *target_info = getTargetInfo(); |
| 744 | if (target_info) |
| 745 | m_ast_ap->InitBuiltinTypes(*target_info); |
| 746 | |
| 747 | if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) { |
| 748 | m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage(); |
| 749 | // m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 750 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 751 | |
| 752 | GetASTMap().Insert(m_ast_ap.get(), this); |
| 753 | |
| 754 | llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap( |
| 755 | new ClangExternalASTSourceCallbacks( |
| 756 | ClangASTContext::CompleteTagDecl, |
| 757 | ClangASTContext::CompleteObjCInterfaceDecl, nullptr, |
| 758 | ClangASTContext::LayoutRecordType, this)); |
| 759 | SetExternalSource(ast_source_ap); |
| 760 | } |
| 761 | return m_ast_ap.get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 764 | ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) { |
| 765 | ClangASTContext *clang_ast = GetASTMap().Lookup(ast); |
| 766 | return clang_ast; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 769 | Builtin::Context *ClangASTContext::getBuiltinContext() { |
| 770 | if (m_builtins_ap.get() == nullptr) |
| 771 | m_builtins_ap.reset(new Builtin::Context()); |
| 772 | return m_builtins_ap.get(); |
Sean Callanan | 79439e8 | 2010-11-18 02:56:27 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 775 | IdentifierTable *ClangASTContext::getIdentifierTable() { |
| 776 | if (m_identifier_table_ap.get() == nullptr) |
| 777 | m_identifier_table_ap.reset( |
| 778 | new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr)); |
| 779 | return m_identifier_table_ap.get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 782 | LangOptions *ClangASTContext::getLanguageOptions() { |
| 783 | if (m_language_options_ap.get() == nullptr) { |
| 784 | m_language_options_ap.reset(new LangOptions()); |
Richard Smith | 8186cd4 | 2017-04-26 22:10:53 +0000 | [diff] [blame] | 785 | ParseLangArgs(*m_language_options_ap, InputKind::ObjCXX, GetTargetTriple()); |
| 786 | // InitializeLangOptions(*m_language_options_ap, InputKind::ObjCXX); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 787 | } |
| 788 | return m_language_options_ap.get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 791 | SelectorTable *ClangASTContext::getSelectorTable() { |
| 792 | if (m_selector_table_ap.get() == nullptr) |
| 793 | m_selector_table_ap.reset(new SelectorTable()); |
| 794 | return m_selector_table_ap.get(); |
Greg Clayton | fe68904 | 2015-11-10 17:47:04 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 797 | clang::FileManager *ClangASTContext::getFileManager() { |
| 798 | if (m_file_manager_ap.get() == nullptr) { |
| 799 | clang::FileSystemOptions file_system_options; |
| 800 | m_file_manager_ap.reset(new clang::FileManager(file_system_options)); |
| 801 | } |
| 802 | return m_file_manager_ap.get(); |
| 803 | } |
| 804 | |
| 805 | clang::SourceManager *ClangASTContext::getSourceManager() { |
| 806 | if (m_source_manager_ap.get() == nullptr) |
| 807 | m_source_manager_ap.reset( |
| 808 | new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager())); |
| 809 | return m_source_manager_ap.get(); |
| 810 | } |
| 811 | |
| 812 | clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() { |
| 813 | if (m_diagnostics_engine_ap.get() == nullptr) { |
| 814 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs()); |
| 815 | m_diagnostics_engine_ap.reset( |
| 816 | new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions())); |
| 817 | } |
| 818 | return m_diagnostics_engine_ap.get(); |
| 819 | } |
| 820 | |
| 821 | clang::MangleContext *ClangASTContext::getMangleContext() { |
| 822 | if (m_mangle_ctx_ap.get() == nullptr) |
| 823 | m_mangle_ctx_ap.reset(getASTContext()->createMangleContext()); |
| 824 | return m_mangle_ctx_ap.get(); |
| 825 | } |
| 826 | |
| 827 | class NullDiagnosticConsumer : public DiagnosticConsumer { |
Sean Callanan | 7fddd4c | 2010-12-11 00:08:56 +0000 | [diff] [blame] | 828 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 829 | NullDiagnosticConsumer() { |
| 830 | m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS); |
| 831 | } |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 832 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 833 | void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| 834 | const clang::Diagnostic &info) { |
| 835 | if (m_log) { |
| 836 | llvm::SmallVector<char, 32> diag_str(10); |
| 837 | info.FormatDiagnostic(diag_str); |
| 838 | diag_str.push_back('\0'); |
| 839 | m_log->Printf("Compiler diagnostic: %s\n", diag_str.data()); |
Sean Callanan | 7fddd4c | 2010-12-11 00:08:56 +0000 | [diff] [blame] | 840 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const { |
| 844 | return new NullDiagnosticConsumer(); |
| 845 | } |
| 846 | |
Sean Callanan | 7fddd4c | 2010-12-11 00:08:56 +0000 | [diff] [blame] | 847 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 848 | Log *m_log; |
Sean Callanan | 7fddd4c | 2010-12-11 00:08:56 +0000 | [diff] [blame] | 849 | }; |
| 850 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 851 | DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() { |
| 852 | if (m_diagnostic_consumer_ap.get() == nullptr) |
| 853 | m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer); |
| 854 | |
| 855 | return m_diagnostic_consumer_ap.get(); |
Sean Callanan | 7fddd4c | 2010-12-11 00:08:56 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 858 | std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() { |
| 859 | if (m_target_options_rp.get() == nullptr && !m_target_triple.empty()) { |
| 860 | m_target_options_rp = std::make_shared<clang::TargetOptions>(); |
| 861 | if (m_target_options_rp.get() != nullptr) |
| 862 | m_target_options_rp->Triple = m_target_triple; |
| 863 | } |
| 864 | return m_target_options_rp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 867 | TargetInfo *ClangASTContext::getTargetInfo() { |
| 868 | // target_triple should be something like "x86_64-apple-macosx" |
| 869 | if (m_target_info_ap.get() == nullptr && !m_target_triple.empty()) |
| 870 | m_target_info_ap.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), |
| 871 | getTargetOptions())); |
| 872 | return m_target_info_ap.get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | #pragma mark Basic Types |
| 876 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 877 | static inline bool QualTypeMatchesBitSize(const uint64_t bit_size, |
| 878 | ASTContext *ast, QualType qual_type) { |
| 879 | uint64_t qual_type_bit_size = ast->getTypeSize(qual_type); |
| 880 | if (qual_type_bit_size == bit_size) |
| 881 | return true; |
| 882 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 883 | } |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 884 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 885 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 886 | ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding, |
| 887 | size_t bit_size) { |
| 888 | return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize( |
| 889 | getASTContext(), encoding, bit_size); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 892 | CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize( |
| 893 | ASTContext *ast, Encoding encoding, uint32_t bit_size) { |
| 894 | if (!ast) |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 895 | return CompilerType(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 896 | switch (encoding) { |
| 897 | case eEncodingInvalid: |
| 898 | if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy)) |
| 899 | return CompilerType(ast, ast->VoidPtrTy); |
| 900 | break; |
| 901 | |
| 902 | case eEncodingUint: |
| 903 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy)) |
| 904 | return CompilerType(ast, ast->UnsignedCharTy); |
| 905 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy)) |
| 906 | return CompilerType(ast, ast->UnsignedShortTy); |
| 907 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy)) |
| 908 | return CompilerType(ast, ast->UnsignedIntTy); |
| 909 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy)) |
| 910 | return CompilerType(ast, ast->UnsignedLongTy); |
| 911 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy)) |
| 912 | return CompilerType(ast, ast->UnsignedLongLongTy); |
| 913 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty)) |
| 914 | return CompilerType(ast, ast->UnsignedInt128Ty); |
| 915 | break; |
| 916 | |
| 917 | case eEncodingSint: |
| 918 | if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy)) |
| 919 | return CompilerType(ast, ast->SignedCharTy); |
| 920 | if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy)) |
| 921 | return CompilerType(ast, ast->ShortTy); |
| 922 | if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy)) |
| 923 | return CompilerType(ast, ast->IntTy); |
| 924 | if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy)) |
| 925 | return CompilerType(ast, ast->LongTy); |
| 926 | if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy)) |
| 927 | return CompilerType(ast, ast->LongLongTy); |
| 928 | if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty)) |
| 929 | return CompilerType(ast, ast->Int128Ty); |
| 930 | break; |
| 931 | |
| 932 | case eEncodingIEEE754: |
| 933 | if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy)) |
| 934 | return CompilerType(ast, ast->FloatTy); |
| 935 | if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy)) |
| 936 | return CompilerType(ast, ast->DoubleTy); |
| 937 | if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy)) |
| 938 | return CompilerType(ast, ast->LongDoubleTy); |
| 939 | if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy)) |
| 940 | return CompilerType(ast, ast->HalfTy); |
| 941 | break; |
| 942 | |
| 943 | case eEncodingVector: |
| 944 | // Sanity check that bit_size is a multiple of 8's. |
| 945 | if (bit_size && !(bit_size & 0x7u)) |
| 946 | return CompilerType( |
| 947 | ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8)); |
| 948 | break; |
| 949 | } |
| 950 | |
| 951 | return CompilerType(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 952 | } |
| 953 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 954 | lldb::BasicType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 955 | ClangASTContext::GetBasicTypeEnumeration(const ConstString &name) { |
| 956 | if (name) { |
| 957 | typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap; |
| 958 | static TypeNameToBasicTypeMap g_type_map; |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 959 | static llvm::once_flag g_once_flag; |
| 960 | llvm::call_once(g_once_flag, []() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 961 | // "void" |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 962 | g_type_map.Append(ConstString("void"), eBasicTypeVoid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 963 | |
| 964 | // "char" |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 965 | g_type_map.Append(ConstString("char"), eBasicTypeChar); |
| 966 | g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar); |
| 967 | g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar); |
| 968 | g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar); |
| 969 | g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar); |
| 970 | g_type_map.Append(ConstString("unsigned wchar_t"), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 971 | eBasicTypeUnsignedWChar); |
| 972 | // "short" |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 973 | g_type_map.Append(ConstString("short"), eBasicTypeShort); |
| 974 | g_type_map.Append(ConstString("short int"), eBasicTypeShort); |
| 975 | g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort); |
| 976 | g_type_map.Append(ConstString("unsigned short int"), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 977 | eBasicTypeUnsignedShort); |
| 978 | |
| 979 | // "int" |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 980 | g_type_map.Append(ConstString("int"), eBasicTypeInt); |
| 981 | g_type_map.Append(ConstString("signed int"), eBasicTypeInt); |
| 982 | g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt); |
| 983 | g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 984 | |
| 985 | // "long" |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 986 | g_type_map.Append(ConstString("long"), eBasicTypeLong); |
| 987 | g_type_map.Append(ConstString("long int"), eBasicTypeLong); |
| 988 | g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong); |
| 989 | g_type_map.Append(ConstString("unsigned long int"), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 990 | eBasicTypeUnsignedLong); |
| 991 | |
| 992 | // "long long" |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 993 | g_type_map.Append(ConstString("long long"), eBasicTypeLongLong); |
| 994 | g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong); |
| 995 | g_type_map.Append(ConstString("unsigned long long"), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 996 | eBasicTypeUnsignedLongLong); |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 997 | g_type_map.Append(ConstString("unsigned long long int"), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 998 | eBasicTypeUnsignedLongLong); |
| 999 | |
| 1000 | // "int128" |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 1001 | g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128); |
| 1002 | g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1003 | |
| 1004 | // Miscellaneous |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 1005 | g_type_map.Append(ConstString("bool"), eBasicTypeBool); |
| 1006 | g_type_map.Append(ConstString("float"), eBasicTypeFloat); |
| 1007 | g_type_map.Append(ConstString("double"), eBasicTypeDouble); |
| 1008 | g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble); |
| 1009 | g_type_map.Append(ConstString("id"), eBasicTypeObjCID); |
| 1010 | g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel); |
| 1011 | g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1012 | g_type_map.Sort(); |
| 1013 | }); |
| 1014 | |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 1015 | return g_type_map.Find(name, eBasicTypeInvalid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1016 | } |
| 1017 | return eBasicTypeInvalid; |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1020 | CompilerType ClangASTContext::GetBasicType(ASTContext *ast, |
| 1021 | const ConstString &name) { |
| 1022 | if (ast) { |
| 1023 | lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name); |
| 1024 | return ClangASTContext::GetBasicType(ast, basic_type); |
| 1025 | } |
| 1026 | return CompilerType(); |
| 1027 | } |
| 1028 | |
| 1029 | uint32_t ClangASTContext::GetPointerByteSize() { |
| 1030 | if (m_pointer_byte_size == 0) |
| 1031 | m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid) |
| 1032 | .GetPointerType() |
| 1033 | .GetByteSize(nullptr); |
| 1034 | return m_pointer_byte_size; |
| 1035 | } |
| 1036 | |
| 1037 | CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) { |
| 1038 | return GetBasicType(getASTContext(), basic_type); |
| 1039 | } |
| 1040 | |
| 1041 | CompilerType ClangASTContext::GetBasicType(ASTContext *ast, |
| 1042 | lldb::BasicType basic_type) { |
| 1043 | if (!ast) |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 1044 | return CompilerType(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1045 | lldb::opaque_compiler_type_t clang_type = |
| 1046 | GetOpaqueCompilerType(ast, basic_type); |
| 1047 | |
| 1048 | if (clang_type) |
| 1049 | return CompilerType(GetASTContext(ast), clang_type); |
| 1050 | return CompilerType(); |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1053 | CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize( |
| 1054 | const char *type_name, uint32_t dw_ate, uint32_t bit_size) { |
| 1055 | ASTContext *ast = getASTContext(); |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1056 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1057 | #define streq(a, b) strcmp(a, b) == 0 |
| 1058 | assert(ast != nullptr); |
| 1059 | if (ast) { |
| 1060 | switch (dw_ate) { |
| 1061 | default: |
| 1062 | break; |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1063 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1064 | case DW_ATE_address: |
| 1065 | if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy)) |
| 1066 | return CompilerType(ast, ast->VoidPtrTy); |
| 1067 | break; |
Zachary Turner | 9d8a97e | 2016-04-01 23:20:35 +0000 | [diff] [blame] | 1068 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1069 | case DW_ATE_boolean: |
| 1070 | if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy)) |
| 1071 | return CompilerType(ast, ast->BoolTy); |
| 1072 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy)) |
| 1073 | return CompilerType(ast, ast->UnsignedCharTy); |
| 1074 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy)) |
| 1075 | return CompilerType(ast, ast->UnsignedShortTy); |
| 1076 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy)) |
| 1077 | return CompilerType(ast, ast->UnsignedIntTy); |
| 1078 | break; |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1079 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1080 | case DW_ATE_lo_user: |
| 1081 | // This has been seen to mean DW_AT_complex_integer |
| 1082 | if (type_name) { |
| 1083 | if (::strstr(type_name, "complex")) { |
| 1084 | CompilerType complex_int_clang_type = |
| 1085 | GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed, |
| 1086 | bit_size / 2); |
| 1087 | return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType( |
| 1088 | complex_int_clang_type))); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1089 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1090 | } |
| 1091 | break; |
| 1092 | |
| 1093 | case DW_ATE_complex_float: |
| 1094 | if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy)) |
| 1095 | return CompilerType(ast, ast->FloatComplexTy); |
| 1096 | else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy)) |
| 1097 | return CompilerType(ast, ast->DoubleComplexTy); |
| 1098 | else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy)) |
| 1099 | return CompilerType(ast, ast->LongDoubleComplexTy); |
| 1100 | else { |
| 1101 | CompilerType complex_float_clang_type = |
| 1102 | GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float, |
| 1103 | bit_size / 2); |
| 1104 | return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType( |
| 1105 | complex_float_clang_type))); |
| 1106 | } |
| 1107 | break; |
| 1108 | |
| 1109 | case DW_ATE_float: |
| 1110 | if (streq(type_name, "float") && |
| 1111 | QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy)) |
| 1112 | return CompilerType(ast, ast->FloatTy); |
| 1113 | if (streq(type_name, "double") && |
| 1114 | QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy)) |
| 1115 | return CompilerType(ast, ast->DoubleTy); |
| 1116 | if (streq(type_name, "long double") && |
| 1117 | QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy)) |
| 1118 | return CompilerType(ast, ast->LongDoubleTy); |
| 1119 | // Fall back to not requiring a name match |
| 1120 | if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy)) |
| 1121 | return CompilerType(ast, ast->FloatTy); |
| 1122 | if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy)) |
| 1123 | return CompilerType(ast, ast->DoubleTy); |
| 1124 | if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy)) |
| 1125 | return CompilerType(ast, ast->LongDoubleTy); |
| 1126 | if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy)) |
| 1127 | return CompilerType(ast, ast->HalfTy); |
| 1128 | break; |
| 1129 | |
| 1130 | case DW_ATE_signed: |
| 1131 | if (type_name) { |
| 1132 | if (streq(type_name, "wchar_t") && |
| 1133 | QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) && |
| 1134 | (getTargetInfo() && |
| 1135 | TargetInfo::isTypeSigned(getTargetInfo()->getWCharType()))) |
| 1136 | return CompilerType(ast, ast->WCharTy); |
| 1137 | if (streq(type_name, "void") && |
| 1138 | QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy)) |
| 1139 | return CompilerType(ast, ast->VoidTy); |
| 1140 | if (strstr(type_name, "long long") && |
| 1141 | QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy)) |
| 1142 | return CompilerType(ast, ast->LongLongTy); |
| 1143 | if (strstr(type_name, "long") && |
| 1144 | QualTypeMatchesBitSize(bit_size, ast, ast->LongTy)) |
| 1145 | return CompilerType(ast, ast->LongTy); |
| 1146 | if (strstr(type_name, "short") && |
| 1147 | QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy)) |
| 1148 | return CompilerType(ast, ast->ShortTy); |
| 1149 | if (strstr(type_name, "char")) { |
| 1150 | if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy)) |
| 1151 | return CompilerType(ast, ast->CharTy); |
| 1152 | if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy)) |
| 1153 | return CompilerType(ast, ast->SignedCharTy); |
| 1154 | } |
| 1155 | if (strstr(type_name, "int")) { |
| 1156 | if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy)) |
| 1157 | return CompilerType(ast, ast->IntTy); |
| 1158 | if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty)) |
| 1159 | return CompilerType(ast, ast->Int128Ty); |
| 1160 | } |
| 1161 | } |
| 1162 | // We weren't able to match up a type name, just search by size |
| 1163 | if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy)) |
| 1164 | return CompilerType(ast, ast->CharTy); |
| 1165 | if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy)) |
| 1166 | return CompilerType(ast, ast->ShortTy); |
| 1167 | if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy)) |
| 1168 | return CompilerType(ast, ast->IntTy); |
| 1169 | if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy)) |
| 1170 | return CompilerType(ast, ast->LongTy); |
| 1171 | if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy)) |
| 1172 | return CompilerType(ast, ast->LongLongTy); |
| 1173 | if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty)) |
| 1174 | return CompilerType(ast, ast->Int128Ty); |
| 1175 | break; |
| 1176 | |
| 1177 | case DW_ATE_signed_char: |
| 1178 | if (ast->getLangOpts().CharIsSigned && type_name && |
| 1179 | streq(type_name, "char")) { |
| 1180 | if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy)) |
| 1181 | return CompilerType(ast, ast->CharTy); |
| 1182 | } |
| 1183 | if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy)) |
| 1184 | return CompilerType(ast, ast->SignedCharTy); |
| 1185 | break; |
| 1186 | |
| 1187 | case DW_ATE_unsigned: |
| 1188 | if (type_name) { |
| 1189 | if (streq(type_name, "wchar_t")) { |
| 1190 | if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) { |
| 1191 | if (!(getTargetInfo() && |
| 1192 | TargetInfo::isTypeSigned(getTargetInfo()->getWCharType()))) |
| 1193 | return CompilerType(ast, ast->WCharTy); |
| 1194 | } |
| 1195 | } |
| 1196 | if (strstr(type_name, "long long")) { |
| 1197 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy)) |
| 1198 | return CompilerType(ast, ast->UnsignedLongLongTy); |
| 1199 | } else if (strstr(type_name, "long")) { |
| 1200 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy)) |
| 1201 | return CompilerType(ast, ast->UnsignedLongTy); |
| 1202 | } else if (strstr(type_name, "short")) { |
| 1203 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy)) |
| 1204 | return CompilerType(ast, ast->UnsignedShortTy); |
| 1205 | } else if (strstr(type_name, "char")) { |
| 1206 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy)) |
| 1207 | return CompilerType(ast, ast->UnsignedCharTy); |
| 1208 | } else if (strstr(type_name, "int")) { |
| 1209 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy)) |
| 1210 | return CompilerType(ast, ast->UnsignedIntTy); |
| 1211 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty)) |
| 1212 | return CompilerType(ast, ast->UnsignedInt128Ty); |
| 1213 | } |
| 1214 | } |
| 1215 | // We weren't able to match up a type name, just search by size |
| 1216 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy)) |
| 1217 | return CompilerType(ast, ast->UnsignedCharTy); |
| 1218 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy)) |
| 1219 | return CompilerType(ast, ast->UnsignedShortTy); |
| 1220 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy)) |
| 1221 | return CompilerType(ast, ast->UnsignedIntTy); |
| 1222 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy)) |
| 1223 | return CompilerType(ast, ast->UnsignedLongTy); |
| 1224 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy)) |
| 1225 | return CompilerType(ast, ast->UnsignedLongLongTy); |
| 1226 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty)) |
| 1227 | return CompilerType(ast, ast->UnsignedInt128Ty); |
| 1228 | break; |
| 1229 | |
| 1230 | case DW_ATE_unsigned_char: |
| 1231 | if (!ast->getLangOpts().CharIsSigned && type_name && |
| 1232 | streq(type_name, "char")) { |
| 1233 | if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy)) |
| 1234 | return CompilerType(ast, ast->CharTy); |
| 1235 | } |
| 1236 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy)) |
| 1237 | return CompilerType(ast, ast->UnsignedCharTy); |
| 1238 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy)) |
| 1239 | return CompilerType(ast, ast->UnsignedShortTy); |
| 1240 | break; |
| 1241 | |
| 1242 | case DW_ATE_imaginary_float: |
| 1243 | break; |
| 1244 | |
| 1245 | case DW_ATE_UTF: |
| 1246 | if (type_name) { |
| 1247 | if (streq(type_name, "char16_t")) { |
| 1248 | return CompilerType(ast, ast->Char16Ty); |
| 1249 | } else if (streq(type_name, "char32_t")) { |
| 1250 | return CompilerType(ast, ast->Char32Ty); |
| 1251 | } |
| 1252 | } |
| 1253 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1254 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1255 | } |
| 1256 | // This assert should fire for anything that we don't catch above so we know |
| 1257 | // to fix any issues we run into. |
| 1258 | if (type_name) { |
| 1259 | Host::SystemLog(Host::eSystemLogError, "error: need to add support for " |
| 1260 | "DW_TAG_base_type '%s' encoded with " |
| 1261 | "DW_ATE = 0x%x, bit_size = %u\n", |
| 1262 | type_name, dw_ate, bit_size); |
| 1263 | } else { |
| 1264 | Host::SystemLog(Host::eSystemLogError, "error: need to add support for " |
| 1265 | "DW_TAG_base_type encoded with " |
| 1266 | "DW_ATE = 0x%x, bit_size = %u\n", |
| 1267 | dw_ate, bit_size); |
| 1268 | } |
| 1269 | return CompilerType(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1272 | CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) { |
| 1273 | if (ast) |
| 1274 | return CompilerType(ast, ast->UnknownAnyTy); |
| 1275 | return CompilerType(); |
Sean Callanan | 7750226 | 2011-05-12 23:54:16 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1278 | CompilerType ClangASTContext::GetCStringType(bool is_const) { |
| 1279 | ASTContext *ast = getASTContext(); |
| 1280 | QualType char_type(ast->CharTy); |
| 1281 | |
| 1282 | if (is_const) |
| 1283 | char_type.addConst(); |
| 1284 | |
| 1285 | return CompilerType(ast, ast->getPointerType(char_type)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Sean Callanan | 09ab4b7 | 2011-11-30 22:11:59 +0000 | [diff] [blame] | 1288 | clang::DeclContext * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1289 | ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) { |
| 1290 | return ast->getTranslationUnitDecl(); |
Sean Callanan | 09ab4b7 | 2011-11-30 22:11:59 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1293 | clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast, |
| 1294 | clang::Decl *source_decl) { |
| 1295 | FileSystemOptions file_system_options; |
| 1296 | FileManager file_manager(file_system_options); |
| 1297 | ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false); |
| 1298 | |
| 1299 | return importer.Import(source_decl); |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1302 | bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2, |
| 1303 | bool ignore_qualifiers) { |
| 1304 | ClangASTContext *ast = |
| 1305 | llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem()); |
| 1306 | if (!ast || ast != type2.GetTypeSystem()) |
| 1307 | return false; |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1308 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1309 | if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType()) |
| 1310 | return true; |
Greg Clayton | 55995eb | 2012-04-06 17:38:55 +0000 | [diff] [blame] | 1311 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1312 | QualType type1_qual = ClangUtil::GetQualType(type1); |
| 1313 | QualType type2_qual = ClangUtil::GetQualType(type2); |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 1314 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1315 | if (ignore_qualifiers) { |
| 1316 | type1_qual = type1_qual.getUnqualifiedType(); |
| 1317 | type2_qual = type2_qual.getUnqualifiedType(); |
| 1318 | } |
| 1319 | |
| 1320 | return ast->getASTContext()->hasSameType(type1_qual, type2_qual); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1323 | CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) { |
| 1324 | if (clang::ObjCInterfaceDecl *interface_decl = |
| 1325 | llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) |
| 1326 | return GetTypeForDecl(interface_decl); |
| 1327 | if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) |
| 1328 | return GetTypeForDecl(tag_decl); |
| 1329 | return CompilerType(); |
Sean Callanan | 9998acd | 2014-12-05 01:21:59 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1332 | CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1333 | // No need to call the getASTContext() accessor (which can create the AST if |
| 1334 | // it isn't created yet, because we can't have created a decl in this |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1335 | // AST if our AST didn't already exist... |
| 1336 | ASTContext *ast = &decl->getASTContext(); |
| 1337 | if (ast) |
| 1338 | return CompilerType(ast, ast->getTagDeclType(decl)); |
| 1339 | return CompilerType(); |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1342 | CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1343 | // No need to call the getASTContext() accessor (which can create the AST if |
| 1344 | // it isn't created yet, because we can't have created a decl in this |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1345 | // AST if our AST didn't already exist... |
| 1346 | ASTContext *ast = &decl->getASTContext(); |
| 1347 | if (ast) |
| 1348 | return CompilerType(ast, ast->getObjCInterfaceType(decl)); |
| 1349 | return CompilerType(); |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1352 | #pragma mark Structure, Unions, Classes |
| 1353 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1354 | CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx, |
| 1355 | AccessType access_type, |
| 1356 | const char *name, int kind, |
| 1357 | LanguageType language, |
| 1358 | ClangASTMetadata *metadata) { |
| 1359 | ASTContext *ast = getASTContext(); |
| 1360 | assert(ast != nullptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1361 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1362 | if (decl_ctx == nullptr) |
| 1363 | decl_ctx = ast->getTranslationUnitDecl(); |
Greg Clayton | 9e40956 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 1364 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1365 | if (language == eLanguageTypeObjC || |
| 1366 | language == eLanguageTypeObjC_plus_plus) { |
| 1367 | bool isForwardDecl = true; |
| 1368 | bool isInternal = false; |
| 1369 | return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata); |
| 1370 | } |
Greg Clayton | 9e40956 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 1371 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1372 | // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1373 | // we will need to update this code. I was told to currently always use the |
| 1374 | // CXXRecordDecl class since we often don't know from debug information if |
| 1375 | // something is struct or a class, so we default to always use the more |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1376 | // complete definition just in case. |
Greg Clayton | c4ffd66 | 2013-03-08 01:37:30 +0000 | [diff] [blame] | 1377 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1378 | bool is_anonymous = (!name) || (!name[0]); |
Greg Clayton | c4ffd66 | 2013-03-08 01:37:30 +0000 | [diff] [blame] | 1379 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1380 | CXXRecordDecl *decl = CXXRecordDecl::Create( |
| 1381 | *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(), |
| 1382 | SourceLocation(), is_anonymous ? nullptr : &ast->Idents.get(name)); |
| 1383 | |
| 1384 | if (is_anonymous) |
| 1385 | decl->setAnonymousStructOrUnion(true); |
| 1386 | |
| 1387 | if (decl) { |
| 1388 | if (metadata) |
| 1389 | SetMetadata(ast, decl, *metadata); |
| 1390 | |
| 1391 | if (access_type != eAccessNone) |
| 1392 | decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type)); |
| 1393 | |
| 1394 | if (decl_ctx) |
| 1395 | decl_ctx->addDecl(decl); |
| 1396 | |
| 1397 | return CompilerType(ast, ast->getTagDeclType(decl)); |
| 1398 | } |
| 1399 | return CompilerType(); |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1402 | namespace { |
| 1403 | bool IsValueParam(const clang::TemplateArgument &argument) { |
| 1404 | return argument.getKind() == TemplateArgument::Integral; |
| 1405 | } |
| 1406 | } |
| 1407 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1408 | static TemplateParameterList *CreateTemplateParameterList( |
| 1409 | ASTContext *ast, |
| 1410 | const ClangASTContext::TemplateParameterInfos &template_param_infos, |
| 1411 | llvm::SmallVector<NamedDecl *, 8> &template_param_decls) { |
| 1412 | const bool parameter_pack = false; |
| 1413 | const bool is_typename = false; |
| 1414 | const unsigned depth = 0; |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1415 | const size_t num_template_params = template_param_infos.args.size(); |
| 1416 | DeclContext *const decl_context = |
| 1417 | ast->getTranslationUnitDecl(); // Is this the right decl context?, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1418 | for (size_t i = 0; i < num_template_params; ++i) { |
| 1419 | const char *name = template_param_infos.names[i]; |
Greg Clayton | 3c2e3ae | 2012-02-06 06:42:51 +0000 | [diff] [blame] | 1420 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1421 | IdentifierInfo *identifier_info = nullptr; |
| 1422 | if (name && name[0]) |
| 1423 | identifier_info = &ast->Idents.get(name); |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1424 | if (IsValueParam(template_param_infos.args[i])) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1425 | template_param_decls.push_back(NonTypeTemplateParmDecl::Create( |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1426 | *ast, decl_context, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1427 | SourceLocation(), SourceLocation(), depth, i, identifier_info, |
| 1428 | template_param_infos.args[i].getIntegralType(), parameter_pack, |
| 1429 | nullptr)); |
| 1430 | |
| 1431 | } else { |
| 1432 | template_param_decls.push_back(TemplateTypeParmDecl::Create( |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1433 | *ast, decl_context, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1434 | SourceLocation(), SourceLocation(), depth, i, identifier_info, |
| 1435 | is_typename, parameter_pack)); |
| 1436 | } |
| 1437 | } |
Eugene Zemtsov | a9d928c | 2017-09-29 03:15:08 +0000 | [diff] [blame] | 1438 | |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1439 | if (template_param_infos.packed_args && |
| 1440 | template_param_infos.packed_args->args.size()) { |
| 1441 | IdentifierInfo *identifier_info = nullptr; |
| 1442 | if (template_param_infos.pack_name && template_param_infos.pack_name[0]) |
| 1443 | identifier_info = &ast->Idents.get(template_param_infos.pack_name); |
| 1444 | const bool parameter_pack_true = true; |
| 1445 | if (IsValueParam(template_param_infos.packed_args->args[0])) { |
| 1446 | template_param_decls.push_back(NonTypeTemplateParmDecl::Create( |
| 1447 | *ast, decl_context, |
| 1448 | SourceLocation(), SourceLocation(), depth, num_template_params, |
| 1449 | identifier_info, |
| 1450 | template_param_infos.packed_args->args[0].getIntegralType(), |
| 1451 | parameter_pack_true, nullptr)); |
| 1452 | } else { |
| 1453 | template_param_decls.push_back(TemplateTypeParmDecl::Create( |
| 1454 | *ast, decl_context, |
| 1455 | SourceLocation(), SourceLocation(), depth, num_template_params, |
| 1456 | identifier_info, |
| 1457 | is_typename, parameter_pack_true)); |
| 1458 | } |
| 1459 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1460 | clang::Expr *const requires_clause = nullptr; // TODO: Concepts |
| 1461 | TemplateParameterList *template_param_list = TemplateParameterList::Create( |
| 1462 | *ast, SourceLocation(), SourceLocation(), template_param_decls, |
| 1463 | SourceLocation(), requires_clause); |
| 1464 | return template_param_list; |
Greg Clayton | 3c2e3ae | 2012-02-06 06:42:51 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1467 | clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl( |
| 1468 | clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl, |
| 1469 | const char *name, const TemplateParameterInfos &template_param_infos) { |
Adrian Prantl | d8f460e | 2018-05-02 16:55:16 +0000 | [diff] [blame] | 1470 | // /// Create a function template node. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1471 | ASTContext *ast = getASTContext(); |
Greg Clayton | 3c2e3ae | 2012-02-06 06:42:51 +0000 | [diff] [blame] | 1472 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1473 | llvm::SmallVector<NamedDecl *, 8> template_param_decls; |
Greg Clayton | 3c2e3ae | 2012-02-06 06:42:51 +0000 | [diff] [blame] | 1474 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1475 | TemplateParameterList *template_param_list = CreateTemplateParameterList( |
| 1476 | ast, template_param_infos, template_param_decls); |
| 1477 | FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create( |
| 1478 | *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(), |
| 1479 | template_param_list, func_decl); |
| 1480 | |
| 1481 | for (size_t i = 0, template_param_decl_count = template_param_decls.size(); |
| 1482 | i < template_param_decl_count; ++i) { |
| 1483 | // TODO: verify which decl context we should put template_param_decls into.. |
| 1484 | template_param_decls[i]->setDeclContext(func_decl); |
| 1485 | } |
| 1486 | |
| 1487 | return func_tmpl_decl; |
Greg Clayton | 3c2e3ae | 2012-02-06 06:42:51 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1490 | void ClangASTContext::CreateFunctionTemplateSpecializationInfo( |
| 1491 | FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl, |
| 1492 | const TemplateParameterInfos &infos) { |
| 1493 | TemplateArgumentList template_args(TemplateArgumentList::OnStack, infos.args); |
Greg Clayton | 3c2e3ae | 2012-02-06 06:42:51 +0000 | [diff] [blame] | 1494 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1495 | func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, &template_args, |
| 1496 | nullptr); |
Greg Clayton | 3c2e3ae | 2012-02-06 06:42:51 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1499 | ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl( |
| 1500 | DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name, |
| 1501 | int kind, const TemplateParameterInfos &template_param_infos) { |
| 1502 | ASTContext *ast = getASTContext(); |
Greg Clayton | 3c2e3ae | 2012-02-06 06:42:51 +0000 | [diff] [blame] | 1503 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1504 | ClassTemplateDecl *class_template_decl = nullptr; |
| 1505 | if (decl_ctx == nullptr) |
| 1506 | decl_ctx = ast->getTranslationUnitDecl(); |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1507 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1508 | IdentifierInfo &identifier_info = ast->Idents.get(class_name); |
| 1509 | DeclarationName decl_name(&identifier_info); |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1510 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1511 | clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name); |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1512 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1513 | for (NamedDecl *decl : result) { |
| 1514 | class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl); |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1515 | if (class_template_decl) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1516 | return class_template_decl; |
| 1517 | } |
| 1518 | |
| 1519 | llvm::SmallVector<NamedDecl *, 8> template_param_decls; |
| 1520 | |
| 1521 | TemplateParameterList *template_param_list = CreateTemplateParameterList( |
| 1522 | ast, template_param_infos, template_param_decls); |
| 1523 | |
| 1524 | CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create( |
| 1525 | *ast, (TagDecl::TagKind)kind, |
| 1526 | decl_ctx, // What decl context do we use here? TU? The actual decl |
| 1527 | // context? |
| 1528 | SourceLocation(), SourceLocation(), &identifier_info); |
| 1529 | |
| 1530 | for (size_t i = 0, template_param_decl_count = template_param_decls.size(); |
| 1531 | i < template_param_decl_count; ++i) { |
| 1532 | template_param_decls[i]->setDeclContext(template_cxx_decl); |
| 1533 | } |
| 1534 | |
| 1535 | // With templated classes, we say that a class is templated with |
| 1536 | // specializations, but that the bare class has no functions. |
| 1537 | // template_cxx_decl->startDefinition(); |
| 1538 | // template_cxx_decl->completeDefinition(); |
| 1539 | |
| 1540 | class_template_decl = ClassTemplateDecl::Create( |
| 1541 | *ast, |
| 1542 | decl_ctx, // What decl context do we use here? TU? The actual decl |
| 1543 | // context? |
Pavel Labath | 4294de3 | 2017-01-12 10:44:16 +0000 | [diff] [blame] | 1544 | SourceLocation(), decl_name, template_param_list, template_cxx_decl); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1545 | |
| 1546 | if (class_template_decl) { |
| 1547 | if (access_type != eAccessNone) |
| 1548 | class_template_decl->setAccess( |
| 1549 | ConvertAccessTypeToAccessSpecifier(access_type)); |
| 1550 | |
| 1551 | // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx)) |
| 1552 | // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl)); |
| 1553 | |
| 1554 | decl_ctx->addDecl(class_template_decl); |
| 1555 | |
Sean Callanan | 5e9e199 | 2011-10-26 01:06:27 +0000 | [diff] [blame] | 1556 | #ifdef LLDB_CONFIGURATION_DEBUG |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1557 | VerifyDecl(class_template_decl); |
Sean Callanan | 5e9e199 | 2011-10-26 01:06:27 +0000 | [diff] [blame] | 1558 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1559 | } |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1560 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1561 | return class_template_decl; |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
Frederic Riss | f4e7e52 | 2018-04-02 16:18:32 +0000 | [diff] [blame] | 1564 | TemplateTemplateParmDecl * |
| 1565 | ClangASTContext::CreateTemplateTemplateParmDecl(const char *template_name) { |
| 1566 | ASTContext *ast = getASTContext(); |
| 1567 | |
| 1568 | auto *decl_ctx = ast->getTranslationUnitDecl(); |
| 1569 | |
| 1570 | IdentifierInfo &identifier_info = ast->Idents.get(template_name); |
| 1571 | llvm::SmallVector<NamedDecl *, 8> template_param_decls; |
| 1572 | |
| 1573 | ClangASTContext::TemplateParameterInfos template_param_infos; |
| 1574 | TemplateParameterList *template_param_list = CreateTemplateParameterList( |
| 1575 | ast, template_param_infos, template_param_decls); |
| 1576 | |
| 1577 | // LLDB needs to create those decls only to be able to display a |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1578 | // type that includes a template template argument. Only the name matters for |
| 1579 | // this purpose, so we use dummy values for the other characterisitcs of the |
| 1580 | // type. |
Frederic Riss | f4e7e52 | 2018-04-02 16:18:32 +0000 | [diff] [blame] | 1581 | return TemplateTemplateParmDecl::Create( |
| 1582 | *ast, decl_ctx, SourceLocation(), |
| 1583 | /*Depth*/ 0, /*Position*/ 0, |
| 1584 | /*IsParameterPack*/ false, &identifier_info, template_param_list); |
| 1585 | } |
| 1586 | |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1587 | ClassTemplateSpecializationDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1588 | ClangASTContext::CreateClassTemplateSpecializationDecl( |
| 1589 | DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind, |
| 1590 | const TemplateParameterInfos &template_param_infos) { |
| 1591 | ASTContext *ast = getASTContext(); |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1592 | llvm::SmallVector<clang::TemplateArgument, 2> args( |
| 1593 | template_param_infos.args.size() + |
| 1594 | (template_param_infos.packed_args ? 1 : 0)); |
| 1595 | std::copy(template_param_infos.args.begin(), template_param_infos.args.end(), |
| 1596 | args.begin()); |
| 1597 | if (template_param_infos.packed_args) { |
| 1598 | args[args.size() - 1] = TemplateArgument::CreatePackCopy( |
| 1599 | *ast, template_param_infos.packed_args->args); |
| 1600 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1601 | ClassTemplateSpecializationDecl *class_template_specialization_decl = |
| 1602 | ClassTemplateSpecializationDecl::Create( |
| 1603 | *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(), |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1604 | SourceLocation(), class_template_decl, args, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1605 | nullptr); |
| 1606 | |
| 1607 | class_template_specialization_decl->setSpecializationKind( |
| 1608 | TSK_ExplicitSpecialization); |
| 1609 | |
| 1610 | return class_template_specialization_decl; |
| 1611 | } |
| 1612 | |
| 1613 | CompilerType ClangASTContext::CreateClassTemplateSpecializationType( |
| 1614 | ClassTemplateSpecializationDecl *class_template_specialization_decl) { |
| 1615 | if (class_template_specialization_decl) { |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1616 | ASTContext *ast = getASTContext(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1617 | if (ast) |
| 1618 | return CompilerType( |
| 1619 | ast, ast->getTagDeclType(class_template_specialization_decl)); |
| 1620 | } |
| 1621 | return CompilerType(); |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1624 | static inline bool check_op_param(bool is_method, |
| 1625 | clang::OverloadedOperatorKind op_kind, |
| 1626 | bool unary, bool binary, |
| 1627 | uint32_t num_params) { |
| 1628 | // Special-case call since it can take any number of operands |
| 1629 | if (op_kind == OO_Call) |
| 1630 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1631 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1632 | // The parameter count doesn't include "this" |
| 1633 | if (is_method) |
| 1634 | ++num_params; |
| 1635 | if (num_params == 1) |
| 1636 | return unary; |
| 1637 | if (num_params == 2) |
| 1638 | return binary; |
| 1639 | else |
Greg Clayton | 090d098 | 2011-06-19 03:43:27 +0000 | [diff] [blame] | 1640 | return false; |
| 1641 | } |
Daniel Dunbar | dacdfb5 | 2011-10-31 22:50:57 +0000 | [diff] [blame] | 1642 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1643 | bool ClangASTContext::CheckOverloadedOperatorKindParameterCount( |
| 1644 | bool is_method, clang::OverloadedOperatorKind op_kind, |
| 1645 | uint32_t num_params) { |
| 1646 | switch (op_kind) { |
| 1647 | default: |
| 1648 | break; |
| 1649 | // C++ standard allows any number of arguments to new/delete |
| 1650 | case OO_New: |
| 1651 | case OO_Array_New: |
| 1652 | case OO_Delete: |
| 1653 | case OO_Array_Delete: |
| 1654 | return true; |
| 1655 | } |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 1656 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1657 | #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \ |
| 1658 | case OO_##Name: \ |
| 1659 | return check_op_param(is_method, op_kind, Unary, Binary, num_params); |
| 1660 | switch (op_kind) { |
Greg Clayton | 090d098 | 2011-06-19 03:43:27 +0000 | [diff] [blame] | 1661 | #include "clang/Basic/OperatorKinds.def" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1662 | default: |
| 1663 | break; |
| 1664 | } |
| 1665 | return false; |
Greg Clayton | 090d098 | 2011-06-19 03:43:27 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1668 | clang::AccessSpecifier |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1669 | ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs, |
| 1670 | clang::AccessSpecifier rhs) { |
| 1671 | // Make the access equal to the stricter of the field and the nested field's |
| 1672 | // access |
| 1673 | if (lhs == AS_none || rhs == AS_none) |
| 1674 | return AS_none; |
| 1675 | if (lhs == AS_private || rhs == AS_private) |
| 1676 | return AS_private; |
| 1677 | if (lhs == AS_protected || rhs == AS_protected) |
| 1678 | return AS_protected; |
| 1679 | return AS_public; |
Sean Callanan | e8c0cfb | 2012-03-02 01:03:45 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1682 | bool ClangASTContext::FieldIsBitfield(FieldDecl *field, |
| 1683 | uint32_t &bitfield_bit_size) { |
| 1684 | return FieldIsBitfield(getASTContext(), field, bitfield_bit_size); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1687 | bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field, |
| 1688 | uint32_t &bitfield_bit_size) { |
| 1689 | if (ast == nullptr || field == nullptr) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1690 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1691 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1692 | if (field->isBitField()) { |
| 1693 | Expr *bit_width_expr = field->getBitWidth(); |
| 1694 | if (bit_width_expr) { |
| 1695 | llvm::APSInt bit_width_apsint; |
| 1696 | if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) { |
| 1697 | bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1698 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1699 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1700 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1701 | } |
| 1702 | return false; |
| 1703 | } |
| 1704 | |
| 1705 | bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) { |
| 1706 | if (record_decl == nullptr) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1707 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1708 | |
| 1709 | if (!record_decl->field_empty()) |
| 1710 | return true; |
| 1711 | |
| 1712 | // No fields, lets check this is a CXX record and check the base classes |
| 1713 | const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); |
| 1714 | if (cxx_record_decl) { |
| 1715 | CXXRecordDecl::base_class_const_iterator base_class, base_class_end; |
| 1716 | for (base_class = cxx_record_decl->bases_begin(), |
| 1717 | base_class_end = cxx_record_decl->bases_end(); |
| 1718 | base_class != base_class_end; ++base_class) { |
| 1719 | const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>( |
| 1720 | base_class->getType()->getAs<RecordType>()->getDecl()); |
| 1721 | if (RecordHasFields(base_class_decl)) |
| 1722 | return true; |
| 1723 | } |
| 1724 | } |
| 1725 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
Adrian Prantl | 4e8be2c | 2018-06-13 16:21:24 +0000 | [diff] [blame] | 1728 | #pragma mark Objective-C Classes |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1729 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1730 | CompilerType ClangASTContext::CreateObjCClass(const char *name, |
| 1731 | DeclContext *decl_ctx, |
| 1732 | bool isForwardDecl, |
| 1733 | bool isInternal, |
| 1734 | ClangASTMetadata *metadata) { |
| 1735 | ASTContext *ast = getASTContext(); |
| 1736 | assert(ast != nullptr); |
| 1737 | assert(name && name[0]); |
| 1738 | if (decl_ctx == nullptr) |
| 1739 | decl_ctx = ast->getTranslationUnitDecl(); |
Greg Clayton | 8cf0593 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 1740 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1741 | ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create( |
| 1742 | *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr, |
| 1743 | nullptr, SourceLocation(), |
| 1744 | /*isForwardDecl,*/ |
| 1745 | isInternal); |
| 1746 | |
| 1747 | if (decl && metadata) |
| 1748 | SetMetadata(ast, decl, *metadata); |
| 1749 | |
| 1750 | return CompilerType(ast, ast->getObjCInterfaceType(decl)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1753 | static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) { |
| 1754 | return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == |
| 1755 | false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1758 | uint32_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1759 | ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl, |
| 1760 | bool omit_empty_base_classes) { |
| 1761 | uint32_t num_bases = 0; |
| 1762 | if (cxx_record_decl) { |
| 1763 | if (omit_empty_base_classes) { |
| 1764 | CXXRecordDecl::base_class_const_iterator base_class, base_class_end; |
| 1765 | for (base_class = cxx_record_decl->bases_begin(), |
| 1766 | base_class_end = cxx_record_decl->bases_end(); |
| 1767 | base_class != base_class_end; ++base_class) { |
| 1768 | // Skip empty base classes |
| 1769 | if (omit_empty_base_classes) { |
| 1770 | if (BaseSpecifierIsEmpty(base_class)) |
| 1771 | continue; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1772 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1773 | ++num_bases; |
| 1774 | } |
| 1775 | } else |
| 1776 | num_bases = cxx_record_decl->getNumBases(); |
| 1777 | } |
| 1778 | return num_bases; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1781 | #pragma mark Namespace Declarations |
| 1782 | |
| 1783 | NamespaceDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1784 | ClangASTContext::GetUniqueNamespaceDeclaration(const char *name, |
| 1785 | DeclContext *decl_ctx) { |
| 1786 | NamespaceDecl *namespace_decl = nullptr; |
| 1787 | ASTContext *ast = getASTContext(); |
| 1788 | TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl(); |
| 1789 | if (decl_ctx == nullptr) |
| 1790 | decl_ctx = translation_unit_decl; |
Greg Clayton | 030a204 | 2011-10-14 21:34:45 +0000 | [diff] [blame] | 1791 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1792 | if (name) { |
| 1793 | IdentifierInfo &identifier_info = ast->Idents.get(name); |
| 1794 | DeclarationName decl_name(&identifier_info); |
| 1795 | clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name); |
| 1796 | for (NamedDecl *decl : result) { |
| 1797 | namespace_decl = dyn_cast<clang::NamespaceDecl>(decl); |
| 1798 | if (namespace_decl) |
| 1799 | return namespace_decl; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1800 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1801 | |
| 1802 | namespace_decl = |
| 1803 | NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(), |
| 1804 | SourceLocation(), &identifier_info, nullptr); |
| 1805 | |
| 1806 | decl_ctx->addDecl(namespace_decl); |
| 1807 | } else { |
| 1808 | if (decl_ctx == translation_unit_decl) { |
| 1809 | namespace_decl = translation_unit_decl->getAnonymousNamespace(); |
| 1810 | if (namespace_decl) |
| 1811 | return namespace_decl; |
| 1812 | |
| 1813 | namespace_decl = |
| 1814 | NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(), |
| 1815 | SourceLocation(), nullptr, nullptr); |
| 1816 | translation_unit_decl->setAnonymousNamespace(namespace_decl); |
| 1817 | translation_unit_decl->addDecl(namespace_decl); |
| 1818 | assert(namespace_decl == translation_unit_decl->getAnonymousNamespace()); |
| 1819 | } else { |
| 1820 | NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx); |
| 1821 | if (parent_namespace_decl) { |
| 1822 | namespace_decl = parent_namespace_decl->getAnonymousNamespace(); |
| 1823 | if (namespace_decl) |
| 1824 | return namespace_decl; |
| 1825 | namespace_decl = |
| 1826 | NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(), |
| 1827 | SourceLocation(), nullptr, nullptr); |
| 1828 | parent_namespace_decl->setAnonymousNamespace(namespace_decl); |
| 1829 | parent_namespace_decl->addDecl(namespace_decl); |
| 1830 | assert(namespace_decl == |
| 1831 | parent_namespace_decl->getAnonymousNamespace()); |
| 1832 | } else { |
| 1833 | // BAD!!! |
| 1834 | } |
Greg Clayton | 9d3d688 | 2011-10-31 23:51:19 +0000 | [diff] [blame] | 1835 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1836 | } |
Greg Clayton | 9d3d688 | 2011-10-31 23:51:19 +0000 | [diff] [blame] | 1837 | #ifdef LLDB_CONFIGURATION_DEBUG |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1838 | VerifyDecl(namespace_decl); |
Greg Clayton | 9d3d688 | 2011-10-31 23:51:19 +0000 | [diff] [blame] | 1839 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1840 | return namespace_decl; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1843 | NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration( |
| 1844 | clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx) { |
| 1845 | ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast); |
| 1846 | if (ast_ctx == nullptr) |
| 1847 | return nullptr; |
Siva Chandra | 03ff5c8 | 2016-02-05 19:10:04 +0000 | [diff] [blame] | 1848 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1849 | return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx); |
Siva Chandra | 03ff5c8 | 2016-02-05 19:10:04 +0000 | [diff] [blame] | 1850 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1851 | |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 1852 | clang::BlockDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1853 | ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) { |
| 1854 | if (ctx != nullptr) { |
| 1855 | clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx, |
| 1856 | clang::SourceLocation()); |
| 1857 | ctx->addDecl(decl); |
| 1858 | return decl; |
| 1859 | } |
| 1860 | return nullptr; |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1863 | clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left, |
| 1864 | clang::DeclContext *right, |
| 1865 | clang::DeclContext *root) { |
| 1866 | if (root == nullptr) |
Paul Herman | ea188fc | 2015-09-16 18:48:30 +0000 | [diff] [blame] | 1867 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1868 | |
| 1869 | std::set<clang::DeclContext *> path_left; |
| 1870 | for (clang::DeclContext *d = left; d != nullptr; d = d->getParent()) |
| 1871 | path_left.insert(d); |
| 1872 | |
| 1873 | for (clang::DeclContext *d = right; d != nullptr; d = d->getParent()) |
| 1874 | if (path_left.find(d) != path_left.end()) |
| 1875 | return d; |
| 1876 | |
| 1877 | return nullptr; |
Paul Herman | ea188fc | 2015-09-16 18:48:30 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1880 | clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration( |
| 1881 | clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) { |
| 1882 | if (decl_ctx != nullptr && ns_decl != nullptr) { |
| 1883 | clang::TranslationUnitDecl *translation_unit = |
| 1884 | (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext()); |
| 1885 | clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create( |
| 1886 | *getASTContext(), decl_ctx, clang::SourceLocation(), |
| 1887 | clang::SourceLocation(), clang::NestedNameSpecifierLoc(), |
| 1888 | clang::SourceLocation(), ns_decl, |
| 1889 | FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit)); |
| 1890 | decl_ctx->addDecl(using_decl); |
| 1891 | return using_decl; |
| 1892 | } |
| 1893 | return nullptr; |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
| 1896 | clang::UsingDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1897 | ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx, |
| 1898 | clang::NamedDecl *target) { |
| 1899 | if (current_decl_ctx != nullptr && target != nullptr) { |
| 1900 | clang::UsingDecl *using_decl = clang::UsingDecl::Create( |
| 1901 | *getASTContext(), current_decl_ctx, clang::SourceLocation(), |
| 1902 | clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false); |
| 1903 | clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create( |
| 1904 | *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl, |
| 1905 | target); |
| 1906 | using_decl->addShadowDecl(shadow_decl); |
| 1907 | current_decl_ctx->addDecl(using_decl); |
| 1908 | return using_decl; |
| 1909 | } |
| 1910 | return nullptr; |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1913 | clang::VarDecl *ClangASTContext::CreateVariableDeclaration( |
| 1914 | clang::DeclContext *decl_context, const char *name, clang::QualType type) { |
| 1915 | if (decl_context != nullptr) { |
| 1916 | clang::VarDecl *var_decl = clang::VarDecl::Create( |
| 1917 | *getASTContext(), decl_context, clang::SourceLocation(), |
| 1918 | clang::SourceLocation(), |
| 1919 | name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type, |
| 1920 | nullptr, clang::SC_None); |
| 1921 | var_decl->setAccess(clang::AS_public); |
| 1922 | decl_context->addDecl(var_decl); |
| 1923 | return var_decl; |
| 1924 | } |
| 1925 | return nullptr; |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
Zachary Turner | 9d8a97e | 2016-04-01 23:20:35 +0000 | [diff] [blame] | 1928 | lldb::opaque_compiler_type_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1929 | ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast, |
| 1930 | lldb::BasicType basic_type) { |
| 1931 | switch (basic_type) { |
| 1932 | case eBasicTypeVoid: |
| 1933 | return ast->VoidTy.getAsOpaquePtr(); |
| 1934 | case eBasicTypeChar: |
| 1935 | return ast->CharTy.getAsOpaquePtr(); |
| 1936 | case eBasicTypeSignedChar: |
| 1937 | return ast->SignedCharTy.getAsOpaquePtr(); |
| 1938 | case eBasicTypeUnsignedChar: |
| 1939 | return ast->UnsignedCharTy.getAsOpaquePtr(); |
| 1940 | case eBasicTypeWChar: |
| 1941 | return ast->getWCharType().getAsOpaquePtr(); |
| 1942 | case eBasicTypeSignedWChar: |
| 1943 | return ast->getSignedWCharType().getAsOpaquePtr(); |
| 1944 | case eBasicTypeUnsignedWChar: |
| 1945 | return ast->getUnsignedWCharType().getAsOpaquePtr(); |
| 1946 | case eBasicTypeChar16: |
| 1947 | return ast->Char16Ty.getAsOpaquePtr(); |
| 1948 | case eBasicTypeChar32: |
| 1949 | return ast->Char32Ty.getAsOpaquePtr(); |
| 1950 | case eBasicTypeShort: |
| 1951 | return ast->ShortTy.getAsOpaquePtr(); |
| 1952 | case eBasicTypeUnsignedShort: |
| 1953 | return ast->UnsignedShortTy.getAsOpaquePtr(); |
| 1954 | case eBasicTypeInt: |
| 1955 | return ast->IntTy.getAsOpaquePtr(); |
| 1956 | case eBasicTypeUnsignedInt: |
| 1957 | return ast->UnsignedIntTy.getAsOpaquePtr(); |
| 1958 | case eBasicTypeLong: |
| 1959 | return ast->LongTy.getAsOpaquePtr(); |
| 1960 | case eBasicTypeUnsignedLong: |
| 1961 | return ast->UnsignedLongTy.getAsOpaquePtr(); |
| 1962 | case eBasicTypeLongLong: |
| 1963 | return ast->LongLongTy.getAsOpaquePtr(); |
| 1964 | case eBasicTypeUnsignedLongLong: |
| 1965 | return ast->UnsignedLongLongTy.getAsOpaquePtr(); |
| 1966 | case eBasicTypeInt128: |
| 1967 | return ast->Int128Ty.getAsOpaquePtr(); |
| 1968 | case eBasicTypeUnsignedInt128: |
| 1969 | return ast->UnsignedInt128Ty.getAsOpaquePtr(); |
| 1970 | case eBasicTypeBool: |
| 1971 | return ast->BoolTy.getAsOpaquePtr(); |
| 1972 | case eBasicTypeHalf: |
| 1973 | return ast->HalfTy.getAsOpaquePtr(); |
| 1974 | case eBasicTypeFloat: |
| 1975 | return ast->FloatTy.getAsOpaquePtr(); |
| 1976 | case eBasicTypeDouble: |
| 1977 | return ast->DoubleTy.getAsOpaquePtr(); |
| 1978 | case eBasicTypeLongDouble: |
| 1979 | return ast->LongDoubleTy.getAsOpaquePtr(); |
| 1980 | case eBasicTypeFloatComplex: |
| 1981 | return ast->FloatComplexTy.getAsOpaquePtr(); |
| 1982 | case eBasicTypeDoubleComplex: |
| 1983 | return ast->DoubleComplexTy.getAsOpaquePtr(); |
| 1984 | case eBasicTypeLongDoubleComplex: |
| 1985 | return ast->LongDoubleComplexTy.getAsOpaquePtr(); |
| 1986 | case eBasicTypeObjCID: |
| 1987 | return ast->getObjCIdType().getAsOpaquePtr(); |
| 1988 | case eBasicTypeObjCClass: |
| 1989 | return ast->getObjCClassType().getAsOpaquePtr(); |
| 1990 | case eBasicTypeObjCSel: |
| 1991 | return ast->getObjCSelType().getAsOpaquePtr(); |
| 1992 | case eBasicTypeNullPtr: |
| 1993 | return ast->NullPtrTy.getAsOpaquePtr(); |
| 1994 | default: |
| 1995 | return nullptr; |
| 1996 | } |
Zachary Turner | 9d8a97e | 2016-04-01 23:20:35 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1999 | #pragma mark Function Types |
| 2000 | |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 2001 | clang::DeclarationName |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2002 | ClangASTContext::GetDeclarationName(const char *name, |
| 2003 | const CompilerType &function_clang_type) { |
| 2004 | if (!name || !name[0]) |
| 2005 | return clang::DeclarationName(); |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 2006 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2007 | clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS; |
| 2008 | if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS) |
| 2009 | return DeclarationName(&getASTContext()->Idents.get( |
| 2010 | name)); // Not operator, but a regular function. |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 2011 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2012 | // Check the number of operator parameters. Sometimes we have seen bad DWARF |
| 2013 | // that doesn't correctly describe operators and if we try to create a method |
| 2014 | // and add it to the class, clang will assert and crash, so we need to make |
| 2015 | // sure things are acceptable. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2016 | clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type)); |
| 2017 | const clang::FunctionProtoType *function_type = |
| 2018 | llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr()); |
| 2019 | if (function_type == nullptr) |
| 2020 | return clang::DeclarationName(); |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 2021 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2022 | const bool is_method = false; |
| 2023 | const unsigned int num_params = function_type->getNumParams(); |
| 2024 | if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount( |
| 2025 | is_method, op_kind, num_params)) |
| 2026 | return clang::DeclarationName(); |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 2027 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2028 | return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind); |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2031 | FunctionDecl *ClangASTContext::CreateFunctionDeclaration( |
| 2032 | DeclContext *decl_ctx, const char *name, |
| 2033 | const CompilerType &function_clang_type, int storage, bool is_inline) { |
| 2034 | FunctionDecl *func_decl = nullptr; |
| 2035 | ASTContext *ast = getASTContext(); |
| 2036 | if (decl_ctx == nullptr) |
| 2037 | decl_ctx = ast->getTranslationUnitDecl(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2038 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2039 | const bool hasWrittenPrototype = true; |
| 2040 | const bool isConstexprSpecified = false; |
Greg Clayton | 0d55104 | 2013-06-28 21:08:47 +0000 | [diff] [blame] | 2041 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2042 | clang::DeclarationName declarationName = |
| 2043 | GetDeclarationName(name, function_clang_type); |
| 2044 | func_decl = FunctionDecl::Create( |
| 2045 | *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName, |
| 2046 | ClangUtil::GetQualType(function_clang_type), nullptr, |
| 2047 | (clang::StorageClass)storage, is_inline, hasWrittenPrototype, |
| 2048 | isConstexprSpecified); |
| 2049 | if (func_decl) |
| 2050 | decl_ctx->addDecl(func_decl); |
| 2051 | |
Sean Callanan | 5e9e199 | 2011-10-26 01:06:27 +0000 | [diff] [blame] | 2052 | #ifdef LLDB_CONFIGURATION_DEBUG |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2053 | VerifyDecl(func_decl); |
Sean Callanan | 5e9e199 | 2011-10-26 01:06:27 +0000 | [diff] [blame] | 2054 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2055 | |
| 2056 | return func_decl; |
| 2057 | } |
| 2058 | |
| 2059 | CompilerType ClangASTContext::CreateFunctionType( |
| 2060 | ASTContext *ast, const CompilerType &result_type, const CompilerType *args, |
| 2061 | unsigned num_args, bool is_variadic, unsigned type_quals) { |
| 2062 | if (ast == nullptr) |
| 2063 | return CompilerType(); // invalid AST |
| 2064 | |
| 2065 | if (!result_type || !ClangUtil::IsClangType(result_type)) |
| 2066 | return CompilerType(); // invalid return type |
| 2067 | |
| 2068 | std::vector<QualType> qual_type_args; |
| 2069 | if (num_args > 0 && args == nullptr) |
| 2070 | return CompilerType(); // invalid argument array passed in |
| 2071 | |
| 2072 | // Verify that all arguments are valid and the right type |
| 2073 | for (unsigned i = 0; i < num_args; ++i) { |
| 2074 | if (args[i]) { |
| 2075 | // Make sure we have a clang type in args[i] and not a type from another |
| 2076 | // language whose name might match |
| 2077 | const bool is_clang_type = ClangUtil::IsClangType(args[i]); |
| 2078 | lldbassert(is_clang_type); |
| 2079 | if (is_clang_type) |
| 2080 | qual_type_args.push_back(ClangUtil::GetQualType(args[i])); |
| 2081 | else |
| 2082 | return CompilerType(); // invalid argument type (must be a clang type) |
| 2083 | } else |
| 2084 | return CompilerType(); // invalid argument type (empty) |
| 2085 | } |
| 2086 | |
| 2087 | // TODO: Detect calling convention in DWARF? |
| 2088 | FunctionProtoType::ExtProtoInfo proto_info; |
| 2089 | proto_info.Variadic = is_variadic; |
| 2090 | proto_info.ExceptionSpec = EST_None; |
| 2091 | proto_info.TypeQuals = type_quals; |
| 2092 | proto_info.RefQualifier = RQ_None; |
| 2093 | |
| 2094 | return CompilerType(ast, |
| 2095 | ast->getFunctionType(ClangUtil::GetQualType(result_type), |
| 2096 | qual_type_args, proto_info)); |
| 2097 | } |
| 2098 | |
| 2099 | ParmVarDecl *ClangASTContext::CreateParameterDeclaration( |
| 2100 | const char *name, const CompilerType ¶m_type, int storage) { |
| 2101 | ASTContext *ast = getASTContext(); |
| 2102 | assert(ast != nullptr); |
| 2103 | return ParmVarDecl::Create(*ast, ast->getTranslationUnitDecl(), |
| 2104 | SourceLocation(), SourceLocation(), |
| 2105 | name && name[0] ? &ast->Idents.get(name) : nullptr, |
| 2106 | ClangUtil::GetQualType(param_type), nullptr, |
| 2107 | (clang::StorageClass)storage, nullptr); |
| 2108 | } |
| 2109 | |
| 2110 | void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl, |
| 2111 | ParmVarDecl **params, |
| 2112 | unsigned num_params) { |
| 2113 | if (function_decl) |
| 2114 | function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2115 | } |
| 2116 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 2117 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2118 | ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) { |
| 2119 | QualType block_type = m_ast_ap->getBlockPointerType( |
| 2120 | clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType())); |
Greg Clayton | ceeb521 | 2016-05-26 22:33:25 +0000 | [diff] [blame] | 2121 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2122 | return CompilerType(this, block_type.getAsOpaquePtr()); |
Sean Callanan | c530ba9 | 2016-05-02 21:15:31 +0000 | [diff] [blame] | 2123 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2124 | |
| 2125 | #pragma mark Array Types |
| 2126 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2127 | CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type, |
| 2128 | size_t element_count, |
| 2129 | bool is_vector) { |
| 2130 | if (element_type.IsValid()) { |
| 2131 | ASTContext *ast = getASTContext(); |
| 2132 | assert(ast != nullptr); |
Greg Clayton | 4ef877f | 2012-12-06 02:33:54 +0000 | [diff] [blame] | 2133 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2134 | if (is_vector) { |
| 2135 | return CompilerType( |
| 2136 | ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type), |
| 2137 | element_count)); |
| 2138 | } else { |
| 2139 | |
| 2140 | llvm::APInt ap_element_count(64, element_count); |
| 2141 | if (element_count == 0) { |
| 2142 | return CompilerType(ast, ast->getIncompleteArrayType( |
| 2143 | ClangUtil::GetQualType(element_type), |
| 2144 | clang::ArrayType::Normal, 0)); |
| 2145 | } else { |
| 2146 | return CompilerType( |
| 2147 | ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type), |
| 2148 | ap_element_count, |
| 2149 | clang::ArrayType::Normal, 0)); |
| 2150 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2151 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2152 | } |
| 2153 | return CompilerType(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2156 | CompilerType ClangASTContext::CreateStructForIdentifier( |
| 2157 | const ConstString &type_name, |
| 2158 | const std::initializer_list<std::pair<const char *, CompilerType>> |
| 2159 | &type_fields, |
| 2160 | bool packed) { |
| 2161 | CompilerType type; |
| 2162 | if (!type_name.IsEmpty() && |
| 2163 | (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)) |
| 2164 | .IsValid()) { |
Pavel Labath | f31c9d2 | 2017-01-05 13:18:42 +0000 | [diff] [blame] | 2165 | lldbassert(0 && "Trying to create a type for an existing name"); |
Enrico Granata | 76b08d5 | 2014-10-29 23:08:02 +0000 | [diff] [blame] | 2166 | return type; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(), |
| 2170 | clang::TTK_Struct, lldb::eLanguageTypeC); |
| 2171 | StartTagDeclarationDefinition(type); |
| 2172 | for (const auto &field : type_fields) |
| 2173 | AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic, |
| 2174 | 0); |
| 2175 | if (packed) |
| 2176 | SetIsPacked(type); |
| 2177 | CompleteTagDeclarationDefinition(type); |
| 2178 | return type; |
Enrico Granata | 76b08d5 | 2014-10-29 23:08:02 +0000 | [diff] [blame] | 2179 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2180 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2181 | CompilerType ClangASTContext::GetOrCreateStructForIdentifier( |
| 2182 | const ConstString &type_name, |
| 2183 | const std::initializer_list<std::pair<const char *, CompilerType>> |
| 2184 | &type_fields, |
| 2185 | bool packed) { |
| 2186 | CompilerType type; |
| 2187 | if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid()) |
| 2188 | return type; |
Sean Callanan | c530ba9 | 2016-05-02 21:15:31 +0000 | [diff] [blame] | 2189 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2190 | return CreateStructForIdentifier(type_name, type_fields, packed); |
Sean Callanan | c530ba9 | 2016-05-02 21:15:31 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2193 | #pragma mark Enumeration Types |
| 2194 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 2195 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2196 | ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx, |
| 2197 | const Declaration &decl, |
Tamas Berghammer | 5976583 | 2017-11-07 10:39:22 +0000 | [diff] [blame] | 2198 | const CompilerType &integer_clang_type, |
| 2199 | bool is_scoped) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2200 | // TODO: Do something intelligent with the Declaration object passed in |
| 2201 | // like maybe filling in the SourceLocation with it... |
| 2202 | ASTContext *ast = getASTContext(); |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 2203 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2204 | // TODO: ask about these... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2205 | // const bool IsFixed = false; |
| 2206 | |
| 2207 | EnumDecl *enum_decl = EnumDecl::Create( |
| 2208 | *ast, decl_ctx, SourceLocation(), SourceLocation(), |
| 2209 | name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr, |
Tamas Berghammer | cf6bf4c | 2017-11-07 13:43:55 +0000 | [diff] [blame] | 2210 | is_scoped, // IsScoped |
| 2211 | is_scoped, // IsScopedUsingClassTag |
| 2212 | false); // IsFixed |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2213 | |
| 2214 | if (enum_decl) { |
| 2215 | // TODO: check if we should be setting the promotion type too? |
| 2216 | enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type)); |
| 2217 | |
| 2218 | enum_decl->setAccess(AS_public); // TODO respect what's in the debug info |
| 2219 | |
| 2220 | return CompilerType(ast, ast->getTagDeclType(enum_decl)); |
| 2221 | } |
| 2222 | return CompilerType(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2225 | CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast, |
| 2226 | size_t bit_size, |
| 2227 | bool is_signed) { |
| 2228 | if (ast) { |
| 2229 | if (is_signed) { |
| 2230 | if (bit_size == ast->getTypeSize(ast->SignedCharTy)) |
| 2231 | return CompilerType(ast, ast->SignedCharTy); |
| 2232 | |
| 2233 | if (bit_size == ast->getTypeSize(ast->ShortTy)) |
| 2234 | return CompilerType(ast, ast->ShortTy); |
| 2235 | |
| 2236 | if (bit_size == ast->getTypeSize(ast->IntTy)) |
| 2237 | return CompilerType(ast, ast->IntTy); |
| 2238 | |
| 2239 | if (bit_size == ast->getTypeSize(ast->LongTy)) |
| 2240 | return CompilerType(ast, ast->LongTy); |
| 2241 | |
| 2242 | if (bit_size == ast->getTypeSize(ast->LongLongTy)) |
| 2243 | return CompilerType(ast, ast->LongLongTy); |
| 2244 | |
| 2245 | if (bit_size == ast->getTypeSize(ast->Int128Ty)) |
| 2246 | return CompilerType(ast, ast->Int128Ty); |
| 2247 | } else { |
| 2248 | if (bit_size == ast->getTypeSize(ast->UnsignedCharTy)) |
| 2249 | return CompilerType(ast, ast->UnsignedCharTy); |
| 2250 | |
| 2251 | if (bit_size == ast->getTypeSize(ast->UnsignedShortTy)) |
| 2252 | return CompilerType(ast, ast->UnsignedShortTy); |
| 2253 | |
| 2254 | if (bit_size == ast->getTypeSize(ast->UnsignedIntTy)) |
| 2255 | return CompilerType(ast, ast->UnsignedIntTy); |
| 2256 | |
| 2257 | if (bit_size == ast->getTypeSize(ast->UnsignedLongTy)) |
| 2258 | return CompilerType(ast, ast->UnsignedLongTy); |
| 2259 | |
| 2260 | if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy)) |
| 2261 | return CompilerType(ast, ast->UnsignedLongLongTy); |
| 2262 | |
| 2263 | if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty)) |
| 2264 | return CompilerType(ast, ast->UnsignedInt128Ty); |
Enrico Granata | e8bf749 | 2014-08-15 23:00:02 +0000 | [diff] [blame] | 2265 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2266 | } |
| 2267 | return CompilerType(); |
Enrico Granata | e8bf749 | 2014-08-15 23:00:02 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2270 | CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast, |
| 2271 | bool is_signed) { |
| 2272 | if (ast) |
| 2273 | return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy), |
| 2274 | is_signed); |
| 2275 | return CompilerType(); |
Enrico Granata | e8bf749 | 2014-08-15 23:00:02 +0000 | [diff] [blame] | 2276 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2277 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2278 | void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) { |
| 2279 | if (decl_ctx) { |
| 2280 | DumpDeclContextHiearchy(decl_ctx->getParent()); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2281 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2282 | clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx); |
| 2283 | if (named_decl) { |
| 2284 | printf("%20s: %s\n", decl_ctx->getDeclKindName(), |
| 2285 | named_decl->getDeclName().getAsString().c_str()); |
| 2286 | } else { |
| 2287 | printf("%20s\n", decl_ctx->getDeclKindName()); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2288 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2289 | } |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2292 | void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) { |
| 2293 | if (decl == nullptr) |
| 2294 | return; |
| 2295 | DumpDeclContextHiearchy(decl->getDeclContext()); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2296 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2297 | clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl); |
| 2298 | if (record_decl) { |
| 2299 | printf("%20s: %s%s\n", decl->getDeclKindName(), |
| 2300 | record_decl->getDeclName().getAsString().c_str(), |
| 2301 | record_decl->isInjectedClassName() ? " (injected class name)" : ""); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2302 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2303 | } else { |
| 2304 | clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl); |
| 2305 | if (named_decl) { |
| 2306 | printf("%20s: %s\n", decl->getDeclKindName(), |
| 2307 | named_decl->getDeclName().getAsString().c_str()); |
| 2308 | } else { |
| 2309 | printf("%20s\n", decl->getDeclKindName()); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2310 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2311 | } |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2312 | } |
| 2313 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2314 | bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl, |
| 2315 | clang::Decl *rhs_decl) { |
| 2316 | if (lhs_decl && rhs_decl) { |
| 2317 | //---------------------------------------------------------------------- |
| 2318 | // Make sure the decl kinds match first |
| 2319 | //---------------------------------------------------------------------- |
| 2320 | const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind(); |
| 2321 | const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind(); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2322 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2323 | if (lhs_decl_kind == rhs_decl_kind) { |
| 2324 | //------------------------------------------------------------------ |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2325 | // Now check that the decl contexts kinds are all equivalent before we |
| 2326 | // have to check any names of the decl contexts... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2327 | //------------------------------------------------------------------ |
| 2328 | clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext(); |
| 2329 | clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext(); |
| 2330 | if (lhs_decl_ctx && rhs_decl_ctx) { |
| 2331 | while (1) { |
| 2332 | if (lhs_decl_ctx && rhs_decl_ctx) { |
| 2333 | const clang::Decl::Kind lhs_decl_ctx_kind = |
| 2334 | lhs_decl_ctx->getDeclKind(); |
| 2335 | const clang::Decl::Kind rhs_decl_ctx_kind = |
| 2336 | rhs_decl_ctx->getDeclKind(); |
| 2337 | if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) { |
| 2338 | lhs_decl_ctx = lhs_decl_ctx->getParent(); |
| 2339 | rhs_decl_ctx = rhs_decl_ctx->getParent(); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2340 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2341 | if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr) |
| 2342 | break; |
| 2343 | } else |
| 2344 | return false; |
| 2345 | } else |
Tamas Berghammer | fcf334b | 2015-12-02 11:35:54 +0000 | [diff] [blame] | 2346 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
| 2349 | //-------------------------------------------------------------- |
| 2350 | // Now make sure the name of the decls match |
| 2351 | //-------------------------------------------------------------- |
| 2352 | clang::NamedDecl *lhs_named_decl = |
| 2353 | llvm::dyn_cast<clang::NamedDecl>(lhs_decl); |
| 2354 | clang::NamedDecl *rhs_named_decl = |
| 2355 | llvm::dyn_cast<clang::NamedDecl>(rhs_decl); |
| 2356 | if (lhs_named_decl && rhs_named_decl) { |
| 2357 | clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName(); |
| 2358 | clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName(); |
| 2359 | if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) { |
| 2360 | if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString()) |
| 2361 | return false; |
| 2362 | } else |
Greg Clayton | a272147 | 2011-06-25 00:44:06 +0000 | [diff] [blame] | 2363 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2364 | } else |
| 2365 | return false; |
Greg Clayton | a272147 | 2011-06-25 00:44:06 +0000 | [diff] [blame] | 2366 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2367 | //-------------------------------------------------------------- |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2368 | // We know that the decl context kinds all match, so now we need to |
| 2369 | // make sure the names match as well |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2370 | //-------------------------------------------------------------- |
| 2371 | lhs_decl_ctx = lhs_decl->getDeclContext(); |
| 2372 | rhs_decl_ctx = rhs_decl->getDeclContext(); |
| 2373 | while (1) { |
| 2374 | switch (lhs_decl_ctx->getDeclKind()) { |
| 2375 | case clang::Decl::TranslationUnit: |
| 2376 | // We don't care about the translation unit names |
| 2377 | return true; |
| 2378 | default: { |
| 2379 | clang::NamedDecl *lhs_named_decl = |
| 2380 | llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx); |
| 2381 | clang::NamedDecl *rhs_named_decl = |
| 2382 | llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx); |
| 2383 | if (lhs_named_decl && rhs_named_decl) { |
| 2384 | clang::DeclarationName lhs_decl_name = |
| 2385 | lhs_named_decl->getDeclName(); |
| 2386 | clang::DeclarationName rhs_decl_name = |
| 2387 | rhs_named_decl->getDeclName(); |
| 2388 | if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) { |
| 2389 | if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString()) |
| 2390 | return false; |
| 2391 | } else |
| 2392 | return false; |
| 2393 | } else |
| 2394 | return false; |
| 2395 | } break; |
| 2396 | } |
| 2397 | lhs_decl_ctx = lhs_decl_ctx->getParent(); |
| 2398 | rhs_decl_ctx = rhs_decl_ctx->getParent(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2399 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2400 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2401 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2402 | } |
| 2403 | return false; |
| 2404 | } |
| 2405 | bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast, |
| 2406 | clang::Decl *decl) { |
| 2407 | if (!decl) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2408 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2409 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2410 | ExternalASTSource *ast_source = ast->getExternalSource(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2411 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2412 | if (!ast_source) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2413 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2414 | |
| 2415 | if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) { |
| 2416 | if (tag_decl->isCompleteDefinition()) |
| 2417 | return true; |
| 2418 | |
| 2419 | if (!tag_decl->hasExternalLexicalStorage()) |
| 2420 | return false; |
| 2421 | |
| 2422 | ast_source->CompleteType(tag_decl); |
| 2423 | |
| 2424 | return !tag_decl->getTypeForDecl()->isIncompleteType(); |
| 2425 | } else if (clang::ObjCInterfaceDecl *objc_interface_decl = |
| 2426 | llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) { |
| 2427 | if (objc_interface_decl->getDefinition()) |
| 2428 | return true; |
| 2429 | |
| 2430 | if (!objc_interface_decl->hasExternalLexicalStorage()) |
| 2431 | return false; |
| 2432 | |
| 2433 | ast_source->CompleteType(objc_interface_decl); |
| 2434 | |
| 2435 | return !objc_interface_decl->getTypeForDecl()->isIncompleteType(); |
| 2436 | } else { |
| 2437 | return false; |
| 2438 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2439 | } |
| 2440 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2441 | void ClangASTContext::SetMetadataAsUserID(const void *object, |
| 2442 | user_id_t user_id) { |
| 2443 | ClangASTMetadata meta_data; |
| 2444 | meta_data.SetUserID(user_id); |
| 2445 | SetMetadata(object, meta_data); |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2448 | void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object, |
| 2449 | ClangASTMetadata &metadata) { |
| 2450 | ClangExternalASTSourceCommon *external_source = |
| 2451 | ClangExternalASTSourceCommon::Lookup(ast->getExternalSource()); |
| 2452 | |
| 2453 | if (external_source) |
| 2454 | external_source->SetMetadata(object, metadata); |
| 2455 | } |
| 2456 | |
| 2457 | ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast, |
| 2458 | const void *object) { |
| 2459 | ClangExternalASTSourceCommon *external_source = |
| 2460 | ClangExternalASTSourceCommon::Lookup(ast->getExternalSource()); |
| 2461 | |
| 2462 | if (external_source && external_source->HasMetadata(object)) |
| 2463 | return external_source->GetMetadata(object); |
| 2464 | else |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2465 | return nullptr; |
| 2466 | } |
| 2467 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2468 | clang::DeclContext * |
| 2469 | ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) { |
| 2470 | return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl); |
| 2471 | } |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2472 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2473 | clang::DeclContext * |
| 2474 | ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) { |
| 2475 | return llvm::dyn_cast<clang::DeclContext>(objc_method_decl); |
| 2476 | } |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2477 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2478 | bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type, |
| 2479 | int kind) const { |
| 2480 | const clang::Type *clang_type = tag_qual_type.getTypePtr(); |
| 2481 | if (clang_type) { |
| 2482 | const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type); |
| 2483 | if (tag_type) { |
| 2484 | clang::TagDecl *tag_decl = |
| 2485 | llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl()); |
| 2486 | if (tag_decl) { |
| 2487 | tag_decl->setTagKind((clang::TagDecl::TagKind)kind); |
| 2488 | return true; |
| 2489 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2490 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2491 | } |
| 2492 | return false; |
| 2493 | } |
| 2494 | |
| 2495 | bool ClangASTContext::SetDefaultAccessForRecordFields( |
| 2496 | clang::RecordDecl *record_decl, int default_accessibility, |
| 2497 | int *assigned_accessibilities, size_t num_assigned_accessibilities) { |
| 2498 | if (record_decl) { |
| 2499 | uint32_t field_idx; |
| 2500 | clang::RecordDecl::field_iterator field, field_end; |
| 2501 | for (field = record_decl->field_begin(), |
| 2502 | field_end = record_decl->field_end(), field_idx = 0; |
| 2503 | field != field_end; ++field, ++field_idx) { |
| 2504 | // If no accessibility was assigned, assign the correct one |
| 2505 | if (field_idx < num_assigned_accessibilities && |
| 2506 | assigned_accessibilities[field_idx] == clang::AS_none) |
| 2507 | field->setAccess((clang::AccessSpecifier)default_accessibility); |
| 2508 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2509 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2510 | } |
| 2511 | return false; |
| 2512 | } |
| 2513 | |
| 2514 | clang::DeclContext * |
| 2515 | ClangASTContext::GetDeclContextForType(const CompilerType &type) { |
| 2516 | return GetDeclContextForType(ClangUtil::GetQualType(type)); |
| 2517 | } |
| 2518 | |
| 2519 | clang::DeclContext * |
| 2520 | ClangASTContext::GetDeclContextForType(clang::QualType type) { |
| 2521 | if (type.isNull()) |
| 2522 | return nullptr; |
| 2523 | |
| 2524 | clang::QualType qual_type = type.getCanonicalType(); |
| 2525 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 2526 | switch (type_class) { |
| 2527 | case clang::Type::ObjCInterface: |
| 2528 | return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr()) |
| 2529 | ->getInterface(); |
| 2530 | case clang::Type::ObjCObjectPointer: |
| 2531 | return GetDeclContextForType( |
| 2532 | llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr()) |
| 2533 | ->getPointeeType()); |
| 2534 | case clang::Type::Record: |
| 2535 | return llvm::cast<clang::RecordType>(qual_type)->getDecl(); |
| 2536 | case clang::Type::Enum: |
| 2537 | return llvm::cast<clang::EnumType>(qual_type)->getDecl(); |
| 2538 | case clang::Type::Typedef: |
| 2539 | return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type) |
| 2540 | ->getDecl() |
| 2541 | ->getUnderlyingType()); |
| 2542 | case clang::Type::Auto: |
| 2543 | return GetDeclContextForType( |
| 2544 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()); |
| 2545 | case clang::Type::Elaborated: |
| 2546 | return GetDeclContextForType( |
| 2547 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()); |
| 2548 | case clang::Type::Paren: |
| 2549 | return GetDeclContextForType( |
| 2550 | llvm::cast<clang::ParenType>(qual_type)->desugar()); |
| 2551 | default: |
| 2552 | break; |
| 2553 | } |
| 2554 | // No DeclContext in this type... |
| 2555 | return nullptr; |
| 2556 | } |
| 2557 | |
| 2558 | static bool GetCompleteQualType(clang::ASTContext *ast, |
| 2559 | clang::QualType qual_type, |
| 2560 | bool allow_completion = true) { |
| 2561 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 2562 | switch (type_class) { |
| 2563 | case clang::Type::ConstantArray: |
| 2564 | case clang::Type::IncompleteArray: |
| 2565 | case clang::Type::VariableArray: { |
| 2566 | const clang::ArrayType *array_type = |
| 2567 | llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr()); |
| 2568 | |
| 2569 | if (array_type) |
| 2570 | return GetCompleteQualType(ast, array_type->getElementType(), |
| 2571 | allow_completion); |
| 2572 | } break; |
| 2573 | case clang::Type::Record: { |
| 2574 | clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); |
| 2575 | if (cxx_record_decl) { |
| 2576 | if (cxx_record_decl->hasExternalLexicalStorage()) { |
| 2577 | const bool is_complete = cxx_record_decl->isCompleteDefinition(); |
| 2578 | const bool fields_loaded = |
| 2579 | cxx_record_decl->hasLoadedFieldsFromExternalStorage(); |
| 2580 | if (is_complete && fields_loaded) |
| 2581 | return true; |
| 2582 | |
| 2583 | if (!allow_completion) |
| 2584 | return false; |
| 2585 | |
| 2586 | // Call the field_begin() accessor to for it to use the external source |
| 2587 | // to load the fields... |
| 2588 | clang::ExternalASTSource *external_ast_source = |
| 2589 | ast->getExternalSource(); |
| 2590 | if (external_ast_source) { |
| 2591 | external_ast_source->CompleteType(cxx_record_decl); |
| 2592 | if (cxx_record_decl->isCompleteDefinition()) { |
| 2593 | cxx_record_decl->field_begin(); |
| 2594 | cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true); |
| 2595 | } |
| 2596 | } |
| 2597 | } |
| 2598 | } |
| 2599 | const clang::TagType *tag_type = |
| 2600 | llvm::cast<clang::TagType>(qual_type.getTypePtr()); |
| 2601 | return !tag_type->isIncompleteType(); |
| 2602 | } break; |
| 2603 | |
| 2604 | case clang::Type::Enum: { |
| 2605 | const clang::TagType *tag_type = |
| 2606 | llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()); |
| 2607 | if (tag_type) { |
| 2608 | clang::TagDecl *tag_decl = tag_type->getDecl(); |
| 2609 | if (tag_decl) { |
| 2610 | if (tag_decl->getDefinition()) |
| 2611 | return true; |
| 2612 | |
| 2613 | if (!allow_completion) |
| 2614 | return false; |
| 2615 | |
| 2616 | if (tag_decl->hasExternalLexicalStorage()) { |
| 2617 | if (ast) { |
| 2618 | clang::ExternalASTSource *external_ast_source = |
| 2619 | ast->getExternalSource(); |
| 2620 | if (external_ast_source) { |
| 2621 | external_ast_source->CompleteType(tag_decl); |
| 2622 | return !tag_type->isIncompleteType(); |
| 2623 | } |
| 2624 | } |
| 2625 | } |
| 2626 | return false; |
| 2627 | } |
| 2628 | } |
| 2629 | |
| 2630 | } break; |
| 2631 | case clang::Type::ObjCObject: |
| 2632 | case clang::Type::ObjCInterface: { |
| 2633 | const clang::ObjCObjectType *objc_class_type = |
| 2634 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type); |
| 2635 | if (objc_class_type) { |
| 2636 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 2637 | objc_class_type->getInterface(); |
| 2638 | // We currently can't complete objective C types through the newly added |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2639 | // ASTContext because it only supports TagDecl objects right now... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2640 | if (class_interface_decl) { |
| 2641 | if (class_interface_decl->getDefinition()) |
| 2642 | return true; |
| 2643 | |
| 2644 | if (!allow_completion) |
| 2645 | return false; |
| 2646 | |
| 2647 | if (class_interface_decl->hasExternalLexicalStorage()) { |
| 2648 | if (ast) { |
| 2649 | clang::ExternalASTSource *external_ast_source = |
| 2650 | ast->getExternalSource(); |
| 2651 | if (external_ast_source) { |
| 2652 | external_ast_source->CompleteType(class_interface_decl); |
| 2653 | return !objc_class_type->isIncompleteType(); |
| 2654 | } |
| 2655 | } |
| 2656 | } |
| 2657 | return false; |
| 2658 | } |
| 2659 | } |
| 2660 | } break; |
| 2661 | |
| 2662 | case clang::Type::Typedef: |
| 2663 | return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type) |
| 2664 | ->getDecl() |
| 2665 | ->getUnderlyingType(), |
| 2666 | allow_completion); |
| 2667 | |
| 2668 | case clang::Type::Auto: |
| 2669 | return GetCompleteQualType( |
| 2670 | ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(), |
| 2671 | allow_completion); |
| 2672 | |
| 2673 | case clang::Type::Elaborated: |
| 2674 | return GetCompleteQualType( |
| 2675 | ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(), |
| 2676 | allow_completion); |
| 2677 | |
| 2678 | case clang::Type::Paren: |
| 2679 | return GetCompleteQualType( |
| 2680 | ast, llvm::cast<clang::ParenType>(qual_type)->desugar(), |
| 2681 | allow_completion); |
| 2682 | |
| 2683 | case clang::Type::Attributed: |
| 2684 | return GetCompleteQualType( |
| 2685 | ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(), |
| 2686 | allow_completion); |
| 2687 | |
| 2688 | default: |
| 2689 | break; |
| 2690 | } |
| 2691 | |
| 2692 | return true; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | static clang::ObjCIvarDecl::AccessControl |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2696 | ConvertAccessTypeToObjCIvarAccessControl(AccessType access) { |
| 2697 | switch (access) { |
| 2698 | case eAccessNone: |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2699 | return clang::ObjCIvarDecl::None; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2700 | case eAccessPublic: |
| 2701 | return clang::ObjCIvarDecl::Public; |
| 2702 | case eAccessPrivate: |
| 2703 | return clang::ObjCIvarDecl::Private; |
| 2704 | case eAccessProtected: |
| 2705 | return clang::ObjCIvarDecl::Protected; |
| 2706 | case eAccessPackage: |
| 2707 | return clang::ObjCIvarDecl::Package; |
| 2708 | } |
| 2709 | return clang::ObjCIvarDecl::None; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2712 | //---------------------------------------------------------------------- |
| 2713 | // Tests |
| 2714 | //---------------------------------------------------------------------- |
| 2715 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2716 | bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) { |
| 2717 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 2718 | |
| 2719 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 2720 | switch (type_class) { |
| 2721 | case clang::Type::IncompleteArray: |
| 2722 | case clang::Type::VariableArray: |
| 2723 | case clang::Type::ConstantArray: |
| 2724 | case clang::Type::ExtVector: |
| 2725 | case clang::Type::Vector: |
| 2726 | case clang::Type::Record: |
| 2727 | case clang::Type::ObjCObject: |
| 2728 | case clang::Type::ObjCInterface: |
| 2729 | return true; |
| 2730 | case clang::Type::Auto: |
| 2731 | return IsAggregateType(llvm::cast<clang::AutoType>(qual_type) |
| 2732 | ->getDeducedType() |
| 2733 | .getAsOpaquePtr()); |
| 2734 | case clang::Type::Elaborated: |
| 2735 | return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 2736 | ->getNamedType() |
| 2737 | .getAsOpaquePtr()); |
| 2738 | case clang::Type::Typedef: |
| 2739 | return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type) |
| 2740 | ->getDecl() |
| 2741 | ->getUnderlyingType() |
| 2742 | .getAsOpaquePtr()); |
| 2743 | case clang::Type::Paren: |
| 2744 | return IsAggregateType( |
| 2745 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()); |
| 2746 | default: |
| 2747 | break; |
| 2748 | } |
| 2749 | // The clang type does have a value |
| 2750 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2751 | } |
| 2752 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2753 | bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) { |
| 2754 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 2755 | |
| 2756 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 2757 | switch (type_class) { |
| 2758 | case clang::Type::Record: { |
| 2759 | if (const clang::RecordType *record_type = |
| 2760 | llvm::dyn_cast_or_null<clang::RecordType>( |
| 2761 | qual_type.getTypePtrOrNull())) { |
| 2762 | if (const clang::RecordDecl *record_decl = record_type->getDecl()) { |
| 2763 | return record_decl->isAnonymousStructOrUnion(); |
| 2764 | } |
Enrico Granata | 7123e2b | 2015-11-07 02:06:57 +0000 | [diff] [blame] | 2765 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2766 | break; |
| 2767 | } |
| 2768 | case clang::Type::Auto: |
| 2769 | return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type) |
| 2770 | ->getDeducedType() |
| 2771 | .getAsOpaquePtr()); |
| 2772 | case clang::Type::Elaborated: |
| 2773 | return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 2774 | ->getNamedType() |
| 2775 | .getAsOpaquePtr()); |
| 2776 | case clang::Type::Typedef: |
| 2777 | return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type) |
| 2778 | ->getDecl() |
| 2779 | ->getUnderlyingType() |
| 2780 | .getAsOpaquePtr()); |
| 2781 | case clang::Type::Paren: |
| 2782 | return IsAnonymousType( |
| 2783 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()); |
| 2784 | default: |
| 2785 | break; |
| 2786 | } |
| 2787 | // The clang type does have a value |
| 2788 | return false; |
Enrico Granata | 7123e2b | 2015-11-07 02:06:57 +0000 | [diff] [blame] | 2789 | } |
| 2790 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2791 | bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type, |
| 2792 | CompilerType *element_type_ptr, |
| 2793 | uint64_t *size, bool *is_incomplete) { |
| 2794 | clang::QualType qual_type(GetCanonicalQualType(type)); |
Tamas Berghammer | 69d0b33 | 2015-10-09 12:43:08 +0000 | [diff] [blame] | 2795 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2796 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 2797 | switch (type_class) { |
| 2798 | default: |
| 2799 | break; |
Tamas Berghammer | 69d0b33 | 2015-10-09 12:43:08 +0000 | [diff] [blame] | 2800 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2801 | case clang::Type::ConstantArray: |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2802 | if (element_type_ptr) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2803 | element_type_ptr->SetCompilerType( |
| 2804 | getASTContext(), |
| 2805 | llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType()); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2806 | if (size) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2807 | *size = llvm::cast<clang::ConstantArrayType>(qual_type) |
| 2808 | ->getSize() |
| 2809 | .getLimitedValue(ULLONG_MAX); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2810 | if (is_incomplete) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2811 | *is_incomplete = false; |
| 2812 | return true; |
| 2813 | |
| 2814 | case clang::Type::IncompleteArray: |
| 2815 | if (element_type_ptr) |
| 2816 | element_type_ptr->SetCompilerType( |
| 2817 | getASTContext(), |
| 2818 | llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType()); |
| 2819 | if (size) |
| 2820 | *size = 0; |
| 2821 | if (is_incomplete) |
| 2822 | *is_incomplete = true; |
| 2823 | return true; |
| 2824 | |
| 2825 | case clang::Type::VariableArray: |
| 2826 | if (element_type_ptr) |
| 2827 | element_type_ptr->SetCompilerType( |
| 2828 | getASTContext(), |
| 2829 | llvm::cast<clang::VariableArrayType>(qual_type)->getElementType()); |
| 2830 | if (size) |
| 2831 | *size = 0; |
| 2832 | if (is_incomplete) |
| 2833 | *is_incomplete = false; |
| 2834 | return true; |
| 2835 | |
| 2836 | case clang::Type::DependentSizedArray: |
| 2837 | if (element_type_ptr) |
| 2838 | element_type_ptr->SetCompilerType( |
| 2839 | getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type) |
| 2840 | ->getElementType()); |
| 2841 | if (size) |
| 2842 | *size = 0; |
| 2843 | if (is_incomplete) |
| 2844 | *is_incomplete = false; |
| 2845 | return true; |
| 2846 | |
| 2847 | case clang::Type::Typedef: |
| 2848 | return IsArrayType(llvm::cast<clang::TypedefType>(qual_type) |
| 2849 | ->getDecl() |
| 2850 | ->getUnderlyingType() |
| 2851 | .getAsOpaquePtr(), |
| 2852 | element_type_ptr, size, is_incomplete); |
| 2853 | case clang::Type::Auto: |
| 2854 | return IsArrayType(llvm::cast<clang::AutoType>(qual_type) |
| 2855 | ->getDeducedType() |
| 2856 | .getAsOpaquePtr(), |
| 2857 | element_type_ptr, size, is_incomplete); |
| 2858 | case clang::Type::Elaborated: |
| 2859 | return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 2860 | ->getNamedType() |
| 2861 | .getAsOpaquePtr(), |
| 2862 | element_type_ptr, size, is_incomplete); |
| 2863 | case clang::Type::Paren: |
| 2864 | return IsArrayType( |
| 2865 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 2866 | element_type_ptr, size, is_incomplete); |
| 2867 | } |
| 2868 | if (element_type_ptr) |
| 2869 | element_type_ptr->Clear(); |
| 2870 | if (size) |
| 2871 | *size = 0; |
| 2872 | if (is_incomplete) |
| 2873 | *is_incomplete = false; |
| 2874 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2877 | bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type, |
| 2878 | CompilerType *element_type, uint64_t *size) { |
| 2879 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 2880 | |
| 2881 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 2882 | switch (type_class) { |
| 2883 | case clang::Type::Vector: { |
| 2884 | const clang::VectorType *vector_type = |
| 2885 | qual_type->getAs<clang::VectorType>(); |
| 2886 | if (vector_type) { |
| 2887 | if (size) |
| 2888 | *size = vector_type->getNumElements(); |
| 2889 | if (element_type) |
| 2890 | *element_type = |
| 2891 | CompilerType(getASTContext(), vector_type->getElementType()); |
| 2892 | } |
| 2893 | return true; |
| 2894 | } break; |
| 2895 | case clang::Type::ExtVector: { |
| 2896 | const clang::ExtVectorType *ext_vector_type = |
| 2897 | qual_type->getAs<clang::ExtVectorType>(); |
| 2898 | if (ext_vector_type) { |
| 2899 | if (size) |
| 2900 | *size = ext_vector_type->getNumElements(); |
| 2901 | if (element_type) |
| 2902 | *element_type = |
| 2903 | CompilerType(getASTContext(), ext_vector_type->getElementType()); |
| 2904 | } |
| 2905 | return true; |
| 2906 | } |
| 2907 | default: |
| 2908 | break; |
| 2909 | } |
| 2910 | return false; |
| 2911 | } |
| 2912 | |
| 2913 | bool ClangASTContext::IsRuntimeGeneratedType( |
| 2914 | lldb::opaque_compiler_type_t type) { |
| 2915 | clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext()) |
| 2916 | ->GetDeclContextForType(GetQualType(type)); |
| 2917 | if (!decl_ctx) |
| 2918 | return false; |
| 2919 | |
| 2920 | if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx)) |
| 2921 | return false; |
| 2922 | |
| 2923 | clang::ObjCInterfaceDecl *result_iface_decl = |
| 2924 | llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx); |
| 2925 | |
| 2926 | ClangASTMetadata *ast_metadata = |
| 2927 | ClangASTContext::GetMetadata(getASTContext(), result_iface_decl); |
| 2928 | if (!ast_metadata) |
| 2929 | return false; |
| 2930 | return (ast_metadata->GetISAPtr() != 0); |
| 2931 | } |
| 2932 | |
| 2933 | bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) { |
| 2934 | return GetQualType(type).getUnqualifiedType()->isCharType(); |
| 2935 | } |
| 2936 | |
| 2937 | bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) { |
| 2938 | const bool allow_completion = false; |
| 2939 | return GetCompleteQualType(getASTContext(), GetQualType(type), |
| 2940 | allow_completion); |
| 2941 | } |
| 2942 | |
| 2943 | bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) { |
| 2944 | return GetQualType(type).isConstQualified(); |
| 2945 | } |
| 2946 | |
| 2947 | bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type, |
| 2948 | uint32_t &length) { |
| 2949 | CompilerType pointee_or_element_clang_type; |
| 2950 | length = 0; |
| 2951 | Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type)); |
| 2952 | |
| 2953 | if (!pointee_or_element_clang_type.IsValid()) |
| 2954 | return false; |
| 2955 | |
| 2956 | if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) { |
| 2957 | if (pointee_or_element_clang_type.IsCharType()) { |
| 2958 | if (type_flags.Test(eTypeIsArray)) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2959 | // We know the size of the array and it could be a C string since it is |
| 2960 | // an array of characters |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2961 | length = llvm::cast<clang::ConstantArrayType>( |
| 2962 | GetCanonicalQualType(type).getTypePtr()) |
| 2963 | ->getSize() |
| 2964 | .getLimitedValue(); |
| 2965 | } |
| 2966 | return true; |
| 2967 | } |
| 2968 | } |
| 2969 | return false; |
| 2970 | } |
| 2971 | |
| 2972 | bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type, |
| 2973 | bool *is_variadic_ptr) { |
| 2974 | if (type) { |
| 2975 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 2976 | |
| 2977 | if (qual_type->isFunctionType()) { |
| 2978 | if (is_variadic_ptr) { |
| 2979 | const clang::FunctionProtoType *function_proto_type = |
| 2980 | llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr()); |
| 2981 | if (function_proto_type) |
| 2982 | *is_variadic_ptr = function_proto_type->isVariadic(); |
| 2983 | else |
| 2984 | *is_variadic_ptr = false; |
| 2985 | } |
| 2986 | return true; |
| 2987 | } |
| 2988 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2989 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2990 | switch (type_class) { |
| 2991 | default: |
| 2992 | break; |
| 2993 | case clang::Type::Typedef: |
| 2994 | return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type) |
| 2995 | ->getDecl() |
| 2996 | ->getUnderlyingType() |
| 2997 | .getAsOpaquePtr(), |
| 2998 | nullptr); |
| 2999 | case clang::Type::Auto: |
| 3000 | return IsFunctionType(llvm::cast<clang::AutoType>(qual_type) |
| 3001 | ->getDeducedType() |
| 3002 | .getAsOpaquePtr(), |
| 3003 | nullptr); |
| 3004 | case clang::Type::Elaborated: |
| 3005 | return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 3006 | ->getNamedType() |
| 3007 | .getAsOpaquePtr(), |
| 3008 | nullptr); |
| 3009 | case clang::Type::Paren: |
| 3010 | return IsFunctionType( |
| 3011 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 3012 | nullptr); |
| 3013 | case clang::Type::LValueReference: |
| 3014 | case clang::Type::RValueReference: { |
| 3015 | const clang::ReferenceType *reference_type = |
| 3016 | llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()); |
| 3017 | if (reference_type) |
| 3018 | return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(), |
| 3019 | nullptr); |
| 3020 | } break; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3021 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3022 | } |
| 3023 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3024 | } |
| 3025 | |
| 3026 | // Used to detect "Homogeneous Floating-point Aggregates" |
| 3027 | uint32_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3028 | ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type, |
| 3029 | CompilerType *base_type_ptr) { |
| 3030 | if (!type) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3031 | return 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3032 | |
| 3033 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3034 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3035 | switch (type_class) { |
| 3036 | case clang::Type::Record: |
| 3037 | if (GetCompleteType(type)) { |
| 3038 | const clang::CXXRecordDecl *cxx_record_decl = |
| 3039 | qual_type->getAsCXXRecordDecl(); |
| 3040 | if (cxx_record_decl) { |
| 3041 | if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass()) |
| 3042 | return 0; |
| 3043 | } |
| 3044 | const clang::RecordType *record_type = |
| 3045 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 3046 | if (record_type) { |
| 3047 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 3048 | if (record_decl) { |
| 3049 | // We are looking for a structure that contains only floating point |
| 3050 | // types |
| 3051 | clang::RecordDecl::field_iterator field_pos, |
| 3052 | field_end = record_decl->field_end(); |
| 3053 | uint32_t num_fields = 0; |
| 3054 | bool is_hva = false; |
| 3055 | bool is_hfa = false; |
| 3056 | clang::QualType base_qual_type; |
| 3057 | uint64_t base_bitwidth = 0; |
| 3058 | for (field_pos = record_decl->field_begin(); field_pos != field_end; |
| 3059 | ++field_pos) { |
| 3060 | clang::QualType field_qual_type = field_pos->getType(); |
| 3061 | uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type); |
| 3062 | if (field_qual_type->isFloatingType()) { |
| 3063 | if (field_qual_type->isComplexType()) |
| 3064 | return 0; |
| 3065 | else { |
| 3066 | if (num_fields == 0) |
| 3067 | base_qual_type = field_qual_type; |
| 3068 | else { |
| 3069 | if (is_hva) |
| 3070 | return 0; |
| 3071 | is_hfa = true; |
| 3072 | if (field_qual_type.getTypePtr() != |
| 3073 | base_qual_type.getTypePtr()) |
| 3074 | return 0; |
| 3075 | } |
| 3076 | } |
| 3077 | } else if (field_qual_type->isVectorType() || |
| 3078 | field_qual_type->isExtVectorType()) { |
| 3079 | if (num_fields == 0) { |
| 3080 | base_qual_type = field_qual_type; |
| 3081 | base_bitwidth = field_bitwidth; |
| 3082 | } else { |
| 3083 | if (is_hfa) |
| 3084 | return 0; |
| 3085 | is_hva = true; |
| 3086 | if (base_bitwidth != field_bitwidth) |
| 3087 | return 0; |
| 3088 | if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr()) |
| 3089 | return 0; |
| 3090 | } |
| 3091 | } else |
| 3092 | return 0; |
| 3093 | ++num_fields; |
| 3094 | } |
| 3095 | if (base_type_ptr) |
| 3096 | *base_type_ptr = CompilerType(getASTContext(), base_qual_type); |
| 3097 | return num_fields; |
| 3098 | } |
| 3099 | } |
| 3100 | } |
| 3101 | break; |
| 3102 | |
| 3103 | case clang::Type::Typedef: |
| 3104 | return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type) |
| 3105 | ->getDecl() |
| 3106 | ->getUnderlyingType() |
| 3107 | .getAsOpaquePtr(), |
| 3108 | base_type_ptr); |
| 3109 | |
| 3110 | case clang::Type::Auto: |
| 3111 | return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type) |
| 3112 | ->getDeducedType() |
| 3113 | .getAsOpaquePtr(), |
| 3114 | base_type_ptr); |
| 3115 | |
| 3116 | case clang::Type::Elaborated: |
| 3117 | return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type) |
| 3118 | ->getNamedType() |
| 3119 | .getAsOpaquePtr(), |
| 3120 | base_type_ptr); |
| 3121 | default: |
| 3122 | break; |
| 3123 | } |
| 3124 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3125 | } |
| 3126 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3127 | size_t ClangASTContext::GetNumberOfFunctionArguments( |
| 3128 | lldb::opaque_compiler_type_t type) { |
| 3129 | if (type) { |
| 3130 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3131 | const clang::FunctionProtoType *func = |
| 3132 | llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr()); |
| 3133 | if (func) |
| 3134 | return func->getNumParams(); |
| 3135 | } |
| 3136 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3137 | } |
| 3138 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 3139 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3140 | ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type, |
| 3141 | const size_t index) { |
| 3142 | if (type) { |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3143 | clang::QualType qual_type(GetQualType(type)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3144 | const clang::FunctionProtoType *func = |
| 3145 | llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr()); |
| 3146 | if (func) { |
| 3147 | if (index < func->getNumParams()) |
| 3148 | return CompilerType(getASTContext(), func->getParamType(index)); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3149 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3150 | } |
| 3151 | return CompilerType(); |
| 3152 | } |
| 3153 | |
| 3154 | bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) { |
| 3155 | if (type) { |
| 3156 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3157 | |
| 3158 | if (qual_type->isFunctionPointerType()) |
| 3159 | return true; |
| 3160 | |
| 3161 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3162 | switch (type_class) { |
| 3163 | default: |
| 3164 | break; |
| 3165 | case clang::Type::Typedef: |
| 3166 | return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type) |
| 3167 | ->getDecl() |
| 3168 | ->getUnderlyingType() |
| 3169 | .getAsOpaquePtr()); |
| 3170 | case clang::Type::Auto: |
| 3171 | return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type) |
| 3172 | ->getDeducedType() |
| 3173 | .getAsOpaquePtr()); |
| 3174 | case clang::Type::Elaborated: |
| 3175 | return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 3176 | ->getNamedType() |
| 3177 | .getAsOpaquePtr()); |
| 3178 | case clang::Type::Paren: |
| 3179 | return IsFunctionPointerType( |
| 3180 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()); |
| 3181 | |
| 3182 | case clang::Type::LValueReference: |
| 3183 | case clang::Type::RValueReference: { |
| 3184 | const clang::ReferenceType *reference_type = |
| 3185 | llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()); |
| 3186 | if (reference_type) |
| 3187 | return IsFunctionPointerType( |
| 3188 | reference_type->getPointeeType().getAsOpaquePtr()); |
| 3189 | } break; |
| 3190 | } |
| 3191 | } |
| 3192 | return false; |
| 3193 | } |
| 3194 | |
| 3195 | bool ClangASTContext::IsBlockPointerType( |
| 3196 | lldb::opaque_compiler_type_t type, |
| 3197 | CompilerType *function_pointer_type_ptr) { |
| 3198 | if (type) { |
| 3199 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3200 | |
| 3201 | if (qual_type->isBlockPointerType()) { |
| 3202 | if (function_pointer_type_ptr) { |
| 3203 | const clang::BlockPointerType *block_pointer_type = |
| 3204 | qual_type->getAs<clang::BlockPointerType>(); |
| 3205 | QualType pointee_type = block_pointer_type->getPointeeType(); |
| 3206 | QualType function_pointer_type = m_ast_ap->getPointerType(pointee_type); |
| 3207 | *function_pointer_type_ptr = |
| 3208 | CompilerType(getASTContext(), function_pointer_type); |
| 3209 | } |
| 3210 | return true; |
| 3211 | } |
| 3212 | |
| 3213 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3214 | switch (type_class) { |
| 3215 | default: |
| 3216 | break; |
| 3217 | case clang::Type::Typedef: |
| 3218 | return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type) |
| 3219 | ->getDecl() |
| 3220 | ->getUnderlyingType() |
| 3221 | .getAsOpaquePtr(), |
| 3222 | function_pointer_type_ptr); |
| 3223 | case clang::Type::Auto: |
| 3224 | return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type) |
| 3225 | ->getDeducedType() |
| 3226 | .getAsOpaquePtr(), |
| 3227 | function_pointer_type_ptr); |
| 3228 | case clang::Type::Elaborated: |
| 3229 | return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 3230 | ->getNamedType() |
| 3231 | .getAsOpaquePtr(), |
| 3232 | function_pointer_type_ptr); |
| 3233 | case clang::Type::Paren: |
| 3234 | return IsBlockPointerType( |
| 3235 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 3236 | function_pointer_type_ptr); |
| 3237 | |
| 3238 | case clang::Type::LValueReference: |
| 3239 | case clang::Type::RValueReference: { |
| 3240 | const clang::ReferenceType *reference_type = |
| 3241 | llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()); |
| 3242 | if (reference_type) |
| 3243 | return IsBlockPointerType( |
| 3244 | reference_type->getPointeeType().getAsOpaquePtr(), |
| 3245 | function_pointer_type_ptr); |
| 3246 | } break; |
| 3247 | } |
| 3248 | } |
| 3249 | return false; |
| 3250 | } |
| 3251 | |
| 3252 | bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type, |
| 3253 | bool &is_signed) { |
| 3254 | if (!type) |
| 3255 | return false; |
| 3256 | |
| 3257 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3258 | const clang::BuiltinType *builtin_type = |
| 3259 | llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal()); |
| 3260 | |
| 3261 | if (builtin_type) { |
| 3262 | if (builtin_type->isInteger()) { |
| 3263 | is_signed = builtin_type->isSignedInteger(); |
| 3264 | return true; |
| 3265 | } |
| 3266 | } |
| 3267 | |
| 3268 | return false; |
| 3269 | } |
| 3270 | |
| 3271 | bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type, |
| 3272 | bool &is_signed) { |
| 3273 | if (type) { |
| 3274 | const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>( |
| 3275 | GetCanonicalQualType(type)->getCanonicalTypeInternal()); |
| 3276 | |
| 3277 | if (enum_type) { |
| 3278 | IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(), |
| 3279 | is_signed); |
| 3280 | return true; |
| 3281 | } |
| 3282 | } |
| 3283 | |
| 3284 | return false; |
| 3285 | } |
| 3286 | |
| 3287 | bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type, |
| 3288 | CompilerType *pointee_type) { |
| 3289 | if (type) { |
| 3290 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3291 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3292 | switch (type_class) { |
| 3293 | case clang::Type::Builtin: |
| 3294 | switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { |
| 3295 | default: |
| 3296 | break; |
| 3297 | case clang::BuiltinType::ObjCId: |
| 3298 | case clang::BuiltinType::ObjCClass: |
| 3299 | return true; |
| 3300 | } |
| 3301 | return false; |
| 3302 | case clang::Type::ObjCObjectPointer: |
| 3303 | if (pointee_type) |
| 3304 | pointee_type->SetCompilerType( |
| 3305 | getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type) |
| 3306 | ->getPointeeType()); |
| 3307 | return true; |
| 3308 | case clang::Type::BlockPointer: |
| 3309 | if (pointee_type) |
| 3310 | pointee_type->SetCompilerType( |
| 3311 | getASTContext(), |
| 3312 | llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType()); |
| 3313 | return true; |
| 3314 | case clang::Type::Pointer: |
| 3315 | if (pointee_type) |
| 3316 | pointee_type->SetCompilerType( |
| 3317 | getASTContext(), |
| 3318 | llvm::cast<clang::PointerType>(qual_type)->getPointeeType()); |
| 3319 | return true; |
| 3320 | case clang::Type::MemberPointer: |
| 3321 | if (pointee_type) |
| 3322 | pointee_type->SetCompilerType( |
| 3323 | getASTContext(), |
| 3324 | llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType()); |
| 3325 | return true; |
| 3326 | case clang::Type::Typedef: |
| 3327 | return IsPointerType(llvm::cast<clang::TypedefType>(qual_type) |
| 3328 | ->getDecl() |
| 3329 | ->getUnderlyingType() |
| 3330 | .getAsOpaquePtr(), |
| 3331 | pointee_type); |
| 3332 | case clang::Type::Auto: |
| 3333 | return IsPointerType(llvm::cast<clang::AutoType>(qual_type) |
| 3334 | ->getDeducedType() |
| 3335 | .getAsOpaquePtr(), |
| 3336 | pointee_type); |
| 3337 | case clang::Type::Elaborated: |
| 3338 | return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 3339 | ->getNamedType() |
| 3340 | .getAsOpaquePtr(), |
| 3341 | pointee_type); |
| 3342 | case clang::Type::Paren: |
| 3343 | return IsPointerType( |
| 3344 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 3345 | pointee_type); |
| 3346 | default: |
| 3347 | break; |
| 3348 | } |
| 3349 | } |
| 3350 | if (pointee_type) |
| 3351 | pointee_type->Clear(); |
| 3352 | return false; |
| 3353 | } |
| 3354 | |
| 3355 | bool ClangASTContext::IsPointerOrReferenceType( |
| 3356 | lldb::opaque_compiler_type_t type, CompilerType *pointee_type) { |
| 3357 | if (type) { |
| 3358 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3359 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3360 | switch (type_class) { |
| 3361 | case clang::Type::Builtin: |
| 3362 | switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { |
| 3363 | default: |
| 3364 | break; |
| 3365 | case clang::BuiltinType::ObjCId: |
| 3366 | case clang::BuiltinType::ObjCClass: |
| 3367 | return true; |
| 3368 | } |
| 3369 | return false; |
| 3370 | case clang::Type::ObjCObjectPointer: |
| 3371 | if (pointee_type) |
| 3372 | pointee_type->SetCompilerType( |
| 3373 | getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type) |
| 3374 | ->getPointeeType()); |
| 3375 | return true; |
| 3376 | case clang::Type::BlockPointer: |
| 3377 | if (pointee_type) |
| 3378 | pointee_type->SetCompilerType( |
| 3379 | getASTContext(), |
| 3380 | llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType()); |
| 3381 | return true; |
| 3382 | case clang::Type::Pointer: |
| 3383 | if (pointee_type) |
| 3384 | pointee_type->SetCompilerType( |
| 3385 | getASTContext(), |
| 3386 | llvm::cast<clang::PointerType>(qual_type)->getPointeeType()); |
| 3387 | return true; |
| 3388 | case clang::Type::MemberPointer: |
| 3389 | if (pointee_type) |
| 3390 | pointee_type->SetCompilerType( |
| 3391 | getASTContext(), |
| 3392 | llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType()); |
| 3393 | return true; |
| 3394 | case clang::Type::LValueReference: |
| 3395 | if (pointee_type) |
| 3396 | pointee_type->SetCompilerType( |
| 3397 | getASTContext(), |
| 3398 | llvm::cast<clang::LValueReferenceType>(qual_type)->desugar()); |
| 3399 | return true; |
| 3400 | case clang::Type::RValueReference: |
| 3401 | if (pointee_type) |
| 3402 | pointee_type->SetCompilerType( |
| 3403 | getASTContext(), |
| 3404 | llvm::cast<clang::RValueReferenceType>(qual_type)->desugar()); |
| 3405 | return true; |
| 3406 | case clang::Type::Typedef: |
| 3407 | return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type) |
| 3408 | ->getDecl() |
| 3409 | ->getUnderlyingType() |
| 3410 | .getAsOpaquePtr(), |
| 3411 | pointee_type); |
| 3412 | case clang::Type::Auto: |
| 3413 | return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type) |
| 3414 | ->getDeducedType() |
| 3415 | .getAsOpaquePtr(), |
| 3416 | pointee_type); |
| 3417 | case clang::Type::Elaborated: |
| 3418 | return IsPointerOrReferenceType( |
| 3419 | llvm::cast<clang::ElaboratedType>(qual_type) |
| 3420 | ->getNamedType() |
| 3421 | .getAsOpaquePtr(), |
| 3422 | pointee_type); |
| 3423 | case clang::Type::Paren: |
| 3424 | return IsPointerOrReferenceType( |
| 3425 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 3426 | pointee_type); |
| 3427 | default: |
| 3428 | break; |
| 3429 | } |
| 3430 | } |
| 3431 | if (pointee_type) |
| 3432 | pointee_type->Clear(); |
| 3433 | return false; |
| 3434 | } |
| 3435 | |
| 3436 | bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type, |
| 3437 | CompilerType *pointee_type, |
| 3438 | bool *is_rvalue) { |
| 3439 | if (type) { |
| 3440 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3441 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3442 | |
| 3443 | switch (type_class) { |
| 3444 | case clang::Type::LValueReference: |
| 3445 | if (pointee_type) |
| 3446 | pointee_type->SetCompilerType( |
| 3447 | getASTContext(), |
| 3448 | llvm::cast<clang::LValueReferenceType>(qual_type)->desugar()); |
| 3449 | if (is_rvalue) |
| 3450 | *is_rvalue = false; |
| 3451 | return true; |
| 3452 | case clang::Type::RValueReference: |
| 3453 | if (pointee_type) |
| 3454 | pointee_type->SetCompilerType( |
| 3455 | getASTContext(), |
| 3456 | llvm::cast<clang::RValueReferenceType>(qual_type)->desugar()); |
| 3457 | if (is_rvalue) |
| 3458 | *is_rvalue = true; |
| 3459 | return true; |
| 3460 | case clang::Type::Typedef: |
| 3461 | return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type) |
| 3462 | ->getDecl() |
| 3463 | ->getUnderlyingType() |
| 3464 | .getAsOpaquePtr(), |
| 3465 | pointee_type, is_rvalue); |
| 3466 | case clang::Type::Auto: |
| 3467 | return IsReferenceType(llvm::cast<clang::AutoType>(qual_type) |
| 3468 | ->getDeducedType() |
| 3469 | .getAsOpaquePtr(), |
| 3470 | pointee_type, is_rvalue); |
| 3471 | case clang::Type::Elaborated: |
| 3472 | return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 3473 | ->getNamedType() |
| 3474 | .getAsOpaquePtr(), |
| 3475 | pointee_type, is_rvalue); |
| 3476 | case clang::Type::Paren: |
| 3477 | return IsReferenceType( |
| 3478 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 3479 | pointee_type, is_rvalue); |
| 3480 | |
| 3481 | default: |
| 3482 | break; |
| 3483 | } |
| 3484 | } |
| 3485 | if (pointee_type) |
| 3486 | pointee_type->Clear(); |
| 3487 | return false; |
| 3488 | } |
| 3489 | |
| 3490 | bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type, |
| 3491 | uint32_t &count, bool &is_complex) { |
| 3492 | if (type) { |
| 3493 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3494 | |
| 3495 | if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>( |
| 3496 | qual_type->getCanonicalTypeInternal())) { |
| 3497 | clang::BuiltinType::Kind kind = BT->getKind(); |
| 3498 | if (kind >= clang::BuiltinType::Float && |
| 3499 | kind <= clang::BuiltinType::LongDouble) { |
| 3500 | count = 1; |
| 3501 | is_complex = false; |
| 3502 | return true; |
| 3503 | } |
| 3504 | } else if (const clang::ComplexType *CT = |
| 3505 | llvm::dyn_cast<clang::ComplexType>( |
| 3506 | qual_type->getCanonicalTypeInternal())) { |
| 3507 | if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count, |
| 3508 | is_complex)) { |
| 3509 | count = 2; |
| 3510 | is_complex = true; |
| 3511 | return true; |
| 3512 | } |
| 3513 | } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>( |
| 3514 | qual_type->getCanonicalTypeInternal())) { |
| 3515 | if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count, |
| 3516 | is_complex)) { |
| 3517 | count = VT->getNumElements(); |
| 3518 | is_complex = false; |
| 3519 | return true; |
| 3520 | } |
| 3521 | } |
| 3522 | } |
| 3523 | count = 0; |
| 3524 | is_complex = false; |
| 3525 | return false; |
| 3526 | } |
| 3527 | |
| 3528 | bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) { |
| 3529 | if (!type) |
| 3530 | return false; |
| 3531 | |
| 3532 | clang::QualType qual_type(GetQualType(type)); |
| 3533 | const clang::TagType *tag_type = |
| 3534 | llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()); |
| 3535 | if (tag_type) { |
| 3536 | clang::TagDecl *tag_decl = tag_type->getDecl(); |
| 3537 | if (tag_decl) |
| 3538 | return tag_decl->isCompleteDefinition(); |
| 3539 | return false; |
| 3540 | } else { |
| 3541 | const clang::ObjCObjectType *objc_class_type = |
| 3542 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type); |
| 3543 | if (objc_class_type) { |
| 3544 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 3545 | objc_class_type->getInterface(); |
| 3546 | if (class_interface_decl) |
| 3547 | return class_interface_decl->getDefinition() != nullptr; |
| 3548 | return false; |
| 3549 | } |
| 3550 | } |
| 3551 | return true; |
| 3552 | } |
| 3553 | |
| 3554 | bool ClangASTContext::IsObjCClassType(const CompilerType &type) { |
| 3555 | if (type) { |
| 3556 | clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type)); |
| 3557 | |
| 3558 | const clang::ObjCObjectPointerType *obj_pointer_type = |
| 3559 | llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type); |
| 3560 | |
| 3561 | if (obj_pointer_type) |
| 3562 | return obj_pointer_type->isObjCClassType(); |
| 3563 | } |
| 3564 | return false; |
| 3565 | } |
| 3566 | |
| 3567 | bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) { |
| 3568 | if (ClangUtil::IsClangType(type)) |
| 3569 | return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType(); |
| 3570 | return false; |
| 3571 | } |
| 3572 | |
| 3573 | bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) { |
| 3574 | if (!type) |
| 3575 | return false; |
| 3576 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3577 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3578 | return (type_class == clang::Type::Record); |
| 3579 | } |
| 3580 | |
| 3581 | bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) { |
| 3582 | if (!type) |
| 3583 | return false; |
| 3584 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3585 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3586 | return (type_class == clang::Type::Enum); |
| 3587 | } |
| 3588 | |
| 3589 | bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) { |
| 3590 | if (type) { |
| 3591 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3592 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3593 | switch (type_class) { |
| 3594 | case clang::Type::Record: |
| 3595 | if (GetCompleteType(type)) { |
| 3596 | const clang::RecordType *record_type = |
| 3597 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 3598 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 3599 | if (record_decl) { |
| 3600 | const clang::CXXRecordDecl *cxx_record_decl = |
| 3601 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 3602 | if (cxx_record_decl) |
| 3603 | return cxx_record_decl->isPolymorphic(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3604 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3605 | } |
| 3606 | break; |
| 3607 | |
| 3608 | default: |
| 3609 | break; |
| 3610 | } |
| 3611 | } |
| 3612 | return false; |
| 3613 | } |
| 3614 | |
| 3615 | bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type, |
| 3616 | CompilerType *dynamic_pointee_type, |
| 3617 | bool check_cplusplus, |
| 3618 | bool check_objc) { |
| 3619 | clang::QualType pointee_qual_type; |
| 3620 | if (type) { |
| 3621 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3622 | bool success = false; |
| 3623 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3624 | switch (type_class) { |
| 3625 | case clang::Type::Builtin: |
| 3626 | if (check_objc && |
| 3627 | llvm::cast<clang::BuiltinType>(qual_type)->getKind() == |
| 3628 | clang::BuiltinType::ObjCId) { |
| 3629 | if (dynamic_pointee_type) |
| 3630 | dynamic_pointee_type->SetCompilerType(this, type); |
| 3631 | return true; |
| 3632 | } |
| 3633 | break; |
| 3634 | |
| 3635 | case clang::Type::ObjCObjectPointer: |
| 3636 | if (check_objc) { |
| 3637 | if (auto objc_pointee_type = |
| 3638 | qual_type->getPointeeType().getTypePtrOrNull()) { |
| 3639 | if (auto objc_object_type = |
| 3640 | llvm::dyn_cast_or_null<clang::ObjCObjectType>( |
| 3641 | objc_pointee_type)) { |
| 3642 | if (objc_object_type->isObjCClass()) |
| 3643 | return false; |
| 3644 | } |
| 3645 | } |
| 3646 | if (dynamic_pointee_type) |
| 3647 | dynamic_pointee_type->SetCompilerType( |
| 3648 | getASTContext(), |
| 3649 | llvm::cast<clang::ObjCObjectPointerType>(qual_type) |
| 3650 | ->getPointeeType()); |
| 3651 | return true; |
| 3652 | } |
| 3653 | break; |
| 3654 | |
| 3655 | case clang::Type::Pointer: |
| 3656 | pointee_qual_type = |
| 3657 | llvm::cast<clang::PointerType>(qual_type)->getPointeeType(); |
| 3658 | success = true; |
| 3659 | break; |
| 3660 | |
| 3661 | case clang::Type::LValueReference: |
| 3662 | case clang::Type::RValueReference: |
| 3663 | pointee_qual_type = |
| 3664 | llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType(); |
| 3665 | success = true; |
| 3666 | break; |
| 3667 | |
| 3668 | case clang::Type::Typedef: |
| 3669 | return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type) |
| 3670 | ->getDecl() |
| 3671 | ->getUnderlyingType() |
| 3672 | .getAsOpaquePtr(), |
| 3673 | dynamic_pointee_type, check_cplusplus, |
| 3674 | check_objc); |
| 3675 | |
| 3676 | case clang::Type::Auto: |
| 3677 | return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type) |
| 3678 | ->getDeducedType() |
| 3679 | .getAsOpaquePtr(), |
| 3680 | dynamic_pointee_type, check_cplusplus, |
| 3681 | check_objc); |
| 3682 | |
| 3683 | case clang::Type::Elaborated: |
| 3684 | return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 3685 | ->getNamedType() |
| 3686 | .getAsOpaquePtr(), |
| 3687 | dynamic_pointee_type, check_cplusplus, |
| 3688 | check_objc); |
| 3689 | |
| 3690 | case clang::Type::Paren: |
| 3691 | return IsPossibleDynamicType( |
| 3692 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 3693 | dynamic_pointee_type, check_cplusplus, check_objc); |
| 3694 | default: |
| 3695 | break; |
| 3696 | } |
| 3697 | |
| 3698 | if (success) { |
| 3699 | // Check to make sure what we are pointing too is a possible dynamic C++ |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3700 | // type We currently accept any "void *" (in case we have a class that |
| 3701 | // has been watered down to an opaque pointer) and virtual C++ classes. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3702 | const clang::Type::TypeClass pointee_type_class = |
| 3703 | pointee_qual_type.getCanonicalType()->getTypeClass(); |
| 3704 | switch (pointee_type_class) { |
| 3705 | case clang::Type::Builtin: |
| 3706 | switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) { |
| 3707 | case clang::BuiltinType::UnknownAny: |
| 3708 | case clang::BuiltinType::Void: |
| 3709 | if (dynamic_pointee_type) |
| 3710 | dynamic_pointee_type->SetCompilerType(getASTContext(), |
| 3711 | pointee_qual_type); |
| 3712 | return true; |
| 3713 | default: |
| 3714 | break; |
| 3715 | } |
| 3716 | break; |
| 3717 | |
| 3718 | case clang::Type::Record: |
| 3719 | if (check_cplusplus) { |
| 3720 | clang::CXXRecordDecl *cxx_record_decl = |
| 3721 | pointee_qual_type->getAsCXXRecordDecl(); |
| 3722 | if (cxx_record_decl) { |
| 3723 | bool is_complete = cxx_record_decl->isCompleteDefinition(); |
| 3724 | |
| 3725 | if (is_complete) |
| 3726 | success = cxx_record_decl->isDynamicClass(); |
| 3727 | else { |
| 3728 | ClangASTMetadata *metadata = ClangASTContext::GetMetadata( |
| 3729 | getASTContext(), cxx_record_decl); |
| 3730 | if (metadata) |
| 3731 | success = metadata->GetIsDynamicCXXType(); |
| 3732 | else { |
| 3733 | is_complete = CompilerType(getASTContext(), pointee_qual_type) |
| 3734 | .GetCompleteType(); |
| 3735 | if (is_complete) |
| 3736 | success = cxx_record_decl->isDynamicClass(); |
| 3737 | else |
| 3738 | success = false; |
| 3739 | } |
| 3740 | } |
| 3741 | |
| 3742 | if (success) { |
| 3743 | if (dynamic_pointee_type) |
| 3744 | dynamic_pointee_type->SetCompilerType(getASTContext(), |
| 3745 | pointee_qual_type); |
| 3746 | return true; |
| 3747 | } |
| 3748 | } |
| 3749 | } |
| 3750 | break; |
| 3751 | |
| 3752 | case clang::Type::ObjCObject: |
| 3753 | case clang::Type::ObjCInterface: |
| 3754 | if (check_objc) { |
| 3755 | if (dynamic_pointee_type) |
| 3756 | dynamic_pointee_type->SetCompilerType(getASTContext(), |
| 3757 | pointee_qual_type); |
| 3758 | return true; |
| 3759 | } |
| 3760 | break; |
| 3761 | |
| 3762 | default: |
| 3763 | break; |
| 3764 | } |
| 3765 | } |
| 3766 | } |
| 3767 | if (dynamic_pointee_type) |
| 3768 | dynamic_pointee_type->Clear(); |
| 3769 | return false; |
| 3770 | } |
| 3771 | |
| 3772 | bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) { |
| 3773 | if (!type) |
| 3774 | return false; |
| 3775 | |
| 3776 | return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0; |
| 3777 | } |
| 3778 | |
| 3779 | bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) { |
| 3780 | if (!type) |
| 3781 | return false; |
| 3782 | return GetQualType(type)->getTypeClass() == clang::Type::Typedef; |
| 3783 | } |
| 3784 | |
| 3785 | bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) { |
| 3786 | if (!type) |
| 3787 | return false; |
| 3788 | return GetCanonicalQualType(type)->isVoidType(); |
| 3789 | } |
| 3790 | |
| 3791 | bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) { |
| 3792 | return ClangASTContextSupportsLanguage(language); |
| 3793 | } |
| 3794 | |
| 3795 | bool ClangASTContext::GetCXXClassName(const CompilerType &type, |
| 3796 | std::string &class_name) { |
| 3797 | if (type) { |
| 3798 | clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type)); |
| 3799 | if (!qual_type.isNull()) { |
| 3800 | clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); |
| 3801 | if (cxx_record_decl) { |
| 3802 | class_name.assign(cxx_record_decl->getIdentifier()->getNameStart()); |
| 3803 | return true; |
| 3804 | } |
| 3805 | } |
| 3806 | } |
| 3807 | class_name.clear(); |
| 3808 | return false; |
| 3809 | } |
| 3810 | |
| 3811 | bool ClangASTContext::IsCXXClassType(const CompilerType &type) { |
| 3812 | if (!type) |
| 3813 | return false; |
| 3814 | |
| 3815 | clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type)); |
| 3816 | if (!qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr) |
| 3817 | return true; |
| 3818 | return false; |
| 3819 | } |
| 3820 | |
| 3821 | bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) { |
| 3822 | if (!type) |
| 3823 | return false; |
| 3824 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3825 | const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type); |
| 3826 | if (tag_type) |
| 3827 | return tag_type->isBeingDefined(); |
| 3828 | return false; |
| 3829 | } |
| 3830 | |
| 3831 | bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type, |
| 3832 | CompilerType *class_type_ptr) { |
| 3833 | if (!type) |
| 3834 | return false; |
| 3835 | |
| 3836 | clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type)); |
| 3837 | |
| 3838 | if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) { |
| 3839 | if (class_type_ptr) { |
| 3840 | if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) { |
| 3841 | const clang::ObjCObjectPointerType *obj_pointer_type = |
| 3842 | llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type); |
| 3843 | if (obj_pointer_type == nullptr) |
| 3844 | class_type_ptr->Clear(); |
| 3845 | else |
| 3846 | class_type_ptr->SetCompilerType( |
| 3847 | type.GetTypeSystem(), |
| 3848 | clang::QualType(obj_pointer_type->getInterfaceType(), 0) |
| 3849 | .getAsOpaquePtr()); |
| 3850 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3851 | } |
| 3852 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3853 | } |
| 3854 | if (class_type_ptr) |
| 3855 | class_type_ptr->Clear(); |
| 3856 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3857 | } |
| 3858 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3859 | bool ClangASTContext::GetObjCClassName(const CompilerType &type, |
| 3860 | std::string &class_name) { |
| 3861 | if (!type) |
| 3862 | return false; |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 3863 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3864 | clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type)); |
| 3865 | |
| 3866 | const clang::ObjCObjectType *object_type = |
| 3867 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type); |
| 3868 | if (object_type) { |
| 3869 | const clang::ObjCInterfaceDecl *interface = object_type->getInterface(); |
| 3870 | if (interface) { |
| 3871 | class_name = interface->getNameAsString(); |
| 3872 | return true; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3873 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3874 | } |
| 3875 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3876 | } |
| 3877 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3878 | //---------------------------------------------------------------------- |
| 3879 | // Type Completion |
| 3880 | //---------------------------------------------------------------------- |
| 3881 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3882 | bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) { |
| 3883 | if (!type) |
| 3884 | return false; |
| 3885 | const bool allow_completion = true; |
| 3886 | return GetCompleteQualType(getASTContext(), GetQualType(type), |
| 3887 | allow_completion); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3888 | } |
| 3889 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3890 | ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) { |
| 3891 | std::string type_name; |
| 3892 | if (type) { |
| 3893 | clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy()); |
| 3894 | clang::QualType qual_type(GetQualType(type)); |
| 3895 | printing_policy.SuppressTagKeyword = true; |
| 3896 | const clang::TypedefType *typedef_type = |
| 3897 | qual_type->getAs<clang::TypedefType>(); |
| 3898 | if (typedef_type) { |
| 3899 | const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl(); |
| 3900 | type_name = typedef_decl->getQualifiedNameAsString(); |
| 3901 | } else { |
| 3902 | type_name = qual_type.getAsString(printing_policy); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3903 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3904 | } |
| 3905 | return ConstString(type_name); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3906 | } |
| 3907 | |
| 3908 | uint32_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3909 | ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type, |
| 3910 | CompilerType *pointee_or_element_clang_type) { |
| 3911 | if (!type) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3912 | return 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3913 | |
| 3914 | if (pointee_or_element_clang_type) |
| 3915 | pointee_or_element_clang_type->Clear(); |
| 3916 | |
| 3917 | clang::QualType qual_type(GetQualType(type)); |
| 3918 | |
| 3919 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3920 | switch (type_class) { |
Sean Callanan | ddf802a | 2017-06-02 01:24:18 +0000 | [diff] [blame] | 3921 | case clang::Type::Attributed: |
| 3922 | return GetTypeInfo( |
| 3923 | qual_type->getAs<clang::AttributedType>() |
| 3924 | ->getModifiedType().getAsOpaquePtr(), |
| 3925 | pointee_or_element_clang_type); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3926 | case clang::Type::Builtin: { |
| 3927 | const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>( |
| 3928 | qual_type->getCanonicalTypeInternal()); |
| 3929 | |
| 3930 | uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue; |
| 3931 | switch (builtin_type->getKind()) { |
| 3932 | case clang::BuiltinType::ObjCId: |
| 3933 | case clang::BuiltinType::ObjCClass: |
| 3934 | if (pointee_or_element_clang_type) |
| 3935 | pointee_or_element_clang_type->SetCompilerType( |
| 3936 | getASTContext(), getASTContext()->ObjCBuiltinClassTy); |
| 3937 | builtin_type_flags |= eTypeIsPointer | eTypeIsObjC; |
| 3938 | break; |
| 3939 | |
| 3940 | case clang::BuiltinType::ObjCSel: |
| 3941 | if (pointee_or_element_clang_type) |
| 3942 | pointee_or_element_clang_type->SetCompilerType(getASTContext(), |
| 3943 | getASTContext()->CharTy); |
| 3944 | builtin_type_flags |= eTypeIsPointer | eTypeIsObjC; |
| 3945 | break; |
| 3946 | |
| 3947 | case clang::BuiltinType::Bool: |
| 3948 | case clang::BuiltinType::Char_U: |
| 3949 | case clang::BuiltinType::UChar: |
| 3950 | case clang::BuiltinType::WChar_U: |
| 3951 | case clang::BuiltinType::Char16: |
| 3952 | case clang::BuiltinType::Char32: |
| 3953 | case clang::BuiltinType::UShort: |
| 3954 | case clang::BuiltinType::UInt: |
| 3955 | case clang::BuiltinType::ULong: |
| 3956 | case clang::BuiltinType::ULongLong: |
| 3957 | case clang::BuiltinType::UInt128: |
| 3958 | case clang::BuiltinType::Char_S: |
| 3959 | case clang::BuiltinType::SChar: |
| 3960 | case clang::BuiltinType::WChar_S: |
| 3961 | case clang::BuiltinType::Short: |
| 3962 | case clang::BuiltinType::Int: |
| 3963 | case clang::BuiltinType::Long: |
| 3964 | case clang::BuiltinType::LongLong: |
| 3965 | case clang::BuiltinType::Int128: |
| 3966 | case clang::BuiltinType::Float: |
| 3967 | case clang::BuiltinType::Double: |
| 3968 | case clang::BuiltinType::LongDouble: |
| 3969 | builtin_type_flags |= eTypeIsScalar; |
| 3970 | if (builtin_type->isInteger()) { |
| 3971 | builtin_type_flags |= eTypeIsInteger; |
| 3972 | if (builtin_type->isSignedInteger()) |
| 3973 | builtin_type_flags |= eTypeIsSigned; |
| 3974 | } else if (builtin_type->isFloatingPoint()) |
| 3975 | builtin_type_flags |= eTypeIsFloat; |
| 3976 | break; |
| 3977 | default: |
| 3978 | break; |
| 3979 | } |
| 3980 | return builtin_type_flags; |
| 3981 | } |
| 3982 | |
| 3983 | case clang::Type::BlockPointer: |
| 3984 | if (pointee_or_element_clang_type) |
| 3985 | pointee_or_element_clang_type->SetCompilerType( |
| 3986 | getASTContext(), qual_type->getPointeeType()); |
| 3987 | return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock; |
| 3988 | |
| 3989 | case clang::Type::Complex: { |
| 3990 | uint32_t complex_type_flags = |
| 3991 | eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex; |
| 3992 | const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>( |
| 3993 | qual_type->getCanonicalTypeInternal()); |
| 3994 | if (complex_type) { |
| 3995 | clang::QualType complex_element_type(complex_type->getElementType()); |
| 3996 | if (complex_element_type->isIntegerType()) |
| 3997 | complex_type_flags |= eTypeIsFloat; |
| 3998 | else if (complex_element_type->isFloatingType()) |
| 3999 | complex_type_flags |= eTypeIsInteger; |
| 4000 | } |
| 4001 | return complex_type_flags; |
| 4002 | } break; |
| 4003 | |
| 4004 | case clang::Type::ConstantArray: |
| 4005 | case clang::Type::DependentSizedArray: |
| 4006 | case clang::Type::IncompleteArray: |
| 4007 | case clang::Type::VariableArray: |
| 4008 | if (pointee_or_element_clang_type) |
| 4009 | pointee_or_element_clang_type->SetCompilerType( |
| 4010 | getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr()) |
| 4011 | ->getElementType()); |
| 4012 | return eTypeHasChildren | eTypeIsArray; |
| 4013 | |
| 4014 | case clang::Type::DependentName: |
| 4015 | return 0; |
| 4016 | case clang::Type::DependentSizedExtVector: |
| 4017 | return eTypeHasChildren | eTypeIsVector; |
| 4018 | case clang::Type::DependentTemplateSpecialization: |
| 4019 | return eTypeIsTemplate; |
| 4020 | case clang::Type::Decltype: |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 4021 | return CompilerType( |
| 4022 | getASTContext(), |
| 4023 | llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType()) |
| 4024 | .GetTypeInfo(pointee_or_element_clang_type); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4025 | |
| 4026 | case clang::Type::Enum: |
| 4027 | if (pointee_or_element_clang_type) |
| 4028 | pointee_or_element_clang_type->SetCompilerType( |
| 4029 | getASTContext(), |
| 4030 | llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType()); |
| 4031 | return eTypeIsEnumeration | eTypeHasValue; |
| 4032 | |
| 4033 | case clang::Type::Auto: |
| 4034 | return CompilerType( |
| 4035 | getASTContext(), |
| 4036 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) |
| 4037 | .GetTypeInfo(pointee_or_element_clang_type); |
| 4038 | case clang::Type::Elaborated: |
| 4039 | return CompilerType( |
| 4040 | getASTContext(), |
| 4041 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) |
| 4042 | .GetTypeInfo(pointee_or_element_clang_type); |
| 4043 | case clang::Type::Paren: |
| 4044 | return CompilerType(getASTContext(), |
| 4045 | llvm::cast<clang::ParenType>(qual_type)->desugar()) |
| 4046 | .GetTypeInfo(pointee_or_element_clang_type); |
| 4047 | |
| 4048 | case clang::Type::FunctionProto: |
| 4049 | return eTypeIsFuncPrototype | eTypeHasValue; |
| 4050 | case clang::Type::FunctionNoProto: |
| 4051 | return eTypeIsFuncPrototype | eTypeHasValue; |
| 4052 | case clang::Type::InjectedClassName: |
| 4053 | return 0; |
| 4054 | |
| 4055 | case clang::Type::LValueReference: |
| 4056 | case clang::Type::RValueReference: |
| 4057 | if (pointee_or_element_clang_type) |
| 4058 | pointee_or_element_clang_type->SetCompilerType( |
| 4059 | getASTContext(), |
| 4060 | llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()) |
| 4061 | ->getPointeeType()); |
| 4062 | return eTypeHasChildren | eTypeIsReference | eTypeHasValue; |
| 4063 | |
| 4064 | case clang::Type::MemberPointer: |
| 4065 | return eTypeIsPointer | eTypeIsMember | eTypeHasValue; |
| 4066 | |
| 4067 | case clang::Type::ObjCObjectPointer: |
| 4068 | if (pointee_or_element_clang_type) |
| 4069 | pointee_or_element_clang_type->SetCompilerType( |
| 4070 | getASTContext(), qual_type->getPointeeType()); |
| 4071 | return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | |
| 4072 | eTypeHasValue; |
| 4073 | |
| 4074 | case clang::Type::ObjCObject: |
| 4075 | return eTypeHasChildren | eTypeIsObjC | eTypeIsClass; |
| 4076 | case clang::Type::ObjCInterface: |
| 4077 | return eTypeHasChildren | eTypeIsObjC | eTypeIsClass; |
| 4078 | |
| 4079 | case clang::Type::Pointer: |
| 4080 | if (pointee_or_element_clang_type) |
| 4081 | pointee_or_element_clang_type->SetCompilerType( |
| 4082 | getASTContext(), qual_type->getPointeeType()); |
| 4083 | return eTypeHasChildren | eTypeIsPointer | eTypeHasValue; |
| 4084 | |
| 4085 | case clang::Type::Record: |
| 4086 | if (qual_type->getAsCXXRecordDecl()) |
| 4087 | return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus; |
| 4088 | else |
| 4089 | return eTypeHasChildren | eTypeIsStructUnion; |
| 4090 | break; |
| 4091 | case clang::Type::SubstTemplateTypeParm: |
| 4092 | return eTypeIsTemplate; |
| 4093 | case clang::Type::TemplateTypeParm: |
| 4094 | return eTypeIsTemplate; |
| 4095 | case clang::Type::TemplateSpecialization: |
| 4096 | return eTypeIsTemplate; |
| 4097 | |
| 4098 | case clang::Type::Typedef: |
| 4099 | return eTypeIsTypedef | |
| 4100 | CompilerType(getASTContext(), |
| 4101 | llvm::cast<clang::TypedefType>(qual_type) |
| 4102 | ->getDecl() |
| 4103 | ->getUnderlyingType()) |
| 4104 | .GetTypeInfo(pointee_or_element_clang_type); |
| 4105 | case clang::Type::TypeOfExpr: |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 4106 | return CompilerType(getASTContext(), |
| 4107 | llvm::cast<clang::TypeOfExprType>(qual_type) |
| 4108 | ->getUnderlyingExpr() |
| 4109 | ->getType()) |
| 4110 | .GetTypeInfo(pointee_or_element_clang_type); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4111 | case clang::Type::TypeOf: |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 4112 | return CompilerType( |
| 4113 | getASTContext(), |
| 4114 | llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType()) |
| 4115 | .GetTypeInfo(pointee_or_element_clang_type); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4116 | case clang::Type::UnresolvedUsing: |
| 4117 | return 0; |
| 4118 | |
| 4119 | case clang::Type::ExtVector: |
| 4120 | case clang::Type::Vector: { |
| 4121 | uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector; |
| 4122 | const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>( |
| 4123 | qual_type->getCanonicalTypeInternal()); |
| 4124 | if (vector_type) { |
| 4125 | if (vector_type->isIntegerType()) |
| 4126 | vector_type_flags |= eTypeIsFloat; |
| 4127 | else if (vector_type->isFloatingType()) |
| 4128 | vector_type_flags |= eTypeIsInteger; |
| 4129 | } |
| 4130 | return vector_type_flags; |
| 4131 | } |
| 4132 | default: |
| 4133 | return 0; |
| 4134 | } |
| 4135 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4136 | } |
| 4137 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4138 | lldb::LanguageType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4139 | ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) { |
| 4140 | if (!type) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4141 | return lldb::eLanguageTypeC; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4142 | |
| 4143 | // If the type is a reference, then resolve it to what it refers to first: |
| 4144 | clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType()); |
| 4145 | if (qual_type->isAnyPointerType()) { |
| 4146 | if (qual_type->isObjCObjectPointerType()) |
| 4147 | return lldb::eLanguageTypeObjC; |
| 4148 | |
| 4149 | clang::QualType pointee_type(qual_type->getPointeeType()); |
| 4150 | if (pointee_type->getPointeeCXXRecordDecl() != nullptr) |
| 4151 | return lldb::eLanguageTypeC_plus_plus; |
| 4152 | if (pointee_type->isObjCObjectOrInterfaceType()) |
| 4153 | return lldb::eLanguageTypeObjC; |
| 4154 | if (pointee_type->isObjCClassType()) |
| 4155 | return lldb::eLanguageTypeObjC; |
| 4156 | if (pointee_type.getTypePtr() == |
| 4157 | getASTContext()->ObjCBuiltinIdTy.getTypePtr()) |
| 4158 | return lldb::eLanguageTypeObjC; |
| 4159 | } else { |
| 4160 | if (qual_type->isObjCObjectOrInterfaceType()) |
| 4161 | return lldb::eLanguageTypeObjC; |
| 4162 | if (qual_type->getAsCXXRecordDecl()) |
| 4163 | return lldb::eLanguageTypeC_plus_plus; |
| 4164 | switch (qual_type->getTypeClass()) { |
| 4165 | default: |
| 4166 | break; |
| 4167 | case clang::Type::Builtin: |
| 4168 | switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { |
| 4169 | default: |
| 4170 | case clang::BuiltinType::Void: |
| 4171 | case clang::BuiltinType::Bool: |
| 4172 | case clang::BuiltinType::Char_U: |
| 4173 | case clang::BuiltinType::UChar: |
| 4174 | case clang::BuiltinType::WChar_U: |
| 4175 | case clang::BuiltinType::Char16: |
| 4176 | case clang::BuiltinType::Char32: |
| 4177 | case clang::BuiltinType::UShort: |
| 4178 | case clang::BuiltinType::UInt: |
| 4179 | case clang::BuiltinType::ULong: |
| 4180 | case clang::BuiltinType::ULongLong: |
| 4181 | case clang::BuiltinType::UInt128: |
| 4182 | case clang::BuiltinType::Char_S: |
| 4183 | case clang::BuiltinType::SChar: |
| 4184 | case clang::BuiltinType::WChar_S: |
| 4185 | case clang::BuiltinType::Short: |
| 4186 | case clang::BuiltinType::Int: |
| 4187 | case clang::BuiltinType::Long: |
| 4188 | case clang::BuiltinType::LongLong: |
| 4189 | case clang::BuiltinType::Int128: |
| 4190 | case clang::BuiltinType::Float: |
| 4191 | case clang::BuiltinType::Double: |
| 4192 | case clang::BuiltinType::LongDouble: |
| 4193 | break; |
| 4194 | |
| 4195 | case clang::BuiltinType::NullPtr: |
| 4196 | return eLanguageTypeC_plus_plus; |
| 4197 | |
| 4198 | case clang::BuiltinType::ObjCId: |
| 4199 | case clang::BuiltinType::ObjCClass: |
| 4200 | case clang::BuiltinType::ObjCSel: |
| 4201 | return eLanguageTypeObjC; |
| 4202 | |
| 4203 | case clang::BuiltinType::Dependent: |
| 4204 | case clang::BuiltinType::Overload: |
| 4205 | case clang::BuiltinType::BoundMember: |
| 4206 | case clang::BuiltinType::UnknownAny: |
| 4207 | break; |
| 4208 | } |
| 4209 | break; |
| 4210 | case clang::Type::Typedef: |
| 4211 | return CompilerType(getASTContext(), |
| 4212 | llvm::cast<clang::TypedefType>(qual_type) |
| 4213 | ->getDecl() |
| 4214 | ->getUnderlyingType()) |
| 4215 | .GetMinimumLanguage(); |
| 4216 | } |
| 4217 | } |
| 4218 | return lldb::eLanguageTypeC; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4219 | } |
| 4220 | |
| 4221 | lldb::TypeClass |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4222 | ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) { |
| 4223 | if (!type) |
| 4224 | return lldb::eTypeClassInvalid; |
| 4225 | |
| 4226 | clang::QualType qual_type(GetQualType(type)); |
| 4227 | |
| 4228 | switch (qual_type->getTypeClass()) { |
| 4229 | case clang::Type::UnaryTransform: |
| 4230 | break; |
| 4231 | case clang::Type::FunctionNoProto: |
| 4232 | return lldb::eTypeClassFunction; |
| 4233 | case clang::Type::FunctionProto: |
| 4234 | return lldb::eTypeClassFunction; |
| 4235 | case clang::Type::IncompleteArray: |
| 4236 | return lldb::eTypeClassArray; |
| 4237 | case clang::Type::VariableArray: |
| 4238 | return lldb::eTypeClassArray; |
| 4239 | case clang::Type::ConstantArray: |
| 4240 | return lldb::eTypeClassArray; |
| 4241 | case clang::Type::DependentSizedArray: |
| 4242 | return lldb::eTypeClassArray; |
| 4243 | case clang::Type::DependentSizedExtVector: |
| 4244 | return lldb::eTypeClassVector; |
Fangrui Song | 8f28488 | 2018-07-13 22:40:40 +0000 | [diff] [blame] | 4245 | case clang::Type::DependentVector: |
| 4246 | return lldb::eTypeClassVector; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4247 | case clang::Type::ExtVector: |
| 4248 | return lldb::eTypeClassVector; |
| 4249 | case clang::Type::Vector: |
| 4250 | return lldb::eTypeClassVector; |
| 4251 | case clang::Type::Builtin: |
| 4252 | return lldb::eTypeClassBuiltin; |
| 4253 | case clang::Type::ObjCObjectPointer: |
| 4254 | return lldb::eTypeClassObjCObjectPointer; |
| 4255 | case clang::Type::BlockPointer: |
| 4256 | return lldb::eTypeClassBlockPointer; |
| 4257 | case clang::Type::Pointer: |
| 4258 | return lldb::eTypeClassPointer; |
| 4259 | case clang::Type::LValueReference: |
| 4260 | return lldb::eTypeClassReference; |
| 4261 | case clang::Type::RValueReference: |
| 4262 | return lldb::eTypeClassReference; |
| 4263 | case clang::Type::MemberPointer: |
| 4264 | return lldb::eTypeClassMemberPointer; |
| 4265 | case clang::Type::Complex: |
| 4266 | if (qual_type->isComplexType()) |
| 4267 | return lldb::eTypeClassComplexFloat; |
| 4268 | else |
| 4269 | return lldb::eTypeClassComplexInteger; |
| 4270 | case clang::Type::ObjCObject: |
| 4271 | return lldb::eTypeClassObjCObject; |
| 4272 | case clang::Type::ObjCInterface: |
| 4273 | return lldb::eTypeClassObjCInterface; |
| 4274 | case clang::Type::Record: { |
| 4275 | const clang::RecordType *record_type = |
| 4276 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 4277 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 4278 | if (record_decl->isUnion()) |
| 4279 | return lldb::eTypeClassUnion; |
| 4280 | else if (record_decl->isStruct()) |
| 4281 | return lldb::eTypeClassStruct; |
| 4282 | else |
| 4283 | return lldb::eTypeClassClass; |
| 4284 | } break; |
| 4285 | case clang::Type::Enum: |
| 4286 | return lldb::eTypeClassEnumeration; |
| 4287 | case clang::Type::Typedef: |
| 4288 | return lldb::eTypeClassTypedef; |
| 4289 | case clang::Type::UnresolvedUsing: |
| 4290 | break; |
| 4291 | case clang::Type::Paren: |
| 4292 | return CompilerType(getASTContext(), |
| 4293 | llvm::cast<clang::ParenType>(qual_type)->desugar()) |
| 4294 | .GetTypeClass(); |
| 4295 | case clang::Type::Auto: |
| 4296 | return CompilerType( |
| 4297 | getASTContext(), |
| 4298 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) |
| 4299 | .GetTypeClass(); |
| 4300 | case clang::Type::Elaborated: |
| 4301 | return CompilerType( |
| 4302 | getASTContext(), |
| 4303 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) |
| 4304 | .GetTypeClass(); |
| 4305 | |
| 4306 | case clang::Type::Attributed: |
| 4307 | break; |
| 4308 | case clang::Type::TemplateTypeParm: |
| 4309 | break; |
| 4310 | case clang::Type::SubstTemplateTypeParm: |
| 4311 | break; |
| 4312 | case clang::Type::SubstTemplateTypeParmPack: |
| 4313 | break; |
| 4314 | case clang::Type::InjectedClassName: |
| 4315 | break; |
| 4316 | case clang::Type::DependentName: |
| 4317 | break; |
| 4318 | case clang::Type::DependentTemplateSpecialization: |
| 4319 | break; |
| 4320 | case clang::Type::PackExpansion: |
| 4321 | break; |
| 4322 | |
| 4323 | case clang::Type::TypeOfExpr: |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 4324 | return CompilerType(getASTContext(), |
| 4325 | llvm::cast<clang::TypeOfExprType>(qual_type) |
| 4326 | ->getUnderlyingExpr() |
| 4327 | ->getType()) |
| 4328 | .GetTypeClass(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4329 | case clang::Type::TypeOf: |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 4330 | return CompilerType( |
| 4331 | getASTContext(), |
| 4332 | llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType()) |
| 4333 | .GetTypeClass(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4334 | case clang::Type::Decltype: |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 4335 | return CompilerType( |
| 4336 | getASTContext(), |
| 4337 | llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType()) |
| 4338 | .GetTypeClass(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4339 | case clang::Type::TemplateSpecialization: |
| 4340 | break; |
Pavel Labath | 4f19fce2 | 2017-02-17 13:39:50 +0000 | [diff] [blame] | 4341 | case clang::Type::DeducedTemplateSpecialization: |
| 4342 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4343 | case clang::Type::Atomic: |
| 4344 | break; |
| 4345 | case clang::Type::Pipe: |
| 4346 | break; |
| 4347 | |
| 4348 | // pointer type decayed from an array or function type. |
| 4349 | case clang::Type::Decayed: |
| 4350 | break; |
| 4351 | case clang::Type::Adjusted: |
| 4352 | break; |
Zachary Turner | 5a8ad459 | 2016-10-05 17:07:34 +0000 | [diff] [blame] | 4353 | case clang::Type::ObjCTypeParam: |
| 4354 | break; |
Ted Woodward | 66060cf | 2017-10-11 22:42:21 +0000 | [diff] [blame] | 4355 | |
| 4356 | case clang::Type::DependentAddressSpace: |
| 4357 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4358 | } |
| 4359 | // We don't know hot to display this type... |
| 4360 | return lldb::eTypeClassOther; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4361 | } |
| 4362 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4363 | unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) { |
| 4364 | if (type) |
| 4365 | return GetQualType(type).getQualifiers().getCVRQualifiers(); |
| 4366 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4367 | } |
| 4368 | |
| 4369 | //---------------------------------------------------------------------- |
| 4370 | // Creating related types |
| 4371 | //---------------------------------------------------------------------- |
| 4372 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 4373 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4374 | ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type, |
| 4375 | uint64_t *stride) { |
| 4376 | if (type) { |
| 4377 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 4378 | |
| 4379 | const clang::Type *array_eletype = |
| 4380 | qual_type.getTypePtr()->getArrayElementTypeNoTypeQual(); |
| 4381 | |
| 4382 | if (!array_eletype) |
| 4383 | return CompilerType(); |
| 4384 | |
| 4385 | CompilerType element_type(getASTContext(), |
| 4386 | array_eletype->getCanonicalTypeUnqualified()); |
| 4387 | |
| 4388 | // TODO: the real stride will be >= this value.. find the real one! |
| 4389 | if (stride) |
| 4390 | *stride = element_type.GetByteSize(nullptr); |
| 4391 | |
| 4392 | return element_type; |
| 4393 | } |
| 4394 | return CompilerType(); |
| 4395 | } |
| 4396 | |
| 4397 | CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type, |
| 4398 | uint64_t size) { |
| 4399 | if (type) { |
| 4400 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 4401 | if (clang::ASTContext *ast_ctx = getASTContext()) { |
| 4402 | if (size != 0) |
| 4403 | return CompilerType( |
| 4404 | ast_ctx, ast_ctx->getConstantArrayType( |
| 4405 | qual_type, llvm::APInt(64, size), |
| 4406 | clang::ArrayType::ArraySizeModifier::Normal, 0)); |
| 4407 | else |
| 4408 | return CompilerType( |
| 4409 | ast_ctx, |
| 4410 | ast_ctx->getIncompleteArrayType( |
| 4411 | qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0)); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4412 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4413 | } |
| 4414 | |
| 4415 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4416 | } |
| 4417 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 4418 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4419 | ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) { |
| 4420 | if (type) |
| 4421 | return CompilerType(getASTContext(), GetCanonicalQualType(type)); |
| 4422 | return CompilerType(); |
| 4423 | } |
| 4424 | |
| 4425 | static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast, |
| 4426 | clang::QualType qual_type) { |
| 4427 | if (qual_type->isPointerType()) |
| 4428 | qual_type = ast->getPointerType( |
| 4429 | GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType())); |
| 4430 | else |
| 4431 | qual_type = qual_type.getUnqualifiedType(); |
| 4432 | qual_type.removeLocalConst(); |
| 4433 | qual_type.removeLocalRestrict(); |
| 4434 | qual_type.removeLocalVolatile(); |
| 4435 | return qual_type; |
Enrico Granata | 639392f | 2016-08-30 20:39:58 +0000 | [diff] [blame] | 4436 | } |
| 4437 | |
| 4438 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4439 | ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) { |
| 4440 | if (type) |
| 4441 | return CompilerType( |
| 4442 | getASTContext(), |
| 4443 | GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type))); |
| 4444 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4445 | } |
| 4446 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4447 | int ClangASTContext::GetFunctionArgumentCount( |
| 4448 | lldb::opaque_compiler_type_t type) { |
| 4449 | if (type) { |
| 4450 | const clang::FunctionProtoType *func = |
| 4451 | llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type)); |
| 4452 | if (func) |
| 4453 | return func->getNumParams(); |
| 4454 | } |
| 4455 | return -1; |
| 4456 | } |
| 4457 | |
| 4458 | CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex( |
| 4459 | lldb::opaque_compiler_type_t type, size_t idx) { |
| 4460 | if (type) { |
| 4461 | const clang::FunctionProtoType *func = |
| 4462 | llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type)); |
| 4463 | if (func) { |
| 4464 | const uint32_t num_args = func->getNumParams(); |
| 4465 | if (idx < num_args) |
| 4466 | return CompilerType(getASTContext(), func->getParamType(idx)); |
| 4467 | } |
| 4468 | } |
| 4469 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4470 | } |
| 4471 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 4472 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4473 | ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) { |
| 4474 | if (type) { |
| 4475 | clang::QualType qual_type(GetQualType(type)); |
| 4476 | const clang::FunctionProtoType *func = |
| 4477 | llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr()); |
| 4478 | if (func) |
| 4479 | return CompilerType(getASTContext(), func->getReturnType()); |
| 4480 | } |
| 4481 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4482 | } |
| 4483 | |
| 4484 | size_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4485 | ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) { |
| 4486 | size_t num_functions = 0; |
| 4487 | if (type) { |
| 4488 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 4489 | switch (qual_type->getTypeClass()) { |
| 4490 | case clang::Type::Record: |
| 4491 | if (GetCompleteQualType(getASTContext(), qual_type)) { |
| 4492 | const clang::RecordType *record_type = |
| 4493 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 4494 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 4495 | assert(record_decl); |
| 4496 | const clang::CXXRecordDecl *cxx_record_decl = |
| 4497 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 4498 | if (cxx_record_decl) |
| 4499 | num_functions = std::distance(cxx_record_decl->method_begin(), |
| 4500 | cxx_record_decl->method_end()); |
| 4501 | } |
| 4502 | break; |
Enrico Granata | 36f51e4 | 2015-12-18 22:41:25 +0000 | [diff] [blame] | 4503 | |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 4504 | case clang::Type::ObjCObjectPointer: { |
| 4505 | const clang::ObjCObjectPointerType *objc_class_type = |
Sean Callanan | 732a6f4 | 2017-05-15 19:55:20 +0000 | [diff] [blame] | 4506 | qual_type->getAs<clang::ObjCObjectPointerType>(); |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 4507 | const clang::ObjCInterfaceType *objc_interface_type = |
| 4508 | objc_class_type->getInterfaceType(); |
| 4509 | if (objc_interface_type && |
Davide Italiano | 52ffb53 | 2017-04-17 18:24:18 +0000 | [diff] [blame] | 4510 | GetCompleteType(static_cast<lldb::opaque_compiler_type_t>( |
| 4511 | const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) { |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 4512 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 4513 | objc_interface_type->getDecl(); |
| 4514 | if (class_interface_decl) { |
| 4515 | num_functions = std::distance(class_interface_decl->meth_begin(), |
| 4516 | class_interface_decl->meth_end()); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4517 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4518 | } |
| 4519 | break; |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 4520 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4521 | |
| 4522 | case clang::Type::ObjCObject: |
| 4523 | case clang::Type::ObjCInterface: |
| 4524 | if (GetCompleteType(type)) { |
| 4525 | const clang::ObjCObjectType *objc_class_type = |
| 4526 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 4527 | if (objc_class_type) { |
| 4528 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 4529 | objc_class_type->getInterface(); |
| 4530 | if (class_interface_decl) |
| 4531 | num_functions = std::distance(class_interface_decl->meth_begin(), |
| 4532 | class_interface_decl->meth_end()); |
| 4533 | } |
| 4534 | } |
| 4535 | break; |
| 4536 | |
| 4537 | case clang::Type::Typedef: |
| 4538 | return CompilerType(getASTContext(), |
| 4539 | llvm::cast<clang::TypedefType>(qual_type) |
| 4540 | ->getDecl() |
| 4541 | ->getUnderlyingType()) |
| 4542 | .GetNumMemberFunctions(); |
| 4543 | |
| 4544 | case clang::Type::Auto: |
| 4545 | return CompilerType( |
| 4546 | getASTContext(), |
| 4547 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) |
| 4548 | .GetNumMemberFunctions(); |
| 4549 | |
| 4550 | case clang::Type::Elaborated: |
| 4551 | return CompilerType( |
| 4552 | getASTContext(), |
| 4553 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) |
| 4554 | .GetNumMemberFunctions(); |
| 4555 | |
| 4556 | case clang::Type::Paren: |
| 4557 | return CompilerType(getASTContext(), |
| 4558 | llvm::cast<clang::ParenType>(qual_type)->desugar()) |
| 4559 | .GetNumMemberFunctions(); |
| 4560 | |
| 4561 | default: |
| 4562 | break; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4563 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4564 | } |
| 4565 | return num_functions; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4566 | } |
| 4567 | |
| 4568 | TypeMemberFunctionImpl |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4569 | ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type, |
| 4570 | size_t idx) { |
| 4571 | std::string name; |
| 4572 | MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown); |
| 4573 | CompilerType clang_type; |
| 4574 | CompilerDecl clang_decl; |
| 4575 | if (type) { |
| 4576 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 4577 | switch (qual_type->getTypeClass()) { |
| 4578 | case clang::Type::Record: |
| 4579 | if (GetCompleteQualType(getASTContext(), qual_type)) { |
| 4580 | const clang::RecordType *record_type = |
| 4581 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 4582 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 4583 | assert(record_decl); |
| 4584 | const clang::CXXRecordDecl *cxx_record_decl = |
| 4585 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 4586 | if (cxx_record_decl) { |
| 4587 | auto method_iter = cxx_record_decl->method_begin(); |
| 4588 | auto method_end = cxx_record_decl->method_end(); |
| 4589 | if (idx < |
| 4590 | static_cast<size_t>(std::distance(method_iter, method_end))) { |
| 4591 | std::advance(method_iter, idx); |
| 4592 | clang::CXXMethodDecl *cxx_method_decl = |
| 4593 | method_iter->getCanonicalDecl(); |
| 4594 | if (cxx_method_decl) { |
| 4595 | name = cxx_method_decl->getDeclName().getAsString(); |
| 4596 | if (cxx_method_decl->isStatic()) |
| 4597 | kind = lldb::eMemberFunctionKindStaticMethod; |
| 4598 | else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl)) |
| 4599 | kind = lldb::eMemberFunctionKindConstructor; |
| 4600 | else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl)) |
| 4601 | kind = lldb::eMemberFunctionKindDestructor; |
| 4602 | else |
| 4603 | kind = lldb::eMemberFunctionKindInstanceMethod; |
| 4604 | clang_type = CompilerType( |
| 4605 | this, cxx_method_decl->getType().getAsOpaquePtr()); |
| 4606 | clang_decl = CompilerDecl(this, cxx_method_decl); |
| 4607 | } |
| 4608 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4609 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4610 | } |
| 4611 | break; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4612 | |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 4613 | case clang::Type::ObjCObjectPointer: { |
| 4614 | const clang::ObjCObjectPointerType *objc_class_type = |
Sean Callanan | 732a6f4 | 2017-05-15 19:55:20 +0000 | [diff] [blame] | 4615 | qual_type->getAs<clang::ObjCObjectPointerType>(); |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 4616 | const clang::ObjCInterfaceType *objc_interface_type = |
| 4617 | objc_class_type->getInterfaceType(); |
| 4618 | if (objc_interface_type && |
Davide Italiano | 52ffb53 | 2017-04-17 18:24:18 +0000 | [diff] [blame] | 4619 | GetCompleteType(static_cast<lldb::opaque_compiler_type_t>( |
| 4620 | const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) { |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 4621 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 4622 | objc_interface_type->getDecl(); |
| 4623 | if (class_interface_decl) { |
| 4624 | auto method_iter = class_interface_decl->meth_begin(); |
| 4625 | auto method_end = class_interface_decl->meth_end(); |
| 4626 | if (idx < |
| 4627 | static_cast<size_t>(std::distance(method_iter, method_end))) { |
| 4628 | std::advance(method_iter, idx); |
| 4629 | clang::ObjCMethodDecl *objc_method_decl = |
| 4630 | method_iter->getCanonicalDecl(); |
| 4631 | if (objc_method_decl) { |
| 4632 | clang_decl = CompilerDecl(this, objc_method_decl); |
| 4633 | name = objc_method_decl->getSelector().getAsString(); |
| 4634 | if (objc_method_decl->isClassMethod()) |
| 4635 | kind = lldb::eMemberFunctionKindStaticMethod; |
| 4636 | else |
| 4637 | kind = lldb::eMemberFunctionKindInstanceMethod; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4638 | } |
| 4639 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4640 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4641 | } |
| 4642 | break; |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 4643 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4644 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4645 | case clang::Type::ObjCObject: |
| 4646 | case clang::Type::ObjCInterface: |
| 4647 | if (GetCompleteType(type)) { |
| 4648 | const clang::ObjCObjectType *objc_class_type = |
| 4649 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 4650 | if (objc_class_type) { |
| 4651 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 4652 | objc_class_type->getInterface(); |
| 4653 | if (class_interface_decl) { |
| 4654 | auto method_iter = class_interface_decl->meth_begin(); |
| 4655 | auto method_end = class_interface_decl->meth_end(); |
| 4656 | if (idx < |
| 4657 | static_cast<size_t>(std::distance(method_iter, method_end))) { |
| 4658 | std::advance(method_iter, idx); |
| 4659 | clang::ObjCMethodDecl *objc_method_decl = |
| 4660 | method_iter->getCanonicalDecl(); |
| 4661 | if (objc_method_decl) { |
| 4662 | clang_decl = CompilerDecl(this, objc_method_decl); |
| 4663 | name = objc_method_decl->getSelector().getAsString(); |
| 4664 | if (objc_method_decl->isClassMethod()) |
| 4665 | kind = lldb::eMemberFunctionKindStaticMethod; |
| 4666 | else |
| 4667 | kind = lldb::eMemberFunctionKindInstanceMethod; |
| 4668 | } |
| 4669 | } |
| 4670 | } |
Ewan Crawford | 27fc7a7 | 2016-03-15 09:50:16 +0000 | [diff] [blame] | 4671 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4672 | } |
| 4673 | break; |
Ewan Crawford | 27fc7a7 | 2016-03-15 09:50:16 +0000 | [diff] [blame] | 4674 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4675 | case clang::Type::Typedef: |
| 4676 | return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type) |
| 4677 | ->getDecl() |
| 4678 | ->getUnderlyingType() |
| 4679 | .getAsOpaquePtr(), |
| 4680 | idx); |
Ewan Crawford | 27fc7a7 | 2016-03-15 09:50:16 +0000 | [diff] [blame] | 4681 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4682 | case clang::Type::Auto: |
| 4683 | return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type) |
| 4684 | ->getDeducedType() |
| 4685 | .getAsOpaquePtr(), |
| 4686 | idx); |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 4687 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4688 | case clang::Type::Elaborated: |
| 4689 | return GetMemberFunctionAtIndex( |
| 4690 | llvm::cast<clang::ElaboratedType>(qual_type) |
| 4691 | ->getNamedType() |
| 4692 | .getAsOpaquePtr(), |
| 4693 | idx); |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 4694 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4695 | case clang::Type::Paren: |
| 4696 | return GetMemberFunctionAtIndex( |
| 4697 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 4698 | idx); |
| 4699 | |
| 4700 | default: |
| 4701 | break; |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 4702 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4703 | } |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 4704 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4705 | if (kind == eMemberFunctionKindUnknown) |
| 4706 | return TypeMemberFunctionImpl(); |
| 4707 | else |
| 4708 | return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind); |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 4709 | } |
| 4710 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 4711 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4712 | ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) { |
| 4713 | if (type) |
| 4714 | return CompilerType(getASTContext(), |
| 4715 | GetQualType(type).getNonReferenceType()); |
| 4716 | return CompilerType(); |
| 4717 | } |
| 4718 | |
| 4719 | CompilerType ClangASTContext::CreateTypedefType( |
| 4720 | const CompilerType &type, const char *typedef_name, |
| 4721 | const CompilerDeclContext &compiler_decl_ctx) { |
| 4722 | if (type && typedef_name && typedef_name[0]) { |
| 4723 | ClangASTContext *ast = |
| 4724 | llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); |
| 4725 | if (!ast) |
| 4726 | return CompilerType(); |
| 4727 | clang::ASTContext *clang_ast = ast->getASTContext(); |
| 4728 | clang::QualType qual_type(ClangUtil::GetQualType(type)); |
| 4729 | |
| 4730 | clang::DeclContext *decl_ctx = |
| 4731 | ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx); |
| 4732 | if (decl_ctx == nullptr) |
| 4733 | decl_ctx = ast->getASTContext()->getTranslationUnitDecl(); |
| 4734 | |
| 4735 | clang::TypedefDecl *decl = clang::TypedefDecl::Create( |
| 4736 | *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(), |
| 4737 | &clang_ast->Idents.get(typedef_name), |
| 4738 | clang_ast->getTrivialTypeSourceInfo(qual_type)); |
| 4739 | |
| 4740 | decl->setAccess(clang::AS_public); // TODO respect proper access specifier |
| 4741 | |
| 4742 | // Get a uniqued clang::QualType for the typedef decl type |
| 4743 | return CompilerType(clang_ast, clang_ast->getTypedefType(decl)); |
| 4744 | } |
| 4745 | return CompilerType(); |
| 4746 | } |
| 4747 | |
| 4748 | CompilerType |
| 4749 | ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) { |
| 4750 | if (type) { |
| 4751 | clang::QualType qual_type(GetQualType(type)); |
| 4752 | return CompilerType(getASTContext(), |
| 4753 | qual_type.getTypePtr()->getPointeeType()); |
| 4754 | } |
| 4755 | return CompilerType(); |
| 4756 | } |
| 4757 | |
| 4758 | CompilerType |
| 4759 | ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) { |
| 4760 | if (type) { |
| 4761 | clang::QualType qual_type(GetQualType(type)); |
| 4762 | |
| 4763 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 4764 | switch (type_class) { |
| 4765 | case clang::Type::ObjCObject: |
| 4766 | case clang::Type::ObjCInterface: |
| 4767 | return CompilerType(getASTContext(), |
| 4768 | getASTContext()->getObjCObjectPointerType(qual_type)); |
| 4769 | |
| 4770 | default: |
| 4771 | return CompilerType(getASTContext(), |
| 4772 | getASTContext()->getPointerType(qual_type)); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4773 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4774 | } |
| 4775 | return CompilerType(); |
| 4776 | } |
| 4777 | |
| 4778 | CompilerType |
| 4779 | ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) { |
| 4780 | if (type) |
| 4781 | return CompilerType(this, getASTContext() |
| 4782 | ->getLValueReferenceType(GetQualType(type)) |
| 4783 | .getAsOpaquePtr()); |
| 4784 | else |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 4785 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4786 | } |
| 4787 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4788 | CompilerType |
| 4789 | ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) { |
| 4790 | if (type) |
| 4791 | return CompilerType(this, getASTContext() |
| 4792 | ->getRValueReferenceType(GetQualType(type)) |
| 4793 | .getAsOpaquePtr()); |
| 4794 | else |
| 4795 | return CompilerType(); |
| 4796 | } |
| 4797 | |
| 4798 | CompilerType |
| 4799 | ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) { |
| 4800 | if (type) { |
| 4801 | clang::QualType result(GetQualType(type)); |
| 4802 | result.addConst(); |
| 4803 | return CompilerType(this, result.getAsOpaquePtr()); |
| 4804 | } |
| 4805 | return CompilerType(); |
| 4806 | } |
| 4807 | |
| 4808 | CompilerType |
| 4809 | ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) { |
| 4810 | if (type) { |
| 4811 | clang::QualType result(GetQualType(type)); |
| 4812 | result.addVolatile(); |
| 4813 | return CompilerType(this, result.getAsOpaquePtr()); |
| 4814 | } |
| 4815 | return CompilerType(); |
| 4816 | } |
| 4817 | |
| 4818 | CompilerType |
| 4819 | ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) { |
| 4820 | if (type) { |
| 4821 | clang::QualType result(GetQualType(type)); |
| 4822 | result.addRestrict(); |
| 4823 | return CompilerType(this, result.getAsOpaquePtr()); |
| 4824 | } |
| 4825 | return CompilerType(); |
| 4826 | } |
| 4827 | |
| 4828 | CompilerType |
| 4829 | ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type, |
| 4830 | const char *typedef_name, |
| 4831 | const CompilerDeclContext &compiler_decl_ctx) { |
| 4832 | if (type) { |
| 4833 | clang::ASTContext *clang_ast = getASTContext(); |
| 4834 | clang::QualType qual_type(GetQualType(type)); |
| 4835 | |
| 4836 | clang::DeclContext *decl_ctx = |
| 4837 | ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx); |
| 4838 | if (decl_ctx == nullptr) |
| 4839 | decl_ctx = getASTContext()->getTranslationUnitDecl(); |
| 4840 | |
| 4841 | clang::TypedefDecl *decl = clang::TypedefDecl::Create( |
| 4842 | *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(), |
| 4843 | &clang_ast->Idents.get(typedef_name), |
| 4844 | clang_ast->getTrivialTypeSourceInfo(qual_type)); |
| 4845 | |
| 4846 | clang::TagDecl *tdecl = nullptr; |
| 4847 | if (!qual_type.isNull()) { |
| 4848 | if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>()) |
| 4849 | tdecl = rt->getDecl(); |
| 4850 | if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>()) |
| 4851 | tdecl = et->getDecl(); |
| 4852 | } |
| 4853 | |
| 4854 | // Check whether this declaration is an anonymous struct, union, or enum, |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4855 | // hidden behind a typedef. If so, we try to check whether we have a |
| 4856 | // typedef tag to attach to the original record declaration |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4857 | if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl()) |
| 4858 | tdecl->setTypedefNameForAnonDecl(decl); |
| 4859 | |
| 4860 | decl->setAccess(clang::AS_public); // TODO respect proper access specifier |
| 4861 | |
| 4862 | // Get a uniqued clang::QualType for the typedef decl type |
| 4863 | return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr()); |
| 4864 | } |
| 4865 | return CompilerType(); |
| 4866 | } |
| 4867 | |
| 4868 | CompilerType |
| 4869 | ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) { |
| 4870 | if (type) { |
| 4871 | const clang::TypedefType *typedef_type = |
| 4872 | llvm::dyn_cast<clang::TypedefType>(GetQualType(type)); |
| 4873 | if (typedef_type) |
| 4874 | return CompilerType(getASTContext(), |
| 4875 | typedef_type->getDecl()->getUnderlyingType()); |
| 4876 | } |
| 4877 | return CompilerType(); |
| 4878 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4879 | |
| 4880 | //---------------------------------------------------------------------- |
| 4881 | // Create related types using the current type's AST |
| 4882 | //---------------------------------------------------------------------- |
| 4883 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4884 | CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) { |
| 4885 | return ClangASTContext::GetBasicType(getASTContext(), basic_type); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4886 | } |
| 4887 | //---------------------------------------------------------------------- |
| 4888 | // Exploring the type |
| 4889 | //---------------------------------------------------------------------- |
| 4890 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4891 | uint64_t ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type, |
| 4892 | ExecutionContextScope *exe_scope) { |
| 4893 | if (GetCompleteType(type)) { |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4894 | clang::QualType qual_type(GetCanonicalQualType(type)); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4895 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4896 | switch (type_class) { |
| 4897 | case clang::Type::Record: |
| 4898 | if (GetCompleteType(type)) |
| 4899 | return getASTContext()->getTypeSize(qual_type); |
| 4900 | else |
| 4901 | return 0; |
| 4902 | break; |
Enrico Granata | 36f51e4 | 2015-12-18 22:41:25 +0000 | [diff] [blame] | 4903 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4904 | case clang::Type::ObjCInterface: |
| 4905 | case clang::Type::ObjCObject: { |
| 4906 | ExecutionContext exe_ctx(exe_scope); |
| 4907 | Process *process = exe_ctx.GetProcessPtr(); |
| 4908 | if (process) { |
| 4909 | ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime(); |
| 4910 | if (objc_runtime) { |
| 4911 | uint64_t bit_size = 0; |
| 4912 | if (objc_runtime->GetTypeBitSize( |
| 4913 | CompilerType(getASTContext(), qual_type), bit_size)) |
| 4914 | return bit_size; |
| 4915 | } |
| 4916 | } else { |
| 4917 | static bool g_printed = false; |
| 4918 | if (!g_printed) { |
| 4919 | StreamString s; |
| 4920 | DumpTypeDescription(type, &s); |
| 4921 | |
| 4922 | llvm::outs() << "warning: trying to determine the size of type "; |
| 4923 | llvm::outs() << s.GetString() << "\n"; |
| 4924 | llvm::outs() << "without a valid ExecutionContext. this is not " |
| 4925 | "reliable. please file a bug against LLDB.\n"; |
| 4926 | llvm::outs() << "backtrace:\n"; |
| 4927 | llvm::sys::PrintStackTrace(llvm::outs()); |
| 4928 | llvm::outs() << "\n"; |
| 4929 | g_printed = true; |
| 4930 | } |
| 4931 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4932 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4933 | LLVM_FALLTHROUGH; |
| 4934 | default: |
| 4935 | const uint32_t bit_size = getASTContext()->getTypeSize(qual_type); |
| 4936 | if (bit_size == 0) { |
| 4937 | if (qual_type->isIncompleteArrayType()) |
| 4938 | return getASTContext()->getTypeSize( |
| 4939 | qual_type->getArrayElementTypeNoTypeQual() |
| 4940 | ->getCanonicalTypeUnqualified()); |
| 4941 | } |
| 4942 | if (qual_type->isObjCObjectOrInterfaceType()) |
| 4943 | return bit_size + |
| 4944 | getASTContext()->getTypeSize( |
| 4945 | getASTContext()->ObjCBuiltinClassTy); |
| 4946 | return bit_size; |
| 4947 | } |
| 4948 | } |
| 4949 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4950 | } |
| 4951 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4952 | size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) { |
| 4953 | if (GetCompleteType(type)) |
| 4954 | return getASTContext()->getTypeAlign(GetQualType(type)); |
| 4955 | return 0; |
| 4956 | } |
| 4957 | |
| 4958 | lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type, |
| 4959 | uint64_t &count) { |
| 4960 | if (!type) |
| 4961 | return lldb::eEncodingInvalid; |
| 4962 | |
| 4963 | count = 1; |
| 4964 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 4965 | |
| 4966 | switch (qual_type->getTypeClass()) { |
| 4967 | case clang::Type::UnaryTransform: |
| 4968 | break; |
| 4969 | |
| 4970 | case clang::Type::FunctionNoProto: |
| 4971 | case clang::Type::FunctionProto: |
| 4972 | break; |
| 4973 | |
| 4974 | case clang::Type::IncompleteArray: |
| 4975 | case clang::Type::VariableArray: |
| 4976 | break; |
| 4977 | |
| 4978 | case clang::Type::ConstantArray: |
| 4979 | break; |
| 4980 | |
Fangrui Song | 8f28488 | 2018-07-13 22:40:40 +0000 | [diff] [blame] | 4981 | case clang::Type::DependentVector: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4982 | case clang::Type::ExtVector: |
| 4983 | case clang::Type::Vector: |
| 4984 | // TODO: Set this to more than one??? |
| 4985 | break; |
| 4986 | |
| 4987 | case clang::Type::Builtin: |
| 4988 | switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { |
| 4989 | case clang::BuiltinType::Void: |
| 4990 | break; |
| 4991 | |
| 4992 | case clang::BuiltinType::Bool: |
| 4993 | case clang::BuiltinType::Char_S: |
| 4994 | case clang::BuiltinType::SChar: |
| 4995 | case clang::BuiltinType::WChar_S: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4996 | case clang::BuiltinType::Short: |
| 4997 | case clang::BuiltinType::Int: |
| 4998 | case clang::BuiltinType::Long: |
| 4999 | case clang::BuiltinType::LongLong: |
| 5000 | case clang::BuiltinType::Int128: |
| 5001 | return lldb::eEncodingSint; |
| 5002 | |
| 5003 | case clang::BuiltinType::Char_U: |
| 5004 | case clang::BuiltinType::UChar: |
| 5005 | case clang::BuiltinType::WChar_U: |
Richard Smith | 51d12d8 | 2018-05-02 02:43:22 +0000 | [diff] [blame] | 5006 | case clang::BuiltinType::Char8: |
| 5007 | case clang::BuiltinType::Char16: |
| 5008 | case clang::BuiltinType::Char32: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5009 | case clang::BuiltinType::UShort: |
| 5010 | case clang::BuiltinType::UInt: |
| 5011 | case clang::BuiltinType::ULong: |
| 5012 | case clang::BuiltinType::ULongLong: |
| 5013 | case clang::BuiltinType::UInt128: |
| 5014 | return lldb::eEncodingUint; |
| 5015 | |
Ilya Biryukov | fc48ac6 | 2018-06-05 10:07:07 +0000 | [diff] [blame] | 5016 | // Fixed point types. Note that they are currently ignored. |
| 5017 | case clang::BuiltinType::ShortAccum: |
| 5018 | case clang::BuiltinType::Accum: |
| 5019 | case clang::BuiltinType::LongAccum: |
| 5020 | case clang::BuiltinType::UShortAccum: |
| 5021 | case clang::BuiltinType::UAccum: |
| 5022 | case clang::BuiltinType::ULongAccum: |
Fangrui Song | a5e59c5 | 2018-06-14 18:19:40 +0000 | [diff] [blame] | 5023 | case clang::BuiltinType::ShortFract: |
Fangrui Song | a5e59c5 | 2018-06-14 18:19:40 +0000 | [diff] [blame] | 5024 | case clang::BuiltinType::Fract: |
| 5025 | case clang::BuiltinType::LongFract: |
| 5026 | case clang::BuiltinType::UShortFract: |
| 5027 | case clang::BuiltinType::UFract: |
| 5028 | case clang::BuiltinType::ULongFract: |
| 5029 | case clang::BuiltinType::SatShortAccum: |
| 5030 | case clang::BuiltinType::SatAccum: |
| 5031 | case clang::BuiltinType::SatLongAccum: |
| 5032 | case clang::BuiltinType::SatUShortAccum: |
| 5033 | case clang::BuiltinType::SatUAccum: |
| 5034 | case clang::BuiltinType::SatULongAccum: |
| 5035 | case clang::BuiltinType::SatShortFract: |
| 5036 | case clang::BuiltinType::SatFract: |
| 5037 | case clang::BuiltinType::SatLongFract: |
| 5038 | case clang::BuiltinType::SatUShortFract: |
| 5039 | case clang::BuiltinType::SatUFract: |
| 5040 | case clang::BuiltinType::SatULongFract: |
Ilya Biryukov | fc48ac6 | 2018-06-05 10:07:07 +0000 | [diff] [blame] | 5041 | break; |
| 5042 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5043 | case clang::BuiltinType::Half: |
| 5044 | case clang::BuiltinType::Float: |
Ted Woodward | 4355c7c | 2017-09-20 19:16:53 +0000 | [diff] [blame] | 5045 | case clang::BuiltinType::Float16: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5046 | case clang::BuiltinType::Float128: |
| 5047 | case clang::BuiltinType::Double: |
| 5048 | case clang::BuiltinType::LongDouble: |
| 5049 | return lldb::eEncodingIEEE754; |
| 5050 | |
| 5051 | case clang::BuiltinType::ObjCClass: |
| 5052 | case clang::BuiltinType::ObjCId: |
| 5053 | case clang::BuiltinType::ObjCSel: |
| 5054 | return lldb::eEncodingUint; |
| 5055 | |
| 5056 | case clang::BuiltinType::NullPtr: |
| 5057 | return lldb::eEncodingUint; |
| 5058 | |
| 5059 | case clang::BuiltinType::Kind::ARCUnbridgedCast: |
| 5060 | case clang::BuiltinType::Kind::BoundMember: |
| 5061 | case clang::BuiltinType::Kind::BuiltinFn: |
| 5062 | case clang::BuiltinType::Kind::Dependent: |
| 5063 | case clang::BuiltinType::Kind::OCLClkEvent: |
| 5064 | case clang::BuiltinType::Kind::OCLEvent: |
| 5065 | case clang::BuiltinType::Kind::OCLImage1dRO: |
| 5066 | case clang::BuiltinType::Kind::OCLImage1dWO: |
| 5067 | case clang::BuiltinType::Kind::OCLImage1dRW: |
| 5068 | case clang::BuiltinType::Kind::OCLImage1dArrayRO: |
| 5069 | case clang::BuiltinType::Kind::OCLImage1dArrayWO: |
| 5070 | case clang::BuiltinType::Kind::OCLImage1dArrayRW: |
| 5071 | case clang::BuiltinType::Kind::OCLImage1dBufferRO: |
| 5072 | case clang::BuiltinType::Kind::OCLImage1dBufferWO: |
| 5073 | case clang::BuiltinType::Kind::OCLImage1dBufferRW: |
| 5074 | case clang::BuiltinType::Kind::OCLImage2dRO: |
| 5075 | case clang::BuiltinType::Kind::OCLImage2dWO: |
| 5076 | case clang::BuiltinType::Kind::OCLImage2dRW: |
| 5077 | case clang::BuiltinType::Kind::OCLImage2dArrayRO: |
| 5078 | case clang::BuiltinType::Kind::OCLImage2dArrayWO: |
| 5079 | case clang::BuiltinType::Kind::OCLImage2dArrayRW: |
| 5080 | case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO: |
| 5081 | case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO: |
| 5082 | case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW: |
| 5083 | case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO: |
| 5084 | case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO: |
| 5085 | case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW: |
| 5086 | case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO: |
| 5087 | case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO: |
| 5088 | case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW: |
| 5089 | case clang::BuiltinType::Kind::OCLImage2dDepthRO: |
| 5090 | case clang::BuiltinType::Kind::OCLImage2dDepthWO: |
| 5091 | case clang::BuiltinType::Kind::OCLImage2dDepthRW: |
| 5092 | case clang::BuiltinType::Kind::OCLImage2dMSAARO: |
| 5093 | case clang::BuiltinType::Kind::OCLImage2dMSAAWO: |
| 5094 | case clang::BuiltinType::Kind::OCLImage2dMSAARW: |
| 5095 | case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO: |
| 5096 | case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO: |
| 5097 | case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW: |
| 5098 | case clang::BuiltinType::Kind::OCLImage3dRO: |
| 5099 | case clang::BuiltinType::Kind::OCLImage3dWO: |
| 5100 | case clang::BuiltinType::Kind::OCLImage3dRW: |
| 5101 | case clang::BuiltinType::Kind::OCLQueue: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5102 | case clang::BuiltinType::Kind::OCLReserveID: |
| 5103 | case clang::BuiltinType::Kind::OCLSampler: |
| 5104 | case clang::BuiltinType::Kind::OMPArraySection: |
| 5105 | case clang::BuiltinType::Kind::Overload: |
| 5106 | case clang::BuiltinType::Kind::PseudoObject: |
| 5107 | case clang::BuiltinType::Kind::UnknownAny: |
| 5108 | break; |
| 5109 | } |
| 5110 | break; |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5111 | // All pointer types are represented as unsigned integer encodings. We may |
| 5112 | // nee to add a eEncodingPointer if we ever need to know the difference |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5113 | case clang::Type::ObjCObjectPointer: |
| 5114 | case clang::Type::BlockPointer: |
| 5115 | case clang::Type::Pointer: |
| 5116 | case clang::Type::LValueReference: |
| 5117 | case clang::Type::RValueReference: |
| 5118 | case clang::Type::MemberPointer: |
| 5119 | return lldb::eEncodingUint; |
| 5120 | case clang::Type::Complex: { |
| 5121 | lldb::Encoding encoding = lldb::eEncodingIEEE754; |
| 5122 | if (qual_type->isComplexType()) |
| 5123 | encoding = lldb::eEncodingIEEE754; |
| 5124 | else { |
| 5125 | const clang::ComplexType *complex_type = |
| 5126 | qual_type->getAsComplexIntegerType(); |
| 5127 | if (complex_type) |
| 5128 | encoding = CompilerType(getASTContext(), complex_type->getElementType()) |
| 5129 | .GetEncoding(count); |
| 5130 | else |
| 5131 | encoding = lldb::eEncodingSint; |
| 5132 | } |
| 5133 | count = 2; |
| 5134 | return encoding; |
| 5135 | } |
| 5136 | |
| 5137 | case clang::Type::ObjCInterface: |
| 5138 | break; |
| 5139 | case clang::Type::Record: |
| 5140 | break; |
| 5141 | case clang::Type::Enum: |
| 5142 | return lldb::eEncodingSint; |
| 5143 | case clang::Type::Typedef: |
| 5144 | return CompilerType(getASTContext(), |
| 5145 | llvm::cast<clang::TypedefType>(qual_type) |
| 5146 | ->getDecl() |
| 5147 | ->getUnderlyingType()) |
| 5148 | .GetEncoding(count); |
| 5149 | |
| 5150 | case clang::Type::Auto: |
| 5151 | return CompilerType( |
| 5152 | getASTContext(), |
| 5153 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) |
| 5154 | .GetEncoding(count); |
| 5155 | |
| 5156 | case clang::Type::Elaborated: |
| 5157 | return CompilerType( |
| 5158 | getASTContext(), |
| 5159 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) |
| 5160 | .GetEncoding(count); |
| 5161 | |
| 5162 | case clang::Type::Paren: |
| 5163 | return CompilerType(getASTContext(), |
| 5164 | llvm::cast<clang::ParenType>(qual_type)->desugar()) |
| 5165 | .GetEncoding(count); |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 5166 | case clang::Type::TypeOfExpr: |
| 5167 | return CompilerType(getASTContext(), |
| 5168 | llvm::cast<clang::TypeOfExprType>(qual_type) |
| 5169 | ->getUnderlyingExpr() |
| 5170 | ->getType()) |
| 5171 | .GetEncoding(count); |
| 5172 | case clang::Type::TypeOf: |
| 5173 | return CompilerType( |
| 5174 | getASTContext(), |
| 5175 | llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType()) |
| 5176 | .GetEncoding(count); |
| 5177 | case clang::Type::Decltype: |
| 5178 | return CompilerType( |
| 5179 | getASTContext(), |
| 5180 | llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType()) |
| 5181 | .GetEncoding(count); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5182 | case clang::Type::DependentSizedArray: |
| 5183 | case clang::Type::DependentSizedExtVector: |
| 5184 | case clang::Type::UnresolvedUsing: |
| 5185 | case clang::Type::Attributed: |
| 5186 | case clang::Type::TemplateTypeParm: |
| 5187 | case clang::Type::SubstTemplateTypeParm: |
| 5188 | case clang::Type::SubstTemplateTypeParmPack: |
| 5189 | case clang::Type::InjectedClassName: |
| 5190 | case clang::Type::DependentName: |
| 5191 | case clang::Type::DependentTemplateSpecialization: |
| 5192 | case clang::Type::PackExpansion: |
| 5193 | case clang::Type::ObjCObject: |
| 5194 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5195 | case clang::Type::TemplateSpecialization: |
Pavel Labath | 4f19fce2 | 2017-02-17 13:39:50 +0000 | [diff] [blame] | 5196 | case clang::Type::DeducedTemplateSpecialization: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5197 | case clang::Type::Atomic: |
| 5198 | case clang::Type::Adjusted: |
| 5199 | case clang::Type::Pipe: |
| 5200 | break; |
| 5201 | |
| 5202 | // pointer type decayed from an array or function type. |
| 5203 | case clang::Type::Decayed: |
| 5204 | break; |
Zachary Turner | 5a8ad459 | 2016-10-05 17:07:34 +0000 | [diff] [blame] | 5205 | case clang::Type::ObjCTypeParam: |
| 5206 | break; |
Ted Woodward | 66060cf | 2017-10-11 22:42:21 +0000 | [diff] [blame] | 5207 | |
| 5208 | case clang::Type::DependentAddressSpace: |
| 5209 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5210 | } |
| 5211 | count = 0; |
| 5212 | return lldb::eEncodingInvalid; |
| 5213 | } |
| 5214 | |
| 5215 | lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) { |
| 5216 | if (!type) |
| 5217 | return lldb::eFormatDefault; |
| 5218 | |
| 5219 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 5220 | |
| 5221 | switch (qual_type->getTypeClass()) { |
| 5222 | case clang::Type::UnaryTransform: |
| 5223 | break; |
| 5224 | |
| 5225 | case clang::Type::FunctionNoProto: |
| 5226 | case clang::Type::FunctionProto: |
| 5227 | break; |
| 5228 | |
| 5229 | case clang::Type::IncompleteArray: |
| 5230 | case clang::Type::VariableArray: |
| 5231 | break; |
| 5232 | |
| 5233 | case clang::Type::ConstantArray: |
| 5234 | return lldb::eFormatVoid; // no value |
| 5235 | |
Fangrui Song | 8f28488 | 2018-07-13 22:40:40 +0000 | [diff] [blame] | 5236 | case clang::Type::DependentVector: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5237 | case clang::Type::ExtVector: |
| 5238 | case clang::Type::Vector: |
| 5239 | break; |
| 5240 | |
| 5241 | case clang::Type::Builtin: |
| 5242 | switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { |
| 5243 | // default: assert(0 && "Unknown builtin type!"); |
| 5244 | case clang::BuiltinType::UnknownAny: |
| 5245 | case clang::BuiltinType::Void: |
| 5246 | case clang::BuiltinType::BoundMember: |
| 5247 | break; |
| 5248 | |
| 5249 | case clang::BuiltinType::Bool: |
| 5250 | return lldb::eFormatBoolean; |
| 5251 | case clang::BuiltinType::Char_S: |
| 5252 | case clang::BuiltinType::SChar: |
| 5253 | case clang::BuiltinType::WChar_S: |
| 5254 | case clang::BuiltinType::Char_U: |
| 5255 | case clang::BuiltinType::UChar: |
| 5256 | case clang::BuiltinType::WChar_U: |
| 5257 | return lldb::eFormatChar; |
| 5258 | case clang::BuiltinType::Char16: |
| 5259 | return lldb::eFormatUnicode16; |
| 5260 | case clang::BuiltinType::Char32: |
| 5261 | return lldb::eFormatUnicode32; |
| 5262 | case clang::BuiltinType::UShort: |
| 5263 | return lldb::eFormatUnsigned; |
| 5264 | case clang::BuiltinType::Short: |
| 5265 | return lldb::eFormatDecimal; |
| 5266 | case clang::BuiltinType::UInt: |
| 5267 | return lldb::eFormatUnsigned; |
| 5268 | case clang::BuiltinType::Int: |
| 5269 | return lldb::eFormatDecimal; |
| 5270 | case clang::BuiltinType::ULong: |
| 5271 | return lldb::eFormatUnsigned; |
| 5272 | case clang::BuiltinType::Long: |
| 5273 | return lldb::eFormatDecimal; |
| 5274 | case clang::BuiltinType::ULongLong: |
| 5275 | return lldb::eFormatUnsigned; |
| 5276 | case clang::BuiltinType::LongLong: |
| 5277 | return lldb::eFormatDecimal; |
| 5278 | case clang::BuiltinType::UInt128: |
| 5279 | return lldb::eFormatUnsigned; |
| 5280 | case clang::BuiltinType::Int128: |
| 5281 | return lldb::eFormatDecimal; |
| 5282 | case clang::BuiltinType::Half: |
| 5283 | case clang::BuiltinType::Float: |
| 5284 | case clang::BuiltinType::Double: |
| 5285 | case clang::BuiltinType::LongDouble: |
| 5286 | return lldb::eFormatFloat; |
| 5287 | default: |
| 5288 | return lldb::eFormatHex; |
| 5289 | } |
| 5290 | break; |
| 5291 | case clang::Type::ObjCObjectPointer: |
| 5292 | return lldb::eFormatHex; |
| 5293 | case clang::Type::BlockPointer: |
| 5294 | return lldb::eFormatHex; |
| 5295 | case clang::Type::Pointer: |
| 5296 | return lldb::eFormatHex; |
| 5297 | case clang::Type::LValueReference: |
| 5298 | case clang::Type::RValueReference: |
| 5299 | return lldb::eFormatHex; |
| 5300 | case clang::Type::MemberPointer: |
| 5301 | break; |
| 5302 | case clang::Type::Complex: { |
| 5303 | if (qual_type->isComplexType()) |
| 5304 | return lldb::eFormatComplex; |
| 5305 | else |
| 5306 | return lldb::eFormatComplexInteger; |
| 5307 | } |
| 5308 | case clang::Type::ObjCInterface: |
| 5309 | break; |
| 5310 | case clang::Type::Record: |
| 5311 | break; |
| 5312 | case clang::Type::Enum: |
| 5313 | return lldb::eFormatEnum; |
| 5314 | case clang::Type::Typedef: |
| 5315 | return CompilerType(getASTContext(), |
| 5316 | llvm::cast<clang::TypedefType>(qual_type) |
| 5317 | ->getDecl() |
| 5318 | ->getUnderlyingType()) |
| 5319 | .GetFormat(); |
| 5320 | case clang::Type::Auto: |
| 5321 | return CompilerType(getASTContext(), |
| 5322 | llvm::cast<clang::AutoType>(qual_type)->desugar()) |
| 5323 | .GetFormat(); |
| 5324 | case clang::Type::Paren: |
| 5325 | return CompilerType(getASTContext(), |
| 5326 | llvm::cast<clang::ParenType>(qual_type)->desugar()) |
| 5327 | .GetFormat(); |
| 5328 | case clang::Type::Elaborated: |
| 5329 | return CompilerType( |
| 5330 | getASTContext(), |
| 5331 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) |
| 5332 | .GetFormat(); |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 5333 | case clang::Type::TypeOfExpr: |
| 5334 | return CompilerType(getASTContext(), |
| 5335 | llvm::cast<clang::TypeOfExprType>(qual_type) |
| 5336 | ->getUnderlyingExpr() |
| 5337 | ->getType()) |
| 5338 | .GetFormat(); |
| 5339 | case clang::Type::TypeOf: |
| 5340 | return CompilerType( |
| 5341 | getASTContext(), |
| 5342 | llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType()) |
| 5343 | .GetFormat(); |
| 5344 | case clang::Type::Decltype: |
| 5345 | return CompilerType( |
| 5346 | getASTContext(), |
| 5347 | llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType()) |
| 5348 | .GetFormat(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5349 | case clang::Type::DependentSizedArray: |
| 5350 | case clang::Type::DependentSizedExtVector: |
| 5351 | case clang::Type::UnresolvedUsing: |
| 5352 | case clang::Type::Attributed: |
| 5353 | case clang::Type::TemplateTypeParm: |
| 5354 | case clang::Type::SubstTemplateTypeParm: |
| 5355 | case clang::Type::SubstTemplateTypeParmPack: |
| 5356 | case clang::Type::InjectedClassName: |
| 5357 | case clang::Type::DependentName: |
| 5358 | case clang::Type::DependentTemplateSpecialization: |
| 5359 | case clang::Type::PackExpansion: |
| 5360 | case clang::Type::ObjCObject: |
| 5361 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5362 | case clang::Type::TemplateSpecialization: |
Pavel Labath | 4f19fce2 | 2017-02-17 13:39:50 +0000 | [diff] [blame] | 5363 | case clang::Type::DeducedTemplateSpecialization: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5364 | case clang::Type::Atomic: |
| 5365 | case clang::Type::Adjusted: |
| 5366 | case clang::Type::Pipe: |
| 5367 | break; |
| 5368 | |
| 5369 | // pointer type decayed from an array or function type. |
| 5370 | case clang::Type::Decayed: |
| 5371 | break; |
Zachary Turner | 5a8ad459 | 2016-10-05 17:07:34 +0000 | [diff] [blame] | 5372 | case clang::Type::ObjCTypeParam: |
| 5373 | break; |
Ted Woodward | 66060cf | 2017-10-11 22:42:21 +0000 | [diff] [blame] | 5374 | |
| 5375 | case clang::Type::DependentAddressSpace: |
| 5376 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5377 | } |
| 5378 | // We don't know hot to display this type... |
| 5379 | return lldb::eFormatBytes; |
| 5380 | } |
| 5381 | |
| 5382 | static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl, |
| 5383 | bool check_superclass) { |
| 5384 | while (class_interface_decl) { |
| 5385 | if (class_interface_decl->ivar_size() > 0) |
| 5386 | return true; |
| 5387 | |
| 5388 | if (check_superclass) |
| 5389 | class_interface_decl = class_interface_decl->getSuperClass(); |
| 5390 | else |
| 5391 | break; |
| 5392 | } |
| 5393 | return false; |
| 5394 | } |
| 5395 | |
| 5396 | uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type, |
| 5397 | bool omit_empty_base_classes) { |
| 5398 | if (!type) |
| 5399 | return 0; |
| 5400 | |
| 5401 | uint32_t num_children = 0; |
| 5402 | clang::QualType qual_type(GetQualType(type)); |
| 5403 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 5404 | switch (type_class) { |
| 5405 | case clang::Type::Builtin: |
| 5406 | switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { |
| 5407 | case clang::BuiltinType::ObjCId: // child is Class |
| 5408 | case clang::BuiltinType::ObjCClass: // child is Class |
| 5409 | num_children = 1; |
| 5410 | break; |
| 5411 | |
| 5412 | default: |
| 5413 | break; |
| 5414 | } |
| 5415 | break; |
| 5416 | |
| 5417 | case clang::Type::Complex: |
| 5418 | return 0; |
| 5419 | |
| 5420 | case clang::Type::Record: |
| 5421 | if (GetCompleteQualType(getASTContext(), qual_type)) { |
| 5422 | const clang::RecordType *record_type = |
| 5423 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 5424 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 5425 | assert(record_decl); |
| 5426 | const clang::CXXRecordDecl *cxx_record_decl = |
| 5427 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 5428 | if (cxx_record_decl) { |
| 5429 | if (omit_empty_base_classes) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5430 | // Check each base classes to see if it or any of its base classes |
| 5431 | // contain any fields. This can help limit the noise in variable |
| 5432 | // views by not having to show base classes that contain no members. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5433 | clang::CXXRecordDecl::base_class_const_iterator base_class, |
| 5434 | base_class_end; |
| 5435 | for (base_class = cxx_record_decl->bases_begin(), |
| 5436 | base_class_end = cxx_record_decl->bases_end(); |
| 5437 | base_class != base_class_end; ++base_class) { |
| 5438 | const clang::CXXRecordDecl *base_class_decl = |
| 5439 | llvm::cast<clang::CXXRecordDecl>( |
| 5440 | base_class->getType() |
| 5441 | ->getAs<clang::RecordType>() |
| 5442 | ->getDecl()); |
| 5443 | |
| 5444 | // Skip empty base classes |
| 5445 | if (ClangASTContext::RecordHasFields(base_class_decl) == false) |
| 5446 | continue; |
| 5447 | |
| 5448 | num_children++; |
| 5449 | } |
| 5450 | } else { |
| 5451 | // Include all base classes |
| 5452 | num_children += cxx_record_decl->getNumBases(); |
| 5453 | } |
| 5454 | } |
| 5455 | clang::RecordDecl::field_iterator field, field_end; |
| 5456 | for (field = record_decl->field_begin(), |
| 5457 | field_end = record_decl->field_end(); |
| 5458 | field != field_end; ++field) |
| 5459 | ++num_children; |
| 5460 | } |
| 5461 | break; |
| 5462 | |
| 5463 | case clang::Type::ObjCObject: |
| 5464 | case clang::Type::ObjCInterface: |
| 5465 | if (GetCompleteQualType(getASTContext(), qual_type)) { |
| 5466 | const clang::ObjCObjectType *objc_class_type = |
| 5467 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 5468 | assert(objc_class_type); |
| 5469 | if (objc_class_type) { |
| 5470 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 5471 | objc_class_type->getInterface(); |
| 5472 | |
| 5473 | if (class_interface_decl) { |
| 5474 | |
| 5475 | clang::ObjCInterfaceDecl *superclass_interface_decl = |
| 5476 | class_interface_decl->getSuperClass(); |
| 5477 | if (superclass_interface_decl) { |
| 5478 | if (omit_empty_base_classes) { |
| 5479 | if (ObjCDeclHasIVars(superclass_interface_decl, true)) |
| 5480 | ++num_children; |
| 5481 | } else |
| 5482 | ++num_children; |
| 5483 | } |
| 5484 | |
| 5485 | num_children += class_interface_decl->ivar_size(); |
| 5486 | } |
| 5487 | } |
| 5488 | } |
| 5489 | break; |
| 5490 | |
| 5491 | case clang::Type::ObjCObjectPointer: { |
| 5492 | const clang::ObjCObjectPointerType *pointer_type = |
| 5493 | llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr()); |
| 5494 | clang::QualType pointee_type = pointer_type->getPointeeType(); |
| 5495 | uint32_t num_pointee_children = |
| 5496 | CompilerType(getASTContext(), pointee_type) |
| 5497 | .GetNumChildren(omit_empty_base_classes); |
| 5498 | // If this type points to a simple type, then it has 1 child |
| 5499 | if (num_pointee_children == 0) |
| 5500 | num_children = 1; |
| 5501 | else |
| 5502 | num_children = num_pointee_children; |
| 5503 | } break; |
| 5504 | |
| 5505 | case clang::Type::Vector: |
| 5506 | case clang::Type::ExtVector: |
| 5507 | num_children = |
| 5508 | llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements(); |
| 5509 | break; |
| 5510 | |
| 5511 | case clang::Type::ConstantArray: |
| 5512 | num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr()) |
| 5513 | ->getSize() |
| 5514 | .getLimitedValue(); |
| 5515 | break; |
| 5516 | |
| 5517 | case clang::Type::Pointer: { |
| 5518 | const clang::PointerType *pointer_type = |
| 5519 | llvm::cast<clang::PointerType>(qual_type.getTypePtr()); |
| 5520 | clang::QualType pointee_type(pointer_type->getPointeeType()); |
| 5521 | uint32_t num_pointee_children = |
| 5522 | CompilerType(getASTContext(), pointee_type) |
| 5523 | .GetNumChildren(omit_empty_base_classes); |
| 5524 | if (num_pointee_children == 0) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5525 | // We have a pointer to a pointee type that claims it has no children. We |
| 5526 | // will want to look at |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5527 | num_children = GetNumPointeeChildren(pointee_type); |
| 5528 | } else |
| 5529 | num_children = num_pointee_children; |
| 5530 | } break; |
| 5531 | |
| 5532 | case clang::Type::LValueReference: |
| 5533 | case clang::Type::RValueReference: { |
| 5534 | const clang::ReferenceType *reference_type = |
| 5535 | llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()); |
| 5536 | clang::QualType pointee_type = reference_type->getPointeeType(); |
| 5537 | uint32_t num_pointee_children = |
| 5538 | CompilerType(getASTContext(), pointee_type) |
| 5539 | .GetNumChildren(omit_empty_base_classes); |
| 5540 | // If this type points to a simple type, then it has 1 child |
| 5541 | if (num_pointee_children == 0) |
| 5542 | num_children = 1; |
| 5543 | else |
| 5544 | num_children = num_pointee_children; |
| 5545 | } break; |
| 5546 | |
| 5547 | case clang::Type::Typedef: |
| 5548 | num_children = |
| 5549 | CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type) |
| 5550 | ->getDecl() |
| 5551 | ->getUnderlyingType()) |
| 5552 | .GetNumChildren(omit_empty_base_classes); |
| 5553 | break; |
| 5554 | |
| 5555 | case clang::Type::Auto: |
| 5556 | num_children = |
| 5557 | CompilerType(getASTContext(), |
| 5558 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) |
| 5559 | .GetNumChildren(omit_empty_base_classes); |
| 5560 | break; |
| 5561 | |
| 5562 | case clang::Type::Elaborated: |
| 5563 | num_children = |
| 5564 | CompilerType( |
| 5565 | getASTContext(), |
| 5566 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) |
| 5567 | .GetNumChildren(omit_empty_base_classes); |
| 5568 | break; |
| 5569 | |
| 5570 | case clang::Type::Paren: |
| 5571 | num_children = |
| 5572 | CompilerType(getASTContext(), |
| 5573 | llvm::cast<clang::ParenType>(qual_type)->desugar()) |
| 5574 | .GetNumChildren(omit_empty_base_classes); |
| 5575 | break; |
| 5576 | default: |
| 5577 | break; |
| 5578 | } |
| 5579 | return num_children; |
| 5580 | } |
| 5581 | |
| 5582 | CompilerType ClangASTContext::GetBuiltinTypeByName(const ConstString &name) { |
| 5583 | return GetBasicType(GetBasicTypeEnumeration(name)); |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 5584 | } |
| 5585 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5586 | lldb::BasicType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5587 | ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) { |
| 5588 | if (type) { |
| 5589 | clang::QualType qual_type(GetQualType(type)); |
| 5590 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 5591 | if (type_class == clang::Type::Builtin) { |
| 5592 | switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { |
| 5593 | case clang::BuiltinType::Void: |
| 5594 | return eBasicTypeVoid; |
| 5595 | case clang::BuiltinType::Bool: |
| 5596 | return eBasicTypeBool; |
| 5597 | case clang::BuiltinType::Char_S: |
| 5598 | return eBasicTypeSignedChar; |
| 5599 | case clang::BuiltinType::Char_U: |
| 5600 | return eBasicTypeUnsignedChar; |
| 5601 | case clang::BuiltinType::Char16: |
| 5602 | return eBasicTypeChar16; |
| 5603 | case clang::BuiltinType::Char32: |
| 5604 | return eBasicTypeChar32; |
| 5605 | case clang::BuiltinType::UChar: |
| 5606 | return eBasicTypeUnsignedChar; |
| 5607 | case clang::BuiltinType::SChar: |
| 5608 | return eBasicTypeSignedChar; |
| 5609 | case clang::BuiltinType::WChar_S: |
| 5610 | return eBasicTypeSignedWChar; |
| 5611 | case clang::BuiltinType::WChar_U: |
| 5612 | return eBasicTypeUnsignedWChar; |
| 5613 | case clang::BuiltinType::Short: |
| 5614 | return eBasicTypeShort; |
| 5615 | case clang::BuiltinType::UShort: |
| 5616 | return eBasicTypeUnsignedShort; |
| 5617 | case clang::BuiltinType::Int: |
| 5618 | return eBasicTypeInt; |
| 5619 | case clang::BuiltinType::UInt: |
| 5620 | return eBasicTypeUnsignedInt; |
| 5621 | case clang::BuiltinType::Long: |
| 5622 | return eBasicTypeLong; |
| 5623 | case clang::BuiltinType::ULong: |
| 5624 | return eBasicTypeUnsignedLong; |
| 5625 | case clang::BuiltinType::LongLong: |
| 5626 | return eBasicTypeLongLong; |
| 5627 | case clang::BuiltinType::ULongLong: |
| 5628 | return eBasicTypeUnsignedLongLong; |
| 5629 | case clang::BuiltinType::Int128: |
| 5630 | return eBasicTypeInt128; |
| 5631 | case clang::BuiltinType::UInt128: |
| 5632 | return eBasicTypeUnsignedInt128; |
| 5633 | |
| 5634 | case clang::BuiltinType::Half: |
| 5635 | return eBasicTypeHalf; |
| 5636 | case clang::BuiltinType::Float: |
| 5637 | return eBasicTypeFloat; |
| 5638 | case clang::BuiltinType::Double: |
| 5639 | return eBasicTypeDouble; |
| 5640 | case clang::BuiltinType::LongDouble: |
| 5641 | return eBasicTypeLongDouble; |
| 5642 | |
| 5643 | case clang::BuiltinType::NullPtr: |
| 5644 | return eBasicTypeNullPtr; |
| 5645 | case clang::BuiltinType::ObjCId: |
| 5646 | return eBasicTypeObjCID; |
| 5647 | case clang::BuiltinType::ObjCClass: |
| 5648 | return eBasicTypeObjCClass; |
| 5649 | case clang::BuiltinType::ObjCSel: |
| 5650 | return eBasicTypeObjCSel; |
| 5651 | default: |
| 5652 | return eBasicTypeOther; |
| 5653 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5654 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5655 | } |
| 5656 | return eBasicTypeInvalid; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5657 | } |
| 5658 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5659 | void ClangASTContext::ForEachEnumerator( |
| 5660 | lldb::opaque_compiler_type_t type, |
| 5661 | std::function<bool(const CompilerType &integer_type, |
| 5662 | const ConstString &name, |
| 5663 | const llvm::APSInt &value)> const &callback) { |
| 5664 | const clang::EnumType *enum_type = |
| 5665 | llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type)); |
| 5666 | if (enum_type) { |
| 5667 | const clang::EnumDecl *enum_decl = enum_type->getDecl(); |
| 5668 | if (enum_decl) { |
| 5669 | CompilerType integer_type(this, |
| 5670 | enum_decl->getIntegerType().getAsOpaquePtr()); |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 5671 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5672 | clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos; |
| 5673 | for (enum_pos = enum_decl->enumerator_begin(), |
| 5674 | enum_end_pos = enum_decl->enumerator_end(); |
| 5675 | enum_pos != enum_end_pos; ++enum_pos) { |
| 5676 | ConstString name(enum_pos->getNameAsString().c_str()); |
| 5677 | if (!callback(integer_type, name, enum_pos->getInitVal())) |
| 5678 | break; |
| 5679 | } |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 5680 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5681 | } |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 5682 | } |
| 5683 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5684 | #pragma mark Aggregate Types |
| 5685 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5686 | uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) { |
| 5687 | if (!type) |
| 5688 | return 0; |
Enrico Granata | 36f51e4 | 2015-12-18 22:41:25 +0000 | [diff] [blame] | 5689 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5690 | uint32_t count = 0; |
| 5691 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 5692 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 5693 | switch (type_class) { |
| 5694 | case clang::Type::Record: |
| 5695 | if (GetCompleteType(type)) { |
| 5696 | const clang::RecordType *record_type = |
| 5697 | llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr()); |
| 5698 | if (record_type) { |
| 5699 | clang::RecordDecl *record_decl = record_type->getDecl(); |
| 5700 | if (record_decl) { |
| 5701 | uint32_t field_idx = 0; |
| 5702 | clang::RecordDecl::field_iterator field, field_end; |
| 5703 | for (field = record_decl->field_begin(), |
| 5704 | field_end = record_decl->field_end(); |
| 5705 | field != field_end; ++field) |
| 5706 | ++field_idx; |
| 5707 | count = field_idx; |
| 5708 | } |
| 5709 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5710 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5711 | break; |
| 5712 | |
| 5713 | case clang::Type::Typedef: |
| 5714 | count = |
| 5715 | CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type) |
| 5716 | ->getDecl() |
| 5717 | ->getUnderlyingType()) |
| 5718 | .GetNumFields(); |
| 5719 | break; |
| 5720 | |
| 5721 | case clang::Type::Auto: |
| 5722 | count = |
| 5723 | CompilerType(getASTContext(), |
| 5724 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) |
| 5725 | .GetNumFields(); |
| 5726 | break; |
| 5727 | |
| 5728 | case clang::Type::Elaborated: |
| 5729 | count = CompilerType( |
| 5730 | getASTContext(), |
| 5731 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) |
| 5732 | .GetNumFields(); |
| 5733 | break; |
| 5734 | |
| 5735 | case clang::Type::Paren: |
| 5736 | count = CompilerType(getASTContext(), |
| 5737 | llvm::cast<clang::ParenType>(qual_type)->desugar()) |
| 5738 | .GetNumFields(); |
| 5739 | break; |
| 5740 | |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 5741 | case clang::Type::ObjCObjectPointer: { |
| 5742 | const clang::ObjCObjectPointerType *objc_class_type = |
Sean Callanan | 732a6f4 | 2017-05-15 19:55:20 +0000 | [diff] [blame] | 5743 | qual_type->getAs<clang::ObjCObjectPointerType>(); |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 5744 | const clang::ObjCInterfaceType *objc_interface_type = |
| 5745 | objc_class_type->getInterfaceType(); |
| 5746 | if (objc_interface_type && |
Davide Italiano | 52ffb53 | 2017-04-17 18:24:18 +0000 | [diff] [blame] | 5747 | GetCompleteType(static_cast<lldb::opaque_compiler_type_t>( |
| 5748 | const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) { |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 5749 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 5750 | objc_interface_type->getDecl(); |
| 5751 | if (class_interface_decl) { |
| 5752 | count = class_interface_decl->ivar_size(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5753 | } |
| 5754 | } |
| 5755 | break; |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 5756 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5757 | |
| 5758 | case clang::Type::ObjCObject: |
| 5759 | case clang::Type::ObjCInterface: |
| 5760 | if (GetCompleteType(type)) { |
| 5761 | const clang::ObjCObjectType *objc_class_type = |
| 5762 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 5763 | if (objc_class_type) { |
| 5764 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 5765 | objc_class_type->getInterface(); |
| 5766 | |
| 5767 | if (class_interface_decl) |
| 5768 | count = class_interface_decl->ivar_size(); |
| 5769 | } |
| 5770 | } |
| 5771 | break; |
| 5772 | |
| 5773 | default: |
| 5774 | break; |
| 5775 | } |
| 5776 | return count; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5777 | } |
| 5778 | |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 5779 | static lldb::opaque_compiler_type_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5780 | GetObjCFieldAtIndex(clang::ASTContext *ast, |
| 5781 | clang::ObjCInterfaceDecl *class_interface_decl, size_t idx, |
| 5782 | std::string &name, uint64_t *bit_offset_ptr, |
| 5783 | uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) { |
| 5784 | if (class_interface_decl) { |
| 5785 | if (idx < (class_interface_decl->ivar_size())) { |
| 5786 | clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, |
| 5787 | ivar_end = class_interface_decl->ivar_end(); |
| 5788 | uint32_t ivar_idx = 0; |
| 5789 | |
| 5790 | for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; |
| 5791 | ++ivar_pos, ++ivar_idx) { |
| 5792 | if (ivar_idx == idx) { |
| 5793 | const clang::ObjCIvarDecl *ivar_decl = *ivar_pos; |
| 5794 | |
| 5795 | clang::QualType ivar_qual_type(ivar_decl->getType()); |
| 5796 | |
| 5797 | name.assign(ivar_decl->getNameAsString()); |
| 5798 | |
| 5799 | if (bit_offset_ptr) { |
| 5800 | const clang::ASTRecordLayout &interface_layout = |
| 5801 | ast->getASTObjCInterfaceLayout(class_interface_decl); |
| 5802 | *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx); |
| 5803 | } |
| 5804 | |
| 5805 | const bool is_bitfield = ivar_pos->isBitField(); |
| 5806 | |
| 5807 | if (bitfield_bit_size_ptr) { |
| 5808 | *bitfield_bit_size_ptr = 0; |
| 5809 | |
| 5810 | if (is_bitfield && ast) { |
| 5811 | clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth(); |
| 5812 | llvm::APSInt bitfield_apsint; |
| 5813 | if (bitfield_bit_size_expr && |
| 5814 | bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, |
| 5815 | *ast)) { |
| 5816 | *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue(); |
| 5817 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5818 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5819 | } |
| 5820 | if (is_bitfield_ptr) |
| 5821 | *is_bitfield_ptr = is_bitfield; |
| 5822 | |
| 5823 | return ivar_qual_type.getAsOpaquePtr(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5824 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5825 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5826 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5827 | } |
| 5828 | return nullptr; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5829 | } |
| 5830 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5831 | CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type, |
| 5832 | size_t idx, std::string &name, |
| 5833 | uint64_t *bit_offset_ptr, |
| 5834 | uint32_t *bitfield_bit_size_ptr, |
| 5835 | bool *is_bitfield_ptr) { |
| 5836 | if (!type) |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 5837 | return CompilerType(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5838 | |
| 5839 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 5840 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 5841 | switch (type_class) { |
| 5842 | case clang::Type::Record: |
| 5843 | if (GetCompleteType(type)) { |
| 5844 | const clang::RecordType *record_type = |
| 5845 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 5846 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 5847 | uint32_t field_idx = 0; |
| 5848 | clang::RecordDecl::field_iterator field, field_end; |
| 5849 | for (field = record_decl->field_begin(), |
| 5850 | field_end = record_decl->field_end(); |
| 5851 | field != field_end; ++field, ++field_idx) { |
| 5852 | if (idx == field_idx) { |
| 5853 | // Print the member type if requested |
| 5854 | // Print the member name and equal sign |
| 5855 | name.assign(field->getNameAsString()); |
| 5856 | |
| 5857 | // Figure out the type byte size (field_type_info.first) and |
| 5858 | // alignment (field_type_info.second) from the AST context. |
| 5859 | if (bit_offset_ptr) { |
| 5860 | const clang::ASTRecordLayout &record_layout = |
| 5861 | getASTContext()->getASTRecordLayout(record_decl); |
| 5862 | *bit_offset_ptr = record_layout.getFieldOffset(field_idx); |
| 5863 | } |
| 5864 | |
| 5865 | const bool is_bitfield = field->isBitField(); |
| 5866 | |
| 5867 | if (bitfield_bit_size_ptr) { |
| 5868 | *bitfield_bit_size_ptr = 0; |
| 5869 | |
| 5870 | if (is_bitfield) { |
| 5871 | clang::Expr *bitfield_bit_size_expr = field->getBitWidth(); |
| 5872 | llvm::APSInt bitfield_apsint; |
| 5873 | if (bitfield_bit_size_expr && |
| 5874 | bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, |
| 5875 | *getASTContext())) { |
| 5876 | *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue(); |
| 5877 | } |
| 5878 | } |
| 5879 | } |
| 5880 | if (is_bitfield_ptr) |
| 5881 | *is_bitfield_ptr = is_bitfield; |
| 5882 | |
| 5883 | return CompilerType(getASTContext(), field->getType()); |
| 5884 | } |
| 5885 | } |
| 5886 | } |
| 5887 | break; |
| 5888 | |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 5889 | case clang::Type::ObjCObjectPointer: { |
| 5890 | const clang::ObjCObjectPointerType *objc_class_type = |
Sean Callanan | 732a6f4 | 2017-05-15 19:55:20 +0000 | [diff] [blame] | 5891 | qual_type->getAs<clang::ObjCObjectPointerType>(); |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 5892 | const clang::ObjCInterfaceType *objc_interface_type = |
| 5893 | objc_class_type->getInterfaceType(); |
| 5894 | if (objc_interface_type && |
Davide Italiano | 52ffb53 | 2017-04-17 18:24:18 +0000 | [diff] [blame] | 5895 | GetCompleteType(static_cast<lldb::opaque_compiler_type_t>( |
| 5896 | const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) { |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 5897 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 5898 | objc_interface_type->getDecl(); |
| 5899 | if (class_interface_decl) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5900 | return CompilerType( |
| 5901 | this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl, |
| 5902 | idx, name, bit_offset_ptr, |
| 5903 | bitfield_bit_size_ptr, is_bitfield_ptr)); |
| 5904 | } |
| 5905 | } |
| 5906 | break; |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 5907 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5908 | |
| 5909 | case clang::Type::ObjCObject: |
| 5910 | case clang::Type::ObjCInterface: |
| 5911 | if (GetCompleteType(type)) { |
| 5912 | const clang::ObjCObjectType *objc_class_type = |
| 5913 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 5914 | assert(objc_class_type); |
| 5915 | if (objc_class_type) { |
| 5916 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 5917 | objc_class_type->getInterface(); |
| 5918 | return CompilerType( |
| 5919 | this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl, |
| 5920 | idx, name, bit_offset_ptr, |
| 5921 | bitfield_bit_size_ptr, is_bitfield_ptr)); |
| 5922 | } |
| 5923 | } |
| 5924 | break; |
| 5925 | |
| 5926 | case clang::Type::Typedef: |
| 5927 | return CompilerType(getASTContext(), |
| 5928 | llvm::cast<clang::TypedefType>(qual_type) |
| 5929 | ->getDecl() |
| 5930 | ->getUnderlyingType()) |
| 5931 | .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr, |
| 5932 | is_bitfield_ptr); |
| 5933 | |
| 5934 | case clang::Type::Auto: |
| 5935 | return CompilerType( |
| 5936 | getASTContext(), |
| 5937 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) |
| 5938 | .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr, |
| 5939 | is_bitfield_ptr); |
| 5940 | |
| 5941 | case clang::Type::Elaborated: |
| 5942 | return CompilerType( |
| 5943 | getASTContext(), |
| 5944 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) |
| 5945 | .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr, |
| 5946 | is_bitfield_ptr); |
| 5947 | |
| 5948 | case clang::Type::Paren: |
| 5949 | return CompilerType(getASTContext(), |
| 5950 | llvm::cast<clang::ParenType>(qual_type)->desugar()) |
| 5951 | .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr, |
| 5952 | is_bitfield_ptr); |
| 5953 | |
| 5954 | default: |
| 5955 | break; |
| 5956 | } |
| 5957 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5958 | } |
| 5959 | |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 5960 | uint32_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5961 | ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) { |
| 5962 | uint32_t count = 0; |
| 5963 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 5964 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 5965 | switch (type_class) { |
| 5966 | case clang::Type::Record: |
| 5967 | if (GetCompleteType(type)) { |
| 5968 | const clang::CXXRecordDecl *cxx_record_decl = |
| 5969 | qual_type->getAsCXXRecordDecl(); |
| 5970 | if (cxx_record_decl) |
| 5971 | count = cxx_record_decl->getNumBases(); |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 5972 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5973 | break; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 5974 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5975 | case clang::Type::ObjCObjectPointer: |
| 5976 | count = GetPointeeType(type).GetNumDirectBaseClasses(); |
| 5977 | break; |
| 5978 | |
| 5979 | case clang::Type::ObjCObject: |
| 5980 | if (GetCompleteType(type)) { |
| 5981 | const clang::ObjCObjectType *objc_class_type = |
| 5982 | qual_type->getAsObjCQualifiedInterfaceType(); |
| 5983 | if (objc_class_type) { |
| 5984 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 5985 | objc_class_type->getInterface(); |
| 5986 | |
| 5987 | if (class_interface_decl && class_interface_decl->getSuperClass()) |
| 5988 | count = 1; |
| 5989 | } |
| 5990 | } |
| 5991 | break; |
| 5992 | case clang::Type::ObjCInterface: |
| 5993 | if (GetCompleteType(type)) { |
| 5994 | const clang::ObjCInterfaceType *objc_interface_type = |
| 5995 | qual_type->getAs<clang::ObjCInterfaceType>(); |
| 5996 | if (objc_interface_type) { |
| 5997 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 5998 | objc_interface_type->getInterface(); |
| 5999 | |
| 6000 | if (class_interface_decl && class_interface_decl->getSuperClass()) |
| 6001 | count = 1; |
| 6002 | } |
| 6003 | } |
| 6004 | break; |
| 6005 | |
| 6006 | case clang::Type::Typedef: |
| 6007 | count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type) |
| 6008 | ->getDecl() |
| 6009 | ->getUnderlyingType() |
| 6010 | .getAsOpaquePtr()); |
| 6011 | break; |
| 6012 | |
| 6013 | case clang::Type::Auto: |
| 6014 | count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type) |
| 6015 | ->getDeducedType() |
| 6016 | .getAsOpaquePtr()); |
| 6017 | break; |
| 6018 | |
| 6019 | case clang::Type::Elaborated: |
| 6020 | count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type) |
| 6021 | ->getNamedType() |
| 6022 | .getAsOpaquePtr()); |
| 6023 | break; |
| 6024 | |
| 6025 | case clang::Type::Paren: |
| 6026 | return GetNumDirectBaseClasses( |
| 6027 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()); |
| 6028 | |
| 6029 | default: |
| 6030 | break; |
| 6031 | } |
| 6032 | return count; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6033 | } |
| 6034 | |
| 6035 | uint32_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6036 | ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) { |
| 6037 | uint32_t count = 0; |
| 6038 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 6039 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 6040 | switch (type_class) { |
| 6041 | case clang::Type::Record: |
| 6042 | if (GetCompleteType(type)) { |
| 6043 | const clang::CXXRecordDecl *cxx_record_decl = |
| 6044 | qual_type->getAsCXXRecordDecl(); |
| 6045 | if (cxx_record_decl) |
| 6046 | count = cxx_record_decl->getNumVBases(); |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6047 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6048 | break; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6049 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6050 | case clang::Type::Typedef: |
| 6051 | count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type) |
| 6052 | ->getDecl() |
| 6053 | ->getUnderlyingType() |
| 6054 | .getAsOpaquePtr()); |
| 6055 | break; |
| 6056 | |
| 6057 | case clang::Type::Auto: |
| 6058 | count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type) |
| 6059 | ->getDeducedType() |
| 6060 | .getAsOpaquePtr()); |
| 6061 | break; |
| 6062 | |
| 6063 | case clang::Type::Elaborated: |
| 6064 | count = |
| 6065 | GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type) |
| 6066 | ->getNamedType() |
| 6067 | .getAsOpaquePtr()); |
| 6068 | break; |
| 6069 | |
| 6070 | case clang::Type::Paren: |
| 6071 | count = GetNumVirtualBaseClasses( |
| 6072 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()); |
| 6073 | break; |
| 6074 | |
| 6075 | default: |
| 6076 | break; |
| 6077 | } |
| 6078 | return count; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6079 | } |
| 6080 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6081 | CompilerType ClangASTContext::GetDirectBaseClassAtIndex( |
| 6082 | lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) { |
| 6083 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 6084 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 6085 | switch (type_class) { |
| 6086 | case clang::Type::Record: |
| 6087 | if (GetCompleteType(type)) { |
| 6088 | const clang::CXXRecordDecl *cxx_record_decl = |
| 6089 | qual_type->getAsCXXRecordDecl(); |
| 6090 | if (cxx_record_decl) { |
| 6091 | uint32_t curr_idx = 0; |
| 6092 | clang::CXXRecordDecl::base_class_const_iterator base_class, |
| 6093 | base_class_end; |
| 6094 | for (base_class = cxx_record_decl->bases_begin(), |
| 6095 | base_class_end = cxx_record_decl->bases_end(); |
| 6096 | base_class != base_class_end; ++base_class, ++curr_idx) { |
| 6097 | if (curr_idx == idx) { |
| 6098 | if (bit_offset_ptr) { |
| 6099 | const clang::ASTRecordLayout &record_layout = |
| 6100 | getASTContext()->getASTRecordLayout(cxx_record_decl); |
| 6101 | const clang::CXXRecordDecl *base_class_decl = |
| 6102 | llvm::cast<clang::CXXRecordDecl>( |
| 6103 | base_class->getType() |
| 6104 | ->getAs<clang::RecordType>() |
| 6105 | ->getDecl()); |
| 6106 | if (base_class->isVirtual()) |
| 6107 | *bit_offset_ptr = |
| 6108 | record_layout.getVBaseClassOffset(base_class_decl) |
| 6109 | .getQuantity() * |
| 6110 | 8; |
| 6111 | else |
| 6112 | *bit_offset_ptr = |
| 6113 | record_layout.getBaseClassOffset(base_class_decl) |
| 6114 | .getQuantity() * |
| 6115 | 8; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6116 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6117 | return CompilerType(this, base_class->getType().getAsOpaquePtr()); |
| 6118 | } |
| 6119 | } |
| 6120 | } |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6121 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6122 | break; |
| 6123 | |
| 6124 | case clang::Type::ObjCObjectPointer: |
| 6125 | return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr); |
| 6126 | |
| 6127 | case clang::Type::ObjCObject: |
| 6128 | if (idx == 0 && GetCompleteType(type)) { |
| 6129 | const clang::ObjCObjectType *objc_class_type = |
| 6130 | qual_type->getAsObjCQualifiedInterfaceType(); |
| 6131 | if (objc_class_type) { |
| 6132 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 6133 | objc_class_type->getInterface(); |
| 6134 | |
| 6135 | if (class_interface_decl) { |
| 6136 | clang::ObjCInterfaceDecl *superclass_interface_decl = |
| 6137 | class_interface_decl->getSuperClass(); |
| 6138 | if (superclass_interface_decl) { |
| 6139 | if (bit_offset_ptr) |
| 6140 | *bit_offset_ptr = 0; |
| 6141 | return CompilerType(getASTContext(), |
| 6142 | getASTContext()->getObjCInterfaceType( |
| 6143 | superclass_interface_decl)); |
| 6144 | } |
| 6145 | } |
| 6146 | } |
| 6147 | } |
| 6148 | break; |
| 6149 | case clang::Type::ObjCInterface: |
| 6150 | if (idx == 0 && GetCompleteType(type)) { |
| 6151 | const clang::ObjCObjectType *objc_interface_type = |
| 6152 | qual_type->getAs<clang::ObjCInterfaceType>(); |
| 6153 | if (objc_interface_type) { |
| 6154 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 6155 | objc_interface_type->getInterface(); |
| 6156 | |
| 6157 | if (class_interface_decl) { |
| 6158 | clang::ObjCInterfaceDecl *superclass_interface_decl = |
| 6159 | class_interface_decl->getSuperClass(); |
| 6160 | if (superclass_interface_decl) { |
| 6161 | if (bit_offset_ptr) |
| 6162 | *bit_offset_ptr = 0; |
| 6163 | return CompilerType(getASTContext(), |
| 6164 | getASTContext()->getObjCInterfaceType( |
| 6165 | superclass_interface_decl)); |
| 6166 | } |
| 6167 | } |
| 6168 | } |
| 6169 | } |
| 6170 | break; |
| 6171 | |
| 6172 | case clang::Type::Typedef: |
| 6173 | return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type) |
| 6174 | ->getDecl() |
| 6175 | ->getUnderlyingType() |
| 6176 | .getAsOpaquePtr(), |
| 6177 | idx, bit_offset_ptr); |
| 6178 | |
| 6179 | case clang::Type::Auto: |
| 6180 | return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type) |
| 6181 | ->getDeducedType() |
| 6182 | .getAsOpaquePtr(), |
| 6183 | idx, bit_offset_ptr); |
| 6184 | |
| 6185 | case clang::Type::Elaborated: |
| 6186 | return GetDirectBaseClassAtIndex( |
| 6187 | llvm::cast<clang::ElaboratedType>(qual_type) |
| 6188 | ->getNamedType() |
| 6189 | .getAsOpaquePtr(), |
| 6190 | idx, bit_offset_ptr); |
| 6191 | |
| 6192 | case clang::Type::Paren: |
| 6193 | return GetDirectBaseClassAtIndex( |
| 6194 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 6195 | idx, bit_offset_ptr); |
| 6196 | |
| 6197 | default: |
| 6198 | break; |
| 6199 | } |
| 6200 | return CompilerType(); |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6201 | } |
| 6202 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6203 | CompilerType ClangASTContext::GetVirtualBaseClassAtIndex( |
| 6204 | lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) { |
| 6205 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 6206 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 6207 | switch (type_class) { |
| 6208 | case clang::Type::Record: |
| 6209 | if (GetCompleteType(type)) { |
| 6210 | const clang::CXXRecordDecl *cxx_record_decl = |
| 6211 | qual_type->getAsCXXRecordDecl(); |
| 6212 | if (cxx_record_decl) { |
| 6213 | uint32_t curr_idx = 0; |
| 6214 | clang::CXXRecordDecl::base_class_const_iterator base_class, |
| 6215 | base_class_end; |
| 6216 | for (base_class = cxx_record_decl->vbases_begin(), |
| 6217 | base_class_end = cxx_record_decl->vbases_end(); |
| 6218 | base_class != base_class_end; ++base_class, ++curr_idx) { |
| 6219 | if (curr_idx == idx) { |
| 6220 | if (bit_offset_ptr) { |
| 6221 | const clang::ASTRecordLayout &record_layout = |
| 6222 | getASTContext()->getASTRecordLayout(cxx_record_decl); |
| 6223 | const clang::CXXRecordDecl *base_class_decl = |
| 6224 | llvm::cast<clang::CXXRecordDecl>( |
| 6225 | base_class->getType() |
| 6226 | ->getAs<clang::RecordType>() |
| 6227 | ->getDecl()); |
| 6228 | *bit_offset_ptr = |
| 6229 | record_layout.getVBaseClassOffset(base_class_decl) |
| 6230 | .getQuantity() * |
| 6231 | 8; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6232 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6233 | return CompilerType(this, base_class->getType().getAsOpaquePtr()); |
| 6234 | } |
| 6235 | } |
| 6236 | } |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6237 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6238 | break; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6239 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6240 | case clang::Type::Typedef: |
| 6241 | return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type) |
| 6242 | ->getDecl() |
| 6243 | ->getUnderlyingType() |
| 6244 | .getAsOpaquePtr(), |
| 6245 | idx, bit_offset_ptr); |
| 6246 | |
| 6247 | case clang::Type::Auto: |
| 6248 | return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type) |
| 6249 | ->getDeducedType() |
| 6250 | .getAsOpaquePtr(), |
| 6251 | idx, bit_offset_ptr); |
| 6252 | |
| 6253 | case clang::Type::Elaborated: |
| 6254 | return GetVirtualBaseClassAtIndex( |
| 6255 | llvm::cast<clang::ElaboratedType>(qual_type) |
| 6256 | ->getNamedType() |
| 6257 | .getAsOpaquePtr(), |
| 6258 | idx, bit_offset_ptr); |
| 6259 | |
| 6260 | case clang::Type::Paren: |
| 6261 | return GetVirtualBaseClassAtIndex( |
| 6262 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 6263 | idx, bit_offset_ptr); |
| 6264 | |
| 6265 | default: |
| 6266 | break; |
| 6267 | } |
| 6268 | return CompilerType(); |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6269 | } |
| 6270 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 6271 | // If a pointer to a pointee type (the clang_type arg) says that it has no |
| 6272 | // children, then we either need to trust it, or override it and return a |
| 6273 | // different result. For example, an "int *" has one child that is an integer, |
| 6274 | // but a function pointer doesn't have any children. Likewise if a Record type |
| 6275 | // claims it has no children, then there really is nothing to show. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6276 | uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) { |
| 6277 | if (type.isNull()) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 6278 | return 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6279 | |
| 6280 | clang::QualType qual_type(type.getCanonicalType()); |
| 6281 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 6282 | switch (type_class) { |
| 6283 | case clang::Type::Builtin: |
| 6284 | switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { |
| 6285 | case clang::BuiltinType::UnknownAny: |
| 6286 | case clang::BuiltinType::Void: |
| 6287 | case clang::BuiltinType::NullPtr: |
| 6288 | case clang::BuiltinType::OCLEvent: |
| 6289 | case clang::BuiltinType::OCLImage1dRO: |
| 6290 | case clang::BuiltinType::OCLImage1dWO: |
| 6291 | case clang::BuiltinType::OCLImage1dRW: |
| 6292 | case clang::BuiltinType::OCLImage1dArrayRO: |
| 6293 | case clang::BuiltinType::OCLImage1dArrayWO: |
| 6294 | case clang::BuiltinType::OCLImage1dArrayRW: |
| 6295 | case clang::BuiltinType::OCLImage1dBufferRO: |
| 6296 | case clang::BuiltinType::OCLImage1dBufferWO: |
| 6297 | case clang::BuiltinType::OCLImage1dBufferRW: |
| 6298 | case clang::BuiltinType::OCLImage2dRO: |
| 6299 | case clang::BuiltinType::OCLImage2dWO: |
| 6300 | case clang::BuiltinType::OCLImage2dRW: |
| 6301 | case clang::BuiltinType::OCLImage2dArrayRO: |
| 6302 | case clang::BuiltinType::OCLImage2dArrayWO: |
| 6303 | case clang::BuiltinType::OCLImage2dArrayRW: |
| 6304 | case clang::BuiltinType::OCLImage3dRO: |
| 6305 | case clang::BuiltinType::OCLImage3dWO: |
| 6306 | case clang::BuiltinType::OCLImage3dRW: |
| 6307 | case clang::BuiltinType::OCLSampler: |
| 6308 | return 0; |
| 6309 | case clang::BuiltinType::Bool: |
| 6310 | case clang::BuiltinType::Char_U: |
| 6311 | case clang::BuiltinType::UChar: |
| 6312 | case clang::BuiltinType::WChar_U: |
| 6313 | case clang::BuiltinType::Char16: |
| 6314 | case clang::BuiltinType::Char32: |
| 6315 | case clang::BuiltinType::UShort: |
| 6316 | case clang::BuiltinType::UInt: |
| 6317 | case clang::BuiltinType::ULong: |
| 6318 | case clang::BuiltinType::ULongLong: |
| 6319 | case clang::BuiltinType::UInt128: |
| 6320 | case clang::BuiltinType::Char_S: |
| 6321 | case clang::BuiltinType::SChar: |
| 6322 | case clang::BuiltinType::WChar_S: |
| 6323 | case clang::BuiltinType::Short: |
| 6324 | case clang::BuiltinType::Int: |
| 6325 | case clang::BuiltinType::Long: |
| 6326 | case clang::BuiltinType::LongLong: |
| 6327 | case clang::BuiltinType::Int128: |
| 6328 | case clang::BuiltinType::Float: |
| 6329 | case clang::BuiltinType::Double: |
| 6330 | case clang::BuiltinType::LongDouble: |
| 6331 | case clang::BuiltinType::Dependent: |
| 6332 | case clang::BuiltinType::Overload: |
| 6333 | case clang::BuiltinType::ObjCId: |
| 6334 | case clang::BuiltinType::ObjCClass: |
| 6335 | case clang::BuiltinType::ObjCSel: |
| 6336 | case clang::BuiltinType::BoundMember: |
| 6337 | case clang::BuiltinType::Half: |
| 6338 | case clang::BuiltinType::ARCUnbridgedCast: |
| 6339 | case clang::BuiltinType::PseudoObject: |
| 6340 | case clang::BuiltinType::BuiltinFn: |
| 6341 | case clang::BuiltinType::OMPArraySection: |
| 6342 | return 1; |
| 6343 | default: |
| 6344 | return 0; |
| 6345 | } |
| 6346 | break; |
| 6347 | |
| 6348 | case clang::Type::Complex: |
| 6349 | return 1; |
| 6350 | case clang::Type::Pointer: |
| 6351 | return 1; |
| 6352 | case clang::Type::BlockPointer: |
| 6353 | return 0; // If block pointers don't have debug info, then no children for |
| 6354 | // them |
| 6355 | case clang::Type::LValueReference: |
| 6356 | return 1; |
| 6357 | case clang::Type::RValueReference: |
| 6358 | return 1; |
| 6359 | case clang::Type::MemberPointer: |
| 6360 | return 0; |
| 6361 | case clang::Type::ConstantArray: |
| 6362 | return 0; |
| 6363 | case clang::Type::IncompleteArray: |
| 6364 | return 0; |
| 6365 | case clang::Type::VariableArray: |
| 6366 | return 0; |
| 6367 | case clang::Type::DependentSizedArray: |
| 6368 | return 0; |
| 6369 | case clang::Type::DependentSizedExtVector: |
| 6370 | return 0; |
| 6371 | case clang::Type::Vector: |
| 6372 | return 0; |
| 6373 | case clang::Type::ExtVector: |
| 6374 | return 0; |
| 6375 | case clang::Type::FunctionProto: |
| 6376 | return 0; // When we function pointers, they have no children... |
| 6377 | case clang::Type::FunctionNoProto: |
| 6378 | return 0; // When we function pointers, they have no children... |
| 6379 | case clang::Type::UnresolvedUsing: |
| 6380 | return 0; |
| 6381 | case clang::Type::Paren: |
| 6382 | return GetNumPointeeChildren( |
| 6383 | llvm::cast<clang::ParenType>(qual_type)->desugar()); |
| 6384 | case clang::Type::Typedef: |
| 6385 | return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type) |
| 6386 | ->getDecl() |
| 6387 | ->getUnderlyingType()); |
| 6388 | case clang::Type::Auto: |
| 6389 | return GetNumPointeeChildren( |
| 6390 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()); |
| 6391 | case clang::Type::Elaborated: |
| 6392 | return GetNumPointeeChildren( |
| 6393 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()); |
| 6394 | case clang::Type::TypeOfExpr: |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 6395 | return GetNumPointeeChildren(llvm::cast<clang::TypeOfExprType>(qual_type) |
| 6396 | ->getUnderlyingExpr() |
| 6397 | ->getType()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6398 | case clang::Type::TypeOf: |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 6399 | return GetNumPointeeChildren( |
| 6400 | llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6401 | case clang::Type::Decltype: |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 6402 | return GetNumPointeeChildren( |
| 6403 | llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6404 | case clang::Type::Record: |
| 6405 | return 0; |
| 6406 | case clang::Type::Enum: |
| 6407 | return 1; |
| 6408 | case clang::Type::TemplateTypeParm: |
| 6409 | return 1; |
| 6410 | case clang::Type::SubstTemplateTypeParm: |
| 6411 | return 1; |
| 6412 | case clang::Type::TemplateSpecialization: |
| 6413 | return 1; |
| 6414 | case clang::Type::InjectedClassName: |
| 6415 | return 0; |
| 6416 | case clang::Type::DependentName: |
| 6417 | return 1; |
| 6418 | case clang::Type::DependentTemplateSpecialization: |
| 6419 | return 1; |
| 6420 | case clang::Type::ObjCObject: |
| 6421 | return 0; |
| 6422 | case clang::Type::ObjCInterface: |
| 6423 | return 0; |
| 6424 | case clang::Type::ObjCObjectPointer: |
| 6425 | return 1; |
| 6426 | default: |
| 6427 | break; |
| 6428 | } |
| 6429 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 6430 | } |
| 6431 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6432 | CompilerType ClangASTContext::GetChildCompilerTypeAtIndex( |
| 6433 | lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx, |
| 6434 | bool transparent_pointers, bool omit_empty_base_classes, |
| 6435 | bool ignore_array_bounds, std::string &child_name, |
| 6436 | uint32_t &child_byte_size, int32_t &child_byte_offset, |
| 6437 | uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset, |
| 6438 | bool &child_is_base_class, bool &child_is_deref_of_parent, |
| 6439 | ValueObject *valobj, uint64_t &language_flags) { |
| 6440 | if (!type) |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 6441 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 6442 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6443 | clang::QualType parent_qual_type(GetCanonicalQualType(type)); |
| 6444 | const clang::Type::TypeClass parent_type_class = |
| 6445 | parent_qual_type->getTypeClass(); |
| 6446 | child_bitfield_bit_size = 0; |
| 6447 | child_bitfield_bit_offset = 0; |
| 6448 | child_is_base_class = false; |
| 6449 | language_flags = 0; |
| 6450 | |
| 6451 | const bool idx_is_valid = idx < GetNumChildren(type, omit_empty_base_classes); |
Aleksandr Urakov | 7d2a74f | 2018-08-14 07:57:44 +0000 | [diff] [blame^] | 6452 | int32_t bit_offset; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6453 | switch (parent_type_class) { |
| 6454 | case clang::Type::Builtin: |
| 6455 | if (idx_is_valid) { |
| 6456 | switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) { |
| 6457 | case clang::BuiltinType::ObjCId: |
| 6458 | case clang::BuiltinType::ObjCClass: |
| 6459 | child_name = "isa"; |
| 6460 | child_byte_size = |
| 6461 | getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) / |
| 6462 | CHAR_BIT; |
| 6463 | return CompilerType(getASTContext(), |
| 6464 | getASTContext()->ObjCBuiltinClassTy); |
| 6465 | |
| 6466 | default: |
| 6467 | break; |
| 6468 | } |
| 6469 | } |
| 6470 | break; |
| 6471 | |
| 6472 | case clang::Type::Record: |
| 6473 | if (idx_is_valid && GetCompleteType(type)) { |
| 6474 | const clang::RecordType *record_type = |
| 6475 | llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr()); |
| 6476 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 6477 | assert(record_decl); |
| 6478 | const clang::ASTRecordLayout &record_layout = |
| 6479 | getASTContext()->getASTRecordLayout(record_decl); |
| 6480 | uint32_t child_idx = 0; |
| 6481 | |
| 6482 | const clang::CXXRecordDecl *cxx_record_decl = |
| 6483 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 6484 | if (cxx_record_decl) { |
| 6485 | // We might have base classes to print out first |
| 6486 | clang::CXXRecordDecl::base_class_const_iterator base_class, |
| 6487 | base_class_end; |
| 6488 | for (base_class = cxx_record_decl->bases_begin(), |
| 6489 | base_class_end = cxx_record_decl->bases_end(); |
| 6490 | base_class != base_class_end; ++base_class) { |
| 6491 | const clang::CXXRecordDecl *base_class_decl = nullptr; |
| 6492 | |
| 6493 | // Skip empty base classes |
| 6494 | if (omit_empty_base_classes) { |
| 6495 | base_class_decl = llvm::cast<clang::CXXRecordDecl>( |
| 6496 | base_class->getType()->getAs<clang::RecordType>()->getDecl()); |
| 6497 | if (ClangASTContext::RecordHasFields(base_class_decl) == false) |
| 6498 | continue; |
| 6499 | } |
| 6500 | |
| 6501 | if (idx == child_idx) { |
| 6502 | if (base_class_decl == nullptr) |
| 6503 | base_class_decl = llvm::cast<clang::CXXRecordDecl>( |
| 6504 | base_class->getType()->getAs<clang::RecordType>()->getDecl()); |
| 6505 | |
| 6506 | if (base_class->isVirtual()) { |
| 6507 | bool handled = false; |
| 6508 | if (valobj) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 6509 | Status err; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6510 | AddressType addr_type = eAddressTypeInvalid; |
| 6511 | lldb::addr_t vtable_ptr_addr = |
| 6512 | valobj->GetCPPVTableAddress(addr_type); |
| 6513 | |
| 6514 | if (vtable_ptr_addr != LLDB_INVALID_ADDRESS && |
| 6515 | addr_type == eAddressTypeLoad) { |
| 6516 | |
| 6517 | ExecutionContext exe_ctx(valobj->GetExecutionContextRef()); |
| 6518 | Process *process = exe_ctx.GetProcessPtr(); |
| 6519 | if (process) { |
| 6520 | clang::VTableContextBase *vtable_ctx = |
| 6521 | getASTContext()->getVTableContext(); |
| 6522 | if (vtable_ctx) { |
| 6523 | if (vtable_ctx->isMicrosoft()) { |
| 6524 | clang::MicrosoftVTableContext *msoft_vtable_ctx = |
| 6525 | static_cast<clang::MicrosoftVTableContext *>( |
| 6526 | vtable_ctx); |
| 6527 | |
| 6528 | if (vtable_ptr_addr) { |
| 6529 | const lldb::addr_t vbtable_ptr_addr = |
| 6530 | vtable_ptr_addr + |
| 6531 | record_layout.getVBPtrOffset().getQuantity(); |
| 6532 | |
| 6533 | const lldb::addr_t vbtable_ptr = |
| 6534 | process->ReadPointerFromMemory(vbtable_ptr_addr, |
| 6535 | err); |
| 6536 | if (vbtable_ptr != LLDB_INVALID_ADDRESS) { |
| 6537 | // Get the index into the virtual base table. The |
| 6538 | // index is the index in uint32_t from vbtable_ptr |
| 6539 | const unsigned vbtable_index = |
| 6540 | msoft_vtable_ctx->getVBTableIndex( |
| 6541 | cxx_record_decl, base_class_decl); |
| 6542 | const lldb::addr_t base_offset_addr = |
| 6543 | vbtable_ptr + vbtable_index * 4; |
Aleksandr Urakov | 7d2a74f | 2018-08-14 07:57:44 +0000 | [diff] [blame^] | 6544 | const int32_t base_offset = |
| 6545 | process->ReadSignedIntegerFromMemory( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6546 | base_offset_addr, 4, UINT32_MAX, err); |
| 6547 | if (base_offset != UINT32_MAX) { |
| 6548 | handled = true; |
| 6549 | bit_offset = base_offset * 8; |
| 6550 | } |
| 6551 | } |
| 6552 | } |
| 6553 | } else { |
| 6554 | clang::ItaniumVTableContext *itanium_vtable_ctx = |
| 6555 | static_cast<clang::ItaniumVTableContext *>( |
| 6556 | vtable_ctx); |
| 6557 | if (vtable_ptr_addr) { |
| 6558 | const lldb::addr_t vtable_ptr = |
| 6559 | process->ReadPointerFromMemory(vtable_ptr_addr, |
| 6560 | err); |
| 6561 | if (vtable_ptr != LLDB_INVALID_ADDRESS) { |
| 6562 | clang::CharUnits base_offset_offset = |
| 6563 | itanium_vtable_ctx->getVirtualBaseOffsetOffset( |
| 6564 | cxx_record_decl, base_class_decl); |
| 6565 | const lldb::addr_t base_offset_addr = |
| 6566 | vtable_ptr + base_offset_offset.getQuantity(); |
| 6567 | const uint32_t base_offset_size = |
| 6568 | process->GetAddressByteSize(); |
Aleksandr Urakov | 7d2a74f | 2018-08-14 07:57:44 +0000 | [diff] [blame^] | 6569 | const int64_t base_offset = |
| 6570 | process->ReadSignedIntegerFromMemory( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6571 | base_offset_addr, base_offset_size, |
| 6572 | UINT32_MAX, err); |
| 6573 | if (base_offset < UINT32_MAX) { |
| 6574 | handled = true; |
| 6575 | bit_offset = base_offset * 8; |
| 6576 | } |
| 6577 | } |
| 6578 | } |
| 6579 | } |
| 6580 | } |
| 6581 | } |
| 6582 | } |
| 6583 | } |
| 6584 | if (!handled) |
| 6585 | bit_offset = record_layout.getVBaseClassOffset(base_class_decl) |
| 6586 | .getQuantity() * |
| 6587 | 8; |
| 6588 | } else |
| 6589 | bit_offset = record_layout.getBaseClassOffset(base_class_decl) |
| 6590 | .getQuantity() * |
| 6591 | 8; |
| 6592 | |
| 6593 | // Base classes should be a multiple of 8 bits in size |
| 6594 | child_byte_offset = bit_offset / 8; |
| 6595 | CompilerType base_class_clang_type(getASTContext(), |
| 6596 | base_class->getType()); |
| 6597 | child_name = base_class_clang_type.GetTypeName().AsCString(""); |
| 6598 | uint64_t base_class_clang_type_bit_size = |
| 6599 | base_class_clang_type.GetBitSize( |
| 6600 | exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL); |
| 6601 | |
| 6602 | // Base classes bit sizes should be a multiple of 8 bits in size |
| 6603 | assert(base_class_clang_type_bit_size % 8 == 0); |
| 6604 | child_byte_size = base_class_clang_type_bit_size / 8; |
| 6605 | child_is_base_class = true; |
| 6606 | return base_class_clang_type; |
| 6607 | } |
| 6608 | // We don't increment the child index in the for loop since we might |
| 6609 | // be skipping empty base classes |
| 6610 | ++child_idx; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 6611 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6612 | } |
| 6613 | // Make sure index is in range... |
| 6614 | uint32_t field_idx = 0; |
| 6615 | clang::RecordDecl::field_iterator field, field_end; |
| 6616 | for (field = record_decl->field_begin(), |
| 6617 | field_end = record_decl->field_end(); |
| 6618 | field != field_end; ++field, ++field_idx, ++child_idx) { |
| 6619 | if (idx == child_idx) { |
| 6620 | // Print the member type if requested |
| 6621 | // Print the member name and equal sign |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 6622 | child_name.assign(field->getNameAsString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6623 | |
| 6624 | // Figure out the type byte size (field_type_info.first) and |
| 6625 | // alignment (field_type_info.second) from the AST context. |
| 6626 | CompilerType field_clang_type(getASTContext(), field->getType()); |
| 6627 | assert(field_idx < record_layout.getFieldCount()); |
| 6628 | child_byte_size = field_clang_type.GetByteSize( |
| 6629 | exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL); |
| 6630 | const uint32_t child_bit_size = child_byte_size * 8; |
| 6631 | |
| 6632 | // Figure out the field offset within the current struct/union/class |
| 6633 | // type |
| 6634 | bit_offset = record_layout.getFieldOffset(field_idx); |
| 6635 | if (ClangASTContext::FieldIsBitfield(getASTContext(), *field, |
| 6636 | child_bitfield_bit_size)) { |
| 6637 | child_bitfield_bit_offset = bit_offset % child_bit_size; |
| 6638 | const uint32_t child_bit_offset = |
| 6639 | bit_offset - child_bitfield_bit_offset; |
| 6640 | child_byte_offset = child_bit_offset / 8; |
| 6641 | } else { |
| 6642 | child_byte_offset = bit_offset / 8; |
| 6643 | } |
| 6644 | |
| 6645 | return field_clang_type; |
| 6646 | } |
| 6647 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 6648 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6649 | break; |
| 6650 | |
| 6651 | case clang::Type::ObjCObject: |
| 6652 | case clang::Type::ObjCInterface: |
| 6653 | if (idx_is_valid && GetCompleteType(type)) { |
| 6654 | const clang::ObjCObjectType *objc_class_type = |
| 6655 | llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr()); |
| 6656 | assert(objc_class_type); |
| 6657 | if (objc_class_type) { |
| 6658 | uint32_t child_idx = 0; |
| 6659 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 6660 | objc_class_type->getInterface(); |
| 6661 | |
| 6662 | if (class_interface_decl) { |
| 6663 | |
| 6664 | const clang::ASTRecordLayout &interface_layout = |
| 6665 | getASTContext()->getASTObjCInterfaceLayout(class_interface_decl); |
| 6666 | clang::ObjCInterfaceDecl *superclass_interface_decl = |
| 6667 | class_interface_decl->getSuperClass(); |
| 6668 | if (superclass_interface_decl) { |
| 6669 | if (omit_empty_base_classes) { |
| 6670 | CompilerType base_class_clang_type( |
| 6671 | getASTContext(), getASTContext()->getObjCInterfaceType( |
| 6672 | superclass_interface_decl)); |
| 6673 | if (base_class_clang_type.GetNumChildren( |
| 6674 | omit_empty_base_classes) > 0) { |
| 6675 | if (idx == 0) { |
| 6676 | clang::QualType ivar_qual_type( |
| 6677 | getASTContext()->getObjCInterfaceType( |
| 6678 | superclass_interface_decl)); |
| 6679 | |
| 6680 | child_name.assign( |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 6681 | superclass_interface_decl->getNameAsString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6682 | |
| 6683 | clang::TypeInfo ivar_type_info = |
| 6684 | getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr()); |
| 6685 | |
| 6686 | child_byte_size = ivar_type_info.Width / 8; |
| 6687 | child_byte_offset = 0; |
| 6688 | child_is_base_class = true; |
| 6689 | |
| 6690 | return CompilerType(getASTContext(), ivar_qual_type); |
| 6691 | } |
| 6692 | |
| 6693 | ++child_idx; |
| 6694 | } |
| 6695 | } else |
| 6696 | ++child_idx; |
| 6697 | } |
| 6698 | |
| 6699 | const uint32_t superclass_idx = child_idx; |
| 6700 | |
| 6701 | if (idx < (child_idx + class_interface_decl->ivar_size())) { |
| 6702 | clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, |
| 6703 | ivar_end = class_interface_decl->ivar_end(); |
| 6704 | |
| 6705 | for (ivar_pos = class_interface_decl->ivar_begin(); |
| 6706 | ivar_pos != ivar_end; ++ivar_pos) { |
| 6707 | if (child_idx == idx) { |
| 6708 | clang::ObjCIvarDecl *ivar_decl = *ivar_pos; |
| 6709 | |
| 6710 | clang::QualType ivar_qual_type(ivar_decl->getType()); |
| 6711 | |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 6712 | child_name.assign(ivar_decl->getNameAsString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6713 | |
| 6714 | clang::TypeInfo ivar_type_info = |
| 6715 | getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr()); |
| 6716 | |
| 6717 | child_byte_size = ivar_type_info.Width / 8; |
| 6718 | |
| 6719 | // Figure out the field offset within the current |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 6720 | // struct/union/class type For ObjC objects, we can't trust the |
| 6721 | // bit offset we get from the Clang AST, since that doesn't |
| 6722 | // account for the space taken up by unbacked properties, or |
| 6723 | // from the changing size of base classes that are newer than |
| 6724 | // this class. So if we have a process around that we can ask |
| 6725 | // about this object, do so. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6726 | child_byte_offset = LLDB_INVALID_IVAR_OFFSET; |
| 6727 | Process *process = nullptr; |
| 6728 | if (exe_ctx) |
| 6729 | process = exe_ctx->GetProcessPtr(); |
| 6730 | if (process) { |
| 6731 | ObjCLanguageRuntime *objc_runtime = |
| 6732 | process->GetObjCLanguageRuntime(); |
| 6733 | if (objc_runtime != nullptr) { |
| 6734 | CompilerType parent_ast_type(getASTContext(), |
| 6735 | parent_qual_type); |
| 6736 | child_byte_offset = objc_runtime->GetByteOffsetForIvar( |
| 6737 | parent_ast_type, ivar_decl->getNameAsString().c_str()); |
| 6738 | } |
| 6739 | } |
| 6740 | |
| 6741 | // Setting this to UINT32_MAX to make sure we don't compute it |
| 6742 | // twice... |
| 6743 | bit_offset = UINT32_MAX; |
| 6744 | |
| 6745 | if (child_byte_offset == |
| 6746 | static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) { |
| 6747 | bit_offset = interface_layout.getFieldOffset(child_idx - |
| 6748 | superclass_idx); |
| 6749 | child_byte_offset = bit_offset / 8; |
| 6750 | } |
| 6751 | |
| 6752 | // Note, the ObjC Ivar Byte offset is just that, it doesn't |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 6753 | // account for the bit offset of a bitfield within its |
| 6754 | // containing object. So regardless of where we get the byte |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6755 | // offset from, we still need to get the bit offset for |
| 6756 | // bitfields from the layout. |
| 6757 | |
| 6758 | if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl, |
| 6759 | child_bitfield_bit_size)) { |
| 6760 | if (bit_offset == UINT32_MAX) |
| 6761 | bit_offset = interface_layout.getFieldOffset( |
| 6762 | child_idx - superclass_idx); |
| 6763 | |
| 6764 | child_bitfield_bit_offset = bit_offset % 8; |
| 6765 | } |
| 6766 | return CompilerType(getASTContext(), ivar_qual_type); |
| 6767 | } |
| 6768 | ++child_idx; |
| 6769 | } |
| 6770 | } |
| 6771 | } |
| 6772 | } |
| 6773 | } |
| 6774 | break; |
| 6775 | |
| 6776 | case clang::Type::ObjCObjectPointer: |
| 6777 | if (idx_is_valid) { |
| 6778 | CompilerType pointee_clang_type(GetPointeeType(type)); |
| 6779 | |
| 6780 | if (transparent_pointers && pointee_clang_type.IsAggregateType()) { |
| 6781 | child_is_deref_of_parent = false; |
| 6782 | bool tmp_child_is_deref_of_parent = false; |
| 6783 | return pointee_clang_type.GetChildCompilerTypeAtIndex( |
| 6784 | exe_ctx, idx, transparent_pointers, omit_empty_base_classes, |
| 6785 | ignore_array_bounds, child_name, child_byte_size, child_byte_offset, |
| 6786 | child_bitfield_bit_size, child_bitfield_bit_offset, |
| 6787 | child_is_base_class, tmp_child_is_deref_of_parent, valobj, |
| 6788 | language_flags); |
| 6789 | } else { |
| 6790 | child_is_deref_of_parent = true; |
| 6791 | const char *parent_name = |
| 6792 | valobj ? valobj->GetName().GetCString() : NULL; |
| 6793 | if (parent_name) { |
| 6794 | child_name.assign(1, '*'); |
| 6795 | child_name += parent_name; |
| 6796 | } |
| 6797 | |
| 6798 | // We have a pointer to an simple type |
| 6799 | if (idx == 0 && pointee_clang_type.GetCompleteType()) { |
| 6800 | child_byte_size = pointee_clang_type.GetByteSize( |
| 6801 | exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL); |
| 6802 | child_byte_offset = 0; |
| 6803 | return pointee_clang_type; |
| 6804 | } |
| 6805 | } |
| 6806 | } |
| 6807 | break; |
| 6808 | |
| 6809 | case clang::Type::Vector: |
| 6810 | case clang::Type::ExtVector: |
| 6811 | if (idx_is_valid) { |
| 6812 | const clang::VectorType *array = |
| 6813 | llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr()); |
| 6814 | if (array) { |
| 6815 | CompilerType element_type(getASTContext(), array->getElementType()); |
| 6816 | if (element_type.GetCompleteType()) { |
| 6817 | char element_name[64]; |
| 6818 | ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]", |
| 6819 | static_cast<uint64_t>(idx)); |
| 6820 | child_name.assign(element_name); |
| 6821 | child_byte_size = element_type.GetByteSize( |
| 6822 | exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL); |
| 6823 | child_byte_offset = (int32_t)idx * (int32_t)child_byte_size; |
| 6824 | return element_type; |
| 6825 | } |
| 6826 | } |
| 6827 | } |
| 6828 | break; |
| 6829 | |
| 6830 | case clang::Type::ConstantArray: |
| 6831 | case clang::Type::IncompleteArray: |
| 6832 | if (ignore_array_bounds || idx_is_valid) { |
| 6833 | const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe(); |
| 6834 | if (array) { |
| 6835 | CompilerType element_type(getASTContext(), array->getElementType()); |
| 6836 | if (element_type.GetCompleteType()) { |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 6837 | child_name = llvm::formatv("[{0}]", idx); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6838 | child_byte_size = element_type.GetByteSize( |
| 6839 | exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL); |
| 6840 | child_byte_offset = (int32_t)idx * (int32_t)child_byte_size; |
| 6841 | return element_type; |
| 6842 | } |
| 6843 | } |
| 6844 | } |
| 6845 | break; |
| 6846 | |
Tamas Berghammer | 1c62e03 | 2017-01-07 16:39:07 +0000 | [diff] [blame] | 6847 | case clang::Type::Pointer: { |
| 6848 | CompilerType pointee_clang_type(GetPointeeType(type)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6849 | |
Tamas Berghammer | 1c62e03 | 2017-01-07 16:39:07 +0000 | [diff] [blame] | 6850 | // Don't dereference "void *" pointers |
| 6851 | if (pointee_clang_type.IsVoidType()) |
| 6852 | return CompilerType(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6853 | |
Tamas Berghammer | 1c62e03 | 2017-01-07 16:39:07 +0000 | [diff] [blame] | 6854 | if (transparent_pointers && pointee_clang_type.IsAggregateType()) { |
| 6855 | child_is_deref_of_parent = false; |
| 6856 | bool tmp_child_is_deref_of_parent = false; |
| 6857 | return pointee_clang_type.GetChildCompilerTypeAtIndex( |
| 6858 | exe_ctx, idx, transparent_pointers, omit_empty_base_classes, |
| 6859 | ignore_array_bounds, child_name, child_byte_size, child_byte_offset, |
| 6860 | child_bitfield_bit_size, child_bitfield_bit_offset, |
| 6861 | child_is_base_class, tmp_child_is_deref_of_parent, valobj, |
| 6862 | language_flags); |
| 6863 | } else { |
| 6864 | child_is_deref_of_parent = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6865 | |
Tamas Berghammer | 1c62e03 | 2017-01-07 16:39:07 +0000 | [diff] [blame] | 6866 | const char *parent_name = |
| 6867 | valobj ? valobj->GetName().GetCString() : NULL; |
| 6868 | if (parent_name) { |
| 6869 | child_name.assign(1, '*'); |
| 6870 | child_name += parent_name; |
| 6871 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6872 | |
Tamas Berghammer | 1c62e03 | 2017-01-07 16:39:07 +0000 | [diff] [blame] | 6873 | // We have a pointer to an simple type |
| 6874 | if (idx == 0) { |
| 6875 | child_byte_size = pointee_clang_type.GetByteSize( |
| 6876 | exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL); |
| 6877 | child_byte_offset = 0; |
| 6878 | return pointee_clang_type; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6879 | } |
| 6880 | } |
| 6881 | break; |
Tamas Berghammer | 1c62e03 | 2017-01-07 16:39:07 +0000 | [diff] [blame] | 6882 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6883 | |
| 6884 | case clang::Type::LValueReference: |
| 6885 | case clang::Type::RValueReference: |
| 6886 | if (idx_is_valid) { |
| 6887 | const clang::ReferenceType *reference_type = |
| 6888 | llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr()); |
| 6889 | CompilerType pointee_clang_type(getASTContext(), |
| 6890 | reference_type->getPointeeType()); |
| 6891 | if (transparent_pointers && pointee_clang_type.IsAggregateType()) { |
| 6892 | child_is_deref_of_parent = false; |
| 6893 | bool tmp_child_is_deref_of_parent = false; |
| 6894 | return pointee_clang_type.GetChildCompilerTypeAtIndex( |
| 6895 | exe_ctx, idx, transparent_pointers, omit_empty_base_classes, |
| 6896 | ignore_array_bounds, child_name, child_byte_size, child_byte_offset, |
| 6897 | child_bitfield_bit_size, child_bitfield_bit_offset, |
| 6898 | child_is_base_class, tmp_child_is_deref_of_parent, valobj, |
| 6899 | language_flags); |
| 6900 | } else { |
| 6901 | const char *parent_name = |
| 6902 | valobj ? valobj->GetName().GetCString() : NULL; |
| 6903 | if (parent_name) { |
| 6904 | child_name.assign(1, '&'); |
| 6905 | child_name += parent_name; |
| 6906 | } |
| 6907 | |
| 6908 | // We have a pointer to an simple type |
| 6909 | if (idx == 0) { |
| 6910 | child_byte_size = pointee_clang_type.GetByteSize( |
| 6911 | exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL); |
| 6912 | child_byte_offset = 0; |
| 6913 | return pointee_clang_type; |
| 6914 | } |
| 6915 | } |
| 6916 | } |
| 6917 | break; |
| 6918 | |
| 6919 | case clang::Type::Typedef: { |
| 6920 | CompilerType typedefed_clang_type( |
| 6921 | getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type) |
| 6922 | ->getDecl() |
| 6923 | ->getUnderlyingType()); |
| 6924 | return typedefed_clang_type.GetChildCompilerTypeAtIndex( |
| 6925 | exe_ctx, idx, transparent_pointers, omit_empty_base_classes, |
| 6926 | ignore_array_bounds, child_name, child_byte_size, child_byte_offset, |
| 6927 | child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class, |
| 6928 | child_is_deref_of_parent, valobj, language_flags); |
| 6929 | } break; |
| 6930 | |
| 6931 | case clang::Type::Auto: { |
| 6932 | CompilerType elaborated_clang_type( |
| 6933 | getASTContext(), |
| 6934 | llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType()); |
| 6935 | return elaborated_clang_type.GetChildCompilerTypeAtIndex( |
| 6936 | exe_ctx, idx, transparent_pointers, omit_empty_base_classes, |
| 6937 | ignore_array_bounds, child_name, child_byte_size, child_byte_offset, |
| 6938 | child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class, |
| 6939 | child_is_deref_of_parent, valobj, language_flags); |
| 6940 | } |
| 6941 | |
| 6942 | case clang::Type::Elaborated: { |
| 6943 | CompilerType elaborated_clang_type( |
| 6944 | getASTContext(), |
| 6945 | llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType()); |
| 6946 | return elaborated_clang_type.GetChildCompilerTypeAtIndex( |
| 6947 | exe_ctx, idx, transparent_pointers, omit_empty_base_classes, |
| 6948 | ignore_array_bounds, child_name, child_byte_size, child_byte_offset, |
| 6949 | child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class, |
| 6950 | child_is_deref_of_parent, valobj, language_flags); |
| 6951 | } |
| 6952 | |
| 6953 | case clang::Type::Paren: { |
| 6954 | CompilerType paren_clang_type( |
| 6955 | getASTContext(), |
| 6956 | llvm::cast<clang::ParenType>(parent_qual_type)->desugar()); |
| 6957 | return paren_clang_type.GetChildCompilerTypeAtIndex( |
| 6958 | exe_ctx, idx, transparent_pointers, omit_empty_base_classes, |
| 6959 | ignore_array_bounds, child_name, child_byte_size, child_byte_offset, |
| 6960 | child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class, |
| 6961 | child_is_deref_of_parent, valobj, language_flags); |
| 6962 | } |
| 6963 | |
| 6964 | default: |
| 6965 | break; |
| 6966 | } |
| 6967 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 6968 | } |
| 6969 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6970 | static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl, |
| 6971 | const clang::CXXBaseSpecifier *base_spec, |
| 6972 | bool omit_empty_base_classes) { |
| 6973 | uint32_t child_idx = 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 6974 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6975 | const clang::CXXRecordDecl *cxx_record_decl = |
| 6976 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 6977 | |
| 6978 | // const char *super_name = record_decl->getNameAsCString(); |
| 6979 | // const char *base_name = |
| 6980 | // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString(); |
| 6981 | // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name); |
| 6982 | // |
| 6983 | if (cxx_record_decl) { |
| 6984 | clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end; |
| 6985 | for (base_class = cxx_record_decl->bases_begin(), |
| 6986 | base_class_end = cxx_record_decl->bases_end(); |
| 6987 | base_class != base_class_end; ++base_class) { |
| 6988 | if (omit_empty_base_classes) { |
| 6989 | if (BaseSpecifierIsEmpty(base_class)) |
| 6990 | continue; |
| 6991 | } |
| 6992 | |
| 6993 | // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", |
| 6994 | // super_name, base_name, |
| 6995 | // child_idx, |
| 6996 | // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString()); |
| 6997 | // |
| 6998 | // |
| 6999 | if (base_class == base_spec) |
| 7000 | return child_idx; |
| 7001 | ++child_idx; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7002 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7003 | } |
| 7004 | |
| 7005 | return UINT32_MAX; |
| 7006 | } |
| 7007 | |
| 7008 | static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl, |
| 7009 | clang::NamedDecl *canonical_decl, |
| 7010 | bool omit_empty_base_classes) { |
| 7011 | uint32_t child_idx = ClangASTContext::GetNumBaseClasses( |
| 7012 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl), |
| 7013 | omit_empty_base_classes); |
| 7014 | |
| 7015 | clang::RecordDecl::field_iterator field, field_end; |
| 7016 | for (field = record_decl->field_begin(), field_end = record_decl->field_end(); |
| 7017 | field != field_end; ++field, ++child_idx) { |
| 7018 | if (field->getCanonicalDecl() == canonical_decl) |
| 7019 | return child_idx; |
| 7020 | } |
| 7021 | |
| 7022 | return UINT32_MAX; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7023 | } |
| 7024 | |
| 7025 | // Look for a child member (doesn't include base classes, but it does include |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 7026 | // their members) in the type hierarchy. Returns an index path into |
| 7027 | // "clang_type" on how to reach the appropriate member. |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7028 | // |
| 7029 | // class A |
| 7030 | // { |
| 7031 | // public: |
| 7032 | // int m_a; |
| 7033 | // int m_b; |
| 7034 | // }; |
| 7035 | // |
| 7036 | // class B |
| 7037 | // { |
| 7038 | // }; |
| 7039 | // |
| 7040 | // class C : |
| 7041 | // public B, |
| 7042 | // public A |
| 7043 | // { |
| 7044 | // }; |
| 7045 | // |
| 7046 | // If we have a clang type that describes "class C", and we wanted to looked |
| 7047 | // "m_b" in it: |
| 7048 | // |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7049 | // With omit_empty_base_classes == false we would get an integer array back |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 7050 | // with: { 1, 1 } The first index 1 is the child index for "class A" within |
| 7051 | // class C The second index 1 is the child index for "m_b" within class A |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7052 | // |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 7053 | // With omit_empty_base_classes == true we would get an integer array back |
| 7054 | // with: { 0, 1 } The first index 0 is the child index for "class A" within |
| 7055 | // class C (since class B doesn't have any members it doesn't count) The second |
| 7056 | // index 1 is the child index for "m_b" within class A |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7057 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7058 | size_t ClangASTContext::GetIndexOfChildMemberWithName( |
| 7059 | lldb::opaque_compiler_type_t type, const char *name, |
| 7060 | bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) { |
| 7061 | if (type && name && name[0]) { |
| 7062 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 7063 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 7064 | switch (type_class) { |
| 7065 | case clang::Type::Record: |
| 7066 | if (GetCompleteType(type)) { |
| 7067 | const clang::RecordType *record_type = |
| 7068 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 7069 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
Enrico Granata | 36f51e4 | 2015-12-18 22:41:25 +0000 | [diff] [blame] | 7070 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7071 | assert(record_decl); |
| 7072 | uint32_t child_idx = 0; |
| 7073 | |
| 7074 | const clang::CXXRecordDecl *cxx_record_decl = |
| 7075 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 7076 | |
| 7077 | // Try and find a field that matches NAME |
| 7078 | clang::RecordDecl::field_iterator field, field_end; |
| 7079 | llvm::StringRef name_sref(name); |
| 7080 | for (field = record_decl->field_begin(), |
| 7081 | field_end = record_decl->field_end(); |
| 7082 | field != field_end; ++field, ++child_idx) { |
| 7083 | llvm::StringRef field_name = field->getName(); |
| 7084 | if (field_name.empty()) { |
| 7085 | CompilerType field_type(getASTContext(), field->getType()); |
| 7086 | child_indexes.push_back(child_idx); |
| 7087 | if (field_type.GetIndexOfChildMemberWithName( |
| 7088 | name, omit_empty_base_classes, child_indexes)) |
| 7089 | return child_indexes.size(); |
| 7090 | child_indexes.pop_back(); |
| 7091 | |
| 7092 | } else if (field_name.equals(name_sref)) { |
| 7093 | // We have to add on the number of base classes to this index! |
| 7094 | child_indexes.push_back( |
| 7095 | child_idx + ClangASTContext::GetNumBaseClasses( |
| 7096 | cxx_record_decl, omit_empty_base_classes)); |
| 7097 | return child_indexes.size(); |
| 7098 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7099 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7100 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7101 | if (cxx_record_decl) { |
| 7102 | const clang::RecordDecl *parent_record_decl = cxx_record_decl; |
| 7103 | |
| 7104 | // printf ("parent = %s\n", parent_record_decl->getNameAsCString()); |
| 7105 | |
| 7106 | // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl(); |
| 7107 | // Didn't find things easily, lets let clang do its thang... |
| 7108 | clang::IdentifierInfo &ident_ref = |
| 7109 | getASTContext()->Idents.get(name_sref); |
| 7110 | clang::DeclarationName decl_name(&ident_ref); |
| 7111 | |
| 7112 | clang::CXXBasePaths paths; |
| 7113 | if (cxx_record_decl->lookupInBases( |
| 7114 | [decl_name](const clang::CXXBaseSpecifier *specifier, |
| 7115 | clang::CXXBasePath &path) { |
| 7116 | return clang::CXXRecordDecl::FindOrdinaryMember( |
| 7117 | specifier, path, decl_name); |
| 7118 | }, |
| 7119 | paths)) { |
| 7120 | clang::CXXBasePaths::const_paths_iterator path, |
| 7121 | path_end = paths.end(); |
| 7122 | for (path = paths.begin(); path != path_end; ++path) { |
| 7123 | const size_t num_path_elements = path->size(); |
| 7124 | for (size_t e = 0; e < num_path_elements; ++e) { |
| 7125 | clang::CXXBasePathElement elem = (*path)[e]; |
| 7126 | |
| 7127 | child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base, |
| 7128 | omit_empty_base_classes); |
| 7129 | if (child_idx == UINT32_MAX) { |
| 7130 | child_indexes.clear(); |
| 7131 | return 0; |
| 7132 | } else { |
| 7133 | child_indexes.push_back(child_idx); |
| 7134 | parent_record_decl = llvm::cast<clang::RecordDecl>( |
| 7135 | elem.Base->getType() |
| 7136 | ->getAs<clang::RecordType>() |
| 7137 | ->getDecl()); |
| 7138 | } |
| 7139 | } |
| 7140 | for (clang::NamedDecl *path_decl : path->Decls) { |
| 7141 | child_idx = GetIndexForRecordChild( |
| 7142 | parent_record_decl, path_decl, omit_empty_base_classes); |
| 7143 | if (child_idx == UINT32_MAX) { |
| 7144 | child_indexes.clear(); |
| 7145 | return 0; |
| 7146 | } else { |
| 7147 | child_indexes.push_back(child_idx); |
| 7148 | } |
| 7149 | } |
| 7150 | } |
| 7151 | return child_indexes.size(); |
| 7152 | } |
| 7153 | } |
| 7154 | } |
| 7155 | break; |
| 7156 | |
| 7157 | case clang::Type::ObjCObject: |
| 7158 | case clang::Type::ObjCInterface: |
| 7159 | if (GetCompleteType(type)) { |
| 7160 | llvm::StringRef name_sref(name); |
| 7161 | const clang::ObjCObjectType *objc_class_type = |
| 7162 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 7163 | assert(objc_class_type); |
| 7164 | if (objc_class_type) { |
| 7165 | uint32_t child_idx = 0; |
| 7166 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 7167 | objc_class_type->getInterface(); |
| 7168 | |
| 7169 | if (class_interface_decl) { |
| 7170 | clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, |
| 7171 | ivar_end = class_interface_decl->ivar_end(); |
| 7172 | clang::ObjCInterfaceDecl *superclass_interface_decl = |
| 7173 | class_interface_decl->getSuperClass(); |
| 7174 | |
| 7175 | for (ivar_pos = class_interface_decl->ivar_begin(); |
| 7176 | ivar_pos != ivar_end; ++ivar_pos, ++child_idx) { |
| 7177 | const clang::ObjCIvarDecl *ivar_decl = *ivar_pos; |
| 7178 | |
| 7179 | if (ivar_decl->getName().equals(name_sref)) { |
| 7180 | if ((!omit_empty_base_classes && superclass_interface_decl) || |
| 7181 | (omit_empty_base_classes && |
| 7182 | ObjCDeclHasIVars(superclass_interface_decl, true))) |
| 7183 | ++child_idx; |
| 7184 | |
| 7185 | child_indexes.push_back(child_idx); |
| 7186 | return child_indexes.size(); |
| 7187 | } |
| 7188 | } |
| 7189 | |
| 7190 | if (superclass_interface_decl) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 7191 | // The super class index is always zero for ObjC classes, so we |
| 7192 | // push it onto the child indexes in case we find an ivar in our |
| 7193 | // superclass... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7194 | child_indexes.push_back(0); |
| 7195 | |
| 7196 | CompilerType superclass_clang_type( |
| 7197 | getASTContext(), getASTContext()->getObjCInterfaceType( |
| 7198 | superclass_interface_decl)); |
| 7199 | if (superclass_clang_type.GetIndexOfChildMemberWithName( |
| 7200 | name, omit_empty_base_classes, child_indexes)) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 7201 | // We did find an ivar in a superclass so just return the |
| 7202 | // results! |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7203 | return child_indexes.size(); |
| 7204 | } |
| 7205 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 7206 | // We didn't find an ivar matching "name" in our superclass, pop |
| 7207 | // the superclass zero index that we pushed on above. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7208 | child_indexes.pop_back(); |
| 7209 | } |
| 7210 | } |
| 7211 | } |
| 7212 | } |
| 7213 | break; |
| 7214 | |
| 7215 | case clang::Type::ObjCObjectPointer: { |
| 7216 | CompilerType objc_object_clang_type( |
| 7217 | getASTContext(), |
| 7218 | llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr()) |
| 7219 | ->getPointeeType()); |
| 7220 | return objc_object_clang_type.GetIndexOfChildMemberWithName( |
| 7221 | name, omit_empty_base_classes, child_indexes); |
| 7222 | } break; |
| 7223 | |
| 7224 | case clang::Type::ConstantArray: { |
| 7225 | // const clang::ConstantArrayType *array = |
| 7226 | // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr()); |
| 7227 | // const uint64_t element_count = |
| 7228 | // array->getSize().getLimitedValue(); |
| 7229 | // |
| 7230 | // if (idx < element_count) |
| 7231 | // { |
| 7232 | // std::pair<uint64_t, unsigned> field_type_info = |
| 7233 | // ast->getTypeInfo(array->getElementType()); |
| 7234 | // |
| 7235 | // char element_name[32]; |
| 7236 | // ::snprintf (element_name, sizeof (element_name), |
| 7237 | // "%s[%u]", parent_name ? parent_name : "", idx); |
| 7238 | // |
| 7239 | // child_name.assign(element_name); |
| 7240 | // assert(field_type_info.first % 8 == 0); |
| 7241 | // child_byte_size = field_type_info.first / 8; |
| 7242 | // child_byte_offset = idx * child_byte_size; |
| 7243 | // return array->getElementType().getAsOpaquePtr(); |
| 7244 | // } |
| 7245 | } break; |
| 7246 | |
| 7247 | // case clang::Type::MemberPointerType: |
| 7248 | // { |
| 7249 | // MemberPointerType *mem_ptr_type = |
| 7250 | // llvm::cast<MemberPointerType>(qual_type.getTypePtr()); |
| 7251 | // clang::QualType pointee_type = |
| 7252 | // mem_ptr_type->getPointeeType(); |
| 7253 | // |
| 7254 | // if (ClangASTContext::IsAggregateType |
| 7255 | // (pointee_type.getAsOpaquePtr())) |
| 7256 | // { |
| 7257 | // return GetIndexOfChildWithName (ast, |
| 7258 | // mem_ptr_type->getPointeeType().getAsOpaquePtr(), |
| 7259 | // name); |
| 7260 | // } |
| 7261 | // } |
| 7262 | // break; |
| 7263 | // |
| 7264 | case clang::Type::LValueReference: |
| 7265 | case clang::Type::RValueReference: { |
| 7266 | const clang::ReferenceType *reference_type = |
| 7267 | llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()); |
| 7268 | clang::QualType pointee_type(reference_type->getPointeeType()); |
| 7269 | CompilerType pointee_clang_type(getASTContext(), pointee_type); |
| 7270 | |
| 7271 | if (pointee_clang_type.IsAggregateType()) { |
| 7272 | return pointee_clang_type.GetIndexOfChildMemberWithName( |
| 7273 | name, omit_empty_base_classes, child_indexes); |
| 7274 | } |
| 7275 | } break; |
| 7276 | |
| 7277 | case clang::Type::Pointer: { |
| 7278 | CompilerType pointee_clang_type(GetPointeeType(type)); |
| 7279 | |
| 7280 | if (pointee_clang_type.IsAggregateType()) { |
| 7281 | return pointee_clang_type.GetIndexOfChildMemberWithName( |
| 7282 | name, omit_empty_base_classes, child_indexes); |
| 7283 | } |
| 7284 | } break; |
| 7285 | |
| 7286 | case clang::Type::Typedef: |
| 7287 | return CompilerType(getASTContext(), |
| 7288 | llvm::cast<clang::TypedefType>(qual_type) |
| 7289 | ->getDecl() |
| 7290 | ->getUnderlyingType()) |
| 7291 | .GetIndexOfChildMemberWithName(name, omit_empty_base_classes, |
| 7292 | child_indexes); |
| 7293 | |
| 7294 | case clang::Type::Auto: |
| 7295 | return CompilerType( |
| 7296 | getASTContext(), |
| 7297 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) |
| 7298 | .GetIndexOfChildMemberWithName(name, omit_empty_base_classes, |
| 7299 | child_indexes); |
| 7300 | |
| 7301 | case clang::Type::Elaborated: |
| 7302 | return CompilerType( |
| 7303 | getASTContext(), |
| 7304 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) |
| 7305 | .GetIndexOfChildMemberWithName(name, omit_empty_base_classes, |
| 7306 | child_indexes); |
| 7307 | |
| 7308 | case clang::Type::Paren: |
| 7309 | return CompilerType(getASTContext(), |
| 7310 | llvm::cast<clang::ParenType>(qual_type)->desugar()) |
| 7311 | .GetIndexOfChildMemberWithName(name, omit_empty_base_classes, |
| 7312 | child_indexes); |
| 7313 | |
| 7314 | default: |
| 7315 | break; |
| 7316 | } |
| 7317 | } |
| 7318 | return 0; |
| 7319 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7320 | |
| 7321 | // Get the index of the child of "clang_type" whose name matches. This function |
| 7322 | // doesn't descend into the children, but only looks one level deep and name |
| 7323 | // matches can include base class names. |
| 7324 | |
| 7325 | uint32_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7326 | ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type, |
| 7327 | const char *name, |
| 7328 | bool omit_empty_base_classes) { |
| 7329 | if (type && name && name[0]) { |
| 7330 | clang::QualType qual_type(GetCanonicalQualType(type)); |
Enrico Granata | 36f51e4 | 2015-12-18 22:41:25 +0000 | [diff] [blame] | 7331 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7332 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 7333 | |
| 7334 | switch (type_class) { |
| 7335 | case clang::Type::Record: |
| 7336 | if (GetCompleteType(type)) { |
| 7337 | const clang::RecordType *record_type = |
| 7338 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 7339 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 7340 | |
| 7341 | assert(record_decl); |
| 7342 | uint32_t child_idx = 0; |
| 7343 | |
| 7344 | const clang::CXXRecordDecl *cxx_record_decl = |
| 7345 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 7346 | |
| 7347 | if (cxx_record_decl) { |
| 7348 | clang::CXXRecordDecl::base_class_const_iterator base_class, |
| 7349 | base_class_end; |
| 7350 | for (base_class = cxx_record_decl->bases_begin(), |
| 7351 | base_class_end = cxx_record_decl->bases_end(); |
| 7352 | base_class != base_class_end; ++base_class) { |
| 7353 | // Skip empty base classes |
| 7354 | clang::CXXRecordDecl *base_class_decl = |
| 7355 | llvm::cast<clang::CXXRecordDecl>( |
| 7356 | base_class->getType() |
| 7357 | ->getAs<clang::RecordType>() |
| 7358 | ->getDecl()); |
| 7359 | if (omit_empty_base_classes && |
| 7360 | ClangASTContext::RecordHasFields(base_class_decl) == false) |
| 7361 | continue; |
| 7362 | |
| 7363 | CompilerType base_class_clang_type(getASTContext(), |
| 7364 | base_class->getType()); |
| 7365 | std::string base_class_type_name( |
| 7366 | base_class_clang_type.GetTypeName().AsCString("")); |
| 7367 | if (base_class_type_name.compare(name) == 0) |
| 7368 | return child_idx; |
| 7369 | ++child_idx; |
| 7370 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7371 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7372 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7373 | // Try and find a field that matches NAME |
| 7374 | clang::RecordDecl::field_iterator field, field_end; |
| 7375 | llvm::StringRef name_sref(name); |
| 7376 | for (field = record_decl->field_begin(), |
| 7377 | field_end = record_decl->field_end(); |
| 7378 | field != field_end; ++field, ++child_idx) { |
| 7379 | if (field->getName().equals(name_sref)) |
| 7380 | return child_idx; |
| 7381 | } |
| 7382 | } |
| 7383 | break; |
| 7384 | |
| 7385 | case clang::Type::ObjCObject: |
| 7386 | case clang::Type::ObjCInterface: |
| 7387 | if (GetCompleteType(type)) { |
| 7388 | llvm::StringRef name_sref(name); |
| 7389 | const clang::ObjCObjectType *objc_class_type = |
| 7390 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 7391 | assert(objc_class_type); |
| 7392 | if (objc_class_type) { |
| 7393 | uint32_t child_idx = 0; |
| 7394 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 7395 | objc_class_type->getInterface(); |
| 7396 | |
| 7397 | if (class_interface_decl) { |
| 7398 | clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, |
| 7399 | ivar_end = class_interface_decl->ivar_end(); |
| 7400 | clang::ObjCInterfaceDecl *superclass_interface_decl = |
| 7401 | class_interface_decl->getSuperClass(); |
| 7402 | |
| 7403 | for (ivar_pos = class_interface_decl->ivar_begin(); |
| 7404 | ivar_pos != ivar_end; ++ivar_pos, ++child_idx) { |
| 7405 | const clang::ObjCIvarDecl *ivar_decl = *ivar_pos; |
| 7406 | |
| 7407 | if (ivar_decl->getName().equals(name_sref)) { |
| 7408 | if ((!omit_empty_base_classes && superclass_interface_decl) || |
| 7409 | (omit_empty_base_classes && |
| 7410 | ObjCDeclHasIVars(superclass_interface_decl, true))) |
| 7411 | ++child_idx; |
| 7412 | |
| 7413 | return child_idx; |
| 7414 | } |
| 7415 | } |
| 7416 | |
| 7417 | if (superclass_interface_decl) { |
| 7418 | if (superclass_interface_decl->getName().equals(name_sref)) |
| 7419 | return 0; |
| 7420 | } |
| 7421 | } |
| 7422 | } |
| 7423 | } |
| 7424 | break; |
| 7425 | |
| 7426 | case clang::Type::ObjCObjectPointer: { |
| 7427 | CompilerType pointee_clang_type( |
| 7428 | getASTContext(), |
| 7429 | llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr()) |
| 7430 | ->getPointeeType()); |
| 7431 | return pointee_clang_type.GetIndexOfChildWithName( |
| 7432 | name, omit_empty_base_classes); |
| 7433 | } break; |
| 7434 | |
| 7435 | case clang::Type::ConstantArray: { |
| 7436 | // const clang::ConstantArrayType *array = |
| 7437 | // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr()); |
| 7438 | // const uint64_t element_count = |
| 7439 | // array->getSize().getLimitedValue(); |
| 7440 | // |
| 7441 | // if (idx < element_count) |
| 7442 | // { |
| 7443 | // std::pair<uint64_t, unsigned> field_type_info = |
| 7444 | // ast->getTypeInfo(array->getElementType()); |
| 7445 | // |
| 7446 | // char element_name[32]; |
| 7447 | // ::snprintf (element_name, sizeof (element_name), |
| 7448 | // "%s[%u]", parent_name ? parent_name : "", idx); |
| 7449 | // |
| 7450 | // child_name.assign(element_name); |
| 7451 | // assert(field_type_info.first % 8 == 0); |
| 7452 | // child_byte_size = field_type_info.first / 8; |
| 7453 | // child_byte_offset = idx * child_byte_size; |
| 7454 | // return array->getElementType().getAsOpaquePtr(); |
| 7455 | // } |
| 7456 | } break; |
| 7457 | |
| 7458 | // case clang::Type::MemberPointerType: |
| 7459 | // { |
| 7460 | // MemberPointerType *mem_ptr_type = |
| 7461 | // llvm::cast<MemberPointerType>(qual_type.getTypePtr()); |
| 7462 | // clang::QualType pointee_type = |
| 7463 | // mem_ptr_type->getPointeeType(); |
| 7464 | // |
| 7465 | // if (ClangASTContext::IsAggregateType |
| 7466 | // (pointee_type.getAsOpaquePtr())) |
| 7467 | // { |
| 7468 | // return GetIndexOfChildWithName (ast, |
| 7469 | // mem_ptr_type->getPointeeType().getAsOpaquePtr(), |
| 7470 | // name); |
| 7471 | // } |
| 7472 | // } |
| 7473 | // break; |
| 7474 | // |
| 7475 | case clang::Type::LValueReference: |
| 7476 | case clang::Type::RValueReference: { |
| 7477 | const clang::ReferenceType *reference_type = |
| 7478 | llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()); |
| 7479 | CompilerType pointee_type(getASTContext(), |
| 7480 | reference_type->getPointeeType()); |
| 7481 | |
| 7482 | if (pointee_type.IsAggregateType()) { |
| 7483 | return pointee_type.GetIndexOfChildWithName(name, |
| 7484 | omit_empty_base_classes); |
| 7485 | } |
| 7486 | } break; |
| 7487 | |
| 7488 | case clang::Type::Pointer: { |
| 7489 | const clang::PointerType *pointer_type = |
| 7490 | llvm::cast<clang::PointerType>(qual_type.getTypePtr()); |
| 7491 | CompilerType pointee_type(getASTContext(), |
| 7492 | pointer_type->getPointeeType()); |
| 7493 | |
| 7494 | if (pointee_type.IsAggregateType()) { |
| 7495 | return pointee_type.GetIndexOfChildWithName(name, |
| 7496 | omit_empty_base_classes); |
| 7497 | } else { |
| 7498 | // if (parent_name) |
| 7499 | // { |
| 7500 | // child_name.assign(1, '*'); |
| 7501 | // child_name += parent_name; |
| 7502 | // } |
| 7503 | // |
| 7504 | // // We have a pointer to an simple type |
| 7505 | // if (idx == 0) |
| 7506 | // { |
| 7507 | // std::pair<uint64_t, unsigned> clang_type_info |
| 7508 | // = ast->getTypeInfo(pointee_type); |
| 7509 | // assert(clang_type_info.first % 8 == 0); |
| 7510 | // child_byte_size = clang_type_info.first / 8; |
| 7511 | // child_byte_offset = 0; |
| 7512 | // return pointee_type.getAsOpaquePtr(); |
| 7513 | // } |
| 7514 | } |
| 7515 | } break; |
| 7516 | |
| 7517 | case clang::Type::Auto: |
| 7518 | return CompilerType( |
| 7519 | getASTContext(), |
| 7520 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) |
| 7521 | .GetIndexOfChildWithName(name, omit_empty_base_classes); |
| 7522 | |
| 7523 | case clang::Type::Elaborated: |
| 7524 | return CompilerType( |
| 7525 | getASTContext(), |
| 7526 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) |
| 7527 | .GetIndexOfChildWithName(name, omit_empty_base_classes); |
| 7528 | |
| 7529 | case clang::Type::Paren: |
| 7530 | return CompilerType(getASTContext(), |
| 7531 | llvm::cast<clang::ParenType>(qual_type)->desugar()) |
| 7532 | .GetIndexOfChildWithName(name, omit_empty_base_classes); |
| 7533 | |
| 7534 | case clang::Type::Typedef: |
| 7535 | return CompilerType(getASTContext(), |
| 7536 | llvm::cast<clang::TypedefType>(qual_type) |
| 7537 | ->getDecl() |
| 7538 | ->getUnderlyingType()) |
| 7539 | .GetIndexOfChildWithName(name, omit_empty_base_classes); |
| 7540 | |
| 7541 | default: |
| 7542 | break; |
| 7543 | } |
| 7544 | } |
| 7545 | return UINT32_MAX; |
| 7546 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7547 | |
| 7548 | size_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7549 | ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) { |
| 7550 | if (!type) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7551 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7552 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7553 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 7554 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 7555 | switch (type_class) { |
| 7556 | case clang::Type::Record: |
| 7557 | if (GetCompleteType(type)) { |
| 7558 | const clang::CXXRecordDecl *cxx_record_decl = |
| 7559 | qual_type->getAsCXXRecordDecl(); |
| 7560 | if (cxx_record_decl) { |
| 7561 | const clang::ClassTemplateSpecializationDecl *template_decl = |
| 7562 | llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>( |
| 7563 | cxx_record_decl); |
| 7564 | if (template_decl) |
| 7565 | return template_decl->getTemplateArgs().size(); |
| 7566 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7567 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7568 | break; |
| 7569 | |
| 7570 | case clang::Type::Typedef: |
| 7571 | return (CompilerType(getASTContext(), |
| 7572 | llvm::cast<clang::TypedefType>(qual_type) |
| 7573 | ->getDecl() |
| 7574 | ->getUnderlyingType())) |
| 7575 | .GetNumTemplateArguments(); |
| 7576 | |
| 7577 | case clang::Type::Auto: |
| 7578 | return (CompilerType( |
| 7579 | getASTContext(), |
| 7580 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType())) |
| 7581 | .GetNumTemplateArguments(); |
| 7582 | |
| 7583 | case clang::Type::Elaborated: |
| 7584 | return (CompilerType( |
| 7585 | getASTContext(), |
| 7586 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())) |
| 7587 | .GetNumTemplateArguments(); |
| 7588 | |
| 7589 | case clang::Type::Paren: |
| 7590 | return (CompilerType(getASTContext(), |
| 7591 | llvm::cast<clang::ParenType>(qual_type)->desugar())) |
| 7592 | .GetNumTemplateArguments(); |
| 7593 | |
| 7594 | default: |
| 7595 | break; |
| 7596 | } |
| 7597 | |
| 7598 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7599 | } |
| 7600 | |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7601 | const clang::ClassTemplateSpecializationDecl * |
| 7602 | ClangASTContext::GetAsTemplateSpecialization( |
| 7603 | lldb::opaque_compiler_type_t type) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7604 | if (!type) |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7605 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7606 | |
| 7607 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 7608 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 7609 | switch (type_class) { |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7610 | case clang::Type::Record: { |
| 7611 | if (! GetCompleteType(type)) |
| 7612 | return nullptr; |
| 7613 | const clang::CXXRecordDecl *cxx_record_decl = |
| 7614 | qual_type->getAsCXXRecordDecl(); |
| 7615 | if (!cxx_record_decl) |
| 7616 | return nullptr; |
| 7617 | return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>( |
| 7618 | cxx_record_decl); |
| 7619 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7620 | |
| 7621 | case clang::Type::Typedef: |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7622 | return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type) |
| 7623 | ->getDecl() |
| 7624 | ->getUnderlyingType() |
| 7625 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7626 | |
| 7627 | case clang::Type::Auto: |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7628 | return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type) |
| 7629 | ->getDeducedType() |
| 7630 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7631 | |
| 7632 | case clang::Type::Elaborated: |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7633 | return GetAsTemplateSpecialization( |
| 7634 | llvm::cast<clang::ElaboratedType>(qual_type) |
| 7635 | ->getNamedType() |
| 7636 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7637 | |
| 7638 | case clang::Type::Paren: |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7639 | return GetAsTemplateSpecialization( |
| 7640 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7641 | |
| 7642 | default: |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7643 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7644 | } |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7645 | } |
| 7646 | |
| 7647 | lldb::TemplateArgumentKind |
| 7648 | ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type, |
| 7649 | size_t arg_idx) { |
| 7650 | const clang::ClassTemplateSpecializationDecl *template_decl = |
| 7651 | GetAsTemplateSpecialization(type); |
| 7652 | if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size()) |
| 7653 | return eTemplateArgumentKindNull; |
| 7654 | |
| 7655 | switch (template_decl->getTemplateArgs()[arg_idx].getKind()) { |
| 7656 | case clang::TemplateArgument::Null: |
| 7657 | return eTemplateArgumentKindNull; |
| 7658 | |
| 7659 | case clang::TemplateArgument::NullPtr: |
| 7660 | return eTemplateArgumentKindNullPtr; |
| 7661 | |
| 7662 | case clang::TemplateArgument::Type: |
| 7663 | return eTemplateArgumentKindType; |
| 7664 | |
| 7665 | case clang::TemplateArgument::Declaration: |
| 7666 | return eTemplateArgumentKindDeclaration; |
| 7667 | |
| 7668 | case clang::TemplateArgument::Integral: |
| 7669 | return eTemplateArgumentKindIntegral; |
| 7670 | |
| 7671 | case clang::TemplateArgument::Template: |
| 7672 | return eTemplateArgumentKindTemplate; |
| 7673 | |
| 7674 | case clang::TemplateArgument::TemplateExpansion: |
| 7675 | return eTemplateArgumentKindTemplateExpansion; |
| 7676 | |
| 7677 | case clang::TemplateArgument::Expression: |
| 7678 | return eTemplateArgumentKindExpression; |
| 7679 | |
| 7680 | case clang::TemplateArgument::Pack: |
| 7681 | return eTemplateArgumentKindPack; |
| 7682 | } |
| 7683 | llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind"); |
| 7684 | } |
| 7685 | |
| 7686 | CompilerType |
| 7687 | ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type, |
| 7688 | size_t idx) { |
| 7689 | const clang::ClassTemplateSpecializationDecl *template_decl = |
| 7690 | GetAsTemplateSpecialization(type); |
| 7691 | if (!template_decl || idx >= template_decl->getTemplateArgs().size()) |
| 7692 | return CompilerType(); |
| 7693 | |
| 7694 | const clang::TemplateArgument &template_arg = |
| 7695 | template_decl->getTemplateArgs()[idx]; |
| 7696 | if (template_arg.getKind() != clang::TemplateArgument::Type) |
| 7697 | return CompilerType(); |
| 7698 | |
| 7699 | return CompilerType(getASTContext(), template_arg.getAsType()); |
| 7700 | } |
| 7701 | |
Pavel Labath | f59056f | 2017-11-30 10:16:54 +0000 | [diff] [blame] | 7702 | llvm::Optional<CompilerType::IntegralTemplateArgument> |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7703 | ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type, |
| 7704 | size_t idx) { |
| 7705 | const clang::ClassTemplateSpecializationDecl *template_decl = |
| 7706 | GetAsTemplateSpecialization(type); |
| 7707 | if (! template_decl || idx >= template_decl->getTemplateArgs().size()) |
Pavel Labath | f59056f | 2017-11-30 10:16:54 +0000 | [diff] [blame] | 7708 | return llvm::None; |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7709 | |
| 7710 | const clang::TemplateArgument &template_arg = |
| 7711 | template_decl->getTemplateArgs()[idx]; |
| 7712 | if (template_arg.getKind() != clang::TemplateArgument::Integral) |
Pavel Labath | f59056f | 2017-11-30 10:16:54 +0000 | [diff] [blame] | 7713 | return llvm::None; |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7714 | |
Pavel Labath | f59056f | 2017-11-30 10:16:54 +0000 | [diff] [blame] | 7715 | return {{template_arg.getAsIntegral(), |
| 7716 | CompilerType(getASTContext(), template_arg.getIntegralType())}}; |
Enrico Granata | c6bf2e2 | 2015-09-23 01:39:46 +0000 | [diff] [blame] | 7717 | } |
| 7718 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7719 | CompilerType ClangASTContext::GetTypeForFormatters(void *type) { |
| 7720 | if (type) |
| 7721 | return ClangUtil::RemoveFastQualifiers(CompilerType(this, type)); |
| 7722 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7723 | } |
| 7724 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7725 | clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) { |
| 7726 | const clang::EnumType *enutype = |
| 7727 | llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type)); |
| 7728 | if (enutype) |
| 7729 | return enutype->getDecl(); |
| 7730 | return NULL; |
| 7731 | } |
| 7732 | |
| 7733 | clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) { |
| 7734 | const clang::RecordType *record_type = |
| 7735 | llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type)); |
| 7736 | if (record_type) |
| 7737 | return record_type->getDecl(); |
| 7738 | return nullptr; |
| 7739 | } |
| 7740 | |
| 7741 | clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) { |
| 7742 | clang::QualType qual_type = ClangUtil::GetCanonicalQualType(type); |
| 7743 | if (qual_type.isNull()) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7744 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7745 | else |
| 7746 | return qual_type->getAsTagDecl(); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 7747 | } |
| 7748 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7749 | clang::CXXRecordDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7750 | ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) { |
| 7751 | return GetCanonicalQualType(type)->getAsCXXRecordDecl(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7752 | } |
| 7753 | |
| 7754 | clang::ObjCInterfaceDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7755 | ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) { |
| 7756 | const clang::ObjCObjectType *objc_class_type = |
| 7757 | llvm::dyn_cast<clang::ObjCObjectType>( |
| 7758 | ClangUtil::GetCanonicalQualType(type)); |
| 7759 | if (objc_class_type) |
| 7760 | return objc_class_type->getInterface(); |
| 7761 | return nullptr; |
| 7762 | } |
| 7763 | |
| 7764 | clang::FieldDecl *ClangASTContext::AddFieldToRecordType( |
| 7765 | const CompilerType &type, const char *name, |
| 7766 | const CompilerType &field_clang_type, AccessType access, |
| 7767 | uint32_t bitfield_bit_size) { |
| 7768 | if (!type.IsValid() || !field_clang_type.IsValid()) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7769 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7770 | ClangASTContext *ast = |
| 7771 | llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem()); |
| 7772 | if (!ast) |
| 7773 | return nullptr; |
| 7774 | clang::ASTContext *clang_ast = ast->getASTContext(); |
| 7775 | |
| 7776 | clang::FieldDecl *field = nullptr; |
| 7777 | |
| 7778 | clang::Expr *bit_width = nullptr; |
| 7779 | if (bitfield_bit_size != 0) { |
| 7780 | llvm::APInt bitfield_bit_size_apint( |
| 7781 | clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size); |
| 7782 | bit_width = new (*clang_ast) |
| 7783 | clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint, |
| 7784 | clang_ast->IntTy, clang::SourceLocation()); |
| 7785 | } |
| 7786 | |
| 7787 | clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type); |
| 7788 | if (record_decl) { |
| 7789 | field = clang::FieldDecl::Create( |
| 7790 | *clang_ast, record_decl, clang::SourceLocation(), |
| 7791 | clang::SourceLocation(), |
| 7792 | name ? &clang_ast->Idents.get(name) : nullptr, // Identifier |
| 7793 | ClangUtil::GetQualType(field_clang_type), // Field type |
| 7794 | nullptr, // TInfo * |
| 7795 | bit_width, // BitWidth |
| 7796 | false, // Mutable |
| 7797 | clang::ICIS_NoInit); // HasInit |
| 7798 | |
| 7799 | if (!name) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 7800 | // Determine whether this field corresponds to an anonymous struct or |
| 7801 | // union. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7802 | if (const clang::TagType *TagT = |
| 7803 | field->getType()->getAs<clang::TagType>()) { |
| 7804 | if (clang::RecordDecl *Rec = |
| 7805 | llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl())) |
| 7806 | if (!Rec->getDeclName()) { |
| 7807 | Rec->setAnonymousStructOrUnion(true); |
| 7808 | field->setImplicit(); |
| 7809 | } |
| 7810 | } |
| 7811 | } |
| 7812 | |
| 7813 | if (field) { |
| 7814 | field->setAccess( |
| 7815 | ClangASTContext::ConvertAccessTypeToAccessSpecifier(access)); |
| 7816 | |
| 7817 | record_decl->addDecl(field); |
| 7818 | |
| 7819 | #ifdef LLDB_CONFIGURATION_DEBUG |
| 7820 | VerifyDecl(field); |
| 7821 | #endif |
| 7822 | } |
| 7823 | } else { |
| 7824 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 7825 | ast->GetAsObjCInterfaceDecl(type); |
| 7826 | |
| 7827 | if (class_interface_decl) { |
| 7828 | const bool is_synthesized = false; |
| 7829 | |
| 7830 | field_clang_type.GetCompleteType(); |
| 7831 | |
| 7832 | field = clang::ObjCIvarDecl::Create( |
| 7833 | *clang_ast, class_interface_decl, clang::SourceLocation(), |
| 7834 | clang::SourceLocation(), |
| 7835 | name ? &clang_ast->Idents.get(name) : nullptr, // Identifier |
| 7836 | ClangUtil::GetQualType(field_clang_type), // Field type |
| 7837 | nullptr, // TypeSourceInfo * |
| 7838 | ConvertAccessTypeToObjCIvarAccessControl(access), bit_width, |
| 7839 | is_synthesized); |
| 7840 | |
| 7841 | if (field) { |
| 7842 | class_interface_decl->addDecl(field); |
| 7843 | |
| 7844 | #ifdef LLDB_CONFIGURATION_DEBUG |
| 7845 | VerifyDecl(field); |
| 7846 | #endif |
| 7847 | } |
| 7848 | } |
| 7849 | } |
| 7850 | return field; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7851 | } |
| 7852 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7853 | void ClangASTContext::BuildIndirectFields(const CompilerType &type) { |
| 7854 | if (!type) |
| 7855 | return; |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 7856 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7857 | ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); |
| 7858 | if (!ast) |
| 7859 | return; |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 7860 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7861 | clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type); |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 7862 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7863 | if (!record_decl) |
| 7864 | return; |
| 7865 | |
| 7866 | typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector; |
| 7867 | |
| 7868 | IndirectFieldVector indirect_fields; |
| 7869 | clang::RecordDecl::field_iterator field_pos; |
| 7870 | clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end(); |
| 7871 | clang::RecordDecl::field_iterator last_field_pos = field_end_pos; |
| 7872 | for (field_pos = record_decl->field_begin(); field_pos != field_end_pos; |
| 7873 | last_field_pos = field_pos++) { |
| 7874 | if (field_pos->isAnonymousStructOrUnion()) { |
| 7875 | clang::QualType field_qual_type = field_pos->getType(); |
| 7876 | |
| 7877 | const clang::RecordType *field_record_type = |
| 7878 | field_qual_type->getAs<clang::RecordType>(); |
| 7879 | |
| 7880 | if (!field_record_type) |
| 7881 | continue; |
| 7882 | |
| 7883 | clang::RecordDecl *field_record_decl = field_record_type->getDecl(); |
| 7884 | |
| 7885 | if (!field_record_decl) |
| 7886 | continue; |
| 7887 | |
| 7888 | for (clang::RecordDecl::decl_iterator |
| 7889 | di = field_record_decl->decls_begin(), |
| 7890 | de = field_record_decl->decls_end(); |
| 7891 | di != de; ++di) { |
| 7892 | if (clang::FieldDecl *nested_field_decl = |
| 7893 | llvm::dyn_cast<clang::FieldDecl>(*di)) { |
| 7894 | clang::NamedDecl **chain = |
| 7895 | new (*ast->getASTContext()) clang::NamedDecl *[2]; |
| 7896 | chain[0] = *field_pos; |
| 7897 | chain[1] = nested_field_decl; |
| 7898 | clang::IndirectFieldDecl *indirect_field = |
| 7899 | clang::IndirectFieldDecl::Create( |
| 7900 | *ast->getASTContext(), record_decl, clang::SourceLocation(), |
| 7901 | nested_field_decl->getIdentifier(), |
| 7902 | nested_field_decl->getType(), {chain, 2}); |
| 7903 | |
| 7904 | indirect_field->setImplicit(); |
| 7905 | |
| 7906 | indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers( |
| 7907 | field_pos->getAccess(), nested_field_decl->getAccess())); |
| 7908 | |
| 7909 | indirect_fields.push_back(indirect_field); |
| 7910 | } else if (clang::IndirectFieldDecl *nested_indirect_field_decl = |
| 7911 | llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) { |
| 7912 | size_t nested_chain_size = |
| 7913 | nested_indirect_field_decl->getChainingSize(); |
| 7914 | clang::NamedDecl **chain = new (*ast->getASTContext()) |
| 7915 | clang::NamedDecl *[nested_chain_size + 1]; |
| 7916 | chain[0] = *field_pos; |
| 7917 | |
| 7918 | int chain_index = 1; |
| 7919 | for (clang::IndirectFieldDecl::chain_iterator |
| 7920 | nci = nested_indirect_field_decl->chain_begin(), |
| 7921 | nce = nested_indirect_field_decl->chain_end(); |
| 7922 | nci < nce; ++nci) { |
| 7923 | chain[chain_index] = *nci; |
| 7924 | chain_index++; |
| 7925 | } |
| 7926 | |
| 7927 | clang::IndirectFieldDecl *indirect_field = |
| 7928 | clang::IndirectFieldDecl::Create( |
| 7929 | *ast->getASTContext(), record_decl, clang::SourceLocation(), |
| 7930 | nested_indirect_field_decl->getIdentifier(), |
| 7931 | nested_indirect_field_decl->getType(), |
| 7932 | {chain, nested_chain_size + 1}); |
| 7933 | |
| 7934 | indirect_field->setImplicit(); |
| 7935 | |
| 7936 | indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers( |
| 7937 | field_pos->getAccess(), nested_indirect_field_decl->getAccess())); |
| 7938 | |
| 7939 | indirect_fields.push_back(indirect_field); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7940 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7941 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7942 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7943 | } |
| 7944 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 7945 | // Check the last field to see if it has an incomplete array type as its last |
| 7946 | // member and if it does, the tell the record decl about it |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7947 | if (last_field_pos != field_end_pos) { |
| 7948 | if (last_field_pos->getType()->isIncompleteArrayType()) |
| 7949 | record_decl->hasFlexibleArrayMember(); |
| 7950 | } |
| 7951 | |
| 7952 | for (IndirectFieldVector::iterator ifi = indirect_fields.begin(), |
| 7953 | ife = indirect_fields.end(); |
| 7954 | ifi < ife; ++ifi) { |
| 7955 | record_decl->addDecl(*ifi); |
| 7956 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7957 | } |
| 7958 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7959 | void ClangASTContext::SetIsPacked(const CompilerType &type) { |
| 7960 | if (type) { |
| 7961 | ClangASTContext *ast = |
| 7962 | llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); |
| 7963 | if (ast) { |
| 7964 | clang::RecordDecl *record_decl = GetAsRecordDecl(type); |
| 7965 | |
| 7966 | if (!record_decl) |
Greg Clayton | f73034f | 2015-09-08 18:15:05 +0000 | [diff] [blame] | 7967 | return; |
| 7968 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7969 | record_decl->addAttr( |
| 7970 | clang::PackedAttr::CreateImplicit(*ast->getASTContext())); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7971 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7972 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7973 | } |
| 7974 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7975 | clang::VarDecl *ClangASTContext::AddVariableToRecordType( |
| 7976 | const CompilerType &type, const char *name, const CompilerType &var_type, |
| 7977 | AccessType access) { |
| 7978 | clang::VarDecl *var_decl = nullptr; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7979 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7980 | if (!type.IsValid() || !var_type.IsValid()) |
| 7981 | return nullptr; |
| 7982 | ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); |
| 7983 | if (!ast) |
| 7984 | return nullptr; |
| 7985 | |
| 7986 | clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type); |
| 7987 | if (record_decl) { |
| 7988 | var_decl = clang::VarDecl::Create( |
| 7989 | *ast->getASTContext(), // ASTContext & |
| 7990 | record_decl, // DeclContext * |
| 7991 | clang::SourceLocation(), // clang::SourceLocation StartLoc |
| 7992 | clang::SourceLocation(), // clang::SourceLocation IdLoc |
| 7993 | name ? &ast->getASTContext()->Idents.get(name) |
| 7994 | : nullptr, // clang::IdentifierInfo * |
| 7995 | ClangUtil::GetQualType(var_type), // Variable clang::QualType |
| 7996 | nullptr, // TypeSourceInfo * |
| 7997 | clang::SC_Static); // StorageClass |
| 7998 | if (var_decl) { |
| 7999 | var_decl->setAccess( |
| 8000 | ClangASTContext::ConvertAccessTypeToAccessSpecifier(access)); |
| 8001 | record_decl->addDecl(var_decl); |
| 8002 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8003 | #ifdef LLDB_CONFIGURATION_DEBUG |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8004 | VerifyDecl(var_decl); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8005 | #endif |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8006 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8007 | } |
| 8008 | return var_decl; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8009 | } |
| 8010 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8011 | clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType( |
Davide Italiano | 675767a | 2018-03-27 19:40:50 +0000 | [diff] [blame] | 8012 | lldb::opaque_compiler_type_t type, const char *name, const char *mangled_name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8013 | const CompilerType &method_clang_type, lldb::AccessType access, |
| 8014 | bool is_virtual, bool is_static, bool is_inline, bool is_explicit, |
| 8015 | bool is_attr_used, bool is_artificial) { |
| 8016 | if (!type || !method_clang_type.IsValid() || name == nullptr || |
| 8017 | name[0] == '\0') |
| 8018 | return nullptr; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8019 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8020 | clang::QualType record_qual_type(GetCanonicalQualType(type)); |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 8021 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8022 | clang::CXXRecordDecl *cxx_record_decl = |
| 8023 | record_qual_type->getAsCXXRecordDecl(); |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 8024 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8025 | if (cxx_record_decl == nullptr) |
| 8026 | return nullptr; |
| 8027 | |
| 8028 | clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type)); |
| 8029 | |
| 8030 | clang::CXXMethodDecl *cxx_method_decl = nullptr; |
| 8031 | |
| 8032 | clang::DeclarationName decl_name(&getASTContext()->Idents.get(name)); |
| 8033 | |
| 8034 | const clang::FunctionType *function_type = |
| 8035 | llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr()); |
| 8036 | |
| 8037 | if (function_type == nullptr) |
| 8038 | return nullptr; |
| 8039 | |
| 8040 | const clang::FunctionProtoType *method_function_prototype( |
| 8041 | llvm::dyn_cast<clang::FunctionProtoType>(function_type)); |
| 8042 | |
| 8043 | if (!method_function_prototype) |
| 8044 | return nullptr; |
| 8045 | |
| 8046 | unsigned int num_params = method_function_prototype->getNumParams(); |
| 8047 | |
| 8048 | clang::CXXDestructorDecl *cxx_dtor_decl(nullptr); |
| 8049 | clang::CXXConstructorDecl *cxx_ctor_decl(nullptr); |
| 8050 | |
| 8051 | if (is_artificial) |
| 8052 | return nullptr; // skip everything artificial |
| 8053 | |
| 8054 | if (name[0] == '~') { |
| 8055 | cxx_dtor_decl = clang::CXXDestructorDecl::Create( |
| 8056 | *getASTContext(), cxx_record_decl, clang::SourceLocation(), |
| 8057 | clang::DeclarationNameInfo( |
| 8058 | getASTContext()->DeclarationNames.getCXXDestructorName( |
| 8059 | getASTContext()->getCanonicalType(record_qual_type)), |
| 8060 | clang::SourceLocation()), |
| 8061 | method_qual_type, nullptr, is_inline, is_artificial); |
| 8062 | cxx_method_decl = cxx_dtor_decl; |
| 8063 | } else if (decl_name == cxx_record_decl->getDeclName()) { |
| 8064 | cxx_ctor_decl = clang::CXXConstructorDecl::Create( |
| 8065 | *getASTContext(), cxx_record_decl, clang::SourceLocation(), |
| 8066 | clang::DeclarationNameInfo( |
| 8067 | getASTContext()->DeclarationNames.getCXXConstructorName( |
| 8068 | getASTContext()->getCanonicalType(record_qual_type)), |
| 8069 | clang::SourceLocation()), |
| 8070 | method_qual_type, |
| 8071 | nullptr, // TypeSourceInfo * |
| 8072 | is_explicit, is_inline, is_artificial, false /*is_constexpr*/); |
| 8073 | cxx_method_decl = cxx_ctor_decl; |
| 8074 | } else { |
| 8075 | clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None; |
| 8076 | clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS; |
| 8077 | |
| 8078 | if (IsOperator(name, op_kind)) { |
| 8079 | if (op_kind != clang::NUM_OVERLOADED_OPERATORS) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 8080 | // Check the number of operator parameters. Sometimes we have seen bad |
| 8081 | // DWARF that doesn't correctly describe operators and if we try to |
| 8082 | // create a method and add it to the class, clang will assert and |
| 8083 | // crash, so we need to make sure things are acceptable. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8084 | const bool is_method = true; |
| 8085 | if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount( |
| 8086 | is_method, op_kind, num_params)) |
| 8087 | return nullptr; |
| 8088 | cxx_method_decl = clang::CXXMethodDecl::Create( |
| 8089 | *getASTContext(), cxx_record_decl, clang::SourceLocation(), |
| 8090 | clang::DeclarationNameInfo( |
| 8091 | getASTContext()->DeclarationNames.getCXXOperatorName(op_kind), |
| 8092 | clang::SourceLocation()), |
| 8093 | method_qual_type, |
| 8094 | nullptr, // TypeSourceInfo * |
| 8095 | SC, is_inline, false /*is_constexpr*/, clang::SourceLocation()); |
| 8096 | } else if (num_params == 0) { |
| 8097 | // Conversion operators don't take params... |
| 8098 | cxx_method_decl = clang::CXXConversionDecl::Create( |
| 8099 | *getASTContext(), cxx_record_decl, clang::SourceLocation(), |
| 8100 | clang::DeclarationNameInfo( |
| 8101 | getASTContext()->DeclarationNames.getCXXConversionFunctionName( |
| 8102 | getASTContext()->getCanonicalType( |
| 8103 | function_type->getReturnType())), |
| 8104 | clang::SourceLocation()), |
| 8105 | method_qual_type, |
| 8106 | nullptr, // TypeSourceInfo * |
| 8107 | is_inline, is_explicit, false /*is_constexpr*/, |
| 8108 | clang::SourceLocation()); |
| 8109 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8110 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8111 | |
| 8112 | if (cxx_method_decl == nullptr) { |
| 8113 | cxx_method_decl = clang::CXXMethodDecl::Create( |
| 8114 | *getASTContext(), cxx_record_decl, clang::SourceLocation(), |
| 8115 | clang::DeclarationNameInfo(decl_name, clang::SourceLocation()), |
| 8116 | method_qual_type, |
| 8117 | nullptr, // TypeSourceInfo * |
| 8118 | SC, is_inline, false /*is_constexpr*/, clang::SourceLocation()); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8119 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8120 | } |
| 8121 | |
| 8122 | clang::AccessSpecifier access_specifier = |
| 8123 | ClangASTContext::ConvertAccessTypeToAccessSpecifier(access); |
| 8124 | |
| 8125 | cxx_method_decl->setAccess(access_specifier); |
| 8126 | cxx_method_decl->setVirtualAsWritten(is_virtual); |
| 8127 | |
| 8128 | if (is_attr_used) |
| 8129 | cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext())); |
| 8130 | |
Davide Italiano | 675767a | 2018-03-27 19:40:50 +0000 | [diff] [blame] | 8131 | if (mangled_name != NULL) { |
| 8132 | cxx_method_decl->addAttr( |
| 8133 | clang::AsmLabelAttr::CreateImplicit(*getASTContext(), mangled_name)); |
| 8134 | } |
| 8135 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8136 | // Populate the method decl with parameter decls |
| 8137 | |
| 8138 | llvm::SmallVector<clang::ParmVarDecl *, 12> params; |
| 8139 | |
| 8140 | for (unsigned param_index = 0; param_index < num_params; ++param_index) { |
| 8141 | params.push_back(clang::ParmVarDecl::Create( |
| 8142 | *getASTContext(), cxx_method_decl, clang::SourceLocation(), |
| 8143 | clang::SourceLocation(), |
| 8144 | nullptr, // anonymous |
| 8145 | method_function_prototype->getParamType(param_index), nullptr, |
| 8146 | clang::SC_None, nullptr)); |
| 8147 | } |
| 8148 | |
| 8149 | cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params)); |
| 8150 | |
| 8151 | cxx_record_decl->addDecl(cxx_method_decl); |
| 8152 | |
| 8153 | // Sometimes the debug info will mention a constructor (default/copy/move), |
| 8154 | // destructor, or assignment operator (copy/move) but there won't be any |
| 8155 | // version of this in the code. So we check if the function was artificially |
| 8156 | // generated and if it is trivial and this lets the compiler/backend know |
| 8157 | // that it can inline the IR for these when it needs to and we can avoid a |
| 8158 | // "missing function" error when running expressions. |
| 8159 | |
| 8160 | if (is_artificial) { |
| 8161 | if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() && |
| 8162 | cxx_record_decl->hasTrivialDefaultConstructor()) || |
| 8163 | (cxx_ctor_decl->isCopyConstructor() && |
| 8164 | cxx_record_decl->hasTrivialCopyConstructor()) || |
| 8165 | (cxx_ctor_decl->isMoveConstructor() && |
| 8166 | cxx_record_decl->hasTrivialMoveConstructor()))) { |
| 8167 | cxx_ctor_decl->setDefaulted(); |
| 8168 | cxx_ctor_decl->setTrivial(true); |
| 8169 | } else if (cxx_dtor_decl) { |
| 8170 | if (cxx_record_decl->hasTrivialDestructor()) { |
| 8171 | cxx_dtor_decl->setDefaulted(); |
| 8172 | cxx_dtor_decl->setTrivial(true); |
| 8173 | } |
| 8174 | } else if ((cxx_method_decl->isCopyAssignmentOperator() && |
| 8175 | cxx_record_decl->hasTrivialCopyAssignment()) || |
| 8176 | (cxx_method_decl->isMoveAssignmentOperator() && |
| 8177 | cxx_record_decl->hasTrivialMoveAssignment())) { |
| 8178 | cxx_method_decl->setDefaulted(); |
| 8179 | cxx_method_decl->setTrivial(true); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8180 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8181 | } |
| 8182 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8183 | #ifdef LLDB_CONFIGURATION_DEBUG |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8184 | VerifyDecl(cxx_method_decl); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8185 | #endif |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8186 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8187 | // printf ("decl->isPolymorphic() = %i\n", |
| 8188 | // cxx_record_decl->isPolymorphic()); |
| 8189 | // printf ("decl->isAggregate() = %i\n", |
| 8190 | // cxx_record_decl->isAggregate()); |
| 8191 | // printf ("decl->isPOD() = %i\n", |
| 8192 | // cxx_record_decl->isPOD()); |
| 8193 | // printf ("decl->isEmpty() = %i\n", |
| 8194 | // cxx_record_decl->isEmpty()); |
| 8195 | // printf ("decl->isAbstract() = %i\n", |
| 8196 | // cxx_record_decl->isAbstract()); |
| 8197 | // printf ("decl->hasTrivialConstructor() = %i\n", |
| 8198 | // cxx_record_decl->hasTrivialConstructor()); |
| 8199 | // printf ("decl->hasTrivialCopyConstructor() = %i\n", |
| 8200 | // cxx_record_decl->hasTrivialCopyConstructor()); |
| 8201 | // printf ("decl->hasTrivialCopyAssignment() = %i\n", |
| 8202 | // cxx_record_decl->hasTrivialCopyAssignment()); |
| 8203 | // printf ("decl->hasTrivialDestructor() = %i\n", |
| 8204 | // cxx_record_decl->hasTrivialDestructor()); |
| 8205 | return cxx_method_decl; |
| 8206 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8207 | |
Aleksandr Urakov | 7d2a74f | 2018-08-14 07:57:44 +0000 | [diff] [blame^] | 8208 | void ClangASTContext::AddMethodOverridesForCXXRecordType( |
| 8209 | lldb::opaque_compiler_type_t type) { |
| 8210 | if (auto *record = GetAsCXXRecordDecl(type)) |
| 8211 | for (auto *method : record->methods()) |
| 8212 | addOverridesForMethod(method); |
| 8213 | } |
| 8214 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8215 | #pragma mark C++ Base Classes |
| 8216 | |
| 8217 | clang::CXXBaseSpecifier * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8218 | ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type, |
| 8219 | AccessType access, bool is_virtual, |
| 8220 | bool base_of_class) { |
| 8221 | if (type) |
| 8222 | return new clang::CXXBaseSpecifier( |
| 8223 | clang::SourceRange(), is_virtual, base_of_class, |
| 8224 | ClangASTContext::ConvertAccessTypeToAccessSpecifier(access), |
| 8225 | getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)), |
| 8226 | clang::SourceLocation()); |
| 8227 | return nullptr; |
| 8228 | } |
| 8229 | |
| 8230 | void ClangASTContext::DeleteBaseClassSpecifiers( |
| 8231 | clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes) { |
| 8232 | for (unsigned i = 0; i < num_base_classes; ++i) { |
| 8233 | delete base_classes[i]; |
| 8234 | base_classes[i] = nullptr; |
| 8235 | } |
| 8236 | } |
| 8237 | |
| 8238 | bool ClangASTContext::SetBaseClassesForClassType( |
| 8239 | lldb::opaque_compiler_type_t type, |
| 8240 | clang::CXXBaseSpecifier const *const *base_classes, |
| 8241 | unsigned num_base_classes) { |
| 8242 | if (type) { |
| 8243 | clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type); |
| 8244 | if (cxx_record_decl) { |
| 8245 | cxx_record_decl->setBases(base_classes, num_base_classes); |
| 8246 | return true; |
| 8247 | } |
| 8248 | } |
| 8249 | return false; |
| 8250 | } |
| 8251 | |
| 8252 | bool ClangASTContext::SetObjCSuperClass( |
| 8253 | const CompilerType &type, const CompilerType &superclass_clang_type) { |
| 8254 | ClangASTContext *ast = |
| 8255 | llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem()); |
| 8256 | if (!ast) |
| 8257 | return false; |
| 8258 | clang::ASTContext *clang_ast = ast->getASTContext(); |
| 8259 | |
| 8260 | if (type && superclass_clang_type.IsValid() && |
| 8261 | superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) { |
| 8262 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 8263 | GetAsObjCInterfaceDecl(type); |
| 8264 | clang::ObjCInterfaceDecl *super_interface_decl = |
| 8265 | GetAsObjCInterfaceDecl(superclass_clang_type); |
| 8266 | if (class_interface_decl && super_interface_decl) { |
| 8267 | class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo( |
| 8268 | clang_ast->getObjCInterfaceType(super_interface_decl))); |
| 8269 | return true; |
| 8270 | } |
| 8271 | } |
| 8272 | return false; |
| 8273 | } |
| 8274 | |
| 8275 | bool ClangASTContext::AddObjCClassProperty( |
| 8276 | const CompilerType &type, const char *property_name, |
| 8277 | const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl, |
| 8278 | const char *property_setter_name, const char *property_getter_name, |
| 8279 | uint32_t property_attributes, ClangASTMetadata *metadata) { |
| 8280 | if (!type || !property_clang_type.IsValid() || property_name == nullptr || |
| 8281 | property_name[0] == '\0') |
| 8282 | return false; |
| 8283 | ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); |
| 8284 | if (!ast) |
| 8285 | return false; |
| 8286 | clang::ASTContext *clang_ast = ast->getASTContext(); |
| 8287 | |
| 8288 | clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type); |
| 8289 | |
| 8290 | if (class_interface_decl) { |
| 8291 | CompilerType property_clang_type_to_access; |
| 8292 | |
| 8293 | if (property_clang_type.IsValid()) |
| 8294 | property_clang_type_to_access = property_clang_type; |
| 8295 | else if (ivar_decl) |
| 8296 | property_clang_type_to_access = |
| 8297 | CompilerType(clang_ast, ivar_decl->getType()); |
| 8298 | |
| 8299 | if (class_interface_decl && property_clang_type_to_access.IsValid()) { |
| 8300 | clang::TypeSourceInfo *prop_type_source; |
| 8301 | if (ivar_decl) |
| 8302 | prop_type_source = |
| 8303 | clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType()); |
| 8304 | else |
| 8305 | prop_type_source = clang_ast->getTrivialTypeSourceInfo( |
| 8306 | ClangUtil::GetQualType(property_clang_type)); |
| 8307 | |
| 8308 | clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create( |
| 8309 | *clang_ast, class_interface_decl, |
| 8310 | clang::SourceLocation(), // Source Location |
| 8311 | &clang_ast->Idents.get(property_name), |
| 8312 | clang::SourceLocation(), // Source Location for AT |
| 8313 | clang::SourceLocation(), // Source location for ( |
| 8314 | ivar_decl ? ivar_decl->getType() |
| 8315 | : ClangUtil::GetQualType(property_clang_type), |
| 8316 | prop_type_source); |
| 8317 | |
| 8318 | if (property_decl) { |
| 8319 | if (metadata) |
| 8320 | ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata); |
| 8321 | |
| 8322 | class_interface_decl->addDecl(property_decl); |
| 8323 | |
| 8324 | clang::Selector setter_sel, getter_sel; |
| 8325 | |
| 8326 | if (property_setter_name != nullptr) { |
| 8327 | std::string property_setter_no_colon( |
| 8328 | property_setter_name, strlen(property_setter_name) - 1); |
| 8329 | clang::IdentifierInfo *setter_ident = |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 8330 | &clang_ast->Idents.get(property_setter_no_colon); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8331 | setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident); |
| 8332 | } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) { |
| 8333 | std::string setter_sel_string("set"); |
| 8334 | setter_sel_string.push_back(::toupper(property_name[0])); |
| 8335 | setter_sel_string.append(&property_name[1]); |
| 8336 | clang::IdentifierInfo *setter_ident = |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 8337 | &clang_ast->Idents.get(setter_sel_string); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8338 | setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident); |
| 8339 | } |
| 8340 | property_decl->setSetterName(setter_sel); |
| 8341 | property_decl->setPropertyAttributes( |
| 8342 | clang::ObjCPropertyDecl::OBJC_PR_setter); |
| 8343 | |
| 8344 | if (property_getter_name != nullptr) { |
| 8345 | clang::IdentifierInfo *getter_ident = |
| 8346 | &clang_ast->Idents.get(property_getter_name); |
| 8347 | getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident); |
| 8348 | } else { |
| 8349 | clang::IdentifierInfo *getter_ident = |
| 8350 | &clang_ast->Idents.get(property_name); |
| 8351 | getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident); |
| 8352 | } |
| 8353 | property_decl->setGetterName(getter_sel); |
| 8354 | property_decl->setPropertyAttributes( |
| 8355 | clang::ObjCPropertyDecl::OBJC_PR_getter); |
| 8356 | |
| 8357 | if (ivar_decl) |
| 8358 | property_decl->setPropertyIvarDecl(ivar_decl); |
| 8359 | |
| 8360 | if (property_attributes & DW_APPLE_PROPERTY_readonly) |
| 8361 | property_decl->setPropertyAttributes( |
| 8362 | clang::ObjCPropertyDecl::OBJC_PR_readonly); |
| 8363 | if (property_attributes & DW_APPLE_PROPERTY_readwrite) |
| 8364 | property_decl->setPropertyAttributes( |
| 8365 | clang::ObjCPropertyDecl::OBJC_PR_readwrite); |
| 8366 | if (property_attributes & DW_APPLE_PROPERTY_assign) |
| 8367 | property_decl->setPropertyAttributes( |
| 8368 | clang::ObjCPropertyDecl::OBJC_PR_assign); |
| 8369 | if (property_attributes & DW_APPLE_PROPERTY_retain) |
| 8370 | property_decl->setPropertyAttributes( |
| 8371 | clang::ObjCPropertyDecl::OBJC_PR_retain); |
| 8372 | if (property_attributes & DW_APPLE_PROPERTY_copy) |
| 8373 | property_decl->setPropertyAttributes( |
| 8374 | clang::ObjCPropertyDecl::OBJC_PR_copy); |
| 8375 | if (property_attributes & DW_APPLE_PROPERTY_nonatomic) |
| 8376 | property_decl->setPropertyAttributes( |
| 8377 | clang::ObjCPropertyDecl::OBJC_PR_nonatomic); |
| 8378 | if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability) |
| 8379 | property_decl->setPropertyAttributes( |
| 8380 | clang::ObjCPropertyDecl::OBJC_PR_nullability); |
| 8381 | if (property_attributes & |
| 8382 | clang::ObjCPropertyDecl::OBJC_PR_null_resettable) |
| 8383 | property_decl->setPropertyAttributes( |
| 8384 | clang::ObjCPropertyDecl::OBJC_PR_null_resettable); |
| 8385 | if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) |
| 8386 | property_decl->setPropertyAttributes( |
| 8387 | clang::ObjCPropertyDecl::OBJC_PR_class); |
| 8388 | |
| 8389 | const bool isInstance = |
| 8390 | (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0; |
| 8391 | |
| 8392 | if (!getter_sel.isNull() && |
| 8393 | !(isInstance |
| 8394 | ? class_interface_decl->lookupInstanceMethod(getter_sel) |
| 8395 | : class_interface_decl->lookupClassMethod(getter_sel))) { |
| 8396 | const bool isVariadic = false; |
| 8397 | const bool isSynthesized = false; |
| 8398 | const bool isImplicitlyDeclared = true; |
| 8399 | const bool isDefined = false; |
| 8400 | const clang::ObjCMethodDecl::ImplementationControl impControl = |
| 8401 | clang::ObjCMethodDecl::None; |
| 8402 | const bool HasRelatedResultType = false; |
| 8403 | |
| 8404 | clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create( |
| 8405 | *clang_ast, clang::SourceLocation(), clang::SourceLocation(), |
| 8406 | getter_sel, ClangUtil::GetQualType(property_clang_type_to_access), |
| 8407 | nullptr, class_interface_decl, isInstance, isVariadic, |
| 8408 | isSynthesized, isImplicitlyDeclared, isDefined, impControl, |
| 8409 | HasRelatedResultType); |
| 8410 | |
| 8411 | if (getter && metadata) |
| 8412 | ClangASTContext::SetMetadata(clang_ast, getter, *metadata); |
| 8413 | |
| 8414 | if (getter) { |
| 8415 | getter->setMethodParams(*clang_ast, |
| 8416 | llvm::ArrayRef<clang::ParmVarDecl *>(), |
| 8417 | llvm::ArrayRef<clang::SourceLocation>()); |
| 8418 | |
| 8419 | class_interface_decl->addDecl(getter); |
| 8420 | } |
| 8421 | } |
| 8422 | |
| 8423 | if (!setter_sel.isNull() && |
| 8424 | !(isInstance |
| 8425 | ? class_interface_decl->lookupInstanceMethod(setter_sel) |
| 8426 | : class_interface_decl->lookupClassMethod(setter_sel))) { |
| 8427 | clang::QualType result_type = clang_ast->VoidTy; |
| 8428 | const bool isVariadic = false; |
| 8429 | const bool isSynthesized = false; |
| 8430 | const bool isImplicitlyDeclared = true; |
| 8431 | const bool isDefined = false; |
| 8432 | const clang::ObjCMethodDecl::ImplementationControl impControl = |
| 8433 | clang::ObjCMethodDecl::None; |
| 8434 | const bool HasRelatedResultType = false; |
| 8435 | |
| 8436 | clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create( |
| 8437 | *clang_ast, clang::SourceLocation(), clang::SourceLocation(), |
| 8438 | setter_sel, result_type, nullptr, class_interface_decl, |
| 8439 | isInstance, isVariadic, isSynthesized, isImplicitlyDeclared, |
| 8440 | isDefined, impControl, HasRelatedResultType); |
| 8441 | |
| 8442 | if (setter && metadata) |
| 8443 | ClangASTContext::SetMetadata(clang_ast, setter, *metadata); |
| 8444 | |
| 8445 | llvm::SmallVector<clang::ParmVarDecl *, 1> params; |
| 8446 | |
| 8447 | params.push_back(clang::ParmVarDecl::Create( |
| 8448 | *clang_ast, setter, clang::SourceLocation(), |
| 8449 | clang::SourceLocation(), |
| 8450 | nullptr, // anonymous |
| 8451 | ClangUtil::GetQualType(property_clang_type_to_access), nullptr, |
| 8452 | clang::SC_Auto, nullptr)); |
| 8453 | |
| 8454 | if (setter) { |
| 8455 | setter->setMethodParams( |
| 8456 | *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params), |
| 8457 | llvm::ArrayRef<clang::SourceLocation>()); |
| 8458 | |
| 8459 | class_interface_decl->addDecl(setter); |
| 8460 | } |
| 8461 | } |
| 8462 | |
| 8463 | return true; |
| 8464 | } |
| 8465 | } |
| 8466 | } |
| 8467 | return false; |
| 8468 | } |
| 8469 | |
| 8470 | bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type, |
| 8471 | bool check_superclass) { |
| 8472 | clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type); |
| 8473 | if (class_interface_decl) |
| 8474 | return ObjCDeclHasIVars(class_interface_decl, check_superclass); |
| 8475 | return false; |
| 8476 | } |
| 8477 | |
| 8478 | clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType( |
| 8479 | const CompilerType &type, |
| 8480 | const char *name, // the full symbol name as seen in the symbol table |
| 8481 | // (lldb::opaque_compiler_type_t type, "-[NString |
| 8482 | // stringWithCString:]") |
| 8483 | const CompilerType &method_clang_type, lldb::AccessType access, |
| 8484 | bool is_artificial, bool is_variadic) { |
| 8485 | if (!type || !method_clang_type.IsValid()) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8486 | return nullptr; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8487 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8488 | clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type); |
| 8489 | |
| 8490 | if (class_interface_decl == nullptr) |
| 8491 | return nullptr; |
| 8492 | ClangASTContext *lldb_ast = |
| 8493 | llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); |
| 8494 | if (lldb_ast == nullptr) |
| 8495 | return nullptr; |
| 8496 | clang::ASTContext *ast = lldb_ast->getASTContext(); |
| 8497 | |
| 8498 | const char *selector_start = ::strchr(name, ' '); |
| 8499 | if (selector_start == nullptr) |
| 8500 | return nullptr; |
| 8501 | |
| 8502 | selector_start++; |
| 8503 | llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents; |
| 8504 | |
| 8505 | size_t len = 0; |
| 8506 | const char *start; |
| 8507 | // printf ("name = '%s'\n", name); |
| 8508 | |
| 8509 | unsigned num_selectors_with_args = 0; |
| 8510 | for (start = selector_start; start && *start != '\0' && *start != ']'; |
| 8511 | start += len) { |
| 8512 | len = ::strcspn(start, ":]"); |
| 8513 | bool has_arg = (start[len] == ':'); |
| 8514 | if (has_arg) |
| 8515 | ++num_selectors_with_args; |
| 8516 | selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len))); |
| 8517 | if (has_arg) |
| 8518 | len += 1; |
| 8519 | } |
| 8520 | |
| 8521 | if (selector_idents.size() == 0) |
| 8522 | return nullptr; |
| 8523 | |
| 8524 | clang::Selector method_selector = ast->Selectors.getSelector( |
| 8525 | num_selectors_with_args ? selector_idents.size() : 0, |
| 8526 | selector_idents.data()); |
| 8527 | |
| 8528 | clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type)); |
| 8529 | |
| 8530 | // Populate the method decl with parameter decls |
| 8531 | const clang::Type *method_type(method_qual_type.getTypePtr()); |
| 8532 | |
| 8533 | if (method_type == nullptr) |
| 8534 | return nullptr; |
| 8535 | |
| 8536 | const clang::FunctionProtoType *method_function_prototype( |
| 8537 | llvm::dyn_cast<clang::FunctionProtoType>(method_type)); |
| 8538 | |
| 8539 | if (!method_function_prototype) |
| 8540 | return nullptr; |
| 8541 | |
| 8542 | bool is_synthesized = false; |
| 8543 | bool is_defined = false; |
| 8544 | clang::ObjCMethodDecl::ImplementationControl imp_control = |
| 8545 | clang::ObjCMethodDecl::None; |
| 8546 | |
| 8547 | const unsigned num_args = method_function_prototype->getNumParams(); |
| 8548 | |
| 8549 | if (num_args != num_selectors_with_args) |
| 8550 | return nullptr; // some debug information is corrupt. We are not going to |
| 8551 | // deal with it. |
| 8552 | |
| 8553 | clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create( |
| 8554 | *ast, |
| 8555 | clang::SourceLocation(), // beginLoc, |
| 8556 | clang::SourceLocation(), // endLoc, |
| 8557 | method_selector, method_function_prototype->getReturnType(), |
| 8558 | nullptr, // TypeSourceInfo *ResultTInfo, |
| 8559 | ClangASTContext::GetASTContext(ast)->GetDeclContextForType( |
| 8560 | ClangUtil::GetQualType(type)), |
| 8561 | name[0] == '-', is_variadic, is_synthesized, |
| 8562 | true, // is_implicitly_declared; we force this to true because we don't |
| 8563 | // have source locations |
| 8564 | is_defined, imp_control, false /*has_related_result_type*/); |
| 8565 | |
| 8566 | if (objc_method_decl == nullptr) |
| 8567 | return nullptr; |
| 8568 | |
| 8569 | if (num_args > 0) { |
| 8570 | llvm::SmallVector<clang::ParmVarDecl *, 12> params; |
| 8571 | |
| 8572 | for (unsigned param_index = 0; param_index < num_args; ++param_index) { |
| 8573 | params.push_back(clang::ParmVarDecl::Create( |
| 8574 | *ast, objc_method_decl, clang::SourceLocation(), |
| 8575 | clang::SourceLocation(), |
| 8576 | nullptr, // anonymous |
| 8577 | method_function_prototype->getParamType(param_index), nullptr, |
| 8578 | clang::SC_Auto, nullptr)); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8579 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8580 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8581 | objc_method_decl->setMethodParams( |
| 8582 | *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params), |
| 8583 | llvm::ArrayRef<clang::SourceLocation>()); |
| 8584 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8585 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8586 | class_interface_decl->addDecl(objc_method_decl); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8587 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8588 | #ifdef LLDB_CONFIGURATION_DEBUG |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8589 | VerifyDecl(objc_method_decl); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8590 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8591 | |
| 8592 | return objc_method_decl; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8593 | } |
| 8594 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8595 | bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) { |
| 8596 | if (ClangUtil::IsClangType(type)) |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 8597 | return false; |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 8598 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8599 | clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type)); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 8600 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8601 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 8602 | switch (type_class) { |
| 8603 | case clang::Type::Record: { |
| 8604 | clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); |
| 8605 | if (cxx_record_decl) |
| 8606 | return cxx_record_decl->hasExternalLexicalStorage() || |
| 8607 | cxx_record_decl->hasExternalVisibleStorage(); |
| 8608 | } break; |
Enrico Granata | 36f51e4 | 2015-12-18 22:41:25 +0000 | [diff] [blame] | 8609 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8610 | case clang::Type::Enum: { |
| 8611 | clang::EnumDecl *enum_decl = |
| 8612 | llvm::cast<clang::EnumType>(qual_type)->getDecl(); |
| 8613 | if (enum_decl) |
| 8614 | return enum_decl->hasExternalLexicalStorage() || |
| 8615 | enum_decl->hasExternalVisibleStorage(); |
| 8616 | } break; |
| 8617 | |
| 8618 | case clang::Type::ObjCObject: |
| 8619 | case clang::Type::ObjCInterface: { |
| 8620 | const clang::ObjCObjectType *objc_class_type = |
| 8621 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 8622 | assert(objc_class_type); |
| 8623 | if (objc_class_type) { |
| 8624 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 8625 | objc_class_type->getInterface(); |
| 8626 | |
| 8627 | if (class_interface_decl) |
| 8628 | return class_interface_decl->hasExternalLexicalStorage() || |
| 8629 | class_interface_decl->hasExternalVisibleStorage(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8630 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8631 | } break; |
| 8632 | |
| 8633 | case clang::Type::Typedef: |
| 8634 | return GetHasExternalStorage(CompilerType( |
| 8635 | type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type) |
| 8636 | ->getDecl() |
| 8637 | ->getUnderlyingType() |
| 8638 | .getAsOpaquePtr())); |
| 8639 | |
| 8640 | case clang::Type::Auto: |
| 8641 | return GetHasExternalStorage(CompilerType( |
| 8642 | type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type) |
| 8643 | ->getDeducedType() |
| 8644 | .getAsOpaquePtr())); |
| 8645 | |
| 8646 | case clang::Type::Elaborated: |
| 8647 | return GetHasExternalStorage(CompilerType( |
| 8648 | type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type) |
| 8649 | ->getNamedType() |
| 8650 | .getAsOpaquePtr())); |
| 8651 | |
| 8652 | case clang::Type::Paren: |
| 8653 | return GetHasExternalStorage(CompilerType( |
| 8654 | type.GetTypeSystem(), |
| 8655 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr())); |
| 8656 | |
| 8657 | default: |
| 8658 | break; |
| 8659 | } |
| 8660 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8661 | } |
| 8662 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8663 | bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type, |
| 8664 | bool has_extern) { |
| 8665 | if (!type) |
| 8666 | return false; |
| 8667 | |
| 8668 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 8669 | |
| 8670 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 8671 | switch (type_class) { |
| 8672 | case clang::Type::Record: { |
| 8673 | clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); |
| 8674 | if (cxx_record_decl) { |
| 8675 | cxx_record_decl->setHasExternalLexicalStorage(has_extern); |
| 8676 | cxx_record_decl->setHasExternalVisibleStorage(has_extern); |
| 8677 | return true; |
| 8678 | } |
| 8679 | } break; |
| 8680 | |
| 8681 | case clang::Type::Enum: { |
| 8682 | clang::EnumDecl *enum_decl = |
| 8683 | llvm::cast<clang::EnumType>(qual_type)->getDecl(); |
| 8684 | if (enum_decl) { |
| 8685 | enum_decl->setHasExternalLexicalStorage(has_extern); |
| 8686 | enum_decl->setHasExternalVisibleStorage(has_extern); |
| 8687 | return true; |
| 8688 | } |
| 8689 | } break; |
| 8690 | |
| 8691 | case clang::Type::ObjCObject: |
| 8692 | case clang::Type::ObjCInterface: { |
| 8693 | const clang::ObjCObjectType *objc_class_type = |
| 8694 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 8695 | assert(objc_class_type); |
| 8696 | if (objc_class_type) { |
| 8697 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 8698 | objc_class_type->getInterface(); |
| 8699 | |
| 8700 | if (class_interface_decl) { |
| 8701 | class_interface_decl->setHasExternalLexicalStorage(has_extern); |
| 8702 | class_interface_decl->setHasExternalVisibleStorage(has_extern); |
| 8703 | return true; |
| 8704 | } |
| 8705 | } |
| 8706 | } break; |
| 8707 | |
| 8708 | case clang::Type::Typedef: |
| 8709 | return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type) |
| 8710 | ->getDecl() |
| 8711 | ->getUnderlyingType() |
| 8712 | .getAsOpaquePtr(), |
| 8713 | has_extern); |
| 8714 | |
| 8715 | case clang::Type::Auto: |
| 8716 | return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type) |
| 8717 | ->getDeducedType() |
| 8718 | .getAsOpaquePtr(), |
| 8719 | has_extern); |
| 8720 | |
| 8721 | case clang::Type::Elaborated: |
| 8722 | return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type) |
| 8723 | ->getNamedType() |
| 8724 | .getAsOpaquePtr(), |
| 8725 | has_extern); |
| 8726 | |
| 8727 | case clang::Type::Paren: |
| 8728 | return SetHasExternalStorage( |
| 8729 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 8730 | has_extern); |
| 8731 | |
| 8732 | default: |
| 8733 | break; |
| 8734 | } |
| 8735 | return false; |
| 8736 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8737 | |
| 8738 | #pragma mark TagDecl |
| 8739 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8740 | bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) { |
| 8741 | clang::QualType qual_type(ClangUtil::GetQualType(type)); |
| 8742 | if (!qual_type.isNull()) { |
| 8743 | const clang::TagType *tag_type = qual_type->getAs<clang::TagType>(); |
| 8744 | if (tag_type) { |
| 8745 | clang::TagDecl *tag_decl = tag_type->getDecl(); |
| 8746 | if (tag_decl) { |
| 8747 | tag_decl->startDefinition(); |
| 8748 | return true; |
| 8749 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8750 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8751 | |
| 8752 | const clang::ObjCObjectType *object_type = |
| 8753 | qual_type->getAs<clang::ObjCObjectType>(); |
| 8754 | if (object_type) { |
| 8755 | clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface(); |
| 8756 | if (interface_decl) { |
| 8757 | interface_decl->startDefinition(); |
| 8758 | return true; |
| 8759 | } |
| 8760 | } |
| 8761 | } |
| 8762 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8763 | } |
| 8764 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8765 | bool ClangASTContext::CompleteTagDeclarationDefinition( |
| 8766 | const CompilerType &type) { |
| 8767 | clang::QualType qual_type(ClangUtil::GetQualType(type)); |
| 8768 | if (!qual_type.isNull()) { |
| 8769 | // Make sure we use the same methodology as |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 8770 | // ClangASTContext::StartTagDeclarationDefinition() as to how we start/end |
| 8771 | // the definition. Previously we were calling |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8772 | const clang::TagType *tag_type = qual_type->getAs<clang::TagType>(); |
| 8773 | if (tag_type) { |
| 8774 | clang::TagDecl *tag_decl = tag_type->getDecl(); |
| 8775 | if (tag_decl) { |
| 8776 | clang::CXXRecordDecl *cxx_record_decl = |
| 8777 | llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl); |
| 8778 | |
| 8779 | if (cxx_record_decl) { |
| 8780 | if (!cxx_record_decl->isCompleteDefinition()) |
| 8781 | cxx_record_decl->completeDefinition(); |
| 8782 | cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true); |
| 8783 | cxx_record_decl->setHasExternalLexicalStorage(false); |
| 8784 | cxx_record_decl->setHasExternalVisibleStorage(false); |
| 8785 | return true; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8786 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8787 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8788 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8789 | |
| 8790 | const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>(); |
| 8791 | |
| 8792 | if (enutype) { |
| 8793 | clang::EnumDecl *enum_decl = enutype->getDecl(); |
| 8794 | |
| 8795 | if (enum_decl) { |
| 8796 | if (!enum_decl->isCompleteDefinition()) { |
| 8797 | ClangASTContext *lldb_ast = |
| 8798 | llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); |
| 8799 | if (lldb_ast == nullptr) |
| 8800 | return false; |
| 8801 | clang::ASTContext *ast = lldb_ast->getASTContext(); |
| 8802 | |
| 8803 | /// TODO This really needs to be fixed. |
| 8804 | |
| 8805 | QualType integer_type(enum_decl->getIntegerType()); |
| 8806 | if (!integer_type.isNull()) { |
| 8807 | unsigned NumPositiveBits = 1; |
| 8808 | unsigned NumNegativeBits = 0; |
| 8809 | |
| 8810 | clang::QualType promotion_qual_type; |
| 8811 | // If the enum integer type is less than an integer in bit width, |
| 8812 | // then we must promote it to an integer size. |
| 8813 | if (ast->getTypeSize(enum_decl->getIntegerType()) < |
| 8814 | ast->getTypeSize(ast->IntTy)) { |
| 8815 | if (enum_decl->getIntegerType()->isSignedIntegerType()) |
| 8816 | promotion_qual_type = ast->IntTy; |
| 8817 | else |
| 8818 | promotion_qual_type = ast->UnsignedIntTy; |
| 8819 | } else |
| 8820 | promotion_qual_type = enum_decl->getIntegerType(); |
| 8821 | |
| 8822 | enum_decl->completeDefinition(enum_decl->getIntegerType(), |
| 8823 | promotion_qual_type, NumPositiveBits, |
| 8824 | NumNegativeBits); |
| 8825 | } |
| 8826 | } |
| 8827 | return true; |
| 8828 | } |
| 8829 | } |
| 8830 | } |
| 8831 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8832 | } |
| 8833 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8834 | bool ClangASTContext::AddEnumerationValueToEnumerationType( |
| 8835 | lldb::opaque_compiler_type_t type, |
| 8836 | const CompilerType &enumerator_clang_type, const Declaration &decl, |
| 8837 | const char *name, int64_t enum_value, uint32_t enum_value_bit_size) { |
| 8838 | if (type && enumerator_clang_type.IsValid() && name && name[0]) { |
| 8839 | clang::QualType enum_qual_type(GetCanonicalQualType(type)); |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 8840 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8841 | bool is_signed = false; |
| 8842 | enumerator_clang_type.IsIntegerType(is_signed); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8843 | const clang::Type *clang_type = enum_qual_type.getTypePtr(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8844 | if (clang_type) { |
| 8845 | const clang::EnumType *enutype = |
| 8846 | llvm::dyn_cast<clang::EnumType>(clang_type); |
| 8847 | |
| 8848 | if (enutype) { |
| 8849 | llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed); |
| 8850 | enum_llvm_apsint = enum_value; |
| 8851 | clang::EnumConstantDecl *enumerator_decl = |
| 8852 | clang::EnumConstantDecl::Create( |
| 8853 | *getASTContext(), enutype->getDecl(), clang::SourceLocation(), |
| 8854 | name ? &getASTContext()->Idents.get(name) |
| 8855 | : nullptr, // Identifier |
| 8856 | ClangUtil::GetQualType(enumerator_clang_type), |
| 8857 | nullptr, enum_llvm_apsint); |
| 8858 | |
| 8859 | if (enumerator_decl) { |
| 8860 | enutype->getDecl()->addDecl(enumerator_decl); |
| 8861 | |
| 8862 | #ifdef LLDB_CONFIGURATION_DEBUG |
| 8863 | VerifyDecl(enumerator_decl); |
| 8864 | #endif |
| 8865 | |
| 8866 | return true; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8867 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8868 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8869 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8870 | } |
| 8871 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8872 | } |
| 8873 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 8874 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8875 | ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) { |
| 8876 | clang::QualType enum_qual_type(GetCanonicalQualType(type)); |
| 8877 | const clang::Type *clang_type = enum_qual_type.getTypePtr(); |
| 8878 | if (clang_type) { |
| 8879 | const clang::EnumType *enutype = |
| 8880 | llvm::dyn_cast<clang::EnumType>(clang_type); |
| 8881 | if (enutype) { |
| 8882 | clang::EnumDecl *enum_decl = enutype->getDecl(); |
| 8883 | if (enum_decl) |
| 8884 | return CompilerType(getASTContext(), enum_decl->getIntegerType()); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8885 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8886 | } |
| 8887 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8888 | } |
| 8889 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8890 | CompilerType |
| 8891 | ClangASTContext::CreateMemberPointerType(const CompilerType &type, |
| 8892 | const CompilerType &pointee_type) { |
| 8893 | if (type && pointee_type.IsValid() && |
| 8894 | type.GetTypeSystem() == pointee_type.GetTypeSystem()) { |
| 8895 | ClangASTContext *ast = |
| 8896 | llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); |
| 8897 | if (!ast) |
| 8898 | return CompilerType(); |
| 8899 | return CompilerType(ast->getASTContext(), |
| 8900 | ast->getASTContext()->getMemberPointerType( |
| 8901 | ClangUtil::GetQualType(pointee_type), |
| 8902 | ClangUtil::GetQualType(type).getTypePtr())); |
| 8903 | } |
| 8904 | return CompilerType(); |
| 8905 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8906 | |
| 8907 | size_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8908 | ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type, |
| 8909 | const char *s, uint8_t *dst, |
| 8910 | size_t dst_size) { |
| 8911 | if (type) { |
| 8912 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 8913 | uint32_t count = 0; |
| 8914 | bool is_complex = false; |
| 8915 | if (IsFloatingPointType(type, count, is_complex)) { |
| 8916 | // TODO: handle complex and vector types |
| 8917 | if (count != 1) |
| 8918 | return false; |
| 8919 | |
| 8920 | llvm::StringRef s_sref(s); |
| 8921 | llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type), |
| 8922 | s_sref); |
| 8923 | |
| 8924 | const uint64_t bit_size = getASTContext()->getTypeSize(qual_type); |
| 8925 | const uint64_t byte_size = bit_size / 8; |
| 8926 | if (dst_size >= byte_size) { |
| 8927 | Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc( |
| 8928 | llvm::NextPowerOf2(byte_size) * 8); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 8929 | lldb_private::Status get_data_error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8930 | if (scalar.GetAsMemoryData(dst, byte_size, |
| 8931 | lldb_private::endian::InlHostByteOrder(), |
| 8932 | get_data_error)) |
| 8933 | return byte_size; |
| 8934 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8935 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8936 | } |
| 8937 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8938 | } |
| 8939 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8940 | //---------------------------------------------------------------------- |
| 8941 | // Dumping types |
| 8942 | //---------------------------------------------------------------------- |
| 8943 | #define DEPTH_INCREMENT 2 |
| 8944 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8945 | void ClangASTContext::DumpValue( |
| 8946 | lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s, |
Zachary Turner | 29cb868 | 2017-03-03 20:57:05 +0000 | [diff] [blame] | 8947 | lldb::Format format, const DataExtractor &data, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8948 | lldb::offset_t data_byte_offset, size_t data_byte_size, |
| 8949 | uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types, |
| 8950 | bool show_summary, bool verbose, uint32_t depth) { |
| 8951 | if (!type) |
| 8952 | return; |
| 8953 | |
| 8954 | clang::QualType qual_type(GetQualType(type)); |
| 8955 | switch (qual_type->getTypeClass()) { |
| 8956 | case clang::Type::Record: |
| 8957 | if (GetCompleteType(type)) { |
| 8958 | const clang::RecordType *record_type = |
| 8959 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 8960 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 8961 | assert(record_decl); |
| 8962 | uint32_t field_bit_offset = 0; |
| 8963 | uint32_t field_byte_offset = 0; |
| 8964 | const clang::ASTRecordLayout &record_layout = |
| 8965 | getASTContext()->getASTRecordLayout(record_decl); |
| 8966 | uint32_t child_idx = 0; |
| 8967 | |
| 8968 | const clang::CXXRecordDecl *cxx_record_decl = |
| 8969 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 8970 | if (cxx_record_decl) { |
| 8971 | // We might have base classes to print out first |
| 8972 | clang::CXXRecordDecl::base_class_const_iterator base_class, |
| 8973 | base_class_end; |
| 8974 | for (base_class = cxx_record_decl->bases_begin(), |
| 8975 | base_class_end = cxx_record_decl->bases_end(); |
| 8976 | base_class != base_class_end; ++base_class) { |
| 8977 | const clang::CXXRecordDecl *base_class_decl = |
| 8978 | llvm::cast<clang::CXXRecordDecl>( |
| 8979 | base_class->getType()->getAs<clang::RecordType>()->getDecl()); |
| 8980 | |
| 8981 | // Skip empty base classes |
| 8982 | if (verbose == false && |
| 8983 | ClangASTContext::RecordHasFields(base_class_decl) == false) |
| 8984 | continue; |
| 8985 | |
| 8986 | if (base_class->isVirtual()) |
| 8987 | field_bit_offset = |
| 8988 | record_layout.getVBaseClassOffset(base_class_decl) |
| 8989 | .getQuantity() * |
| 8990 | 8; |
| 8991 | else |
| 8992 | field_bit_offset = record_layout.getBaseClassOffset(base_class_decl) |
| 8993 | .getQuantity() * |
| 8994 | 8; |
| 8995 | field_byte_offset = field_bit_offset / 8; |
| 8996 | assert(field_bit_offset % 8 == 0); |
| 8997 | if (child_idx == 0) |
| 8998 | s->PutChar('{'); |
| 8999 | else |
| 9000 | s->PutChar(','); |
| 9001 | |
| 9002 | clang::QualType base_class_qual_type = base_class->getType(); |
| 9003 | std::string base_class_type_name(base_class_qual_type.getAsString()); |
| 9004 | |
| 9005 | // Indent and print the base class type name |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 9006 | s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT), |
| 9007 | base_class_type_name); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9008 | |
| 9009 | clang::TypeInfo base_class_type_info = |
| 9010 | getASTContext()->getTypeInfo(base_class_qual_type); |
| 9011 | |
| 9012 | // Dump the value of the member |
| 9013 | CompilerType base_clang_type(getASTContext(), base_class_qual_type); |
| 9014 | base_clang_type.DumpValue( |
| 9015 | exe_ctx, |
| 9016 | s, // Stream to dump to |
| 9017 | base_clang_type |
| 9018 | .GetFormat(), // The format with which to display the member |
| 9019 | data, // Data buffer containing all bytes for this type |
| 9020 | data_byte_offset + field_byte_offset, // Offset into "data" where |
| 9021 | // to grab value from |
| 9022 | base_class_type_info.Width / 8, // Size of this type in bytes |
| 9023 | 0, // Bitfield bit size |
| 9024 | 0, // Bitfield bit offset |
| 9025 | show_types, // Boolean indicating if we should show the variable |
| 9026 | // types |
| 9027 | show_summary, // Boolean indicating if we should show a summary |
| 9028 | // for the current type |
| 9029 | verbose, // Verbose output? |
| 9030 | depth + DEPTH_INCREMENT); // Scope depth for any types that have |
| 9031 | // children |
| 9032 | |
| 9033 | ++child_idx; |
| 9034 | } |
| 9035 | } |
| 9036 | uint32_t field_idx = 0; |
| 9037 | clang::RecordDecl::field_iterator field, field_end; |
| 9038 | for (field = record_decl->field_begin(), |
| 9039 | field_end = record_decl->field_end(); |
| 9040 | field != field_end; ++field, ++field_idx, ++child_idx) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 9041 | // Print the starting squiggly bracket (if this is the first member) or |
| 9042 | // comma (for member 2 and beyond) for the struct/union/class member. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9043 | if (child_idx == 0) |
| 9044 | s->PutChar('{'); |
| 9045 | else |
| 9046 | s->PutChar(','); |
| 9047 | |
| 9048 | // Indent |
| 9049 | s->Printf("\n%*s", depth + DEPTH_INCREMENT, ""); |
| 9050 | |
| 9051 | clang::QualType field_type = field->getType(); |
| 9052 | // Print the member type if requested |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 9053 | // Figure out the type byte size (field_type_info.first) and alignment |
| 9054 | // (field_type_info.second) from the AST context. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9055 | clang::TypeInfo field_type_info = |
| 9056 | getASTContext()->getTypeInfo(field_type); |
| 9057 | assert(field_idx < record_layout.getFieldCount()); |
| 9058 | // Figure out the field offset within the current struct/union/class |
| 9059 | // type |
| 9060 | field_bit_offset = record_layout.getFieldOffset(field_idx); |
| 9061 | field_byte_offset = field_bit_offset / 8; |
| 9062 | uint32_t field_bitfield_bit_size = 0; |
| 9063 | uint32_t field_bitfield_bit_offset = 0; |
| 9064 | if (ClangASTContext::FieldIsBitfield(getASTContext(), *field, |
| 9065 | field_bitfield_bit_size)) |
| 9066 | field_bitfield_bit_offset = field_bit_offset % 8; |
| 9067 | |
| 9068 | if (show_types) { |
| 9069 | std::string field_type_name(field_type.getAsString()); |
| 9070 | if (field_bitfield_bit_size > 0) |
| 9071 | s->Printf("(%s:%u) ", field_type_name.c_str(), |
| 9072 | field_bitfield_bit_size); |
| 9073 | else |
| 9074 | s->Printf("(%s) ", field_type_name.c_str()); |
| 9075 | } |
| 9076 | // Print the member name and equal sign |
| 9077 | s->Printf("%s = ", field->getNameAsString().c_str()); |
| 9078 | |
| 9079 | // Dump the value of the member |
| 9080 | CompilerType field_clang_type(getASTContext(), field_type); |
| 9081 | field_clang_type.DumpValue( |
| 9082 | exe_ctx, |
| 9083 | s, // Stream to dump to |
| 9084 | field_clang_type |
| 9085 | .GetFormat(), // The format with which to display the member |
| 9086 | data, // Data buffer containing all bytes for this type |
| 9087 | data_byte_offset + field_byte_offset, // Offset into "data" where to |
| 9088 | // grab value from |
| 9089 | field_type_info.Width / 8, // Size of this type in bytes |
| 9090 | field_bitfield_bit_size, // Bitfield bit size |
| 9091 | field_bitfield_bit_offset, // Bitfield bit offset |
| 9092 | show_types, // Boolean indicating if we should show the variable |
| 9093 | // types |
| 9094 | show_summary, // Boolean indicating if we should show a summary for |
| 9095 | // the current type |
| 9096 | verbose, // Verbose output? |
| 9097 | depth + DEPTH_INCREMENT); // Scope depth for any types that have |
| 9098 | // children |
| 9099 | } |
| 9100 | |
| 9101 | // Indent the trailing squiggly bracket |
| 9102 | if (child_idx > 0) |
| 9103 | s->Printf("\n%*s}", depth, ""); |
| 9104 | } |
| 9105 | return; |
| 9106 | |
| 9107 | case clang::Type::Enum: |
| 9108 | if (GetCompleteType(type)) { |
| 9109 | const clang::EnumType *enutype = |
| 9110 | llvm::cast<clang::EnumType>(qual_type.getTypePtr()); |
| 9111 | const clang::EnumDecl *enum_decl = enutype->getDecl(); |
| 9112 | assert(enum_decl); |
| 9113 | clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos; |
| 9114 | lldb::offset_t offset = data_byte_offset; |
| 9115 | const int64_t enum_value = data.GetMaxU64Bitfield( |
| 9116 | &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset); |
| 9117 | for (enum_pos = enum_decl->enumerator_begin(), |
| 9118 | enum_end_pos = enum_decl->enumerator_end(); |
| 9119 | enum_pos != enum_end_pos; ++enum_pos) { |
| 9120 | if (enum_pos->getInitVal() == enum_value) { |
| 9121 | s->Printf("%s", enum_pos->getNameAsString().c_str()); |
| 9122 | return; |
| 9123 | } |
| 9124 | } |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 9125 | // If we have gotten here we didn't get find the enumerator in the enum |
| 9126 | // decl, so just print the integer. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9127 | s->Printf("%" PRIi64, enum_value); |
| 9128 | } |
| 9129 | return; |
| 9130 | |
| 9131 | case clang::Type::ConstantArray: { |
| 9132 | const clang::ConstantArrayType *array = |
| 9133 | llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr()); |
| 9134 | bool is_array_of_characters = false; |
| 9135 | clang::QualType element_qual_type = array->getElementType(); |
| 9136 | |
| 9137 | const clang::Type *canonical_type = |
| 9138 | element_qual_type->getCanonicalTypeInternal().getTypePtr(); |
| 9139 | if (canonical_type) |
| 9140 | is_array_of_characters = canonical_type->isCharType(); |
| 9141 | |
| 9142 | const uint64_t element_count = array->getSize().getLimitedValue(); |
| 9143 | |
| 9144 | clang::TypeInfo field_type_info = |
| 9145 | getASTContext()->getTypeInfo(element_qual_type); |
| 9146 | |
| 9147 | uint32_t element_idx = 0; |
| 9148 | uint32_t element_offset = 0; |
| 9149 | uint64_t element_byte_size = field_type_info.Width / 8; |
| 9150 | uint32_t element_stride = element_byte_size; |
| 9151 | |
| 9152 | if (is_array_of_characters) { |
| 9153 | s->PutChar('"'); |
Zachary Turner | 29cb868 | 2017-03-03 20:57:05 +0000 | [diff] [blame] | 9154 | DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar, |
| 9155 | element_byte_size, element_count, UINT32_MAX, |
| 9156 | LLDB_INVALID_ADDRESS, 0, 0); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9157 | s->PutChar('"'); |
| 9158 | return; |
| 9159 | } else { |
| 9160 | CompilerType element_clang_type(getASTContext(), element_qual_type); |
| 9161 | lldb::Format element_format = element_clang_type.GetFormat(); |
| 9162 | |
| 9163 | for (element_idx = 0; element_idx < element_count; ++element_idx) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 9164 | // Print the starting squiggly bracket (if this is the first member) or |
| 9165 | // comman (for member 2 and beyong) for the struct/union/class member. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9166 | if (element_idx == 0) |
| 9167 | s->PutChar('{'); |
| 9168 | else |
| 9169 | s->PutChar(','); |
| 9170 | |
| 9171 | // Indent and print the index |
| 9172 | s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx); |
| 9173 | |
| 9174 | // Figure out the field offset within the current struct/union/class |
| 9175 | // type |
| 9176 | element_offset = element_idx * element_stride; |
| 9177 | |
| 9178 | // Dump the value of the member |
| 9179 | element_clang_type.DumpValue( |
| 9180 | exe_ctx, |
| 9181 | s, // Stream to dump to |
| 9182 | element_format, // The format with which to display the element |
| 9183 | data, // Data buffer containing all bytes for this type |
| 9184 | data_byte_offset + |
| 9185 | element_offset, // Offset into "data" where to grab value from |
| 9186 | element_byte_size, // Size of this type in bytes |
| 9187 | 0, // Bitfield bit size |
| 9188 | 0, // Bitfield bit offset |
| 9189 | show_types, // Boolean indicating if we should show the variable |
| 9190 | // types |
| 9191 | show_summary, // Boolean indicating if we should show a summary for |
| 9192 | // the current type |
| 9193 | verbose, // Verbose output? |
| 9194 | depth + DEPTH_INCREMENT); // Scope depth for any types that have |
| 9195 | // children |
| 9196 | } |
| 9197 | |
| 9198 | // Indent the trailing squiggly bracket |
| 9199 | if (element_idx > 0) |
| 9200 | s->Printf("\n%*s}", depth, ""); |
| 9201 | } |
| 9202 | } |
| 9203 | return; |
| 9204 | |
| 9205 | case clang::Type::Typedef: { |
| 9206 | clang::QualType typedef_qual_type = |
| 9207 | llvm::cast<clang::TypedefType>(qual_type) |
| 9208 | ->getDecl() |
| 9209 | ->getUnderlyingType(); |
| 9210 | |
| 9211 | CompilerType typedef_clang_type(getASTContext(), typedef_qual_type); |
| 9212 | lldb::Format typedef_format = typedef_clang_type.GetFormat(); |
| 9213 | clang::TypeInfo typedef_type_info = |
| 9214 | getASTContext()->getTypeInfo(typedef_qual_type); |
| 9215 | uint64_t typedef_byte_size = typedef_type_info.Width / 8; |
| 9216 | |
| 9217 | return typedef_clang_type.DumpValue( |
| 9218 | exe_ctx, |
| 9219 | s, // Stream to dump to |
| 9220 | typedef_format, // The format with which to display the element |
| 9221 | data, // Data buffer containing all bytes for this type |
| 9222 | data_byte_offset, // Offset into "data" where to grab value from |
| 9223 | typedef_byte_size, // Size of this type in bytes |
| 9224 | bitfield_bit_size, // Bitfield bit size |
| 9225 | bitfield_bit_offset, // Bitfield bit offset |
| 9226 | show_types, // Boolean indicating if we should show the variable types |
| 9227 | show_summary, // Boolean indicating if we should show a summary for the |
| 9228 | // current type |
| 9229 | verbose, // Verbose output? |
| 9230 | depth); // Scope depth for any types that have children |
| 9231 | } break; |
| 9232 | |
| 9233 | case clang::Type::Auto: { |
| 9234 | clang::QualType elaborated_qual_type = |
| 9235 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType(); |
| 9236 | CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type); |
| 9237 | lldb::Format elaborated_format = elaborated_clang_type.GetFormat(); |
| 9238 | clang::TypeInfo elaborated_type_info = |
| 9239 | getASTContext()->getTypeInfo(elaborated_qual_type); |
| 9240 | uint64_t elaborated_byte_size = elaborated_type_info.Width / 8; |
| 9241 | |
| 9242 | return elaborated_clang_type.DumpValue( |
| 9243 | exe_ctx, |
| 9244 | s, // Stream to dump to |
| 9245 | elaborated_format, // The format with which to display the element |
| 9246 | data, // Data buffer containing all bytes for this type |
| 9247 | data_byte_offset, // Offset into "data" where to grab value from |
| 9248 | elaborated_byte_size, // Size of this type in bytes |
| 9249 | bitfield_bit_size, // Bitfield bit size |
| 9250 | bitfield_bit_offset, // Bitfield bit offset |
| 9251 | show_types, // Boolean indicating if we should show the variable types |
| 9252 | show_summary, // Boolean indicating if we should show a summary for the |
| 9253 | // current type |
| 9254 | verbose, // Verbose output? |
| 9255 | depth); // Scope depth for any types that have children |
| 9256 | } break; |
| 9257 | |
| 9258 | case clang::Type::Elaborated: { |
| 9259 | clang::QualType elaborated_qual_type = |
| 9260 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(); |
| 9261 | CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type); |
| 9262 | lldb::Format elaborated_format = elaborated_clang_type.GetFormat(); |
| 9263 | clang::TypeInfo elaborated_type_info = |
| 9264 | getASTContext()->getTypeInfo(elaborated_qual_type); |
| 9265 | uint64_t elaborated_byte_size = elaborated_type_info.Width / 8; |
| 9266 | |
| 9267 | return elaborated_clang_type.DumpValue( |
| 9268 | exe_ctx, |
| 9269 | s, // Stream to dump to |
| 9270 | elaborated_format, // The format with which to display the element |
| 9271 | data, // Data buffer containing all bytes for this type |
| 9272 | data_byte_offset, // Offset into "data" where to grab value from |
| 9273 | elaborated_byte_size, // Size of this type in bytes |
| 9274 | bitfield_bit_size, // Bitfield bit size |
| 9275 | bitfield_bit_offset, // Bitfield bit offset |
| 9276 | show_types, // Boolean indicating if we should show the variable types |
| 9277 | show_summary, // Boolean indicating if we should show a summary for the |
| 9278 | // current type |
| 9279 | verbose, // Verbose output? |
| 9280 | depth); // Scope depth for any types that have children |
| 9281 | } break; |
| 9282 | |
| 9283 | case clang::Type::Paren: { |
| 9284 | clang::QualType desugar_qual_type = |
| 9285 | llvm::cast<clang::ParenType>(qual_type)->desugar(); |
| 9286 | CompilerType desugar_clang_type(getASTContext(), desugar_qual_type); |
| 9287 | |
| 9288 | lldb::Format desugar_format = desugar_clang_type.GetFormat(); |
| 9289 | clang::TypeInfo desugar_type_info = |
| 9290 | getASTContext()->getTypeInfo(desugar_qual_type); |
| 9291 | uint64_t desugar_byte_size = desugar_type_info.Width / 8; |
| 9292 | |
| 9293 | return desugar_clang_type.DumpValue( |
| 9294 | exe_ctx, |
| 9295 | s, // Stream to dump to |
| 9296 | desugar_format, // The format with which to display the element |
| 9297 | data, // Data buffer containing all bytes for this type |
| 9298 | data_byte_offset, // Offset into "data" where to grab value from |
| 9299 | desugar_byte_size, // Size of this type in bytes |
| 9300 | bitfield_bit_size, // Bitfield bit size |
| 9301 | bitfield_bit_offset, // Bitfield bit offset |
| 9302 | show_types, // Boolean indicating if we should show the variable types |
| 9303 | show_summary, // Boolean indicating if we should show a summary for the |
| 9304 | // current type |
| 9305 | verbose, // Verbose output? |
| 9306 | depth); // Scope depth for any types that have children |
| 9307 | } break; |
| 9308 | |
| 9309 | default: |
| 9310 | // We are down to a scalar type that we just need to display. |
Zachary Turner | 29cb868 | 2017-03-03 20:57:05 +0000 | [diff] [blame] | 9311 | DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1, |
| 9312 | UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size, |
| 9313 | bitfield_bit_offset); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9314 | |
| 9315 | if (show_summary) |
| 9316 | DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size); |
| 9317 | break; |
| 9318 | } |
| 9319 | } |
| 9320 | |
| 9321 | bool ClangASTContext::DumpTypeValue( |
| 9322 | lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format, |
Zachary Turner | 29cb868 | 2017-03-03 20:57:05 +0000 | [diff] [blame] | 9323 | const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size, |
| 9324 | uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9325 | ExecutionContextScope *exe_scope) { |
| 9326 | if (!type) |
| 9327 | return false; |
| 9328 | if (IsAggregateType(type)) { |
| 9329 | return false; |
| 9330 | } else { |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9331 | clang::QualType qual_type(GetQualType(type)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9332 | |
| 9333 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 9334 | switch (type_class) { |
| 9335 | case clang::Type::Typedef: { |
| 9336 | clang::QualType typedef_qual_type = |
| 9337 | llvm::cast<clang::TypedefType>(qual_type) |
| 9338 | ->getDecl() |
| 9339 | ->getUnderlyingType(); |
| 9340 | CompilerType typedef_clang_type(getASTContext(), typedef_qual_type); |
| 9341 | if (format == eFormatDefault) |
| 9342 | format = typedef_clang_type.GetFormat(); |
| 9343 | clang::TypeInfo typedef_type_info = |
| 9344 | getASTContext()->getTypeInfo(typedef_qual_type); |
| 9345 | uint64_t typedef_byte_size = typedef_type_info.Width / 8; |
| 9346 | |
| 9347 | return typedef_clang_type.DumpTypeValue( |
| 9348 | s, |
| 9349 | format, // The format with which to display the element |
| 9350 | data, // Data buffer containing all bytes for this type |
| 9351 | byte_offset, // Offset into "data" where to grab value from |
| 9352 | typedef_byte_size, // Size of this type in bytes |
| 9353 | bitfield_bit_size, // Size in bits of a bitfield value, if zero don't |
| 9354 | // treat as a bitfield |
| 9355 | bitfield_bit_offset, // Offset in bits of a bitfield value if |
| 9356 | // bitfield_bit_size != 0 |
| 9357 | exe_scope); |
| 9358 | } break; |
| 9359 | |
| 9360 | case clang::Type::Enum: |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 9361 | // If our format is enum or default, show the enumeration value as its |
| 9362 | // enumeration string value, else just display it as requested. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9363 | if ((format == eFormatEnum || format == eFormatDefault) && |
| 9364 | GetCompleteType(type)) { |
| 9365 | const clang::EnumType *enutype = |
| 9366 | llvm::cast<clang::EnumType>(qual_type.getTypePtr()); |
| 9367 | const clang::EnumDecl *enum_decl = enutype->getDecl(); |
| 9368 | assert(enum_decl); |
| 9369 | clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos; |
| 9370 | const bool is_signed = qual_type->isSignedIntegerOrEnumerationType(); |
| 9371 | lldb::offset_t offset = byte_offset; |
| 9372 | if (is_signed) { |
| 9373 | const int64_t enum_svalue = data.GetMaxS64Bitfield( |
| 9374 | &offset, byte_size, bitfield_bit_size, bitfield_bit_offset); |
| 9375 | for (enum_pos = enum_decl->enumerator_begin(), |
| 9376 | enum_end_pos = enum_decl->enumerator_end(); |
| 9377 | enum_pos != enum_end_pos; ++enum_pos) { |
| 9378 | if (enum_pos->getInitVal().getSExtValue() == enum_svalue) { |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 9379 | s->PutCString(enum_pos->getNameAsString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9380 | return true; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9381 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9382 | } |
| 9383 | // If we have gotten here we didn't get find the enumerator in the |
| 9384 | // enum decl, so just print the integer. |
| 9385 | s->Printf("%" PRIi64, enum_svalue); |
| 9386 | } else { |
| 9387 | const uint64_t enum_uvalue = data.GetMaxU64Bitfield( |
| 9388 | &offset, byte_size, bitfield_bit_size, bitfield_bit_offset); |
| 9389 | for (enum_pos = enum_decl->enumerator_begin(), |
| 9390 | enum_end_pos = enum_decl->enumerator_end(); |
| 9391 | enum_pos != enum_end_pos; ++enum_pos) { |
| 9392 | if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) { |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 9393 | s->PutCString(enum_pos->getNameAsString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9394 | return true; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9395 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9396 | } |
| 9397 | // If we have gotten here we didn't get find the enumerator in the |
| 9398 | // enum decl, so just print the integer. |
| 9399 | s->Printf("%" PRIu64, enum_uvalue); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9400 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9401 | return true; |
| 9402 | } |
| 9403 | // format was not enum, just fall through and dump the value as |
| 9404 | // requested.... |
| 9405 | LLVM_FALLTHROUGH; |
| 9406 | |
| 9407 | default: |
| 9408 | // We are down to a scalar type that we just need to display. |
| 9409 | { |
| 9410 | uint32_t item_count = 1; |
| 9411 | // A few formats, we might need to modify our size and count for |
| 9412 | // depending |
| 9413 | // on how we are trying to display the value... |
| 9414 | switch (format) { |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9415 | default: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9416 | case eFormatBoolean: |
| 9417 | case eFormatBinary: |
| 9418 | case eFormatComplex: |
| 9419 | case eFormatCString: // NULL terminated C strings |
| 9420 | case eFormatDecimal: |
| 9421 | case eFormatEnum: |
| 9422 | case eFormatHex: |
| 9423 | case eFormatHexUppercase: |
| 9424 | case eFormatFloat: |
| 9425 | case eFormatOctal: |
| 9426 | case eFormatOSType: |
| 9427 | case eFormatUnsigned: |
| 9428 | case eFormatPointer: |
| 9429 | case eFormatVectorOfChar: |
| 9430 | case eFormatVectorOfSInt8: |
| 9431 | case eFormatVectorOfUInt8: |
| 9432 | case eFormatVectorOfSInt16: |
| 9433 | case eFormatVectorOfUInt16: |
| 9434 | case eFormatVectorOfSInt32: |
| 9435 | case eFormatVectorOfUInt32: |
| 9436 | case eFormatVectorOfSInt64: |
| 9437 | case eFormatVectorOfUInt64: |
| 9438 | case eFormatVectorOfFloat32: |
| 9439 | case eFormatVectorOfFloat64: |
| 9440 | case eFormatVectorOfUInt128: |
| 9441 | break; |
| 9442 | |
| 9443 | case eFormatChar: |
| 9444 | case eFormatCharPrintable: |
| 9445 | case eFormatCharArray: |
| 9446 | case eFormatBytes: |
| 9447 | case eFormatBytesWithASCII: |
| 9448 | item_count = byte_size; |
| 9449 | byte_size = 1; |
| 9450 | break; |
| 9451 | |
| 9452 | case eFormatUnicode16: |
| 9453 | item_count = byte_size / 2; |
| 9454 | byte_size = 2; |
| 9455 | break; |
| 9456 | |
| 9457 | case eFormatUnicode32: |
| 9458 | item_count = byte_size / 4; |
| 9459 | byte_size = 4; |
| 9460 | break; |
| 9461 | } |
Zachary Turner | 29cb868 | 2017-03-03 20:57:05 +0000 | [diff] [blame] | 9462 | return DumpDataExtractor(data, s, byte_offset, format, byte_size, |
| 9463 | item_count, UINT32_MAX, LLDB_INVALID_ADDRESS, |
| 9464 | bitfield_bit_size, bitfield_bit_offset, |
| 9465 | exe_scope); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9466 | } |
| 9467 | break; |
| 9468 | } |
| 9469 | } |
| 9470 | return 0; |
| 9471 | } |
| 9472 | |
| 9473 | void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type, |
| 9474 | ExecutionContext *exe_ctx, Stream *s, |
| 9475 | const lldb_private::DataExtractor &data, |
| 9476 | lldb::offset_t data_byte_offset, |
| 9477 | size_t data_byte_size) { |
| 9478 | uint32_t length = 0; |
| 9479 | if (IsCStringType(type, length)) { |
| 9480 | if (exe_ctx) { |
| 9481 | Process *process = exe_ctx->GetProcessPtr(); |
| 9482 | if (process) { |
| 9483 | lldb::offset_t offset = data_byte_offset; |
| 9484 | lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size); |
| 9485 | std::vector<uint8_t> buf; |
| 9486 | if (length > 0) |
| 9487 | buf.resize(length); |
| 9488 | else |
| 9489 | buf.resize(256); |
| 9490 | |
Zachary Turner | 29cb868 | 2017-03-03 20:57:05 +0000 | [diff] [blame] | 9491 | DataExtractor cstr_data(&buf.front(), buf.size(), |
| 9492 | process->GetByteOrder(), 4); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9493 | buf.back() = '\0'; |
| 9494 | size_t bytes_read; |
| 9495 | size_t total_cstr_len = 0; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 9496 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9497 | while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(), |
| 9498 | buf.size(), error)) > 0) { |
| 9499 | const size_t len = strlen((const char *)&buf.front()); |
| 9500 | if (len == 0) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9501 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9502 | if (total_cstr_len == 0) |
| 9503 | s->PutCString(" \""); |
Zachary Turner | 29cb868 | 2017-03-03 20:57:05 +0000 | [diff] [blame] | 9504 | DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len, |
| 9505 | UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9506 | total_cstr_len += len; |
| 9507 | if (len < buf.size()) |
| 9508 | break; |
| 9509 | pointer_address += total_cstr_len; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9510 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9511 | if (total_cstr_len > 0) |
| 9512 | s->PutChar('"'); |
| 9513 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9514 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9515 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9516 | } |
| 9517 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9518 | void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) { |
| 9519 | StreamFile s(stdout, false); |
| 9520 | DumpTypeDescription(type, &s); |
| 9521 | ClangASTMetadata *metadata = |
| 9522 | ClangASTContext::GetMetadata(getASTContext(), type); |
| 9523 | if (metadata) { |
| 9524 | metadata->Dump(&s); |
| 9525 | } |
| 9526 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9527 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9528 | void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type, |
| 9529 | Stream *s) { |
| 9530 | if (type) { |
| 9531 | clang::QualType qual_type(GetQualType(type)); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9532 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9533 | llvm::SmallVector<char, 1024> buf; |
| 9534 | llvm::raw_svector_ostream llvm_ostrm(buf); |
| 9535 | |
| 9536 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 9537 | switch (type_class) { |
| 9538 | case clang::Type::ObjCObject: |
| 9539 | case clang::Type::ObjCInterface: { |
| 9540 | GetCompleteType(type); |
| 9541 | |
| 9542 | const clang::ObjCObjectType *objc_class_type = |
| 9543 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 9544 | assert(objc_class_type); |
| 9545 | if (objc_class_type) { |
| 9546 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 9547 | objc_class_type->getInterface(); |
| 9548 | if (class_interface_decl) { |
| 9549 | clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy(); |
| 9550 | class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel()); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9551 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9552 | } |
| 9553 | } break; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9554 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9555 | case clang::Type::Typedef: { |
| 9556 | const clang::TypedefType *typedef_type = |
| 9557 | qual_type->getAs<clang::TypedefType>(); |
| 9558 | if (typedef_type) { |
| 9559 | const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl(); |
| 9560 | std::string clang_typedef_name( |
| 9561 | typedef_decl->getQualifiedNameAsString()); |
| 9562 | if (!clang_typedef_name.empty()) { |
| 9563 | s->PutCString("typedef "); |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 9564 | s->PutCString(clang_typedef_name); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9565 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9566 | } |
| 9567 | } break; |
| 9568 | |
| 9569 | case clang::Type::Auto: |
| 9570 | CompilerType(getASTContext(), |
| 9571 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) |
| 9572 | .DumpTypeDescription(s); |
| 9573 | return; |
| 9574 | |
| 9575 | case clang::Type::Elaborated: |
| 9576 | CompilerType(getASTContext(), |
| 9577 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) |
| 9578 | .DumpTypeDescription(s); |
| 9579 | return; |
| 9580 | |
| 9581 | case clang::Type::Paren: |
| 9582 | CompilerType(getASTContext(), |
| 9583 | llvm::cast<clang::ParenType>(qual_type)->desugar()) |
| 9584 | .DumpTypeDescription(s); |
| 9585 | return; |
| 9586 | |
| 9587 | case clang::Type::Record: { |
| 9588 | GetCompleteType(type); |
| 9589 | |
| 9590 | const clang::RecordType *record_type = |
| 9591 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 9592 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 9593 | const clang::CXXRecordDecl *cxx_record_decl = |
| 9594 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 9595 | |
| 9596 | if (cxx_record_decl) |
| 9597 | cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), |
| 9598 | s->GetIndentLevel()); |
| 9599 | else |
| 9600 | record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), |
| 9601 | s->GetIndentLevel()); |
| 9602 | } break; |
| 9603 | |
| 9604 | default: { |
| 9605 | const clang::TagType *tag_type = |
| 9606 | llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()); |
| 9607 | if (tag_type) { |
| 9608 | clang::TagDecl *tag_decl = tag_type->getDecl(); |
| 9609 | if (tag_decl) |
| 9610 | tag_decl->print(llvm_ostrm, 0); |
| 9611 | } else { |
| 9612 | std::string clang_type_name(qual_type.getAsString()); |
| 9613 | if (!clang_type_name.empty()) |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 9614 | s->PutCString(clang_type_name); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9615 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9616 | } |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 9617 | } |
| 9618 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9619 | if (buf.size() > 0) { |
| 9620 | s->Write(buf.data(), buf.size()); |
Greg Clayton | 8b4edba | 2015-08-14 20:02:05 +0000 | [diff] [blame] | 9621 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9622 | } |
Greg Clayton | 8b4edba | 2015-08-14 20:02:05 +0000 | [diff] [blame] | 9623 | } |
| 9624 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9625 | void ClangASTContext::DumpTypeName(const CompilerType &type) { |
| 9626 | if (ClangUtil::IsClangType(type)) { |
| 9627 | clang::QualType qual_type( |
| 9628 | ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type))); |
| 9629 | |
| 9630 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 9631 | switch (type_class) { |
| 9632 | case clang::Type::Record: { |
| 9633 | const clang::CXXRecordDecl *cxx_record_decl = |
| 9634 | qual_type->getAsCXXRecordDecl(); |
| 9635 | if (cxx_record_decl) |
| 9636 | printf("class %s", cxx_record_decl->getName().str().c_str()); |
| 9637 | } break; |
| 9638 | |
| 9639 | case clang::Type::Enum: { |
| 9640 | clang::EnumDecl *enum_decl = |
| 9641 | llvm::cast<clang::EnumType>(qual_type)->getDecl(); |
| 9642 | if (enum_decl) { |
| 9643 | printf("enum %s", enum_decl->getName().str().c_str()); |
| 9644 | } |
| 9645 | } break; |
| 9646 | |
| 9647 | case clang::Type::ObjCObject: |
| 9648 | case clang::Type::ObjCInterface: { |
| 9649 | const clang::ObjCObjectType *objc_class_type = |
| 9650 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type); |
| 9651 | if (objc_class_type) { |
| 9652 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 9653 | objc_class_type->getInterface(); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 9654 | // We currently can't complete objective C types through the newly |
| 9655 | // added ASTContext because it only supports TagDecl objects right |
| 9656 | // now... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9657 | if (class_interface_decl) |
| 9658 | printf("@class %s", class_interface_decl->getName().str().c_str()); |
| 9659 | } |
| 9660 | } break; |
| 9661 | |
| 9662 | case clang::Type::Typedef: |
| 9663 | printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type) |
| 9664 | ->getDecl() |
| 9665 | ->getName() |
| 9666 | .str() |
| 9667 | .c_str()); |
| 9668 | break; |
| 9669 | |
| 9670 | case clang::Type::Auto: |
| 9671 | printf("auto "); |
| 9672 | return DumpTypeName(CompilerType(type.GetTypeSystem(), |
| 9673 | llvm::cast<clang::AutoType>(qual_type) |
| 9674 | ->getDeducedType() |
| 9675 | .getAsOpaquePtr())); |
| 9676 | |
| 9677 | case clang::Type::Elaborated: |
| 9678 | printf("elaborated "); |
| 9679 | return DumpTypeName(CompilerType( |
| 9680 | type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type) |
| 9681 | ->getNamedType() |
| 9682 | .getAsOpaquePtr())); |
| 9683 | |
| 9684 | case clang::Type::Paren: |
| 9685 | printf("paren "); |
| 9686 | return DumpTypeName(CompilerType( |
| 9687 | type.GetTypeSystem(), |
| 9688 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr())); |
| 9689 | |
| 9690 | default: |
| 9691 | printf("ClangASTContext::DumpTypeName() type_class = %u", type_class); |
| 9692 | break; |
Greg Clayton | 6dc8d58 | 2015-08-18 22:32:36 +0000 | [diff] [blame] | 9693 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9694 | } |
Greg Clayton | 6dc8d58 | 2015-08-18 22:32:36 +0000 | [diff] [blame] | 9695 | } |
| 9696 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9697 | clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl( |
| 9698 | clang::DeclContext *decl_ctx, lldb::AccessType access_type, |
| 9699 | const char *parent_name, int tag_decl_kind, |
| 9700 | const ClangASTContext::TemplateParameterInfos &template_param_infos) { |
| 9701 | if (template_param_infos.IsValid()) { |
| 9702 | std::string template_basename(parent_name); |
| 9703 | template_basename.erase(template_basename.find('<')); |
| 9704 | |
| 9705 | return CreateClassTemplateDecl(decl_ctx, access_type, |
| 9706 | template_basename.c_str(), tag_decl_kind, |
| 9707 | template_param_infos); |
| 9708 | } |
| 9709 | return NULL; |
Greg Clayton | 6dc8d58 | 2015-08-18 22:32:36 +0000 | [diff] [blame] | 9710 | } |
Greg Clayton | 8b4edba | 2015-08-14 20:02:05 +0000 | [diff] [blame] | 9711 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9712 | void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) { |
| 9713 | ClangASTContext *ast = (ClangASTContext *)baton; |
| 9714 | SymbolFile *sym_file = ast->GetSymbolFile(); |
| 9715 | if (sym_file) { |
| 9716 | CompilerType clang_type = GetTypeForDecl(decl); |
| 9717 | if (clang_type) |
| 9718 | sym_file->CompleteType(clang_type); |
| 9719 | } |
Greg Clayton | 261ac3f | 2015-08-28 01:01:03 +0000 | [diff] [blame] | 9720 | } |
| 9721 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9722 | void ClangASTContext::CompleteObjCInterfaceDecl( |
| 9723 | void *baton, clang::ObjCInterfaceDecl *decl) { |
| 9724 | ClangASTContext *ast = (ClangASTContext *)baton; |
| 9725 | SymbolFile *sym_file = ast->GetSymbolFile(); |
| 9726 | if (sym_file) { |
| 9727 | CompilerType clang_type = GetTypeForDecl(decl); |
| 9728 | if (clang_type) |
| 9729 | sym_file->CompleteType(clang_type); |
| 9730 | } |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 9731 | } |
Greg Clayton | 261ac3f | 2015-08-28 01:01:03 +0000 | [diff] [blame] | 9732 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9733 | DWARFASTParser *ClangASTContext::GetDWARFParser() { |
| 9734 | if (!m_dwarf_ast_parser_ap) |
| 9735 | m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this)); |
| 9736 | return m_dwarf_ast_parser_ap.get(); |
| 9737 | } |
| 9738 | |
| 9739 | PDBASTParser *ClangASTContext::GetPDBParser() { |
| 9740 | if (!m_pdb_ast_parser_ap) |
| 9741 | m_pdb_ast_parser_ap.reset(new PDBASTParser(*this)); |
| 9742 | return m_pdb_ast_parser_ap.get(); |
| 9743 | } |
| 9744 | |
| 9745 | bool ClangASTContext::LayoutRecordType( |
| 9746 | void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size, |
| 9747 | uint64_t &alignment, |
| 9748 | llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets, |
| 9749 | llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> |
| 9750 | &base_offsets, |
| 9751 | llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> |
| 9752 | &vbase_offsets) { |
| 9753 | ClangASTContext *ast = (ClangASTContext *)baton; |
Aleksandr Urakov | 7d2a74f | 2018-08-14 07:57:44 +0000 | [diff] [blame^] | 9754 | lldb_private::ClangASTImporter *importer = nullptr; |
| 9755 | if (ast->m_dwarf_ast_parser_ap) |
| 9756 | importer = &ast->m_dwarf_ast_parser_ap->GetClangASTImporter(); |
| 9757 | if (!importer && ast->m_pdb_ast_parser_ap) |
| 9758 | importer = &ast->m_pdb_ast_parser_ap->GetClangASTImporter(); |
| 9759 | if (!importer) |
| 9760 | return false; |
| 9761 | |
| 9762 | return importer->LayoutRecordType(record_decl, bit_size, alignment, |
| 9763 | field_offsets, base_offsets, vbase_offsets); |
Greg Clayton | 8b4edba | 2015-08-14 20:02:05 +0000 | [diff] [blame] | 9764 | } |
| 9765 | |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 9766 | //---------------------------------------------------------------------- |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 9767 | // CompilerDecl override functions |
| 9768 | //---------------------------------------------------------------------- |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 9769 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9770 | ConstString ClangASTContext::DeclGetName(void *opaque_decl) { |
| 9771 | if (opaque_decl) { |
| 9772 | clang::NamedDecl *nd = |
| 9773 | llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl); |
| 9774 | if (nd != nullptr) |
| 9775 | return ConstString(nd->getDeclName().getAsString()); |
| 9776 | } |
| 9777 | return ConstString(); |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 9778 | } |
| 9779 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9780 | ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) { |
| 9781 | if (opaque_decl) { |
| 9782 | clang::NamedDecl *nd = |
| 9783 | llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl); |
| 9784 | if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) { |
| 9785 | clang::MangleContext *mc = getMangleContext(); |
| 9786 | if (mc && mc->shouldMangleCXXName(nd)) { |
| 9787 | llvm::SmallVector<char, 1024> buf; |
| 9788 | llvm::raw_svector_ostream llvm_ostrm(buf); |
| 9789 | if (llvm::isa<clang::CXXConstructorDecl>(nd)) { |
| 9790 | mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd), |
| 9791 | Ctor_Complete, llvm_ostrm); |
| 9792 | } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) { |
| 9793 | mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd), |
| 9794 | Dtor_Complete, llvm_ostrm); |
| 9795 | } else { |
| 9796 | mc->mangleName(nd, llvm_ostrm); |
Greg Clayton | fe68904 | 2015-11-10 17:47:04 +0000 | [diff] [blame] | 9797 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9798 | if (buf.size() > 0) |
| 9799 | return ConstString(buf.data(), buf.size()); |
| 9800 | } |
Greg Clayton | fe68904 | 2015-11-10 17:47:04 +0000 | [diff] [blame] | 9801 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9802 | } |
| 9803 | return ConstString(); |
Greg Clayton | fe68904 | 2015-11-10 17:47:04 +0000 | [diff] [blame] | 9804 | } |
| 9805 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9806 | CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) { |
| 9807 | if (opaque_decl) |
| 9808 | return CompilerDeclContext(this, |
| 9809 | ((clang::Decl *)opaque_decl)->getDeclContext()); |
| 9810 | else |
| 9811 | return CompilerDeclContext(); |
Greg Clayton | fe68904 | 2015-11-10 17:47:04 +0000 | [diff] [blame] | 9812 | } |
| 9813 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9814 | CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) { |
| 9815 | if (clang::FunctionDecl *func_decl = |
| 9816 | llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) |
| 9817 | return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr()); |
| 9818 | if (clang::ObjCMethodDecl *objc_method = |
| 9819 | llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl)) |
| 9820 | return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr()); |
| 9821 | else |
Greg Clayton | fe68904 | 2015-11-10 17:47:04 +0000 | [diff] [blame] | 9822 | return CompilerType(); |
| 9823 | } |
| 9824 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9825 | size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) { |
| 9826 | if (clang::FunctionDecl *func_decl = |
| 9827 | llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) |
| 9828 | return func_decl->param_size(); |
| 9829 | if (clang::ObjCMethodDecl *objc_method = |
| 9830 | llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl)) |
| 9831 | return objc_method->param_size(); |
| 9832 | else |
| 9833 | return 0; |
| 9834 | } |
| 9835 | |
| 9836 | CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl, |
| 9837 | size_t idx) { |
| 9838 | if (clang::FunctionDecl *func_decl = |
| 9839 | llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) { |
| 9840 | if (idx < func_decl->param_size()) { |
| 9841 | ParmVarDecl *var_decl = func_decl->getParamDecl(idx); |
| 9842 | if (var_decl) |
| 9843 | return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr()); |
| 9844 | } |
| 9845 | } else if (clang::ObjCMethodDecl *objc_method = |
| 9846 | llvm::dyn_cast<clang::ObjCMethodDecl>( |
| 9847 | (clang::Decl *)opaque_decl)) { |
| 9848 | if (idx < objc_method->param_size()) |
| 9849 | return CompilerType( |
| 9850 | this, |
| 9851 | objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr()); |
| 9852 | } |
| 9853 | return CompilerType(); |
| 9854 | } |
| 9855 | |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 9856 | //---------------------------------------------------------------------- |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 9857 | // CompilerDeclContext functions |
| 9858 | //---------------------------------------------------------------------- |
| 9859 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9860 | std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName( |
| 9861 | void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) { |
| 9862 | std::vector<CompilerDecl> found_decls; |
| 9863 | if (opaque_decl_ctx) { |
| 9864 | DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx; |
| 9865 | std::set<DeclContext *> searched; |
| 9866 | std::multimap<DeclContext *, DeclContext *> search_queue; |
| 9867 | SymbolFile *symbol_file = GetSymbolFile(); |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 9868 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9869 | for (clang::DeclContext *decl_context = root_decl_ctx; |
| 9870 | decl_context != nullptr && found_decls.empty(); |
| 9871 | decl_context = decl_context->getParent()) { |
| 9872 | search_queue.insert(std::make_pair(decl_context, decl_context)); |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 9873 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9874 | for (auto it = search_queue.find(decl_context); it != search_queue.end(); |
| 9875 | it++) { |
| 9876 | if (!searched.insert(it->second).second) |
| 9877 | continue; |
| 9878 | symbol_file->ParseDeclsForContext( |
| 9879 | CompilerDeclContext(this, it->second)); |
Paul Herman | ea188fc | 2015-09-16 18:48:30 +0000 | [diff] [blame] | 9880 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9881 | for (clang::Decl *child : it->second->decls()) { |
| 9882 | if (clang::UsingDirectiveDecl *ud = |
| 9883 | llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) { |
| 9884 | if (ignore_using_decls) |
| 9885 | continue; |
| 9886 | clang::DeclContext *from = ud->getCommonAncestor(); |
| 9887 | if (searched.find(ud->getNominatedNamespace()) == searched.end()) |
| 9888 | search_queue.insert( |
| 9889 | std::make_pair(from, ud->getNominatedNamespace())); |
| 9890 | } else if (clang::UsingDecl *ud = |
| 9891 | llvm::dyn_cast<clang::UsingDecl>(child)) { |
| 9892 | if (ignore_using_decls) |
| 9893 | continue; |
| 9894 | for (clang::UsingShadowDecl *usd : ud->shadows()) { |
| 9895 | clang::Decl *target = usd->getTargetDecl(); |
| 9896 | if (clang::NamedDecl *nd = |
| 9897 | llvm::dyn_cast<clang::NamedDecl>(target)) { |
| 9898 | IdentifierInfo *ii = nd->getIdentifier(); |
| 9899 | if (ii != nullptr && |
| 9900 | ii->getName().equals(name.AsCString(nullptr))) |
| 9901 | found_decls.push_back(CompilerDecl(this, nd)); |
| 9902 | } |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 9903 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9904 | } else if (clang::NamedDecl *nd = |
| 9905 | llvm::dyn_cast<clang::NamedDecl>(child)) { |
| 9906 | IdentifierInfo *ii = nd->getIdentifier(); |
| 9907 | if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr))) |
| 9908 | found_decls.push_back(CompilerDecl(this, nd)); |
| 9909 | } |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 9910 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9911 | } |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 9912 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9913 | } |
| 9914 | return found_decls; |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 9915 | } |
| 9916 | |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 9917 | // Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9918 | // and return the number of levels it took to find it, or |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 9919 | // LLDB_INVALID_DECL_LEVEL if not found. If the decl was imported via a using |
| 9920 | // declaration, its name and/or type, if set, will be used to check that the |
| 9921 | // decl found in the scope is a match. |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 9922 | // |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9923 | // The optional name is required by languages (like C++) to handle using |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 9924 | // declarations like: |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 9925 | // |
| 9926 | // void poo(); |
| 9927 | // namespace ns { |
| 9928 | // void foo(); |
| 9929 | // void goo(); |
| 9930 | // } |
| 9931 | // void bar() { |
| 9932 | // using ns::foo; |
| 9933 | // // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and |
| 9934 | // // LLDB_INVALID_DECL_LEVEL for 'goo'. |
| 9935 | // } |
| 9936 | // |
| 9937 | // The optional type is useful in the case that there's a specific overload |
| 9938 | // that we're looking for that might otherwise be shadowed, like: |
| 9939 | // |
| 9940 | // void foo(int); |
| 9941 | // namespace ns { |
| 9942 | // void foo(); |
| 9943 | // } |
| 9944 | // void bar() { |
| 9945 | // using ns::foo; |
| 9946 | // // CountDeclLevels returns 0 for { 'foo', void() }, |
| 9947 | // // 1 for { 'foo', void(int) }, and |
| 9948 | // // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }. |
| 9949 | // } |
| 9950 | // |
| 9951 | // NOTE: Because file statics are at the TranslationUnit along with globals, a |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9952 | // function at file scope will return the same level as a function at global |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 9953 | // scope. Ideally we'd like to treat the file scope as an additional scope just |
| 9954 | // below the global scope. More work needs to be done to recognise that, if |
| 9955 | // the decl we're trying to look up is static, we should compare its source |
| 9956 | // file with that of the current scope and return a lower number for it. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9957 | uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx, |
| 9958 | clang::DeclContext *child_decl_ctx, |
| 9959 | ConstString *child_name, |
| 9960 | CompilerType *child_type) { |
| 9961 | if (frame_decl_ctx) { |
| 9962 | std::set<DeclContext *> searched; |
| 9963 | std::multimap<DeclContext *, DeclContext *> search_queue; |
| 9964 | SymbolFile *symbol_file = GetSymbolFile(); |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 9965 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9966 | // Get the lookup scope for the decl we're trying to find. |
| 9967 | clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent(); |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 9968 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9969 | // Look for it in our scope's decl context and its parents. |
| 9970 | uint32_t level = 0; |
| 9971 | for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr; |
| 9972 | decl_ctx = decl_ctx->getParent()) { |
| 9973 | if (!decl_ctx->isLookupContext()) |
| 9974 | continue; |
| 9975 | if (decl_ctx == parent_decl_ctx) |
| 9976 | // Found it! |
| 9977 | return level; |
| 9978 | search_queue.insert(std::make_pair(decl_ctx, decl_ctx)); |
| 9979 | for (auto it = search_queue.find(decl_ctx); it != search_queue.end(); |
| 9980 | it++) { |
| 9981 | if (searched.find(it->second) != searched.end()) |
| 9982 | continue; |
| 9983 | |
| 9984 | // Currently DWARF has one shared translation unit for all Decls at top |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 9985 | // level, so this would erroneously find using statements anywhere. So |
| 9986 | // don't look at the top-level translation unit. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9987 | // TODO fix this and add a testcase that depends on it. |
| 9988 | |
| 9989 | if (llvm::isa<clang::TranslationUnitDecl>(it->second)) |
| 9990 | continue; |
| 9991 | |
| 9992 | searched.insert(it->second); |
| 9993 | symbol_file->ParseDeclsForContext( |
| 9994 | CompilerDeclContext(this, it->second)); |
| 9995 | |
| 9996 | for (clang::Decl *child : it->second->decls()) { |
| 9997 | if (clang::UsingDirectiveDecl *ud = |
| 9998 | llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) { |
| 9999 | clang::DeclContext *ns = ud->getNominatedNamespace(); |
| 10000 | if (ns == parent_decl_ctx) |
| 10001 | // Found it! |
| 10002 | return level; |
| 10003 | clang::DeclContext *from = ud->getCommonAncestor(); |
| 10004 | if (searched.find(ns) == searched.end()) |
| 10005 | search_queue.insert(std::make_pair(from, ns)); |
| 10006 | } else if (child_name) { |
| 10007 | if (clang::UsingDecl *ud = |
| 10008 | llvm::dyn_cast<clang::UsingDecl>(child)) { |
| 10009 | for (clang::UsingShadowDecl *usd : ud->shadows()) { |
| 10010 | clang::Decl *target = usd->getTargetDecl(); |
| 10011 | clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target); |
| 10012 | if (!nd) |
| 10013 | continue; |
| 10014 | // Check names. |
| 10015 | IdentifierInfo *ii = nd->getIdentifier(); |
| 10016 | if (ii == nullptr || |
| 10017 | !ii->getName().equals(child_name->AsCString(nullptr))) |
| 10018 | continue; |
| 10019 | // Check types, if one was provided. |
| 10020 | if (child_type) { |
| 10021 | CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd); |
| 10022 | if (!AreTypesSame(clang_type, *child_type, |
| 10023 | /*ignore_qualifiers=*/true)) |
| 10024 | continue; |
| 10025 | } |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 10026 | // Found it! |
| 10027 | return level; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10028 | } |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 10029 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10030 | } |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 10031 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10032 | } |
| 10033 | ++level; |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 10034 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10035 | } |
| 10036 | return LLDB_INVALID_DECL_LEVEL; |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 10037 | } |
| 10038 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10039 | bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) { |
| 10040 | if (opaque_decl_ctx) |
| 10041 | return ((clang::DeclContext *)opaque_decl_ctx)->isRecord(); |
| 10042 | else |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10043 | return false; |
| 10044 | } |
| 10045 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10046 | ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) { |
| 10047 | if (opaque_decl_ctx) { |
| 10048 | clang::NamedDecl *named_decl = |
| 10049 | llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx); |
| 10050 | if (named_decl) |
| 10051 | return ConstString(named_decl->getName()); |
| 10052 | } |
| 10053 | return ConstString(); |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10054 | } |
| 10055 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10056 | ConstString |
| 10057 | ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) { |
| 10058 | if (opaque_decl_ctx) { |
| 10059 | clang::NamedDecl *named_decl = |
| 10060 | llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx); |
| 10061 | if (named_decl) |
| 10062 | return ConstString( |
| 10063 | llvm::StringRef(named_decl->getQualifiedNameAsString())); |
| 10064 | } |
| 10065 | return ConstString(); |
| 10066 | } |
| 10067 | |
| 10068 | bool ClangASTContext::DeclContextIsClassMethod( |
| 10069 | void *opaque_decl_ctx, lldb::LanguageType *language_ptr, |
| 10070 | bool *is_instance_method_ptr, ConstString *language_object_name_ptr) { |
| 10071 | if (opaque_decl_ctx) { |
| 10072 | clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx; |
| 10073 | if (ObjCMethodDecl *objc_method = |
| 10074 | llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) { |
| 10075 | if (is_instance_method_ptr) |
| 10076 | *is_instance_method_ptr = objc_method->isInstanceMethod(); |
| 10077 | if (language_ptr) |
| 10078 | *language_ptr = eLanguageTypeObjC; |
| 10079 | if (language_object_name_ptr) |
| 10080 | language_object_name_ptr->SetCString("self"); |
| 10081 | return true; |
| 10082 | } else if (CXXMethodDecl *cxx_method = |
| 10083 | llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) { |
| 10084 | if (is_instance_method_ptr) |
| 10085 | *is_instance_method_ptr = cxx_method->isInstance(); |
| 10086 | if (language_ptr) |
| 10087 | *language_ptr = eLanguageTypeC_plus_plus; |
| 10088 | if (language_object_name_ptr) |
| 10089 | language_object_name_ptr->SetCString("this"); |
| 10090 | return true; |
| 10091 | } else if (clang::FunctionDecl *function_decl = |
| 10092 | llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) { |
| 10093 | ClangASTMetadata *metadata = |
| 10094 | GetMetadata(&decl_ctx->getParentASTContext(), function_decl); |
| 10095 | if (metadata && metadata->HasObjectPtr()) { |
| 10096 | if (is_instance_method_ptr) |
| 10097 | *is_instance_method_ptr = true; |
| 10098 | if (language_ptr) |
| 10099 | *language_ptr = eLanguageTypeObjC; |
| 10100 | if (language_object_name_ptr) |
| 10101 | language_object_name_ptr->SetCString(metadata->GetObjectPtrName()); |
| 10102 | return true; |
| 10103 | } |
| 10104 | } |
| 10105 | } |
| 10106 | return false; |
| 10107 | } |
| 10108 | |
| 10109 | clang::DeclContext * |
| 10110 | ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) { |
| 10111 | if (dc.IsClang()) |
| 10112 | return (clang::DeclContext *)dc.GetOpaqueDeclContext(); |
| 10113 | return nullptr; |
| 10114 | } |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10115 | |
| 10116 | ObjCMethodDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10117 | ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) { |
| 10118 | if (dc.IsClang()) |
| 10119 | return llvm::dyn_cast<clang::ObjCMethodDecl>( |
| 10120 | (clang::DeclContext *)dc.GetOpaqueDeclContext()); |
| 10121 | return nullptr; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10122 | } |
| 10123 | |
| 10124 | CXXMethodDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10125 | ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) { |
| 10126 | if (dc.IsClang()) |
| 10127 | return llvm::dyn_cast<clang::CXXMethodDecl>( |
| 10128 | (clang::DeclContext *)dc.GetOpaqueDeclContext()); |
| 10129 | return nullptr; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10130 | } |
| 10131 | |
| 10132 | clang::FunctionDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10133 | ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) { |
| 10134 | if (dc.IsClang()) |
| 10135 | return llvm::dyn_cast<clang::FunctionDecl>( |
| 10136 | (clang::DeclContext *)dc.GetOpaqueDeclContext()); |
| 10137 | return nullptr; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10138 | } |
| 10139 | |
| 10140 | clang::NamespaceDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10141 | ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) { |
| 10142 | if (dc.IsClang()) |
| 10143 | return llvm::dyn_cast<clang::NamespaceDecl>( |
| 10144 | (clang::DeclContext *)dc.GetOpaqueDeclContext()); |
| 10145 | return nullptr; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10146 | } |
| 10147 | |
| 10148 | ClangASTMetadata * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10149 | ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc, |
| 10150 | const void *object) { |
| 10151 | clang::ASTContext *ast = DeclContextGetClangASTContext(dc); |
| 10152 | if (ast) |
| 10153 | return ClangASTContext::GetMetadata(ast, object); |
| 10154 | return nullptr; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10155 | } |
| 10156 | |
| 10157 | clang::ASTContext * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10158 | ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) { |
| 10159 | ClangASTContext *ast = |
| 10160 | llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem()); |
| 10161 | if (ast) |
| 10162 | return ast->getASTContext(); |
| 10163 | return nullptr; |
| 10164 | } |
| 10165 | |
| 10166 | ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target) |
| 10167 | : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()), |
| 10168 | m_target_wp(target.shared_from_this()), |
| 10169 | m_persistent_variables(new ClangPersistentVariables) {} |
| 10170 | |
| 10171 | UserExpression *ClangASTContextForExpressions::GetUserExpression( |
Zachary Turner | c5d7df9 | 2016-11-08 04:52:16 +0000 | [diff] [blame] | 10172 | llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10173 | Expression::ResultType desired_type, |
| 10174 | const EvaluateExpressionOptions &options) { |
| 10175 | TargetSP target_sp = m_target_wp.lock(); |
| 10176 | if (!target_sp) |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10177 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10178 | |
Zachary Turner | c5d7df9 | 2016-11-08 04:52:16 +0000 | [diff] [blame] | 10179 | return new ClangUserExpression(*target_sp.get(), expr, prefix, language, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10180 | desired_type, options); |
Greg Clayton | 8b4edba | 2015-08-14 20:02:05 +0000 | [diff] [blame] | 10181 | } |
| 10182 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10183 | FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller( |
| 10184 | const CompilerType &return_type, const Address &function_address, |
| 10185 | const ValueList &arg_value_list, const char *name) { |
| 10186 | TargetSP target_sp = m_target_wp.lock(); |
| 10187 | if (!target_sp) |
| 10188 | return nullptr; |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 10189 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10190 | Process *process = target_sp->GetProcessSP().get(); |
| 10191 | if (!process) |
| 10192 | return nullptr; |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 10193 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10194 | return new ClangFunctionCaller(*process, return_type, function_address, |
| 10195 | arg_value_list, name); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 10196 | } |
| 10197 | |
| 10198 | UtilityFunction * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10199 | ClangASTContextForExpressions::GetUtilityFunction(const char *text, |
| 10200 | const char *name) { |
| 10201 | TargetSP target_sp = m_target_wp.lock(); |
| 10202 | if (!target_sp) |
| 10203 | return nullptr; |
| 10204 | |
| 10205 | return new ClangUtilityFunction(*target_sp.get(), text, name); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 10206 | } |
Sean Callanan | 8f1f9a1 | 2015-09-30 19:57:57 +0000 | [diff] [blame] | 10207 | |
| 10208 | PersistentExpressionState * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10209 | ClangASTContextForExpressions::GetPersistentExpressionState() { |
| 10210 | return m_persistent_variables.get(); |
Sean Callanan | 8f1f9a1 | 2015-09-30 19:57:57 +0000 | [diff] [blame] | 10211 | } |
Sean Callanan | 68e4423 | 2017-09-28 20:20:25 +0000 | [diff] [blame] | 10212 | |
| 10213 | clang::ExternalASTMerger & |
| 10214 | ClangASTContextForExpressions::GetMergerUnchecked() { |
Eugene Zemtsov | a9d928c | 2017-09-29 03:15:08 +0000 | [diff] [blame] | 10215 | lldbassert(m_scratch_ast_source_ap != nullptr); |
Sean Callanan | 68e4423 | 2017-09-28 20:20:25 +0000 | [diff] [blame] | 10216 | return m_scratch_ast_source_ap->GetMergerUnchecked(); |
| 10217 | } |