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 | |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 15 | #include <mutex> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | #include <string> |
Sean Callanan | fe38c85 | 2015-10-08 23:07:53 +0000 | [diff] [blame] | 17 | #include <vector> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 19 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 20 | // 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] | 21 | // related features using "#ifndef NDEBUG" preprocessor blocks to do one thing |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 22 | // or another. This is bad because it means that if clang was built in release |
| 23 | // mode, it assumes that you are building in release mode which is not always |
| 24 | // the case. You can end up with functions that are defined as empty in header |
| 25 | // files when NDEBUG is not defined, and this can cause link errors with the |
| 26 | // clang .a files that you have since you might be missing functions in the .a |
| 27 | // file. So we have to define NDEBUG when including clang headers to avoid any |
| 28 | // mismatches. This is covered by rdar://problem/8691220 |
| 29 | |
Sean Callanan | 3b1d4f6 | 2011-10-26 17:46:51 +0000 | [diff] [blame] | 30 | #if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF) |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 31 | #define LLDB_DEFINED_NDEBUG_FOR_CLANG |
Sean Callanan | 246549c | 2010-07-08 18:16:16 +0000 | [diff] [blame] | 32 | #define NDEBUG |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 33 | // Need to include assert.h so it is as clang would expect it to be (disabled) |
| 34 | #include <assert.h> |
| 35 | #endif |
| 36 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 37 | #include "clang/AST/ASTContext.h" |
| 38 | #include "clang/AST/ASTImporter.h" |
Greg Clayton | f74c403 | 2012-12-03 18:29:55 +0000 | [diff] [blame] | 39 | #include "clang/AST/Attr.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | #include "clang/AST/CXXInheritance.h" |
Greg Clayton | 8cf0593 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 41 | #include "clang/AST/DeclObjC.h" |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 42 | #include "clang/AST/DeclTemplate.h" |
Greg Clayton | fe68904 | 2015-11-10 17:47:04 +0000 | [diff] [blame] | 43 | #include "clang/AST/Mangle.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | #include "clang/AST/RecordLayout.h" |
| 45 | #include "clang/AST/Type.h" |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 46 | #include "clang/AST/VTableBuilder.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | #include "clang/Basic/Builtins.h" |
Sean Callanan | 7e2863b | 2012-02-06 21:28:03 +0000 | [diff] [blame] | 48 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | #include "clang/Basic/FileManager.h" |
Sean Callanan | 79439e8 | 2010-11-18 02:56:27 +0000 | [diff] [blame] | 50 | #include "clang/Basic/FileSystemOptions.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | #include "clang/Basic/SourceManager.h" |
| 52 | #include "clang/Basic/TargetInfo.h" |
| 53 | #include "clang/Basic/TargetOptions.h" |
| 54 | #include "clang/Frontend/FrontendOptions.h" |
| 55 | #include "clang/Frontend/LangStandard.h" |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 56 | |
| 57 | #ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG |
Sean Callanan | 246549c | 2010-07-08 18:16:16 +0000 | [diff] [blame] | 58 | #undef NDEBUG |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 59 | #undef LLDB_DEFINED_NDEBUG_FOR_CLANG |
| 60 | // Need to re-include assert.h so it is as _we_ would expect it to be (enabled) |
| 61 | #include <assert.h> |
| 62 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 63 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 64 | #include "llvm/Support/Signals.h" |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 65 | #include "llvm/Support/Threading.h" |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 66 | |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 67 | #include "Plugins/ExpressionParser/Clang/ClangFunctionCaller.h" |
| 68 | #include "Plugins/ExpressionParser/Clang/ClangUserExpression.h" |
| 69 | #include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h" |
Pavel Labath | 5f19b90 | 2017-11-13 16:16:33 +0000 | [diff] [blame] | 70 | #include "lldb/Utility/ArchSpec.h" |
Zachary Turner | 01c3243 | 2017-02-14 19:06:07 +0000 | [diff] [blame] | 71 | #include "lldb/Utility/Flags.h" |
| 72 | |
Zachary Turner | 29cb868 | 2017-03-03 20:57:05 +0000 | [diff] [blame] | 73 | #include "lldb/Core/DumpDataExtractor.h" |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 74 | #include "lldb/Core/Module.h" |
| 75 | #include "lldb/Core/PluginManager.h" |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 76 | #include "lldb/Core/StreamFile.h" |
Enrico Granata | 2267ad4 | 2014-09-16 17:28:40 +0000 | [diff] [blame] | 77 | #include "lldb/Core/ThreadSafeDenseMap.h" |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 78 | #include "lldb/Core/UniqueCStringMap.h" |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 79 | #include "lldb/Symbol/ClangASTContext.h" |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 80 | #include "lldb/Symbol/ClangASTImporter.h" |
Greg Clayton | 6dc8d58 | 2015-08-18 22:32:36 +0000 | [diff] [blame] | 81 | #include "lldb/Symbol/ClangExternalASTSourceCallbacks.h" |
Sean Callanan | 3b107b1 | 2011-12-03 03:15:28 +0000 | [diff] [blame] | 82 | #include "lldb/Symbol/ClangExternalASTSourceCommon.h" |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 83 | #include "lldb/Symbol/ClangUtil.h" |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 84 | #include "lldb/Symbol/ObjectFile.h" |
Greg Clayton | 261ac3f | 2015-08-28 01:01:03 +0000 | [diff] [blame] | 85 | #include "lldb/Symbol/SymbolFile.h" |
Sean Callanan | 5e9e199 | 2011-10-26 01:06:27 +0000 | [diff] [blame] | 86 | #include "lldb/Symbol/VerifyDecl.h" |
Jim Ingham | d555bac | 2011-06-24 22:03:24 +0000 | [diff] [blame] | 87 | #include "lldb/Target/ExecutionContext.h" |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 88 | #include "lldb/Target/Language.h" |
Jim Ingham | d555bac | 2011-06-24 22:03:24 +0000 | [diff] [blame] | 89 | #include "lldb/Target/ObjCLanguageRuntime.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 90 | #include "lldb/Target/Process.h" |
| 91 | #include "lldb/Target/Target.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 92 | #include "lldb/Utility/DataExtractor.h" |
Sean Callanan | c530ba9 | 2016-05-02 21:15:31 +0000 | [diff] [blame] | 93 | #include "lldb/Utility/LLDBAssert.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 94 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 95 | #include "lldb/Utility/RegularExpression.h" |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 +0000 | [diff] [blame] | 96 | #include "lldb/Utility/Scalar.h" |
Jim Ingham | d555bac | 2011-06-24 22:03:24 +0000 | [diff] [blame] | 97 | |
Greg Clayton | 261ac3f | 2015-08-28 01:01:03 +0000 | [diff] [blame] | 98 | #include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h" |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 99 | #include "Plugins/SymbolFile/PDB/PDBASTParser.h" |
Greg Clayton | 261ac3f | 2015-08-28 01:01:03 +0000 | [diff] [blame] | 100 | |
Eli Friedman | 932197d | 2010-06-13 19:06:42 +0000 | [diff] [blame] | 101 | #include <stdio.h> |
| 102 | |
Greg Clayton | 1341baf | 2013-07-11 23:36:31 +0000 | [diff] [blame] | 103 | #include <mutex> |
| 104 | |
Greg Clayton | c86103d | 2010-08-05 01:57:25 +0000 | [diff] [blame] | 105 | using namespace lldb; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 106 | using namespace lldb_private; |
| 107 | using namespace llvm; |
| 108 | using namespace clang; |
| 109 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 110 | namespace { |
| 111 | static inline bool |
| 112 | ClangASTContextSupportsLanguage(lldb::LanguageType language) { |
| 113 | return language == eLanguageTypeUnknown || // Clang is the default type system |
| 114 | Language::LanguageIsC(language) || |
| 115 | Language::LanguageIsCPlusPlus(language) || |
| 116 | Language::LanguageIsObjC(language) || |
| 117 | Language::LanguageIsPascal(language) || |
| 118 | // Use Clang for Rust until there is a proper language plugin for it |
| 119 | language == eLanguageTypeRust || |
Johan Engelen | 0479957 | 2016-11-25 11:01:12 +0000 | [diff] [blame] | 120 | language == eLanguageTypeExtRenderScript || |
| 121 | // Use Clang for D until there is a proper language plugin for it |
Bruce Mitchener | b8233f8 | 2018-11-27 05:37:27 +0000 | [diff] [blame] | 122 | language == eLanguageTypeD || |
| 123 | // Open Dylan compiler debug info is designed to be Clang-compatible |
| 124 | language == eLanguageTypeDylan; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 125 | } |
Aleksandr Urakov | 7d2a74f | 2018-08-14 07:57:44 +0000 | [diff] [blame] | 126 | |
| 127 | // Checks whether m1 is an overload of m2 (as opposed to an override). This is |
| 128 | // called by addOverridesForMethod to distinguish overrides (which share a |
| 129 | // vtable entry) from overloads (which require distinct entries). |
| 130 | bool isOverload(clang::CXXMethodDecl *m1, clang::CXXMethodDecl *m2) { |
| 131 | // FIXME: This should detect covariant return types, but currently doesn't. |
| 132 | lldbassert(&m1->getASTContext() == &m2->getASTContext() && |
| 133 | "Methods should have the same AST context"); |
| 134 | clang::ASTContext &context = m1->getASTContext(); |
| 135 | |
| 136 | const auto *m1Type = llvm::cast<clang::FunctionProtoType>( |
| 137 | context.getCanonicalType(m1->getType())); |
| 138 | |
| 139 | const auto *m2Type = llvm::cast<clang::FunctionProtoType>( |
| 140 | context.getCanonicalType(m2->getType())); |
| 141 | |
| 142 | auto compareArgTypes = [&context](const clang::QualType &m1p, |
| 143 | const clang::QualType &m2p) { |
| 144 | return context.hasSameType(m1p.getUnqualifiedType(), |
| 145 | m2p.getUnqualifiedType()); |
| 146 | }; |
| 147 | |
| 148 | // FIXME: In C++14 and later, we can just pass m2Type->param_type_end() |
| 149 | // as a fourth parameter to std::equal(). |
| 150 | return (m1->getNumParams() != m2->getNumParams()) || |
| 151 | !std::equal(m1Type->param_type_begin(), m1Type->param_type_end(), |
| 152 | m2Type->param_type_begin(), compareArgTypes); |
| 153 | } |
| 154 | |
| 155 | // If decl is a virtual method, walk the base classes looking for methods that |
| 156 | // decl overrides. This table of overridden methods is used by IRGen to |
| 157 | // determine the vtable layout for decl's parent class. |
| 158 | void addOverridesForMethod(clang::CXXMethodDecl *decl) { |
| 159 | if (!decl->isVirtual()) |
| 160 | return; |
| 161 | |
| 162 | clang::CXXBasePaths paths; |
| 163 | |
| 164 | auto find_overridden_methods = |
| 165 | [decl](const clang::CXXBaseSpecifier *specifier, |
| 166 | clang::CXXBasePath &path) { |
| 167 | if (auto *base_record = llvm::dyn_cast<clang::CXXRecordDecl>( |
| 168 | specifier->getType()->getAs<clang::RecordType>()->getDecl())) { |
| 169 | |
| 170 | clang::DeclarationName name = decl->getDeclName(); |
| 171 | |
| 172 | // If this is a destructor, check whether the base class destructor is |
| 173 | // virtual. |
| 174 | if (name.getNameKind() == clang::DeclarationName::CXXDestructorName) |
| 175 | if (auto *baseDtorDecl = base_record->getDestructor()) { |
| 176 | if (baseDtorDecl->isVirtual()) { |
| 177 | path.Decls = baseDtorDecl; |
| 178 | return true; |
| 179 | } else |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | // Otherwise, search for name in the base class. |
| 184 | for (path.Decls = base_record->lookup(name); !path.Decls.empty(); |
| 185 | path.Decls = path.Decls.slice(1)) { |
| 186 | if (auto *method_decl = |
| 187 | llvm::dyn_cast<clang::CXXMethodDecl>(path.Decls.front())) |
| 188 | if (method_decl->isVirtual() && !isOverload(decl, method_decl)) { |
| 189 | path.Decls = method_decl; |
| 190 | return true; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | return false; |
| 196 | }; |
| 197 | |
| 198 | if (decl->getParent()->lookupInBases(find_overridden_methods, paths)) { |
| 199 | for (auto *overridden_decl : paths.found_decls()) |
| 200 | decl->addOverriddenMethod( |
| 201 | llvm::cast<clang::CXXMethodDecl>(overridden_decl)); |
| 202 | } |
| 203 | } |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Aleksandr Urakov | 1dc51db | 2018-11-12 16:23:50 +0000 | [diff] [blame] | 206 | static lldb::addr_t GetVTableAddress(Process &process, |
| 207 | VTableContextBase &vtable_ctx, |
| 208 | ValueObject &valobj, |
| 209 | const ASTRecordLayout &record_layout) { |
| 210 | // Retrieve type info |
| 211 | CompilerType pointee_type; |
| 212 | CompilerType this_type(valobj.GetCompilerType()); |
| 213 | uint32_t type_info = this_type.GetTypeInfo(&pointee_type); |
| 214 | if (!type_info) |
| 215 | return LLDB_INVALID_ADDRESS; |
| 216 | |
| 217 | // Check if it's a pointer or reference |
| 218 | bool ptr_or_ref = false; |
| 219 | if (type_info & (eTypeIsPointer | eTypeIsReference)) { |
| 220 | ptr_or_ref = true; |
| 221 | type_info = pointee_type.GetTypeInfo(); |
| 222 | } |
| 223 | |
| 224 | // We process only C++ classes |
| 225 | const uint32_t cpp_class = eTypeIsClass | eTypeIsCPlusPlus; |
| 226 | if ((type_info & cpp_class) != cpp_class) |
| 227 | return LLDB_INVALID_ADDRESS; |
| 228 | |
| 229 | // Calculate offset to VTable pointer |
| 230 | lldb::offset_t vbtable_ptr_offset = |
| 231 | vtable_ctx.isMicrosoft() ? record_layout.getVBPtrOffset().getQuantity() |
| 232 | : 0; |
| 233 | |
| 234 | if (ptr_or_ref) { |
| 235 | // We have a pointer / ref to object, so read |
| 236 | // VTable pointer from process memory |
| 237 | |
| 238 | if (valobj.GetAddressTypeOfChildren() != eAddressTypeLoad) |
| 239 | return LLDB_INVALID_ADDRESS; |
| 240 | |
| 241 | auto vbtable_ptr_addr = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS); |
| 242 | if (vbtable_ptr_addr == LLDB_INVALID_ADDRESS) |
| 243 | return LLDB_INVALID_ADDRESS; |
| 244 | |
| 245 | vbtable_ptr_addr += vbtable_ptr_offset; |
| 246 | |
| 247 | Status err; |
| 248 | return process.ReadPointerFromMemory(vbtable_ptr_addr, err); |
| 249 | } |
| 250 | |
| 251 | // We have an object already read from process memory, |
| 252 | // so just extract VTable pointer from it |
| 253 | |
| 254 | DataExtractor data; |
| 255 | Status err; |
| 256 | auto size = valobj.GetData(data, err); |
| 257 | if (err.Fail() || vbtable_ptr_offset + data.GetAddressByteSize() > size) |
| 258 | return LLDB_INVALID_ADDRESS; |
| 259 | |
| 260 | return data.GetPointer(&vbtable_ptr_offset); |
| 261 | } |
| 262 | |
| 263 | static int64_t ReadVBaseOffsetFromVTable(Process &process, |
| 264 | VTableContextBase &vtable_ctx, |
| 265 | lldb::addr_t vtable_ptr, |
| 266 | const CXXRecordDecl *cxx_record_decl, |
| 267 | const CXXRecordDecl *base_class_decl) { |
| 268 | if (vtable_ctx.isMicrosoft()) { |
| 269 | clang::MicrosoftVTableContext &msoft_vtable_ctx = |
| 270 | static_cast<clang::MicrosoftVTableContext &>(vtable_ctx); |
| 271 | |
| 272 | // Get the index into the virtual base table. The |
| 273 | // index is the index in uint32_t from vbtable_ptr |
| 274 | const unsigned vbtable_index = |
| 275 | msoft_vtable_ctx.getVBTableIndex(cxx_record_decl, base_class_decl); |
| 276 | const lldb::addr_t base_offset_addr = vtable_ptr + vbtable_index * 4; |
| 277 | Status err; |
| 278 | return process.ReadSignedIntegerFromMemory(base_offset_addr, 4, INT64_MAX, |
| 279 | err); |
| 280 | } |
| 281 | |
| 282 | clang::ItaniumVTableContext &itanium_vtable_ctx = |
| 283 | static_cast<clang::ItaniumVTableContext &>(vtable_ctx); |
| 284 | |
| 285 | clang::CharUnits base_offset_offset = |
| 286 | itanium_vtable_ctx.getVirtualBaseOffsetOffset(cxx_record_decl, |
| 287 | base_class_decl); |
| 288 | const lldb::addr_t base_offset_addr = |
| 289 | vtable_ptr + base_offset_offset.getQuantity(); |
| 290 | const uint32_t base_offset_size = process.GetAddressByteSize(); |
| 291 | Status err; |
| 292 | return process.ReadSignedIntegerFromMemory(base_offset_addr, base_offset_size, |
| 293 | INT64_MAX, err); |
| 294 | } |
| 295 | |
| 296 | static bool GetVBaseBitOffset(VTableContextBase &vtable_ctx, |
| 297 | ValueObject &valobj, |
| 298 | const ASTRecordLayout &record_layout, |
| 299 | const CXXRecordDecl *cxx_record_decl, |
| 300 | const CXXRecordDecl *base_class_decl, |
| 301 | int32_t &bit_offset) { |
| 302 | ExecutionContext exe_ctx(valobj.GetExecutionContextRef()); |
| 303 | Process *process = exe_ctx.GetProcessPtr(); |
| 304 | if (!process) |
| 305 | return false; |
| 306 | |
| 307 | lldb::addr_t vtable_ptr = |
| 308 | GetVTableAddress(*process, vtable_ctx, valobj, record_layout); |
| 309 | if (vtable_ptr == LLDB_INVALID_ADDRESS) |
| 310 | return false; |
| 311 | |
| 312 | auto base_offset = ReadVBaseOffsetFromVTable( |
| 313 | *process, vtable_ctx, vtable_ptr, cxx_record_decl, base_class_decl); |
| 314 | if (base_offset == INT64_MAX) |
| 315 | return false; |
| 316 | |
| 317 | bit_offset = base_offset * 8; |
| 318 | |
| 319 | return true; |
| 320 | } |
| 321 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 322 | typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext *> |
| 323 | ClangASTMap; |
Enrico Granata | 5d84a69 | 2014-08-19 21:46:37 +0000 | [diff] [blame] | 324 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 325 | static ClangASTMap &GetASTMap() { |
| 326 | static ClangASTMap *g_map_ptr = nullptr; |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 327 | static llvm::once_flag g_once_flag; |
| 328 | llvm::call_once(g_once_flag, []() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 329 | g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins |
| 330 | }); |
| 331 | return *g_map_ptr; |
Enrico Granata | 5d84a69 | 2014-08-19 21:46:37 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Davide Italiano | 7e3ef4d | 2018-03-20 19:46:32 +0000 | [diff] [blame] | 334 | bool ClangASTContext::IsOperator(const char *name, |
| 335 | clang::OverloadedOperatorKind &op_kind) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 336 | if (name == nullptr || name[0] == '\0') |
| 337 | return false; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 338 | |
| 339 | #define OPERATOR_PREFIX "operator" |
| 340 | #define OPERATOR_PREFIX_LENGTH (sizeof(OPERATOR_PREFIX) - 1) |
| 341 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 342 | const char *post_op_name = nullptr; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 343 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 344 | bool no_space = true; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 345 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 346 | if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH)) |
| 347 | return false; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 348 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 349 | post_op_name = name + OPERATOR_PREFIX_LENGTH; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 350 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 351 | if (post_op_name[0] == ' ') { |
| 352 | post_op_name++; |
| 353 | no_space = false; |
| 354 | } |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 355 | |
| 356 | #undef OPERATOR_PREFIX |
| 357 | #undef OPERATOR_PREFIX_LENGTH |
| 358 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 359 | // This is an operator, set the overloaded operator kind to invalid in case |
| 360 | // this is a conversion operator... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 361 | op_kind = clang::NUM_OVERLOADED_OPERATORS; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 362 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 363 | switch (post_op_name[0]) { |
| 364 | default: |
| 365 | if (no_space) |
| 366 | return false; |
| 367 | break; |
| 368 | case 'n': |
| 369 | if (no_space) |
| 370 | return false; |
| 371 | if (strcmp(post_op_name, "new") == 0) |
| 372 | op_kind = clang::OO_New; |
| 373 | else if (strcmp(post_op_name, "new[]") == 0) |
| 374 | op_kind = clang::OO_Array_New; |
| 375 | break; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 376 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 377 | case 'd': |
| 378 | if (no_space) |
| 379 | return false; |
| 380 | if (strcmp(post_op_name, "delete") == 0) |
| 381 | op_kind = clang::OO_Delete; |
| 382 | else if (strcmp(post_op_name, "delete[]") == 0) |
| 383 | op_kind = clang::OO_Array_Delete; |
| 384 | break; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 385 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 386 | case '+': |
| 387 | if (post_op_name[1] == '\0') |
| 388 | op_kind = clang::OO_Plus; |
| 389 | else if (post_op_name[2] == '\0') { |
| 390 | if (post_op_name[1] == '=') |
| 391 | op_kind = clang::OO_PlusEqual; |
| 392 | else if (post_op_name[1] == '+') |
| 393 | op_kind = clang::OO_PlusPlus; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 394 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 395 | break; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 396 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 397 | case '-': |
| 398 | if (post_op_name[1] == '\0') |
| 399 | op_kind = clang::OO_Minus; |
| 400 | else if (post_op_name[2] == '\0') { |
| 401 | switch (post_op_name[1]) { |
| 402 | case '=': |
| 403 | op_kind = clang::OO_MinusEqual; |
| 404 | break; |
| 405 | case '-': |
| 406 | op_kind = clang::OO_MinusMinus; |
| 407 | break; |
| 408 | case '>': |
| 409 | op_kind = clang::OO_Arrow; |
| 410 | break; |
| 411 | } |
| 412 | } else if (post_op_name[3] == '\0') { |
| 413 | if (post_op_name[2] == '*') |
| 414 | op_kind = clang::OO_ArrowStar; |
| 415 | break; |
| 416 | } |
| 417 | break; |
| 418 | |
| 419 | case '*': |
| 420 | if (post_op_name[1] == '\0') |
| 421 | op_kind = clang::OO_Star; |
| 422 | else if (post_op_name[1] == '=' && post_op_name[2] == '\0') |
| 423 | op_kind = clang::OO_StarEqual; |
| 424 | break; |
| 425 | |
| 426 | case '/': |
| 427 | if (post_op_name[1] == '\0') |
| 428 | op_kind = clang::OO_Slash; |
| 429 | else if (post_op_name[1] == '=' && post_op_name[2] == '\0') |
| 430 | op_kind = clang::OO_SlashEqual; |
| 431 | break; |
| 432 | |
| 433 | case '%': |
| 434 | if (post_op_name[1] == '\0') |
| 435 | op_kind = clang::OO_Percent; |
| 436 | else if (post_op_name[1] == '=' && post_op_name[2] == '\0') |
| 437 | op_kind = clang::OO_PercentEqual; |
| 438 | break; |
| 439 | |
| 440 | case '^': |
| 441 | if (post_op_name[1] == '\0') |
| 442 | op_kind = clang::OO_Caret; |
| 443 | else if (post_op_name[1] == '=' && post_op_name[2] == '\0') |
| 444 | op_kind = clang::OO_CaretEqual; |
| 445 | break; |
| 446 | |
| 447 | case '&': |
| 448 | if (post_op_name[1] == '\0') |
| 449 | op_kind = clang::OO_Amp; |
| 450 | else if (post_op_name[2] == '\0') { |
| 451 | switch (post_op_name[1]) { |
| 452 | case '=': |
| 453 | op_kind = clang::OO_AmpEqual; |
| 454 | break; |
| 455 | case '&': |
| 456 | op_kind = clang::OO_AmpAmp; |
| 457 | break; |
| 458 | } |
| 459 | } |
| 460 | break; |
| 461 | |
| 462 | case '|': |
| 463 | if (post_op_name[1] == '\0') |
| 464 | op_kind = clang::OO_Pipe; |
| 465 | else if (post_op_name[2] == '\0') { |
| 466 | switch (post_op_name[1]) { |
| 467 | case '=': |
| 468 | op_kind = clang::OO_PipeEqual; |
| 469 | break; |
| 470 | case '|': |
| 471 | op_kind = clang::OO_PipePipe; |
| 472 | break; |
| 473 | } |
| 474 | } |
| 475 | break; |
| 476 | |
| 477 | case '~': |
| 478 | if (post_op_name[1] == '\0') |
| 479 | op_kind = clang::OO_Tilde; |
| 480 | break; |
| 481 | |
| 482 | case '!': |
| 483 | if (post_op_name[1] == '\0') |
| 484 | op_kind = clang::OO_Exclaim; |
| 485 | else if (post_op_name[1] == '=' && post_op_name[2] == '\0') |
| 486 | op_kind = clang::OO_ExclaimEqual; |
| 487 | break; |
| 488 | |
| 489 | case '=': |
| 490 | if (post_op_name[1] == '\0') |
| 491 | op_kind = clang::OO_Equal; |
| 492 | else if (post_op_name[1] == '=' && post_op_name[2] == '\0') |
| 493 | op_kind = clang::OO_EqualEqual; |
| 494 | break; |
| 495 | |
| 496 | case '<': |
| 497 | if (post_op_name[1] == '\0') |
| 498 | op_kind = clang::OO_Less; |
| 499 | else if (post_op_name[2] == '\0') { |
| 500 | switch (post_op_name[1]) { |
| 501 | case '<': |
| 502 | op_kind = clang::OO_LessLess; |
| 503 | break; |
| 504 | case '=': |
| 505 | op_kind = clang::OO_LessEqual; |
| 506 | break; |
| 507 | } |
| 508 | } else if (post_op_name[3] == '\0') { |
| 509 | if (post_op_name[2] == '=') |
| 510 | op_kind = clang::OO_LessLessEqual; |
| 511 | } |
| 512 | break; |
| 513 | |
| 514 | case '>': |
| 515 | if (post_op_name[1] == '\0') |
| 516 | op_kind = clang::OO_Greater; |
| 517 | else if (post_op_name[2] == '\0') { |
| 518 | switch (post_op_name[1]) { |
| 519 | case '>': |
| 520 | op_kind = clang::OO_GreaterGreater; |
| 521 | break; |
| 522 | case '=': |
| 523 | op_kind = clang::OO_GreaterEqual; |
| 524 | break; |
| 525 | } |
| 526 | } else if (post_op_name[1] == '>' && post_op_name[2] == '=' && |
| 527 | post_op_name[3] == '\0') { |
| 528 | op_kind = clang::OO_GreaterGreaterEqual; |
| 529 | } |
| 530 | break; |
| 531 | |
| 532 | case ',': |
| 533 | if (post_op_name[1] == '\0') |
| 534 | op_kind = clang::OO_Comma; |
| 535 | break; |
| 536 | |
| 537 | case '(': |
| 538 | if (post_op_name[1] == ')' && post_op_name[2] == '\0') |
| 539 | op_kind = clang::OO_Call; |
| 540 | break; |
| 541 | |
| 542 | case '[': |
| 543 | if (post_op_name[1] == ']' && post_op_name[2] == '\0') |
| 544 | op_kind = clang::OO_Subscript; |
| 545 | break; |
| 546 | } |
| 547 | |
| 548 | return true; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 549 | } |
Enrico Granata | 5d84a69 | 2014-08-19 21:46:37 +0000 | [diff] [blame] | 550 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 551 | clang::AccessSpecifier |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 552 | ClangASTContext::ConvertAccessTypeToAccessSpecifier(AccessType access) { |
| 553 | switch (access) { |
| 554 | default: |
| 555 | break; |
| 556 | case eAccessNone: |
Greg Clayton | 8cf0593 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 557 | return AS_none; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 558 | case eAccessPublic: |
| 559 | return AS_public; |
| 560 | case eAccessPrivate: |
| 561 | return AS_private; |
| 562 | case eAccessProtected: |
| 563 | return AS_protected; |
| 564 | } |
| 565 | return AS_none; |
Greg Clayton | 8cf0593 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 568 | static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) { |
| 569 | // FIXME: Cleanup per-file based stuff. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 570 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 571 | // Set some properties which depend solely on the input kind; it would be |
| 572 | // nice to move these to the language standard, and have the driver resolve |
| 573 | // the input kind + language standard. |
Richard Smith | 8186cd4 | 2017-04-26 22:10:53 +0000 | [diff] [blame] | 574 | if (IK.getLanguage() == InputKind::Asm) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 575 | Opts.AsmPreprocessor = 1; |
Richard Smith | 8186cd4 | 2017-04-26 22:10:53 +0000 | [diff] [blame] | 576 | } else if (IK.isObjectiveC()) { |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 577 | Opts.ObjC = 1; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | LangStandard::Kind LangStd = LangStandard::lang_unspecified; |
| 581 | |
| 582 | if (LangStd == LangStandard::lang_unspecified) { |
| 583 | // Based on the base language, pick one. |
Richard Smith | 8186cd4 | 2017-04-26 22:10:53 +0000 | [diff] [blame] | 584 | switch (IK.getLanguage()) { |
| 585 | case InputKind::Unknown: |
| 586 | case InputKind::LLVM_IR: |
| 587 | case InputKind::RenderScript: |
David Blaikie | a322f36 | 2017-01-06 00:38:06 +0000 | [diff] [blame] | 588 | llvm_unreachable("Invalid input kind!"); |
Richard Smith | 8186cd4 | 2017-04-26 22:10:53 +0000 | [diff] [blame] | 589 | case InputKind::OpenCL: |
Pavel Labath | 4716854 | 2017-04-27 08:49:19 +0000 | [diff] [blame] | 590 | LangStd = LangStandard::lang_opencl10; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 591 | break; |
Richard Smith | 8186cd4 | 2017-04-26 22:10:53 +0000 | [diff] [blame] | 592 | case InputKind::CUDA: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 593 | LangStd = LangStandard::lang_cuda; |
| 594 | break; |
Richard Smith | 8186cd4 | 2017-04-26 22:10:53 +0000 | [diff] [blame] | 595 | case InputKind::Asm: |
| 596 | case InputKind::C: |
| 597 | case InputKind::ObjC: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 598 | LangStd = LangStandard::lang_gnu99; |
| 599 | break; |
Richard Smith | 8186cd4 | 2017-04-26 22:10:53 +0000 | [diff] [blame] | 600 | case InputKind::CXX: |
| 601 | case InputKind::ObjCXX: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 602 | LangStd = LangStandard::lang_gnucxx98; |
| 603 | break; |
Benjamin Kramer | 0d97c22 | 2018-04-25 13:22:47 +0000 | [diff] [blame] | 604 | case InputKind::HIP: |
| 605 | LangStd = LangStandard::lang_hip; |
| 606 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 607 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 608 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 609 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 610 | const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd); |
| 611 | Opts.LineComment = Std.hasLineComments(); |
| 612 | Opts.C99 = Std.isC99(); |
| 613 | Opts.CPlusPlus = Std.isCPlusPlus(); |
| 614 | Opts.CPlusPlus11 = Std.isCPlusPlus11(); |
| 615 | Opts.Digraphs = Std.hasDigraphs(); |
| 616 | Opts.GNUMode = Std.isGNUMode(); |
| 617 | Opts.GNUInline = !Std.isC99(); |
| 618 | Opts.HexFloats = Std.hasHexFloats(); |
| 619 | Opts.ImplicitInt = Std.hasImplicitInt(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 620 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 621 | Opts.WChar = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 622 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 623 | // OpenCL has some additional defaults. |
Pavel Labath | 4716854 | 2017-04-27 08:49:19 +0000 | [diff] [blame] | 624 | if (LangStd == LangStandard::lang_opencl10) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 625 | Opts.OpenCL = 1; |
| 626 | Opts.AltiVec = 1; |
| 627 | Opts.CXXOperatorNames = 1; |
| 628 | Opts.LaxVectorConversions = 1; |
| 629 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 630 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 631 | // OpenCL and C++ both have bool, true, false keywords. |
| 632 | Opts.Bool = Opts.OpenCL || Opts.CPlusPlus; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 633 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 634 | Opts.setValueVisibilityMode(DefaultVisibility); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 635 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 636 | // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs is |
| 637 | // specified, or -std is set to a conforming mode. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 638 | Opts.Trigraphs = !Opts.GNUMode; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 639 | Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 640 | Opts.OptimizeSize = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 641 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 642 | // FIXME: Eliminate this dependency. |
| 643 | // unsigned Opt = |
| 644 | // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags); |
| 645 | // Opts.Optimize = Opt != 0; |
| 646 | unsigned Opt = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 647 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 648 | // This is the __NO_INLINE__ define, which just depends on things like the |
| 649 | // optimization level and -fno-inline, not actually whether the backend has |
| 650 | // inlining enabled. |
| 651 | // |
| 652 | // FIXME: This is affected by other options (-fno-inline). |
| 653 | Opts.NoInlineDefine = !Opt; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 656 | ClangASTContext::ClangASTContext(const char *target_triple) |
| 657 | : TypeSystem(TypeSystem::eKindClang), m_target_triple(), m_ast_ap(), |
| 658 | m_language_options_ap(), m_source_manager_ap(), m_diagnostics_engine_ap(), |
| 659 | m_target_options_rp(), m_target_info_ap(), m_identifier_table_ap(), |
| 660 | m_selector_table_ap(), m_builtins_ap(), m_callback_tag_decl(nullptr), |
| 661 | m_callback_objc_decl(nullptr), m_callback_baton(nullptr), |
| 662 | m_pointer_byte_size(0), m_ast_owned(false) { |
| 663 | if (target_triple && target_triple[0]) |
| 664 | SetTargetTriple(target_triple); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | //---------------------------------------------------------------------- |
| 668 | // Destructor |
| 669 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 670 | ClangASTContext::~ClangASTContext() { Finalize(); } |
| 671 | |
| 672 | ConstString ClangASTContext::GetPluginNameStatic() { |
| 673 | return ConstString("clang"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 676 | ConstString ClangASTContext::GetPluginName() { |
| 677 | return ClangASTContext::GetPluginNameStatic(); |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 680 | uint32_t ClangASTContext::GetPluginVersion() { return 1; } |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 681 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 682 | lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language, |
| 683 | lldb_private::Module *module, |
| 684 | Target *target) { |
| 685 | if (ClangASTContextSupportsLanguage(language)) { |
| 686 | ArchSpec arch; |
| 687 | if (module) |
| 688 | arch = module->GetArchitecture(); |
| 689 | else if (target) |
| 690 | arch = target->GetArchitecture(); |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 691 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 692 | if (arch.IsValid()) { |
| 693 | ArchSpec fixed_arch = arch; |
| 694 | // LLVM wants this to be set to iOS or MacOSX; if we're working on |
| 695 | // a bare-boards type image, change the triple for llvm's benefit. |
| 696 | if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple && |
| 697 | fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) { |
| 698 | if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm || |
| 699 | fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 || |
| 700 | fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) { |
| 701 | fixed_arch.GetTriple().setOS(llvm::Triple::IOS); |
| 702 | } else { |
| 703 | fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX); |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 704 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 705 | } |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 706 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 707 | if (module) { |
| 708 | std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext); |
| 709 | if (ast_sp) { |
| 710 | ast_sp->SetArchitecture(fixed_arch); |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 711 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 712 | return ast_sp; |
| 713 | } else if (target && target->IsValid()) { |
| 714 | std::shared_ptr<ClangASTContextForExpressions> ast_sp( |
| 715 | new ClangASTContextForExpressions(*target)); |
| 716 | if (ast_sp) { |
| 717 | ast_sp->SetArchitecture(fixed_arch); |
| 718 | ast_sp->m_scratch_ast_source_ap.reset( |
| 719 | new ClangASTSource(target->shared_from_this())); |
Sean Callanan | 68e4423 | 2017-09-28 20:20:25 +0000 | [diff] [blame] | 720 | lldbassert(ast_sp->getFileManager()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 721 | ast_sp->m_scratch_ast_source_ap->InstallASTContext( |
Sean Callanan | 68e4423 | 2017-09-28 20:20:25 +0000 | [diff] [blame] | 722 | *ast_sp->getASTContext(), *ast_sp->getFileManager(), true); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 723 | llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source( |
| 724 | ast_sp->m_scratch_ast_source_ap->CreateProxy()); |
| 725 | ast_sp->SetExternalSource(proxy_ast_source); |
| 726 | return ast_sp; |
| 727 | } |
| 728 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 729 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 730 | } |
| 731 | return lldb::TypeSystemSP(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 734 | void ClangASTContext::EnumerateSupportedLanguages( |
| 735 | std::set<lldb::LanguageType> &languages_for_types, |
| 736 | std::set<lldb::LanguageType> &languages_for_expressions) { |
| 737 | static std::vector<lldb::LanguageType> s_supported_languages_for_types( |
| 738 | {lldb::eLanguageTypeC89, lldb::eLanguageTypeC, lldb::eLanguageTypeC11, |
| 739 | lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeC99, |
| 740 | lldb::eLanguageTypeObjC, lldb::eLanguageTypeObjC_plus_plus, |
| 741 | lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11, |
| 742 | lldb::eLanguageTypeC11, lldb::eLanguageTypeC_plus_plus_14}); |
| 743 | |
| 744 | static std::vector<lldb::LanguageType> s_supported_languages_for_expressions( |
| 745 | {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC_plus_plus, |
| 746 | lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11, |
| 747 | lldb::eLanguageTypeC_plus_plus_14}); |
| 748 | |
| 749 | languages_for_types.insert(s_supported_languages_for_types.begin(), |
| 750 | s_supported_languages_for_types.end()); |
| 751 | languages_for_expressions.insert( |
| 752 | s_supported_languages_for_expressions.begin(), |
| 753 | s_supported_languages_for_expressions.end()); |
Enrico Granata | 5d84a69 | 2014-08-19 21:46:37 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 756 | void ClangASTContext::Initialize() { |
| 757 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 758 | "clang base AST context plug-in", |
| 759 | CreateInstance, EnumerateSupportedLanguages); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 762 | void ClangASTContext::Terminate() { |
| 763 | PluginManager::UnregisterPlugin(CreateInstance); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 766 | void ClangASTContext::Finalize() { |
| 767 | if (m_ast_ap.get()) { |
| 768 | GetASTMap().Erase(m_ast_ap.get()); |
| 769 | if (!m_ast_owned) |
| 770 | m_ast_ap.release(); |
| 771 | } |
| 772 | |
| 773 | m_builtins_ap.reset(); |
| 774 | m_selector_table_ap.reset(); |
| 775 | m_identifier_table_ap.reset(); |
| 776 | m_target_info_ap.reset(); |
| 777 | m_target_options_rp.reset(); |
| 778 | m_diagnostics_engine_ap.reset(); |
| 779 | m_source_manager_ap.reset(); |
| 780 | m_language_options_ap.reset(); |
| 781 | m_ast_ap.reset(); |
| 782 | m_scratch_ast_source_ap.reset(); |
| 783 | } |
| 784 | |
| 785 | void ClangASTContext::Clear() { |
| 786 | m_ast_ap.reset(); |
| 787 | m_language_options_ap.reset(); |
| 788 | m_source_manager_ap.reset(); |
| 789 | m_diagnostics_engine_ap.reset(); |
| 790 | m_target_options_rp.reset(); |
| 791 | m_target_info_ap.reset(); |
| 792 | m_identifier_table_ap.reset(); |
| 793 | m_selector_table_ap.reset(); |
| 794 | m_builtins_ap.reset(); |
| 795 | m_pointer_byte_size = 0; |
| 796 | } |
| 797 | |
| 798 | const char *ClangASTContext::GetTargetTriple() { |
| 799 | return m_target_triple.c_str(); |
| 800 | } |
| 801 | |
| 802 | void ClangASTContext::SetTargetTriple(const char *target_triple) { |
| 803 | Clear(); |
| 804 | m_target_triple.assign(target_triple); |
| 805 | } |
| 806 | |
| 807 | void ClangASTContext::SetArchitecture(const ArchSpec &arch) { |
| 808 | SetTargetTriple(arch.GetTriple().str().c_str()); |
| 809 | } |
| 810 | |
| 811 | bool ClangASTContext::HasExternalSource() { |
| 812 | ASTContext *ast = getASTContext(); |
| 813 | if (ast) |
| 814 | return ast->getExternalSource() != nullptr; |
| 815 | return false; |
| 816 | } |
| 817 | |
| 818 | void ClangASTContext::SetExternalSource( |
| 819 | llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap) { |
| 820 | ASTContext *ast = getASTContext(); |
| 821 | if (ast) { |
| 822 | ast->setExternalSource(ast_source_ap); |
| 823 | ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 824 | } |
| 825 | } |
| 826 | |
| 827 | void ClangASTContext::RemoveExternalSource() { |
| 828 | ASTContext *ast = getASTContext(); |
| 829 | |
| 830 | if (ast) { |
| 831 | llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap; |
| 832 | ast->setExternalSource(empty_ast_source_ap); |
| 833 | ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 834 | } |
| 835 | } |
| 836 | |
| 837 | void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) { |
| 838 | if (!m_ast_owned) { |
| 839 | m_ast_ap.release(); |
| 840 | } |
| 841 | m_ast_owned = false; |
| 842 | m_ast_ap.reset(ast_ctx); |
| 843 | GetASTMap().Insert(ast_ctx, this); |
| 844 | } |
| 845 | |
| 846 | ASTContext *ClangASTContext::getASTContext() { |
| 847 | if (m_ast_ap.get() == nullptr) { |
| 848 | m_ast_owned = true; |
| 849 | m_ast_ap.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(), |
| 850 | *getIdentifierTable(), *getSelectorTable(), |
| 851 | *getBuiltinContext())); |
| 852 | |
| 853 | m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false); |
| 854 | |
| 855 | // 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] | 856 | // the target for an architecture isn't enabled in the llvm/clang that we |
| 857 | // built |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 858 | TargetInfo *target_info = getTargetInfo(); |
| 859 | if (target_info) |
| 860 | m_ast_ap->InitBuiltinTypes(*target_info); |
| 861 | |
| 862 | if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) { |
| 863 | m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage(); |
| 864 | // m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 865 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 866 | |
| 867 | GetASTMap().Insert(m_ast_ap.get(), this); |
| 868 | |
| 869 | llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap( |
| 870 | new ClangExternalASTSourceCallbacks( |
| 871 | ClangASTContext::CompleteTagDecl, |
| 872 | ClangASTContext::CompleteObjCInterfaceDecl, nullptr, |
| 873 | ClangASTContext::LayoutRecordType, this)); |
| 874 | SetExternalSource(ast_source_ap); |
| 875 | } |
| 876 | return m_ast_ap.get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 879 | ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) { |
| 880 | ClangASTContext *clang_ast = GetASTMap().Lookup(ast); |
| 881 | return clang_ast; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 884 | Builtin::Context *ClangASTContext::getBuiltinContext() { |
| 885 | if (m_builtins_ap.get() == nullptr) |
| 886 | m_builtins_ap.reset(new Builtin::Context()); |
| 887 | return m_builtins_ap.get(); |
Sean Callanan | 79439e8 | 2010-11-18 02:56:27 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 890 | IdentifierTable *ClangASTContext::getIdentifierTable() { |
| 891 | if (m_identifier_table_ap.get() == nullptr) |
| 892 | m_identifier_table_ap.reset( |
| 893 | new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr)); |
| 894 | return m_identifier_table_ap.get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 897 | LangOptions *ClangASTContext::getLanguageOptions() { |
| 898 | if (m_language_options_ap.get() == nullptr) { |
| 899 | m_language_options_ap.reset(new LangOptions()); |
Richard Smith | 8186cd4 | 2017-04-26 22:10:53 +0000 | [diff] [blame] | 900 | ParseLangArgs(*m_language_options_ap, InputKind::ObjCXX, GetTargetTriple()); |
| 901 | // InitializeLangOptions(*m_language_options_ap, InputKind::ObjCXX); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 902 | } |
| 903 | return m_language_options_ap.get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 906 | SelectorTable *ClangASTContext::getSelectorTable() { |
| 907 | if (m_selector_table_ap.get() == nullptr) |
| 908 | m_selector_table_ap.reset(new SelectorTable()); |
| 909 | return m_selector_table_ap.get(); |
Greg Clayton | fe68904 | 2015-11-10 17:47:04 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 912 | clang::FileManager *ClangASTContext::getFileManager() { |
| 913 | if (m_file_manager_ap.get() == nullptr) { |
| 914 | clang::FileSystemOptions file_system_options; |
| 915 | m_file_manager_ap.reset(new clang::FileManager(file_system_options)); |
| 916 | } |
| 917 | return m_file_manager_ap.get(); |
| 918 | } |
| 919 | |
| 920 | clang::SourceManager *ClangASTContext::getSourceManager() { |
| 921 | if (m_source_manager_ap.get() == nullptr) |
| 922 | m_source_manager_ap.reset( |
| 923 | new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager())); |
| 924 | return m_source_manager_ap.get(); |
| 925 | } |
| 926 | |
| 927 | clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() { |
| 928 | if (m_diagnostics_engine_ap.get() == nullptr) { |
| 929 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs()); |
| 930 | m_diagnostics_engine_ap.reset( |
| 931 | new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions())); |
| 932 | } |
| 933 | return m_diagnostics_engine_ap.get(); |
| 934 | } |
| 935 | |
| 936 | clang::MangleContext *ClangASTContext::getMangleContext() { |
| 937 | if (m_mangle_ctx_ap.get() == nullptr) |
| 938 | m_mangle_ctx_ap.reset(getASTContext()->createMangleContext()); |
| 939 | return m_mangle_ctx_ap.get(); |
| 940 | } |
| 941 | |
| 942 | class NullDiagnosticConsumer : public DiagnosticConsumer { |
Sean Callanan | 7fddd4c | 2010-12-11 00:08:56 +0000 | [diff] [blame] | 943 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 944 | NullDiagnosticConsumer() { |
| 945 | m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS); |
| 946 | } |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 947 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 948 | void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| 949 | const clang::Diagnostic &info) { |
| 950 | if (m_log) { |
| 951 | llvm::SmallVector<char, 32> diag_str(10); |
| 952 | info.FormatDiagnostic(diag_str); |
| 953 | diag_str.push_back('\0'); |
| 954 | m_log->Printf("Compiler diagnostic: %s\n", diag_str.data()); |
Sean Callanan | 7fddd4c | 2010-12-11 00:08:56 +0000 | [diff] [blame] | 955 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const { |
| 959 | return new NullDiagnosticConsumer(); |
| 960 | } |
| 961 | |
Sean Callanan | 7fddd4c | 2010-12-11 00:08:56 +0000 | [diff] [blame] | 962 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 963 | Log *m_log; |
Sean Callanan | 7fddd4c | 2010-12-11 00:08:56 +0000 | [diff] [blame] | 964 | }; |
| 965 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 966 | DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() { |
| 967 | if (m_diagnostic_consumer_ap.get() == nullptr) |
| 968 | m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer); |
| 969 | |
| 970 | return m_diagnostic_consumer_ap.get(); |
Sean Callanan | 7fddd4c | 2010-12-11 00:08:56 +0000 | [diff] [blame] | 971 | } |
| 972 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 973 | std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() { |
| 974 | if (m_target_options_rp.get() == nullptr && !m_target_triple.empty()) { |
| 975 | m_target_options_rp = std::make_shared<clang::TargetOptions>(); |
| 976 | if (m_target_options_rp.get() != nullptr) |
| 977 | m_target_options_rp->Triple = m_target_triple; |
| 978 | } |
| 979 | return m_target_options_rp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 982 | TargetInfo *ClangASTContext::getTargetInfo() { |
| 983 | // target_triple should be something like "x86_64-apple-macosx" |
| 984 | if (m_target_info_ap.get() == nullptr && !m_target_triple.empty()) |
| 985 | m_target_info_ap.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), |
| 986 | getTargetOptions())); |
| 987 | return m_target_info_ap.get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | #pragma mark Basic Types |
| 991 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 992 | static inline bool QualTypeMatchesBitSize(const uint64_t bit_size, |
| 993 | ASTContext *ast, QualType qual_type) { |
| 994 | uint64_t qual_type_bit_size = ast->getTypeSize(qual_type); |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 995 | return qual_type_bit_size == bit_size; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 996 | } |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 997 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 998 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 999 | ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding, |
| 1000 | size_t bit_size) { |
| 1001 | return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize( |
| 1002 | getASTContext(), encoding, bit_size); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1005 | CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize( |
| 1006 | ASTContext *ast, Encoding encoding, uint32_t bit_size) { |
| 1007 | if (!ast) |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 1008 | return CompilerType(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1009 | switch (encoding) { |
| 1010 | case eEncodingInvalid: |
| 1011 | if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy)) |
| 1012 | return CompilerType(ast, ast->VoidPtrTy); |
| 1013 | break; |
| 1014 | |
| 1015 | case eEncodingUint: |
| 1016 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy)) |
| 1017 | return CompilerType(ast, ast->UnsignedCharTy); |
| 1018 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy)) |
| 1019 | return CompilerType(ast, ast->UnsignedShortTy); |
| 1020 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy)) |
| 1021 | return CompilerType(ast, ast->UnsignedIntTy); |
| 1022 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy)) |
| 1023 | return CompilerType(ast, ast->UnsignedLongTy); |
| 1024 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy)) |
| 1025 | return CompilerType(ast, ast->UnsignedLongLongTy); |
| 1026 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty)) |
| 1027 | return CompilerType(ast, ast->UnsignedInt128Ty); |
| 1028 | break; |
| 1029 | |
| 1030 | case eEncodingSint: |
| 1031 | if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy)) |
| 1032 | return CompilerType(ast, ast->SignedCharTy); |
| 1033 | if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy)) |
| 1034 | return CompilerType(ast, ast->ShortTy); |
| 1035 | if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy)) |
| 1036 | return CompilerType(ast, ast->IntTy); |
| 1037 | if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy)) |
| 1038 | return CompilerType(ast, ast->LongTy); |
| 1039 | if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy)) |
| 1040 | return CompilerType(ast, ast->LongLongTy); |
| 1041 | if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty)) |
| 1042 | return CompilerType(ast, ast->Int128Ty); |
| 1043 | break; |
| 1044 | |
| 1045 | case eEncodingIEEE754: |
| 1046 | if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy)) |
| 1047 | return CompilerType(ast, ast->FloatTy); |
| 1048 | if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy)) |
| 1049 | return CompilerType(ast, ast->DoubleTy); |
| 1050 | if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy)) |
| 1051 | return CompilerType(ast, ast->LongDoubleTy); |
| 1052 | if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy)) |
| 1053 | return CompilerType(ast, ast->HalfTy); |
| 1054 | break; |
| 1055 | |
| 1056 | case eEncodingVector: |
| 1057 | // Sanity check that bit_size is a multiple of 8's. |
| 1058 | if (bit_size && !(bit_size & 0x7u)) |
| 1059 | return CompilerType( |
| 1060 | ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8)); |
| 1061 | break; |
| 1062 | } |
| 1063 | |
| 1064 | return CompilerType(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1067 | lldb::BasicType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1068 | ClangASTContext::GetBasicTypeEnumeration(const ConstString &name) { |
| 1069 | if (name) { |
| 1070 | typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap; |
| 1071 | static TypeNameToBasicTypeMap g_type_map; |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 1072 | static llvm::once_flag g_once_flag; |
| 1073 | llvm::call_once(g_once_flag, []() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1074 | // "void" |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 1075 | g_type_map.Append(ConstString("void"), eBasicTypeVoid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1076 | |
| 1077 | // "char" |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 1078 | g_type_map.Append(ConstString("char"), eBasicTypeChar); |
| 1079 | g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar); |
| 1080 | g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar); |
| 1081 | g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar); |
| 1082 | g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar); |
| 1083 | g_type_map.Append(ConstString("unsigned wchar_t"), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1084 | eBasicTypeUnsignedWChar); |
| 1085 | // "short" |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 1086 | g_type_map.Append(ConstString("short"), eBasicTypeShort); |
| 1087 | g_type_map.Append(ConstString("short int"), eBasicTypeShort); |
| 1088 | g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort); |
| 1089 | g_type_map.Append(ConstString("unsigned short int"), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1090 | eBasicTypeUnsignedShort); |
| 1091 | |
| 1092 | // "int" |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 1093 | g_type_map.Append(ConstString("int"), eBasicTypeInt); |
| 1094 | g_type_map.Append(ConstString("signed int"), eBasicTypeInt); |
| 1095 | g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt); |
| 1096 | g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1097 | |
| 1098 | // "long" |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 1099 | g_type_map.Append(ConstString("long"), eBasicTypeLong); |
| 1100 | g_type_map.Append(ConstString("long int"), eBasicTypeLong); |
| 1101 | g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong); |
| 1102 | g_type_map.Append(ConstString("unsigned long int"), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1103 | eBasicTypeUnsignedLong); |
| 1104 | |
| 1105 | // "long long" |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 1106 | g_type_map.Append(ConstString("long long"), eBasicTypeLongLong); |
| 1107 | g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong); |
| 1108 | g_type_map.Append(ConstString("unsigned long long"), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1109 | eBasicTypeUnsignedLongLong); |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 1110 | g_type_map.Append(ConstString("unsigned long long int"), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1111 | eBasicTypeUnsignedLongLong); |
| 1112 | |
| 1113 | // "int128" |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 1114 | g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128); |
| 1115 | g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1116 | |
| 1117 | // Miscellaneous |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 1118 | g_type_map.Append(ConstString("bool"), eBasicTypeBool); |
| 1119 | g_type_map.Append(ConstString("float"), eBasicTypeFloat); |
| 1120 | g_type_map.Append(ConstString("double"), eBasicTypeDouble); |
| 1121 | g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble); |
| 1122 | g_type_map.Append(ConstString("id"), eBasicTypeObjCID); |
| 1123 | g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel); |
| 1124 | g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1125 | g_type_map.Sort(); |
| 1126 | }); |
| 1127 | |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 1128 | return g_type_map.Find(name, eBasicTypeInvalid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1129 | } |
| 1130 | return eBasicTypeInvalid; |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1133 | CompilerType ClangASTContext::GetBasicType(ASTContext *ast, |
| 1134 | const ConstString &name) { |
| 1135 | if (ast) { |
| 1136 | lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name); |
| 1137 | return ClangASTContext::GetBasicType(ast, basic_type); |
| 1138 | } |
| 1139 | return CompilerType(); |
| 1140 | } |
| 1141 | |
| 1142 | uint32_t ClangASTContext::GetPointerByteSize() { |
| 1143 | if (m_pointer_byte_size == 0) |
Adrian Prantl | d963a7c | 2019-01-15 18:07:52 +0000 | [diff] [blame^] | 1144 | if (auto size = GetBasicType(lldb::eBasicTypeVoid) |
| 1145 | .GetPointerType() |
| 1146 | .GetByteSize(nullptr)) |
| 1147 | m_pointer_byte_size = *size; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1148 | return m_pointer_byte_size; |
| 1149 | } |
| 1150 | |
| 1151 | CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) { |
| 1152 | return GetBasicType(getASTContext(), basic_type); |
| 1153 | } |
| 1154 | |
| 1155 | CompilerType ClangASTContext::GetBasicType(ASTContext *ast, |
| 1156 | lldb::BasicType basic_type) { |
| 1157 | if (!ast) |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 1158 | return CompilerType(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1159 | lldb::opaque_compiler_type_t clang_type = |
| 1160 | GetOpaqueCompilerType(ast, basic_type); |
| 1161 | |
| 1162 | if (clang_type) |
| 1163 | return CompilerType(GetASTContext(ast), clang_type); |
| 1164 | return CompilerType(); |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1167 | CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize( |
| 1168 | const char *type_name, uint32_t dw_ate, uint32_t bit_size) { |
| 1169 | ASTContext *ast = getASTContext(); |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1170 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1171 | #define streq(a, b) strcmp(a, b) == 0 |
| 1172 | assert(ast != nullptr); |
| 1173 | if (ast) { |
| 1174 | switch (dw_ate) { |
| 1175 | default: |
| 1176 | break; |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1177 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1178 | case DW_ATE_address: |
| 1179 | if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy)) |
| 1180 | return CompilerType(ast, ast->VoidPtrTy); |
| 1181 | break; |
Zachary Turner | 9d8a97e | 2016-04-01 23:20:35 +0000 | [diff] [blame] | 1182 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1183 | case DW_ATE_boolean: |
| 1184 | if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy)) |
| 1185 | return CompilerType(ast, ast->BoolTy); |
| 1186 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy)) |
| 1187 | return CompilerType(ast, ast->UnsignedCharTy); |
| 1188 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy)) |
| 1189 | return CompilerType(ast, ast->UnsignedShortTy); |
| 1190 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy)) |
| 1191 | return CompilerType(ast, ast->UnsignedIntTy); |
| 1192 | break; |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1193 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1194 | case DW_ATE_lo_user: |
| 1195 | // This has been seen to mean DW_AT_complex_integer |
| 1196 | if (type_name) { |
| 1197 | if (::strstr(type_name, "complex")) { |
| 1198 | CompilerType complex_int_clang_type = |
| 1199 | GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed, |
| 1200 | bit_size / 2); |
| 1201 | return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType( |
| 1202 | complex_int_clang_type))); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1203 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1204 | } |
| 1205 | break; |
| 1206 | |
| 1207 | case DW_ATE_complex_float: |
| 1208 | if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy)) |
| 1209 | return CompilerType(ast, ast->FloatComplexTy); |
| 1210 | else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy)) |
| 1211 | return CompilerType(ast, ast->DoubleComplexTy); |
| 1212 | else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy)) |
| 1213 | return CompilerType(ast, ast->LongDoubleComplexTy); |
| 1214 | else { |
| 1215 | CompilerType complex_float_clang_type = |
| 1216 | GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float, |
| 1217 | bit_size / 2); |
| 1218 | return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType( |
| 1219 | complex_float_clang_type))); |
| 1220 | } |
| 1221 | break; |
| 1222 | |
| 1223 | case DW_ATE_float: |
| 1224 | if (streq(type_name, "float") && |
| 1225 | QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy)) |
| 1226 | return CompilerType(ast, ast->FloatTy); |
| 1227 | if (streq(type_name, "double") && |
| 1228 | QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy)) |
| 1229 | return CompilerType(ast, ast->DoubleTy); |
| 1230 | if (streq(type_name, "long double") && |
| 1231 | QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy)) |
| 1232 | return CompilerType(ast, ast->LongDoubleTy); |
| 1233 | // Fall back to not requiring a name match |
| 1234 | if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy)) |
| 1235 | return CompilerType(ast, ast->FloatTy); |
| 1236 | if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy)) |
| 1237 | return CompilerType(ast, ast->DoubleTy); |
| 1238 | if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy)) |
| 1239 | return CompilerType(ast, ast->LongDoubleTy); |
| 1240 | if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy)) |
| 1241 | return CompilerType(ast, ast->HalfTy); |
| 1242 | break; |
| 1243 | |
| 1244 | case DW_ATE_signed: |
| 1245 | if (type_name) { |
| 1246 | if (streq(type_name, "wchar_t") && |
| 1247 | QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) && |
| 1248 | (getTargetInfo() && |
| 1249 | TargetInfo::isTypeSigned(getTargetInfo()->getWCharType()))) |
| 1250 | return CompilerType(ast, ast->WCharTy); |
| 1251 | if (streq(type_name, "void") && |
| 1252 | QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy)) |
| 1253 | return CompilerType(ast, ast->VoidTy); |
| 1254 | if (strstr(type_name, "long long") && |
| 1255 | QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy)) |
| 1256 | return CompilerType(ast, ast->LongLongTy); |
| 1257 | if (strstr(type_name, "long") && |
| 1258 | QualTypeMatchesBitSize(bit_size, ast, ast->LongTy)) |
| 1259 | return CompilerType(ast, ast->LongTy); |
| 1260 | if (strstr(type_name, "short") && |
| 1261 | QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy)) |
| 1262 | return CompilerType(ast, ast->ShortTy); |
| 1263 | if (strstr(type_name, "char")) { |
| 1264 | if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy)) |
| 1265 | return CompilerType(ast, ast->CharTy); |
| 1266 | if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy)) |
| 1267 | return CompilerType(ast, ast->SignedCharTy); |
| 1268 | } |
| 1269 | if (strstr(type_name, "int")) { |
| 1270 | if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy)) |
| 1271 | return CompilerType(ast, ast->IntTy); |
| 1272 | if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty)) |
| 1273 | return CompilerType(ast, ast->Int128Ty); |
| 1274 | } |
| 1275 | } |
| 1276 | // We weren't able to match up a type name, just search by size |
| 1277 | if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy)) |
| 1278 | return CompilerType(ast, ast->CharTy); |
| 1279 | if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy)) |
| 1280 | return CompilerType(ast, ast->ShortTy); |
| 1281 | if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy)) |
| 1282 | return CompilerType(ast, ast->IntTy); |
| 1283 | if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy)) |
| 1284 | return CompilerType(ast, ast->LongTy); |
| 1285 | if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy)) |
| 1286 | return CompilerType(ast, ast->LongLongTy); |
| 1287 | if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty)) |
| 1288 | return CompilerType(ast, ast->Int128Ty); |
| 1289 | break; |
| 1290 | |
| 1291 | case DW_ATE_signed_char: |
| 1292 | if (ast->getLangOpts().CharIsSigned && type_name && |
| 1293 | streq(type_name, "char")) { |
| 1294 | if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy)) |
| 1295 | return CompilerType(ast, ast->CharTy); |
| 1296 | } |
| 1297 | if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy)) |
| 1298 | return CompilerType(ast, ast->SignedCharTy); |
| 1299 | break; |
| 1300 | |
| 1301 | case DW_ATE_unsigned: |
| 1302 | if (type_name) { |
| 1303 | if (streq(type_name, "wchar_t")) { |
| 1304 | if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) { |
| 1305 | if (!(getTargetInfo() && |
| 1306 | TargetInfo::isTypeSigned(getTargetInfo()->getWCharType()))) |
| 1307 | return CompilerType(ast, ast->WCharTy); |
| 1308 | } |
| 1309 | } |
| 1310 | if (strstr(type_name, "long long")) { |
| 1311 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy)) |
| 1312 | return CompilerType(ast, ast->UnsignedLongLongTy); |
| 1313 | } else if (strstr(type_name, "long")) { |
| 1314 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy)) |
| 1315 | return CompilerType(ast, ast->UnsignedLongTy); |
| 1316 | } else if (strstr(type_name, "short")) { |
| 1317 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy)) |
| 1318 | return CompilerType(ast, ast->UnsignedShortTy); |
| 1319 | } else if (strstr(type_name, "char")) { |
| 1320 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy)) |
| 1321 | return CompilerType(ast, ast->UnsignedCharTy); |
| 1322 | } else if (strstr(type_name, "int")) { |
| 1323 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy)) |
| 1324 | return CompilerType(ast, ast->UnsignedIntTy); |
| 1325 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty)) |
| 1326 | return CompilerType(ast, ast->UnsignedInt128Ty); |
| 1327 | } |
| 1328 | } |
| 1329 | // We weren't able to match up a type name, just search by size |
| 1330 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy)) |
| 1331 | return CompilerType(ast, ast->UnsignedCharTy); |
| 1332 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy)) |
| 1333 | return CompilerType(ast, ast->UnsignedShortTy); |
| 1334 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy)) |
| 1335 | return CompilerType(ast, ast->UnsignedIntTy); |
| 1336 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy)) |
| 1337 | return CompilerType(ast, ast->UnsignedLongTy); |
| 1338 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy)) |
| 1339 | return CompilerType(ast, ast->UnsignedLongLongTy); |
| 1340 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty)) |
| 1341 | return CompilerType(ast, ast->UnsignedInt128Ty); |
| 1342 | break; |
| 1343 | |
| 1344 | case DW_ATE_unsigned_char: |
| 1345 | if (!ast->getLangOpts().CharIsSigned && type_name && |
| 1346 | streq(type_name, "char")) { |
| 1347 | if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy)) |
| 1348 | return CompilerType(ast, ast->CharTy); |
| 1349 | } |
| 1350 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy)) |
| 1351 | return CompilerType(ast, ast->UnsignedCharTy); |
| 1352 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy)) |
| 1353 | return CompilerType(ast, ast->UnsignedShortTy); |
| 1354 | break; |
| 1355 | |
| 1356 | case DW_ATE_imaginary_float: |
| 1357 | break; |
| 1358 | |
| 1359 | case DW_ATE_UTF: |
| 1360 | if (type_name) { |
| 1361 | if (streq(type_name, "char16_t")) { |
| 1362 | return CompilerType(ast, ast->Char16Ty); |
| 1363 | } else if (streq(type_name, "char32_t")) { |
| 1364 | return CompilerType(ast, ast->Char32Ty); |
| 1365 | } |
| 1366 | } |
| 1367 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1368 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1369 | } |
| 1370 | // This assert should fire for anything that we don't catch above so we know |
| 1371 | // to fix any issues we run into. |
| 1372 | if (type_name) { |
| 1373 | Host::SystemLog(Host::eSystemLogError, "error: need to add support for " |
| 1374 | "DW_TAG_base_type '%s' encoded with " |
| 1375 | "DW_ATE = 0x%x, bit_size = %u\n", |
| 1376 | type_name, dw_ate, bit_size); |
| 1377 | } else { |
| 1378 | Host::SystemLog(Host::eSystemLogError, "error: need to add support for " |
| 1379 | "DW_TAG_base_type encoded with " |
| 1380 | "DW_ATE = 0x%x, bit_size = %u\n", |
| 1381 | dw_ate, bit_size); |
| 1382 | } |
| 1383 | return CompilerType(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1386 | CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) { |
| 1387 | if (ast) |
| 1388 | return CompilerType(ast, ast->UnknownAnyTy); |
| 1389 | return CompilerType(); |
Sean Callanan | 7750226 | 2011-05-12 23:54:16 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1392 | CompilerType ClangASTContext::GetCStringType(bool is_const) { |
| 1393 | ASTContext *ast = getASTContext(); |
| 1394 | QualType char_type(ast->CharTy); |
| 1395 | |
| 1396 | if (is_const) |
| 1397 | char_type.addConst(); |
| 1398 | |
| 1399 | return CompilerType(ast, ast->getPointerType(char_type)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Zachary Turner | 115209e | 2018-11-05 19:25:39 +0000 | [diff] [blame] | 1402 | clang::DeclContext * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1403 | ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) { |
| 1404 | return ast->getTranslationUnitDecl(); |
Sean Callanan | 09ab4b7 | 2011-11-30 22:11:59 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1407 | clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast, |
| 1408 | clang::Decl *source_decl) { |
| 1409 | FileSystemOptions file_system_options; |
| 1410 | FileManager file_manager(file_system_options); |
| 1411 | ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false); |
| 1412 | |
| 1413 | return importer.Import(source_decl); |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1416 | bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2, |
| 1417 | bool ignore_qualifiers) { |
| 1418 | ClangASTContext *ast = |
| 1419 | llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem()); |
| 1420 | if (!ast || ast != type2.GetTypeSystem()) |
| 1421 | return false; |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1422 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1423 | if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType()) |
| 1424 | return true; |
Greg Clayton | 55995eb | 2012-04-06 17:38:55 +0000 | [diff] [blame] | 1425 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1426 | QualType type1_qual = ClangUtil::GetQualType(type1); |
| 1427 | QualType type2_qual = ClangUtil::GetQualType(type2); |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 1428 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1429 | if (ignore_qualifiers) { |
| 1430 | type1_qual = type1_qual.getUnqualifiedType(); |
| 1431 | type2_qual = type2_qual.getUnqualifiedType(); |
| 1432 | } |
| 1433 | |
| 1434 | return ast->getASTContext()->hasSameType(type1_qual, type2_qual); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1437 | CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) { |
| 1438 | if (clang::ObjCInterfaceDecl *interface_decl = |
| 1439 | llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) |
| 1440 | return GetTypeForDecl(interface_decl); |
| 1441 | if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) |
| 1442 | return GetTypeForDecl(tag_decl); |
| 1443 | return CompilerType(); |
Sean Callanan | 9998acd | 2014-12-05 01:21:59 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1446 | CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1447 | // No need to call the getASTContext() accessor (which can create the AST if |
| 1448 | // 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] | 1449 | // AST if our AST didn't already exist... |
| 1450 | ASTContext *ast = &decl->getASTContext(); |
| 1451 | if (ast) |
| 1452 | return CompilerType(ast, ast->getTagDeclType(decl)); |
| 1453 | return CompilerType(); |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1456 | CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1457 | // No need to call the getASTContext() accessor (which can create the AST if |
| 1458 | // 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] | 1459 | // AST if our AST didn't already exist... |
| 1460 | ASTContext *ast = &decl->getASTContext(); |
| 1461 | if (ast) |
| 1462 | return CompilerType(ast, ast->getObjCInterfaceType(decl)); |
| 1463 | return CompilerType(); |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1466 | #pragma mark Structure, Unions, Classes |
| 1467 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1468 | CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx, |
| 1469 | AccessType access_type, |
| 1470 | const char *name, int kind, |
| 1471 | LanguageType language, |
| 1472 | ClangASTMetadata *metadata) { |
| 1473 | ASTContext *ast = getASTContext(); |
| 1474 | assert(ast != nullptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1475 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1476 | if (decl_ctx == nullptr) |
| 1477 | decl_ctx = ast->getTranslationUnitDecl(); |
Greg Clayton | 9e40956 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 1478 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1479 | if (language == eLanguageTypeObjC || |
| 1480 | language == eLanguageTypeObjC_plus_plus) { |
| 1481 | bool isForwardDecl = true; |
| 1482 | bool isInternal = false; |
| 1483 | return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata); |
| 1484 | } |
Greg Clayton | 9e40956 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 1485 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1486 | // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1487 | // we will need to update this code. I was told to currently always use the |
| 1488 | // CXXRecordDecl class since we often don't know from debug information if |
| 1489 | // 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] | 1490 | // complete definition just in case. |
Greg Clayton | c4ffd66 | 2013-03-08 01:37:30 +0000 | [diff] [blame] | 1491 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1492 | bool is_anonymous = (!name) || (!name[0]); |
Greg Clayton | c4ffd66 | 2013-03-08 01:37:30 +0000 | [diff] [blame] | 1493 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1494 | CXXRecordDecl *decl = CXXRecordDecl::Create( |
| 1495 | *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(), |
| 1496 | SourceLocation(), is_anonymous ? nullptr : &ast->Idents.get(name)); |
| 1497 | |
| 1498 | if (is_anonymous) |
| 1499 | decl->setAnonymousStructOrUnion(true); |
| 1500 | |
| 1501 | if (decl) { |
| 1502 | if (metadata) |
| 1503 | SetMetadata(ast, decl, *metadata); |
| 1504 | |
| 1505 | if (access_type != eAccessNone) |
| 1506 | decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type)); |
| 1507 | |
| 1508 | if (decl_ctx) |
| 1509 | decl_ctx->addDecl(decl); |
| 1510 | |
| 1511 | return CompilerType(ast, ast->getTagDeclType(decl)); |
| 1512 | } |
| 1513 | return CompilerType(); |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1516 | namespace { |
| 1517 | bool IsValueParam(const clang::TemplateArgument &argument) { |
| 1518 | return argument.getKind() == TemplateArgument::Integral; |
| 1519 | } |
| 1520 | } |
| 1521 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1522 | static TemplateParameterList *CreateTemplateParameterList( |
| 1523 | ASTContext *ast, |
| 1524 | const ClangASTContext::TemplateParameterInfos &template_param_infos, |
| 1525 | llvm::SmallVector<NamedDecl *, 8> &template_param_decls) { |
| 1526 | const bool parameter_pack = false; |
| 1527 | const bool is_typename = false; |
| 1528 | const unsigned depth = 0; |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1529 | const size_t num_template_params = template_param_infos.args.size(); |
| 1530 | DeclContext *const decl_context = |
| 1531 | ast->getTranslationUnitDecl(); // Is this the right decl context?, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1532 | for (size_t i = 0; i < num_template_params; ++i) { |
| 1533 | const char *name = template_param_infos.names[i]; |
Greg Clayton | 3c2e3ae | 2012-02-06 06:42:51 +0000 | [diff] [blame] | 1534 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1535 | IdentifierInfo *identifier_info = nullptr; |
| 1536 | if (name && name[0]) |
| 1537 | identifier_info = &ast->Idents.get(name); |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1538 | if (IsValueParam(template_param_infos.args[i])) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1539 | template_param_decls.push_back(NonTypeTemplateParmDecl::Create( |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1540 | *ast, decl_context, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1541 | SourceLocation(), SourceLocation(), depth, i, identifier_info, |
| 1542 | template_param_infos.args[i].getIntegralType(), parameter_pack, |
| 1543 | nullptr)); |
| 1544 | |
| 1545 | } else { |
| 1546 | template_param_decls.push_back(TemplateTypeParmDecl::Create( |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1547 | *ast, decl_context, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1548 | SourceLocation(), SourceLocation(), depth, i, identifier_info, |
| 1549 | is_typename, parameter_pack)); |
| 1550 | } |
| 1551 | } |
Eugene Zemtsov | a9d928c | 2017-09-29 03:15:08 +0000 | [diff] [blame] | 1552 | |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1553 | if (template_param_infos.packed_args && |
| 1554 | template_param_infos.packed_args->args.size()) { |
| 1555 | IdentifierInfo *identifier_info = nullptr; |
| 1556 | if (template_param_infos.pack_name && template_param_infos.pack_name[0]) |
| 1557 | identifier_info = &ast->Idents.get(template_param_infos.pack_name); |
| 1558 | const bool parameter_pack_true = true; |
| 1559 | if (IsValueParam(template_param_infos.packed_args->args[0])) { |
| 1560 | template_param_decls.push_back(NonTypeTemplateParmDecl::Create( |
| 1561 | *ast, decl_context, |
| 1562 | SourceLocation(), SourceLocation(), depth, num_template_params, |
| 1563 | identifier_info, |
| 1564 | template_param_infos.packed_args->args[0].getIntegralType(), |
| 1565 | parameter_pack_true, nullptr)); |
| 1566 | } else { |
| 1567 | template_param_decls.push_back(TemplateTypeParmDecl::Create( |
| 1568 | *ast, decl_context, |
| 1569 | SourceLocation(), SourceLocation(), depth, num_template_params, |
| 1570 | identifier_info, |
| 1571 | is_typename, parameter_pack_true)); |
| 1572 | } |
| 1573 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1574 | clang::Expr *const requires_clause = nullptr; // TODO: Concepts |
| 1575 | TemplateParameterList *template_param_list = TemplateParameterList::Create( |
| 1576 | *ast, SourceLocation(), SourceLocation(), template_param_decls, |
| 1577 | SourceLocation(), requires_clause); |
| 1578 | return template_param_list; |
Greg Clayton | 3c2e3ae | 2012-02-06 06:42:51 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1581 | clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl( |
| 1582 | clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl, |
| 1583 | const char *name, const TemplateParameterInfos &template_param_infos) { |
Adrian Prantl | d8f460e | 2018-05-02 16:55:16 +0000 | [diff] [blame] | 1584 | // /// Create a function template node. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1585 | ASTContext *ast = getASTContext(); |
Greg Clayton | 3c2e3ae | 2012-02-06 06:42:51 +0000 | [diff] [blame] | 1586 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1587 | llvm::SmallVector<NamedDecl *, 8> template_param_decls; |
Greg Clayton | 3c2e3ae | 2012-02-06 06:42:51 +0000 | [diff] [blame] | 1588 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1589 | TemplateParameterList *template_param_list = CreateTemplateParameterList( |
| 1590 | ast, template_param_infos, template_param_decls); |
| 1591 | FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create( |
| 1592 | *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(), |
| 1593 | template_param_list, func_decl); |
| 1594 | |
| 1595 | for (size_t i = 0, template_param_decl_count = template_param_decls.size(); |
| 1596 | i < template_param_decl_count; ++i) { |
| 1597 | // TODO: verify which decl context we should put template_param_decls into.. |
| 1598 | template_param_decls[i]->setDeclContext(func_decl); |
| 1599 | } |
| 1600 | |
| 1601 | return func_tmpl_decl; |
Greg Clayton | 3c2e3ae | 2012-02-06 06:42:51 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1604 | void ClangASTContext::CreateFunctionTemplateSpecializationInfo( |
| 1605 | FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl, |
| 1606 | const TemplateParameterInfos &infos) { |
| 1607 | TemplateArgumentList template_args(TemplateArgumentList::OnStack, infos.args); |
Greg Clayton | 3c2e3ae | 2012-02-06 06:42:51 +0000 | [diff] [blame] | 1608 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1609 | func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, &template_args, |
| 1610 | nullptr); |
Greg Clayton | 3c2e3ae | 2012-02-06 06:42:51 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1613 | ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl( |
| 1614 | DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name, |
| 1615 | int kind, const TemplateParameterInfos &template_param_infos) { |
| 1616 | ASTContext *ast = getASTContext(); |
Greg Clayton | 3c2e3ae | 2012-02-06 06:42:51 +0000 | [diff] [blame] | 1617 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1618 | ClassTemplateDecl *class_template_decl = nullptr; |
| 1619 | if (decl_ctx == nullptr) |
| 1620 | decl_ctx = ast->getTranslationUnitDecl(); |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1621 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1622 | IdentifierInfo &identifier_info = ast->Idents.get(class_name); |
| 1623 | DeclarationName decl_name(&identifier_info); |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1624 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1625 | clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name); |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1626 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1627 | for (NamedDecl *decl : result) { |
| 1628 | class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl); |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1629 | if (class_template_decl) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1630 | return class_template_decl; |
| 1631 | } |
| 1632 | |
| 1633 | llvm::SmallVector<NamedDecl *, 8> template_param_decls; |
| 1634 | |
| 1635 | TemplateParameterList *template_param_list = CreateTemplateParameterList( |
| 1636 | ast, template_param_infos, template_param_decls); |
| 1637 | |
| 1638 | CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create( |
| 1639 | *ast, (TagDecl::TagKind)kind, |
| 1640 | decl_ctx, // What decl context do we use here? TU? The actual decl |
| 1641 | // context? |
| 1642 | SourceLocation(), SourceLocation(), &identifier_info); |
| 1643 | |
| 1644 | for (size_t i = 0, template_param_decl_count = template_param_decls.size(); |
| 1645 | i < template_param_decl_count; ++i) { |
| 1646 | template_param_decls[i]->setDeclContext(template_cxx_decl); |
| 1647 | } |
| 1648 | |
| 1649 | // With templated classes, we say that a class is templated with |
| 1650 | // specializations, but that the bare class has no functions. |
| 1651 | // template_cxx_decl->startDefinition(); |
| 1652 | // template_cxx_decl->completeDefinition(); |
| 1653 | |
| 1654 | class_template_decl = ClassTemplateDecl::Create( |
| 1655 | *ast, |
| 1656 | decl_ctx, // What decl context do we use here? TU? The actual decl |
| 1657 | // context? |
Pavel Labath | 4294de3 | 2017-01-12 10:44:16 +0000 | [diff] [blame] | 1658 | SourceLocation(), decl_name, template_param_list, template_cxx_decl); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1659 | |
| 1660 | if (class_template_decl) { |
| 1661 | if (access_type != eAccessNone) |
| 1662 | class_template_decl->setAccess( |
| 1663 | ConvertAccessTypeToAccessSpecifier(access_type)); |
| 1664 | |
| 1665 | // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx)) |
| 1666 | // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl)); |
| 1667 | |
| 1668 | decl_ctx->addDecl(class_template_decl); |
| 1669 | |
Sean Callanan | 5e9e199 | 2011-10-26 01:06:27 +0000 | [diff] [blame] | 1670 | #ifdef LLDB_CONFIGURATION_DEBUG |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1671 | VerifyDecl(class_template_decl); |
Sean Callanan | 5e9e199 | 2011-10-26 01:06:27 +0000 | [diff] [blame] | 1672 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1673 | } |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1674 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1675 | return class_template_decl; |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
Frederic Riss | f4e7e52 | 2018-04-02 16:18:32 +0000 | [diff] [blame] | 1678 | TemplateTemplateParmDecl * |
| 1679 | ClangASTContext::CreateTemplateTemplateParmDecl(const char *template_name) { |
| 1680 | ASTContext *ast = getASTContext(); |
| 1681 | |
| 1682 | auto *decl_ctx = ast->getTranslationUnitDecl(); |
| 1683 | |
| 1684 | IdentifierInfo &identifier_info = ast->Idents.get(template_name); |
| 1685 | llvm::SmallVector<NamedDecl *, 8> template_param_decls; |
| 1686 | |
| 1687 | ClangASTContext::TemplateParameterInfos template_param_infos; |
| 1688 | TemplateParameterList *template_param_list = CreateTemplateParameterList( |
| 1689 | ast, template_param_infos, template_param_decls); |
| 1690 | |
| 1691 | // 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] | 1692 | // type that includes a template template argument. Only the name matters for |
| 1693 | // this purpose, so we use dummy values for the other characterisitcs of the |
| 1694 | // type. |
Frederic Riss | f4e7e52 | 2018-04-02 16:18:32 +0000 | [diff] [blame] | 1695 | return TemplateTemplateParmDecl::Create( |
| 1696 | *ast, decl_ctx, SourceLocation(), |
| 1697 | /*Depth*/ 0, /*Position*/ 0, |
| 1698 | /*IsParameterPack*/ false, &identifier_info, template_param_list); |
| 1699 | } |
| 1700 | |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1701 | ClassTemplateSpecializationDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1702 | ClangASTContext::CreateClassTemplateSpecializationDecl( |
| 1703 | DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind, |
| 1704 | const TemplateParameterInfos &template_param_infos) { |
| 1705 | ASTContext *ast = getASTContext(); |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1706 | llvm::SmallVector<clang::TemplateArgument, 2> args( |
| 1707 | template_param_infos.args.size() + |
| 1708 | (template_param_infos.packed_args ? 1 : 0)); |
| 1709 | std::copy(template_param_infos.args.begin(), template_param_infos.args.end(), |
| 1710 | args.begin()); |
| 1711 | if (template_param_infos.packed_args) { |
| 1712 | args[args.size() - 1] = TemplateArgument::CreatePackCopy( |
| 1713 | *ast, template_param_infos.packed_args->args); |
| 1714 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1715 | ClassTemplateSpecializationDecl *class_template_specialization_decl = |
| 1716 | ClassTemplateSpecializationDecl::Create( |
| 1717 | *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(), |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1718 | SourceLocation(), class_template_decl, args, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1719 | nullptr); |
| 1720 | |
| 1721 | class_template_specialization_decl->setSpecializationKind( |
| 1722 | TSK_ExplicitSpecialization); |
| 1723 | |
| 1724 | return class_template_specialization_decl; |
| 1725 | } |
| 1726 | |
| 1727 | CompilerType ClangASTContext::CreateClassTemplateSpecializationType( |
| 1728 | ClassTemplateSpecializationDecl *class_template_specialization_decl) { |
| 1729 | if (class_template_specialization_decl) { |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1730 | ASTContext *ast = getASTContext(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1731 | if (ast) |
| 1732 | return CompilerType( |
| 1733 | ast, ast->getTagDeclType(class_template_specialization_decl)); |
| 1734 | } |
| 1735 | return CompilerType(); |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1738 | static inline bool check_op_param(bool is_method, |
| 1739 | clang::OverloadedOperatorKind op_kind, |
| 1740 | bool unary, bool binary, |
| 1741 | uint32_t num_params) { |
| 1742 | // Special-case call since it can take any number of operands |
| 1743 | if (op_kind == OO_Call) |
| 1744 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1745 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1746 | // The parameter count doesn't include "this" |
| 1747 | if (is_method) |
| 1748 | ++num_params; |
| 1749 | if (num_params == 1) |
| 1750 | return unary; |
| 1751 | if (num_params == 2) |
| 1752 | return binary; |
| 1753 | else |
Greg Clayton | 090d098 | 2011-06-19 03:43:27 +0000 | [diff] [blame] | 1754 | return false; |
| 1755 | } |
Daniel Dunbar | dacdfb5 | 2011-10-31 22:50:57 +0000 | [diff] [blame] | 1756 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1757 | bool ClangASTContext::CheckOverloadedOperatorKindParameterCount( |
| 1758 | bool is_method, clang::OverloadedOperatorKind op_kind, |
| 1759 | uint32_t num_params) { |
| 1760 | switch (op_kind) { |
| 1761 | default: |
| 1762 | break; |
| 1763 | // C++ standard allows any number of arguments to new/delete |
| 1764 | case OO_New: |
| 1765 | case OO_Array_New: |
| 1766 | case OO_Delete: |
| 1767 | case OO_Array_Delete: |
| 1768 | return true; |
| 1769 | } |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 1770 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1771 | #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \ |
| 1772 | case OO_##Name: \ |
| 1773 | return check_op_param(is_method, op_kind, Unary, Binary, num_params); |
| 1774 | switch (op_kind) { |
Greg Clayton | 090d098 | 2011-06-19 03:43:27 +0000 | [diff] [blame] | 1775 | #include "clang/Basic/OperatorKinds.def" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1776 | default: |
| 1777 | break; |
| 1778 | } |
| 1779 | return false; |
Greg Clayton | 090d098 | 2011-06-19 03:43:27 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1782 | clang::AccessSpecifier |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1783 | ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs, |
| 1784 | clang::AccessSpecifier rhs) { |
| 1785 | // Make the access equal to the stricter of the field and the nested field's |
| 1786 | // access |
| 1787 | if (lhs == AS_none || rhs == AS_none) |
| 1788 | return AS_none; |
| 1789 | if (lhs == AS_private || rhs == AS_private) |
| 1790 | return AS_private; |
| 1791 | if (lhs == AS_protected || rhs == AS_protected) |
| 1792 | return AS_protected; |
| 1793 | return AS_public; |
Sean Callanan | e8c0cfb | 2012-03-02 01:03:45 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1796 | bool ClangASTContext::FieldIsBitfield(FieldDecl *field, |
| 1797 | uint32_t &bitfield_bit_size) { |
| 1798 | return FieldIsBitfield(getASTContext(), field, bitfield_bit_size); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1801 | bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field, |
| 1802 | uint32_t &bitfield_bit_size) { |
| 1803 | if (ast == nullptr || field == nullptr) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1804 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1805 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1806 | if (field->isBitField()) { |
| 1807 | Expr *bit_width_expr = field->getBitWidth(); |
| 1808 | if (bit_width_expr) { |
| 1809 | llvm::APSInt bit_width_apsint; |
| 1810 | if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) { |
| 1811 | bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1812 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1813 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1814 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1815 | } |
| 1816 | return false; |
| 1817 | } |
| 1818 | |
| 1819 | bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) { |
| 1820 | if (record_decl == nullptr) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1821 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1822 | |
| 1823 | if (!record_decl->field_empty()) |
| 1824 | return true; |
| 1825 | |
| 1826 | // No fields, lets check this is a CXX record and check the base classes |
| 1827 | const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); |
| 1828 | if (cxx_record_decl) { |
| 1829 | CXXRecordDecl::base_class_const_iterator base_class, base_class_end; |
| 1830 | for (base_class = cxx_record_decl->bases_begin(), |
| 1831 | base_class_end = cxx_record_decl->bases_end(); |
| 1832 | base_class != base_class_end; ++base_class) { |
| 1833 | const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>( |
| 1834 | base_class->getType()->getAs<RecordType>()->getDecl()); |
| 1835 | if (RecordHasFields(base_class_decl)) |
| 1836 | return true; |
| 1837 | } |
| 1838 | } |
| 1839 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
Adrian Prantl | 4e8be2c | 2018-06-13 16:21:24 +0000 | [diff] [blame] | 1842 | #pragma mark Objective-C Classes |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1843 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1844 | CompilerType ClangASTContext::CreateObjCClass(const char *name, |
| 1845 | DeclContext *decl_ctx, |
| 1846 | bool isForwardDecl, |
| 1847 | bool isInternal, |
| 1848 | ClangASTMetadata *metadata) { |
| 1849 | ASTContext *ast = getASTContext(); |
| 1850 | assert(ast != nullptr); |
| 1851 | assert(name && name[0]); |
| 1852 | if (decl_ctx == nullptr) |
| 1853 | decl_ctx = ast->getTranslationUnitDecl(); |
Greg Clayton | 8cf0593 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 1854 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1855 | ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create( |
| 1856 | *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr, |
| 1857 | nullptr, SourceLocation(), |
| 1858 | /*isForwardDecl,*/ |
| 1859 | isInternal); |
| 1860 | |
| 1861 | if (decl && metadata) |
| 1862 | SetMetadata(ast, decl, *metadata); |
| 1863 | |
| 1864 | return CompilerType(ast, ast->getObjCInterfaceType(decl)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1867 | static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) { |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 1868 | return !ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1869 | } |
| 1870 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1871 | uint32_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1872 | ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl, |
| 1873 | bool omit_empty_base_classes) { |
| 1874 | uint32_t num_bases = 0; |
| 1875 | if (cxx_record_decl) { |
| 1876 | if (omit_empty_base_classes) { |
| 1877 | CXXRecordDecl::base_class_const_iterator base_class, base_class_end; |
| 1878 | for (base_class = cxx_record_decl->bases_begin(), |
| 1879 | base_class_end = cxx_record_decl->bases_end(); |
| 1880 | base_class != base_class_end; ++base_class) { |
| 1881 | // Skip empty base classes |
| 1882 | if (omit_empty_base_classes) { |
| 1883 | if (BaseSpecifierIsEmpty(base_class)) |
| 1884 | continue; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1885 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1886 | ++num_bases; |
| 1887 | } |
| 1888 | } else |
| 1889 | num_bases = cxx_record_decl->getNumBases(); |
| 1890 | } |
| 1891 | return num_bases; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1894 | #pragma mark Namespace Declarations |
| 1895 | |
| 1896 | NamespaceDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1897 | ClangASTContext::GetUniqueNamespaceDeclaration(const char *name, |
| 1898 | DeclContext *decl_ctx) { |
| 1899 | NamespaceDecl *namespace_decl = nullptr; |
| 1900 | ASTContext *ast = getASTContext(); |
| 1901 | TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl(); |
| 1902 | if (decl_ctx == nullptr) |
| 1903 | decl_ctx = translation_unit_decl; |
Greg Clayton | 030a204 | 2011-10-14 21:34:45 +0000 | [diff] [blame] | 1904 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1905 | if (name) { |
| 1906 | IdentifierInfo &identifier_info = ast->Idents.get(name); |
| 1907 | DeclarationName decl_name(&identifier_info); |
| 1908 | clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name); |
| 1909 | for (NamedDecl *decl : result) { |
| 1910 | namespace_decl = dyn_cast<clang::NamespaceDecl>(decl); |
| 1911 | if (namespace_decl) |
| 1912 | return namespace_decl; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1913 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1914 | |
| 1915 | namespace_decl = |
| 1916 | NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(), |
| 1917 | SourceLocation(), &identifier_info, nullptr); |
| 1918 | |
| 1919 | decl_ctx->addDecl(namespace_decl); |
| 1920 | } else { |
| 1921 | if (decl_ctx == translation_unit_decl) { |
| 1922 | namespace_decl = translation_unit_decl->getAnonymousNamespace(); |
| 1923 | if (namespace_decl) |
| 1924 | return namespace_decl; |
| 1925 | |
| 1926 | namespace_decl = |
| 1927 | NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(), |
| 1928 | SourceLocation(), nullptr, nullptr); |
| 1929 | translation_unit_decl->setAnonymousNamespace(namespace_decl); |
| 1930 | translation_unit_decl->addDecl(namespace_decl); |
| 1931 | assert(namespace_decl == translation_unit_decl->getAnonymousNamespace()); |
| 1932 | } else { |
| 1933 | NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx); |
| 1934 | if (parent_namespace_decl) { |
| 1935 | namespace_decl = parent_namespace_decl->getAnonymousNamespace(); |
| 1936 | if (namespace_decl) |
| 1937 | return namespace_decl; |
| 1938 | namespace_decl = |
| 1939 | NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(), |
| 1940 | SourceLocation(), nullptr, nullptr); |
| 1941 | parent_namespace_decl->setAnonymousNamespace(namespace_decl); |
| 1942 | parent_namespace_decl->addDecl(namespace_decl); |
| 1943 | assert(namespace_decl == |
| 1944 | parent_namespace_decl->getAnonymousNamespace()); |
| 1945 | } else { |
| 1946 | // BAD!!! |
| 1947 | } |
Greg Clayton | 9d3d688 | 2011-10-31 23:51:19 +0000 | [diff] [blame] | 1948 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1949 | } |
Greg Clayton | 9d3d688 | 2011-10-31 23:51:19 +0000 | [diff] [blame] | 1950 | #ifdef LLDB_CONFIGURATION_DEBUG |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1951 | VerifyDecl(namespace_decl); |
Greg Clayton | 9d3d688 | 2011-10-31 23:51:19 +0000 | [diff] [blame] | 1952 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1953 | return namespace_decl; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1956 | NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration( |
| 1957 | clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx) { |
| 1958 | ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast); |
| 1959 | if (ast_ctx == nullptr) |
| 1960 | return nullptr; |
Siva Chandra | 03ff5c8 | 2016-02-05 19:10:04 +0000 | [diff] [blame] | 1961 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1962 | return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx); |
Siva Chandra | 03ff5c8 | 2016-02-05 19:10:04 +0000 | [diff] [blame] | 1963 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1964 | |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 1965 | clang::BlockDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1966 | ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) { |
| 1967 | if (ctx != nullptr) { |
| 1968 | clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx, |
| 1969 | clang::SourceLocation()); |
| 1970 | ctx->addDecl(decl); |
| 1971 | return decl; |
| 1972 | } |
| 1973 | return nullptr; |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1976 | clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left, |
| 1977 | clang::DeclContext *right, |
| 1978 | clang::DeclContext *root) { |
| 1979 | if (root == nullptr) |
Paul Herman | ea188fc | 2015-09-16 18:48:30 +0000 | [diff] [blame] | 1980 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1981 | |
| 1982 | std::set<clang::DeclContext *> path_left; |
| 1983 | for (clang::DeclContext *d = left; d != nullptr; d = d->getParent()) |
| 1984 | path_left.insert(d); |
| 1985 | |
| 1986 | for (clang::DeclContext *d = right; d != nullptr; d = d->getParent()) |
| 1987 | if (path_left.find(d) != path_left.end()) |
| 1988 | return d; |
| 1989 | |
| 1990 | return nullptr; |
Paul Herman | ea188fc | 2015-09-16 18:48:30 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1993 | clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration( |
| 1994 | clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) { |
| 1995 | if (decl_ctx != nullptr && ns_decl != nullptr) { |
| 1996 | clang::TranslationUnitDecl *translation_unit = |
| 1997 | (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext()); |
| 1998 | clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create( |
| 1999 | *getASTContext(), decl_ctx, clang::SourceLocation(), |
| 2000 | clang::SourceLocation(), clang::NestedNameSpecifierLoc(), |
| 2001 | clang::SourceLocation(), ns_decl, |
| 2002 | FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit)); |
| 2003 | decl_ctx->addDecl(using_decl); |
| 2004 | return using_decl; |
| 2005 | } |
| 2006 | return nullptr; |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
| 2009 | clang::UsingDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2010 | ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx, |
| 2011 | clang::NamedDecl *target) { |
| 2012 | if (current_decl_ctx != nullptr && target != nullptr) { |
| 2013 | clang::UsingDecl *using_decl = clang::UsingDecl::Create( |
| 2014 | *getASTContext(), current_decl_ctx, clang::SourceLocation(), |
| 2015 | clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false); |
| 2016 | clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create( |
| 2017 | *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl, |
| 2018 | target); |
| 2019 | using_decl->addShadowDecl(shadow_decl); |
| 2020 | current_decl_ctx->addDecl(using_decl); |
| 2021 | return using_decl; |
| 2022 | } |
| 2023 | return nullptr; |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2026 | clang::VarDecl *ClangASTContext::CreateVariableDeclaration( |
| 2027 | clang::DeclContext *decl_context, const char *name, clang::QualType type) { |
| 2028 | if (decl_context != nullptr) { |
| 2029 | clang::VarDecl *var_decl = clang::VarDecl::Create( |
| 2030 | *getASTContext(), decl_context, clang::SourceLocation(), |
| 2031 | clang::SourceLocation(), |
| 2032 | name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type, |
| 2033 | nullptr, clang::SC_None); |
| 2034 | var_decl->setAccess(clang::AS_public); |
| 2035 | decl_context->addDecl(var_decl); |
| 2036 | return var_decl; |
| 2037 | } |
| 2038 | return nullptr; |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
Zachary Turner | 9d8a97e | 2016-04-01 23:20:35 +0000 | [diff] [blame] | 2041 | lldb::opaque_compiler_type_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2042 | ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast, |
| 2043 | lldb::BasicType basic_type) { |
| 2044 | switch (basic_type) { |
| 2045 | case eBasicTypeVoid: |
| 2046 | return ast->VoidTy.getAsOpaquePtr(); |
| 2047 | case eBasicTypeChar: |
| 2048 | return ast->CharTy.getAsOpaquePtr(); |
| 2049 | case eBasicTypeSignedChar: |
| 2050 | return ast->SignedCharTy.getAsOpaquePtr(); |
| 2051 | case eBasicTypeUnsignedChar: |
| 2052 | return ast->UnsignedCharTy.getAsOpaquePtr(); |
| 2053 | case eBasicTypeWChar: |
| 2054 | return ast->getWCharType().getAsOpaquePtr(); |
| 2055 | case eBasicTypeSignedWChar: |
| 2056 | return ast->getSignedWCharType().getAsOpaquePtr(); |
| 2057 | case eBasicTypeUnsignedWChar: |
| 2058 | return ast->getUnsignedWCharType().getAsOpaquePtr(); |
| 2059 | case eBasicTypeChar16: |
| 2060 | return ast->Char16Ty.getAsOpaquePtr(); |
| 2061 | case eBasicTypeChar32: |
| 2062 | return ast->Char32Ty.getAsOpaquePtr(); |
| 2063 | case eBasicTypeShort: |
| 2064 | return ast->ShortTy.getAsOpaquePtr(); |
| 2065 | case eBasicTypeUnsignedShort: |
| 2066 | return ast->UnsignedShortTy.getAsOpaquePtr(); |
| 2067 | case eBasicTypeInt: |
| 2068 | return ast->IntTy.getAsOpaquePtr(); |
| 2069 | case eBasicTypeUnsignedInt: |
| 2070 | return ast->UnsignedIntTy.getAsOpaquePtr(); |
| 2071 | case eBasicTypeLong: |
| 2072 | return ast->LongTy.getAsOpaquePtr(); |
| 2073 | case eBasicTypeUnsignedLong: |
| 2074 | return ast->UnsignedLongTy.getAsOpaquePtr(); |
| 2075 | case eBasicTypeLongLong: |
| 2076 | return ast->LongLongTy.getAsOpaquePtr(); |
| 2077 | case eBasicTypeUnsignedLongLong: |
| 2078 | return ast->UnsignedLongLongTy.getAsOpaquePtr(); |
| 2079 | case eBasicTypeInt128: |
| 2080 | return ast->Int128Ty.getAsOpaquePtr(); |
| 2081 | case eBasicTypeUnsignedInt128: |
| 2082 | return ast->UnsignedInt128Ty.getAsOpaquePtr(); |
| 2083 | case eBasicTypeBool: |
| 2084 | return ast->BoolTy.getAsOpaquePtr(); |
| 2085 | case eBasicTypeHalf: |
| 2086 | return ast->HalfTy.getAsOpaquePtr(); |
| 2087 | case eBasicTypeFloat: |
| 2088 | return ast->FloatTy.getAsOpaquePtr(); |
| 2089 | case eBasicTypeDouble: |
| 2090 | return ast->DoubleTy.getAsOpaquePtr(); |
| 2091 | case eBasicTypeLongDouble: |
| 2092 | return ast->LongDoubleTy.getAsOpaquePtr(); |
| 2093 | case eBasicTypeFloatComplex: |
| 2094 | return ast->FloatComplexTy.getAsOpaquePtr(); |
| 2095 | case eBasicTypeDoubleComplex: |
| 2096 | return ast->DoubleComplexTy.getAsOpaquePtr(); |
| 2097 | case eBasicTypeLongDoubleComplex: |
| 2098 | return ast->LongDoubleComplexTy.getAsOpaquePtr(); |
| 2099 | case eBasicTypeObjCID: |
| 2100 | return ast->getObjCIdType().getAsOpaquePtr(); |
| 2101 | case eBasicTypeObjCClass: |
| 2102 | return ast->getObjCClassType().getAsOpaquePtr(); |
| 2103 | case eBasicTypeObjCSel: |
| 2104 | return ast->getObjCSelType().getAsOpaquePtr(); |
| 2105 | case eBasicTypeNullPtr: |
| 2106 | return ast->NullPtrTy.getAsOpaquePtr(); |
| 2107 | default: |
| 2108 | return nullptr; |
| 2109 | } |
Zachary Turner | 9d8a97e | 2016-04-01 23:20:35 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2112 | #pragma mark Function Types |
| 2113 | |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 2114 | clang::DeclarationName |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2115 | ClangASTContext::GetDeclarationName(const char *name, |
| 2116 | const CompilerType &function_clang_type) { |
| 2117 | if (!name || !name[0]) |
| 2118 | return clang::DeclarationName(); |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 2119 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2120 | clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS; |
| 2121 | if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS) |
| 2122 | return DeclarationName(&getASTContext()->Idents.get( |
| 2123 | name)); // Not operator, but a regular function. |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 2124 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2125 | // Check the number of operator parameters. Sometimes we have seen bad DWARF |
| 2126 | // that doesn't correctly describe operators and if we try to create a method |
| 2127 | // and add it to the class, clang will assert and crash, so we need to make |
| 2128 | // sure things are acceptable. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2129 | clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type)); |
| 2130 | const clang::FunctionProtoType *function_type = |
| 2131 | llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr()); |
| 2132 | if (function_type == nullptr) |
| 2133 | return clang::DeclarationName(); |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 2134 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2135 | const bool is_method = false; |
| 2136 | const unsigned int num_params = function_type->getNumParams(); |
| 2137 | if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount( |
| 2138 | is_method, op_kind, num_params)) |
| 2139 | return clang::DeclarationName(); |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 2140 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2141 | return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind); |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 2142 | } |
| 2143 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2144 | FunctionDecl *ClangASTContext::CreateFunctionDeclaration( |
| 2145 | DeclContext *decl_ctx, const char *name, |
| 2146 | const CompilerType &function_clang_type, int storage, bool is_inline) { |
| 2147 | FunctionDecl *func_decl = nullptr; |
| 2148 | ASTContext *ast = getASTContext(); |
| 2149 | if (decl_ctx == nullptr) |
| 2150 | decl_ctx = ast->getTranslationUnitDecl(); |
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 | const bool hasWrittenPrototype = true; |
| 2153 | const bool isConstexprSpecified = false; |
Greg Clayton | 0d55104 | 2013-06-28 21:08:47 +0000 | [diff] [blame] | 2154 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2155 | clang::DeclarationName declarationName = |
| 2156 | GetDeclarationName(name, function_clang_type); |
| 2157 | func_decl = FunctionDecl::Create( |
| 2158 | *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName, |
| 2159 | ClangUtil::GetQualType(function_clang_type), nullptr, |
| 2160 | (clang::StorageClass)storage, is_inline, hasWrittenPrototype, |
| 2161 | isConstexprSpecified); |
| 2162 | if (func_decl) |
| 2163 | decl_ctx->addDecl(func_decl); |
| 2164 | |
Sean Callanan | 5e9e199 | 2011-10-26 01:06:27 +0000 | [diff] [blame] | 2165 | #ifdef LLDB_CONFIGURATION_DEBUG |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2166 | VerifyDecl(func_decl); |
Sean Callanan | 5e9e199 | 2011-10-26 01:06:27 +0000 | [diff] [blame] | 2167 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2168 | |
| 2169 | return func_decl; |
| 2170 | } |
| 2171 | |
| 2172 | CompilerType ClangASTContext::CreateFunctionType( |
| 2173 | ASTContext *ast, const CompilerType &result_type, const CompilerType *args, |
Aleksandr Urakov | bc4707c | 2018-09-26 09:03:34 +0000 | [diff] [blame] | 2174 | unsigned num_args, bool is_variadic, unsigned type_quals, |
| 2175 | clang::CallingConv cc) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2176 | if (ast == nullptr) |
| 2177 | return CompilerType(); // invalid AST |
| 2178 | |
| 2179 | if (!result_type || !ClangUtil::IsClangType(result_type)) |
| 2180 | return CompilerType(); // invalid return type |
| 2181 | |
| 2182 | std::vector<QualType> qual_type_args; |
| 2183 | if (num_args > 0 && args == nullptr) |
| 2184 | return CompilerType(); // invalid argument array passed in |
| 2185 | |
| 2186 | // Verify that all arguments are valid and the right type |
| 2187 | for (unsigned i = 0; i < num_args; ++i) { |
| 2188 | if (args[i]) { |
| 2189 | // Make sure we have a clang type in args[i] and not a type from another |
| 2190 | // language whose name might match |
| 2191 | const bool is_clang_type = ClangUtil::IsClangType(args[i]); |
| 2192 | lldbassert(is_clang_type); |
| 2193 | if (is_clang_type) |
| 2194 | qual_type_args.push_back(ClangUtil::GetQualType(args[i])); |
| 2195 | else |
| 2196 | return CompilerType(); // invalid argument type (must be a clang type) |
| 2197 | } else |
| 2198 | return CompilerType(); // invalid argument type (empty) |
| 2199 | } |
| 2200 | |
| 2201 | // TODO: Detect calling convention in DWARF? |
| 2202 | FunctionProtoType::ExtProtoInfo proto_info; |
Aleksandr Urakov | bc4707c | 2018-09-26 09:03:34 +0000 | [diff] [blame] | 2203 | proto_info.ExtInfo = cc; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2204 | proto_info.Variadic = is_variadic; |
| 2205 | proto_info.ExceptionSpec = EST_None; |
Mikael Nilsson | 8b3bf6c | 2018-12-13 10:17:26 +0000 | [diff] [blame] | 2206 | proto_info.TypeQuals = clang::Qualifiers::fromFastMask(type_quals); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2207 | proto_info.RefQualifier = RQ_None; |
| 2208 | |
| 2209 | return CompilerType(ast, |
| 2210 | ast->getFunctionType(ClangUtil::GetQualType(result_type), |
| 2211 | qual_type_args, proto_info)); |
| 2212 | } |
| 2213 | |
| 2214 | ParmVarDecl *ClangASTContext::CreateParameterDeclaration( |
Zachary Turner | 6753d2d | 2018-12-12 17:17:53 +0000 | [diff] [blame] | 2215 | clang::DeclContext *decl_ctx, const char *name, |
| 2216 | const CompilerType ¶m_type, int storage) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2217 | ASTContext *ast = getASTContext(); |
| 2218 | assert(ast != nullptr); |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 2219 | auto *decl = |
| 2220 | ParmVarDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(), |
| 2221 | name && name[0] ? &ast->Idents.get(name) : nullptr, |
| 2222 | ClangUtil::GetQualType(param_type), nullptr, |
| 2223 | (clang::StorageClass)storage, nullptr); |
| 2224 | decl_ctx->addDecl(decl); |
| 2225 | return decl; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
| 2228 | void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl, |
| 2229 | ParmVarDecl **params, |
| 2230 | unsigned num_params) { |
| 2231 | if (function_decl) |
| 2232 | function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 2235 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2236 | ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) { |
| 2237 | QualType block_type = m_ast_ap->getBlockPointerType( |
| 2238 | clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType())); |
Greg Clayton | ceeb521 | 2016-05-26 22:33:25 +0000 | [diff] [blame] | 2239 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2240 | return CompilerType(this, block_type.getAsOpaquePtr()); |
Sean Callanan | c530ba9 | 2016-05-02 21:15:31 +0000 | [diff] [blame] | 2241 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2242 | |
| 2243 | #pragma mark Array Types |
| 2244 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2245 | CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type, |
| 2246 | size_t element_count, |
| 2247 | bool is_vector) { |
| 2248 | if (element_type.IsValid()) { |
| 2249 | ASTContext *ast = getASTContext(); |
| 2250 | assert(ast != nullptr); |
Greg Clayton | 4ef877f | 2012-12-06 02:33:54 +0000 | [diff] [blame] | 2251 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2252 | if (is_vector) { |
| 2253 | return CompilerType( |
| 2254 | ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type), |
| 2255 | element_count)); |
| 2256 | } else { |
| 2257 | |
| 2258 | llvm::APInt ap_element_count(64, element_count); |
| 2259 | if (element_count == 0) { |
| 2260 | return CompilerType(ast, ast->getIncompleteArrayType( |
| 2261 | ClangUtil::GetQualType(element_type), |
| 2262 | clang::ArrayType::Normal, 0)); |
| 2263 | } else { |
| 2264 | return CompilerType( |
| 2265 | ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type), |
| 2266 | ap_element_count, |
| 2267 | clang::ArrayType::Normal, 0)); |
| 2268 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2269 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2270 | } |
| 2271 | return CompilerType(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2274 | CompilerType ClangASTContext::CreateStructForIdentifier( |
| 2275 | const ConstString &type_name, |
| 2276 | const std::initializer_list<std::pair<const char *, CompilerType>> |
| 2277 | &type_fields, |
| 2278 | bool packed) { |
| 2279 | CompilerType type; |
| 2280 | if (!type_name.IsEmpty() && |
| 2281 | (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)) |
| 2282 | .IsValid()) { |
Pavel Labath | f31c9d2 | 2017-01-05 13:18:42 +0000 | [diff] [blame] | 2283 | lldbassert(0 && "Trying to create a type for an existing name"); |
Enrico Granata | 76b08d5 | 2014-10-29 23:08:02 +0000 | [diff] [blame] | 2284 | return type; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
| 2287 | type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(), |
| 2288 | clang::TTK_Struct, lldb::eLanguageTypeC); |
| 2289 | StartTagDeclarationDefinition(type); |
| 2290 | for (const auto &field : type_fields) |
| 2291 | AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic, |
| 2292 | 0); |
| 2293 | if (packed) |
| 2294 | SetIsPacked(type); |
| 2295 | CompleteTagDeclarationDefinition(type); |
| 2296 | return type; |
Enrico Granata | 76b08d5 | 2014-10-29 23:08:02 +0000 | [diff] [blame] | 2297 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2298 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2299 | CompilerType ClangASTContext::GetOrCreateStructForIdentifier( |
| 2300 | const ConstString &type_name, |
| 2301 | const std::initializer_list<std::pair<const char *, CompilerType>> |
| 2302 | &type_fields, |
| 2303 | bool packed) { |
| 2304 | CompilerType type; |
| 2305 | if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid()) |
| 2306 | return type; |
Sean Callanan | c530ba9 | 2016-05-02 21:15:31 +0000 | [diff] [blame] | 2307 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2308 | return CreateStructForIdentifier(type_name, type_fields, packed); |
Sean Callanan | c530ba9 | 2016-05-02 21:15:31 +0000 | [diff] [blame] | 2309 | } |
| 2310 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2311 | #pragma mark Enumeration Types |
| 2312 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 2313 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2314 | ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx, |
| 2315 | const Declaration &decl, |
Tamas Berghammer | 5976583 | 2017-11-07 10:39:22 +0000 | [diff] [blame] | 2316 | const CompilerType &integer_clang_type, |
| 2317 | bool is_scoped) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2318 | // TODO: Do something intelligent with the Declaration object passed in |
| 2319 | // like maybe filling in the SourceLocation with it... |
| 2320 | ASTContext *ast = getASTContext(); |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 2321 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2322 | // TODO: ask about these... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2323 | // const bool IsFixed = false; |
| 2324 | |
| 2325 | EnumDecl *enum_decl = EnumDecl::Create( |
| 2326 | *ast, decl_ctx, SourceLocation(), SourceLocation(), |
| 2327 | name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr, |
Tamas Berghammer | cf6bf4c | 2017-11-07 13:43:55 +0000 | [diff] [blame] | 2328 | is_scoped, // IsScoped |
| 2329 | is_scoped, // IsScopedUsingClassTag |
| 2330 | false); // IsFixed |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2331 | |
| 2332 | if (enum_decl) { |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 2333 | if (decl_ctx) |
| 2334 | decl_ctx->addDecl(enum_decl); |
| 2335 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2336 | // TODO: check if we should be setting the promotion type too? |
| 2337 | enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type)); |
| 2338 | |
| 2339 | enum_decl->setAccess(AS_public); // TODO respect what's in the debug info |
| 2340 | |
| 2341 | return CompilerType(ast, ast->getTagDeclType(enum_decl)); |
| 2342 | } |
| 2343 | return CompilerType(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2344 | } |
| 2345 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2346 | CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast, |
| 2347 | size_t bit_size, |
| 2348 | bool is_signed) { |
| 2349 | if (ast) { |
| 2350 | if (is_signed) { |
| 2351 | if (bit_size == ast->getTypeSize(ast->SignedCharTy)) |
| 2352 | return CompilerType(ast, ast->SignedCharTy); |
| 2353 | |
| 2354 | if (bit_size == ast->getTypeSize(ast->ShortTy)) |
| 2355 | return CompilerType(ast, ast->ShortTy); |
| 2356 | |
| 2357 | if (bit_size == ast->getTypeSize(ast->IntTy)) |
| 2358 | return CompilerType(ast, ast->IntTy); |
| 2359 | |
| 2360 | if (bit_size == ast->getTypeSize(ast->LongTy)) |
| 2361 | return CompilerType(ast, ast->LongTy); |
| 2362 | |
| 2363 | if (bit_size == ast->getTypeSize(ast->LongLongTy)) |
| 2364 | return CompilerType(ast, ast->LongLongTy); |
| 2365 | |
| 2366 | if (bit_size == ast->getTypeSize(ast->Int128Ty)) |
| 2367 | return CompilerType(ast, ast->Int128Ty); |
| 2368 | } else { |
| 2369 | if (bit_size == ast->getTypeSize(ast->UnsignedCharTy)) |
| 2370 | return CompilerType(ast, ast->UnsignedCharTy); |
| 2371 | |
| 2372 | if (bit_size == ast->getTypeSize(ast->UnsignedShortTy)) |
| 2373 | return CompilerType(ast, ast->UnsignedShortTy); |
| 2374 | |
| 2375 | if (bit_size == ast->getTypeSize(ast->UnsignedIntTy)) |
| 2376 | return CompilerType(ast, ast->UnsignedIntTy); |
| 2377 | |
| 2378 | if (bit_size == ast->getTypeSize(ast->UnsignedLongTy)) |
| 2379 | return CompilerType(ast, ast->UnsignedLongTy); |
| 2380 | |
| 2381 | if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy)) |
| 2382 | return CompilerType(ast, ast->UnsignedLongLongTy); |
| 2383 | |
| 2384 | if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty)) |
| 2385 | return CompilerType(ast, ast->UnsignedInt128Ty); |
Enrico Granata | e8bf749 | 2014-08-15 23:00:02 +0000 | [diff] [blame] | 2386 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2387 | } |
| 2388 | return CompilerType(); |
Enrico Granata | e8bf749 | 2014-08-15 23:00:02 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2391 | CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast, |
| 2392 | bool is_signed) { |
| 2393 | if (ast) |
| 2394 | return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy), |
| 2395 | is_signed); |
| 2396 | return CompilerType(); |
Enrico Granata | e8bf749 | 2014-08-15 23:00:02 +0000 | [diff] [blame] | 2397 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2398 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2399 | void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) { |
| 2400 | if (decl_ctx) { |
| 2401 | DumpDeclContextHiearchy(decl_ctx->getParent()); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2402 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2403 | clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx); |
| 2404 | if (named_decl) { |
| 2405 | printf("%20s: %s\n", decl_ctx->getDeclKindName(), |
| 2406 | named_decl->getDeclName().getAsString().c_str()); |
| 2407 | } else { |
| 2408 | printf("%20s\n", decl_ctx->getDeclKindName()); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2409 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2410 | } |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2413 | void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) { |
| 2414 | if (decl == nullptr) |
| 2415 | return; |
| 2416 | DumpDeclContextHiearchy(decl->getDeclContext()); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2417 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2418 | clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl); |
| 2419 | if (record_decl) { |
| 2420 | printf("%20s: %s%s\n", decl->getDeclKindName(), |
| 2421 | record_decl->getDeclName().getAsString().c_str(), |
| 2422 | record_decl->isInjectedClassName() ? " (injected class name)" : ""); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2423 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2424 | } else { |
| 2425 | clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl); |
| 2426 | if (named_decl) { |
| 2427 | printf("%20s: %s\n", decl->getDeclKindName(), |
| 2428 | named_decl->getDeclName().getAsString().c_str()); |
| 2429 | } else { |
| 2430 | printf("%20s\n", decl->getDeclKindName()); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2431 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2432 | } |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2435 | bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl, |
| 2436 | clang::Decl *rhs_decl) { |
| 2437 | if (lhs_decl && rhs_decl) { |
| 2438 | //---------------------------------------------------------------------- |
| 2439 | // Make sure the decl kinds match first |
| 2440 | //---------------------------------------------------------------------- |
| 2441 | const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind(); |
| 2442 | const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind(); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2443 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2444 | if (lhs_decl_kind == rhs_decl_kind) { |
| 2445 | //------------------------------------------------------------------ |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2446 | // Now check that the decl contexts kinds are all equivalent before we |
| 2447 | // have to check any names of the decl contexts... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2448 | //------------------------------------------------------------------ |
| 2449 | clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext(); |
| 2450 | clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext(); |
| 2451 | if (lhs_decl_ctx && rhs_decl_ctx) { |
| 2452 | while (1) { |
| 2453 | if (lhs_decl_ctx && rhs_decl_ctx) { |
| 2454 | const clang::Decl::Kind lhs_decl_ctx_kind = |
| 2455 | lhs_decl_ctx->getDeclKind(); |
| 2456 | const clang::Decl::Kind rhs_decl_ctx_kind = |
| 2457 | rhs_decl_ctx->getDeclKind(); |
| 2458 | if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) { |
| 2459 | lhs_decl_ctx = lhs_decl_ctx->getParent(); |
| 2460 | rhs_decl_ctx = rhs_decl_ctx->getParent(); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2461 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2462 | if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr) |
| 2463 | break; |
| 2464 | } else |
| 2465 | return false; |
| 2466 | } else |
Tamas Berghammer | fcf334b | 2015-12-02 11:35:54 +0000 | [diff] [blame] | 2467 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | //-------------------------------------------------------------- |
| 2471 | // Now make sure the name of the decls match |
| 2472 | //-------------------------------------------------------------- |
| 2473 | clang::NamedDecl *lhs_named_decl = |
| 2474 | llvm::dyn_cast<clang::NamedDecl>(lhs_decl); |
| 2475 | clang::NamedDecl *rhs_named_decl = |
| 2476 | llvm::dyn_cast<clang::NamedDecl>(rhs_decl); |
| 2477 | if (lhs_named_decl && rhs_named_decl) { |
| 2478 | clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName(); |
| 2479 | clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName(); |
| 2480 | if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) { |
| 2481 | if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString()) |
| 2482 | return false; |
| 2483 | } else |
Greg Clayton | a272147 | 2011-06-25 00:44:06 +0000 | [diff] [blame] | 2484 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2485 | } else |
| 2486 | return false; |
Greg Clayton | a272147 | 2011-06-25 00:44:06 +0000 | [diff] [blame] | 2487 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2488 | //-------------------------------------------------------------- |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2489 | // We know that the decl context kinds all match, so now we need to |
| 2490 | // make sure the names match as well |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2491 | //-------------------------------------------------------------- |
| 2492 | lhs_decl_ctx = lhs_decl->getDeclContext(); |
| 2493 | rhs_decl_ctx = rhs_decl->getDeclContext(); |
| 2494 | while (1) { |
| 2495 | switch (lhs_decl_ctx->getDeclKind()) { |
| 2496 | case clang::Decl::TranslationUnit: |
| 2497 | // We don't care about the translation unit names |
| 2498 | return true; |
| 2499 | default: { |
| 2500 | clang::NamedDecl *lhs_named_decl = |
| 2501 | llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx); |
| 2502 | clang::NamedDecl *rhs_named_decl = |
| 2503 | llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx); |
| 2504 | if (lhs_named_decl && rhs_named_decl) { |
| 2505 | clang::DeclarationName lhs_decl_name = |
| 2506 | lhs_named_decl->getDeclName(); |
| 2507 | clang::DeclarationName rhs_decl_name = |
| 2508 | rhs_named_decl->getDeclName(); |
| 2509 | if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) { |
| 2510 | if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString()) |
| 2511 | return false; |
| 2512 | } else |
| 2513 | return false; |
| 2514 | } else |
| 2515 | return false; |
| 2516 | } break; |
| 2517 | } |
| 2518 | lhs_decl_ctx = lhs_decl_ctx->getParent(); |
| 2519 | rhs_decl_ctx = rhs_decl_ctx->getParent(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2520 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2521 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2522 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2523 | } |
| 2524 | return false; |
| 2525 | } |
| 2526 | bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast, |
| 2527 | clang::Decl *decl) { |
| 2528 | if (!decl) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2529 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2530 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2531 | ExternalASTSource *ast_source = ast->getExternalSource(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2532 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2533 | if (!ast_source) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2534 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2535 | |
| 2536 | if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) { |
| 2537 | if (tag_decl->isCompleteDefinition()) |
| 2538 | return true; |
| 2539 | |
| 2540 | if (!tag_decl->hasExternalLexicalStorage()) |
| 2541 | return false; |
| 2542 | |
| 2543 | ast_source->CompleteType(tag_decl); |
| 2544 | |
| 2545 | return !tag_decl->getTypeForDecl()->isIncompleteType(); |
| 2546 | } else if (clang::ObjCInterfaceDecl *objc_interface_decl = |
| 2547 | llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) { |
| 2548 | if (objc_interface_decl->getDefinition()) |
| 2549 | return true; |
| 2550 | |
| 2551 | if (!objc_interface_decl->hasExternalLexicalStorage()) |
| 2552 | return false; |
| 2553 | |
| 2554 | ast_source->CompleteType(objc_interface_decl); |
| 2555 | |
| 2556 | return !objc_interface_decl->getTypeForDecl()->isIncompleteType(); |
| 2557 | } else { |
| 2558 | return false; |
| 2559 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2560 | } |
| 2561 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2562 | void ClangASTContext::SetMetadataAsUserID(const void *object, |
| 2563 | user_id_t user_id) { |
| 2564 | ClangASTMetadata meta_data; |
| 2565 | meta_data.SetUserID(user_id); |
| 2566 | SetMetadata(object, meta_data); |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 2567 | } |
| 2568 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2569 | void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object, |
| 2570 | ClangASTMetadata &metadata) { |
| 2571 | ClangExternalASTSourceCommon *external_source = |
| 2572 | ClangExternalASTSourceCommon::Lookup(ast->getExternalSource()); |
| 2573 | |
| 2574 | if (external_source) |
| 2575 | external_source->SetMetadata(object, metadata); |
| 2576 | } |
| 2577 | |
| 2578 | ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast, |
| 2579 | const void *object) { |
| 2580 | ClangExternalASTSourceCommon *external_source = |
| 2581 | ClangExternalASTSourceCommon::Lookup(ast->getExternalSource()); |
| 2582 | |
| 2583 | if (external_source && external_source->HasMetadata(object)) |
| 2584 | return external_source->GetMetadata(object); |
| 2585 | else |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2586 | return nullptr; |
| 2587 | } |
| 2588 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2589 | clang::DeclContext * |
| 2590 | ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) { |
| 2591 | return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl); |
| 2592 | } |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2593 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2594 | clang::DeclContext * |
| 2595 | ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) { |
| 2596 | return llvm::dyn_cast<clang::DeclContext>(objc_method_decl); |
| 2597 | } |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2598 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2599 | bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type, |
| 2600 | int kind) const { |
| 2601 | const clang::Type *clang_type = tag_qual_type.getTypePtr(); |
| 2602 | if (clang_type) { |
| 2603 | const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type); |
| 2604 | if (tag_type) { |
| 2605 | clang::TagDecl *tag_decl = |
| 2606 | llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl()); |
| 2607 | if (tag_decl) { |
| 2608 | tag_decl->setTagKind((clang::TagDecl::TagKind)kind); |
| 2609 | return true; |
| 2610 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2611 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2612 | } |
| 2613 | return false; |
| 2614 | } |
| 2615 | |
| 2616 | bool ClangASTContext::SetDefaultAccessForRecordFields( |
| 2617 | clang::RecordDecl *record_decl, int default_accessibility, |
| 2618 | int *assigned_accessibilities, size_t num_assigned_accessibilities) { |
| 2619 | if (record_decl) { |
| 2620 | uint32_t field_idx; |
| 2621 | clang::RecordDecl::field_iterator field, field_end; |
| 2622 | for (field = record_decl->field_begin(), |
| 2623 | field_end = record_decl->field_end(), field_idx = 0; |
| 2624 | field != field_end; ++field, ++field_idx) { |
| 2625 | // If no accessibility was assigned, assign the correct one |
| 2626 | if (field_idx < num_assigned_accessibilities && |
| 2627 | assigned_accessibilities[field_idx] == clang::AS_none) |
| 2628 | field->setAccess((clang::AccessSpecifier)default_accessibility); |
| 2629 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2630 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2631 | } |
| 2632 | return false; |
| 2633 | } |
| 2634 | |
| 2635 | clang::DeclContext * |
| 2636 | ClangASTContext::GetDeclContextForType(const CompilerType &type) { |
| 2637 | return GetDeclContextForType(ClangUtil::GetQualType(type)); |
| 2638 | } |
| 2639 | |
| 2640 | clang::DeclContext * |
| 2641 | ClangASTContext::GetDeclContextForType(clang::QualType type) { |
| 2642 | if (type.isNull()) |
| 2643 | return nullptr; |
| 2644 | |
| 2645 | clang::QualType qual_type = type.getCanonicalType(); |
| 2646 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 2647 | switch (type_class) { |
| 2648 | case clang::Type::ObjCInterface: |
| 2649 | return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr()) |
| 2650 | ->getInterface(); |
| 2651 | case clang::Type::ObjCObjectPointer: |
| 2652 | return GetDeclContextForType( |
| 2653 | llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr()) |
| 2654 | ->getPointeeType()); |
| 2655 | case clang::Type::Record: |
| 2656 | return llvm::cast<clang::RecordType>(qual_type)->getDecl(); |
| 2657 | case clang::Type::Enum: |
| 2658 | return llvm::cast<clang::EnumType>(qual_type)->getDecl(); |
| 2659 | case clang::Type::Typedef: |
| 2660 | return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type) |
| 2661 | ->getDecl() |
| 2662 | ->getUnderlyingType()); |
| 2663 | case clang::Type::Auto: |
| 2664 | return GetDeclContextForType( |
| 2665 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()); |
| 2666 | case clang::Type::Elaborated: |
| 2667 | return GetDeclContextForType( |
| 2668 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()); |
| 2669 | case clang::Type::Paren: |
| 2670 | return GetDeclContextForType( |
| 2671 | llvm::cast<clang::ParenType>(qual_type)->desugar()); |
| 2672 | default: |
| 2673 | break; |
| 2674 | } |
| 2675 | // No DeclContext in this type... |
| 2676 | return nullptr; |
| 2677 | } |
| 2678 | |
| 2679 | static bool GetCompleteQualType(clang::ASTContext *ast, |
| 2680 | clang::QualType qual_type, |
| 2681 | bool allow_completion = true) { |
| 2682 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 2683 | switch (type_class) { |
| 2684 | case clang::Type::ConstantArray: |
| 2685 | case clang::Type::IncompleteArray: |
| 2686 | case clang::Type::VariableArray: { |
| 2687 | const clang::ArrayType *array_type = |
| 2688 | llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr()); |
| 2689 | |
| 2690 | if (array_type) |
| 2691 | return GetCompleteQualType(ast, array_type->getElementType(), |
| 2692 | allow_completion); |
| 2693 | } break; |
| 2694 | case clang::Type::Record: { |
| 2695 | clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); |
| 2696 | if (cxx_record_decl) { |
| 2697 | if (cxx_record_decl->hasExternalLexicalStorage()) { |
| 2698 | const bool is_complete = cxx_record_decl->isCompleteDefinition(); |
| 2699 | const bool fields_loaded = |
| 2700 | cxx_record_decl->hasLoadedFieldsFromExternalStorage(); |
| 2701 | if (is_complete && fields_loaded) |
| 2702 | return true; |
| 2703 | |
| 2704 | if (!allow_completion) |
| 2705 | return false; |
| 2706 | |
| 2707 | // Call the field_begin() accessor to for it to use the external source |
| 2708 | // to load the fields... |
| 2709 | clang::ExternalASTSource *external_ast_source = |
| 2710 | ast->getExternalSource(); |
| 2711 | if (external_ast_source) { |
| 2712 | external_ast_source->CompleteType(cxx_record_decl); |
| 2713 | if (cxx_record_decl->isCompleteDefinition()) { |
| 2714 | cxx_record_decl->field_begin(); |
| 2715 | cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true); |
| 2716 | } |
| 2717 | } |
| 2718 | } |
| 2719 | } |
| 2720 | const clang::TagType *tag_type = |
| 2721 | llvm::cast<clang::TagType>(qual_type.getTypePtr()); |
| 2722 | return !tag_type->isIncompleteType(); |
| 2723 | } break; |
| 2724 | |
| 2725 | case clang::Type::Enum: { |
| 2726 | const clang::TagType *tag_type = |
| 2727 | llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()); |
| 2728 | if (tag_type) { |
| 2729 | clang::TagDecl *tag_decl = tag_type->getDecl(); |
| 2730 | if (tag_decl) { |
| 2731 | if (tag_decl->getDefinition()) |
| 2732 | return true; |
| 2733 | |
| 2734 | if (!allow_completion) |
| 2735 | return false; |
| 2736 | |
| 2737 | if (tag_decl->hasExternalLexicalStorage()) { |
| 2738 | if (ast) { |
| 2739 | clang::ExternalASTSource *external_ast_source = |
| 2740 | ast->getExternalSource(); |
| 2741 | if (external_ast_source) { |
| 2742 | external_ast_source->CompleteType(tag_decl); |
| 2743 | return !tag_type->isIncompleteType(); |
| 2744 | } |
| 2745 | } |
| 2746 | } |
| 2747 | return false; |
| 2748 | } |
| 2749 | } |
| 2750 | |
| 2751 | } break; |
| 2752 | case clang::Type::ObjCObject: |
| 2753 | case clang::Type::ObjCInterface: { |
| 2754 | const clang::ObjCObjectType *objc_class_type = |
| 2755 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type); |
| 2756 | if (objc_class_type) { |
| 2757 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 2758 | objc_class_type->getInterface(); |
| 2759 | // We currently can't complete objective C types through the newly added |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2760 | // ASTContext because it only supports TagDecl objects right now... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2761 | if (class_interface_decl) { |
| 2762 | if (class_interface_decl->getDefinition()) |
| 2763 | return true; |
| 2764 | |
| 2765 | if (!allow_completion) |
| 2766 | return false; |
| 2767 | |
| 2768 | if (class_interface_decl->hasExternalLexicalStorage()) { |
| 2769 | if (ast) { |
| 2770 | clang::ExternalASTSource *external_ast_source = |
| 2771 | ast->getExternalSource(); |
| 2772 | if (external_ast_source) { |
| 2773 | external_ast_source->CompleteType(class_interface_decl); |
| 2774 | return !objc_class_type->isIncompleteType(); |
| 2775 | } |
| 2776 | } |
| 2777 | } |
| 2778 | return false; |
| 2779 | } |
| 2780 | } |
| 2781 | } break; |
| 2782 | |
| 2783 | case clang::Type::Typedef: |
| 2784 | return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type) |
| 2785 | ->getDecl() |
| 2786 | ->getUnderlyingType(), |
| 2787 | allow_completion); |
| 2788 | |
| 2789 | case clang::Type::Auto: |
| 2790 | return GetCompleteQualType( |
| 2791 | ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(), |
| 2792 | allow_completion); |
| 2793 | |
| 2794 | case clang::Type::Elaborated: |
| 2795 | return GetCompleteQualType( |
| 2796 | ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(), |
| 2797 | allow_completion); |
| 2798 | |
| 2799 | case clang::Type::Paren: |
| 2800 | return GetCompleteQualType( |
| 2801 | ast, llvm::cast<clang::ParenType>(qual_type)->desugar(), |
| 2802 | allow_completion); |
| 2803 | |
| 2804 | case clang::Type::Attributed: |
| 2805 | return GetCompleteQualType( |
| 2806 | ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(), |
| 2807 | allow_completion); |
| 2808 | |
| 2809 | default: |
| 2810 | break; |
| 2811 | } |
| 2812 | |
| 2813 | return true; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2814 | } |
| 2815 | |
| 2816 | static clang::ObjCIvarDecl::AccessControl |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2817 | ConvertAccessTypeToObjCIvarAccessControl(AccessType access) { |
| 2818 | switch (access) { |
| 2819 | case eAccessNone: |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2820 | return clang::ObjCIvarDecl::None; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2821 | case eAccessPublic: |
| 2822 | return clang::ObjCIvarDecl::Public; |
| 2823 | case eAccessPrivate: |
| 2824 | return clang::ObjCIvarDecl::Private; |
| 2825 | case eAccessProtected: |
| 2826 | return clang::ObjCIvarDecl::Protected; |
| 2827 | case eAccessPackage: |
| 2828 | return clang::ObjCIvarDecl::Package; |
| 2829 | } |
| 2830 | return clang::ObjCIvarDecl::None; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2831 | } |
| 2832 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2833 | //---------------------------------------------------------------------- |
| 2834 | // Tests |
| 2835 | //---------------------------------------------------------------------- |
| 2836 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2837 | bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) { |
| 2838 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 2839 | |
| 2840 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 2841 | switch (type_class) { |
| 2842 | case clang::Type::IncompleteArray: |
| 2843 | case clang::Type::VariableArray: |
| 2844 | case clang::Type::ConstantArray: |
| 2845 | case clang::Type::ExtVector: |
| 2846 | case clang::Type::Vector: |
| 2847 | case clang::Type::Record: |
| 2848 | case clang::Type::ObjCObject: |
| 2849 | case clang::Type::ObjCInterface: |
| 2850 | return true; |
| 2851 | case clang::Type::Auto: |
| 2852 | return IsAggregateType(llvm::cast<clang::AutoType>(qual_type) |
| 2853 | ->getDeducedType() |
| 2854 | .getAsOpaquePtr()); |
| 2855 | case clang::Type::Elaborated: |
| 2856 | return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 2857 | ->getNamedType() |
| 2858 | .getAsOpaquePtr()); |
| 2859 | case clang::Type::Typedef: |
| 2860 | return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type) |
| 2861 | ->getDecl() |
| 2862 | ->getUnderlyingType() |
| 2863 | .getAsOpaquePtr()); |
| 2864 | case clang::Type::Paren: |
| 2865 | return IsAggregateType( |
| 2866 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()); |
| 2867 | default: |
| 2868 | break; |
| 2869 | } |
| 2870 | // The clang type does have a value |
| 2871 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2872 | } |
| 2873 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2874 | bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) { |
| 2875 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 2876 | |
| 2877 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 2878 | switch (type_class) { |
| 2879 | case clang::Type::Record: { |
| 2880 | if (const clang::RecordType *record_type = |
| 2881 | llvm::dyn_cast_or_null<clang::RecordType>( |
| 2882 | qual_type.getTypePtrOrNull())) { |
| 2883 | if (const clang::RecordDecl *record_decl = record_type->getDecl()) { |
| 2884 | return record_decl->isAnonymousStructOrUnion(); |
| 2885 | } |
Enrico Granata | 7123e2b | 2015-11-07 02:06:57 +0000 | [diff] [blame] | 2886 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2887 | break; |
| 2888 | } |
| 2889 | case clang::Type::Auto: |
| 2890 | return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type) |
| 2891 | ->getDeducedType() |
| 2892 | .getAsOpaquePtr()); |
| 2893 | case clang::Type::Elaborated: |
| 2894 | return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 2895 | ->getNamedType() |
| 2896 | .getAsOpaquePtr()); |
| 2897 | case clang::Type::Typedef: |
| 2898 | return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type) |
| 2899 | ->getDecl() |
| 2900 | ->getUnderlyingType() |
| 2901 | .getAsOpaquePtr()); |
| 2902 | case clang::Type::Paren: |
| 2903 | return IsAnonymousType( |
| 2904 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()); |
| 2905 | default: |
| 2906 | break; |
| 2907 | } |
| 2908 | // The clang type does have a value |
| 2909 | return false; |
Enrico Granata | 7123e2b | 2015-11-07 02:06:57 +0000 | [diff] [blame] | 2910 | } |
| 2911 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2912 | bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type, |
| 2913 | CompilerType *element_type_ptr, |
| 2914 | uint64_t *size, bool *is_incomplete) { |
| 2915 | clang::QualType qual_type(GetCanonicalQualType(type)); |
Tamas Berghammer | 69d0b33 | 2015-10-09 12:43:08 +0000 | [diff] [blame] | 2916 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2917 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 2918 | switch (type_class) { |
| 2919 | default: |
| 2920 | break; |
Tamas Berghammer | 69d0b33 | 2015-10-09 12:43:08 +0000 | [diff] [blame] | 2921 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2922 | case clang::Type::ConstantArray: |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2923 | if (element_type_ptr) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2924 | element_type_ptr->SetCompilerType( |
| 2925 | getASTContext(), |
| 2926 | llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType()); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2927 | if (size) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2928 | *size = llvm::cast<clang::ConstantArrayType>(qual_type) |
| 2929 | ->getSize() |
| 2930 | .getLimitedValue(ULLONG_MAX); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2931 | if (is_incomplete) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2932 | *is_incomplete = false; |
| 2933 | return true; |
| 2934 | |
| 2935 | case clang::Type::IncompleteArray: |
| 2936 | if (element_type_ptr) |
| 2937 | element_type_ptr->SetCompilerType( |
| 2938 | getASTContext(), |
| 2939 | llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType()); |
| 2940 | if (size) |
| 2941 | *size = 0; |
| 2942 | if (is_incomplete) |
| 2943 | *is_incomplete = true; |
| 2944 | return true; |
| 2945 | |
| 2946 | case clang::Type::VariableArray: |
| 2947 | if (element_type_ptr) |
| 2948 | element_type_ptr->SetCompilerType( |
| 2949 | getASTContext(), |
| 2950 | llvm::cast<clang::VariableArrayType>(qual_type)->getElementType()); |
| 2951 | if (size) |
| 2952 | *size = 0; |
| 2953 | if (is_incomplete) |
| 2954 | *is_incomplete = false; |
| 2955 | return true; |
| 2956 | |
| 2957 | case clang::Type::DependentSizedArray: |
| 2958 | if (element_type_ptr) |
| 2959 | element_type_ptr->SetCompilerType( |
| 2960 | getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type) |
| 2961 | ->getElementType()); |
| 2962 | if (size) |
| 2963 | *size = 0; |
| 2964 | if (is_incomplete) |
| 2965 | *is_incomplete = false; |
| 2966 | return true; |
| 2967 | |
| 2968 | case clang::Type::Typedef: |
| 2969 | return IsArrayType(llvm::cast<clang::TypedefType>(qual_type) |
| 2970 | ->getDecl() |
| 2971 | ->getUnderlyingType() |
| 2972 | .getAsOpaquePtr(), |
| 2973 | element_type_ptr, size, is_incomplete); |
| 2974 | case clang::Type::Auto: |
| 2975 | return IsArrayType(llvm::cast<clang::AutoType>(qual_type) |
| 2976 | ->getDeducedType() |
| 2977 | .getAsOpaquePtr(), |
| 2978 | element_type_ptr, size, is_incomplete); |
| 2979 | case clang::Type::Elaborated: |
| 2980 | return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 2981 | ->getNamedType() |
| 2982 | .getAsOpaquePtr(), |
| 2983 | element_type_ptr, size, is_incomplete); |
| 2984 | case clang::Type::Paren: |
| 2985 | return IsArrayType( |
| 2986 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 2987 | element_type_ptr, size, is_incomplete); |
| 2988 | } |
| 2989 | if (element_type_ptr) |
| 2990 | element_type_ptr->Clear(); |
| 2991 | if (size) |
| 2992 | *size = 0; |
| 2993 | if (is_incomplete) |
| 2994 | *is_incomplete = false; |
| 2995 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2996 | } |
| 2997 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2998 | bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type, |
| 2999 | CompilerType *element_type, uint64_t *size) { |
| 3000 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3001 | |
| 3002 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3003 | switch (type_class) { |
| 3004 | case clang::Type::Vector: { |
| 3005 | const clang::VectorType *vector_type = |
| 3006 | qual_type->getAs<clang::VectorType>(); |
| 3007 | if (vector_type) { |
| 3008 | if (size) |
| 3009 | *size = vector_type->getNumElements(); |
| 3010 | if (element_type) |
| 3011 | *element_type = |
| 3012 | CompilerType(getASTContext(), vector_type->getElementType()); |
| 3013 | } |
| 3014 | return true; |
| 3015 | } break; |
| 3016 | case clang::Type::ExtVector: { |
| 3017 | const clang::ExtVectorType *ext_vector_type = |
| 3018 | qual_type->getAs<clang::ExtVectorType>(); |
| 3019 | if (ext_vector_type) { |
| 3020 | if (size) |
| 3021 | *size = ext_vector_type->getNumElements(); |
| 3022 | if (element_type) |
| 3023 | *element_type = |
| 3024 | CompilerType(getASTContext(), ext_vector_type->getElementType()); |
| 3025 | } |
| 3026 | return true; |
| 3027 | } |
| 3028 | default: |
| 3029 | break; |
| 3030 | } |
| 3031 | return false; |
| 3032 | } |
| 3033 | |
| 3034 | bool ClangASTContext::IsRuntimeGeneratedType( |
| 3035 | lldb::opaque_compiler_type_t type) { |
| 3036 | clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext()) |
| 3037 | ->GetDeclContextForType(GetQualType(type)); |
| 3038 | if (!decl_ctx) |
| 3039 | return false; |
| 3040 | |
| 3041 | if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx)) |
| 3042 | return false; |
| 3043 | |
| 3044 | clang::ObjCInterfaceDecl *result_iface_decl = |
| 3045 | llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx); |
| 3046 | |
| 3047 | ClangASTMetadata *ast_metadata = |
| 3048 | ClangASTContext::GetMetadata(getASTContext(), result_iface_decl); |
| 3049 | if (!ast_metadata) |
| 3050 | return false; |
| 3051 | return (ast_metadata->GetISAPtr() != 0); |
| 3052 | } |
| 3053 | |
| 3054 | bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) { |
| 3055 | return GetQualType(type).getUnqualifiedType()->isCharType(); |
| 3056 | } |
| 3057 | |
| 3058 | bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) { |
| 3059 | const bool allow_completion = false; |
| 3060 | return GetCompleteQualType(getASTContext(), GetQualType(type), |
| 3061 | allow_completion); |
| 3062 | } |
| 3063 | |
| 3064 | bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) { |
| 3065 | return GetQualType(type).isConstQualified(); |
| 3066 | } |
| 3067 | |
| 3068 | bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type, |
| 3069 | uint32_t &length) { |
| 3070 | CompilerType pointee_or_element_clang_type; |
| 3071 | length = 0; |
| 3072 | Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type)); |
| 3073 | |
| 3074 | if (!pointee_or_element_clang_type.IsValid()) |
| 3075 | return false; |
| 3076 | |
| 3077 | if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) { |
| 3078 | if (pointee_or_element_clang_type.IsCharType()) { |
| 3079 | if (type_flags.Test(eTypeIsArray)) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3080 | // We know the size of the array and it could be a C string since it is |
| 3081 | // an array of characters |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3082 | length = llvm::cast<clang::ConstantArrayType>( |
| 3083 | GetCanonicalQualType(type).getTypePtr()) |
| 3084 | ->getSize() |
| 3085 | .getLimitedValue(); |
| 3086 | } |
| 3087 | return true; |
| 3088 | } |
| 3089 | } |
| 3090 | return false; |
| 3091 | } |
| 3092 | |
| 3093 | bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type, |
| 3094 | bool *is_variadic_ptr) { |
| 3095 | if (type) { |
| 3096 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3097 | |
| 3098 | if (qual_type->isFunctionType()) { |
| 3099 | if (is_variadic_ptr) { |
| 3100 | const clang::FunctionProtoType *function_proto_type = |
| 3101 | llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr()); |
| 3102 | if (function_proto_type) |
| 3103 | *is_variadic_ptr = function_proto_type->isVariadic(); |
| 3104 | else |
| 3105 | *is_variadic_ptr = false; |
| 3106 | } |
| 3107 | return true; |
| 3108 | } |
| 3109 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3110 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3111 | switch (type_class) { |
| 3112 | default: |
| 3113 | break; |
| 3114 | case clang::Type::Typedef: |
| 3115 | return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type) |
| 3116 | ->getDecl() |
| 3117 | ->getUnderlyingType() |
| 3118 | .getAsOpaquePtr(), |
| 3119 | nullptr); |
| 3120 | case clang::Type::Auto: |
| 3121 | return IsFunctionType(llvm::cast<clang::AutoType>(qual_type) |
| 3122 | ->getDeducedType() |
| 3123 | .getAsOpaquePtr(), |
| 3124 | nullptr); |
| 3125 | case clang::Type::Elaborated: |
| 3126 | return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 3127 | ->getNamedType() |
| 3128 | .getAsOpaquePtr(), |
| 3129 | nullptr); |
| 3130 | case clang::Type::Paren: |
| 3131 | return IsFunctionType( |
| 3132 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 3133 | nullptr); |
| 3134 | case clang::Type::LValueReference: |
| 3135 | case clang::Type::RValueReference: { |
| 3136 | const clang::ReferenceType *reference_type = |
| 3137 | llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()); |
| 3138 | if (reference_type) |
| 3139 | return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(), |
| 3140 | nullptr); |
| 3141 | } break; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3142 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3143 | } |
| 3144 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3145 | } |
| 3146 | |
| 3147 | // Used to detect "Homogeneous Floating-point Aggregates" |
| 3148 | uint32_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3149 | ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type, |
| 3150 | CompilerType *base_type_ptr) { |
| 3151 | if (!type) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3152 | return 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3153 | |
| 3154 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3155 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3156 | switch (type_class) { |
| 3157 | case clang::Type::Record: |
| 3158 | if (GetCompleteType(type)) { |
| 3159 | const clang::CXXRecordDecl *cxx_record_decl = |
| 3160 | qual_type->getAsCXXRecordDecl(); |
| 3161 | if (cxx_record_decl) { |
| 3162 | if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass()) |
| 3163 | return 0; |
| 3164 | } |
| 3165 | const clang::RecordType *record_type = |
| 3166 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 3167 | if (record_type) { |
| 3168 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 3169 | if (record_decl) { |
| 3170 | // We are looking for a structure that contains only floating point |
| 3171 | // types |
| 3172 | clang::RecordDecl::field_iterator field_pos, |
| 3173 | field_end = record_decl->field_end(); |
| 3174 | uint32_t num_fields = 0; |
| 3175 | bool is_hva = false; |
| 3176 | bool is_hfa = false; |
| 3177 | clang::QualType base_qual_type; |
| 3178 | uint64_t base_bitwidth = 0; |
| 3179 | for (field_pos = record_decl->field_begin(); field_pos != field_end; |
| 3180 | ++field_pos) { |
| 3181 | clang::QualType field_qual_type = field_pos->getType(); |
| 3182 | uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type); |
| 3183 | if (field_qual_type->isFloatingType()) { |
| 3184 | if (field_qual_type->isComplexType()) |
| 3185 | return 0; |
| 3186 | else { |
| 3187 | if (num_fields == 0) |
| 3188 | base_qual_type = field_qual_type; |
| 3189 | else { |
| 3190 | if (is_hva) |
| 3191 | return 0; |
| 3192 | is_hfa = true; |
| 3193 | if (field_qual_type.getTypePtr() != |
| 3194 | base_qual_type.getTypePtr()) |
| 3195 | return 0; |
| 3196 | } |
| 3197 | } |
| 3198 | } else if (field_qual_type->isVectorType() || |
| 3199 | field_qual_type->isExtVectorType()) { |
| 3200 | if (num_fields == 0) { |
| 3201 | base_qual_type = field_qual_type; |
| 3202 | base_bitwidth = field_bitwidth; |
| 3203 | } else { |
| 3204 | if (is_hfa) |
| 3205 | return 0; |
| 3206 | is_hva = true; |
| 3207 | if (base_bitwidth != field_bitwidth) |
| 3208 | return 0; |
| 3209 | if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr()) |
| 3210 | return 0; |
| 3211 | } |
| 3212 | } else |
| 3213 | return 0; |
| 3214 | ++num_fields; |
| 3215 | } |
| 3216 | if (base_type_ptr) |
| 3217 | *base_type_ptr = CompilerType(getASTContext(), base_qual_type); |
| 3218 | return num_fields; |
| 3219 | } |
| 3220 | } |
| 3221 | } |
| 3222 | break; |
| 3223 | |
| 3224 | case clang::Type::Typedef: |
| 3225 | return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type) |
| 3226 | ->getDecl() |
| 3227 | ->getUnderlyingType() |
| 3228 | .getAsOpaquePtr(), |
| 3229 | base_type_ptr); |
| 3230 | |
| 3231 | case clang::Type::Auto: |
| 3232 | return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type) |
| 3233 | ->getDeducedType() |
| 3234 | .getAsOpaquePtr(), |
| 3235 | base_type_ptr); |
| 3236 | |
| 3237 | case clang::Type::Elaborated: |
| 3238 | return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type) |
| 3239 | ->getNamedType() |
| 3240 | .getAsOpaquePtr(), |
| 3241 | base_type_ptr); |
| 3242 | default: |
| 3243 | break; |
| 3244 | } |
| 3245 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3246 | } |
| 3247 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3248 | size_t ClangASTContext::GetNumberOfFunctionArguments( |
| 3249 | lldb::opaque_compiler_type_t type) { |
| 3250 | if (type) { |
| 3251 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3252 | const clang::FunctionProtoType *func = |
| 3253 | llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr()); |
| 3254 | if (func) |
| 3255 | return func->getNumParams(); |
| 3256 | } |
| 3257 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3258 | } |
| 3259 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 3260 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3261 | ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type, |
| 3262 | const size_t index) { |
| 3263 | if (type) { |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3264 | clang::QualType qual_type(GetQualType(type)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3265 | const clang::FunctionProtoType *func = |
| 3266 | llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr()); |
| 3267 | if (func) { |
| 3268 | if (index < func->getNumParams()) |
| 3269 | return CompilerType(getASTContext(), func->getParamType(index)); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3270 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3271 | } |
| 3272 | return CompilerType(); |
| 3273 | } |
| 3274 | |
| 3275 | bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) { |
| 3276 | if (type) { |
| 3277 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3278 | |
| 3279 | if (qual_type->isFunctionPointerType()) |
| 3280 | return true; |
| 3281 | |
| 3282 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3283 | switch (type_class) { |
| 3284 | default: |
| 3285 | break; |
| 3286 | case clang::Type::Typedef: |
| 3287 | return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type) |
| 3288 | ->getDecl() |
| 3289 | ->getUnderlyingType() |
| 3290 | .getAsOpaquePtr()); |
| 3291 | case clang::Type::Auto: |
| 3292 | return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type) |
| 3293 | ->getDeducedType() |
| 3294 | .getAsOpaquePtr()); |
| 3295 | case clang::Type::Elaborated: |
| 3296 | return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 3297 | ->getNamedType() |
| 3298 | .getAsOpaquePtr()); |
| 3299 | case clang::Type::Paren: |
| 3300 | return IsFunctionPointerType( |
| 3301 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()); |
| 3302 | |
| 3303 | case clang::Type::LValueReference: |
| 3304 | case clang::Type::RValueReference: { |
| 3305 | const clang::ReferenceType *reference_type = |
| 3306 | llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()); |
| 3307 | if (reference_type) |
| 3308 | return IsFunctionPointerType( |
| 3309 | reference_type->getPointeeType().getAsOpaquePtr()); |
| 3310 | } break; |
| 3311 | } |
| 3312 | } |
| 3313 | return false; |
| 3314 | } |
| 3315 | |
| 3316 | bool ClangASTContext::IsBlockPointerType( |
| 3317 | lldb::opaque_compiler_type_t type, |
| 3318 | CompilerType *function_pointer_type_ptr) { |
| 3319 | if (type) { |
| 3320 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3321 | |
| 3322 | if (qual_type->isBlockPointerType()) { |
| 3323 | if (function_pointer_type_ptr) { |
| 3324 | const clang::BlockPointerType *block_pointer_type = |
| 3325 | qual_type->getAs<clang::BlockPointerType>(); |
| 3326 | QualType pointee_type = block_pointer_type->getPointeeType(); |
| 3327 | QualType function_pointer_type = m_ast_ap->getPointerType(pointee_type); |
| 3328 | *function_pointer_type_ptr = |
| 3329 | CompilerType(getASTContext(), function_pointer_type); |
| 3330 | } |
| 3331 | return true; |
| 3332 | } |
| 3333 | |
| 3334 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3335 | switch (type_class) { |
| 3336 | default: |
| 3337 | break; |
| 3338 | case clang::Type::Typedef: |
| 3339 | return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type) |
| 3340 | ->getDecl() |
| 3341 | ->getUnderlyingType() |
| 3342 | .getAsOpaquePtr(), |
| 3343 | function_pointer_type_ptr); |
| 3344 | case clang::Type::Auto: |
| 3345 | return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type) |
| 3346 | ->getDeducedType() |
| 3347 | .getAsOpaquePtr(), |
| 3348 | function_pointer_type_ptr); |
| 3349 | case clang::Type::Elaborated: |
| 3350 | return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 3351 | ->getNamedType() |
| 3352 | .getAsOpaquePtr(), |
| 3353 | function_pointer_type_ptr); |
| 3354 | case clang::Type::Paren: |
| 3355 | return IsBlockPointerType( |
| 3356 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 3357 | function_pointer_type_ptr); |
| 3358 | |
| 3359 | case clang::Type::LValueReference: |
| 3360 | case clang::Type::RValueReference: { |
| 3361 | const clang::ReferenceType *reference_type = |
| 3362 | llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()); |
| 3363 | if (reference_type) |
| 3364 | return IsBlockPointerType( |
| 3365 | reference_type->getPointeeType().getAsOpaquePtr(), |
| 3366 | function_pointer_type_ptr); |
| 3367 | } break; |
| 3368 | } |
| 3369 | } |
| 3370 | return false; |
| 3371 | } |
| 3372 | |
| 3373 | bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type, |
| 3374 | bool &is_signed) { |
| 3375 | if (!type) |
| 3376 | return false; |
| 3377 | |
| 3378 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3379 | const clang::BuiltinType *builtin_type = |
| 3380 | llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal()); |
| 3381 | |
| 3382 | if (builtin_type) { |
| 3383 | if (builtin_type->isInteger()) { |
| 3384 | is_signed = builtin_type->isSignedInteger(); |
| 3385 | return true; |
| 3386 | } |
| 3387 | } |
| 3388 | |
| 3389 | return false; |
| 3390 | } |
| 3391 | |
| 3392 | bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type, |
| 3393 | bool &is_signed) { |
| 3394 | if (type) { |
| 3395 | const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>( |
| 3396 | GetCanonicalQualType(type)->getCanonicalTypeInternal()); |
| 3397 | |
| 3398 | if (enum_type) { |
| 3399 | IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(), |
| 3400 | is_signed); |
| 3401 | return true; |
| 3402 | } |
| 3403 | } |
| 3404 | |
| 3405 | return false; |
| 3406 | } |
| 3407 | |
| 3408 | bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type, |
| 3409 | CompilerType *pointee_type) { |
| 3410 | if (type) { |
| 3411 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3412 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3413 | switch (type_class) { |
| 3414 | case clang::Type::Builtin: |
| 3415 | switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { |
| 3416 | default: |
| 3417 | break; |
| 3418 | case clang::BuiltinType::ObjCId: |
| 3419 | case clang::BuiltinType::ObjCClass: |
| 3420 | return true; |
| 3421 | } |
| 3422 | return false; |
| 3423 | case clang::Type::ObjCObjectPointer: |
| 3424 | if (pointee_type) |
| 3425 | pointee_type->SetCompilerType( |
| 3426 | getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type) |
| 3427 | ->getPointeeType()); |
| 3428 | return true; |
| 3429 | case clang::Type::BlockPointer: |
| 3430 | if (pointee_type) |
| 3431 | pointee_type->SetCompilerType( |
| 3432 | getASTContext(), |
| 3433 | llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType()); |
| 3434 | return true; |
| 3435 | case clang::Type::Pointer: |
| 3436 | if (pointee_type) |
| 3437 | pointee_type->SetCompilerType( |
| 3438 | getASTContext(), |
| 3439 | llvm::cast<clang::PointerType>(qual_type)->getPointeeType()); |
| 3440 | return true; |
| 3441 | case clang::Type::MemberPointer: |
| 3442 | if (pointee_type) |
| 3443 | pointee_type->SetCompilerType( |
| 3444 | getASTContext(), |
| 3445 | llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType()); |
| 3446 | return true; |
| 3447 | case clang::Type::Typedef: |
| 3448 | return IsPointerType(llvm::cast<clang::TypedefType>(qual_type) |
| 3449 | ->getDecl() |
| 3450 | ->getUnderlyingType() |
| 3451 | .getAsOpaquePtr(), |
| 3452 | pointee_type); |
| 3453 | case clang::Type::Auto: |
| 3454 | return IsPointerType(llvm::cast<clang::AutoType>(qual_type) |
| 3455 | ->getDeducedType() |
| 3456 | .getAsOpaquePtr(), |
| 3457 | pointee_type); |
| 3458 | case clang::Type::Elaborated: |
| 3459 | return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 3460 | ->getNamedType() |
| 3461 | .getAsOpaquePtr(), |
| 3462 | pointee_type); |
| 3463 | case clang::Type::Paren: |
| 3464 | return IsPointerType( |
| 3465 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 3466 | pointee_type); |
| 3467 | default: |
| 3468 | break; |
| 3469 | } |
| 3470 | } |
| 3471 | if (pointee_type) |
| 3472 | pointee_type->Clear(); |
| 3473 | return false; |
| 3474 | } |
| 3475 | |
| 3476 | bool ClangASTContext::IsPointerOrReferenceType( |
| 3477 | lldb::opaque_compiler_type_t type, CompilerType *pointee_type) { |
| 3478 | if (type) { |
| 3479 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3480 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3481 | switch (type_class) { |
| 3482 | case clang::Type::Builtin: |
| 3483 | switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { |
| 3484 | default: |
| 3485 | break; |
| 3486 | case clang::BuiltinType::ObjCId: |
| 3487 | case clang::BuiltinType::ObjCClass: |
| 3488 | return true; |
| 3489 | } |
| 3490 | return false; |
| 3491 | case clang::Type::ObjCObjectPointer: |
| 3492 | if (pointee_type) |
| 3493 | pointee_type->SetCompilerType( |
| 3494 | getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type) |
| 3495 | ->getPointeeType()); |
| 3496 | return true; |
| 3497 | case clang::Type::BlockPointer: |
| 3498 | if (pointee_type) |
| 3499 | pointee_type->SetCompilerType( |
| 3500 | getASTContext(), |
| 3501 | llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType()); |
| 3502 | return true; |
| 3503 | case clang::Type::Pointer: |
| 3504 | if (pointee_type) |
| 3505 | pointee_type->SetCompilerType( |
| 3506 | getASTContext(), |
| 3507 | llvm::cast<clang::PointerType>(qual_type)->getPointeeType()); |
| 3508 | return true; |
| 3509 | case clang::Type::MemberPointer: |
| 3510 | if (pointee_type) |
| 3511 | pointee_type->SetCompilerType( |
| 3512 | getASTContext(), |
| 3513 | llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType()); |
| 3514 | return true; |
| 3515 | case clang::Type::LValueReference: |
| 3516 | if (pointee_type) |
| 3517 | pointee_type->SetCompilerType( |
| 3518 | getASTContext(), |
| 3519 | llvm::cast<clang::LValueReferenceType>(qual_type)->desugar()); |
| 3520 | return true; |
| 3521 | case clang::Type::RValueReference: |
| 3522 | if (pointee_type) |
| 3523 | pointee_type->SetCompilerType( |
| 3524 | getASTContext(), |
| 3525 | llvm::cast<clang::RValueReferenceType>(qual_type)->desugar()); |
| 3526 | return true; |
| 3527 | case clang::Type::Typedef: |
| 3528 | return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type) |
| 3529 | ->getDecl() |
| 3530 | ->getUnderlyingType() |
| 3531 | .getAsOpaquePtr(), |
| 3532 | pointee_type); |
| 3533 | case clang::Type::Auto: |
| 3534 | return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type) |
| 3535 | ->getDeducedType() |
| 3536 | .getAsOpaquePtr(), |
| 3537 | pointee_type); |
| 3538 | case clang::Type::Elaborated: |
| 3539 | return IsPointerOrReferenceType( |
| 3540 | llvm::cast<clang::ElaboratedType>(qual_type) |
| 3541 | ->getNamedType() |
| 3542 | .getAsOpaquePtr(), |
| 3543 | pointee_type); |
| 3544 | case clang::Type::Paren: |
| 3545 | return IsPointerOrReferenceType( |
| 3546 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 3547 | pointee_type); |
| 3548 | default: |
| 3549 | break; |
| 3550 | } |
| 3551 | } |
| 3552 | if (pointee_type) |
| 3553 | pointee_type->Clear(); |
| 3554 | return false; |
| 3555 | } |
| 3556 | |
| 3557 | bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type, |
| 3558 | CompilerType *pointee_type, |
| 3559 | bool *is_rvalue) { |
| 3560 | if (type) { |
| 3561 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3562 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3563 | |
| 3564 | switch (type_class) { |
| 3565 | case clang::Type::LValueReference: |
| 3566 | if (pointee_type) |
| 3567 | pointee_type->SetCompilerType( |
| 3568 | getASTContext(), |
| 3569 | llvm::cast<clang::LValueReferenceType>(qual_type)->desugar()); |
| 3570 | if (is_rvalue) |
| 3571 | *is_rvalue = false; |
| 3572 | return true; |
| 3573 | case clang::Type::RValueReference: |
| 3574 | if (pointee_type) |
| 3575 | pointee_type->SetCompilerType( |
| 3576 | getASTContext(), |
| 3577 | llvm::cast<clang::RValueReferenceType>(qual_type)->desugar()); |
| 3578 | if (is_rvalue) |
| 3579 | *is_rvalue = true; |
| 3580 | return true; |
| 3581 | case clang::Type::Typedef: |
| 3582 | return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type) |
| 3583 | ->getDecl() |
| 3584 | ->getUnderlyingType() |
| 3585 | .getAsOpaquePtr(), |
| 3586 | pointee_type, is_rvalue); |
| 3587 | case clang::Type::Auto: |
| 3588 | return IsReferenceType(llvm::cast<clang::AutoType>(qual_type) |
| 3589 | ->getDeducedType() |
| 3590 | .getAsOpaquePtr(), |
| 3591 | pointee_type, is_rvalue); |
| 3592 | case clang::Type::Elaborated: |
| 3593 | return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 3594 | ->getNamedType() |
| 3595 | .getAsOpaquePtr(), |
| 3596 | pointee_type, is_rvalue); |
| 3597 | case clang::Type::Paren: |
| 3598 | return IsReferenceType( |
| 3599 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 3600 | pointee_type, is_rvalue); |
| 3601 | |
| 3602 | default: |
| 3603 | break; |
| 3604 | } |
| 3605 | } |
| 3606 | if (pointee_type) |
| 3607 | pointee_type->Clear(); |
| 3608 | return false; |
| 3609 | } |
| 3610 | |
| 3611 | bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type, |
| 3612 | uint32_t &count, bool &is_complex) { |
| 3613 | if (type) { |
| 3614 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3615 | |
| 3616 | if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>( |
| 3617 | qual_type->getCanonicalTypeInternal())) { |
| 3618 | clang::BuiltinType::Kind kind = BT->getKind(); |
| 3619 | if (kind >= clang::BuiltinType::Float && |
| 3620 | kind <= clang::BuiltinType::LongDouble) { |
| 3621 | count = 1; |
| 3622 | is_complex = false; |
| 3623 | return true; |
| 3624 | } |
| 3625 | } else if (const clang::ComplexType *CT = |
| 3626 | llvm::dyn_cast<clang::ComplexType>( |
| 3627 | qual_type->getCanonicalTypeInternal())) { |
| 3628 | if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count, |
| 3629 | is_complex)) { |
| 3630 | count = 2; |
| 3631 | is_complex = true; |
| 3632 | return true; |
| 3633 | } |
| 3634 | } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>( |
| 3635 | qual_type->getCanonicalTypeInternal())) { |
| 3636 | if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count, |
| 3637 | is_complex)) { |
| 3638 | count = VT->getNumElements(); |
| 3639 | is_complex = false; |
| 3640 | return true; |
| 3641 | } |
| 3642 | } |
| 3643 | } |
| 3644 | count = 0; |
| 3645 | is_complex = false; |
| 3646 | return false; |
| 3647 | } |
| 3648 | |
| 3649 | bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) { |
| 3650 | if (!type) |
| 3651 | return false; |
| 3652 | |
| 3653 | clang::QualType qual_type(GetQualType(type)); |
| 3654 | const clang::TagType *tag_type = |
| 3655 | llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()); |
| 3656 | if (tag_type) { |
| 3657 | clang::TagDecl *tag_decl = tag_type->getDecl(); |
| 3658 | if (tag_decl) |
| 3659 | return tag_decl->isCompleteDefinition(); |
| 3660 | return false; |
| 3661 | } else { |
| 3662 | const clang::ObjCObjectType *objc_class_type = |
| 3663 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type); |
| 3664 | if (objc_class_type) { |
| 3665 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 3666 | objc_class_type->getInterface(); |
| 3667 | if (class_interface_decl) |
| 3668 | return class_interface_decl->getDefinition() != nullptr; |
| 3669 | return false; |
| 3670 | } |
| 3671 | } |
| 3672 | return true; |
| 3673 | } |
| 3674 | |
| 3675 | bool ClangASTContext::IsObjCClassType(const CompilerType &type) { |
| 3676 | if (type) { |
| 3677 | clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type)); |
| 3678 | |
| 3679 | const clang::ObjCObjectPointerType *obj_pointer_type = |
| 3680 | llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type); |
| 3681 | |
| 3682 | if (obj_pointer_type) |
| 3683 | return obj_pointer_type->isObjCClassType(); |
| 3684 | } |
| 3685 | return false; |
| 3686 | } |
| 3687 | |
| 3688 | bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) { |
| 3689 | if (ClangUtil::IsClangType(type)) |
| 3690 | return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType(); |
| 3691 | return false; |
| 3692 | } |
| 3693 | |
| 3694 | bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) { |
| 3695 | if (!type) |
| 3696 | return false; |
| 3697 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3698 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3699 | return (type_class == clang::Type::Record); |
| 3700 | } |
| 3701 | |
| 3702 | bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) { |
| 3703 | if (!type) |
| 3704 | return false; |
| 3705 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3706 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3707 | return (type_class == clang::Type::Enum); |
| 3708 | } |
| 3709 | |
| 3710 | bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) { |
| 3711 | if (type) { |
| 3712 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3713 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3714 | switch (type_class) { |
| 3715 | case clang::Type::Record: |
| 3716 | if (GetCompleteType(type)) { |
| 3717 | const clang::RecordType *record_type = |
| 3718 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 3719 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 3720 | if (record_decl) { |
| 3721 | const clang::CXXRecordDecl *cxx_record_decl = |
| 3722 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 3723 | if (cxx_record_decl) |
| 3724 | return cxx_record_decl->isPolymorphic(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3725 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3726 | } |
| 3727 | break; |
| 3728 | |
| 3729 | default: |
| 3730 | break; |
| 3731 | } |
| 3732 | } |
| 3733 | return false; |
| 3734 | } |
| 3735 | |
| 3736 | bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type, |
| 3737 | CompilerType *dynamic_pointee_type, |
| 3738 | bool check_cplusplus, |
| 3739 | bool check_objc) { |
| 3740 | clang::QualType pointee_qual_type; |
| 3741 | if (type) { |
| 3742 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3743 | bool success = false; |
| 3744 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3745 | switch (type_class) { |
| 3746 | case clang::Type::Builtin: |
| 3747 | if (check_objc && |
| 3748 | llvm::cast<clang::BuiltinType>(qual_type)->getKind() == |
| 3749 | clang::BuiltinType::ObjCId) { |
| 3750 | if (dynamic_pointee_type) |
| 3751 | dynamic_pointee_type->SetCompilerType(this, type); |
| 3752 | return true; |
| 3753 | } |
| 3754 | break; |
| 3755 | |
| 3756 | case clang::Type::ObjCObjectPointer: |
| 3757 | if (check_objc) { |
| 3758 | if (auto objc_pointee_type = |
| 3759 | qual_type->getPointeeType().getTypePtrOrNull()) { |
| 3760 | if (auto objc_object_type = |
| 3761 | llvm::dyn_cast_or_null<clang::ObjCObjectType>( |
| 3762 | objc_pointee_type)) { |
| 3763 | if (objc_object_type->isObjCClass()) |
| 3764 | return false; |
| 3765 | } |
| 3766 | } |
| 3767 | if (dynamic_pointee_type) |
| 3768 | dynamic_pointee_type->SetCompilerType( |
| 3769 | getASTContext(), |
| 3770 | llvm::cast<clang::ObjCObjectPointerType>(qual_type) |
| 3771 | ->getPointeeType()); |
| 3772 | return true; |
| 3773 | } |
| 3774 | break; |
| 3775 | |
| 3776 | case clang::Type::Pointer: |
| 3777 | pointee_qual_type = |
| 3778 | llvm::cast<clang::PointerType>(qual_type)->getPointeeType(); |
| 3779 | success = true; |
| 3780 | break; |
| 3781 | |
| 3782 | case clang::Type::LValueReference: |
| 3783 | case clang::Type::RValueReference: |
| 3784 | pointee_qual_type = |
| 3785 | llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType(); |
| 3786 | success = true; |
| 3787 | break; |
| 3788 | |
| 3789 | case clang::Type::Typedef: |
| 3790 | return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type) |
| 3791 | ->getDecl() |
| 3792 | ->getUnderlyingType() |
| 3793 | .getAsOpaquePtr(), |
| 3794 | dynamic_pointee_type, check_cplusplus, |
| 3795 | check_objc); |
| 3796 | |
| 3797 | case clang::Type::Auto: |
| 3798 | return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type) |
| 3799 | ->getDeducedType() |
| 3800 | .getAsOpaquePtr(), |
| 3801 | dynamic_pointee_type, check_cplusplus, |
| 3802 | check_objc); |
| 3803 | |
| 3804 | case clang::Type::Elaborated: |
| 3805 | return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 3806 | ->getNamedType() |
| 3807 | .getAsOpaquePtr(), |
| 3808 | dynamic_pointee_type, check_cplusplus, |
| 3809 | check_objc); |
| 3810 | |
| 3811 | case clang::Type::Paren: |
| 3812 | return IsPossibleDynamicType( |
| 3813 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 3814 | dynamic_pointee_type, check_cplusplus, check_objc); |
| 3815 | default: |
| 3816 | break; |
| 3817 | } |
| 3818 | |
| 3819 | if (success) { |
| 3820 | // 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] | 3821 | // type We currently accept any "void *" (in case we have a class that |
| 3822 | // has been watered down to an opaque pointer) and virtual C++ classes. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3823 | const clang::Type::TypeClass pointee_type_class = |
| 3824 | pointee_qual_type.getCanonicalType()->getTypeClass(); |
| 3825 | switch (pointee_type_class) { |
| 3826 | case clang::Type::Builtin: |
| 3827 | switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) { |
| 3828 | case clang::BuiltinType::UnknownAny: |
| 3829 | case clang::BuiltinType::Void: |
| 3830 | if (dynamic_pointee_type) |
| 3831 | dynamic_pointee_type->SetCompilerType(getASTContext(), |
| 3832 | pointee_qual_type); |
| 3833 | return true; |
| 3834 | default: |
| 3835 | break; |
| 3836 | } |
| 3837 | break; |
| 3838 | |
| 3839 | case clang::Type::Record: |
| 3840 | if (check_cplusplus) { |
| 3841 | clang::CXXRecordDecl *cxx_record_decl = |
| 3842 | pointee_qual_type->getAsCXXRecordDecl(); |
| 3843 | if (cxx_record_decl) { |
| 3844 | bool is_complete = cxx_record_decl->isCompleteDefinition(); |
| 3845 | |
| 3846 | if (is_complete) |
| 3847 | success = cxx_record_decl->isDynamicClass(); |
| 3848 | else { |
| 3849 | ClangASTMetadata *metadata = ClangASTContext::GetMetadata( |
| 3850 | getASTContext(), cxx_record_decl); |
| 3851 | if (metadata) |
| 3852 | success = metadata->GetIsDynamicCXXType(); |
| 3853 | else { |
| 3854 | is_complete = CompilerType(getASTContext(), pointee_qual_type) |
| 3855 | .GetCompleteType(); |
| 3856 | if (is_complete) |
| 3857 | success = cxx_record_decl->isDynamicClass(); |
| 3858 | else |
| 3859 | success = false; |
| 3860 | } |
| 3861 | } |
| 3862 | |
| 3863 | if (success) { |
| 3864 | if (dynamic_pointee_type) |
| 3865 | dynamic_pointee_type->SetCompilerType(getASTContext(), |
| 3866 | pointee_qual_type); |
| 3867 | return true; |
| 3868 | } |
| 3869 | } |
| 3870 | } |
| 3871 | break; |
| 3872 | |
| 3873 | case clang::Type::ObjCObject: |
| 3874 | case clang::Type::ObjCInterface: |
| 3875 | if (check_objc) { |
| 3876 | if (dynamic_pointee_type) |
| 3877 | dynamic_pointee_type->SetCompilerType(getASTContext(), |
| 3878 | pointee_qual_type); |
| 3879 | return true; |
| 3880 | } |
| 3881 | break; |
| 3882 | |
| 3883 | default: |
| 3884 | break; |
| 3885 | } |
| 3886 | } |
| 3887 | } |
| 3888 | if (dynamic_pointee_type) |
| 3889 | dynamic_pointee_type->Clear(); |
| 3890 | return false; |
| 3891 | } |
| 3892 | |
| 3893 | bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) { |
| 3894 | if (!type) |
| 3895 | return false; |
| 3896 | |
| 3897 | return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0; |
| 3898 | } |
| 3899 | |
| 3900 | bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) { |
| 3901 | if (!type) |
| 3902 | return false; |
| 3903 | return GetQualType(type)->getTypeClass() == clang::Type::Typedef; |
| 3904 | } |
| 3905 | |
| 3906 | bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) { |
| 3907 | if (!type) |
| 3908 | return false; |
| 3909 | return GetCanonicalQualType(type)->isVoidType(); |
| 3910 | } |
| 3911 | |
| 3912 | bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) { |
| 3913 | return ClangASTContextSupportsLanguage(language); |
| 3914 | } |
| 3915 | |
| 3916 | bool ClangASTContext::GetCXXClassName(const CompilerType &type, |
| 3917 | std::string &class_name) { |
| 3918 | if (type) { |
| 3919 | clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type)); |
| 3920 | if (!qual_type.isNull()) { |
| 3921 | clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); |
| 3922 | if (cxx_record_decl) { |
| 3923 | class_name.assign(cxx_record_decl->getIdentifier()->getNameStart()); |
| 3924 | return true; |
| 3925 | } |
| 3926 | } |
| 3927 | } |
| 3928 | class_name.clear(); |
| 3929 | return false; |
| 3930 | } |
| 3931 | |
| 3932 | bool ClangASTContext::IsCXXClassType(const CompilerType &type) { |
| 3933 | if (!type) |
| 3934 | return false; |
| 3935 | |
| 3936 | clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type)); |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 3937 | return !qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3938 | } |
| 3939 | |
| 3940 | bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) { |
| 3941 | if (!type) |
| 3942 | return false; |
| 3943 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3944 | const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type); |
| 3945 | if (tag_type) |
| 3946 | return tag_type->isBeingDefined(); |
| 3947 | return false; |
| 3948 | } |
| 3949 | |
| 3950 | bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type, |
| 3951 | CompilerType *class_type_ptr) { |
| 3952 | if (!type) |
| 3953 | return false; |
| 3954 | |
| 3955 | clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type)); |
| 3956 | |
| 3957 | if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) { |
| 3958 | if (class_type_ptr) { |
| 3959 | if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) { |
| 3960 | const clang::ObjCObjectPointerType *obj_pointer_type = |
| 3961 | llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type); |
| 3962 | if (obj_pointer_type == nullptr) |
| 3963 | class_type_ptr->Clear(); |
| 3964 | else |
| 3965 | class_type_ptr->SetCompilerType( |
| 3966 | type.GetTypeSystem(), |
| 3967 | clang::QualType(obj_pointer_type->getInterfaceType(), 0) |
| 3968 | .getAsOpaquePtr()); |
| 3969 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3970 | } |
| 3971 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3972 | } |
| 3973 | if (class_type_ptr) |
| 3974 | class_type_ptr->Clear(); |
| 3975 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3976 | } |
| 3977 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3978 | bool ClangASTContext::GetObjCClassName(const CompilerType &type, |
| 3979 | std::string &class_name) { |
| 3980 | if (!type) |
| 3981 | return false; |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 3982 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3983 | clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type)); |
| 3984 | |
| 3985 | const clang::ObjCObjectType *object_type = |
| 3986 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type); |
| 3987 | if (object_type) { |
| 3988 | const clang::ObjCInterfaceDecl *interface = object_type->getInterface(); |
| 3989 | if (interface) { |
| 3990 | class_name = interface->getNameAsString(); |
| 3991 | return true; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3992 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3993 | } |
| 3994 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3995 | } |
| 3996 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3997 | //---------------------------------------------------------------------- |
| 3998 | // Type Completion |
| 3999 | //---------------------------------------------------------------------- |
| 4000 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4001 | bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) { |
| 4002 | if (!type) |
| 4003 | return false; |
| 4004 | const bool allow_completion = true; |
| 4005 | return GetCompleteQualType(getASTContext(), GetQualType(type), |
| 4006 | allow_completion); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4007 | } |
| 4008 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4009 | ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) { |
| 4010 | std::string type_name; |
| 4011 | if (type) { |
| 4012 | clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy()); |
| 4013 | clang::QualType qual_type(GetQualType(type)); |
| 4014 | printing_policy.SuppressTagKeyword = true; |
| 4015 | const clang::TypedefType *typedef_type = |
| 4016 | qual_type->getAs<clang::TypedefType>(); |
| 4017 | if (typedef_type) { |
| 4018 | const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl(); |
| 4019 | type_name = typedef_decl->getQualifiedNameAsString(); |
| 4020 | } else { |
| 4021 | type_name = qual_type.getAsString(printing_policy); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4022 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4023 | } |
| 4024 | return ConstString(type_name); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4025 | } |
| 4026 | |
| 4027 | uint32_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4028 | ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type, |
| 4029 | CompilerType *pointee_or_element_clang_type) { |
| 4030 | if (!type) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4031 | return 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4032 | |
| 4033 | if (pointee_or_element_clang_type) |
| 4034 | pointee_or_element_clang_type->Clear(); |
| 4035 | |
| 4036 | clang::QualType qual_type(GetQualType(type)); |
| 4037 | |
| 4038 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 4039 | switch (type_class) { |
Sean Callanan | ddf802a | 2017-06-02 01:24:18 +0000 | [diff] [blame] | 4040 | case clang::Type::Attributed: |
| 4041 | return GetTypeInfo( |
| 4042 | qual_type->getAs<clang::AttributedType>() |
| 4043 | ->getModifiedType().getAsOpaquePtr(), |
| 4044 | pointee_or_element_clang_type); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4045 | case clang::Type::Builtin: { |
| 4046 | const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>( |
| 4047 | qual_type->getCanonicalTypeInternal()); |
| 4048 | |
| 4049 | uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue; |
| 4050 | switch (builtin_type->getKind()) { |
| 4051 | case clang::BuiltinType::ObjCId: |
| 4052 | case clang::BuiltinType::ObjCClass: |
| 4053 | if (pointee_or_element_clang_type) |
| 4054 | pointee_or_element_clang_type->SetCompilerType( |
| 4055 | getASTContext(), getASTContext()->ObjCBuiltinClassTy); |
| 4056 | builtin_type_flags |= eTypeIsPointer | eTypeIsObjC; |
| 4057 | break; |
| 4058 | |
| 4059 | case clang::BuiltinType::ObjCSel: |
| 4060 | if (pointee_or_element_clang_type) |
| 4061 | pointee_or_element_clang_type->SetCompilerType(getASTContext(), |
| 4062 | getASTContext()->CharTy); |
| 4063 | builtin_type_flags |= eTypeIsPointer | eTypeIsObjC; |
| 4064 | break; |
| 4065 | |
| 4066 | case clang::BuiltinType::Bool: |
| 4067 | case clang::BuiltinType::Char_U: |
| 4068 | case clang::BuiltinType::UChar: |
| 4069 | case clang::BuiltinType::WChar_U: |
| 4070 | case clang::BuiltinType::Char16: |
| 4071 | case clang::BuiltinType::Char32: |
| 4072 | case clang::BuiltinType::UShort: |
| 4073 | case clang::BuiltinType::UInt: |
| 4074 | case clang::BuiltinType::ULong: |
| 4075 | case clang::BuiltinType::ULongLong: |
| 4076 | case clang::BuiltinType::UInt128: |
| 4077 | case clang::BuiltinType::Char_S: |
| 4078 | case clang::BuiltinType::SChar: |
| 4079 | case clang::BuiltinType::WChar_S: |
| 4080 | case clang::BuiltinType::Short: |
| 4081 | case clang::BuiltinType::Int: |
| 4082 | case clang::BuiltinType::Long: |
| 4083 | case clang::BuiltinType::LongLong: |
| 4084 | case clang::BuiltinType::Int128: |
| 4085 | case clang::BuiltinType::Float: |
| 4086 | case clang::BuiltinType::Double: |
| 4087 | case clang::BuiltinType::LongDouble: |
| 4088 | builtin_type_flags |= eTypeIsScalar; |
| 4089 | if (builtin_type->isInteger()) { |
| 4090 | builtin_type_flags |= eTypeIsInteger; |
| 4091 | if (builtin_type->isSignedInteger()) |
| 4092 | builtin_type_flags |= eTypeIsSigned; |
| 4093 | } else if (builtin_type->isFloatingPoint()) |
| 4094 | builtin_type_flags |= eTypeIsFloat; |
| 4095 | break; |
| 4096 | default: |
| 4097 | break; |
| 4098 | } |
| 4099 | return builtin_type_flags; |
| 4100 | } |
| 4101 | |
| 4102 | case clang::Type::BlockPointer: |
| 4103 | if (pointee_or_element_clang_type) |
| 4104 | pointee_or_element_clang_type->SetCompilerType( |
| 4105 | getASTContext(), qual_type->getPointeeType()); |
| 4106 | return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock; |
| 4107 | |
| 4108 | case clang::Type::Complex: { |
| 4109 | uint32_t complex_type_flags = |
| 4110 | eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex; |
| 4111 | const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>( |
| 4112 | qual_type->getCanonicalTypeInternal()); |
| 4113 | if (complex_type) { |
| 4114 | clang::QualType complex_element_type(complex_type->getElementType()); |
| 4115 | if (complex_element_type->isIntegerType()) |
| 4116 | complex_type_flags |= eTypeIsFloat; |
| 4117 | else if (complex_element_type->isFloatingType()) |
| 4118 | complex_type_flags |= eTypeIsInteger; |
| 4119 | } |
| 4120 | return complex_type_flags; |
| 4121 | } break; |
| 4122 | |
| 4123 | case clang::Type::ConstantArray: |
| 4124 | case clang::Type::DependentSizedArray: |
| 4125 | case clang::Type::IncompleteArray: |
| 4126 | case clang::Type::VariableArray: |
| 4127 | if (pointee_or_element_clang_type) |
| 4128 | pointee_or_element_clang_type->SetCompilerType( |
| 4129 | getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr()) |
| 4130 | ->getElementType()); |
| 4131 | return eTypeHasChildren | eTypeIsArray; |
| 4132 | |
| 4133 | case clang::Type::DependentName: |
| 4134 | return 0; |
| 4135 | case clang::Type::DependentSizedExtVector: |
| 4136 | return eTypeHasChildren | eTypeIsVector; |
| 4137 | case clang::Type::DependentTemplateSpecialization: |
| 4138 | return eTypeIsTemplate; |
| 4139 | case clang::Type::Decltype: |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 4140 | return CompilerType( |
| 4141 | getASTContext(), |
| 4142 | llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType()) |
| 4143 | .GetTypeInfo(pointee_or_element_clang_type); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4144 | |
| 4145 | case clang::Type::Enum: |
| 4146 | if (pointee_or_element_clang_type) |
| 4147 | pointee_or_element_clang_type->SetCompilerType( |
| 4148 | getASTContext(), |
| 4149 | llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType()); |
| 4150 | return eTypeIsEnumeration | eTypeHasValue; |
| 4151 | |
| 4152 | case clang::Type::Auto: |
| 4153 | return CompilerType( |
| 4154 | getASTContext(), |
| 4155 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) |
| 4156 | .GetTypeInfo(pointee_or_element_clang_type); |
| 4157 | case clang::Type::Elaborated: |
| 4158 | return CompilerType( |
| 4159 | getASTContext(), |
| 4160 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) |
| 4161 | .GetTypeInfo(pointee_or_element_clang_type); |
| 4162 | case clang::Type::Paren: |
| 4163 | return CompilerType(getASTContext(), |
| 4164 | llvm::cast<clang::ParenType>(qual_type)->desugar()) |
| 4165 | .GetTypeInfo(pointee_or_element_clang_type); |
| 4166 | |
| 4167 | case clang::Type::FunctionProto: |
| 4168 | return eTypeIsFuncPrototype | eTypeHasValue; |
| 4169 | case clang::Type::FunctionNoProto: |
| 4170 | return eTypeIsFuncPrototype | eTypeHasValue; |
| 4171 | case clang::Type::InjectedClassName: |
| 4172 | return 0; |
| 4173 | |
| 4174 | case clang::Type::LValueReference: |
| 4175 | case clang::Type::RValueReference: |
| 4176 | if (pointee_or_element_clang_type) |
| 4177 | pointee_or_element_clang_type->SetCompilerType( |
| 4178 | getASTContext(), |
| 4179 | llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()) |
| 4180 | ->getPointeeType()); |
| 4181 | return eTypeHasChildren | eTypeIsReference | eTypeHasValue; |
| 4182 | |
| 4183 | case clang::Type::MemberPointer: |
| 4184 | return eTypeIsPointer | eTypeIsMember | eTypeHasValue; |
| 4185 | |
| 4186 | case clang::Type::ObjCObjectPointer: |
| 4187 | if (pointee_or_element_clang_type) |
| 4188 | pointee_or_element_clang_type->SetCompilerType( |
| 4189 | getASTContext(), qual_type->getPointeeType()); |
| 4190 | return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | |
| 4191 | eTypeHasValue; |
| 4192 | |
| 4193 | case clang::Type::ObjCObject: |
| 4194 | return eTypeHasChildren | eTypeIsObjC | eTypeIsClass; |
| 4195 | case clang::Type::ObjCInterface: |
| 4196 | return eTypeHasChildren | eTypeIsObjC | eTypeIsClass; |
| 4197 | |
| 4198 | case clang::Type::Pointer: |
| 4199 | if (pointee_or_element_clang_type) |
| 4200 | pointee_or_element_clang_type->SetCompilerType( |
| 4201 | getASTContext(), qual_type->getPointeeType()); |
| 4202 | return eTypeHasChildren | eTypeIsPointer | eTypeHasValue; |
| 4203 | |
| 4204 | case clang::Type::Record: |
| 4205 | if (qual_type->getAsCXXRecordDecl()) |
| 4206 | return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus; |
| 4207 | else |
| 4208 | return eTypeHasChildren | eTypeIsStructUnion; |
| 4209 | break; |
| 4210 | case clang::Type::SubstTemplateTypeParm: |
| 4211 | return eTypeIsTemplate; |
| 4212 | case clang::Type::TemplateTypeParm: |
| 4213 | return eTypeIsTemplate; |
| 4214 | case clang::Type::TemplateSpecialization: |
| 4215 | return eTypeIsTemplate; |
| 4216 | |
| 4217 | case clang::Type::Typedef: |
| 4218 | return eTypeIsTypedef | |
| 4219 | CompilerType(getASTContext(), |
| 4220 | llvm::cast<clang::TypedefType>(qual_type) |
| 4221 | ->getDecl() |
| 4222 | ->getUnderlyingType()) |
| 4223 | .GetTypeInfo(pointee_or_element_clang_type); |
| 4224 | case clang::Type::TypeOfExpr: |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 4225 | return CompilerType(getASTContext(), |
| 4226 | llvm::cast<clang::TypeOfExprType>(qual_type) |
| 4227 | ->getUnderlyingExpr() |
| 4228 | ->getType()) |
| 4229 | .GetTypeInfo(pointee_or_element_clang_type); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4230 | case clang::Type::TypeOf: |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 4231 | return CompilerType( |
| 4232 | getASTContext(), |
| 4233 | llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType()) |
| 4234 | .GetTypeInfo(pointee_or_element_clang_type); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4235 | case clang::Type::UnresolvedUsing: |
| 4236 | return 0; |
| 4237 | |
| 4238 | case clang::Type::ExtVector: |
| 4239 | case clang::Type::Vector: { |
| 4240 | uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector; |
| 4241 | const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>( |
| 4242 | qual_type->getCanonicalTypeInternal()); |
| 4243 | if (vector_type) { |
| 4244 | if (vector_type->isIntegerType()) |
| 4245 | vector_type_flags |= eTypeIsFloat; |
| 4246 | else if (vector_type->isFloatingType()) |
| 4247 | vector_type_flags |= eTypeIsInteger; |
| 4248 | } |
| 4249 | return vector_type_flags; |
| 4250 | } |
| 4251 | default: |
| 4252 | return 0; |
| 4253 | } |
| 4254 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4255 | } |
| 4256 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4257 | lldb::LanguageType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4258 | ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) { |
| 4259 | if (!type) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4260 | return lldb::eLanguageTypeC; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4261 | |
| 4262 | // If the type is a reference, then resolve it to what it refers to first: |
| 4263 | clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType()); |
| 4264 | if (qual_type->isAnyPointerType()) { |
| 4265 | if (qual_type->isObjCObjectPointerType()) |
| 4266 | return lldb::eLanguageTypeObjC; |
| 4267 | |
| 4268 | clang::QualType pointee_type(qual_type->getPointeeType()); |
| 4269 | if (pointee_type->getPointeeCXXRecordDecl() != nullptr) |
| 4270 | return lldb::eLanguageTypeC_plus_plus; |
| 4271 | if (pointee_type->isObjCObjectOrInterfaceType()) |
| 4272 | return lldb::eLanguageTypeObjC; |
| 4273 | if (pointee_type->isObjCClassType()) |
| 4274 | return lldb::eLanguageTypeObjC; |
| 4275 | if (pointee_type.getTypePtr() == |
| 4276 | getASTContext()->ObjCBuiltinIdTy.getTypePtr()) |
| 4277 | return lldb::eLanguageTypeObjC; |
| 4278 | } else { |
| 4279 | if (qual_type->isObjCObjectOrInterfaceType()) |
| 4280 | return lldb::eLanguageTypeObjC; |
| 4281 | if (qual_type->getAsCXXRecordDecl()) |
| 4282 | return lldb::eLanguageTypeC_plus_plus; |
| 4283 | switch (qual_type->getTypeClass()) { |
| 4284 | default: |
| 4285 | break; |
| 4286 | case clang::Type::Builtin: |
| 4287 | switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { |
| 4288 | default: |
| 4289 | case clang::BuiltinType::Void: |
| 4290 | case clang::BuiltinType::Bool: |
| 4291 | case clang::BuiltinType::Char_U: |
| 4292 | case clang::BuiltinType::UChar: |
| 4293 | case clang::BuiltinType::WChar_U: |
| 4294 | case clang::BuiltinType::Char16: |
| 4295 | case clang::BuiltinType::Char32: |
| 4296 | case clang::BuiltinType::UShort: |
| 4297 | case clang::BuiltinType::UInt: |
| 4298 | case clang::BuiltinType::ULong: |
| 4299 | case clang::BuiltinType::ULongLong: |
| 4300 | case clang::BuiltinType::UInt128: |
| 4301 | case clang::BuiltinType::Char_S: |
| 4302 | case clang::BuiltinType::SChar: |
| 4303 | case clang::BuiltinType::WChar_S: |
| 4304 | case clang::BuiltinType::Short: |
| 4305 | case clang::BuiltinType::Int: |
| 4306 | case clang::BuiltinType::Long: |
| 4307 | case clang::BuiltinType::LongLong: |
| 4308 | case clang::BuiltinType::Int128: |
| 4309 | case clang::BuiltinType::Float: |
| 4310 | case clang::BuiltinType::Double: |
| 4311 | case clang::BuiltinType::LongDouble: |
| 4312 | break; |
| 4313 | |
| 4314 | case clang::BuiltinType::NullPtr: |
| 4315 | return eLanguageTypeC_plus_plus; |
| 4316 | |
| 4317 | case clang::BuiltinType::ObjCId: |
| 4318 | case clang::BuiltinType::ObjCClass: |
| 4319 | case clang::BuiltinType::ObjCSel: |
| 4320 | return eLanguageTypeObjC; |
| 4321 | |
| 4322 | case clang::BuiltinType::Dependent: |
| 4323 | case clang::BuiltinType::Overload: |
| 4324 | case clang::BuiltinType::BoundMember: |
| 4325 | case clang::BuiltinType::UnknownAny: |
| 4326 | break; |
| 4327 | } |
| 4328 | break; |
| 4329 | case clang::Type::Typedef: |
| 4330 | return CompilerType(getASTContext(), |
| 4331 | llvm::cast<clang::TypedefType>(qual_type) |
| 4332 | ->getDecl() |
| 4333 | ->getUnderlyingType()) |
| 4334 | .GetMinimumLanguage(); |
| 4335 | } |
| 4336 | } |
| 4337 | return lldb::eLanguageTypeC; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4338 | } |
| 4339 | |
| 4340 | lldb::TypeClass |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4341 | ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) { |
| 4342 | if (!type) |
| 4343 | return lldb::eTypeClassInvalid; |
| 4344 | |
| 4345 | clang::QualType qual_type(GetQualType(type)); |
| 4346 | |
| 4347 | switch (qual_type->getTypeClass()) { |
| 4348 | case clang::Type::UnaryTransform: |
| 4349 | break; |
| 4350 | case clang::Type::FunctionNoProto: |
| 4351 | return lldb::eTypeClassFunction; |
| 4352 | case clang::Type::FunctionProto: |
| 4353 | return lldb::eTypeClassFunction; |
| 4354 | case clang::Type::IncompleteArray: |
| 4355 | return lldb::eTypeClassArray; |
| 4356 | case clang::Type::VariableArray: |
| 4357 | return lldb::eTypeClassArray; |
| 4358 | case clang::Type::ConstantArray: |
| 4359 | return lldb::eTypeClassArray; |
| 4360 | case clang::Type::DependentSizedArray: |
| 4361 | return lldb::eTypeClassArray; |
| 4362 | case clang::Type::DependentSizedExtVector: |
| 4363 | return lldb::eTypeClassVector; |
Fangrui Song | 8f28488 | 2018-07-13 22:40:40 +0000 | [diff] [blame] | 4364 | case clang::Type::DependentVector: |
| 4365 | return lldb::eTypeClassVector; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4366 | case clang::Type::ExtVector: |
| 4367 | return lldb::eTypeClassVector; |
| 4368 | case clang::Type::Vector: |
| 4369 | return lldb::eTypeClassVector; |
| 4370 | case clang::Type::Builtin: |
| 4371 | return lldb::eTypeClassBuiltin; |
| 4372 | case clang::Type::ObjCObjectPointer: |
| 4373 | return lldb::eTypeClassObjCObjectPointer; |
| 4374 | case clang::Type::BlockPointer: |
| 4375 | return lldb::eTypeClassBlockPointer; |
| 4376 | case clang::Type::Pointer: |
| 4377 | return lldb::eTypeClassPointer; |
| 4378 | case clang::Type::LValueReference: |
| 4379 | return lldb::eTypeClassReference; |
| 4380 | case clang::Type::RValueReference: |
| 4381 | return lldb::eTypeClassReference; |
| 4382 | case clang::Type::MemberPointer: |
| 4383 | return lldb::eTypeClassMemberPointer; |
| 4384 | case clang::Type::Complex: |
| 4385 | if (qual_type->isComplexType()) |
| 4386 | return lldb::eTypeClassComplexFloat; |
| 4387 | else |
| 4388 | return lldb::eTypeClassComplexInteger; |
| 4389 | case clang::Type::ObjCObject: |
| 4390 | return lldb::eTypeClassObjCObject; |
| 4391 | case clang::Type::ObjCInterface: |
| 4392 | return lldb::eTypeClassObjCInterface; |
| 4393 | case clang::Type::Record: { |
| 4394 | const clang::RecordType *record_type = |
| 4395 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 4396 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 4397 | if (record_decl->isUnion()) |
| 4398 | return lldb::eTypeClassUnion; |
| 4399 | else if (record_decl->isStruct()) |
| 4400 | return lldb::eTypeClassStruct; |
| 4401 | else |
| 4402 | return lldb::eTypeClassClass; |
| 4403 | } break; |
| 4404 | case clang::Type::Enum: |
| 4405 | return lldb::eTypeClassEnumeration; |
| 4406 | case clang::Type::Typedef: |
| 4407 | return lldb::eTypeClassTypedef; |
| 4408 | case clang::Type::UnresolvedUsing: |
| 4409 | break; |
| 4410 | case clang::Type::Paren: |
| 4411 | return CompilerType(getASTContext(), |
| 4412 | llvm::cast<clang::ParenType>(qual_type)->desugar()) |
| 4413 | .GetTypeClass(); |
| 4414 | case clang::Type::Auto: |
| 4415 | return CompilerType( |
| 4416 | getASTContext(), |
| 4417 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) |
| 4418 | .GetTypeClass(); |
| 4419 | case clang::Type::Elaborated: |
| 4420 | return CompilerType( |
| 4421 | getASTContext(), |
| 4422 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) |
| 4423 | .GetTypeClass(); |
| 4424 | |
| 4425 | case clang::Type::Attributed: |
| 4426 | break; |
| 4427 | case clang::Type::TemplateTypeParm: |
| 4428 | break; |
| 4429 | case clang::Type::SubstTemplateTypeParm: |
| 4430 | break; |
| 4431 | case clang::Type::SubstTemplateTypeParmPack: |
| 4432 | break; |
| 4433 | case clang::Type::InjectedClassName: |
| 4434 | break; |
| 4435 | case clang::Type::DependentName: |
| 4436 | break; |
| 4437 | case clang::Type::DependentTemplateSpecialization: |
| 4438 | break; |
| 4439 | case clang::Type::PackExpansion: |
| 4440 | break; |
| 4441 | |
| 4442 | case clang::Type::TypeOfExpr: |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 4443 | return CompilerType(getASTContext(), |
| 4444 | llvm::cast<clang::TypeOfExprType>(qual_type) |
| 4445 | ->getUnderlyingExpr() |
| 4446 | ->getType()) |
| 4447 | .GetTypeClass(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4448 | case clang::Type::TypeOf: |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 4449 | return CompilerType( |
| 4450 | getASTContext(), |
| 4451 | llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType()) |
| 4452 | .GetTypeClass(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4453 | case clang::Type::Decltype: |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 4454 | return CompilerType( |
| 4455 | getASTContext(), |
| 4456 | llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType()) |
| 4457 | .GetTypeClass(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4458 | case clang::Type::TemplateSpecialization: |
| 4459 | break; |
Pavel Labath | 4f19fce2 | 2017-02-17 13:39:50 +0000 | [diff] [blame] | 4460 | case clang::Type::DeducedTemplateSpecialization: |
| 4461 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4462 | case clang::Type::Atomic: |
| 4463 | break; |
| 4464 | case clang::Type::Pipe: |
| 4465 | break; |
| 4466 | |
| 4467 | // pointer type decayed from an array or function type. |
| 4468 | case clang::Type::Decayed: |
| 4469 | break; |
| 4470 | case clang::Type::Adjusted: |
| 4471 | break; |
Zachary Turner | 5a8ad459 | 2016-10-05 17:07:34 +0000 | [diff] [blame] | 4472 | case clang::Type::ObjCTypeParam: |
| 4473 | break; |
Ted Woodward | 66060cf | 2017-10-11 22:42:21 +0000 | [diff] [blame] | 4474 | |
| 4475 | case clang::Type::DependentAddressSpace: |
| 4476 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4477 | } |
| 4478 | // We don't know hot to display this type... |
| 4479 | return lldb::eTypeClassOther; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4480 | } |
| 4481 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4482 | unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) { |
| 4483 | if (type) |
| 4484 | return GetQualType(type).getQualifiers().getCVRQualifiers(); |
| 4485 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4486 | } |
| 4487 | |
| 4488 | //---------------------------------------------------------------------- |
| 4489 | // Creating related types |
| 4490 | //---------------------------------------------------------------------- |
| 4491 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 4492 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4493 | ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type, |
| 4494 | uint64_t *stride) { |
| 4495 | if (type) { |
| 4496 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 4497 | |
| 4498 | const clang::Type *array_eletype = |
| 4499 | qual_type.getTypePtr()->getArrayElementTypeNoTypeQual(); |
| 4500 | |
| 4501 | if (!array_eletype) |
| 4502 | return CompilerType(); |
| 4503 | |
| 4504 | CompilerType element_type(getASTContext(), |
| 4505 | array_eletype->getCanonicalTypeUnqualified()); |
| 4506 | |
| 4507 | // TODO: the real stride will be >= this value.. find the real one! |
| 4508 | if (stride) |
Adrian Prantl | d963a7c | 2019-01-15 18:07:52 +0000 | [diff] [blame^] | 4509 | if (auto size = element_type.GetByteSize(nullptr)) |
| 4510 | *stride = *size; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4511 | |
| 4512 | return element_type; |
| 4513 | } |
| 4514 | return CompilerType(); |
| 4515 | } |
| 4516 | |
| 4517 | CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type, |
| 4518 | uint64_t size) { |
| 4519 | if (type) { |
| 4520 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 4521 | if (clang::ASTContext *ast_ctx = getASTContext()) { |
| 4522 | if (size != 0) |
| 4523 | return CompilerType( |
| 4524 | ast_ctx, ast_ctx->getConstantArrayType( |
| 4525 | qual_type, llvm::APInt(64, size), |
| 4526 | clang::ArrayType::ArraySizeModifier::Normal, 0)); |
| 4527 | else |
| 4528 | return CompilerType( |
| 4529 | ast_ctx, |
| 4530 | ast_ctx->getIncompleteArrayType( |
| 4531 | qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0)); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4532 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4533 | } |
| 4534 | |
| 4535 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 4538 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4539 | ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) { |
| 4540 | if (type) |
| 4541 | return CompilerType(getASTContext(), GetCanonicalQualType(type)); |
| 4542 | return CompilerType(); |
| 4543 | } |
| 4544 | |
| 4545 | static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast, |
| 4546 | clang::QualType qual_type) { |
| 4547 | if (qual_type->isPointerType()) |
| 4548 | qual_type = ast->getPointerType( |
| 4549 | GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType())); |
| 4550 | else |
| 4551 | qual_type = qual_type.getUnqualifiedType(); |
| 4552 | qual_type.removeLocalConst(); |
| 4553 | qual_type.removeLocalRestrict(); |
| 4554 | qual_type.removeLocalVolatile(); |
| 4555 | return qual_type; |
Enrico Granata | 639392f | 2016-08-30 20:39:58 +0000 | [diff] [blame] | 4556 | } |
| 4557 | |
| 4558 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4559 | ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) { |
| 4560 | if (type) |
| 4561 | return CompilerType( |
| 4562 | getASTContext(), |
| 4563 | GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type))); |
| 4564 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4565 | } |
| 4566 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4567 | int ClangASTContext::GetFunctionArgumentCount( |
| 4568 | lldb::opaque_compiler_type_t type) { |
| 4569 | if (type) { |
| 4570 | const clang::FunctionProtoType *func = |
| 4571 | llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type)); |
| 4572 | if (func) |
| 4573 | return func->getNumParams(); |
| 4574 | } |
| 4575 | return -1; |
| 4576 | } |
| 4577 | |
| 4578 | CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex( |
| 4579 | lldb::opaque_compiler_type_t type, size_t idx) { |
| 4580 | if (type) { |
| 4581 | const clang::FunctionProtoType *func = |
| 4582 | llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type)); |
| 4583 | if (func) { |
| 4584 | const uint32_t num_args = func->getNumParams(); |
| 4585 | if (idx < num_args) |
| 4586 | return CompilerType(getASTContext(), func->getParamType(idx)); |
| 4587 | } |
| 4588 | } |
| 4589 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4590 | } |
| 4591 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 4592 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4593 | ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) { |
| 4594 | if (type) { |
| 4595 | clang::QualType qual_type(GetQualType(type)); |
| 4596 | const clang::FunctionProtoType *func = |
| 4597 | llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr()); |
| 4598 | if (func) |
| 4599 | return CompilerType(getASTContext(), func->getReturnType()); |
| 4600 | } |
| 4601 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4602 | } |
| 4603 | |
| 4604 | size_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4605 | ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) { |
| 4606 | size_t num_functions = 0; |
| 4607 | if (type) { |
| 4608 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 4609 | switch (qual_type->getTypeClass()) { |
| 4610 | case clang::Type::Record: |
| 4611 | if (GetCompleteQualType(getASTContext(), qual_type)) { |
| 4612 | const clang::RecordType *record_type = |
| 4613 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 4614 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 4615 | assert(record_decl); |
| 4616 | const clang::CXXRecordDecl *cxx_record_decl = |
| 4617 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 4618 | if (cxx_record_decl) |
| 4619 | num_functions = std::distance(cxx_record_decl->method_begin(), |
| 4620 | cxx_record_decl->method_end()); |
| 4621 | } |
| 4622 | break; |
Enrico Granata | 36f51e4 | 2015-12-18 22:41:25 +0000 | [diff] [blame] | 4623 | |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 4624 | case clang::Type::ObjCObjectPointer: { |
| 4625 | const clang::ObjCObjectPointerType *objc_class_type = |
Sean Callanan | 732a6f4 | 2017-05-15 19:55:20 +0000 | [diff] [blame] | 4626 | qual_type->getAs<clang::ObjCObjectPointerType>(); |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 4627 | const clang::ObjCInterfaceType *objc_interface_type = |
| 4628 | objc_class_type->getInterfaceType(); |
| 4629 | if (objc_interface_type && |
Davide Italiano | 52ffb53 | 2017-04-17 18:24:18 +0000 | [diff] [blame] | 4630 | GetCompleteType(static_cast<lldb::opaque_compiler_type_t>( |
| 4631 | const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) { |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 4632 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 4633 | objc_interface_type->getDecl(); |
| 4634 | if (class_interface_decl) { |
| 4635 | num_functions = std::distance(class_interface_decl->meth_begin(), |
| 4636 | class_interface_decl->meth_end()); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4637 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4638 | } |
| 4639 | break; |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 4640 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4641 | |
| 4642 | case clang::Type::ObjCObject: |
| 4643 | case clang::Type::ObjCInterface: |
| 4644 | if (GetCompleteType(type)) { |
| 4645 | const clang::ObjCObjectType *objc_class_type = |
| 4646 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 4647 | if (objc_class_type) { |
| 4648 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 4649 | objc_class_type->getInterface(); |
| 4650 | if (class_interface_decl) |
| 4651 | num_functions = std::distance(class_interface_decl->meth_begin(), |
| 4652 | class_interface_decl->meth_end()); |
| 4653 | } |
| 4654 | } |
| 4655 | break; |
| 4656 | |
| 4657 | case clang::Type::Typedef: |
| 4658 | return CompilerType(getASTContext(), |
| 4659 | llvm::cast<clang::TypedefType>(qual_type) |
| 4660 | ->getDecl() |
| 4661 | ->getUnderlyingType()) |
| 4662 | .GetNumMemberFunctions(); |
| 4663 | |
| 4664 | case clang::Type::Auto: |
| 4665 | return CompilerType( |
| 4666 | getASTContext(), |
| 4667 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) |
| 4668 | .GetNumMemberFunctions(); |
| 4669 | |
| 4670 | case clang::Type::Elaborated: |
| 4671 | return CompilerType( |
| 4672 | getASTContext(), |
| 4673 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) |
| 4674 | .GetNumMemberFunctions(); |
| 4675 | |
| 4676 | case clang::Type::Paren: |
| 4677 | return CompilerType(getASTContext(), |
| 4678 | llvm::cast<clang::ParenType>(qual_type)->desugar()) |
| 4679 | .GetNumMemberFunctions(); |
| 4680 | |
| 4681 | default: |
| 4682 | break; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4683 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4684 | } |
| 4685 | return num_functions; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4686 | } |
| 4687 | |
| 4688 | TypeMemberFunctionImpl |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4689 | ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type, |
| 4690 | size_t idx) { |
| 4691 | std::string name; |
| 4692 | MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown); |
| 4693 | CompilerType clang_type; |
| 4694 | CompilerDecl clang_decl; |
| 4695 | if (type) { |
| 4696 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 4697 | switch (qual_type->getTypeClass()) { |
| 4698 | case clang::Type::Record: |
| 4699 | if (GetCompleteQualType(getASTContext(), qual_type)) { |
| 4700 | const clang::RecordType *record_type = |
| 4701 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 4702 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 4703 | assert(record_decl); |
| 4704 | const clang::CXXRecordDecl *cxx_record_decl = |
| 4705 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 4706 | if (cxx_record_decl) { |
| 4707 | auto method_iter = cxx_record_decl->method_begin(); |
| 4708 | auto method_end = cxx_record_decl->method_end(); |
| 4709 | if (idx < |
| 4710 | static_cast<size_t>(std::distance(method_iter, method_end))) { |
| 4711 | std::advance(method_iter, idx); |
| 4712 | clang::CXXMethodDecl *cxx_method_decl = |
| 4713 | method_iter->getCanonicalDecl(); |
| 4714 | if (cxx_method_decl) { |
| 4715 | name = cxx_method_decl->getDeclName().getAsString(); |
| 4716 | if (cxx_method_decl->isStatic()) |
| 4717 | kind = lldb::eMemberFunctionKindStaticMethod; |
| 4718 | else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl)) |
| 4719 | kind = lldb::eMemberFunctionKindConstructor; |
| 4720 | else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl)) |
| 4721 | kind = lldb::eMemberFunctionKindDestructor; |
| 4722 | else |
| 4723 | kind = lldb::eMemberFunctionKindInstanceMethod; |
| 4724 | clang_type = CompilerType( |
| 4725 | this, cxx_method_decl->getType().getAsOpaquePtr()); |
| 4726 | clang_decl = CompilerDecl(this, cxx_method_decl); |
| 4727 | } |
| 4728 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4729 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4730 | } |
| 4731 | break; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4732 | |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 4733 | case clang::Type::ObjCObjectPointer: { |
| 4734 | const clang::ObjCObjectPointerType *objc_class_type = |
Sean Callanan | 732a6f4 | 2017-05-15 19:55:20 +0000 | [diff] [blame] | 4735 | qual_type->getAs<clang::ObjCObjectPointerType>(); |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 4736 | const clang::ObjCInterfaceType *objc_interface_type = |
| 4737 | objc_class_type->getInterfaceType(); |
| 4738 | if (objc_interface_type && |
Davide Italiano | 52ffb53 | 2017-04-17 18:24:18 +0000 | [diff] [blame] | 4739 | GetCompleteType(static_cast<lldb::opaque_compiler_type_t>( |
| 4740 | const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) { |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 4741 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 4742 | objc_interface_type->getDecl(); |
| 4743 | if (class_interface_decl) { |
| 4744 | auto method_iter = class_interface_decl->meth_begin(); |
| 4745 | auto method_end = class_interface_decl->meth_end(); |
| 4746 | if (idx < |
| 4747 | static_cast<size_t>(std::distance(method_iter, method_end))) { |
| 4748 | std::advance(method_iter, idx); |
| 4749 | clang::ObjCMethodDecl *objc_method_decl = |
| 4750 | method_iter->getCanonicalDecl(); |
| 4751 | if (objc_method_decl) { |
| 4752 | clang_decl = CompilerDecl(this, objc_method_decl); |
| 4753 | name = objc_method_decl->getSelector().getAsString(); |
| 4754 | if (objc_method_decl->isClassMethod()) |
| 4755 | kind = lldb::eMemberFunctionKindStaticMethod; |
| 4756 | else |
| 4757 | kind = lldb::eMemberFunctionKindInstanceMethod; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4758 | } |
| 4759 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4760 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4761 | } |
| 4762 | break; |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 4763 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4764 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4765 | case clang::Type::ObjCObject: |
| 4766 | case clang::Type::ObjCInterface: |
| 4767 | if (GetCompleteType(type)) { |
| 4768 | const clang::ObjCObjectType *objc_class_type = |
| 4769 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 4770 | if (objc_class_type) { |
| 4771 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 4772 | objc_class_type->getInterface(); |
| 4773 | if (class_interface_decl) { |
| 4774 | auto method_iter = class_interface_decl->meth_begin(); |
| 4775 | auto method_end = class_interface_decl->meth_end(); |
| 4776 | if (idx < |
| 4777 | static_cast<size_t>(std::distance(method_iter, method_end))) { |
| 4778 | std::advance(method_iter, idx); |
| 4779 | clang::ObjCMethodDecl *objc_method_decl = |
| 4780 | method_iter->getCanonicalDecl(); |
| 4781 | if (objc_method_decl) { |
| 4782 | clang_decl = CompilerDecl(this, objc_method_decl); |
| 4783 | name = objc_method_decl->getSelector().getAsString(); |
| 4784 | if (objc_method_decl->isClassMethod()) |
| 4785 | kind = lldb::eMemberFunctionKindStaticMethod; |
| 4786 | else |
| 4787 | kind = lldb::eMemberFunctionKindInstanceMethod; |
| 4788 | } |
| 4789 | } |
| 4790 | } |
Ewan Crawford | 27fc7a7 | 2016-03-15 09:50:16 +0000 | [diff] [blame] | 4791 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4792 | } |
| 4793 | break; |
Ewan Crawford | 27fc7a7 | 2016-03-15 09:50:16 +0000 | [diff] [blame] | 4794 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4795 | case clang::Type::Typedef: |
| 4796 | return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type) |
| 4797 | ->getDecl() |
| 4798 | ->getUnderlyingType() |
| 4799 | .getAsOpaquePtr(), |
| 4800 | idx); |
Ewan Crawford | 27fc7a7 | 2016-03-15 09:50:16 +0000 | [diff] [blame] | 4801 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4802 | case clang::Type::Auto: |
| 4803 | return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type) |
| 4804 | ->getDeducedType() |
| 4805 | .getAsOpaquePtr(), |
| 4806 | idx); |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 4807 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4808 | case clang::Type::Elaborated: |
| 4809 | return GetMemberFunctionAtIndex( |
| 4810 | llvm::cast<clang::ElaboratedType>(qual_type) |
| 4811 | ->getNamedType() |
| 4812 | .getAsOpaquePtr(), |
| 4813 | idx); |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 4814 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4815 | case clang::Type::Paren: |
| 4816 | return GetMemberFunctionAtIndex( |
| 4817 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 4818 | idx); |
| 4819 | |
| 4820 | default: |
| 4821 | break; |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 4822 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4823 | } |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 4824 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4825 | if (kind == eMemberFunctionKindUnknown) |
| 4826 | return TypeMemberFunctionImpl(); |
| 4827 | else |
| 4828 | return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind); |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 4829 | } |
| 4830 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 4831 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4832 | ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) { |
| 4833 | if (type) |
| 4834 | return CompilerType(getASTContext(), |
| 4835 | GetQualType(type).getNonReferenceType()); |
| 4836 | return CompilerType(); |
| 4837 | } |
| 4838 | |
| 4839 | CompilerType ClangASTContext::CreateTypedefType( |
| 4840 | const CompilerType &type, const char *typedef_name, |
| 4841 | const CompilerDeclContext &compiler_decl_ctx) { |
| 4842 | if (type && typedef_name && typedef_name[0]) { |
| 4843 | ClangASTContext *ast = |
| 4844 | llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); |
| 4845 | if (!ast) |
| 4846 | return CompilerType(); |
| 4847 | clang::ASTContext *clang_ast = ast->getASTContext(); |
| 4848 | clang::QualType qual_type(ClangUtil::GetQualType(type)); |
| 4849 | |
| 4850 | clang::DeclContext *decl_ctx = |
| 4851 | ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx); |
| 4852 | if (decl_ctx == nullptr) |
| 4853 | decl_ctx = ast->getASTContext()->getTranslationUnitDecl(); |
| 4854 | |
| 4855 | clang::TypedefDecl *decl = clang::TypedefDecl::Create( |
| 4856 | *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(), |
| 4857 | &clang_ast->Idents.get(typedef_name), |
| 4858 | clang_ast->getTrivialTypeSourceInfo(qual_type)); |
| 4859 | |
| 4860 | decl->setAccess(clang::AS_public); // TODO respect proper access specifier |
| 4861 | |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 4862 | decl_ctx->addDecl(decl); |
| 4863 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4864 | // Get a uniqued clang::QualType for the typedef decl type |
| 4865 | return CompilerType(clang_ast, clang_ast->getTypedefType(decl)); |
| 4866 | } |
| 4867 | return CompilerType(); |
| 4868 | } |
| 4869 | |
| 4870 | CompilerType |
| 4871 | ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) { |
| 4872 | if (type) { |
| 4873 | clang::QualType qual_type(GetQualType(type)); |
| 4874 | return CompilerType(getASTContext(), |
| 4875 | qual_type.getTypePtr()->getPointeeType()); |
| 4876 | } |
| 4877 | return CompilerType(); |
| 4878 | } |
| 4879 | |
| 4880 | CompilerType |
| 4881 | ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) { |
| 4882 | if (type) { |
| 4883 | clang::QualType qual_type(GetQualType(type)); |
| 4884 | |
| 4885 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 4886 | switch (type_class) { |
| 4887 | case clang::Type::ObjCObject: |
| 4888 | case clang::Type::ObjCInterface: |
| 4889 | return CompilerType(getASTContext(), |
| 4890 | getASTContext()->getObjCObjectPointerType(qual_type)); |
| 4891 | |
| 4892 | default: |
| 4893 | return CompilerType(getASTContext(), |
| 4894 | getASTContext()->getPointerType(qual_type)); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4895 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4896 | } |
| 4897 | return CompilerType(); |
| 4898 | } |
| 4899 | |
| 4900 | CompilerType |
| 4901 | ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) { |
| 4902 | if (type) |
| 4903 | return CompilerType(this, getASTContext() |
| 4904 | ->getLValueReferenceType(GetQualType(type)) |
| 4905 | .getAsOpaquePtr()); |
| 4906 | else |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 4907 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4908 | } |
| 4909 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4910 | CompilerType |
| 4911 | ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) { |
| 4912 | if (type) |
| 4913 | return CompilerType(this, getASTContext() |
| 4914 | ->getRValueReferenceType(GetQualType(type)) |
| 4915 | .getAsOpaquePtr()); |
| 4916 | else |
| 4917 | return CompilerType(); |
| 4918 | } |
| 4919 | |
| 4920 | CompilerType |
| 4921 | ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) { |
| 4922 | if (type) { |
| 4923 | clang::QualType result(GetQualType(type)); |
| 4924 | result.addConst(); |
| 4925 | return CompilerType(this, result.getAsOpaquePtr()); |
| 4926 | } |
| 4927 | return CompilerType(); |
| 4928 | } |
| 4929 | |
| 4930 | CompilerType |
| 4931 | ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) { |
| 4932 | if (type) { |
| 4933 | clang::QualType result(GetQualType(type)); |
| 4934 | result.addVolatile(); |
| 4935 | return CompilerType(this, result.getAsOpaquePtr()); |
| 4936 | } |
| 4937 | return CompilerType(); |
| 4938 | } |
| 4939 | |
| 4940 | CompilerType |
| 4941 | ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) { |
| 4942 | if (type) { |
| 4943 | clang::QualType result(GetQualType(type)); |
| 4944 | result.addRestrict(); |
| 4945 | return CompilerType(this, result.getAsOpaquePtr()); |
| 4946 | } |
| 4947 | return CompilerType(); |
| 4948 | } |
| 4949 | |
| 4950 | CompilerType |
| 4951 | ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type, |
| 4952 | const char *typedef_name, |
| 4953 | const CompilerDeclContext &compiler_decl_ctx) { |
| 4954 | if (type) { |
| 4955 | clang::ASTContext *clang_ast = getASTContext(); |
| 4956 | clang::QualType qual_type(GetQualType(type)); |
| 4957 | |
| 4958 | clang::DeclContext *decl_ctx = |
| 4959 | ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx); |
| 4960 | if (decl_ctx == nullptr) |
| 4961 | decl_ctx = getASTContext()->getTranslationUnitDecl(); |
| 4962 | |
| 4963 | clang::TypedefDecl *decl = clang::TypedefDecl::Create( |
| 4964 | *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(), |
| 4965 | &clang_ast->Idents.get(typedef_name), |
| 4966 | clang_ast->getTrivialTypeSourceInfo(qual_type)); |
| 4967 | |
| 4968 | clang::TagDecl *tdecl = nullptr; |
| 4969 | if (!qual_type.isNull()) { |
| 4970 | if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>()) |
| 4971 | tdecl = rt->getDecl(); |
| 4972 | if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>()) |
| 4973 | tdecl = et->getDecl(); |
| 4974 | } |
| 4975 | |
| 4976 | // Check whether this declaration is an anonymous struct, union, or enum, |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4977 | // hidden behind a typedef. If so, we try to check whether we have a |
| 4978 | // typedef tag to attach to the original record declaration |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4979 | if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl()) |
| 4980 | tdecl->setTypedefNameForAnonDecl(decl); |
| 4981 | |
| 4982 | decl->setAccess(clang::AS_public); // TODO respect proper access specifier |
| 4983 | |
| 4984 | // Get a uniqued clang::QualType for the typedef decl type |
| 4985 | return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr()); |
| 4986 | } |
| 4987 | return CompilerType(); |
| 4988 | } |
| 4989 | |
| 4990 | CompilerType |
| 4991 | ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) { |
| 4992 | if (type) { |
| 4993 | const clang::TypedefType *typedef_type = |
| 4994 | llvm::dyn_cast<clang::TypedefType>(GetQualType(type)); |
| 4995 | if (typedef_type) |
| 4996 | return CompilerType(getASTContext(), |
| 4997 | typedef_type->getDecl()->getUnderlyingType()); |
| 4998 | } |
| 4999 | return CompilerType(); |
| 5000 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5001 | |
| 5002 | //---------------------------------------------------------------------- |
| 5003 | // Create related types using the current type's AST |
| 5004 | //---------------------------------------------------------------------- |
| 5005 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5006 | CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) { |
| 5007 | return ClangASTContext::GetBasicType(getASTContext(), basic_type); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5008 | } |
| 5009 | //---------------------------------------------------------------------- |
| 5010 | // Exploring the type |
| 5011 | //---------------------------------------------------------------------- |
| 5012 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5013 | uint64_t ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type, |
| 5014 | ExecutionContextScope *exe_scope) { |
| 5015 | if (GetCompleteType(type)) { |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5016 | clang::QualType qual_type(GetCanonicalQualType(type)); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5017 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5018 | switch (type_class) { |
| 5019 | case clang::Type::Record: |
| 5020 | if (GetCompleteType(type)) |
| 5021 | return getASTContext()->getTypeSize(qual_type); |
| 5022 | else |
| 5023 | return 0; |
| 5024 | break; |
Enrico Granata | 36f51e4 | 2015-12-18 22:41:25 +0000 | [diff] [blame] | 5025 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5026 | case clang::Type::ObjCInterface: |
| 5027 | case clang::Type::ObjCObject: { |
| 5028 | ExecutionContext exe_ctx(exe_scope); |
| 5029 | Process *process = exe_ctx.GetProcessPtr(); |
| 5030 | if (process) { |
| 5031 | ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime(); |
| 5032 | if (objc_runtime) { |
| 5033 | uint64_t bit_size = 0; |
| 5034 | if (objc_runtime->GetTypeBitSize( |
| 5035 | CompilerType(getASTContext(), qual_type), bit_size)) |
| 5036 | return bit_size; |
| 5037 | } |
| 5038 | } else { |
| 5039 | static bool g_printed = false; |
| 5040 | if (!g_printed) { |
| 5041 | StreamString s; |
| 5042 | DumpTypeDescription(type, &s); |
| 5043 | |
| 5044 | llvm::outs() << "warning: trying to determine the size of type "; |
| 5045 | llvm::outs() << s.GetString() << "\n"; |
| 5046 | llvm::outs() << "without a valid ExecutionContext. this is not " |
| 5047 | "reliable. please file a bug against LLDB.\n"; |
| 5048 | llvm::outs() << "backtrace:\n"; |
| 5049 | llvm::sys::PrintStackTrace(llvm::outs()); |
| 5050 | llvm::outs() << "\n"; |
| 5051 | g_printed = true; |
| 5052 | } |
| 5053 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5054 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5055 | LLVM_FALLTHROUGH; |
| 5056 | default: |
| 5057 | const uint32_t bit_size = getASTContext()->getTypeSize(qual_type); |
| 5058 | if (bit_size == 0) { |
| 5059 | if (qual_type->isIncompleteArrayType()) |
| 5060 | return getASTContext()->getTypeSize( |
| 5061 | qual_type->getArrayElementTypeNoTypeQual() |
| 5062 | ->getCanonicalTypeUnqualified()); |
| 5063 | } |
| 5064 | if (qual_type->isObjCObjectOrInterfaceType()) |
| 5065 | return bit_size + |
| 5066 | getASTContext()->getTypeSize( |
| 5067 | getASTContext()->ObjCBuiltinClassTy); |
| 5068 | return bit_size; |
| 5069 | } |
| 5070 | } |
| 5071 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5072 | } |
| 5073 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5074 | size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) { |
| 5075 | if (GetCompleteType(type)) |
| 5076 | return getASTContext()->getTypeAlign(GetQualType(type)); |
| 5077 | return 0; |
| 5078 | } |
| 5079 | |
| 5080 | lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type, |
| 5081 | uint64_t &count) { |
| 5082 | if (!type) |
| 5083 | return lldb::eEncodingInvalid; |
| 5084 | |
| 5085 | count = 1; |
| 5086 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 5087 | |
| 5088 | switch (qual_type->getTypeClass()) { |
| 5089 | case clang::Type::UnaryTransform: |
| 5090 | break; |
| 5091 | |
| 5092 | case clang::Type::FunctionNoProto: |
| 5093 | case clang::Type::FunctionProto: |
| 5094 | break; |
| 5095 | |
| 5096 | case clang::Type::IncompleteArray: |
| 5097 | case clang::Type::VariableArray: |
| 5098 | break; |
| 5099 | |
| 5100 | case clang::Type::ConstantArray: |
| 5101 | break; |
| 5102 | |
Fangrui Song | 8f28488 | 2018-07-13 22:40:40 +0000 | [diff] [blame] | 5103 | case clang::Type::DependentVector: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5104 | case clang::Type::ExtVector: |
| 5105 | case clang::Type::Vector: |
| 5106 | // TODO: Set this to more than one??? |
| 5107 | break; |
| 5108 | |
| 5109 | case clang::Type::Builtin: |
| 5110 | switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { |
| 5111 | case clang::BuiltinType::Void: |
| 5112 | break; |
| 5113 | |
| 5114 | case clang::BuiltinType::Bool: |
| 5115 | case clang::BuiltinType::Char_S: |
| 5116 | case clang::BuiltinType::SChar: |
| 5117 | case clang::BuiltinType::WChar_S: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5118 | case clang::BuiltinType::Short: |
| 5119 | case clang::BuiltinType::Int: |
| 5120 | case clang::BuiltinType::Long: |
| 5121 | case clang::BuiltinType::LongLong: |
| 5122 | case clang::BuiltinType::Int128: |
| 5123 | return lldb::eEncodingSint; |
| 5124 | |
| 5125 | case clang::BuiltinType::Char_U: |
| 5126 | case clang::BuiltinType::UChar: |
| 5127 | case clang::BuiltinType::WChar_U: |
Richard Smith | 51d12d8 | 2018-05-02 02:43:22 +0000 | [diff] [blame] | 5128 | case clang::BuiltinType::Char8: |
| 5129 | case clang::BuiltinType::Char16: |
| 5130 | case clang::BuiltinType::Char32: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5131 | case clang::BuiltinType::UShort: |
| 5132 | case clang::BuiltinType::UInt: |
| 5133 | case clang::BuiltinType::ULong: |
| 5134 | case clang::BuiltinType::ULongLong: |
| 5135 | case clang::BuiltinType::UInt128: |
| 5136 | return lldb::eEncodingUint; |
| 5137 | |
Ilya Biryukov | fc48ac6 | 2018-06-05 10:07:07 +0000 | [diff] [blame] | 5138 | // Fixed point types. Note that they are currently ignored. |
| 5139 | case clang::BuiltinType::ShortAccum: |
| 5140 | case clang::BuiltinType::Accum: |
| 5141 | case clang::BuiltinType::LongAccum: |
| 5142 | case clang::BuiltinType::UShortAccum: |
| 5143 | case clang::BuiltinType::UAccum: |
| 5144 | case clang::BuiltinType::ULongAccum: |
Fangrui Song | a5e59c5 | 2018-06-14 18:19:40 +0000 | [diff] [blame] | 5145 | case clang::BuiltinType::ShortFract: |
Fangrui Song | a5e59c5 | 2018-06-14 18:19:40 +0000 | [diff] [blame] | 5146 | case clang::BuiltinType::Fract: |
| 5147 | case clang::BuiltinType::LongFract: |
| 5148 | case clang::BuiltinType::UShortFract: |
| 5149 | case clang::BuiltinType::UFract: |
| 5150 | case clang::BuiltinType::ULongFract: |
| 5151 | case clang::BuiltinType::SatShortAccum: |
| 5152 | case clang::BuiltinType::SatAccum: |
| 5153 | case clang::BuiltinType::SatLongAccum: |
| 5154 | case clang::BuiltinType::SatUShortAccum: |
| 5155 | case clang::BuiltinType::SatUAccum: |
| 5156 | case clang::BuiltinType::SatULongAccum: |
| 5157 | case clang::BuiltinType::SatShortFract: |
| 5158 | case clang::BuiltinType::SatFract: |
| 5159 | case clang::BuiltinType::SatLongFract: |
| 5160 | case clang::BuiltinType::SatUShortFract: |
| 5161 | case clang::BuiltinType::SatUFract: |
| 5162 | case clang::BuiltinType::SatULongFract: |
Ilya Biryukov | fc48ac6 | 2018-06-05 10:07:07 +0000 | [diff] [blame] | 5163 | break; |
| 5164 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5165 | case clang::BuiltinType::Half: |
| 5166 | case clang::BuiltinType::Float: |
Ted Woodward | 4355c7c | 2017-09-20 19:16:53 +0000 | [diff] [blame] | 5167 | case clang::BuiltinType::Float16: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5168 | case clang::BuiltinType::Float128: |
| 5169 | case clang::BuiltinType::Double: |
| 5170 | case clang::BuiltinType::LongDouble: |
| 5171 | return lldb::eEncodingIEEE754; |
| 5172 | |
| 5173 | case clang::BuiltinType::ObjCClass: |
| 5174 | case clang::BuiltinType::ObjCId: |
| 5175 | case clang::BuiltinType::ObjCSel: |
| 5176 | return lldb::eEncodingUint; |
| 5177 | |
| 5178 | case clang::BuiltinType::NullPtr: |
| 5179 | return lldb::eEncodingUint; |
| 5180 | |
| 5181 | case clang::BuiltinType::Kind::ARCUnbridgedCast: |
| 5182 | case clang::BuiltinType::Kind::BoundMember: |
| 5183 | case clang::BuiltinType::Kind::BuiltinFn: |
| 5184 | case clang::BuiltinType::Kind::Dependent: |
| 5185 | case clang::BuiltinType::Kind::OCLClkEvent: |
| 5186 | case clang::BuiltinType::Kind::OCLEvent: |
| 5187 | case clang::BuiltinType::Kind::OCLImage1dRO: |
| 5188 | case clang::BuiltinType::Kind::OCLImage1dWO: |
| 5189 | case clang::BuiltinType::Kind::OCLImage1dRW: |
| 5190 | case clang::BuiltinType::Kind::OCLImage1dArrayRO: |
| 5191 | case clang::BuiltinType::Kind::OCLImage1dArrayWO: |
| 5192 | case clang::BuiltinType::Kind::OCLImage1dArrayRW: |
| 5193 | case clang::BuiltinType::Kind::OCLImage1dBufferRO: |
| 5194 | case clang::BuiltinType::Kind::OCLImage1dBufferWO: |
| 5195 | case clang::BuiltinType::Kind::OCLImage1dBufferRW: |
| 5196 | case clang::BuiltinType::Kind::OCLImage2dRO: |
| 5197 | case clang::BuiltinType::Kind::OCLImage2dWO: |
| 5198 | case clang::BuiltinType::Kind::OCLImage2dRW: |
| 5199 | case clang::BuiltinType::Kind::OCLImage2dArrayRO: |
| 5200 | case clang::BuiltinType::Kind::OCLImage2dArrayWO: |
| 5201 | case clang::BuiltinType::Kind::OCLImage2dArrayRW: |
| 5202 | case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO: |
| 5203 | case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO: |
| 5204 | case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW: |
| 5205 | case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO: |
| 5206 | case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO: |
| 5207 | case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW: |
| 5208 | case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO: |
| 5209 | case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO: |
| 5210 | case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW: |
| 5211 | case clang::BuiltinType::Kind::OCLImage2dDepthRO: |
| 5212 | case clang::BuiltinType::Kind::OCLImage2dDepthWO: |
| 5213 | case clang::BuiltinType::Kind::OCLImage2dDepthRW: |
| 5214 | case clang::BuiltinType::Kind::OCLImage2dMSAARO: |
| 5215 | case clang::BuiltinType::Kind::OCLImage2dMSAAWO: |
| 5216 | case clang::BuiltinType::Kind::OCLImage2dMSAARW: |
| 5217 | case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO: |
| 5218 | case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO: |
| 5219 | case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW: |
| 5220 | case clang::BuiltinType::Kind::OCLImage3dRO: |
| 5221 | case clang::BuiltinType::Kind::OCLImage3dWO: |
| 5222 | case clang::BuiltinType::Kind::OCLImage3dRW: |
| 5223 | case clang::BuiltinType::Kind::OCLQueue: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5224 | case clang::BuiltinType::Kind::OCLReserveID: |
| 5225 | case clang::BuiltinType::Kind::OCLSampler: |
| 5226 | case clang::BuiltinType::Kind::OMPArraySection: |
| 5227 | case clang::BuiltinType::Kind::Overload: |
| 5228 | case clang::BuiltinType::Kind::PseudoObject: |
| 5229 | case clang::BuiltinType::Kind::UnknownAny: |
| 5230 | break; |
Jorge Gorbe Moya | a6e6c18 | 2018-11-08 22:04:58 +0000 | [diff] [blame] | 5231 | |
| 5232 | case clang::BuiltinType::OCLIntelSubgroupAVCMcePayload: |
| 5233 | case clang::BuiltinType::OCLIntelSubgroupAVCImePayload: |
| 5234 | case clang::BuiltinType::OCLIntelSubgroupAVCRefPayload: |
| 5235 | case clang::BuiltinType::OCLIntelSubgroupAVCSicPayload: |
| 5236 | case clang::BuiltinType::OCLIntelSubgroupAVCMceResult: |
| 5237 | case clang::BuiltinType::OCLIntelSubgroupAVCImeResult: |
| 5238 | case clang::BuiltinType::OCLIntelSubgroupAVCRefResult: |
| 5239 | case clang::BuiltinType::OCLIntelSubgroupAVCSicResult: |
| 5240 | case clang::BuiltinType::OCLIntelSubgroupAVCImeResultSingleRefStreamout: |
| 5241 | case clang::BuiltinType::OCLIntelSubgroupAVCImeResultDualRefStreamout: |
| 5242 | case clang::BuiltinType::OCLIntelSubgroupAVCImeSingleRefStreamin: |
| 5243 | case clang::BuiltinType::OCLIntelSubgroupAVCImeDualRefStreamin: |
| 5244 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5245 | } |
| 5246 | break; |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5247 | // All pointer types are represented as unsigned integer encodings. We may |
| 5248 | // 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] | 5249 | case clang::Type::ObjCObjectPointer: |
| 5250 | case clang::Type::BlockPointer: |
| 5251 | case clang::Type::Pointer: |
| 5252 | case clang::Type::LValueReference: |
| 5253 | case clang::Type::RValueReference: |
| 5254 | case clang::Type::MemberPointer: |
| 5255 | return lldb::eEncodingUint; |
| 5256 | case clang::Type::Complex: { |
| 5257 | lldb::Encoding encoding = lldb::eEncodingIEEE754; |
| 5258 | if (qual_type->isComplexType()) |
| 5259 | encoding = lldb::eEncodingIEEE754; |
| 5260 | else { |
| 5261 | const clang::ComplexType *complex_type = |
| 5262 | qual_type->getAsComplexIntegerType(); |
| 5263 | if (complex_type) |
| 5264 | encoding = CompilerType(getASTContext(), complex_type->getElementType()) |
| 5265 | .GetEncoding(count); |
| 5266 | else |
| 5267 | encoding = lldb::eEncodingSint; |
| 5268 | } |
| 5269 | count = 2; |
| 5270 | return encoding; |
| 5271 | } |
| 5272 | |
| 5273 | case clang::Type::ObjCInterface: |
| 5274 | break; |
| 5275 | case clang::Type::Record: |
| 5276 | break; |
| 5277 | case clang::Type::Enum: |
| 5278 | return lldb::eEncodingSint; |
| 5279 | case clang::Type::Typedef: |
| 5280 | return CompilerType(getASTContext(), |
| 5281 | llvm::cast<clang::TypedefType>(qual_type) |
| 5282 | ->getDecl() |
| 5283 | ->getUnderlyingType()) |
| 5284 | .GetEncoding(count); |
| 5285 | |
| 5286 | case clang::Type::Auto: |
| 5287 | return CompilerType( |
| 5288 | getASTContext(), |
| 5289 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) |
| 5290 | .GetEncoding(count); |
| 5291 | |
| 5292 | case clang::Type::Elaborated: |
| 5293 | return CompilerType( |
| 5294 | getASTContext(), |
| 5295 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) |
| 5296 | .GetEncoding(count); |
| 5297 | |
| 5298 | case clang::Type::Paren: |
| 5299 | return CompilerType(getASTContext(), |
| 5300 | llvm::cast<clang::ParenType>(qual_type)->desugar()) |
| 5301 | .GetEncoding(count); |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 5302 | case clang::Type::TypeOfExpr: |
| 5303 | return CompilerType(getASTContext(), |
| 5304 | llvm::cast<clang::TypeOfExprType>(qual_type) |
| 5305 | ->getUnderlyingExpr() |
| 5306 | ->getType()) |
| 5307 | .GetEncoding(count); |
| 5308 | case clang::Type::TypeOf: |
| 5309 | return CompilerType( |
| 5310 | getASTContext(), |
| 5311 | llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType()) |
| 5312 | .GetEncoding(count); |
| 5313 | case clang::Type::Decltype: |
| 5314 | return CompilerType( |
| 5315 | getASTContext(), |
| 5316 | llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType()) |
| 5317 | .GetEncoding(count); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5318 | case clang::Type::DependentSizedArray: |
| 5319 | case clang::Type::DependentSizedExtVector: |
| 5320 | case clang::Type::UnresolvedUsing: |
| 5321 | case clang::Type::Attributed: |
| 5322 | case clang::Type::TemplateTypeParm: |
| 5323 | case clang::Type::SubstTemplateTypeParm: |
| 5324 | case clang::Type::SubstTemplateTypeParmPack: |
| 5325 | case clang::Type::InjectedClassName: |
| 5326 | case clang::Type::DependentName: |
| 5327 | case clang::Type::DependentTemplateSpecialization: |
| 5328 | case clang::Type::PackExpansion: |
| 5329 | case clang::Type::ObjCObject: |
| 5330 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5331 | case clang::Type::TemplateSpecialization: |
Pavel Labath | 4f19fce2 | 2017-02-17 13:39:50 +0000 | [diff] [blame] | 5332 | case clang::Type::DeducedTemplateSpecialization: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5333 | case clang::Type::Atomic: |
| 5334 | case clang::Type::Adjusted: |
| 5335 | case clang::Type::Pipe: |
| 5336 | break; |
| 5337 | |
| 5338 | // pointer type decayed from an array or function type. |
| 5339 | case clang::Type::Decayed: |
| 5340 | break; |
Zachary Turner | 5a8ad459 | 2016-10-05 17:07:34 +0000 | [diff] [blame] | 5341 | case clang::Type::ObjCTypeParam: |
| 5342 | break; |
Ted Woodward | 66060cf | 2017-10-11 22:42:21 +0000 | [diff] [blame] | 5343 | |
| 5344 | case clang::Type::DependentAddressSpace: |
| 5345 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5346 | } |
| 5347 | count = 0; |
| 5348 | return lldb::eEncodingInvalid; |
| 5349 | } |
| 5350 | |
| 5351 | lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) { |
| 5352 | if (!type) |
| 5353 | return lldb::eFormatDefault; |
| 5354 | |
| 5355 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 5356 | |
| 5357 | switch (qual_type->getTypeClass()) { |
| 5358 | case clang::Type::UnaryTransform: |
| 5359 | break; |
| 5360 | |
| 5361 | case clang::Type::FunctionNoProto: |
| 5362 | case clang::Type::FunctionProto: |
| 5363 | break; |
| 5364 | |
| 5365 | case clang::Type::IncompleteArray: |
| 5366 | case clang::Type::VariableArray: |
| 5367 | break; |
| 5368 | |
| 5369 | case clang::Type::ConstantArray: |
| 5370 | return lldb::eFormatVoid; // no value |
| 5371 | |
Fangrui Song | 8f28488 | 2018-07-13 22:40:40 +0000 | [diff] [blame] | 5372 | case clang::Type::DependentVector: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5373 | case clang::Type::ExtVector: |
| 5374 | case clang::Type::Vector: |
| 5375 | break; |
| 5376 | |
| 5377 | case clang::Type::Builtin: |
| 5378 | switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { |
| 5379 | // default: assert(0 && "Unknown builtin type!"); |
| 5380 | case clang::BuiltinType::UnknownAny: |
| 5381 | case clang::BuiltinType::Void: |
| 5382 | case clang::BuiltinType::BoundMember: |
| 5383 | break; |
| 5384 | |
| 5385 | case clang::BuiltinType::Bool: |
| 5386 | return lldb::eFormatBoolean; |
| 5387 | case clang::BuiltinType::Char_S: |
| 5388 | case clang::BuiltinType::SChar: |
| 5389 | case clang::BuiltinType::WChar_S: |
| 5390 | case clang::BuiltinType::Char_U: |
| 5391 | case clang::BuiltinType::UChar: |
| 5392 | case clang::BuiltinType::WChar_U: |
| 5393 | return lldb::eFormatChar; |
| 5394 | case clang::BuiltinType::Char16: |
| 5395 | return lldb::eFormatUnicode16; |
| 5396 | case clang::BuiltinType::Char32: |
| 5397 | return lldb::eFormatUnicode32; |
| 5398 | case clang::BuiltinType::UShort: |
| 5399 | return lldb::eFormatUnsigned; |
| 5400 | case clang::BuiltinType::Short: |
| 5401 | return lldb::eFormatDecimal; |
| 5402 | case clang::BuiltinType::UInt: |
| 5403 | return lldb::eFormatUnsigned; |
| 5404 | case clang::BuiltinType::Int: |
| 5405 | return lldb::eFormatDecimal; |
| 5406 | case clang::BuiltinType::ULong: |
| 5407 | return lldb::eFormatUnsigned; |
| 5408 | case clang::BuiltinType::Long: |
| 5409 | return lldb::eFormatDecimal; |
| 5410 | case clang::BuiltinType::ULongLong: |
| 5411 | return lldb::eFormatUnsigned; |
| 5412 | case clang::BuiltinType::LongLong: |
| 5413 | return lldb::eFormatDecimal; |
| 5414 | case clang::BuiltinType::UInt128: |
| 5415 | return lldb::eFormatUnsigned; |
| 5416 | case clang::BuiltinType::Int128: |
| 5417 | return lldb::eFormatDecimal; |
| 5418 | case clang::BuiltinType::Half: |
| 5419 | case clang::BuiltinType::Float: |
| 5420 | case clang::BuiltinType::Double: |
| 5421 | case clang::BuiltinType::LongDouble: |
| 5422 | return lldb::eFormatFloat; |
| 5423 | default: |
| 5424 | return lldb::eFormatHex; |
| 5425 | } |
| 5426 | break; |
| 5427 | case clang::Type::ObjCObjectPointer: |
| 5428 | return lldb::eFormatHex; |
| 5429 | case clang::Type::BlockPointer: |
| 5430 | return lldb::eFormatHex; |
| 5431 | case clang::Type::Pointer: |
| 5432 | return lldb::eFormatHex; |
| 5433 | case clang::Type::LValueReference: |
| 5434 | case clang::Type::RValueReference: |
| 5435 | return lldb::eFormatHex; |
| 5436 | case clang::Type::MemberPointer: |
| 5437 | break; |
| 5438 | case clang::Type::Complex: { |
| 5439 | if (qual_type->isComplexType()) |
| 5440 | return lldb::eFormatComplex; |
| 5441 | else |
| 5442 | return lldb::eFormatComplexInteger; |
| 5443 | } |
| 5444 | case clang::Type::ObjCInterface: |
| 5445 | break; |
| 5446 | case clang::Type::Record: |
| 5447 | break; |
| 5448 | case clang::Type::Enum: |
| 5449 | return lldb::eFormatEnum; |
| 5450 | case clang::Type::Typedef: |
| 5451 | return CompilerType(getASTContext(), |
| 5452 | llvm::cast<clang::TypedefType>(qual_type) |
| 5453 | ->getDecl() |
| 5454 | ->getUnderlyingType()) |
| 5455 | .GetFormat(); |
| 5456 | case clang::Type::Auto: |
| 5457 | return CompilerType(getASTContext(), |
| 5458 | llvm::cast<clang::AutoType>(qual_type)->desugar()) |
| 5459 | .GetFormat(); |
| 5460 | case clang::Type::Paren: |
| 5461 | return CompilerType(getASTContext(), |
| 5462 | llvm::cast<clang::ParenType>(qual_type)->desugar()) |
| 5463 | .GetFormat(); |
| 5464 | case clang::Type::Elaborated: |
| 5465 | return CompilerType( |
| 5466 | getASTContext(), |
| 5467 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) |
| 5468 | .GetFormat(); |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 5469 | case clang::Type::TypeOfExpr: |
| 5470 | return CompilerType(getASTContext(), |
| 5471 | llvm::cast<clang::TypeOfExprType>(qual_type) |
| 5472 | ->getUnderlyingExpr() |
| 5473 | ->getType()) |
| 5474 | .GetFormat(); |
| 5475 | case clang::Type::TypeOf: |
| 5476 | return CompilerType( |
| 5477 | getASTContext(), |
| 5478 | llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType()) |
| 5479 | .GetFormat(); |
| 5480 | case clang::Type::Decltype: |
| 5481 | return CompilerType( |
| 5482 | getASTContext(), |
| 5483 | llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType()) |
| 5484 | .GetFormat(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5485 | case clang::Type::DependentSizedArray: |
| 5486 | case clang::Type::DependentSizedExtVector: |
| 5487 | case clang::Type::UnresolvedUsing: |
| 5488 | case clang::Type::Attributed: |
| 5489 | case clang::Type::TemplateTypeParm: |
| 5490 | case clang::Type::SubstTemplateTypeParm: |
| 5491 | case clang::Type::SubstTemplateTypeParmPack: |
| 5492 | case clang::Type::InjectedClassName: |
| 5493 | case clang::Type::DependentName: |
| 5494 | case clang::Type::DependentTemplateSpecialization: |
| 5495 | case clang::Type::PackExpansion: |
| 5496 | case clang::Type::ObjCObject: |
| 5497 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5498 | case clang::Type::TemplateSpecialization: |
Pavel Labath | 4f19fce2 | 2017-02-17 13:39:50 +0000 | [diff] [blame] | 5499 | case clang::Type::DeducedTemplateSpecialization: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5500 | case clang::Type::Atomic: |
| 5501 | case clang::Type::Adjusted: |
| 5502 | case clang::Type::Pipe: |
| 5503 | break; |
| 5504 | |
| 5505 | // pointer type decayed from an array or function type. |
| 5506 | case clang::Type::Decayed: |
| 5507 | break; |
Zachary Turner | 5a8ad459 | 2016-10-05 17:07:34 +0000 | [diff] [blame] | 5508 | case clang::Type::ObjCTypeParam: |
| 5509 | break; |
Ted Woodward | 66060cf | 2017-10-11 22:42:21 +0000 | [diff] [blame] | 5510 | |
| 5511 | case clang::Type::DependentAddressSpace: |
| 5512 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5513 | } |
| 5514 | // We don't know hot to display this type... |
| 5515 | return lldb::eFormatBytes; |
| 5516 | } |
| 5517 | |
| 5518 | static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl, |
| 5519 | bool check_superclass) { |
| 5520 | while (class_interface_decl) { |
| 5521 | if (class_interface_decl->ivar_size() > 0) |
| 5522 | return true; |
| 5523 | |
| 5524 | if (check_superclass) |
| 5525 | class_interface_decl = class_interface_decl->getSuperClass(); |
| 5526 | else |
| 5527 | break; |
| 5528 | } |
| 5529 | return false; |
| 5530 | } |
| 5531 | |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 5532 | static llvm::Optional<SymbolFile::ArrayInfo> |
| 5533 | GetDynamicArrayInfo(ClangASTContext &ast, SymbolFile *sym_file, |
| 5534 | clang::QualType qual_type, |
| 5535 | const ExecutionContext *exe_ctx) { |
| 5536 | if (qual_type->isIncompleteArrayType()) |
| 5537 | if (auto *metadata = ast.GetMetadata(qual_type.getAsOpaquePtr())) |
Pavel Labath | ffec31e | 2018-12-28 13:34:44 +0000 | [diff] [blame] | 5538 | return sym_file->GetDynamicArrayInfoForUID(metadata->GetUserID(), |
| 5539 | exe_ctx); |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 5540 | return llvm::None; |
| 5541 | } |
| 5542 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5543 | uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type, |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 5544 | bool omit_empty_base_classes, |
| 5545 | const ExecutionContext *exe_ctx) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5546 | if (!type) |
| 5547 | return 0; |
| 5548 | |
| 5549 | uint32_t num_children = 0; |
| 5550 | clang::QualType qual_type(GetQualType(type)); |
| 5551 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 5552 | switch (type_class) { |
| 5553 | case clang::Type::Builtin: |
| 5554 | switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { |
| 5555 | case clang::BuiltinType::ObjCId: // child is Class |
| 5556 | case clang::BuiltinType::ObjCClass: // child is Class |
| 5557 | num_children = 1; |
| 5558 | break; |
| 5559 | |
| 5560 | default: |
| 5561 | break; |
| 5562 | } |
| 5563 | break; |
| 5564 | |
| 5565 | case clang::Type::Complex: |
| 5566 | return 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5567 | case clang::Type::Record: |
| 5568 | if (GetCompleteQualType(getASTContext(), qual_type)) { |
| 5569 | const clang::RecordType *record_type = |
| 5570 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 5571 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 5572 | assert(record_decl); |
| 5573 | const clang::CXXRecordDecl *cxx_record_decl = |
| 5574 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 5575 | if (cxx_record_decl) { |
| 5576 | if (omit_empty_base_classes) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5577 | // Check each base classes to see if it or any of its base classes |
| 5578 | // contain any fields. This can help limit the noise in variable |
| 5579 | // views by not having to show base classes that contain no members. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5580 | clang::CXXRecordDecl::base_class_const_iterator base_class, |
| 5581 | base_class_end; |
| 5582 | for (base_class = cxx_record_decl->bases_begin(), |
| 5583 | base_class_end = cxx_record_decl->bases_end(); |
| 5584 | base_class != base_class_end; ++base_class) { |
| 5585 | const clang::CXXRecordDecl *base_class_decl = |
| 5586 | llvm::cast<clang::CXXRecordDecl>( |
| 5587 | base_class->getType() |
| 5588 | ->getAs<clang::RecordType>() |
| 5589 | ->getDecl()); |
| 5590 | |
| 5591 | // Skip empty base classes |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 5592 | if (!ClangASTContext::RecordHasFields(base_class_decl)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5593 | continue; |
| 5594 | |
| 5595 | num_children++; |
| 5596 | } |
| 5597 | } else { |
| 5598 | // Include all base classes |
| 5599 | num_children += cxx_record_decl->getNumBases(); |
| 5600 | } |
| 5601 | } |
| 5602 | clang::RecordDecl::field_iterator field, field_end; |
| 5603 | for (field = record_decl->field_begin(), |
| 5604 | field_end = record_decl->field_end(); |
| 5605 | field != field_end; ++field) |
| 5606 | ++num_children; |
| 5607 | } |
| 5608 | break; |
| 5609 | |
| 5610 | case clang::Type::ObjCObject: |
| 5611 | case clang::Type::ObjCInterface: |
| 5612 | if (GetCompleteQualType(getASTContext(), qual_type)) { |
| 5613 | const clang::ObjCObjectType *objc_class_type = |
| 5614 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 5615 | assert(objc_class_type); |
| 5616 | if (objc_class_type) { |
| 5617 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 5618 | objc_class_type->getInterface(); |
| 5619 | |
| 5620 | if (class_interface_decl) { |
| 5621 | |
| 5622 | clang::ObjCInterfaceDecl *superclass_interface_decl = |
| 5623 | class_interface_decl->getSuperClass(); |
| 5624 | if (superclass_interface_decl) { |
| 5625 | if (omit_empty_base_classes) { |
| 5626 | if (ObjCDeclHasIVars(superclass_interface_decl, true)) |
| 5627 | ++num_children; |
| 5628 | } else |
| 5629 | ++num_children; |
| 5630 | } |
| 5631 | |
| 5632 | num_children += class_interface_decl->ivar_size(); |
| 5633 | } |
| 5634 | } |
| 5635 | } |
| 5636 | break; |
| 5637 | |
| 5638 | case clang::Type::ObjCObjectPointer: { |
| 5639 | const clang::ObjCObjectPointerType *pointer_type = |
| 5640 | llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr()); |
| 5641 | clang::QualType pointee_type = pointer_type->getPointeeType(); |
| 5642 | uint32_t num_pointee_children = |
| 5643 | CompilerType(getASTContext(), pointee_type) |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 5644 | .GetNumChildren(omit_empty_base_classes, exe_ctx); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5645 | // If this type points to a simple type, then it has 1 child |
| 5646 | if (num_pointee_children == 0) |
| 5647 | num_children = 1; |
| 5648 | else |
| 5649 | num_children = num_pointee_children; |
| 5650 | } break; |
| 5651 | |
| 5652 | case clang::Type::Vector: |
| 5653 | case clang::Type::ExtVector: |
| 5654 | num_children = |
| 5655 | llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements(); |
| 5656 | break; |
| 5657 | |
| 5658 | case clang::Type::ConstantArray: |
| 5659 | num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr()) |
| 5660 | ->getSize() |
| 5661 | .getLimitedValue(); |
| 5662 | break; |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 5663 | case clang::Type::IncompleteArray: |
| 5664 | if (auto array_info = |
| 5665 | GetDynamicArrayInfo(*this, GetSymbolFile(), qual_type, exe_ctx)) |
| 5666 | // Only 1-dimensional arrays are supported. |
| 5667 | num_children = array_info->element_orders.size() |
| 5668 | ? array_info->element_orders.back() |
| 5669 | : 0; |
| 5670 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5671 | |
| 5672 | case clang::Type::Pointer: { |
| 5673 | const clang::PointerType *pointer_type = |
| 5674 | llvm::cast<clang::PointerType>(qual_type.getTypePtr()); |
| 5675 | clang::QualType pointee_type(pointer_type->getPointeeType()); |
| 5676 | uint32_t num_pointee_children = |
| 5677 | CompilerType(getASTContext(), pointee_type) |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 5678 | .GetNumChildren(omit_empty_base_classes, exe_ctx); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5679 | if (num_pointee_children == 0) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5680 | // We have a pointer to a pointee type that claims it has no children. We |
| 5681 | // will want to look at |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5682 | num_children = GetNumPointeeChildren(pointee_type); |
| 5683 | } else |
| 5684 | num_children = num_pointee_children; |
| 5685 | } break; |
| 5686 | |
| 5687 | case clang::Type::LValueReference: |
| 5688 | case clang::Type::RValueReference: { |
| 5689 | const clang::ReferenceType *reference_type = |
| 5690 | llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()); |
| 5691 | clang::QualType pointee_type = reference_type->getPointeeType(); |
| 5692 | uint32_t num_pointee_children = |
| 5693 | CompilerType(getASTContext(), pointee_type) |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 5694 | .GetNumChildren(omit_empty_base_classes, exe_ctx); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5695 | // If this type points to a simple type, then it has 1 child |
| 5696 | if (num_pointee_children == 0) |
| 5697 | num_children = 1; |
| 5698 | else |
| 5699 | num_children = num_pointee_children; |
| 5700 | } break; |
| 5701 | |
| 5702 | case clang::Type::Typedef: |
| 5703 | num_children = |
| 5704 | CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type) |
| 5705 | ->getDecl() |
| 5706 | ->getUnderlyingType()) |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 5707 | .GetNumChildren(omit_empty_base_classes, exe_ctx); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5708 | break; |
| 5709 | |
| 5710 | case clang::Type::Auto: |
| 5711 | num_children = |
| 5712 | CompilerType(getASTContext(), |
| 5713 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 5714 | .GetNumChildren(omit_empty_base_classes, exe_ctx); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5715 | break; |
| 5716 | |
| 5717 | case clang::Type::Elaborated: |
| 5718 | num_children = |
| 5719 | CompilerType( |
| 5720 | getASTContext(), |
| 5721 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 5722 | .GetNumChildren(omit_empty_base_classes, exe_ctx); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5723 | break; |
| 5724 | |
| 5725 | case clang::Type::Paren: |
| 5726 | num_children = |
| 5727 | CompilerType(getASTContext(), |
| 5728 | llvm::cast<clang::ParenType>(qual_type)->desugar()) |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 5729 | .GetNumChildren(omit_empty_base_classes, exe_ctx); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5730 | break; |
| 5731 | default: |
| 5732 | break; |
| 5733 | } |
| 5734 | return num_children; |
| 5735 | } |
| 5736 | |
| 5737 | CompilerType ClangASTContext::GetBuiltinTypeByName(const ConstString &name) { |
| 5738 | return GetBasicType(GetBasicTypeEnumeration(name)); |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 5739 | } |
| 5740 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5741 | lldb::BasicType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5742 | ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) { |
| 5743 | if (type) { |
| 5744 | clang::QualType qual_type(GetQualType(type)); |
| 5745 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 5746 | if (type_class == clang::Type::Builtin) { |
| 5747 | switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { |
| 5748 | case clang::BuiltinType::Void: |
| 5749 | return eBasicTypeVoid; |
| 5750 | case clang::BuiltinType::Bool: |
| 5751 | return eBasicTypeBool; |
| 5752 | case clang::BuiltinType::Char_S: |
| 5753 | return eBasicTypeSignedChar; |
| 5754 | case clang::BuiltinType::Char_U: |
| 5755 | return eBasicTypeUnsignedChar; |
| 5756 | case clang::BuiltinType::Char16: |
| 5757 | return eBasicTypeChar16; |
| 5758 | case clang::BuiltinType::Char32: |
| 5759 | return eBasicTypeChar32; |
| 5760 | case clang::BuiltinType::UChar: |
| 5761 | return eBasicTypeUnsignedChar; |
| 5762 | case clang::BuiltinType::SChar: |
| 5763 | return eBasicTypeSignedChar; |
| 5764 | case clang::BuiltinType::WChar_S: |
| 5765 | return eBasicTypeSignedWChar; |
| 5766 | case clang::BuiltinType::WChar_U: |
| 5767 | return eBasicTypeUnsignedWChar; |
| 5768 | case clang::BuiltinType::Short: |
| 5769 | return eBasicTypeShort; |
| 5770 | case clang::BuiltinType::UShort: |
| 5771 | return eBasicTypeUnsignedShort; |
| 5772 | case clang::BuiltinType::Int: |
| 5773 | return eBasicTypeInt; |
| 5774 | case clang::BuiltinType::UInt: |
| 5775 | return eBasicTypeUnsignedInt; |
| 5776 | case clang::BuiltinType::Long: |
| 5777 | return eBasicTypeLong; |
| 5778 | case clang::BuiltinType::ULong: |
| 5779 | return eBasicTypeUnsignedLong; |
| 5780 | case clang::BuiltinType::LongLong: |
| 5781 | return eBasicTypeLongLong; |
| 5782 | case clang::BuiltinType::ULongLong: |
| 5783 | return eBasicTypeUnsignedLongLong; |
| 5784 | case clang::BuiltinType::Int128: |
| 5785 | return eBasicTypeInt128; |
| 5786 | case clang::BuiltinType::UInt128: |
| 5787 | return eBasicTypeUnsignedInt128; |
| 5788 | |
| 5789 | case clang::BuiltinType::Half: |
| 5790 | return eBasicTypeHalf; |
| 5791 | case clang::BuiltinType::Float: |
| 5792 | return eBasicTypeFloat; |
| 5793 | case clang::BuiltinType::Double: |
| 5794 | return eBasicTypeDouble; |
| 5795 | case clang::BuiltinType::LongDouble: |
| 5796 | return eBasicTypeLongDouble; |
| 5797 | |
| 5798 | case clang::BuiltinType::NullPtr: |
| 5799 | return eBasicTypeNullPtr; |
| 5800 | case clang::BuiltinType::ObjCId: |
| 5801 | return eBasicTypeObjCID; |
| 5802 | case clang::BuiltinType::ObjCClass: |
| 5803 | return eBasicTypeObjCClass; |
| 5804 | case clang::BuiltinType::ObjCSel: |
| 5805 | return eBasicTypeObjCSel; |
| 5806 | default: |
| 5807 | return eBasicTypeOther; |
| 5808 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5809 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5810 | } |
| 5811 | return eBasicTypeInvalid; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5812 | } |
| 5813 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5814 | void ClangASTContext::ForEachEnumerator( |
| 5815 | lldb::opaque_compiler_type_t type, |
| 5816 | std::function<bool(const CompilerType &integer_type, |
| 5817 | const ConstString &name, |
| 5818 | const llvm::APSInt &value)> const &callback) { |
| 5819 | const clang::EnumType *enum_type = |
| 5820 | llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type)); |
| 5821 | if (enum_type) { |
| 5822 | const clang::EnumDecl *enum_decl = enum_type->getDecl(); |
| 5823 | if (enum_decl) { |
| 5824 | CompilerType integer_type(this, |
| 5825 | enum_decl->getIntegerType().getAsOpaquePtr()); |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 5826 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5827 | clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos; |
| 5828 | for (enum_pos = enum_decl->enumerator_begin(), |
| 5829 | enum_end_pos = enum_decl->enumerator_end(); |
| 5830 | enum_pos != enum_end_pos; ++enum_pos) { |
| 5831 | ConstString name(enum_pos->getNameAsString().c_str()); |
| 5832 | if (!callback(integer_type, name, enum_pos->getInitVal())) |
| 5833 | break; |
| 5834 | } |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 5835 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5836 | } |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 5837 | } |
| 5838 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5839 | #pragma mark Aggregate Types |
| 5840 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5841 | uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) { |
| 5842 | if (!type) |
| 5843 | return 0; |
Enrico Granata | 36f51e4 | 2015-12-18 22:41:25 +0000 | [diff] [blame] | 5844 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5845 | uint32_t count = 0; |
| 5846 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 5847 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 5848 | switch (type_class) { |
| 5849 | case clang::Type::Record: |
| 5850 | if (GetCompleteType(type)) { |
| 5851 | const clang::RecordType *record_type = |
| 5852 | llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr()); |
| 5853 | if (record_type) { |
| 5854 | clang::RecordDecl *record_decl = record_type->getDecl(); |
| 5855 | if (record_decl) { |
| 5856 | uint32_t field_idx = 0; |
| 5857 | clang::RecordDecl::field_iterator field, field_end; |
| 5858 | for (field = record_decl->field_begin(), |
| 5859 | field_end = record_decl->field_end(); |
| 5860 | field != field_end; ++field) |
| 5861 | ++field_idx; |
| 5862 | count = field_idx; |
| 5863 | } |
| 5864 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5865 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5866 | break; |
| 5867 | |
| 5868 | case clang::Type::Typedef: |
| 5869 | count = |
| 5870 | CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type) |
| 5871 | ->getDecl() |
| 5872 | ->getUnderlyingType()) |
| 5873 | .GetNumFields(); |
| 5874 | break; |
| 5875 | |
| 5876 | case clang::Type::Auto: |
| 5877 | count = |
| 5878 | CompilerType(getASTContext(), |
| 5879 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) |
| 5880 | .GetNumFields(); |
| 5881 | break; |
| 5882 | |
| 5883 | case clang::Type::Elaborated: |
| 5884 | count = CompilerType( |
| 5885 | getASTContext(), |
| 5886 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) |
| 5887 | .GetNumFields(); |
| 5888 | break; |
| 5889 | |
| 5890 | case clang::Type::Paren: |
| 5891 | count = CompilerType(getASTContext(), |
| 5892 | llvm::cast<clang::ParenType>(qual_type)->desugar()) |
| 5893 | .GetNumFields(); |
| 5894 | break; |
| 5895 | |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 5896 | case clang::Type::ObjCObjectPointer: { |
| 5897 | const clang::ObjCObjectPointerType *objc_class_type = |
Sean Callanan | 732a6f4 | 2017-05-15 19:55:20 +0000 | [diff] [blame] | 5898 | qual_type->getAs<clang::ObjCObjectPointerType>(); |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 5899 | const clang::ObjCInterfaceType *objc_interface_type = |
| 5900 | objc_class_type->getInterfaceType(); |
| 5901 | if (objc_interface_type && |
Davide Italiano | 52ffb53 | 2017-04-17 18:24:18 +0000 | [diff] [blame] | 5902 | GetCompleteType(static_cast<lldb::opaque_compiler_type_t>( |
| 5903 | const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) { |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 5904 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 5905 | objc_interface_type->getDecl(); |
| 5906 | if (class_interface_decl) { |
| 5907 | count = class_interface_decl->ivar_size(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5908 | } |
| 5909 | } |
| 5910 | break; |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 5911 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5912 | |
| 5913 | case clang::Type::ObjCObject: |
| 5914 | case clang::Type::ObjCInterface: |
| 5915 | if (GetCompleteType(type)) { |
| 5916 | const clang::ObjCObjectType *objc_class_type = |
| 5917 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 5918 | if (objc_class_type) { |
| 5919 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 5920 | objc_class_type->getInterface(); |
| 5921 | |
| 5922 | if (class_interface_decl) |
| 5923 | count = class_interface_decl->ivar_size(); |
| 5924 | } |
| 5925 | } |
| 5926 | break; |
| 5927 | |
| 5928 | default: |
| 5929 | break; |
| 5930 | } |
| 5931 | return count; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5932 | } |
| 5933 | |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 5934 | static lldb::opaque_compiler_type_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5935 | GetObjCFieldAtIndex(clang::ASTContext *ast, |
| 5936 | clang::ObjCInterfaceDecl *class_interface_decl, size_t idx, |
| 5937 | std::string &name, uint64_t *bit_offset_ptr, |
| 5938 | uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) { |
| 5939 | if (class_interface_decl) { |
| 5940 | if (idx < (class_interface_decl->ivar_size())) { |
| 5941 | clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, |
| 5942 | ivar_end = class_interface_decl->ivar_end(); |
| 5943 | uint32_t ivar_idx = 0; |
| 5944 | |
| 5945 | for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; |
| 5946 | ++ivar_pos, ++ivar_idx) { |
| 5947 | if (ivar_idx == idx) { |
| 5948 | const clang::ObjCIvarDecl *ivar_decl = *ivar_pos; |
| 5949 | |
| 5950 | clang::QualType ivar_qual_type(ivar_decl->getType()); |
| 5951 | |
| 5952 | name.assign(ivar_decl->getNameAsString()); |
| 5953 | |
| 5954 | if (bit_offset_ptr) { |
| 5955 | const clang::ASTRecordLayout &interface_layout = |
| 5956 | ast->getASTObjCInterfaceLayout(class_interface_decl); |
| 5957 | *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx); |
| 5958 | } |
| 5959 | |
| 5960 | const bool is_bitfield = ivar_pos->isBitField(); |
| 5961 | |
| 5962 | if (bitfield_bit_size_ptr) { |
| 5963 | *bitfield_bit_size_ptr = 0; |
| 5964 | |
| 5965 | if (is_bitfield && ast) { |
| 5966 | clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth(); |
Hans Wennborg | 30ce962 | 2018-11-28 14:30:18 +0000 | [diff] [blame] | 5967 | clang::Expr::EvalResult result; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5968 | if (bitfield_bit_size_expr && |
Hans Wennborg | 30ce962 | 2018-11-28 14:30:18 +0000 | [diff] [blame] | 5969 | bitfield_bit_size_expr->EvaluateAsInt(result, *ast)) { |
| 5970 | llvm::APSInt bitfield_apsint = result.Val.getInt(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5971 | *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue(); |
| 5972 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5973 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5974 | } |
| 5975 | if (is_bitfield_ptr) |
| 5976 | *is_bitfield_ptr = is_bitfield; |
| 5977 | |
| 5978 | return ivar_qual_type.getAsOpaquePtr(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5979 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5980 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5981 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5982 | } |
| 5983 | return nullptr; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5984 | } |
| 5985 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5986 | CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type, |
| 5987 | size_t idx, std::string &name, |
| 5988 | uint64_t *bit_offset_ptr, |
| 5989 | uint32_t *bitfield_bit_size_ptr, |
| 5990 | bool *is_bitfield_ptr) { |
| 5991 | if (!type) |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 5992 | return CompilerType(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5993 | |
| 5994 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 5995 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 5996 | switch (type_class) { |
| 5997 | case clang::Type::Record: |
| 5998 | if (GetCompleteType(type)) { |
| 5999 | const clang::RecordType *record_type = |
| 6000 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 6001 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 6002 | uint32_t field_idx = 0; |
| 6003 | clang::RecordDecl::field_iterator field, field_end; |
| 6004 | for (field = record_decl->field_begin(), |
| 6005 | field_end = record_decl->field_end(); |
| 6006 | field != field_end; ++field, ++field_idx) { |
| 6007 | if (idx == field_idx) { |
| 6008 | // Print the member type if requested |
| 6009 | // Print the member name and equal sign |
| 6010 | name.assign(field->getNameAsString()); |
| 6011 | |
| 6012 | // Figure out the type byte size (field_type_info.first) and |
| 6013 | // alignment (field_type_info.second) from the AST context. |
| 6014 | if (bit_offset_ptr) { |
| 6015 | const clang::ASTRecordLayout &record_layout = |
| 6016 | getASTContext()->getASTRecordLayout(record_decl); |
| 6017 | *bit_offset_ptr = record_layout.getFieldOffset(field_idx); |
| 6018 | } |
| 6019 | |
| 6020 | const bool is_bitfield = field->isBitField(); |
| 6021 | |
| 6022 | if (bitfield_bit_size_ptr) { |
| 6023 | *bitfield_bit_size_ptr = 0; |
| 6024 | |
| 6025 | if (is_bitfield) { |
| 6026 | clang::Expr *bitfield_bit_size_expr = field->getBitWidth(); |
Hans Wennborg | 30ce962 | 2018-11-28 14:30:18 +0000 | [diff] [blame] | 6027 | clang::Expr::EvalResult result; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6028 | if (bitfield_bit_size_expr && |
Hans Wennborg | 30ce962 | 2018-11-28 14:30:18 +0000 | [diff] [blame] | 6029 | bitfield_bit_size_expr->EvaluateAsInt(result, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6030 | *getASTContext())) { |
Hans Wennborg | 30ce962 | 2018-11-28 14:30:18 +0000 | [diff] [blame] | 6031 | llvm::APSInt bitfield_apsint = result.Val.getInt(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6032 | *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue(); |
| 6033 | } |
| 6034 | } |
| 6035 | } |
| 6036 | if (is_bitfield_ptr) |
| 6037 | *is_bitfield_ptr = is_bitfield; |
| 6038 | |
| 6039 | return CompilerType(getASTContext(), field->getType()); |
| 6040 | } |
| 6041 | } |
| 6042 | } |
| 6043 | break; |
| 6044 | |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 6045 | case clang::Type::ObjCObjectPointer: { |
| 6046 | const clang::ObjCObjectPointerType *objc_class_type = |
Sean Callanan | 732a6f4 | 2017-05-15 19:55:20 +0000 | [diff] [blame] | 6047 | qual_type->getAs<clang::ObjCObjectPointerType>(); |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 6048 | const clang::ObjCInterfaceType *objc_interface_type = |
| 6049 | objc_class_type->getInterfaceType(); |
| 6050 | if (objc_interface_type && |
Davide Italiano | 52ffb53 | 2017-04-17 18:24:18 +0000 | [diff] [blame] | 6051 | GetCompleteType(static_cast<lldb::opaque_compiler_type_t>( |
| 6052 | const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) { |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 6053 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 6054 | objc_interface_type->getDecl(); |
| 6055 | if (class_interface_decl) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6056 | return CompilerType( |
| 6057 | this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl, |
| 6058 | idx, name, bit_offset_ptr, |
| 6059 | bitfield_bit_size_ptr, is_bitfield_ptr)); |
| 6060 | } |
| 6061 | } |
| 6062 | break; |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 6063 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6064 | |
| 6065 | case clang::Type::ObjCObject: |
| 6066 | case clang::Type::ObjCInterface: |
| 6067 | if (GetCompleteType(type)) { |
| 6068 | const clang::ObjCObjectType *objc_class_type = |
| 6069 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 6070 | assert(objc_class_type); |
| 6071 | if (objc_class_type) { |
| 6072 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 6073 | objc_class_type->getInterface(); |
| 6074 | return CompilerType( |
| 6075 | this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl, |
| 6076 | idx, name, bit_offset_ptr, |
| 6077 | bitfield_bit_size_ptr, is_bitfield_ptr)); |
| 6078 | } |
| 6079 | } |
| 6080 | break; |
| 6081 | |
| 6082 | case clang::Type::Typedef: |
| 6083 | return CompilerType(getASTContext(), |
| 6084 | llvm::cast<clang::TypedefType>(qual_type) |
| 6085 | ->getDecl() |
| 6086 | ->getUnderlyingType()) |
| 6087 | .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr, |
| 6088 | is_bitfield_ptr); |
| 6089 | |
| 6090 | case clang::Type::Auto: |
| 6091 | return CompilerType( |
| 6092 | getASTContext(), |
| 6093 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) |
| 6094 | .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr, |
| 6095 | is_bitfield_ptr); |
| 6096 | |
| 6097 | case clang::Type::Elaborated: |
| 6098 | return CompilerType( |
| 6099 | getASTContext(), |
| 6100 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) |
| 6101 | .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr, |
| 6102 | is_bitfield_ptr); |
| 6103 | |
| 6104 | case clang::Type::Paren: |
| 6105 | return CompilerType(getASTContext(), |
| 6106 | llvm::cast<clang::ParenType>(qual_type)->desugar()) |
| 6107 | .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr, |
| 6108 | is_bitfield_ptr); |
| 6109 | |
| 6110 | default: |
| 6111 | break; |
| 6112 | } |
| 6113 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 6114 | } |
| 6115 | |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6116 | uint32_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6117 | ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) { |
| 6118 | uint32_t count = 0; |
| 6119 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 6120 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 6121 | switch (type_class) { |
| 6122 | case clang::Type::Record: |
| 6123 | if (GetCompleteType(type)) { |
| 6124 | const clang::CXXRecordDecl *cxx_record_decl = |
| 6125 | qual_type->getAsCXXRecordDecl(); |
| 6126 | if (cxx_record_decl) |
| 6127 | count = cxx_record_decl->getNumBases(); |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6128 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6129 | break; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6130 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6131 | case clang::Type::ObjCObjectPointer: |
| 6132 | count = GetPointeeType(type).GetNumDirectBaseClasses(); |
| 6133 | break; |
| 6134 | |
| 6135 | case clang::Type::ObjCObject: |
| 6136 | if (GetCompleteType(type)) { |
| 6137 | const clang::ObjCObjectType *objc_class_type = |
| 6138 | qual_type->getAsObjCQualifiedInterfaceType(); |
| 6139 | if (objc_class_type) { |
| 6140 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 6141 | objc_class_type->getInterface(); |
| 6142 | |
| 6143 | if (class_interface_decl && class_interface_decl->getSuperClass()) |
| 6144 | count = 1; |
| 6145 | } |
| 6146 | } |
| 6147 | break; |
| 6148 | case clang::Type::ObjCInterface: |
| 6149 | if (GetCompleteType(type)) { |
| 6150 | const clang::ObjCInterfaceType *objc_interface_type = |
| 6151 | qual_type->getAs<clang::ObjCInterfaceType>(); |
| 6152 | if (objc_interface_type) { |
| 6153 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 6154 | objc_interface_type->getInterface(); |
| 6155 | |
| 6156 | if (class_interface_decl && class_interface_decl->getSuperClass()) |
| 6157 | count = 1; |
| 6158 | } |
| 6159 | } |
| 6160 | break; |
| 6161 | |
| 6162 | case clang::Type::Typedef: |
| 6163 | count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type) |
| 6164 | ->getDecl() |
| 6165 | ->getUnderlyingType() |
| 6166 | .getAsOpaquePtr()); |
| 6167 | break; |
| 6168 | |
| 6169 | case clang::Type::Auto: |
| 6170 | count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type) |
| 6171 | ->getDeducedType() |
| 6172 | .getAsOpaquePtr()); |
| 6173 | break; |
| 6174 | |
| 6175 | case clang::Type::Elaborated: |
| 6176 | count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type) |
| 6177 | ->getNamedType() |
| 6178 | .getAsOpaquePtr()); |
| 6179 | break; |
| 6180 | |
| 6181 | case clang::Type::Paren: |
| 6182 | return GetNumDirectBaseClasses( |
| 6183 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()); |
| 6184 | |
| 6185 | default: |
| 6186 | break; |
| 6187 | } |
| 6188 | return count; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6189 | } |
| 6190 | |
| 6191 | uint32_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6192 | ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) { |
| 6193 | uint32_t count = 0; |
| 6194 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 6195 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 6196 | switch (type_class) { |
| 6197 | case clang::Type::Record: |
| 6198 | if (GetCompleteType(type)) { |
| 6199 | const clang::CXXRecordDecl *cxx_record_decl = |
| 6200 | qual_type->getAsCXXRecordDecl(); |
| 6201 | if (cxx_record_decl) |
| 6202 | count = cxx_record_decl->getNumVBases(); |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6203 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6204 | break; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6205 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6206 | case clang::Type::Typedef: |
| 6207 | count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type) |
| 6208 | ->getDecl() |
| 6209 | ->getUnderlyingType() |
| 6210 | .getAsOpaquePtr()); |
| 6211 | break; |
| 6212 | |
| 6213 | case clang::Type::Auto: |
| 6214 | count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type) |
| 6215 | ->getDeducedType() |
| 6216 | .getAsOpaquePtr()); |
| 6217 | break; |
| 6218 | |
| 6219 | case clang::Type::Elaborated: |
| 6220 | count = |
| 6221 | GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type) |
| 6222 | ->getNamedType() |
| 6223 | .getAsOpaquePtr()); |
| 6224 | break; |
| 6225 | |
| 6226 | case clang::Type::Paren: |
| 6227 | count = GetNumVirtualBaseClasses( |
| 6228 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()); |
| 6229 | break; |
| 6230 | |
| 6231 | default: |
| 6232 | break; |
| 6233 | } |
| 6234 | return count; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6235 | } |
| 6236 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6237 | CompilerType ClangASTContext::GetDirectBaseClassAtIndex( |
| 6238 | lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) { |
| 6239 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 6240 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 6241 | switch (type_class) { |
| 6242 | case clang::Type::Record: |
| 6243 | if (GetCompleteType(type)) { |
| 6244 | const clang::CXXRecordDecl *cxx_record_decl = |
| 6245 | qual_type->getAsCXXRecordDecl(); |
| 6246 | if (cxx_record_decl) { |
| 6247 | uint32_t curr_idx = 0; |
| 6248 | clang::CXXRecordDecl::base_class_const_iterator base_class, |
| 6249 | base_class_end; |
| 6250 | for (base_class = cxx_record_decl->bases_begin(), |
| 6251 | base_class_end = cxx_record_decl->bases_end(); |
| 6252 | base_class != base_class_end; ++base_class, ++curr_idx) { |
| 6253 | if (curr_idx == idx) { |
| 6254 | if (bit_offset_ptr) { |
| 6255 | const clang::ASTRecordLayout &record_layout = |
| 6256 | getASTContext()->getASTRecordLayout(cxx_record_decl); |
| 6257 | const clang::CXXRecordDecl *base_class_decl = |
| 6258 | llvm::cast<clang::CXXRecordDecl>( |
| 6259 | base_class->getType() |
| 6260 | ->getAs<clang::RecordType>() |
| 6261 | ->getDecl()); |
| 6262 | if (base_class->isVirtual()) |
| 6263 | *bit_offset_ptr = |
| 6264 | record_layout.getVBaseClassOffset(base_class_decl) |
| 6265 | .getQuantity() * |
| 6266 | 8; |
| 6267 | else |
| 6268 | *bit_offset_ptr = |
| 6269 | record_layout.getBaseClassOffset(base_class_decl) |
| 6270 | .getQuantity() * |
| 6271 | 8; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6272 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6273 | return CompilerType(this, base_class->getType().getAsOpaquePtr()); |
| 6274 | } |
| 6275 | } |
| 6276 | } |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6277 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6278 | break; |
| 6279 | |
| 6280 | case clang::Type::ObjCObjectPointer: |
| 6281 | return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr); |
| 6282 | |
| 6283 | case clang::Type::ObjCObject: |
| 6284 | if (idx == 0 && GetCompleteType(type)) { |
| 6285 | const clang::ObjCObjectType *objc_class_type = |
| 6286 | qual_type->getAsObjCQualifiedInterfaceType(); |
| 6287 | if (objc_class_type) { |
| 6288 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 6289 | objc_class_type->getInterface(); |
| 6290 | |
| 6291 | if (class_interface_decl) { |
| 6292 | clang::ObjCInterfaceDecl *superclass_interface_decl = |
| 6293 | class_interface_decl->getSuperClass(); |
| 6294 | if (superclass_interface_decl) { |
| 6295 | if (bit_offset_ptr) |
| 6296 | *bit_offset_ptr = 0; |
| 6297 | return CompilerType(getASTContext(), |
| 6298 | getASTContext()->getObjCInterfaceType( |
| 6299 | superclass_interface_decl)); |
| 6300 | } |
| 6301 | } |
| 6302 | } |
| 6303 | } |
| 6304 | break; |
| 6305 | case clang::Type::ObjCInterface: |
| 6306 | if (idx == 0 && GetCompleteType(type)) { |
| 6307 | const clang::ObjCObjectType *objc_interface_type = |
| 6308 | qual_type->getAs<clang::ObjCInterfaceType>(); |
| 6309 | if (objc_interface_type) { |
| 6310 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 6311 | objc_interface_type->getInterface(); |
| 6312 | |
| 6313 | if (class_interface_decl) { |
| 6314 | clang::ObjCInterfaceDecl *superclass_interface_decl = |
| 6315 | class_interface_decl->getSuperClass(); |
| 6316 | if (superclass_interface_decl) { |
| 6317 | if (bit_offset_ptr) |
| 6318 | *bit_offset_ptr = 0; |
| 6319 | return CompilerType(getASTContext(), |
| 6320 | getASTContext()->getObjCInterfaceType( |
| 6321 | superclass_interface_decl)); |
| 6322 | } |
| 6323 | } |
| 6324 | } |
| 6325 | } |
| 6326 | break; |
| 6327 | |
| 6328 | case clang::Type::Typedef: |
| 6329 | return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type) |
| 6330 | ->getDecl() |
| 6331 | ->getUnderlyingType() |
| 6332 | .getAsOpaquePtr(), |
| 6333 | idx, bit_offset_ptr); |
| 6334 | |
| 6335 | case clang::Type::Auto: |
| 6336 | return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type) |
| 6337 | ->getDeducedType() |
| 6338 | .getAsOpaquePtr(), |
| 6339 | idx, bit_offset_ptr); |
| 6340 | |
| 6341 | case clang::Type::Elaborated: |
| 6342 | return GetDirectBaseClassAtIndex( |
| 6343 | llvm::cast<clang::ElaboratedType>(qual_type) |
| 6344 | ->getNamedType() |
| 6345 | .getAsOpaquePtr(), |
| 6346 | idx, bit_offset_ptr); |
| 6347 | |
| 6348 | case clang::Type::Paren: |
| 6349 | return GetDirectBaseClassAtIndex( |
| 6350 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 6351 | idx, bit_offset_ptr); |
| 6352 | |
| 6353 | default: |
| 6354 | break; |
| 6355 | } |
| 6356 | return CompilerType(); |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6357 | } |
| 6358 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6359 | CompilerType ClangASTContext::GetVirtualBaseClassAtIndex( |
| 6360 | lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) { |
| 6361 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 6362 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 6363 | switch (type_class) { |
| 6364 | case clang::Type::Record: |
| 6365 | if (GetCompleteType(type)) { |
| 6366 | const clang::CXXRecordDecl *cxx_record_decl = |
| 6367 | qual_type->getAsCXXRecordDecl(); |
| 6368 | if (cxx_record_decl) { |
| 6369 | uint32_t curr_idx = 0; |
| 6370 | clang::CXXRecordDecl::base_class_const_iterator base_class, |
| 6371 | base_class_end; |
| 6372 | for (base_class = cxx_record_decl->vbases_begin(), |
| 6373 | base_class_end = cxx_record_decl->vbases_end(); |
| 6374 | base_class != base_class_end; ++base_class, ++curr_idx) { |
| 6375 | if (curr_idx == idx) { |
| 6376 | if (bit_offset_ptr) { |
| 6377 | const clang::ASTRecordLayout &record_layout = |
| 6378 | getASTContext()->getASTRecordLayout(cxx_record_decl); |
| 6379 | const clang::CXXRecordDecl *base_class_decl = |
| 6380 | llvm::cast<clang::CXXRecordDecl>( |
| 6381 | base_class->getType() |
| 6382 | ->getAs<clang::RecordType>() |
| 6383 | ->getDecl()); |
| 6384 | *bit_offset_ptr = |
| 6385 | record_layout.getVBaseClassOffset(base_class_decl) |
| 6386 | .getQuantity() * |
| 6387 | 8; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6388 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6389 | return CompilerType(this, base_class->getType().getAsOpaquePtr()); |
| 6390 | } |
| 6391 | } |
| 6392 | } |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6393 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6394 | break; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6395 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6396 | case clang::Type::Typedef: |
| 6397 | return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type) |
| 6398 | ->getDecl() |
| 6399 | ->getUnderlyingType() |
| 6400 | .getAsOpaquePtr(), |
| 6401 | idx, bit_offset_ptr); |
| 6402 | |
| 6403 | case clang::Type::Auto: |
| 6404 | return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type) |
| 6405 | ->getDeducedType() |
| 6406 | .getAsOpaquePtr(), |
| 6407 | idx, bit_offset_ptr); |
| 6408 | |
| 6409 | case clang::Type::Elaborated: |
| 6410 | return GetVirtualBaseClassAtIndex( |
| 6411 | llvm::cast<clang::ElaboratedType>(qual_type) |
| 6412 | ->getNamedType() |
| 6413 | .getAsOpaquePtr(), |
| 6414 | idx, bit_offset_ptr); |
| 6415 | |
| 6416 | case clang::Type::Paren: |
| 6417 | return GetVirtualBaseClassAtIndex( |
| 6418 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 6419 | idx, bit_offset_ptr); |
| 6420 | |
| 6421 | default: |
| 6422 | break; |
| 6423 | } |
| 6424 | return CompilerType(); |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6425 | } |
| 6426 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 6427 | // If a pointer to a pointee type (the clang_type arg) says that it has no |
| 6428 | // children, then we either need to trust it, or override it and return a |
| 6429 | // different result. For example, an "int *" has one child that is an integer, |
| 6430 | // but a function pointer doesn't have any children. Likewise if a Record type |
| 6431 | // claims it has no children, then there really is nothing to show. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6432 | uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) { |
| 6433 | if (type.isNull()) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 6434 | return 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6435 | |
| 6436 | clang::QualType qual_type(type.getCanonicalType()); |
| 6437 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 6438 | switch (type_class) { |
| 6439 | case clang::Type::Builtin: |
| 6440 | switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { |
| 6441 | case clang::BuiltinType::UnknownAny: |
| 6442 | case clang::BuiltinType::Void: |
| 6443 | case clang::BuiltinType::NullPtr: |
| 6444 | case clang::BuiltinType::OCLEvent: |
| 6445 | case clang::BuiltinType::OCLImage1dRO: |
| 6446 | case clang::BuiltinType::OCLImage1dWO: |
| 6447 | case clang::BuiltinType::OCLImage1dRW: |
| 6448 | case clang::BuiltinType::OCLImage1dArrayRO: |
| 6449 | case clang::BuiltinType::OCLImage1dArrayWO: |
| 6450 | case clang::BuiltinType::OCLImage1dArrayRW: |
| 6451 | case clang::BuiltinType::OCLImage1dBufferRO: |
| 6452 | case clang::BuiltinType::OCLImage1dBufferWO: |
| 6453 | case clang::BuiltinType::OCLImage1dBufferRW: |
| 6454 | case clang::BuiltinType::OCLImage2dRO: |
| 6455 | case clang::BuiltinType::OCLImage2dWO: |
| 6456 | case clang::BuiltinType::OCLImage2dRW: |
| 6457 | case clang::BuiltinType::OCLImage2dArrayRO: |
| 6458 | case clang::BuiltinType::OCLImage2dArrayWO: |
| 6459 | case clang::BuiltinType::OCLImage2dArrayRW: |
| 6460 | case clang::BuiltinType::OCLImage3dRO: |
| 6461 | case clang::BuiltinType::OCLImage3dWO: |
| 6462 | case clang::BuiltinType::OCLImage3dRW: |
| 6463 | case clang::BuiltinType::OCLSampler: |
| 6464 | return 0; |
| 6465 | case clang::BuiltinType::Bool: |
| 6466 | case clang::BuiltinType::Char_U: |
| 6467 | case clang::BuiltinType::UChar: |
| 6468 | case clang::BuiltinType::WChar_U: |
| 6469 | case clang::BuiltinType::Char16: |
| 6470 | case clang::BuiltinType::Char32: |
| 6471 | case clang::BuiltinType::UShort: |
| 6472 | case clang::BuiltinType::UInt: |
| 6473 | case clang::BuiltinType::ULong: |
| 6474 | case clang::BuiltinType::ULongLong: |
| 6475 | case clang::BuiltinType::UInt128: |
| 6476 | case clang::BuiltinType::Char_S: |
| 6477 | case clang::BuiltinType::SChar: |
| 6478 | case clang::BuiltinType::WChar_S: |
| 6479 | case clang::BuiltinType::Short: |
| 6480 | case clang::BuiltinType::Int: |
| 6481 | case clang::BuiltinType::Long: |
| 6482 | case clang::BuiltinType::LongLong: |
| 6483 | case clang::BuiltinType::Int128: |
| 6484 | case clang::BuiltinType::Float: |
| 6485 | case clang::BuiltinType::Double: |
| 6486 | case clang::BuiltinType::LongDouble: |
| 6487 | case clang::BuiltinType::Dependent: |
| 6488 | case clang::BuiltinType::Overload: |
| 6489 | case clang::BuiltinType::ObjCId: |
| 6490 | case clang::BuiltinType::ObjCClass: |
| 6491 | case clang::BuiltinType::ObjCSel: |
| 6492 | case clang::BuiltinType::BoundMember: |
| 6493 | case clang::BuiltinType::Half: |
| 6494 | case clang::BuiltinType::ARCUnbridgedCast: |
| 6495 | case clang::BuiltinType::PseudoObject: |
| 6496 | case clang::BuiltinType::BuiltinFn: |
| 6497 | case clang::BuiltinType::OMPArraySection: |
| 6498 | return 1; |
| 6499 | default: |
| 6500 | return 0; |
| 6501 | } |
| 6502 | break; |
| 6503 | |
| 6504 | case clang::Type::Complex: |
| 6505 | return 1; |
| 6506 | case clang::Type::Pointer: |
| 6507 | return 1; |
| 6508 | case clang::Type::BlockPointer: |
| 6509 | return 0; // If block pointers don't have debug info, then no children for |
| 6510 | // them |
| 6511 | case clang::Type::LValueReference: |
| 6512 | return 1; |
| 6513 | case clang::Type::RValueReference: |
| 6514 | return 1; |
| 6515 | case clang::Type::MemberPointer: |
| 6516 | return 0; |
| 6517 | case clang::Type::ConstantArray: |
| 6518 | return 0; |
| 6519 | case clang::Type::IncompleteArray: |
| 6520 | return 0; |
| 6521 | case clang::Type::VariableArray: |
| 6522 | return 0; |
| 6523 | case clang::Type::DependentSizedArray: |
| 6524 | return 0; |
| 6525 | case clang::Type::DependentSizedExtVector: |
| 6526 | return 0; |
| 6527 | case clang::Type::Vector: |
| 6528 | return 0; |
| 6529 | case clang::Type::ExtVector: |
| 6530 | return 0; |
| 6531 | case clang::Type::FunctionProto: |
| 6532 | return 0; // When we function pointers, they have no children... |
| 6533 | case clang::Type::FunctionNoProto: |
| 6534 | return 0; // When we function pointers, they have no children... |
| 6535 | case clang::Type::UnresolvedUsing: |
| 6536 | return 0; |
| 6537 | case clang::Type::Paren: |
| 6538 | return GetNumPointeeChildren( |
| 6539 | llvm::cast<clang::ParenType>(qual_type)->desugar()); |
| 6540 | case clang::Type::Typedef: |
| 6541 | return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type) |
| 6542 | ->getDecl() |
| 6543 | ->getUnderlyingType()); |
| 6544 | case clang::Type::Auto: |
| 6545 | return GetNumPointeeChildren( |
| 6546 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()); |
| 6547 | case clang::Type::Elaborated: |
| 6548 | return GetNumPointeeChildren( |
| 6549 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()); |
| 6550 | case clang::Type::TypeOfExpr: |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 6551 | return GetNumPointeeChildren(llvm::cast<clang::TypeOfExprType>(qual_type) |
| 6552 | ->getUnderlyingExpr() |
| 6553 | ->getType()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6554 | case clang::Type::TypeOf: |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 6555 | return GetNumPointeeChildren( |
| 6556 | llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6557 | case clang::Type::Decltype: |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 6558 | return GetNumPointeeChildren( |
| 6559 | llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6560 | case clang::Type::Record: |
| 6561 | return 0; |
| 6562 | case clang::Type::Enum: |
| 6563 | return 1; |
| 6564 | case clang::Type::TemplateTypeParm: |
| 6565 | return 1; |
| 6566 | case clang::Type::SubstTemplateTypeParm: |
| 6567 | return 1; |
| 6568 | case clang::Type::TemplateSpecialization: |
| 6569 | return 1; |
| 6570 | case clang::Type::InjectedClassName: |
| 6571 | return 0; |
| 6572 | case clang::Type::DependentName: |
| 6573 | return 1; |
| 6574 | case clang::Type::DependentTemplateSpecialization: |
| 6575 | return 1; |
| 6576 | case clang::Type::ObjCObject: |
| 6577 | return 0; |
| 6578 | case clang::Type::ObjCInterface: |
| 6579 | return 0; |
| 6580 | case clang::Type::ObjCObjectPointer: |
| 6581 | return 1; |
| 6582 | default: |
| 6583 | break; |
| 6584 | } |
| 6585 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 6586 | } |
| 6587 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6588 | CompilerType ClangASTContext::GetChildCompilerTypeAtIndex( |
| 6589 | lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx, |
| 6590 | bool transparent_pointers, bool omit_empty_base_classes, |
| 6591 | bool ignore_array_bounds, std::string &child_name, |
| 6592 | uint32_t &child_byte_size, int32_t &child_byte_offset, |
| 6593 | uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset, |
| 6594 | bool &child_is_base_class, bool &child_is_deref_of_parent, |
| 6595 | ValueObject *valobj, uint64_t &language_flags) { |
| 6596 | if (!type) |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 6597 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 6598 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6599 | clang::QualType parent_qual_type(GetCanonicalQualType(type)); |
| 6600 | const clang::Type::TypeClass parent_type_class = |
| 6601 | parent_qual_type->getTypeClass(); |
| 6602 | child_bitfield_bit_size = 0; |
| 6603 | child_bitfield_bit_offset = 0; |
| 6604 | child_is_base_class = false; |
| 6605 | language_flags = 0; |
| 6606 | |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 6607 | const bool idx_is_valid = |
| 6608 | idx < GetNumChildren(type, omit_empty_base_classes, exe_ctx); |
Aleksandr Urakov | 7d2a74f | 2018-08-14 07:57:44 +0000 | [diff] [blame] | 6609 | int32_t bit_offset; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6610 | switch (parent_type_class) { |
| 6611 | case clang::Type::Builtin: |
| 6612 | if (idx_is_valid) { |
| 6613 | switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) { |
| 6614 | case clang::BuiltinType::ObjCId: |
| 6615 | case clang::BuiltinType::ObjCClass: |
| 6616 | child_name = "isa"; |
| 6617 | child_byte_size = |
| 6618 | getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) / |
| 6619 | CHAR_BIT; |
| 6620 | return CompilerType(getASTContext(), |
| 6621 | getASTContext()->ObjCBuiltinClassTy); |
| 6622 | |
| 6623 | default: |
| 6624 | break; |
| 6625 | } |
| 6626 | } |
| 6627 | break; |
| 6628 | |
| 6629 | case clang::Type::Record: |
| 6630 | if (idx_is_valid && GetCompleteType(type)) { |
| 6631 | const clang::RecordType *record_type = |
| 6632 | llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr()); |
| 6633 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 6634 | assert(record_decl); |
| 6635 | const clang::ASTRecordLayout &record_layout = |
| 6636 | getASTContext()->getASTRecordLayout(record_decl); |
| 6637 | uint32_t child_idx = 0; |
| 6638 | |
| 6639 | const clang::CXXRecordDecl *cxx_record_decl = |
| 6640 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 6641 | if (cxx_record_decl) { |
| 6642 | // We might have base classes to print out first |
| 6643 | clang::CXXRecordDecl::base_class_const_iterator base_class, |
| 6644 | base_class_end; |
| 6645 | for (base_class = cxx_record_decl->bases_begin(), |
| 6646 | base_class_end = cxx_record_decl->bases_end(); |
| 6647 | base_class != base_class_end; ++base_class) { |
| 6648 | const clang::CXXRecordDecl *base_class_decl = nullptr; |
| 6649 | |
| 6650 | // Skip empty base classes |
| 6651 | if (omit_empty_base_classes) { |
| 6652 | base_class_decl = llvm::cast<clang::CXXRecordDecl>( |
| 6653 | base_class->getType()->getAs<clang::RecordType>()->getDecl()); |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 6654 | if (!ClangASTContext::RecordHasFields(base_class_decl)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6655 | continue; |
| 6656 | } |
| 6657 | |
| 6658 | if (idx == child_idx) { |
| 6659 | if (base_class_decl == nullptr) |
| 6660 | base_class_decl = llvm::cast<clang::CXXRecordDecl>( |
| 6661 | base_class->getType()->getAs<clang::RecordType>()->getDecl()); |
| 6662 | |
| 6663 | if (base_class->isVirtual()) { |
| 6664 | bool handled = false; |
| 6665 | if (valobj) { |
Aleksandr Urakov | 1dc51db | 2018-11-12 16:23:50 +0000 | [diff] [blame] | 6666 | clang::VTableContextBase *vtable_ctx = |
| 6667 | getASTContext()->getVTableContext(); |
| 6668 | if (vtable_ctx) |
| 6669 | handled = GetVBaseBitOffset(*vtable_ctx, *valobj, |
| 6670 | record_layout, cxx_record_decl, |
| 6671 | base_class_decl, bit_offset); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6672 | } |
| 6673 | if (!handled) |
| 6674 | bit_offset = record_layout.getVBaseClassOffset(base_class_decl) |
| 6675 | .getQuantity() * |
| 6676 | 8; |
| 6677 | } else |
| 6678 | bit_offset = record_layout.getBaseClassOffset(base_class_decl) |
| 6679 | .getQuantity() * |
| 6680 | 8; |
| 6681 | |
| 6682 | // Base classes should be a multiple of 8 bits in size |
| 6683 | child_byte_offset = bit_offset / 8; |
| 6684 | CompilerType base_class_clang_type(getASTContext(), |
| 6685 | base_class->getType()); |
| 6686 | child_name = base_class_clang_type.GetTypeName().AsCString(""); |
Adrian Prantl | d963a7c | 2019-01-15 18:07:52 +0000 | [diff] [blame^] | 6687 | auto size = base_class_clang_type.GetBitSize( |
| 6688 | exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL); |
| 6689 | if (!size) |
| 6690 | return {}; |
| 6691 | uint64_t base_class_clang_type_bit_size = *size; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6692 | |
| 6693 | // Base classes bit sizes should be a multiple of 8 bits in size |
| 6694 | assert(base_class_clang_type_bit_size % 8 == 0); |
| 6695 | child_byte_size = base_class_clang_type_bit_size / 8; |
| 6696 | child_is_base_class = true; |
| 6697 | return base_class_clang_type; |
| 6698 | } |
| 6699 | // We don't increment the child index in the for loop since we might |
| 6700 | // be skipping empty base classes |
| 6701 | ++child_idx; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 6702 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6703 | } |
| 6704 | // Make sure index is in range... |
| 6705 | uint32_t field_idx = 0; |
| 6706 | clang::RecordDecl::field_iterator field, field_end; |
| 6707 | for (field = record_decl->field_begin(), |
| 6708 | field_end = record_decl->field_end(); |
| 6709 | field != field_end; ++field, ++field_idx, ++child_idx) { |
| 6710 | if (idx == child_idx) { |
| 6711 | // Print the member type if requested |
| 6712 | // Print the member name and equal sign |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 6713 | child_name.assign(field->getNameAsString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6714 | |
| 6715 | // Figure out the type byte size (field_type_info.first) and |
| 6716 | // alignment (field_type_info.second) from the AST context. |
| 6717 | CompilerType field_clang_type(getASTContext(), field->getType()); |
| 6718 | assert(field_idx < record_layout.getFieldCount()); |
Adrian Prantl | d963a7c | 2019-01-15 18:07:52 +0000 | [diff] [blame^] | 6719 | auto size = field_clang_type.GetByteSize( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6720 | exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL); |
Adrian Prantl | d963a7c | 2019-01-15 18:07:52 +0000 | [diff] [blame^] | 6721 | if (!size) |
| 6722 | return {}; |
| 6723 | child_byte_size = *size; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6724 | const uint32_t child_bit_size = child_byte_size * 8; |
| 6725 | |
| 6726 | // Figure out the field offset within the current struct/union/class |
| 6727 | // type |
| 6728 | bit_offset = record_layout.getFieldOffset(field_idx); |
| 6729 | if (ClangASTContext::FieldIsBitfield(getASTContext(), *field, |
| 6730 | child_bitfield_bit_size)) { |
| 6731 | child_bitfield_bit_offset = bit_offset % child_bit_size; |
| 6732 | const uint32_t child_bit_offset = |
| 6733 | bit_offset - child_bitfield_bit_offset; |
| 6734 | child_byte_offset = child_bit_offset / 8; |
| 6735 | } else { |
| 6736 | child_byte_offset = bit_offset / 8; |
| 6737 | } |
| 6738 | |
| 6739 | return field_clang_type; |
| 6740 | } |
| 6741 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 6742 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6743 | break; |
| 6744 | |
| 6745 | case clang::Type::ObjCObject: |
| 6746 | case clang::Type::ObjCInterface: |
| 6747 | if (idx_is_valid && GetCompleteType(type)) { |
| 6748 | const clang::ObjCObjectType *objc_class_type = |
| 6749 | llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr()); |
| 6750 | assert(objc_class_type); |
| 6751 | if (objc_class_type) { |
| 6752 | uint32_t child_idx = 0; |
| 6753 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 6754 | objc_class_type->getInterface(); |
| 6755 | |
| 6756 | if (class_interface_decl) { |
| 6757 | |
| 6758 | const clang::ASTRecordLayout &interface_layout = |
| 6759 | getASTContext()->getASTObjCInterfaceLayout(class_interface_decl); |
| 6760 | clang::ObjCInterfaceDecl *superclass_interface_decl = |
| 6761 | class_interface_decl->getSuperClass(); |
| 6762 | if (superclass_interface_decl) { |
| 6763 | if (omit_empty_base_classes) { |
| 6764 | CompilerType base_class_clang_type( |
| 6765 | getASTContext(), getASTContext()->getObjCInterfaceType( |
| 6766 | superclass_interface_decl)); |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 6767 | if (base_class_clang_type.GetNumChildren(omit_empty_base_classes, |
| 6768 | exe_ctx) > 0) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6769 | if (idx == 0) { |
| 6770 | clang::QualType ivar_qual_type( |
| 6771 | getASTContext()->getObjCInterfaceType( |
| 6772 | superclass_interface_decl)); |
| 6773 | |
| 6774 | child_name.assign( |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 6775 | superclass_interface_decl->getNameAsString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6776 | |
| 6777 | clang::TypeInfo ivar_type_info = |
| 6778 | getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr()); |
| 6779 | |
| 6780 | child_byte_size = ivar_type_info.Width / 8; |
| 6781 | child_byte_offset = 0; |
| 6782 | child_is_base_class = true; |
| 6783 | |
| 6784 | return CompilerType(getASTContext(), ivar_qual_type); |
| 6785 | } |
| 6786 | |
| 6787 | ++child_idx; |
| 6788 | } |
| 6789 | } else |
| 6790 | ++child_idx; |
| 6791 | } |
| 6792 | |
| 6793 | const uint32_t superclass_idx = child_idx; |
| 6794 | |
| 6795 | if (idx < (child_idx + class_interface_decl->ivar_size())) { |
| 6796 | clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, |
| 6797 | ivar_end = class_interface_decl->ivar_end(); |
| 6798 | |
| 6799 | for (ivar_pos = class_interface_decl->ivar_begin(); |
| 6800 | ivar_pos != ivar_end; ++ivar_pos) { |
| 6801 | if (child_idx == idx) { |
| 6802 | clang::ObjCIvarDecl *ivar_decl = *ivar_pos; |
| 6803 | |
| 6804 | clang::QualType ivar_qual_type(ivar_decl->getType()); |
| 6805 | |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 6806 | child_name.assign(ivar_decl->getNameAsString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6807 | |
| 6808 | clang::TypeInfo ivar_type_info = |
| 6809 | getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr()); |
| 6810 | |
| 6811 | child_byte_size = ivar_type_info.Width / 8; |
| 6812 | |
| 6813 | // Figure out the field offset within the current |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 6814 | // struct/union/class type For ObjC objects, we can't trust the |
| 6815 | // bit offset we get from the Clang AST, since that doesn't |
| 6816 | // account for the space taken up by unbacked properties, or |
| 6817 | // from the changing size of base classes that are newer than |
| 6818 | // this class. So if we have a process around that we can ask |
| 6819 | // about this object, do so. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6820 | child_byte_offset = LLDB_INVALID_IVAR_OFFSET; |
| 6821 | Process *process = nullptr; |
| 6822 | if (exe_ctx) |
| 6823 | process = exe_ctx->GetProcessPtr(); |
| 6824 | if (process) { |
| 6825 | ObjCLanguageRuntime *objc_runtime = |
| 6826 | process->GetObjCLanguageRuntime(); |
| 6827 | if (objc_runtime != nullptr) { |
| 6828 | CompilerType parent_ast_type(getASTContext(), |
| 6829 | parent_qual_type); |
| 6830 | child_byte_offset = objc_runtime->GetByteOffsetForIvar( |
| 6831 | parent_ast_type, ivar_decl->getNameAsString().c_str()); |
| 6832 | } |
| 6833 | } |
| 6834 | |
Aleksandr Urakov | ff70172 | 2018-08-20 05:59:27 +0000 | [diff] [blame] | 6835 | // Setting this to INT32_MAX to make sure we don't compute it |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6836 | // twice... |
Aleksandr Urakov | 5345948 | 2018-08-17 07:28:24 +0000 | [diff] [blame] | 6837 | bit_offset = INT32_MAX; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6838 | |
| 6839 | if (child_byte_offset == |
| 6840 | static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) { |
| 6841 | bit_offset = interface_layout.getFieldOffset(child_idx - |
| 6842 | superclass_idx); |
| 6843 | child_byte_offset = bit_offset / 8; |
| 6844 | } |
| 6845 | |
| 6846 | // Note, the ObjC Ivar Byte offset is just that, it doesn't |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 6847 | // account for the bit offset of a bitfield within its |
| 6848 | // containing object. So regardless of where we get the byte |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6849 | // offset from, we still need to get the bit offset for |
| 6850 | // bitfields from the layout. |
| 6851 | |
| 6852 | if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl, |
| 6853 | child_bitfield_bit_size)) { |
Aleksandr Urakov | 5345948 | 2018-08-17 07:28:24 +0000 | [diff] [blame] | 6854 | if (bit_offset == INT32_MAX) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6855 | bit_offset = interface_layout.getFieldOffset( |
| 6856 | child_idx - superclass_idx); |
| 6857 | |
| 6858 | child_bitfield_bit_offset = bit_offset % 8; |
| 6859 | } |
| 6860 | return CompilerType(getASTContext(), ivar_qual_type); |
| 6861 | } |
| 6862 | ++child_idx; |
| 6863 | } |
| 6864 | } |
| 6865 | } |
| 6866 | } |
| 6867 | } |
| 6868 | break; |
| 6869 | |
| 6870 | case clang::Type::ObjCObjectPointer: |
| 6871 | if (idx_is_valid) { |
| 6872 | CompilerType pointee_clang_type(GetPointeeType(type)); |
| 6873 | |
| 6874 | if (transparent_pointers && pointee_clang_type.IsAggregateType()) { |
| 6875 | child_is_deref_of_parent = false; |
| 6876 | bool tmp_child_is_deref_of_parent = false; |
| 6877 | return pointee_clang_type.GetChildCompilerTypeAtIndex( |
| 6878 | exe_ctx, idx, transparent_pointers, omit_empty_base_classes, |
| 6879 | ignore_array_bounds, child_name, child_byte_size, child_byte_offset, |
| 6880 | child_bitfield_bit_size, child_bitfield_bit_offset, |
| 6881 | child_is_base_class, tmp_child_is_deref_of_parent, valobj, |
| 6882 | language_flags); |
| 6883 | } else { |
| 6884 | child_is_deref_of_parent = true; |
| 6885 | const char *parent_name = |
| 6886 | valobj ? valobj->GetName().GetCString() : NULL; |
| 6887 | if (parent_name) { |
| 6888 | child_name.assign(1, '*'); |
| 6889 | child_name += parent_name; |
| 6890 | } |
| 6891 | |
| 6892 | // We have a pointer to an simple type |
| 6893 | if (idx == 0 && pointee_clang_type.GetCompleteType()) { |
Adrian Prantl | d963a7c | 2019-01-15 18:07:52 +0000 | [diff] [blame^] | 6894 | if (auto size = pointee_clang_type.GetByteSize( |
| 6895 | exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL)) { |
| 6896 | child_byte_size = *size; |
| 6897 | child_byte_offset = 0; |
| 6898 | return pointee_clang_type; |
| 6899 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6900 | } |
| 6901 | } |
| 6902 | } |
| 6903 | break; |
| 6904 | |
| 6905 | case clang::Type::Vector: |
| 6906 | case clang::Type::ExtVector: |
| 6907 | if (idx_is_valid) { |
| 6908 | const clang::VectorType *array = |
| 6909 | llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr()); |
| 6910 | if (array) { |
| 6911 | CompilerType element_type(getASTContext(), array->getElementType()); |
| 6912 | if (element_type.GetCompleteType()) { |
| 6913 | char element_name[64]; |
| 6914 | ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]", |
| 6915 | static_cast<uint64_t>(idx)); |
| 6916 | child_name.assign(element_name); |
Adrian Prantl | d963a7c | 2019-01-15 18:07:52 +0000 | [diff] [blame^] | 6917 | if (auto size = element_type.GetByteSize( |
| 6918 | exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL)) { |
| 6919 | child_byte_size = *size; |
| 6920 | child_byte_offset = (int32_t)idx * (int32_t)child_byte_size; |
| 6921 | return element_type; |
| 6922 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6923 | } |
| 6924 | } |
| 6925 | } |
| 6926 | break; |
| 6927 | |
| 6928 | case clang::Type::ConstantArray: |
| 6929 | case clang::Type::IncompleteArray: |
| 6930 | if (ignore_array_bounds || idx_is_valid) { |
| 6931 | const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe(); |
| 6932 | if (array) { |
| 6933 | CompilerType element_type(getASTContext(), array->getElementType()); |
| 6934 | if (element_type.GetCompleteType()) { |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 6935 | child_name = llvm::formatv("[{0}]", idx); |
Adrian Prantl | d963a7c | 2019-01-15 18:07:52 +0000 | [diff] [blame^] | 6936 | if (auto size = element_type.GetByteSize( |
| 6937 | exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL)) { |
| 6938 | child_byte_size = *size; |
| 6939 | child_byte_offset = (int32_t)idx * (int32_t)child_byte_size; |
| 6940 | return element_type; |
| 6941 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6942 | } |
| 6943 | } |
| 6944 | } |
| 6945 | break; |
| 6946 | |
Tamas Berghammer | 1c62e03 | 2017-01-07 16:39:07 +0000 | [diff] [blame] | 6947 | case clang::Type::Pointer: { |
| 6948 | CompilerType pointee_clang_type(GetPointeeType(type)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6949 | |
Tamas Berghammer | 1c62e03 | 2017-01-07 16:39:07 +0000 | [diff] [blame] | 6950 | // Don't dereference "void *" pointers |
| 6951 | if (pointee_clang_type.IsVoidType()) |
| 6952 | return CompilerType(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6953 | |
Tamas Berghammer | 1c62e03 | 2017-01-07 16:39:07 +0000 | [diff] [blame] | 6954 | if (transparent_pointers && pointee_clang_type.IsAggregateType()) { |
| 6955 | child_is_deref_of_parent = false; |
| 6956 | bool tmp_child_is_deref_of_parent = false; |
| 6957 | return pointee_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, |
| 6961 | child_is_base_class, tmp_child_is_deref_of_parent, valobj, |
| 6962 | language_flags); |
| 6963 | } else { |
| 6964 | child_is_deref_of_parent = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6965 | |
Tamas Berghammer | 1c62e03 | 2017-01-07 16:39:07 +0000 | [diff] [blame] | 6966 | const char *parent_name = |
| 6967 | valobj ? valobj->GetName().GetCString() : NULL; |
| 6968 | if (parent_name) { |
| 6969 | child_name.assign(1, '*'); |
| 6970 | child_name += parent_name; |
| 6971 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6972 | |
Tamas Berghammer | 1c62e03 | 2017-01-07 16:39:07 +0000 | [diff] [blame] | 6973 | // We have a pointer to an simple type |
| 6974 | if (idx == 0) { |
Adrian Prantl | d963a7c | 2019-01-15 18:07:52 +0000 | [diff] [blame^] | 6975 | if (auto size = pointee_clang_type.GetByteSize( |
| 6976 | exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL)) { |
| 6977 | child_byte_size = *size; |
| 6978 | child_byte_offset = 0; |
| 6979 | return pointee_clang_type; |
| 6980 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6981 | } |
| 6982 | } |
| 6983 | break; |
Tamas Berghammer | 1c62e03 | 2017-01-07 16:39:07 +0000 | [diff] [blame] | 6984 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6985 | |
| 6986 | case clang::Type::LValueReference: |
| 6987 | case clang::Type::RValueReference: |
| 6988 | if (idx_is_valid) { |
| 6989 | const clang::ReferenceType *reference_type = |
| 6990 | llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr()); |
| 6991 | CompilerType pointee_clang_type(getASTContext(), |
| 6992 | reference_type->getPointeeType()); |
| 6993 | if (transparent_pointers && pointee_clang_type.IsAggregateType()) { |
| 6994 | child_is_deref_of_parent = false; |
| 6995 | bool tmp_child_is_deref_of_parent = false; |
| 6996 | return pointee_clang_type.GetChildCompilerTypeAtIndex( |
| 6997 | exe_ctx, idx, transparent_pointers, omit_empty_base_classes, |
| 6998 | ignore_array_bounds, child_name, child_byte_size, child_byte_offset, |
| 6999 | child_bitfield_bit_size, child_bitfield_bit_offset, |
| 7000 | child_is_base_class, tmp_child_is_deref_of_parent, valobj, |
| 7001 | language_flags); |
| 7002 | } else { |
| 7003 | const char *parent_name = |
| 7004 | valobj ? valobj->GetName().GetCString() : NULL; |
| 7005 | if (parent_name) { |
| 7006 | child_name.assign(1, '&'); |
| 7007 | child_name += parent_name; |
| 7008 | } |
| 7009 | |
| 7010 | // We have a pointer to an simple type |
| 7011 | if (idx == 0) { |
Adrian Prantl | d963a7c | 2019-01-15 18:07:52 +0000 | [diff] [blame^] | 7012 | if (auto size = pointee_clang_type.GetByteSize( |
| 7013 | exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL)) { |
| 7014 | child_byte_size = *size; |
| 7015 | child_byte_offset = 0; |
| 7016 | return pointee_clang_type; |
| 7017 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7018 | } |
| 7019 | } |
| 7020 | } |
| 7021 | break; |
| 7022 | |
| 7023 | case clang::Type::Typedef: { |
| 7024 | CompilerType typedefed_clang_type( |
| 7025 | getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type) |
| 7026 | ->getDecl() |
| 7027 | ->getUnderlyingType()); |
| 7028 | return typedefed_clang_type.GetChildCompilerTypeAtIndex( |
| 7029 | exe_ctx, idx, transparent_pointers, omit_empty_base_classes, |
| 7030 | ignore_array_bounds, child_name, child_byte_size, child_byte_offset, |
| 7031 | child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class, |
| 7032 | child_is_deref_of_parent, valobj, language_flags); |
| 7033 | } break; |
| 7034 | |
| 7035 | case clang::Type::Auto: { |
| 7036 | CompilerType elaborated_clang_type( |
| 7037 | getASTContext(), |
| 7038 | llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType()); |
| 7039 | return elaborated_clang_type.GetChildCompilerTypeAtIndex( |
| 7040 | exe_ctx, idx, transparent_pointers, omit_empty_base_classes, |
| 7041 | ignore_array_bounds, child_name, child_byte_size, child_byte_offset, |
| 7042 | child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class, |
| 7043 | child_is_deref_of_parent, valobj, language_flags); |
| 7044 | } |
| 7045 | |
| 7046 | case clang::Type::Elaborated: { |
| 7047 | CompilerType elaborated_clang_type( |
| 7048 | getASTContext(), |
| 7049 | llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType()); |
| 7050 | return elaborated_clang_type.GetChildCompilerTypeAtIndex( |
| 7051 | exe_ctx, idx, transparent_pointers, omit_empty_base_classes, |
| 7052 | ignore_array_bounds, child_name, child_byte_size, child_byte_offset, |
| 7053 | child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class, |
| 7054 | child_is_deref_of_parent, valobj, language_flags); |
| 7055 | } |
| 7056 | |
| 7057 | case clang::Type::Paren: { |
| 7058 | CompilerType paren_clang_type( |
| 7059 | getASTContext(), |
| 7060 | llvm::cast<clang::ParenType>(parent_qual_type)->desugar()); |
| 7061 | return paren_clang_type.GetChildCompilerTypeAtIndex( |
| 7062 | exe_ctx, idx, transparent_pointers, omit_empty_base_classes, |
| 7063 | ignore_array_bounds, child_name, child_byte_size, child_byte_offset, |
| 7064 | child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class, |
| 7065 | child_is_deref_of_parent, valobj, language_flags); |
| 7066 | } |
| 7067 | |
| 7068 | default: |
| 7069 | break; |
| 7070 | } |
| 7071 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7072 | } |
| 7073 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7074 | static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl, |
| 7075 | const clang::CXXBaseSpecifier *base_spec, |
| 7076 | bool omit_empty_base_classes) { |
| 7077 | uint32_t child_idx = 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7078 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7079 | const clang::CXXRecordDecl *cxx_record_decl = |
| 7080 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 7081 | |
| 7082 | // const char *super_name = record_decl->getNameAsCString(); |
| 7083 | // const char *base_name = |
| 7084 | // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString(); |
| 7085 | // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name); |
| 7086 | // |
| 7087 | if (cxx_record_decl) { |
| 7088 | clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end; |
| 7089 | for (base_class = cxx_record_decl->bases_begin(), |
| 7090 | base_class_end = cxx_record_decl->bases_end(); |
| 7091 | base_class != base_class_end; ++base_class) { |
| 7092 | if (omit_empty_base_classes) { |
| 7093 | if (BaseSpecifierIsEmpty(base_class)) |
| 7094 | continue; |
| 7095 | } |
| 7096 | |
| 7097 | // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", |
| 7098 | // super_name, base_name, |
| 7099 | // child_idx, |
| 7100 | // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString()); |
| 7101 | // |
| 7102 | // |
| 7103 | if (base_class == base_spec) |
| 7104 | return child_idx; |
| 7105 | ++child_idx; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7106 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7107 | } |
| 7108 | |
| 7109 | return UINT32_MAX; |
| 7110 | } |
| 7111 | |
| 7112 | static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl, |
| 7113 | clang::NamedDecl *canonical_decl, |
| 7114 | bool omit_empty_base_classes) { |
| 7115 | uint32_t child_idx = ClangASTContext::GetNumBaseClasses( |
| 7116 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl), |
| 7117 | omit_empty_base_classes); |
| 7118 | |
| 7119 | clang::RecordDecl::field_iterator field, field_end; |
| 7120 | for (field = record_decl->field_begin(), field_end = record_decl->field_end(); |
| 7121 | field != field_end; ++field, ++child_idx) { |
| 7122 | if (field->getCanonicalDecl() == canonical_decl) |
| 7123 | return child_idx; |
| 7124 | } |
| 7125 | |
| 7126 | return UINT32_MAX; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7127 | } |
| 7128 | |
| 7129 | // 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] | 7130 | // their members) in the type hierarchy. Returns an index path into |
| 7131 | // "clang_type" on how to reach the appropriate member. |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7132 | // |
| 7133 | // class A |
| 7134 | // { |
| 7135 | // public: |
| 7136 | // int m_a; |
| 7137 | // int m_b; |
| 7138 | // }; |
| 7139 | // |
| 7140 | // class B |
| 7141 | // { |
| 7142 | // }; |
| 7143 | // |
| 7144 | // class C : |
| 7145 | // public B, |
| 7146 | // public A |
| 7147 | // { |
| 7148 | // }; |
| 7149 | // |
| 7150 | // If we have a clang type that describes "class C", and we wanted to looked |
| 7151 | // "m_b" in it: |
| 7152 | // |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7153 | // 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] | 7154 | // with: { 1, 1 } The first index 1 is the child index for "class A" within |
| 7155 | // 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] | 7156 | // |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 7157 | // With omit_empty_base_classes == true we would get an integer array back |
| 7158 | // with: { 0, 1 } The first index 0 is the child index for "class A" within |
| 7159 | // class C (since class B doesn't have any members it doesn't count) The second |
| 7160 | // index 1 is the child index for "m_b" within class A |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7161 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7162 | size_t ClangASTContext::GetIndexOfChildMemberWithName( |
| 7163 | lldb::opaque_compiler_type_t type, const char *name, |
| 7164 | bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) { |
| 7165 | if (type && name && name[0]) { |
| 7166 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 7167 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 7168 | switch (type_class) { |
| 7169 | case clang::Type::Record: |
| 7170 | if (GetCompleteType(type)) { |
| 7171 | const clang::RecordType *record_type = |
| 7172 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 7173 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
Enrico Granata | 36f51e4 | 2015-12-18 22:41:25 +0000 | [diff] [blame] | 7174 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7175 | assert(record_decl); |
| 7176 | uint32_t child_idx = 0; |
| 7177 | |
| 7178 | const clang::CXXRecordDecl *cxx_record_decl = |
| 7179 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 7180 | |
| 7181 | // Try and find a field that matches NAME |
| 7182 | clang::RecordDecl::field_iterator field, field_end; |
| 7183 | llvm::StringRef name_sref(name); |
| 7184 | for (field = record_decl->field_begin(), |
| 7185 | field_end = record_decl->field_end(); |
| 7186 | field != field_end; ++field, ++child_idx) { |
| 7187 | llvm::StringRef field_name = field->getName(); |
| 7188 | if (field_name.empty()) { |
| 7189 | CompilerType field_type(getASTContext(), field->getType()); |
| 7190 | child_indexes.push_back(child_idx); |
| 7191 | if (field_type.GetIndexOfChildMemberWithName( |
| 7192 | name, omit_empty_base_classes, child_indexes)) |
| 7193 | return child_indexes.size(); |
| 7194 | child_indexes.pop_back(); |
| 7195 | |
| 7196 | } else if (field_name.equals(name_sref)) { |
| 7197 | // We have to add on the number of base classes to this index! |
| 7198 | child_indexes.push_back( |
| 7199 | child_idx + ClangASTContext::GetNumBaseClasses( |
| 7200 | cxx_record_decl, omit_empty_base_classes)); |
| 7201 | return child_indexes.size(); |
| 7202 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7203 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7204 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7205 | if (cxx_record_decl) { |
| 7206 | const clang::RecordDecl *parent_record_decl = cxx_record_decl; |
| 7207 | |
| 7208 | // printf ("parent = %s\n", parent_record_decl->getNameAsCString()); |
| 7209 | |
| 7210 | // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl(); |
| 7211 | // Didn't find things easily, lets let clang do its thang... |
| 7212 | clang::IdentifierInfo &ident_ref = |
| 7213 | getASTContext()->Idents.get(name_sref); |
| 7214 | clang::DeclarationName decl_name(&ident_ref); |
| 7215 | |
| 7216 | clang::CXXBasePaths paths; |
| 7217 | if (cxx_record_decl->lookupInBases( |
| 7218 | [decl_name](const clang::CXXBaseSpecifier *specifier, |
| 7219 | clang::CXXBasePath &path) { |
| 7220 | return clang::CXXRecordDecl::FindOrdinaryMember( |
| 7221 | specifier, path, decl_name); |
| 7222 | }, |
| 7223 | paths)) { |
| 7224 | clang::CXXBasePaths::const_paths_iterator path, |
| 7225 | path_end = paths.end(); |
| 7226 | for (path = paths.begin(); path != path_end; ++path) { |
| 7227 | const size_t num_path_elements = path->size(); |
| 7228 | for (size_t e = 0; e < num_path_elements; ++e) { |
| 7229 | clang::CXXBasePathElement elem = (*path)[e]; |
| 7230 | |
| 7231 | child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base, |
| 7232 | omit_empty_base_classes); |
| 7233 | if (child_idx == UINT32_MAX) { |
| 7234 | child_indexes.clear(); |
| 7235 | return 0; |
| 7236 | } else { |
| 7237 | child_indexes.push_back(child_idx); |
| 7238 | parent_record_decl = llvm::cast<clang::RecordDecl>( |
| 7239 | elem.Base->getType() |
| 7240 | ->getAs<clang::RecordType>() |
| 7241 | ->getDecl()); |
| 7242 | } |
| 7243 | } |
| 7244 | for (clang::NamedDecl *path_decl : path->Decls) { |
| 7245 | child_idx = GetIndexForRecordChild( |
| 7246 | parent_record_decl, path_decl, omit_empty_base_classes); |
| 7247 | if (child_idx == UINT32_MAX) { |
| 7248 | child_indexes.clear(); |
| 7249 | return 0; |
| 7250 | } else { |
| 7251 | child_indexes.push_back(child_idx); |
| 7252 | } |
| 7253 | } |
| 7254 | } |
| 7255 | return child_indexes.size(); |
| 7256 | } |
| 7257 | } |
| 7258 | } |
| 7259 | break; |
| 7260 | |
| 7261 | case clang::Type::ObjCObject: |
| 7262 | case clang::Type::ObjCInterface: |
| 7263 | if (GetCompleteType(type)) { |
| 7264 | llvm::StringRef name_sref(name); |
| 7265 | const clang::ObjCObjectType *objc_class_type = |
| 7266 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 7267 | assert(objc_class_type); |
| 7268 | if (objc_class_type) { |
| 7269 | uint32_t child_idx = 0; |
| 7270 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 7271 | objc_class_type->getInterface(); |
| 7272 | |
| 7273 | if (class_interface_decl) { |
| 7274 | clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, |
| 7275 | ivar_end = class_interface_decl->ivar_end(); |
| 7276 | clang::ObjCInterfaceDecl *superclass_interface_decl = |
| 7277 | class_interface_decl->getSuperClass(); |
| 7278 | |
| 7279 | for (ivar_pos = class_interface_decl->ivar_begin(); |
| 7280 | ivar_pos != ivar_end; ++ivar_pos, ++child_idx) { |
| 7281 | const clang::ObjCIvarDecl *ivar_decl = *ivar_pos; |
| 7282 | |
| 7283 | if (ivar_decl->getName().equals(name_sref)) { |
| 7284 | if ((!omit_empty_base_classes && superclass_interface_decl) || |
| 7285 | (omit_empty_base_classes && |
| 7286 | ObjCDeclHasIVars(superclass_interface_decl, true))) |
| 7287 | ++child_idx; |
| 7288 | |
| 7289 | child_indexes.push_back(child_idx); |
| 7290 | return child_indexes.size(); |
| 7291 | } |
| 7292 | } |
| 7293 | |
| 7294 | if (superclass_interface_decl) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 7295 | // The super class index is always zero for ObjC classes, so we |
| 7296 | // push it onto the child indexes in case we find an ivar in our |
| 7297 | // superclass... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7298 | child_indexes.push_back(0); |
| 7299 | |
| 7300 | CompilerType superclass_clang_type( |
| 7301 | getASTContext(), getASTContext()->getObjCInterfaceType( |
| 7302 | superclass_interface_decl)); |
| 7303 | if (superclass_clang_type.GetIndexOfChildMemberWithName( |
| 7304 | name, omit_empty_base_classes, child_indexes)) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 7305 | // We did find an ivar in a superclass so just return the |
| 7306 | // results! |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7307 | return child_indexes.size(); |
| 7308 | } |
| 7309 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 7310 | // We didn't find an ivar matching "name" in our superclass, pop |
| 7311 | // the superclass zero index that we pushed on above. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7312 | child_indexes.pop_back(); |
| 7313 | } |
| 7314 | } |
| 7315 | } |
| 7316 | } |
| 7317 | break; |
| 7318 | |
| 7319 | case clang::Type::ObjCObjectPointer: { |
| 7320 | CompilerType objc_object_clang_type( |
| 7321 | getASTContext(), |
| 7322 | llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr()) |
| 7323 | ->getPointeeType()); |
| 7324 | return objc_object_clang_type.GetIndexOfChildMemberWithName( |
| 7325 | name, omit_empty_base_classes, child_indexes); |
| 7326 | } break; |
| 7327 | |
| 7328 | case clang::Type::ConstantArray: { |
| 7329 | // const clang::ConstantArrayType *array = |
| 7330 | // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr()); |
| 7331 | // const uint64_t element_count = |
| 7332 | // array->getSize().getLimitedValue(); |
| 7333 | // |
| 7334 | // if (idx < element_count) |
| 7335 | // { |
| 7336 | // std::pair<uint64_t, unsigned> field_type_info = |
| 7337 | // ast->getTypeInfo(array->getElementType()); |
| 7338 | // |
| 7339 | // char element_name[32]; |
| 7340 | // ::snprintf (element_name, sizeof (element_name), |
| 7341 | // "%s[%u]", parent_name ? parent_name : "", idx); |
| 7342 | // |
| 7343 | // child_name.assign(element_name); |
| 7344 | // assert(field_type_info.first % 8 == 0); |
| 7345 | // child_byte_size = field_type_info.first / 8; |
| 7346 | // child_byte_offset = idx * child_byte_size; |
| 7347 | // return array->getElementType().getAsOpaquePtr(); |
| 7348 | // } |
| 7349 | } break; |
| 7350 | |
| 7351 | // case clang::Type::MemberPointerType: |
| 7352 | // { |
| 7353 | // MemberPointerType *mem_ptr_type = |
| 7354 | // llvm::cast<MemberPointerType>(qual_type.getTypePtr()); |
| 7355 | // clang::QualType pointee_type = |
| 7356 | // mem_ptr_type->getPointeeType(); |
| 7357 | // |
| 7358 | // if (ClangASTContext::IsAggregateType |
| 7359 | // (pointee_type.getAsOpaquePtr())) |
| 7360 | // { |
| 7361 | // return GetIndexOfChildWithName (ast, |
| 7362 | // mem_ptr_type->getPointeeType().getAsOpaquePtr(), |
| 7363 | // name); |
| 7364 | // } |
| 7365 | // } |
| 7366 | // break; |
| 7367 | // |
| 7368 | case clang::Type::LValueReference: |
| 7369 | case clang::Type::RValueReference: { |
| 7370 | const clang::ReferenceType *reference_type = |
| 7371 | llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()); |
| 7372 | clang::QualType pointee_type(reference_type->getPointeeType()); |
| 7373 | CompilerType pointee_clang_type(getASTContext(), pointee_type); |
| 7374 | |
| 7375 | if (pointee_clang_type.IsAggregateType()) { |
| 7376 | return pointee_clang_type.GetIndexOfChildMemberWithName( |
| 7377 | name, omit_empty_base_classes, child_indexes); |
| 7378 | } |
| 7379 | } break; |
| 7380 | |
| 7381 | case clang::Type::Pointer: { |
| 7382 | CompilerType pointee_clang_type(GetPointeeType(type)); |
| 7383 | |
| 7384 | if (pointee_clang_type.IsAggregateType()) { |
| 7385 | return pointee_clang_type.GetIndexOfChildMemberWithName( |
| 7386 | name, omit_empty_base_classes, child_indexes); |
| 7387 | } |
| 7388 | } break; |
| 7389 | |
| 7390 | case clang::Type::Typedef: |
| 7391 | return CompilerType(getASTContext(), |
| 7392 | llvm::cast<clang::TypedefType>(qual_type) |
| 7393 | ->getDecl() |
| 7394 | ->getUnderlyingType()) |
| 7395 | .GetIndexOfChildMemberWithName(name, omit_empty_base_classes, |
| 7396 | child_indexes); |
| 7397 | |
| 7398 | case clang::Type::Auto: |
| 7399 | return CompilerType( |
| 7400 | getASTContext(), |
| 7401 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) |
| 7402 | .GetIndexOfChildMemberWithName(name, omit_empty_base_classes, |
| 7403 | child_indexes); |
| 7404 | |
| 7405 | case clang::Type::Elaborated: |
| 7406 | return CompilerType( |
| 7407 | getASTContext(), |
| 7408 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) |
| 7409 | .GetIndexOfChildMemberWithName(name, omit_empty_base_classes, |
| 7410 | child_indexes); |
| 7411 | |
| 7412 | case clang::Type::Paren: |
| 7413 | return CompilerType(getASTContext(), |
| 7414 | llvm::cast<clang::ParenType>(qual_type)->desugar()) |
| 7415 | .GetIndexOfChildMemberWithName(name, omit_empty_base_classes, |
| 7416 | child_indexes); |
| 7417 | |
| 7418 | default: |
| 7419 | break; |
| 7420 | } |
| 7421 | } |
| 7422 | return 0; |
| 7423 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7424 | |
| 7425 | // Get the index of the child of "clang_type" whose name matches. This function |
| 7426 | // doesn't descend into the children, but only looks one level deep and name |
| 7427 | // matches can include base class names. |
| 7428 | |
| 7429 | uint32_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7430 | ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type, |
| 7431 | const char *name, |
| 7432 | bool omit_empty_base_classes) { |
| 7433 | if (type && name && name[0]) { |
| 7434 | clang::QualType qual_type(GetCanonicalQualType(type)); |
Enrico Granata | 36f51e4 | 2015-12-18 22:41:25 +0000 | [diff] [blame] | 7435 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7436 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 7437 | |
| 7438 | switch (type_class) { |
| 7439 | case clang::Type::Record: |
| 7440 | if (GetCompleteType(type)) { |
| 7441 | const clang::RecordType *record_type = |
| 7442 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 7443 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 7444 | |
| 7445 | assert(record_decl); |
| 7446 | uint32_t child_idx = 0; |
| 7447 | |
| 7448 | const clang::CXXRecordDecl *cxx_record_decl = |
| 7449 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 7450 | |
| 7451 | if (cxx_record_decl) { |
| 7452 | clang::CXXRecordDecl::base_class_const_iterator base_class, |
| 7453 | base_class_end; |
| 7454 | for (base_class = cxx_record_decl->bases_begin(), |
| 7455 | base_class_end = cxx_record_decl->bases_end(); |
| 7456 | base_class != base_class_end; ++base_class) { |
| 7457 | // Skip empty base classes |
| 7458 | clang::CXXRecordDecl *base_class_decl = |
| 7459 | llvm::cast<clang::CXXRecordDecl>( |
| 7460 | base_class->getType() |
| 7461 | ->getAs<clang::RecordType>() |
| 7462 | ->getDecl()); |
| 7463 | if (omit_empty_base_classes && |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 7464 | !ClangASTContext::RecordHasFields(base_class_decl)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7465 | continue; |
| 7466 | |
| 7467 | CompilerType base_class_clang_type(getASTContext(), |
| 7468 | base_class->getType()); |
| 7469 | std::string base_class_type_name( |
| 7470 | base_class_clang_type.GetTypeName().AsCString("")); |
Jonas Devlieghere | 8d20cfd | 2018-12-21 22:46:10 +0000 | [diff] [blame] | 7471 | if (base_class_type_name == name) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7472 | return child_idx; |
| 7473 | ++child_idx; |
| 7474 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7475 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7476 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7477 | // Try and find a field that matches NAME |
| 7478 | clang::RecordDecl::field_iterator field, field_end; |
| 7479 | llvm::StringRef name_sref(name); |
| 7480 | for (field = record_decl->field_begin(), |
| 7481 | field_end = record_decl->field_end(); |
| 7482 | field != field_end; ++field, ++child_idx) { |
| 7483 | if (field->getName().equals(name_sref)) |
| 7484 | return child_idx; |
| 7485 | } |
| 7486 | } |
| 7487 | break; |
| 7488 | |
| 7489 | case clang::Type::ObjCObject: |
| 7490 | case clang::Type::ObjCInterface: |
| 7491 | if (GetCompleteType(type)) { |
| 7492 | llvm::StringRef name_sref(name); |
| 7493 | const clang::ObjCObjectType *objc_class_type = |
| 7494 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 7495 | assert(objc_class_type); |
| 7496 | if (objc_class_type) { |
| 7497 | uint32_t child_idx = 0; |
| 7498 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 7499 | objc_class_type->getInterface(); |
| 7500 | |
| 7501 | if (class_interface_decl) { |
| 7502 | clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, |
| 7503 | ivar_end = class_interface_decl->ivar_end(); |
| 7504 | clang::ObjCInterfaceDecl *superclass_interface_decl = |
| 7505 | class_interface_decl->getSuperClass(); |
| 7506 | |
| 7507 | for (ivar_pos = class_interface_decl->ivar_begin(); |
| 7508 | ivar_pos != ivar_end; ++ivar_pos, ++child_idx) { |
| 7509 | const clang::ObjCIvarDecl *ivar_decl = *ivar_pos; |
| 7510 | |
| 7511 | if (ivar_decl->getName().equals(name_sref)) { |
| 7512 | if ((!omit_empty_base_classes && superclass_interface_decl) || |
| 7513 | (omit_empty_base_classes && |
| 7514 | ObjCDeclHasIVars(superclass_interface_decl, true))) |
| 7515 | ++child_idx; |
| 7516 | |
| 7517 | return child_idx; |
| 7518 | } |
| 7519 | } |
| 7520 | |
| 7521 | if (superclass_interface_decl) { |
| 7522 | if (superclass_interface_decl->getName().equals(name_sref)) |
| 7523 | return 0; |
| 7524 | } |
| 7525 | } |
| 7526 | } |
| 7527 | } |
| 7528 | break; |
| 7529 | |
| 7530 | case clang::Type::ObjCObjectPointer: { |
| 7531 | CompilerType pointee_clang_type( |
| 7532 | getASTContext(), |
| 7533 | llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr()) |
| 7534 | ->getPointeeType()); |
| 7535 | return pointee_clang_type.GetIndexOfChildWithName( |
| 7536 | name, omit_empty_base_classes); |
| 7537 | } break; |
| 7538 | |
| 7539 | case clang::Type::ConstantArray: { |
| 7540 | // const clang::ConstantArrayType *array = |
| 7541 | // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr()); |
| 7542 | // const uint64_t element_count = |
| 7543 | // array->getSize().getLimitedValue(); |
| 7544 | // |
| 7545 | // if (idx < element_count) |
| 7546 | // { |
| 7547 | // std::pair<uint64_t, unsigned> field_type_info = |
| 7548 | // ast->getTypeInfo(array->getElementType()); |
| 7549 | // |
| 7550 | // char element_name[32]; |
| 7551 | // ::snprintf (element_name, sizeof (element_name), |
| 7552 | // "%s[%u]", parent_name ? parent_name : "", idx); |
| 7553 | // |
| 7554 | // child_name.assign(element_name); |
| 7555 | // assert(field_type_info.first % 8 == 0); |
| 7556 | // child_byte_size = field_type_info.first / 8; |
| 7557 | // child_byte_offset = idx * child_byte_size; |
| 7558 | // return array->getElementType().getAsOpaquePtr(); |
| 7559 | // } |
| 7560 | } break; |
| 7561 | |
| 7562 | // case clang::Type::MemberPointerType: |
| 7563 | // { |
| 7564 | // MemberPointerType *mem_ptr_type = |
| 7565 | // llvm::cast<MemberPointerType>(qual_type.getTypePtr()); |
| 7566 | // clang::QualType pointee_type = |
| 7567 | // mem_ptr_type->getPointeeType(); |
| 7568 | // |
| 7569 | // if (ClangASTContext::IsAggregateType |
| 7570 | // (pointee_type.getAsOpaquePtr())) |
| 7571 | // { |
| 7572 | // return GetIndexOfChildWithName (ast, |
| 7573 | // mem_ptr_type->getPointeeType().getAsOpaquePtr(), |
| 7574 | // name); |
| 7575 | // } |
| 7576 | // } |
| 7577 | // break; |
| 7578 | // |
| 7579 | case clang::Type::LValueReference: |
| 7580 | case clang::Type::RValueReference: { |
| 7581 | const clang::ReferenceType *reference_type = |
| 7582 | llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()); |
| 7583 | CompilerType pointee_type(getASTContext(), |
| 7584 | reference_type->getPointeeType()); |
| 7585 | |
| 7586 | if (pointee_type.IsAggregateType()) { |
| 7587 | return pointee_type.GetIndexOfChildWithName(name, |
| 7588 | omit_empty_base_classes); |
| 7589 | } |
| 7590 | } break; |
| 7591 | |
| 7592 | case clang::Type::Pointer: { |
| 7593 | const clang::PointerType *pointer_type = |
| 7594 | llvm::cast<clang::PointerType>(qual_type.getTypePtr()); |
| 7595 | CompilerType pointee_type(getASTContext(), |
| 7596 | pointer_type->getPointeeType()); |
| 7597 | |
| 7598 | if (pointee_type.IsAggregateType()) { |
| 7599 | return pointee_type.GetIndexOfChildWithName(name, |
| 7600 | omit_empty_base_classes); |
| 7601 | } else { |
| 7602 | // if (parent_name) |
| 7603 | // { |
| 7604 | // child_name.assign(1, '*'); |
| 7605 | // child_name += parent_name; |
| 7606 | // } |
| 7607 | // |
| 7608 | // // We have a pointer to an simple type |
| 7609 | // if (idx == 0) |
| 7610 | // { |
| 7611 | // std::pair<uint64_t, unsigned> clang_type_info |
| 7612 | // = ast->getTypeInfo(pointee_type); |
| 7613 | // assert(clang_type_info.first % 8 == 0); |
| 7614 | // child_byte_size = clang_type_info.first / 8; |
| 7615 | // child_byte_offset = 0; |
| 7616 | // return pointee_type.getAsOpaquePtr(); |
| 7617 | // } |
| 7618 | } |
| 7619 | } break; |
| 7620 | |
| 7621 | case clang::Type::Auto: |
| 7622 | return CompilerType( |
| 7623 | getASTContext(), |
| 7624 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) |
| 7625 | .GetIndexOfChildWithName(name, omit_empty_base_classes); |
| 7626 | |
| 7627 | case clang::Type::Elaborated: |
| 7628 | return CompilerType( |
| 7629 | getASTContext(), |
| 7630 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) |
| 7631 | .GetIndexOfChildWithName(name, omit_empty_base_classes); |
| 7632 | |
| 7633 | case clang::Type::Paren: |
| 7634 | return CompilerType(getASTContext(), |
| 7635 | llvm::cast<clang::ParenType>(qual_type)->desugar()) |
| 7636 | .GetIndexOfChildWithName(name, omit_empty_base_classes); |
| 7637 | |
| 7638 | case clang::Type::Typedef: |
| 7639 | return CompilerType(getASTContext(), |
| 7640 | llvm::cast<clang::TypedefType>(qual_type) |
| 7641 | ->getDecl() |
| 7642 | ->getUnderlyingType()) |
| 7643 | .GetIndexOfChildWithName(name, omit_empty_base_classes); |
| 7644 | |
| 7645 | default: |
| 7646 | break; |
| 7647 | } |
| 7648 | } |
| 7649 | return UINT32_MAX; |
| 7650 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7651 | |
| 7652 | size_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7653 | ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) { |
| 7654 | if (!type) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7655 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7656 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7657 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 7658 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 7659 | switch (type_class) { |
| 7660 | case clang::Type::Record: |
| 7661 | if (GetCompleteType(type)) { |
| 7662 | const clang::CXXRecordDecl *cxx_record_decl = |
| 7663 | qual_type->getAsCXXRecordDecl(); |
| 7664 | if (cxx_record_decl) { |
| 7665 | const clang::ClassTemplateSpecializationDecl *template_decl = |
| 7666 | llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>( |
| 7667 | cxx_record_decl); |
| 7668 | if (template_decl) |
| 7669 | return template_decl->getTemplateArgs().size(); |
| 7670 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7671 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7672 | break; |
| 7673 | |
| 7674 | case clang::Type::Typedef: |
| 7675 | return (CompilerType(getASTContext(), |
| 7676 | llvm::cast<clang::TypedefType>(qual_type) |
| 7677 | ->getDecl() |
| 7678 | ->getUnderlyingType())) |
| 7679 | .GetNumTemplateArguments(); |
| 7680 | |
| 7681 | case clang::Type::Auto: |
| 7682 | return (CompilerType( |
| 7683 | getASTContext(), |
| 7684 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType())) |
| 7685 | .GetNumTemplateArguments(); |
| 7686 | |
| 7687 | case clang::Type::Elaborated: |
| 7688 | return (CompilerType( |
| 7689 | getASTContext(), |
| 7690 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())) |
| 7691 | .GetNumTemplateArguments(); |
| 7692 | |
| 7693 | case clang::Type::Paren: |
| 7694 | return (CompilerType(getASTContext(), |
| 7695 | llvm::cast<clang::ParenType>(qual_type)->desugar())) |
| 7696 | .GetNumTemplateArguments(); |
| 7697 | |
| 7698 | default: |
| 7699 | break; |
| 7700 | } |
| 7701 | |
| 7702 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7703 | } |
| 7704 | |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7705 | const clang::ClassTemplateSpecializationDecl * |
| 7706 | ClangASTContext::GetAsTemplateSpecialization( |
| 7707 | lldb::opaque_compiler_type_t type) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7708 | if (!type) |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7709 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7710 | |
| 7711 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 7712 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 7713 | switch (type_class) { |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7714 | case clang::Type::Record: { |
| 7715 | if (! GetCompleteType(type)) |
| 7716 | return nullptr; |
| 7717 | const clang::CXXRecordDecl *cxx_record_decl = |
| 7718 | qual_type->getAsCXXRecordDecl(); |
| 7719 | if (!cxx_record_decl) |
| 7720 | return nullptr; |
| 7721 | return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>( |
| 7722 | cxx_record_decl); |
| 7723 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7724 | |
| 7725 | case clang::Type::Typedef: |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7726 | return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type) |
| 7727 | ->getDecl() |
| 7728 | ->getUnderlyingType() |
| 7729 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7730 | |
| 7731 | case clang::Type::Auto: |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7732 | return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type) |
| 7733 | ->getDeducedType() |
| 7734 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7735 | |
| 7736 | case clang::Type::Elaborated: |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7737 | return GetAsTemplateSpecialization( |
| 7738 | llvm::cast<clang::ElaboratedType>(qual_type) |
| 7739 | ->getNamedType() |
| 7740 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7741 | |
| 7742 | case clang::Type::Paren: |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7743 | return GetAsTemplateSpecialization( |
| 7744 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7745 | |
| 7746 | default: |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7747 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7748 | } |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7749 | } |
| 7750 | |
| 7751 | lldb::TemplateArgumentKind |
| 7752 | ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type, |
| 7753 | size_t arg_idx) { |
| 7754 | const clang::ClassTemplateSpecializationDecl *template_decl = |
| 7755 | GetAsTemplateSpecialization(type); |
| 7756 | if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size()) |
| 7757 | return eTemplateArgumentKindNull; |
| 7758 | |
| 7759 | switch (template_decl->getTemplateArgs()[arg_idx].getKind()) { |
| 7760 | case clang::TemplateArgument::Null: |
| 7761 | return eTemplateArgumentKindNull; |
| 7762 | |
| 7763 | case clang::TemplateArgument::NullPtr: |
| 7764 | return eTemplateArgumentKindNullPtr; |
| 7765 | |
| 7766 | case clang::TemplateArgument::Type: |
| 7767 | return eTemplateArgumentKindType; |
| 7768 | |
| 7769 | case clang::TemplateArgument::Declaration: |
| 7770 | return eTemplateArgumentKindDeclaration; |
| 7771 | |
| 7772 | case clang::TemplateArgument::Integral: |
| 7773 | return eTemplateArgumentKindIntegral; |
| 7774 | |
| 7775 | case clang::TemplateArgument::Template: |
| 7776 | return eTemplateArgumentKindTemplate; |
| 7777 | |
| 7778 | case clang::TemplateArgument::TemplateExpansion: |
| 7779 | return eTemplateArgumentKindTemplateExpansion; |
| 7780 | |
| 7781 | case clang::TemplateArgument::Expression: |
| 7782 | return eTemplateArgumentKindExpression; |
| 7783 | |
| 7784 | case clang::TemplateArgument::Pack: |
| 7785 | return eTemplateArgumentKindPack; |
| 7786 | } |
| 7787 | llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind"); |
| 7788 | } |
| 7789 | |
| 7790 | CompilerType |
| 7791 | ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type, |
| 7792 | size_t idx) { |
| 7793 | const clang::ClassTemplateSpecializationDecl *template_decl = |
| 7794 | GetAsTemplateSpecialization(type); |
| 7795 | if (!template_decl || idx >= template_decl->getTemplateArgs().size()) |
| 7796 | return CompilerType(); |
| 7797 | |
| 7798 | const clang::TemplateArgument &template_arg = |
| 7799 | template_decl->getTemplateArgs()[idx]; |
| 7800 | if (template_arg.getKind() != clang::TemplateArgument::Type) |
| 7801 | return CompilerType(); |
| 7802 | |
| 7803 | return CompilerType(getASTContext(), template_arg.getAsType()); |
| 7804 | } |
| 7805 | |
Pavel Labath | f59056f | 2017-11-30 10:16:54 +0000 | [diff] [blame] | 7806 | llvm::Optional<CompilerType::IntegralTemplateArgument> |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7807 | ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type, |
| 7808 | size_t idx) { |
| 7809 | const clang::ClassTemplateSpecializationDecl *template_decl = |
| 7810 | GetAsTemplateSpecialization(type); |
| 7811 | if (! template_decl || idx >= template_decl->getTemplateArgs().size()) |
Pavel Labath | f59056f | 2017-11-30 10:16:54 +0000 | [diff] [blame] | 7812 | return llvm::None; |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7813 | |
| 7814 | const clang::TemplateArgument &template_arg = |
| 7815 | template_decl->getTemplateArgs()[idx]; |
| 7816 | if (template_arg.getKind() != clang::TemplateArgument::Integral) |
Pavel Labath | f59056f | 2017-11-30 10:16:54 +0000 | [diff] [blame] | 7817 | return llvm::None; |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7818 | |
Pavel Labath | f59056f | 2017-11-30 10:16:54 +0000 | [diff] [blame] | 7819 | return {{template_arg.getAsIntegral(), |
| 7820 | CompilerType(getASTContext(), template_arg.getIntegralType())}}; |
Enrico Granata | c6bf2e2 | 2015-09-23 01:39:46 +0000 | [diff] [blame] | 7821 | } |
| 7822 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7823 | CompilerType ClangASTContext::GetTypeForFormatters(void *type) { |
| 7824 | if (type) |
| 7825 | return ClangUtil::RemoveFastQualifiers(CompilerType(this, type)); |
| 7826 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7827 | } |
| 7828 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7829 | clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) { |
| 7830 | const clang::EnumType *enutype = |
| 7831 | llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type)); |
| 7832 | if (enutype) |
| 7833 | return enutype->getDecl(); |
| 7834 | return NULL; |
| 7835 | } |
| 7836 | |
| 7837 | clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) { |
| 7838 | const clang::RecordType *record_type = |
| 7839 | llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type)); |
| 7840 | if (record_type) |
| 7841 | return record_type->getDecl(); |
| 7842 | return nullptr; |
| 7843 | } |
| 7844 | |
| 7845 | clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) { |
Zachary Turner | 1639c6b | 2018-12-17 16:15:13 +0000 | [diff] [blame] | 7846 | return ClangUtil::GetAsTagDecl(type); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 7847 | } |
| 7848 | |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 7849 | clang::TypedefNameDecl * |
| 7850 | ClangASTContext::GetAsTypedefDecl(const CompilerType &type) { |
| 7851 | const clang::TypedefType *typedef_type = |
| 7852 | llvm::dyn_cast<clang::TypedefType>(ClangUtil::GetQualType(type)); |
| 7853 | if (typedef_type) |
| 7854 | return typedef_type->getDecl(); |
| 7855 | return nullptr; |
| 7856 | } |
| 7857 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7858 | clang::CXXRecordDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7859 | ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) { |
| 7860 | return GetCanonicalQualType(type)->getAsCXXRecordDecl(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7861 | } |
| 7862 | |
| 7863 | clang::ObjCInterfaceDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7864 | ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) { |
| 7865 | const clang::ObjCObjectType *objc_class_type = |
| 7866 | llvm::dyn_cast<clang::ObjCObjectType>( |
| 7867 | ClangUtil::GetCanonicalQualType(type)); |
| 7868 | if (objc_class_type) |
| 7869 | return objc_class_type->getInterface(); |
| 7870 | return nullptr; |
| 7871 | } |
| 7872 | |
| 7873 | clang::FieldDecl *ClangASTContext::AddFieldToRecordType( |
Zachary Turner | a3e2ea1 | 2018-10-23 17:22:02 +0000 | [diff] [blame] | 7874 | const CompilerType &type, llvm::StringRef name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7875 | const CompilerType &field_clang_type, AccessType access, |
| 7876 | uint32_t bitfield_bit_size) { |
| 7877 | if (!type.IsValid() || !field_clang_type.IsValid()) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7878 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7879 | ClangASTContext *ast = |
| 7880 | llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem()); |
| 7881 | if (!ast) |
| 7882 | return nullptr; |
| 7883 | clang::ASTContext *clang_ast = ast->getASTContext(); |
Zachary Turner | a3e2ea1 | 2018-10-23 17:22:02 +0000 | [diff] [blame] | 7884 | clang::IdentifierInfo *ident = nullptr; |
| 7885 | if (!name.empty()) |
| 7886 | ident = &clang_ast->Idents.get(name); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7887 | |
| 7888 | clang::FieldDecl *field = nullptr; |
| 7889 | |
| 7890 | clang::Expr *bit_width = nullptr; |
| 7891 | if (bitfield_bit_size != 0) { |
| 7892 | llvm::APInt bitfield_bit_size_apint( |
| 7893 | clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size); |
| 7894 | bit_width = new (*clang_ast) |
| 7895 | clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint, |
| 7896 | clang_ast->IntTy, clang::SourceLocation()); |
| 7897 | } |
| 7898 | |
| 7899 | clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type); |
| 7900 | if (record_decl) { |
| 7901 | field = clang::FieldDecl::Create( |
| 7902 | *clang_ast, record_decl, clang::SourceLocation(), |
| 7903 | clang::SourceLocation(), |
Zachary Turner | a3e2ea1 | 2018-10-23 17:22:02 +0000 | [diff] [blame] | 7904 | ident, // Identifier |
| 7905 | ClangUtil::GetQualType(field_clang_type), // Field type |
| 7906 | nullptr, // TInfo * |
| 7907 | bit_width, // BitWidth |
| 7908 | false, // Mutable |
| 7909 | clang::ICIS_NoInit); // HasInit |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7910 | |
Zachary Turner | a3e2ea1 | 2018-10-23 17:22:02 +0000 | [diff] [blame] | 7911 | if (name.empty()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 7912 | // Determine whether this field corresponds to an anonymous struct or |
| 7913 | // union. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7914 | if (const clang::TagType *TagT = |
| 7915 | field->getType()->getAs<clang::TagType>()) { |
| 7916 | if (clang::RecordDecl *Rec = |
| 7917 | llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl())) |
| 7918 | if (!Rec->getDeclName()) { |
| 7919 | Rec->setAnonymousStructOrUnion(true); |
| 7920 | field->setImplicit(); |
| 7921 | } |
| 7922 | } |
| 7923 | } |
| 7924 | |
| 7925 | if (field) { |
| 7926 | field->setAccess( |
| 7927 | ClangASTContext::ConvertAccessTypeToAccessSpecifier(access)); |
| 7928 | |
| 7929 | record_decl->addDecl(field); |
| 7930 | |
| 7931 | #ifdef LLDB_CONFIGURATION_DEBUG |
| 7932 | VerifyDecl(field); |
| 7933 | #endif |
| 7934 | } |
| 7935 | } else { |
| 7936 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 7937 | ast->GetAsObjCInterfaceDecl(type); |
| 7938 | |
| 7939 | if (class_interface_decl) { |
| 7940 | const bool is_synthesized = false; |
| 7941 | |
| 7942 | field_clang_type.GetCompleteType(); |
| 7943 | |
| 7944 | field = clang::ObjCIvarDecl::Create( |
| 7945 | *clang_ast, class_interface_decl, clang::SourceLocation(), |
| 7946 | clang::SourceLocation(), |
Zachary Turner | a3e2ea1 | 2018-10-23 17:22:02 +0000 | [diff] [blame] | 7947 | ident, // Identifier |
| 7948 | ClangUtil::GetQualType(field_clang_type), // Field type |
| 7949 | nullptr, // TypeSourceInfo * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7950 | ConvertAccessTypeToObjCIvarAccessControl(access), bit_width, |
| 7951 | is_synthesized); |
| 7952 | |
| 7953 | if (field) { |
| 7954 | class_interface_decl->addDecl(field); |
| 7955 | |
| 7956 | #ifdef LLDB_CONFIGURATION_DEBUG |
| 7957 | VerifyDecl(field); |
| 7958 | #endif |
| 7959 | } |
| 7960 | } |
| 7961 | } |
| 7962 | return field; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7963 | } |
| 7964 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7965 | void ClangASTContext::BuildIndirectFields(const CompilerType &type) { |
| 7966 | if (!type) |
| 7967 | return; |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 7968 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7969 | ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); |
| 7970 | if (!ast) |
| 7971 | return; |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 7972 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7973 | clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type); |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 7974 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7975 | if (!record_decl) |
| 7976 | return; |
| 7977 | |
| 7978 | typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector; |
| 7979 | |
| 7980 | IndirectFieldVector indirect_fields; |
| 7981 | clang::RecordDecl::field_iterator field_pos; |
| 7982 | clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end(); |
| 7983 | clang::RecordDecl::field_iterator last_field_pos = field_end_pos; |
| 7984 | for (field_pos = record_decl->field_begin(); field_pos != field_end_pos; |
| 7985 | last_field_pos = field_pos++) { |
| 7986 | if (field_pos->isAnonymousStructOrUnion()) { |
| 7987 | clang::QualType field_qual_type = field_pos->getType(); |
| 7988 | |
| 7989 | const clang::RecordType *field_record_type = |
| 7990 | field_qual_type->getAs<clang::RecordType>(); |
| 7991 | |
| 7992 | if (!field_record_type) |
| 7993 | continue; |
| 7994 | |
| 7995 | clang::RecordDecl *field_record_decl = field_record_type->getDecl(); |
| 7996 | |
| 7997 | if (!field_record_decl) |
| 7998 | continue; |
| 7999 | |
| 8000 | for (clang::RecordDecl::decl_iterator |
| 8001 | di = field_record_decl->decls_begin(), |
| 8002 | de = field_record_decl->decls_end(); |
| 8003 | di != de; ++di) { |
| 8004 | if (clang::FieldDecl *nested_field_decl = |
| 8005 | llvm::dyn_cast<clang::FieldDecl>(*di)) { |
| 8006 | clang::NamedDecl **chain = |
| 8007 | new (*ast->getASTContext()) clang::NamedDecl *[2]; |
| 8008 | chain[0] = *field_pos; |
| 8009 | chain[1] = nested_field_decl; |
| 8010 | clang::IndirectFieldDecl *indirect_field = |
| 8011 | clang::IndirectFieldDecl::Create( |
| 8012 | *ast->getASTContext(), record_decl, clang::SourceLocation(), |
| 8013 | nested_field_decl->getIdentifier(), |
| 8014 | nested_field_decl->getType(), {chain, 2}); |
| 8015 | |
| 8016 | indirect_field->setImplicit(); |
| 8017 | |
| 8018 | indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers( |
| 8019 | field_pos->getAccess(), nested_field_decl->getAccess())); |
| 8020 | |
| 8021 | indirect_fields.push_back(indirect_field); |
| 8022 | } else if (clang::IndirectFieldDecl *nested_indirect_field_decl = |
| 8023 | llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) { |
| 8024 | size_t nested_chain_size = |
| 8025 | nested_indirect_field_decl->getChainingSize(); |
| 8026 | clang::NamedDecl **chain = new (*ast->getASTContext()) |
| 8027 | clang::NamedDecl *[nested_chain_size + 1]; |
| 8028 | chain[0] = *field_pos; |
| 8029 | |
| 8030 | int chain_index = 1; |
| 8031 | for (clang::IndirectFieldDecl::chain_iterator |
| 8032 | nci = nested_indirect_field_decl->chain_begin(), |
| 8033 | nce = nested_indirect_field_decl->chain_end(); |
| 8034 | nci < nce; ++nci) { |
| 8035 | chain[chain_index] = *nci; |
| 8036 | chain_index++; |
| 8037 | } |
| 8038 | |
| 8039 | clang::IndirectFieldDecl *indirect_field = |
| 8040 | clang::IndirectFieldDecl::Create( |
| 8041 | *ast->getASTContext(), record_decl, clang::SourceLocation(), |
| 8042 | nested_indirect_field_decl->getIdentifier(), |
| 8043 | nested_indirect_field_decl->getType(), |
| 8044 | {chain, nested_chain_size + 1}); |
| 8045 | |
| 8046 | indirect_field->setImplicit(); |
| 8047 | |
| 8048 | indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers( |
| 8049 | field_pos->getAccess(), nested_indirect_field_decl->getAccess())); |
| 8050 | |
| 8051 | indirect_fields.push_back(indirect_field); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8052 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8053 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8054 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8055 | } |
| 8056 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 8057 | // Check the last field to see if it has an incomplete array type as its last |
| 8058 | // member and if it does, the tell the record decl about it |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8059 | if (last_field_pos != field_end_pos) { |
| 8060 | if (last_field_pos->getType()->isIncompleteArrayType()) |
| 8061 | record_decl->hasFlexibleArrayMember(); |
| 8062 | } |
| 8063 | |
| 8064 | for (IndirectFieldVector::iterator ifi = indirect_fields.begin(), |
| 8065 | ife = indirect_fields.end(); |
| 8066 | ifi < ife; ++ifi) { |
| 8067 | record_decl->addDecl(*ifi); |
| 8068 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8069 | } |
| 8070 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8071 | void ClangASTContext::SetIsPacked(const CompilerType &type) { |
| 8072 | if (type) { |
| 8073 | ClangASTContext *ast = |
| 8074 | llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); |
| 8075 | if (ast) { |
| 8076 | clang::RecordDecl *record_decl = GetAsRecordDecl(type); |
| 8077 | |
| 8078 | if (!record_decl) |
Greg Clayton | f73034f | 2015-09-08 18:15:05 +0000 | [diff] [blame] | 8079 | return; |
| 8080 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8081 | record_decl->addAttr( |
| 8082 | clang::PackedAttr::CreateImplicit(*ast->getASTContext())); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8083 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8084 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8085 | } |
| 8086 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8087 | clang::VarDecl *ClangASTContext::AddVariableToRecordType( |
Zachary Turner | a3e2ea1 | 2018-10-23 17:22:02 +0000 | [diff] [blame] | 8088 | const CompilerType &type, llvm::StringRef name, |
| 8089 | const CompilerType &var_type, AccessType access) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8090 | if (!type.IsValid() || !var_type.IsValid()) |
| 8091 | return nullptr; |
Zachary Turner | a3e2ea1 | 2018-10-23 17:22:02 +0000 | [diff] [blame] | 8092 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8093 | ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); |
| 8094 | if (!ast) |
| 8095 | return nullptr; |
| 8096 | |
| 8097 | clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type); |
Zachary Turner | a3e2ea1 | 2018-10-23 17:22:02 +0000 | [diff] [blame] | 8098 | if (!record_decl) |
| 8099 | return nullptr; |
| 8100 | |
| 8101 | clang::VarDecl *var_decl = nullptr; |
| 8102 | clang::IdentifierInfo *ident = nullptr; |
| 8103 | if (!name.empty()) |
| 8104 | ident = &ast->getASTContext()->Idents.get(name); |
| 8105 | |
| 8106 | var_decl = clang::VarDecl::Create( |
| 8107 | *ast->getASTContext(), // ASTContext & |
| 8108 | record_decl, // DeclContext * |
| 8109 | clang::SourceLocation(), // clang::SourceLocation StartLoc |
| 8110 | clang::SourceLocation(), // clang::SourceLocation IdLoc |
| 8111 | ident, // clang::IdentifierInfo * |
| 8112 | ClangUtil::GetQualType(var_type), // Variable clang::QualType |
| 8113 | nullptr, // TypeSourceInfo * |
| 8114 | clang::SC_Static); // StorageClass |
| 8115 | if (!var_decl) |
| 8116 | return nullptr; |
| 8117 | |
| 8118 | var_decl->setAccess( |
| 8119 | ClangASTContext::ConvertAccessTypeToAccessSpecifier(access)); |
| 8120 | record_decl->addDecl(var_decl); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8121 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8122 | #ifdef LLDB_CONFIGURATION_DEBUG |
Zachary Turner | a3e2ea1 | 2018-10-23 17:22:02 +0000 | [diff] [blame] | 8123 | VerifyDecl(var_decl); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8124 | #endif |
Zachary Turner | a3e2ea1 | 2018-10-23 17:22:02 +0000 | [diff] [blame] | 8125 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8126 | return var_decl; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8127 | } |
| 8128 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8129 | clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType( |
Davide Italiano | 675767a | 2018-03-27 19:40:50 +0000 | [diff] [blame] | 8130 | 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] | 8131 | const CompilerType &method_clang_type, lldb::AccessType access, |
| 8132 | bool is_virtual, bool is_static, bool is_inline, bool is_explicit, |
| 8133 | bool is_attr_used, bool is_artificial) { |
| 8134 | if (!type || !method_clang_type.IsValid() || name == nullptr || |
| 8135 | name[0] == '\0') |
| 8136 | return nullptr; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8137 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8138 | clang::QualType record_qual_type(GetCanonicalQualType(type)); |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 8139 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8140 | clang::CXXRecordDecl *cxx_record_decl = |
| 8141 | record_qual_type->getAsCXXRecordDecl(); |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 8142 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8143 | if (cxx_record_decl == nullptr) |
| 8144 | return nullptr; |
| 8145 | |
| 8146 | clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type)); |
| 8147 | |
| 8148 | clang::CXXMethodDecl *cxx_method_decl = nullptr; |
| 8149 | |
| 8150 | clang::DeclarationName decl_name(&getASTContext()->Idents.get(name)); |
| 8151 | |
| 8152 | const clang::FunctionType *function_type = |
| 8153 | llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr()); |
| 8154 | |
| 8155 | if (function_type == nullptr) |
| 8156 | return nullptr; |
| 8157 | |
| 8158 | const clang::FunctionProtoType *method_function_prototype( |
| 8159 | llvm::dyn_cast<clang::FunctionProtoType>(function_type)); |
| 8160 | |
| 8161 | if (!method_function_prototype) |
| 8162 | return nullptr; |
| 8163 | |
| 8164 | unsigned int num_params = method_function_prototype->getNumParams(); |
| 8165 | |
| 8166 | clang::CXXDestructorDecl *cxx_dtor_decl(nullptr); |
| 8167 | clang::CXXConstructorDecl *cxx_ctor_decl(nullptr); |
| 8168 | |
| 8169 | if (is_artificial) |
| 8170 | return nullptr; // skip everything artificial |
| 8171 | |
| 8172 | if (name[0] == '~') { |
| 8173 | cxx_dtor_decl = clang::CXXDestructorDecl::Create( |
| 8174 | *getASTContext(), cxx_record_decl, clang::SourceLocation(), |
| 8175 | clang::DeclarationNameInfo( |
| 8176 | getASTContext()->DeclarationNames.getCXXDestructorName( |
| 8177 | getASTContext()->getCanonicalType(record_qual_type)), |
| 8178 | clang::SourceLocation()), |
| 8179 | method_qual_type, nullptr, is_inline, is_artificial); |
| 8180 | cxx_method_decl = cxx_dtor_decl; |
| 8181 | } else if (decl_name == cxx_record_decl->getDeclName()) { |
| 8182 | cxx_ctor_decl = clang::CXXConstructorDecl::Create( |
| 8183 | *getASTContext(), cxx_record_decl, clang::SourceLocation(), |
| 8184 | clang::DeclarationNameInfo( |
| 8185 | getASTContext()->DeclarationNames.getCXXConstructorName( |
| 8186 | getASTContext()->getCanonicalType(record_qual_type)), |
| 8187 | clang::SourceLocation()), |
| 8188 | method_qual_type, |
| 8189 | nullptr, // TypeSourceInfo * |
| 8190 | is_explicit, is_inline, is_artificial, false /*is_constexpr*/); |
| 8191 | cxx_method_decl = cxx_ctor_decl; |
| 8192 | } else { |
| 8193 | clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None; |
| 8194 | clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS; |
| 8195 | |
| 8196 | if (IsOperator(name, op_kind)) { |
| 8197 | if (op_kind != clang::NUM_OVERLOADED_OPERATORS) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 8198 | // Check the number of operator parameters. Sometimes we have seen bad |
| 8199 | // DWARF that doesn't correctly describe operators and if we try to |
| 8200 | // create a method and add it to the class, clang will assert and |
| 8201 | // crash, so we need to make sure things are acceptable. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8202 | const bool is_method = true; |
| 8203 | if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount( |
| 8204 | is_method, op_kind, num_params)) |
| 8205 | return nullptr; |
| 8206 | cxx_method_decl = clang::CXXMethodDecl::Create( |
| 8207 | *getASTContext(), cxx_record_decl, clang::SourceLocation(), |
| 8208 | clang::DeclarationNameInfo( |
| 8209 | getASTContext()->DeclarationNames.getCXXOperatorName(op_kind), |
| 8210 | clang::SourceLocation()), |
| 8211 | method_qual_type, |
| 8212 | nullptr, // TypeSourceInfo * |
| 8213 | SC, is_inline, false /*is_constexpr*/, clang::SourceLocation()); |
| 8214 | } else if (num_params == 0) { |
| 8215 | // Conversion operators don't take params... |
| 8216 | cxx_method_decl = clang::CXXConversionDecl::Create( |
| 8217 | *getASTContext(), cxx_record_decl, clang::SourceLocation(), |
| 8218 | clang::DeclarationNameInfo( |
| 8219 | getASTContext()->DeclarationNames.getCXXConversionFunctionName( |
| 8220 | getASTContext()->getCanonicalType( |
| 8221 | function_type->getReturnType())), |
| 8222 | clang::SourceLocation()), |
| 8223 | method_qual_type, |
| 8224 | nullptr, // TypeSourceInfo * |
| 8225 | is_inline, is_explicit, false /*is_constexpr*/, |
| 8226 | clang::SourceLocation()); |
| 8227 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8228 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8229 | |
| 8230 | if (cxx_method_decl == nullptr) { |
| 8231 | cxx_method_decl = clang::CXXMethodDecl::Create( |
| 8232 | *getASTContext(), cxx_record_decl, clang::SourceLocation(), |
| 8233 | clang::DeclarationNameInfo(decl_name, clang::SourceLocation()), |
| 8234 | method_qual_type, |
| 8235 | nullptr, // TypeSourceInfo * |
| 8236 | SC, is_inline, false /*is_constexpr*/, clang::SourceLocation()); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8237 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8238 | } |
| 8239 | |
| 8240 | clang::AccessSpecifier access_specifier = |
| 8241 | ClangASTContext::ConvertAccessTypeToAccessSpecifier(access); |
| 8242 | |
| 8243 | cxx_method_decl->setAccess(access_specifier); |
| 8244 | cxx_method_decl->setVirtualAsWritten(is_virtual); |
| 8245 | |
| 8246 | if (is_attr_used) |
| 8247 | cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext())); |
| 8248 | |
Davide Italiano | 675767a | 2018-03-27 19:40:50 +0000 | [diff] [blame] | 8249 | if (mangled_name != NULL) { |
| 8250 | cxx_method_decl->addAttr( |
| 8251 | clang::AsmLabelAttr::CreateImplicit(*getASTContext(), mangled_name)); |
| 8252 | } |
| 8253 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8254 | // Populate the method decl with parameter decls |
| 8255 | |
| 8256 | llvm::SmallVector<clang::ParmVarDecl *, 12> params; |
| 8257 | |
| 8258 | for (unsigned param_index = 0; param_index < num_params; ++param_index) { |
| 8259 | params.push_back(clang::ParmVarDecl::Create( |
| 8260 | *getASTContext(), cxx_method_decl, clang::SourceLocation(), |
| 8261 | clang::SourceLocation(), |
| 8262 | nullptr, // anonymous |
| 8263 | method_function_prototype->getParamType(param_index), nullptr, |
| 8264 | clang::SC_None, nullptr)); |
| 8265 | } |
| 8266 | |
| 8267 | cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params)); |
| 8268 | |
| 8269 | cxx_record_decl->addDecl(cxx_method_decl); |
| 8270 | |
| 8271 | // Sometimes the debug info will mention a constructor (default/copy/move), |
| 8272 | // destructor, or assignment operator (copy/move) but there won't be any |
| 8273 | // version of this in the code. So we check if the function was artificially |
| 8274 | // generated and if it is trivial and this lets the compiler/backend know |
| 8275 | // that it can inline the IR for these when it needs to and we can avoid a |
| 8276 | // "missing function" error when running expressions. |
| 8277 | |
| 8278 | if (is_artificial) { |
| 8279 | if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() && |
| 8280 | cxx_record_decl->hasTrivialDefaultConstructor()) || |
| 8281 | (cxx_ctor_decl->isCopyConstructor() && |
| 8282 | cxx_record_decl->hasTrivialCopyConstructor()) || |
| 8283 | (cxx_ctor_decl->isMoveConstructor() && |
| 8284 | cxx_record_decl->hasTrivialMoveConstructor()))) { |
| 8285 | cxx_ctor_decl->setDefaulted(); |
| 8286 | cxx_ctor_decl->setTrivial(true); |
| 8287 | } else if (cxx_dtor_decl) { |
| 8288 | if (cxx_record_decl->hasTrivialDestructor()) { |
| 8289 | cxx_dtor_decl->setDefaulted(); |
| 8290 | cxx_dtor_decl->setTrivial(true); |
| 8291 | } |
| 8292 | } else if ((cxx_method_decl->isCopyAssignmentOperator() && |
| 8293 | cxx_record_decl->hasTrivialCopyAssignment()) || |
| 8294 | (cxx_method_decl->isMoveAssignmentOperator() && |
| 8295 | cxx_record_decl->hasTrivialMoveAssignment())) { |
| 8296 | cxx_method_decl->setDefaulted(); |
| 8297 | cxx_method_decl->setTrivial(true); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8298 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8299 | } |
| 8300 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8301 | #ifdef LLDB_CONFIGURATION_DEBUG |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8302 | VerifyDecl(cxx_method_decl); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8303 | #endif |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8304 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8305 | // printf ("decl->isPolymorphic() = %i\n", |
| 8306 | // cxx_record_decl->isPolymorphic()); |
| 8307 | // printf ("decl->isAggregate() = %i\n", |
| 8308 | // cxx_record_decl->isAggregate()); |
| 8309 | // printf ("decl->isPOD() = %i\n", |
| 8310 | // cxx_record_decl->isPOD()); |
| 8311 | // printf ("decl->isEmpty() = %i\n", |
| 8312 | // cxx_record_decl->isEmpty()); |
| 8313 | // printf ("decl->isAbstract() = %i\n", |
| 8314 | // cxx_record_decl->isAbstract()); |
| 8315 | // printf ("decl->hasTrivialConstructor() = %i\n", |
| 8316 | // cxx_record_decl->hasTrivialConstructor()); |
| 8317 | // printf ("decl->hasTrivialCopyConstructor() = %i\n", |
| 8318 | // cxx_record_decl->hasTrivialCopyConstructor()); |
| 8319 | // printf ("decl->hasTrivialCopyAssignment() = %i\n", |
| 8320 | // cxx_record_decl->hasTrivialCopyAssignment()); |
| 8321 | // printf ("decl->hasTrivialDestructor() = %i\n", |
| 8322 | // cxx_record_decl->hasTrivialDestructor()); |
| 8323 | return cxx_method_decl; |
| 8324 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8325 | |
Aleksandr Urakov | 7d2a74f | 2018-08-14 07:57:44 +0000 | [diff] [blame] | 8326 | void ClangASTContext::AddMethodOverridesForCXXRecordType( |
| 8327 | lldb::opaque_compiler_type_t type) { |
| 8328 | if (auto *record = GetAsCXXRecordDecl(type)) |
| 8329 | for (auto *method : record->methods()) |
| 8330 | addOverridesForMethod(method); |
| 8331 | } |
| 8332 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8333 | #pragma mark C++ Base Classes |
| 8334 | |
Zachary Turner | 970f38e | 2018-10-25 20:44:56 +0000 | [diff] [blame] | 8335 | std::unique_ptr<clang::CXXBaseSpecifier> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8336 | ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type, |
| 8337 | AccessType access, bool is_virtual, |
| 8338 | bool base_of_class) { |
Zachary Turner | 970f38e | 2018-10-25 20:44:56 +0000 | [diff] [blame] | 8339 | if (!type) |
| 8340 | return nullptr; |
| 8341 | |
| 8342 | return llvm::make_unique<clang::CXXBaseSpecifier>( |
| 8343 | clang::SourceRange(), is_virtual, base_of_class, |
| 8344 | ClangASTContext::ConvertAccessTypeToAccessSpecifier(access), |
| 8345 | getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)), |
| 8346 | clang::SourceLocation()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8347 | } |
| 8348 | |
Zachary Turner | 970f38e | 2018-10-25 20:44:56 +0000 | [diff] [blame] | 8349 | bool ClangASTContext::TransferBaseClasses( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8350 | lldb::opaque_compiler_type_t type, |
Zachary Turner | 970f38e | 2018-10-25 20:44:56 +0000 | [diff] [blame] | 8351 | std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases) { |
| 8352 | if (!type) |
| 8353 | return false; |
| 8354 | clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type); |
| 8355 | if (!cxx_record_decl) |
| 8356 | return false; |
| 8357 | std::vector<clang::CXXBaseSpecifier *> raw_bases; |
| 8358 | raw_bases.reserve(bases.size()); |
| 8359 | |
| 8360 | // Clang will make a copy of them, so it's ok that we pass pointers that we're |
| 8361 | // about to destroy. |
| 8362 | for (auto &b : bases) |
| 8363 | raw_bases.push_back(b.get()); |
| 8364 | cxx_record_decl->setBases(raw_bases.data(), raw_bases.size()); |
| 8365 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8366 | } |
| 8367 | |
| 8368 | bool ClangASTContext::SetObjCSuperClass( |
| 8369 | const CompilerType &type, const CompilerType &superclass_clang_type) { |
| 8370 | ClangASTContext *ast = |
| 8371 | llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem()); |
| 8372 | if (!ast) |
| 8373 | return false; |
| 8374 | clang::ASTContext *clang_ast = ast->getASTContext(); |
| 8375 | |
| 8376 | if (type && superclass_clang_type.IsValid() && |
| 8377 | superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) { |
| 8378 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 8379 | GetAsObjCInterfaceDecl(type); |
| 8380 | clang::ObjCInterfaceDecl *super_interface_decl = |
| 8381 | GetAsObjCInterfaceDecl(superclass_clang_type); |
| 8382 | if (class_interface_decl && super_interface_decl) { |
| 8383 | class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo( |
| 8384 | clang_ast->getObjCInterfaceType(super_interface_decl))); |
| 8385 | return true; |
| 8386 | } |
| 8387 | } |
| 8388 | return false; |
| 8389 | } |
| 8390 | |
| 8391 | bool ClangASTContext::AddObjCClassProperty( |
| 8392 | const CompilerType &type, const char *property_name, |
| 8393 | const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl, |
| 8394 | const char *property_setter_name, const char *property_getter_name, |
| 8395 | uint32_t property_attributes, ClangASTMetadata *metadata) { |
| 8396 | if (!type || !property_clang_type.IsValid() || property_name == nullptr || |
| 8397 | property_name[0] == '\0') |
| 8398 | return false; |
| 8399 | ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); |
| 8400 | if (!ast) |
| 8401 | return false; |
| 8402 | clang::ASTContext *clang_ast = ast->getASTContext(); |
| 8403 | |
| 8404 | clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type); |
| 8405 | |
| 8406 | if (class_interface_decl) { |
| 8407 | CompilerType property_clang_type_to_access; |
| 8408 | |
| 8409 | if (property_clang_type.IsValid()) |
| 8410 | property_clang_type_to_access = property_clang_type; |
| 8411 | else if (ivar_decl) |
| 8412 | property_clang_type_to_access = |
| 8413 | CompilerType(clang_ast, ivar_decl->getType()); |
| 8414 | |
| 8415 | if (class_interface_decl && property_clang_type_to_access.IsValid()) { |
| 8416 | clang::TypeSourceInfo *prop_type_source; |
| 8417 | if (ivar_decl) |
| 8418 | prop_type_source = |
| 8419 | clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType()); |
| 8420 | else |
| 8421 | prop_type_source = clang_ast->getTrivialTypeSourceInfo( |
| 8422 | ClangUtil::GetQualType(property_clang_type)); |
| 8423 | |
| 8424 | clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create( |
| 8425 | *clang_ast, class_interface_decl, |
| 8426 | clang::SourceLocation(), // Source Location |
| 8427 | &clang_ast->Idents.get(property_name), |
| 8428 | clang::SourceLocation(), // Source Location for AT |
| 8429 | clang::SourceLocation(), // Source location for ( |
| 8430 | ivar_decl ? ivar_decl->getType() |
| 8431 | : ClangUtil::GetQualType(property_clang_type), |
| 8432 | prop_type_source); |
| 8433 | |
| 8434 | if (property_decl) { |
| 8435 | if (metadata) |
| 8436 | ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata); |
| 8437 | |
| 8438 | class_interface_decl->addDecl(property_decl); |
| 8439 | |
| 8440 | clang::Selector setter_sel, getter_sel; |
| 8441 | |
| 8442 | if (property_setter_name != nullptr) { |
| 8443 | std::string property_setter_no_colon( |
| 8444 | property_setter_name, strlen(property_setter_name) - 1); |
| 8445 | clang::IdentifierInfo *setter_ident = |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 8446 | &clang_ast->Idents.get(property_setter_no_colon); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8447 | setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident); |
| 8448 | } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) { |
| 8449 | std::string setter_sel_string("set"); |
| 8450 | setter_sel_string.push_back(::toupper(property_name[0])); |
| 8451 | setter_sel_string.append(&property_name[1]); |
| 8452 | clang::IdentifierInfo *setter_ident = |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 8453 | &clang_ast->Idents.get(setter_sel_string); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8454 | setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident); |
| 8455 | } |
| 8456 | property_decl->setSetterName(setter_sel); |
| 8457 | property_decl->setPropertyAttributes( |
| 8458 | clang::ObjCPropertyDecl::OBJC_PR_setter); |
| 8459 | |
| 8460 | if (property_getter_name != nullptr) { |
| 8461 | clang::IdentifierInfo *getter_ident = |
| 8462 | &clang_ast->Idents.get(property_getter_name); |
| 8463 | getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident); |
| 8464 | } else { |
| 8465 | clang::IdentifierInfo *getter_ident = |
| 8466 | &clang_ast->Idents.get(property_name); |
| 8467 | getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident); |
| 8468 | } |
| 8469 | property_decl->setGetterName(getter_sel); |
| 8470 | property_decl->setPropertyAttributes( |
| 8471 | clang::ObjCPropertyDecl::OBJC_PR_getter); |
| 8472 | |
| 8473 | if (ivar_decl) |
| 8474 | property_decl->setPropertyIvarDecl(ivar_decl); |
| 8475 | |
| 8476 | if (property_attributes & DW_APPLE_PROPERTY_readonly) |
| 8477 | property_decl->setPropertyAttributes( |
| 8478 | clang::ObjCPropertyDecl::OBJC_PR_readonly); |
| 8479 | if (property_attributes & DW_APPLE_PROPERTY_readwrite) |
| 8480 | property_decl->setPropertyAttributes( |
| 8481 | clang::ObjCPropertyDecl::OBJC_PR_readwrite); |
| 8482 | if (property_attributes & DW_APPLE_PROPERTY_assign) |
| 8483 | property_decl->setPropertyAttributes( |
| 8484 | clang::ObjCPropertyDecl::OBJC_PR_assign); |
| 8485 | if (property_attributes & DW_APPLE_PROPERTY_retain) |
| 8486 | property_decl->setPropertyAttributes( |
| 8487 | clang::ObjCPropertyDecl::OBJC_PR_retain); |
| 8488 | if (property_attributes & DW_APPLE_PROPERTY_copy) |
| 8489 | property_decl->setPropertyAttributes( |
| 8490 | clang::ObjCPropertyDecl::OBJC_PR_copy); |
| 8491 | if (property_attributes & DW_APPLE_PROPERTY_nonatomic) |
| 8492 | property_decl->setPropertyAttributes( |
| 8493 | clang::ObjCPropertyDecl::OBJC_PR_nonatomic); |
| 8494 | if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability) |
| 8495 | property_decl->setPropertyAttributes( |
| 8496 | clang::ObjCPropertyDecl::OBJC_PR_nullability); |
| 8497 | if (property_attributes & |
| 8498 | clang::ObjCPropertyDecl::OBJC_PR_null_resettable) |
| 8499 | property_decl->setPropertyAttributes( |
| 8500 | clang::ObjCPropertyDecl::OBJC_PR_null_resettable); |
| 8501 | if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) |
| 8502 | property_decl->setPropertyAttributes( |
| 8503 | clang::ObjCPropertyDecl::OBJC_PR_class); |
| 8504 | |
| 8505 | const bool isInstance = |
| 8506 | (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0; |
| 8507 | |
| 8508 | if (!getter_sel.isNull() && |
| 8509 | !(isInstance |
| 8510 | ? class_interface_decl->lookupInstanceMethod(getter_sel) |
| 8511 | : class_interface_decl->lookupClassMethod(getter_sel))) { |
| 8512 | const bool isVariadic = false; |
| 8513 | const bool isSynthesized = false; |
| 8514 | const bool isImplicitlyDeclared = true; |
| 8515 | const bool isDefined = false; |
| 8516 | const clang::ObjCMethodDecl::ImplementationControl impControl = |
| 8517 | clang::ObjCMethodDecl::None; |
| 8518 | const bool HasRelatedResultType = false; |
| 8519 | |
| 8520 | clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create( |
| 8521 | *clang_ast, clang::SourceLocation(), clang::SourceLocation(), |
| 8522 | getter_sel, ClangUtil::GetQualType(property_clang_type_to_access), |
| 8523 | nullptr, class_interface_decl, isInstance, isVariadic, |
| 8524 | isSynthesized, isImplicitlyDeclared, isDefined, impControl, |
| 8525 | HasRelatedResultType); |
| 8526 | |
| 8527 | if (getter && metadata) |
| 8528 | ClangASTContext::SetMetadata(clang_ast, getter, *metadata); |
| 8529 | |
| 8530 | if (getter) { |
| 8531 | getter->setMethodParams(*clang_ast, |
| 8532 | llvm::ArrayRef<clang::ParmVarDecl *>(), |
| 8533 | llvm::ArrayRef<clang::SourceLocation>()); |
| 8534 | |
| 8535 | class_interface_decl->addDecl(getter); |
| 8536 | } |
| 8537 | } |
| 8538 | |
| 8539 | if (!setter_sel.isNull() && |
| 8540 | !(isInstance |
| 8541 | ? class_interface_decl->lookupInstanceMethod(setter_sel) |
| 8542 | : class_interface_decl->lookupClassMethod(setter_sel))) { |
| 8543 | clang::QualType result_type = clang_ast->VoidTy; |
| 8544 | const bool isVariadic = false; |
| 8545 | const bool isSynthesized = false; |
| 8546 | const bool isImplicitlyDeclared = true; |
| 8547 | const bool isDefined = false; |
| 8548 | const clang::ObjCMethodDecl::ImplementationControl impControl = |
| 8549 | clang::ObjCMethodDecl::None; |
| 8550 | const bool HasRelatedResultType = false; |
| 8551 | |
| 8552 | clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create( |
| 8553 | *clang_ast, clang::SourceLocation(), clang::SourceLocation(), |
| 8554 | setter_sel, result_type, nullptr, class_interface_decl, |
| 8555 | isInstance, isVariadic, isSynthesized, isImplicitlyDeclared, |
| 8556 | isDefined, impControl, HasRelatedResultType); |
| 8557 | |
| 8558 | if (setter && metadata) |
| 8559 | ClangASTContext::SetMetadata(clang_ast, setter, *metadata); |
| 8560 | |
| 8561 | llvm::SmallVector<clang::ParmVarDecl *, 1> params; |
| 8562 | |
| 8563 | params.push_back(clang::ParmVarDecl::Create( |
| 8564 | *clang_ast, setter, clang::SourceLocation(), |
| 8565 | clang::SourceLocation(), |
| 8566 | nullptr, // anonymous |
| 8567 | ClangUtil::GetQualType(property_clang_type_to_access), nullptr, |
| 8568 | clang::SC_Auto, nullptr)); |
| 8569 | |
| 8570 | if (setter) { |
| 8571 | setter->setMethodParams( |
| 8572 | *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params), |
| 8573 | llvm::ArrayRef<clang::SourceLocation>()); |
| 8574 | |
| 8575 | class_interface_decl->addDecl(setter); |
| 8576 | } |
| 8577 | } |
| 8578 | |
| 8579 | return true; |
| 8580 | } |
| 8581 | } |
| 8582 | } |
| 8583 | return false; |
| 8584 | } |
| 8585 | |
| 8586 | bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type, |
| 8587 | bool check_superclass) { |
| 8588 | clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type); |
| 8589 | if (class_interface_decl) |
| 8590 | return ObjCDeclHasIVars(class_interface_decl, check_superclass); |
| 8591 | return false; |
| 8592 | } |
| 8593 | |
| 8594 | clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType( |
| 8595 | const CompilerType &type, |
| 8596 | const char *name, // the full symbol name as seen in the symbol table |
| 8597 | // (lldb::opaque_compiler_type_t type, "-[NString |
| 8598 | // stringWithCString:]") |
| 8599 | const CompilerType &method_clang_type, lldb::AccessType access, |
| 8600 | bool is_artificial, bool is_variadic) { |
| 8601 | if (!type || !method_clang_type.IsValid()) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8602 | return nullptr; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8603 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8604 | clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type); |
| 8605 | |
| 8606 | if (class_interface_decl == nullptr) |
| 8607 | return nullptr; |
| 8608 | ClangASTContext *lldb_ast = |
| 8609 | llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); |
| 8610 | if (lldb_ast == nullptr) |
| 8611 | return nullptr; |
| 8612 | clang::ASTContext *ast = lldb_ast->getASTContext(); |
| 8613 | |
| 8614 | const char *selector_start = ::strchr(name, ' '); |
| 8615 | if (selector_start == nullptr) |
| 8616 | return nullptr; |
| 8617 | |
| 8618 | selector_start++; |
| 8619 | llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents; |
| 8620 | |
| 8621 | size_t len = 0; |
| 8622 | const char *start; |
| 8623 | // printf ("name = '%s'\n", name); |
| 8624 | |
| 8625 | unsigned num_selectors_with_args = 0; |
| 8626 | for (start = selector_start; start && *start != '\0' && *start != ']'; |
| 8627 | start += len) { |
| 8628 | len = ::strcspn(start, ":]"); |
| 8629 | bool has_arg = (start[len] == ':'); |
| 8630 | if (has_arg) |
| 8631 | ++num_selectors_with_args; |
| 8632 | selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len))); |
| 8633 | if (has_arg) |
| 8634 | len += 1; |
| 8635 | } |
| 8636 | |
| 8637 | if (selector_idents.size() == 0) |
| 8638 | return nullptr; |
| 8639 | |
| 8640 | clang::Selector method_selector = ast->Selectors.getSelector( |
| 8641 | num_selectors_with_args ? selector_idents.size() : 0, |
| 8642 | selector_idents.data()); |
| 8643 | |
| 8644 | clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type)); |
| 8645 | |
| 8646 | // Populate the method decl with parameter decls |
| 8647 | const clang::Type *method_type(method_qual_type.getTypePtr()); |
| 8648 | |
| 8649 | if (method_type == nullptr) |
| 8650 | return nullptr; |
| 8651 | |
| 8652 | const clang::FunctionProtoType *method_function_prototype( |
| 8653 | llvm::dyn_cast<clang::FunctionProtoType>(method_type)); |
| 8654 | |
| 8655 | if (!method_function_prototype) |
| 8656 | return nullptr; |
| 8657 | |
| 8658 | bool is_synthesized = false; |
| 8659 | bool is_defined = false; |
| 8660 | clang::ObjCMethodDecl::ImplementationControl imp_control = |
| 8661 | clang::ObjCMethodDecl::None; |
| 8662 | |
| 8663 | const unsigned num_args = method_function_prototype->getNumParams(); |
| 8664 | |
| 8665 | if (num_args != num_selectors_with_args) |
| 8666 | return nullptr; // some debug information is corrupt. We are not going to |
| 8667 | // deal with it. |
| 8668 | |
| 8669 | clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create( |
| 8670 | *ast, |
| 8671 | clang::SourceLocation(), // beginLoc, |
| 8672 | clang::SourceLocation(), // endLoc, |
| 8673 | method_selector, method_function_prototype->getReturnType(), |
| 8674 | nullptr, // TypeSourceInfo *ResultTInfo, |
| 8675 | ClangASTContext::GetASTContext(ast)->GetDeclContextForType( |
| 8676 | ClangUtil::GetQualType(type)), |
| 8677 | name[0] == '-', is_variadic, is_synthesized, |
| 8678 | true, // is_implicitly_declared; we force this to true because we don't |
| 8679 | // have source locations |
| 8680 | is_defined, imp_control, false /*has_related_result_type*/); |
| 8681 | |
| 8682 | if (objc_method_decl == nullptr) |
| 8683 | return nullptr; |
| 8684 | |
| 8685 | if (num_args > 0) { |
| 8686 | llvm::SmallVector<clang::ParmVarDecl *, 12> params; |
| 8687 | |
| 8688 | for (unsigned param_index = 0; param_index < num_args; ++param_index) { |
| 8689 | params.push_back(clang::ParmVarDecl::Create( |
| 8690 | *ast, objc_method_decl, clang::SourceLocation(), |
| 8691 | clang::SourceLocation(), |
| 8692 | nullptr, // anonymous |
| 8693 | method_function_prototype->getParamType(param_index), nullptr, |
| 8694 | clang::SC_Auto, nullptr)); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8695 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8696 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8697 | objc_method_decl->setMethodParams( |
| 8698 | *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params), |
| 8699 | llvm::ArrayRef<clang::SourceLocation>()); |
| 8700 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8701 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8702 | class_interface_decl->addDecl(objc_method_decl); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8703 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8704 | #ifdef LLDB_CONFIGURATION_DEBUG |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8705 | VerifyDecl(objc_method_decl); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8706 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8707 | |
| 8708 | return objc_method_decl; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8709 | } |
| 8710 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8711 | bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) { |
| 8712 | if (ClangUtil::IsClangType(type)) |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 8713 | return false; |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 8714 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8715 | clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type)); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 8716 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8717 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 8718 | switch (type_class) { |
| 8719 | case clang::Type::Record: { |
| 8720 | clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); |
| 8721 | if (cxx_record_decl) |
| 8722 | return cxx_record_decl->hasExternalLexicalStorage() || |
| 8723 | cxx_record_decl->hasExternalVisibleStorage(); |
| 8724 | } break; |
Enrico Granata | 36f51e4 | 2015-12-18 22:41:25 +0000 | [diff] [blame] | 8725 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8726 | case clang::Type::Enum: { |
| 8727 | clang::EnumDecl *enum_decl = |
| 8728 | llvm::cast<clang::EnumType>(qual_type)->getDecl(); |
| 8729 | if (enum_decl) |
| 8730 | return enum_decl->hasExternalLexicalStorage() || |
| 8731 | enum_decl->hasExternalVisibleStorage(); |
| 8732 | } break; |
| 8733 | |
| 8734 | case clang::Type::ObjCObject: |
| 8735 | case clang::Type::ObjCInterface: { |
| 8736 | const clang::ObjCObjectType *objc_class_type = |
| 8737 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 8738 | assert(objc_class_type); |
| 8739 | if (objc_class_type) { |
| 8740 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 8741 | objc_class_type->getInterface(); |
| 8742 | |
| 8743 | if (class_interface_decl) |
| 8744 | return class_interface_decl->hasExternalLexicalStorage() || |
| 8745 | class_interface_decl->hasExternalVisibleStorage(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8746 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8747 | } break; |
| 8748 | |
| 8749 | case clang::Type::Typedef: |
| 8750 | return GetHasExternalStorage(CompilerType( |
| 8751 | type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type) |
| 8752 | ->getDecl() |
| 8753 | ->getUnderlyingType() |
| 8754 | .getAsOpaquePtr())); |
| 8755 | |
| 8756 | case clang::Type::Auto: |
| 8757 | return GetHasExternalStorage(CompilerType( |
| 8758 | type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type) |
| 8759 | ->getDeducedType() |
| 8760 | .getAsOpaquePtr())); |
| 8761 | |
| 8762 | case clang::Type::Elaborated: |
| 8763 | return GetHasExternalStorage(CompilerType( |
| 8764 | type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type) |
| 8765 | ->getNamedType() |
| 8766 | .getAsOpaquePtr())); |
| 8767 | |
| 8768 | case clang::Type::Paren: |
| 8769 | return GetHasExternalStorage(CompilerType( |
| 8770 | type.GetTypeSystem(), |
| 8771 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr())); |
| 8772 | |
| 8773 | default: |
| 8774 | break; |
| 8775 | } |
| 8776 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8777 | } |
| 8778 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8779 | bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type, |
| 8780 | bool has_extern) { |
| 8781 | if (!type) |
| 8782 | return false; |
| 8783 | |
| 8784 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 8785 | |
| 8786 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 8787 | switch (type_class) { |
| 8788 | case clang::Type::Record: { |
| 8789 | clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); |
| 8790 | if (cxx_record_decl) { |
| 8791 | cxx_record_decl->setHasExternalLexicalStorage(has_extern); |
| 8792 | cxx_record_decl->setHasExternalVisibleStorage(has_extern); |
| 8793 | return true; |
| 8794 | } |
| 8795 | } break; |
| 8796 | |
| 8797 | case clang::Type::Enum: { |
| 8798 | clang::EnumDecl *enum_decl = |
| 8799 | llvm::cast<clang::EnumType>(qual_type)->getDecl(); |
| 8800 | if (enum_decl) { |
| 8801 | enum_decl->setHasExternalLexicalStorage(has_extern); |
| 8802 | enum_decl->setHasExternalVisibleStorage(has_extern); |
| 8803 | return true; |
| 8804 | } |
| 8805 | } break; |
| 8806 | |
| 8807 | case clang::Type::ObjCObject: |
| 8808 | case clang::Type::ObjCInterface: { |
| 8809 | const clang::ObjCObjectType *objc_class_type = |
| 8810 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 8811 | assert(objc_class_type); |
| 8812 | if (objc_class_type) { |
| 8813 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 8814 | objc_class_type->getInterface(); |
| 8815 | |
| 8816 | if (class_interface_decl) { |
| 8817 | class_interface_decl->setHasExternalLexicalStorage(has_extern); |
| 8818 | class_interface_decl->setHasExternalVisibleStorage(has_extern); |
| 8819 | return true; |
| 8820 | } |
| 8821 | } |
| 8822 | } break; |
| 8823 | |
| 8824 | case clang::Type::Typedef: |
| 8825 | return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type) |
| 8826 | ->getDecl() |
| 8827 | ->getUnderlyingType() |
| 8828 | .getAsOpaquePtr(), |
| 8829 | has_extern); |
| 8830 | |
| 8831 | case clang::Type::Auto: |
| 8832 | return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type) |
| 8833 | ->getDeducedType() |
| 8834 | .getAsOpaquePtr(), |
| 8835 | has_extern); |
| 8836 | |
| 8837 | case clang::Type::Elaborated: |
| 8838 | return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type) |
| 8839 | ->getNamedType() |
| 8840 | .getAsOpaquePtr(), |
| 8841 | has_extern); |
| 8842 | |
| 8843 | case clang::Type::Paren: |
| 8844 | return SetHasExternalStorage( |
| 8845 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 8846 | has_extern); |
| 8847 | |
| 8848 | default: |
| 8849 | break; |
| 8850 | } |
| 8851 | return false; |
| 8852 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8853 | |
| 8854 | #pragma mark TagDecl |
| 8855 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8856 | bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) { |
| 8857 | clang::QualType qual_type(ClangUtil::GetQualType(type)); |
| 8858 | if (!qual_type.isNull()) { |
| 8859 | const clang::TagType *tag_type = qual_type->getAs<clang::TagType>(); |
| 8860 | if (tag_type) { |
| 8861 | clang::TagDecl *tag_decl = tag_type->getDecl(); |
| 8862 | if (tag_decl) { |
| 8863 | tag_decl->startDefinition(); |
| 8864 | return true; |
| 8865 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8866 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8867 | |
| 8868 | const clang::ObjCObjectType *object_type = |
| 8869 | qual_type->getAs<clang::ObjCObjectType>(); |
| 8870 | if (object_type) { |
| 8871 | clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface(); |
| 8872 | if (interface_decl) { |
| 8873 | interface_decl->startDefinition(); |
| 8874 | return true; |
| 8875 | } |
| 8876 | } |
| 8877 | } |
| 8878 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8879 | } |
| 8880 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8881 | bool ClangASTContext::CompleteTagDeclarationDefinition( |
| 8882 | const CompilerType &type) { |
| 8883 | clang::QualType qual_type(ClangUtil::GetQualType(type)); |
| 8884 | if (!qual_type.isNull()) { |
| 8885 | // Make sure we use the same methodology as |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 8886 | // ClangASTContext::StartTagDeclarationDefinition() as to how we start/end |
| 8887 | // the definition. Previously we were calling |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8888 | const clang::TagType *tag_type = qual_type->getAs<clang::TagType>(); |
| 8889 | if (tag_type) { |
| 8890 | clang::TagDecl *tag_decl = tag_type->getDecl(); |
| 8891 | if (tag_decl) { |
| 8892 | clang::CXXRecordDecl *cxx_record_decl = |
| 8893 | llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl); |
| 8894 | |
| 8895 | if (cxx_record_decl) { |
| 8896 | if (!cxx_record_decl->isCompleteDefinition()) |
| 8897 | cxx_record_decl->completeDefinition(); |
| 8898 | cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true); |
| 8899 | cxx_record_decl->setHasExternalLexicalStorage(false); |
| 8900 | cxx_record_decl->setHasExternalVisibleStorage(false); |
| 8901 | return true; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8902 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8903 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8904 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8905 | |
| 8906 | const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>(); |
| 8907 | |
| 8908 | if (enutype) { |
| 8909 | clang::EnumDecl *enum_decl = enutype->getDecl(); |
| 8910 | |
| 8911 | if (enum_decl) { |
| 8912 | if (!enum_decl->isCompleteDefinition()) { |
| 8913 | ClangASTContext *lldb_ast = |
| 8914 | llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); |
| 8915 | if (lldb_ast == nullptr) |
| 8916 | return false; |
| 8917 | clang::ASTContext *ast = lldb_ast->getASTContext(); |
| 8918 | |
| 8919 | /// TODO This really needs to be fixed. |
| 8920 | |
| 8921 | QualType integer_type(enum_decl->getIntegerType()); |
| 8922 | if (!integer_type.isNull()) { |
| 8923 | unsigned NumPositiveBits = 1; |
| 8924 | unsigned NumNegativeBits = 0; |
| 8925 | |
| 8926 | clang::QualType promotion_qual_type; |
| 8927 | // If the enum integer type is less than an integer in bit width, |
| 8928 | // then we must promote it to an integer size. |
| 8929 | if (ast->getTypeSize(enum_decl->getIntegerType()) < |
| 8930 | ast->getTypeSize(ast->IntTy)) { |
| 8931 | if (enum_decl->getIntegerType()->isSignedIntegerType()) |
| 8932 | promotion_qual_type = ast->IntTy; |
| 8933 | else |
| 8934 | promotion_qual_type = ast->UnsignedIntTy; |
| 8935 | } else |
| 8936 | promotion_qual_type = enum_decl->getIntegerType(); |
| 8937 | |
| 8938 | enum_decl->completeDefinition(enum_decl->getIntegerType(), |
| 8939 | promotion_qual_type, NumPositiveBits, |
| 8940 | NumNegativeBits); |
| 8941 | } |
| 8942 | } |
| 8943 | return true; |
| 8944 | } |
| 8945 | } |
| 8946 | } |
| 8947 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8948 | } |
| 8949 | |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 8950 | clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType( |
Shafik Yaghmour | 8c5ec1f | 2018-11-08 18:42:00 +0000 | [diff] [blame] | 8951 | const CompilerType &enum_type, const Declaration &decl, const char *name, |
Zachary Turner | 1639c6b | 2018-12-17 16:15:13 +0000 | [diff] [blame] | 8952 | const llvm::APSInt &value) { |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 8953 | |
Shafik Yaghmour | 8c5ec1f | 2018-11-08 18:42:00 +0000 | [diff] [blame] | 8954 | if (!enum_type || ConstString(name).IsEmpty()) |
| 8955 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8956 | |
Shafik Yaghmour | 8c5ec1f | 2018-11-08 18:42:00 +0000 | [diff] [blame] | 8957 | lldbassert(enum_type.GetTypeSystem() == static_cast<TypeSystem *>(this)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8958 | |
Shafik Yaghmour | 8c5ec1f | 2018-11-08 18:42:00 +0000 | [diff] [blame] | 8959 | lldb::opaque_compiler_type_t enum_opaque_compiler_type = |
| 8960 | enum_type.GetOpaqueQualType(); |
| 8961 | |
| 8962 | if (!enum_opaque_compiler_type) |
| 8963 | return nullptr; |
| 8964 | |
Shafik Yaghmour | 8c5ec1f | 2018-11-08 18:42:00 +0000 | [diff] [blame] | 8965 | clang::QualType enum_qual_type( |
| 8966 | GetCanonicalQualType(enum_opaque_compiler_type)); |
| 8967 | |
Shafik Yaghmour | 8c5ec1f | 2018-11-08 18:42:00 +0000 | [diff] [blame] | 8968 | const clang::Type *clang_type = enum_qual_type.getTypePtr(); |
| 8969 | |
| 8970 | if (!clang_type) |
| 8971 | return nullptr; |
| 8972 | |
| 8973 | const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type); |
| 8974 | |
| 8975 | if (!enutype) |
| 8976 | return nullptr; |
| 8977 | |
Shafik Yaghmour | 8c5ec1f | 2018-11-08 18:42:00 +0000 | [diff] [blame] | 8978 | clang::EnumConstantDecl *enumerator_decl = clang::EnumConstantDecl::Create( |
| 8979 | *getASTContext(), enutype->getDecl(), clang::SourceLocation(), |
| 8980 | name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier |
Zachary Turner | 1639c6b | 2018-12-17 16:15:13 +0000 | [diff] [blame] | 8981 | clang::QualType(enutype, 0), nullptr, value); |
Shafik Yaghmour | 8c5ec1f | 2018-11-08 18:42:00 +0000 | [diff] [blame] | 8982 | |
| 8983 | if (!enumerator_decl) |
| 8984 | return nullptr; |
| 8985 | |
| 8986 | enutype->getDecl()->addDecl(enumerator_decl); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8987 | |
| 8988 | #ifdef LLDB_CONFIGURATION_DEBUG |
Shafik Yaghmour | 8c5ec1f | 2018-11-08 18:42:00 +0000 | [diff] [blame] | 8989 | VerifyDecl(enumerator_decl); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8990 | #endif |
| 8991 | |
Shafik Yaghmour | 8c5ec1f | 2018-11-08 18:42:00 +0000 | [diff] [blame] | 8992 | return enumerator_decl; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8993 | } |
| 8994 | |
Zachary Turner | 1639c6b | 2018-12-17 16:15:13 +0000 | [diff] [blame] | 8995 | clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType( |
| 8996 | const CompilerType &enum_type, const Declaration &decl, const char *name, |
| 8997 | int64_t enum_value, uint32_t enum_value_bit_size) { |
| 8998 | CompilerType underlying_type = |
| 8999 | GetEnumerationIntegerType(enum_type.GetOpaqueQualType()); |
| 9000 | bool is_signed = false; |
| 9001 | underlying_type.IsIntegerType(is_signed); |
| 9002 | |
| 9003 | llvm::APSInt value(enum_value_bit_size, is_signed); |
| 9004 | value = enum_value; |
| 9005 | |
| 9006 | return AddEnumerationValueToEnumerationType(enum_type, decl, name, value); |
| 9007 | } |
| 9008 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 9009 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9010 | ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) { |
| 9011 | clang::QualType enum_qual_type(GetCanonicalQualType(type)); |
| 9012 | const clang::Type *clang_type = enum_qual_type.getTypePtr(); |
| 9013 | if (clang_type) { |
| 9014 | const clang::EnumType *enutype = |
| 9015 | llvm::dyn_cast<clang::EnumType>(clang_type); |
| 9016 | if (enutype) { |
| 9017 | clang::EnumDecl *enum_decl = enutype->getDecl(); |
| 9018 | if (enum_decl) |
| 9019 | return CompilerType(getASTContext(), enum_decl->getIntegerType()); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9020 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9021 | } |
| 9022 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9023 | } |
| 9024 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9025 | CompilerType |
| 9026 | ClangASTContext::CreateMemberPointerType(const CompilerType &type, |
| 9027 | const CompilerType &pointee_type) { |
| 9028 | if (type && pointee_type.IsValid() && |
| 9029 | type.GetTypeSystem() == pointee_type.GetTypeSystem()) { |
| 9030 | ClangASTContext *ast = |
| 9031 | llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); |
| 9032 | if (!ast) |
| 9033 | return CompilerType(); |
| 9034 | return CompilerType(ast->getASTContext(), |
| 9035 | ast->getASTContext()->getMemberPointerType( |
| 9036 | ClangUtil::GetQualType(pointee_type), |
| 9037 | ClangUtil::GetQualType(type).getTypePtr())); |
| 9038 | } |
| 9039 | return CompilerType(); |
| 9040 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9041 | |
| 9042 | size_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9043 | ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type, |
| 9044 | const char *s, uint8_t *dst, |
| 9045 | size_t dst_size) { |
| 9046 | if (type) { |
| 9047 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 9048 | uint32_t count = 0; |
| 9049 | bool is_complex = false; |
| 9050 | if (IsFloatingPointType(type, count, is_complex)) { |
| 9051 | // TODO: handle complex and vector types |
| 9052 | if (count != 1) |
| 9053 | return false; |
| 9054 | |
| 9055 | llvm::StringRef s_sref(s); |
| 9056 | llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type), |
| 9057 | s_sref); |
| 9058 | |
| 9059 | const uint64_t bit_size = getASTContext()->getTypeSize(qual_type); |
| 9060 | const uint64_t byte_size = bit_size / 8; |
| 9061 | if (dst_size >= byte_size) { |
| 9062 | Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc( |
| 9063 | llvm::NextPowerOf2(byte_size) * 8); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 9064 | lldb_private::Status get_data_error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9065 | if (scalar.GetAsMemoryData(dst, byte_size, |
| 9066 | lldb_private::endian::InlHostByteOrder(), |
| 9067 | get_data_error)) |
| 9068 | return byte_size; |
| 9069 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9070 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9071 | } |
| 9072 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9073 | } |
| 9074 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9075 | //---------------------------------------------------------------------- |
| 9076 | // Dumping types |
| 9077 | //---------------------------------------------------------------------- |
| 9078 | #define DEPTH_INCREMENT 2 |
| 9079 | |
Zachary Turner | 4911023 | 2018-11-05 17:40:28 +0000 | [diff] [blame] | 9080 | void ClangASTContext::Dump(Stream &s) { |
Zachary Turner | 115209e | 2018-11-05 19:25:39 +0000 | [diff] [blame] | 9081 | Decl *tu = Decl::castFromDeclContext(GetTranslationUnitDecl()); |
Zachary Turner | 4911023 | 2018-11-05 17:40:28 +0000 | [diff] [blame] | 9082 | tu->dump(s.AsRawOstream()); |
| 9083 | } |
| 9084 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9085 | void ClangASTContext::DumpValue( |
| 9086 | lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s, |
Zachary Turner | 29cb868 | 2017-03-03 20:57:05 +0000 | [diff] [blame] | 9087 | lldb::Format format, const DataExtractor &data, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9088 | lldb::offset_t data_byte_offset, size_t data_byte_size, |
| 9089 | uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types, |
| 9090 | bool show_summary, bool verbose, uint32_t depth) { |
| 9091 | if (!type) |
| 9092 | return; |
| 9093 | |
| 9094 | clang::QualType qual_type(GetQualType(type)); |
| 9095 | switch (qual_type->getTypeClass()) { |
| 9096 | case clang::Type::Record: |
| 9097 | if (GetCompleteType(type)) { |
| 9098 | const clang::RecordType *record_type = |
| 9099 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 9100 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 9101 | assert(record_decl); |
| 9102 | uint32_t field_bit_offset = 0; |
| 9103 | uint32_t field_byte_offset = 0; |
| 9104 | const clang::ASTRecordLayout &record_layout = |
| 9105 | getASTContext()->getASTRecordLayout(record_decl); |
| 9106 | uint32_t child_idx = 0; |
| 9107 | |
| 9108 | const clang::CXXRecordDecl *cxx_record_decl = |
| 9109 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 9110 | if (cxx_record_decl) { |
| 9111 | // We might have base classes to print out first |
| 9112 | clang::CXXRecordDecl::base_class_const_iterator base_class, |
| 9113 | base_class_end; |
| 9114 | for (base_class = cxx_record_decl->bases_begin(), |
| 9115 | base_class_end = cxx_record_decl->bases_end(); |
| 9116 | base_class != base_class_end; ++base_class) { |
| 9117 | const clang::CXXRecordDecl *base_class_decl = |
| 9118 | llvm::cast<clang::CXXRecordDecl>( |
| 9119 | base_class->getType()->getAs<clang::RecordType>()->getDecl()); |
| 9120 | |
| 9121 | // Skip empty base classes |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 9122 | if (!verbose && !ClangASTContext::RecordHasFields(base_class_decl)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9123 | continue; |
| 9124 | |
| 9125 | if (base_class->isVirtual()) |
| 9126 | field_bit_offset = |
| 9127 | record_layout.getVBaseClassOffset(base_class_decl) |
| 9128 | .getQuantity() * |
| 9129 | 8; |
| 9130 | else |
| 9131 | field_bit_offset = record_layout.getBaseClassOffset(base_class_decl) |
| 9132 | .getQuantity() * |
| 9133 | 8; |
| 9134 | field_byte_offset = field_bit_offset / 8; |
| 9135 | assert(field_bit_offset % 8 == 0); |
| 9136 | if (child_idx == 0) |
| 9137 | s->PutChar('{'); |
| 9138 | else |
| 9139 | s->PutChar(','); |
| 9140 | |
| 9141 | clang::QualType base_class_qual_type = base_class->getType(); |
| 9142 | std::string base_class_type_name(base_class_qual_type.getAsString()); |
| 9143 | |
| 9144 | // Indent and print the base class type name |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 9145 | s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT), |
| 9146 | base_class_type_name); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9147 | |
| 9148 | clang::TypeInfo base_class_type_info = |
| 9149 | getASTContext()->getTypeInfo(base_class_qual_type); |
| 9150 | |
| 9151 | // Dump the value of the member |
| 9152 | CompilerType base_clang_type(getASTContext(), base_class_qual_type); |
| 9153 | base_clang_type.DumpValue( |
| 9154 | exe_ctx, |
| 9155 | s, // Stream to dump to |
| 9156 | base_clang_type |
| 9157 | .GetFormat(), // The format with which to display the member |
| 9158 | data, // Data buffer containing all bytes for this type |
| 9159 | data_byte_offset + field_byte_offset, // Offset into "data" where |
| 9160 | // to grab value from |
| 9161 | base_class_type_info.Width / 8, // Size of this type in bytes |
| 9162 | 0, // Bitfield bit size |
| 9163 | 0, // Bitfield bit offset |
| 9164 | show_types, // Boolean indicating if we should show the variable |
| 9165 | // types |
| 9166 | show_summary, // Boolean indicating if we should show a summary |
| 9167 | // for the current type |
| 9168 | verbose, // Verbose output? |
| 9169 | depth + DEPTH_INCREMENT); // Scope depth for any types that have |
| 9170 | // children |
| 9171 | |
| 9172 | ++child_idx; |
| 9173 | } |
| 9174 | } |
| 9175 | uint32_t field_idx = 0; |
| 9176 | clang::RecordDecl::field_iterator field, field_end; |
| 9177 | for (field = record_decl->field_begin(), |
| 9178 | field_end = record_decl->field_end(); |
| 9179 | field != field_end; ++field, ++field_idx, ++child_idx) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 9180 | // Print the starting squiggly bracket (if this is the first member) or |
| 9181 | // comma (for member 2 and beyond) for the struct/union/class member. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9182 | if (child_idx == 0) |
| 9183 | s->PutChar('{'); |
| 9184 | else |
| 9185 | s->PutChar(','); |
| 9186 | |
| 9187 | // Indent |
| 9188 | s->Printf("\n%*s", depth + DEPTH_INCREMENT, ""); |
| 9189 | |
| 9190 | clang::QualType field_type = field->getType(); |
| 9191 | // Print the member type if requested |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 9192 | // Figure out the type byte size (field_type_info.first) and alignment |
| 9193 | // (field_type_info.second) from the AST context. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9194 | clang::TypeInfo field_type_info = |
| 9195 | getASTContext()->getTypeInfo(field_type); |
| 9196 | assert(field_idx < record_layout.getFieldCount()); |
| 9197 | // Figure out the field offset within the current struct/union/class |
| 9198 | // type |
| 9199 | field_bit_offset = record_layout.getFieldOffset(field_idx); |
| 9200 | field_byte_offset = field_bit_offset / 8; |
| 9201 | uint32_t field_bitfield_bit_size = 0; |
| 9202 | uint32_t field_bitfield_bit_offset = 0; |
| 9203 | if (ClangASTContext::FieldIsBitfield(getASTContext(), *field, |
| 9204 | field_bitfield_bit_size)) |
| 9205 | field_bitfield_bit_offset = field_bit_offset % 8; |
| 9206 | |
| 9207 | if (show_types) { |
| 9208 | std::string field_type_name(field_type.getAsString()); |
| 9209 | if (field_bitfield_bit_size > 0) |
| 9210 | s->Printf("(%s:%u) ", field_type_name.c_str(), |
| 9211 | field_bitfield_bit_size); |
| 9212 | else |
| 9213 | s->Printf("(%s) ", field_type_name.c_str()); |
| 9214 | } |
| 9215 | // Print the member name and equal sign |
| 9216 | s->Printf("%s = ", field->getNameAsString().c_str()); |
| 9217 | |
| 9218 | // Dump the value of the member |
| 9219 | CompilerType field_clang_type(getASTContext(), field_type); |
| 9220 | field_clang_type.DumpValue( |
| 9221 | exe_ctx, |
| 9222 | s, // Stream to dump to |
| 9223 | field_clang_type |
| 9224 | .GetFormat(), // The format with which to display the member |
| 9225 | data, // Data buffer containing all bytes for this type |
| 9226 | data_byte_offset + field_byte_offset, // Offset into "data" where to |
| 9227 | // grab value from |
| 9228 | field_type_info.Width / 8, // Size of this type in bytes |
| 9229 | field_bitfield_bit_size, // Bitfield bit size |
| 9230 | field_bitfield_bit_offset, // Bitfield bit offset |
| 9231 | show_types, // Boolean indicating if we should show the variable |
| 9232 | // types |
| 9233 | show_summary, // Boolean indicating if we should show a summary for |
| 9234 | // the current type |
| 9235 | verbose, // Verbose output? |
| 9236 | depth + DEPTH_INCREMENT); // Scope depth for any types that have |
| 9237 | // children |
| 9238 | } |
| 9239 | |
| 9240 | // Indent the trailing squiggly bracket |
| 9241 | if (child_idx > 0) |
| 9242 | s->Printf("\n%*s}", depth, ""); |
| 9243 | } |
| 9244 | return; |
| 9245 | |
| 9246 | case clang::Type::Enum: |
| 9247 | if (GetCompleteType(type)) { |
| 9248 | const clang::EnumType *enutype = |
| 9249 | llvm::cast<clang::EnumType>(qual_type.getTypePtr()); |
| 9250 | const clang::EnumDecl *enum_decl = enutype->getDecl(); |
| 9251 | assert(enum_decl); |
| 9252 | clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos; |
| 9253 | lldb::offset_t offset = data_byte_offset; |
| 9254 | const int64_t enum_value = data.GetMaxU64Bitfield( |
| 9255 | &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset); |
| 9256 | for (enum_pos = enum_decl->enumerator_begin(), |
| 9257 | enum_end_pos = enum_decl->enumerator_end(); |
| 9258 | enum_pos != enum_end_pos; ++enum_pos) { |
| 9259 | if (enum_pos->getInitVal() == enum_value) { |
| 9260 | s->Printf("%s", enum_pos->getNameAsString().c_str()); |
| 9261 | return; |
| 9262 | } |
| 9263 | } |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 9264 | // If we have gotten here we didn't get find the enumerator in the enum |
| 9265 | // decl, so just print the integer. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9266 | s->Printf("%" PRIi64, enum_value); |
| 9267 | } |
| 9268 | return; |
| 9269 | |
| 9270 | case clang::Type::ConstantArray: { |
| 9271 | const clang::ConstantArrayType *array = |
| 9272 | llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr()); |
| 9273 | bool is_array_of_characters = false; |
| 9274 | clang::QualType element_qual_type = array->getElementType(); |
| 9275 | |
| 9276 | const clang::Type *canonical_type = |
| 9277 | element_qual_type->getCanonicalTypeInternal().getTypePtr(); |
| 9278 | if (canonical_type) |
| 9279 | is_array_of_characters = canonical_type->isCharType(); |
| 9280 | |
| 9281 | const uint64_t element_count = array->getSize().getLimitedValue(); |
| 9282 | |
| 9283 | clang::TypeInfo field_type_info = |
| 9284 | getASTContext()->getTypeInfo(element_qual_type); |
| 9285 | |
| 9286 | uint32_t element_idx = 0; |
| 9287 | uint32_t element_offset = 0; |
| 9288 | uint64_t element_byte_size = field_type_info.Width / 8; |
| 9289 | uint32_t element_stride = element_byte_size; |
| 9290 | |
| 9291 | if (is_array_of_characters) { |
| 9292 | s->PutChar('"'); |
Zachary Turner | 29cb868 | 2017-03-03 20:57:05 +0000 | [diff] [blame] | 9293 | DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar, |
| 9294 | element_byte_size, element_count, UINT32_MAX, |
| 9295 | LLDB_INVALID_ADDRESS, 0, 0); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9296 | s->PutChar('"'); |
| 9297 | return; |
| 9298 | } else { |
| 9299 | CompilerType element_clang_type(getASTContext(), element_qual_type); |
| 9300 | lldb::Format element_format = element_clang_type.GetFormat(); |
| 9301 | |
| 9302 | for (element_idx = 0; element_idx < element_count; ++element_idx) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 9303 | // Print the starting squiggly bracket (if this is the first member) or |
| 9304 | // comman (for member 2 and beyong) for the struct/union/class member. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9305 | if (element_idx == 0) |
| 9306 | s->PutChar('{'); |
| 9307 | else |
| 9308 | s->PutChar(','); |
| 9309 | |
| 9310 | // Indent and print the index |
| 9311 | s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx); |
| 9312 | |
| 9313 | // Figure out the field offset within the current struct/union/class |
| 9314 | // type |
| 9315 | element_offset = element_idx * element_stride; |
| 9316 | |
| 9317 | // Dump the value of the member |
| 9318 | element_clang_type.DumpValue( |
| 9319 | exe_ctx, |
| 9320 | s, // Stream to dump to |
| 9321 | element_format, // The format with which to display the element |
| 9322 | data, // Data buffer containing all bytes for this type |
| 9323 | data_byte_offset + |
| 9324 | element_offset, // Offset into "data" where to grab value from |
| 9325 | element_byte_size, // Size of this type in bytes |
| 9326 | 0, // Bitfield bit size |
| 9327 | 0, // Bitfield bit offset |
| 9328 | show_types, // Boolean indicating if we should show the variable |
| 9329 | // types |
| 9330 | show_summary, // Boolean indicating if we should show a summary for |
| 9331 | // the current type |
| 9332 | verbose, // Verbose output? |
| 9333 | depth + DEPTH_INCREMENT); // Scope depth for any types that have |
| 9334 | // children |
| 9335 | } |
| 9336 | |
| 9337 | // Indent the trailing squiggly bracket |
| 9338 | if (element_idx > 0) |
| 9339 | s->Printf("\n%*s}", depth, ""); |
| 9340 | } |
| 9341 | } |
| 9342 | return; |
| 9343 | |
| 9344 | case clang::Type::Typedef: { |
| 9345 | clang::QualType typedef_qual_type = |
| 9346 | llvm::cast<clang::TypedefType>(qual_type) |
| 9347 | ->getDecl() |
| 9348 | ->getUnderlyingType(); |
| 9349 | |
| 9350 | CompilerType typedef_clang_type(getASTContext(), typedef_qual_type); |
| 9351 | lldb::Format typedef_format = typedef_clang_type.GetFormat(); |
| 9352 | clang::TypeInfo typedef_type_info = |
| 9353 | getASTContext()->getTypeInfo(typedef_qual_type); |
| 9354 | uint64_t typedef_byte_size = typedef_type_info.Width / 8; |
| 9355 | |
| 9356 | return typedef_clang_type.DumpValue( |
| 9357 | exe_ctx, |
| 9358 | s, // Stream to dump to |
| 9359 | typedef_format, // The format with which to display the element |
| 9360 | data, // Data buffer containing all bytes for this type |
| 9361 | data_byte_offset, // Offset into "data" where to grab value from |
| 9362 | typedef_byte_size, // Size of this type in bytes |
| 9363 | bitfield_bit_size, // Bitfield bit size |
| 9364 | bitfield_bit_offset, // Bitfield bit offset |
| 9365 | show_types, // Boolean indicating if we should show the variable types |
| 9366 | show_summary, // Boolean indicating if we should show a summary for the |
| 9367 | // current type |
| 9368 | verbose, // Verbose output? |
| 9369 | depth); // Scope depth for any types that have children |
| 9370 | } break; |
| 9371 | |
| 9372 | case clang::Type::Auto: { |
| 9373 | clang::QualType elaborated_qual_type = |
| 9374 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType(); |
| 9375 | CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type); |
| 9376 | lldb::Format elaborated_format = elaborated_clang_type.GetFormat(); |
| 9377 | clang::TypeInfo elaborated_type_info = |
| 9378 | getASTContext()->getTypeInfo(elaborated_qual_type); |
| 9379 | uint64_t elaborated_byte_size = elaborated_type_info.Width / 8; |
| 9380 | |
| 9381 | return elaborated_clang_type.DumpValue( |
| 9382 | exe_ctx, |
| 9383 | s, // Stream to dump to |
| 9384 | elaborated_format, // The format with which to display the element |
| 9385 | data, // Data buffer containing all bytes for this type |
| 9386 | data_byte_offset, // Offset into "data" where to grab value from |
| 9387 | elaborated_byte_size, // Size of this type in bytes |
| 9388 | bitfield_bit_size, // Bitfield bit size |
| 9389 | bitfield_bit_offset, // Bitfield bit offset |
| 9390 | show_types, // Boolean indicating if we should show the variable types |
| 9391 | show_summary, // Boolean indicating if we should show a summary for the |
| 9392 | // current type |
| 9393 | verbose, // Verbose output? |
| 9394 | depth); // Scope depth for any types that have children |
| 9395 | } break; |
| 9396 | |
| 9397 | case clang::Type::Elaborated: { |
| 9398 | clang::QualType elaborated_qual_type = |
| 9399 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(); |
| 9400 | CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type); |
| 9401 | lldb::Format elaborated_format = elaborated_clang_type.GetFormat(); |
| 9402 | clang::TypeInfo elaborated_type_info = |
| 9403 | getASTContext()->getTypeInfo(elaborated_qual_type); |
| 9404 | uint64_t elaborated_byte_size = elaborated_type_info.Width / 8; |
| 9405 | |
| 9406 | return elaborated_clang_type.DumpValue( |
| 9407 | exe_ctx, |
| 9408 | s, // Stream to dump to |
| 9409 | elaborated_format, // The format with which to display the element |
| 9410 | data, // Data buffer containing all bytes for this type |
| 9411 | data_byte_offset, // Offset into "data" where to grab value from |
| 9412 | elaborated_byte_size, // Size of this type in bytes |
| 9413 | bitfield_bit_size, // Bitfield bit size |
| 9414 | bitfield_bit_offset, // Bitfield bit offset |
| 9415 | show_types, // Boolean indicating if we should show the variable types |
| 9416 | show_summary, // Boolean indicating if we should show a summary for the |
| 9417 | // current type |
| 9418 | verbose, // Verbose output? |
| 9419 | depth); // Scope depth for any types that have children |
| 9420 | } break; |
| 9421 | |
| 9422 | case clang::Type::Paren: { |
| 9423 | clang::QualType desugar_qual_type = |
| 9424 | llvm::cast<clang::ParenType>(qual_type)->desugar(); |
| 9425 | CompilerType desugar_clang_type(getASTContext(), desugar_qual_type); |
| 9426 | |
| 9427 | lldb::Format desugar_format = desugar_clang_type.GetFormat(); |
| 9428 | clang::TypeInfo desugar_type_info = |
| 9429 | getASTContext()->getTypeInfo(desugar_qual_type); |
| 9430 | uint64_t desugar_byte_size = desugar_type_info.Width / 8; |
| 9431 | |
| 9432 | return desugar_clang_type.DumpValue( |
| 9433 | exe_ctx, |
| 9434 | s, // Stream to dump to |
| 9435 | desugar_format, // The format with which to display the element |
| 9436 | data, // Data buffer containing all bytes for this type |
| 9437 | data_byte_offset, // Offset into "data" where to grab value from |
| 9438 | desugar_byte_size, // Size of this type in bytes |
| 9439 | bitfield_bit_size, // Bitfield bit size |
| 9440 | bitfield_bit_offset, // Bitfield bit offset |
| 9441 | show_types, // Boolean indicating if we should show the variable types |
| 9442 | show_summary, // Boolean indicating if we should show a summary for the |
| 9443 | // current type |
| 9444 | verbose, // Verbose output? |
| 9445 | depth); // Scope depth for any types that have children |
| 9446 | } break; |
| 9447 | |
| 9448 | default: |
| 9449 | // 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] | 9450 | DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1, |
| 9451 | UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size, |
| 9452 | bitfield_bit_offset); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9453 | |
| 9454 | if (show_summary) |
| 9455 | DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size); |
| 9456 | break; |
| 9457 | } |
| 9458 | } |
| 9459 | |
| 9460 | bool ClangASTContext::DumpTypeValue( |
| 9461 | lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format, |
Zachary Turner | 29cb868 | 2017-03-03 20:57:05 +0000 | [diff] [blame] | 9462 | const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size, |
| 9463 | uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9464 | ExecutionContextScope *exe_scope) { |
| 9465 | if (!type) |
| 9466 | return false; |
| 9467 | if (IsAggregateType(type)) { |
| 9468 | return false; |
| 9469 | } else { |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9470 | clang::QualType qual_type(GetQualType(type)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9471 | |
| 9472 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 9473 | switch (type_class) { |
| 9474 | case clang::Type::Typedef: { |
| 9475 | clang::QualType typedef_qual_type = |
| 9476 | llvm::cast<clang::TypedefType>(qual_type) |
| 9477 | ->getDecl() |
| 9478 | ->getUnderlyingType(); |
| 9479 | CompilerType typedef_clang_type(getASTContext(), typedef_qual_type); |
| 9480 | if (format == eFormatDefault) |
| 9481 | format = typedef_clang_type.GetFormat(); |
| 9482 | clang::TypeInfo typedef_type_info = |
| 9483 | getASTContext()->getTypeInfo(typedef_qual_type); |
| 9484 | uint64_t typedef_byte_size = typedef_type_info.Width / 8; |
| 9485 | |
| 9486 | return typedef_clang_type.DumpTypeValue( |
| 9487 | s, |
| 9488 | format, // The format with which to display the element |
| 9489 | data, // Data buffer containing all bytes for this type |
| 9490 | byte_offset, // Offset into "data" where to grab value from |
| 9491 | typedef_byte_size, // Size of this type in bytes |
| 9492 | bitfield_bit_size, // Size in bits of a bitfield value, if zero don't |
| 9493 | // treat as a bitfield |
| 9494 | bitfield_bit_offset, // Offset in bits of a bitfield value if |
| 9495 | // bitfield_bit_size != 0 |
| 9496 | exe_scope); |
| 9497 | } break; |
| 9498 | |
| 9499 | case clang::Type::Enum: |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 9500 | // If our format is enum or default, show the enumeration value as its |
| 9501 | // enumeration string value, else just display it as requested. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9502 | if ((format == eFormatEnum || format == eFormatDefault) && |
| 9503 | GetCompleteType(type)) { |
| 9504 | const clang::EnumType *enutype = |
| 9505 | llvm::cast<clang::EnumType>(qual_type.getTypePtr()); |
| 9506 | const clang::EnumDecl *enum_decl = enutype->getDecl(); |
| 9507 | assert(enum_decl); |
| 9508 | clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos; |
| 9509 | const bool is_signed = qual_type->isSignedIntegerOrEnumerationType(); |
| 9510 | lldb::offset_t offset = byte_offset; |
| 9511 | if (is_signed) { |
| 9512 | const int64_t enum_svalue = data.GetMaxS64Bitfield( |
| 9513 | &offset, byte_size, bitfield_bit_size, bitfield_bit_offset); |
| 9514 | for (enum_pos = enum_decl->enumerator_begin(), |
| 9515 | enum_end_pos = enum_decl->enumerator_end(); |
| 9516 | enum_pos != enum_end_pos; ++enum_pos) { |
| 9517 | if (enum_pos->getInitVal().getSExtValue() == enum_svalue) { |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 9518 | s->PutCString(enum_pos->getNameAsString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9519 | return true; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9520 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9521 | } |
| 9522 | // If we have gotten here we didn't get find the enumerator in the |
| 9523 | // enum decl, so just print the integer. |
| 9524 | s->Printf("%" PRIi64, enum_svalue); |
| 9525 | } else { |
| 9526 | const uint64_t enum_uvalue = data.GetMaxU64Bitfield( |
| 9527 | &offset, byte_size, bitfield_bit_size, bitfield_bit_offset); |
| 9528 | for (enum_pos = enum_decl->enumerator_begin(), |
| 9529 | enum_end_pos = enum_decl->enumerator_end(); |
| 9530 | enum_pos != enum_end_pos; ++enum_pos) { |
| 9531 | if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) { |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 9532 | s->PutCString(enum_pos->getNameAsString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9533 | return true; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9534 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9535 | } |
| 9536 | // If we have gotten here we didn't get find the enumerator in the |
| 9537 | // enum decl, so just print the integer. |
| 9538 | s->Printf("%" PRIu64, enum_uvalue); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9539 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9540 | return true; |
| 9541 | } |
| 9542 | // format was not enum, just fall through and dump the value as |
| 9543 | // requested.... |
| 9544 | LLVM_FALLTHROUGH; |
| 9545 | |
| 9546 | default: |
| 9547 | // We are down to a scalar type that we just need to display. |
| 9548 | { |
| 9549 | uint32_t item_count = 1; |
| 9550 | // A few formats, we might need to modify our size and count for |
| 9551 | // depending |
| 9552 | // on how we are trying to display the value... |
| 9553 | switch (format) { |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9554 | default: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9555 | case eFormatBoolean: |
| 9556 | case eFormatBinary: |
| 9557 | case eFormatComplex: |
| 9558 | case eFormatCString: // NULL terminated C strings |
| 9559 | case eFormatDecimal: |
| 9560 | case eFormatEnum: |
| 9561 | case eFormatHex: |
| 9562 | case eFormatHexUppercase: |
| 9563 | case eFormatFloat: |
| 9564 | case eFormatOctal: |
| 9565 | case eFormatOSType: |
| 9566 | case eFormatUnsigned: |
| 9567 | case eFormatPointer: |
| 9568 | case eFormatVectorOfChar: |
| 9569 | case eFormatVectorOfSInt8: |
| 9570 | case eFormatVectorOfUInt8: |
| 9571 | case eFormatVectorOfSInt16: |
| 9572 | case eFormatVectorOfUInt16: |
| 9573 | case eFormatVectorOfSInt32: |
| 9574 | case eFormatVectorOfUInt32: |
| 9575 | case eFormatVectorOfSInt64: |
| 9576 | case eFormatVectorOfUInt64: |
| 9577 | case eFormatVectorOfFloat32: |
| 9578 | case eFormatVectorOfFloat64: |
| 9579 | case eFormatVectorOfUInt128: |
| 9580 | break; |
| 9581 | |
| 9582 | case eFormatChar: |
| 9583 | case eFormatCharPrintable: |
| 9584 | case eFormatCharArray: |
| 9585 | case eFormatBytes: |
| 9586 | case eFormatBytesWithASCII: |
| 9587 | item_count = byte_size; |
| 9588 | byte_size = 1; |
| 9589 | break; |
| 9590 | |
| 9591 | case eFormatUnicode16: |
| 9592 | item_count = byte_size / 2; |
| 9593 | byte_size = 2; |
| 9594 | break; |
| 9595 | |
| 9596 | case eFormatUnicode32: |
| 9597 | item_count = byte_size / 4; |
| 9598 | byte_size = 4; |
| 9599 | break; |
| 9600 | } |
Zachary Turner | 29cb868 | 2017-03-03 20:57:05 +0000 | [diff] [blame] | 9601 | return DumpDataExtractor(data, s, byte_offset, format, byte_size, |
| 9602 | item_count, UINT32_MAX, LLDB_INVALID_ADDRESS, |
| 9603 | bitfield_bit_size, bitfield_bit_offset, |
| 9604 | exe_scope); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9605 | } |
| 9606 | break; |
| 9607 | } |
| 9608 | } |
| 9609 | return 0; |
| 9610 | } |
| 9611 | |
| 9612 | void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type, |
| 9613 | ExecutionContext *exe_ctx, Stream *s, |
| 9614 | const lldb_private::DataExtractor &data, |
| 9615 | lldb::offset_t data_byte_offset, |
| 9616 | size_t data_byte_size) { |
| 9617 | uint32_t length = 0; |
| 9618 | if (IsCStringType(type, length)) { |
| 9619 | if (exe_ctx) { |
| 9620 | Process *process = exe_ctx->GetProcessPtr(); |
| 9621 | if (process) { |
| 9622 | lldb::offset_t offset = data_byte_offset; |
| 9623 | lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size); |
| 9624 | std::vector<uint8_t> buf; |
| 9625 | if (length > 0) |
| 9626 | buf.resize(length); |
| 9627 | else |
| 9628 | buf.resize(256); |
| 9629 | |
Zachary Turner | 29cb868 | 2017-03-03 20:57:05 +0000 | [diff] [blame] | 9630 | DataExtractor cstr_data(&buf.front(), buf.size(), |
| 9631 | process->GetByteOrder(), 4); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9632 | buf.back() = '\0'; |
| 9633 | size_t bytes_read; |
| 9634 | size_t total_cstr_len = 0; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 9635 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9636 | while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(), |
| 9637 | buf.size(), error)) > 0) { |
| 9638 | const size_t len = strlen((const char *)&buf.front()); |
| 9639 | if (len == 0) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9640 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9641 | if (total_cstr_len == 0) |
| 9642 | s->PutCString(" \""); |
Zachary Turner | 29cb868 | 2017-03-03 20:57:05 +0000 | [diff] [blame] | 9643 | DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len, |
| 9644 | UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9645 | total_cstr_len += len; |
| 9646 | if (len < buf.size()) |
| 9647 | break; |
| 9648 | pointer_address += total_cstr_len; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9649 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9650 | if (total_cstr_len > 0) |
| 9651 | s->PutChar('"'); |
| 9652 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9653 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9654 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9655 | } |
| 9656 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9657 | void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) { |
| 9658 | StreamFile s(stdout, false); |
| 9659 | DumpTypeDescription(type, &s); |
| 9660 | ClangASTMetadata *metadata = |
| 9661 | ClangASTContext::GetMetadata(getASTContext(), type); |
| 9662 | if (metadata) { |
| 9663 | metadata->Dump(&s); |
| 9664 | } |
| 9665 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9666 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9667 | void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type, |
| 9668 | Stream *s) { |
| 9669 | if (type) { |
| 9670 | clang::QualType qual_type(GetQualType(type)); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9671 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9672 | llvm::SmallVector<char, 1024> buf; |
| 9673 | llvm::raw_svector_ostream llvm_ostrm(buf); |
| 9674 | |
| 9675 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 9676 | switch (type_class) { |
| 9677 | case clang::Type::ObjCObject: |
| 9678 | case clang::Type::ObjCInterface: { |
| 9679 | GetCompleteType(type); |
| 9680 | |
| 9681 | const clang::ObjCObjectType *objc_class_type = |
| 9682 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 9683 | assert(objc_class_type); |
| 9684 | if (objc_class_type) { |
| 9685 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 9686 | objc_class_type->getInterface(); |
| 9687 | if (class_interface_decl) { |
| 9688 | clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy(); |
| 9689 | class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel()); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9690 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9691 | } |
| 9692 | } break; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9693 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9694 | case clang::Type::Typedef: { |
| 9695 | const clang::TypedefType *typedef_type = |
| 9696 | qual_type->getAs<clang::TypedefType>(); |
| 9697 | if (typedef_type) { |
| 9698 | const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl(); |
| 9699 | std::string clang_typedef_name( |
| 9700 | typedef_decl->getQualifiedNameAsString()); |
| 9701 | if (!clang_typedef_name.empty()) { |
| 9702 | s->PutCString("typedef "); |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 9703 | s->PutCString(clang_typedef_name); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9704 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9705 | } |
| 9706 | } break; |
| 9707 | |
| 9708 | case clang::Type::Auto: |
| 9709 | CompilerType(getASTContext(), |
| 9710 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()) |
| 9711 | .DumpTypeDescription(s); |
| 9712 | return; |
| 9713 | |
| 9714 | case clang::Type::Elaborated: |
| 9715 | CompilerType(getASTContext(), |
| 9716 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()) |
| 9717 | .DumpTypeDescription(s); |
| 9718 | return; |
| 9719 | |
| 9720 | case clang::Type::Paren: |
| 9721 | CompilerType(getASTContext(), |
| 9722 | llvm::cast<clang::ParenType>(qual_type)->desugar()) |
| 9723 | .DumpTypeDescription(s); |
| 9724 | return; |
| 9725 | |
| 9726 | case clang::Type::Record: { |
| 9727 | GetCompleteType(type); |
| 9728 | |
| 9729 | const clang::RecordType *record_type = |
| 9730 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 9731 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 9732 | const clang::CXXRecordDecl *cxx_record_decl = |
| 9733 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 9734 | |
| 9735 | if (cxx_record_decl) |
| 9736 | cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), |
| 9737 | s->GetIndentLevel()); |
| 9738 | else |
| 9739 | record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), |
| 9740 | s->GetIndentLevel()); |
| 9741 | } break; |
| 9742 | |
| 9743 | default: { |
| 9744 | const clang::TagType *tag_type = |
| 9745 | llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()); |
| 9746 | if (tag_type) { |
| 9747 | clang::TagDecl *tag_decl = tag_type->getDecl(); |
| 9748 | if (tag_decl) |
| 9749 | tag_decl->print(llvm_ostrm, 0); |
| 9750 | } else { |
| 9751 | std::string clang_type_name(qual_type.getAsString()); |
| 9752 | if (!clang_type_name.empty()) |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 9753 | s->PutCString(clang_type_name); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9754 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9755 | } |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 9756 | } |
| 9757 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9758 | if (buf.size() > 0) { |
| 9759 | s->Write(buf.data(), buf.size()); |
Greg Clayton | 8b4edba | 2015-08-14 20:02:05 +0000 | [diff] [blame] | 9760 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9761 | } |
Greg Clayton | 8b4edba | 2015-08-14 20:02:05 +0000 | [diff] [blame] | 9762 | } |
| 9763 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9764 | void ClangASTContext::DumpTypeName(const CompilerType &type) { |
| 9765 | if (ClangUtil::IsClangType(type)) { |
| 9766 | clang::QualType qual_type( |
| 9767 | ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type))); |
| 9768 | |
| 9769 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 9770 | switch (type_class) { |
| 9771 | case clang::Type::Record: { |
| 9772 | const clang::CXXRecordDecl *cxx_record_decl = |
| 9773 | qual_type->getAsCXXRecordDecl(); |
| 9774 | if (cxx_record_decl) |
| 9775 | printf("class %s", cxx_record_decl->getName().str().c_str()); |
| 9776 | } break; |
| 9777 | |
| 9778 | case clang::Type::Enum: { |
| 9779 | clang::EnumDecl *enum_decl = |
| 9780 | llvm::cast<clang::EnumType>(qual_type)->getDecl(); |
| 9781 | if (enum_decl) { |
| 9782 | printf("enum %s", enum_decl->getName().str().c_str()); |
| 9783 | } |
| 9784 | } break; |
| 9785 | |
| 9786 | case clang::Type::ObjCObject: |
| 9787 | case clang::Type::ObjCInterface: { |
| 9788 | const clang::ObjCObjectType *objc_class_type = |
| 9789 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type); |
| 9790 | if (objc_class_type) { |
| 9791 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 9792 | objc_class_type->getInterface(); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 9793 | // We currently can't complete objective C types through the newly |
| 9794 | // added ASTContext because it only supports TagDecl objects right |
| 9795 | // now... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9796 | if (class_interface_decl) |
| 9797 | printf("@class %s", class_interface_decl->getName().str().c_str()); |
| 9798 | } |
| 9799 | } break; |
| 9800 | |
| 9801 | case clang::Type::Typedef: |
| 9802 | printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type) |
| 9803 | ->getDecl() |
| 9804 | ->getName() |
| 9805 | .str() |
| 9806 | .c_str()); |
| 9807 | break; |
| 9808 | |
| 9809 | case clang::Type::Auto: |
| 9810 | printf("auto "); |
| 9811 | return DumpTypeName(CompilerType(type.GetTypeSystem(), |
| 9812 | llvm::cast<clang::AutoType>(qual_type) |
| 9813 | ->getDeducedType() |
| 9814 | .getAsOpaquePtr())); |
| 9815 | |
| 9816 | case clang::Type::Elaborated: |
| 9817 | printf("elaborated "); |
| 9818 | return DumpTypeName(CompilerType( |
| 9819 | type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type) |
| 9820 | ->getNamedType() |
| 9821 | .getAsOpaquePtr())); |
| 9822 | |
| 9823 | case clang::Type::Paren: |
| 9824 | printf("paren "); |
| 9825 | return DumpTypeName(CompilerType( |
| 9826 | type.GetTypeSystem(), |
| 9827 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr())); |
| 9828 | |
| 9829 | default: |
| 9830 | printf("ClangASTContext::DumpTypeName() type_class = %u", type_class); |
| 9831 | break; |
Greg Clayton | 6dc8d58 | 2015-08-18 22:32:36 +0000 | [diff] [blame] | 9832 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9833 | } |
Greg Clayton | 6dc8d58 | 2015-08-18 22:32:36 +0000 | [diff] [blame] | 9834 | } |
| 9835 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9836 | clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl( |
| 9837 | clang::DeclContext *decl_ctx, lldb::AccessType access_type, |
| 9838 | const char *parent_name, int tag_decl_kind, |
| 9839 | const ClangASTContext::TemplateParameterInfos &template_param_infos) { |
| 9840 | if (template_param_infos.IsValid()) { |
| 9841 | std::string template_basename(parent_name); |
| 9842 | template_basename.erase(template_basename.find('<')); |
| 9843 | |
| 9844 | return CreateClassTemplateDecl(decl_ctx, access_type, |
| 9845 | template_basename.c_str(), tag_decl_kind, |
| 9846 | template_param_infos); |
| 9847 | } |
| 9848 | return NULL; |
Greg Clayton | 6dc8d58 | 2015-08-18 22:32:36 +0000 | [diff] [blame] | 9849 | } |
Greg Clayton | 8b4edba | 2015-08-14 20:02:05 +0000 | [diff] [blame] | 9850 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9851 | void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) { |
| 9852 | ClangASTContext *ast = (ClangASTContext *)baton; |
| 9853 | SymbolFile *sym_file = ast->GetSymbolFile(); |
| 9854 | if (sym_file) { |
| 9855 | CompilerType clang_type = GetTypeForDecl(decl); |
| 9856 | if (clang_type) |
| 9857 | sym_file->CompleteType(clang_type); |
| 9858 | } |
Greg Clayton | 261ac3f | 2015-08-28 01:01:03 +0000 | [diff] [blame] | 9859 | } |
| 9860 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9861 | void ClangASTContext::CompleteObjCInterfaceDecl( |
| 9862 | void *baton, clang::ObjCInterfaceDecl *decl) { |
| 9863 | ClangASTContext *ast = (ClangASTContext *)baton; |
| 9864 | SymbolFile *sym_file = ast->GetSymbolFile(); |
| 9865 | if (sym_file) { |
| 9866 | CompilerType clang_type = GetTypeForDecl(decl); |
| 9867 | if (clang_type) |
| 9868 | sym_file->CompleteType(clang_type); |
| 9869 | } |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 9870 | } |
Greg Clayton | 261ac3f | 2015-08-28 01:01:03 +0000 | [diff] [blame] | 9871 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9872 | DWARFASTParser *ClangASTContext::GetDWARFParser() { |
| 9873 | if (!m_dwarf_ast_parser_ap) |
| 9874 | m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this)); |
| 9875 | return m_dwarf_ast_parser_ap.get(); |
| 9876 | } |
| 9877 | |
| 9878 | PDBASTParser *ClangASTContext::GetPDBParser() { |
| 9879 | if (!m_pdb_ast_parser_ap) |
| 9880 | m_pdb_ast_parser_ap.reset(new PDBASTParser(*this)); |
| 9881 | return m_pdb_ast_parser_ap.get(); |
| 9882 | } |
| 9883 | |
| 9884 | bool ClangASTContext::LayoutRecordType( |
| 9885 | void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size, |
| 9886 | uint64_t &alignment, |
| 9887 | llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets, |
| 9888 | llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> |
| 9889 | &base_offsets, |
| 9890 | llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> |
| 9891 | &vbase_offsets) { |
| 9892 | ClangASTContext *ast = (ClangASTContext *)baton; |
Aleksandr Urakov | 7d2a74f | 2018-08-14 07:57:44 +0000 | [diff] [blame] | 9893 | lldb_private::ClangASTImporter *importer = nullptr; |
| 9894 | if (ast->m_dwarf_ast_parser_ap) |
| 9895 | importer = &ast->m_dwarf_ast_parser_ap->GetClangASTImporter(); |
| 9896 | if (!importer && ast->m_pdb_ast_parser_ap) |
| 9897 | importer = &ast->m_pdb_ast_parser_ap->GetClangASTImporter(); |
| 9898 | if (!importer) |
| 9899 | return false; |
| 9900 | |
| 9901 | return importer->LayoutRecordType(record_decl, bit_size, alignment, |
| 9902 | field_offsets, base_offsets, vbase_offsets); |
Greg Clayton | 8b4edba | 2015-08-14 20:02:05 +0000 | [diff] [blame] | 9903 | } |
| 9904 | |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 9905 | //---------------------------------------------------------------------- |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 9906 | // CompilerDecl override functions |
| 9907 | //---------------------------------------------------------------------- |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 9908 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9909 | ConstString ClangASTContext::DeclGetName(void *opaque_decl) { |
| 9910 | if (opaque_decl) { |
| 9911 | clang::NamedDecl *nd = |
| 9912 | llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl); |
| 9913 | if (nd != nullptr) |
| 9914 | return ConstString(nd->getDeclName().getAsString()); |
| 9915 | } |
| 9916 | return ConstString(); |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 9917 | } |
| 9918 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9919 | ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) { |
| 9920 | if (opaque_decl) { |
| 9921 | clang::NamedDecl *nd = |
| 9922 | llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl); |
| 9923 | if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) { |
| 9924 | clang::MangleContext *mc = getMangleContext(); |
| 9925 | if (mc && mc->shouldMangleCXXName(nd)) { |
| 9926 | llvm::SmallVector<char, 1024> buf; |
| 9927 | llvm::raw_svector_ostream llvm_ostrm(buf); |
| 9928 | if (llvm::isa<clang::CXXConstructorDecl>(nd)) { |
| 9929 | mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd), |
| 9930 | Ctor_Complete, llvm_ostrm); |
| 9931 | } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) { |
| 9932 | mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd), |
| 9933 | Dtor_Complete, llvm_ostrm); |
| 9934 | } else { |
| 9935 | mc->mangleName(nd, llvm_ostrm); |
Greg Clayton | fe68904 | 2015-11-10 17:47:04 +0000 | [diff] [blame] | 9936 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9937 | if (buf.size() > 0) |
| 9938 | return ConstString(buf.data(), buf.size()); |
| 9939 | } |
Greg Clayton | fe68904 | 2015-11-10 17:47:04 +0000 | [diff] [blame] | 9940 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9941 | } |
| 9942 | return ConstString(); |
Greg Clayton | fe68904 | 2015-11-10 17:47:04 +0000 | [diff] [blame] | 9943 | } |
| 9944 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9945 | CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) { |
| 9946 | if (opaque_decl) |
| 9947 | return CompilerDeclContext(this, |
| 9948 | ((clang::Decl *)opaque_decl)->getDeclContext()); |
| 9949 | else |
| 9950 | return CompilerDeclContext(); |
Greg Clayton | fe68904 | 2015-11-10 17:47:04 +0000 | [diff] [blame] | 9951 | } |
| 9952 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9953 | CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) { |
| 9954 | if (clang::FunctionDecl *func_decl = |
| 9955 | llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) |
| 9956 | return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr()); |
| 9957 | if (clang::ObjCMethodDecl *objc_method = |
| 9958 | llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl)) |
| 9959 | return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr()); |
| 9960 | else |
Greg Clayton | fe68904 | 2015-11-10 17:47:04 +0000 | [diff] [blame] | 9961 | return CompilerType(); |
| 9962 | } |
| 9963 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9964 | size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) { |
| 9965 | if (clang::FunctionDecl *func_decl = |
| 9966 | llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) |
| 9967 | return func_decl->param_size(); |
| 9968 | if (clang::ObjCMethodDecl *objc_method = |
| 9969 | llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl)) |
| 9970 | return objc_method->param_size(); |
| 9971 | else |
| 9972 | return 0; |
| 9973 | } |
| 9974 | |
| 9975 | CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl, |
| 9976 | size_t idx) { |
| 9977 | if (clang::FunctionDecl *func_decl = |
| 9978 | llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) { |
| 9979 | if (idx < func_decl->param_size()) { |
| 9980 | ParmVarDecl *var_decl = func_decl->getParamDecl(idx); |
| 9981 | if (var_decl) |
| 9982 | return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr()); |
| 9983 | } |
| 9984 | } else if (clang::ObjCMethodDecl *objc_method = |
| 9985 | llvm::dyn_cast<clang::ObjCMethodDecl>( |
| 9986 | (clang::Decl *)opaque_decl)) { |
| 9987 | if (idx < objc_method->param_size()) |
| 9988 | return CompilerType( |
| 9989 | this, |
| 9990 | objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr()); |
| 9991 | } |
| 9992 | return CompilerType(); |
| 9993 | } |
| 9994 | |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 9995 | //---------------------------------------------------------------------- |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 9996 | // CompilerDeclContext functions |
| 9997 | //---------------------------------------------------------------------- |
| 9998 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9999 | std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName( |
| 10000 | void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) { |
| 10001 | std::vector<CompilerDecl> found_decls; |
| 10002 | if (opaque_decl_ctx) { |
| 10003 | DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx; |
| 10004 | std::set<DeclContext *> searched; |
| 10005 | std::multimap<DeclContext *, DeclContext *> search_queue; |
| 10006 | SymbolFile *symbol_file = GetSymbolFile(); |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 10007 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10008 | for (clang::DeclContext *decl_context = root_decl_ctx; |
| 10009 | decl_context != nullptr && found_decls.empty(); |
| 10010 | decl_context = decl_context->getParent()) { |
| 10011 | search_queue.insert(std::make_pair(decl_context, decl_context)); |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 10012 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10013 | for (auto it = search_queue.find(decl_context); it != search_queue.end(); |
| 10014 | it++) { |
| 10015 | if (!searched.insert(it->second).second) |
| 10016 | continue; |
| 10017 | symbol_file->ParseDeclsForContext( |
| 10018 | CompilerDeclContext(this, it->second)); |
Paul Herman | ea188fc | 2015-09-16 18:48:30 +0000 | [diff] [blame] | 10019 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10020 | for (clang::Decl *child : it->second->decls()) { |
| 10021 | if (clang::UsingDirectiveDecl *ud = |
| 10022 | llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) { |
| 10023 | if (ignore_using_decls) |
| 10024 | continue; |
| 10025 | clang::DeclContext *from = ud->getCommonAncestor(); |
| 10026 | if (searched.find(ud->getNominatedNamespace()) == searched.end()) |
| 10027 | search_queue.insert( |
| 10028 | std::make_pair(from, ud->getNominatedNamespace())); |
| 10029 | } else if (clang::UsingDecl *ud = |
| 10030 | llvm::dyn_cast<clang::UsingDecl>(child)) { |
| 10031 | if (ignore_using_decls) |
| 10032 | continue; |
| 10033 | for (clang::UsingShadowDecl *usd : ud->shadows()) { |
| 10034 | clang::Decl *target = usd->getTargetDecl(); |
| 10035 | if (clang::NamedDecl *nd = |
| 10036 | llvm::dyn_cast<clang::NamedDecl>(target)) { |
| 10037 | IdentifierInfo *ii = nd->getIdentifier(); |
| 10038 | if (ii != nullptr && |
| 10039 | ii->getName().equals(name.AsCString(nullptr))) |
| 10040 | found_decls.push_back(CompilerDecl(this, nd)); |
| 10041 | } |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 10042 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10043 | } else if (clang::NamedDecl *nd = |
| 10044 | llvm::dyn_cast<clang::NamedDecl>(child)) { |
| 10045 | IdentifierInfo *ii = nd->getIdentifier(); |
| 10046 | if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr))) |
| 10047 | found_decls.push_back(CompilerDecl(this, nd)); |
| 10048 | } |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 10049 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10050 | } |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 10051 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10052 | } |
| 10053 | return found_decls; |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 10054 | } |
| 10055 | |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 10056 | // 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] | 10057 | // and return the number of levels it took to find it, or |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 10058 | // LLDB_INVALID_DECL_LEVEL if not found. If the decl was imported via a using |
| 10059 | // declaration, its name and/or type, if set, will be used to check that the |
| 10060 | // decl found in the scope is a match. |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 10061 | // |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10062 | // The optional name is required by languages (like C++) to handle using |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 10063 | // declarations like: |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 10064 | // |
| 10065 | // void poo(); |
| 10066 | // namespace ns { |
| 10067 | // void foo(); |
| 10068 | // void goo(); |
| 10069 | // } |
| 10070 | // void bar() { |
| 10071 | // using ns::foo; |
| 10072 | // // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and |
| 10073 | // // LLDB_INVALID_DECL_LEVEL for 'goo'. |
| 10074 | // } |
| 10075 | // |
| 10076 | // The optional type is useful in the case that there's a specific overload |
| 10077 | // that we're looking for that might otherwise be shadowed, like: |
| 10078 | // |
| 10079 | // void foo(int); |
| 10080 | // namespace ns { |
| 10081 | // void foo(); |
| 10082 | // } |
| 10083 | // void bar() { |
| 10084 | // using ns::foo; |
| 10085 | // // CountDeclLevels returns 0 for { 'foo', void() }, |
| 10086 | // // 1 for { 'foo', void(int) }, and |
| 10087 | // // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }. |
| 10088 | // } |
| 10089 | // |
| 10090 | // NOTE: Because file statics are at the TranslationUnit along with globals, a |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10091 | // 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] | 10092 | // scope. Ideally we'd like to treat the file scope as an additional scope just |
| 10093 | // below the global scope. More work needs to be done to recognise that, if |
| 10094 | // the decl we're trying to look up is static, we should compare its source |
| 10095 | // 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] | 10096 | uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx, |
| 10097 | clang::DeclContext *child_decl_ctx, |
| 10098 | ConstString *child_name, |
| 10099 | CompilerType *child_type) { |
| 10100 | if (frame_decl_ctx) { |
| 10101 | std::set<DeclContext *> searched; |
| 10102 | std::multimap<DeclContext *, DeclContext *> search_queue; |
| 10103 | SymbolFile *symbol_file = GetSymbolFile(); |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 10104 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10105 | // Get the lookup scope for the decl we're trying to find. |
| 10106 | clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent(); |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 10107 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10108 | // Look for it in our scope's decl context and its parents. |
| 10109 | uint32_t level = 0; |
| 10110 | for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr; |
| 10111 | decl_ctx = decl_ctx->getParent()) { |
| 10112 | if (!decl_ctx->isLookupContext()) |
| 10113 | continue; |
| 10114 | if (decl_ctx == parent_decl_ctx) |
| 10115 | // Found it! |
| 10116 | return level; |
| 10117 | search_queue.insert(std::make_pair(decl_ctx, decl_ctx)); |
| 10118 | for (auto it = search_queue.find(decl_ctx); it != search_queue.end(); |
| 10119 | it++) { |
| 10120 | if (searched.find(it->second) != searched.end()) |
| 10121 | continue; |
| 10122 | |
| 10123 | // Currently DWARF has one shared translation unit for all Decls at top |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 10124 | // level, so this would erroneously find using statements anywhere. So |
| 10125 | // don't look at the top-level translation unit. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10126 | // TODO fix this and add a testcase that depends on it. |
| 10127 | |
| 10128 | if (llvm::isa<clang::TranslationUnitDecl>(it->second)) |
| 10129 | continue; |
| 10130 | |
| 10131 | searched.insert(it->second); |
| 10132 | symbol_file->ParseDeclsForContext( |
| 10133 | CompilerDeclContext(this, it->second)); |
| 10134 | |
| 10135 | for (clang::Decl *child : it->second->decls()) { |
| 10136 | if (clang::UsingDirectiveDecl *ud = |
| 10137 | llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) { |
| 10138 | clang::DeclContext *ns = ud->getNominatedNamespace(); |
| 10139 | if (ns == parent_decl_ctx) |
| 10140 | // Found it! |
| 10141 | return level; |
| 10142 | clang::DeclContext *from = ud->getCommonAncestor(); |
| 10143 | if (searched.find(ns) == searched.end()) |
| 10144 | search_queue.insert(std::make_pair(from, ns)); |
| 10145 | } else if (child_name) { |
| 10146 | if (clang::UsingDecl *ud = |
| 10147 | llvm::dyn_cast<clang::UsingDecl>(child)) { |
| 10148 | for (clang::UsingShadowDecl *usd : ud->shadows()) { |
| 10149 | clang::Decl *target = usd->getTargetDecl(); |
| 10150 | clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target); |
| 10151 | if (!nd) |
| 10152 | continue; |
| 10153 | // Check names. |
| 10154 | IdentifierInfo *ii = nd->getIdentifier(); |
| 10155 | if (ii == nullptr || |
| 10156 | !ii->getName().equals(child_name->AsCString(nullptr))) |
| 10157 | continue; |
| 10158 | // Check types, if one was provided. |
| 10159 | if (child_type) { |
| 10160 | CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd); |
| 10161 | if (!AreTypesSame(clang_type, *child_type, |
| 10162 | /*ignore_qualifiers=*/true)) |
| 10163 | continue; |
| 10164 | } |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 10165 | // Found it! |
| 10166 | return level; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10167 | } |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 10168 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10169 | } |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 10170 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10171 | } |
| 10172 | ++level; |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 10173 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10174 | } |
| 10175 | return LLDB_INVALID_DECL_LEVEL; |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 10176 | } |
| 10177 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10178 | bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) { |
| 10179 | if (opaque_decl_ctx) |
| 10180 | return ((clang::DeclContext *)opaque_decl_ctx)->isRecord(); |
| 10181 | else |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10182 | return false; |
| 10183 | } |
| 10184 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10185 | ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) { |
| 10186 | if (opaque_decl_ctx) { |
| 10187 | clang::NamedDecl *named_decl = |
| 10188 | llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx); |
| 10189 | if (named_decl) |
| 10190 | return ConstString(named_decl->getName()); |
| 10191 | } |
| 10192 | return ConstString(); |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10193 | } |
| 10194 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10195 | ConstString |
| 10196 | ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) { |
| 10197 | if (opaque_decl_ctx) { |
| 10198 | clang::NamedDecl *named_decl = |
| 10199 | llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx); |
| 10200 | if (named_decl) |
| 10201 | return ConstString( |
| 10202 | llvm::StringRef(named_decl->getQualifiedNameAsString())); |
| 10203 | } |
| 10204 | return ConstString(); |
| 10205 | } |
| 10206 | |
| 10207 | bool ClangASTContext::DeclContextIsClassMethod( |
| 10208 | void *opaque_decl_ctx, lldb::LanguageType *language_ptr, |
| 10209 | bool *is_instance_method_ptr, ConstString *language_object_name_ptr) { |
| 10210 | if (opaque_decl_ctx) { |
| 10211 | clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx; |
| 10212 | if (ObjCMethodDecl *objc_method = |
| 10213 | llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) { |
| 10214 | if (is_instance_method_ptr) |
| 10215 | *is_instance_method_ptr = objc_method->isInstanceMethod(); |
| 10216 | if (language_ptr) |
| 10217 | *language_ptr = eLanguageTypeObjC; |
| 10218 | if (language_object_name_ptr) |
| 10219 | language_object_name_ptr->SetCString("self"); |
| 10220 | return true; |
| 10221 | } else if (CXXMethodDecl *cxx_method = |
| 10222 | llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) { |
| 10223 | if (is_instance_method_ptr) |
| 10224 | *is_instance_method_ptr = cxx_method->isInstance(); |
| 10225 | if (language_ptr) |
| 10226 | *language_ptr = eLanguageTypeC_plus_plus; |
| 10227 | if (language_object_name_ptr) |
| 10228 | language_object_name_ptr->SetCString("this"); |
| 10229 | return true; |
| 10230 | } else if (clang::FunctionDecl *function_decl = |
| 10231 | llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) { |
| 10232 | ClangASTMetadata *metadata = |
| 10233 | GetMetadata(&decl_ctx->getParentASTContext(), function_decl); |
| 10234 | if (metadata && metadata->HasObjectPtr()) { |
| 10235 | if (is_instance_method_ptr) |
| 10236 | *is_instance_method_ptr = true; |
| 10237 | if (language_ptr) |
| 10238 | *language_ptr = eLanguageTypeObjC; |
| 10239 | if (language_object_name_ptr) |
| 10240 | language_object_name_ptr->SetCString(metadata->GetObjectPtrName()); |
| 10241 | return true; |
| 10242 | } |
| 10243 | } |
| 10244 | } |
| 10245 | return false; |
| 10246 | } |
| 10247 | |
| 10248 | clang::DeclContext * |
| 10249 | ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) { |
| 10250 | if (dc.IsClang()) |
| 10251 | return (clang::DeclContext *)dc.GetOpaqueDeclContext(); |
| 10252 | return nullptr; |
| 10253 | } |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10254 | |
| 10255 | ObjCMethodDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10256 | ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) { |
| 10257 | if (dc.IsClang()) |
| 10258 | return llvm::dyn_cast<clang::ObjCMethodDecl>( |
| 10259 | (clang::DeclContext *)dc.GetOpaqueDeclContext()); |
| 10260 | return nullptr; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10261 | } |
| 10262 | |
| 10263 | CXXMethodDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10264 | ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) { |
| 10265 | if (dc.IsClang()) |
| 10266 | return llvm::dyn_cast<clang::CXXMethodDecl>( |
| 10267 | (clang::DeclContext *)dc.GetOpaqueDeclContext()); |
| 10268 | return nullptr; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10269 | } |
| 10270 | |
| 10271 | clang::FunctionDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10272 | ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) { |
| 10273 | if (dc.IsClang()) |
| 10274 | return llvm::dyn_cast<clang::FunctionDecl>( |
| 10275 | (clang::DeclContext *)dc.GetOpaqueDeclContext()); |
| 10276 | return nullptr; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10277 | } |
| 10278 | |
| 10279 | clang::NamespaceDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10280 | ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) { |
| 10281 | if (dc.IsClang()) |
| 10282 | return llvm::dyn_cast<clang::NamespaceDecl>( |
| 10283 | (clang::DeclContext *)dc.GetOpaqueDeclContext()); |
| 10284 | return nullptr; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10285 | } |
| 10286 | |
| 10287 | ClangASTMetadata * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10288 | ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc, |
| 10289 | const void *object) { |
| 10290 | clang::ASTContext *ast = DeclContextGetClangASTContext(dc); |
| 10291 | if (ast) |
| 10292 | return ClangASTContext::GetMetadata(ast, object); |
| 10293 | return nullptr; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10294 | } |
| 10295 | |
| 10296 | clang::ASTContext * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10297 | ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) { |
| 10298 | ClangASTContext *ast = |
| 10299 | llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem()); |
| 10300 | if (ast) |
| 10301 | return ast->getASTContext(); |
| 10302 | return nullptr; |
| 10303 | } |
| 10304 | |
| 10305 | ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target) |
| 10306 | : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()), |
| 10307 | m_target_wp(target.shared_from_this()), |
| 10308 | m_persistent_variables(new ClangPersistentVariables) {} |
| 10309 | |
| 10310 | UserExpression *ClangASTContextForExpressions::GetUserExpression( |
Zachary Turner | c5d7df9 | 2016-11-08 04:52:16 +0000 | [diff] [blame] | 10311 | llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10312 | Expression::ResultType desired_type, |
| 10313 | const EvaluateExpressionOptions &options) { |
| 10314 | TargetSP target_sp = m_target_wp.lock(); |
| 10315 | if (!target_sp) |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10316 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10317 | |
Zachary Turner | c5d7df9 | 2016-11-08 04:52:16 +0000 | [diff] [blame] | 10318 | return new ClangUserExpression(*target_sp.get(), expr, prefix, language, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10319 | desired_type, options); |
Greg Clayton | 8b4edba | 2015-08-14 20:02:05 +0000 | [diff] [blame] | 10320 | } |
| 10321 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10322 | FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller( |
| 10323 | const CompilerType &return_type, const Address &function_address, |
| 10324 | const ValueList &arg_value_list, const char *name) { |
| 10325 | TargetSP target_sp = m_target_wp.lock(); |
| 10326 | if (!target_sp) |
| 10327 | return nullptr; |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 10328 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10329 | Process *process = target_sp->GetProcessSP().get(); |
| 10330 | if (!process) |
| 10331 | return nullptr; |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 10332 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10333 | return new ClangFunctionCaller(*process, return_type, function_address, |
| 10334 | arg_value_list, name); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 10335 | } |
| 10336 | |
| 10337 | UtilityFunction * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10338 | ClangASTContextForExpressions::GetUtilityFunction(const char *text, |
| 10339 | const char *name) { |
| 10340 | TargetSP target_sp = m_target_wp.lock(); |
| 10341 | if (!target_sp) |
| 10342 | return nullptr; |
| 10343 | |
| 10344 | return new ClangUtilityFunction(*target_sp.get(), text, name); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 10345 | } |
Sean Callanan | 8f1f9a1 | 2015-09-30 19:57:57 +0000 | [diff] [blame] | 10346 | |
| 10347 | PersistentExpressionState * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10348 | ClangASTContextForExpressions::GetPersistentExpressionState() { |
| 10349 | return m_persistent_variables.get(); |
Sean Callanan | 8f1f9a1 | 2015-09-30 19:57:57 +0000 | [diff] [blame] | 10350 | } |
Sean Callanan | 68e4423 | 2017-09-28 20:20:25 +0000 | [diff] [blame] | 10351 | |
| 10352 | clang::ExternalASTMerger & |
| 10353 | ClangASTContextForExpressions::GetMergerUnchecked() { |
Eugene Zemtsov | a9d928c | 2017-09-29 03:15:08 +0000 | [diff] [blame] | 10354 | lldbassert(m_scratch_ast_source_ap != nullptr); |
Sean Callanan | 68e4423 | 2017-09-28 20:20:25 +0000 | [diff] [blame] | 10355 | return m_scratch_ast_source_ap->GetMergerUnchecked(); |
| 10356 | } |