Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- ClangASTContext.cpp -------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Eli Friedman | 932197d | 2010-06-13 19:06:42 +0000 | [diff] [blame] | 9 | #include "lldb/Symbol/ClangASTContext.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 10 | |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 11 | #include "llvm/Support/FormatAdapters.h" |
| 12 | #include "llvm/Support/FormatVariadic.h" |
| 13 | |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 14 | #include <mutex> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | #include <string> |
Sean Callanan | fe38c85 | 2015-10-08 23:07:53 +0000 | [diff] [blame] | 16 | #include <vector> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 18 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 19 | // 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] | 20 | // related features using "#ifndef NDEBUG" preprocessor blocks to do one thing |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 21 | // or another. This is bad because it means that if clang was built in release |
| 22 | // mode, it assumes that you are building in release mode which is not always |
| 23 | // the case. You can end up with functions that are defined as empty in header |
| 24 | // files when NDEBUG is not defined, and this can cause link errors with the |
| 25 | // clang .a files that you have since you might be missing functions in the .a |
| 26 | // file. So we have to define NDEBUG when including clang headers to avoid any |
| 27 | // mismatches. This is covered by rdar://problem/8691220 |
| 28 | |
Sean Callanan | 3b1d4f6 | 2011-10-26 17:46:51 +0000 | [diff] [blame] | 29 | #if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF) |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 30 | #define LLDB_DEFINED_NDEBUG_FOR_CLANG |
Sean Callanan | 246549c | 2010-07-08 18:16:16 +0000 | [diff] [blame] | 31 | #define NDEBUG |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 32 | // Need to include assert.h so it is as clang would expect it to be (disabled) |
| 33 | #include <assert.h> |
| 34 | #endif |
| 35 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | #include "clang/AST/ASTContext.h" |
| 37 | #include "clang/AST/ASTImporter.h" |
Greg Clayton | f74c403 | 2012-12-03 18:29:55 +0000 | [diff] [blame] | 38 | #include "clang/AST/Attr.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 39 | #include "clang/AST/CXXInheritance.h" |
Greg Clayton | 8cf0593 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 40 | #include "clang/AST/DeclObjC.h" |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 41 | #include "clang/AST/DeclTemplate.h" |
Greg Clayton | fe68904 | 2015-11-10 17:47:04 +0000 | [diff] [blame] | 42 | #include "clang/AST/Mangle.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | #include "clang/AST/RecordLayout.h" |
| 44 | #include "clang/AST/Type.h" |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 45 | #include "clang/AST/VTableBuilder.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 46 | #include "clang/Basic/Builtins.h" |
Sean Callanan | 7e2863b | 2012-02-06 21:28:03 +0000 | [diff] [blame] | 47 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | #include "clang/Basic/FileManager.h" |
Sean Callanan | 79439e8 | 2010-11-18 02:56:27 +0000 | [diff] [blame] | 49 | #include "clang/Basic/FileSystemOptions.h" |
Rainer Orth | 6ca1707 | 2019-08-05 14:00:43 +0000 | [diff] [blame] | 50 | #include "clang/Basic/LangStandard.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" |
Raphael Isemann | f74a4c1 | 2019-04-30 08:41:35 +0000 | [diff] [blame] | 55 | #include "clang/Sema/Sema.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" |
Alex Langford | 9e86561 | 2019-09-09 23:11:43 +0000 | [diff] [blame] | 68 | #include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h" |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 69 | #include "Plugins/ExpressionParser/Clang/ClangUserExpression.h" |
| 70 | #include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h" |
Pavel Labath | 5f19b90 | 2017-11-13 16:16:33 +0000 | [diff] [blame] | 71 | #include "lldb/Utility/ArchSpec.h" |
Zachary Turner | 01c3243 | 2017-02-14 19:06:07 +0000 | [diff] [blame] | 72 | #include "lldb/Utility/Flags.h" |
| 73 | |
Zachary Turner | 29cb868 | 2017-03-03 20:57:05 +0000 | [diff] [blame] | 74 | #include "lldb/Core/DumpDataExtractor.h" |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 75 | #include "lldb/Core/Module.h" |
| 76 | #include "lldb/Core/PluginManager.h" |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 77 | #include "lldb/Core/StreamFile.h" |
Enrico Granata | 2267ad4 | 2014-09-16 17:28:40 +0000 | [diff] [blame] | 78 | #include "lldb/Core/ThreadSafeDenseMap.h" |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 79 | #include "lldb/Core/UniqueCStringMap.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" |
Jim Ingham | d555bac | 2011-06-24 22:03:24 +0000 | [diff] [blame] | 86 | #include "lldb/Target/ExecutionContext.h" |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 87 | #include "lldb/Target/Language.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 88 | #include "lldb/Target/Process.h" |
| 89 | #include "lldb/Target/Target.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 90 | #include "lldb/Utility/DataExtractor.h" |
Sean Callanan | c530ba9 | 2016-05-02 21:15:31 +0000 | [diff] [blame] | 91 | #include "lldb/Utility/LLDBAssert.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 92 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 93 | #include "lldb/Utility/RegularExpression.h" |
Pavel Labath | d821c99 | 2018-08-07 11:07:21 +0000 | [diff] [blame] | 94 | #include "lldb/Utility/Scalar.h" |
Jim Ingham | d555bac | 2011-06-24 22:03:24 +0000 | [diff] [blame] | 95 | |
Alex Langford | b5701710 | 2019-07-15 22:56:12 +0000 | [diff] [blame] | 96 | #include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" |
Greg Clayton | 261ac3f | 2015-08-28 01:01:03 +0000 | [diff] [blame] | 97 | #include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h" |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 98 | #include "Plugins/SymbolFile/PDB/PDBASTParser.h" |
Greg Clayton | 261ac3f | 2015-08-28 01:01:03 +0000 | [diff] [blame] | 99 | |
Eli Friedman | 932197d | 2010-06-13 19:06:42 +0000 | [diff] [blame] | 100 | #include <stdio.h> |
| 101 | |
Greg Clayton | 1341baf | 2013-07-11 23:36:31 +0000 | [diff] [blame] | 102 | #include <mutex> |
| 103 | |
Greg Clayton | c86103d | 2010-08-05 01:57:25 +0000 | [diff] [blame] | 104 | using namespace lldb; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 105 | using namespace lldb_private; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 106 | using namespace clang; |
Pavel Labath | 6f23a68 | 2019-09-30 13:44:17 +0000 | [diff] [blame] | 107 | using llvm::StringSwitch; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 108 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 109 | namespace { |
Pavel Labath | 65a376f | 2019-08-21 13:11:30 +0000 | [diff] [blame] | 110 | #ifdef LLDB_CONFIGURATION_DEBUG |
Alex Langford | b2232a1 | 2019-08-20 22:06:13 +0000 | [diff] [blame] | 111 | static void VerifyDecl(clang::Decl *decl) { |
| 112 | assert(decl && "VerifyDecl called with nullptr?"); |
| 113 | decl->getAccess(); |
| 114 | } |
Pavel Labath | 65a376f | 2019-08-21 13:11:30 +0000 | [diff] [blame] | 115 | #endif |
Alex Langford | b2232a1 | 2019-08-20 22:06:13 +0000 | [diff] [blame] | 116 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 117 | static inline bool |
| 118 | ClangASTContextSupportsLanguage(lldb::LanguageType language) { |
| 119 | return language == eLanguageTypeUnknown || // Clang is the default type system |
Rainer Orth | 6ca1707 | 2019-08-05 14:00:43 +0000 | [diff] [blame] | 120 | lldb_private::Language::LanguageIsC(language) || |
| 121 | lldb_private::Language::LanguageIsCPlusPlus(language) || |
| 122 | lldb_private::Language::LanguageIsObjC(language) || |
| 123 | lldb_private::Language::LanguageIsPascal(language) || |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 124 | // Use Clang for Rust until there is a proper language plugin for it |
| 125 | language == eLanguageTypeRust || |
Johan Engelen | 0479957 | 2016-11-25 11:01:12 +0000 | [diff] [blame] | 126 | language == eLanguageTypeExtRenderScript || |
| 127 | // 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] | 128 | language == eLanguageTypeD || |
| 129 | // Open Dylan compiler debug info is designed to be Clang-compatible |
| 130 | language == eLanguageTypeDylan; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 131 | } |
Aleksandr Urakov | 7d2a74f | 2018-08-14 07:57:44 +0000 | [diff] [blame] | 132 | |
| 133 | // Checks whether m1 is an overload of m2 (as opposed to an override). This is |
| 134 | // called by addOverridesForMethod to distinguish overrides (which share a |
| 135 | // vtable entry) from overloads (which require distinct entries). |
| 136 | bool isOverload(clang::CXXMethodDecl *m1, clang::CXXMethodDecl *m2) { |
| 137 | // FIXME: This should detect covariant return types, but currently doesn't. |
| 138 | lldbassert(&m1->getASTContext() == &m2->getASTContext() && |
| 139 | "Methods should have the same AST context"); |
| 140 | clang::ASTContext &context = m1->getASTContext(); |
| 141 | |
| 142 | const auto *m1Type = llvm::cast<clang::FunctionProtoType>( |
| 143 | context.getCanonicalType(m1->getType())); |
| 144 | |
| 145 | const auto *m2Type = llvm::cast<clang::FunctionProtoType>( |
| 146 | context.getCanonicalType(m2->getType())); |
| 147 | |
| 148 | auto compareArgTypes = [&context](const clang::QualType &m1p, |
| 149 | const clang::QualType &m2p) { |
| 150 | return context.hasSameType(m1p.getUnqualifiedType(), |
| 151 | m2p.getUnqualifiedType()); |
| 152 | }; |
| 153 | |
| 154 | // FIXME: In C++14 and later, we can just pass m2Type->param_type_end() |
| 155 | // as a fourth parameter to std::equal(). |
| 156 | return (m1->getNumParams() != m2->getNumParams()) || |
| 157 | !std::equal(m1Type->param_type_begin(), m1Type->param_type_end(), |
| 158 | m2Type->param_type_begin(), compareArgTypes); |
| 159 | } |
| 160 | |
| 161 | // If decl is a virtual method, walk the base classes looking for methods that |
| 162 | // decl overrides. This table of overridden methods is used by IRGen to |
| 163 | // determine the vtable layout for decl's parent class. |
| 164 | void addOverridesForMethod(clang::CXXMethodDecl *decl) { |
| 165 | if (!decl->isVirtual()) |
| 166 | return; |
| 167 | |
| 168 | clang::CXXBasePaths paths; |
| 169 | |
| 170 | auto find_overridden_methods = |
| 171 | [decl](const clang::CXXBaseSpecifier *specifier, |
| 172 | clang::CXXBasePath &path) { |
| 173 | if (auto *base_record = llvm::dyn_cast<clang::CXXRecordDecl>( |
| 174 | specifier->getType()->getAs<clang::RecordType>()->getDecl())) { |
| 175 | |
| 176 | clang::DeclarationName name = decl->getDeclName(); |
| 177 | |
| 178 | // If this is a destructor, check whether the base class destructor is |
| 179 | // virtual. |
| 180 | if (name.getNameKind() == clang::DeclarationName::CXXDestructorName) |
| 181 | if (auto *baseDtorDecl = base_record->getDestructor()) { |
| 182 | if (baseDtorDecl->isVirtual()) { |
| 183 | path.Decls = baseDtorDecl; |
| 184 | return true; |
| 185 | } else |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | // Otherwise, search for name in the base class. |
| 190 | for (path.Decls = base_record->lookup(name); !path.Decls.empty(); |
| 191 | path.Decls = path.Decls.slice(1)) { |
| 192 | if (auto *method_decl = |
| 193 | llvm::dyn_cast<clang::CXXMethodDecl>(path.Decls.front())) |
| 194 | if (method_decl->isVirtual() && !isOverload(decl, method_decl)) { |
| 195 | path.Decls = method_decl; |
| 196 | return true; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | return false; |
| 202 | }; |
| 203 | |
| 204 | if (decl->getParent()->lookupInBases(find_overridden_methods, paths)) { |
| 205 | for (auto *overridden_decl : paths.found_decls()) |
| 206 | decl->addOverriddenMethod( |
| 207 | llvm::cast<clang::CXXMethodDecl>(overridden_decl)); |
| 208 | } |
| 209 | } |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Aleksandr Urakov | 1dc51db | 2018-11-12 16:23:50 +0000 | [diff] [blame] | 212 | static lldb::addr_t GetVTableAddress(Process &process, |
| 213 | VTableContextBase &vtable_ctx, |
| 214 | ValueObject &valobj, |
| 215 | const ASTRecordLayout &record_layout) { |
| 216 | // Retrieve type info |
| 217 | CompilerType pointee_type; |
| 218 | CompilerType this_type(valobj.GetCompilerType()); |
| 219 | uint32_t type_info = this_type.GetTypeInfo(&pointee_type); |
| 220 | if (!type_info) |
| 221 | return LLDB_INVALID_ADDRESS; |
| 222 | |
| 223 | // Check if it's a pointer or reference |
| 224 | bool ptr_or_ref = false; |
| 225 | if (type_info & (eTypeIsPointer | eTypeIsReference)) { |
| 226 | ptr_or_ref = true; |
| 227 | type_info = pointee_type.GetTypeInfo(); |
| 228 | } |
| 229 | |
| 230 | // We process only C++ classes |
| 231 | const uint32_t cpp_class = eTypeIsClass | eTypeIsCPlusPlus; |
| 232 | if ((type_info & cpp_class) != cpp_class) |
| 233 | return LLDB_INVALID_ADDRESS; |
| 234 | |
| 235 | // Calculate offset to VTable pointer |
| 236 | lldb::offset_t vbtable_ptr_offset = |
| 237 | vtable_ctx.isMicrosoft() ? record_layout.getVBPtrOffset().getQuantity() |
| 238 | : 0; |
| 239 | |
| 240 | if (ptr_or_ref) { |
| 241 | // We have a pointer / ref to object, so read |
| 242 | // VTable pointer from process memory |
| 243 | |
| 244 | if (valobj.GetAddressTypeOfChildren() != eAddressTypeLoad) |
| 245 | return LLDB_INVALID_ADDRESS; |
| 246 | |
| 247 | auto vbtable_ptr_addr = valobj.GetValueAsUnsigned(LLDB_INVALID_ADDRESS); |
| 248 | if (vbtable_ptr_addr == LLDB_INVALID_ADDRESS) |
| 249 | return LLDB_INVALID_ADDRESS; |
| 250 | |
| 251 | vbtable_ptr_addr += vbtable_ptr_offset; |
| 252 | |
| 253 | Status err; |
| 254 | return process.ReadPointerFromMemory(vbtable_ptr_addr, err); |
| 255 | } |
| 256 | |
| 257 | // We have an object already read from process memory, |
| 258 | // so just extract VTable pointer from it |
| 259 | |
| 260 | DataExtractor data; |
| 261 | Status err; |
| 262 | auto size = valobj.GetData(data, err); |
| 263 | if (err.Fail() || vbtable_ptr_offset + data.GetAddressByteSize() > size) |
| 264 | return LLDB_INVALID_ADDRESS; |
| 265 | |
| 266 | return data.GetPointer(&vbtable_ptr_offset); |
| 267 | } |
| 268 | |
| 269 | static int64_t ReadVBaseOffsetFromVTable(Process &process, |
| 270 | VTableContextBase &vtable_ctx, |
| 271 | lldb::addr_t vtable_ptr, |
| 272 | const CXXRecordDecl *cxx_record_decl, |
| 273 | const CXXRecordDecl *base_class_decl) { |
| 274 | if (vtable_ctx.isMicrosoft()) { |
| 275 | clang::MicrosoftVTableContext &msoft_vtable_ctx = |
| 276 | static_cast<clang::MicrosoftVTableContext &>(vtable_ctx); |
| 277 | |
| 278 | // Get the index into the virtual base table. The |
| 279 | // index is the index in uint32_t from vbtable_ptr |
| 280 | const unsigned vbtable_index = |
| 281 | msoft_vtable_ctx.getVBTableIndex(cxx_record_decl, base_class_decl); |
| 282 | const lldb::addr_t base_offset_addr = vtable_ptr + vbtable_index * 4; |
| 283 | Status err; |
| 284 | return process.ReadSignedIntegerFromMemory(base_offset_addr, 4, INT64_MAX, |
| 285 | err); |
| 286 | } |
| 287 | |
| 288 | clang::ItaniumVTableContext &itanium_vtable_ctx = |
| 289 | static_cast<clang::ItaniumVTableContext &>(vtable_ctx); |
| 290 | |
| 291 | clang::CharUnits base_offset_offset = |
| 292 | itanium_vtable_ctx.getVirtualBaseOffsetOffset(cxx_record_decl, |
| 293 | base_class_decl); |
| 294 | const lldb::addr_t base_offset_addr = |
| 295 | vtable_ptr + base_offset_offset.getQuantity(); |
| 296 | const uint32_t base_offset_size = process.GetAddressByteSize(); |
| 297 | Status err; |
| 298 | return process.ReadSignedIntegerFromMemory(base_offset_addr, base_offset_size, |
| 299 | INT64_MAX, err); |
| 300 | } |
| 301 | |
| 302 | static bool GetVBaseBitOffset(VTableContextBase &vtable_ctx, |
| 303 | ValueObject &valobj, |
| 304 | const ASTRecordLayout &record_layout, |
| 305 | const CXXRecordDecl *cxx_record_decl, |
| 306 | const CXXRecordDecl *base_class_decl, |
| 307 | int32_t &bit_offset) { |
| 308 | ExecutionContext exe_ctx(valobj.GetExecutionContextRef()); |
| 309 | Process *process = exe_ctx.GetProcessPtr(); |
| 310 | if (!process) |
| 311 | return false; |
| 312 | |
| 313 | lldb::addr_t vtable_ptr = |
| 314 | GetVTableAddress(*process, vtable_ctx, valobj, record_layout); |
| 315 | if (vtable_ptr == LLDB_INVALID_ADDRESS) |
| 316 | return false; |
| 317 | |
| 318 | auto base_offset = ReadVBaseOffsetFromVTable( |
| 319 | *process, vtable_ctx, vtable_ptr, cxx_record_decl, base_class_decl); |
| 320 | if (base_offset == INT64_MAX) |
| 321 | return false; |
| 322 | |
| 323 | bit_offset = base_offset * 8; |
| 324 | |
| 325 | return true; |
| 326 | } |
| 327 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 328 | typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext *> |
| 329 | ClangASTMap; |
Enrico Granata | 5d84a69 | 2014-08-19 21:46:37 +0000 | [diff] [blame] | 330 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 331 | static ClangASTMap &GetASTMap() { |
| 332 | static ClangASTMap *g_map_ptr = nullptr; |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 333 | static llvm::once_flag g_once_flag; |
| 334 | llvm::call_once(g_once_flag, []() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 335 | g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins |
| 336 | }); |
| 337 | return *g_map_ptr; |
Enrico Granata | 5d84a69 | 2014-08-19 21:46:37 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Raphael Isemann | 92d5ea5d | 2019-11-27 10:27:25 +0100 | [diff] [blame] | 340 | char ClangASTContext::ID; |
| 341 | |
Raphael Isemann | 2f323fc | 2019-08-28 13:46:01 +0000 | [diff] [blame] | 342 | bool ClangASTContext::IsOperator(llvm::StringRef name, |
Davide Italiano | 7e3ef4d | 2018-03-20 19:46:32 +0000 | [diff] [blame] | 343 | clang::OverloadedOperatorKind &op_kind) { |
Raphael Isemann | 2f323fc | 2019-08-28 13:46:01 +0000 | [diff] [blame] | 344 | // All operators have to start with "operator". |
| 345 | if (!name.consume_front("operator")) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 346 | return false; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 347 | |
Raphael Isemann | 2f323fc | 2019-08-28 13:46:01 +0000 | [diff] [blame] | 348 | // Remember if there was a space after "operator". This is necessary to |
| 349 | // check for collisions with strangely named functions like "operatorint()". |
| 350 | bool space_after_operator = name.consume_front(" "); |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 351 | |
Raphael Isemann | 2f323fc | 2019-08-28 13:46:01 +0000 | [diff] [blame] | 352 | op_kind = StringSwitch<clang::OverloadedOperatorKind>(name) |
| 353 | .Case("+", clang::OO_Plus) |
| 354 | .Case("+=", clang::OO_PlusEqual) |
| 355 | .Case("++", clang::OO_PlusPlus) |
| 356 | .Case("-", clang::OO_Minus) |
| 357 | .Case("-=", clang::OO_MinusEqual) |
| 358 | .Case("--", clang::OO_MinusMinus) |
| 359 | .Case("->", clang::OO_Arrow) |
| 360 | .Case("->*", clang::OO_ArrowStar) |
| 361 | .Case("*", clang::OO_Star) |
| 362 | .Case("*=", clang::OO_StarEqual) |
| 363 | .Case("/", clang::OO_Slash) |
| 364 | .Case("/=", clang::OO_SlashEqual) |
| 365 | .Case("%", clang::OO_Percent) |
| 366 | .Case("%=", clang::OO_PercentEqual) |
| 367 | .Case("^", clang::OO_Caret) |
| 368 | .Case("^=", clang::OO_CaretEqual) |
| 369 | .Case("&", clang::OO_Amp) |
| 370 | .Case("&=", clang::OO_AmpEqual) |
| 371 | .Case("&&", clang::OO_AmpAmp) |
| 372 | .Case("|", clang::OO_Pipe) |
| 373 | .Case("|=", clang::OO_PipeEqual) |
| 374 | .Case("||", clang::OO_PipePipe) |
| 375 | .Case("~", clang::OO_Tilde) |
| 376 | .Case("!", clang::OO_Exclaim) |
| 377 | .Case("!=", clang::OO_ExclaimEqual) |
| 378 | .Case("=", clang::OO_Equal) |
| 379 | .Case("==", clang::OO_EqualEqual) |
| 380 | .Case("<", clang::OO_Less) |
| 381 | .Case("<<", clang::OO_LessLess) |
| 382 | .Case("<<=", clang::OO_LessLessEqual) |
| 383 | .Case("<=", clang::OO_LessEqual) |
| 384 | .Case(">", clang::OO_Greater) |
| 385 | .Case(">>", clang::OO_GreaterGreater) |
| 386 | .Case(">>=", clang::OO_GreaterGreaterEqual) |
| 387 | .Case(">=", clang::OO_GreaterEqual) |
| 388 | .Case("()", clang::OO_Call) |
| 389 | .Case("[]", clang::OO_Subscript) |
| 390 | .Case(",", clang::OO_Comma) |
| 391 | .Default(clang::NUM_OVERLOADED_OPERATORS); |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 392 | |
Raphael Isemann | 2f323fc | 2019-08-28 13:46:01 +0000 | [diff] [blame] | 393 | // We found a fitting operator, so we can exit now. |
| 394 | if (op_kind != clang::NUM_OVERLOADED_OPERATORS) |
| 395 | return true; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 396 | |
Raphael Isemann | 2f323fc | 2019-08-28 13:46:01 +0000 | [diff] [blame] | 397 | // After the "operator " or "operator" part is something unknown. This means |
| 398 | // it's either one of the named operators (new/delete), a conversion operator |
| 399 | // (e.g. operator bool) or a function which name starts with "operator" |
| 400 | // (e.g. void operatorbool). |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 401 | |
Raphael Isemann | 2f323fc | 2019-08-28 13:46:01 +0000 | [diff] [blame] | 402 | // If it's a function that starts with operator it can't have a space after |
| 403 | // "operator" because identifiers can't contain spaces. |
| 404 | // E.g. "operator int" (conversion operator) |
| 405 | // vs. "operatorint" (function with colliding name). |
| 406 | if (!space_after_operator) |
| 407 | return false; // not an operator. |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 408 | |
Raphael Isemann | 2f323fc | 2019-08-28 13:46:01 +0000 | [diff] [blame] | 409 | // Now the operator is either one of the named operators or a conversion |
| 410 | // operator. |
| 411 | op_kind = StringSwitch<clang::OverloadedOperatorKind>(name) |
| 412 | .Case("new", clang::OO_New) |
| 413 | .Case("new[]", clang::OO_Array_New) |
| 414 | .Case("delete", clang::OO_Delete) |
| 415 | .Case("delete[]", clang::OO_Array_Delete) |
| 416 | // conversion operators hit this case. |
| 417 | .Default(clang::NUM_OVERLOADED_OPERATORS); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 418 | |
| 419 | return true; |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 420 | } |
Enrico Granata | 5d84a69 | 2014-08-19 21:46:37 +0000 | [diff] [blame] | 421 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 422 | clang::AccessSpecifier |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 423 | ClangASTContext::ConvertAccessTypeToAccessSpecifier(AccessType access) { |
| 424 | switch (access) { |
| 425 | default: |
| 426 | break; |
| 427 | case eAccessNone: |
Greg Clayton | 8cf0593 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 428 | return AS_none; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 429 | case eAccessPublic: |
| 430 | return AS_public; |
| 431 | case eAccessPrivate: |
| 432 | return AS_private; |
| 433 | case eAccessProtected: |
| 434 | return AS_protected; |
| 435 | } |
| 436 | return AS_none; |
Greg Clayton | 8cf0593 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 439 | static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) { |
| 440 | // FIXME: Cleanup per-file based stuff. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 441 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 442 | // Set some properties which depend solely on the input kind; it would be |
| 443 | // nice to move these to the language standard, and have the driver resolve |
| 444 | // the input kind + language standard. |
Rainer Orth | 6ca1707 | 2019-08-05 14:00:43 +0000 | [diff] [blame] | 445 | if (IK.getLanguage() == clang::Language::Asm) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 446 | Opts.AsmPreprocessor = 1; |
Richard Smith | 8186cd4 | 2017-04-26 22:10:53 +0000 | [diff] [blame] | 447 | } else if (IK.isObjectiveC()) { |
Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 448 | Opts.ObjC = 1; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | LangStandard::Kind LangStd = LangStandard::lang_unspecified; |
| 452 | |
| 453 | if (LangStd == LangStandard::lang_unspecified) { |
| 454 | // Based on the base language, pick one. |
Richard Smith | 8186cd4 | 2017-04-26 22:10:53 +0000 | [diff] [blame] | 455 | switch (IK.getLanguage()) { |
Rainer Orth | 6ca1707 | 2019-08-05 14:00:43 +0000 | [diff] [blame] | 456 | case clang::Language::Unknown: |
| 457 | case clang::Language::LLVM_IR: |
| 458 | case clang::Language::RenderScript: |
David Blaikie | a322f36 | 2017-01-06 00:38:06 +0000 | [diff] [blame] | 459 | llvm_unreachable("Invalid input kind!"); |
Rainer Orth | 6ca1707 | 2019-08-05 14:00:43 +0000 | [diff] [blame] | 460 | case clang::Language::OpenCL: |
Pavel Labath | 4716854 | 2017-04-27 08:49:19 +0000 | [diff] [blame] | 461 | LangStd = LangStandard::lang_opencl10; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 462 | break; |
Rainer Orth | 6ca1707 | 2019-08-05 14:00:43 +0000 | [diff] [blame] | 463 | case clang::Language::CUDA: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 464 | LangStd = LangStandard::lang_cuda; |
| 465 | break; |
Rainer Orth | 6ca1707 | 2019-08-05 14:00:43 +0000 | [diff] [blame] | 466 | case clang::Language::Asm: |
| 467 | case clang::Language::C: |
| 468 | case clang::Language::ObjC: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 469 | LangStd = LangStandard::lang_gnu99; |
| 470 | break; |
Rainer Orth | 6ca1707 | 2019-08-05 14:00:43 +0000 | [diff] [blame] | 471 | case clang::Language::CXX: |
| 472 | case clang::Language::ObjCXX: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 473 | LangStd = LangStandard::lang_gnucxx98; |
| 474 | break; |
Rainer Orth | 6ca1707 | 2019-08-05 14:00:43 +0000 | [diff] [blame] | 475 | case clang::Language::HIP: |
Benjamin Kramer | 0d97c22 | 2018-04-25 13:22:47 +0000 | [diff] [blame] | 476 | LangStd = LangStandard::lang_hip; |
| 477 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 478 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 479 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 480 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 481 | const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd); |
| 482 | Opts.LineComment = Std.hasLineComments(); |
| 483 | Opts.C99 = Std.isC99(); |
| 484 | Opts.CPlusPlus = Std.isCPlusPlus(); |
| 485 | Opts.CPlusPlus11 = Std.isCPlusPlus11(); |
| 486 | Opts.Digraphs = Std.hasDigraphs(); |
| 487 | Opts.GNUMode = Std.isGNUMode(); |
| 488 | Opts.GNUInline = !Std.isC99(); |
| 489 | Opts.HexFloats = Std.hasHexFloats(); |
| 490 | Opts.ImplicitInt = Std.hasImplicitInt(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 491 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 492 | Opts.WChar = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 493 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 494 | // OpenCL has some additional defaults. |
Pavel Labath | 4716854 | 2017-04-27 08:49:19 +0000 | [diff] [blame] | 495 | if (LangStd == LangStandard::lang_opencl10) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 496 | Opts.OpenCL = 1; |
| 497 | Opts.AltiVec = 1; |
| 498 | Opts.CXXOperatorNames = 1; |
Richard Smith | c624510 | 2019-09-13 06:02:15 +0000 | [diff] [blame] | 499 | Opts.setLaxVectorConversions(LangOptions::LaxVectorConversionKind::All); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 500 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 501 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 502 | // OpenCL and C++ both have bool, true, false keywords. |
| 503 | Opts.Bool = Opts.OpenCL || Opts.CPlusPlus; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 504 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 505 | Opts.setValueVisibilityMode(DefaultVisibility); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 506 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 507 | // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs is |
| 508 | // specified, or -std is set to a conforming mode. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 509 | Opts.Trigraphs = !Opts.GNUMode; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 510 | Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 511 | Opts.OptimizeSize = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 512 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 513 | // FIXME: Eliminate this dependency. |
| 514 | // unsigned Opt = |
| 515 | // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags); |
| 516 | // Opts.Optimize = Opt != 0; |
| 517 | unsigned Opt = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 518 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 519 | // This is the __NO_INLINE__ define, which just depends on things like the |
| 520 | // optimization level and -fno-inline, not actually whether the backend has |
| 521 | // inlining enabled. |
| 522 | // |
| 523 | // FIXME: This is affected by other options (-fno-inline). |
| 524 | Opts.NoInlineDefine = !Opt; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Raphael Isemann | 92d5ea5d | 2019-11-27 10:27:25 +0100 | [diff] [blame] | 527 | ClangASTContext::ClangASTContext(llvm::StringRef target_triple) { |
Raphael Isemann | d01b4a7 | 2019-10-01 12:28:14 +0000 | [diff] [blame] | 528 | if (!target_triple.empty()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 529 | SetTargetTriple(target_triple); |
Raphael Isemann | 2eb963a | 2019-10-02 12:26:08 +0000 | [diff] [blame] | 530 | // The caller didn't pass an ASTContext so create a new one for this |
| 531 | // ClangASTContext. |
| 532 | CreateASTContext(); |
| 533 | } |
| 534 | |
Raphael Isemann | 92d5ea5d | 2019-11-27 10:27:25 +0100 | [diff] [blame] | 535 | ClangASTContext::ClangASTContext(ArchSpec arch) { |
Raphael Isemann | 2eb963a | 2019-10-02 12:26:08 +0000 | [diff] [blame] | 536 | SetTargetTriple(arch.GetTriple().str()); |
| 537 | // The caller didn't pass an ASTContext so create a new one for this |
| 538 | // ClangASTContext. |
| 539 | CreateASTContext(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Raphael Isemann | 92d5ea5d | 2019-11-27 10:27:25 +0100 | [diff] [blame] | 542 | ClangASTContext::ClangASTContext(ASTContext &existing_ctxt) { |
Raphael Isemann | c73bfc9 | 2019-10-01 12:55:37 +0000 | [diff] [blame] | 543 | SetTargetTriple(existing_ctxt.getTargetInfo().getTriple().str()); |
| 544 | |
| 545 | m_ast_up.reset(&existing_ctxt); |
| 546 | GetASTMap().Insert(&existing_ctxt, this); |
| 547 | } |
| 548 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 549 | // Destructor |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 550 | ClangASTContext::~ClangASTContext() { Finalize(); } |
| 551 | |
| 552 | ConstString ClangASTContext::GetPluginNameStatic() { |
| 553 | return ConstString("clang"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 556 | ConstString ClangASTContext::GetPluginName() { |
| 557 | return ClangASTContext::GetPluginNameStatic(); |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 560 | uint32_t ClangASTContext::GetPluginVersion() { return 1; } |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 561 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 562 | lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language, |
| 563 | lldb_private::Module *module, |
| 564 | Target *target) { |
Raphael Isemann | 76016f9 | 2019-11-29 12:40:19 +0100 | [diff] [blame] | 565 | if (!ClangASTContextSupportsLanguage(language)) |
| 566 | return lldb::TypeSystemSP(); |
| 567 | ArchSpec arch; |
| 568 | if (module) |
| 569 | arch = module->GetArchitecture(); |
| 570 | else if (target) |
| 571 | arch = target->GetArchitecture(); |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 572 | |
Raphael Isemann | 76016f9 | 2019-11-29 12:40:19 +0100 | [diff] [blame] | 573 | if (!arch.IsValid()) |
| 574 | return lldb::TypeSystemSP(); |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 575 | |
Raphael Isemann | 76016f9 | 2019-11-29 12:40:19 +0100 | [diff] [blame] | 576 | ArchSpec fixed_arch = arch; |
| 577 | // LLVM wants this to be set to iOS or MacOSX; if we're working on |
| 578 | // a bare-boards type image, change the triple for llvm's benefit. |
| 579 | if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple && |
| 580 | fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) { |
| 581 | if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm || |
| 582 | fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 || |
| 583 | fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64_32 || |
| 584 | fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) { |
| 585 | fixed_arch.GetTriple().setOS(llvm::Triple::IOS); |
| 586 | } else { |
| 587 | fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 588 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 589 | } |
Raphael Isemann | 76016f9 | 2019-11-29 12:40:19 +0100 | [diff] [blame] | 590 | |
| 591 | if (module) { |
| 592 | std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext(fixed_arch)); |
| 593 | return ast_sp; |
| 594 | } else if (target && target->IsValid()) { |
| 595 | std::shared_ptr<ClangASTContextForExpressions> ast_sp( |
| 596 | new ClangASTContextForExpressions(*target, fixed_arch)); |
| 597 | ast_sp->m_scratch_ast_source_up.reset( |
| 598 | new ClangASTSource(target->shared_from_this())); |
| 599 | lldbassert(ast_sp->getFileManager()); |
| 600 | ast_sp->m_scratch_ast_source_up->InstallASTContext( |
Raphael Isemann | bc7f1df | 2019-11-29 13:02:41 +0100 | [diff] [blame^] | 601 | *ast_sp, *ast_sp->getFileManager(), true); |
Raphael Isemann | 76016f9 | 2019-11-29 12:40:19 +0100 | [diff] [blame] | 602 | llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source( |
| 603 | ast_sp->m_scratch_ast_source_up->CreateProxy()); |
| 604 | ast_sp->SetExternalSource(proxy_ast_source); |
| 605 | return ast_sp; |
| 606 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 607 | return lldb::TypeSystemSP(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Adrian Prantl | aa97a89 | 2019-08-22 21:45:58 +0000 | [diff] [blame] | 610 | LanguageSet ClangASTContext::GetSupportedLanguagesForTypes() { |
| 611 | LanguageSet languages; |
| 612 | languages.Insert(lldb::eLanguageTypeC89); |
| 613 | languages.Insert(lldb::eLanguageTypeC); |
| 614 | languages.Insert(lldb::eLanguageTypeC11); |
| 615 | languages.Insert(lldb::eLanguageTypeC_plus_plus); |
| 616 | languages.Insert(lldb::eLanguageTypeC99); |
| 617 | languages.Insert(lldb::eLanguageTypeObjC); |
| 618 | languages.Insert(lldb::eLanguageTypeObjC_plus_plus); |
| 619 | languages.Insert(lldb::eLanguageTypeC_plus_plus_03); |
| 620 | languages.Insert(lldb::eLanguageTypeC_plus_plus_11); |
| 621 | languages.Insert(lldb::eLanguageTypeC11); |
| 622 | languages.Insert(lldb::eLanguageTypeC_plus_plus_14); |
| 623 | return languages; |
| 624 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 625 | |
Adrian Prantl | aa97a89 | 2019-08-22 21:45:58 +0000 | [diff] [blame] | 626 | LanguageSet ClangASTContext::GetSupportedLanguagesForExpressions() { |
| 627 | LanguageSet languages; |
| 628 | languages.Insert(lldb::eLanguageTypeC_plus_plus); |
| 629 | languages.Insert(lldb::eLanguageTypeObjC_plus_plus); |
| 630 | languages.Insert(lldb::eLanguageTypeC_plus_plus_03); |
| 631 | languages.Insert(lldb::eLanguageTypeC_plus_plus_11); |
| 632 | languages.Insert(lldb::eLanguageTypeC_plus_plus_14); |
| 633 | return languages; |
Enrico Granata | 5d84a69 | 2014-08-19 21:46:37 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 636 | void ClangASTContext::Initialize() { |
Adrian Prantl | aa97a89 | 2019-08-22 21:45:58 +0000 | [diff] [blame] | 637 | PluginManager::RegisterPlugin( |
| 638 | GetPluginNameStatic(), "clang base AST context plug-in", CreateInstance, |
| 639 | GetSupportedLanguagesForTypes(), GetSupportedLanguagesForExpressions()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 642 | void ClangASTContext::Terminate() { |
| 643 | PluginManager::UnregisterPlugin(CreateInstance); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 646 | void ClangASTContext::Finalize() { |
Raphael Isemann | 2eb963a | 2019-10-02 12:26:08 +0000 | [diff] [blame] | 647 | assert(m_ast_up); |
| 648 | GetASTMap().Erase(m_ast_up.get()); |
| 649 | if (!m_ast_owned) |
| 650 | m_ast_up.release(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 651 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 652 | m_builtins_up.reset(); |
| 653 | m_selector_table_up.reset(); |
| 654 | m_identifier_table_up.reset(); |
| 655 | m_target_info_up.reset(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 656 | m_target_options_rp.reset(); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 657 | m_diagnostics_engine_up.reset(); |
| 658 | m_source_manager_up.reset(); |
| 659 | m_language_options_up.reset(); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 660 | m_scratch_ast_source_up.reset(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Raphael Isemann | f74a4c1 | 2019-04-30 08:41:35 +0000 | [diff] [blame] | 663 | void ClangASTContext::setSema(Sema *s) { |
| 664 | // Ensure that the new sema actually belongs to our ASTContext. |
| 665 | assert(s == nullptr || &s->getASTContext() == m_ast_up.get()); |
| 666 | m_sema = s; |
| 667 | } |
| 668 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 669 | const char *ClangASTContext::GetTargetTriple() { |
| 670 | return m_target_triple.c_str(); |
| 671 | } |
| 672 | |
Raphael Isemann | d01b4a7 | 2019-10-01 12:28:14 +0000 | [diff] [blame] | 673 | void ClangASTContext::SetTargetTriple(llvm::StringRef target_triple) { |
Raphael Isemann | d01b4a7 | 2019-10-01 12:28:14 +0000 | [diff] [blame] | 674 | m_target_triple = target_triple.str(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 677 | void ClangASTContext::SetExternalSource( |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 678 | llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_up) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 679 | ASTContext *ast = getASTContext(); |
| 680 | if (ast) { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 681 | ast->setExternalSource(ast_source_up); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 682 | ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 686 | ASTContext *ClangASTContext::getASTContext() { |
Raphael Isemann | 2eb963a | 2019-10-02 12:26:08 +0000 | [diff] [blame] | 687 | assert(m_ast_up); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 688 | return m_ast_up.get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Raphael Isemann | 2eb963a | 2019-10-02 12:26:08 +0000 | [diff] [blame] | 691 | void ClangASTContext::CreateASTContext() { |
| 692 | assert(!m_ast_up); |
| 693 | m_ast_owned = true; |
| 694 | m_ast_up.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(), |
| 695 | *getIdentifierTable(), *getSelectorTable(), |
| 696 | *getBuiltinContext())); |
| 697 | |
| 698 | m_ast_up->getDiagnostics().setClient(getDiagnosticConsumer(), false); |
| 699 | |
| 700 | // This can be NULL if we don't know anything about the architecture or if |
| 701 | // the target for an architecture isn't enabled in the llvm/clang that we |
| 702 | // built |
| 703 | TargetInfo *target_info = getTargetInfo(); |
| 704 | if (target_info) |
| 705 | m_ast_up->InitBuiltinTypes(*target_info); |
| 706 | |
| 707 | if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) { |
| 708 | m_ast_up->getTranslationUnitDecl()->setHasExternalLexicalStorage(); |
| 709 | // m_ast_up->getTranslationUnitDecl()->setHasExternalVisibleStorage(); |
| 710 | } |
| 711 | |
| 712 | GetASTMap().Insert(m_ast_up.get(), this); |
| 713 | |
| 714 | llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_up( |
| 715 | new ClangExternalASTSourceCallbacks( |
| 716 | ClangASTContext::CompleteTagDecl, |
| 717 | ClangASTContext::CompleteObjCInterfaceDecl, nullptr, |
| 718 | ClangASTContext::LayoutRecordType, this)); |
| 719 | SetExternalSource(ast_source_up); |
| 720 | } |
| 721 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 722 | ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) { |
| 723 | ClangASTContext *clang_ast = GetASTMap().Lookup(ast); |
| 724 | return clang_ast; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 727 | Builtin::Context *ClangASTContext::getBuiltinContext() { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 728 | if (m_builtins_up == nullptr) |
| 729 | m_builtins_up.reset(new Builtin::Context()); |
| 730 | return m_builtins_up.get(); |
Sean Callanan | 79439e8 | 2010-11-18 02:56:27 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 733 | IdentifierTable *ClangASTContext::getIdentifierTable() { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 734 | if (m_identifier_table_up == nullptr) |
| 735 | m_identifier_table_up.reset( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 736 | new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr)); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 737 | return m_identifier_table_up.get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 740 | LangOptions *ClangASTContext::getLanguageOptions() { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 741 | if (m_language_options_up == nullptr) { |
| 742 | m_language_options_up.reset(new LangOptions()); |
Rainer Orth | 6ca1707 | 2019-08-05 14:00:43 +0000 | [diff] [blame] | 743 | ParseLangArgs(*m_language_options_up, clang::Language::ObjCXX, |
| 744 | GetTargetTriple()); |
| 745 | // InitializeLangOptions(*m_language_options_up, Language::ObjCXX); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 746 | } |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 747 | return m_language_options_up.get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 750 | SelectorTable *ClangASTContext::getSelectorTable() { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 751 | if (m_selector_table_up == nullptr) |
| 752 | m_selector_table_up.reset(new SelectorTable()); |
| 753 | return m_selector_table_up.get(); |
Greg Clayton | fe68904 | 2015-11-10 17:47:04 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 756 | clang::FileManager *ClangASTContext::getFileManager() { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 757 | if (m_file_manager_up == nullptr) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 758 | clang::FileSystemOptions file_system_options; |
Jonas Devlieghere | 9764b65 | 2019-02-18 20:31:18 +0000 | [diff] [blame] | 759 | m_file_manager_up.reset(new clang::FileManager( |
| 760 | file_system_options, FileSystem::Instance().GetVirtualFileSystem())); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 761 | } |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 762 | return m_file_manager_up.get(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | clang::SourceManager *ClangASTContext::getSourceManager() { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 766 | if (m_source_manager_up == nullptr) |
| 767 | m_source_manager_up.reset( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 768 | new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager())); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 769 | return m_source_manager_up.get(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 773 | if (m_diagnostics_engine_up == nullptr) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 774 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs()); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 775 | m_diagnostics_engine_up.reset( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 776 | new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions())); |
| 777 | } |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 778 | return m_diagnostics_engine_up.get(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | clang::MangleContext *ClangASTContext::getMangleContext() { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 782 | if (m_mangle_ctx_up == nullptr) |
| 783 | m_mangle_ctx_up.reset(getASTContext()->createMangleContext()); |
| 784 | return m_mangle_ctx_up.get(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | class NullDiagnosticConsumer : public DiagnosticConsumer { |
Sean Callanan | 7fddd4c | 2010-12-11 00:08:56 +0000 | [diff] [blame] | 788 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 789 | NullDiagnosticConsumer() { |
| 790 | m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS); |
| 791 | } |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 792 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 793 | void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
Raphael Isemann | 1756630 | 2019-05-03 10:03:28 +0000 | [diff] [blame] | 794 | const clang::Diagnostic &info) override { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 795 | if (m_log) { |
| 796 | llvm::SmallVector<char, 32> diag_str(10); |
| 797 | info.FormatDiagnostic(diag_str); |
| 798 | diag_str.push_back('\0'); |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 799 | LLDB_LOGF(m_log, "Compiler diagnostic: %s\n", diag_str.data()); |
Sean Callanan | 7fddd4c | 2010-12-11 00:08:56 +0000 | [diff] [blame] | 800 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const { |
| 804 | return new NullDiagnosticConsumer(); |
| 805 | } |
| 806 | |
Sean Callanan | 7fddd4c | 2010-12-11 00:08:56 +0000 | [diff] [blame] | 807 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 808 | Log *m_log; |
Sean Callanan | 7fddd4c | 2010-12-11 00:08:56 +0000 | [diff] [blame] | 809 | }; |
| 810 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 811 | DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 812 | if (m_diagnostic_consumer_up == nullptr) |
| 813 | m_diagnostic_consumer_up.reset(new NullDiagnosticConsumer); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 814 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 815 | return m_diagnostic_consumer_up.get(); |
Sean Callanan | 7fddd4c | 2010-12-11 00:08:56 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 818 | std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() { |
Jonas Devlieghere | 70355ac | 2019-02-12 03:47:39 +0000 | [diff] [blame] | 819 | if (m_target_options_rp == nullptr && !m_target_triple.empty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 820 | m_target_options_rp = std::make_shared<clang::TargetOptions>(); |
Jonas Devlieghere | 70355ac | 2019-02-12 03:47:39 +0000 | [diff] [blame] | 821 | if (m_target_options_rp != nullptr) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 822 | m_target_options_rp->Triple = m_target_triple; |
| 823 | } |
| 824 | return m_target_options_rp; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 827 | TargetInfo *ClangASTContext::getTargetInfo() { |
| 828 | // target_triple should be something like "x86_64-apple-macosx" |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 829 | if (m_target_info_up == nullptr && !m_target_triple.empty()) |
| 830 | m_target_info_up.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 831 | getTargetOptions())); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 832 | return m_target_info_up.get(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | #pragma mark Basic Types |
| 836 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 837 | static inline bool QualTypeMatchesBitSize(const uint64_t bit_size, |
| 838 | ASTContext *ast, QualType qual_type) { |
| 839 | uint64_t qual_type_bit_size = ast->getTypeSize(qual_type); |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 840 | return qual_type_bit_size == bit_size; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 841 | } |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 842 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 843 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 844 | ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding, |
| 845 | size_t bit_size) { |
| 846 | return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize( |
| 847 | getASTContext(), encoding, bit_size); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 850 | CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize( |
| 851 | ASTContext *ast, Encoding encoding, uint32_t bit_size) { |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 852 | auto *clang_ast_context = ClangASTContext::GetASTContext(ast); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 853 | if (!ast) |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 854 | return CompilerType(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 855 | switch (encoding) { |
| 856 | case eEncodingInvalid: |
| 857 | if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 858 | return CompilerType(clang_ast_context, ast->VoidPtrTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 859 | break; |
| 860 | |
| 861 | case eEncodingUint: |
| 862 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 863 | return CompilerType(clang_ast_context, |
| 864 | ast->UnsignedCharTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 865 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 866 | return CompilerType(clang_ast_context, |
| 867 | ast->UnsignedShortTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 868 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 869 | return CompilerType(clang_ast_context, |
| 870 | ast->UnsignedIntTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 871 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 872 | return CompilerType(clang_ast_context, |
| 873 | ast->UnsignedLongTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 874 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 875 | return CompilerType(clang_ast_context, |
| 876 | ast->UnsignedLongLongTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 877 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 878 | return CompilerType(clang_ast_context, |
| 879 | ast->UnsignedInt128Ty.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 880 | break; |
| 881 | |
| 882 | case eEncodingSint: |
| 883 | if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 884 | return CompilerType(clang_ast_context, |
| 885 | ast->SignedCharTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 886 | if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 887 | return CompilerType(clang_ast_context, ast->ShortTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 888 | if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 889 | return CompilerType(clang_ast_context, ast->IntTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 890 | if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 891 | return CompilerType(clang_ast_context, ast->LongTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 892 | if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 893 | return CompilerType(clang_ast_context, ast->LongLongTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 894 | if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 895 | return CompilerType(clang_ast_context, ast->Int128Ty.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 896 | break; |
| 897 | |
| 898 | case eEncodingIEEE754: |
| 899 | if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 900 | return CompilerType(clang_ast_context, ast->FloatTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 901 | if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 902 | return CompilerType(clang_ast_context, ast->DoubleTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 903 | if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 904 | return CompilerType(clang_ast_context, |
| 905 | ast->LongDoubleTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 906 | if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 907 | return CompilerType(clang_ast_context, ast->HalfTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 908 | break; |
| 909 | |
| 910 | case eEncodingVector: |
| 911 | // Sanity check that bit_size is a multiple of 8's. |
| 912 | if (bit_size && !(bit_size & 0x7u)) |
| 913 | return CompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 914 | clang_ast_context, |
| 915 | ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8) |
| 916 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 917 | break; |
| 918 | } |
| 919 | |
| 920 | return CompilerType(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 921 | } |
| 922 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 923 | lldb::BasicType |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 924 | ClangASTContext::GetBasicTypeEnumeration(ConstString name) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 925 | if (name) { |
| 926 | typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap; |
| 927 | static TypeNameToBasicTypeMap g_type_map; |
Kamil Rytarowski | c5f28e2 | 2017-02-06 17:55:02 +0000 | [diff] [blame] | 928 | static llvm::once_flag g_once_flag; |
| 929 | llvm::call_once(g_once_flag, []() { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 930 | // "void" |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 931 | g_type_map.Append(ConstString("void"), eBasicTypeVoid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 932 | |
| 933 | // "char" |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 934 | g_type_map.Append(ConstString("char"), eBasicTypeChar); |
| 935 | g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar); |
| 936 | g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar); |
| 937 | g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar); |
| 938 | g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar); |
| 939 | g_type_map.Append(ConstString("unsigned wchar_t"), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 940 | eBasicTypeUnsignedWChar); |
| 941 | // "short" |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 942 | g_type_map.Append(ConstString("short"), eBasicTypeShort); |
| 943 | g_type_map.Append(ConstString("short int"), eBasicTypeShort); |
| 944 | g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort); |
| 945 | g_type_map.Append(ConstString("unsigned short int"), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 946 | eBasicTypeUnsignedShort); |
| 947 | |
| 948 | // "int" |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 949 | g_type_map.Append(ConstString("int"), eBasicTypeInt); |
| 950 | g_type_map.Append(ConstString("signed int"), eBasicTypeInt); |
| 951 | g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt); |
| 952 | g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 953 | |
| 954 | // "long" |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 955 | g_type_map.Append(ConstString("long"), eBasicTypeLong); |
| 956 | g_type_map.Append(ConstString("long int"), eBasicTypeLong); |
| 957 | g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong); |
| 958 | g_type_map.Append(ConstString("unsigned long int"), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 959 | eBasicTypeUnsignedLong); |
| 960 | |
| 961 | // "long long" |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 962 | g_type_map.Append(ConstString("long long"), eBasicTypeLongLong); |
| 963 | g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong); |
| 964 | g_type_map.Append(ConstString("unsigned long long"), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 965 | eBasicTypeUnsignedLongLong); |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 966 | g_type_map.Append(ConstString("unsigned long long int"), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 967 | eBasicTypeUnsignedLongLong); |
| 968 | |
| 969 | // "int128" |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 970 | g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128); |
| 971 | g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 972 | |
| 973 | // Miscellaneous |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 974 | g_type_map.Append(ConstString("bool"), eBasicTypeBool); |
| 975 | g_type_map.Append(ConstString("float"), eBasicTypeFloat); |
| 976 | g_type_map.Append(ConstString("double"), eBasicTypeDouble); |
| 977 | g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble); |
| 978 | g_type_map.Append(ConstString("id"), eBasicTypeObjCID); |
| 979 | g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel); |
| 980 | g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 981 | g_type_map.Sort(); |
| 982 | }); |
| 983 | |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 984 | return g_type_map.Find(name, eBasicTypeInvalid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 985 | } |
| 986 | return eBasicTypeInvalid; |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Raphael Isemann | c502bae | 2019-11-20 12:40:08 +0100 | [diff] [blame] | 989 | CompilerType ClangASTContext::GetBasicType(ConstString name) { |
| 990 | lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name); |
| 991 | return GetBasicType(basic_type); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | uint32_t ClangASTContext::GetPointerByteSize() { |
| 995 | if (m_pointer_byte_size == 0) |
Adrian Prantl | d963a7c | 2019-01-15 18:07:52 +0000 | [diff] [blame] | 996 | if (auto size = GetBasicType(lldb::eBasicTypeVoid) |
| 997 | .GetPointerType() |
| 998 | .GetByteSize(nullptr)) |
| 999 | m_pointer_byte_size = *size; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1000 | return m_pointer_byte_size; |
| 1001 | } |
| 1002 | |
| 1003 | CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) { |
Raphael Isemann | c502bae | 2019-11-20 12:40:08 +0100 | [diff] [blame] | 1004 | clang::ASTContext *ast = getASTContext(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1005 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1006 | lldb::opaque_compiler_type_t clang_type = |
| 1007 | GetOpaqueCompilerType(ast, basic_type); |
| 1008 | |
| 1009 | if (clang_type) |
| 1010 | return CompilerType(GetASTContext(ast), clang_type); |
| 1011 | return CompilerType(); |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1014 | CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize( |
| 1015 | const char *type_name, uint32_t dw_ate, uint32_t bit_size) { |
| 1016 | ASTContext *ast = getASTContext(); |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1017 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1018 | #define streq(a, b) strcmp(a, b) == 0 |
| 1019 | assert(ast != nullptr); |
| 1020 | if (ast) { |
| 1021 | switch (dw_ate) { |
| 1022 | default: |
| 1023 | break; |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1024 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1025 | case DW_ATE_address: |
| 1026 | if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1027 | return CompilerType(this, ast->VoidPtrTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1028 | break; |
Zachary Turner | 9d8a97e | 2016-04-01 23:20:35 +0000 | [diff] [blame] | 1029 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1030 | case DW_ATE_boolean: |
| 1031 | if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1032 | return CompilerType(this, ast->BoolTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1033 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1034 | return CompilerType(this, ast->UnsignedCharTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1035 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1036 | return CompilerType(this, ast->UnsignedShortTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1037 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1038 | return CompilerType(this, ast->UnsignedIntTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1039 | break; |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1040 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1041 | case DW_ATE_lo_user: |
| 1042 | // This has been seen to mean DW_AT_complex_integer |
| 1043 | if (type_name) { |
| 1044 | if (::strstr(type_name, "complex")) { |
| 1045 | CompilerType complex_int_clang_type = |
| 1046 | GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed, |
| 1047 | bit_size / 2); |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1048 | return CompilerType( |
| 1049 | this, ast->getComplexType( |
| 1050 | ClangUtil::GetQualType(complex_int_clang_type)) |
| 1051 | .getAsOpaquePtr()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1052 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1053 | } |
| 1054 | break; |
| 1055 | |
| 1056 | case DW_ATE_complex_float: |
| 1057 | if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1058 | return CompilerType(this, ast->FloatComplexTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1059 | else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1060 | return CompilerType(this, ast->DoubleComplexTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1061 | else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1062 | return CompilerType(this, ast->LongDoubleComplexTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1063 | else { |
| 1064 | CompilerType complex_float_clang_type = |
| 1065 | GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float, |
| 1066 | bit_size / 2); |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1067 | return CompilerType( |
| 1068 | this, ast->getComplexType( |
| 1069 | ClangUtil::GetQualType(complex_float_clang_type)) |
| 1070 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1071 | } |
| 1072 | break; |
| 1073 | |
| 1074 | case DW_ATE_float: |
| 1075 | if (streq(type_name, "float") && |
| 1076 | QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1077 | return CompilerType(this, ast->FloatTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1078 | if (streq(type_name, "double") && |
| 1079 | QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1080 | return CompilerType(this, ast->DoubleTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1081 | if (streq(type_name, "long double") && |
| 1082 | QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1083 | return CompilerType(this, ast->LongDoubleTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1084 | // Fall back to not requiring a name match |
| 1085 | if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1086 | return CompilerType(this, ast->FloatTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1087 | if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1088 | return CompilerType(this, ast->DoubleTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1089 | if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1090 | return CompilerType(this, ast->LongDoubleTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1091 | if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1092 | return CompilerType(this, ast->HalfTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1093 | break; |
| 1094 | |
| 1095 | case DW_ATE_signed: |
| 1096 | if (type_name) { |
| 1097 | if (streq(type_name, "wchar_t") && |
| 1098 | QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) && |
| 1099 | (getTargetInfo() && |
| 1100 | TargetInfo::isTypeSigned(getTargetInfo()->getWCharType()))) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1101 | return CompilerType(this, ast->WCharTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1102 | if (streq(type_name, "void") && |
| 1103 | QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1104 | return CompilerType(this, ast->VoidTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1105 | if (strstr(type_name, "long long") && |
| 1106 | QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1107 | return CompilerType(this, ast->LongLongTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1108 | if (strstr(type_name, "long") && |
| 1109 | QualTypeMatchesBitSize(bit_size, ast, ast->LongTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1110 | return CompilerType(this, ast->LongTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1111 | if (strstr(type_name, "short") && |
| 1112 | QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1113 | return CompilerType(this, ast->ShortTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1114 | if (strstr(type_name, "char")) { |
| 1115 | if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1116 | return CompilerType(this, ast->CharTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1117 | if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1118 | return CompilerType(this, ast->SignedCharTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1119 | } |
| 1120 | if (strstr(type_name, "int")) { |
| 1121 | if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1122 | return CompilerType(this, ast->IntTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1123 | if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1124 | return CompilerType(this, ast->Int128Ty.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1125 | } |
| 1126 | } |
| 1127 | // We weren't able to match up a type name, just search by size |
| 1128 | if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1129 | return CompilerType(this, ast->CharTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1130 | if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1131 | return CompilerType(this, ast->ShortTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1132 | if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1133 | return CompilerType(this, ast->IntTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1134 | if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1135 | return CompilerType(this, ast->LongTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1136 | if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1137 | return CompilerType(this, ast->LongLongTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1138 | if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1139 | return CompilerType(this, ast->Int128Ty.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1140 | break; |
| 1141 | |
| 1142 | case DW_ATE_signed_char: |
| 1143 | if (ast->getLangOpts().CharIsSigned && type_name && |
| 1144 | streq(type_name, "char")) { |
| 1145 | if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1146 | return CompilerType(this, ast->CharTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1147 | } |
| 1148 | if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1149 | return CompilerType(this, ast->SignedCharTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1150 | break; |
| 1151 | |
| 1152 | case DW_ATE_unsigned: |
| 1153 | if (type_name) { |
| 1154 | if (streq(type_name, "wchar_t")) { |
| 1155 | if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) { |
| 1156 | if (!(getTargetInfo() && |
| 1157 | TargetInfo::isTypeSigned(getTargetInfo()->getWCharType()))) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1158 | return CompilerType(this, ast->WCharTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1159 | } |
| 1160 | } |
| 1161 | if (strstr(type_name, "long long")) { |
| 1162 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1163 | return CompilerType(this, ast->UnsignedLongLongTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1164 | } else if (strstr(type_name, "long")) { |
| 1165 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1166 | return CompilerType(this, ast->UnsignedLongTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1167 | } else if (strstr(type_name, "short")) { |
| 1168 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1169 | return CompilerType(this, ast->UnsignedShortTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1170 | } else if (strstr(type_name, "char")) { |
| 1171 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1172 | return CompilerType(this, ast->UnsignedCharTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1173 | } else if (strstr(type_name, "int")) { |
| 1174 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1175 | return CompilerType(this, ast->UnsignedIntTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1176 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1177 | return CompilerType(this, ast->UnsignedInt128Ty.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1178 | } |
| 1179 | } |
| 1180 | // We weren't able to match up a type name, just search by size |
| 1181 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1182 | return CompilerType(this, ast->UnsignedCharTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1183 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1184 | return CompilerType(this, ast->UnsignedShortTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1185 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1186 | return CompilerType(this, ast->UnsignedIntTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1187 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1188 | return CompilerType(this, ast->UnsignedLongTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1189 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1190 | return CompilerType(this, ast->UnsignedLongLongTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1191 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1192 | return CompilerType(this, ast->UnsignedInt128Ty.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1193 | break; |
| 1194 | |
| 1195 | case DW_ATE_unsigned_char: |
| 1196 | if (!ast->getLangOpts().CharIsSigned && type_name && |
| 1197 | streq(type_name, "char")) { |
| 1198 | if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1199 | return CompilerType(this, ast->CharTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1200 | } |
| 1201 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1202 | return CompilerType(this, ast->UnsignedCharTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1203 | if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1204 | return CompilerType(this, ast->UnsignedShortTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1205 | break; |
| 1206 | |
| 1207 | case DW_ATE_imaginary_float: |
| 1208 | break; |
| 1209 | |
| 1210 | case DW_ATE_UTF: |
| 1211 | if (type_name) { |
Jonas Devlieghere | c46d39b | 2019-08-21 21:30:55 +0000 | [diff] [blame] | 1212 | if (streq(type_name, "char16_t")) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1213 | return CompilerType(this, ast->Char16Ty.getAsOpaquePtr()); |
Jonas Devlieghere | 6c9dc12 | 2019-08-23 04:11:38 +0000 | [diff] [blame] | 1214 | if (streq(type_name, "char32_t")) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1215 | return CompilerType(this, ast->Char32Ty.getAsOpaquePtr()); |
Jonas Devlieghere | 6c9dc12 | 2019-08-23 04:11:38 +0000 | [diff] [blame] | 1216 | if (streq(type_name, "char8_t")) |
Jonas Devlieghere | c46d39b | 2019-08-21 21:30:55 +0000 | [diff] [blame] | 1217 | return CompilerType(this, ast->Char8Ty.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1218 | } |
| 1219 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1220 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1221 | } |
| 1222 | // This assert should fire for anything that we don't catch above so we know |
| 1223 | // to fix any issues we run into. |
| 1224 | if (type_name) { |
| 1225 | Host::SystemLog(Host::eSystemLogError, "error: need to add support for " |
| 1226 | "DW_TAG_base_type '%s' encoded with " |
| 1227 | "DW_ATE = 0x%x, bit_size = %u\n", |
| 1228 | type_name, dw_ate, bit_size); |
| 1229 | } else { |
| 1230 | Host::SystemLog(Host::eSystemLogError, "error: need to add support for " |
| 1231 | "DW_TAG_base_type encoded with " |
| 1232 | "DW_ATE = 0x%x, bit_size = %u\n", |
| 1233 | dw_ate, bit_size); |
| 1234 | } |
| 1235 | return CompilerType(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1238 | CompilerType ClangASTContext::GetCStringType(bool is_const) { |
| 1239 | ASTContext *ast = getASTContext(); |
| 1240 | QualType char_type(ast->CharTy); |
| 1241 | |
| 1242 | if (is_const) |
| 1243 | char_type.addConst(); |
| 1244 | |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1245 | return CompilerType(this, ast->getPointerType(char_type).getAsOpaquePtr()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
Zachary Turner | 115209e | 2018-11-05 19:25:39 +0000 | [diff] [blame] | 1248 | clang::DeclContext * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1249 | ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) { |
| 1250 | return ast->getTranslationUnitDecl(); |
Sean Callanan | 09ab4b7 | 2011-11-30 22:11:59 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1253 | clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast, |
| 1254 | clang::Decl *source_decl) { |
| 1255 | FileSystemOptions file_system_options; |
| 1256 | FileManager file_manager(file_system_options); |
| 1257 | ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false); |
| 1258 | |
Gabor Marton | 5ac6d49 | 2019-05-15 10:29:48 +0000 | [diff] [blame] | 1259 | if (llvm::Expected<clang::Decl *> ret_or_error = |
| 1260 | importer.Import(source_decl)) { |
| 1261 | return *ret_or_error; |
| 1262 | } else { |
| 1263 | Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS); |
| 1264 | LLDB_LOG_ERROR(log, ret_or_error.takeError(), "Couldn't import decl: {0}"); |
| 1265 | return nullptr; |
| 1266 | } |
Greg Clayton | 526e5af | 2010-11-13 03:52:47 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1269 | bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2, |
| 1270 | bool ignore_qualifiers) { |
| 1271 | ClangASTContext *ast = |
| 1272 | llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem()); |
| 1273 | if (!ast || ast != type2.GetTypeSystem()) |
| 1274 | return false; |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1275 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1276 | if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType()) |
| 1277 | return true; |
Greg Clayton | 55995eb | 2012-04-06 17:38:55 +0000 | [diff] [blame] | 1278 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1279 | QualType type1_qual = ClangUtil::GetQualType(type1); |
| 1280 | QualType type2_qual = ClangUtil::GetQualType(type2); |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 1281 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1282 | if (ignore_qualifiers) { |
| 1283 | type1_qual = type1_qual.getUnqualifiedType(); |
| 1284 | type2_qual = type2_qual.getUnqualifiedType(); |
| 1285 | } |
| 1286 | |
| 1287 | return ast->getASTContext()->hasSameType(type1_qual, type2_qual); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
Alex Langford | cb68bd7 | 2019-08-23 06:11:32 +0000 | [diff] [blame] | 1290 | CompilerType ClangASTContext::GetTypeForDecl(void *opaque_decl) { |
| 1291 | if (!opaque_decl) |
| 1292 | return CompilerType(); |
| 1293 | |
| 1294 | clang::Decl *decl = static_cast<clang::Decl *>(opaque_decl); |
| 1295 | if (auto *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl)) |
| 1296 | return GetTypeForDecl(named_decl); |
| 1297 | return CompilerType(); |
| 1298 | } |
| 1299 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1300 | CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) { |
| 1301 | if (clang::ObjCInterfaceDecl *interface_decl = |
| 1302 | llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) |
| 1303 | return GetTypeForDecl(interface_decl); |
| 1304 | if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) |
| 1305 | return GetTypeForDecl(tag_decl); |
| 1306 | return CompilerType(); |
Sean Callanan | 9998acd | 2014-12-05 01:21:59 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1309 | CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1310 | // No need to call the getASTContext() accessor (which can create the AST if |
| 1311 | // 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] | 1312 | // AST if our AST didn't already exist... |
| 1313 | ASTContext *ast = &decl->getASTContext(); |
| 1314 | if (ast) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1315 | return CompilerType(ClangASTContext::GetASTContext(ast), |
| 1316 | ast->getTagDeclType(decl).getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1317 | return CompilerType(); |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1320 | CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1321 | // No need to call the getASTContext() accessor (which can create the AST if |
| 1322 | // 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] | 1323 | // AST if our AST didn't already exist... |
| 1324 | ASTContext *ast = &decl->getASTContext(); |
| 1325 | if (ast) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1326 | return CompilerType(ClangASTContext::GetASTContext(ast), |
| 1327 | ast->getObjCInterfaceType(decl).getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1328 | return CompilerType(); |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1331 | #pragma mark Structure, Unions, Classes |
| 1332 | |
shafik | de2c7ca | 2019-10-28 14:26:54 -0700 | [diff] [blame] | 1333 | CompilerType ClangASTContext::CreateRecordType( |
| 1334 | DeclContext *decl_ctx, AccessType access_type, const char *name, int kind, |
| 1335 | LanguageType language, ClangASTMetadata *metadata, bool exports_symbols) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1336 | ASTContext *ast = getASTContext(); |
| 1337 | assert(ast != nullptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1338 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1339 | if (decl_ctx == nullptr) |
| 1340 | decl_ctx = ast->getTranslationUnitDecl(); |
Greg Clayton | 9e40956 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 1341 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1342 | if (language == eLanguageTypeObjC || |
| 1343 | language == eLanguageTypeObjC_plus_plus) { |
| 1344 | bool isForwardDecl = true; |
| 1345 | bool isInternal = false; |
| 1346 | return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata); |
| 1347 | } |
Greg Clayton | 9e40956 | 2010-07-28 02:04:09 +0000 | [diff] [blame] | 1348 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1349 | // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1350 | // we will need to update this code. I was told to currently always use the |
| 1351 | // CXXRecordDecl class since we often don't know from debug information if |
| 1352 | // 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] | 1353 | // complete definition just in case. |
Greg Clayton | c4ffd66 | 2013-03-08 01:37:30 +0000 | [diff] [blame] | 1354 | |
Shafik Yaghmour | 62abe49 | 2019-08-14 22:30:29 +0000 | [diff] [blame] | 1355 | bool has_name = name && name[0]; |
Greg Clayton | c4ffd66 | 2013-03-08 01:37:30 +0000 | [diff] [blame] | 1356 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1357 | CXXRecordDecl *decl = CXXRecordDecl::Create( |
| 1358 | *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(), |
Shafik Yaghmour | 62abe49 | 2019-08-14 22:30:29 +0000 | [diff] [blame] | 1359 | SourceLocation(), has_name ? &ast->Idents.get(name) : nullptr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1360 | |
Shafik Yaghmour | 62abe49 | 2019-08-14 22:30:29 +0000 | [diff] [blame] | 1361 | if (!has_name) { |
| 1362 | // In C++ a lambda is also represented as an unnamed class. This is |
| 1363 | // different from an *anonymous class* that the user wrote: |
| 1364 | // |
| 1365 | // struct A { |
| 1366 | // // anonymous class (GNU/MSVC extension) |
| 1367 | // struct { |
| 1368 | // int x; |
| 1369 | // }; |
| 1370 | // // unnamed class within a class |
| 1371 | // struct { |
| 1372 | // int y; |
| 1373 | // } B; |
| 1374 | // }; |
| 1375 | // |
| 1376 | // void f() { |
| 1377 | // // unammed class outside of a class |
| 1378 | // struct { |
| 1379 | // int z; |
| 1380 | // } C; |
| 1381 | // } |
| 1382 | // |
| 1383 | // Anonymous classes is a GNU/MSVC extension that clang supports. It |
| 1384 | // requires the anonymous class be embedded within a class. So the new |
| 1385 | // heuristic verifies this condition. |
shafik | de2c7ca | 2019-10-28 14:26:54 -0700 | [diff] [blame] | 1386 | if (isa<CXXRecordDecl>(decl_ctx) && exports_symbols) |
Shafik Yaghmour | 62abe49 | 2019-08-14 22:30:29 +0000 | [diff] [blame] | 1387 | decl->setAnonymousStructOrUnion(true); |
Shafik Yaghmour | 62abe49 | 2019-08-14 22:30:29 +0000 | [diff] [blame] | 1388 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1389 | |
| 1390 | if (decl) { |
| 1391 | if (metadata) |
| 1392 | SetMetadata(ast, decl, *metadata); |
| 1393 | |
| 1394 | if (access_type != eAccessNone) |
| 1395 | decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type)); |
| 1396 | |
| 1397 | if (decl_ctx) |
| 1398 | decl_ctx->addDecl(decl); |
| 1399 | |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1400 | return CompilerType(this, ast->getTagDeclType(decl).getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1401 | } |
| 1402 | return CompilerType(); |
Greg Clayton | 6beaaa6 | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1405 | namespace { |
| 1406 | bool IsValueParam(const clang::TemplateArgument &argument) { |
| 1407 | return argument.getKind() == TemplateArgument::Integral; |
| 1408 | } |
| 1409 | } |
| 1410 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1411 | static TemplateParameterList *CreateTemplateParameterList( |
| 1412 | ASTContext *ast, |
| 1413 | const ClangASTContext::TemplateParameterInfos &template_param_infos, |
| 1414 | llvm::SmallVector<NamedDecl *, 8> &template_param_decls) { |
| 1415 | const bool parameter_pack = false; |
| 1416 | const bool is_typename = false; |
| 1417 | const unsigned depth = 0; |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1418 | const size_t num_template_params = template_param_infos.args.size(); |
| 1419 | DeclContext *const decl_context = |
| 1420 | ast->getTranslationUnitDecl(); // Is this the right decl context?, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1421 | for (size_t i = 0; i < num_template_params; ++i) { |
| 1422 | const char *name = template_param_infos.names[i]; |
Greg Clayton | 3c2e3ae | 2012-02-06 06:42:51 +0000 | [diff] [blame] | 1423 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1424 | IdentifierInfo *identifier_info = nullptr; |
| 1425 | if (name && name[0]) |
| 1426 | identifier_info = &ast->Idents.get(name); |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1427 | if (IsValueParam(template_param_infos.args[i])) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1428 | template_param_decls.push_back(NonTypeTemplateParmDecl::Create( |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1429 | *ast, decl_context, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1430 | SourceLocation(), SourceLocation(), depth, i, identifier_info, |
| 1431 | template_param_infos.args[i].getIntegralType(), parameter_pack, |
| 1432 | nullptr)); |
| 1433 | |
| 1434 | } else { |
| 1435 | template_param_decls.push_back(TemplateTypeParmDecl::Create( |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1436 | *ast, decl_context, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1437 | SourceLocation(), SourceLocation(), depth, i, identifier_info, |
| 1438 | is_typename, parameter_pack)); |
| 1439 | } |
| 1440 | } |
Eugene Zemtsov | a9d928c | 2017-09-29 03:15:08 +0000 | [diff] [blame] | 1441 | |
Shafik Yaghmour | 1849dd4 | 2019-01-30 21:48:56 +0000 | [diff] [blame] | 1442 | if (template_param_infos.packed_args) { |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1443 | IdentifierInfo *identifier_info = nullptr; |
| 1444 | if (template_param_infos.pack_name && template_param_infos.pack_name[0]) |
| 1445 | identifier_info = &ast->Idents.get(template_param_infos.pack_name); |
| 1446 | const bool parameter_pack_true = true; |
Shafik Yaghmour | 1849dd4 | 2019-01-30 21:48:56 +0000 | [diff] [blame] | 1447 | |
| 1448 | if (!template_param_infos.packed_args->args.empty() && |
| 1449 | IsValueParam(template_param_infos.packed_args->args[0])) { |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1450 | template_param_decls.push_back(NonTypeTemplateParmDecl::Create( |
Shafik Yaghmour | 1849dd4 | 2019-01-30 21:48:56 +0000 | [diff] [blame] | 1451 | *ast, decl_context, SourceLocation(), SourceLocation(), depth, |
| 1452 | num_template_params, identifier_info, |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1453 | template_param_infos.packed_args->args[0].getIntegralType(), |
| 1454 | parameter_pack_true, nullptr)); |
| 1455 | } else { |
| 1456 | template_param_decls.push_back(TemplateTypeParmDecl::Create( |
Shafik Yaghmour | 1849dd4 | 2019-01-30 21:48:56 +0000 | [diff] [blame] | 1457 | *ast, decl_context, SourceLocation(), SourceLocation(), depth, |
| 1458 | num_template_params, identifier_info, is_typename, |
| 1459 | parameter_pack_true)); |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1460 | } |
| 1461 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1462 | clang::Expr *const requires_clause = nullptr; // TODO: Concepts |
| 1463 | TemplateParameterList *template_param_list = TemplateParameterList::Create( |
| 1464 | *ast, SourceLocation(), SourceLocation(), template_param_decls, |
| 1465 | SourceLocation(), requires_clause); |
| 1466 | return template_param_list; |
Greg Clayton | 3c2e3ae | 2012-02-06 06:42:51 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1469 | clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl( |
| 1470 | clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl, |
| 1471 | const char *name, const TemplateParameterInfos &template_param_infos) { |
Adrian Prantl | d8f460e | 2018-05-02 16:55:16 +0000 | [diff] [blame] | 1472 | // /// Create a function template node. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1473 | ASTContext *ast = getASTContext(); |
Greg Clayton | 3c2e3ae | 2012-02-06 06:42:51 +0000 | [diff] [blame] | 1474 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1475 | llvm::SmallVector<NamedDecl *, 8> template_param_decls; |
Greg Clayton | 3c2e3ae | 2012-02-06 06:42:51 +0000 | [diff] [blame] | 1476 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1477 | TemplateParameterList *template_param_list = CreateTemplateParameterList( |
| 1478 | ast, template_param_infos, template_param_decls); |
| 1479 | FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create( |
| 1480 | *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(), |
| 1481 | template_param_list, func_decl); |
| 1482 | |
| 1483 | for (size_t i = 0, template_param_decl_count = template_param_decls.size(); |
| 1484 | i < template_param_decl_count; ++i) { |
| 1485 | // TODO: verify which decl context we should put template_param_decls into.. |
| 1486 | template_param_decls[i]->setDeclContext(func_decl); |
| 1487 | } |
| 1488 | |
| 1489 | return func_tmpl_decl; |
Greg Clayton | 3c2e3ae | 2012-02-06 06:42:51 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1492 | void ClangASTContext::CreateFunctionTemplateSpecializationInfo( |
| 1493 | FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl, |
| 1494 | const TemplateParameterInfos &infos) { |
Shafik Yaghmour | a0858e2 | 2019-07-17 20:16:13 +0000 | [diff] [blame] | 1495 | TemplateArgumentList *template_args_ptr = |
| 1496 | TemplateArgumentList::CreateCopy(func_decl->getASTContext(), infos.args); |
Greg Clayton | 3c2e3ae | 2012-02-06 06:42:51 +0000 | [diff] [blame] | 1497 | |
Shafik Yaghmour | a0858e2 | 2019-07-17 20:16:13 +0000 | [diff] [blame] | 1498 | func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, |
| 1499 | template_args_ptr, nullptr); |
Greg Clayton | 3c2e3ae | 2012-02-06 06:42:51 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1502 | ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl( |
| 1503 | DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name, |
| 1504 | int kind, const TemplateParameterInfos &template_param_infos) { |
| 1505 | ASTContext *ast = getASTContext(); |
Greg Clayton | 3c2e3ae | 2012-02-06 06:42:51 +0000 | [diff] [blame] | 1506 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1507 | ClassTemplateDecl *class_template_decl = nullptr; |
| 1508 | if (decl_ctx == nullptr) |
| 1509 | decl_ctx = ast->getTranslationUnitDecl(); |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1510 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1511 | IdentifierInfo &identifier_info = ast->Idents.get(class_name); |
| 1512 | DeclarationName decl_name(&identifier_info); |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1513 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1514 | clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name); |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1515 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1516 | for (NamedDecl *decl : result) { |
| 1517 | class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl); |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1518 | if (class_template_decl) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1519 | return class_template_decl; |
| 1520 | } |
| 1521 | |
| 1522 | llvm::SmallVector<NamedDecl *, 8> template_param_decls; |
| 1523 | |
| 1524 | TemplateParameterList *template_param_list = CreateTemplateParameterList( |
| 1525 | ast, template_param_infos, template_param_decls); |
| 1526 | |
| 1527 | CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create( |
| 1528 | *ast, (TagDecl::TagKind)kind, |
| 1529 | decl_ctx, // What decl context do we use here? TU? The actual decl |
| 1530 | // context? |
| 1531 | SourceLocation(), SourceLocation(), &identifier_info); |
| 1532 | |
| 1533 | for (size_t i = 0, template_param_decl_count = template_param_decls.size(); |
| 1534 | i < template_param_decl_count; ++i) { |
| 1535 | template_param_decls[i]->setDeclContext(template_cxx_decl); |
| 1536 | } |
| 1537 | |
| 1538 | // With templated classes, we say that a class is templated with |
| 1539 | // specializations, but that the bare class has no functions. |
| 1540 | // template_cxx_decl->startDefinition(); |
| 1541 | // template_cxx_decl->completeDefinition(); |
| 1542 | |
| 1543 | class_template_decl = ClassTemplateDecl::Create( |
| 1544 | *ast, |
| 1545 | decl_ctx, // What decl context do we use here? TU? The actual decl |
| 1546 | // context? |
Pavel Labath | 4294de3 | 2017-01-12 10:44:16 +0000 | [diff] [blame] | 1547 | SourceLocation(), decl_name, template_param_list, template_cxx_decl); |
Richard Smith | 35b007e | 2019-02-15 21:48:09 +0000 | [diff] [blame] | 1548 | template_cxx_decl->setDescribedClassTemplate(class_template_decl); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1549 | |
| 1550 | if (class_template_decl) { |
| 1551 | if (access_type != eAccessNone) |
| 1552 | class_template_decl->setAccess( |
| 1553 | ConvertAccessTypeToAccessSpecifier(access_type)); |
| 1554 | |
| 1555 | // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx)) |
| 1556 | // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl)); |
| 1557 | |
| 1558 | decl_ctx->addDecl(class_template_decl); |
| 1559 | |
Sean Callanan | 5e9e199 | 2011-10-26 01:06:27 +0000 | [diff] [blame] | 1560 | #ifdef LLDB_CONFIGURATION_DEBUG |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1561 | VerifyDecl(class_template_decl); |
Sean Callanan | 5e9e199 | 2011-10-26 01:06:27 +0000 | [diff] [blame] | 1562 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1563 | } |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1564 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1565 | return class_template_decl; |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Frederic Riss | f4e7e52 | 2018-04-02 16:18:32 +0000 | [diff] [blame] | 1568 | TemplateTemplateParmDecl * |
| 1569 | ClangASTContext::CreateTemplateTemplateParmDecl(const char *template_name) { |
| 1570 | ASTContext *ast = getASTContext(); |
| 1571 | |
| 1572 | auto *decl_ctx = ast->getTranslationUnitDecl(); |
| 1573 | |
| 1574 | IdentifierInfo &identifier_info = ast->Idents.get(template_name); |
| 1575 | llvm::SmallVector<NamedDecl *, 8> template_param_decls; |
| 1576 | |
| 1577 | ClangASTContext::TemplateParameterInfos template_param_infos; |
| 1578 | TemplateParameterList *template_param_list = CreateTemplateParameterList( |
| 1579 | ast, template_param_infos, template_param_decls); |
| 1580 | |
| 1581 | // 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] | 1582 | // type that includes a template template argument. Only the name matters for |
| 1583 | // this purpose, so we use dummy values for the other characterisitcs of the |
| 1584 | // type. |
Frederic Riss | f4e7e52 | 2018-04-02 16:18:32 +0000 | [diff] [blame] | 1585 | return TemplateTemplateParmDecl::Create( |
| 1586 | *ast, decl_ctx, SourceLocation(), |
| 1587 | /*Depth*/ 0, /*Position*/ 0, |
| 1588 | /*IsParameterPack*/ false, &identifier_info, template_param_list); |
| 1589 | } |
| 1590 | |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1591 | ClassTemplateSpecializationDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1592 | ClangASTContext::CreateClassTemplateSpecializationDecl( |
| 1593 | DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind, |
| 1594 | const TemplateParameterInfos &template_param_infos) { |
| 1595 | ASTContext *ast = getASTContext(); |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1596 | llvm::SmallVector<clang::TemplateArgument, 2> args( |
| 1597 | template_param_infos.args.size() + |
| 1598 | (template_param_infos.packed_args ? 1 : 0)); |
| 1599 | std::copy(template_param_infos.args.begin(), template_param_infos.args.end(), |
| 1600 | args.begin()); |
| 1601 | if (template_param_infos.packed_args) { |
| 1602 | args[args.size() - 1] = TemplateArgument::CreatePackCopy( |
| 1603 | *ast, template_param_infos.packed_args->args); |
| 1604 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1605 | ClassTemplateSpecializationDecl *class_template_specialization_decl = |
| 1606 | ClassTemplateSpecializationDecl::Create( |
| 1607 | *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(), |
Sean Callanan | 09e91ac | 2017-05-11 22:08:05 +0000 | [diff] [blame] | 1608 | SourceLocation(), class_template_decl, args, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1609 | nullptr); |
| 1610 | |
| 1611 | class_template_specialization_decl->setSpecializationKind( |
| 1612 | TSK_ExplicitSpecialization); |
| 1613 | |
| 1614 | return class_template_specialization_decl; |
| 1615 | } |
| 1616 | |
| 1617 | CompilerType ClangASTContext::CreateClassTemplateSpecializationType( |
| 1618 | ClassTemplateSpecializationDecl *class_template_specialization_decl) { |
| 1619 | if (class_template_specialization_decl) { |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1620 | ASTContext *ast = getASTContext(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1621 | if (ast) |
| 1622 | return CompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1623 | this, ast->getTagDeclType(class_template_specialization_decl) |
| 1624 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1625 | } |
| 1626 | return CompilerType(); |
Greg Clayton | f0705c8 | 2011-10-22 03:33:13 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1629 | static inline bool check_op_param(bool is_method, |
| 1630 | clang::OverloadedOperatorKind op_kind, |
| 1631 | bool unary, bool binary, |
| 1632 | uint32_t num_params) { |
| 1633 | // Special-case call since it can take any number of operands |
| 1634 | if (op_kind == OO_Call) |
| 1635 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1636 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1637 | // The parameter count doesn't include "this" |
| 1638 | if (is_method) |
| 1639 | ++num_params; |
| 1640 | if (num_params == 1) |
| 1641 | return unary; |
| 1642 | if (num_params == 2) |
| 1643 | return binary; |
| 1644 | else |
Greg Clayton | 090d098 | 2011-06-19 03:43:27 +0000 | [diff] [blame] | 1645 | return false; |
| 1646 | } |
Daniel Dunbar | dacdfb5 | 2011-10-31 22:50:57 +0000 | [diff] [blame] | 1647 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1648 | bool ClangASTContext::CheckOverloadedOperatorKindParameterCount( |
| 1649 | bool is_method, clang::OverloadedOperatorKind op_kind, |
| 1650 | uint32_t num_params) { |
| 1651 | switch (op_kind) { |
| 1652 | default: |
| 1653 | break; |
| 1654 | // C++ standard allows any number of arguments to new/delete |
| 1655 | case OO_New: |
| 1656 | case OO_Array_New: |
| 1657 | case OO_Delete: |
| 1658 | case OO_Array_Delete: |
| 1659 | return true; |
| 1660 | } |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 1661 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1662 | #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \ |
| 1663 | case OO_##Name: \ |
| 1664 | return check_op_param(is_method, op_kind, Unary, Binary, num_params); |
| 1665 | switch (op_kind) { |
Greg Clayton | 090d098 | 2011-06-19 03:43:27 +0000 | [diff] [blame] | 1666 | #include "clang/Basic/OperatorKinds.def" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1667 | default: |
| 1668 | break; |
| 1669 | } |
| 1670 | return false; |
Greg Clayton | 090d098 | 2011-06-19 03:43:27 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1673 | clang::AccessSpecifier |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1674 | ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs, |
| 1675 | clang::AccessSpecifier rhs) { |
| 1676 | // Make the access equal to the stricter of the field and the nested field's |
| 1677 | // access |
| 1678 | if (lhs == AS_none || rhs == AS_none) |
| 1679 | return AS_none; |
| 1680 | if (lhs == AS_private || rhs == AS_private) |
| 1681 | return AS_private; |
| 1682 | if (lhs == AS_protected || rhs == AS_protected) |
| 1683 | return AS_protected; |
| 1684 | return AS_public; |
Sean Callanan | e8c0cfb | 2012-03-02 01:03:45 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1687 | bool ClangASTContext::FieldIsBitfield(FieldDecl *field, |
| 1688 | uint32_t &bitfield_bit_size) { |
Raphael Isemann | 02e9113 | 2019-11-20 12:09:19 +0100 | [diff] [blame] | 1689 | ASTContext *ast = getASTContext(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1690 | if (ast == nullptr || field == nullptr) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1691 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1692 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1693 | if (field->isBitField()) { |
| 1694 | Expr *bit_width_expr = field->getBitWidth(); |
| 1695 | if (bit_width_expr) { |
| 1696 | llvm::APSInt bit_width_apsint; |
| 1697 | if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) { |
| 1698 | bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1699 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1700 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1701 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1702 | } |
| 1703 | return false; |
| 1704 | } |
| 1705 | |
| 1706 | bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) { |
| 1707 | if (record_decl == nullptr) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1708 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1709 | |
| 1710 | if (!record_decl->field_empty()) |
| 1711 | return true; |
| 1712 | |
| 1713 | // No fields, lets check this is a CXX record and check the base classes |
| 1714 | const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl); |
| 1715 | if (cxx_record_decl) { |
| 1716 | CXXRecordDecl::base_class_const_iterator base_class, base_class_end; |
| 1717 | for (base_class = cxx_record_decl->bases_begin(), |
| 1718 | base_class_end = cxx_record_decl->bases_end(); |
| 1719 | base_class != base_class_end; ++base_class) { |
| 1720 | const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>( |
| 1721 | base_class->getType()->getAs<RecordType>()->getDecl()); |
| 1722 | if (RecordHasFields(base_class_decl)) |
| 1723 | return true; |
| 1724 | } |
| 1725 | } |
| 1726 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
Adrian Prantl | 4e8be2c | 2018-06-13 16:21:24 +0000 | [diff] [blame] | 1729 | #pragma mark Objective-C Classes |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1730 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1731 | CompilerType ClangASTContext::CreateObjCClass(const char *name, |
| 1732 | DeclContext *decl_ctx, |
| 1733 | bool isForwardDecl, |
| 1734 | bool isInternal, |
| 1735 | ClangASTMetadata *metadata) { |
| 1736 | ASTContext *ast = getASTContext(); |
| 1737 | assert(ast != nullptr); |
| 1738 | assert(name && name[0]); |
| 1739 | if (decl_ctx == nullptr) |
| 1740 | decl_ctx = ast->getTranslationUnitDecl(); |
Greg Clayton | 8cf0593 | 2010-07-22 18:30:50 +0000 | [diff] [blame] | 1741 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1742 | ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create( |
| 1743 | *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr, |
| 1744 | nullptr, SourceLocation(), |
| 1745 | /*isForwardDecl,*/ |
| 1746 | isInternal); |
| 1747 | |
| 1748 | if (decl && metadata) |
| 1749 | SetMetadata(ast, decl, *metadata); |
| 1750 | |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 1751 | return CompilerType(this, ast->getObjCInterfaceType(decl).getAsOpaquePtr()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1754 | static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) { |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 1755 | return !ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
Greg Clayton | 57ee306 | 2013-07-11 22:46:58 +0000 | [diff] [blame] | 1758 | uint32_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1759 | ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl, |
| 1760 | bool omit_empty_base_classes) { |
| 1761 | uint32_t num_bases = 0; |
| 1762 | if (cxx_record_decl) { |
| 1763 | if (omit_empty_base_classes) { |
| 1764 | CXXRecordDecl::base_class_const_iterator base_class, base_class_end; |
| 1765 | for (base_class = cxx_record_decl->bases_begin(), |
| 1766 | base_class_end = cxx_record_decl->bases_end(); |
| 1767 | base_class != base_class_end; ++base_class) { |
| 1768 | // Skip empty base classes |
| 1769 | if (omit_empty_base_classes) { |
| 1770 | if (BaseSpecifierIsEmpty(base_class)) |
| 1771 | continue; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1772 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1773 | ++num_bases; |
| 1774 | } |
| 1775 | } else |
| 1776 | num_bases = cxx_record_decl->getNumBases(); |
| 1777 | } |
| 1778 | return num_bases; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1781 | #pragma mark Namespace Declarations |
| 1782 | |
Raphael Isemann | a946997 | 2019-03-12 07:45:04 +0000 | [diff] [blame] | 1783 | NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration( |
| 1784 | const char *name, DeclContext *decl_ctx, bool is_inline) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1785 | NamespaceDecl *namespace_decl = nullptr; |
| 1786 | ASTContext *ast = getASTContext(); |
| 1787 | TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl(); |
| 1788 | if (decl_ctx == nullptr) |
| 1789 | decl_ctx = translation_unit_decl; |
Greg Clayton | 030a204 | 2011-10-14 21:34:45 +0000 | [diff] [blame] | 1790 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1791 | if (name) { |
| 1792 | IdentifierInfo &identifier_info = ast->Idents.get(name); |
| 1793 | DeclarationName decl_name(&identifier_info); |
| 1794 | clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name); |
| 1795 | for (NamedDecl *decl : result) { |
| 1796 | namespace_decl = dyn_cast<clang::NamespaceDecl>(decl); |
| 1797 | if (namespace_decl) |
| 1798 | return namespace_decl; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1799 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1800 | |
| 1801 | namespace_decl = |
Raphael Isemann | a946997 | 2019-03-12 07:45:04 +0000 | [diff] [blame] | 1802 | NamespaceDecl::Create(*ast, decl_ctx, is_inline, SourceLocation(), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1803 | SourceLocation(), &identifier_info, nullptr); |
| 1804 | |
| 1805 | decl_ctx->addDecl(namespace_decl); |
| 1806 | } else { |
| 1807 | if (decl_ctx == translation_unit_decl) { |
| 1808 | namespace_decl = translation_unit_decl->getAnonymousNamespace(); |
| 1809 | if (namespace_decl) |
| 1810 | return namespace_decl; |
| 1811 | |
| 1812 | namespace_decl = |
| 1813 | NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(), |
| 1814 | SourceLocation(), nullptr, nullptr); |
| 1815 | translation_unit_decl->setAnonymousNamespace(namespace_decl); |
| 1816 | translation_unit_decl->addDecl(namespace_decl); |
| 1817 | assert(namespace_decl == translation_unit_decl->getAnonymousNamespace()); |
| 1818 | } else { |
| 1819 | NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx); |
| 1820 | if (parent_namespace_decl) { |
| 1821 | namespace_decl = parent_namespace_decl->getAnonymousNamespace(); |
| 1822 | if (namespace_decl) |
| 1823 | return namespace_decl; |
| 1824 | namespace_decl = |
| 1825 | NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(), |
| 1826 | SourceLocation(), nullptr, nullptr); |
| 1827 | parent_namespace_decl->setAnonymousNamespace(namespace_decl); |
| 1828 | parent_namespace_decl->addDecl(namespace_decl); |
| 1829 | assert(namespace_decl == |
| 1830 | parent_namespace_decl->getAnonymousNamespace()); |
| 1831 | } else { |
Raphael Isemann | c494481 | 2019-07-04 19:49:31 +0000 | [diff] [blame] | 1832 | assert(false && "GetUniqueNamespaceDeclaration called with no name and " |
| 1833 | "no namespace as decl_ctx"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1834 | } |
Greg Clayton | 9d3d688 | 2011-10-31 23:51:19 +0000 | [diff] [blame] | 1835 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1836 | } |
Greg Clayton | 9d3d688 | 2011-10-31 23:51:19 +0000 | [diff] [blame] | 1837 | #ifdef LLDB_CONFIGURATION_DEBUG |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1838 | VerifyDecl(namespace_decl); |
Greg Clayton | 9d3d688 | 2011-10-31 23:51:19 +0000 | [diff] [blame] | 1839 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1840 | return namespace_decl; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 1843 | clang::BlockDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1844 | ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) { |
| 1845 | if (ctx != nullptr) { |
| 1846 | clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx, |
| 1847 | clang::SourceLocation()); |
| 1848 | ctx->addDecl(decl); |
| 1849 | return decl; |
| 1850 | } |
| 1851 | return nullptr; |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1854 | clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left, |
| 1855 | clang::DeclContext *right, |
| 1856 | clang::DeclContext *root) { |
| 1857 | if (root == nullptr) |
Paul Herman | ea188fc | 2015-09-16 18:48:30 +0000 | [diff] [blame] | 1858 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1859 | |
| 1860 | std::set<clang::DeclContext *> path_left; |
| 1861 | for (clang::DeclContext *d = left; d != nullptr; d = d->getParent()) |
| 1862 | path_left.insert(d); |
| 1863 | |
| 1864 | for (clang::DeclContext *d = right; d != nullptr; d = d->getParent()) |
| 1865 | if (path_left.find(d) != path_left.end()) |
| 1866 | return d; |
| 1867 | |
| 1868 | return nullptr; |
Paul Herman | ea188fc | 2015-09-16 18:48:30 +0000 | [diff] [blame] | 1869 | } |
| 1870 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1871 | clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration( |
| 1872 | clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) { |
| 1873 | if (decl_ctx != nullptr && ns_decl != nullptr) { |
| 1874 | clang::TranslationUnitDecl *translation_unit = |
| 1875 | (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext()); |
| 1876 | clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create( |
| 1877 | *getASTContext(), decl_ctx, clang::SourceLocation(), |
| 1878 | clang::SourceLocation(), clang::NestedNameSpecifierLoc(), |
| 1879 | clang::SourceLocation(), ns_decl, |
| 1880 | FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit)); |
| 1881 | decl_ctx->addDecl(using_decl); |
| 1882 | return using_decl; |
| 1883 | } |
| 1884 | return nullptr; |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | clang::UsingDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1888 | ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx, |
| 1889 | clang::NamedDecl *target) { |
| 1890 | if (current_decl_ctx != nullptr && target != nullptr) { |
| 1891 | clang::UsingDecl *using_decl = clang::UsingDecl::Create( |
| 1892 | *getASTContext(), current_decl_ctx, clang::SourceLocation(), |
| 1893 | clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false); |
| 1894 | clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create( |
| 1895 | *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl, |
| 1896 | target); |
| 1897 | using_decl->addShadowDecl(shadow_decl); |
| 1898 | current_decl_ctx->addDecl(using_decl); |
| 1899 | return using_decl; |
| 1900 | } |
| 1901 | return nullptr; |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1904 | clang::VarDecl *ClangASTContext::CreateVariableDeclaration( |
| 1905 | clang::DeclContext *decl_context, const char *name, clang::QualType type) { |
| 1906 | if (decl_context != nullptr) { |
| 1907 | clang::VarDecl *var_decl = clang::VarDecl::Create( |
| 1908 | *getASTContext(), decl_context, clang::SourceLocation(), |
| 1909 | clang::SourceLocation(), |
| 1910 | name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type, |
| 1911 | nullptr, clang::SC_None); |
| 1912 | var_decl->setAccess(clang::AS_public); |
| 1913 | decl_context->addDecl(var_decl); |
| 1914 | return var_decl; |
| 1915 | } |
| 1916 | return nullptr; |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
Zachary Turner | 9d8a97e | 2016-04-01 23:20:35 +0000 | [diff] [blame] | 1919 | lldb::opaque_compiler_type_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1920 | ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast, |
| 1921 | lldb::BasicType basic_type) { |
| 1922 | switch (basic_type) { |
| 1923 | case eBasicTypeVoid: |
| 1924 | return ast->VoidTy.getAsOpaquePtr(); |
| 1925 | case eBasicTypeChar: |
| 1926 | return ast->CharTy.getAsOpaquePtr(); |
| 1927 | case eBasicTypeSignedChar: |
| 1928 | return ast->SignedCharTy.getAsOpaquePtr(); |
| 1929 | case eBasicTypeUnsignedChar: |
| 1930 | return ast->UnsignedCharTy.getAsOpaquePtr(); |
| 1931 | case eBasicTypeWChar: |
| 1932 | return ast->getWCharType().getAsOpaquePtr(); |
| 1933 | case eBasicTypeSignedWChar: |
| 1934 | return ast->getSignedWCharType().getAsOpaquePtr(); |
| 1935 | case eBasicTypeUnsignedWChar: |
| 1936 | return ast->getUnsignedWCharType().getAsOpaquePtr(); |
| 1937 | case eBasicTypeChar16: |
| 1938 | return ast->Char16Ty.getAsOpaquePtr(); |
| 1939 | case eBasicTypeChar32: |
| 1940 | return ast->Char32Ty.getAsOpaquePtr(); |
| 1941 | case eBasicTypeShort: |
| 1942 | return ast->ShortTy.getAsOpaquePtr(); |
| 1943 | case eBasicTypeUnsignedShort: |
| 1944 | return ast->UnsignedShortTy.getAsOpaquePtr(); |
| 1945 | case eBasicTypeInt: |
| 1946 | return ast->IntTy.getAsOpaquePtr(); |
| 1947 | case eBasicTypeUnsignedInt: |
| 1948 | return ast->UnsignedIntTy.getAsOpaquePtr(); |
| 1949 | case eBasicTypeLong: |
| 1950 | return ast->LongTy.getAsOpaquePtr(); |
| 1951 | case eBasicTypeUnsignedLong: |
| 1952 | return ast->UnsignedLongTy.getAsOpaquePtr(); |
| 1953 | case eBasicTypeLongLong: |
| 1954 | return ast->LongLongTy.getAsOpaquePtr(); |
| 1955 | case eBasicTypeUnsignedLongLong: |
| 1956 | return ast->UnsignedLongLongTy.getAsOpaquePtr(); |
| 1957 | case eBasicTypeInt128: |
| 1958 | return ast->Int128Ty.getAsOpaquePtr(); |
| 1959 | case eBasicTypeUnsignedInt128: |
| 1960 | return ast->UnsignedInt128Ty.getAsOpaquePtr(); |
| 1961 | case eBasicTypeBool: |
| 1962 | return ast->BoolTy.getAsOpaquePtr(); |
| 1963 | case eBasicTypeHalf: |
| 1964 | return ast->HalfTy.getAsOpaquePtr(); |
| 1965 | case eBasicTypeFloat: |
| 1966 | return ast->FloatTy.getAsOpaquePtr(); |
| 1967 | case eBasicTypeDouble: |
| 1968 | return ast->DoubleTy.getAsOpaquePtr(); |
| 1969 | case eBasicTypeLongDouble: |
| 1970 | return ast->LongDoubleTy.getAsOpaquePtr(); |
| 1971 | case eBasicTypeFloatComplex: |
| 1972 | return ast->FloatComplexTy.getAsOpaquePtr(); |
| 1973 | case eBasicTypeDoubleComplex: |
| 1974 | return ast->DoubleComplexTy.getAsOpaquePtr(); |
| 1975 | case eBasicTypeLongDoubleComplex: |
| 1976 | return ast->LongDoubleComplexTy.getAsOpaquePtr(); |
| 1977 | case eBasicTypeObjCID: |
| 1978 | return ast->getObjCIdType().getAsOpaquePtr(); |
| 1979 | case eBasicTypeObjCClass: |
| 1980 | return ast->getObjCClassType().getAsOpaquePtr(); |
| 1981 | case eBasicTypeObjCSel: |
| 1982 | return ast->getObjCSelType().getAsOpaquePtr(); |
| 1983 | case eBasicTypeNullPtr: |
| 1984 | return ast->NullPtrTy.getAsOpaquePtr(); |
| 1985 | default: |
| 1986 | return nullptr; |
| 1987 | } |
Zachary Turner | 9d8a97e | 2016-04-01 23:20:35 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1990 | #pragma mark Function Types |
| 1991 | |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 1992 | clang::DeclarationName |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1993 | ClangASTContext::GetDeclarationName(const char *name, |
| 1994 | const CompilerType &function_clang_type) { |
| 1995 | if (!name || !name[0]) |
| 1996 | return clang::DeclarationName(); |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 1997 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1998 | clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS; |
| 1999 | if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS) |
| 2000 | return DeclarationName(&getASTContext()->Idents.get( |
| 2001 | name)); // Not operator, but a regular function. |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 2002 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2003 | // Check the number of operator parameters. Sometimes we have seen bad DWARF |
| 2004 | // that doesn't correctly describe operators and if we try to create a method |
| 2005 | // and add it to the class, clang will assert and crash, so we need to make |
| 2006 | // sure things are acceptable. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2007 | clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type)); |
| 2008 | const clang::FunctionProtoType *function_type = |
| 2009 | llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr()); |
| 2010 | if (function_type == nullptr) |
| 2011 | return clang::DeclarationName(); |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 2012 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2013 | const bool is_method = false; |
| 2014 | const unsigned int num_params = function_type->getNumParams(); |
| 2015 | if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount( |
| 2016 | is_method, op_kind, num_params)) |
| 2017 | return clang::DeclarationName(); |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 2018 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2019 | return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind); |
Pavel Labath | 1ac2b20 | 2016-08-15 14:32:32 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2022 | FunctionDecl *ClangASTContext::CreateFunctionDeclaration( |
| 2023 | DeclContext *decl_ctx, const char *name, |
| 2024 | const CompilerType &function_clang_type, int storage, bool is_inline) { |
| 2025 | FunctionDecl *func_decl = nullptr; |
| 2026 | ASTContext *ast = getASTContext(); |
| 2027 | if (decl_ctx == nullptr) |
| 2028 | decl_ctx = ast->getTranslationUnitDecl(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2029 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2030 | const bool hasWrittenPrototype = true; |
| 2031 | const bool isConstexprSpecified = false; |
Greg Clayton | 0d55104 | 2013-06-28 21:08:47 +0000 | [diff] [blame] | 2032 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2033 | clang::DeclarationName declarationName = |
| 2034 | GetDeclarationName(name, function_clang_type); |
| 2035 | func_decl = FunctionDecl::Create( |
| 2036 | *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName, |
| 2037 | ClangUtil::GetQualType(function_clang_type), nullptr, |
| 2038 | (clang::StorageClass)storage, is_inline, hasWrittenPrototype, |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 2039 | isConstexprSpecified ? CSK_constexpr : CSK_unspecified); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2040 | if (func_decl) |
| 2041 | decl_ctx->addDecl(func_decl); |
| 2042 | |
Sean Callanan | 5e9e199 | 2011-10-26 01:06:27 +0000 | [diff] [blame] | 2043 | #ifdef LLDB_CONFIGURATION_DEBUG |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2044 | VerifyDecl(func_decl); |
Sean Callanan | 5e9e199 | 2011-10-26 01:06:27 +0000 | [diff] [blame] | 2045 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2046 | |
| 2047 | return func_decl; |
| 2048 | } |
| 2049 | |
| 2050 | CompilerType ClangASTContext::CreateFunctionType( |
| 2051 | ASTContext *ast, const CompilerType &result_type, const CompilerType *args, |
Aleksandr Urakov | bc4707c | 2018-09-26 09:03:34 +0000 | [diff] [blame] | 2052 | unsigned num_args, bool is_variadic, unsigned type_quals, |
| 2053 | clang::CallingConv cc) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2054 | if (ast == nullptr) |
| 2055 | return CompilerType(); // invalid AST |
| 2056 | |
| 2057 | if (!result_type || !ClangUtil::IsClangType(result_type)) |
| 2058 | return CompilerType(); // invalid return type |
| 2059 | |
| 2060 | std::vector<QualType> qual_type_args; |
| 2061 | if (num_args > 0 && args == nullptr) |
| 2062 | return CompilerType(); // invalid argument array passed in |
| 2063 | |
| 2064 | // Verify that all arguments are valid and the right type |
| 2065 | for (unsigned i = 0; i < num_args; ++i) { |
| 2066 | if (args[i]) { |
| 2067 | // Make sure we have a clang type in args[i] and not a type from another |
| 2068 | // language whose name might match |
| 2069 | const bool is_clang_type = ClangUtil::IsClangType(args[i]); |
| 2070 | lldbassert(is_clang_type); |
| 2071 | if (is_clang_type) |
| 2072 | qual_type_args.push_back(ClangUtil::GetQualType(args[i])); |
| 2073 | else |
| 2074 | return CompilerType(); // invalid argument type (must be a clang type) |
| 2075 | } else |
| 2076 | return CompilerType(); // invalid argument type (empty) |
| 2077 | } |
| 2078 | |
| 2079 | // TODO: Detect calling convention in DWARF? |
| 2080 | FunctionProtoType::ExtProtoInfo proto_info; |
Aleksandr Urakov | bc4707c | 2018-09-26 09:03:34 +0000 | [diff] [blame] | 2081 | proto_info.ExtInfo = cc; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2082 | proto_info.Variadic = is_variadic; |
| 2083 | proto_info.ExceptionSpec = EST_None; |
Mikael Nilsson | 8b3bf6c | 2018-12-13 10:17:26 +0000 | [diff] [blame] | 2084 | proto_info.TypeQuals = clang::Qualifiers::fromFastMask(type_quals); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2085 | proto_info.RefQualifier = RQ_None; |
| 2086 | |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2087 | return CompilerType(ClangASTContext::GetASTContext(ast), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2088 | ast->getFunctionType(ClangUtil::GetQualType(result_type), |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2089 | qual_type_args, proto_info).getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
| 2092 | ParmVarDecl *ClangASTContext::CreateParameterDeclaration( |
Zachary Turner | 6753d2d | 2018-12-12 17:17:53 +0000 | [diff] [blame] | 2093 | clang::DeclContext *decl_ctx, const char *name, |
Shafik Yaghmour | fa5c340 | 2019-08-02 21:41:50 +0000 | [diff] [blame] | 2094 | const CompilerType ¶m_type, int storage, bool add_decl) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2095 | ASTContext *ast = getASTContext(); |
| 2096 | assert(ast != nullptr); |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 2097 | auto *decl = |
| 2098 | ParmVarDecl::Create(*ast, decl_ctx, SourceLocation(), SourceLocation(), |
| 2099 | name && name[0] ? &ast->Idents.get(name) : nullptr, |
| 2100 | ClangUtil::GetQualType(param_type), nullptr, |
| 2101 | (clang::StorageClass)storage, nullptr); |
Shafik Yaghmour | fa5c340 | 2019-08-02 21:41:50 +0000 | [diff] [blame] | 2102 | if (add_decl) |
| 2103 | decl_ctx->addDecl(decl); |
| 2104 | |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 2105 | return decl; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl, |
| 2109 | ParmVarDecl **params, |
| 2110 | unsigned num_params) { |
| 2111 | if (function_decl) |
| 2112 | function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 2115 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2116 | ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 2117 | QualType block_type = m_ast_up->getBlockPointerType( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2118 | clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType())); |
Greg Clayton | ceeb521 | 2016-05-26 22:33:25 +0000 | [diff] [blame] | 2119 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2120 | return CompilerType(this, block_type.getAsOpaquePtr()); |
Sean Callanan | c530ba9 | 2016-05-02 21:15:31 +0000 | [diff] [blame] | 2121 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2122 | |
| 2123 | #pragma mark Array Types |
| 2124 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2125 | CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type, |
| 2126 | size_t element_count, |
| 2127 | bool is_vector) { |
| 2128 | if (element_type.IsValid()) { |
| 2129 | ASTContext *ast = getASTContext(); |
| 2130 | assert(ast != nullptr); |
Greg Clayton | 4ef877f | 2012-12-06 02:33:54 +0000 | [diff] [blame] | 2131 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2132 | if (is_vector) { |
| 2133 | return CompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2134 | this, ast->getExtVectorType(ClangUtil::GetQualType(element_type), |
| 2135 | element_count) |
| 2136 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2137 | } else { |
| 2138 | |
| 2139 | llvm::APInt ap_element_count(64, element_count); |
| 2140 | if (element_count == 0) { |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2141 | return CompilerType(this, ast->getIncompleteArrayType( |
| 2142 | ClangUtil::GetQualType(element_type), |
| 2143 | clang::ArrayType::Normal, 0) |
| 2144 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2145 | } else { |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2146 | return CompilerType(this, ast->getConstantArrayType( |
| 2147 | ClangUtil::GetQualType(element_type), |
Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 2148 | ap_element_count, nullptr, |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2149 | clang::ArrayType::Normal, 0) |
| 2150 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2151 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2152 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2153 | } |
| 2154 | return CompilerType(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2155 | } |
| 2156 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2157 | CompilerType ClangASTContext::CreateStructForIdentifier( |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 2158 | ConstString type_name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2159 | const std::initializer_list<std::pair<const char *, CompilerType>> |
| 2160 | &type_fields, |
| 2161 | bool packed) { |
| 2162 | CompilerType type; |
| 2163 | if (!type_name.IsEmpty() && |
| 2164 | (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)) |
| 2165 | .IsValid()) { |
Pavel Labath | f31c9d2 | 2017-01-05 13:18:42 +0000 | [diff] [blame] | 2166 | lldbassert(0 && "Trying to create a type for an existing name"); |
Enrico Granata | 76b08d5 | 2014-10-29 23:08:02 +0000 | [diff] [blame] | 2167 | return type; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2168 | } |
| 2169 | |
| 2170 | type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(), |
| 2171 | clang::TTK_Struct, lldb::eLanguageTypeC); |
| 2172 | StartTagDeclarationDefinition(type); |
| 2173 | for (const auto &field : type_fields) |
| 2174 | AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic, |
| 2175 | 0); |
| 2176 | if (packed) |
| 2177 | SetIsPacked(type); |
| 2178 | CompleteTagDeclarationDefinition(type); |
| 2179 | return type; |
Enrico Granata | 76b08d5 | 2014-10-29 23:08:02 +0000 | [diff] [blame] | 2180 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2181 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2182 | CompilerType ClangASTContext::GetOrCreateStructForIdentifier( |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 2183 | ConstString type_name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2184 | const std::initializer_list<std::pair<const char *, CompilerType>> |
| 2185 | &type_fields, |
| 2186 | bool packed) { |
| 2187 | CompilerType type; |
| 2188 | if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid()) |
| 2189 | return type; |
Sean Callanan | c530ba9 | 2016-05-02 21:15:31 +0000 | [diff] [blame] | 2190 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2191 | return CreateStructForIdentifier(type_name, type_fields, packed); |
Sean Callanan | c530ba9 | 2016-05-02 21:15:31 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2194 | #pragma mark Enumeration Types |
| 2195 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 2196 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2197 | ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx, |
| 2198 | const Declaration &decl, |
Tamas Berghammer | 5976583 | 2017-11-07 10:39:22 +0000 | [diff] [blame] | 2199 | const CompilerType &integer_clang_type, |
| 2200 | bool is_scoped) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2201 | // TODO: Do something intelligent with the Declaration object passed in |
| 2202 | // like maybe filling in the SourceLocation with it... |
| 2203 | ASTContext *ast = getASTContext(); |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 2204 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2205 | // TODO: ask about these... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2206 | // const bool IsFixed = false; |
| 2207 | |
| 2208 | EnumDecl *enum_decl = EnumDecl::Create( |
| 2209 | *ast, decl_ctx, SourceLocation(), SourceLocation(), |
| 2210 | name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr, |
Tamas Berghammer | cf6bf4c | 2017-11-07 13:43:55 +0000 | [diff] [blame] | 2211 | is_scoped, // IsScoped |
| 2212 | is_scoped, // IsScopedUsingClassTag |
| 2213 | false); // IsFixed |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2214 | |
| 2215 | if (enum_decl) { |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 2216 | if (decl_ctx) |
| 2217 | decl_ctx->addDecl(enum_decl); |
| 2218 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2219 | // TODO: check if we should be setting the promotion type too? |
| 2220 | enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type)); |
| 2221 | |
| 2222 | enum_decl->setAccess(AS_public); // TODO respect what's in the debug info |
| 2223 | |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2224 | return CompilerType(this, ast->getTagDeclType(enum_decl).getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2225 | } |
| 2226 | return CompilerType(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2227 | } |
| 2228 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2229 | CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast, |
| 2230 | size_t bit_size, |
| 2231 | bool is_signed) { |
| 2232 | if (ast) { |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2233 | auto *clang_ast_context = ClangASTContext::GetASTContext(ast); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2234 | if (is_signed) { |
| 2235 | if (bit_size == ast->getTypeSize(ast->SignedCharTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2236 | return CompilerType(clang_ast_context, |
| 2237 | ast->SignedCharTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2238 | |
| 2239 | if (bit_size == ast->getTypeSize(ast->ShortTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2240 | return CompilerType(clang_ast_context, ast->ShortTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2241 | |
| 2242 | if (bit_size == ast->getTypeSize(ast->IntTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2243 | return CompilerType(clang_ast_context, ast->IntTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2244 | |
| 2245 | if (bit_size == ast->getTypeSize(ast->LongTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2246 | return CompilerType(clang_ast_context, ast->LongTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2247 | |
| 2248 | if (bit_size == ast->getTypeSize(ast->LongLongTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2249 | return CompilerType(clang_ast_context, |
| 2250 | ast->LongLongTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2251 | |
| 2252 | if (bit_size == ast->getTypeSize(ast->Int128Ty)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2253 | return CompilerType(clang_ast_context, ast->Int128Ty.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2254 | } else { |
| 2255 | if (bit_size == ast->getTypeSize(ast->UnsignedCharTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2256 | return CompilerType(clang_ast_context, |
| 2257 | ast->UnsignedCharTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2258 | |
| 2259 | if (bit_size == ast->getTypeSize(ast->UnsignedShortTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2260 | return CompilerType(clang_ast_context, |
| 2261 | ast->UnsignedShortTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2262 | |
| 2263 | if (bit_size == ast->getTypeSize(ast->UnsignedIntTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2264 | return CompilerType(clang_ast_context, |
| 2265 | ast->UnsignedIntTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2266 | |
| 2267 | if (bit_size == ast->getTypeSize(ast->UnsignedLongTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2268 | return CompilerType(clang_ast_context, |
| 2269 | ast->UnsignedLongTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2270 | |
| 2271 | if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2272 | return CompilerType(clang_ast_context, |
| 2273 | ast->UnsignedLongLongTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2274 | |
| 2275 | if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty)) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2276 | return CompilerType(clang_ast_context, |
| 2277 | ast->UnsignedInt128Ty.getAsOpaquePtr()); |
Enrico Granata | e8bf749 | 2014-08-15 23:00:02 +0000 | [diff] [blame] | 2278 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2279 | } |
| 2280 | return CompilerType(); |
Enrico Granata | e8bf749 | 2014-08-15 23:00:02 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2283 | CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast, |
| 2284 | bool is_signed) { |
| 2285 | if (ast) |
| 2286 | return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy), |
| 2287 | is_signed); |
| 2288 | return CompilerType(); |
Enrico Granata | e8bf749 | 2014-08-15 23:00:02 +0000 | [diff] [blame] | 2289 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2290 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2291 | void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) { |
| 2292 | if (decl_ctx) { |
| 2293 | DumpDeclContextHiearchy(decl_ctx->getParent()); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2294 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2295 | clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx); |
| 2296 | if (named_decl) { |
| 2297 | printf("%20s: %s\n", decl_ctx->getDeclKindName(), |
| 2298 | named_decl->getDeclName().getAsString().c_str()); |
| 2299 | } else { |
| 2300 | printf("%20s\n", decl_ctx->getDeclKindName()); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2301 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2302 | } |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2303 | } |
| 2304 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2305 | void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) { |
| 2306 | if (decl == nullptr) |
| 2307 | return; |
| 2308 | DumpDeclContextHiearchy(decl->getDeclContext()); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2309 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2310 | clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl); |
| 2311 | if (record_decl) { |
| 2312 | printf("%20s: %s%s\n", decl->getDeclKindName(), |
| 2313 | record_decl->getDeclName().getAsString().c_str(), |
| 2314 | record_decl->isInjectedClassName() ? " (injected class name)" : ""); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2315 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2316 | } else { |
| 2317 | clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl); |
| 2318 | if (named_decl) { |
| 2319 | printf("%20s: %s\n", decl->getDeclKindName(), |
| 2320 | named_decl->getDeclName().getAsString().c_str()); |
| 2321 | } else { |
| 2322 | printf("%20s\n", decl->getDeclKindName()); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2323 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2324 | } |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2325 | } |
| 2326 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2327 | bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl, |
| 2328 | clang::Decl *rhs_decl) { |
| 2329 | if (lhs_decl && rhs_decl) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2330 | // Make sure the decl kinds match first |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2331 | const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind(); |
| 2332 | const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind(); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2333 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2334 | if (lhs_decl_kind == rhs_decl_kind) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2335 | // Now check that the decl contexts kinds are all equivalent before we |
| 2336 | // have to check any names of the decl contexts... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2337 | clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext(); |
| 2338 | clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext(); |
| 2339 | if (lhs_decl_ctx && rhs_decl_ctx) { |
Jonas Devlieghere | 09ad8c8 | 2019-05-24 00:44:33 +0000 | [diff] [blame] | 2340 | while (true) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2341 | if (lhs_decl_ctx && rhs_decl_ctx) { |
| 2342 | const clang::Decl::Kind lhs_decl_ctx_kind = |
| 2343 | lhs_decl_ctx->getDeclKind(); |
| 2344 | const clang::Decl::Kind rhs_decl_ctx_kind = |
| 2345 | rhs_decl_ctx->getDeclKind(); |
| 2346 | if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) { |
| 2347 | lhs_decl_ctx = lhs_decl_ctx->getParent(); |
| 2348 | rhs_decl_ctx = rhs_decl_ctx->getParent(); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 2349 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2350 | if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr) |
| 2351 | break; |
| 2352 | } else |
| 2353 | return false; |
| 2354 | } else |
Tamas Berghammer | fcf334b | 2015-12-02 11:35:54 +0000 | [diff] [blame] | 2355 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2358 | // Now make sure the name of the decls match |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2359 | clang::NamedDecl *lhs_named_decl = |
| 2360 | llvm::dyn_cast<clang::NamedDecl>(lhs_decl); |
| 2361 | clang::NamedDecl *rhs_named_decl = |
| 2362 | llvm::dyn_cast<clang::NamedDecl>(rhs_decl); |
| 2363 | if (lhs_named_decl && rhs_named_decl) { |
| 2364 | clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName(); |
| 2365 | clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName(); |
| 2366 | if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) { |
| 2367 | if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString()) |
| 2368 | return false; |
| 2369 | } else |
Greg Clayton | a272147 | 2011-06-25 00:44:06 +0000 | [diff] [blame] | 2370 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2371 | } else |
| 2372 | return false; |
Greg Clayton | a272147 | 2011-06-25 00:44:06 +0000 | [diff] [blame] | 2373 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2374 | // We know that the decl context kinds all match, so now we need to |
| 2375 | // make sure the names match as well |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2376 | lhs_decl_ctx = lhs_decl->getDeclContext(); |
| 2377 | rhs_decl_ctx = rhs_decl->getDeclContext(); |
Jonas Devlieghere | 09ad8c8 | 2019-05-24 00:44:33 +0000 | [diff] [blame] | 2378 | while (true) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2379 | switch (lhs_decl_ctx->getDeclKind()) { |
| 2380 | case clang::Decl::TranslationUnit: |
| 2381 | // We don't care about the translation unit names |
| 2382 | return true; |
| 2383 | default: { |
| 2384 | clang::NamedDecl *lhs_named_decl = |
| 2385 | llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx); |
| 2386 | clang::NamedDecl *rhs_named_decl = |
| 2387 | llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx); |
| 2388 | if (lhs_named_decl && rhs_named_decl) { |
| 2389 | clang::DeclarationName lhs_decl_name = |
| 2390 | lhs_named_decl->getDeclName(); |
| 2391 | clang::DeclarationName rhs_decl_name = |
| 2392 | rhs_named_decl->getDeclName(); |
| 2393 | if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) { |
| 2394 | if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString()) |
| 2395 | return false; |
| 2396 | } else |
| 2397 | return false; |
| 2398 | } else |
| 2399 | return false; |
| 2400 | } break; |
| 2401 | } |
| 2402 | lhs_decl_ctx = lhs_decl_ctx->getParent(); |
| 2403 | rhs_decl_ctx = rhs_decl_ctx->getParent(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2404 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2405 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2406 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2407 | } |
| 2408 | return false; |
| 2409 | } |
| 2410 | bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast, |
| 2411 | clang::Decl *decl) { |
| 2412 | if (!decl) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2413 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2414 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2415 | ExternalASTSource *ast_source = ast->getExternalSource(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2416 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2417 | if (!ast_source) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2418 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2419 | |
| 2420 | if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) { |
| 2421 | if (tag_decl->isCompleteDefinition()) |
| 2422 | return true; |
| 2423 | |
| 2424 | if (!tag_decl->hasExternalLexicalStorage()) |
| 2425 | return false; |
| 2426 | |
| 2427 | ast_source->CompleteType(tag_decl); |
| 2428 | |
| 2429 | return !tag_decl->getTypeForDecl()->isIncompleteType(); |
| 2430 | } else if (clang::ObjCInterfaceDecl *objc_interface_decl = |
| 2431 | llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) { |
| 2432 | if (objc_interface_decl->getDefinition()) |
| 2433 | return true; |
| 2434 | |
| 2435 | if (!objc_interface_decl->hasExternalLexicalStorage()) |
| 2436 | return false; |
| 2437 | |
| 2438 | ast_source->CompleteType(objc_interface_decl); |
| 2439 | |
| 2440 | return !objc_interface_decl->getTypeForDecl()->isIncompleteType(); |
| 2441 | } else { |
| 2442 | return false; |
| 2443 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2446 | void ClangASTContext::SetMetadataAsUserID(const void *object, |
| 2447 | user_id_t user_id) { |
| 2448 | ClangASTMetadata meta_data; |
| 2449 | meta_data.SetUserID(user_id); |
| 2450 | SetMetadata(object, meta_data); |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 2451 | } |
| 2452 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2453 | void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object, |
| 2454 | ClangASTMetadata &metadata) { |
| 2455 | ClangExternalASTSourceCommon *external_source = |
| 2456 | ClangExternalASTSourceCommon::Lookup(ast->getExternalSource()); |
| 2457 | |
| 2458 | if (external_source) |
| 2459 | external_source->SetMetadata(object, metadata); |
| 2460 | } |
| 2461 | |
| 2462 | ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast, |
| 2463 | const void *object) { |
| 2464 | ClangExternalASTSourceCommon *external_source = |
| 2465 | ClangExternalASTSourceCommon::Lookup(ast->getExternalSource()); |
| 2466 | |
| 2467 | if (external_source && external_source->HasMetadata(object)) |
| 2468 | return external_source->GetMetadata(object); |
| 2469 | else |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2470 | return nullptr; |
| 2471 | } |
| 2472 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2473 | bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type, |
| 2474 | int kind) const { |
| 2475 | const clang::Type *clang_type = tag_qual_type.getTypePtr(); |
| 2476 | if (clang_type) { |
| 2477 | const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type); |
| 2478 | if (tag_type) { |
| 2479 | clang::TagDecl *tag_decl = |
| 2480 | llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl()); |
| 2481 | if (tag_decl) { |
| 2482 | tag_decl->setTagKind((clang::TagDecl::TagKind)kind); |
| 2483 | return true; |
| 2484 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2485 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2486 | } |
| 2487 | return false; |
| 2488 | } |
| 2489 | |
| 2490 | bool ClangASTContext::SetDefaultAccessForRecordFields( |
| 2491 | clang::RecordDecl *record_decl, int default_accessibility, |
| 2492 | int *assigned_accessibilities, size_t num_assigned_accessibilities) { |
| 2493 | if (record_decl) { |
| 2494 | uint32_t field_idx; |
| 2495 | clang::RecordDecl::field_iterator field, field_end; |
| 2496 | for (field = record_decl->field_begin(), |
| 2497 | field_end = record_decl->field_end(), field_idx = 0; |
| 2498 | field != field_end; ++field, ++field_idx) { |
| 2499 | // If no accessibility was assigned, assign the correct one |
| 2500 | if (field_idx < num_assigned_accessibilities && |
| 2501 | assigned_accessibilities[field_idx] == clang::AS_none) |
| 2502 | field->setAccess((clang::AccessSpecifier)default_accessibility); |
| 2503 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2504 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2505 | } |
| 2506 | return false; |
| 2507 | } |
| 2508 | |
| 2509 | clang::DeclContext * |
| 2510 | ClangASTContext::GetDeclContextForType(const CompilerType &type) { |
| 2511 | return GetDeclContextForType(ClangUtil::GetQualType(type)); |
| 2512 | } |
| 2513 | |
| 2514 | clang::DeclContext * |
| 2515 | ClangASTContext::GetDeclContextForType(clang::QualType type) { |
| 2516 | if (type.isNull()) |
| 2517 | return nullptr; |
| 2518 | |
| 2519 | clang::QualType qual_type = type.getCanonicalType(); |
| 2520 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 2521 | switch (type_class) { |
| 2522 | case clang::Type::ObjCInterface: |
| 2523 | return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr()) |
| 2524 | ->getInterface(); |
| 2525 | case clang::Type::ObjCObjectPointer: |
| 2526 | return GetDeclContextForType( |
| 2527 | llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr()) |
| 2528 | ->getPointeeType()); |
| 2529 | case clang::Type::Record: |
| 2530 | return llvm::cast<clang::RecordType>(qual_type)->getDecl(); |
| 2531 | case clang::Type::Enum: |
| 2532 | return llvm::cast<clang::EnumType>(qual_type)->getDecl(); |
| 2533 | case clang::Type::Typedef: |
| 2534 | return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type) |
| 2535 | ->getDecl() |
| 2536 | ->getUnderlyingType()); |
| 2537 | case clang::Type::Auto: |
| 2538 | return GetDeclContextForType( |
| 2539 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()); |
| 2540 | case clang::Type::Elaborated: |
| 2541 | return GetDeclContextForType( |
| 2542 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()); |
| 2543 | case clang::Type::Paren: |
| 2544 | return GetDeclContextForType( |
| 2545 | llvm::cast<clang::ParenType>(qual_type)->desugar()); |
| 2546 | default: |
| 2547 | break; |
| 2548 | } |
| 2549 | // No DeclContext in this type... |
| 2550 | return nullptr; |
| 2551 | } |
| 2552 | |
| 2553 | static bool GetCompleteQualType(clang::ASTContext *ast, |
| 2554 | clang::QualType qual_type, |
| 2555 | bool allow_completion = true) { |
| 2556 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 2557 | switch (type_class) { |
| 2558 | case clang::Type::ConstantArray: |
| 2559 | case clang::Type::IncompleteArray: |
| 2560 | case clang::Type::VariableArray: { |
| 2561 | const clang::ArrayType *array_type = |
| 2562 | llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr()); |
| 2563 | |
| 2564 | if (array_type) |
| 2565 | return GetCompleteQualType(ast, array_type->getElementType(), |
| 2566 | allow_completion); |
| 2567 | } break; |
| 2568 | case clang::Type::Record: { |
| 2569 | clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); |
| 2570 | if (cxx_record_decl) { |
| 2571 | if (cxx_record_decl->hasExternalLexicalStorage()) { |
| 2572 | const bool is_complete = cxx_record_decl->isCompleteDefinition(); |
| 2573 | const bool fields_loaded = |
| 2574 | cxx_record_decl->hasLoadedFieldsFromExternalStorage(); |
| 2575 | if (is_complete && fields_loaded) |
| 2576 | return true; |
| 2577 | |
| 2578 | if (!allow_completion) |
| 2579 | return false; |
| 2580 | |
| 2581 | // Call the field_begin() accessor to for it to use the external source |
| 2582 | // to load the fields... |
| 2583 | clang::ExternalASTSource *external_ast_source = |
| 2584 | ast->getExternalSource(); |
| 2585 | if (external_ast_source) { |
| 2586 | external_ast_source->CompleteType(cxx_record_decl); |
| 2587 | if (cxx_record_decl->isCompleteDefinition()) { |
| 2588 | cxx_record_decl->field_begin(); |
| 2589 | cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true); |
| 2590 | } |
| 2591 | } |
| 2592 | } |
| 2593 | } |
| 2594 | const clang::TagType *tag_type = |
| 2595 | llvm::cast<clang::TagType>(qual_type.getTypePtr()); |
| 2596 | return !tag_type->isIncompleteType(); |
| 2597 | } break; |
| 2598 | |
| 2599 | case clang::Type::Enum: { |
| 2600 | const clang::TagType *tag_type = |
| 2601 | llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()); |
| 2602 | if (tag_type) { |
| 2603 | clang::TagDecl *tag_decl = tag_type->getDecl(); |
| 2604 | if (tag_decl) { |
| 2605 | if (tag_decl->getDefinition()) |
| 2606 | return true; |
| 2607 | |
| 2608 | if (!allow_completion) |
| 2609 | return false; |
| 2610 | |
| 2611 | if (tag_decl->hasExternalLexicalStorage()) { |
| 2612 | if (ast) { |
| 2613 | clang::ExternalASTSource *external_ast_source = |
| 2614 | ast->getExternalSource(); |
| 2615 | if (external_ast_source) { |
| 2616 | external_ast_source->CompleteType(tag_decl); |
| 2617 | return !tag_type->isIncompleteType(); |
| 2618 | } |
| 2619 | } |
| 2620 | } |
| 2621 | return false; |
| 2622 | } |
| 2623 | } |
| 2624 | |
| 2625 | } break; |
| 2626 | case clang::Type::ObjCObject: |
| 2627 | case clang::Type::ObjCInterface: { |
| 2628 | const clang::ObjCObjectType *objc_class_type = |
| 2629 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type); |
| 2630 | if (objc_class_type) { |
| 2631 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 2632 | objc_class_type->getInterface(); |
| 2633 | // We currently can't complete objective C types through the newly added |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2634 | // ASTContext because it only supports TagDecl objects right now... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2635 | if (class_interface_decl) { |
| 2636 | if (class_interface_decl->getDefinition()) |
| 2637 | return true; |
| 2638 | |
| 2639 | if (!allow_completion) |
| 2640 | return false; |
| 2641 | |
| 2642 | if (class_interface_decl->hasExternalLexicalStorage()) { |
| 2643 | if (ast) { |
| 2644 | clang::ExternalASTSource *external_ast_source = |
| 2645 | ast->getExternalSource(); |
| 2646 | if (external_ast_source) { |
| 2647 | external_ast_source->CompleteType(class_interface_decl); |
| 2648 | return !objc_class_type->isIncompleteType(); |
| 2649 | } |
| 2650 | } |
| 2651 | } |
| 2652 | return false; |
| 2653 | } |
| 2654 | } |
| 2655 | } break; |
| 2656 | |
| 2657 | case clang::Type::Typedef: |
| 2658 | return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type) |
| 2659 | ->getDecl() |
| 2660 | ->getUnderlyingType(), |
| 2661 | allow_completion); |
| 2662 | |
| 2663 | case clang::Type::Auto: |
| 2664 | return GetCompleteQualType( |
| 2665 | ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(), |
| 2666 | allow_completion); |
| 2667 | |
| 2668 | case clang::Type::Elaborated: |
| 2669 | return GetCompleteQualType( |
| 2670 | ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(), |
| 2671 | allow_completion); |
| 2672 | |
| 2673 | case clang::Type::Paren: |
| 2674 | return GetCompleteQualType( |
| 2675 | ast, llvm::cast<clang::ParenType>(qual_type)->desugar(), |
| 2676 | allow_completion); |
| 2677 | |
| 2678 | case clang::Type::Attributed: |
| 2679 | return GetCompleteQualType( |
| 2680 | ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(), |
| 2681 | allow_completion); |
| 2682 | |
| 2683 | default: |
| 2684 | break; |
| 2685 | } |
| 2686 | |
| 2687 | return true; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2688 | } |
| 2689 | |
| 2690 | static clang::ObjCIvarDecl::AccessControl |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2691 | ConvertAccessTypeToObjCIvarAccessControl(AccessType access) { |
| 2692 | switch (access) { |
| 2693 | case eAccessNone: |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2694 | return clang::ObjCIvarDecl::None; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2695 | case eAccessPublic: |
| 2696 | return clang::ObjCIvarDecl::Public; |
| 2697 | case eAccessPrivate: |
| 2698 | return clang::ObjCIvarDecl::Private; |
| 2699 | case eAccessProtected: |
| 2700 | return clang::ObjCIvarDecl::Protected; |
| 2701 | case eAccessPackage: |
| 2702 | return clang::ObjCIvarDecl::Package; |
| 2703 | } |
| 2704 | return clang::ObjCIvarDecl::None; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2705 | } |
| 2706 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2707 | // Tests |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2708 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2709 | bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) { |
| 2710 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 2711 | |
| 2712 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 2713 | switch (type_class) { |
| 2714 | case clang::Type::IncompleteArray: |
| 2715 | case clang::Type::VariableArray: |
| 2716 | case clang::Type::ConstantArray: |
| 2717 | case clang::Type::ExtVector: |
| 2718 | case clang::Type::Vector: |
| 2719 | case clang::Type::Record: |
| 2720 | case clang::Type::ObjCObject: |
| 2721 | case clang::Type::ObjCInterface: |
| 2722 | return true; |
| 2723 | case clang::Type::Auto: |
| 2724 | return IsAggregateType(llvm::cast<clang::AutoType>(qual_type) |
| 2725 | ->getDeducedType() |
| 2726 | .getAsOpaquePtr()); |
| 2727 | case clang::Type::Elaborated: |
| 2728 | return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 2729 | ->getNamedType() |
| 2730 | .getAsOpaquePtr()); |
| 2731 | case clang::Type::Typedef: |
| 2732 | return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type) |
| 2733 | ->getDecl() |
| 2734 | ->getUnderlyingType() |
| 2735 | .getAsOpaquePtr()); |
| 2736 | case clang::Type::Paren: |
| 2737 | return IsAggregateType( |
| 2738 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()); |
| 2739 | default: |
| 2740 | break; |
| 2741 | } |
| 2742 | // The clang type does have a value |
| 2743 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2744 | } |
| 2745 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2746 | bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) { |
| 2747 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 2748 | |
| 2749 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 2750 | switch (type_class) { |
| 2751 | case clang::Type::Record: { |
| 2752 | if (const clang::RecordType *record_type = |
| 2753 | llvm::dyn_cast_or_null<clang::RecordType>( |
| 2754 | qual_type.getTypePtrOrNull())) { |
| 2755 | if (const clang::RecordDecl *record_decl = record_type->getDecl()) { |
| 2756 | return record_decl->isAnonymousStructOrUnion(); |
| 2757 | } |
Enrico Granata | 7123e2b | 2015-11-07 02:06:57 +0000 | [diff] [blame] | 2758 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2759 | break; |
| 2760 | } |
| 2761 | case clang::Type::Auto: |
| 2762 | return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type) |
| 2763 | ->getDeducedType() |
| 2764 | .getAsOpaquePtr()); |
| 2765 | case clang::Type::Elaborated: |
| 2766 | return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 2767 | ->getNamedType() |
| 2768 | .getAsOpaquePtr()); |
| 2769 | case clang::Type::Typedef: |
| 2770 | return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type) |
| 2771 | ->getDecl() |
| 2772 | ->getUnderlyingType() |
| 2773 | .getAsOpaquePtr()); |
| 2774 | case clang::Type::Paren: |
| 2775 | return IsAnonymousType( |
| 2776 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()); |
| 2777 | default: |
| 2778 | break; |
| 2779 | } |
| 2780 | // The clang type does have a value |
| 2781 | return false; |
Enrico Granata | 7123e2b | 2015-11-07 02:06:57 +0000 | [diff] [blame] | 2782 | } |
| 2783 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2784 | bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type, |
| 2785 | CompilerType *element_type_ptr, |
| 2786 | uint64_t *size, bool *is_incomplete) { |
| 2787 | clang::QualType qual_type(GetCanonicalQualType(type)); |
Tamas Berghammer | 69d0b33 | 2015-10-09 12:43:08 +0000 | [diff] [blame] | 2788 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2789 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 2790 | switch (type_class) { |
| 2791 | default: |
| 2792 | break; |
Tamas Berghammer | 69d0b33 | 2015-10-09 12:43:08 +0000 | [diff] [blame] | 2793 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2794 | case clang::Type::ConstantArray: |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2795 | if (element_type_ptr) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2796 | element_type_ptr->SetCompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2797 | this, llvm::cast<clang::ConstantArrayType>(qual_type) |
| 2798 | ->getElementType() |
| 2799 | .getAsOpaquePtr()); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2800 | if (size) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2801 | *size = llvm::cast<clang::ConstantArrayType>(qual_type) |
| 2802 | ->getSize() |
| 2803 | .getLimitedValue(ULLONG_MAX); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2804 | if (is_incomplete) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2805 | *is_incomplete = false; |
| 2806 | return true; |
| 2807 | |
| 2808 | case clang::Type::IncompleteArray: |
| 2809 | if (element_type_ptr) |
| 2810 | element_type_ptr->SetCompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2811 | this, llvm::cast<clang::IncompleteArrayType>(qual_type) |
| 2812 | ->getElementType() |
| 2813 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2814 | if (size) |
| 2815 | *size = 0; |
| 2816 | if (is_incomplete) |
| 2817 | *is_incomplete = true; |
| 2818 | return true; |
| 2819 | |
| 2820 | case clang::Type::VariableArray: |
| 2821 | if (element_type_ptr) |
| 2822 | element_type_ptr->SetCompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2823 | this, llvm::cast<clang::VariableArrayType>(qual_type) |
| 2824 | ->getElementType() |
| 2825 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2826 | if (size) |
| 2827 | *size = 0; |
| 2828 | if (is_incomplete) |
| 2829 | *is_incomplete = false; |
| 2830 | return true; |
| 2831 | |
| 2832 | case clang::Type::DependentSizedArray: |
| 2833 | if (element_type_ptr) |
| 2834 | element_type_ptr->SetCompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2835 | this, llvm::cast<clang::DependentSizedArrayType>(qual_type) |
| 2836 | ->getElementType() |
| 2837 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2838 | if (size) |
| 2839 | *size = 0; |
| 2840 | if (is_incomplete) |
| 2841 | *is_incomplete = false; |
| 2842 | return true; |
| 2843 | |
| 2844 | case clang::Type::Typedef: |
| 2845 | return IsArrayType(llvm::cast<clang::TypedefType>(qual_type) |
| 2846 | ->getDecl() |
| 2847 | ->getUnderlyingType() |
| 2848 | .getAsOpaquePtr(), |
| 2849 | element_type_ptr, size, is_incomplete); |
| 2850 | case clang::Type::Auto: |
| 2851 | return IsArrayType(llvm::cast<clang::AutoType>(qual_type) |
| 2852 | ->getDeducedType() |
| 2853 | .getAsOpaquePtr(), |
| 2854 | element_type_ptr, size, is_incomplete); |
| 2855 | case clang::Type::Elaborated: |
| 2856 | return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 2857 | ->getNamedType() |
| 2858 | .getAsOpaquePtr(), |
| 2859 | element_type_ptr, size, is_incomplete); |
| 2860 | case clang::Type::Paren: |
| 2861 | return IsArrayType( |
| 2862 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 2863 | element_type_ptr, size, is_incomplete); |
| 2864 | } |
| 2865 | if (element_type_ptr) |
| 2866 | element_type_ptr->Clear(); |
| 2867 | if (size) |
| 2868 | *size = 0; |
| 2869 | if (is_incomplete) |
| 2870 | *is_incomplete = false; |
| 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::IsVectorType(lldb::opaque_compiler_type_t type, |
| 2875 | CompilerType *element_type, uint64_t *size) { |
| 2876 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 2877 | |
| 2878 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 2879 | switch (type_class) { |
| 2880 | case clang::Type::Vector: { |
| 2881 | const clang::VectorType *vector_type = |
| 2882 | qual_type->getAs<clang::VectorType>(); |
| 2883 | if (vector_type) { |
| 2884 | if (size) |
| 2885 | *size = vector_type->getNumElements(); |
| 2886 | if (element_type) |
| 2887 | *element_type = |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2888 | CompilerType(this, vector_type->getElementType().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2889 | } |
| 2890 | return true; |
| 2891 | } break; |
| 2892 | case clang::Type::ExtVector: { |
| 2893 | const clang::ExtVectorType *ext_vector_type = |
| 2894 | qual_type->getAs<clang::ExtVectorType>(); |
| 2895 | if (ext_vector_type) { |
| 2896 | if (size) |
| 2897 | *size = ext_vector_type->getNumElements(); |
| 2898 | if (element_type) |
| 2899 | *element_type = |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 2900 | CompilerType(this, ext_vector_type->getElementType().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2901 | } |
| 2902 | return true; |
| 2903 | } |
| 2904 | default: |
| 2905 | break; |
| 2906 | } |
| 2907 | return false; |
| 2908 | } |
| 2909 | |
| 2910 | bool ClangASTContext::IsRuntimeGeneratedType( |
| 2911 | lldb::opaque_compiler_type_t type) { |
| 2912 | clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext()) |
| 2913 | ->GetDeclContextForType(GetQualType(type)); |
| 2914 | if (!decl_ctx) |
| 2915 | return false; |
| 2916 | |
| 2917 | if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx)) |
| 2918 | return false; |
| 2919 | |
| 2920 | clang::ObjCInterfaceDecl *result_iface_decl = |
| 2921 | llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx); |
| 2922 | |
| 2923 | ClangASTMetadata *ast_metadata = |
| 2924 | ClangASTContext::GetMetadata(getASTContext(), result_iface_decl); |
| 2925 | if (!ast_metadata) |
| 2926 | return false; |
| 2927 | return (ast_metadata->GetISAPtr() != 0); |
| 2928 | } |
| 2929 | |
| 2930 | bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) { |
| 2931 | return GetQualType(type).getUnqualifiedType()->isCharType(); |
| 2932 | } |
| 2933 | |
| 2934 | bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) { |
| 2935 | const bool allow_completion = false; |
| 2936 | return GetCompleteQualType(getASTContext(), GetQualType(type), |
| 2937 | allow_completion); |
| 2938 | } |
| 2939 | |
| 2940 | bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) { |
| 2941 | return GetQualType(type).isConstQualified(); |
| 2942 | } |
| 2943 | |
| 2944 | bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type, |
| 2945 | uint32_t &length) { |
| 2946 | CompilerType pointee_or_element_clang_type; |
| 2947 | length = 0; |
| 2948 | Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type)); |
| 2949 | |
| 2950 | if (!pointee_or_element_clang_type.IsValid()) |
| 2951 | return false; |
| 2952 | |
| 2953 | if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) { |
| 2954 | if (pointee_or_element_clang_type.IsCharType()) { |
| 2955 | if (type_flags.Test(eTypeIsArray)) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2956 | // We know the size of the array and it could be a C string since it is |
| 2957 | // an array of characters |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2958 | length = llvm::cast<clang::ConstantArrayType>( |
| 2959 | GetCanonicalQualType(type).getTypePtr()) |
| 2960 | ->getSize() |
| 2961 | .getLimitedValue(); |
| 2962 | } |
| 2963 | return true; |
| 2964 | } |
| 2965 | } |
| 2966 | return false; |
| 2967 | } |
| 2968 | |
| 2969 | bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type, |
| 2970 | bool *is_variadic_ptr) { |
| 2971 | if (type) { |
| 2972 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 2973 | |
| 2974 | if (qual_type->isFunctionType()) { |
| 2975 | if (is_variadic_ptr) { |
| 2976 | const clang::FunctionProtoType *function_proto_type = |
| 2977 | llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr()); |
| 2978 | if (function_proto_type) |
| 2979 | *is_variadic_ptr = function_proto_type->isVariadic(); |
| 2980 | else |
| 2981 | *is_variadic_ptr = false; |
| 2982 | } |
| 2983 | return true; |
| 2984 | } |
| 2985 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 2986 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2987 | switch (type_class) { |
| 2988 | default: |
| 2989 | break; |
| 2990 | case clang::Type::Typedef: |
| 2991 | return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type) |
| 2992 | ->getDecl() |
| 2993 | ->getUnderlyingType() |
| 2994 | .getAsOpaquePtr(), |
| 2995 | nullptr); |
| 2996 | case clang::Type::Auto: |
| 2997 | return IsFunctionType(llvm::cast<clang::AutoType>(qual_type) |
| 2998 | ->getDeducedType() |
| 2999 | .getAsOpaquePtr(), |
| 3000 | nullptr); |
| 3001 | case clang::Type::Elaborated: |
| 3002 | return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 3003 | ->getNamedType() |
| 3004 | .getAsOpaquePtr(), |
| 3005 | nullptr); |
| 3006 | case clang::Type::Paren: |
| 3007 | return IsFunctionType( |
| 3008 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 3009 | nullptr); |
| 3010 | case clang::Type::LValueReference: |
| 3011 | case clang::Type::RValueReference: { |
| 3012 | const clang::ReferenceType *reference_type = |
| 3013 | llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()); |
| 3014 | if (reference_type) |
| 3015 | return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(), |
| 3016 | nullptr); |
| 3017 | } break; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3018 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3019 | } |
| 3020 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3021 | } |
| 3022 | |
| 3023 | // Used to detect "Homogeneous Floating-point Aggregates" |
| 3024 | uint32_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3025 | ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type, |
| 3026 | CompilerType *base_type_ptr) { |
| 3027 | if (!type) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3028 | return 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3029 | |
| 3030 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3031 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3032 | switch (type_class) { |
| 3033 | case clang::Type::Record: |
| 3034 | if (GetCompleteType(type)) { |
| 3035 | const clang::CXXRecordDecl *cxx_record_decl = |
| 3036 | qual_type->getAsCXXRecordDecl(); |
| 3037 | if (cxx_record_decl) { |
| 3038 | if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass()) |
| 3039 | return 0; |
| 3040 | } |
| 3041 | const clang::RecordType *record_type = |
| 3042 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 3043 | if (record_type) { |
| 3044 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 3045 | if (record_decl) { |
| 3046 | // We are looking for a structure that contains only floating point |
| 3047 | // types |
| 3048 | clang::RecordDecl::field_iterator field_pos, |
| 3049 | field_end = record_decl->field_end(); |
| 3050 | uint32_t num_fields = 0; |
| 3051 | bool is_hva = false; |
| 3052 | bool is_hfa = false; |
| 3053 | clang::QualType base_qual_type; |
| 3054 | uint64_t base_bitwidth = 0; |
| 3055 | for (field_pos = record_decl->field_begin(); field_pos != field_end; |
| 3056 | ++field_pos) { |
| 3057 | clang::QualType field_qual_type = field_pos->getType(); |
| 3058 | uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type); |
| 3059 | if (field_qual_type->isFloatingType()) { |
| 3060 | if (field_qual_type->isComplexType()) |
| 3061 | return 0; |
| 3062 | else { |
| 3063 | if (num_fields == 0) |
| 3064 | base_qual_type = field_qual_type; |
| 3065 | else { |
| 3066 | if (is_hva) |
| 3067 | return 0; |
| 3068 | is_hfa = true; |
| 3069 | if (field_qual_type.getTypePtr() != |
| 3070 | base_qual_type.getTypePtr()) |
| 3071 | return 0; |
| 3072 | } |
| 3073 | } |
| 3074 | } else if (field_qual_type->isVectorType() || |
| 3075 | field_qual_type->isExtVectorType()) { |
| 3076 | if (num_fields == 0) { |
| 3077 | base_qual_type = field_qual_type; |
| 3078 | base_bitwidth = field_bitwidth; |
| 3079 | } else { |
| 3080 | if (is_hfa) |
| 3081 | return 0; |
| 3082 | is_hva = true; |
| 3083 | if (base_bitwidth != field_bitwidth) |
| 3084 | return 0; |
| 3085 | if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr()) |
| 3086 | return 0; |
| 3087 | } |
| 3088 | } else |
| 3089 | return 0; |
| 3090 | ++num_fields; |
| 3091 | } |
| 3092 | if (base_type_ptr) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 3093 | *base_type_ptr = CompilerType(this, base_qual_type.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3094 | return num_fields; |
| 3095 | } |
| 3096 | } |
| 3097 | } |
| 3098 | break; |
| 3099 | |
| 3100 | case clang::Type::Typedef: |
| 3101 | return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type) |
| 3102 | ->getDecl() |
| 3103 | ->getUnderlyingType() |
| 3104 | .getAsOpaquePtr(), |
| 3105 | base_type_ptr); |
| 3106 | |
| 3107 | case clang::Type::Auto: |
| 3108 | return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type) |
| 3109 | ->getDeducedType() |
| 3110 | .getAsOpaquePtr(), |
| 3111 | base_type_ptr); |
| 3112 | |
| 3113 | case clang::Type::Elaborated: |
| 3114 | return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type) |
| 3115 | ->getNamedType() |
| 3116 | .getAsOpaquePtr(), |
| 3117 | base_type_ptr); |
| 3118 | default: |
| 3119 | break; |
| 3120 | } |
| 3121 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3122 | } |
| 3123 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3124 | size_t ClangASTContext::GetNumberOfFunctionArguments( |
| 3125 | lldb::opaque_compiler_type_t type) { |
| 3126 | if (type) { |
| 3127 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3128 | const clang::FunctionProtoType *func = |
| 3129 | llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr()); |
| 3130 | if (func) |
| 3131 | return func->getNumParams(); |
| 3132 | } |
| 3133 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3134 | } |
| 3135 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 3136 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3137 | ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type, |
| 3138 | const size_t index) { |
| 3139 | if (type) { |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3140 | clang::QualType qual_type(GetQualType(type)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3141 | const clang::FunctionProtoType *func = |
| 3142 | llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr()); |
| 3143 | if (func) { |
| 3144 | if (index < func->getNumParams()) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 3145 | return CompilerType(this, func->getParamType(index).getAsOpaquePtr()); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3146 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3147 | } |
| 3148 | return CompilerType(); |
| 3149 | } |
| 3150 | |
| 3151 | bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) { |
| 3152 | if (type) { |
| 3153 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3154 | |
| 3155 | if (qual_type->isFunctionPointerType()) |
| 3156 | return true; |
| 3157 | |
| 3158 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3159 | switch (type_class) { |
| 3160 | default: |
| 3161 | break; |
| 3162 | case clang::Type::Typedef: |
| 3163 | return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type) |
| 3164 | ->getDecl() |
| 3165 | ->getUnderlyingType() |
| 3166 | .getAsOpaquePtr()); |
| 3167 | case clang::Type::Auto: |
| 3168 | return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type) |
| 3169 | ->getDeducedType() |
| 3170 | .getAsOpaquePtr()); |
| 3171 | case clang::Type::Elaborated: |
| 3172 | return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 3173 | ->getNamedType() |
| 3174 | .getAsOpaquePtr()); |
| 3175 | case clang::Type::Paren: |
| 3176 | return IsFunctionPointerType( |
| 3177 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()); |
| 3178 | |
| 3179 | case clang::Type::LValueReference: |
| 3180 | case clang::Type::RValueReference: { |
| 3181 | const clang::ReferenceType *reference_type = |
| 3182 | llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()); |
| 3183 | if (reference_type) |
| 3184 | return IsFunctionPointerType( |
| 3185 | reference_type->getPointeeType().getAsOpaquePtr()); |
| 3186 | } break; |
| 3187 | } |
| 3188 | } |
| 3189 | return false; |
| 3190 | } |
| 3191 | |
| 3192 | bool ClangASTContext::IsBlockPointerType( |
| 3193 | lldb::opaque_compiler_type_t type, |
| 3194 | CompilerType *function_pointer_type_ptr) { |
| 3195 | if (type) { |
| 3196 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3197 | |
| 3198 | if (qual_type->isBlockPointerType()) { |
| 3199 | if (function_pointer_type_ptr) { |
| 3200 | const clang::BlockPointerType *block_pointer_type = |
| 3201 | qual_type->getAs<clang::BlockPointerType>(); |
| 3202 | QualType pointee_type = block_pointer_type->getPointeeType(); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 3203 | QualType function_pointer_type = m_ast_up->getPointerType(pointee_type); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3204 | *function_pointer_type_ptr = |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 3205 | CompilerType(this, function_pointer_type.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3206 | } |
| 3207 | return true; |
| 3208 | } |
| 3209 | |
| 3210 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3211 | switch (type_class) { |
| 3212 | default: |
| 3213 | break; |
| 3214 | case clang::Type::Typedef: |
| 3215 | return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type) |
| 3216 | ->getDecl() |
| 3217 | ->getUnderlyingType() |
| 3218 | .getAsOpaquePtr(), |
| 3219 | function_pointer_type_ptr); |
| 3220 | case clang::Type::Auto: |
| 3221 | return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type) |
| 3222 | ->getDeducedType() |
| 3223 | .getAsOpaquePtr(), |
| 3224 | function_pointer_type_ptr); |
| 3225 | case clang::Type::Elaborated: |
| 3226 | return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 3227 | ->getNamedType() |
| 3228 | .getAsOpaquePtr(), |
| 3229 | function_pointer_type_ptr); |
| 3230 | case clang::Type::Paren: |
| 3231 | return IsBlockPointerType( |
| 3232 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 3233 | function_pointer_type_ptr); |
| 3234 | |
| 3235 | case clang::Type::LValueReference: |
| 3236 | case clang::Type::RValueReference: { |
| 3237 | const clang::ReferenceType *reference_type = |
| 3238 | llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()); |
| 3239 | if (reference_type) |
| 3240 | return IsBlockPointerType( |
| 3241 | reference_type->getPointeeType().getAsOpaquePtr(), |
| 3242 | function_pointer_type_ptr); |
| 3243 | } break; |
| 3244 | } |
| 3245 | } |
| 3246 | return false; |
| 3247 | } |
| 3248 | |
| 3249 | bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type, |
| 3250 | bool &is_signed) { |
| 3251 | if (!type) |
| 3252 | return false; |
| 3253 | |
| 3254 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3255 | const clang::BuiltinType *builtin_type = |
| 3256 | llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal()); |
| 3257 | |
| 3258 | if (builtin_type) { |
| 3259 | if (builtin_type->isInteger()) { |
| 3260 | is_signed = builtin_type->isSignedInteger(); |
| 3261 | return true; |
| 3262 | } |
| 3263 | } |
| 3264 | |
| 3265 | return false; |
| 3266 | } |
| 3267 | |
| 3268 | bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type, |
| 3269 | bool &is_signed) { |
| 3270 | if (type) { |
| 3271 | const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>( |
| 3272 | GetCanonicalQualType(type)->getCanonicalTypeInternal()); |
| 3273 | |
| 3274 | if (enum_type) { |
| 3275 | IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(), |
| 3276 | is_signed); |
| 3277 | return true; |
| 3278 | } |
| 3279 | } |
| 3280 | |
| 3281 | return false; |
| 3282 | } |
| 3283 | |
| 3284 | bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type, |
| 3285 | CompilerType *pointee_type) { |
| 3286 | if (type) { |
| 3287 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3288 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3289 | switch (type_class) { |
| 3290 | case clang::Type::Builtin: |
| 3291 | switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { |
| 3292 | default: |
| 3293 | break; |
| 3294 | case clang::BuiltinType::ObjCId: |
| 3295 | case clang::BuiltinType::ObjCClass: |
| 3296 | return true; |
| 3297 | } |
| 3298 | return false; |
| 3299 | case clang::Type::ObjCObjectPointer: |
| 3300 | if (pointee_type) |
| 3301 | pointee_type->SetCompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 3302 | this, llvm::cast<clang::ObjCObjectPointerType>(qual_type) |
| 3303 | ->getPointeeType() |
| 3304 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3305 | return true; |
| 3306 | case clang::Type::BlockPointer: |
| 3307 | if (pointee_type) |
| 3308 | pointee_type->SetCompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 3309 | this, llvm::cast<clang::BlockPointerType>(qual_type) |
| 3310 | ->getPointeeType() |
| 3311 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3312 | return true; |
| 3313 | case clang::Type::Pointer: |
| 3314 | if (pointee_type) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 3315 | pointee_type->SetCompilerType(this, |
| 3316 | llvm::cast<clang::PointerType>(qual_type) |
| 3317 | ->getPointeeType() |
| 3318 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3319 | return true; |
| 3320 | case clang::Type::MemberPointer: |
| 3321 | if (pointee_type) |
| 3322 | pointee_type->SetCompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 3323 | this, llvm::cast<clang::MemberPointerType>(qual_type) |
| 3324 | ->getPointeeType() |
| 3325 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3326 | return true; |
| 3327 | case clang::Type::Typedef: |
| 3328 | return IsPointerType(llvm::cast<clang::TypedefType>(qual_type) |
| 3329 | ->getDecl() |
| 3330 | ->getUnderlyingType() |
| 3331 | .getAsOpaquePtr(), |
| 3332 | pointee_type); |
| 3333 | case clang::Type::Auto: |
| 3334 | return IsPointerType(llvm::cast<clang::AutoType>(qual_type) |
| 3335 | ->getDeducedType() |
| 3336 | .getAsOpaquePtr(), |
| 3337 | pointee_type); |
| 3338 | case clang::Type::Elaborated: |
| 3339 | return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 3340 | ->getNamedType() |
| 3341 | .getAsOpaquePtr(), |
| 3342 | pointee_type); |
| 3343 | case clang::Type::Paren: |
| 3344 | return IsPointerType( |
| 3345 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 3346 | pointee_type); |
| 3347 | default: |
| 3348 | break; |
| 3349 | } |
| 3350 | } |
| 3351 | if (pointee_type) |
| 3352 | pointee_type->Clear(); |
| 3353 | return false; |
| 3354 | } |
| 3355 | |
| 3356 | bool ClangASTContext::IsPointerOrReferenceType( |
| 3357 | lldb::opaque_compiler_type_t type, CompilerType *pointee_type) { |
| 3358 | if (type) { |
| 3359 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3360 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3361 | switch (type_class) { |
| 3362 | case clang::Type::Builtin: |
| 3363 | switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { |
| 3364 | default: |
| 3365 | break; |
| 3366 | case clang::BuiltinType::ObjCId: |
| 3367 | case clang::BuiltinType::ObjCClass: |
| 3368 | return true; |
| 3369 | } |
| 3370 | return false; |
| 3371 | case clang::Type::ObjCObjectPointer: |
| 3372 | if (pointee_type) |
| 3373 | pointee_type->SetCompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 3374 | this, llvm::cast<clang::ObjCObjectPointerType>(qual_type) |
| 3375 | ->getPointeeType().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3376 | return true; |
| 3377 | case clang::Type::BlockPointer: |
| 3378 | if (pointee_type) |
| 3379 | pointee_type->SetCompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 3380 | this, llvm::cast<clang::BlockPointerType>(qual_type) |
| 3381 | ->getPointeeType() |
| 3382 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3383 | return true; |
| 3384 | case clang::Type::Pointer: |
| 3385 | if (pointee_type) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 3386 | pointee_type->SetCompilerType(this, |
| 3387 | llvm::cast<clang::PointerType>(qual_type) |
| 3388 | ->getPointeeType() |
| 3389 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3390 | return true; |
| 3391 | case clang::Type::MemberPointer: |
| 3392 | if (pointee_type) |
| 3393 | pointee_type->SetCompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 3394 | this, llvm::cast<clang::MemberPointerType>(qual_type) |
| 3395 | ->getPointeeType() |
| 3396 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3397 | return true; |
| 3398 | case clang::Type::LValueReference: |
| 3399 | if (pointee_type) |
| 3400 | pointee_type->SetCompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 3401 | this, llvm::cast<clang::LValueReferenceType>(qual_type) |
| 3402 | ->desugar() |
| 3403 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3404 | return true; |
| 3405 | case clang::Type::RValueReference: |
| 3406 | if (pointee_type) |
| 3407 | pointee_type->SetCompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 3408 | this, llvm::cast<clang::RValueReferenceType>(qual_type) |
| 3409 | ->desugar() |
| 3410 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3411 | return true; |
| 3412 | case clang::Type::Typedef: |
| 3413 | return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type) |
| 3414 | ->getDecl() |
| 3415 | ->getUnderlyingType() |
| 3416 | .getAsOpaquePtr(), |
| 3417 | pointee_type); |
| 3418 | case clang::Type::Auto: |
| 3419 | return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type) |
| 3420 | ->getDeducedType() |
| 3421 | .getAsOpaquePtr(), |
| 3422 | pointee_type); |
| 3423 | case clang::Type::Elaborated: |
| 3424 | return IsPointerOrReferenceType( |
| 3425 | llvm::cast<clang::ElaboratedType>(qual_type) |
| 3426 | ->getNamedType() |
| 3427 | .getAsOpaquePtr(), |
| 3428 | pointee_type); |
| 3429 | case clang::Type::Paren: |
| 3430 | return IsPointerOrReferenceType( |
| 3431 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 3432 | pointee_type); |
| 3433 | default: |
| 3434 | break; |
| 3435 | } |
| 3436 | } |
| 3437 | if (pointee_type) |
| 3438 | pointee_type->Clear(); |
| 3439 | return false; |
| 3440 | } |
| 3441 | |
| 3442 | bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type, |
| 3443 | CompilerType *pointee_type, |
| 3444 | bool *is_rvalue) { |
| 3445 | if (type) { |
| 3446 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3447 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3448 | |
| 3449 | switch (type_class) { |
| 3450 | case clang::Type::LValueReference: |
| 3451 | if (pointee_type) |
| 3452 | pointee_type->SetCompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 3453 | this, llvm::cast<clang::LValueReferenceType>(qual_type) |
| 3454 | ->desugar() |
| 3455 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3456 | if (is_rvalue) |
| 3457 | *is_rvalue = false; |
| 3458 | return true; |
| 3459 | case clang::Type::RValueReference: |
| 3460 | if (pointee_type) |
| 3461 | pointee_type->SetCompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 3462 | this, llvm::cast<clang::RValueReferenceType>(qual_type) |
| 3463 | ->desugar() |
| 3464 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3465 | if (is_rvalue) |
| 3466 | *is_rvalue = true; |
| 3467 | return true; |
| 3468 | case clang::Type::Typedef: |
| 3469 | return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type) |
| 3470 | ->getDecl() |
| 3471 | ->getUnderlyingType() |
| 3472 | .getAsOpaquePtr(), |
| 3473 | pointee_type, is_rvalue); |
| 3474 | case clang::Type::Auto: |
| 3475 | return IsReferenceType(llvm::cast<clang::AutoType>(qual_type) |
| 3476 | ->getDeducedType() |
| 3477 | .getAsOpaquePtr(), |
| 3478 | pointee_type, is_rvalue); |
| 3479 | case clang::Type::Elaborated: |
| 3480 | return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 3481 | ->getNamedType() |
| 3482 | .getAsOpaquePtr(), |
| 3483 | pointee_type, is_rvalue); |
| 3484 | case clang::Type::Paren: |
| 3485 | return IsReferenceType( |
| 3486 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 3487 | pointee_type, is_rvalue); |
| 3488 | |
| 3489 | default: |
| 3490 | break; |
| 3491 | } |
| 3492 | } |
| 3493 | if (pointee_type) |
| 3494 | pointee_type->Clear(); |
| 3495 | return false; |
| 3496 | } |
| 3497 | |
| 3498 | bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type, |
| 3499 | uint32_t &count, bool &is_complex) { |
| 3500 | if (type) { |
| 3501 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3502 | |
| 3503 | if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>( |
| 3504 | qual_type->getCanonicalTypeInternal())) { |
| 3505 | clang::BuiltinType::Kind kind = BT->getKind(); |
| 3506 | if (kind >= clang::BuiltinType::Float && |
| 3507 | kind <= clang::BuiltinType::LongDouble) { |
| 3508 | count = 1; |
| 3509 | is_complex = false; |
| 3510 | return true; |
| 3511 | } |
| 3512 | } else if (const clang::ComplexType *CT = |
| 3513 | llvm::dyn_cast<clang::ComplexType>( |
| 3514 | qual_type->getCanonicalTypeInternal())) { |
| 3515 | if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count, |
| 3516 | is_complex)) { |
| 3517 | count = 2; |
| 3518 | is_complex = true; |
| 3519 | return true; |
| 3520 | } |
| 3521 | } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>( |
| 3522 | qual_type->getCanonicalTypeInternal())) { |
| 3523 | if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count, |
| 3524 | is_complex)) { |
| 3525 | count = VT->getNumElements(); |
| 3526 | is_complex = false; |
| 3527 | return true; |
| 3528 | } |
| 3529 | } |
| 3530 | } |
| 3531 | count = 0; |
| 3532 | is_complex = false; |
| 3533 | return false; |
| 3534 | } |
| 3535 | |
| 3536 | bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) { |
| 3537 | if (!type) |
| 3538 | return false; |
| 3539 | |
| 3540 | clang::QualType qual_type(GetQualType(type)); |
| 3541 | const clang::TagType *tag_type = |
| 3542 | llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()); |
| 3543 | if (tag_type) { |
| 3544 | clang::TagDecl *tag_decl = tag_type->getDecl(); |
| 3545 | if (tag_decl) |
| 3546 | return tag_decl->isCompleteDefinition(); |
| 3547 | return false; |
| 3548 | } else { |
| 3549 | const clang::ObjCObjectType *objc_class_type = |
| 3550 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type); |
| 3551 | if (objc_class_type) { |
| 3552 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 3553 | objc_class_type->getInterface(); |
| 3554 | if (class_interface_decl) |
| 3555 | return class_interface_decl->getDefinition() != nullptr; |
| 3556 | return false; |
| 3557 | } |
| 3558 | } |
| 3559 | return true; |
| 3560 | } |
| 3561 | |
| 3562 | bool ClangASTContext::IsObjCClassType(const CompilerType &type) { |
Raphael Isemann | 79b3cce | 2019-11-08 12:03:28 +0100 | [diff] [blame] | 3563 | if (ClangUtil::IsClangType(type)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3564 | clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type)); |
| 3565 | |
| 3566 | const clang::ObjCObjectPointerType *obj_pointer_type = |
| 3567 | llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type); |
| 3568 | |
| 3569 | if (obj_pointer_type) |
| 3570 | return obj_pointer_type->isObjCClassType(); |
| 3571 | } |
| 3572 | return false; |
| 3573 | } |
| 3574 | |
| 3575 | bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) { |
| 3576 | if (ClangUtil::IsClangType(type)) |
| 3577 | return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType(); |
| 3578 | return false; |
| 3579 | } |
| 3580 | |
| 3581 | bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) { |
| 3582 | if (!type) |
| 3583 | return false; |
| 3584 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3585 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3586 | return (type_class == clang::Type::Record); |
| 3587 | } |
| 3588 | |
| 3589 | bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) { |
| 3590 | if (!type) |
| 3591 | return false; |
| 3592 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3593 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3594 | return (type_class == clang::Type::Enum); |
| 3595 | } |
| 3596 | |
| 3597 | bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) { |
| 3598 | if (type) { |
| 3599 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3600 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3601 | switch (type_class) { |
| 3602 | case clang::Type::Record: |
| 3603 | if (GetCompleteType(type)) { |
| 3604 | const clang::RecordType *record_type = |
| 3605 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 3606 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 3607 | if (record_decl) { |
| 3608 | const clang::CXXRecordDecl *cxx_record_decl = |
| 3609 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 3610 | if (cxx_record_decl) |
| 3611 | return cxx_record_decl->isPolymorphic(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3612 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3613 | } |
| 3614 | break; |
| 3615 | |
| 3616 | default: |
| 3617 | break; |
| 3618 | } |
| 3619 | } |
| 3620 | return false; |
| 3621 | } |
| 3622 | |
| 3623 | bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type, |
| 3624 | CompilerType *dynamic_pointee_type, |
| 3625 | bool check_cplusplus, |
| 3626 | bool check_objc) { |
| 3627 | clang::QualType pointee_qual_type; |
| 3628 | if (type) { |
| 3629 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3630 | bool success = false; |
| 3631 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3632 | switch (type_class) { |
| 3633 | case clang::Type::Builtin: |
| 3634 | if (check_objc && |
| 3635 | llvm::cast<clang::BuiltinType>(qual_type)->getKind() == |
| 3636 | clang::BuiltinType::ObjCId) { |
| 3637 | if (dynamic_pointee_type) |
| 3638 | dynamic_pointee_type->SetCompilerType(this, type); |
| 3639 | return true; |
| 3640 | } |
| 3641 | break; |
| 3642 | |
| 3643 | case clang::Type::ObjCObjectPointer: |
| 3644 | if (check_objc) { |
| 3645 | if (auto objc_pointee_type = |
| 3646 | qual_type->getPointeeType().getTypePtrOrNull()) { |
| 3647 | if (auto objc_object_type = |
| 3648 | llvm::dyn_cast_or_null<clang::ObjCObjectType>( |
| 3649 | objc_pointee_type)) { |
| 3650 | if (objc_object_type->isObjCClass()) |
| 3651 | return false; |
| 3652 | } |
| 3653 | } |
| 3654 | if (dynamic_pointee_type) |
| 3655 | dynamic_pointee_type->SetCompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 3656 | this, llvm::cast<clang::ObjCObjectPointerType>(qual_type) |
| 3657 | ->getPointeeType() |
| 3658 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3659 | return true; |
| 3660 | } |
| 3661 | break; |
| 3662 | |
| 3663 | case clang::Type::Pointer: |
| 3664 | pointee_qual_type = |
| 3665 | llvm::cast<clang::PointerType>(qual_type)->getPointeeType(); |
| 3666 | success = true; |
| 3667 | break; |
| 3668 | |
| 3669 | case clang::Type::LValueReference: |
| 3670 | case clang::Type::RValueReference: |
| 3671 | pointee_qual_type = |
| 3672 | llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType(); |
| 3673 | success = true; |
| 3674 | break; |
| 3675 | |
| 3676 | case clang::Type::Typedef: |
| 3677 | return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type) |
| 3678 | ->getDecl() |
| 3679 | ->getUnderlyingType() |
| 3680 | .getAsOpaquePtr(), |
| 3681 | dynamic_pointee_type, check_cplusplus, |
| 3682 | check_objc); |
| 3683 | |
| 3684 | case clang::Type::Auto: |
| 3685 | return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type) |
| 3686 | ->getDeducedType() |
| 3687 | .getAsOpaquePtr(), |
| 3688 | dynamic_pointee_type, check_cplusplus, |
| 3689 | check_objc); |
| 3690 | |
| 3691 | case clang::Type::Elaborated: |
| 3692 | return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type) |
| 3693 | ->getNamedType() |
| 3694 | .getAsOpaquePtr(), |
| 3695 | dynamic_pointee_type, check_cplusplus, |
| 3696 | check_objc); |
| 3697 | |
| 3698 | case clang::Type::Paren: |
| 3699 | return IsPossibleDynamicType( |
| 3700 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 3701 | dynamic_pointee_type, check_cplusplus, check_objc); |
| 3702 | default: |
| 3703 | break; |
| 3704 | } |
| 3705 | |
| 3706 | if (success) { |
| 3707 | // 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] | 3708 | // type We currently accept any "void *" (in case we have a class that |
| 3709 | // has been watered down to an opaque pointer) and virtual C++ classes. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3710 | const clang::Type::TypeClass pointee_type_class = |
| 3711 | pointee_qual_type.getCanonicalType()->getTypeClass(); |
| 3712 | switch (pointee_type_class) { |
| 3713 | case clang::Type::Builtin: |
| 3714 | switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) { |
| 3715 | case clang::BuiltinType::UnknownAny: |
| 3716 | case clang::BuiltinType::Void: |
| 3717 | if (dynamic_pointee_type) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 3718 | dynamic_pointee_type->SetCompilerType( |
| 3719 | this, pointee_qual_type.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3720 | return true; |
| 3721 | default: |
| 3722 | break; |
| 3723 | } |
| 3724 | break; |
| 3725 | |
| 3726 | case clang::Type::Record: |
| 3727 | if (check_cplusplus) { |
| 3728 | clang::CXXRecordDecl *cxx_record_decl = |
| 3729 | pointee_qual_type->getAsCXXRecordDecl(); |
| 3730 | if (cxx_record_decl) { |
| 3731 | bool is_complete = cxx_record_decl->isCompleteDefinition(); |
| 3732 | |
| 3733 | if (is_complete) |
| 3734 | success = cxx_record_decl->isDynamicClass(); |
| 3735 | else { |
| 3736 | ClangASTMetadata *metadata = ClangASTContext::GetMetadata( |
| 3737 | getASTContext(), cxx_record_decl); |
| 3738 | if (metadata) |
| 3739 | success = metadata->GetIsDynamicCXXType(); |
| 3740 | else { |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 3741 | is_complete = |
| 3742 | CompilerType(this, pointee_qual_type.getAsOpaquePtr()) |
| 3743 | .GetCompleteType(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3744 | if (is_complete) |
| 3745 | success = cxx_record_decl->isDynamicClass(); |
| 3746 | else |
| 3747 | success = false; |
| 3748 | } |
| 3749 | } |
| 3750 | |
| 3751 | if (success) { |
| 3752 | if (dynamic_pointee_type) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 3753 | dynamic_pointee_type->SetCompilerType( |
| 3754 | this, pointee_qual_type.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3755 | return true; |
| 3756 | } |
| 3757 | } |
| 3758 | } |
| 3759 | break; |
| 3760 | |
| 3761 | case clang::Type::ObjCObject: |
| 3762 | case clang::Type::ObjCInterface: |
| 3763 | if (check_objc) { |
| 3764 | if (dynamic_pointee_type) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 3765 | dynamic_pointee_type->SetCompilerType( |
| 3766 | this, pointee_qual_type.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3767 | return true; |
| 3768 | } |
| 3769 | break; |
| 3770 | |
| 3771 | default: |
| 3772 | break; |
| 3773 | } |
| 3774 | } |
| 3775 | } |
| 3776 | if (dynamic_pointee_type) |
| 3777 | dynamic_pointee_type->Clear(); |
| 3778 | return false; |
| 3779 | } |
| 3780 | |
| 3781 | bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) { |
| 3782 | if (!type) |
| 3783 | return false; |
| 3784 | |
| 3785 | return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0; |
| 3786 | } |
| 3787 | |
| 3788 | bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) { |
| 3789 | if (!type) |
| 3790 | return false; |
| 3791 | return GetQualType(type)->getTypeClass() == clang::Type::Typedef; |
| 3792 | } |
| 3793 | |
| 3794 | bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) { |
| 3795 | if (!type) |
| 3796 | return false; |
| 3797 | return GetCanonicalQualType(type)->isVoidType(); |
| 3798 | } |
| 3799 | |
Alex Langford | a03e2b2 | 2019-06-04 19:29:59 +0000 | [diff] [blame] | 3800 | bool ClangASTContext::CanPassInRegisters(const CompilerType &type) { |
| 3801 | if (auto *record_decl = |
| 3802 | ClangASTContext::GetAsRecordDecl(type)) { |
| 3803 | return record_decl->canPassInRegisters(); |
| 3804 | } |
| 3805 | return false; |
| 3806 | } |
| 3807 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3808 | bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) { |
| 3809 | return ClangASTContextSupportsLanguage(language); |
| 3810 | } |
| 3811 | |
Alex Langford | db54245 | 2019-10-30 12:50:05 -0700 | [diff] [blame] | 3812 | Optional<std::string> |
| 3813 | ClangASTContext::GetCXXClassName(const CompilerType &type) { |
| 3814 | if (!type) |
| 3815 | return llvm::None; |
| 3816 | |
| 3817 | clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type)); |
| 3818 | if (qual_type.isNull()) |
| 3819 | return llvm::None; |
| 3820 | |
| 3821 | clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); |
| 3822 | if (!cxx_record_decl) |
| 3823 | return llvm::None; |
| 3824 | |
| 3825 | return std::string(cxx_record_decl->getIdentifier()->getNameStart()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3826 | } |
| 3827 | |
| 3828 | bool ClangASTContext::IsCXXClassType(const CompilerType &type) { |
| 3829 | if (!type) |
| 3830 | return false; |
| 3831 | |
| 3832 | clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type)); |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 3833 | return !qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3834 | } |
| 3835 | |
| 3836 | bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) { |
| 3837 | if (!type) |
| 3838 | return false; |
| 3839 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 3840 | const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type); |
| 3841 | if (tag_type) |
| 3842 | return tag_type->isBeingDefined(); |
| 3843 | return false; |
| 3844 | } |
| 3845 | |
| 3846 | bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type, |
| 3847 | CompilerType *class_type_ptr) { |
Raphael Isemann | 79b3cce | 2019-11-08 12:03:28 +0100 | [diff] [blame] | 3848 | if (!ClangUtil::IsClangType(type)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3849 | return false; |
| 3850 | |
| 3851 | clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type)); |
| 3852 | |
| 3853 | if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) { |
| 3854 | if (class_type_ptr) { |
| 3855 | if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) { |
| 3856 | const clang::ObjCObjectPointerType *obj_pointer_type = |
| 3857 | llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type); |
| 3858 | if (obj_pointer_type == nullptr) |
| 3859 | class_type_ptr->Clear(); |
| 3860 | else |
| 3861 | class_type_ptr->SetCompilerType( |
| 3862 | type.GetTypeSystem(), |
| 3863 | clang::QualType(obj_pointer_type->getInterfaceType(), 0) |
| 3864 | .getAsOpaquePtr()); |
| 3865 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3866 | } |
| 3867 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3868 | } |
| 3869 | if (class_type_ptr) |
| 3870 | class_type_ptr->Clear(); |
| 3871 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3872 | } |
| 3873 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3874 | // Type Completion |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3875 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3876 | bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) { |
| 3877 | if (!type) |
| 3878 | return false; |
| 3879 | const bool allow_completion = true; |
| 3880 | return GetCompleteQualType(getASTContext(), GetQualType(type), |
| 3881 | allow_completion); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3882 | } |
| 3883 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3884 | ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) { |
| 3885 | std::string type_name; |
| 3886 | if (type) { |
| 3887 | clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy()); |
| 3888 | clang::QualType qual_type(GetQualType(type)); |
| 3889 | printing_policy.SuppressTagKeyword = true; |
| 3890 | const clang::TypedefType *typedef_type = |
| 3891 | qual_type->getAs<clang::TypedefType>(); |
| 3892 | if (typedef_type) { |
| 3893 | const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl(); |
| 3894 | type_name = typedef_decl->getQualifiedNameAsString(); |
| 3895 | } else { |
| 3896 | type_name = qual_type.getAsString(printing_policy); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3897 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3898 | } |
| 3899 | return ConstString(type_name); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3900 | } |
| 3901 | |
| 3902 | uint32_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3903 | ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type, |
| 3904 | CompilerType *pointee_or_element_clang_type) { |
| 3905 | if (!type) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 3906 | return 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3907 | |
| 3908 | if (pointee_or_element_clang_type) |
| 3909 | pointee_or_element_clang_type->Clear(); |
| 3910 | |
| 3911 | clang::QualType qual_type(GetQualType(type)); |
| 3912 | |
| 3913 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 3914 | switch (type_class) { |
Sean Callanan | ddf802a | 2017-06-02 01:24:18 +0000 | [diff] [blame] | 3915 | case clang::Type::Attributed: |
| 3916 | return GetTypeInfo( |
| 3917 | qual_type->getAs<clang::AttributedType>() |
| 3918 | ->getModifiedType().getAsOpaquePtr(), |
| 3919 | pointee_or_element_clang_type); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3920 | case clang::Type::Builtin: { |
| 3921 | const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>( |
| 3922 | qual_type->getCanonicalTypeInternal()); |
| 3923 | |
| 3924 | uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue; |
| 3925 | switch (builtin_type->getKind()) { |
| 3926 | case clang::BuiltinType::ObjCId: |
| 3927 | case clang::BuiltinType::ObjCClass: |
| 3928 | if (pointee_or_element_clang_type) |
| 3929 | pointee_or_element_clang_type->SetCompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 3930 | this, getASTContext()->ObjCBuiltinClassTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3931 | builtin_type_flags |= eTypeIsPointer | eTypeIsObjC; |
| 3932 | break; |
| 3933 | |
| 3934 | case clang::BuiltinType::ObjCSel: |
| 3935 | if (pointee_or_element_clang_type) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 3936 | pointee_or_element_clang_type->SetCompilerType( |
| 3937 | this, getASTContext()->CharTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3938 | builtin_type_flags |= eTypeIsPointer | eTypeIsObjC; |
| 3939 | break; |
| 3940 | |
| 3941 | case clang::BuiltinType::Bool: |
| 3942 | case clang::BuiltinType::Char_U: |
| 3943 | case clang::BuiltinType::UChar: |
| 3944 | case clang::BuiltinType::WChar_U: |
| 3945 | case clang::BuiltinType::Char16: |
| 3946 | case clang::BuiltinType::Char32: |
| 3947 | case clang::BuiltinType::UShort: |
| 3948 | case clang::BuiltinType::UInt: |
| 3949 | case clang::BuiltinType::ULong: |
| 3950 | case clang::BuiltinType::ULongLong: |
| 3951 | case clang::BuiltinType::UInt128: |
| 3952 | case clang::BuiltinType::Char_S: |
| 3953 | case clang::BuiltinType::SChar: |
| 3954 | case clang::BuiltinType::WChar_S: |
| 3955 | case clang::BuiltinType::Short: |
| 3956 | case clang::BuiltinType::Int: |
| 3957 | case clang::BuiltinType::Long: |
| 3958 | case clang::BuiltinType::LongLong: |
| 3959 | case clang::BuiltinType::Int128: |
| 3960 | case clang::BuiltinType::Float: |
| 3961 | case clang::BuiltinType::Double: |
| 3962 | case clang::BuiltinType::LongDouble: |
| 3963 | builtin_type_flags |= eTypeIsScalar; |
| 3964 | if (builtin_type->isInteger()) { |
| 3965 | builtin_type_flags |= eTypeIsInteger; |
| 3966 | if (builtin_type->isSignedInteger()) |
| 3967 | builtin_type_flags |= eTypeIsSigned; |
| 3968 | } else if (builtin_type->isFloatingPoint()) |
| 3969 | builtin_type_flags |= eTypeIsFloat; |
| 3970 | break; |
| 3971 | default: |
| 3972 | break; |
| 3973 | } |
| 3974 | return builtin_type_flags; |
| 3975 | } |
| 3976 | |
| 3977 | case clang::Type::BlockPointer: |
| 3978 | if (pointee_or_element_clang_type) |
| 3979 | pointee_or_element_clang_type->SetCompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 3980 | this, qual_type->getPointeeType().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3981 | return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock; |
| 3982 | |
| 3983 | case clang::Type::Complex: { |
| 3984 | uint32_t complex_type_flags = |
| 3985 | eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex; |
| 3986 | const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>( |
| 3987 | qual_type->getCanonicalTypeInternal()); |
| 3988 | if (complex_type) { |
| 3989 | clang::QualType complex_element_type(complex_type->getElementType()); |
| 3990 | if (complex_element_type->isIntegerType()) |
| 3991 | complex_type_flags |= eTypeIsFloat; |
| 3992 | else if (complex_element_type->isFloatingType()) |
| 3993 | complex_type_flags |= eTypeIsInteger; |
| 3994 | } |
| 3995 | return complex_type_flags; |
| 3996 | } break; |
| 3997 | |
| 3998 | case clang::Type::ConstantArray: |
| 3999 | case clang::Type::DependentSizedArray: |
| 4000 | case clang::Type::IncompleteArray: |
| 4001 | case clang::Type::VariableArray: |
| 4002 | if (pointee_or_element_clang_type) |
| 4003 | pointee_or_element_clang_type->SetCompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4004 | this, llvm::cast<clang::ArrayType>(qual_type.getTypePtr()) |
| 4005 | ->getElementType() |
| 4006 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4007 | return eTypeHasChildren | eTypeIsArray; |
| 4008 | |
| 4009 | case clang::Type::DependentName: |
| 4010 | return 0; |
| 4011 | case clang::Type::DependentSizedExtVector: |
| 4012 | return eTypeHasChildren | eTypeIsVector; |
| 4013 | case clang::Type::DependentTemplateSpecialization: |
| 4014 | return eTypeIsTemplate; |
| 4015 | case clang::Type::Decltype: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4016 | return CompilerType(this, llvm::cast<clang::DecltypeType>(qual_type) |
| 4017 | ->getUnderlyingType() |
| 4018 | .getAsOpaquePtr()) |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 4019 | .GetTypeInfo(pointee_or_element_clang_type); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4020 | |
| 4021 | case clang::Type::Enum: |
| 4022 | if (pointee_or_element_clang_type) |
| 4023 | pointee_or_element_clang_type->SetCompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4024 | this, llvm::cast<clang::EnumType>(qual_type) |
| 4025 | ->getDecl() |
| 4026 | ->getIntegerType() |
| 4027 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4028 | return eTypeIsEnumeration | eTypeHasValue; |
| 4029 | |
| 4030 | case clang::Type::Auto: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4031 | return CompilerType(this, llvm::cast<clang::AutoType>(qual_type) |
| 4032 | ->getDeducedType() |
| 4033 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4034 | .GetTypeInfo(pointee_or_element_clang_type); |
| 4035 | case clang::Type::Elaborated: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4036 | return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type) |
| 4037 | ->getNamedType() |
| 4038 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4039 | .GetTypeInfo(pointee_or_element_clang_type); |
| 4040 | case clang::Type::Paren: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4041 | return CompilerType(this, llvm::cast<clang::ParenType>(qual_type) |
| 4042 | ->desugar() |
| 4043 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4044 | .GetTypeInfo(pointee_or_element_clang_type); |
| 4045 | |
| 4046 | case clang::Type::FunctionProto: |
| 4047 | return eTypeIsFuncPrototype | eTypeHasValue; |
| 4048 | case clang::Type::FunctionNoProto: |
| 4049 | return eTypeIsFuncPrototype | eTypeHasValue; |
| 4050 | case clang::Type::InjectedClassName: |
| 4051 | return 0; |
| 4052 | |
| 4053 | case clang::Type::LValueReference: |
| 4054 | case clang::Type::RValueReference: |
| 4055 | if (pointee_or_element_clang_type) |
| 4056 | pointee_or_element_clang_type->SetCompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4057 | this, llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()) |
| 4058 | ->getPointeeType() |
| 4059 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4060 | return eTypeHasChildren | eTypeIsReference | eTypeHasValue; |
| 4061 | |
| 4062 | case clang::Type::MemberPointer: |
| 4063 | return eTypeIsPointer | eTypeIsMember | eTypeHasValue; |
| 4064 | |
| 4065 | case clang::Type::ObjCObjectPointer: |
| 4066 | if (pointee_or_element_clang_type) |
| 4067 | pointee_or_element_clang_type->SetCompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4068 | this, qual_type->getPointeeType().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4069 | return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | |
| 4070 | eTypeHasValue; |
| 4071 | |
| 4072 | case clang::Type::ObjCObject: |
| 4073 | return eTypeHasChildren | eTypeIsObjC | eTypeIsClass; |
| 4074 | case clang::Type::ObjCInterface: |
| 4075 | return eTypeHasChildren | eTypeIsObjC | eTypeIsClass; |
| 4076 | |
| 4077 | case clang::Type::Pointer: |
| 4078 | if (pointee_or_element_clang_type) |
| 4079 | pointee_or_element_clang_type->SetCompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4080 | this, qual_type->getPointeeType().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4081 | return eTypeHasChildren | eTypeIsPointer | eTypeHasValue; |
| 4082 | |
| 4083 | case clang::Type::Record: |
| 4084 | if (qual_type->getAsCXXRecordDecl()) |
| 4085 | return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus; |
| 4086 | else |
| 4087 | return eTypeHasChildren | eTypeIsStructUnion; |
| 4088 | break; |
| 4089 | case clang::Type::SubstTemplateTypeParm: |
| 4090 | return eTypeIsTemplate; |
| 4091 | case clang::Type::TemplateTypeParm: |
| 4092 | return eTypeIsTemplate; |
| 4093 | case clang::Type::TemplateSpecialization: |
| 4094 | return eTypeIsTemplate; |
| 4095 | |
| 4096 | case clang::Type::Typedef: |
| 4097 | return eTypeIsTypedef | |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4098 | CompilerType(this, llvm::cast<clang::TypedefType>(qual_type) |
| 4099 | ->getDecl() |
| 4100 | ->getUnderlyingType() |
| 4101 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4102 | .GetTypeInfo(pointee_or_element_clang_type); |
| 4103 | case clang::Type::TypeOfExpr: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4104 | return CompilerType(this, llvm::cast<clang::TypeOfExprType>(qual_type) |
| 4105 | ->getUnderlyingExpr() |
| 4106 | ->getType() |
| 4107 | .getAsOpaquePtr()) |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 4108 | .GetTypeInfo(pointee_or_element_clang_type); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4109 | case clang::Type::TypeOf: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4110 | return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type) |
| 4111 | ->getUnderlyingType() |
| 4112 | .getAsOpaquePtr()) |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 4113 | .GetTypeInfo(pointee_or_element_clang_type); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4114 | case clang::Type::UnresolvedUsing: |
| 4115 | return 0; |
| 4116 | |
| 4117 | case clang::Type::ExtVector: |
| 4118 | case clang::Type::Vector: { |
| 4119 | uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector; |
| 4120 | const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>( |
| 4121 | qual_type->getCanonicalTypeInternal()); |
| 4122 | if (vector_type) { |
| 4123 | if (vector_type->isIntegerType()) |
| 4124 | vector_type_flags |= eTypeIsFloat; |
| 4125 | else if (vector_type->isFloatingType()) |
| 4126 | vector_type_flags |= eTypeIsInteger; |
| 4127 | } |
| 4128 | return vector_type_flags; |
| 4129 | } |
| 4130 | default: |
| 4131 | return 0; |
| 4132 | } |
| 4133 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4134 | } |
| 4135 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4136 | lldb::LanguageType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4137 | ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) { |
| 4138 | if (!type) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4139 | return lldb::eLanguageTypeC; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4140 | |
| 4141 | // If the type is a reference, then resolve it to what it refers to first: |
| 4142 | clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType()); |
| 4143 | if (qual_type->isAnyPointerType()) { |
| 4144 | if (qual_type->isObjCObjectPointerType()) |
| 4145 | return lldb::eLanguageTypeObjC; |
Adrian Prantl | 1db0f0c | 2019-05-02 23:07:23 +0000 | [diff] [blame] | 4146 | if (qual_type->getPointeeCXXRecordDecl()) |
| 4147 | return lldb::eLanguageTypeC_plus_plus; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4148 | |
| 4149 | clang::QualType pointee_type(qual_type->getPointeeType()); |
Adrian Prantl | 1db0f0c | 2019-05-02 23:07:23 +0000 | [diff] [blame] | 4150 | if (pointee_type->getPointeeCXXRecordDecl()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4151 | return lldb::eLanguageTypeC_plus_plus; |
| 4152 | if (pointee_type->isObjCObjectOrInterfaceType()) |
| 4153 | return lldb::eLanguageTypeObjC; |
| 4154 | if (pointee_type->isObjCClassType()) |
| 4155 | return lldb::eLanguageTypeObjC; |
| 4156 | if (pointee_type.getTypePtr() == |
| 4157 | getASTContext()->ObjCBuiltinIdTy.getTypePtr()) |
| 4158 | return lldb::eLanguageTypeObjC; |
| 4159 | } else { |
| 4160 | if (qual_type->isObjCObjectOrInterfaceType()) |
| 4161 | return lldb::eLanguageTypeObjC; |
| 4162 | if (qual_type->getAsCXXRecordDecl()) |
| 4163 | return lldb::eLanguageTypeC_plus_plus; |
| 4164 | switch (qual_type->getTypeClass()) { |
| 4165 | default: |
| 4166 | break; |
| 4167 | case clang::Type::Builtin: |
| 4168 | switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { |
| 4169 | default: |
| 4170 | case clang::BuiltinType::Void: |
| 4171 | case clang::BuiltinType::Bool: |
| 4172 | case clang::BuiltinType::Char_U: |
| 4173 | case clang::BuiltinType::UChar: |
| 4174 | case clang::BuiltinType::WChar_U: |
| 4175 | case clang::BuiltinType::Char16: |
| 4176 | case clang::BuiltinType::Char32: |
| 4177 | case clang::BuiltinType::UShort: |
| 4178 | case clang::BuiltinType::UInt: |
| 4179 | case clang::BuiltinType::ULong: |
| 4180 | case clang::BuiltinType::ULongLong: |
| 4181 | case clang::BuiltinType::UInt128: |
| 4182 | case clang::BuiltinType::Char_S: |
| 4183 | case clang::BuiltinType::SChar: |
| 4184 | case clang::BuiltinType::WChar_S: |
| 4185 | case clang::BuiltinType::Short: |
| 4186 | case clang::BuiltinType::Int: |
| 4187 | case clang::BuiltinType::Long: |
| 4188 | case clang::BuiltinType::LongLong: |
| 4189 | case clang::BuiltinType::Int128: |
| 4190 | case clang::BuiltinType::Float: |
| 4191 | case clang::BuiltinType::Double: |
| 4192 | case clang::BuiltinType::LongDouble: |
| 4193 | break; |
| 4194 | |
| 4195 | case clang::BuiltinType::NullPtr: |
| 4196 | return eLanguageTypeC_plus_plus; |
| 4197 | |
| 4198 | case clang::BuiltinType::ObjCId: |
| 4199 | case clang::BuiltinType::ObjCClass: |
| 4200 | case clang::BuiltinType::ObjCSel: |
| 4201 | return eLanguageTypeObjC; |
| 4202 | |
| 4203 | case clang::BuiltinType::Dependent: |
| 4204 | case clang::BuiltinType::Overload: |
| 4205 | case clang::BuiltinType::BoundMember: |
| 4206 | case clang::BuiltinType::UnknownAny: |
| 4207 | break; |
| 4208 | } |
| 4209 | break; |
| 4210 | case clang::Type::Typedef: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4211 | return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type) |
| 4212 | ->getDecl() |
| 4213 | ->getUnderlyingType() |
| 4214 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4215 | .GetMinimumLanguage(); |
| 4216 | } |
| 4217 | } |
| 4218 | return lldb::eLanguageTypeC; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4219 | } |
| 4220 | |
| 4221 | lldb::TypeClass |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4222 | ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) { |
| 4223 | if (!type) |
| 4224 | return lldb::eTypeClassInvalid; |
| 4225 | |
| 4226 | clang::QualType qual_type(GetQualType(type)); |
| 4227 | |
| 4228 | switch (qual_type->getTypeClass()) { |
| 4229 | case clang::Type::UnaryTransform: |
| 4230 | break; |
| 4231 | case clang::Type::FunctionNoProto: |
| 4232 | return lldb::eTypeClassFunction; |
| 4233 | case clang::Type::FunctionProto: |
| 4234 | return lldb::eTypeClassFunction; |
| 4235 | case clang::Type::IncompleteArray: |
| 4236 | return lldb::eTypeClassArray; |
| 4237 | case clang::Type::VariableArray: |
| 4238 | return lldb::eTypeClassArray; |
| 4239 | case clang::Type::ConstantArray: |
| 4240 | return lldb::eTypeClassArray; |
| 4241 | case clang::Type::DependentSizedArray: |
| 4242 | return lldb::eTypeClassArray; |
| 4243 | case clang::Type::DependentSizedExtVector: |
| 4244 | return lldb::eTypeClassVector; |
Fangrui Song | 8f28488 | 2018-07-13 22:40:40 +0000 | [diff] [blame] | 4245 | case clang::Type::DependentVector: |
| 4246 | return lldb::eTypeClassVector; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4247 | case clang::Type::ExtVector: |
| 4248 | return lldb::eTypeClassVector; |
| 4249 | case clang::Type::Vector: |
| 4250 | return lldb::eTypeClassVector; |
| 4251 | case clang::Type::Builtin: |
| 4252 | return lldb::eTypeClassBuiltin; |
| 4253 | case clang::Type::ObjCObjectPointer: |
| 4254 | return lldb::eTypeClassObjCObjectPointer; |
| 4255 | case clang::Type::BlockPointer: |
| 4256 | return lldb::eTypeClassBlockPointer; |
| 4257 | case clang::Type::Pointer: |
| 4258 | return lldb::eTypeClassPointer; |
| 4259 | case clang::Type::LValueReference: |
| 4260 | return lldb::eTypeClassReference; |
| 4261 | case clang::Type::RValueReference: |
| 4262 | return lldb::eTypeClassReference; |
| 4263 | case clang::Type::MemberPointer: |
| 4264 | return lldb::eTypeClassMemberPointer; |
| 4265 | case clang::Type::Complex: |
| 4266 | if (qual_type->isComplexType()) |
| 4267 | return lldb::eTypeClassComplexFloat; |
| 4268 | else |
| 4269 | return lldb::eTypeClassComplexInteger; |
| 4270 | case clang::Type::ObjCObject: |
| 4271 | return lldb::eTypeClassObjCObject; |
| 4272 | case clang::Type::ObjCInterface: |
| 4273 | return lldb::eTypeClassObjCInterface; |
| 4274 | case clang::Type::Record: { |
| 4275 | const clang::RecordType *record_type = |
| 4276 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 4277 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 4278 | if (record_decl->isUnion()) |
| 4279 | return lldb::eTypeClassUnion; |
| 4280 | else if (record_decl->isStruct()) |
| 4281 | return lldb::eTypeClassStruct; |
| 4282 | else |
| 4283 | return lldb::eTypeClassClass; |
| 4284 | } break; |
| 4285 | case clang::Type::Enum: |
| 4286 | return lldb::eTypeClassEnumeration; |
| 4287 | case clang::Type::Typedef: |
| 4288 | return lldb::eTypeClassTypedef; |
| 4289 | case clang::Type::UnresolvedUsing: |
| 4290 | break; |
| 4291 | case clang::Type::Paren: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4292 | return CompilerType(this, llvm::cast<clang::ParenType>(qual_type) |
| 4293 | ->desugar() |
| 4294 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4295 | .GetTypeClass(); |
| 4296 | case clang::Type::Auto: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4297 | return CompilerType(this, llvm::cast<clang::AutoType>(qual_type) |
| 4298 | ->getDeducedType() |
| 4299 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4300 | .GetTypeClass(); |
| 4301 | case clang::Type::Elaborated: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4302 | return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type) |
| 4303 | ->getNamedType() |
| 4304 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4305 | .GetTypeClass(); |
| 4306 | |
| 4307 | case clang::Type::Attributed: |
| 4308 | break; |
| 4309 | case clang::Type::TemplateTypeParm: |
| 4310 | break; |
| 4311 | case clang::Type::SubstTemplateTypeParm: |
| 4312 | break; |
| 4313 | case clang::Type::SubstTemplateTypeParmPack: |
| 4314 | break; |
| 4315 | case clang::Type::InjectedClassName: |
| 4316 | break; |
| 4317 | case clang::Type::DependentName: |
| 4318 | break; |
| 4319 | case clang::Type::DependentTemplateSpecialization: |
| 4320 | break; |
| 4321 | case clang::Type::PackExpansion: |
| 4322 | break; |
| 4323 | |
| 4324 | case clang::Type::TypeOfExpr: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4325 | return CompilerType(this, llvm::cast<clang::TypeOfExprType>(qual_type) |
| 4326 | ->getUnderlyingExpr() |
| 4327 | ->getType() |
| 4328 | .getAsOpaquePtr()) |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 4329 | .GetTypeClass(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4330 | case clang::Type::TypeOf: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4331 | return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type) |
| 4332 | ->getUnderlyingType() |
| 4333 | .getAsOpaquePtr()) |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 4334 | .GetTypeClass(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4335 | case clang::Type::Decltype: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4336 | return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type) |
| 4337 | ->getUnderlyingType() |
| 4338 | .getAsOpaquePtr()) |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 4339 | .GetTypeClass(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4340 | case clang::Type::TemplateSpecialization: |
| 4341 | break; |
Pavel Labath | 4f19fce2 | 2017-02-17 13:39:50 +0000 | [diff] [blame] | 4342 | case clang::Type::DeducedTemplateSpecialization: |
| 4343 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4344 | case clang::Type::Atomic: |
| 4345 | break; |
| 4346 | case clang::Type::Pipe: |
| 4347 | break; |
| 4348 | |
| 4349 | // pointer type decayed from an array or function type. |
| 4350 | case clang::Type::Decayed: |
| 4351 | break; |
| 4352 | case clang::Type::Adjusted: |
| 4353 | break; |
Zachary Turner | 5a8ad459 | 2016-10-05 17:07:34 +0000 | [diff] [blame] | 4354 | case clang::Type::ObjCTypeParam: |
| 4355 | break; |
Ted Woodward | 66060cf | 2017-10-11 22:42:21 +0000 | [diff] [blame] | 4356 | |
| 4357 | case clang::Type::DependentAddressSpace: |
| 4358 | break; |
Krasimir Georgiev | 435e76a | 2019-05-07 13:59:30 +0000 | [diff] [blame] | 4359 | case clang::Type::MacroQualified: |
| 4360 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4361 | } |
| 4362 | // We don't know hot to display this type... |
| 4363 | return lldb::eTypeClassOther; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4364 | } |
| 4365 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4366 | unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) { |
| 4367 | if (type) |
| 4368 | return GetQualType(type).getQualifiers().getCVRQualifiers(); |
| 4369 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4370 | } |
| 4371 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4372 | // Creating related types |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4373 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 4374 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4375 | ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type, |
| 4376 | uint64_t *stride) { |
| 4377 | if (type) { |
| 4378 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 4379 | |
| 4380 | const clang::Type *array_eletype = |
| 4381 | qual_type.getTypePtr()->getArrayElementTypeNoTypeQual(); |
| 4382 | |
| 4383 | if (!array_eletype) |
| 4384 | return CompilerType(); |
| 4385 | |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4386 | CompilerType element_type( |
| 4387 | this, array_eletype->getCanonicalTypeUnqualified().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4388 | |
| 4389 | // TODO: the real stride will be >= this value.. find the real one! |
| 4390 | if (stride) |
Adrian Prantl | 2f1fa7a | 2019-01-15 21:04:19 +0000 | [diff] [blame] | 4391 | if (Optional<uint64_t> size = element_type.GetByteSize(nullptr)) |
Adrian Prantl | d963a7c | 2019-01-15 18:07:52 +0000 | [diff] [blame] | 4392 | *stride = *size; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4393 | |
| 4394 | return element_type; |
| 4395 | } |
| 4396 | return CompilerType(); |
| 4397 | } |
| 4398 | |
| 4399 | CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type, |
| 4400 | uint64_t size) { |
| 4401 | if (type) { |
| 4402 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 4403 | if (clang::ASTContext *ast_ctx = getASTContext()) { |
| 4404 | if (size != 0) |
| 4405 | return CompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4406 | this, ast_ctx |
| 4407 | ->getConstantArrayType( |
Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 4408 | qual_type, llvm::APInt(64, size), nullptr, |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4409 | clang::ArrayType::ArraySizeModifier::Normal, 0) |
| 4410 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4411 | else |
| 4412 | return CompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4413 | this, |
| 4414 | ast_ctx |
| 4415 | ->getIncompleteArrayType( |
| 4416 | qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0) |
| 4417 | .getAsOpaquePtr()); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4418 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4419 | } |
| 4420 | |
| 4421 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4422 | } |
| 4423 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 4424 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4425 | ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) { |
| 4426 | if (type) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4427 | return CompilerType(this, GetCanonicalQualType(type).getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4428 | return CompilerType(); |
| 4429 | } |
| 4430 | |
| 4431 | static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast, |
| 4432 | clang::QualType qual_type) { |
| 4433 | if (qual_type->isPointerType()) |
| 4434 | qual_type = ast->getPointerType( |
| 4435 | GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType())); |
| 4436 | else |
| 4437 | qual_type = qual_type.getUnqualifiedType(); |
| 4438 | qual_type.removeLocalConst(); |
| 4439 | qual_type.removeLocalRestrict(); |
| 4440 | qual_type.removeLocalVolatile(); |
| 4441 | return qual_type; |
Enrico Granata | 639392f | 2016-08-30 20:39:58 +0000 | [diff] [blame] | 4442 | } |
| 4443 | |
| 4444 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4445 | ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) { |
| 4446 | if (type) |
| 4447 | return CompilerType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4448 | this, |
| 4449 | GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)).getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4450 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4451 | } |
| 4452 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4453 | int ClangASTContext::GetFunctionArgumentCount( |
| 4454 | lldb::opaque_compiler_type_t type) { |
| 4455 | if (type) { |
| 4456 | const clang::FunctionProtoType *func = |
| 4457 | llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type)); |
| 4458 | if (func) |
| 4459 | return func->getNumParams(); |
| 4460 | } |
| 4461 | return -1; |
| 4462 | } |
| 4463 | |
| 4464 | CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex( |
| 4465 | lldb::opaque_compiler_type_t type, size_t idx) { |
| 4466 | if (type) { |
| 4467 | const clang::FunctionProtoType *func = |
| 4468 | llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type)); |
| 4469 | if (func) { |
| 4470 | const uint32_t num_args = func->getNumParams(); |
| 4471 | if (idx < num_args) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4472 | return CompilerType(this, func->getParamType(idx).getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4473 | } |
| 4474 | } |
| 4475 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4476 | } |
| 4477 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 4478 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4479 | ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) { |
| 4480 | if (type) { |
| 4481 | clang::QualType qual_type(GetQualType(type)); |
| 4482 | const clang::FunctionProtoType *func = |
| 4483 | llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr()); |
| 4484 | if (func) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4485 | return CompilerType(this, func->getReturnType().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4486 | } |
| 4487 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4488 | } |
| 4489 | |
| 4490 | size_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4491 | ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) { |
| 4492 | size_t num_functions = 0; |
| 4493 | if (type) { |
| 4494 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 4495 | switch (qual_type->getTypeClass()) { |
| 4496 | case clang::Type::Record: |
| 4497 | if (GetCompleteQualType(getASTContext(), qual_type)) { |
| 4498 | const clang::RecordType *record_type = |
| 4499 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 4500 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 4501 | assert(record_decl); |
| 4502 | const clang::CXXRecordDecl *cxx_record_decl = |
| 4503 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 4504 | if (cxx_record_decl) |
| 4505 | num_functions = std::distance(cxx_record_decl->method_begin(), |
| 4506 | cxx_record_decl->method_end()); |
| 4507 | } |
| 4508 | break; |
Enrico Granata | 36f51e4 | 2015-12-18 22:41:25 +0000 | [diff] [blame] | 4509 | |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 4510 | case clang::Type::ObjCObjectPointer: { |
| 4511 | const clang::ObjCObjectPointerType *objc_class_type = |
Sean Callanan | 732a6f4 | 2017-05-15 19:55:20 +0000 | [diff] [blame] | 4512 | qual_type->getAs<clang::ObjCObjectPointerType>(); |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 4513 | const clang::ObjCInterfaceType *objc_interface_type = |
| 4514 | objc_class_type->getInterfaceType(); |
| 4515 | if (objc_interface_type && |
Davide Italiano | 52ffb53 | 2017-04-17 18:24:18 +0000 | [diff] [blame] | 4516 | GetCompleteType(static_cast<lldb::opaque_compiler_type_t>( |
| 4517 | const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) { |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 4518 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 4519 | objc_interface_type->getDecl(); |
| 4520 | if (class_interface_decl) { |
| 4521 | num_functions = std::distance(class_interface_decl->meth_begin(), |
| 4522 | class_interface_decl->meth_end()); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4523 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4524 | } |
| 4525 | break; |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 4526 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4527 | |
| 4528 | case clang::Type::ObjCObject: |
| 4529 | case clang::Type::ObjCInterface: |
| 4530 | if (GetCompleteType(type)) { |
| 4531 | const clang::ObjCObjectType *objc_class_type = |
| 4532 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 4533 | if (objc_class_type) { |
| 4534 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 4535 | objc_class_type->getInterface(); |
| 4536 | if (class_interface_decl) |
| 4537 | num_functions = std::distance(class_interface_decl->meth_begin(), |
| 4538 | class_interface_decl->meth_end()); |
| 4539 | } |
| 4540 | } |
| 4541 | break; |
| 4542 | |
| 4543 | case clang::Type::Typedef: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4544 | return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type) |
| 4545 | ->getDecl() |
| 4546 | ->getUnderlyingType() |
| 4547 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4548 | .GetNumMemberFunctions(); |
| 4549 | |
| 4550 | case clang::Type::Auto: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4551 | return CompilerType(this, llvm::cast<clang::AutoType>(qual_type) |
| 4552 | ->getDeducedType() |
| 4553 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4554 | .GetNumMemberFunctions(); |
| 4555 | |
| 4556 | case clang::Type::Elaborated: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4557 | return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type) |
| 4558 | ->getNamedType() |
| 4559 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4560 | .GetNumMemberFunctions(); |
| 4561 | |
| 4562 | case clang::Type::Paren: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4563 | return CompilerType(this, llvm::cast<clang::ParenType>(qual_type) |
| 4564 | ->desugar() |
| 4565 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4566 | .GetNumMemberFunctions(); |
| 4567 | |
| 4568 | default: |
| 4569 | break; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4570 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4571 | } |
| 4572 | return num_functions; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4573 | } |
| 4574 | |
| 4575 | TypeMemberFunctionImpl |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4576 | ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type, |
| 4577 | size_t idx) { |
| 4578 | std::string name; |
| 4579 | MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown); |
| 4580 | CompilerType clang_type; |
| 4581 | CompilerDecl clang_decl; |
| 4582 | if (type) { |
| 4583 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 4584 | switch (qual_type->getTypeClass()) { |
| 4585 | case clang::Type::Record: |
| 4586 | if (GetCompleteQualType(getASTContext(), qual_type)) { |
| 4587 | const clang::RecordType *record_type = |
| 4588 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 4589 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 4590 | assert(record_decl); |
| 4591 | const clang::CXXRecordDecl *cxx_record_decl = |
| 4592 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 4593 | if (cxx_record_decl) { |
| 4594 | auto method_iter = cxx_record_decl->method_begin(); |
| 4595 | auto method_end = cxx_record_decl->method_end(); |
| 4596 | if (idx < |
| 4597 | static_cast<size_t>(std::distance(method_iter, method_end))) { |
| 4598 | std::advance(method_iter, idx); |
| 4599 | clang::CXXMethodDecl *cxx_method_decl = |
| 4600 | method_iter->getCanonicalDecl(); |
| 4601 | if (cxx_method_decl) { |
| 4602 | name = cxx_method_decl->getDeclName().getAsString(); |
| 4603 | if (cxx_method_decl->isStatic()) |
| 4604 | kind = lldb::eMemberFunctionKindStaticMethod; |
| 4605 | else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl)) |
| 4606 | kind = lldb::eMemberFunctionKindConstructor; |
| 4607 | else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl)) |
| 4608 | kind = lldb::eMemberFunctionKindDestructor; |
| 4609 | else |
| 4610 | kind = lldb::eMemberFunctionKindInstanceMethod; |
| 4611 | clang_type = CompilerType( |
| 4612 | this, cxx_method_decl->getType().getAsOpaquePtr()); |
| 4613 | clang_decl = CompilerDecl(this, cxx_method_decl); |
| 4614 | } |
| 4615 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4616 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4617 | } |
| 4618 | break; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4619 | |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 4620 | case clang::Type::ObjCObjectPointer: { |
| 4621 | const clang::ObjCObjectPointerType *objc_class_type = |
Sean Callanan | 732a6f4 | 2017-05-15 19:55:20 +0000 | [diff] [blame] | 4622 | qual_type->getAs<clang::ObjCObjectPointerType>(); |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 4623 | const clang::ObjCInterfaceType *objc_interface_type = |
| 4624 | objc_class_type->getInterfaceType(); |
| 4625 | if (objc_interface_type && |
Davide Italiano | 52ffb53 | 2017-04-17 18:24:18 +0000 | [diff] [blame] | 4626 | GetCompleteType(static_cast<lldb::opaque_compiler_type_t>( |
| 4627 | const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) { |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 4628 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 4629 | objc_interface_type->getDecl(); |
| 4630 | if (class_interface_decl) { |
| 4631 | auto method_iter = class_interface_decl->meth_begin(); |
| 4632 | auto method_end = class_interface_decl->meth_end(); |
| 4633 | if (idx < |
| 4634 | static_cast<size_t>(std::distance(method_iter, method_end))) { |
| 4635 | std::advance(method_iter, idx); |
| 4636 | clang::ObjCMethodDecl *objc_method_decl = |
| 4637 | method_iter->getCanonicalDecl(); |
| 4638 | if (objc_method_decl) { |
| 4639 | clang_decl = CompilerDecl(this, objc_method_decl); |
| 4640 | name = objc_method_decl->getSelector().getAsString(); |
| 4641 | if (objc_method_decl->isClassMethod()) |
| 4642 | kind = lldb::eMemberFunctionKindStaticMethod; |
| 4643 | else |
| 4644 | kind = lldb::eMemberFunctionKindInstanceMethod; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4645 | } |
| 4646 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4647 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4648 | } |
| 4649 | break; |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 4650 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4651 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4652 | case clang::Type::ObjCObject: |
| 4653 | case clang::Type::ObjCInterface: |
| 4654 | if (GetCompleteType(type)) { |
| 4655 | const clang::ObjCObjectType *objc_class_type = |
| 4656 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 4657 | if (objc_class_type) { |
| 4658 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 4659 | objc_class_type->getInterface(); |
| 4660 | if (class_interface_decl) { |
| 4661 | auto method_iter = class_interface_decl->meth_begin(); |
| 4662 | auto method_end = class_interface_decl->meth_end(); |
| 4663 | if (idx < |
| 4664 | static_cast<size_t>(std::distance(method_iter, method_end))) { |
| 4665 | std::advance(method_iter, idx); |
| 4666 | clang::ObjCMethodDecl *objc_method_decl = |
| 4667 | method_iter->getCanonicalDecl(); |
| 4668 | if (objc_method_decl) { |
| 4669 | clang_decl = CompilerDecl(this, objc_method_decl); |
| 4670 | name = objc_method_decl->getSelector().getAsString(); |
| 4671 | if (objc_method_decl->isClassMethod()) |
| 4672 | kind = lldb::eMemberFunctionKindStaticMethod; |
| 4673 | else |
| 4674 | kind = lldb::eMemberFunctionKindInstanceMethod; |
| 4675 | } |
| 4676 | } |
| 4677 | } |
Ewan Crawford | 27fc7a7 | 2016-03-15 09:50:16 +0000 | [diff] [blame] | 4678 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4679 | } |
| 4680 | break; |
Ewan Crawford | 27fc7a7 | 2016-03-15 09:50:16 +0000 | [diff] [blame] | 4681 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4682 | case clang::Type::Typedef: |
| 4683 | return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type) |
| 4684 | ->getDecl() |
| 4685 | ->getUnderlyingType() |
| 4686 | .getAsOpaquePtr(), |
| 4687 | idx); |
Ewan Crawford | 27fc7a7 | 2016-03-15 09:50:16 +0000 | [diff] [blame] | 4688 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4689 | case clang::Type::Auto: |
| 4690 | return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type) |
| 4691 | ->getDeducedType() |
| 4692 | .getAsOpaquePtr(), |
| 4693 | idx); |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 4694 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4695 | case clang::Type::Elaborated: |
| 4696 | return GetMemberFunctionAtIndex( |
| 4697 | llvm::cast<clang::ElaboratedType>(qual_type) |
| 4698 | ->getNamedType() |
| 4699 | .getAsOpaquePtr(), |
| 4700 | idx); |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 4701 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4702 | case clang::Type::Paren: |
| 4703 | return GetMemberFunctionAtIndex( |
| 4704 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 4705 | idx); |
| 4706 | |
| 4707 | default: |
| 4708 | break; |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 4709 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4710 | } |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 4711 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4712 | if (kind == eMemberFunctionKindUnknown) |
| 4713 | return TypeMemberFunctionImpl(); |
| 4714 | else |
| 4715 | return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind); |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 4716 | } |
| 4717 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 4718 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4719 | ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) { |
| 4720 | if (type) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4721 | return CompilerType( |
| 4722 | this, GetQualType(type).getNonReferenceType().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4723 | return CompilerType(); |
| 4724 | } |
| 4725 | |
| 4726 | CompilerType ClangASTContext::CreateTypedefType( |
| 4727 | const CompilerType &type, const char *typedef_name, |
| 4728 | const CompilerDeclContext &compiler_decl_ctx) { |
| 4729 | if (type && typedef_name && typedef_name[0]) { |
| 4730 | ClangASTContext *ast = |
| 4731 | llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); |
| 4732 | if (!ast) |
| 4733 | return CompilerType(); |
| 4734 | clang::ASTContext *clang_ast = ast->getASTContext(); |
| 4735 | clang::QualType qual_type(ClangUtil::GetQualType(type)); |
| 4736 | |
| 4737 | clang::DeclContext *decl_ctx = |
| 4738 | ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx); |
| 4739 | if (decl_ctx == nullptr) |
| 4740 | decl_ctx = ast->getASTContext()->getTranslationUnitDecl(); |
| 4741 | |
| 4742 | clang::TypedefDecl *decl = clang::TypedefDecl::Create( |
| 4743 | *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(), |
| 4744 | &clang_ast->Idents.get(typedef_name), |
| 4745 | clang_ast->getTrivialTypeSourceInfo(qual_type)); |
| 4746 | |
| 4747 | decl->setAccess(clang::AS_public); // TODO respect proper access specifier |
| 4748 | |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 4749 | decl_ctx->addDecl(decl); |
| 4750 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4751 | // Get a uniqued clang::QualType for the typedef decl type |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4752 | return CompilerType(ast, clang_ast->getTypedefType(decl).getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4753 | } |
| 4754 | return CompilerType(); |
| 4755 | } |
| 4756 | |
| 4757 | CompilerType |
| 4758 | ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) { |
| 4759 | if (type) { |
| 4760 | clang::QualType qual_type(GetQualType(type)); |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4761 | return CompilerType( |
| 4762 | this, qual_type.getTypePtr()->getPointeeType().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4763 | } |
| 4764 | return CompilerType(); |
| 4765 | } |
| 4766 | |
| 4767 | CompilerType |
| 4768 | ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) { |
| 4769 | if (type) { |
| 4770 | clang::QualType qual_type(GetQualType(type)); |
| 4771 | |
| 4772 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 4773 | switch (type_class) { |
| 4774 | case clang::Type::ObjCObject: |
| 4775 | case clang::Type::ObjCInterface: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4776 | return CompilerType(this, getASTContext() |
| 4777 | ->getObjCObjectPointerType(qual_type) |
| 4778 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4779 | |
| 4780 | default: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4781 | return CompilerType( |
| 4782 | this, getASTContext()->getPointerType(qual_type).getAsOpaquePtr()); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4783 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4784 | } |
| 4785 | return CompilerType(); |
| 4786 | } |
| 4787 | |
| 4788 | CompilerType |
| 4789 | ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) { |
| 4790 | if (type) |
| 4791 | return CompilerType(this, getASTContext() |
| 4792 | ->getLValueReferenceType(GetQualType(type)) |
| 4793 | .getAsOpaquePtr()); |
| 4794 | else |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 4795 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4796 | } |
| 4797 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4798 | CompilerType |
| 4799 | ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) { |
| 4800 | if (type) |
| 4801 | return CompilerType(this, getASTContext() |
| 4802 | ->getRValueReferenceType(GetQualType(type)) |
| 4803 | .getAsOpaquePtr()); |
| 4804 | else |
| 4805 | return CompilerType(); |
| 4806 | } |
| 4807 | |
| 4808 | CompilerType |
| 4809 | ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) { |
| 4810 | if (type) { |
| 4811 | clang::QualType result(GetQualType(type)); |
| 4812 | result.addConst(); |
| 4813 | return CompilerType(this, result.getAsOpaquePtr()); |
| 4814 | } |
| 4815 | return CompilerType(); |
| 4816 | } |
| 4817 | |
| 4818 | CompilerType |
| 4819 | ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) { |
| 4820 | if (type) { |
| 4821 | clang::QualType result(GetQualType(type)); |
| 4822 | result.addVolatile(); |
| 4823 | return CompilerType(this, result.getAsOpaquePtr()); |
| 4824 | } |
| 4825 | return CompilerType(); |
| 4826 | } |
| 4827 | |
| 4828 | CompilerType |
| 4829 | ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) { |
| 4830 | if (type) { |
| 4831 | clang::QualType result(GetQualType(type)); |
| 4832 | result.addRestrict(); |
| 4833 | return CompilerType(this, result.getAsOpaquePtr()); |
| 4834 | } |
| 4835 | return CompilerType(); |
| 4836 | } |
| 4837 | |
| 4838 | CompilerType |
| 4839 | ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type, |
| 4840 | const char *typedef_name, |
| 4841 | const CompilerDeclContext &compiler_decl_ctx) { |
| 4842 | if (type) { |
| 4843 | clang::ASTContext *clang_ast = getASTContext(); |
| 4844 | clang::QualType qual_type(GetQualType(type)); |
| 4845 | |
| 4846 | clang::DeclContext *decl_ctx = |
| 4847 | ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx); |
| 4848 | if (decl_ctx == nullptr) |
| 4849 | decl_ctx = getASTContext()->getTranslationUnitDecl(); |
| 4850 | |
| 4851 | clang::TypedefDecl *decl = clang::TypedefDecl::Create( |
| 4852 | *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(), |
| 4853 | &clang_ast->Idents.get(typedef_name), |
| 4854 | clang_ast->getTrivialTypeSourceInfo(qual_type)); |
| 4855 | |
| 4856 | clang::TagDecl *tdecl = nullptr; |
| 4857 | if (!qual_type.isNull()) { |
| 4858 | if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>()) |
| 4859 | tdecl = rt->getDecl(); |
| 4860 | if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>()) |
| 4861 | tdecl = et->getDecl(); |
| 4862 | } |
| 4863 | |
| 4864 | // Check whether this declaration is an anonymous struct, union, or enum, |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 4865 | // hidden behind a typedef. If so, we try to check whether we have a |
| 4866 | // typedef tag to attach to the original record declaration |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4867 | if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl()) |
| 4868 | tdecl->setTypedefNameForAnonDecl(decl); |
| 4869 | |
| 4870 | decl->setAccess(clang::AS_public); // TODO respect proper access specifier |
| 4871 | |
| 4872 | // Get a uniqued clang::QualType for the typedef decl type |
| 4873 | return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr()); |
| 4874 | } |
| 4875 | return CompilerType(); |
| 4876 | } |
| 4877 | |
| 4878 | CompilerType |
| 4879 | ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) { |
| 4880 | if (type) { |
| 4881 | const clang::TypedefType *typedef_type = |
| 4882 | llvm::dyn_cast<clang::TypedefType>(GetQualType(type)); |
| 4883 | if (typedef_type) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4884 | return CompilerType( |
| 4885 | this, typedef_type->getDecl()->getUnderlyingType().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4886 | } |
| 4887 | return CompilerType(); |
| 4888 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4889 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4890 | // Create related types using the current type's AST |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4891 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4892 | CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) { |
Raphael Isemann | c502bae | 2019-11-20 12:40:08 +0100 | [diff] [blame] | 4893 | return ClangASTContext::GetBasicType(basic_type); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4894 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4895 | // Exploring the type |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4896 | |
Alex Langford | b482db6 | 2019-09-06 21:05:21 +0000 | [diff] [blame] | 4897 | const llvm::fltSemantics & |
| 4898 | ClangASTContext::GetFloatTypeSemantics(size_t byte_size) { |
| 4899 | if (auto *ast = getASTContext()) { |
| 4900 | const size_t bit_size = byte_size * 8; |
| 4901 | if (bit_size == ast->getTypeSize(ast->FloatTy)) |
| 4902 | return ast->getFloatTypeSemantics(ast->FloatTy); |
| 4903 | else if (bit_size == ast->getTypeSize(ast->DoubleTy)) |
| 4904 | return ast->getFloatTypeSemantics(ast->DoubleTy); |
| 4905 | else if (bit_size == ast->getTypeSize(ast->LongDoubleTy)) |
| 4906 | return ast->getFloatTypeSemantics(ast->LongDoubleTy); |
| 4907 | else if (bit_size == ast->getTypeSize(ast->HalfTy)) |
| 4908 | return ast->getFloatTypeSemantics(ast->HalfTy); |
| 4909 | } |
| 4910 | return llvm::APFloatBase::Bogus(); |
| 4911 | } |
| 4912 | |
Adrian Prantl | 2ee7b88 | 2019-01-16 21:19:20 +0000 | [diff] [blame] | 4913 | Optional<uint64_t> |
| 4914 | ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type, |
| 4915 | ExecutionContextScope *exe_scope) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4916 | if (GetCompleteType(type)) { |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4917 | clang::QualType qual_type(GetCanonicalQualType(type)); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4918 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4919 | switch (type_class) { |
| 4920 | case clang::Type::Record: |
| 4921 | if (GetCompleteType(type)) |
| 4922 | return getASTContext()->getTypeSize(qual_type); |
| 4923 | else |
Adrian Prantl | 2ee7b88 | 2019-01-16 21:19:20 +0000 | [diff] [blame] | 4924 | return None; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4925 | break; |
Enrico Granata | 36f51e4 | 2015-12-18 22:41:25 +0000 | [diff] [blame] | 4926 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4927 | case clang::Type::ObjCInterface: |
| 4928 | case clang::Type::ObjCObject: { |
| 4929 | ExecutionContext exe_ctx(exe_scope); |
| 4930 | Process *process = exe_ctx.GetProcessPtr(); |
| 4931 | if (process) { |
Alex Langford | e823bbe | 2019-06-10 20:53:23 +0000 | [diff] [blame] | 4932 | ObjCLanguageRuntime *objc_runtime = ObjCLanguageRuntime::Get(*process); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4933 | if (objc_runtime) { |
| 4934 | uint64_t bit_size = 0; |
| 4935 | if (objc_runtime->GetTypeBitSize( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 4936 | CompilerType(this, qual_type.getAsOpaquePtr()), bit_size)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4937 | return bit_size; |
| 4938 | } |
| 4939 | } else { |
| 4940 | static bool g_printed = false; |
| 4941 | if (!g_printed) { |
| 4942 | StreamString s; |
| 4943 | DumpTypeDescription(type, &s); |
| 4944 | |
| 4945 | llvm::outs() << "warning: trying to determine the size of type "; |
| 4946 | llvm::outs() << s.GetString() << "\n"; |
| 4947 | llvm::outs() << "without a valid ExecutionContext. this is not " |
| 4948 | "reliable. please file a bug against LLDB.\n"; |
| 4949 | llvm::outs() << "backtrace:\n"; |
| 4950 | llvm::sys::PrintStackTrace(llvm::outs()); |
| 4951 | llvm::outs() << "\n"; |
| 4952 | g_printed = true; |
| 4953 | } |
| 4954 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4955 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4956 | LLVM_FALLTHROUGH; |
| 4957 | default: |
| 4958 | const uint32_t bit_size = getASTContext()->getTypeSize(qual_type); |
| 4959 | if (bit_size == 0) { |
| 4960 | if (qual_type->isIncompleteArrayType()) |
| 4961 | return getASTContext()->getTypeSize( |
| 4962 | qual_type->getArrayElementTypeNoTypeQual() |
| 4963 | ->getCanonicalTypeUnqualified()); |
| 4964 | } |
| 4965 | if (qual_type->isObjCObjectOrInterfaceType()) |
| 4966 | return bit_size + |
| 4967 | getASTContext()->getTypeSize( |
| 4968 | getASTContext()->ObjCBuiltinClassTy); |
Adrian Prantl | 2ee7b88 | 2019-01-16 21:19:20 +0000 | [diff] [blame] | 4969 | // Function types actually have a size of 0, that's not an error. |
| 4970 | if (qual_type->isFunctionProtoType()) |
| 4971 | return bit_size; |
| 4972 | if (bit_size) |
| 4973 | return bit_size; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4974 | } |
| 4975 | } |
Adrian Prantl | 2ee7b88 | 2019-01-16 21:19:20 +0000 | [diff] [blame] | 4976 | return None; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 4977 | } |
| 4978 | |
Davide Italiano | 36f13e4 | 2019-08-12 20:03:19 +0000 | [diff] [blame] | 4979 | llvm::Optional<size_t> |
Davide Italiano | 7f9bbe0 | 2019-08-12 21:49:54 +0000 | [diff] [blame] | 4980 | ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type, |
| 4981 | ExecutionContextScope *exe_scope) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4982 | if (GetCompleteType(type)) |
| 4983 | return getASTContext()->getTypeAlign(GetQualType(type)); |
Davide Italiano | 36f13e4 | 2019-08-12 20:03:19 +0000 | [diff] [blame] | 4984 | return {}; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 4985 | } |
| 4986 | |
| 4987 | lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type, |
| 4988 | uint64_t &count) { |
| 4989 | if (!type) |
| 4990 | return lldb::eEncodingInvalid; |
| 4991 | |
| 4992 | count = 1; |
| 4993 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 4994 | |
| 4995 | switch (qual_type->getTypeClass()) { |
| 4996 | case clang::Type::UnaryTransform: |
| 4997 | break; |
| 4998 | |
| 4999 | case clang::Type::FunctionNoProto: |
| 5000 | case clang::Type::FunctionProto: |
| 5001 | break; |
| 5002 | |
| 5003 | case clang::Type::IncompleteArray: |
| 5004 | case clang::Type::VariableArray: |
| 5005 | break; |
| 5006 | |
| 5007 | case clang::Type::ConstantArray: |
| 5008 | break; |
| 5009 | |
Fangrui Song | 8f28488 | 2018-07-13 22:40:40 +0000 | [diff] [blame] | 5010 | case clang::Type::DependentVector: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5011 | case clang::Type::ExtVector: |
| 5012 | case clang::Type::Vector: |
| 5013 | // TODO: Set this to more than one??? |
| 5014 | break; |
| 5015 | |
| 5016 | case clang::Type::Builtin: |
| 5017 | switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { |
| 5018 | case clang::BuiltinType::Void: |
| 5019 | break; |
| 5020 | |
| 5021 | case clang::BuiltinType::Bool: |
| 5022 | case clang::BuiltinType::Char_S: |
| 5023 | case clang::BuiltinType::SChar: |
| 5024 | case clang::BuiltinType::WChar_S: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5025 | case clang::BuiltinType::Short: |
| 5026 | case clang::BuiltinType::Int: |
| 5027 | case clang::BuiltinType::Long: |
| 5028 | case clang::BuiltinType::LongLong: |
| 5029 | case clang::BuiltinType::Int128: |
| 5030 | return lldb::eEncodingSint; |
| 5031 | |
| 5032 | case clang::BuiltinType::Char_U: |
| 5033 | case clang::BuiltinType::UChar: |
| 5034 | case clang::BuiltinType::WChar_U: |
Richard Smith | 51d12d8 | 2018-05-02 02:43:22 +0000 | [diff] [blame] | 5035 | case clang::BuiltinType::Char8: |
| 5036 | case clang::BuiltinType::Char16: |
| 5037 | case clang::BuiltinType::Char32: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5038 | case clang::BuiltinType::UShort: |
| 5039 | case clang::BuiltinType::UInt: |
| 5040 | case clang::BuiltinType::ULong: |
| 5041 | case clang::BuiltinType::ULongLong: |
| 5042 | case clang::BuiltinType::UInt128: |
| 5043 | return lldb::eEncodingUint; |
| 5044 | |
Ilya Biryukov | fc48ac6 | 2018-06-05 10:07:07 +0000 | [diff] [blame] | 5045 | // Fixed point types. Note that they are currently ignored. |
| 5046 | case clang::BuiltinType::ShortAccum: |
| 5047 | case clang::BuiltinType::Accum: |
| 5048 | case clang::BuiltinType::LongAccum: |
| 5049 | case clang::BuiltinType::UShortAccum: |
| 5050 | case clang::BuiltinType::UAccum: |
| 5051 | case clang::BuiltinType::ULongAccum: |
Fangrui Song | a5e59c5 | 2018-06-14 18:19:40 +0000 | [diff] [blame] | 5052 | case clang::BuiltinType::ShortFract: |
Fangrui Song | a5e59c5 | 2018-06-14 18:19:40 +0000 | [diff] [blame] | 5053 | case clang::BuiltinType::Fract: |
| 5054 | case clang::BuiltinType::LongFract: |
| 5055 | case clang::BuiltinType::UShortFract: |
| 5056 | case clang::BuiltinType::UFract: |
| 5057 | case clang::BuiltinType::ULongFract: |
| 5058 | case clang::BuiltinType::SatShortAccum: |
| 5059 | case clang::BuiltinType::SatAccum: |
| 5060 | case clang::BuiltinType::SatLongAccum: |
| 5061 | case clang::BuiltinType::SatUShortAccum: |
| 5062 | case clang::BuiltinType::SatUAccum: |
| 5063 | case clang::BuiltinType::SatULongAccum: |
| 5064 | case clang::BuiltinType::SatShortFract: |
| 5065 | case clang::BuiltinType::SatFract: |
| 5066 | case clang::BuiltinType::SatLongFract: |
| 5067 | case clang::BuiltinType::SatUShortFract: |
| 5068 | case clang::BuiltinType::SatUFract: |
| 5069 | case clang::BuiltinType::SatULongFract: |
Ilya Biryukov | fc48ac6 | 2018-06-05 10:07:07 +0000 | [diff] [blame] | 5070 | break; |
| 5071 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5072 | case clang::BuiltinType::Half: |
| 5073 | case clang::BuiltinType::Float: |
Ted Woodward | 4355c7c | 2017-09-20 19:16:53 +0000 | [diff] [blame] | 5074 | case clang::BuiltinType::Float16: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5075 | case clang::BuiltinType::Float128: |
| 5076 | case clang::BuiltinType::Double: |
| 5077 | case clang::BuiltinType::LongDouble: |
| 5078 | return lldb::eEncodingIEEE754; |
| 5079 | |
| 5080 | case clang::BuiltinType::ObjCClass: |
| 5081 | case clang::BuiltinType::ObjCId: |
| 5082 | case clang::BuiltinType::ObjCSel: |
| 5083 | return lldb::eEncodingUint; |
| 5084 | |
| 5085 | case clang::BuiltinType::NullPtr: |
| 5086 | return lldb::eEncodingUint; |
| 5087 | |
| 5088 | case clang::BuiltinType::Kind::ARCUnbridgedCast: |
| 5089 | case clang::BuiltinType::Kind::BoundMember: |
| 5090 | case clang::BuiltinType::Kind::BuiltinFn: |
| 5091 | case clang::BuiltinType::Kind::Dependent: |
| 5092 | case clang::BuiltinType::Kind::OCLClkEvent: |
| 5093 | case clang::BuiltinType::Kind::OCLEvent: |
| 5094 | case clang::BuiltinType::Kind::OCLImage1dRO: |
| 5095 | case clang::BuiltinType::Kind::OCLImage1dWO: |
| 5096 | case clang::BuiltinType::Kind::OCLImage1dRW: |
| 5097 | case clang::BuiltinType::Kind::OCLImage1dArrayRO: |
| 5098 | case clang::BuiltinType::Kind::OCLImage1dArrayWO: |
| 5099 | case clang::BuiltinType::Kind::OCLImage1dArrayRW: |
| 5100 | case clang::BuiltinType::Kind::OCLImage1dBufferRO: |
| 5101 | case clang::BuiltinType::Kind::OCLImage1dBufferWO: |
| 5102 | case clang::BuiltinType::Kind::OCLImage1dBufferRW: |
| 5103 | case clang::BuiltinType::Kind::OCLImage2dRO: |
| 5104 | case clang::BuiltinType::Kind::OCLImage2dWO: |
| 5105 | case clang::BuiltinType::Kind::OCLImage2dRW: |
| 5106 | case clang::BuiltinType::Kind::OCLImage2dArrayRO: |
| 5107 | case clang::BuiltinType::Kind::OCLImage2dArrayWO: |
| 5108 | case clang::BuiltinType::Kind::OCLImage2dArrayRW: |
| 5109 | case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO: |
| 5110 | case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO: |
| 5111 | case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW: |
| 5112 | case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO: |
| 5113 | case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO: |
| 5114 | case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW: |
| 5115 | case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO: |
| 5116 | case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO: |
| 5117 | case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW: |
| 5118 | case clang::BuiltinType::Kind::OCLImage2dDepthRO: |
| 5119 | case clang::BuiltinType::Kind::OCLImage2dDepthWO: |
| 5120 | case clang::BuiltinType::Kind::OCLImage2dDepthRW: |
| 5121 | case clang::BuiltinType::Kind::OCLImage2dMSAARO: |
| 5122 | case clang::BuiltinType::Kind::OCLImage2dMSAAWO: |
| 5123 | case clang::BuiltinType::Kind::OCLImage2dMSAARW: |
| 5124 | case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO: |
| 5125 | case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO: |
| 5126 | case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW: |
| 5127 | case clang::BuiltinType::Kind::OCLImage3dRO: |
| 5128 | case clang::BuiltinType::Kind::OCLImage3dWO: |
| 5129 | case clang::BuiltinType::Kind::OCLImage3dRW: |
| 5130 | case clang::BuiltinType::Kind::OCLQueue: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5131 | case clang::BuiltinType::Kind::OCLReserveID: |
| 5132 | case clang::BuiltinType::Kind::OCLSampler: |
| 5133 | case clang::BuiltinType::Kind::OMPArraySection: |
| 5134 | case clang::BuiltinType::Kind::Overload: |
| 5135 | case clang::BuiltinType::Kind::PseudoObject: |
| 5136 | case clang::BuiltinType::Kind::UnknownAny: |
| 5137 | break; |
Jorge Gorbe Moya | a6e6c18 | 2018-11-08 22:04:58 +0000 | [diff] [blame] | 5138 | |
| 5139 | case clang::BuiltinType::OCLIntelSubgroupAVCMcePayload: |
| 5140 | case clang::BuiltinType::OCLIntelSubgroupAVCImePayload: |
| 5141 | case clang::BuiltinType::OCLIntelSubgroupAVCRefPayload: |
| 5142 | case clang::BuiltinType::OCLIntelSubgroupAVCSicPayload: |
| 5143 | case clang::BuiltinType::OCLIntelSubgroupAVCMceResult: |
| 5144 | case clang::BuiltinType::OCLIntelSubgroupAVCImeResult: |
| 5145 | case clang::BuiltinType::OCLIntelSubgroupAVCRefResult: |
| 5146 | case clang::BuiltinType::OCLIntelSubgroupAVCSicResult: |
| 5147 | case clang::BuiltinType::OCLIntelSubgroupAVCImeResultSingleRefStreamout: |
| 5148 | case clang::BuiltinType::OCLIntelSubgroupAVCImeResultDualRefStreamout: |
| 5149 | case clang::BuiltinType::OCLIntelSubgroupAVCImeSingleRefStreamin: |
| 5150 | case clang::BuiltinType::OCLIntelSubgroupAVCImeDualRefStreamin: |
| 5151 | break; |
Raphael Isemann | 339b5d1 | 2019-08-09 09:58:47 +0000 | [diff] [blame] | 5152 | |
| 5153 | case clang::BuiltinType::SveBool: |
| 5154 | case clang::BuiltinType::SveInt8: |
| 5155 | case clang::BuiltinType::SveInt16: |
| 5156 | case clang::BuiltinType::SveInt32: |
| 5157 | case clang::BuiltinType::SveInt64: |
| 5158 | case clang::BuiltinType::SveUint8: |
| 5159 | case clang::BuiltinType::SveUint16: |
| 5160 | case clang::BuiltinType::SveUint32: |
| 5161 | case clang::BuiltinType::SveUint64: |
| 5162 | case clang::BuiltinType::SveFloat16: |
| 5163 | case clang::BuiltinType::SveFloat32: |
| 5164 | case clang::BuiltinType::SveFloat64: |
| 5165 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5166 | } |
| 5167 | break; |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5168 | // All pointer types are represented as unsigned integer encodings. We may |
| 5169 | // 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] | 5170 | case clang::Type::ObjCObjectPointer: |
| 5171 | case clang::Type::BlockPointer: |
| 5172 | case clang::Type::Pointer: |
| 5173 | case clang::Type::LValueReference: |
| 5174 | case clang::Type::RValueReference: |
| 5175 | case clang::Type::MemberPointer: |
| 5176 | return lldb::eEncodingUint; |
| 5177 | case clang::Type::Complex: { |
| 5178 | lldb::Encoding encoding = lldb::eEncodingIEEE754; |
| 5179 | if (qual_type->isComplexType()) |
| 5180 | encoding = lldb::eEncodingIEEE754; |
| 5181 | else { |
| 5182 | const clang::ComplexType *complex_type = |
| 5183 | qual_type->getAsComplexIntegerType(); |
| 5184 | if (complex_type) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5185 | encoding = |
| 5186 | CompilerType(this, complex_type->getElementType().getAsOpaquePtr()) |
| 5187 | .GetEncoding(count); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5188 | else |
| 5189 | encoding = lldb::eEncodingSint; |
| 5190 | } |
| 5191 | count = 2; |
| 5192 | return encoding; |
| 5193 | } |
| 5194 | |
| 5195 | case clang::Type::ObjCInterface: |
| 5196 | break; |
| 5197 | case clang::Type::Record: |
| 5198 | break; |
| 5199 | case clang::Type::Enum: |
| 5200 | return lldb::eEncodingSint; |
| 5201 | case clang::Type::Typedef: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5202 | return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type) |
| 5203 | ->getDecl() |
| 5204 | ->getUnderlyingType() |
| 5205 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5206 | .GetEncoding(count); |
| 5207 | |
| 5208 | case clang::Type::Auto: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5209 | return CompilerType(this, llvm::cast<clang::AutoType>(qual_type) |
| 5210 | ->getDeducedType() |
| 5211 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5212 | .GetEncoding(count); |
| 5213 | |
| 5214 | case clang::Type::Elaborated: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5215 | return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type) |
| 5216 | ->getNamedType() |
| 5217 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5218 | .GetEncoding(count); |
| 5219 | |
| 5220 | case clang::Type::Paren: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5221 | return CompilerType(this, llvm::cast<clang::ParenType>(qual_type) |
| 5222 | ->desugar() |
| 5223 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5224 | .GetEncoding(count); |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 5225 | case clang::Type::TypeOfExpr: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5226 | return CompilerType(this, llvm::cast<clang::TypeOfExprType>(qual_type) |
| 5227 | ->getUnderlyingExpr() |
| 5228 | ->getType() |
| 5229 | .getAsOpaquePtr()) |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 5230 | .GetEncoding(count); |
| 5231 | case clang::Type::TypeOf: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5232 | return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type) |
| 5233 | ->getUnderlyingType() |
| 5234 | .getAsOpaquePtr()) |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 5235 | .GetEncoding(count); |
| 5236 | case clang::Type::Decltype: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5237 | return CompilerType(this, llvm::cast<clang::DecltypeType>(qual_type) |
| 5238 | ->getUnderlyingType() |
| 5239 | .getAsOpaquePtr()) |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 5240 | .GetEncoding(count); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5241 | case clang::Type::DependentSizedArray: |
| 5242 | case clang::Type::DependentSizedExtVector: |
| 5243 | case clang::Type::UnresolvedUsing: |
| 5244 | case clang::Type::Attributed: |
| 5245 | case clang::Type::TemplateTypeParm: |
| 5246 | case clang::Type::SubstTemplateTypeParm: |
| 5247 | case clang::Type::SubstTemplateTypeParmPack: |
| 5248 | case clang::Type::InjectedClassName: |
| 5249 | case clang::Type::DependentName: |
| 5250 | case clang::Type::DependentTemplateSpecialization: |
| 5251 | case clang::Type::PackExpansion: |
| 5252 | case clang::Type::ObjCObject: |
| 5253 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5254 | case clang::Type::TemplateSpecialization: |
Pavel Labath | 4f19fce2 | 2017-02-17 13:39:50 +0000 | [diff] [blame] | 5255 | case clang::Type::DeducedTemplateSpecialization: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5256 | case clang::Type::Atomic: |
| 5257 | case clang::Type::Adjusted: |
| 5258 | case clang::Type::Pipe: |
| 5259 | break; |
| 5260 | |
| 5261 | // pointer type decayed from an array or function type. |
| 5262 | case clang::Type::Decayed: |
| 5263 | break; |
Zachary Turner | 5a8ad459 | 2016-10-05 17:07:34 +0000 | [diff] [blame] | 5264 | case clang::Type::ObjCTypeParam: |
| 5265 | break; |
Ted Woodward | 66060cf | 2017-10-11 22:42:21 +0000 | [diff] [blame] | 5266 | |
| 5267 | case clang::Type::DependentAddressSpace: |
| 5268 | break; |
Krasimir Georgiev | 435e76a | 2019-05-07 13:59:30 +0000 | [diff] [blame] | 5269 | case clang::Type::MacroQualified: |
| 5270 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5271 | } |
| 5272 | count = 0; |
| 5273 | return lldb::eEncodingInvalid; |
| 5274 | } |
| 5275 | |
| 5276 | lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) { |
| 5277 | if (!type) |
| 5278 | return lldb::eFormatDefault; |
| 5279 | |
| 5280 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 5281 | |
| 5282 | switch (qual_type->getTypeClass()) { |
| 5283 | case clang::Type::UnaryTransform: |
| 5284 | break; |
| 5285 | |
| 5286 | case clang::Type::FunctionNoProto: |
| 5287 | case clang::Type::FunctionProto: |
| 5288 | break; |
| 5289 | |
| 5290 | case clang::Type::IncompleteArray: |
| 5291 | case clang::Type::VariableArray: |
| 5292 | break; |
| 5293 | |
| 5294 | case clang::Type::ConstantArray: |
| 5295 | return lldb::eFormatVoid; // no value |
| 5296 | |
Fangrui Song | 8f28488 | 2018-07-13 22:40:40 +0000 | [diff] [blame] | 5297 | case clang::Type::DependentVector: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5298 | case clang::Type::ExtVector: |
| 5299 | case clang::Type::Vector: |
| 5300 | break; |
| 5301 | |
| 5302 | case clang::Type::Builtin: |
| 5303 | switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { |
| 5304 | // default: assert(0 && "Unknown builtin type!"); |
| 5305 | case clang::BuiltinType::UnknownAny: |
| 5306 | case clang::BuiltinType::Void: |
| 5307 | case clang::BuiltinType::BoundMember: |
| 5308 | break; |
| 5309 | |
| 5310 | case clang::BuiltinType::Bool: |
| 5311 | return lldb::eFormatBoolean; |
| 5312 | case clang::BuiltinType::Char_S: |
| 5313 | case clang::BuiltinType::SChar: |
| 5314 | case clang::BuiltinType::WChar_S: |
| 5315 | case clang::BuiltinType::Char_U: |
| 5316 | case clang::BuiltinType::UChar: |
| 5317 | case clang::BuiltinType::WChar_U: |
| 5318 | return lldb::eFormatChar; |
| 5319 | case clang::BuiltinType::Char16: |
| 5320 | return lldb::eFormatUnicode16; |
| 5321 | case clang::BuiltinType::Char32: |
| 5322 | return lldb::eFormatUnicode32; |
| 5323 | case clang::BuiltinType::UShort: |
| 5324 | return lldb::eFormatUnsigned; |
| 5325 | case clang::BuiltinType::Short: |
| 5326 | return lldb::eFormatDecimal; |
| 5327 | case clang::BuiltinType::UInt: |
| 5328 | return lldb::eFormatUnsigned; |
| 5329 | case clang::BuiltinType::Int: |
| 5330 | return lldb::eFormatDecimal; |
| 5331 | case clang::BuiltinType::ULong: |
| 5332 | return lldb::eFormatUnsigned; |
| 5333 | case clang::BuiltinType::Long: |
| 5334 | return lldb::eFormatDecimal; |
| 5335 | case clang::BuiltinType::ULongLong: |
| 5336 | return lldb::eFormatUnsigned; |
| 5337 | case clang::BuiltinType::LongLong: |
| 5338 | return lldb::eFormatDecimal; |
| 5339 | case clang::BuiltinType::UInt128: |
| 5340 | return lldb::eFormatUnsigned; |
| 5341 | case clang::BuiltinType::Int128: |
| 5342 | return lldb::eFormatDecimal; |
| 5343 | case clang::BuiltinType::Half: |
| 5344 | case clang::BuiltinType::Float: |
| 5345 | case clang::BuiltinType::Double: |
| 5346 | case clang::BuiltinType::LongDouble: |
| 5347 | return lldb::eFormatFloat; |
| 5348 | default: |
| 5349 | return lldb::eFormatHex; |
| 5350 | } |
| 5351 | break; |
| 5352 | case clang::Type::ObjCObjectPointer: |
| 5353 | return lldb::eFormatHex; |
| 5354 | case clang::Type::BlockPointer: |
| 5355 | return lldb::eFormatHex; |
| 5356 | case clang::Type::Pointer: |
| 5357 | return lldb::eFormatHex; |
| 5358 | case clang::Type::LValueReference: |
| 5359 | case clang::Type::RValueReference: |
| 5360 | return lldb::eFormatHex; |
| 5361 | case clang::Type::MemberPointer: |
| 5362 | break; |
| 5363 | case clang::Type::Complex: { |
| 5364 | if (qual_type->isComplexType()) |
| 5365 | return lldb::eFormatComplex; |
| 5366 | else |
| 5367 | return lldb::eFormatComplexInteger; |
| 5368 | } |
| 5369 | case clang::Type::ObjCInterface: |
| 5370 | break; |
| 5371 | case clang::Type::Record: |
| 5372 | break; |
| 5373 | case clang::Type::Enum: |
| 5374 | return lldb::eFormatEnum; |
| 5375 | case clang::Type::Typedef: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5376 | return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type) |
| 5377 | ->getDecl() |
| 5378 | ->getUnderlyingType() |
| 5379 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5380 | .GetFormat(); |
| 5381 | case clang::Type::Auto: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5382 | return CompilerType(this, llvm::cast<clang::AutoType>(qual_type) |
| 5383 | ->desugar() |
| 5384 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5385 | .GetFormat(); |
| 5386 | case clang::Type::Paren: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5387 | return CompilerType(this, llvm::cast<clang::ParenType>(qual_type) |
| 5388 | ->desugar() |
| 5389 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5390 | .GetFormat(); |
| 5391 | case clang::Type::Elaborated: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5392 | return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type) |
| 5393 | ->getNamedType() |
| 5394 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5395 | .GetFormat(); |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 5396 | case clang::Type::TypeOfExpr: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5397 | return CompilerType(this, llvm::cast<clang::TypeOfExprType>(qual_type) |
| 5398 | ->getUnderlyingExpr() |
| 5399 | ->getType() |
| 5400 | .getAsOpaquePtr()) |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 5401 | .GetFormat(); |
| 5402 | case clang::Type::TypeOf: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5403 | return CompilerType(this, llvm::cast<clang::TypeOfType>(qual_type) |
| 5404 | ->getUnderlyingType() |
| 5405 | .getAsOpaquePtr()) |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 5406 | .GetFormat(); |
| 5407 | case clang::Type::Decltype: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5408 | return CompilerType(this, llvm::cast<clang::DecltypeType>(qual_type) |
| 5409 | ->getUnderlyingType() |
| 5410 | .getAsOpaquePtr()) |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 5411 | .GetFormat(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5412 | case clang::Type::DependentSizedArray: |
| 5413 | case clang::Type::DependentSizedExtVector: |
| 5414 | case clang::Type::UnresolvedUsing: |
| 5415 | case clang::Type::Attributed: |
| 5416 | case clang::Type::TemplateTypeParm: |
| 5417 | case clang::Type::SubstTemplateTypeParm: |
| 5418 | case clang::Type::SubstTemplateTypeParmPack: |
| 5419 | case clang::Type::InjectedClassName: |
| 5420 | case clang::Type::DependentName: |
| 5421 | case clang::Type::DependentTemplateSpecialization: |
| 5422 | case clang::Type::PackExpansion: |
| 5423 | case clang::Type::ObjCObject: |
| 5424 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5425 | case clang::Type::TemplateSpecialization: |
Pavel Labath | 4f19fce2 | 2017-02-17 13:39:50 +0000 | [diff] [blame] | 5426 | case clang::Type::DeducedTemplateSpecialization: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5427 | case clang::Type::Atomic: |
| 5428 | case clang::Type::Adjusted: |
| 5429 | case clang::Type::Pipe: |
| 5430 | break; |
| 5431 | |
| 5432 | // pointer type decayed from an array or function type. |
| 5433 | case clang::Type::Decayed: |
| 5434 | break; |
Zachary Turner | 5a8ad459 | 2016-10-05 17:07:34 +0000 | [diff] [blame] | 5435 | case clang::Type::ObjCTypeParam: |
| 5436 | break; |
Ted Woodward | 66060cf | 2017-10-11 22:42:21 +0000 | [diff] [blame] | 5437 | |
| 5438 | case clang::Type::DependentAddressSpace: |
| 5439 | break; |
Krasimir Georgiev | 435e76a | 2019-05-07 13:59:30 +0000 | [diff] [blame] | 5440 | case clang::Type::MacroQualified: |
| 5441 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5442 | } |
| 5443 | // We don't know hot to display this type... |
| 5444 | return lldb::eFormatBytes; |
| 5445 | } |
| 5446 | |
| 5447 | static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl, |
| 5448 | bool check_superclass) { |
| 5449 | while (class_interface_decl) { |
| 5450 | if (class_interface_decl->ivar_size() > 0) |
| 5451 | return true; |
| 5452 | |
| 5453 | if (check_superclass) |
| 5454 | class_interface_decl = class_interface_decl->getSuperClass(); |
| 5455 | else |
| 5456 | break; |
| 5457 | } |
| 5458 | return false; |
| 5459 | } |
| 5460 | |
Adrian Prantl | 2f1fa7a | 2019-01-15 21:04:19 +0000 | [diff] [blame] | 5461 | static Optional<SymbolFile::ArrayInfo> |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 5462 | GetDynamicArrayInfo(ClangASTContext &ast, SymbolFile *sym_file, |
| 5463 | clang::QualType qual_type, |
| 5464 | const ExecutionContext *exe_ctx) { |
| 5465 | if (qual_type->isIncompleteArrayType()) |
| 5466 | if (auto *metadata = ast.GetMetadata(qual_type.getAsOpaquePtr())) |
Pavel Labath | ffec31e | 2018-12-28 13:34:44 +0000 | [diff] [blame] | 5467 | return sym_file->GetDynamicArrayInfoForUID(metadata->GetUserID(), |
| 5468 | exe_ctx); |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 5469 | return llvm::None; |
| 5470 | } |
| 5471 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5472 | uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type, |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 5473 | bool omit_empty_base_classes, |
| 5474 | const ExecutionContext *exe_ctx) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5475 | if (!type) |
| 5476 | return 0; |
| 5477 | |
| 5478 | uint32_t num_children = 0; |
| 5479 | clang::QualType qual_type(GetQualType(type)); |
| 5480 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 5481 | switch (type_class) { |
| 5482 | case clang::Type::Builtin: |
| 5483 | switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { |
| 5484 | case clang::BuiltinType::ObjCId: // child is Class |
| 5485 | case clang::BuiltinType::ObjCClass: // child is Class |
| 5486 | num_children = 1; |
| 5487 | break; |
| 5488 | |
| 5489 | default: |
| 5490 | break; |
| 5491 | } |
| 5492 | break; |
| 5493 | |
| 5494 | case clang::Type::Complex: |
| 5495 | return 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5496 | case clang::Type::Record: |
| 5497 | if (GetCompleteQualType(getASTContext(), qual_type)) { |
| 5498 | const clang::RecordType *record_type = |
| 5499 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 5500 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 5501 | assert(record_decl); |
| 5502 | const clang::CXXRecordDecl *cxx_record_decl = |
| 5503 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 5504 | if (cxx_record_decl) { |
| 5505 | if (omit_empty_base_classes) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5506 | // Check each base classes to see if it or any of its base classes |
| 5507 | // contain any fields. This can help limit the noise in variable |
| 5508 | // views by not having to show base classes that contain no members. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5509 | clang::CXXRecordDecl::base_class_const_iterator base_class, |
| 5510 | base_class_end; |
| 5511 | for (base_class = cxx_record_decl->bases_begin(), |
| 5512 | base_class_end = cxx_record_decl->bases_end(); |
| 5513 | base_class != base_class_end; ++base_class) { |
| 5514 | const clang::CXXRecordDecl *base_class_decl = |
| 5515 | llvm::cast<clang::CXXRecordDecl>( |
| 5516 | base_class->getType() |
| 5517 | ->getAs<clang::RecordType>() |
| 5518 | ->getDecl()); |
| 5519 | |
| 5520 | // Skip empty base classes |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 5521 | if (!ClangASTContext::RecordHasFields(base_class_decl)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5522 | continue; |
| 5523 | |
| 5524 | num_children++; |
| 5525 | } |
| 5526 | } else { |
| 5527 | // Include all base classes |
| 5528 | num_children += cxx_record_decl->getNumBases(); |
| 5529 | } |
| 5530 | } |
| 5531 | clang::RecordDecl::field_iterator field, field_end; |
| 5532 | for (field = record_decl->field_begin(), |
| 5533 | field_end = record_decl->field_end(); |
| 5534 | field != field_end; ++field) |
| 5535 | ++num_children; |
| 5536 | } |
| 5537 | break; |
| 5538 | |
| 5539 | case clang::Type::ObjCObject: |
| 5540 | case clang::Type::ObjCInterface: |
| 5541 | if (GetCompleteQualType(getASTContext(), qual_type)) { |
| 5542 | const clang::ObjCObjectType *objc_class_type = |
| 5543 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 5544 | assert(objc_class_type); |
| 5545 | if (objc_class_type) { |
| 5546 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 5547 | objc_class_type->getInterface(); |
| 5548 | |
| 5549 | if (class_interface_decl) { |
| 5550 | |
| 5551 | clang::ObjCInterfaceDecl *superclass_interface_decl = |
| 5552 | class_interface_decl->getSuperClass(); |
| 5553 | if (superclass_interface_decl) { |
| 5554 | if (omit_empty_base_classes) { |
| 5555 | if (ObjCDeclHasIVars(superclass_interface_decl, true)) |
| 5556 | ++num_children; |
| 5557 | } else |
| 5558 | ++num_children; |
| 5559 | } |
| 5560 | |
| 5561 | num_children += class_interface_decl->ivar_size(); |
| 5562 | } |
| 5563 | } |
| 5564 | } |
| 5565 | break; |
| 5566 | |
| 5567 | case clang::Type::ObjCObjectPointer: { |
| 5568 | const clang::ObjCObjectPointerType *pointer_type = |
| 5569 | llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr()); |
| 5570 | clang::QualType pointee_type = pointer_type->getPointeeType(); |
| 5571 | uint32_t num_pointee_children = |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5572 | CompilerType(this, pointee_type.getAsOpaquePtr()) |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 5573 | .GetNumChildren(omit_empty_base_classes, exe_ctx); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5574 | // If this type points to a simple type, then it has 1 child |
| 5575 | if (num_pointee_children == 0) |
| 5576 | num_children = 1; |
| 5577 | else |
| 5578 | num_children = num_pointee_children; |
| 5579 | } break; |
| 5580 | |
| 5581 | case clang::Type::Vector: |
| 5582 | case clang::Type::ExtVector: |
| 5583 | num_children = |
| 5584 | llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements(); |
| 5585 | break; |
| 5586 | |
| 5587 | case clang::Type::ConstantArray: |
| 5588 | num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr()) |
| 5589 | ->getSize() |
| 5590 | .getLimitedValue(); |
| 5591 | break; |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 5592 | case clang::Type::IncompleteArray: |
| 5593 | if (auto array_info = |
| 5594 | GetDynamicArrayInfo(*this, GetSymbolFile(), qual_type, exe_ctx)) |
| 5595 | // Only 1-dimensional arrays are supported. |
| 5596 | num_children = array_info->element_orders.size() |
| 5597 | ? array_info->element_orders.back() |
| 5598 | : 0; |
| 5599 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5600 | |
| 5601 | case clang::Type::Pointer: { |
| 5602 | const clang::PointerType *pointer_type = |
| 5603 | llvm::cast<clang::PointerType>(qual_type.getTypePtr()); |
| 5604 | clang::QualType pointee_type(pointer_type->getPointeeType()); |
| 5605 | uint32_t num_pointee_children = |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5606 | CompilerType(this, pointee_type.getAsOpaquePtr()) |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 5607 | .GetNumChildren(omit_empty_base_classes, exe_ctx); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5608 | if (num_pointee_children == 0) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 5609 | // We have a pointer to a pointee type that claims it has no children. We |
| 5610 | // will want to look at |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5611 | num_children = GetNumPointeeChildren(pointee_type); |
| 5612 | } else |
| 5613 | num_children = num_pointee_children; |
| 5614 | } break; |
| 5615 | |
| 5616 | case clang::Type::LValueReference: |
| 5617 | case clang::Type::RValueReference: { |
| 5618 | const clang::ReferenceType *reference_type = |
| 5619 | llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()); |
| 5620 | clang::QualType pointee_type = reference_type->getPointeeType(); |
| 5621 | uint32_t num_pointee_children = |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5622 | CompilerType(this, pointee_type.getAsOpaquePtr()) |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 5623 | .GetNumChildren(omit_empty_base_classes, exe_ctx); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5624 | // If this type points to a simple type, then it has 1 child |
| 5625 | if (num_pointee_children == 0) |
| 5626 | num_children = 1; |
| 5627 | else |
| 5628 | num_children = num_pointee_children; |
| 5629 | } break; |
| 5630 | |
| 5631 | case clang::Type::Typedef: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5632 | num_children = CompilerType(this, llvm::cast<clang::TypedefType>(qual_type) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5633 | ->getDecl() |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5634 | ->getUnderlyingType() |
| 5635 | .getAsOpaquePtr()) |
| 5636 | .GetNumChildren(omit_empty_base_classes, exe_ctx); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5637 | break; |
| 5638 | |
| 5639 | case clang::Type::Auto: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5640 | num_children = CompilerType(this, llvm::cast<clang::AutoType>(qual_type) |
| 5641 | ->getDeducedType() |
| 5642 | .getAsOpaquePtr()) |
| 5643 | .GetNumChildren(omit_empty_base_classes, exe_ctx); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5644 | break; |
| 5645 | |
| 5646 | case clang::Type::Elaborated: |
| 5647 | num_children = |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5648 | CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type) |
| 5649 | ->getNamedType() |
| 5650 | .getAsOpaquePtr()) |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 5651 | .GetNumChildren(omit_empty_base_classes, exe_ctx); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5652 | break; |
| 5653 | |
| 5654 | case clang::Type::Paren: |
| 5655 | num_children = |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5656 | CompilerType( |
| 5657 | this, |
| 5658 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()) |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 5659 | .GetNumChildren(omit_empty_base_classes, exe_ctx); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5660 | break; |
| 5661 | default: |
| 5662 | break; |
| 5663 | } |
| 5664 | return num_children; |
| 5665 | } |
| 5666 | |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 5667 | CompilerType ClangASTContext::GetBuiltinTypeByName(ConstString name) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5668 | return GetBasicType(GetBasicTypeEnumeration(name)); |
Greg Clayton | 56939cb | 2015-09-17 22:23:34 +0000 | [diff] [blame] | 5669 | } |
| 5670 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5671 | lldb::BasicType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5672 | ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) { |
| 5673 | if (type) { |
| 5674 | clang::QualType qual_type(GetQualType(type)); |
| 5675 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 5676 | if (type_class == clang::Type::Builtin) { |
| 5677 | switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { |
| 5678 | case clang::BuiltinType::Void: |
| 5679 | return eBasicTypeVoid; |
| 5680 | case clang::BuiltinType::Bool: |
| 5681 | return eBasicTypeBool; |
| 5682 | case clang::BuiltinType::Char_S: |
| 5683 | return eBasicTypeSignedChar; |
| 5684 | case clang::BuiltinType::Char_U: |
| 5685 | return eBasicTypeUnsignedChar; |
| 5686 | case clang::BuiltinType::Char16: |
| 5687 | return eBasicTypeChar16; |
| 5688 | case clang::BuiltinType::Char32: |
| 5689 | return eBasicTypeChar32; |
| 5690 | case clang::BuiltinType::UChar: |
| 5691 | return eBasicTypeUnsignedChar; |
| 5692 | case clang::BuiltinType::SChar: |
| 5693 | return eBasicTypeSignedChar; |
| 5694 | case clang::BuiltinType::WChar_S: |
| 5695 | return eBasicTypeSignedWChar; |
| 5696 | case clang::BuiltinType::WChar_U: |
| 5697 | return eBasicTypeUnsignedWChar; |
| 5698 | case clang::BuiltinType::Short: |
| 5699 | return eBasicTypeShort; |
| 5700 | case clang::BuiltinType::UShort: |
| 5701 | return eBasicTypeUnsignedShort; |
| 5702 | case clang::BuiltinType::Int: |
| 5703 | return eBasicTypeInt; |
| 5704 | case clang::BuiltinType::UInt: |
| 5705 | return eBasicTypeUnsignedInt; |
| 5706 | case clang::BuiltinType::Long: |
| 5707 | return eBasicTypeLong; |
| 5708 | case clang::BuiltinType::ULong: |
| 5709 | return eBasicTypeUnsignedLong; |
| 5710 | case clang::BuiltinType::LongLong: |
| 5711 | return eBasicTypeLongLong; |
| 5712 | case clang::BuiltinType::ULongLong: |
| 5713 | return eBasicTypeUnsignedLongLong; |
| 5714 | case clang::BuiltinType::Int128: |
| 5715 | return eBasicTypeInt128; |
| 5716 | case clang::BuiltinType::UInt128: |
| 5717 | return eBasicTypeUnsignedInt128; |
| 5718 | |
| 5719 | case clang::BuiltinType::Half: |
| 5720 | return eBasicTypeHalf; |
| 5721 | case clang::BuiltinType::Float: |
| 5722 | return eBasicTypeFloat; |
| 5723 | case clang::BuiltinType::Double: |
| 5724 | return eBasicTypeDouble; |
| 5725 | case clang::BuiltinType::LongDouble: |
| 5726 | return eBasicTypeLongDouble; |
| 5727 | |
| 5728 | case clang::BuiltinType::NullPtr: |
| 5729 | return eBasicTypeNullPtr; |
| 5730 | case clang::BuiltinType::ObjCId: |
| 5731 | return eBasicTypeObjCID; |
| 5732 | case clang::BuiltinType::ObjCClass: |
| 5733 | return eBasicTypeObjCClass; |
| 5734 | case clang::BuiltinType::ObjCSel: |
| 5735 | return eBasicTypeObjCSel; |
| 5736 | default: |
| 5737 | return eBasicTypeOther; |
| 5738 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5739 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5740 | } |
| 5741 | return eBasicTypeInvalid; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5742 | } |
| 5743 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5744 | void ClangASTContext::ForEachEnumerator( |
| 5745 | lldb::opaque_compiler_type_t type, |
| 5746 | std::function<bool(const CompilerType &integer_type, |
Adrian Prantl | 0e4c482 | 2019-03-06 21:22:25 +0000 | [diff] [blame] | 5747 | ConstString name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5748 | const llvm::APSInt &value)> const &callback) { |
| 5749 | const clang::EnumType *enum_type = |
| 5750 | llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type)); |
| 5751 | if (enum_type) { |
| 5752 | const clang::EnumDecl *enum_decl = enum_type->getDecl(); |
| 5753 | if (enum_decl) { |
| 5754 | CompilerType integer_type(this, |
| 5755 | enum_decl->getIntegerType().getAsOpaquePtr()); |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 5756 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5757 | clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos; |
| 5758 | for (enum_pos = enum_decl->enumerator_begin(), |
| 5759 | enum_end_pos = enum_decl->enumerator_end(); |
| 5760 | enum_pos != enum_end_pos; ++enum_pos) { |
| 5761 | ConstString name(enum_pos->getNameAsString().c_str()); |
| 5762 | if (!callback(integer_type, name, enum_pos->getInitVal())) |
| 5763 | break; |
| 5764 | } |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 5765 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5766 | } |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 5767 | } |
| 5768 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5769 | #pragma mark Aggregate Types |
| 5770 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5771 | uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) { |
| 5772 | if (!type) |
| 5773 | return 0; |
Enrico Granata | 36f51e4 | 2015-12-18 22:41:25 +0000 | [diff] [blame] | 5774 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5775 | uint32_t count = 0; |
| 5776 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 5777 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 5778 | switch (type_class) { |
| 5779 | case clang::Type::Record: |
| 5780 | if (GetCompleteType(type)) { |
| 5781 | const clang::RecordType *record_type = |
| 5782 | llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr()); |
| 5783 | if (record_type) { |
| 5784 | clang::RecordDecl *record_decl = record_type->getDecl(); |
| 5785 | if (record_decl) { |
| 5786 | uint32_t field_idx = 0; |
| 5787 | clang::RecordDecl::field_iterator field, field_end; |
| 5788 | for (field = record_decl->field_begin(), |
| 5789 | field_end = record_decl->field_end(); |
| 5790 | field != field_end; ++field) |
| 5791 | ++field_idx; |
| 5792 | count = field_idx; |
| 5793 | } |
| 5794 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5795 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5796 | break; |
| 5797 | |
| 5798 | case clang::Type::Typedef: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5799 | count = CompilerType(this, llvm::cast<clang::TypedefType>(qual_type) |
| 5800 | ->getDecl() |
| 5801 | ->getUnderlyingType() |
| 5802 | .getAsOpaquePtr()) |
| 5803 | .GetNumFields(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5804 | break; |
| 5805 | |
| 5806 | case clang::Type::Auto: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5807 | count = CompilerType(this, llvm::cast<clang::AutoType>(qual_type) |
| 5808 | ->getDeducedType() |
| 5809 | .getAsOpaquePtr()) |
| 5810 | .GetNumFields(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5811 | break; |
| 5812 | |
| 5813 | case clang::Type::Elaborated: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5814 | count = CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type) |
| 5815 | ->getNamedType() |
| 5816 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5817 | .GetNumFields(); |
| 5818 | break; |
| 5819 | |
| 5820 | case clang::Type::Paren: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5821 | count = |
| 5822 | CompilerType( |
| 5823 | this, |
| 5824 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()) |
| 5825 | .GetNumFields(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5826 | break; |
| 5827 | |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 5828 | case clang::Type::ObjCObjectPointer: { |
| 5829 | const clang::ObjCObjectPointerType *objc_class_type = |
Sean Callanan | 732a6f4 | 2017-05-15 19:55:20 +0000 | [diff] [blame] | 5830 | qual_type->getAs<clang::ObjCObjectPointerType>(); |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 5831 | const clang::ObjCInterfaceType *objc_interface_type = |
| 5832 | objc_class_type->getInterfaceType(); |
| 5833 | if (objc_interface_type && |
Davide Italiano | 52ffb53 | 2017-04-17 18:24:18 +0000 | [diff] [blame] | 5834 | GetCompleteType(static_cast<lldb::opaque_compiler_type_t>( |
| 5835 | const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) { |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 5836 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 5837 | objc_interface_type->getDecl(); |
| 5838 | if (class_interface_decl) { |
| 5839 | count = class_interface_decl->ivar_size(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5840 | } |
| 5841 | } |
| 5842 | break; |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 5843 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5844 | |
| 5845 | case clang::Type::ObjCObject: |
| 5846 | case clang::Type::ObjCInterface: |
| 5847 | if (GetCompleteType(type)) { |
| 5848 | const clang::ObjCObjectType *objc_class_type = |
| 5849 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 5850 | if (objc_class_type) { |
| 5851 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 5852 | objc_class_type->getInterface(); |
| 5853 | |
| 5854 | if (class_interface_decl) |
| 5855 | count = class_interface_decl->ivar_size(); |
| 5856 | } |
| 5857 | } |
| 5858 | break; |
| 5859 | |
| 5860 | default: |
| 5861 | break; |
| 5862 | } |
| 5863 | return count; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5864 | } |
| 5865 | |
Bruce Mitchener | 48ea900 | 2015-09-23 00:18:24 +0000 | [diff] [blame] | 5866 | static lldb::opaque_compiler_type_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5867 | GetObjCFieldAtIndex(clang::ASTContext *ast, |
| 5868 | clang::ObjCInterfaceDecl *class_interface_decl, size_t idx, |
| 5869 | std::string &name, uint64_t *bit_offset_ptr, |
| 5870 | uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) { |
| 5871 | if (class_interface_decl) { |
| 5872 | if (idx < (class_interface_decl->ivar_size())) { |
| 5873 | clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, |
| 5874 | ivar_end = class_interface_decl->ivar_end(); |
| 5875 | uint32_t ivar_idx = 0; |
| 5876 | |
| 5877 | for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; |
| 5878 | ++ivar_pos, ++ivar_idx) { |
| 5879 | if (ivar_idx == idx) { |
| 5880 | const clang::ObjCIvarDecl *ivar_decl = *ivar_pos; |
| 5881 | |
| 5882 | clang::QualType ivar_qual_type(ivar_decl->getType()); |
| 5883 | |
| 5884 | name.assign(ivar_decl->getNameAsString()); |
| 5885 | |
| 5886 | if (bit_offset_ptr) { |
| 5887 | const clang::ASTRecordLayout &interface_layout = |
| 5888 | ast->getASTObjCInterfaceLayout(class_interface_decl); |
| 5889 | *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx); |
| 5890 | } |
| 5891 | |
| 5892 | const bool is_bitfield = ivar_pos->isBitField(); |
| 5893 | |
| 5894 | if (bitfield_bit_size_ptr) { |
| 5895 | *bitfield_bit_size_ptr = 0; |
| 5896 | |
| 5897 | if (is_bitfield && ast) { |
| 5898 | clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth(); |
Hans Wennborg | 30ce962 | 2018-11-28 14:30:18 +0000 | [diff] [blame] | 5899 | clang::Expr::EvalResult result; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5900 | if (bitfield_bit_size_expr && |
Hans Wennborg | 30ce962 | 2018-11-28 14:30:18 +0000 | [diff] [blame] | 5901 | bitfield_bit_size_expr->EvaluateAsInt(result, *ast)) { |
| 5902 | llvm::APSInt bitfield_apsint = result.Val.getInt(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5903 | *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue(); |
| 5904 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5905 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5906 | } |
| 5907 | if (is_bitfield_ptr) |
| 5908 | *is_bitfield_ptr = is_bitfield; |
| 5909 | |
| 5910 | return ivar_qual_type.getAsOpaquePtr(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5911 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5912 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5913 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5914 | } |
| 5915 | return nullptr; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 5916 | } |
| 5917 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5918 | CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type, |
| 5919 | size_t idx, std::string &name, |
| 5920 | uint64_t *bit_offset_ptr, |
| 5921 | uint32_t *bitfield_bit_size_ptr, |
| 5922 | bool *is_bitfield_ptr) { |
| 5923 | if (!type) |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 5924 | return CompilerType(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5925 | |
| 5926 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 5927 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 5928 | switch (type_class) { |
| 5929 | case clang::Type::Record: |
| 5930 | if (GetCompleteType(type)) { |
| 5931 | const clang::RecordType *record_type = |
| 5932 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 5933 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 5934 | uint32_t field_idx = 0; |
| 5935 | clang::RecordDecl::field_iterator field, field_end; |
| 5936 | for (field = record_decl->field_begin(), |
| 5937 | field_end = record_decl->field_end(); |
| 5938 | field != field_end; ++field, ++field_idx) { |
| 5939 | if (idx == field_idx) { |
| 5940 | // Print the member type if requested |
| 5941 | // Print the member name and equal sign |
| 5942 | name.assign(field->getNameAsString()); |
| 5943 | |
| 5944 | // Figure out the type byte size (field_type_info.first) and |
| 5945 | // alignment (field_type_info.second) from the AST context. |
| 5946 | if (bit_offset_ptr) { |
| 5947 | const clang::ASTRecordLayout &record_layout = |
| 5948 | getASTContext()->getASTRecordLayout(record_decl); |
| 5949 | *bit_offset_ptr = record_layout.getFieldOffset(field_idx); |
| 5950 | } |
| 5951 | |
| 5952 | const bool is_bitfield = field->isBitField(); |
| 5953 | |
| 5954 | if (bitfield_bit_size_ptr) { |
| 5955 | *bitfield_bit_size_ptr = 0; |
| 5956 | |
| 5957 | if (is_bitfield) { |
| 5958 | clang::Expr *bitfield_bit_size_expr = field->getBitWidth(); |
Hans Wennborg | 30ce962 | 2018-11-28 14:30:18 +0000 | [diff] [blame] | 5959 | clang::Expr::EvalResult result; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5960 | if (bitfield_bit_size_expr && |
Hans Wennborg | 30ce962 | 2018-11-28 14:30:18 +0000 | [diff] [blame] | 5961 | bitfield_bit_size_expr->EvaluateAsInt(result, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5962 | *getASTContext())) { |
Hans Wennborg | 30ce962 | 2018-11-28 14:30:18 +0000 | [diff] [blame] | 5963 | llvm::APSInt bitfield_apsint = result.Val.getInt(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5964 | *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue(); |
| 5965 | } |
| 5966 | } |
| 5967 | } |
| 5968 | if (is_bitfield_ptr) |
| 5969 | *is_bitfield_ptr = is_bitfield; |
| 5970 | |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 5971 | return CompilerType(this, field->getType().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5972 | } |
| 5973 | } |
| 5974 | } |
| 5975 | break; |
| 5976 | |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 5977 | case clang::Type::ObjCObjectPointer: { |
| 5978 | const clang::ObjCObjectPointerType *objc_class_type = |
Sean Callanan | 732a6f4 | 2017-05-15 19:55:20 +0000 | [diff] [blame] | 5979 | qual_type->getAs<clang::ObjCObjectPointerType>(); |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 5980 | const clang::ObjCInterfaceType *objc_interface_type = |
| 5981 | objc_class_type->getInterfaceType(); |
| 5982 | if (objc_interface_type && |
Davide Italiano | 52ffb53 | 2017-04-17 18:24:18 +0000 | [diff] [blame] | 5983 | GetCompleteType(static_cast<lldb::opaque_compiler_type_t>( |
| 5984 | const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) { |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 5985 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 5986 | objc_interface_type->getDecl(); |
| 5987 | if (class_interface_decl) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5988 | return CompilerType( |
| 5989 | this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl, |
| 5990 | idx, name, bit_offset_ptr, |
| 5991 | bitfield_bit_size_ptr, is_bitfield_ptr)); |
| 5992 | } |
| 5993 | } |
| 5994 | break; |
Sean Callanan | f9c622a | 2016-09-30 18:44:43 +0000 | [diff] [blame] | 5995 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 5996 | |
| 5997 | case clang::Type::ObjCObject: |
| 5998 | case clang::Type::ObjCInterface: |
| 5999 | if (GetCompleteType(type)) { |
| 6000 | const clang::ObjCObjectType *objc_class_type = |
| 6001 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 6002 | assert(objc_class_type); |
| 6003 | if (objc_class_type) { |
| 6004 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 6005 | objc_class_type->getInterface(); |
| 6006 | return CompilerType( |
| 6007 | this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl, |
| 6008 | idx, name, bit_offset_ptr, |
| 6009 | bitfield_bit_size_ptr, is_bitfield_ptr)); |
| 6010 | } |
| 6011 | } |
| 6012 | break; |
| 6013 | |
| 6014 | case clang::Type::Typedef: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 6015 | return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type) |
| 6016 | ->getDecl() |
| 6017 | ->getUnderlyingType() |
| 6018 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6019 | .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr, |
| 6020 | is_bitfield_ptr); |
| 6021 | |
| 6022 | case clang::Type::Auto: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 6023 | return CompilerType(this, llvm::cast<clang::AutoType>(qual_type) |
| 6024 | ->getDeducedType() |
| 6025 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6026 | .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr, |
| 6027 | is_bitfield_ptr); |
| 6028 | |
| 6029 | case clang::Type::Elaborated: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 6030 | return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type) |
| 6031 | ->getNamedType() |
| 6032 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6033 | .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr, |
| 6034 | is_bitfield_ptr); |
| 6035 | |
| 6036 | case clang::Type::Paren: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 6037 | return CompilerType(this, llvm::cast<clang::ParenType>(qual_type) |
| 6038 | ->desugar() |
| 6039 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6040 | .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr, |
| 6041 | is_bitfield_ptr); |
| 6042 | |
| 6043 | default: |
| 6044 | break; |
| 6045 | } |
| 6046 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 6047 | } |
| 6048 | |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6049 | uint32_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6050 | ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) { |
| 6051 | uint32_t count = 0; |
| 6052 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 6053 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 6054 | switch (type_class) { |
| 6055 | case clang::Type::Record: |
| 6056 | if (GetCompleteType(type)) { |
| 6057 | const clang::CXXRecordDecl *cxx_record_decl = |
| 6058 | qual_type->getAsCXXRecordDecl(); |
| 6059 | if (cxx_record_decl) |
| 6060 | count = cxx_record_decl->getNumBases(); |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6061 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6062 | break; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6063 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6064 | case clang::Type::ObjCObjectPointer: |
| 6065 | count = GetPointeeType(type).GetNumDirectBaseClasses(); |
| 6066 | break; |
| 6067 | |
| 6068 | case clang::Type::ObjCObject: |
| 6069 | if (GetCompleteType(type)) { |
| 6070 | const clang::ObjCObjectType *objc_class_type = |
| 6071 | qual_type->getAsObjCQualifiedInterfaceType(); |
| 6072 | if (objc_class_type) { |
| 6073 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 6074 | objc_class_type->getInterface(); |
| 6075 | |
| 6076 | if (class_interface_decl && class_interface_decl->getSuperClass()) |
| 6077 | count = 1; |
| 6078 | } |
| 6079 | } |
| 6080 | break; |
| 6081 | case clang::Type::ObjCInterface: |
| 6082 | if (GetCompleteType(type)) { |
| 6083 | const clang::ObjCInterfaceType *objc_interface_type = |
| 6084 | qual_type->getAs<clang::ObjCInterfaceType>(); |
| 6085 | if (objc_interface_type) { |
| 6086 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 6087 | objc_interface_type->getInterface(); |
| 6088 | |
| 6089 | if (class_interface_decl && class_interface_decl->getSuperClass()) |
| 6090 | count = 1; |
| 6091 | } |
| 6092 | } |
| 6093 | break; |
| 6094 | |
| 6095 | case clang::Type::Typedef: |
| 6096 | count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type) |
| 6097 | ->getDecl() |
| 6098 | ->getUnderlyingType() |
| 6099 | .getAsOpaquePtr()); |
| 6100 | break; |
| 6101 | |
| 6102 | case clang::Type::Auto: |
| 6103 | count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type) |
| 6104 | ->getDeducedType() |
| 6105 | .getAsOpaquePtr()); |
| 6106 | break; |
| 6107 | |
| 6108 | case clang::Type::Elaborated: |
| 6109 | count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type) |
| 6110 | ->getNamedType() |
| 6111 | .getAsOpaquePtr()); |
| 6112 | break; |
| 6113 | |
| 6114 | case clang::Type::Paren: |
| 6115 | return GetNumDirectBaseClasses( |
| 6116 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()); |
| 6117 | |
| 6118 | default: |
| 6119 | break; |
| 6120 | } |
| 6121 | return count; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6122 | } |
| 6123 | |
| 6124 | uint32_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6125 | ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) { |
| 6126 | uint32_t count = 0; |
| 6127 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 6128 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 6129 | switch (type_class) { |
| 6130 | case clang::Type::Record: |
| 6131 | if (GetCompleteType(type)) { |
| 6132 | const clang::CXXRecordDecl *cxx_record_decl = |
| 6133 | qual_type->getAsCXXRecordDecl(); |
| 6134 | if (cxx_record_decl) |
| 6135 | count = cxx_record_decl->getNumVBases(); |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6136 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6137 | break; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6138 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6139 | case clang::Type::Typedef: |
| 6140 | count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type) |
| 6141 | ->getDecl() |
| 6142 | ->getUnderlyingType() |
| 6143 | .getAsOpaquePtr()); |
| 6144 | break; |
| 6145 | |
| 6146 | case clang::Type::Auto: |
| 6147 | count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type) |
| 6148 | ->getDeducedType() |
| 6149 | .getAsOpaquePtr()); |
| 6150 | break; |
| 6151 | |
| 6152 | case clang::Type::Elaborated: |
| 6153 | count = |
| 6154 | GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type) |
| 6155 | ->getNamedType() |
| 6156 | .getAsOpaquePtr()); |
| 6157 | break; |
| 6158 | |
| 6159 | case clang::Type::Paren: |
| 6160 | count = GetNumVirtualBaseClasses( |
| 6161 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()); |
| 6162 | break; |
| 6163 | |
| 6164 | default: |
| 6165 | break; |
| 6166 | } |
| 6167 | return count; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6168 | } |
| 6169 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6170 | CompilerType ClangASTContext::GetDirectBaseClassAtIndex( |
| 6171 | lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) { |
| 6172 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 6173 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 6174 | switch (type_class) { |
| 6175 | case clang::Type::Record: |
| 6176 | if (GetCompleteType(type)) { |
| 6177 | const clang::CXXRecordDecl *cxx_record_decl = |
| 6178 | qual_type->getAsCXXRecordDecl(); |
| 6179 | if (cxx_record_decl) { |
| 6180 | uint32_t curr_idx = 0; |
| 6181 | clang::CXXRecordDecl::base_class_const_iterator base_class, |
| 6182 | base_class_end; |
| 6183 | for (base_class = cxx_record_decl->bases_begin(), |
| 6184 | base_class_end = cxx_record_decl->bases_end(); |
| 6185 | base_class != base_class_end; ++base_class, ++curr_idx) { |
| 6186 | if (curr_idx == idx) { |
| 6187 | if (bit_offset_ptr) { |
| 6188 | const clang::ASTRecordLayout &record_layout = |
| 6189 | getASTContext()->getASTRecordLayout(cxx_record_decl); |
| 6190 | const clang::CXXRecordDecl *base_class_decl = |
| 6191 | llvm::cast<clang::CXXRecordDecl>( |
| 6192 | base_class->getType() |
| 6193 | ->getAs<clang::RecordType>() |
| 6194 | ->getDecl()); |
| 6195 | if (base_class->isVirtual()) |
| 6196 | *bit_offset_ptr = |
| 6197 | record_layout.getVBaseClassOffset(base_class_decl) |
| 6198 | .getQuantity() * |
| 6199 | 8; |
| 6200 | else |
| 6201 | *bit_offset_ptr = |
| 6202 | record_layout.getBaseClassOffset(base_class_decl) |
| 6203 | .getQuantity() * |
| 6204 | 8; |
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 | return CompilerType(this, base_class->getType().getAsOpaquePtr()); |
| 6207 | } |
| 6208 | } |
| 6209 | } |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6210 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6211 | break; |
| 6212 | |
| 6213 | case clang::Type::ObjCObjectPointer: |
| 6214 | return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr); |
| 6215 | |
| 6216 | case clang::Type::ObjCObject: |
| 6217 | if (idx == 0 && GetCompleteType(type)) { |
| 6218 | const clang::ObjCObjectType *objc_class_type = |
| 6219 | qual_type->getAsObjCQualifiedInterfaceType(); |
| 6220 | if (objc_class_type) { |
| 6221 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 6222 | objc_class_type->getInterface(); |
| 6223 | |
| 6224 | if (class_interface_decl) { |
| 6225 | clang::ObjCInterfaceDecl *superclass_interface_decl = |
| 6226 | class_interface_decl->getSuperClass(); |
| 6227 | if (superclass_interface_decl) { |
| 6228 | if (bit_offset_ptr) |
| 6229 | *bit_offset_ptr = 0; |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 6230 | return CompilerType(this, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6231 | getASTContext()->getObjCInterfaceType( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 6232 | superclass_interface_decl).getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6233 | } |
| 6234 | } |
| 6235 | } |
| 6236 | } |
| 6237 | break; |
| 6238 | case clang::Type::ObjCInterface: |
| 6239 | if (idx == 0 && GetCompleteType(type)) { |
| 6240 | const clang::ObjCObjectType *objc_interface_type = |
| 6241 | qual_type->getAs<clang::ObjCInterfaceType>(); |
| 6242 | if (objc_interface_type) { |
| 6243 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 6244 | objc_interface_type->getInterface(); |
| 6245 | |
| 6246 | if (class_interface_decl) { |
| 6247 | clang::ObjCInterfaceDecl *superclass_interface_decl = |
| 6248 | class_interface_decl->getSuperClass(); |
| 6249 | if (superclass_interface_decl) { |
| 6250 | if (bit_offset_ptr) |
| 6251 | *bit_offset_ptr = 0; |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 6252 | return CompilerType( |
| 6253 | this, getASTContext() |
| 6254 | ->getObjCInterfaceType(superclass_interface_decl) |
| 6255 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6256 | } |
| 6257 | } |
| 6258 | } |
| 6259 | } |
| 6260 | break; |
| 6261 | |
| 6262 | case clang::Type::Typedef: |
| 6263 | return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type) |
| 6264 | ->getDecl() |
| 6265 | ->getUnderlyingType() |
| 6266 | .getAsOpaquePtr(), |
| 6267 | idx, bit_offset_ptr); |
| 6268 | |
| 6269 | case clang::Type::Auto: |
| 6270 | return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type) |
| 6271 | ->getDeducedType() |
| 6272 | .getAsOpaquePtr(), |
| 6273 | idx, bit_offset_ptr); |
| 6274 | |
| 6275 | case clang::Type::Elaborated: |
| 6276 | return GetDirectBaseClassAtIndex( |
| 6277 | llvm::cast<clang::ElaboratedType>(qual_type) |
| 6278 | ->getNamedType() |
| 6279 | .getAsOpaquePtr(), |
| 6280 | idx, bit_offset_ptr); |
| 6281 | |
| 6282 | case clang::Type::Paren: |
| 6283 | return GetDirectBaseClassAtIndex( |
| 6284 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 6285 | idx, bit_offset_ptr); |
| 6286 | |
| 6287 | default: |
| 6288 | break; |
| 6289 | } |
| 6290 | return CompilerType(); |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6291 | } |
| 6292 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6293 | CompilerType ClangASTContext::GetVirtualBaseClassAtIndex( |
| 6294 | lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) { |
| 6295 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 6296 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 6297 | switch (type_class) { |
| 6298 | case clang::Type::Record: |
| 6299 | if (GetCompleteType(type)) { |
| 6300 | const clang::CXXRecordDecl *cxx_record_decl = |
| 6301 | qual_type->getAsCXXRecordDecl(); |
| 6302 | if (cxx_record_decl) { |
| 6303 | uint32_t curr_idx = 0; |
| 6304 | clang::CXXRecordDecl::base_class_const_iterator base_class, |
| 6305 | base_class_end; |
| 6306 | for (base_class = cxx_record_decl->vbases_begin(), |
| 6307 | base_class_end = cxx_record_decl->vbases_end(); |
| 6308 | base_class != base_class_end; ++base_class, ++curr_idx) { |
| 6309 | if (curr_idx == idx) { |
| 6310 | if (bit_offset_ptr) { |
| 6311 | const clang::ASTRecordLayout &record_layout = |
| 6312 | getASTContext()->getASTRecordLayout(cxx_record_decl); |
| 6313 | const clang::CXXRecordDecl *base_class_decl = |
| 6314 | llvm::cast<clang::CXXRecordDecl>( |
| 6315 | base_class->getType() |
| 6316 | ->getAs<clang::RecordType>() |
| 6317 | ->getDecl()); |
| 6318 | *bit_offset_ptr = |
| 6319 | record_layout.getVBaseClassOffset(base_class_decl) |
| 6320 | .getQuantity() * |
| 6321 | 8; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6322 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6323 | return CompilerType(this, base_class->getType().getAsOpaquePtr()); |
| 6324 | } |
| 6325 | } |
| 6326 | } |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6327 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6328 | break; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6329 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6330 | case clang::Type::Typedef: |
| 6331 | return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type) |
| 6332 | ->getDecl() |
| 6333 | ->getUnderlyingType() |
| 6334 | .getAsOpaquePtr(), |
| 6335 | idx, bit_offset_ptr); |
| 6336 | |
| 6337 | case clang::Type::Auto: |
| 6338 | return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type) |
| 6339 | ->getDeducedType() |
| 6340 | .getAsOpaquePtr(), |
| 6341 | idx, bit_offset_ptr); |
| 6342 | |
| 6343 | case clang::Type::Elaborated: |
| 6344 | return GetVirtualBaseClassAtIndex( |
| 6345 | llvm::cast<clang::ElaboratedType>(qual_type) |
| 6346 | ->getNamedType() |
| 6347 | .getAsOpaquePtr(), |
| 6348 | idx, bit_offset_ptr); |
| 6349 | |
| 6350 | case clang::Type::Paren: |
| 6351 | return GetVirtualBaseClassAtIndex( |
| 6352 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 6353 | idx, bit_offset_ptr); |
| 6354 | |
| 6355 | default: |
| 6356 | break; |
| 6357 | } |
| 6358 | return CompilerType(); |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 6359 | } |
| 6360 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 6361 | // If a pointer to a pointee type (the clang_type arg) says that it has no |
| 6362 | // children, then we either need to trust it, or override it and return a |
| 6363 | // different result. For example, an "int *" has one child that is an integer, |
| 6364 | // but a function pointer doesn't have any children. Likewise if a Record type |
| 6365 | // claims it has no children, then there really is nothing to show. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6366 | uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) { |
| 6367 | if (type.isNull()) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 6368 | return 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6369 | |
| 6370 | clang::QualType qual_type(type.getCanonicalType()); |
| 6371 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 6372 | switch (type_class) { |
| 6373 | case clang::Type::Builtin: |
| 6374 | switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) { |
| 6375 | case clang::BuiltinType::UnknownAny: |
| 6376 | case clang::BuiltinType::Void: |
| 6377 | case clang::BuiltinType::NullPtr: |
| 6378 | case clang::BuiltinType::OCLEvent: |
| 6379 | case clang::BuiltinType::OCLImage1dRO: |
| 6380 | case clang::BuiltinType::OCLImage1dWO: |
| 6381 | case clang::BuiltinType::OCLImage1dRW: |
| 6382 | case clang::BuiltinType::OCLImage1dArrayRO: |
| 6383 | case clang::BuiltinType::OCLImage1dArrayWO: |
| 6384 | case clang::BuiltinType::OCLImage1dArrayRW: |
| 6385 | case clang::BuiltinType::OCLImage1dBufferRO: |
| 6386 | case clang::BuiltinType::OCLImage1dBufferWO: |
| 6387 | case clang::BuiltinType::OCLImage1dBufferRW: |
| 6388 | case clang::BuiltinType::OCLImage2dRO: |
| 6389 | case clang::BuiltinType::OCLImage2dWO: |
| 6390 | case clang::BuiltinType::OCLImage2dRW: |
| 6391 | case clang::BuiltinType::OCLImage2dArrayRO: |
| 6392 | case clang::BuiltinType::OCLImage2dArrayWO: |
| 6393 | case clang::BuiltinType::OCLImage2dArrayRW: |
| 6394 | case clang::BuiltinType::OCLImage3dRO: |
| 6395 | case clang::BuiltinType::OCLImage3dWO: |
| 6396 | case clang::BuiltinType::OCLImage3dRW: |
| 6397 | case clang::BuiltinType::OCLSampler: |
| 6398 | return 0; |
| 6399 | case clang::BuiltinType::Bool: |
| 6400 | case clang::BuiltinType::Char_U: |
| 6401 | case clang::BuiltinType::UChar: |
| 6402 | case clang::BuiltinType::WChar_U: |
| 6403 | case clang::BuiltinType::Char16: |
| 6404 | case clang::BuiltinType::Char32: |
| 6405 | case clang::BuiltinType::UShort: |
| 6406 | case clang::BuiltinType::UInt: |
| 6407 | case clang::BuiltinType::ULong: |
| 6408 | case clang::BuiltinType::ULongLong: |
| 6409 | case clang::BuiltinType::UInt128: |
| 6410 | case clang::BuiltinType::Char_S: |
| 6411 | case clang::BuiltinType::SChar: |
| 6412 | case clang::BuiltinType::WChar_S: |
| 6413 | case clang::BuiltinType::Short: |
| 6414 | case clang::BuiltinType::Int: |
| 6415 | case clang::BuiltinType::Long: |
| 6416 | case clang::BuiltinType::LongLong: |
| 6417 | case clang::BuiltinType::Int128: |
| 6418 | case clang::BuiltinType::Float: |
| 6419 | case clang::BuiltinType::Double: |
| 6420 | case clang::BuiltinType::LongDouble: |
| 6421 | case clang::BuiltinType::Dependent: |
| 6422 | case clang::BuiltinType::Overload: |
| 6423 | case clang::BuiltinType::ObjCId: |
| 6424 | case clang::BuiltinType::ObjCClass: |
| 6425 | case clang::BuiltinType::ObjCSel: |
| 6426 | case clang::BuiltinType::BoundMember: |
| 6427 | case clang::BuiltinType::Half: |
| 6428 | case clang::BuiltinType::ARCUnbridgedCast: |
| 6429 | case clang::BuiltinType::PseudoObject: |
| 6430 | case clang::BuiltinType::BuiltinFn: |
| 6431 | case clang::BuiltinType::OMPArraySection: |
| 6432 | return 1; |
| 6433 | default: |
| 6434 | return 0; |
| 6435 | } |
| 6436 | break; |
| 6437 | |
| 6438 | case clang::Type::Complex: |
| 6439 | return 1; |
| 6440 | case clang::Type::Pointer: |
| 6441 | return 1; |
| 6442 | case clang::Type::BlockPointer: |
| 6443 | return 0; // If block pointers don't have debug info, then no children for |
| 6444 | // them |
| 6445 | case clang::Type::LValueReference: |
| 6446 | return 1; |
| 6447 | case clang::Type::RValueReference: |
| 6448 | return 1; |
| 6449 | case clang::Type::MemberPointer: |
| 6450 | return 0; |
| 6451 | case clang::Type::ConstantArray: |
| 6452 | return 0; |
| 6453 | case clang::Type::IncompleteArray: |
| 6454 | return 0; |
| 6455 | case clang::Type::VariableArray: |
| 6456 | return 0; |
| 6457 | case clang::Type::DependentSizedArray: |
| 6458 | return 0; |
| 6459 | case clang::Type::DependentSizedExtVector: |
| 6460 | return 0; |
| 6461 | case clang::Type::Vector: |
| 6462 | return 0; |
| 6463 | case clang::Type::ExtVector: |
| 6464 | return 0; |
| 6465 | case clang::Type::FunctionProto: |
| 6466 | return 0; // When we function pointers, they have no children... |
| 6467 | case clang::Type::FunctionNoProto: |
| 6468 | return 0; // When we function pointers, they have no children... |
| 6469 | case clang::Type::UnresolvedUsing: |
| 6470 | return 0; |
| 6471 | case clang::Type::Paren: |
| 6472 | return GetNumPointeeChildren( |
| 6473 | llvm::cast<clang::ParenType>(qual_type)->desugar()); |
| 6474 | case clang::Type::Typedef: |
| 6475 | return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type) |
| 6476 | ->getDecl() |
| 6477 | ->getUnderlyingType()); |
| 6478 | case clang::Type::Auto: |
| 6479 | return GetNumPointeeChildren( |
| 6480 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType()); |
| 6481 | case clang::Type::Elaborated: |
| 6482 | return GetNumPointeeChildren( |
| 6483 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()); |
| 6484 | case clang::Type::TypeOfExpr: |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 6485 | return GetNumPointeeChildren(llvm::cast<clang::TypeOfExprType>(qual_type) |
| 6486 | ->getUnderlyingExpr() |
| 6487 | ->getType()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6488 | case clang::Type::TypeOf: |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 6489 | return GetNumPointeeChildren( |
| 6490 | llvm::cast<clang::TypeOfType>(qual_type)->getUnderlyingType()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6491 | case clang::Type::Decltype: |
Jonas Devlieghere | 65d2d5b | 2018-02-20 10:15:08 +0000 | [diff] [blame] | 6492 | return GetNumPointeeChildren( |
| 6493 | llvm::cast<clang::DecltypeType>(qual_type)->getUnderlyingType()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6494 | case clang::Type::Record: |
| 6495 | return 0; |
| 6496 | case clang::Type::Enum: |
| 6497 | return 1; |
| 6498 | case clang::Type::TemplateTypeParm: |
| 6499 | return 1; |
| 6500 | case clang::Type::SubstTemplateTypeParm: |
| 6501 | return 1; |
| 6502 | case clang::Type::TemplateSpecialization: |
| 6503 | return 1; |
| 6504 | case clang::Type::InjectedClassName: |
| 6505 | return 0; |
| 6506 | case clang::Type::DependentName: |
| 6507 | return 1; |
| 6508 | case clang::Type::DependentTemplateSpecialization: |
| 6509 | return 1; |
| 6510 | case clang::Type::ObjCObject: |
| 6511 | return 0; |
| 6512 | case clang::Type::ObjCInterface: |
| 6513 | return 0; |
| 6514 | case clang::Type::ObjCObjectPointer: |
| 6515 | return 1; |
| 6516 | default: |
| 6517 | break; |
| 6518 | } |
| 6519 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 6520 | } |
| 6521 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6522 | CompilerType ClangASTContext::GetChildCompilerTypeAtIndex( |
| 6523 | lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx, |
| 6524 | bool transparent_pointers, bool omit_empty_base_classes, |
| 6525 | bool ignore_array_bounds, std::string &child_name, |
| 6526 | uint32_t &child_byte_size, int32_t &child_byte_offset, |
| 6527 | uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset, |
| 6528 | bool &child_is_base_class, bool &child_is_deref_of_parent, |
| 6529 | ValueObject *valobj, uint64_t &language_flags) { |
| 6530 | if (!type) |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 6531 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 6532 | |
Adrian Prantl | 2f1fa7a | 2019-01-15 21:04:19 +0000 | [diff] [blame] | 6533 | auto get_exe_scope = [&exe_ctx]() { |
| 6534 | return exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr; |
| 6535 | }; |
| 6536 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6537 | clang::QualType parent_qual_type(GetCanonicalQualType(type)); |
| 6538 | const clang::Type::TypeClass parent_type_class = |
| 6539 | parent_qual_type->getTypeClass(); |
| 6540 | child_bitfield_bit_size = 0; |
| 6541 | child_bitfield_bit_offset = 0; |
| 6542 | child_is_base_class = false; |
| 6543 | language_flags = 0; |
| 6544 | |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 6545 | const bool idx_is_valid = |
| 6546 | idx < GetNumChildren(type, omit_empty_base_classes, exe_ctx); |
Aleksandr Urakov | 7d2a74f | 2018-08-14 07:57:44 +0000 | [diff] [blame] | 6547 | int32_t bit_offset; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6548 | switch (parent_type_class) { |
| 6549 | case clang::Type::Builtin: |
| 6550 | if (idx_is_valid) { |
| 6551 | switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) { |
| 6552 | case clang::BuiltinType::ObjCId: |
| 6553 | case clang::BuiltinType::ObjCClass: |
| 6554 | child_name = "isa"; |
| 6555 | child_byte_size = |
| 6556 | getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) / |
| 6557 | CHAR_BIT; |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 6558 | return CompilerType( |
| 6559 | this, getASTContext()->ObjCBuiltinClassTy.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6560 | |
| 6561 | default: |
| 6562 | break; |
| 6563 | } |
| 6564 | } |
| 6565 | break; |
| 6566 | |
| 6567 | case clang::Type::Record: |
| 6568 | if (idx_is_valid && GetCompleteType(type)) { |
| 6569 | const clang::RecordType *record_type = |
| 6570 | llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr()); |
| 6571 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 6572 | assert(record_decl); |
| 6573 | const clang::ASTRecordLayout &record_layout = |
| 6574 | getASTContext()->getASTRecordLayout(record_decl); |
| 6575 | uint32_t child_idx = 0; |
| 6576 | |
| 6577 | const clang::CXXRecordDecl *cxx_record_decl = |
| 6578 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 6579 | if (cxx_record_decl) { |
| 6580 | // We might have base classes to print out first |
| 6581 | clang::CXXRecordDecl::base_class_const_iterator base_class, |
| 6582 | base_class_end; |
| 6583 | for (base_class = cxx_record_decl->bases_begin(), |
| 6584 | base_class_end = cxx_record_decl->bases_end(); |
| 6585 | base_class != base_class_end; ++base_class) { |
| 6586 | const clang::CXXRecordDecl *base_class_decl = nullptr; |
| 6587 | |
| 6588 | // Skip empty base classes |
| 6589 | if (omit_empty_base_classes) { |
| 6590 | base_class_decl = llvm::cast<clang::CXXRecordDecl>( |
| 6591 | base_class->getType()->getAs<clang::RecordType>()->getDecl()); |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 6592 | if (!ClangASTContext::RecordHasFields(base_class_decl)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6593 | continue; |
| 6594 | } |
| 6595 | |
| 6596 | if (idx == child_idx) { |
| 6597 | if (base_class_decl == nullptr) |
| 6598 | base_class_decl = llvm::cast<clang::CXXRecordDecl>( |
| 6599 | base_class->getType()->getAs<clang::RecordType>()->getDecl()); |
| 6600 | |
| 6601 | if (base_class->isVirtual()) { |
| 6602 | bool handled = false; |
| 6603 | if (valobj) { |
Aleksandr Urakov | 1dc51db | 2018-11-12 16:23:50 +0000 | [diff] [blame] | 6604 | clang::VTableContextBase *vtable_ctx = |
| 6605 | getASTContext()->getVTableContext(); |
| 6606 | if (vtable_ctx) |
| 6607 | handled = GetVBaseBitOffset(*vtable_ctx, *valobj, |
| 6608 | record_layout, cxx_record_decl, |
| 6609 | base_class_decl, bit_offset); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6610 | } |
| 6611 | if (!handled) |
| 6612 | bit_offset = record_layout.getVBaseClassOffset(base_class_decl) |
| 6613 | .getQuantity() * |
| 6614 | 8; |
| 6615 | } else |
| 6616 | bit_offset = record_layout.getBaseClassOffset(base_class_decl) |
| 6617 | .getQuantity() * |
| 6618 | 8; |
| 6619 | |
| 6620 | // Base classes should be a multiple of 8 bits in size |
| 6621 | child_byte_offset = bit_offset / 8; |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 6622 | CompilerType base_class_clang_type( |
| 6623 | this, base_class->getType().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6624 | child_name = base_class_clang_type.GetTypeName().AsCString(""); |
Adrian Prantl | 2f1fa7a | 2019-01-15 21:04:19 +0000 | [diff] [blame] | 6625 | Optional<uint64_t> size = |
| 6626 | base_class_clang_type.GetBitSize(get_exe_scope()); |
Adrian Prantl | d963a7c | 2019-01-15 18:07:52 +0000 | [diff] [blame] | 6627 | if (!size) |
| 6628 | return {}; |
| 6629 | uint64_t base_class_clang_type_bit_size = *size; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6630 | |
| 6631 | // Base classes bit sizes should be a multiple of 8 bits in size |
| 6632 | assert(base_class_clang_type_bit_size % 8 == 0); |
| 6633 | child_byte_size = base_class_clang_type_bit_size / 8; |
| 6634 | child_is_base_class = true; |
| 6635 | return base_class_clang_type; |
| 6636 | } |
| 6637 | // We don't increment the child index in the for loop since we might |
| 6638 | // be skipping empty base classes |
| 6639 | ++child_idx; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 6640 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6641 | } |
| 6642 | // Make sure index is in range... |
| 6643 | uint32_t field_idx = 0; |
| 6644 | clang::RecordDecl::field_iterator field, field_end; |
| 6645 | for (field = record_decl->field_begin(), |
| 6646 | field_end = record_decl->field_end(); |
| 6647 | field != field_end; ++field, ++field_idx, ++child_idx) { |
| 6648 | if (idx == child_idx) { |
| 6649 | // Print the member type if requested |
| 6650 | // Print the member name and equal sign |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 6651 | child_name.assign(field->getNameAsString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6652 | |
| 6653 | // Figure out the type byte size (field_type_info.first) and |
| 6654 | // alignment (field_type_info.second) from the AST context. |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 6655 | CompilerType field_clang_type(this, |
| 6656 | field->getType().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6657 | assert(field_idx < record_layout.getFieldCount()); |
Adrian Prantl | 2f1fa7a | 2019-01-15 21:04:19 +0000 | [diff] [blame] | 6658 | Optional<uint64_t> size = |
| 6659 | field_clang_type.GetByteSize(get_exe_scope()); |
Adrian Prantl | d963a7c | 2019-01-15 18:07:52 +0000 | [diff] [blame] | 6660 | if (!size) |
| 6661 | return {}; |
| 6662 | child_byte_size = *size; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6663 | const uint32_t child_bit_size = child_byte_size * 8; |
| 6664 | |
| 6665 | // Figure out the field offset within the current struct/union/class |
| 6666 | // type |
| 6667 | bit_offset = record_layout.getFieldOffset(field_idx); |
Raphael Isemann | 02e9113 | 2019-11-20 12:09:19 +0100 | [diff] [blame] | 6668 | if (FieldIsBitfield(*field, child_bitfield_bit_size)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6669 | child_bitfield_bit_offset = bit_offset % child_bit_size; |
| 6670 | const uint32_t child_bit_offset = |
| 6671 | bit_offset - child_bitfield_bit_offset; |
| 6672 | child_byte_offset = child_bit_offset / 8; |
| 6673 | } else { |
| 6674 | child_byte_offset = bit_offset / 8; |
| 6675 | } |
| 6676 | |
| 6677 | return field_clang_type; |
| 6678 | } |
| 6679 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 6680 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6681 | break; |
| 6682 | |
| 6683 | case clang::Type::ObjCObject: |
| 6684 | case clang::Type::ObjCInterface: |
| 6685 | if (idx_is_valid && GetCompleteType(type)) { |
| 6686 | const clang::ObjCObjectType *objc_class_type = |
| 6687 | llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr()); |
| 6688 | assert(objc_class_type); |
| 6689 | if (objc_class_type) { |
| 6690 | uint32_t child_idx = 0; |
| 6691 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 6692 | objc_class_type->getInterface(); |
| 6693 | |
| 6694 | if (class_interface_decl) { |
| 6695 | |
| 6696 | const clang::ASTRecordLayout &interface_layout = |
| 6697 | getASTContext()->getASTObjCInterfaceLayout(class_interface_decl); |
| 6698 | clang::ObjCInterfaceDecl *superclass_interface_decl = |
| 6699 | class_interface_decl->getSuperClass(); |
| 6700 | if (superclass_interface_decl) { |
| 6701 | if (omit_empty_base_classes) { |
| 6702 | CompilerType base_class_clang_type( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 6703 | this, getASTContext() |
| 6704 | ->getObjCInterfaceType(superclass_interface_decl) |
| 6705 | .getAsOpaquePtr()); |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 6706 | if (base_class_clang_type.GetNumChildren(omit_empty_base_classes, |
| 6707 | exe_ctx) > 0) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6708 | if (idx == 0) { |
| 6709 | clang::QualType ivar_qual_type( |
| 6710 | getASTContext()->getObjCInterfaceType( |
| 6711 | superclass_interface_decl)); |
| 6712 | |
| 6713 | child_name.assign( |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 6714 | superclass_interface_decl->getNameAsString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6715 | |
| 6716 | clang::TypeInfo ivar_type_info = |
| 6717 | getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr()); |
| 6718 | |
| 6719 | child_byte_size = ivar_type_info.Width / 8; |
| 6720 | child_byte_offset = 0; |
| 6721 | child_is_base_class = true; |
| 6722 | |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 6723 | return CompilerType(this, ivar_qual_type.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6724 | } |
| 6725 | |
| 6726 | ++child_idx; |
| 6727 | } |
| 6728 | } else |
| 6729 | ++child_idx; |
| 6730 | } |
| 6731 | |
| 6732 | const uint32_t superclass_idx = child_idx; |
| 6733 | |
| 6734 | if (idx < (child_idx + class_interface_decl->ivar_size())) { |
| 6735 | clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, |
| 6736 | ivar_end = class_interface_decl->ivar_end(); |
| 6737 | |
| 6738 | for (ivar_pos = class_interface_decl->ivar_begin(); |
| 6739 | ivar_pos != ivar_end; ++ivar_pos) { |
| 6740 | if (child_idx == idx) { |
| 6741 | clang::ObjCIvarDecl *ivar_decl = *ivar_pos; |
| 6742 | |
| 6743 | clang::QualType ivar_qual_type(ivar_decl->getType()); |
| 6744 | |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 6745 | child_name.assign(ivar_decl->getNameAsString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6746 | |
| 6747 | clang::TypeInfo ivar_type_info = |
| 6748 | getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr()); |
| 6749 | |
| 6750 | child_byte_size = ivar_type_info.Width / 8; |
| 6751 | |
| 6752 | // Figure out the field offset within the current |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 6753 | // struct/union/class type For ObjC objects, we can't trust the |
| 6754 | // bit offset we get from the Clang AST, since that doesn't |
| 6755 | // account for the space taken up by unbacked properties, or |
| 6756 | // from the changing size of base classes that are newer than |
| 6757 | // this class. So if we have a process around that we can ask |
| 6758 | // about this object, do so. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6759 | child_byte_offset = LLDB_INVALID_IVAR_OFFSET; |
| 6760 | Process *process = nullptr; |
| 6761 | if (exe_ctx) |
| 6762 | process = exe_ctx->GetProcessPtr(); |
| 6763 | if (process) { |
| 6764 | ObjCLanguageRuntime *objc_runtime = |
Alex Langford | e823bbe | 2019-06-10 20:53:23 +0000 | [diff] [blame] | 6765 | ObjCLanguageRuntime::Get(*process); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6766 | if (objc_runtime != nullptr) { |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 6767 | CompilerType parent_ast_type( |
| 6768 | this, parent_qual_type.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6769 | child_byte_offset = objc_runtime->GetByteOffsetForIvar( |
| 6770 | parent_ast_type, ivar_decl->getNameAsString().c_str()); |
| 6771 | } |
| 6772 | } |
| 6773 | |
Aleksandr Urakov | ff70172 | 2018-08-20 05:59:27 +0000 | [diff] [blame] | 6774 | // 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] | 6775 | // twice... |
Aleksandr Urakov | 5345948 | 2018-08-17 07:28:24 +0000 | [diff] [blame] | 6776 | bit_offset = INT32_MAX; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6777 | |
| 6778 | if (child_byte_offset == |
| 6779 | static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) { |
| 6780 | bit_offset = interface_layout.getFieldOffset(child_idx - |
| 6781 | superclass_idx); |
| 6782 | child_byte_offset = bit_offset / 8; |
| 6783 | } |
| 6784 | |
| 6785 | // Note, the ObjC Ivar Byte offset is just that, it doesn't |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 6786 | // account for the bit offset of a bitfield within its |
| 6787 | // containing object. So regardless of where we get the byte |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6788 | // offset from, we still need to get the bit offset for |
| 6789 | // bitfields from the layout. |
| 6790 | |
Raphael Isemann | 02e9113 | 2019-11-20 12:09:19 +0100 | [diff] [blame] | 6791 | if (FieldIsBitfield(ivar_decl, child_bitfield_bit_size)) { |
Aleksandr Urakov | 5345948 | 2018-08-17 07:28:24 +0000 | [diff] [blame] | 6792 | if (bit_offset == INT32_MAX) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6793 | bit_offset = interface_layout.getFieldOffset( |
| 6794 | child_idx - superclass_idx); |
| 6795 | |
| 6796 | child_bitfield_bit_offset = bit_offset % 8; |
| 6797 | } |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 6798 | return CompilerType(this, ivar_qual_type.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6799 | } |
| 6800 | ++child_idx; |
| 6801 | } |
| 6802 | } |
| 6803 | } |
| 6804 | } |
| 6805 | } |
| 6806 | break; |
| 6807 | |
| 6808 | case clang::Type::ObjCObjectPointer: |
| 6809 | if (idx_is_valid) { |
| 6810 | CompilerType pointee_clang_type(GetPointeeType(type)); |
| 6811 | |
| 6812 | if (transparent_pointers && pointee_clang_type.IsAggregateType()) { |
| 6813 | child_is_deref_of_parent = false; |
| 6814 | bool tmp_child_is_deref_of_parent = false; |
| 6815 | return pointee_clang_type.GetChildCompilerTypeAtIndex( |
| 6816 | exe_ctx, idx, transparent_pointers, omit_empty_base_classes, |
| 6817 | ignore_array_bounds, child_name, child_byte_size, child_byte_offset, |
| 6818 | child_bitfield_bit_size, child_bitfield_bit_offset, |
| 6819 | child_is_base_class, tmp_child_is_deref_of_parent, valobj, |
| 6820 | language_flags); |
| 6821 | } else { |
| 6822 | child_is_deref_of_parent = true; |
| 6823 | const char *parent_name = |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 6824 | valobj ? valobj->GetName().GetCString() : nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6825 | if (parent_name) { |
| 6826 | child_name.assign(1, '*'); |
| 6827 | child_name += parent_name; |
| 6828 | } |
| 6829 | |
| 6830 | // We have a pointer to an simple type |
| 6831 | if (idx == 0 && pointee_clang_type.GetCompleteType()) { |
Adrian Prantl | 2f1fa7a | 2019-01-15 21:04:19 +0000 | [diff] [blame] | 6832 | if (Optional<uint64_t> size = |
| 6833 | pointee_clang_type.GetByteSize(get_exe_scope())) { |
Adrian Prantl | d963a7c | 2019-01-15 18:07:52 +0000 | [diff] [blame] | 6834 | child_byte_size = *size; |
| 6835 | child_byte_offset = 0; |
| 6836 | return pointee_clang_type; |
| 6837 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6838 | } |
| 6839 | } |
| 6840 | } |
| 6841 | break; |
| 6842 | |
| 6843 | case clang::Type::Vector: |
| 6844 | case clang::Type::ExtVector: |
| 6845 | if (idx_is_valid) { |
| 6846 | const clang::VectorType *array = |
| 6847 | llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr()); |
| 6848 | if (array) { |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 6849 | CompilerType element_type(this, |
| 6850 | array->getElementType().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6851 | if (element_type.GetCompleteType()) { |
| 6852 | char element_name[64]; |
| 6853 | ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]", |
| 6854 | static_cast<uint64_t>(idx)); |
| 6855 | child_name.assign(element_name); |
Adrian Prantl | 2f1fa7a | 2019-01-15 21:04:19 +0000 | [diff] [blame] | 6856 | if (Optional<uint64_t> size = |
| 6857 | element_type.GetByteSize(get_exe_scope())) { |
Adrian Prantl | d963a7c | 2019-01-15 18:07:52 +0000 | [diff] [blame] | 6858 | child_byte_size = *size; |
| 6859 | child_byte_offset = (int32_t)idx * (int32_t)child_byte_size; |
| 6860 | return element_type; |
| 6861 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6862 | } |
| 6863 | } |
| 6864 | } |
| 6865 | break; |
| 6866 | |
| 6867 | case clang::Type::ConstantArray: |
| 6868 | case clang::Type::IncompleteArray: |
| 6869 | if (ignore_array_bounds || idx_is_valid) { |
| 6870 | const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe(); |
| 6871 | if (array) { |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 6872 | CompilerType element_type(this, |
| 6873 | array->getElementType().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6874 | if (element_type.GetCompleteType()) { |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 6875 | child_name = llvm::formatv("[{0}]", idx); |
Adrian Prantl | 2f1fa7a | 2019-01-15 21:04:19 +0000 | [diff] [blame] | 6876 | if (Optional<uint64_t> size = |
| 6877 | element_type.GetByteSize(get_exe_scope())) { |
Adrian Prantl | d963a7c | 2019-01-15 18:07:52 +0000 | [diff] [blame] | 6878 | child_byte_size = *size; |
| 6879 | child_byte_offset = (int32_t)idx * (int32_t)child_byte_size; |
| 6880 | return element_type; |
| 6881 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6882 | } |
| 6883 | } |
| 6884 | } |
| 6885 | break; |
| 6886 | |
Tamas Berghammer | 1c62e03 | 2017-01-07 16:39:07 +0000 | [diff] [blame] | 6887 | case clang::Type::Pointer: { |
| 6888 | CompilerType pointee_clang_type(GetPointeeType(type)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6889 | |
Tamas Berghammer | 1c62e03 | 2017-01-07 16:39:07 +0000 | [diff] [blame] | 6890 | // Don't dereference "void *" pointers |
| 6891 | if (pointee_clang_type.IsVoidType()) |
| 6892 | return CompilerType(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6893 | |
Tamas Berghammer | 1c62e03 | 2017-01-07 16:39:07 +0000 | [diff] [blame] | 6894 | if (transparent_pointers && pointee_clang_type.IsAggregateType()) { |
| 6895 | child_is_deref_of_parent = false; |
| 6896 | bool tmp_child_is_deref_of_parent = false; |
| 6897 | return pointee_clang_type.GetChildCompilerTypeAtIndex( |
| 6898 | exe_ctx, idx, transparent_pointers, omit_empty_base_classes, |
| 6899 | ignore_array_bounds, child_name, child_byte_size, child_byte_offset, |
| 6900 | child_bitfield_bit_size, child_bitfield_bit_offset, |
| 6901 | child_is_base_class, tmp_child_is_deref_of_parent, valobj, |
| 6902 | language_flags); |
| 6903 | } else { |
| 6904 | child_is_deref_of_parent = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6905 | |
Tamas Berghammer | 1c62e03 | 2017-01-07 16:39:07 +0000 | [diff] [blame] | 6906 | const char *parent_name = |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 6907 | valobj ? valobj->GetName().GetCString() : nullptr; |
Tamas Berghammer | 1c62e03 | 2017-01-07 16:39:07 +0000 | [diff] [blame] | 6908 | if (parent_name) { |
| 6909 | child_name.assign(1, '*'); |
| 6910 | child_name += parent_name; |
| 6911 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6912 | |
Tamas Berghammer | 1c62e03 | 2017-01-07 16:39:07 +0000 | [diff] [blame] | 6913 | // We have a pointer to an simple type |
| 6914 | if (idx == 0) { |
Adrian Prantl | 2f1fa7a | 2019-01-15 21:04:19 +0000 | [diff] [blame] | 6915 | if (Optional<uint64_t> size = |
| 6916 | pointee_clang_type.GetByteSize(get_exe_scope())) { |
Adrian Prantl | d963a7c | 2019-01-15 18:07:52 +0000 | [diff] [blame] | 6917 | child_byte_size = *size; |
| 6918 | child_byte_offset = 0; |
| 6919 | return pointee_clang_type; |
| 6920 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6921 | } |
| 6922 | } |
| 6923 | break; |
Tamas Berghammer | 1c62e03 | 2017-01-07 16:39:07 +0000 | [diff] [blame] | 6924 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6925 | |
| 6926 | case clang::Type::LValueReference: |
| 6927 | case clang::Type::RValueReference: |
| 6928 | if (idx_is_valid) { |
| 6929 | const clang::ReferenceType *reference_type = |
| 6930 | llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr()); |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 6931 | CompilerType pointee_clang_type( |
| 6932 | this, reference_type->getPointeeType().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6933 | if (transparent_pointers && pointee_clang_type.IsAggregateType()) { |
| 6934 | child_is_deref_of_parent = false; |
| 6935 | bool tmp_child_is_deref_of_parent = false; |
| 6936 | return pointee_clang_type.GetChildCompilerTypeAtIndex( |
| 6937 | exe_ctx, idx, transparent_pointers, omit_empty_base_classes, |
| 6938 | ignore_array_bounds, child_name, child_byte_size, child_byte_offset, |
| 6939 | child_bitfield_bit_size, child_bitfield_bit_offset, |
| 6940 | child_is_base_class, tmp_child_is_deref_of_parent, valobj, |
| 6941 | language_flags); |
| 6942 | } else { |
| 6943 | const char *parent_name = |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 6944 | valobj ? valobj->GetName().GetCString() : nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6945 | if (parent_name) { |
| 6946 | child_name.assign(1, '&'); |
| 6947 | child_name += parent_name; |
| 6948 | } |
| 6949 | |
| 6950 | // We have a pointer to an simple type |
| 6951 | if (idx == 0) { |
Adrian Prantl | 2f1fa7a | 2019-01-15 21:04:19 +0000 | [diff] [blame] | 6952 | if (Optional<uint64_t> size = |
| 6953 | pointee_clang_type.GetByteSize(get_exe_scope())) { |
Adrian Prantl | d963a7c | 2019-01-15 18:07:52 +0000 | [diff] [blame] | 6954 | child_byte_size = *size; |
| 6955 | child_byte_offset = 0; |
| 6956 | return pointee_clang_type; |
| 6957 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6958 | } |
| 6959 | } |
| 6960 | } |
| 6961 | break; |
| 6962 | |
| 6963 | case clang::Type::Typedef: { |
| 6964 | CompilerType typedefed_clang_type( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 6965 | this, llvm::cast<clang::TypedefType>(parent_qual_type) |
| 6966 | ->getDecl() |
| 6967 | ->getUnderlyingType() |
| 6968 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6969 | return typedefed_clang_type.GetChildCompilerTypeAtIndex( |
| 6970 | exe_ctx, idx, transparent_pointers, omit_empty_base_classes, |
| 6971 | ignore_array_bounds, child_name, child_byte_size, child_byte_offset, |
| 6972 | child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class, |
| 6973 | child_is_deref_of_parent, valobj, language_flags); |
| 6974 | } break; |
| 6975 | |
| 6976 | case clang::Type::Auto: { |
| 6977 | CompilerType elaborated_clang_type( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 6978 | this, llvm::cast<clang::AutoType>(parent_qual_type) |
| 6979 | ->getDeducedType() |
| 6980 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6981 | return elaborated_clang_type.GetChildCompilerTypeAtIndex( |
| 6982 | exe_ctx, idx, transparent_pointers, omit_empty_base_classes, |
| 6983 | ignore_array_bounds, child_name, child_byte_size, child_byte_offset, |
| 6984 | child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class, |
| 6985 | child_is_deref_of_parent, valobj, language_flags); |
| 6986 | } |
| 6987 | |
| 6988 | case clang::Type::Elaborated: { |
| 6989 | CompilerType elaborated_clang_type( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 6990 | this, llvm::cast<clang::ElaboratedType>(parent_qual_type) |
| 6991 | ->getNamedType() |
| 6992 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 6993 | return elaborated_clang_type.GetChildCompilerTypeAtIndex( |
| 6994 | exe_ctx, idx, transparent_pointers, omit_empty_base_classes, |
| 6995 | ignore_array_bounds, child_name, child_byte_size, child_byte_offset, |
| 6996 | child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class, |
| 6997 | child_is_deref_of_parent, valobj, language_flags); |
| 6998 | } |
| 6999 | |
| 7000 | case clang::Type::Paren: { |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 7001 | CompilerType paren_clang_type(this, |
| 7002 | llvm::cast<clang::ParenType>(parent_qual_type) |
| 7003 | ->desugar() |
| 7004 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7005 | return paren_clang_type.GetChildCompilerTypeAtIndex( |
| 7006 | exe_ctx, idx, transparent_pointers, omit_empty_base_classes, |
| 7007 | ignore_array_bounds, child_name, child_byte_size, child_byte_offset, |
| 7008 | child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class, |
| 7009 | child_is_deref_of_parent, valobj, language_flags); |
| 7010 | } |
| 7011 | |
| 7012 | default: |
| 7013 | break; |
| 7014 | } |
| 7015 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7016 | } |
| 7017 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7018 | static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl, |
| 7019 | const clang::CXXBaseSpecifier *base_spec, |
| 7020 | bool omit_empty_base_classes) { |
| 7021 | uint32_t child_idx = 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7022 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7023 | const clang::CXXRecordDecl *cxx_record_decl = |
| 7024 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 7025 | |
| 7026 | // const char *super_name = record_decl->getNameAsCString(); |
| 7027 | // const char *base_name = |
| 7028 | // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString(); |
| 7029 | // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name); |
| 7030 | // |
| 7031 | if (cxx_record_decl) { |
| 7032 | clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end; |
| 7033 | for (base_class = cxx_record_decl->bases_begin(), |
| 7034 | base_class_end = cxx_record_decl->bases_end(); |
| 7035 | base_class != base_class_end; ++base_class) { |
| 7036 | if (omit_empty_base_classes) { |
| 7037 | if (BaseSpecifierIsEmpty(base_class)) |
| 7038 | continue; |
| 7039 | } |
| 7040 | |
| 7041 | // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", |
| 7042 | // super_name, base_name, |
| 7043 | // child_idx, |
| 7044 | // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString()); |
| 7045 | // |
| 7046 | // |
| 7047 | if (base_class == base_spec) |
| 7048 | return child_idx; |
| 7049 | ++child_idx; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7050 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7051 | } |
| 7052 | |
| 7053 | return UINT32_MAX; |
| 7054 | } |
| 7055 | |
| 7056 | static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl, |
| 7057 | clang::NamedDecl *canonical_decl, |
| 7058 | bool omit_empty_base_classes) { |
| 7059 | uint32_t child_idx = ClangASTContext::GetNumBaseClasses( |
| 7060 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl), |
| 7061 | omit_empty_base_classes); |
| 7062 | |
| 7063 | clang::RecordDecl::field_iterator field, field_end; |
| 7064 | for (field = record_decl->field_begin(), field_end = record_decl->field_end(); |
| 7065 | field != field_end; ++field, ++child_idx) { |
| 7066 | if (field->getCanonicalDecl() == canonical_decl) |
| 7067 | return child_idx; |
| 7068 | } |
| 7069 | |
| 7070 | return UINT32_MAX; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7071 | } |
| 7072 | |
| 7073 | // 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] | 7074 | // their members) in the type hierarchy. Returns an index path into |
| 7075 | // "clang_type" on how to reach the appropriate member. |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7076 | // |
| 7077 | // class A |
| 7078 | // { |
| 7079 | // public: |
| 7080 | // int m_a; |
| 7081 | // int m_b; |
| 7082 | // }; |
| 7083 | // |
| 7084 | // class B |
| 7085 | // { |
| 7086 | // }; |
| 7087 | // |
| 7088 | // class C : |
| 7089 | // public B, |
| 7090 | // public A |
| 7091 | // { |
| 7092 | // }; |
| 7093 | // |
| 7094 | // If we have a clang type that describes "class C", and we wanted to looked |
| 7095 | // "m_b" in it: |
| 7096 | // |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7097 | // 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] | 7098 | // with: { 1, 1 } The first index 1 is the child index for "class A" within |
| 7099 | // 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] | 7100 | // |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 7101 | // With omit_empty_base_classes == true we would get an integer array back |
| 7102 | // with: { 0, 1 } The first index 0 is the child index for "class A" within |
| 7103 | // class C (since class B doesn't have any members it doesn't count) The second |
| 7104 | // index 1 is the child index for "m_b" within class A |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7105 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7106 | size_t ClangASTContext::GetIndexOfChildMemberWithName( |
| 7107 | lldb::opaque_compiler_type_t type, const char *name, |
| 7108 | bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) { |
| 7109 | if (type && name && name[0]) { |
| 7110 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 7111 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 7112 | switch (type_class) { |
| 7113 | case clang::Type::Record: |
| 7114 | if (GetCompleteType(type)) { |
| 7115 | const clang::RecordType *record_type = |
| 7116 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 7117 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
Enrico Granata | 36f51e4 | 2015-12-18 22:41:25 +0000 | [diff] [blame] | 7118 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7119 | assert(record_decl); |
| 7120 | uint32_t child_idx = 0; |
| 7121 | |
| 7122 | const clang::CXXRecordDecl *cxx_record_decl = |
| 7123 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 7124 | |
| 7125 | // Try and find a field that matches NAME |
| 7126 | clang::RecordDecl::field_iterator field, field_end; |
| 7127 | llvm::StringRef name_sref(name); |
| 7128 | for (field = record_decl->field_begin(), |
| 7129 | field_end = record_decl->field_end(); |
| 7130 | field != field_end; ++field, ++child_idx) { |
| 7131 | llvm::StringRef field_name = field->getName(); |
| 7132 | if (field_name.empty()) { |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 7133 | CompilerType field_type(this, field->getType().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7134 | child_indexes.push_back(child_idx); |
| 7135 | if (field_type.GetIndexOfChildMemberWithName( |
| 7136 | name, omit_empty_base_classes, child_indexes)) |
| 7137 | return child_indexes.size(); |
| 7138 | child_indexes.pop_back(); |
| 7139 | |
| 7140 | } else if (field_name.equals(name_sref)) { |
| 7141 | // We have to add on the number of base classes to this index! |
| 7142 | child_indexes.push_back( |
| 7143 | child_idx + ClangASTContext::GetNumBaseClasses( |
| 7144 | cxx_record_decl, omit_empty_base_classes)); |
| 7145 | return child_indexes.size(); |
| 7146 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7147 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7148 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7149 | if (cxx_record_decl) { |
| 7150 | const clang::RecordDecl *parent_record_decl = cxx_record_decl; |
| 7151 | |
| 7152 | // printf ("parent = %s\n", parent_record_decl->getNameAsCString()); |
| 7153 | |
| 7154 | // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl(); |
| 7155 | // Didn't find things easily, lets let clang do its thang... |
| 7156 | clang::IdentifierInfo &ident_ref = |
| 7157 | getASTContext()->Idents.get(name_sref); |
| 7158 | clang::DeclarationName decl_name(&ident_ref); |
| 7159 | |
| 7160 | clang::CXXBasePaths paths; |
| 7161 | if (cxx_record_decl->lookupInBases( |
| 7162 | [decl_name](const clang::CXXBaseSpecifier *specifier, |
| 7163 | clang::CXXBasePath &path) { |
| 7164 | return clang::CXXRecordDecl::FindOrdinaryMember( |
| 7165 | specifier, path, decl_name); |
| 7166 | }, |
| 7167 | paths)) { |
| 7168 | clang::CXXBasePaths::const_paths_iterator path, |
| 7169 | path_end = paths.end(); |
| 7170 | for (path = paths.begin(); path != path_end; ++path) { |
| 7171 | const size_t num_path_elements = path->size(); |
| 7172 | for (size_t e = 0; e < num_path_elements; ++e) { |
| 7173 | clang::CXXBasePathElement elem = (*path)[e]; |
| 7174 | |
| 7175 | child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base, |
| 7176 | omit_empty_base_classes); |
| 7177 | if (child_idx == UINT32_MAX) { |
| 7178 | child_indexes.clear(); |
| 7179 | return 0; |
| 7180 | } else { |
| 7181 | child_indexes.push_back(child_idx); |
| 7182 | parent_record_decl = llvm::cast<clang::RecordDecl>( |
| 7183 | elem.Base->getType() |
| 7184 | ->getAs<clang::RecordType>() |
| 7185 | ->getDecl()); |
| 7186 | } |
| 7187 | } |
| 7188 | for (clang::NamedDecl *path_decl : path->Decls) { |
| 7189 | child_idx = GetIndexForRecordChild( |
| 7190 | parent_record_decl, path_decl, omit_empty_base_classes); |
| 7191 | if (child_idx == UINT32_MAX) { |
| 7192 | child_indexes.clear(); |
| 7193 | return 0; |
| 7194 | } else { |
| 7195 | child_indexes.push_back(child_idx); |
| 7196 | } |
| 7197 | } |
| 7198 | } |
| 7199 | return child_indexes.size(); |
| 7200 | } |
| 7201 | } |
| 7202 | } |
| 7203 | break; |
| 7204 | |
| 7205 | case clang::Type::ObjCObject: |
| 7206 | case clang::Type::ObjCInterface: |
| 7207 | if (GetCompleteType(type)) { |
| 7208 | llvm::StringRef name_sref(name); |
| 7209 | const clang::ObjCObjectType *objc_class_type = |
| 7210 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 7211 | assert(objc_class_type); |
| 7212 | if (objc_class_type) { |
| 7213 | uint32_t child_idx = 0; |
| 7214 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 7215 | objc_class_type->getInterface(); |
| 7216 | |
| 7217 | if (class_interface_decl) { |
| 7218 | clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, |
| 7219 | ivar_end = class_interface_decl->ivar_end(); |
| 7220 | clang::ObjCInterfaceDecl *superclass_interface_decl = |
| 7221 | class_interface_decl->getSuperClass(); |
| 7222 | |
| 7223 | for (ivar_pos = class_interface_decl->ivar_begin(); |
| 7224 | ivar_pos != ivar_end; ++ivar_pos, ++child_idx) { |
| 7225 | const clang::ObjCIvarDecl *ivar_decl = *ivar_pos; |
| 7226 | |
| 7227 | if (ivar_decl->getName().equals(name_sref)) { |
| 7228 | if ((!omit_empty_base_classes && superclass_interface_decl) || |
| 7229 | (omit_empty_base_classes && |
| 7230 | ObjCDeclHasIVars(superclass_interface_decl, true))) |
| 7231 | ++child_idx; |
| 7232 | |
| 7233 | child_indexes.push_back(child_idx); |
| 7234 | return child_indexes.size(); |
| 7235 | } |
| 7236 | } |
| 7237 | |
| 7238 | if (superclass_interface_decl) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 7239 | // The super class index is always zero for ObjC classes, so we |
| 7240 | // push it onto the child indexes in case we find an ivar in our |
| 7241 | // superclass... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7242 | child_indexes.push_back(0); |
| 7243 | |
| 7244 | CompilerType superclass_clang_type( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 7245 | this, getASTContext() |
| 7246 | ->getObjCInterfaceType(superclass_interface_decl) |
| 7247 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7248 | if (superclass_clang_type.GetIndexOfChildMemberWithName( |
| 7249 | name, omit_empty_base_classes, child_indexes)) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 7250 | // We did find an ivar in a superclass so just return the |
| 7251 | // results! |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7252 | return child_indexes.size(); |
| 7253 | } |
| 7254 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 7255 | // We didn't find an ivar matching "name" in our superclass, pop |
| 7256 | // the superclass zero index that we pushed on above. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7257 | child_indexes.pop_back(); |
| 7258 | } |
| 7259 | } |
| 7260 | } |
| 7261 | } |
| 7262 | break; |
| 7263 | |
| 7264 | case clang::Type::ObjCObjectPointer: { |
| 7265 | CompilerType objc_object_clang_type( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 7266 | this, llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr()) |
| 7267 | ->getPointeeType() |
| 7268 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7269 | return objc_object_clang_type.GetIndexOfChildMemberWithName( |
| 7270 | name, omit_empty_base_classes, child_indexes); |
| 7271 | } break; |
| 7272 | |
| 7273 | case clang::Type::ConstantArray: { |
| 7274 | // const clang::ConstantArrayType *array = |
| 7275 | // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr()); |
| 7276 | // const uint64_t element_count = |
| 7277 | // array->getSize().getLimitedValue(); |
| 7278 | // |
| 7279 | // if (idx < element_count) |
| 7280 | // { |
| 7281 | // std::pair<uint64_t, unsigned> field_type_info = |
| 7282 | // ast->getTypeInfo(array->getElementType()); |
| 7283 | // |
| 7284 | // char element_name[32]; |
| 7285 | // ::snprintf (element_name, sizeof (element_name), |
| 7286 | // "%s[%u]", parent_name ? parent_name : "", idx); |
| 7287 | // |
| 7288 | // child_name.assign(element_name); |
| 7289 | // assert(field_type_info.first % 8 == 0); |
| 7290 | // child_byte_size = field_type_info.first / 8; |
| 7291 | // child_byte_offset = idx * child_byte_size; |
| 7292 | // return array->getElementType().getAsOpaquePtr(); |
| 7293 | // } |
| 7294 | } break; |
| 7295 | |
| 7296 | // case clang::Type::MemberPointerType: |
| 7297 | // { |
| 7298 | // MemberPointerType *mem_ptr_type = |
| 7299 | // llvm::cast<MemberPointerType>(qual_type.getTypePtr()); |
| 7300 | // clang::QualType pointee_type = |
| 7301 | // mem_ptr_type->getPointeeType(); |
| 7302 | // |
| 7303 | // if (ClangASTContext::IsAggregateType |
| 7304 | // (pointee_type.getAsOpaquePtr())) |
| 7305 | // { |
| 7306 | // return GetIndexOfChildWithName (ast, |
| 7307 | // mem_ptr_type->getPointeeType().getAsOpaquePtr(), |
| 7308 | // name); |
| 7309 | // } |
| 7310 | // } |
| 7311 | // break; |
| 7312 | // |
| 7313 | case clang::Type::LValueReference: |
| 7314 | case clang::Type::RValueReference: { |
| 7315 | const clang::ReferenceType *reference_type = |
| 7316 | llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()); |
| 7317 | clang::QualType pointee_type(reference_type->getPointeeType()); |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 7318 | CompilerType pointee_clang_type(this, pointee_type.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7319 | |
| 7320 | if (pointee_clang_type.IsAggregateType()) { |
| 7321 | return pointee_clang_type.GetIndexOfChildMemberWithName( |
| 7322 | name, omit_empty_base_classes, child_indexes); |
| 7323 | } |
| 7324 | } break; |
| 7325 | |
| 7326 | case clang::Type::Pointer: { |
| 7327 | CompilerType pointee_clang_type(GetPointeeType(type)); |
| 7328 | |
| 7329 | if (pointee_clang_type.IsAggregateType()) { |
| 7330 | return pointee_clang_type.GetIndexOfChildMemberWithName( |
| 7331 | name, omit_empty_base_classes, child_indexes); |
| 7332 | } |
| 7333 | } break; |
| 7334 | |
| 7335 | case clang::Type::Typedef: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 7336 | return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type) |
| 7337 | ->getDecl() |
| 7338 | ->getUnderlyingType() |
| 7339 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7340 | .GetIndexOfChildMemberWithName(name, omit_empty_base_classes, |
| 7341 | child_indexes); |
| 7342 | |
| 7343 | case clang::Type::Auto: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 7344 | return CompilerType(this, llvm::cast<clang::AutoType>(qual_type) |
| 7345 | ->getDeducedType() |
| 7346 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7347 | .GetIndexOfChildMemberWithName(name, omit_empty_base_classes, |
| 7348 | child_indexes); |
| 7349 | |
| 7350 | case clang::Type::Elaborated: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 7351 | return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type) |
| 7352 | ->getNamedType() |
| 7353 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7354 | .GetIndexOfChildMemberWithName(name, omit_empty_base_classes, |
| 7355 | child_indexes); |
| 7356 | |
| 7357 | case clang::Type::Paren: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 7358 | return CompilerType(this, llvm::cast<clang::ParenType>(qual_type) |
| 7359 | ->desugar() |
| 7360 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7361 | .GetIndexOfChildMemberWithName(name, omit_empty_base_classes, |
| 7362 | child_indexes); |
| 7363 | |
| 7364 | default: |
| 7365 | break; |
| 7366 | } |
| 7367 | } |
| 7368 | return 0; |
| 7369 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7370 | |
| 7371 | // Get the index of the child of "clang_type" whose name matches. This function |
| 7372 | // doesn't descend into the children, but only looks one level deep and name |
| 7373 | // matches can include base class names. |
| 7374 | |
| 7375 | uint32_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7376 | ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type, |
| 7377 | const char *name, |
| 7378 | bool omit_empty_base_classes) { |
| 7379 | if (type && name && name[0]) { |
| 7380 | clang::QualType qual_type(GetCanonicalQualType(type)); |
Enrico Granata | 36f51e4 | 2015-12-18 22:41:25 +0000 | [diff] [blame] | 7381 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7382 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 7383 | |
| 7384 | switch (type_class) { |
| 7385 | case clang::Type::Record: |
| 7386 | if (GetCompleteType(type)) { |
| 7387 | const clang::RecordType *record_type = |
| 7388 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 7389 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 7390 | |
| 7391 | assert(record_decl); |
| 7392 | uint32_t child_idx = 0; |
| 7393 | |
| 7394 | const clang::CXXRecordDecl *cxx_record_decl = |
| 7395 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 7396 | |
| 7397 | if (cxx_record_decl) { |
| 7398 | clang::CXXRecordDecl::base_class_const_iterator base_class, |
| 7399 | base_class_end; |
| 7400 | for (base_class = cxx_record_decl->bases_begin(), |
| 7401 | base_class_end = cxx_record_decl->bases_end(); |
| 7402 | base_class != base_class_end; ++base_class) { |
| 7403 | // Skip empty base classes |
| 7404 | clang::CXXRecordDecl *base_class_decl = |
| 7405 | llvm::cast<clang::CXXRecordDecl>( |
| 7406 | base_class->getType() |
| 7407 | ->getAs<clang::RecordType>() |
| 7408 | ->getDecl()); |
| 7409 | if (omit_empty_base_classes && |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 7410 | !ClangASTContext::RecordHasFields(base_class_decl)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7411 | continue; |
| 7412 | |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 7413 | CompilerType base_class_clang_type( |
| 7414 | this, base_class->getType().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7415 | std::string base_class_type_name( |
| 7416 | base_class_clang_type.GetTypeName().AsCString("")); |
Jonas Devlieghere | 8d20cfd | 2018-12-21 22:46:10 +0000 | [diff] [blame] | 7417 | if (base_class_type_name == name) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7418 | return child_idx; |
| 7419 | ++child_idx; |
| 7420 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7421 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7422 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7423 | // Try and find a field that matches NAME |
| 7424 | clang::RecordDecl::field_iterator field, field_end; |
| 7425 | llvm::StringRef name_sref(name); |
| 7426 | for (field = record_decl->field_begin(), |
| 7427 | field_end = record_decl->field_end(); |
| 7428 | field != field_end; ++field, ++child_idx) { |
| 7429 | if (field->getName().equals(name_sref)) |
| 7430 | return child_idx; |
| 7431 | } |
| 7432 | } |
| 7433 | break; |
| 7434 | |
| 7435 | case clang::Type::ObjCObject: |
| 7436 | case clang::Type::ObjCInterface: |
| 7437 | if (GetCompleteType(type)) { |
| 7438 | llvm::StringRef name_sref(name); |
| 7439 | const clang::ObjCObjectType *objc_class_type = |
| 7440 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 7441 | assert(objc_class_type); |
| 7442 | if (objc_class_type) { |
| 7443 | uint32_t child_idx = 0; |
| 7444 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 7445 | objc_class_type->getInterface(); |
| 7446 | |
| 7447 | if (class_interface_decl) { |
| 7448 | clang::ObjCInterfaceDecl::ivar_iterator ivar_pos, |
| 7449 | ivar_end = class_interface_decl->ivar_end(); |
| 7450 | clang::ObjCInterfaceDecl *superclass_interface_decl = |
| 7451 | class_interface_decl->getSuperClass(); |
| 7452 | |
| 7453 | for (ivar_pos = class_interface_decl->ivar_begin(); |
| 7454 | ivar_pos != ivar_end; ++ivar_pos, ++child_idx) { |
| 7455 | const clang::ObjCIvarDecl *ivar_decl = *ivar_pos; |
| 7456 | |
| 7457 | if (ivar_decl->getName().equals(name_sref)) { |
| 7458 | if ((!omit_empty_base_classes && superclass_interface_decl) || |
| 7459 | (omit_empty_base_classes && |
| 7460 | ObjCDeclHasIVars(superclass_interface_decl, true))) |
| 7461 | ++child_idx; |
| 7462 | |
| 7463 | return child_idx; |
| 7464 | } |
| 7465 | } |
| 7466 | |
| 7467 | if (superclass_interface_decl) { |
| 7468 | if (superclass_interface_decl->getName().equals(name_sref)) |
| 7469 | return 0; |
| 7470 | } |
| 7471 | } |
| 7472 | } |
| 7473 | } |
| 7474 | break; |
| 7475 | |
| 7476 | case clang::Type::ObjCObjectPointer: { |
| 7477 | CompilerType pointee_clang_type( |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 7478 | this, llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr()) |
| 7479 | ->getPointeeType() |
| 7480 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7481 | return pointee_clang_type.GetIndexOfChildWithName( |
| 7482 | name, omit_empty_base_classes); |
| 7483 | } break; |
| 7484 | |
| 7485 | case clang::Type::ConstantArray: { |
| 7486 | // const clang::ConstantArrayType *array = |
| 7487 | // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr()); |
| 7488 | // const uint64_t element_count = |
| 7489 | // array->getSize().getLimitedValue(); |
| 7490 | // |
| 7491 | // if (idx < element_count) |
| 7492 | // { |
| 7493 | // std::pair<uint64_t, unsigned> field_type_info = |
| 7494 | // ast->getTypeInfo(array->getElementType()); |
| 7495 | // |
| 7496 | // char element_name[32]; |
| 7497 | // ::snprintf (element_name, sizeof (element_name), |
| 7498 | // "%s[%u]", parent_name ? parent_name : "", idx); |
| 7499 | // |
| 7500 | // child_name.assign(element_name); |
| 7501 | // assert(field_type_info.first % 8 == 0); |
| 7502 | // child_byte_size = field_type_info.first / 8; |
| 7503 | // child_byte_offset = idx * child_byte_size; |
| 7504 | // return array->getElementType().getAsOpaquePtr(); |
| 7505 | // } |
| 7506 | } break; |
| 7507 | |
| 7508 | // case clang::Type::MemberPointerType: |
| 7509 | // { |
| 7510 | // MemberPointerType *mem_ptr_type = |
| 7511 | // llvm::cast<MemberPointerType>(qual_type.getTypePtr()); |
| 7512 | // clang::QualType pointee_type = |
| 7513 | // mem_ptr_type->getPointeeType(); |
| 7514 | // |
| 7515 | // if (ClangASTContext::IsAggregateType |
| 7516 | // (pointee_type.getAsOpaquePtr())) |
| 7517 | // { |
| 7518 | // return GetIndexOfChildWithName (ast, |
| 7519 | // mem_ptr_type->getPointeeType().getAsOpaquePtr(), |
| 7520 | // name); |
| 7521 | // } |
| 7522 | // } |
| 7523 | // break; |
| 7524 | // |
| 7525 | case clang::Type::LValueReference: |
| 7526 | case clang::Type::RValueReference: { |
| 7527 | const clang::ReferenceType *reference_type = |
| 7528 | llvm::cast<clang::ReferenceType>(qual_type.getTypePtr()); |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 7529 | CompilerType pointee_type( |
| 7530 | this, reference_type->getPointeeType().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7531 | |
| 7532 | if (pointee_type.IsAggregateType()) { |
| 7533 | return pointee_type.GetIndexOfChildWithName(name, |
| 7534 | omit_empty_base_classes); |
| 7535 | } |
| 7536 | } break; |
| 7537 | |
| 7538 | case clang::Type::Pointer: { |
| 7539 | const clang::PointerType *pointer_type = |
| 7540 | llvm::cast<clang::PointerType>(qual_type.getTypePtr()); |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 7541 | CompilerType pointee_type( |
| 7542 | this, pointer_type->getPointeeType().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7543 | |
| 7544 | if (pointee_type.IsAggregateType()) { |
| 7545 | return pointee_type.GetIndexOfChildWithName(name, |
| 7546 | omit_empty_base_classes); |
| 7547 | } else { |
| 7548 | // if (parent_name) |
| 7549 | // { |
| 7550 | // child_name.assign(1, '*'); |
| 7551 | // child_name += parent_name; |
| 7552 | // } |
| 7553 | // |
| 7554 | // // We have a pointer to an simple type |
| 7555 | // if (idx == 0) |
| 7556 | // { |
| 7557 | // std::pair<uint64_t, unsigned> clang_type_info |
| 7558 | // = ast->getTypeInfo(pointee_type); |
| 7559 | // assert(clang_type_info.first % 8 == 0); |
| 7560 | // child_byte_size = clang_type_info.first / 8; |
| 7561 | // child_byte_offset = 0; |
| 7562 | // return pointee_type.getAsOpaquePtr(); |
| 7563 | // } |
| 7564 | } |
| 7565 | } break; |
| 7566 | |
| 7567 | case clang::Type::Auto: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 7568 | return CompilerType(this, llvm::cast<clang::AutoType>(qual_type) |
| 7569 | ->getDeducedType() |
| 7570 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7571 | .GetIndexOfChildWithName(name, omit_empty_base_classes); |
| 7572 | |
| 7573 | case clang::Type::Elaborated: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 7574 | return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type) |
| 7575 | ->getNamedType() |
| 7576 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7577 | .GetIndexOfChildWithName(name, omit_empty_base_classes); |
| 7578 | |
| 7579 | case clang::Type::Paren: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 7580 | return CompilerType(this, llvm::cast<clang::ParenType>(qual_type) |
| 7581 | ->desugar() |
| 7582 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7583 | .GetIndexOfChildWithName(name, omit_empty_base_classes); |
| 7584 | |
| 7585 | case clang::Type::Typedef: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 7586 | return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type) |
| 7587 | ->getDecl() |
| 7588 | ->getUnderlyingType() |
| 7589 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7590 | .GetIndexOfChildWithName(name, omit_empty_base_classes); |
| 7591 | |
| 7592 | default: |
| 7593 | break; |
| 7594 | } |
| 7595 | } |
| 7596 | return UINT32_MAX; |
| 7597 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7598 | |
| 7599 | size_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7600 | ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) { |
| 7601 | if (!type) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7602 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7603 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7604 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 7605 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 7606 | switch (type_class) { |
| 7607 | case clang::Type::Record: |
| 7608 | if (GetCompleteType(type)) { |
| 7609 | const clang::CXXRecordDecl *cxx_record_decl = |
| 7610 | qual_type->getAsCXXRecordDecl(); |
| 7611 | if (cxx_record_decl) { |
| 7612 | const clang::ClassTemplateSpecializationDecl *template_decl = |
| 7613 | llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>( |
| 7614 | cxx_record_decl); |
| 7615 | if (template_decl) |
| 7616 | return template_decl->getTemplateArgs().size(); |
| 7617 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7618 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7619 | break; |
| 7620 | |
| 7621 | case clang::Type::Typedef: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 7622 | return CompilerType(this, llvm::cast<clang::TypedefType>(qual_type) |
| 7623 | ->getDecl() |
| 7624 | ->getUnderlyingType() |
| 7625 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7626 | .GetNumTemplateArguments(); |
| 7627 | |
| 7628 | case clang::Type::Auto: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 7629 | return CompilerType(this, llvm::cast<clang::AutoType>(qual_type) |
| 7630 | ->getDeducedType() |
| 7631 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7632 | .GetNumTemplateArguments(); |
| 7633 | |
| 7634 | case clang::Type::Elaborated: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 7635 | return CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type) |
| 7636 | ->getNamedType() |
| 7637 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7638 | .GetNumTemplateArguments(); |
| 7639 | |
| 7640 | case clang::Type::Paren: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 7641 | return CompilerType(this, llvm::cast<clang::ParenType>(qual_type) |
| 7642 | ->desugar() |
| 7643 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7644 | .GetNumTemplateArguments(); |
| 7645 | |
| 7646 | default: |
| 7647 | break; |
| 7648 | } |
| 7649 | |
| 7650 | return 0; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7651 | } |
| 7652 | |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7653 | const clang::ClassTemplateSpecializationDecl * |
| 7654 | ClangASTContext::GetAsTemplateSpecialization( |
| 7655 | lldb::opaque_compiler_type_t type) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7656 | if (!type) |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7657 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7658 | |
| 7659 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 7660 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 7661 | switch (type_class) { |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7662 | case clang::Type::Record: { |
| 7663 | if (! GetCompleteType(type)) |
| 7664 | return nullptr; |
| 7665 | const clang::CXXRecordDecl *cxx_record_decl = |
| 7666 | qual_type->getAsCXXRecordDecl(); |
| 7667 | if (!cxx_record_decl) |
| 7668 | return nullptr; |
| 7669 | return llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>( |
| 7670 | cxx_record_decl); |
| 7671 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7672 | |
| 7673 | case clang::Type::Typedef: |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7674 | return GetAsTemplateSpecialization(llvm::cast<clang::TypedefType>(qual_type) |
| 7675 | ->getDecl() |
| 7676 | ->getUnderlyingType() |
| 7677 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7678 | |
| 7679 | case clang::Type::Auto: |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7680 | return GetAsTemplateSpecialization(llvm::cast<clang::AutoType>(qual_type) |
| 7681 | ->getDeducedType() |
| 7682 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7683 | |
| 7684 | case clang::Type::Elaborated: |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7685 | return GetAsTemplateSpecialization( |
| 7686 | llvm::cast<clang::ElaboratedType>(qual_type) |
| 7687 | ->getNamedType() |
| 7688 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7689 | |
| 7690 | case clang::Type::Paren: |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7691 | return GetAsTemplateSpecialization( |
| 7692 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7693 | |
| 7694 | default: |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7695 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7696 | } |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7697 | } |
| 7698 | |
| 7699 | lldb::TemplateArgumentKind |
| 7700 | ClangASTContext::GetTemplateArgumentKind(lldb::opaque_compiler_type_t type, |
| 7701 | size_t arg_idx) { |
| 7702 | const clang::ClassTemplateSpecializationDecl *template_decl = |
| 7703 | GetAsTemplateSpecialization(type); |
| 7704 | if (! template_decl || arg_idx >= template_decl->getTemplateArgs().size()) |
| 7705 | return eTemplateArgumentKindNull; |
| 7706 | |
| 7707 | switch (template_decl->getTemplateArgs()[arg_idx].getKind()) { |
| 7708 | case clang::TemplateArgument::Null: |
| 7709 | return eTemplateArgumentKindNull; |
| 7710 | |
| 7711 | case clang::TemplateArgument::NullPtr: |
| 7712 | return eTemplateArgumentKindNullPtr; |
| 7713 | |
| 7714 | case clang::TemplateArgument::Type: |
| 7715 | return eTemplateArgumentKindType; |
| 7716 | |
| 7717 | case clang::TemplateArgument::Declaration: |
| 7718 | return eTemplateArgumentKindDeclaration; |
| 7719 | |
| 7720 | case clang::TemplateArgument::Integral: |
| 7721 | return eTemplateArgumentKindIntegral; |
| 7722 | |
| 7723 | case clang::TemplateArgument::Template: |
| 7724 | return eTemplateArgumentKindTemplate; |
| 7725 | |
| 7726 | case clang::TemplateArgument::TemplateExpansion: |
| 7727 | return eTemplateArgumentKindTemplateExpansion; |
| 7728 | |
| 7729 | case clang::TemplateArgument::Expression: |
| 7730 | return eTemplateArgumentKindExpression; |
| 7731 | |
| 7732 | case clang::TemplateArgument::Pack: |
| 7733 | return eTemplateArgumentKindPack; |
| 7734 | } |
| 7735 | llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind"); |
| 7736 | } |
| 7737 | |
| 7738 | CompilerType |
| 7739 | ClangASTContext::GetTypeTemplateArgument(lldb::opaque_compiler_type_t type, |
| 7740 | size_t idx) { |
| 7741 | const clang::ClassTemplateSpecializationDecl *template_decl = |
| 7742 | GetAsTemplateSpecialization(type); |
| 7743 | if (!template_decl || idx >= template_decl->getTemplateArgs().size()) |
| 7744 | return CompilerType(); |
| 7745 | |
| 7746 | const clang::TemplateArgument &template_arg = |
| 7747 | template_decl->getTemplateArgs()[idx]; |
| 7748 | if (template_arg.getKind() != clang::TemplateArgument::Type) |
| 7749 | return CompilerType(); |
| 7750 | |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 7751 | return CompilerType(this, template_arg.getAsType().getAsOpaquePtr()); |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7752 | } |
| 7753 | |
Adrian Prantl | 2f1fa7a | 2019-01-15 21:04:19 +0000 | [diff] [blame] | 7754 | Optional<CompilerType::IntegralTemplateArgument> |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7755 | ClangASTContext::GetIntegralTemplateArgument(lldb::opaque_compiler_type_t type, |
| 7756 | size_t idx) { |
| 7757 | const clang::ClassTemplateSpecializationDecl *template_decl = |
| 7758 | GetAsTemplateSpecialization(type); |
| 7759 | if (! template_decl || idx >= template_decl->getTemplateArgs().size()) |
Pavel Labath | f59056f | 2017-11-30 10:16:54 +0000 | [diff] [blame] | 7760 | return llvm::None; |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7761 | |
| 7762 | const clang::TemplateArgument &template_arg = |
| 7763 | template_decl->getTemplateArgs()[idx]; |
| 7764 | if (template_arg.getKind() != clang::TemplateArgument::Integral) |
Pavel Labath | f59056f | 2017-11-30 10:16:54 +0000 | [diff] [blame] | 7765 | return llvm::None; |
Pavel Labath | 769b21e | 2017-11-13 14:26:21 +0000 | [diff] [blame] | 7766 | |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 7767 | return { |
| 7768 | {template_arg.getAsIntegral(), |
| 7769 | CompilerType(this, template_arg.getIntegralType().getAsOpaquePtr())}}; |
Enrico Granata | c6bf2e2 | 2015-09-23 01:39:46 +0000 | [diff] [blame] | 7770 | } |
| 7771 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7772 | CompilerType ClangASTContext::GetTypeForFormatters(void *type) { |
| 7773 | if (type) |
| 7774 | return ClangUtil::RemoveFastQualifiers(CompilerType(this, type)); |
| 7775 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7776 | } |
| 7777 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7778 | clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) { |
| 7779 | const clang::EnumType *enutype = |
| 7780 | llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type)); |
| 7781 | if (enutype) |
| 7782 | return enutype->getDecl(); |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 7783 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7784 | } |
| 7785 | |
| 7786 | clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) { |
| 7787 | const clang::RecordType *record_type = |
| 7788 | llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type)); |
| 7789 | if (record_type) |
| 7790 | return record_type->getDecl(); |
| 7791 | return nullptr; |
| 7792 | } |
| 7793 | |
| 7794 | clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) { |
Zachary Turner | 1639c6b | 2018-12-17 16:15:13 +0000 | [diff] [blame] | 7795 | return ClangUtil::GetAsTagDecl(type); |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 7796 | } |
| 7797 | |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 7798 | clang::TypedefNameDecl * |
| 7799 | ClangASTContext::GetAsTypedefDecl(const CompilerType &type) { |
| 7800 | const clang::TypedefType *typedef_type = |
| 7801 | llvm::dyn_cast<clang::TypedefType>(ClangUtil::GetQualType(type)); |
| 7802 | if (typedef_type) |
| 7803 | return typedef_type->getDecl(); |
| 7804 | return nullptr; |
| 7805 | } |
| 7806 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7807 | clang::CXXRecordDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7808 | ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) { |
| 7809 | return GetCanonicalQualType(type)->getAsCXXRecordDecl(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7810 | } |
| 7811 | |
| 7812 | clang::ObjCInterfaceDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7813 | ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) { |
| 7814 | const clang::ObjCObjectType *objc_class_type = |
| 7815 | llvm::dyn_cast<clang::ObjCObjectType>( |
| 7816 | ClangUtil::GetCanonicalQualType(type)); |
| 7817 | if (objc_class_type) |
| 7818 | return objc_class_type->getInterface(); |
| 7819 | return nullptr; |
| 7820 | } |
| 7821 | |
| 7822 | clang::FieldDecl *ClangASTContext::AddFieldToRecordType( |
Zachary Turner | a3e2ea1 | 2018-10-23 17:22:02 +0000 | [diff] [blame] | 7823 | const CompilerType &type, llvm::StringRef name, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7824 | const CompilerType &field_clang_type, AccessType access, |
| 7825 | uint32_t bitfield_bit_size) { |
| 7826 | if (!type.IsValid() || !field_clang_type.IsValid()) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7827 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7828 | ClangASTContext *ast = |
| 7829 | llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem()); |
| 7830 | if (!ast) |
| 7831 | return nullptr; |
| 7832 | clang::ASTContext *clang_ast = ast->getASTContext(); |
Zachary Turner | a3e2ea1 | 2018-10-23 17:22:02 +0000 | [diff] [blame] | 7833 | clang::IdentifierInfo *ident = nullptr; |
| 7834 | if (!name.empty()) |
| 7835 | ident = &clang_ast->Idents.get(name); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7836 | |
| 7837 | clang::FieldDecl *field = nullptr; |
| 7838 | |
| 7839 | clang::Expr *bit_width = nullptr; |
| 7840 | if (bitfield_bit_size != 0) { |
| 7841 | llvm::APInt bitfield_bit_size_apint( |
| 7842 | clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size); |
| 7843 | bit_width = new (*clang_ast) |
| 7844 | clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint, |
| 7845 | clang_ast->IntTy, clang::SourceLocation()); |
| 7846 | } |
| 7847 | |
| 7848 | clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type); |
| 7849 | if (record_decl) { |
| 7850 | field = clang::FieldDecl::Create( |
| 7851 | *clang_ast, record_decl, clang::SourceLocation(), |
| 7852 | clang::SourceLocation(), |
Zachary Turner | a3e2ea1 | 2018-10-23 17:22:02 +0000 | [diff] [blame] | 7853 | ident, // Identifier |
| 7854 | ClangUtil::GetQualType(field_clang_type), // Field type |
| 7855 | nullptr, // TInfo * |
| 7856 | bit_width, // BitWidth |
| 7857 | false, // Mutable |
| 7858 | clang::ICIS_NoInit); // HasInit |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7859 | |
Zachary Turner | a3e2ea1 | 2018-10-23 17:22:02 +0000 | [diff] [blame] | 7860 | if (name.empty()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 7861 | // Determine whether this field corresponds to an anonymous struct or |
| 7862 | // union. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7863 | if (const clang::TagType *TagT = |
| 7864 | field->getType()->getAs<clang::TagType>()) { |
| 7865 | if (clang::RecordDecl *Rec = |
| 7866 | llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl())) |
| 7867 | if (!Rec->getDeclName()) { |
| 7868 | Rec->setAnonymousStructOrUnion(true); |
| 7869 | field->setImplicit(); |
| 7870 | } |
| 7871 | } |
| 7872 | } |
| 7873 | |
| 7874 | if (field) { |
| 7875 | field->setAccess( |
| 7876 | ClangASTContext::ConvertAccessTypeToAccessSpecifier(access)); |
| 7877 | |
| 7878 | record_decl->addDecl(field); |
| 7879 | |
| 7880 | #ifdef LLDB_CONFIGURATION_DEBUG |
| 7881 | VerifyDecl(field); |
| 7882 | #endif |
| 7883 | } |
| 7884 | } else { |
| 7885 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 7886 | ast->GetAsObjCInterfaceDecl(type); |
| 7887 | |
| 7888 | if (class_interface_decl) { |
| 7889 | const bool is_synthesized = false; |
| 7890 | |
| 7891 | field_clang_type.GetCompleteType(); |
| 7892 | |
| 7893 | field = clang::ObjCIvarDecl::Create( |
| 7894 | *clang_ast, class_interface_decl, clang::SourceLocation(), |
| 7895 | clang::SourceLocation(), |
Zachary Turner | a3e2ea1 | 2018-10-23 17:22:02 +0000 | [diff] [blame] | 7896 | ident, // Identifier |
| 7897 | ClangUtil::GetQualType(field_clang_type), // Field type |
| 7898 | nullptr, // TypeSourceInfo * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7899 | ConvertAccessTypeToObjCIvarAccessControl(access), bit_width, |
| 7900 | is_synthesized); |
| 7901 | |
| 7902 | if (field) { |
| 7903 | class_interface_decl->addDecl(field); |
| 7904 | |
| 7905 | #ifdef LLDB_CONFIGURATION_DEBUG |
| 7906 | VerifyDecl(field); |
| 7907 | #endif |
| 7908 | } |
| 7909 | } |
| 7910 | } |
| 7911 | return field; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 7912 | } |
| 7913 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7914 | void ClangASTContext::BuildIndirectFields(const CompilerType &type) { |
| 7915 | if (!type) |
| 7916 | return; |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 7917 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7918 | ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); |
| 7919 | if (!ast) |
| 7920 | return; |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 7921 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7922 | clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type); |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 7923 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 7924 | if (!record_decl) |
| 7925 | return; |
| 7926 | |
| 7927 | typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector; |
| 7928 | |
| 7929 | IndirectFieldVector indirect_fields; |
| 7930 | clang::RecordDecl::field_iterator field_pos; |
| 7931 | clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end(); |
| 7932 | clang::RecordDecl::field_iterator last_field_pos = field_end_pos; |
| 7933 | for (field_pos = record_decl->field_begin(); field_pos != field_end_pos; |
| 7934 | last_field_pos = field_pos++) { |
| 7935 | if (field_pos->isAnonymousStructOrUnion()) { |
| 7936 | clang::QualType field_qual_type = field_pos->getType(); |
| 7937 | |
| 7938 | const clang::RecordType *field_record_type = |
| 7939 | field_qual_type->getAs<clang::RecordType>(); |
| 7940 | |
| 7941 | if (!field_record_type) |
| 7942 | continue; |
| 7943 | |
| 7944 | clang::RecordDecl *field_record_decl = field_record_type->getDecl(); |
| 7945 | |
| 7946 | if (!field_record_decl) |
| 7947 | continue; |
| 7948 | |
| 7949 | for (clang::RecordDecl::decl_iterator |
| 7950 | di = field_record_decl->decls_begin(), |
| 7951 | de = field_record_decl->decls_end(); |
| 7952 | di != de; ++di) { |
| 7953 | if (clang::FieldDecl *nested_field_decl = |
| 7954 | llvm::dyn_cast<clang::FieldDecl>(*di)) { |
| 7955 | clang::NamedDecl **chain = |
| 7956 | new (*ast->getASTContext()) clang::NamedDecl *[2]; |
| 7957 | chain[0] = *field_pos; |
| 7958 | chain[1] = nested_field_decl; |
| 7959 | clang::IndirectFieldDecl *indirect_field = |
| 7960 | clang::IndirectFieldDecl::Create( |
| 7961 | *ast->getASTContext(), record_decl, clang::SourceLocation(), |
| 7962 | nested_field_decl->getIdentifier(), |
| 7963 | nested_field_decl->getType(), {chain, 2}); |
| 7964 | |
| 7965 | indirect_field->setImplicit(); |
| 7966 | |
| 7967 | indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers( |
| 7968 | field_pos->getAccess(), nested_field_decl->getAccess())); |
| 7969 | |
| 7970 | indirect_fields.push_back(indirect_field); |
| 7971 | } else if (clang::IndirectFieldDecl *nested_indirect_field_decl = |
| 7972 | llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) { |
| 7973 | size_t nested_chain_size = |
| 7974 | nested_indirect_field_decl->getChainingSize(); |
| 7975 | clang::NamedDecl **chain = new (*ast->getASTContext()) |
| 7976 | clang::NamedDecl *[nested_chain_size + 1]; |
| 7977 | chain[0] = *field_pos; |
| 7978 | |
| 7979 | int chain_index = 1; |
| 7980 | for (clang::IndirectFieldDecl::chain_iterator |
| 7981 | nci = nested_indirect_field_decl->chain_begin(), |
| 7982 | nce = nested_indirect_field_decl->chain_end(); |
| 7983 | nci < nce; ++nci) { |
| 7984 | chain[chain_index] = *nci; |
| 7985 | chain_index++; |
| 7986 | } |
| 7987 | |
| 7988 | clang::IndirectFieldDecl *indirect_field = |
| 7989 | clang::IndirectFieldDecl::Create( |
| 7990 | *ast->getASTContext(), record_decl, clang::SourceLocation(), |
| 7991 | nested_indirect_field_decl->getIdentifier(), |
| 7992 | nested_indirect_field_decl->getType(), |
| 7993 | {chain, nested_chain_size + 1}); |
| 7994 | |
| 7995 | indirect_field->setImplicit(); |
| 7996 | |
| 7997 | indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers( |
| 7998 | field_pos->getAccess(), nested_indirect_field_decl->getAccess())); |
| 7999 | |
| 8000 | indirect_fields.push_back(indirect_field); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8001 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8002 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8003 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8004 | } |
| 8005 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 8006 | // Check the last field to see if it has an incomplete array type as its last |
| 8007 | // member and if it does, the tell the record decl about it |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8008 | if (last_field_pos != field_end_pos) { |
| 8009 | if (last_field_pos->getType()->isIncompleteArrayType()) |
| 8010 | record_decl->hasFlexibleArrayMember(); |
| 8011 | } |
| 8012 | |
| 8013 | for (IndirectFieldVector::iterator ifi = indirect_fields.begin(), |
| 8014 | ife = indirect_fields.end(); |
| 8015 | ifi < ife; ++ifi) { |
| 8016 | record_decl->addDecl(*ifi); |
| 8017 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8018 | } |
| 8019 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8020 | void ClangASTContext::SetIsPacked(const CompilerType &type) { |
| 8021 | if (type) { |
| 8022 | ClangASTContext *ast = |
| 8023 | llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); |
| 8024 | if (ast) { |
| 8025 | clang::RecordDecl *record_decl = GetAsRecordDecl(type); |
| 8026 | |
| 8027 | if (!record_decl) |
Greg Clayton | f73034f | 2015-09-08 18:15:05 +0000 | [diff] [blame] | 8028 | return; |
| 8029 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8030 | record_decl->addAttr( |
| 8031 | clang::PackedAttr::CreateImplicit(*ast->getASTContext())); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8032 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8033 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8034 | } |
| 8035 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8036 | clang::VarDecl *ClangASTContext::AddVariableToRecordType( |
Zachary Turner | a3e2ea1 | 2018-10-23 17:22:02 +0000 | [diff] [blame] | 8037 | const CompilerType &type, llvm::StringRef name, |
| 8038 | const CompilerType &var_type, AccessType access) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8039 | if (!type.IsValid() || !var_type.IsValid()) |
| 8040 | return nullptr; |
Zachary Turner | a3e2ea1 | 2018-10-23 17:22:02 +0000 | [diff] [blame] | 8041 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8042 | ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); |
| 8043 | if (!ast) |
| 8044 | return nullptr; |
| 8045 | |
| 8046 | clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type); |
Zachary Turner | a3e2ea1 | 2018-10-23 17:22:02 +0000 | [diff] [blame] | 8047 | if (!record_decl) |
| 8048 | return nullptr; |
| 8049 | |
| 8050 | clang::VarDecl *var_decl = nullptr; |
| 8051 | clang::IdentifierInfo *ident = nullptr; |
| 8052 | if (!name.empty()) |
| 8053 | ident = &ast->getASTContext()->Idents.get(name); |
| 8054 | |
| 8055 | var_decl = clang::VarDecl::Create( |
| 8056 | *ast->getASTContext(), // ASTContext & |
| 8057 | record_decl, // DeclContext * |
| 8058 | clang::SourceLocation(), // clang::SourceLocation StartLoc |
| 8059 | clang::SourceLocation(), // clang::SourceLocation IdLoc |
| 8060 | ident, // clang::IdentifierInfo * |
| 8061 | ClangUtil::GetQualType(var_type), // Variable clang::QualType |
| 8062 | nullptr, // TypeSourceInfo * |
| 8063 | clang::SC_Static); // StorageClass |
| 8064 | if (!var_decl) |
| 8065 | return nullptr; |
| 8066 | |
| 8067 | var_decl->setAccess( |
| 8068 | ClangASTContext::ConvertAccessTypeToAccessSpecifier(access)); |
| 8069 | record_decl->addDecl(var_decl); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8070 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8071 | #ifdef LLDB_CONFIGURATION_DEBUG |
Zachary Turner | a3e2ea1 | 2018-10-23 17:22:02 +0000 | [diff] [blame] | 8072 | VerifyDecl(var_decl); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8073 | #endif |
Zachary Turner | a3e2ea1 | 2018-10-23 17:22:02 +0000 | [diff] [blame] | 8074 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8075 | return var_decl; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8076 | } |
| 8077 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8078 | clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType( |
Davide Italiano | 675767a | 2018-03-27 19:40:50 +0000 | [diff] [blame] | 8079 | 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] | 8080 | const CompilerType &method_clang_type, lldb::AccessType access, |
| 8081 | bool is_virtual, bool is_static, bool is_inline, bool is_explicit, |
| 8082 | bool is_attr_used, bool is_artificial) { |
| 8083 | if (!type || !method_clang_type.IsValid() || name == nullptr || |
| 8084 | name[0] == '\0') |
| 8085 | return nullptr; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8086 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8087 | clang::QualType record_qual_type(GetCanonicalQualType(type)); |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 8088 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8089 | clang::CXXRecordDecl *cxx_record_decl = |
| 8090 | record_qual_type->getAsCXXRecordDecl(); |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 8091 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8092 | if (cxx_record_decl == nullptr) |
| 8093 | return nullptr; |
| 8094 | |
| 8095 | clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type)); |
| 8096 | |
| 8097 | clang::CXXMethodDecl *cxx_method_decl = nullptr; |
| 8098 | |
| 8099 | clang::DeclarationName decl_name(&getASTContext()->Idents.get(name)); |
| 8100 | |
| 8101 | const clang::FunctionType *function_type = |
| 8102 | llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr()); |
| 8103 | |
| 8104 | if (function_type == nullptr) |
| 8105 | return nullptr; |
| 8106 | |
| 8107 | const clang::FunctionProtoType *method_function_prototype( |
| 8108 | llvm::dyn_cast<clang::FunctionProtoType>(function_type)); |
| 8109 | |
| 8110 | if (!method_function_prototype) |
| 8111 | return nullptr; |
| 8112 | |
| 8113 | unsigned int num_params = method_function_prototype->getNumParams(); |
| 8114 | |
| 8115 | clang::CXXDestructorDecl *cxx_dtor_decl(nullptr); |
| 8116 | clang::CXXConstructorDecl *cxx_ctor_decl(nullptr); |
| 8117 | |
| 8118 | if (is_artificial) |
| 8119 | return nullptr; // skip everything artificial |
| 8120 | |
Richard Smith | 36851a6 | 2019-05-09 04:40:57 +0000 | [diff] [blame] | 8121 | const clang::ExplicitSpecifier explicit_spec( |
| 8122 | nullptr /*expr*/, is_explicit |
| 8123 | ? clang::ExplicitSpecKind::ResolvedTrue |
| 8124 | : clang::ExplicitSpecKind::ResolvedFalse); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8125 | if (name[0] == '~') { |
| 8126 | cxx_dtor_decl = clang::CXXDestructorDecl::Create( |
| 8127 | *getASTContext(), cxx_record_decl, clang::SourceLocation(), |
| 8128 | clang::DeclarationNameInfo( |
| 8129 | getASTContext()->DeclarationNames.getCXXDestructorName( |
| 8130 | getASTContext()->getCanonicalType(record_qual_type)), |
| 8131 | clang::SourceLocation()), |
Raphael Isemann | 15695cd | 2019-09-23 06:59:35 +0000 | [diff] [blame] | 8132 | method_qual_type, nullptr, is_inline, is_artificial, |
| 8133 | ConstexprSpecKind::CSK_unspecified); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8134 | cxx_method_decl = cxx_dtor_decl; |
| 8135 | } else if (decl_name == cxx_record_decl->getDeclName()) { |
| 8136 | cxx_ctor_decl = clang::CXXConstructorDecl::Create( |
| 8137 | *getASTContext(), cxx_record_decl, clang::SourceLocation(), |
| 8138 | clang::DeclarationNameInfo( |
| 8139 | getASTContext()->DeclarationNames.getCXXConstructorName( |
| 8140 | getASTContext()->getCanonicalType(record_qual_type)), |
| 8141 | clang::SourceLocation()), |
| 8142 | method_qual_type, |
| 8143 | nullptr, // TypeSourceInfo * |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 8144 | explicit_spec, is_inline, is_artificial, CSK_unspecified); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8145 | cxx_method_decl = cxx_ctor_decl; |
| 8146 | } else { |
| 8147 | clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None; |
| 8148 | clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS; |
| 8149 | |
| 8150 | if (IsOperator(name, op_kind)) { |
| 8151 | if (op_kind != clang::NUM_OVERLOADED_OPERATORS) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 8152 | // Check the number of operator parameters. Sometimes we have seen bad |
| 8153 | // DWARF that doesn't correctly describe operators and if we try to |
| 8154 | // create a method and add it to the class, clang will assert and |
| 8155 | // crash, so we need to make sure things are acceptable. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8156 | const bool is_method = true; |
| 8157 | if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount( |
| 8158 | is_method, op_kind, num_params)) |
| 8159 | return nullptr; |
| 8160 | cxx_method_decl = clang::CXXMethodDecl::Create( |
| 8161 | *getASTContext(), cxx_record_decl, clang::SourceLocation(), |
| 8162 | clang::DeclarationNameInfo( |
| 8163 | getASTContext()->DeclarationNames.getCXXOperatorName(op_kind), |
| 8164 | clang::SourceLocation()), |
| 8165 | method_qual_type, |
| 8166 | nullptr, // TypeSourceInfo * |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 8167 | SC, is_inline, CSK_unspecified, clang::SourceLocation()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8168 | } else if (num_params == 0) { |
| 8169 | // Conversion operators don't take params... |
| 8170 | cxx_method_decl = clang::CXXConversionDecl::Create( |
| 8171 | *getASTContext(), cxx_record_decl, clang::SourceLocation(), |
| 8172 | clang::DeclarationNameInfo( |
| 8173 | getASTContext()->DeclarationNames.getCXXConversionFunctionName( |
| 8174 | getASTContext()->getCanonicalType( |
| 8175 | function_type->getReturnType())), |
| 8176 | clang::SourceLocation()), |
| 8177 | method_qual_type, |
| 8178 | nullptr, // TypeSourceInfo * |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 8179 | is_inline, explicit_spec, CSK_unspecified, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8180 | clang::SourceLocation()); |
| 8181 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8182 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8183 | |
| 8184 | if (cxx_method_decl == nullptr) { |
| 8185 | cxx_method_decl = clang::CXXMethodDecl::Create( |
| 8186 | *getASTContext(), cxx_record_decl, clang::SourceLocation(), |
| 8187 | clang::DeclarationNameInfo(decl_name, clang::SourceLocation()), |
| 8188 | method_qual_type, |
| 8189 | nullptr, // TypeSourceInfo * |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 8190 | SC, is_inline, CSK_unspecified, clang::SourceLocation()); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8191 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8192 | } |
| 8193 | |
| 8194 | clang::AccessSpecifier access_specifier = |
| 8195 | ClangASTContext::ConvertAccessTypeToAccessSpecifier(access); |
| 8196 | |
| 8197 | cxx_method_decl->setAccess(access_specifier); |
| 8198 | cxx_method_decl->setVirtualAsWritten(is_virtual); |
| 8199 | |
| 8200 | if (is_attr_used) |
| 8201 | cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext())); |
| 8202 | |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 8203 | if (mangled_name != nullptr) { |
Vedant Kumar | f6bc251 | 2019-09-25 18:00:31 +0000 | [diff] [blame] | 8204 | cxx_method_decl->addAttr(clang::AsmLabelAttr::CreateImplicit( |
| 8205 | *getASTContext(), mangled_name, /*literal=*/false)); |
Davide Italiano | 675767a | 2018-03-27 19:40:50 +0000 | [diff] [blame] | 8206 | } |
| 8207 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8208 | // Populate the method decl with parameter decls |
| 8209 | |
| 8210 | llvm::SmallVector<clang::ParmVarDecl *, 12> params; |
| 8211 | |
| 8212 | for (unsigned param_index = 0; param_index < num_params; ++param_index) { |
| 8213 | params.push_back(clang::ParmVarDecl::Create( |
| 8214 | *getASTContext(), cxx_method_decl, clang::SourceLocation(), |
| 8215 | clang::SourceLocation(), |
| 8216 | nullptr, // anonymous |
| 8217 | method_function_prototype->getParamType(param_index), nullptr, |
| 8218 | clang::SC_None, nullptr)); |
| 8219 | } |
| 8220 | |
| 8221 | cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params)); |
| 8222 | |
| 8223 | cxx_record_decl->addDecl(cxx_method_decl); |
| 8224 | |
| 8225 | // Sometimes the debug info will mention a constructor (default/copy/move), |
| 8226 | // destructor, or assignment operator (copy/move) but there won't be any |
| 8227 | // version of this in the code. So we check if the function was artificially |
| 8228 | // generated and if it is trivial and this lets the compiler/backend know |
| 8229 | // that it can inline the IR for these when it needs to and we can avoid a |
| 8230 | // "missing function" error when running expressions. |
| 8231 | |
| 8232 | if (is_artificial) { |
| 8233 | if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() && |
| 8234 | cxx_record_decl->hasTrivialDefaultConstructor()) || |
| 8235 | (cxx_ctor_decl->isCopyConstructor() && |
| 8236 | cxx_record_decl->hasTrivialCopyConstructor()) || |
| 8237 | (cxx_ctor_decl->isMoveConstructor() && |
| 8238 | cxx_record_decl->hasTrivialMoveConstructor()))) { |
| 8239 | cxx_ctor_decl->setDefaulted(); |
| 8240 | cxx_ctor_decl->setTrivial(true); |
| 8241 | } else if (cxx_dtor_decl) { |
| 8242 | if (cxx_record_decl->hasTrivialDestructor()) { |
| 8243 | cxx_dtor_decl->setDefaulted(); |
| 8244 | cxx_dtor_decl->setTrivial(true); |
| 8245 | } |
| 8246 | } else if ((cxx_method_decl->isCopyAssignmentOperator() && |
| 8247 | cxx_record_decl->hasTrivialCopyAssignment()) || |
| 8248 | (cxx_method_decl->isMoveAssignmentOperator() && |
| 8249 | cxx_record_decl->hasTrivialMoveAssignment())) { |
| 8250 | cxx_method_decl->setDefaulted(); |
| 8251 | cxx_method_decl->setTrivial(true); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8252 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8253 | } |
| 8254 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8255 | #ifdef LLDB_CONFIGURATION_DEBUG |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8256 | VerifyDecl(cxx_method_decl); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8257 | #endif |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8258 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8259 | return cxx_method_decl; |
| 8260 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8261 | |
Aleksandr Urakov | 7d2a74f | 2018-08-14 07:57:44 +0000 | [diff] [blame] | 8262 | void ClangASTContext::AddMethodOverridesForCXXRecordType( |
| 8263 | lldb::opaque_compiler_type_t type) { |
| 8264 | if (auto *record = GetAsCXXRecordDecl(type)) |
| 8265 | for (auto *method : record->methods()) |
| 8266 | addOverridesForMethod(method); |
| 8267 | } |
| 8268 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8269 | #pragma mark C++ Base Classes |
| 8270 | |
Zachary Turner | 970f38e | 2018-10-25 20:44:56 +0000 | [diff] [blame] | 8271 | std::unique_ptr<clang::CXXBaseSpecifier> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8272 | ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type, |
| 8273 | AccessType access, bool is_virtual, |
| 8274 | bool base_of_class) { |
Zachary Turner | 970f38e | 2018-10-25 20:44:56 +0000 | [diff] [blame] | 8275 | if (!type) |
| 8276 | return nullptr; |
| 8277 | |
Jonas Devlieghere | a8f3ae7 | 2019-08-14 22:19:23 +0000 | [diff] [blame] | 8278 | return std::make_unique<clang::CXXBaseSpecifier>( |
Zachary Turner | 970f38e | 2018-10-25 20:44:56 +0000 | [diff] [blame] | 8279 | clang::SourceRange(), is_virtual, base_of_class, |
| 8280 | ClangASTContext::ConvertAccessTypeToAccessSpecifier(access), |
| 8281 | getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)), |
| 8282 | clang::SourceLocation()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8283 | } |
| 8284 | |
Zachary Turner | 970f38e | 2018-10-25 20:44:56 +0000 | [diff] [blame] | 8285 | bool ClangASTContext::TransferBaseClasses( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8286 | lldb::opaque_compiler_type_t type, |
Zachary Turner | 970f38e | 2018-10-25 20:44:56 +0000 | [diff] [blame] | 8287 | std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases) { |
| 8288 | if (!type) |
| 8289 | return false; |
| 8290 | clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type); |
| 8291 | if (!cxx_record_decl) |
| 8292 | return false; |
| 8293 | std::vector<clang::CXXBaseSpecifier *> raw_bases; |
| 8294 | raw_bases.reserve(bases.size()); |
| 8295 | |
| 8296 | // Clang will make a copy of them, so it's ok that we pass pointers that we're |
| 8297 | // about to destroy. |
| 8298 | for (auto &b : bases) |
| 8299 | raw_bases.push_back(b.get()); |
| 8300 | cxx_record_decl->setBases(raw_bases.data(), raw_bases.size()); |
| 8301 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8302 | } |
| 8303 | |
| 8304 | bool ClangASTContext::SetObjCSuperClass( |
| 8305 | const CompilerType &type, const CompilerType &superclass_clang_type) { |
| 8306 | ClangASTContext *ast = |
| 8307 | llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem()); |
| 8308 | if (!ast) |
| 8309 | return false; |
| 8310 | clang::ASTContext *clang_ast = ast->getASTContext(); |
| 8311 | |
| 8312 | if (type && superclass_clang_type.IsValid() && |
| 8313 | superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) { |
| 8314 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 8315 | GetAsObjCInterfaceDecl(type); |
| 8316 | clang::ObjCInterfaceDecl *super_interface_decl = |
| 8317 | GetAsObjCInterfaceDecl(superclass_clang_type); |
| 8318 | if (class_interface_decl && super_interface_decl) { |
| 8319 | class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo( |
| 8320 | clang_ast->getObjCInterfaceType(super_interface_decl))); |
| 8321 | return true; |
| 8322 | } |
| 8323 | } |
| 8324 | return false; |
| 8325 | } |
| 8326 | |
| 8327 | bool ClangASTContext::AddObjCClassProperty( |
| 8328 | const CompilerType &type, const char *property_name, |
| 8329 | const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl, |
| 8330 | const char *property_setter_name, const char *property_getter_name, |
| 8331 | uint32_t property_attributes, ClangASTMetadata *metadata) { |
| 8332 | if (!type || !property_clang_type.IsValid() || property_name == nullptr || |
| 8333 | property_name[0] == '\0') |
| 8334 | return false; |
| 8335 | ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); |
| 8336 | if (!ast) |
| 8337 | return false; |
| 8338 | clang::ASTContext *clang_ast = ast->getASTContext(); |
| 8339 | |
| 8340 | clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type); |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8341 | if (!class_interface_decl) |
| 8342 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8343 | |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8344 | CompilerType property_clang_type_to_access; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8345 | |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8346 | if (property_clang_type.IsValid()) |
| 8347 | property_clang_type_to_access = property_clang_type; |
| 8348 | else if (ivar_decl) |
| 8349 | property_clang_type_to_access = |
| 8350 | CompilerType(ast, ivar_decl->getType().getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8351 | |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8352 | if (!class_interface_decl || !property_clang_type_to_access.IsValid()) |
| 8353 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8354 | |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8355 | clang::TypeSourceInfo *prop_type_source; |
| 8356 | if (ivar_decl) |
| 8357 | prop_type_source = |
| 8358 | clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType()); |
| 8359 | else |
| 8360 | prop_type_source = clang_ast->getTrivialTypeSourceInfo( |
| 8361 | ClangUtil::GetQualType(property_clang_type)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8362 | |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8363 | clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create( |
| 8364 | *clang_ast, class_interface_decl, |
| 8365 | clang::SourceLocation(), // Source Location |
| 8366 | &clang_ast->Idents.get(property_name), |
| 8367 | clang::SourceLocation(), // Source Location for AT |
| 8368 | clang::SourceLocation(), // Source location for ( |
| 8369 | ivar_decl ? ivar_decl->getType() |
| 8370 | : ClangUtil::GetQualType(property_clang_type), |
| 8371 | prop_type_source); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8372 | |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8373 | if (!property_decl) |
| 8374 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8375 | |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8376 | if (metadata) |
| 8377 | ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8378 | |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8379 | class_interface_decl->addDecl(property_decl); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8380 | |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8381 | clang::Selector setter_sel, getter_sel; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8382 | |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8383 | if (property_setter_name) { |
| 8384 | std::string property_setter_no_colon(property_setter_name, |
| 8385 | strlen(property_setter_name) - 1); |
| 8386 | clang::IdentifierInfo *setter_ident = |
| 8387 | &clang_ast->Idents.get(property_setter_no_colon); |
| 8388 | setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident); |
| 8389 | } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) { |
| 8390 | std::string setter_sel_string("set"); |
| 8391 | setter_sel_string.push_back(::toupper(property_name[0])); |
| 8392 | setter_sel_string.append(&property_name[1]); |
| 8393 | clang::IdentifierInfo *setter_ident = |
| 8394 | &clang_ast->Idents.get(setter_sel_string); |
| 8395 | setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident); |
| 8396 | } |
| 8397 | property_decl->setSetterName(setter_sel); |
| 8398 | property_decl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_setter); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8399 | |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8400 | if (property_getter_name != nullptr) { |
| 8401 | clang::IdentifierInfo *getter_ident = |
| 8402 | &clang_ast->Idents.get(property_getter_name); |
| 8403 | getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident); |
| 8404 | } else { |
| 8405 | clang::IdentifierInfo *getter_ident = &clang_ast->Idents.get(property_name); |
| 8406 | getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident); |
| 8407 | } |
| 8408 | property_decl->setGetterName(getter_sel); |
| 8409 | property_decl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_getter); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8410 | |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8411 | if (ivar_decl) |
| 8412 | property_decl->setPropertyIvarDecl(ivar_decl); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8413 | |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8414 | if (property_attributes & DW_APPLE_PROPERTY_readonly) |
| 8415 | property_decl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readonly); |
| 8416 | if (property_attributes & DW_APPLE_PROPERTY_readwrite) |
| 8417 | property_decl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_readwrite); |
| 8418 | if (property_attributes & DW_APPLE_PROPERTY_assign) |
| 8419 | property_decl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_assign); |
| 8420 | if (property_attributes & DW_APPLE_PROPERTY_retain) |
| 8421 | property_decl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_retain); |
| 8422 | if (property_attributes & DW_APPLE_PROPERTY_copy) |
| 8423 | property_decl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_copy); |
| 8424 | if (property_attributes & DW_APPLE_PROPERTY_nonatomic) |
| 8425 | property_decl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nonatomic); |
| 8426 | if (property_attributes & ObjCPropertyDecl::OBJC_PR_nullability) |
| 8427 | property_decl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_nullability); |
| 8428 | if (property_attributes & ObjCPropertyDecl::OBJC_PR_null_resettable) |
| 8429 | property_decl->setPropertyAttributes( |
| 8430 | ObjCPropertyDecl::OBJC_PR_null_resettable); |
| 8431 | if (property_attributes & ObjCPropertyDecl::OBJC_PR_class) |
| 8432 | property_decl->setPropertyAttributes(ObjCPropertyDecl::OBJC_PR_class); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8433 | |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8434 | const bool isInstance = |
| 8435 | (property_attributes & ObjCPropertyDecl::OBJC_PR_class) == 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8436 | |
Adrian Prantl | c0eeea5 | 2019-11-21 17:21:49 -0800 | [diff] [blame] | 8437 | clang::ObjCMethodDecl *getter = nullptr; |
| 8438 | if (!getter_sel.isNull()) |
| 8439 | getter = isInstance ? class_interface_decl->lookupInstanceMethod(getter_sel) |
| 8440 | : class_interface_decl->lookupClassMethod(getter_sel); |
| 8441 | if (!getter_sel.isNull() && !getter) { |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8442 | const bool isVariadic = false; |
| 8443 | const bool isPropertyAccessor = false; |
| 8444 | const bool isSynthesizedAccessorStub = false; |
| 8445 | const bool isImplicitlyDeclared = true; |
| 8446 | const bool isDefined = false; |
| 8447 | const clang::ObjCMethodDecl::ImplementationControl impControl = |
| 8448 | clang::ObjCMethodDecl::None; |
| 8449 | const bool HasRelatedResultType = false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8450 | |
Adrian Prantl | c0eeea5 | 2019-11-21 17:21:49 -0800 | [diff] [blame] | 8451 | getter = clang::ObjCMethodDecl::Create( |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8452 | *clang_ast, clang::SourceLocation(), clang::SourceLocation(), |
| 8453 | getter_sel, ClangUtil::GetQualType(property_clang_type_to_access), |
| 8454 | nullptr, class_interface_decl, isInstance, isVariadic, |
| 8455 | isPropertyAccessor, isSynthesizedAccessorStub, isImplicitlyDeclared, |
| 8456 | isDefined, impControl, HasRelatedResultType); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8457 | |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8458 | if (getter) { |
Adrian Prantl | c0eeea5 | 2019-11-21 17:21:49 -0800 | [diff] [blame] | 8459 | if (metadata) |
| 8460 | ClangASTContext::SetMetadata(clang_ast, getter, *metadata); |
Adrian Prantl | 8b40bdb | 2019-11-22 10:03:25 -0800 | [diff] [blame] | 8461 | |
| 8462 | getter->setMethodParams(*clang_ast, |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8463 | llvm::ArrayRef<clang::ParmVarDecl *>(), |
| 8464 | llvm::ArrayRef<clang::SourceLocation>()); |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8465 | class_interface_decl->addDecl(getter); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8466 | } |
| 8467 | } |
Adrian Prantl | c0eeea5 | 2019-11-21 17:21:49 -0800 | [diff] [blame] | 8468 | if (getter) { |
| 8469 | getter->setPropertyAccessor(true); |
| 8470 | property_decl->setGetterMethodDecl(getter); |
| 8471 | } |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8472 | |
Adrian Prantl | c0eeea5 | 2019-11-21 17:21:49 -0800 | [diff] [blame] | 8473 | clang::ObjCMethodDecl *setter = nullptr; |
| 8474 | setter = isInstance ? class_interface_decl->lookupInstanceMethod(setter_sel) |
| 8475 | : class_interface_decl->lookupClassMethod(setter_sel); |
| 8476 | if (!setter_sel.isNull() && !setter) { |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8477 | clang::QualType result_type = clang_ast->VoidTy; |
| 8478 | const bool isVariadic = false; |
| 8479 | const bool isPropertyAccessor = true; |
| 8480 | const bool isSynthesizedAccessorStub = false; |
| 8481 | const bool isImplicitlyDeclared = true; |
| 8482 | const bool isDefined = false; |
| 8483 | const clang::ObjCMethodDecl::ImplementationControl impControl = |
| 8484 | clang::ObjCMethodDecl::None; |
| 8485 | const bool HasRelatedResultType = false; |
| 8486 | |
Adrian Prantl | c0eeea5 | 2019-11-21 17:21:49 -0800 | [diff] [blame] | 8487 | setter = clang::ObjCMethodDecl::Create( |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8488 | *clang_ast, clang::SourceLocation(), clang::SourceLocation(), |
| 8489 | setter_sel, result_type, nullptr, class_interface_decl, isInstance, |
| 8490 | isVariadic, isPropertyAccessor, isSynthesizedAccessorStub, |
| 8491 | isImplicitlyDeclared, isDefined, impControl, HasRelatedResultType); |
| 8492 | |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8493 | if (setter) { |
Adrian Prantl | c0eeea5 | 2019-11-21 17:21:49 -0800 | [diff] [blame] | 8494 | if (metadata) |
| 8495 | ClangASTContext::SetMetadata(clang_ast, setter, *metadata); |
| 8496 | |
| 8497 | llvm::SmallVector<clang::ParmVarDecl *, 1> params; |
Adrian Prantl | c0eeea5 | 2019-11-21 17:21:49 -0800 | [diff] [blame] | 8498 | params.push_back(clang::ParmVarDecl::Create( |
| 8499 | *clang_ast, setter, clang::SourceLocation(), clang::SourceLocation(), |
| 8500 | nullptr, // anonymous |
| 8501 | ClangUtil::GetQualType(property_clang_type_to_access), nullptr, |
| 8502 | clang::SC_Auto, nullptr)); |
| 8503 | |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8504 | setter->setMethodParams(*clang_ast, |
| 8505 | llvm::ArrayRef<clang::ParmVarDecl *>(params), |
| 8506 | llvm::ArrayRef<clang::SourceLocation>()); |
| 8507 | |
| 8508 | class_interface_decl->addDecl(setter); |
| 8509 | } |
| 8510 | } |
Adrian Prantl | c0eeea5 | 2019-11-21 17:21:49 -0800 | [diff] [blame] | 8511 | if (setter) { |
| 8512 | setter->setPropertyAccessor(true); |
| 8513 | property_decl->setSetterMethodDecl(setter); |
| 8514 | } |
Adrian Prantl | bc8e88e | 2019-11-21 15:40:50 -0800 | [diff] [blame] | 8515 | |
| 8516 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8517 | } |
| 8518 | |
| 8519 | bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type, |
| 8520 | bool check_superclass) { |
| 8521 | clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type); |
| 8522 | if (class_interface_decl) |
| 8523 | return ObjCDeclHasIVars(class_interface_decl, check_superclass); |
| 8524 | return false; |
| 8525 | } |
| 8526 | |
| 8527 | clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType( |
| 8528 | const CompilerType &type, |
| 8529 | const char *name, // the full symbol name as seen in the symbol table |
| 8530 | // (lldb::opaque_compiler_type_t type, "-[NString |
| 8531 | // stringWithCString:]") |
| 8532 | const CompilerType &method_clang_type, lldb::AccessType access, |
| 8533 | bool is_artificial, bool is_variadic) { |
| 8534 | if (!type || !method_clang_type.IsValid()) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8535 | return nullptr; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8536 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8537 | clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type); |
| 8538 | |
| 8539 | if (class_interface_decl == nullptr) |
| 8540 | return nullptr; |
| 8541 | ClangASTContext *lldb_ast = |
| 8542 | llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); |
| 8543 | if (lldb_ast == nullptr) |
| 8544 | return nullptr; |
| 8545 | clang::ASTContext *ast = lldb_ast->getASTContext(); |
| 8546 | |
| 8547 | const char *selector_start = ::strchr(name, ' '); |
| 8548 | if (selector_start == nullptr) |
| 8549 | return nullptr; |
| 8550 | |
| 8551 | selector_start++; |
| 8552 | llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents; |
| 8553 | |
| 8554 | size_t len = 0; |
| 8555 | const char *start; |
| 8556 | // printf ("name = '%s'\n", name); |
| 8557 | |
| 8558 | unsigned num_selectors_with_args = 0; |
| 8559 | for (start = selector_start; start && *start != '\0' && *start != ']'; |
| 8560 | start += len) { |
| 8561 | len = ::strcspn(start, ":]"); |
| 8562 | bool has_arg = (start[len] == ':'); |
| 8563 | if (has_arg) |
| 8564 | ++num_selectors_with_args; |
| 8565 | selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len))); |
| 8566 | if (has_arg) |
| 8567 | len += 1; |
| 8568 | } |
| 8569 | |
| 8570 | if (selector_idents.size() == 0) |
| 8571 | return nullptr; |
| 8572 | |
| 8573 | clang::Selector method_selector = ast->Selectors.getSelector( |
| 8574 | num_selectors_with_args ? selector_idents.size() : 0, |
| 8575 | selector_idents.data()); |
| 8576 | |
| 8577 | clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type)); |
| 8578 | |
| 8579 | // Populate the method decl with parameter decls |
| 8580 | const clang::Type *method_type(method_qual_type.getTypePtr()); |
| 8581 | |
| 8582 | if (method_type == nullptr) |
| 8583 | return nullptr; |
| 8584 | |
| 8585 | const clang::FunctionProtoType *method_function_prototype( |
| 8586 | llvm::dyn_cast<clang::FunctionProtoType>(method_type)); |
| 8587 | |
| 8588 | if (!method_function_prototype) |
| 8589 | return nullptr; |
| 8590 | |
Adrian Prantl | 454acae | 2019-11-08 08:58:50 -0800 | [diff] [blame] | 8591 | const bool isInstance = (name[0] == '-'); |
Adrian Prantl | 8204d9f | 2019-11-08 09:52:58 -0800 | [diff] [blame] | 8592 | const bool isVariadic = is_variadic; |
Adrian Prantl | 454acae | 2019-11-08 08:58:50 -0800 | [diff] [blame] | 8593 | const bool isPropertyAccessor = false; |
| 8594 | const bool isSynthesizedAccessorStub = false; |
| 8595 | /// Force this to true because we don't have source locations. |
| 8596 | const bool isImplicitlyDeclared = true; |
| 8597 | const bool isDefined = false; |
| 8598 | const clang::ObjCMethodDecl::ImplementationControl impControl = |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8599 | clang::ObjCMethodDecl::None; |
Adrian Prantl | 454acae | 2019-11-08 08:58:50 -0800 | [diff] [blame] | 8600 | const bool HasRelatedResultType = false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8601 | |
| 8602 | const unsigned num_args = method_function_prototype->getNumParams(); |
| 8603 | |
| 8604 | if (num_args != num_selectors_with_args) |
| 8605 | return nullptr; // some debug information is corrupt. We are not going to |
| 8606 | // deal with it. |
| 8607 | |
| 8608 | clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create( |
| 8609 | *ast, |
| 8610 | clang::SourceLocation(), // beginLoc, |
| 8611 | clang::SourceLocation(), // endLoc, |
| 8612 | method_selector, method_function_prototype->getReturnType(), |
| 8613 | nullptr, // TypeSourceInfo *ResultTInfo, |
| 8614 | ClangASTContext::GetASTContext(ast)->GetDeclContextForType( |
| 8615 | ClangUtil::GetQualType(type)), |
Adrian Prantl | 454acae | 2019-11-08 08:58:50 -0800 | [diff] [blame] | 8616 | isInstance, isVariadic, isPropertyAccessor, isSynthesizedAccessorStub, |
| 8617 | isImplicitlyDeclared, isDefined, impControl, HasRelatedResultType); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8618 | |
| 8619 | if (objc_method_decl == nullptr) |
| 8620 | return nullptr; |
| 8621 | |
| 8622 | if (num_args > 0) { |
| 8623 | llvm::SmallVector<clang::ParmVarDecl *, 12> params; |
| 8624 | |
| 8625 | for (unsigned param_index = 0; param_index < num_args; ++param_index) { |
| 8626 | params.push_back(clang::ParmVarDecl::Create( |
| 8627 | *ast, objc_method_decl, clang::SourceLocation(), |
| 8628 | clang::SourceLocation(), |
| 8629 | nullptr, // anonymous |
| 8630 | method_function_prototype->getParamType(param_index), nullptr, |
| 8631 | clang::SC_Auto, nullptr)); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8632 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8633 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8634 | objc_method_decl->setMethodParams( |
| 8635 | *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params), |
| 8636 | llvm::ArrayRef<clang::SourceLocation>()); |
| 8637 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8638 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8639 | class_interface_decl->addDecl(objc_method_decl); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8640 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8641 | #ifdef LLDB_CONFIGURATION_DEBUG |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8642 | VerifyDecl(objc_method_decl); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8643 | #endif |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8644 | |
| 8645 | return objc_method_decl; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8646 | } |
| 8647 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8648 | bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type, |
| 8649 | bool has_extern) { |
| 8650 | if (!type) |
| 8651 | return false; |
| 8652 | |
| 8653 | clang::QualType qual_type(GetCanonicalQualType(type)); |
| 8654 | |
| 8655 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 8656 | switch (type_class) { |
| 8657 | case clang::Type::Record: { |
| 8658 | clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl(); |
| 8659 | if (cxx_record_decl) { |
| 8660 | cxx_record_decl->setHasExternalLexicalStorage(has_extern); |
| 8661 | cxx_record_decl->setHasExternalVisibleStorage(has_extern); |
| 8662 | return true; |
| 8663 | } |
| 8664 | } break; |
| 8665 | |
| 8666 | case clang::Type::Enum: { |
| 8667 | clang::EnumDecl *enum_decl = |
| 8668 | llvm::cast<clang::EnumType>(qual_type)->getDecl(); |
| 8669 | if (enum_decl) { |
| 8670 | enum_decl->setHasExternalLexicalStorage(has_extern); |
| 8671 | enum_decl->setHasExternalVisibleStorage(has_extern); |
| 8672 | return true; |
| 8673 | } |
| 8674 | } break; |
| 8675 | |
| 8676 | case clang::Type::ObjCObject: |
| 8677 | case clang::Type::ObjCInterface: { |
| 8678 | const clang::ObjCObjectType *objc_class_type = |
| 8679 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 8680 | assert(objc_class_type); |
| 8681 | if (objc_class_type) { |
| 8682 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 8683 | objc_class_type->getInterface(); |
| 8684 | |
| 8685 | if (class_interface_decl) { |
| 8686 | class_interface_decl->setHasExternalLexicalStorage(has_extern); |
| 8687 | class_interface_decl->setHasExternalVisibleStorage(has_extern); |
| 8688 | return true; |
| 8689 | } |
| 8690 | } |
| 8691 | } break; |
| 8692 | |
| 8693 | case clang::Type::Typedef: |
| 8694 | return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type) |
| 8695 | ->getDecl() |
| 8696 | ->getUnderlyingType() |
| 8697 | .getAsOpaquePtr(), |
| 8698 | has_extern); |
| 8699 | |
| 8700 | case clang::Type::Auto: |
| 8701 | return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type) |
| 8702 | ->getDeducedType() |
| 8703 | .getAsOpaquePtr(), |
| 8704 | has_extern); |
| 8705 | |
| 8706 | case clang::Type::Elaborated: |
| 8707 | return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type) |
| 8708 | ->getNamedType() |
| 8709 | .getAsOpaquePtr(), |
| 8710 | has_extern); |
| 8711 | |
| 8712 | case clang::Type::Paren: |
| 8713 | return SetHasExternalStorage( |
| 8714 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(), |
| 8715 | has_extern); |
| 8716 | |
| 8717 | default: |
| 8718 | break; |
| 8719 | } |
| 8720 | return false; |
| 8721 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8722 | |
| 8723 | #pragma mark TagDecl |
| 8724 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8725 | bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) { |
| 8726 | clang::QualType qual_type(ClangUtil::GetQualType(type)); |
| 8727 | if (!qual_type.isNull()) { |
| 8728 | const clang::TagType *tag_type = qual_type->getAs<clang::TagType>(); |
| 8729 | if (tag_type) { |
| 8730 | clang::TagDecl *tag_decl = tag_type->getDecl(); |
| 8731 | if (tag_decl) { |
| 8732 | tag_decl->startDefinition(); |
| 8733 | return true; |
| 8734 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8735 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8736 | |
| 8737 | const clang::ObjCObjectType *object_type = |
| 8738 | qual_type->getAs<clang::ObjCObjectType>(); |
| 8739 | if (object_type) { |
| 8740 | clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface(); |
| 8741 | if (interface_decl) { |
| 8742 | interface_decl->startDefinition(); |
| 8743 | return true; |
| 8744 | } |
| 8745 | } |
| 8746 | } |
| 8747 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8748 | } |
| 8749 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8750 | bool ClangASTContext::CompleteTagDeclarationDefinition( |
| 8751 | const CompilerType &type) { |
| 8752 | clang::QualType qual_type(ClangUtil::GetQualType(type)); |
| 8753 | if (!qual_type.isNull()) { |
| 8754 | // Make sure we use the same methodology as |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 8755 | // ClangASTContext::StartTagDeclarationDefinition() as to how we start/end |
| 8756 | // the definition. Previously we were calling |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8757 | const clang::TagType *tag_type = qual_type->getAs<clang::TagType>(); |
| 8758 | if (tag_type) { |
| 8759 | clang::TagDecl *tag_decl = tag_type->getDecl(); |
| 8760 | if (tag_decl) { |
| 8761 | clang::CXXRecordDecl *cxx_record_decl = |
| 8762 | llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl); |
| 8763 | |
| 8764 | if (cxx_record_decl) { |
| 8765 | if (!cxx_record_decl->isCompleteDefinition()) |
| 8766 | cxx_record_decl->completeDefinition(); |
| 8767 | cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true); |
| 8768 | cxx_record_decl->setHasExternalLexicalStorage(false); |
| 8769 | cxx_record_decl->setHasExternalVisibleStorage(false); |
| 8770 | return true; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8771 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8772 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8773 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8774 | |
| 8775 | const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>(); |
| 8776 | |
| 8777 | if (enutype) { |
| 8778 | clang::EnumDecl *enum_decl = enutype->getDecl(); |
| 8779 | |
| 8780 | if (enum_decl) { |
| 8781 | if (!enum_decl->isCompleteDefinition()) { |
| 8782 | ClangASTContext *lldb_ast = |
| 8783 | llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); |
| 8784 | if (lldb_ast == nullptr) |
| 8785 | return false; |
| 8786 | clang::ASTContext *ast = lldb_ast->getASTContext(); |
| 8787 | |
| 8788 | /// TODO This really needs to be fixed. |
| 8789 | |
| 8790 | QualType integer_type(enum_decl->getIntegerType()); |
| 8791 | if (!integer_type.isNull()) { |
| 8792 | unsigned NumPositiveBits = 1; |
| 8793 | unsigned NumNegativeBits = 0; |
| 8794 | |
| 8795 | clang::QualType promotion_qual_type; |
| 8796 | // If the enum integer type is less than an integer in bit width, |
| 8797 | // then we must promote it to an integer size. |
| 8798 | if (ast->getTypeSize(enum_decl->getIntegerType()) < |
| 8799 | ast->getTypeSize(ast->IntTy)) { |
| 8800 | if (enum_decl->getIntegerType()->isSignedIntegerType()) |
| 8801 | promotion_qual_type = ast->IntTy; |
| 8802 | else |
| 8803 | promotion_qual_type = ast->UnsignedIntTy; |
| 8804 | } else |
| 8805 | promotion_qual_type = enum_decl->getIntegerType(); |
| 8806 | |
| 8807 | enum_decl->completeDefinition(enum_decl->getIntegerType(), |
| 8808 | promotion_qual_type, NumPositiveBits, |
| 8809 | NumNegativeBits); |
| 8810 | } |
| 8811 | } |
| 8812 | return true; |
| 8813 | } |
| 8814 | } |
| 8815 | } |
| 8816 | return false; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8817 | } |
| 8818 | |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 8819 | clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType( |
Shafik Yaghmour | 8c5ec1f | 2018-11-08 18:42:00 +0000 | [diff] [blame] | 8820 | const CompilerType &enum_type, const Declaration &decl, const char *name, |
Zachary Turner | 1639c6b | 2018-12-17 16:15:13 +0000 | [diff] [blame] | 8821 | const llvm::APSInt &value) { |
Zachary Turner | d133f6a | 2016-03-28 22:53:41 +0000 | [diff] [blame] | 8822 | |
Shafik Yaghmour | 8c5ec1f | 2018-11-08 18:42:00 +0000 | [diff] [blame] | 8823 | if (!enum_type || ConstString(name).IsEmpty()) |
| 8824 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8825 | |
Shafik Yaghmour | 8c5ec1f | 2018-11-08 18:42:00 +0000 | [diff] [blame] | 8826 | lldbassert(enum_type.GetTypeSystem() == static_cast<TypeSystem *>(this)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8827 | |
Shafik Yaghmour | 8c5ec1f | 2018-11-08 18:42:00 +0000 | [diff] [blame] | 8828 | lldb::opaque_compiler_type_t enum_opaque_compiler_type = |
| 8829 | enum_type.GetOpaqueQualType(); |
| 8830 | |
| 8831 | if (!enum_opaque_compiler_type) |
| 8832 | return nullptr; |
| 8833 | |
Shafik Yaghmour | 8c5ec1f | 2018-11-08 18:42:00 +0000 | [diff] [blame] | 8834 | clang::QualType enum_qual_type( |
| 8835 | GetCanonicalQualType(enum_opaque_compiler_type)); |
| 8836 | |
Shafik Yaghmour | 8c5ec1f | 2018-11-08 18:42:00 +0000 | [diff] [blame] | 8837 | const clang::Type *clang_type = enum_qual_type.getTypePtr(); |
| 8838 | |
| 8839 | if (!clang_type) |
| 8840 | return nullptr; |
| 8841 | |
| 8842 | const clang::EnumType *enutype = llvm::dyn_cast<clang::EnumType>(clang_type); |
| 8843 | |
| 8844 | if (!enutype) |
| 8845 | return nullptr; |
| 8846 | |
Shafik Yaghmour | 8c5ec1f | 2018-11-08 18:42:00 +0000 | [diff] [blame] | 8847 | clang::EnumConstantDecl *enumerator_decl = clang::EnumConstantDecl::Create( |
| 8848 | *getASTContext(), enutype->getDecl(), clang::SourceLocation(), |
| 8849 | name ? &getASTContext()->Idents.get(name) : nullptr, // Identifier |
Zachary Turner | 1639c6b | 2018-12-17 16:15:13 +0000 | [diff] [blame] | 8850 | clang::QualType(enutype, 0), nullptr, value); |
Shafik Yaghmour | 8c5ec1f | 2018-11-08 18:42:00 +0000 | [diff] [blame] | 8851 | |
| 8852 | if (!enumerator_decl) |
| 8853 | return nullptr; |
| 8854 | |
| 8855 | enutype->getDecl()->addDecl(enumerator_decl); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8856 | |
| 8857 | #ifdef LLDB_CONFIGURATION_DEBUG |
Shafik Yaghmour | 8c5ec1f | 2018-11-08 18:42:00 +0000 | [diff] [blame] | 8858 | VerifyDecl(enumerator_decl); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8859 | #endif |
| 8860 | |
Shafik Yaghmour | 8c5ec1f | 2018-11-08 18:42:00 +0000 | [diff] [blame] | 8861 | return enumerator_decl; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8862 | } |
| 8863 | |
Zachary Turner | 1639c6b | 2018-12-17 16:15:13 +0000 | [diff] [blame] | 8864 | clang::EnumConstantDecl *ClangASTContext::AddEnumerationValueToEnumerationType( |
| 8865 | const CompilerType &enum_type, const Declaration &decl, const char *name, |
| 8866 | int64_t enum_value, uint32_t enum_value_bit_size) { |
| 8867 | CompilerType underlying_type = |
| 8868 | GetEnumerationIntegerType(enum_type.GetOpaqueQualType()); |
| 8869 | bool is_signed = false; |
| 8870 | underlying_type.IsIntegerType(is_signed); |
| 8871 | |
| 8872 | llvm::APSInt value(enum_value_bit_size, is_signed); |
| 8873 | value = enum_value; |
| 8874 | |
| 8875 | return AddEnumerationValueToEnumerationType(enum_type, decl, name, value); |
| 8876 | } |
| 8877 | |
Greg Clayton | a1e5dc8 | 2015-08-11 22:53:00 +0000 | [diff] [blame] | 8878 | CompilerType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8879 | ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) { |
| 8880 | clang::QualType enum_qual_type(GetCanonicalQualType(type)); |
| 8881 | const clang::Type *clang_type = enum_qual_type.getTypePtr(); |
| 8882 | if (clang_type) { |
| 8883 | const clang::EnumType *enutype = |
| 8884 | llvm::dyn_cast<clang::EnumType>(clang_type); |
| 8885 | if (enutype) { |
| 8886 | clang::EnumDecl *enum_decl = enutype->getDecl(); |
| 8887 | if (enum_decl) |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 8888 | return CompilerType(this, enum_decl->getIntegerType().getAsOpaquePtr()); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8889 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8890 | } |
| 8891 | return CompilerType(); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8892 | } |
| 8893 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8894 | CompilerType |
| 8895 | ClangASTContext::CreateMemberPointerType(const CompilerType &type, |
| 8896 | const CompilerType &pointee_type) { |
| 8897 | if (type && pointee_type.IsValid() && |
| 8898 | type.GetTypeSystem() == pointee_type.GetTypeSystem()) { |
| 8899 | ClangASTContext *ast = |
| 8900 | llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem()); |
| 8901 | if (!ast) |
| 8902 | return CompilerType(); |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 8903 | return CompilerType(ast, ast->getASTContext() |
| 8904 | ->getMemberPointerType( |
| 8905 | ClangUtil::GetQualType(pointee_type), |
| 8906 | ClangUtil::GetQualType(type).getTypePtr()) |
| 8907 | .getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8908 | } |
| 8909 | return CompilerType(); |
| 8910 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8911 | |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8912 | // Dumping types |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 8913 | #define DEPTH_INCREMENT 2 |
| 8914 | |
Adrian Prantl | 0c72a42 | 2019-03-07 20:20:02 +0000 | [diff] [blame] | 8915 | #ifndef NDEBUG |
| 8916 | LLVM_DUMP_METHOD void |
| 8917 | ClangASTContext::dump(lldb::opaque_compiler_type_t type) const { |
| 8918 | if (!type) |
| 8919 | return; |
| 8920 | clang::QualType qual_type(GetQualType(type)); |
| 8921 | qual_type.dump(); |
| 8922 | } |
| 8923 | #endif |
| 8924 | |
Zachary Turner | 4911023 | 2018-11-05 17:40:28 +0000 | [diff] [blame] | 8925 | void ClangASTContext::Dump(Stream &s) { |
Zachary Turner | 115209e | 2018-11-05 19:25:39 +0000 | [diff] [blame] | 8926 | Decl *tu = Decl::castFromDeclContext(GetTranslationUnitDecl()); |
Zachary Turner | 4911023 | 2018-11-05 17:40:28 +0000 | [diff] [blame] | 8927 | tu->dump(s.AsRawOstream()); |
| 8928 | } |
| 8929 | |
Shafik Yaghmour | 5f46982 | 2019-10-11 16:36:20 +0000 | [diff] [blame] | 8930 | void ClangASTContext::DumpFromSymbolFile(Stream &s, |
| 8931 | llvm::StringRef symbol_name) { |
| 8932 | SymbolFile *symfile = GetSymbolFile(); |
| 8933 | |
| 8934 | if (!symfile) |
| 8935 | return; |
| 8936 | |
| 8937 | lldb_private::TypeList type_list; |
| 8938 | symfile->GetTypes(nullptr, eTypeClassAny, type_list); |
| 8939 | size_t ntypes = type_list.GetSize(); |
| 8940 | |
| 8941 | for (size_t i = 0; i < ntypes; ++i) { |
| 8942 | TypeSP type = type_list.GetTypeAtIndex(i); |
| 8943 | |
| 8944 | if (!symbol_name.empty()) |
shafik | de2c7ca | 2019-10-28 14:26:54 -0700 | [diff] [blame] | 8945 | if (symbol_name != type->GetName().GetStringRef()) |
Shafik Yaghmour | 5f46982 | 2019-10-11 16:36:20 +0000 | [diff] [blame] | 8946 | continue; |
| 8947 | |
| 8948 | s << type->GetName().AsCString() << "\n"; |
| 8949 | |
Adrian Prantl | c0eeea5 | 2019-11-21 17:21:49 -0800 | [diff] [blame] | 8950 | CompilerType full_type = type->GetFullCompilerType(); |
| 8951 | if (clang::TagDecl *tag_decl = GetAsTagDecl(full_type)) { |
Shafik Yaghmour | 5f46982 | 2019-10-11 16:36:20 +0000 | [diff] [blame] | 8952 | tag_decl->dump(s.AsRawOstream()); |
Adrian Prantl | c0eeea5 | 2019-11-21 17:21:49 -0800 | [diff] [blame] | 8953 | continue; |
Shafik Yaghmour | 5f46982 | 2019-10-11 16:36:20 +0000 | [diff] [blame] | 8954 | } |
Adrian Prantl | c0eeea5 | 2019-11-21 17:21:49 -0800 | [diff] [blame] | 8955 | if (clang::TypedefNameDecl *typedef_decl = GetAsTypedefDecl(full_type)) { |
| 8956 | typedef_decl->dump(s.AsRawOstream()); |
| 8957 | continue; |
| 8958 | } |
| 8959 | if (auto *objc_obj = llvm::dyn_cast<clang::ObjCObjectType>( |
| 8960 | ClangUtil::GetQualType(full_type).getTypePtr())) { |
| 8961 | if (clang::ObjCInterfaceDecl *interface_decl = objc_obj->getInterface()) { |
| 8962 | interface_decl->dump(s.AsRawOstream()); |
| 8963 | continue; |
| 8964 | } |
| 8965 | } |
| 8966 | GetCanonicalQualType(full_type.GetOpaqueQualType()).dump(s.AsRawOstream()); |
Shafik Yaghmour | 5f46982 | 2019-10-11 16:36:20 +0000 | [diff] [blame] | 8967 | } |
| 8968 | } |
| 8969 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8970 | void ClangASTContext::DumpValue( |
| 8971 | lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s, |
Zachary Turner | 29cb868 | 2017-03-03 20:57:05 +0000 | [diff] [blame] | 8972 | lldb::Format format, const DataExtractor &data, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 8973 | lldb::offset_t data_byte_offset, size_t data_byte_size, |
| 8974 | uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types, |
| 8975 | bool show_summary, bool verbose, uint32_t depth) { |
| 8976 | if (!type) |
| 8977 | return; |
| 8978 | |
| 8979 | clang::QualType qual_type(GetQualType(type)); |
| 8980 | switch (qual_type->getTypeClass()) { |
| 8981 | case clang::Type::Record: |
| 8982 | if (GetCompleteType(type)) { |
| 8983 | const clang::RecordType *record_type = |
| 8984 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 8985 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 8986 | assert(record_decl); |
| 8987 | uint32_t field_bit_offset = 0; |
| 8988 | uint32_t field_byte_offset = 0; |
| 8989 | const clang::ASTRecordLayout &record_layout = |
| 8990 | getASTContext()->getASTRecordLayout(record_decl); |
| 8991 | uint32_t child_idx = 0; |
| 8992 | |
| 8993 | const clang::CXXRecordDecl *cxx_record_decl = |
| 8994 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 8995 | if (cxx_record_decl) { |
| 8996 | // We might have base classes to print out first |
| 8997 | clang::CXXRecordDecl::base_class_const_iterator base_class, |
| 8998 | base_class_end; |
| 8999 | for (base_class = cxx_record_decl->bases_begin(), |
| 9000 | base_class_end = cxx_record_decl->bases_end(); |
| 9001 | base_class != base_class_end; ++base_class) { |
| 9002 | const clang::CXXRecordDecl *base_class_decl = |
| 9003 | llvm::cast<clang::CXXRecordDecl>( |
| 9004 | base_class->getType()->getAs<clang::RecordType>()->getDecl()); |
| 9005 | |
| 9006 | // Skip empty base classes |
Jonas Devlieghere | a6682a4 | 2018-12-15 00:15:33 +0000 | [diff] [blame] | 9007 | if (!verbose && !ClangASTContext::RecordHasFields(base_class_decl)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9008 | continue; |
| 9009 | |
| 9010 | if (base_class->isVirtual()) |
| 9011 | field_bit_offset = |
| 9012 | record_layout.getVBaseClassOffset(base_class_decl) |
| 9013 | .getQuantity() * |
| 9014 | 8; |
| 9015 | else |
| 9016 | field_bit_offset = record_layout.getBaseClassOffset(base_class_decl) |
| 9017 | .getQuantity() * |
| 9018 | 8; |
| 9019 | field_byte_offset = field_bit_offset / 8; |
| 9020 | assert(field_bit_offset % 8 == 0); |
| 9021 | if (child_idx == 0) |
| 9022 | s->PutChar('{'); |
| 9023 | else |
| 9024 | s->PutChar(','); |
| 9025 | |
| 9026 | clang::QualType base_class_qual_type = base_class->getType(); |
| 9027 | std::string base_class_type_name(base_class_qual_type.getAsString()); |
| 9028 | |
| 9029 | // Indent and print the base class type name |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 9030 | s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT), |
| 9031 | base_class_type_name); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9032 | |
| 9033 | clang::TypeInfo base_class_type_info = |
| 9034 | getASTContext()->getTypeInfo(base_class_qual_type); |
| 9035 | |
| 9036 | // Dump the value of the member |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 9037 | CompilerType base_clang_type(this, |
| 9038 | base_class_qual_type.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9039 | base_clang_type.DumpValue( |
| 9040 | exe_ctx, |
| 9041 | s, // Stream to dump to |
| 9042 | base_clang_type |
| 9043 | .GetFormat(), // The format with which to display the member |
| 9044 | data, // Data buffer containing all bytes for this type |
| 9045 | data_byte_offset + field_byte_offset, // Offset into "data" where |
| 9046 | // to grab value from |
| 9047 | base_class_type_info.Width / 8, // Size of this type in bytes |
| 9048 | 0, // Bitfield bit size |
| 9049 | 0, // Bitfield bit offset |
| 9050 | show_types, // Boolean indicating if we should show the variable |
| 9051 | // types |
| 9052 | show_summary, // Boolean indicating if we should show a summary |
| 9053 | // for the current type |
| 9054 | verbose, // Verbose output? |
| 9055 | depth + DEPTH_INCREMENT); // Scope depth for any types that have |
| 9056 | // children |
| 9057 | |
| 9058 | ++child_idx; |
| 9059 | } |
| 9060 | } |
| 9061 | uint32_t field_idx = 0; |
| 9062 | clang::RecordDecl::field_iterator field, field_end; |
| 9063 | for (field = record_decl->field_begin(), |
| 9064 | field_end = record_decl->field_end(); |
| 9065 | field != field_end; ++field, ++field_idx, ++child_idx) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 9066 | // Print the starting squiggly bracket (if this is the first member) or |
| 9067 | // comma (for member 2 and beyond) for the struct/union/class member. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9068 | if (child_idx == 0) |
| 9069 | s->PutChar('{'); |
| 9070 | else |
| 9071 | s->PutChar(','); |
| 9072 | |
| 9073 | // Indent |
| 9074 | s->Printf("\n%*s", depth + DEPTH_INCREMENT, ""); |
| 9075 | |
| 9076 | clang::QualType field_type = field->getType(); |
| 9077 | // Print the member type if requested |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 9078 | // Figure out the type byte size (field_type_info.first) and alignment |
| 9079 | // (field_type_info.second) from the AST context. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9080 | clang::TypeInfo field_type_info = |
| 9081 | getASTContext()->getTypeInfo(field_type); |
| 9082 | assert(field_idx < record_layout.getFieldCount()); |
| 9083 | // Figure out the field offset within the current struct/union/class |
| 9084 | // type |
| 9085 | field_bit_offset = record_layout.getFieldOffset(field_idx); |
| 9086 | field_byte_offset = field_bit_offset / 8; |
| 9087 | uint32_t field_bitfield_bit_size = 0; |
| 9088 | uint32_t field_bitfield_bit_offset = 0; |
Raphael Isemann | 02e9113 | 2019-11-20 12:09:19 +0100 | [diff] [blame] | 9089 | if (FieldIsBitfield(*field, field_bitfield_bit_size)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9090 | field_bitfield_bit_offset = field_bit_offset % 8; |
| 9091 | |
| 9092 | if (show_types) { |
| 9093 | std::string field_type_name(field_type.getAsString()); |
| 9094 | if (field_bitfield_bit_size > 0) |
| 9095 | s->Printf("(%s:%u) ", field_type_name.c_str(), |
| 9096 | field_bitfield_bit_size); |
| 9097 | else |
| 9098 | s->Printf("(%s) ", field_type_name.c_str()); |
| 9099 | } |
| 9100 | // Print the member name and equal sign |
| 9101 | s->Printf("%s = ", field->getNameAsString().c_str()); |
| 9102 | |
| 9103 | // Dump the value of the member |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 9104 | CompilerType field_clang_type(this, field_type.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9105 | field_clang_type.DumpValue( |
| 9106 | exe_ctx, |
| 9107 | s, // Stream to dump to |
| 9108 | field_clang_type |
| 9109 | .GetFormat(), // The format with which to display the member |
| 9110 | data, // Data buffer containing all bytes for this type |
| 9111 | data_byte_offset + field_byte_offset, // Offset into "data" where to |
| 9112 | // grab value from |
| 9113 | field_type_info.Width / 8, // Size of this type in bytes |
| 9114 | field_bitfield_bit_size, // Bitfield bit size |
| 9115 | field_bitfield_bit_offset, // Bitfield bit offset |
| 9116 | show_types, // Boolean indicating if we should show the variable |
| 9117 | // types |
| 9118 | show_summary, // Boolean indicating if we should show a summary for |
| 9119 | // the current type |
| 9120 | verbose, // Verbose output? |
| 9121 | depth + DEPTH_INCREMENT); // Scope depth for any types that have |
| 9122 | // children |
| 9123 | } |
| 9124 | |
| 9125 | // Indent the trailing squiggly bracket |
| 9126 | if (child_idx > 0) |
| 9127 | s->Printf("\n%*s}", depth, ""); |
| 9128 | } |
| 9129 | return; |
| 9130 | |
| 9131 | case clang::Type::Enum: |
| 9132 | if (GetCompleteType(type)) { |
| 9133 | const clang::EnumType *enutype = |
| 9134 | llvm::cast<clang::EnumType>(qual_type.getTypePtr()); |
| 9135 | const clang::EnumDecl *enum_decl = enutype->getDecl(); |
| 9136 | assert(enum_decl); |
| 9137 | clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos; |
| 9138 | lldb::offset_t offset = data_byte_offset; |
| 9139 | const int64_t enum_value = data.GetMaxU64Bitfield( |
| 9140 | &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset); |
| 9141 | for (enum_pos = enum_decl->enumerator_begin(), |
| 9142 | enum_end_pos = enum_decl->enumerator_end(); |
| 9143 | enum_pos != enum_end_pos; ++enum_pos) { |
| 9144 | if (enum_pos->getInitVal() == enum_value) { |
| 9145 | s->Printf("%s", enum_pos->getNameAsString().c_str()); |
| 9146 | return; |
| 9147 | } |
| 9148 | } |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 9149 | // If we have gotten here we didn't get find the enumerator in the enum |
| 9150 | // decl, so just print the integer. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9151 | s->Printf("%" PRIi64, enum_value); |
| 9152 | } |
| 9153 | return; |
| 9154 | |
| 9155 | case clang::Type::ConstantArray: { |
| 9156 | const clang::ConstantArrayType *array = |
| 9157 | llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr()); |
| 9158 | bool is_array_of_characters = false; |
| 9159 | clang::QualType element_qual_type = array->getElementType(); |
| 9160 | |
| 9161 | const clang::Type *canonical_type = |
| 9162 | element_qual_type->getCanonicalTypeInternal().getTypePtr(); |
| 9163 | if (canonical_type) |
| 9164 | is_array_of_characters = canonical_type->isCharType(); |
| 9165 | |
| 9166 | const uint64_t element_count = array->getSize().getLimitedValue(); |
| 9167 | |
| 9168 | clang::TypeInfo field_type_info = |
| 9169 | getASTContext()->getTypeInfo(element_qual_type); |
| 9170 | |
| 9171 | uint32_t element_idx = 0; |
| 9172 | uint32_t element_offset = 0; |
| 9173 | uint64_t element_byte_size = field_type_info.Width / 8; |
| 9174 | uint32_t element_stride = element_byte_size; |
| 9175 | |
| 9176 | if (is_array_of_characters) { |
| 9177 | s->PutChar('"'); |
Zachary Turner | 29cb868 | 2017-03-03 20:57:05 +0000 | [diff] [blame] | 9178 | DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar, |
| 9179 | element_byte_size, element_count, UINT32_MAX, |
| 9180 | LLDB_INVALID_ADDRESS, 0, 0); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9181 | s->PutChar('"'); |
| 9182 | return; |
| 9183 | } else { |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 9184 | CompilerType element_clang_type(this, element_qual_type.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9185 | lldb::Format element_format = element_clang_type.GetFormat(); |
| 9186 | |
| 9187 | for (element_idx = 0; element_idx < element_count; ++element_idx) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 9188 | // Print the starting squiggly bracket (if this is the first member) or |
| 9189 | // comman (for member 2 and beyong) for the struct/union/class member. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9190 | if (element_idx == 0) |
| 9191 | s->PutChar('{'); |
| 9192 | else |
| 9193 | s->PutChar(','); |
| 9194 | |
| 9195 | // Indent and print the index |
| 9196 | s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx); |
| 9197 | |
| 9198 | // Figure out the field offset within the current struct/union/class |
| 9199 | // type |
| 9200 | element_offset = element_idx * element_stride; |
| 9201 | |
| 9202 | // Dump the value of the member |
| 9203 | element_clang_type.DumpValue( |
| 9204 | exe_ctx, |
| 9205 | s, // Stream to dump to |
| 9206 | element_format, // The format with which to display the element |
| 9207 | data, // Data buffer containing all bytes for this type |
| 9208 | data_byte_offset + |
| 9209 | element_offset, // Offset into "data" where to grab value from |
| 9210 | element_byte_size, // Size of this type in bytes |
| 9211 | 0, // Bitfield bit size |
| 9212 | 0, // Bitfield bit offset |
| 9213 | show_types, // Boolean indicating if we should show the variable |
| 9214 | // types |
| 9215 | show_summary, // Boolean indicating if we should show a summary for |
| 9216 | // the current type |
| 9217 | verbose, // Verbose output? |
| 9218 | depth + DEPTH_INCREMENT); // Scope depth for any types that have |
| 9219 | // children |
| 9220 | } |
| 9221 | |
| 9222 | // Indent the trailing squiggly bracket |
| 9223 | if (element_idx > 0) |
| 9224 | s->Printf("\n%*s}", depth, ""); |
| 9225 | } |
| 9226 | } |
| 9227 | return; |
| 9228 | |
| 9229 | case clang::Type::Typedef: { |
| 9230 | clang::QualType typedef_qual_type = |
| 9231 | llvm::cast<clang::TypedefType>(qual_type) |
| 9232 | ->getDecl() |
| 9233 | ->getUnderlyingType(); |
| 9234 | |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 9235 | CompilerType typedef_clang_type(this, typedef_qual_type.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9236 | lldb::Format typedef_format = typedef_clang_type.GetFormat(); |
| 9237 | clang::TypeInfo typedef_type_info = |
| 9238 | getASTContext()->getTypeInfo(typedef_qual_type); |
| 9239 | uint64_t typedef_byte_size = typedef_type_info.Width / 8; |
| 9240 | |
| 9241 | return typedef_clang_type.DumpValue( |
| 9242 | exe_ctx, |
| 9243 | s, // Stream to dump to |
| 9244 | typedef_format, // The format with which to display the element |
| 9245 | data, // Data buffer containing all bytes for this type |
| 9246 | data_byte_offset, // Offset into "data" where to grab value from |
| 9247 | typedef_byte_size, // Size of this type in bytes |
| 9248 | bitfield_bit_size, // Bitfield bit size |
| 9249 | bitfield_bit_offset, // Bitfield bit offset |
| 9250 | show_types, // Boolean indicating if we should show the variable types |
| 9251 | show_summary, // Boolean indicating if we should show a summary for the |
| 9252 | // current type |
| 9253 | verbose, // Verbose output? |
| 9254 | depth); // Scope depth for any types that have children |
| 9255 | } break; |
| 9256 | |
| 9257 | case clang::Type::Auto: { |
| 9258 | clang::QualType elaborated_qual_type = |
| 9259 | llvm::cast<clang::AutoType>(qual_type)->getDeducedType(); |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 9260 | CompilerType elaborated_clang_type(this, |
| 9261 | elaborated_qual_type.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9262 | lldb::Format elaborated_format = elaborated_clang_type.GetFormat(); |
| 9263 | clang::TypeInfo elaborated_type_info = |
| 9264 | getASTContext()->getTypeInfo(elaborated_qual_type); |
| 9265 | uint64_t elaborated_byte_size = elaborated_type_info.Width / 8; |
| 9266 | |
| 9267 | return elaborated_clang_type.DumpValue( |
| 9268 | exe_ctx, |
| 9269 | s, // Stream to dump to |
| 9270 | elaborated_format, // The format with which to display the element |
| 9271 | data, // Data buffer containing all bytes for this type |
| 9272 | data_byte_offset, // Offset into "data" where to grab value from |
| 9273 | elaborated_byte_size, // Size of this type in bytes |
| 9274 | bitfield_bit_size, // Bitfield bit size |
| 9275 | bitfield_bit_offset, // Bitfield bit offset |
| 9276 | show_types, // Boolean indicating if we should show the variable types |
| 9277 | show_summary, // Boolean indicating if we should show a summary for the |
| 9278 | // current type |
| 9279 | verbose, // Verbose output? |
| 9280 | depth); // Scope depth for any types that have children |
| 9281 | } break; |
| 9282 | |
| 9283 | case clang::Type::Elaborated: { |
| 9284 | clang::QualType elaborated_qual_type = |
| 9285 | llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(); |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 9286 | CompilerType elaborated_clang_type(this, |
| 9287 | elaborated_qual_type.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9288 | lldb::Format elaborated_format = elaborated_clang_type.GetFormat(); |
| 9289 | clang::TypeInfo elaborated_type_info = |
| 9290 | getASTContext()->getTypeInfo(elaborated_qual_type); |
| 9291 | uint64_t elaborated_byte_size = elaborated_type_info.Width / 8; |
| 9292 | |
| 9293 | return elaborated_clang_type.DumpValue( |
| 9294 | exe_ctx, |
| 9295 | s, // Stream to dump to |
| 9296 | elaborated_format, // The format with which to display the element |
| 9297 | data, // Data buffer containing all bytes for this type |
| 9298 | data_byte_offset, // Offset into "data" where to grab value from |
| 9299 | elaborated_byte_size, // Size of this type in bytes |
| 9300 | bitfield_bit_size, // Bitfield bit size |
| 9301 | bitfield_bit_offset, // Bitfield bit offset |
| 9302 | show_types, // Boolean indicating if we should show the variable types |
| 9303 | show_summary, // Boolean indicating if we should show a summary for the |
| 9304 | // current type |
| 9305 | verbose, // Verbose output? |
| 9306 | depth); // Scope depth for any types that have children |
| 9307 | } break; |
| 9308 | |
| 9309 | case clang::Type::Paren: { |
| 9310 | clang::QualType desugar_qual_type = |
| 9311 | llvm::cast<clang::ParenType>(qual_type)->desugar(); |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 9312 | CompilerType desugar_clang_type(this, desugar_qual_type.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9313 | |
| 9314 | lldb::Format desugar_format = desugar_clang_type.GetFormat(); |
| 9315 | clang::TypeInfo desugar_type_info = |
| 9316 | getASTContext()->getTypeInfo(desugar_qual_type); |
| 9317 | uint64_t desugar_byte_size = desugar_type_info.Width / 8; |
| 9318 | |
| 9319 | return desugar_clang_type.DumpValue( |
| 9320 | exe_ctx, |
| 9321 | s, // Stream to dump to |
| 9322 | desugar_format, // The format with which to display the element |
| 9323 | data, // Data buffer containing all bytes for this type |
| 9324 | data_byte_offset, // Offset into "data" where to grab value from |
| 9325 | desugar_byte_size, // Size of this type in bytes |
| 9326 | bitfield_bit_size, // Bitfield bit size |
| 9327 | bitfield_bit_offset, // Bitfield bit offset |
| 9328 | show_types, // Boolean indicating if we should show the variable types |
| 9329 | show_summary, // Boolean indicating if we should show a summary for the |
| 9330 | // current type |
| 9331 | verbose, // Verbose output? |
| 9332 | depth); // Scope depth for any types that have children |
| 9333 | } break; |
| 9334 | |
| 9335 | default: |
| 9336 | // 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] | 9337 | DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1, |
| 9338 | UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size, |
| 9339 | bitfield_bit_offset); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9340 | |
| 9341 | if (show_summary) |
| 9342 | DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size); |
| 9343 | break; |
| 9344 | } |
| 9345 | } |
| 9346 | |
Frederic Riss | d6470fb | 2019-10-08 15:35:58 +0000 | [diff] [blame] | 9347 | static bool DumpEnumValue(const clang::QualType &qual_type, Stream *s, |
| 9348 | const DataExtractor &data, lldb::offset_t byte_offset, |
| 9349 | size_t byte_size, uint32_t bitfield_bit_offset, |
| 9350 | uint32_t bitfield_bit_size) { |
| 9351 | const clang::EnumType *enutype = |
| 9352 | llvm::cast<clang::EnumType>(qual_type.getTypePtr()); |
| 9353 | const clang::EnumDecl *enum_decl = enutype->getDecl(); |
| 9354 | assert(enum_decl); |
Frederic Riss | d6470fb | 2019-10-08 15:35:58 +0000 | [diff] [blame] | 9355 | lldb::offset_t offset = byte_offset; |
Frederic Riss | 41ff396 | 2019-10-08 15:35:59 +0000 | [diff] [blame] | 9356 | const uint64_t enum_svalue = data.GetMaxS64Bitfield( |
Frederic Riss | d6470fb | 2019-10-08 15:35:58 +0000 | [diff] [blame] | 9357 | &offset, byte_size, bitfield_bit_size, bitfield_bit_offset); |
Frederic Riss | 41ff396 | 2019-10-08 15:35:59 +0000 | [diff] [blame] | 9358 | bool can_be_bitfield = true; |
| 9359 | uint64_t covered_bits = 0; |
| 9360 | int num_enumerators = 0; |
Frederic Riss | d6470fb | 2019-10-08 15:35:58 +0000 | [diff] [blame] | 9361 | |
Frederic Riss | 41ff396 | 2019-10-08 15:35:59 +0000 | [diff] [blame] | 9362 | // Try to find an exact match for the value. |
| 9363 | // At the same time, we're applying a heuristic to determine whether we want |
| 9364 | // to print this enum as a bitfield. We're likely dealing with a bitfield if |
| 9365 | // every enumrator is either a one bit value or a superset of the previous |
| 9366 | // enumerators. Also 0 doesn't make sense when the enumerators are used as |
| 9367 | // flags. |
Frederic Riss | d6470fb | 2019-10-08 15:35:58 +0000 | [diff] [blame] | 9368 | for (auto enumerator : enum_decl->enumerators()) { |
Frederic Riss | 41ff396 | 2019-10-08 15:35:59 +0000 | [diff] [blame] | 9369 | uint64_t val = enumerator->getInitVal().getSExtValue(); |
Frederic Riss | 5d415b7 | 2019-10-08 17:59:02 +0000 | [diff] [blame] | 9370 | val = llvm::SignExtend64(val, 8*byte_size); |
Frederic Riss | 41ff396 | 2019-10-08 15:35:59 +0000 | [diff] [blame] | 9371 | if (llvm::countPopulation(val) != 1 && (val & ~covered_bits) != 0) |
| 9372 | can_be_bitfield = false; |
| 9373 | covered_bits |= val; |
| 9374 | ++num_enumerators; |
| 9375 | if (val == enum_svalue) { |
| 9376 | // Found an exact match, that's all we need to do. |
Frederic Riss | d6470fb | 2019-10-08 15:35:58 +0000 | [diff] [blame] | 9377 | s->PutCString(enumerator->getNameAsString()); |
| 9378 | return true; |
| 9379 | } |
| 9380 | } |
Frederic Riss | 41ff396 | 2019-10-08 15:35:59 +0000 | [diff] [blame] | 9381 | |
Frederic Riss | 41ff396 | 2019-10-08 15:35:59 +0000 | [diff] [blame] | 9382 | // Unsigned values make more sense for flags. |
| 9383 | offset = byte_offset; |
| 9384 | const uint64_t enum_uvalue = data.GetMaxU64Bitfield( |
| 9385 | &offset, byte_size, bitfield_bit_size, bitfield_bit_offset); |
| 9386 | |
Frederic Riss | b56e3a1 | 2019-10-08 19:52:01 +0000 | [diff] [blame] | 9387 | // No exact match, but we don't think this is a bitfield. Print the value as |
| 9388 | // decimal. |
| 9389 | if (!can_be_bitfield) { |
| 9390 | if (qual_type->isSignedIntegerOrEnumerationType()) |
| 9391 | s->Printf("%" PRIi64, enum_svalue); |
| 9392 | else |
| 9393 | s->Printf("%" PRIu64, enum_uvalue); |
| 9394 | return true; |
| 9395 | } |
| 9396 | |
Frederic Riss | 41ff396 | 2019-10-08 15:35:59 +0000 | [diff] [blame] | 9397 | uint64_t remaining_value = enum_uvalue; |
| 9398 | std::vector<std::pair<uint64_t, llvm::StringRef>> values; |
| 9399 | values.reserve(num_enumerators); |
| 9400 | for (auto enumerator : enum_decl->enumerators()) |
| 9401 | if (auto val = enumerator->getInitVal().getZExtValue()) |
| 9402 | values.emplace_back(val, enumerator->getName()); |
| 9403 | |
| 9404 | // Sort in reverse order of the number of the population count, so that in |
| 9405 | // `enum {A, B, ALL = A|B }` we visit ALL first. Use a stable sort so that |
| 9406 | // A | C where A is declared before C is displayed in this order. |
| 9407 | std::stable_sort(values.begin(), values.end(), [](const auto &a, const auto &b) { |
| 9408 | return llvm::countPopulation(a.first) > llvm::countPopulation(b.first); |
| 9409 | }); |
| 9410 | |
| 9411 | for (const auto &val : values) { |
| 9412 | if ((remaining_value & val.first) != val.first) |
| 9413 | continue; |
| 9414 | remaining_value &= ~val.first; |
| 9415 | s->PutCString(val.second); |
| 9416 | if (remaining_value) |
| 9417 | s->PutCString(" | "); |
| 9418 | } |
| 9419 | |
| 9420 | // If there is a remainder that is not covered by the value, print it as hex. |
| 9421 | if (remaining_value) |
| 9422 | s->Printf("0x%" PRIx64, remaining_value); |
| 9423 | |
Frederic Riss | d6470fb | 2019-10-08 15:35:58 +0000 | [diff] [blame] | 9424 | return true; |
| 9425 | } |
| 9426 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9427 | bool ClangASTContext::DumpTypeValue( |
| 9428 | lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format, |
Zachary Turner | 29cb868 | 2017-03-03 20:57:05 +0000 | [diff] [blame] | 9429 | const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size, |
| 9430 | uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9431 | ExecutionContextScope *exe_scope) { |
| 9432 | if (!type) |
| 9433 | return false; |
| 9434 | if (IsAggregateType(type)) { |
| 9435 | return false; |
| 9436 | } else { |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9437 | clang::QualType qual_type(GetQualType(type)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9438 | |
| 9439 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
Frederic Riss | 41ff396 | 2019-10-08 15:35:59 +0000 | [diff] [blame] | 9440 | |
| 9441 | if (type_class == clang::Type::Elaborated) { |
| 9442 | qual_type = llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(); |
| 9443 | return DumpTypeValue(qual_type.getAsOpaquePtr(), s, format, data, byte_offset, byte_size, |
| 9444 | bitfield_bit_size, bitfield_bit_offset, exe_scope); |
| 9445 | } |
| 9446 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9447 | switch (type_class) { |
| 9448 | case clang::Type::Typedef: { |
| 9449 | clang::QualType typedef_qual_type = |
| 9450 | llvm::cast<clang::TypedefType>(qual_type) |
| 9451 | ->getDecl() |
| 9452 | ->getUnderlyingType(); |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 9453 | CompilerType typedef_clang_type(this, typedef_qual_type.getAsOpaquePtr()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9454 | if (format == eFormatDefault) |
| 9455 | format = typedef_clang_type.GetFormat(); |
| 9456 | clang::TypeInfo typedef_type_info = |
| 9457 | getASTContext()->getTypeInfo(typedef_qual_type); |
| 9458 | uint64_t typedef_byte_size = typedef_type_info.Width / 8; |
| 9459 | |
| 9460 | return typedef_clang_type.DumpTypeValue( |
| 9461 | s, |
| 9462 | format, // The format with which to display the element |
| 9463 | data, // Data buffer containing all bytes for this type |
| 9464 | byte_offset, // Offset into "data" where to grab value from |
| 9465 | typedef_byte_size, // Size of this type in bytes |
| 9466 | bitfield_bit_size, // Size in bits of a bitfield value, if zero don't |
| 9467 | // treat as a bitfield |
| 9468 | bitfield_bit_offset, // Offset in bits of a bitfield value if |
| 9469 | // bitfield_bit_size != 0 |
| 9470 | exe_scope); |
| 9471 | } break; |
| 9472 | |
| 9473 | case clang::Type::Enum: |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 9474 | // If our format is enum or default, show the enumeration value as its |
| 9475 | // enumeration string value, else just display it as requested. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9476 | if ((format == eFormatEnum || format == eFormatDefault) && |
Frederic Riss | d6470fb | 2019-10-08 15:35:58 +0000 | [diff] [blame] | 9477 | GetCompleteType(type)) |
| 9478 | return DumpEnumValue(qual_type, s, data, byte_offset, byte_size, |
| 9479 | bitfield_bit_offset, bitfield_bit_size); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9480 | // format was not enum, just fall through and dump the value as |
| 9481 | // requested.... |
| 9482 | LLVM_FALLTHROUGH; |
| 9483 | |
| 9484 | default: |
| 9485 | // We are down to a scalar type that we just need to display. |
| 9486 | { |
| 9487 | uint32_t item_count = 1; |
| 9488 | // A few formats, we might need to modify our size and count for |
| 9489 | // depending |
| 9490 | // on how we are trying to display the value... |
| 9491 | switch (format) { |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9492 | default: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9493 | case eFormatBoolean: |
| 9494 | case eFormatBinary: |
| 9495 | case eFormatComplex: |
| 9496 | case eFormatCString: // NULL terminated C strings |
| 9497 | case eFormatDecimal: |
| 9498 | case eFormatEnum: |
| 9499 | case eFormatHex: |
| 9500 | case eFormatHexUppercase: |
| 9501 | case eFormatFloat: |
| 9502 | case eFormatOctal: |
| 9503 | case eFormatOSType: |
| 9504 | case eFormatUnsigned: |
| 9505 | case eFormatPointer: |
| 9506 | case eFormatVectorOfChar: |
| 9507 | case eFormatVectorOfSInt8: |
| 9508 | case eFormatVectorOfUInt8: |
| 9509 | case eFormatVectorOfSInt16: |
| 9510 | case eFormatVectorOfUInt16: |
| 9511 | case eFormatVectorOfSInt32: |
| 9512 | case eFormatVectorOfUInt32: |
| 9513 | case eFormatVectorOfSInt64: |
| 9514 | case eFormatVectorOfUInt64: |
| 9515 | case eFormatVectorOfFloat32: |
| 9516 | case eFormatVectorOfFloat64: |
| 9517 | case eFormatVectorOfUInt128: |
| 9518 | break; |
| 9519 | |
| 9520 | case eFormatChar: |
| 9521 | case eFormatCharPrintable: |
| 9522 | case eFormatCharArray: |
| 9523 | case eFormatBytes: |
| 9524 | case eFormatBytesWithASCII: |
| 9525 | item_count = byte_size; |
| 9526 | byte_size = 1; |
| 9527 | break; |
| 9528 | |
| 9529 | case eFormatUnicode16: |
| 9530 | item_count = byte_size / 2; |
| 9531 | byte_size = 2; |
| 9532 | break; |
| 9533 | |
| 9534 | case eFormatUnicode32: |
| 9535 | item_count = byte_size / 4; |
| 9536 | byte_size = 4; |
| 9537 | break; |
| 9538 | } |
Zachary Turner | 29cb868 | 2017-03-03 20:57:05 +0000 | [diff] [blame] | 9539 | return DumpDataExtractor(data, s, byte_offset, format, byte_size, |
| 9540 | item_count, UINT32_MAX, LLDB_INVALID_ADDRESS, |
| 9541 | bitfield_bit_size, bitfield_bit_offset, |
| 9542 | exe_scope); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9543 | } |
| 9544 | break; |
| 9545 | } |
| 9546 | } |
Jonas Devlieghere | 09ad8c8 | 2019-05-24 00:44:33 +0000 | [diff] [blame] | 9547 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9548 | } |
| 9549 | |
| 9550 | void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type, |
| 9551 | ExecutionContext *exe_ctx, Stream *s, |
| 9552 | const lldb_private::DataExtractor &data, |
| 9553 | lldb::offset_t data_byte_offset, |
| 9554 | size_t data_byte_size) { |
| 9555 | uint32_t length = 0; |
| 9556 | if (IsCStringType(type, length)) { |
| 9557 | if (exe_ctx) { |
| 9558 | Process *process = exe_ctx->GetProcessPtr(); |
| 9559 | if (process) { |
| 9560 | lldb::offset_t offset = data_byte_offset; |
| 9561 | lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size); |
| 9562 | std::vector<uint8_t> buf; |
| 9563 | if (length > 0) |
| 9564 | buf.resize(length); |
| 9565 | else |
| 9566 | buf.resize(256); |
| 9567 | |
Zachary Turner | 29cb868 | 2017-03-03 20:57:05 +0000 | [diff] [blame] | 9568 | DataExtractor cstr_data(&buf.front(), buf.size(), |
| 9569 | process->GetByteOrder(), 4); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9570 | buf.back() = '\0'; |
| 9571 | size_t bytes_read; |
| 9572 | size_t total_cstr_len = 0; |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 9573 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9574 | while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(), |
| 9575 | buf.size(), error)) > 0) { |
| 9576 | const size_t len = strlen((const char *)&buf.front()); |
| 9577 | if (len == 0) |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9578 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9579 | if (total_cstr_len == 0) |
| 9580 | s->PutCString(" \""); |
Zachary Turner | 29cb868 | 2017-03-03 20:57:05 +0000 | [diff] [blame] | 9581 | DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len, |
| 9582 | UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9583 | total_cstr_len += len; |
| 9584 | if (len < buf.size()) |
| 9585 | break; |
| 9586 | pointer_address += total_cstr_len; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9587 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9588 | if (total_cstr_len > 0) |
| 9589 | s->PutChar('"'); |
| 9590 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9591 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9592 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9593 | } |
| 9594 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9595 | void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) { |
| 9596 | StreamFile s(stdout, false); |
| 9597 | DumpTypeDescription(type, &s); |
| 9598 | ClangASTMetadata *metadata = |
| 9599 | ClangASTContext::GetMetadata(getASTContext(), type); |
| 9600 | if (metadata) { |
| 9601 | metadata->Dump(&s); |
| 9602 | } |
| 9603 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9604 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9605 | void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type, |
| 9606 | Stream *s) { |
| 9607 | if (type) { |
| 9608 | clang::QualType qual_type(GetQualType(type)); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9609 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9610 | llvm::SmallVector<char, 1024> buf; |
| 9611 | llvm::raw_svector_ostream llvm_ostrm(buf); |
| 9612 | |
| 9613 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 9614 | switch (type_class) { |
| 9615 | case clang::Type::ObjCObject: |
| 9616 | case clang::Type::ObjCInterface: { |
| 9617 | GetCompleteType(type); |
| 9618 | |
| 9619 | const clang::ObjCObjectType *objc_class_type = |
| 9620 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr()); |
| 9621 | assert(objc_class_type); |
| 9622 | if (objc_class_type) { |
| 9623 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 9624 | objc_class_type->getInterface(); |
| 9625 | if (class_interface_decl) { |
| 9626 | clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy(); |
| 9627 | class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel()); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9628 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9629 | } |
| 9630 | } break; |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9631 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9632 | case clang::Type::Typedef: { |
| 9633 | const clang::TypedefType *typedef_type = |
| 9634 | qual_type->getAs<clang::TypedefType>(); |
| 9635 | if (typedef_type) { |
| 9636 | const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl(); |
| 9637 | std::string clang_typedef_name( |
| 9638 | typedef_decl->getQualifiedNameAsString()); |
| 9639 | if (!clang_typedef_name.empty()) { |
| 9640 | s->PutCString("typedef "); |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 9641 | s->PutCString(clang_typedef_name); |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9642 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9643 | } |
| 9644 | } break; |
| 9645 | |
| 9646 | case clang::Type::Auto: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 9647 | CompilerType(this, llvm::cast<clang::AutoType>(qual_type) |
| 9648 | ->getDeducedType() |
| 9649 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9650 | .DumpTypeDescription(s); |
| 9651 | return; |
| 9652 | |
| 9653 | case clang::Type::Elaborated: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 9654 | CompilerType(this, llvm::cast<clang::ElaboratedType>(qual_type) |
| 9655 | ->getNamedType() |
| 9656 | .getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9657 | .DumpTypeDescription(s); |
| 9658 | return; |
| 9659 | |
| 9660 | case clang::Type::Paren: |
Alex Langford | bddab07 | 2019-08-13 19:40:36 +0000 | [diff] [blame] | 9661 | CompilerType( |
| 9662 | this, |
| 9663 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9664 | .DumpTypeDescription(s); |
| 9665 | return; |
| 9666 | |
| 9667 | case clang::Type::Record: { |
| 9668 | GetCompleteType(type); |
| 9669 | |
| 9670 | const clang::RecordType *record_type = |
| 9671 | llvm::cast<clang::RecordType>(qual_type.getTypePtr()); |
| 9672 | const clang::RecordDecl *record_decl = record_type->getDecl(); |
| 9673 | const clang::CXXRecordDecl *cxx_record_decl = |
| 9674 | llvm::dyn_cast<clang::CXXRecordDecl>(record_decl); |
| 9675 | |
| 9676 | if (cxx_record_decl) |
| 9677 | cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), |
| 9678 | s->GetIndentLevel()); |
| 9679 | else |
| 9680 | record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(), |
| 9681 | s->GetIndentLevel()); |
| 9682 | } break; |
| 9683 | |
| 9684 | default: { |
| 9685 | const clang::TagType *tag_type = |
| 9686 | llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()); |
| 9687 | if (tag_type) { |
| 9688 | clang::TagDecl *tag_decl = tag_type->getDecl(); |
| 9689 | if (tag_decl) |
| 9690 | tag_decl->print(llvm_ostrm, 0); |
| 9691 | } else { |
| 9692 | std::string clang_type_name(qual_type.getAsString()); |
| 9693 | if (!clang_type_name.empty()) |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 9694 | s->PutCString(clang_type_name); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9695 | } |
Greg Clayton | d8d4a57 | 2015-08-11 21:38:15 +0000 | [diff] [blame] | 9696 | } |
Greg Clayton | e6b36cd | 2015-12-08 01:02:08 +0000 | [diff] [blame] | 9697 | } |
| 9698 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9699 | if (buf.size() > 0) { |
| 9700 | s->Write(buf.data(), buf.size()); |
Greg Clayton | 8b4edba | 2015-08-14 20:02:05 +0000 | [diff] [blame] | 9701 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9702 | } |
Greg Clayton | 8b4edba | 2015-08-14 20:02:05 +0000 | [diff] [blame] | 9703 | } |
| 9704 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9705 | void ClangASTContext::DumpTypeName(const CompilerType &type) { |
| 9706 | if (ClangUtil::IsClangType(type)) { |
| 9707 | clang::QualType qual_type( |
| 9708 | ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type))); |
| 9709 | |
| 9710 | const clang::Type::TypeClass type_class = qual_type->getTypeClass(); |
| 9711 | switch (type_class) { |
| 9712 | case clang::Type::Record: { |
| 9713 | const clang::CXXRecordDecl *cxx_record_decl = |
| 9714 | qual_type->getAsCXXRecordDecl(); |
| 9715 | if (cxx_record_decl) |
| 9716 | printf("class %s", cxx_record_decl->getName().str().c_str()); |
| 9717 | } break; |
| 9718 | |
| 9719 | case clang::Type::Enum: { |
| 9720 | clang::EnumDecl *enum_decl = |
| 9721 | llvm::cast<clang::EnumType>(qual_type)->getDecl(); |
| 9722 | if (enum_decl) { |
| 9723 | printf("enum %s", enum_decl->getName().str().c_str()); |
| 9724 | } |
| 9725 | } break; |
| 9726 | |
| 9727 | case clang::Type::ObjCObject: |
| 9728 | case clang::Type::ObjCInterface: { |
| 9729 | const clang::ObjCObjectType *objc_class_type = |
| 9730 | llvm::dyn_cast<clang::ObjCObjectType>(qual_type); |
| 9731 | if (objc_class_type) { |
| 9732 | clang::ObjCInterfaceDecl *class_interface_decl = |
| 9733 | objc_class_type->getInterface(); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 9734 | // We currently can't complete objective C types through the newly |
| 9735 | // added ASTContext because it only supports TagDecl objects right |
| 9736 | // now... |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9737 | if (class_interface_decl) |
| 9738 | printf("@class %s", class_interface_decl->getName().str().c_str()); |
| 9739 | } |
| 9740 | } break; |
| 9741 | |
| 9742 | case clang::Type::Typedef: |
| 9743 | printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type) |
| 9744 | ->getDecl() |
| 9745 | ->getName() |
| 9746 | .str() |
| 9747 | .c_str()); |
| 9748 | break; |
| 9749 | |
| 9750 | case clang::Type::Auto: |
| 9751 | printf("auto "); |
| 9752 | return DumpTypeName(CompilerType(type.GetTypeSystem(), |
| 9753 | llvm::cast<clang::AutoType>(qual_type) |
| 9754 | ->getDeducedType() |
| 9755 | .getAsOpaquePtr())); |
| 9756 | |
| 9757 | case clang::Type::Elaborated: |
| 9758 | printf("elaborated "); |
| 9759 | return DumpTypeName(CompilerType( |
| 9760 | type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type) |
| 9761 | ->getNamedType() |
| 9762 | .getAsOpaquePtr())); |
| 9763 | |
| 9764 | case clang::Type::Paren: |
| 9765 | printf("paren "); |
| 9766 | return DumpTypeName(CompilerType( |
| 9767 | type.GetTypeSystem(), |
| 9768 | llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr())); |
| 9769 | |
| 9770 | default: |
| 9771 | printf("ClangASTContext::DumpTypeName() type_class = %u", type_class); |
| 9772 | break; |
Greg Clayton | 6dc8d58 | 2015-08-18 22:32:36 +0000 | [diff] [blame] | 9773 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9774 | } |
Greg Clayton | 6dc8d58 | 2015-08-18 22:32:36 +0000 | [diff] [blame] | 9775 | } |
| 9776 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9777 | clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl( |
| 9778 | clang::DeclContext *decl_ctx, lldb::AccessType access_type, |
| 9779 | const char *parent_name, int tag_decl_kind, |
| 9780 | const ClangASTContext::TemplateParameterInfos &template_param_infos) { |
| 9781 | if (template_param_infos.IsValid()) { |
| 9782 | std::string template_basename(parent_name); |
| 9783 | template_basename.erase(template_basename.find('<')); |
| 9784 | |
| 9785 | return CreateClassTemplateDecl(decl_ctx, access_type, |
| 9786 | template_basename.c_str(), tag_decl_kind, |
| 9787 | template_param_infos); |
| 9788 | } |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 9789 | return nullptr; |
Greg Clayton | 6dc8d58 | 2015-08-18 22:32:36 +0000 | [diff] [blame] | 9790 | } |
Greg Clayton | 8b4edba | 2015-08-14 20:02:05 +0000 | [diff] [blame] | 9791 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9792 | void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) { |
| 9793 | ClangASTContext *ast = (ClangASTContext *)baton; |
| 9794 | SymbolFile *sym_file = ast->GetSymbolFile(); |
| 9795 | if (sym_file) { |
| 9796 | CompilerType clang_type = GetTypeForDecl(decl); |
| 9797 | if (clang_type) |
| 9798 | sym_file->CompleteType(clang_type); |
| 9799 | } |
Greg Clayton | 261ac3f | 2015-08-28 01:01:03 +0000 | [diff] [blame] | 9800 | } |
| 9801 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9802 | void ClangASTContext::CompleteObjCInterfaceDecl( |
| 9803 | void *baton, clang::ObjCInterfaceDecl *decl) { |
| 9804 | ClangASTContext *ast = (ClangASTContext *)baton; |
| 9805 | SymbolFile *sym_file = ast->GetSymbolFile(); |
| 9806 | if (sym_file) { |
| 9807 | CompilerType clang_type = GetTypeForDecl(decl); |
| 9808 | if (clang_type) |
| 9809 | sym_file->CompleteType(clang_type); |
| 9810 | } |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 9811 | } |
Greg Clayton | 261ac3f | 2015-08-28 01:01:03 +0000 | [diff] [blame] | 9812 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9813 | DWARFASTParser *ClangASTContext::GetDWARFParser() { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 9814 | if (!m_dwarf_ast_parser_up) |
| 9815 | m_dwarf_ast_parser_up.reset(new DWARFASTParserClang(*this)); |
| 9816 | return m_dwarf_ast_parser_up.get(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9817 | } |
| 9818 | |
| 9819 | PDBASTParser *ClangASTContext::GetPDBParser() { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 9820 | if (!m_pdb_ast_parser_up) |
| 9821 | m_pdb_ast_parser_up.reset(new PDBASTParser(*this)); |
| 9822 | return m_pdb_ast_parser_up.get(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9823 | } |
| 9824 | |
| 9825 | bool ClangASTContext::LayoutRecordType( |
| 9826 | void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size, |
| 9827 | uint64_t &alignment, |
| 9828 | llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets, |
| 9829 | llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> |
| 9830 | &base_offsets, |
| 9831 | llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> |
| 9832 | &vbase_offsets) { |
| 9833 | ClangASTContext *ast = (ClangASTContext *)baton; |
Aleksandr Urakov | 7d2a74f | 2018-08-14 07:57:44 +0000 | [diff] [blame] | 9834 | lldb_private::ClangASTImporter *importer = nullptr; |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 9835 | if (ast->m_dwarf_ast_parser_up) |
| 9836 | importer = &ast->m_dwarf_ast_parser_up->GetClangASTImporter(); |
| 9837 | if (!importer && ast->m_pdb_ast_parser_up) |
| 9838 | importer = &ast->m_pdb_ast_parser_up->GetClangASTImporter(); |
Aleksandr Urakov | 7d2a74f | 2018-08-14 07:57:44 +0000 | [diff] [blame] | 9839 | if (!importer) |
| 9840 | return false; |
| 9841 | |
| 9842 | return importer->LayoutRecordType(record_decl, bit_size, alignment, |
| 9843 | field_offsets, base_offsets, vbase_offsets); |
Greg Clayton | 8b4edba | 2015-08-14 20:02:05 +0000 | [diff] [blame] | 9844 | } |
| 9845 | |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 9846 | // CompilerDecl override functions |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 9847 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9848 | ConstString ClangASTContext::DeclGetName(void *opaque_decl) { |
| 9849 | if (opaque_decl) { |
| 9850 | clang::NamedDecl *nd = |
| 9851 | llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl); |
| 9852 | if (nd != nullptr) |
| 9853 | return ConstString(nd->getDeclName().getAsString()); |
| 9854 | } |
| 9855 | return ConstString(); |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 9856 | } |
| 9857 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9858 | ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) { |
| 9859 | if (opaque_decl) { |
| 9860 | clang::NamedDecl *nd = |
| 9861 | llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl); |
| 9862 | if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) { |
| 9863 | clang::MangleContext *mc = getMangleContext(); |
| 9864 | if (mc && mc->shouldMangleCXXName(nd)) { |
| 9865 | llvm::SmallVector<char, 1024> buf; |
| 9866 | llvm::raw_svector_ostream llvm_ostrm(buf); |
| 9867 | if (llvm::isa<clang::CXXConstructorDecl>(nd)) { |
| 9868 | mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd), |
| 9869 | Ctor_Complete, llvm_ostrm); |
| 9870 | } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) { |
| 9871 | mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd), |
| 9872 | Dtor_Complete, llvm_ostrm); |
| 9873 | } else { |
| 9874 | mc->mangleName(nd, llvm_ostrm); |
Greg Clayton | fe68904 | 2015-11-10 17:47:04 +0000 | [diff] [blame] | 9875 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9876 | if (buf.size() > 0) |
| 9877 | return ConstString(buf.data(), buf.size()); |
| 9878 | } |
Greg Clayton | fe68904 | 2015-11-10 17:47:04 +0000 | [diff] [blame] | 9879 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9880 | } |
| 9881 | return ConstString(); |
Greg Clayton | fe68904 | 2015-11-10 17:47:04 +0000 | [diff] [blame] | 9882 | } |
| 9883 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9884 | CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) { |
| 9885 | if (opaque_decl) |
| 9886 | return CompilerDeclContext(this, |
| 9887 | ((clang::Decl *)opaque_decl)->getDeclContext()); |
| 9888 | else |
| 9889 | return CompilerDeclContext(); |
Greg Clayton | fe68904 | 2015-11-10 17:47:04 +0000 | [diff] [blame] | 9890 | } |
| 9891 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9892 | CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) { |
| 9893 | if (clang::FunctionDecl *func_decl = |
| 9894 | llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) |
| 9895 | return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr()); |
| 9896 | if (clang::ObjCMethodDecl *objc_method = |
| 9897 | llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl)) |
| 9898 | return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr()); |
| 9899 | else |
Greg Clayton | fe68904 | 2015-11-10 17:47:04 +0000 | [diff] [blame] | 9900 | return CompilerType(); |
| 9901 | } |
| 9902 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9903 | size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) { |
| 9904 | if (clang::FunctionDecl *func_decl = |
| 9905 | llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) |
| 9906 | return func_decl->param_size(); |
| 9907 | if (clang::ObjCMethodDecl *objc_method = |
| 9908 | llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl)) |
| 9909 | return objc_method->param_size(); |
| 9910 | else |
| 9911 | return 0; |
| 9912 | } |
| 9913 | |
| 9914 | CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl, |
| 9915 | size_t idx) { |
| 9916 | if (clang::FunctionDecl *func_decl = |
| 9917 | llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) { |
| 9918 | if (idx < func_decl->param_size()) { |
| 9919 | ParmVarDecl *var_decl = func_decl->getParamDecl(idx); |
| 9920 | if (var_decl) |
| 9921 | return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr()); |
| 9922 | } |
| 9923 | } else if (clang::ObjCMethodDecl *objc_method = |
| 9924 | llvm::dyn_cast<clang::ObjCMethodDecl>( |
| 9925 | (clang::Decl *)opaque_decl)) { |
| 9926 | if (idx < objc_method->param_size()) |
| 9927 | return CompilerType( |
| 9928 | this, |
| 9929 | objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr()); |
| 9930 | } |
| 9931 | return CompilerType(); |
| 9932 | } |
| 9933 | |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 9934 | // CompilerDeclContext functions |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 9935 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9936 | std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName( |
| 9937 | void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) { |
| 9938 | std::vector<CompilerDecl> found_decls; |
| 9939 | if (opaque_decl_ctx) { |
| 9940 | DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx; |
| 9941 | std::set<DeclContext *> searched; |
| 9942 | std::multimap<DeclContext *, DeclContext *> search_queue; |
| 9943 | SymbolFile *symbol_file = GetSymbolFile(); |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 9944 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9945 | for (clang::DeclContext *decl_context = root_decl_ctx; |
| 9946 | decl_context != nullptr && found_decls.empty(); |
| 9947 | decl_context = decl_context->getParent()) { |
| 9948 | search_queue.insert(std::make_pair(decl_context, decl_context)); |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 9949 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9950 | for (auto it = search_queue.find(decl_context); it != search_queue.end(); |
| 9951 | it++) { |
| 9952 | if (!searched.insert(it->second).second) |
| 9953 | continue; |
| 9954 | symbol_file->ParseDeclsForContext( |
| 9955 | CompilerDeclContext(this, it->second)); |
Paul Herman | ea188fc | 2015-09-16 18:48:30 +0000 | [diff] [blame] | 9956 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9957 | for (clang::Decl *child : it->second->decls()) { |
| 9958 | if (clang::UsingDirectiveDecl *ud = |
| 9959 | llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) { |
| 9960 | if (ignore_using_decls) |
| 9961 | continue; |
| 9962 | clang::DeclContext *from = ud->getCommonAncestor(); |
| 9963 | if (searched.find(ud->getNominatedNamespace()) == searched.end()) |
| 9964 | search_queue.insert( |
| 9965 | std::make_pair(from, ud->getNominatedNamespace())); |
| 9966 | } else if (clang::UsingDecl *ud = |
| 9967 | llvm::dyn_cast<clang::UsingDecl>(child)) { |
| 9968 | if (ignore_using_decls) |
| 9969 | continue; |
| 9970 | for (clang::UsingShadowDecl *usd : ud->shadows()) { |
| 9971 | clang::Decl *target = usd->getTargetDecl(); |
| 9972 | if (clang::NamedDecl *nd = |
| 9973 | llvm::dyn_cast<clang::NamedDecl>(target)) { |
| 9974 | IdentifierInfo *ii = nd->getIdentifier(); |
| 9975 | if (ii != nullptr && |
| 9976 | ii->getName().equals(name.AsCString(nullptr))) |
| 9977 | found_decls.push_back(CompilerDecl(this, nd)); |
| 9978 | } |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 9979 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9980 | } else if (clang::NamedDecl *nd = |
| 9981 | llvm::dyn_cast<clang::NamedDecl>(child)) { |
| 9982 | IdentifierInfo *ii = nd->getIdentifier(); |
| 9983 | if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr))) |
| 9984 | found_decls.push_back(CompilerDecl(this, nd)); |
| 9985 | } |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 9986 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9987 | } |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 9988 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9989 | } |
| 9990 | return found_decls; |
Paul Herman | d628cbb | 2015-09-15 23:44:17 +0000 | [diff] [blame] | 9991 | } |
| 9992 | |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 9993 | // 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] | 9994 | // and return the number of levels it took to find it, or |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 9995 | // LLDB_INVALID_DECL_LEVEL if not found. If the decl was imported via a using |
| 9996 | // declaration, its name and/or type, if set, will be used to check that the |
| 9997 | // decl found in the scope is a match. |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 9998 | // |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 9999 | // The optional name is required by languages (like C++) to handle using |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 10000 | // declarations like: |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 10001 | // |
| 10002 | // void poo(); |
| 10003 | // namespace ns { |
| 10004 | // void foo(); |
| 10005 | // void goo(); |
| 10006 | // } |
| 10007 | // void bar() { |
| 10008 | // using ns::foo; |
| 10009 | // // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and |
| 10010 | // // LLDB_INVALID_DECL_LEVEL for 'goo'. |
| 10011 | // } |
| 10012 | // |
| 10013 | // The optional type is useful in the case that there's a specific overload |
| 10014 | // that we're looking for that might otherwise be shadowed, like: |
| 10015 | // |
| 10016 | // void foo(int); |
| 10017 | // namespace ns { |
| 10018 | // void foo(); |
| 10019 | // } |
| 10020 | // void bar() { |
| 10021 | // using ns::foo; |
| 10022 | // // CountDeclLevels returns 0 for { 'foo', void() }, |
| 10023 | // // 1 for { 'foo', void(int) }, and |
| 10024 | // // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }. |
| 10025 | // } |
| 10026 | // |
| 10027 | // NOTE: Because file statics are at the TranslationUnit along with globals, a |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10028 | // 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] | 10029 | // scope. Ideally we'd like to treat the file scope as an additional scope just |
| 10030 | // below the global scope. More work needs to be done to recognise that, if |
| 10031 | // the decl we're trying to look up is static, we should compare its source |
| 10032 | // 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] | 10033 | uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx, |
| 10034 | clang::DeclContext *child_decl_ctx, |
| 10035 | ConstString *child_name, |
| 10036 | CompilerType *child_type) { |
| 10037 | if (frame_decl_ctx) { |
| 10038 | std::set<DeclContext *> searched; |
| 10039 | std::multimap<DeclContext *, DeclContext *> search_queue; |
| 10040 | SymbolFile *symbol_file = GetSymbolFile(); |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 10041 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10042 | // Get the lookup scope for the decl we're trying to find. |
| 10043 | clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent(); |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 10044 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10045 | // Look for it in our scope's decl context and its parents. |
| 10046 | uint32_t level = 0; |
| 10047 | for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr; |
| 10048 | decl_ctx = decl_ctx->getParent()) { |
| 10049 | if (!decl_ctx->isLookupContext()) |
| 10050 | continue; |
| 10051 | if (decl_ctx == parent_decl_ctx) |
| 10052 | // Found it! |
| 10053 | return level; |
| 10054 | search_queue.insert(std::make_pair(decl_ctx, decl_ctx)); |
| 10055 | for (auto it = search_queue.find(decl_ctx); it != search_queue.end(); |
| 10056 | it++) { |
| 10057 | if (searched.find(it->second) != searched.end()) |
| 10058 | continue; |
| 10059 | |
| 10060 | // Currently DWARF has one shared translation unit for all Decls at top |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 10061 | // level, so this would erroneously find using statements anywhere. So |
| 10062 | // don't look at the top-level translation unit. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10063 | // TODO fix this and add a testcase that depends on it. |
| 10064 | |
| 10065 | if (llvm::isa<clang::TranslationUnitDecl>(it->second)) |
| 10066 | continue; |
| 10067 | |
| 10068 | searched.insert(it->second); |
| 10069 | symbol_file->ParseDeclsForContext( |
| 10070 | CompilerDeclContext(this, it->second)); |
| 10071 | |
| 10072 | for (clang::Decl *child : it->second->decls()) { |
| 10073 | if (clang::UsingDirectiveDecl *ud = |
| 10074 | llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) { |
| 10075 | clang::DeclContext *ns = ud->getNominatedNamespace(); |
| 10076 | if (ns == parent_decl_ctx) |
| 10077 | // Found it! |
| 10078 | return level; |
| 10079 | clang::DeclContext *from = ud->getCommonAncestor(); |
| 10080 | if (searched.find(ns) == searched.end()) |
| 10081 | search_queue.insert(std::make_pair(from, ns)); |
| 10082 | } else if (child_name) { |
| 10083 | if (clang::UsingDecl *ud = |
| 10084 | llvm::dyn_cast<clang::UsingDecl>(child)) { |
| 10085 | for (clang::UsingShadowDecl *usd : ud->shadows()) { |
| 10086 | clang::Decl *target = usd->getTargetDecl(); |
| 10087 | clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target); |
| 10088 | if (!nd) |
| 10089 | continue; |
| 10090 | // Check names. |
| 10091 | IdentifierInfo *ii = nd->getIdentifier(); |
| 10092 | if (ii == nullptr || |
| 10093 | !ii->getName().equals(child_name->AsCString(nullptr))) |
| 10094 | continue; |
| 10095 | // Check types, if one was provided. |
| 10096 | if (child_type) { |
| 10097 | CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd); |
| 10098 | if (!AreTypesSame(clang_type, *child_type, |
| 10099 | /*ignore_qualifiers=*/true)) |
| 10100 | continue; |
| 10101 | } |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 10102 | // Found it! |
| 10103 | return level; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10104 | } |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 10105 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10106 | } |
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 | } |
| 10109 | ++level; |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 10110 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10111 | } |
| 10112 | return LLDB_INVALID_DECL_LEVEL; |
Dawn Perchik | b592578 | 2015-12-12 19:31:41 +0000 | [diff] [blame] | 10113 | } |
| 10114 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10115 | bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) { |
| 10116 | if (opaque_decl_ctx) |
| 10117 | return ((clang::DeclContext *)opaque_decl_ctx)->isRecord(); |
| 10118 | else |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10119 | return false; |
| 10120 | } |
| 10121 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10122 | ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) { |
| 10123 | if (opaque_decl_ctx) { |
| 10124 | clang::NamedDecl *named_decl = |
| 10125 | llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx); |
| 10126 | if (named_decl) |
| 10127 | return ConstString(named_decl->getName()); |
| 10128 | } |
| 10129 | return ConstString(); |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10130 | } |
| 10131 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10132 | ConstString |
| 10133 | ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) { |
| 10134 | if (opaque_decl_ctx) { |
| 10135 | clang::NamedDecl *named_decl = |
| 10136 | llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx); |
| 10137 | if (named_decl) |
| 10138 | return ConstString( |
| 10139 | llvm::StringRef(named_decl->getQualifiedNameAsString())); |
| 10140 | } |
| 10141 | return ConstString(); |
| 10142 | } |
| 10143 | |
| 10144 | bool ClangASTContext::DeclContextIsClassMethod( |
| 10145 | void *opaque_decl_ctx, lldb::LanguageType *language_ptr, |
| 10146 | bool *is_instance_method_ptr, ConstString *language_object_name_ptr) { |
| 10147 | if (opaque_decl_ctx) { |
| 10148 | clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx; |
| 10149 | if (ObjCMethodDecl *objc_method = |
| 10150 | llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) { |
| 10151 | if (is_instance_method_ptr) |
| 10152 | *is_instance_method_ptr = objc_method->isInstanceMethod(); |
| 10153 | if (language_ptr) |
| 10154 | *language_ptr = eLanguageTypeObjC; |
| 10155 | if (language_object_name_ptr) |
| 10156 | language_object_name_ptr->SetCString("self"); |
| 10157 | return true; |
| 10158 | } else if (CXXMethodDecl *cxx_method = |
| 10159 | llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) { |
| 10160 | if (is_instance_method_ptr) |
| 10161 | *is_instance_method_ptr = cxx_method->isInstance(); |
| 10162 | if (language_ptr) |
| 10163 | *language_ptr = eLanguageTypeC_plus_plus; |
| 10164 | if (language_object_name_ptr) |
| 10165 | language_object_name_ptr->SetCString("this"); |
| 10166 | return true; |
| 10167 | } else if (clang::FunctionDecl *function_decl = |
| 10168 | llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) { |
| 10169 | ClangASTMetadata *metadata = |
| 10170 | GetMetadata(&decl_ctx->getParentASTContext(), function_decl); |
| 10171 | if (metadata && metadata->HasObjectPtr()) { |
| 10172 | if (is_instance_method_ptr) |
| 10173 | *is_instance_method_ptr = true; |
| 10174 | if (language_ptr) |
| 10175 | *language_ptr = eLanguageTypeObjC; |
| 10176 | if (language_object_name_ptr) |
| 10177 | language_object_name_ptr->SetCString(metadata->GetObjectPtrName()); |
| 10178 | return true; |
| 10179 | } |
| 10180 | } |
| 10181 | } |
| 10182 | return false; |
| 10183 | } |
| 10184 | |
Raphael Isemann | a946997 | 2019-03-12 07:45:04 +0000 | [diff] [blame] | 10185 | bool ClangASTContext::DeclContextIsContainedInLookup( |
| 10186 | void *opaque_decl_ctx, void *other_opaque_decl_ctx) { |
| 10187 | auto *decl_ctx = (clang::DeclContext *)opaque_decl_ctx; |
| 10188 | auto *other = (clang::DeclContext *)other_opaque_decl_ctx; |
| 10189 | |
| 10190 | do { |
| 10191 | // A decl context always includes its own contents in its lookup. |
| 10192 | if (decl_ctx == other) |
| 10193 | return true; |
| 10194 | |
| 10195 | // If we have an inline namespace, then the lookup of the parent context |
| 10196 | // also includes the inline namespace contents. |
| 10197 | } while (other->isInlineNamespace() && (other = other->getParent())); |
| 10198 | |
| 10199 | return false; |
| 10200 | } |
| 10201 | |
Raphael Isemann | c2dd84e | 2019-11-28 15:43:26 +0100 | [diff] [blame] | 10202 | static bool IsClangDeclContext(const CompilerDeclContext &dc) { |
| 10203 | return dc.IsValid() && isa<ClangASTContext>(dc.GetTypeSystem()); |
| 10204 | } |
| 10205 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10206 | clang::DeclContext * |
| 10207 | ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) { |
Raphael Isemann | c2dd84e | 2019-11-28 15:43:26 +0100 | [diff] [blame] | 10208 | if (IsClangDeclContext(dc)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10209 | return (clang::DeclContext *)dc.GetOpaqueDeclContext(); |
| 10210 | return nullptr; |
| 10211 | } |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10212 | |
| 10213 | ObjCMethodDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10214 | ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) { |
Raphael Isemann | c2dd84e | 2019-11-28 15:43:26 +0100 | [diff] [blame] | 10215 | if (IsClangDeclContext(dc)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10216 | return llvm::dyn_cast<clang::ObjCMethodDecl>( |
| 10217 | (clang::DeclContext *)dc.GetOpaqueDeclContext()); |
| 10218 | return nullptr; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10219 | } |
| 10220 | |
| 10221 | CXXMethodDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10222 | ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) { |
Raphael Isemann | c2dd84e | 2019-11-28 15:43:26 +0100 | [diff] [blame] | 10223 | if (IsClangDeclContext(dc)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10224 | return llvm::dyn_cast<clang::CXXMethodDecl>( |
| 10225 | (clang::DeclContext *)dc.GetOpaqueDeclContext()); |
| 10226 | return nullptr; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10227 | } |
| 10228 | |
| 10229 | clang::FunctionDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10230 | ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) { |
Raphael Isemann | c2dd84e | 2019-11-28 15:43:26 +0100 | [diff] [blame] | 10231 | if (IsClangDeclContext(dc)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10232 | return llvm::dyn_cast<clang::FunctionDecl>( |
| 10233 | (clang::DeclContext *)dc.GetOpaqueDeclContext()); |
| 10234 | return nullptr; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10235 | } |
| 10236 | |
| 10237 | clang::NamespaceDecl * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10238 | ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) { |
Raphael Isemann | c2dd84e | 2019-11-28 15:43:26 +0100 | [diff] [blame] | 10239 | if (IsClangDeclContext(dc)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10240 | return llvm::dyn_cast<clang::NamespaceDecl>( |
| 10241 | (clang::DeclContext *)dc.GetOpaqueDeclContext()); |
| 10242 | return nullptr; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10243 | } |
| 10244 | |
| 10245 | ClangASTMetadata * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10246 | ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc, |
| 10247 | const void *object) { |
| 10248 | clang::ASTContext *ast = DeclContextGetClangASTContext(dc); |
| 10249 | if (ast) |
| 10250 | return ClangASTContext::GetMetadata(ast, object); |
| 10251 | return nullptr; |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10252 | } |
| 10253 | |
| 10254 | clang::ASTContext * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10255 | ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) { |
| 10256 | ClangASTContext *ast = |
| 10257 | llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem()); |
| 10258 | if (ast) |
| 10259 | return ast->getASTContext(); |
| 10260 | return nullptr; |
| 10261 | } |
| 10262 | |
Raphael Isemann | 2eb963a | 2019-10-02 12:26:08 +0000 | [diff] [blame] | 10263 | ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target, |
| 10264 | ArchSpec arch) |
| 10265 | : ClangASTContext(arch), m_target_wp(target.shared_from_this()), |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10266 | m_persistent_variables(new ClangPersistentVariables) {} |
| 10267 | |
| 10268 | UserExpression *ClangASTContextForExpressions::GetUserExpression( |
Zachary Turner | c5d7df9 | 2016-11-08 04:52:16 +0000 | [diff] [blame] | 10269 | llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10270 | Expression::ResultType desired_type, |
Aleksandr Urakov | 40624a0 | 2019-02-05 09:14:36 +0000 | [diff] [blame] | 10271 | const EvaluateExpressionOptions &options, |
| 10272 | ValueObject *ctx_obj) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10273 | TargetSP target_sp = m_target_wp.lock(); |
| 10274 | if (!target_sp) |
Greg Clayton | 99558cc4 | 2015-08-24 23:46:31 +0000 | [diff] [blame] | 10275 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10276 | |
Zachary Turner | c5d7df9 | 2016-11-08 04:52:16 +0000 | [diff] [blame] | 10277 | return new ClangUserExpression(*target_sp.get(), expr, prefix, language, |
Aleksandr Urakov | 40624a0 | 2019-02-05 09:14:36 +0000 | [diff] [blame] | 10278 | desired_type, options, ctx_obj); |
Greg Clayton | 8b4edba | 2015-08-14 20:02:05 +0000 | [diff] [blame] | 10279 | } |
| 10280 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10281 | FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller( |
| 10282 | const CompilerType &return_type, const Address &function_address, |
| 10283 | const ValueList &arg_value_list, const char *name) { |
| 10284 | TargetSP target_sp = m_target_wp.lock(); |
| 10285 | if (!target_sp) |
| 10286 | return nullptr; |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 10287 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10288 | Process *process = target_sp->GetProcessSP().get(); |
| 10289 | if (!process) |
| 10290 | return nullptr; |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 10291 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10292 | return new ClangFunctionCaller(*process, return_type, function_address, |
| 10293 | arg_value_list, name); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 10294 | } |
| 10295 | |
| 10296 | UtilityFunction * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10297 | ClangASTContextForExpressions::GetUtilityFunction(const char *text, |
| 10298 | const char *name) { |
| 10299 | TargetSP target_sp = m_target_wp.lock(); |
| 10300 | if (!target_sp) |
| 10301 | return nullptr; |
| 10302 | |
| 10303 | return new ClangUtilityFunction(*target_sp.get(), text, name); |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 10304 | } |
Sean Callanan | 8f1f9a1 | 2015-09-30 19:57:57 +0000 | [diff] [blame] | 10305 | |
| 10306 | PersistentExpressionState * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 10307 | ClangASTContextForExpressions::GetPersistentExpressionState() { |
| 10308 | return m_persistent_variables.get(); |
Sean Callanan | 8f1f9a1 | 2015-09-30 19:57:57 +0000 | [diff] [blame] | 10309 | } |
Sean Callanan | 68e4423 | 2017-09-28 20:20:25 +0000 | [diff] [blame] | 10310 | |
| 10311 | clang::ExternalASTMerger & |
| 10312 | ClangASTContextForExpressions::GetMergerUnchecked() { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 10313 | lldbassert(m_scratch_ast_source_up != nullptr); |
| 10314 | return m_scratch_ast_source_up->GetMergerUnchecked(); |
Sean Callanan | 68e4423 | 2017-09-28 20:20:25 +0000 | [diff] [blame] | 10315 | } |