blob: 8e2576aaec956f36d9ef81d7ac33706bb0bc254f [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ClangASTContext.cpp -------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Eli Friedman932197d2010-06-13 19:06:42 +000010#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011
Zachary Turner827d5d72016-12-16 04:27:00 +000012#include "llvm/Support/FormatAdapters.h"
13#include "llvm/Support/FormatVariadic.h"
14
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015// C Includes
16// C++ Includes
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000017#include <mutex>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include <string>
Sean Callananfe38c852015-10-08 23:07:53 +000019#include <vector>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020
21// Other libraries and framework includes
Greg Clayton6beaaa62011-01-17 03:46:26 +000022
Kate Stoneb9c1b512016-09-06 20:57:50 +000023// Clang headers like to use NDEBUG inside of them to enable/disable debug
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +000024// related features using "#ifndef NDEBUG" preprocessor blocks to do one thing
Greg Clayton6beaaa62011-01-17 03:46:26 +000025// or another. This is bad because it means that if clang was built in release
26// mode, it assumes that you are building in release mode which is not always
27// the case. You can end up with functions that are defined as empty in header
28// files when NDEBUG is not defined, and this can cause link errors with the
29// clang .a files that you have since you might be missing functions in the .a
30// file. So we have to define NDEBUG when including clang headers to avoid any
31// mismatches. This is covered by rdar://problem/8691220
32
Sean Callanan3b1d4f62011-10-26 17:46:51 +000033#if !defined(NDEBUG) && !defined(LLVM_NDEBUG_OFF)
Greg Clayton6beaaa62011-01-17 03:46:26 +000034#define LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000035#define NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000036// Need to include assert.h so it is as clang would expect it to be (disabled)
37#include <assert.h>
38#endif
39
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "clang/AST/ASTContext.h"
41#include "clang/AST/ASTImporter.h"
Greg Claytonf74c4032012-12-03 18:29:55 +000042#include "clang/AST/Attr.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "clang/AST/CXXInheritance.h"
Greg Clayton8cf05932010-07-22 18:30:50 +000044#include "clang/AST/DeclObjC.h"
Greg Claytonf0705c82011-10-22 03:33:13 +000045#include "clang/AST/DeclTemplate.h"
Greg Claytonfe689042015-11-10 17:47:04 +000046#include "clang/AST/Mangle.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "clang/AST/RecordLayout.h"
48#include "clang/AST/Type.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000049#include "clang/AST/VTableBuilder.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050#include "clang/Basic/Builtins.h"
Sean Callanan7e2863b2012-02-06 21:28:03 +000051#include "clang/Basic/Diagnostic.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052#include "clang/Basic/FileManager.h"
Sean Callanan79439e82010-11-18 02:56:27 +000053#include "clang/Basic/FileSystemOptions.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054#include "clang/Basic/SourceManager.h"
55#include "clang/Basic/TargetInfo.h"
56#include "clang/Basic/TargetOptions.h"
57#include "clang/Frontend/FrontendOptions.h"
58#include "clang/Frontend/LangStandard.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000059
60#ifdef LLDB_DEFINED_NDEBUG_FOR_CLANG
Sean Callanan246549c2010-07-08 18:16:16 +000061#undef NDEBUG
Greg Clayton6beaaa62011-01-17 03:46:26 +000062#undef LLDB_DEFINED_NDEBUG_FOR_CLANG
63// Need to re-include assert.h so it is as _we_ would expect it to be (enabled)
64#include <assert.h>
65#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066
Greg Claytond8d4a572015-08-11 21:38:15 +000067#include "llvm/Support/Signals.h"
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000068#include "llvm/Support/Threading.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000069
Zachary Turnerd133f6a2016-03-28 22:53:41 +000070#include "Plugins/ExpressionParser/Clang/ClangFunctionCaller.h"
71#include "Plugins/ExpressionParser/Clang/ClangUserExpression.h"
72#include "Plugins/ExpressionParser/Clang/ClangUtilityFunction.h"
Greg Clayton514487e2011-02-15 21:59:32 +000073#include "lldb/Core/ArchSpec.h"
Zachary Turner01c32432017-02-14 19:06:07 +000074#include "lldb/Utility/Flags.h"
75
Zachary Turner29cb8682017-03-03 20:57:05 +000076#include "lldb/Core/DumpDataExtractor.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000077#include "lldb/Core/Module.h"
78#include "lldb/Core/PluginManager.h"
Ulrich Weigand9521ad22016-04-15 09:55:52 +000079#include "lldb/Core/Scalar.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000080#include "lldb/Core/StreamFile.h"
Enrico Granata2267ad42014-09-16 17:28:40 +000081#include "lldb/Core/ThreadSafeDenseMap.h"
Greg Clayton57ee3062013-07-11 22:46:58 +000082#include "lldb/Core/UniqueCStringMap.h"
Greg Claytond8d4a572015-08-11 21:38:15 +000083#include "lldb/Symbol/ClangASTContext.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000084#include "lldb/Symbol/ClangASTImporter.h"
Greg Clayton6dc8d582015-08-18 22:32:36 +000085#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Sean Callanan3b107b12011-12-03 03:15:28 +000086#include "lldb/Symbol/ClangExternalASTSourceCommon.h"
Zachary Turnerd133f6a2016-03-28 22:53:41 +000087#include "lldb/Symbol/ClangUtil.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000088#include "lldb/Symbol/ObjectFile.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +000089#include "lldb/Symbol/SymbolFile.h"
Sean Callanan5e9e1992011-10-26 01:06:27 +000090#include "lldb/Symbol/VerifyDecl.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000091#include "lldb/Target/ExecutionContext.h"
Greg Clayton56939cb2015-09-17 22:23:34 +000092#include "lldb/Target/Language.h"
Jim Inghamd555bac2011-06-24 22:03:24 +000093#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham151c0322015-09-15 21:13:50 +000094#include "lldb/Target/Process.h"
95#include "lldb/Target/Target.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000096#include "lldb/Utility/DataExtractor.h"
Sean Callananc530ba92016-05-02 21:15:31 +000097#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000098#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000099#include "lldb/Utility/RegularExpression.h"
Jim Inghamd555bac2011-06-24 22:03:24 +0000100
Greg Clayton261ac3f2015-08-28 01:01:03 +0000101#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h"
Zachary Turner42dff792016-04-15 00:21:26 +0000102#include "Plugins/SymbolFile/PDB/PDBASTParser.h"
Greg Clayton261ac3f2015-08-28 01:01:03 +0000103
Eli Friedman932197d2010-06-13 19:06:42 +0000104#include <stdio.h>
105
Greg Clayton1341baf2013-07-11 23:36:31 +0000106#include <mutex>
107
Greg Claytonc86103d2010-08-05 01:57:25 +0000108using namespace lldb;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109using namespace lldb_private;
110using namespace llvm;
111using namespace clang;
112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113namespace {
114static inline bool
115ClangASTContextSupportsLanguage(lldb::LanguageType language) {
116 return language == eLanguageTypeUnknown || // Clang is the default type system
117 Language::LanguageIsC(language) ||
118 Language::LanguageIsCPlusPlus(language) ||
119 Language::LanguageIsObjC(language) ||
120 Language::LanguageIsPascal(language) ||
121 // Use Clang for Rust until there is a proper language plugin for it
122 language == eLanguageTypeRust ||
Johan Engelen04799572016-11-25 11:01:12 +0000123 language == eLanguageTypeExtRenderScript ||
124 // Use Clang for D until there is a proper language plugin for it
125 language == eLanguageTypeD;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126}
Greg Clayton56939cb2015-09-17 22:23:34 +0000127}
128
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129typedef lldb_private::ThreadSafeDenseMap<clang::ASTContext *, ClangASTContext *>
130 ClangASTMap;
Enrico Granata5d84a692014-08-19 21:46:37 +0000131
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132static ClangASTMap &GetASTMap() {
133 static ClangASTMap *g_map_ptr = nullptr;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000134 static llvm::once_flag g_once_flag;
135 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 g_map_ptr = new ClangASTMap(); // leaked on purpose to avoid spins
137 });
138 return *g_map_ptr;
Enrico Granata5d84a692014-08-19 21:46:37 +0000139}
140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141static bool IsOperator(const char *name,
142 clang::OverloadedOperatorKind &op_kind) {
143 if (name == nullptr || name[0] == '\0')
144 return false;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000145
146#define OPERATOR_PREFIX "operator"
147#define OPERATOR_PREFIX_LENGTH (sizeof(OPERATOR_PREFIX) - 1)
148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149 const char *post_op_name = nullptr;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000150
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151 bool no_space = true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
154 return false;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000155
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156 post_op_name = name + OPERATOR_PREFIX_LENGTH;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000157
Kate Stoneb9c1b512016-09-06 20:57:50 +0000158 if (post_op_name[0] == ' ') {
159 post_op_name++;
160 no_space = false;
161 }
Pavel Labath1ac2b202016-08-15 14:32:32 +0000162
163#undef OPERATOR_PREFIX
164#undef OPERATOR_PREFIX_LENGTH
165
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 // This is an operator, set the overloaded operator kind to invalid
167 // in case this is a conversion operator...
168 op_kind = clang::NUM_OVERLOADED_OPERATORS;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000169
Kate Stoneb9c1b512016-09-06 20:57:50 +0000170 switch (post_op_name[0]) {
171 default:
172 if (no_space)
173 return false;
174 break;
175 case 'n':
176 if (no_space)
177 return false;
178 if (strcmp(post_op_name, "new") == 0)
179 op_kind = clang::OO_New;
180 else if (strcmp(post_op_name, "new[]") == 0)
181 op_kind = clang::OO_Array_New;
182 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 case 'd':
185 if (no_space)
186 return false;
187 if (strcmp(post_op_name, "delete") == 0)
188 op_kind = clang::OO_Delete;
189 else if (strcmp(post_op_name, "delete[]") == 0)
190 op_kind = clang::OO_Array_Delete;
191 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000192
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193 case '+':
194 if (post_op_name[1] == '\0')
195 op_kind = clang::OO_Plus;
196 else if (post_op_name[2] == '\0') {
197 if (post_op_name[1] == '=')
198 op_kind = clang::OO_PlusEqual;
199 else if (post_op_name[1] == '+')
200 op_kind = clang::OO_PlusPlus;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000201 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202 break;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000203
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204 case '-':
205 if (post_op_name[1] == '\0')
206 op_kind = clang::OO_Minus;
207 else if (post_op_name[2] == '\0') {
208 switch (post_op_name[1]) {
209 case '=':
210 op_kind = clang::OO_MinusEqual;
211 break;
212 case '-':
213 op_kind = clang::OO_MinusMinus;
214 break;
215 case '>':
216 op_kind = clang::OO_Arrow;
217 break;
218 }
219 } else if (post_op_name[3] == '\0') {
220 if (post_op_name[2] == '*')
221 op_kind = clang::OO_ArrowStar;
222 break;
223 }
224 break;
225
226 case '*':
227 if (post_op_name[1] == '\0')
228 op_kind = clang::OO_Star;
229 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
230 op_kind = clang::OO_StarEqual;
231 break;
232
233 case '/':
234 if (post_op_name[1] == '\0')
235 op_kind = clang::OO_Slash;
236 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
237 op_kind = clang::OO_SlashEqual;
238 break;
239
240 case '%':
241 if (post_op_name[1] == '\0')
242 op_kind = clang::OO_Percent;
243 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
244 op_kind = clang::OO_PercentEqual;
245 break;
246
247 case '^':
248 if (post_op_name[1] == '\0')
249 op_kind = clang::OO_Caret;
250 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
251 op_kind = clang::OO_CaretEqual;
252 break;
253
254 case '&':
255 if (post_op_name[1] == '\0')
256 op_kind = clang::OO_Amp;
257 else if (post_op_name[2] == '\0') {
258 switch (post_op_name[1]) {
259 case '=':
260 op_kind = clang::OO_AmpEqual;
261 break;
262 case '&':
263 op_kind = clang::OO_AmpAmp;
264 break;
265 }
266 }
267 break;
268
269 case '|':
270 if (post_op_name[1] == '\0')
271 op_kind = clang::OO_Pipe;
272 else if (post_op_name[2] == '\0') {
273 switch (post_op_name[1]) {
274 case '=':
275 op_kind = clang::OO_PipeEqual;
276 break;
277 case '|':
278 op_kind = clang::OO_PipePipe;
279 break;
280 }
281 }
282 break;
283
284 case '~':
285 if (post_op_name[1] == '\0')
286 op_kind = clang::OO_Tilde;
287 break;
288
289 case '!':
290 if (post_op_name[1] == '\0')
291 op_kind = clang::OO_Exclaim;
292 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
293 op_kind = clang::OO_ExclaimEqual;
294 break;
295
296 case '=':
297 if (post_op_name[1] == '\0')
298 op_kind = clang::OO_Equal;
299 else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
300 op_kind = clang::OO_EqualEqual;
301 break;
302
303 case '<':
304 if (post_op_name[1] == '\0')
305 op_kind = clang::OO_Less;
306 else if (post_op_name[2] == '\0') {
307 switch (post_op_name[1]) {
308 case '<':
309 op_kind = clang::OO_LessLess;
310 break;
311 case '=':
312 op_kind = clang::OO_LessEqual;
313 break;
314 }
315 } else if (post_op_name[3] == '\0') {
316 if (post_op_name[2] == '=')
317 op_kind = clang::OO_LessLessEqual;
318 }
319 break;
320
321 case '>':
322 if (post_op_name[1] == '\0')
323 op_kind = clang::OO_Greater;
324 else if (post_op_name[2] == '\0') {
325 switch (post_op_name[1]) {
326 case '>':
327 op_kind = clang::OO_GreaterGreater;
328 break;
329 case '=':
330 op_kind = clang::OO_GreaterEqual;
331 break;
332 }
333 } else if (post_op_name[1] == '>' && post_op_name[2] == '=' &&
334 post_op_name[3] == '\0') {
335 op_kind = clang::OO_GreaterGreaterEqual;
336 }
337 break;
338
339 case ',':
340 if (post_op_name[1] == '\0')
341 op_kind = clang::OO_Comma;
342 break;
343
344 case '(':
345 if (post_op_name[1] == ')' && post_op_name[2] == '\0')
346 op_kind = clang::OO_Call;
347 break;
348
349 case '[':
350 if (post_op_name[1] == ']' && post_op_name[2] == '\0')
351 op_kind = clang::OO_Subscript;
352 break;
353 }
354
355 return true;
Pavel Labath1ac2b202016-08-15 14:32:32 +0000356}
Enrico Granata5d84a692014-08-19 21:46:37 +0000357
Greg Clayton57ee3062013-07-11 22:46:58 +0000358clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +0000359ClangASTContext::ConvertAccessTypeToAccessSpecifier(AccessType access) {
360 switch (access) {
361 default:
362 break;
363 case eAccessNone:
Greg Clayton8cf05932010-07-22 18:30:50 +0000364 return AS_none;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000365 case eAccessPublic:
366 return AS_public;
367 case eAccessPrivate:
368 return AS_private;
369 case eAccessProtected:
370 return AS_protected;
371 }
372 return AS_none;
Greg Clayton8cf05932010-07-22 18:30:50 +0000373}
374
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375static void ParseLangArgs(LangOptions &Opts, InputKind IK, const char *triple) {
376 // FIXME: Cleanup per-file based stuff.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377
Kate Stoneb9c1b512016-09-06 20:57:50 +0000378 // Set some properties which depend solely on the input kind; it would be nice
379 // to move these to the language standard, and have the driver resolve the
380 // input kind + language standard.
Richard Smith8186cd42017-04-26 22:10:53 +0000381 if (IK.getLanguage() == InputKind::Asm) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382 Opts.AsmPreprocessor = 1;
Richard Smith8186cd42017-04-26 22:10:53 +0000383 } else if (IK.isObjectiveC()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000384 Opts.ObjC1 = Opts.ObjC2 = 1;
385 }
386
387 LangStandard::Kind LangStd = LangStandard::lang_unspecified;
388
389 if (LangStd == LangStandard::lang_unspecified) {
390 // Based on the base language, pick one.
Richard Smith8186cd42017-04-26 22:10:53 +0000391 switch (IK.getLanguage()) {
392 case InputKind::Unknown:
393 case InputKind::LLVM_IR:
394 case InputKind::RenderScript:
David Blaikiea322f362017-01-06 00:38:06 +0000395 llvm_unreachable("Invalid input kind!");
Richard Smith8186cd42017-04-26 22:10:53 +0000396 case InputKind::OpenCL:
Pavel Labath47168542017-04-27 08:49:19 +0000397 LangStd = LangStandard::lang_opencl10;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000399 case InputKind::CUDA:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400 LangStd = LangStandard::lang_cuda;
401 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000402 case InputKind::Asm:
403 case InputKind::C:
404 case InputKind::ObjC:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000405 LangStd = LangStandard::lang_gnu99;
406 break;
Richard Smith8186cd42017-04-26 22:10:53 +0000407 case InputKind::CXX:
408 case InputKind::ObjCXX:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000409 LangStd = LangStandard::lang_gnucxx98;
410 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000412 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413
Kate Stoneb9c1b512016-09-06 20:57:50 +0000414 const LangStandard &Std = LangStandard::getLangStandardForKind(LangStd);
415 Opts.LineComment = Std.hasLineComments();
416 Opts.C99 = Std.isC99();
417 Opts.CPlusPlus = Std.isCPlusPlus();
418 Opts.CPlusPlus11 = Std.isCPlusPlus11();
419 Opts.Digraphs = Std.hasDigraphs();
420 Opts.GNUMode = Std.isGNUMode();
421 Opts.GNUInline = !Std.isC99();
422 Opts.HexFloats = Std.hasHexFloats();
423 Opts.ImplicitInt = Std.hasImplicitInt();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000424
Kate Stoneb9c1b512016-09-06 20:57:50 +0000425 Opts.WChar = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426
Kate Stoneb9c1b512016-09-06 20:57:50 +0000427 // OpenCL has some additional defaults.
Pavel Labath47168542017-04-27 08:49:19 +0000428 if (LangStd == LangStandard::lang_opencl10) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000429 Opts.OpenCL = 1;
430 Opts.AltiVec = 1;
431 Opts.CXXOperatorNames = 1;
432 Opts.LaxVectorConversions = 1;
433 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000434
Kate Stoneb9c1b512016-09-06 20:57:50 +0000435 // OpenCL and C++ both have bool, true, false keywords.
436 Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437
Kate Stoneb9c1b512016-09-06 20:57:50 +0000438 // if (Opts.CPlusPlus)
439 // Opts.CXXOperatorNames = !Args.hasArg(OPT_fno_operator_names);
440 //
441 // if (Args.hasArg(OPT_fobjc_gc_only))
442 // Opts.setGCMode(LangOptions::GCOnly);
443 // else if (Args.hasArg(OPT_fobjc_gc))
444 // Opts.setGCMode(LangOptions::HybridGC);
445 //
446 // if (Args.hasArg(OPT_print_ivar_layout))
447 // Opts.ObjCGCBitmapPrint = 1;
448 //
449 // if (Args.hasArg(OPT_faltivec))
450 // Opts.AltiVec = 1;
451 //
452 // if (Args.hasArg(OPT_pthread))
453 // Opts.POSIXThreads = 1;
454 //
455 // llvm::StringRef Vis = getLastArgValue(Args, OPT_fvisibility,
456 // "default");
457 // if (Vis == "default")
458 Opts.setValueVisibilityMode(DefaultVisibility);
459 // else if (Vis == "hidden")
460 // Opts.setVisibilityMode(LangOptions::Hidden);
461 // else if (Vis == "protected")
462 // Opts.setVisibilityMode(LangOptions::Protected);
463 // else
464 // Diags.Report(diag::err_drv_invalid_value)
465 // << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467 // Opts.OverflowChecking = Args.hasArg(OPT_ftrapv);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469 // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs
470 // is specified, or -std is set to a conforming mode.
471 Opts.Trigraphs = !Opts.GNUMode;
472 // if (Args.hasArg(OPT_trigraphs))
473 // Opts.Trigraphs = 1;
474 //
475 // Opts.DollarIdents = Args.hasFlag(OPT_fdollars_in_identifiers,
476 // OPT_fno_dollars_in_identifiers,
477 // !Opts.AsmPreprocessor);
478 // Opts.PascalStrings = Args.hasArg(OPT_fpascal_strings);
479 // Opts.Microsoft = Args.hasArg(OPT_fms_extensions);
480 // Opts.WritableStrings = Args.hasArg(OPT_fwritable_strings);
481 // if (Args.hasArg(OPT_fno_lax_vector_conversions))
482 // Opts.LaxVectorConversions = 0;
483 // Opts.Exceptions = Args.hasArg(OPT_fexceptions);
484 // Opts.RTTI = !Args.hasArg(OPT_fno_rtti);
485 // Opts.Blocks = Args.hasArg(OPT_fblocks);
486 Opts.CharIsSigned = ArchSpec(triple).CharIsSignedByDefault();
487 // Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
488 // Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
489 // Opts.NoBuiltin = Args.hasArg(OPT_fno_builtin) || Opts.Freestanding;
490 // Opts.AssumeSaneOperatorNew =
491 // !Args.hasArg(OPT_fno_assume_sane_operator_new);
492 // Opts.HeinousExtensions = Args.hasArg(OPT_fheinous_gnu_extensions);
493 // Opts.AccessControl = Args.hasArg(OPT_faccess_control);
494 // Opts.ElideConstructors = !Args.hasArg(OPT_fno_elide_constructors);
495 // Opts.MathErrno = !Args.hasArg(OPT_fno_math_errno);
496 // Opts.InstantiationDepth = getLastArgIntValue(Args, OPT_ftemplate_depth,
497 // 99,
498 // Diags);
499 // Opts.NeXTRuntime = !Args.hasArg(OPT_fgnu_runtime);
500 // Opts.ObjCConstantStringClass = getLastArgValue(Args,
501 // OPT_fconstant_string_class);
502 // Opts.ObjCNonFragileABI = Args.hasArg(OPT_fobjc_nonfragile_abi);
503 // Opts.CatchUndefined = Args.hasArg(OPT_fcatch_undefined_behavior);
504 // Opts.EmitAllDecls = Args.hasArg(OPT_femit_all_decls);
505 // Opts.PICLevel = getLastArgIntValue(Args, OPT_pic_level, 0, Diags);
506 // Opts.Static = Args.hasArg(OPT_static_define);
507 Opts.OptimizeSize = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000508
Kate Stoneb9c1b512016-09-06 20:57:50 +0000509 // FIXME: Eliminate this dependency.
510 // unsigned Opt =
511 // Args.hasArg(OPT_Os) ? 2 : getLastArgIntValue(Args, OPT_O, 0, Diags);
512 // Opts.Optimize = Opt != 0;
513 unsigned Opt = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000514
Kate Stoneb9c1b512016-09-06 20:57:50 +0000515 // This is the __NO_INLINE__ define, which just depends on things like the
516 // optimization level and -fno-inline, not actually whether the backend has
517 // inlining enabled.
518 //
519 // FIXME: This is affected by other options (-fno-inline).
520 Opts.NoInlineDefine = !Opt;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000521
Kate Stoneb9c1b512016-09-06 20:57:50 +0000522 // unsigned SSP = getLastArgIntValue(Args, OPT_stack_protector, 0, Diags);
523 // switch (SSP) {
524 // default:
525 // Diags.Report(diag::err_drv_invalid_value)
526 // << Args.getLastArg(OPT_stack_protector)->getAsString(Args) <<
527 // SSP;
528 // break;
529 // case 0: Opts.setStackProtectorMode(LangOptions::SSPOff); break;
530 // case 1: Opts.setStackProtectorMode(LangOptions::SSPOn); break;
531 // case 2: Opts.setStackProtectorMode(LangOptions::SSPReq); break;
532 // }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000533}
534
Kate Stoneb9c1b512016-09-06 20:57:50 +0000535ClangASTContext::ClangASTContext(const char *target_triple)
536 : TypeSystem(TypeSystem::eKindClang), m_target_triple(), m_ast_ap(),
537 m_language_options_ap(), m_source_manager_ap(), m_diagnostics_engine_ap(),
538 m_target_options_rp(), m_target_info_ap(), m_identifier_table_ap(),
539 m_selector_table_ap(), m_builtins_ap(), m_callback_tag_decl(nullptr),
540 m_callback_objc_decl(nullptr), m_callback_baton(nullptr),
541 m_pointer_byte_size(0), m_ast_owned(false) {
542 if (target_triple && target_triple[0])
543 SetTargetTriple(target_triple);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000544}
545
546//----------------------------------------------------------------------
547// Destructor
548//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000549ClangASTContext::~ClangASTContext() { Finalize(); }
550
551ConstString ClangASTContext::GetPluginNameStatic() {
552 return ConstString("clang");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000553}
554
Kate Stoneb9c1b512016-09-06 20:57:50 +0000555ConstString ClangASTContext::GetPluginName() {
556 return ClangASTContext::GetPluginNameStatic();
Greg Clayton56939cb2015-09-17 22:23:34 +0000557}
558
Kate Stoneb9c1b512016-09-06 20:57:50 +0000559uint32_t ClangASTContext::GetPluginVersion() { return 1; }
Greg Clayton56939cb2015-09-17 22:23:34 +0000560
Kate Stoneb9c1b512016-09-06 20:57:50 +0000561lldb::TypeSystemSP ClangASTContext::CreateInstance(lldb::LanguageType language,
562 lldb_private::Module *module,
563 Target *target) {
564 if (ClangASTContextSupportsLanguage(language)) {
565 ArchSpec arch;
566 if (module)
567 arch = module->GetArchitecture();
568 else if (target)
569 arch = target->GetArchitecture();
Greg Clayton56939cb2015-09-17 22:23:34 +0000570
Kate Stoneb9c1b512016-09-06 20:57:50 +0000571 if (arch.IsValid()) {
572 ArchSpec fixed_arch = arch;
573 // LLVM wants this to be set to iOS or MacOSX; if we're working on
574 // a bare-boards type image, change the triple for llvm's benefit.
575 if (fixed_arch.GetTriple().getVendor() == llvm::Triple::Apple &&
576 fixed_arch.GetTriple().getOS() == llvm::Triple::UnknownOS) {
577 if (fixed_arch.GetTriple().getArch() == llvm::Triple::arm ||
578 fixed_arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
579 fixed_arch.GetTriple().getArch() == llvm::Triple::thumb) {
580 fixed_arch.GetTriple().setOS(llvm::Triple::IOS);
581 } else {
582 fixed_arch.GetTriple().setOS(llvm::Triple::MacOSX);
Greg Clayton56939cb2015-09-17 22:23:34 +0000583 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000584 }
Greg Clayton56939cb2015-09-17 22:23:34 +0000585
Kate Stoneb9c1b512016-09-06 20:57:50 +0000586 if (module) {
587 std::shared_ptr<ClangASTContext> ast_sp(new ClangASTContext);
588 if (ast_sp) {
589 ast_sp->SetArchitecture(fixed_arch);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000590 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000591 return ast_sp;
592 } else if (target && target->IsValid()) {
593 std::shared_ptr<ClangASTContextForExpressions> ast_sp(
594 new ClangASTContextForExpressions(*target));
595 if (ast_sp) {
596 ast_sp->SetArchitecture(fixed_arch);
597 ast_sp->m_scratch_ast_source_ap.reset(
598 new ClangASTSource(target->shared_from_this()));
599 ast_sp->m_scratch_ast_source_ap->InstallASTContext(
600 ast_sp->getASTContext());
601 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
602 ast_sp->m_scratch_ast_source_ap->CreateProxy());
603 ast_sp->SetExternalSource(proxy_ast_source);
604 return ast_sp;
605 }
606 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000607 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000608 }
609 return lldb::TypeSystemSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000610}
611
Kate Stoneb9c1b512016-09-06 20:57:50 +0000612void ClangASTContext::EnumerateSupportedLanguages(
613 std::set<lldb::LanguageType> &languages_for_types,
614 std::set<lldb::LanguageType> &languages_for_expressions) {
615 static std::vector<lldb::LanguageType> s_supported_languages_for_types(
616 {lldb::eLanguageTypeC89, lldb::eLanguageTypeC, lldb::eLanguageTypeC11,
617 lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeC99,
618 lldb::eLanguageTypeObjC, lldb::eLanguageTypeObjC_plus_plus,
619 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
620 lldb::eLanguageTypeC11, lldb::eLanguageTypeC_plus_plus_14});
621
622 static std::vector<lldb::LanguageType> s_supported_languages_for_expressions(
623 {lldb::eLanguageTypeC_plus_plus, lldb::eLanguageTypeObjC_plus_plus,
624 lldb::eLanguageTypeC_plus_plus_03, lldb::eLanguageTypeC_plus_plus_11,
625 lldb::eLanguageTypeC_plus_plus_14});
626
627 languages_for_types.insert(s_supported_languages_for_types.begin(),
628 s_supported_languages_for_types.end());
629 languages_for_expressions.insert(
630 s_supported_languages_for_expressions.begin(),
631 s_supported_languages_for_expressions.end());
Enrico Granata5d84a692014-08-19 21:46:37 +0000632}
633
Kate Stoneb9c1b512016-09-06 20:57:50 +0000634void ClangASTContext::Initialize() {
635 PluginManager::RegisterPlugin(GetPluginNameStatic(),
636 "clang base AST context plug-in",
637 CreateInstance, EnumerateSupportedLanguages);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638}
639
Kate Stoneb9c1b512016-09-06 20:57:50 +0000640void ClangASTContext::Terminate() {
641 PluginManager::UnregisterPlugin(CreateInstance);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000642}
643
Kate Stoneb9c1b512016-09-06 20:57:50 +0000644void ClangASTContext::Finalize() {
645 if (m_ast_ap.get()) {
646 GetASTMap().Erase(m_ast_ap.get());
647 if (!m_ast_owned)
648 m_ast_ap.release();
649 }
650
651 m_builtins_ap.reset();
652 m_selector_table_ap.reset();
653 m_identifier_table_ap.reset();
654 m_target_info_ap.reset();
655 m_target_options_rp.reset();
656 m_diagnostics_engine_ap.reset();
657 m_source_manager_ap.reset();
658 m_language_options_ap.reset();
659 m_ast_ap.reset();
660 m_scratch_ast_source_ap.reset();
661}
662
663void ClangASTContext::Clear() {
664 m_ast_ap.reset();
665 m_language_options_ap.reset();
666 m_source_manager_ap.reset();
667 m_diagnostics_engine_ap.reset();
668 m_target_options_rp.reset();
669 m_target_info_ap.reset();
670 m_identifier_table_ap.reset();
671 m_selector_table_ap.reset();
672 m_builtins_ap.reset();
673 m_pointer_byte_size = 0;
674}
675
676const char *ClangASTContext::GetTargetTriple() {
677 return m_target_triple.c_str();
678}
679
680void ClangASTContext::SetTargetTriple(const char *target_triple) {
681 Clear();
682 m_target_triple.assign(target_triple);
683}
684
685void ClangASTContext::SetArchitecture(const ArchSpec &arch) {
686 SetTargetTriple(arch.GetTriple().str().c_str());
687}
688
689bool ClangASTContext::HasExternalSource() {
690 ASTContext *ast = getASTContext();
691 if (ast)
692 return ast->getExternalSource() != nullptr;
693 return false;
694}
695
696void ClangASTContext::SetExternalSource(
697 llvm::IntrusiveRefCntPtr<ExternalASTSource> &ast_source_ap) {
698 ASTContext *ast = getASTContext();
699 if (ast) {
700 ast->setExternalSource(ast_source_ap);
701 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(true);
702 // ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(true);
703 }
704}
705
706void ClangASTContext::RemoveExternalSource() {
707 ASTContext *ast = getASTContext();
708
709 if (ast) {
710 llvm::IntrusiveRefCntPtr<ExternalASTSource> empty_ast_source_ap;
711 ast->setExternalSource(empty_ast_source_ap);
712 ast->getTranslationUnitDecl()->setHasExternalLexicalStorage(false);
713 // ast->getTranslationUnitDecl()->setHasExternalVisibleStorage(false);
714 }
715}
716
717void ClangASTContext::setASTContext(clang::ASTContext *ast_ctx) {
718 if (!m_ast_owned) {
719 m_ast_ap.release();
720 }
721 m_ast_owned = false;
722 m_ast_ap.reset(ast_ctx);
723 GetASTMap().Insert(ast_ctx, this);
724}
725
726ASTContext *ClangASTContext::getASTContext() {
727 if (m_ast_ap.get() == nullptr) {
728 m_ast_owned = true;
729 m_ast_ap.reset(new ASTContext(*getLanguageOptions(), *getSourceManager(),
730 *getIdentifierTable(), *getSelectorTable(),
731 *getBuiltinContext()));
732
733 m_ast_ap->getDiagnostics().setClient(getDiagnosticConsumer(), false);
734
735 // This can be NULL if we don't know anything about the architecture or if
736 // the
737 // target for an architecture isn't enabled in the llvm/clang that we built
738 TargetInfo *target_info = getTargetInfo();
739 if (target_info)
740 m_ast_ap->InitBuiltinTypes(*target_info);
741
742 if ((m_callback_tag_decl || m_callback_objc_decl) && m_callback_baton) {
743 m_ast_ap->getTranslationUnitDecl()->setHasExternalLexicalStorage();
744 // m_ast_ap->getTranslationUnitDecl()->setHasExternalVisibleStorage();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000745 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000746
747 GetASTMap().Insert(m_ast_ap.get(), this);
748
749 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source_ap(
750 new ClangExternalASTSourceCallbacks(
751 ClangASTContext::CompleteTagDecl,
752 ClangASTContext::CompleteObjCInterfaceDecl, nullptr,
753 ClangASTContext::LayoutRecordType, this));
754 SetExternalSource(ast_source_ap);
755 }
756 return m_ast_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000757}
758
Kate Stoneb9c1b512016-09-06 20:57:50 +0000759ClangASTContext *ClangASTContext::GetASTContext(clang::ASTContext *ast) {
760 ClangASTContext *clang_ast = GetASTMap().Lookup(ast);
761 return clang_ast;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000762}
763
Kate Stoneb9c1b512016-09-06 20:57:50 +0000764Builtin::Context *ClangASTContext::getBuiltinContext() {
765 if (m_builtins_ap.get() == nullptr)
766 m_builtins_ap.reset(new Builtin::Context());
767 return m_builtins_ap.get();
Sean Callanan79439e82010-11-18 02:56:27 +0000768}
769
Kate Stoneb9c1b512016-09-06 20:57:50 +0000770IdentifierTable *ClangASTContext::getIdentifierTable() {
771 if (m_identifier_table_ap.get() == nullptr)
772 m_identifier_table_ap.reset(
773 new IdentifierTable(*ClangASTContext::getLanguageOptions(), nullptr));
774 return m_identifier_table_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000775}
776
Kate Stoneb9c1b512016-09-06 20:57:50 +0000777LangOptions *ClangASTContext::getLanguageOptions() {
778 if (m_language_options_ap.get() == nullptr) {
779 m_language_options_ap.reset(new LangOptions());
Richard Smith8186cd42017-04-26 22:10:53 +0000780 ParseLangArgs(*m_language_options_ap, InputKind::ObjCXX, GetTargetTriple());
781 // InitializeLangOptions(*m_language_options_ap, InputKind::ObjCXX);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000782 }
783 return m_language_options_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000784}
785
Kate Stoneb9c1b512016-09-06 20:57:50 +0000786SelectorTable *ClangASTContext::getSelectorTable() {
787 if (m_selector_table_ap.get() == nullptr)
788 m_selector_table_ap.reset(new SelectorTable());
789 return m_selector_table_ap.get();
Greg Claytonfe689042015-11-10 17:47:04 +0000790}
791
Kate Stoneb9c1b512016-09-06 20:57:50 +0000792clang::FileManager *ClangASTContext::getFileManager() {
793 if (m_file_manager_ap.get() == nullptr) {
794 clang::FileSystemOptions file_system_options;
795 m_file_manager_ap.reset(new clang::FileManager(file_system_options));
796 }
797 return m_file_manager_ap.get();
798}
799
800clang::SourceManager *ClangASTContext::getSourceManager() {
801 if (m_source_manager_ap.get() == nullptr)
802 m_source_manager_ap.reset(
803 new clang::SourceManager(*getDiagnosticsEngine(), *getFileManager()));
804 return m_source_manager_ap.get();
805}
806
807clang::DiagnosticsEngine *ClangASTContext::getDiagnosticsEngine() {
808 if (m_diagnostics_engine_ap.get() == nullptr) {
809 llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_id_sp(new DiagnosticIDs());
810 m_diagnostics_engine_ap.reset(
811 new DiagnosticsEngine(diag_id_sp, new DiagnosticOptions()));
812 }
813 return m_diagnostics_engine_ap.get();
814}
815
816clang::MangleContext *ClangASTContext::getMangleContext() {
817 if (m_mangle_ctx_ap.get() == nullptr)
818 m_mangle_ctx_ap.reset(getASTContext()->createMangleContext());
819 return m_mangle_ctx_ap.get();
820}
821
822class NullDiagnosticConsumer : public DiagnosticConsumer {
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000823public:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000824 NullDiagnosticConsumer() {
825 m_log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
826 }
Sean Callanan579e70c2016-03-19 00:03:59 +0000827
Kate Stoneb9c1b512016-09-06 20:57:50 +0000828 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
829 const clang::Diagnostic &info) {
830 if (m_log) {
831 llvm::SmallVector<char, 32> diag_str(10);
832 info.FormatDiagnostic(diag_str);
833 diag_str.push_back('\0');
834 m_log->Printf("Compiler diagnostic: %s\n", diag_str.data());
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000835 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000836 }
837
838 DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {
839 return new NullDiagnosticConsumer();
840 }
841
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000842private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000843 Log *m_log;
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000844};
845
Kate Stoneb9c1b512016-09-06 20:57:50 +0000846DiagnosticConsumer *ClangASTContext::getDiagnosticConsumer() {
847 if (m_diagnostic_consumer_ap.get() == nullptr)
848 m_diagnostic_consumer_ap.reset(new NullDiagnosticConsumer);
849
850 return m_diagnostic_consumer_ap.get();
Sean Callanan7fddd4c2010-12-11 00:08:56 +0000851}
852
Kate Stoneb9c1b512016-09-06 20:57:50 +0000853std::shared_ptr<clang::TargetOptions> &ClangASTContext::getTargetOptions() {
854 if (m_target_options_rp.get() == nullptr && !m_target_triple.empty()) {
855 m_target_options_rp = std::make_shared<clang::TargetOptions>();
856 if (m_target_options_rp.get() != nullptr)
857 m_target_options_rp->Triple = m_target_triple;
858 }
859 return m_target_options_rp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000860}
861
Kate Stoneb9c1b512016-09-06 20:57:50 +0000862TargetInfo *ClangASTContext::getTargetInfo() {
863 // target_triple should be something like "x86_64-apple-macosx"
864 if (m_target_info_ap.get() == nullptr && !m_target_triple.empty())
865 m_target_info_ap.reset(TargetInfo::CreateTargetInfo(*getDiagnosticsEngine(),
866 getTargetOptions()));
867 return m_target_info_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000868}
869
870#pragma mark Basic Types
871
Kate Stoneb9c1b512016-09-06 20:57:50 +0000872static inline bool QualTypeMatchesBitSize(const uint64_t bit_size,
873 ASTContext *ast, QualType qual_type) {
874 uint64_t qual_type_bit_size = ast->getTypeSize(qual_type);
875 if (qual_type_bit_size == bit_size)
876 return true;
877 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000878}
Greg Clayton56939cb2015-09-17 22:23:34 +0000879
Greg Claytona1e5dc82015-08-11 22:53:00 +0000880CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000881ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(Encoding encoding,
882 size_t bit_size) {
883 return ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
884 getASTContext(), encoding, bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000885}
886
Kate Stoneb9c1b512016-09-06 20:57:50 +0000887CompilerType ClangASTContext::GetBuiltinTypeForEncodingAndBitSize(
888 ASTContext *ast, Encoding encoding, uint32_t bit_size) {
889 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +0000890 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000891 switch (encoding) {
892 case eEncodingInvalid:
893 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
894 return CompilerType(ast, ast->VoidPtrTy);
895 break;
896
897 case eEncodingUint:
898 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
899 return CompilerType(ast, ast->UnsignedCharTy);
900 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
901 return CompilerType(ast, ast->UnsignedShortTy);
902 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
903 return CompilerType(ast, ast->UnsignedIntTy);
904 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
905 return CompilerType(ast, ast->UnsignedLongTy);
906 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
907 return CompilerType(ast, ast->UnsignedLongLongTy);
908 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
909 return CompilerType(ast, ast->UnsignedInt128Ty);
910 break;
911
912 case eEncodingSint:
913 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
914 return CompilerType(ast, ast->SignedCharTy);
915 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
916 return CompilerType(ast, ast->ShortTy);
917 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
918 return CompilerType(ast, ast->IntTy);
919 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
920 return CompilerType(ast, ast->LongTy);
921 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
922 return CompilerType(ast, ast->LongLongTy);
923 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
924 return CompilerType(ast, ast->Int128Ty);
925 break;
926
927 case eEncodingIEEE754:
928 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
929 return CompilerType(ast, ast->FloatTy);
930 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
931 return CompilerType(ast, ast->DoubleTy);
932 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
933 return CompilerType(ast, ast->LongDoubleTy);
934 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
935 return CompilerType(ast, ast->HalfTy);
936 break;
937
938 case eEncodingVector:
939 // Sanity check that bit_size is a multiple of 8's.
940 if (bit_size && !(bit_size & 0x7u))
941 return CompilerType(
942 ast, ast->getExtVectorType(ast->UnsignedCharTy, bit_size / 8));
943 break;
944 }
945
946 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000947}
948
Greg Clayton57ee3062013-07-11 22:46:58 +0000949lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000950ClangASTContext::GetBasicTypeEnumeration(const ConstString &name) {
951 if (name) {
952 typedef UniqueCStringMap<lldb::BasicType> TypeNameToBasicTypeMap;
953 static TypeNameToBasicTypeMap g_type_map;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000954 static llvm::once_flag g_once_flag;
955 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000956 // "void"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000957 g_type_map.Append(ConstString("void"), eBasicTypeVoid);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000958
959 // "char"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000960 g_type_map.Append(ConstString("char"), eBasicTypeChar);
961 g_type_map.Append(ConstString("signed char"), eBasicTypeSignedChar);
962 g_type_map.Append(ConstString("unsigned char"), eBasicTypeUnsignedChar);
963 g_type_map.Append(ConstString("wchar_t"), eBasicTypeWChar);
964 g_type_map.Append(ConstString("signed wchar_t"), eBasicTypeSignedWChar);
965 g_type_map.Append(ConstString("unsigned wchar_t"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000966 eBasicTypeUnsignedWChar);
967 // "short"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000968 g_type_map.Append(ConstString("short"), eBasicTypeShort);
969 g_type_map.Append(ConstString("short int"), eBasicTypeShort);
970 g_type_map.Append(ConstString("unsigned short"), eBasicTypeUnsignedShort);
971 g_type_map.Append(ConstString("unsigned short int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000972 eBasicTypeUnsignedShort);
973
974 // "int"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000975 g_type_map.Append(ConstString("int"), eBasicTypeInt);
976 g_type_map.Append(ConstString("signed int"), eBasicTypeInt);
977 g_type_map.Append(ConstString("unsigned int"), eBasicTypeUnsignedInt);
978 g_type_map.Append(ConstString("unsigned"), eBasicTypeUnsignedInt);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000979
980 // "long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000981 g_type_map.Append(ConstString("long"), eBasicTypeLong);
982 g_type_map.Append(ConstString("long int"), eBasicTypeLong);
983 g_type_map.Append(ConstString("unsigned long"), eBasicTypeUnsignedLong);
984 g_type_map.Append(ConstString("unsigned long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000985 eBasicTypeUnsignedLong);
986
987 // "long long"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000988 g_type_map.Append(ConstString("long long"), eBasicTypeLongLong);
989 g_type_map.Append(ConstString("long long int"), eBasicTypeLongLong);
990 g_type_map.Append(ConstString("unsigned long long"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000991 eBasicTypeUnsignedLongLong);
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000992 g_type_map.Append(ConstString("unsigned long long int"),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000993 eBasicTypeUnsignedLongLong);
994
995 // "int128"
Pavel Labath4d35d6b2017-05-02 10:17:30 +0000996 g_type_map.Append(ConstString("__int128_t"), eBasicTypeInt128);
997 g_type_map.Append(ConstString("__uint128_t"), eBasicTypeUnsignedInt128);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000998
999 // Miscellaneous
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001000 g_type_map.Append(ConstString("bool"), eBasicTypeBool);
1001 g_type_map.Append(ConstString("float"), eBasicTypeFloat);
1002 g_type_map.Append(ConstString("double"), eBasicTypeDouble);
1003 g_type_map.Append(ConstString("long double"), eBasicTypeLongDouble);
1004 g_type_map.Append(ConstString("id"), eBasicTypeObjCID);
1005 g_type_map.Append(ConstString("SEL"), eBasicTypeObjCSel);
1006 g_type_map.Append(ConstString("nullptr"), eBasicTypeNullPtr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001007 g_type_map.Sort();
1008 });
1009
Pavel Labath4d35d6b2017-05-02 10:17:30 +00001010 return g_type_map.Find(name, eBasicTypeInvalid);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001011 }
1012 return eBasicTypeInvalid;
Greg Clayton57ee3062013-07-11 22:46:58 +00001013}
1014
Kate Stoneb9c1b512016-09-06 20:57:50 +00001015CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1016 const ConstString &name) {
1017 if (ast) {
1018 lldb::BasicType basic_type = ClangASTContext::GetBasicTypeEnumeration(name);
1019 return ClangASTContext::GetBasicType(ast, basic_type);
1020 }
1021 return CompilerType();
1022}
1023
1024uint32_t ClangASTContext::GetPointerByteSize() {
1025 if (m_pointer_byte_size == 0)
1026 m_pointer_byte_size = GetBasicType(lldb::eBasicTypeVoid)
1027 .GetPointerType()
1028 .GetByteSize(nullptr);
1029 return m_pointer_byte_size;
1030}
1031
1032CompilerType ClangASTContext::GetBasicType(lldb::BasicType basic_type) {
1033 return GetBasicType(getASTContext(), basic_type);
1034}
1035
1036CompilerType ClangASTContext::GetBasicType(ASTContext *ast,
1037 lldb::BasicType basic_type) {
1038 if (!ast)
Greg Claytona1e5dc82015-08-11 22:53:00 +00001039 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001040 lldb::opaque_compiler_type_t clang_type =
1041 GetOpaqueCompilerType(ast, basic_type);
1042
1043 if (clang_type)
1044 return CompilerType(GetASTContext(ast), clang_type);
1045 return CompilerType();
Greg Clayton57ee3062013-07-11 22:46:58 +00001046}
1047
Kate Stoneb9c1b512016-09-06 20:57:50 +00001048CompilerType ClangASTContext::GetBuiltinTypeForDWARFEncodingAndBitSize(
1049 const char *type_name, uint32_t dw_ate, uint32_t bit_size) {
1050 ASTContext *ast = getASTContext();
Greg Clayton57ee3062013-07-11 22:46:58 +00001051
Kate Stoneb9c1b512016-09-06 20:57:50 +00001052#define streq(a, b) strcmp(a, b) == 0
1053 assert(ast != nullptr);
1054 if (ast) {
1055 switch (dw_ate) {
1056 default:
1057 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001058
Kate Stoneb9c1b512016-09-06 20:57:50 +00001059 case DW_ATE_address:
1060 if (QualTypeMatchesBitSize(bit_size, ast, ast->VoidPtrTy))
1061 return CompilerType(ast, ast->VoidPtrTy);
1062 break;
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001063
Kate Stoneb9c1b512016-09-06 20:57:50 +00001064 case DW_ATE_boolean:
1065 if (QualTypeMatchesBitSize(bit_size, ast, ast->BoolTy))
1066 return CompilerType(ast, ast->BoolTy);
1067 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1068 return CompilerType(ast, ast->UnsignedCharTy);
1069 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1070 return CompilerType(ast, ast->UnsignedShortTy);
1071 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1072 return CompilerType(ast, ast->UnsignedIntTy);
1073 break;
Greg Clayton57ee3062013-07-11 22:46:58 +00001074
Kate Stoneb9c1b512016-09-06 20:57:50 +00001075 case DW_ATE_lo_user:
1076 // This has been seen to mean DW_AT_complex_integer
1077 if (type_name) {
1078 if (::strstr(type_name, "complex")) {
1079 CompilerType complex_int_clang_type =
1080 GetBuiltinTypeForDWARFEncodingAndBitSize("int", DW_ATE_signed,
1081 bit_size / 2);
1082 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1083 complex_int_clang_type)));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001084 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001085 }
1086 break;
1087
1088 case DW_ATE_complex_float:
1089 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatComplexTy))
1090 return CompilerType(ast, ast->FloatComplexTy);
1091 else if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleComplexTy))
1092 return CompilerType(ast, ast->DoubleComplexTy);
1093 else if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleComplexTy))
1094 return CompilerType(ast, ast->LongDoubleComplexTy);
1095 else {
1096 CompilerType complex_float_clang_type =
1097 GetBuiltinTypeForDWARFEncodingAndBitSize("float", DW_ATE_float,
1098 bit_size / 2);
1099 return CompilerType(ast, ast->getComplexType(ClangUtil::GetQualType(
1100 complex_float_clang_type)));
1101 }
1102 break;
1103
1104 case DW_ATE_float:
1105 if (streq(type_name, "float") &&
1106 QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1107 return CompilerType(ast, ast->FloatTy);
1108 if (streq(type_name, "double") &&
1109 QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1110 return CompilerType(ast, ast->DoubleTy);
1111 if (streq(type_name, "long double") &&
1112 QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1113 return CompilerType(ast, ast->LongDoubleTy);
1114 // Fall back to not requiring a name match
1115 if (QualTypeMatchesBitSize(bit_size, ast, ast->FloatTy))
1116 return CompilerType(ast, ast->FloatTy);
1117 if (QualTypeMatchesBitSize(bit_size, ast, ast->DoubleTy))
1118 return CompilerType(ast, ast->DoubleTy);
1119 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongDoubleTy))
1120 return CompilerType(ast, ast->LongDoubleTy);
1121 if (QualTypeMatchesBitSize(bit_size, ast, ast->HalfTy))
1122 return CompilerType(ast, ast->HalfTy);
1123 break;
1124
1125 case DW_ATE_signed:
1126 if (type_name) {
1127 if (streq(type_name, "wchar_t") &&
1128 QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy) &&
1129 (getTargetInfo() &&
1130 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1131 return CompilerType(ast, ast->WCharTy);
1132 if (streq(type_name, "void") &&
1133 QualTypeMatchesBitSize(bit_size, ast, ast->VoidTy))
1134 return CompilerType(ast, ast->VoidTy);
1135 if (strstr(type_name, "long long") &&
1136 QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1137 return CompilerType(ast, ast->LongLongTy);
1138 if (strstr(type_name, "long") &&
1139 QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1140 return CompilerType(ast, ast->LongTy);
1141 if (strstr(type_name, "short") &&
1142 QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1143 return CompilerType(ast, ast->ShortTy);
1144 if (strstr(type_name, "char")) {
1145 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1146 return CompilerType(ast, ast->CharTy);
1147 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1148 return CompilerType(ast, ast->SignedCharTy);
1149 }
1150 if (strstr(type_name, "int")) {
1151 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1152 return CompilerType(ast, ast->IntTy);
1153 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1154 return CompilerType(ast, ast->Int128Ty);
1155 }
1156 }
1157 // We weren't able to match up a type name, just search by size
1158 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1159 return CompilerType(ast, ast->CharTy);
1160 if (QualTypeMatchesBitSize(bit_size, ast, ast->ShortTy))
1161 return CompilerType(ast, ast->ShortTy);
1162 if (QualTypeMatchesBitSize(bit_size, ast, ast->IntTy))
1163 return CompilerType(ast, ast->IntTy);
1164 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongTy))
1165 return CompilerType(ast, ast->LongTy);
1166 if (QualTypeMatchesBitSize(bit_size, ast, ast->LongLongTy))
1167 return CompilerType(ast, ast->LongLongTy);
1168 if (QualTypeMatchesBitSize(bit_size, ast, ast->Int128Ty))
1169 return CompilerType(ast, ast->Int128Ty);
1170 break;
1171
1172 case DW_ATE_signed_char:
1173 if (ast->getLangOpts().CharIsSigned && type_name &&
1174 streq(type_name, "char")) {
1175 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1176 return CompilerType(ast, ast->CharTy);
1177 }
1178 if (QualTypeMatchesBitSize(bit_size, ast, ast->SignedCharTy))
1179 return CompilerType(ast, ast->SignedCharTy);
1180 break;
1181
1182 case DW_ATE_unsigned:
1183 if (type_name) {
1184 if (streq(type_name, "wchar_t")) {
1185 if (QualTypeMatchesBitSize(bit_size, ast, ast->WCharTy)) {
1186 if (!(getTargetInfo() &&
1187 TargetInfo::isTypeSigned(getTargetInfo()->getWCharType())))
1188 return CompilerType(ast, ast->WCharTy);
1189 }
1190 }
1191 if (strstr(type_name, "long long")) {
1192 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1193 return CompilerType(ast, ast->UnsignedLongLongTy);
1194 } else if (strstr(type_name, "long")) {
1195 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1196 return CompilerType(ast, ast->UnsignedLongTy);
1197 } else if (strstr(type_name, "short")) {
1198 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1199 return CompilerType(ast, ast->UnsignedShortTy);
1200 } else if (strstr(type_name, "char")) {
1201 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1202 return CompilerType(ast, ast->UnsignedCharTy);
1203 } else if (strstr(type_name, "int")) {
1204 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1205 return CompilerType(ast, ast->UnsignedIntTy);
1206 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1207 return CompilerType(ast, ast->UnsignedInt128Ty);
1208 }
1209 }
1210 // We weren't able to match up a type name, just search by size
1211 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1212 return CompilerType(ast, ast->UnsignedCharTy);
1213 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1214 return CompilerType(ast, ast->UnsignedShortTy);
1215 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedIntTy))
1216 return CompilerType(ast, ast->UnsignedIntTy);
1217 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongTy))
1218 return CompilerType(ast, ast->UnsignedLongTy);
1219 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedLongLongTy))
1220 return CompilerType(ast, ast->UnsignedLongLongTy);
1221 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedInt128Ty))
1222 return CompilerType(ast, ast->UnsignedInt128Ty);
1223 break;
1224
1225 case DW_ATE_unsigned_char:
1226 if (!ast->getLangOpts().CharIsSigned && type_name &&
1227 streq(type_name, "char")) {
1228 if (QualTypeMatchesBitSize(bit_size, ast, ast->CharTy))
1229 return CompilerType(ast, ast->CharTy);
1230 }
1231 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedCharTy))
1232 return CompilerType(ast, ast->UnsignedCharTy);
1233 if (QualTypeMatchesBitSize(bit_size, ast, ast->UnsignedShortTy))
1234 return CompilerType(ast, ast->UnsignedShortTy);
1235 break;
1236
1237 case DW_ATE_imaginary_float:
1238 break;
1239
1240 case DW_ATE_UTF:
1241 if (type_name) {
1242 if (streq(type_name, "char16_t")) {
1243 return CompilerType(ast, ast->Char16Ty);
1244 } else if (streq(type_name, "char32_t")) {
1245 return CompilerType(ast, ast->Char32Ty);
1246 }
1247 }
1248 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001249 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001250 }
1251 // This assert should fire for anything that we don't catch above so we know
1252 // to fix any issues we run into.
1253 if (type_name) {
1254 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1255 "DW_TAG_base_type '%s' encoded with "
1256 "DW_ATE = 0x%x, bit_size = %u\n",
1257 type_name, dw_ate, bit_size);
1258 } else {
1259 Host::SystemLog(Host::eSystemLogError, "error: need to add support for "
1260 "DW_TAG_base_type encoded with "
1261 "DW_ATE = 0x%x, bit_size = %u\n",
1262 dw_ate, bit_size);
1263 }
1264 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001265}
1266
Kate Stoneb9c1b512016-09-06 20:57:50 +00001267CompilerType ClangASTContext::GetUnknownAnyType(clang::ASTContext *ast) {
1268 if (ast)
1269 return CompilerType(ast, ast->UnknownAnyTy);
1270 return CompilerType();
Sean Callanan77502262011-05-12 23:54:16 +00001271}
1272
Kate Stoneb9c1b512016-09-06 20:57:50 +00001273CompilerType ClangASTContext::GetCStringType(bool is_const) {
1274 ASTContext *ast = getASTContext();
1275 QualType char_type(ast->CharTy);
1276
1277 if (is_const)
1278 char_type.addConst();
1279
1280 return CompilerType(ast, ast->getPointerType(char_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001281}
1282
Sean Callanan09ab4b72011-11-30 22:11:59 +00001283clang::DeclContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001284ClangASTContext::GetTranslationUnitDecl(clang::ASTContext *ast) {
1285 return ast->getTranslationUnitDecl();
Sean Callanan09ab4b72011-11-30 22:11:59 +00001286}
1287
Kate Stoneb9c1b512016-09-06 20:57:50 +00001288clang::Decl *ClangASTContext::CopyDecl(ASTContext *dst_ast, ASTContext *src_ast,
1289 clang::Decl *source_decl) {
1290 FileSystemOptions file_system_options;
1291 FileManager file_manager(file_system_options);
1292 ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
1293
1294 return importer.Import(source_decl);
Greg Clayton526e5af2010-11-13 03:52:47 +00001295}
1296
Kate Stoneb9c1b512016-09-06 20:57:50 +00001297bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,
1298 bool ignore_qualifiers) {
1299 ClangASTContext *ast =
1300 llvm::dyn_cast_or_null<ClangASTContext>(type1.GetTypeSystem());
1301 if (!ast || ast != type2.GetTypeSystem())
1302 return false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001303
Kate Stoneb9c1b512016-09-06 20:57:50 +00001304 if (type1.GetOpaqueQualType() == type2.GetOpaqueQualType())
1305 return true;
Greg Clayton55995eb2012-04-06 17:38:55 +00001306
Kate Stoneb9c1b512016-09-06 20:57:50 +00001307 QualType type1_qual = ClangUtil::GetQualType(type1);
1308 QualType type2_qual = ClangUtil::GetQualType(type2);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00001309
Kate Stoneb9c1b512016-09-06 20:57:50 +00001310 if (ignore_qualifiers) {
1311 type1_qual = type1_qual.getUnqualifiedType();
1312 type2_qual = type2_qual.getUnqualifiedType();
1313 }
1314
1315 return ast->getASTContext()->hasSameType(type1_qual, type2_qual);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001316}
1317
Kate Stoneb9c1b512016-09-06 20:57:50 +00001318CompilerType ClangASTContext::GetTypeForDecl(clang::NamedDecl *decl) {
1319 if (clang::ObjCInterfaceDecl *interface_decl =
1320 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl))
1321 return GetTypeForDecl(interface_decl);
1322 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl))
1323 return GetTypeForDecl(tag_decl);
1324 return CompilerType();
Sean Callanan9998acd2014-12-05 01:21:59 +00001325}
1326
Kate Stoneb9c1b512016-09-06 20:57:50 +00001327CompilerType ClangASTContext::GetTypeForDecl(TagDecl *decl) {
1328 // No need to call the getASTContext() accessor (which can create the AST
1329 // if it isn't created yet, because we can't have created a decl in this
1330 // AST if our AST didn't already exist...
1331 ASTContext *ast = &decl->getASTContext();
1332 if (ast)
1333 return CompilerType(ast, ast->getTagDeclType(decl));
1334 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001335}
1336
Kate Stoneb9c1b512016-09-06 20:57:50 +00001337CompilerType ClangASTContext::GetTypeForDecl(ObjCInterfaceDecl *decl) {
1338 // No need to call the getASTContext() accessor (which can create the AST
1339 // if it isn't created yet, because we can't have created a decl in this
1340 // AST if our AST didn't already exist...
1341 ASTContext *ast = &decl->getASTContext();
1342 if (ast)
1343 return CompilerType(ast, ast->getObjCInterfaceType(decl));
1344 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001345}
1346
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001347#pragma mark Structure, Unions, Classes
1348
Kate Stoneb9c1b512016-09-06 20:57:50 +00001349CompilerType ClangASTContext::CreateRecordType(DeclContext *decl_ctx,
1350 AccessType access_type,
1351 const char *name, int kind,
1352 LanguageType language,
1353 ClangASTMetadata *metadata) {
1354 ASTContext *ast = getASTContext();
1355 assert(ast != nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001356
Kate Stoneb9c1b512016-09-06 20:57:50 +00001357 if (decl_ctx == nullptr)
1358 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton9e409562010-07-28 02:04:09 +00001359
Kate Stoneb9c1b512016-09-06 20:57:50 +00001360 if (language == eLanguageTypeObjC ||
1361 language == eLanguageTypeObjC_plus_plus) {
1362 bool isForwardDecl = true;
1363 bool isInternal = false;
1364 return CreateObjCClass(name, decl_ctx, isForwardDecl, isInternal, metadata);
1365 }
Greg Clayton9e409562010-07-28 02:04:09 +00001366
Kate Stoneb9c1b512016-09-06 20:57:50 +00001367 // NOTE: Eventually CXXRecordDecl will be merged back into RecordDecl and
1368 // we will need to update this code. I was told to currently always use
1369 // the CXXRecordDecl class since we often don't know from debug information
1370 // if something is struct or a class, so we default to always use the more
1371 // complete definition just in case.
Greg Claytonc4ffd662013-03-08 01:37:30 +00001372
Kate Stoneb9c1b512016-09-06 20:57:50 +00001373 bool is_anonymous = (!name) || (!name[0]);
Greg Claytonc4ffd662013-03-08 01:37:30 +00001374
Kate Stoneb9c1b512016-09-06 20:57:50 +00001375 CXXRecordDecl *decl = CXXRecordDecl::Create(
1376 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1377 SourceLocation(), is_anonymous ? nullptr : &ast->Idents.get(name));
1378
1379 if (is_anonymous)
1380 decl->setAnonymousStructOrUnion(true);
1381
1382 if (decl) {
1383 if (metadata)
1384 SetMetadata(ast, decl, *metadata);
1385
1386 if (access_type != eAccessNone)
1387 decl->setAccess(ConvertAccessTypeToAccessSpecifier(access_type));
1388
1389 if (decl_ctx)
1390 decl_ctx->addDecl(decl);
1391
1392 return CompilerType(ast, ast->getTagDeclType(decl));
1393 }
1394 return CompilerType();
Greg Clayton6beaaa62011-01-17 03:46:26 +00001395}
1396
Kate Stoneb9c1b512016-09-06 20:57:50 +00001397static TemplateParameterList *CreateTemplateParameterList(
1398 ASTContext *ast,
1399 const ClangASTContext::TemplateParameterInfos &template_param_infos,
1400 llvm::SmallVector<NamedDecl *, 8> &template_param_decls) {
1401 const bool parameter_pack = false;
1402 const bool is_typename = false;
1403 const unsigned depth = 0;
1404 const size_t num_template_params = template_param_infos.GetSize();
1405 for (size_t i = 0; i < num_template_params; ++i) {
1406 const char *name = template_param_infos.names[i];
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001407
Kate Stoneb9c1b512016-09-06 20:57:50 +00001408 IdentifierInfo *identifier_info = nullptr;
1409 if (name && name[0])
1410 identifier_info = &ast->Idents.get(name);
1411 if (template_param_infos.args[i].getKind() == TemplateArgument::Integral) {
1412 template_param_decls.push_back(NonTypeTemplateParmDecl::Create(
1413 *ast,
1414 ast->getTranslationUnitDecl(), // Is this the right decl context?,
1415 // SourceLocation StartLoc,
1416 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1417 template_param_infos.args[i].getIntegralType(), parameter_pack,
1418 nullptr));
1419
1420 } else {
1421 template_param_decls.push_back(TemplateTypeParmDecl::Create(
1422 *ast,
1423 ast->getTranslationUnitDecl(), // Is this the right decl context?
1424 SourceLocation(), SourceLocation(), depth, i, identifier_info,
1425 is_typename, parameter_pack));
1426 }
1427 }
1428
1429 clang::Expr *const requires_clause = nullptr; // TODO: Concepts
1430 TemplateParameterList *template_param_list = TemplateParameterList::Create(
1431 *ast, SourceLocation(), SourceLocation(), template_param_decls,
1432 SourceLocation(), requires_clause);
1433 return template_param_list;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001434}
1435
Kate Stoneb9c1b512016-09-06 20:57:50 +00001436clang::FunctionTemplateDecl *ClangASTContext::CreateFunctionTemplateDecl(
1437 clang::DeclContext *decl_ctx, clang::FunctionDecl *func_decl,
1438 const char *name, const TemplateParameterInfos &template_param_infos) {
1439 // /// \brief Create a function template node.
1440 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001441
Kate Stoneb9c1b512016-09-06 20:57:50 +00001442 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001443
Kate Stoneb9c1b512016-09-06 20:57:50 +00001444 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1445 ast, template_param_infos, template_param_decls);
1446 FunctionTemplateDecl *func_tmpl_decl = FunctionTemplateDecl::Create(
1447 *ast, decl_ctx, func_decl->getLocation(), func_decl->getDeclName(),
1448 template_param_list, func_decl);
1449
1450 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1451 i < template_param_decl_count; ++i) {
1452 // TODO: verify which decl context we should put template_param_decls into..
1453 template_param_decls[i]->setDeclContext(func_decl);
1454 }
1455
1456 return func_tmpl_decl;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001457}
1458
Kate Stoneb9c1b512016-09-06 20:57:50 +00001459void ClangASTContext::CreateFunctionTemplateSpecializationInfo(
1460 FunctionDecl *func_decl, clang::FunctionTemplateDecl *func_tmpl_decl,
1461 const TemplateParameterInfos &infos) {
1462 TemplateArgumentList template_args(TemplateArgumentList::OnStack, infos.args);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001463
Kate Stoneb9c1b512016-09-06 20:57:50 +00001464 func_decl->setFunctionTemplateSpecialization(func_tmpl_decl, &template_args,
1465 nullptr);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001466}
1467
Kate Stoneb9c1b512016-09-06 20:57:50 +00001468ClassTemplateDecl *ClangASTContext::CreateClassTemplateDecl(
1469 DeclContext *decl_ctx, lldb::AccessType access_type, const char *class_name,
1470 int kind, const TemplateParameterInfos &template_param_infos) {
1471 ASTContext *ast = getASTContext();
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001472
Kate Stoneb9c1b512016-09-06 20:57:50 +00001473 ClassTemplateDecl *class_template_decl = nullptr;
1474 if (decl_ctx == nullptr)
1475 decl_ctx = ast->getTranslationUnitDecl();
Greg Claytonf0705c82011-10-22 03:33:13 +00001476
Kate Stoneb9c1b512016-09-06 20:57:50 +00001477 IdentifierInfo &identifier_info = ast->Idents.get(class_name);
1478 DeclarationName decl_name(&identifier_info);
Greg Claytonf0705c82011-10-22 03:33:13 +00001479
Kate Stoneb9c1b512016-09-06 20:57:50 +00001480 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
Greg Claytonf0705c82011-10-22 03:33:13 +00001481
Kate Stoneb9c1b512016-09-06 20:57:50 +00001482 for (NamedDecl *decl : result) {
1483 class_template_decl = dyn_cast<clang::ClassTemplateDecl>(decl);
Greg Claytonf0705c82011-10-22 03:33:13 +00001484 if (class_template_decl)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001485 return class_template_decl;
1486 }
1487
1488 llvm::SmallVector<NamedDecl *, 8> template_param_decls;
1489
1490 TemplateParameterList *template_param_list = CreateTemplateParameterList(
1491 ast, template_param_infos, template_param_decls);
1492
1493 CXXRecordDecl *template_cxx_decl = CXXRecordDecl::Create(
1494 *ast, (TagDecl::TagKind)kind,
1495 decl_ctx, // What decl context do we use here? TU? The actual decl
1496 // context?
1497 SourceLocation(), SourceLocation(), &identifier_info);
1498
1499 for (size_t i = 0, template_param_decl_count = template_param_decls.size();
1500 i < template_param_decl_count; ++i) {
1501 template_param_decls[i]->setDeclContext(template_cxx_decl);
1502 }
1503
1504 // With templated classes, we say that a class is templated with
1505 // specializations, but that the bare class has no functions.
1506 // template_cxx_decl->startDefinition();
1507 // template_cxx_decl->completeDefinition();
1508
1509 class_template_decl = ClassTemplateDecl::Create(
1510 *ast,
1511 decl_ctx, // What decl context do we use here? TU? The actual decl
1512 // context?
Pavel Labath4294de32017-01-12 10:44:16 +00001513 SourceLocation(), decl_name, template_param_list, template_cxx_decl);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001514
1515 if (class_template_decl) {
1516 if (access_type != eAccessNone)
1517 class_template_decl->setAccess(
1518 ConvertAccessTypeToAccessSpecifier(access_type));
1519
1520 // if (TagDecl *ctx_tag_decl = dyn_cast<TagDecl>(decl_ctx))
1521 // CompleteTagDeclarationDefinition(GetTypeForDecl(ctx_tag_decl));
1522
1523 decl_ctx->addDecl(class_template_decl);
1524
Sean Callanan5e9e1992011-10-26 01:06:27 +00001525#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001526 VerifyDecl(class_template_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001527#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001528 }
Greg Claytonf0705c82011-10-22 03:33:13 +00001529
Kate Stoneb9c1b512016-09-06 20:57:50 +00001530 return class_template_decl;
Greg Claytonf0705c82011-10-22 03:33:13 +00001531}
1532
Greg Claytonf0705c82011-10-22 03:33:13 +00001533ClassTemplateSpecializationDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001534ClangASTContext::CreateClassTemplateSpecializationDecl(
1535 DeclContext *decl_ctx, ClassTemplateDecl *class_template_decl, int kind,
1536 const TemplateParameterInfos &template_param_infos) {
1537 ASTContext *ast = getASTContext();
1538 ClassTemplateSpecializationDecl *class_template_specialization_decl =
1539 ClassTemplateSpecializationDecl::Create(
1540 *ast, (TagDecl::TagKind)kind, decl_ctx, SourceLocation(),
1541 SourceLocation(), class_template_decl, template_param_infos.args,
1542 nullptr);
1543
1544 class_template_specialization_decl->setSpecializationKind(
1545 TSK_ExplicitSpecialization);
1546
1547 return class_template_specialization_decl;
1548}
1549
1550CompilerType ClangASTContext::CreateClassTemplateSpecializationType(
1551 ClassTemplateSpecializationDecl *class_template_specialization_decl) {
1552 if (class_template_specialization_decl) {
Greg Claytonf0705c82011-10-22 03:33:13 +00001553 ASTContext *ast = getASTContext();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001554 if (ast)
1555 return CompilerType(
1556 ast, ast->getTagDeclType(class_template_specialization_decl));
1557 }
1558 return CompilerType();
Greg Claytonf0705c82011-10-22 03:33:13 +00001559}
1560
Kate Stoneb9c1b512016-09-06 20:57:50 +00001561static inline bool check_op_param(bool is_method,
1562 clang::OverloadedOperatorKind op_kind,
1563 bool unary, bool binary,
1564 uint32_t num_params) {
1565 // Special-case call since it can take any number of operands
1566 if (op_kind == OO_Call)
1567 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001568
Kate Stoneb9c1b512016-09-06 20:57:50 +00001569 // The parameter count doesn't include "this"
1570 if (is_method)
1571 ++num_params;
1572 if (num_params == 1)
1573 return unary;
1574 if (num_params == 2)
1575 return binary;
1576 else
Greg Clayton090d0982011-06-19 03:43:27 +00001577 return false;
1578}
Daniel Dunbardacdfb52011-10-31 22:50:57 +00001579
Kate Stoneb9c1b512016-09-06 20:57:50 +00001580bool ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1581 bool is_method, clang::OverloadedOperatorKind op_kind,
1582 uint32_t num_params) {
1583 switch (op_kind) {
1584 default:
1585 break;
1586 // C++ standard allows any number of arguments to new/delete
1587 case OO_New:
1588 case OO_Array_New:
1589 case OO_Delete:
1590 case OO_Array_Delete:
1591 return true;
1592 }
Pavel Labath1ac2b202016-08-15 14:32:32 +00001593
Kate Stoneb9c1b512016-09-06 20:57:50 +00001594#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
1595 case OO_##Name: \
1596 return check_op_param(is_method, op_kind, Unary, Binary, num_params);
1597 switch (op_kind) {
Greg Clayton090d0982011-06-19 03:43:27 +00001598#include "clang/Basic/OperatorKinds.def"
Kate Stoneb9c1b512016-09-06 20:57:50 +00001599 default:
1600 break;
1601 }
1602 return false;
Greg Clayton090d0982011-06-19 03:43:27 +00001603}
1604
Greg Clayton57ee3062013-07-11 22:46:58 +00001605clang::AccessSpecifier
Kate Stoneb9c1b512016-09-06 20:57:50 +00001606ClangASTContext::UnifyAccessSpecifiers(clang::AccessSpecifier lhs,
1607 clang::AccessSpecifier rhs) {
1608 // Make the access equal to the stricter of the field and the nested field's
1609 // access
1610 if (lhs == AS_none || rhs == AS_none)
1611 return AS_none;
1612 if (lhs == AS_private || rhs == AS_private)
1613 return AS_private;
1614 if (lhs == AS_protected || rhs == AS_protected)
1615 return AS_protected;
1616 return AS_public;
Sean Callanane8c0cfb2012-03-02 01:03:45 +00001617}
1618
Kate Stoneb9c1b512016-09-06 20:57:50 +00001619bool ClangASTContext::FieldIsBitfield(FieldDecl *field,
1620 uint32_t &bitfield_bit_size) {
1621 return FieldIsBitfield(getASTContext(), field, bitfield_bit_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001622}
1623
Kate Stoneb9c1b512016-09-06 20:57:50 +00001624bool ClangASTContext::FieldIsBitfield(ASTContext *ast, FieldDecl *field,
1625 uint32_t &bitfield_bit_size) {
1626 if (ast == nullptr || field == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001627 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001628
Kate Stoneb9c1b512016-09-06 20:57:50 +00001629 if (field->isBitField()) {
1630 Expr *bit_width_expr = field->getBitWidth();
1631 if (bit_width_expr) {
1632 llvm::APSInt bit_width_apsint;
1633 if (bit_width_expr->isIntegerConstantExpr(bit_width_apsint, *ast)) {
1634 bitfield_bit_size = bit_width_apsint.getLimitedValue(UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001635 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001636 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001637 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001638 }
1639 return false;
1640}
1641
1642bool ClangASTContext::RecordHasFields(const RecordDecl *record_decl) {
1643 if (record_decl == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001644 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001645
1646 if (!record_decl->field_empty())
1647 return true;
1648
1649 // No fields, lets check this is a CXX record and check the base classes
1650 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
1651 if (cxx_record_decl) {
1652 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1653 for (base_class = cxx_record_decl->bases_begin(),
1654 base_class_end = cxx_record_decl->bases_end();
1655 base_class != base_class_end; ++base_class) {
1656 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(
1657 base_class->getType()->getAs<RecordType>()->getDecl());
1658 if (RecordHasFields(base_class_decl))
1659 return true;
1660 }
1661 }
1662 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001663}
1664
Greg Clayton8cf05932010-07-22 18:30:50 +00001665#pragma mark Objective C Classes
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001666
Kate Stoneb9c1b512016-09-06 20:57:50 +00001667CompilerType ClangASTContext::CreateObjCClass(const char *name,
1668 DeclContext *decl_ctx,
1669 bool isForwardDecl,
1670 bool isInternal,
1671 ClangASTMetadata *metadata) {
1672 ASTContext *ast = getASTContext();
1673 assert(ast != nullptr);
1674 assert(name && name[0]);
1675 if (decl_ctx == nullptr)
1676 decl_ctx = ast->getTranslationUnitDecl();
Greg Clayton8cf05932010-07-22 18:30:50 +00001677
Kate Stoneb9c1b512016-09-06 20:57:50 +00001678 ObjCInterfaceDecl *decl = ObjCInterfaceDecl::Create(
1679 *ast, decl_ctx, SourceLocation(), &ast->Idents.get(name), nullptr,
1680 nullptr, SourceLocation(),
1681 /*isForwardDecl,*/
1682 isInternal);
1683
1684 if (decl && metadata)
1685 SetMetadata(ast, decl, *metadata);
1686
1687 return CompilerType(ast, ast->getObjCInterfaceType(decl));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001688}
1689
Kate Stoneb9c1b512016-09-06 20:57:50 +00001690static inline bool BaseSpecifierIsEmpty(const CXXBaseSpecifier *b) {
1691 return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) ==
1692 false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001693}
1694
Greg Clayton57ee3062013-07-11 22:46:58 +00001695uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001696ClangASTContext::GetNumBaseClasses(const CXXRecordDecl *cxx_record_decl,
1697 bool omit_empty_base_classes) {
1698 uint32_t num_bases = 0;
1699 if (cxx_record_decl) {
1700 if (omit_empty_base_classes) {
1701 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
1702 for (base_class = cxx_record_decl->bases_begin(),
1703 base_class_end = cxx_record_decl->bases_end();
1704 base_class != base_class_end; ++base_class) {
1705 // Skip empty base classes
1706 if (omit_empty_base_classes) {
1707 if (BaseSpecifierIsEmpty(base_class))
1708 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001709 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001710 ++num_bases;
1711 }
1712 } else
1713 num_bases = cxx_record_decl->getNumBases();
1714 }
1715 return num_bases;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001716}
1717
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001718#pragma mark Namespace Declarations
1719
1720NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001721ClangASTContext::GetUniqueNamespaceDeclaration(const char *name,
1722 DeclContext *decl_ctx) {
1723 NamespaceDecl *namespace_decl = nullptr;
1724 ASTContext *ast = getASTContext();
1725 TranslationUnitDecl *translation_unit_decl = ast->getTranslationUnitDecl();
1726 if (decl_ctx == nullptr)
1727 decl_ctx = translation_unit_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00001728
Kate Stoneb9c1b512016-09-06 20:57:50 +00001729 if (name) {
1730 IdentifierInfo &identifier_info = ast->Idents.get(name);
1731 DeclarationName decl_name(&identifier_info);
1732 clang::DeclContext::lookup_result result = decl_ctx->lookup(decl_name);
1733 for (NamedDecl *decl : result) {
1734 namespace_decl = dyn_cast<clang::NamespaceDecl>(decl);
1735 if (namespace_decl)
1736 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001737 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001738
1739 namespace_decl =
1740 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1741 SourceLocation(), &identifier_info, nullptr);
1742
1743 decl_ctx->addDecl(namespace_decl);
1744 } else {
1745 if (decl_ctx == translation_unit_decl) {
1746 namespace_decl = translation_unit_decl->getAnonymousNamespace();
1747 if (namespace_decl)
1748 return namespace_decl;
1749
1750 namespace_decl =
1751 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1752 SourceLocation(), nullptr, nullptr);
1753 translation_unit_decl->setAnonymousNamespace(namespace_decl);
1754 translation_unit_decl->addDecl(namespace_decl);
1755 assert(namespace_decl == translation_unit_decl->getAnonymousNamespace());
1756 } else {
1757 NamespaceDecl *parent_namespace_decl = cast<NamespaceDecl>(decl_ctx);
1758 if (parent_namespace_decl) {
1759 namespace_decl = parent_namespace_decl->getAnonymousNamespace();
1760 if (namespace_decl)
1761 return namespace_decl;
1762 namespace_decl =
1763 NamespaceDecl::Create(*ast, decl_ctx, false, SourceLocation(),
1764 SourceLocation(), nullptr, nullptr);
1765 parent_namespace_decl->setAnonymousNamespace(namespace_decl);
1766 parent_namespace_decl->addDecl(namespace_decl);
1767 assert(namespace_decl ==
1768 parent_namespace_decl->getAnonymousNamespace());
1769 } else {
1770 // BAD!!!
1771 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001772 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001773 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00001774#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001775 VerifyDecl(namespace_decl);
Greg Clayton9d3d6882011-10-31 23:51:19 +00001776#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001777 return namespace_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001778}
1779
Kate Stoneb9c1b512016-09-06 20:57:50 +00001780NamespaceDecl *ClangASTContext::GetUniqueNamespaceDeclaration(
1781 clang::ASTContext *ast, const char *name, clang::DeclContext *decl_ctx) {
1782 ClangASTContext *ast_ctx = ClangASTContext::GetASTContext(ast);
1783 if (ast_ctx == nullptr)
1784 return nullptr;
Siva Chandra03ff5c82016-02-05 19:10:04 +00001785
Kate Stoneb9c1b512016-09-06 20:57:50 +00001786 return ast_ctx->GetUniqueNamespaceDeclaration(name, decl_ctx);
Siva Chandra03ff5c82016-02-05 19:10:04 +00001787}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001788
Paul Hermand628cbb2015-09-15 23:44:17 +00001789clang::BlockDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001790ClangASTContext::CreateBlockDeclaration(clang::DeclContext *ctx) {
1791 if (ctx != nullptr) {
1792 clang::BlockDecl *decl = clang::BlockDecl::Create(*getASTContext(), ctx,
1793 clang::SourceLocation());
1794 ctx->addDecl(decl);
1795 return decl;
1796 }
1797 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001798}
1799
Kate Stoneb9c1b512016-09-06 20:57:50 +00001800clang::DeclContext *FindLCABetweenDecls(clang::DeclContext *left,
1801 clang::DeclContext *right,
1802 clang::DeclContext *root) {
1803 if (root == nullptr)
Paul Hermanea188fc2015-09-16 18:48:30 +00001804 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001805
1806 std::set<clang::DeclContext *> path_left;
1807 for (clang::DeclContext *d = left; d != nullptr; d = d->getParent())
1808 path_left.insert(d);
1809
1810 for (clang::DeclContext *d = right; d != nullptr; d = d->getParent())
1811 if (path_left.find(d) != path_left.end())
1812 return d;
1813
1814 return nullptr;
Paul Hermanea188fc2015-09-16 18:48:30 +00001815}
1816
Kate Stoneb9c1b512016-09-06 20:57:50 +00001817clang::UsingDirectiveDecl *ClangASTContext::CreateUsingDirectiveDeclaration(
1818 clang::DeclContext *decl_ctx, clang::NamespaceDecl *ns_decl) {
1819 if (decl_ctx != nullptr && ns_decl != nullptr) {
1820 clang::TranslationUnitDecl *translation_unit =
1821 (clang::TranslationUnitDecl *)GetTranslationUnitDecl(getASTContext());
1822 clang::UsingDirectiveDecl *using_decl = clang::UsingDirectiveDecl::Create(
1823 *getASTContext(), decl_ctx, clang::SourceLocation(),
1824 clang::SourceLocation(), clang::NestedNameSpecifierLoc(),
1825 clang::SourceLocation(), ns_decl,
1826 FindLCABetweenDecls(decl_ctx, ns_decl, translation_unit));
1827 decl_ctx->addDecl(using_decl);
1828 return using_decl;
1829 }
1830 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001831}
1832
1833clang::UsingDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00001834ClangASTContext::CreateUsingDeclaration(clang::DeclContext *current_decl_ctx,
1835 clang::NamedDecl *target) {
1836 if (current_decl_ctx != nullptr && target != nullptr) {
1837 clang::UsingDecl *using_decl = clang::UsingDecl::Create(
1838 *getASTContext(), current_decl_ctx, clang::SourceLocation(),
1839 clang::NestedNameSpecifierLoc(), clang::DeclarationNameInfo(), false);
1840 clang::UsingShadowDecl *shadow_decl = clang::UsingShadowDecl::Create(
1841 *getASTContext(), current_decl_ctx, clang::SourceLocation(), using_decl,
1842 target);
1843 using_decl->addShadowDecl(shadow_decl);
1844 current_decl_ctx->addDecl(using_decl);
1845 return using_decl;
1846 }
1847 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001848}
1849
Kate Stoneb9c1b512016-09-06 20:57:50 +00001850clang::VarDecl *ClangASTContext::CreateVariableDeclaration(
1851 clang::DeclContext *decl_context, const char *name, clang::QualType type) {
1852 if (decl_context != nullptr) {
1853 clang::VarDecl *var_decl = clang::VarDecl::Create(
1854 *getASTContext(), decl_context, clang::SourceLocation(),
1855 clang::SourceLocation(),
1856 name && name[0] ? &getASTContext()->Idents.getOwn(name) : nullptr, type,
1857 nullptr, clang::SC_None);
1858 var_decl->setAccess(clang::AS_public);
1859 decl_context->addDecl(var_decl);
1860 return var_decl;
1861 }
1862 return nullptr;
Paul Hermand628cbb2015-09-15 23:44:17 +00001863}
1864
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001865lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00001866ClangASTContext::GetOpaqueCompilerType(clang::ASTContext *ast,
1867 lldb::BasicType basic_type) {
1868 switch (basic_type) {
1869 case eBasicTypeVoid:
1870 return ast->VoidTy.getAsOpaquePtr();
1871 case eBasicTypeChar:
1872 return ast->CharTy.getAsOpaquePtr();
1873 case eBasicTypeSignedChar:
1874 return ast->SignedCharTy.getAsOpaquePtr();
1875 case eBasicTypeUnsignedChar:
1876 return ast->UnsignedCharTy.getAsOpaquePtr();
1877 case eBasicTypeWChar:
1878 return ast->getWCharType().getAsOpaquePtr();
1879 case eBasicTypeSignedWChar:
1880 return ast->getSignedWCharType().getAsOpaquePtr();
1881 case eBasicTypeUnsignedWChar:
1882 return ast->getUnsignedWCharType().getAsOpaquePtr();
1883 case eBasicTypeChar16:
1884 return ast->Char16Ty.getAsOpaquePtr();
1885 case eBasicTypeChar32:
1886 return ast->Char32Ty.getAsOpaquePtr();
1887 case eBasicTypeShort:
1888 return ast->ShortTy.getAsOpaquePtr();
1889 case eBasicTypeUnsignedShort:
1890 return ast->UnsignedShortTy.getAsOpaquePtr();
1891 case eBasicTypeInt:
1892 return ast->IntTy.getAsOpaquePtr();
1893 case eBasicTypeUnsignedInt:
1894 return ast->UnsignedIntTy.getAsOpaquePtr();
1895 case eBasicTypeLong:
1896 return ast->LongTy.getAsOpaquePtr();
1897 case eBasicTypeUnsignedLong:
1898 return ast->UnsignedLongTy.getAsOpaquePtr();
1899 case eBasicTypeLongLong:
1900 return ast->LongLongTy.getAsOpaquePtr();
1901 case eBasicTypeUnsignedLongLong:
1902 return ast->UnsignedLongLongTy.getAsOpaquePtr();
1903 case eBasicTypeInt128:
1904 return ast->Int128Ty.getAsOpaquePtr();
1905 case eBasicTypeUnsignedInt128:
1906 return ast->UnsignedInt128Ty.getAsOpaquePtr();
1907 case eBasicTypeBool:
1908 return ast->BoolTy.getAsOpaquePtr();
1909 case eBasicTypeHalf:
1910 return ast->HalfTy.getAsOpaquePtr();
1911 case eBasicTypeFloat:
1912 return ast->FloatTy.getAsOpaquePtr();
1913 case eBasicTypeDouble:
1914 return ast->DoubleTy.getAsOpaquePtr();
1915 case eBasicTypeLongDouble:
1916 return ast->LongDoubleTy.getAsOpaquePtr();
1917 case eBasicTypeFloatComplex:
1918 return ast->FloatComplexTy.getAsOpaquePtr();
1919 case eBasicTypeDoubleComplex:
1920 return ast->DoubleComplexTy.getAsOpaquePtr();
1921 case eBasicTypeLongDoubleComplex:
1922 return ast->LongDoubleComplexTy.getAsOpaquePtr();
1923 case eBasicTypeObjCID:
1924 return ast->getObjCIdType().getAsOpaquePtr();
1925 case eBasicTypeObjCClass:
1926 return ast->getObjCClassType().getAsOpaquePtr();
1927 case eBasicTypeObjCSel:
1928 return ast->getObjCSelType().getAsOpaquePtr();
1929 case eBasicTypeNullPtr:
1930 return ast->NullPtrTy.getAsOpaquePtr();
1931 default:
1932 return nullptr;
1933 }
Zachary Turner9d8a97e2016-04-01 23:20:35 +00001934}
1935
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001936#pragma mark Function Types
1937
Pavel Labath1ac2b202016-08-15 14:32:32 +00001938clang::DeclarationName
Kate Stoneb9c1b512016-09-06 20:57:50 +00001939ClangASTContext::GetDeclarationName(const char *name,
1940 const CompilerType &function_clang_type) {
1941 if (!name || !name[0])
1942 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00001943
Kate Stoneb9c1b512016-09-06 20:57:50 +00001944 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
1945 if (!IsOperator(name, op_kind) || op_kind == clang::NUM_OVERLOADED_OPERATORS)
1946 return DeclarationName(&getASTContext()->Idents.get(
1947 name)); // Not operator, but a regular function.
Pavel Labath1ac2b202016-08-15 14:32:32 +00001948
Kate Stoneb9c1b512016-09-06 20:57:50 +00001949 // Check the number of operator parameters. Sometimes we have
1950 // seen bad DWARF that doesn't correctly describe operators and
1951 // if we try to create a method and add it to the class, clang
1952 // will assert and crash, so we need to make sure things are
1953 // acceptable.
1954 clang::QualType method_qual_type(ClangUtil::GetQualType(function_clang_type));
1955 const clang::FunctionProtoType *function_type =
1956 llvm::dyn_cast<clang::FunctionProtoType>(method_qual_type.getTypePtr());
1957 if (function_type == nullptr)
1958 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00001959
Kate Stoneb9c1b512016-09-06 20:57:50 +00001960 const bool is_method = false;
1961 const unsigned int num_params = function_type->getNumParams();
1962 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
1963 is_method, op_kind, num_params))
1964 return clang::DeclarationName();
Pavel Labath1ac2b202016-08-15 14:32:32 +00001965
Kate Stoneb9c1b512016-09-06 20:57:50 +00001966 return getASTContext()->DeclarationNames.getCXXOperatorName(op_kind);
Pavel Labath1ac2b202016-08-15 14:32:32 +00001967}
1968
Kate Stoneb9c1b512016-09-06 20:57:50 +00001969FunctionDecl *ClangASTContext::CreateFunctionDeclaration(
1970 DeclContext *decl_ctx, const char *name,
1971 const CompilerType &function_clang_type, int storage, bool is_inline) {
1972 FunctionDecl *func_decl = nullptr;
1973 ASTContext *ast = getASTContext();
1974 if (decl_ctx == nullptr)
1975 decl_ctx = ast->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001976
Kate Stoneb9c1b512016-09-06 20:57:50 +00001977 const bool hasWrittenPrototype = true;
1978 const bool isConstexprSpecified = false;
Greg Clayton0d551042013-06-28 21:08:47 +00001979
Kate Stoneb9c1b512016-09-06 20:57:50 +00001980 clang::DeclarationName declarationName =
1981 GetDeclarationName(name, function_clang_type);
1982 func_decl = FunctionDecl::Create(
1983 *ast, decl_ctx, SourceLocation(), SourceLocation(), declarationName,
1984 ClangUtil::GetQualType(function_clang_type), nullptr,
1985 (clang::StorageClass)storage, is_inline, hasWrittenPrototype,
1986 isConstexprSpecified);
1987 if (func_decl)
1988 decl_ctx->addDecl(func_decl);
1989
Sean Callanan5e9e1992011-10-26 01:06:27 +00001990#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00001991 VerifyDecl(func_decl);
Sean Callanan5e9e1992011-10-26 01:06:27 +00001992#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001993
1994 return func_decl;
1995}
1996
1997CompilerType ClangASTContext::CreateFunctionType(
1998 ASTContext *ast, const CompilerType &result_type, const CompilerType *args,
1999 unsigned num_args, bool is_variadic, unsigned type_quals) {
2000 if (ast == nullptr)
2001 return CompilerType(); // invalid AST
2002
2003 if (!result_type || !ClangUtil::IsClangType(result_type))
2004 return CompilerType(); // invalid return type
2005
2006 std::vector<QualType> qual_type_args;
2007 if (num_args > 0 && args == nullptr)
2008 return CompilerType(); // invalid argument array passed in
2009
2010 // Verify that all arguments are valid and the right type
2011 for (unsigned i = 0; i < num_args; ++i) {
2012 if (args[i]) {
2013 // Make sure we have a clang type in args[i] and not a type from another
2014 // language whose name might match
2015 const bool is_clang_type = ClangUtil::IsClangType(args[i]);
2016 lldbassert(is_clang_type);
2017 if (is_clang_type)
2018 qual_type_args.push_back(ClangUtil::GetQualType(args[i]));
2019 else
2020 return CompilerType(); // invalid argument type (must be a clang type)
2021 } else
2022 return CompilerType(); // invalid argument type (empty)
2023 }
2024
2025 // TODO: Detect calling convention in DWARF?
2026 FunctionProtoType::ExtProtoInfo proto_info;
2027 proto_info.Variadic = is_variadic;
2028 proto_info.ExceptionSpec = EST_None;
2029 proto_info.TypeQuals = type_quals;
2030 proto_info.RefQualifier = RQ_None;
2031
2032 return CompilerType(ast,
2033 ast->getFunctionType(ClangUtil::GetQualType(result_type),
2034 qual_type_args, proto_info));
2035}
2036
2037ParmVarDecl *ClangASTContext::CreateParameterDeclaration(
2038 const char *name, const CompilerType &param_type, int storage) {
2039 ASTContext *ast = getASTContext();
2040 assert(ast != nullptr);
2041 return ParmVarDecl::Create(*ast, ast->getTranslationUnitDecl(),
2042 SourceLocation(), SourceLocation(),
2043 name && name[0] ? &ast->Idents.get(name) : nullptr,
2044 ClangUtil::GetQualType(param_type), nullptr,
2045 (clang::StorageClass)storage, nullptr);
2046}
2047
2048void ClangASTContext::SetFunctionParameters(FunctionDecl *function_decl,
2049 ParmVarDecl **params,
2050 unsigned num_params) {
2051 if (function_decl)
2052 function_decl->setParams(ArrayRef<ParmVarDecl *>(params, num_params));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002053}
2054
Greg Claytona1e5dc82015-08-11 22:53:00 +00002055CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002056ClangASTContext::CreateBlockPointerType(const CompilerType &function_type) {
2057 QualType block_type = m_ast_ap->getBlockPointerType(
2058 clang::QualType::getFromOpaquePtr(function_type.GetOpaqueQualType()));
Greg Claytonceeb5212016-05-26 22:33:25 +00002059
Kate Stoneb9c1b512016-09-06 20:57:50 +00002060 return CompilerType(this, block_type.getAsOpaquePtr());
Sean Callananc530ba92016-05-02 21:15:31 +00002061}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002062
2063#pragma mark Array Types
2064
Kate Stoneb9c1b512016-09-06 20:57:50 +00002065CompilerType ClangASTContext::CreateArrayType(const CompilerType &element_type,
2066 size_t element_count,
2067 bool is_vector) {
2068 if (element_type.IsValid()) {
2069 ASTContext *ast = getASTContext();
2070 assert(ast != nullptr);
Greg Clayton4ef877f2012-12-06 02:33:54 +00002071
Kate Stoneb9c1b512016-09-06 20:57:50 +00002072 if (is_vector) {
2073 return CompilerType(
2074 ast, ast->getExtVectorType(ClangUtil::GetQualType(element_type),
2075 element_count));
2076 } else {
2077
2078 llvm::APInt ap_element_count(64, element_count);
2079 if (element_count == 0) {
2080 return CompilerType(ast, ast->getIncompleteArrayType(
2081 ClangUtil::GetQualType(element_type),
2082 clang::ArrayType::Normal, 0));
2083 } else {
2084 return CompilerType(
2085 ast, ast->getConstantArrayType(ClangUtil::GetQualType(element_type),
2086 ap_element_count,
2087 clang::ArrayType::Normal, 0));
2088 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002089 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002090 }
2091 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002092}
2093
Kate Stoneb9c1b512016-09-06 20:57:50 +00002094CompilerType ClangASTContext::CreateStructForIdentifier(
2095 const ConstString &type_name,
2096 const std::initializer_list<std::pair<const char *, CompilerType>>
2097 &type_fields,
2098 bool packed) {
2099 CompilerType type;
2100 if (!type_name.IsEmpty() &&
2101 (type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name))
2102 .IsValid()) {
Pavel Labathf31c9d22017-01-05 13:18:42 +00002103 lldbassert(0 && "Trying to create a type for an existing name");
Enrico Granata76b08d52014-10-29 23:08:02 +00002104 return type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002105 }
2106
2107 type = CreateRecordType(nullptr, lldb::eAccessPublic, type_name.GetCString(),
2108 clang::TTK_Struct, lldb::eLanguageTypeC);
2109 StartTagDeclarationDefinition(type);
2110 for (const auto &field : type_fields)
2111 AddFieldToRecordType(type, field.first, field.second, lldb::eAccessPublic,
2112 0);
2113 if (packed)
2114 SetIsPacked(type);
2115 CompleteTagDeclarationDefinition(type);
2116 return type;
Enrico Granata76b08d52014-10-29 23:08:02 +00002117}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002118
Kate Stoneb9c1b512016-09-06 20:57:50 +00002119CompilerType ClangASTContext::GetOrCreateStructForIdentifier(
2120 const ConstString &type_name,
2121 const std::initializer_list<std::pair<const char *, CompilerType>>
2122 &type_fields,
2123 bool packed) {
2124 CompilerType type;
2125 if ((type = GetTypeForIdentifier<clang::CXXRecordDecl>(type_name)).IsValid())
2126 return type;
Sean Callananc530ba92016-05-02 21:15:31 +00002127
Kate Stoneb9c1b512016-09-06 20:57:50 +00002128 return CreateStructForIdentifier(type_name, type_fields, packed);
Sean Callananc530ba92016-05-02 21:15:31 +00002129}
2130
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002131#pragma mark Enumeration Types
2132
Greg Claytona1e5dc82015-08-11 22:53:00 +00002133CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00002134ClangASTContext::CreateEnumerationType(const char *name, DeclContext *decl_ctx,
2135 const Declaration &decl,
2136 const CompilerType &integer_clang_type) {
2137 // TODO: Do something intelligent with the Declaration object passed in
2138 // like maybe filling in the SourceLocation with it...
2139 ASTContext *ast = getASTContext();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00002140
Kate Stoneb9c1b512016-09-06 20:57:50 +00002141 // TODO: ask about these...
2142 // const bool IsScoped = false;
2143 // const bool IsFixed = false;
2144
2145 EnumDecl *enum_decl = EnumDecl::Create(
2146 *ast, decl_ctx, SourceLocation(), SourceLocation(),
2147 name && name[0] ? &ast->Idents.get(name) : nullptr, nullptr,
2148 false, // IsScoped
2149 false, // IsScopedUsingClassTag
2150 false); // IsFixed
2151
2152 if (enum_decl) {
2153 // TODO: check if we should be setting the promotion type too?
2154 enum_decl->setIntegerType(ClangUtil::GetQualType(integer_clang_type));
2155
2156 enum_decl->setAccess(AS_public); // TODO respect what's in the debug info
2157
2158 return CompilerType(ast, ast->getTagDeclType(enum_decl));
2159 }
2160 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002161}
2162
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002163// Disable this for now since I can't seem to get a nicely formatted float
2164// out of the APFloat class without just getting the float, double or quad
2165// and then using a formatted print on it which defeats the purpose. We ideally
2166// would like to get perfect string values for any kind of float semantics
2167// so we can support remote targets. The code below also requires a patch to
2168// llvm::APInt.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002169// bool
2170// ClangASTContext::ConvertFloatValueToString (ASTContext *ast,
2171// lldb::opaque_compiler_type_t clang_type, const uint8_t* bytes, size_t
2172// byte_size, int apint_byte_order, std::string &float_str)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002173//{
2174// uint32_t count = 0;
2175// bool is_complex = false;
2176// if (ClangASTContext::IsFloatingPointType (clang_type, count, is_complex))
2177// {
2178// unsigned num_bytes_per_float = byte_size / count;
2179// unsigned num_bits_per_float = num_bytes_per_float * 8;
2180//
2181// float_str.clear();
2182// uint32_t i;
2183// for (i=0; i<count; i++)
2184// {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002185// APInt ap_int(num_bits_per_float, bytes + i * num_bytes_per_float,
2186// (APInt::ByteOrder)apint_byte_order);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002187// bool is_ieee = false;
2188// APFloat ap_float(ap_int, is_ieee);
2189// char s[1024];
2190// unsigned int hex_digits = 0;
2191// bool upper_case = false;
2192//
Kate Stoneb9c1b512016-09-06 20:57:50 +00002193// if (ap_float.convertToHexString(s, hex_digits, upper_case,
2194// APFloat::rmNearestTiesToEven) > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002195// {
2196// if (i > 0)
2197// float_str.append(", ");
2198// float_str.append(s);
2199// if (i == 1 && is_complex)
2200// float_str.append(1, 'i');
2201// }
2202// }
2203// return !float_str.empty();
2204// }
2205// return false;
2206//}
2207
Kate Stoneb9c1b512016-09-06 20:57:50 +00002208CompilerType ClangASTContext::GetIntTypeFromBitSize(clang::ASTContext *ast,
2209 size_t bit_size,
2210 bool is_signed) {
2211 if (ast) {
2212 if (is_signed) {
2213 if (bit_size == ast->getTypeSize(ast->SignedCharTy))
2214 return CompilerType(ast, ast->SignedCharTy);
2215
2216 if (bit_size == ast->getTypeSize(ast->ShortTy))
2217 return CompilerType(ast, ast->ShortTy);
2218
2219 if (bit_size == ast->getTypeSize(ast->IntTy))
2220 return CompilerType(ast, ast->IntTy);
2221
2222 if (bit_size == ast->getTypeSize(ast->LongTy))
2223 return CompilerType(ast, ast->LongTy);
2224
2225 if (bit_size == ast->getTypeSize(ast->LongLongTy))
2226 return CompilerType(ast, ast->LongLongTy);
2227
2228 if (bit_size == ast->getTypeSize(ast->Int128Ty))
2229 return CompilerType(ast, ast->Int128Ty);
2230 } else {
2231 if (bit_size == ast->getTypeSize(ast->UnsignedCharTy))
2232 return CompilerType(ast, ast->UnsignedCharTy);
2233
2234 if (bit_size == ast->getTypeSize(ast->UnsignedShortTy))
2235 return CompilerType(ast, ast->UnsignedShortTy);
2236
2237 if (bit_size == ast->getTypeSize(ast->UnsignedIntTy))
2238 return CompilerType(ast, ast->UnsignedIntTy);
2239
2240 if (bit_size == ast->getTypeSize(ast->UnsignedLongTy))
2241 return CompilerType(ast, ast->UnsignedLongTy);
2242
2243 if (bit_size == ast->getTypeSize(ast->UnsignedLongLongTy))
2244 return CompilerType(ast, ast->UnsignedLongLongTy);
2245
2246 if (bit_size == ast->getTypeSize(ast->UnsignedInt128Ty))
2247 return CompilerType(ast, ast->UnsignedInt128Ty);
Enrico Granatae8bf7492014-08-15 23:00:02 +00002248 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002249 }
2250 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002251}
2252
Kate Stoneb9c1b512016-09-06 20:57:50 +00002253CompilerType ClangASTContext::GetPointerSizedIntType(clang::ASTContext *ast,
2254 bool is_signed) {
2255 if (ast)
2256 return GetIntTypeFromBitSize(ast, ast->getTypeSize(ast->VoidPtrTy),
2257 is_signed);
2258 return CompilerType();
Enrico Granatae8bf7492014-08-15 23:00:02 +00002259}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002260
Kate Stoneb9c1b512016-09-06 20:57:50 +00002261void ClangASTContext::DumpDeclContextHiearchy(clang::DeclContext *decl_ctx) {
2262 if (decl_ctx) {
2263 DumpDeclContextHiearchy(decl_ctx->getParent());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002264
Kate Stoneb9c1b512016-09-06 20:57:50 +00002265 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl_ctx);
2266 if (named_decl) {
2267 printf("%20s: %s\n", decl_ctx->getDeclKindName(),
2268 named_decl->getDeclName().getAsString().c_str());
2269 } else {
2270 printf("%20s\n", decl_ctx->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002271 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002272 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002273}
2274
Kate Stoneb9c1b512016-09-06 20:57:50 +00002275void ClangASTContext::DumpDeclHiearchy(clang::Decl *decl) {
2276 if (decl == nullptr)
2277 return;
2278 DumpDeclContextHiearchy(decl->getDeclContext());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002279
Kate Stoneb9c1b512016-09-06 20:57:50 +00002280 clang::RecordDecl *record_decl = llvm::dyn_cast<clang::RecordDecl>(decl);
2281 if (record_decl) {
2282 printf("%20s: %s%s\n", decl->getDeclKindName(),
2283 record_decl->getDeclName().getAsString().c_str(),
2284 record_decl->isInjectedClassName() ? " (injected class name)" : "");
Greg Claytone6b36cd2015-12-08 01:02:08 +00002285
Kate Stoneb9c1b512016-09-06 20:57:50 +00002286 } else {
2287 clang::NamedDecl *named_decl = llvm::dyn_cast<clang::NamedDecl>(decl);
2288 if (named_decl) {
2289 printf("%20s: %s\n", decl->getDeclKindName(),
2290 named_decl->getDeclName().getAsString().c_str());
2291 } else {
2292 printf("%20s\n", decl->getDeclKindName());
Greg Claytone6b36cd2015-12-08 01:02:08 +00002293 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002294 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00002295}
2296
Kate Stoneb9c1b512016-09-06 20:57:50 +00002297bool ClangASTContext::DeclsAreEquivalent(clang::Decl *lhs_decl,
2298 clang::Decl *rhs_decl) {
2299 if (lhs_decl && rhs_decl) {
2300 //----------------------------------------------------------------------
2301 // Make sure the decl kinds match first
2302 //----------------------------------------------------------------------
2303 const clang::Decl::Kind lhs_decl_kind = lhs_decl->getKind();
2304 const clang::Decl::Kind rhs_decl_kind = rhs_decl->getKind();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002305
Kate Stoneb9c1b512016-09-06 20:57:50 +00002306 if (lhs_decl_kind == rhs_decl_kind) {
2307 //------------------------------------------------------------------
2308 // Now check that the decl contexts kinds are all equivalent
2309 // before we have to check any names of the decl contexts...
2310 //------------------------------------------------------------------
2311 clang::DeclContext *lhs_decl_ctx = lhs_decl->getDeclContext();
2312 clang::DeclContext *rhs_decl_ctx = rhs_decl->getDeclContext();
2313 if (lhs_decl_ctx && rhs_decl_ctx) {
2314 while (1) {
2315 if (lhs_decl_ctx && rhs_decl_ctx) {
2316 const clang::Decl::Kind lhs_decl_ctx_kind =
2317 lhs_decl_ctx->getDeclKind();
2318 const clang::Decl::Kind rhs_decl_ctx_kind =
2319 rhs_decl_ctx->getDeclKind();
2320 if (lhs_decl_ctx_kind == rhs_decl_ctx_kind) {
2321 lhs_decl_ctx = lhs_decl_ctx->getParent();
2322 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytone6b36cd2015-12-08 01:02:08 +00002323
Kate Stoneb9c1b512016-09-06 20:57:50 +00002324 if (lhs_decl_ctx == nullptr && rhs_decl_ctx == nullptr)
2325 break;
2326 } else
2327 return false;
2328 } else
Tamas Berghammerfcf334b2015-12-02 11:35:54 +00002329 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002330 }
2331
2332 //--------------------------------------------------------------
2333 // Now make sure the name of the decls match
2334 //--------------------------------------------------------------
2335 clang::NamedDecl *lhs_named_decl =
2336 llvm::dyn_cast<clang::NamedDecl>(lhs_decl);
2337 clang::NamedDecl *rhs_named_decl =
2338 llvm::dyn_cast<clang::NamedDecl>(rhs_decl);
2339 if (lhs_named_decl && rhs_named_decl) {
2340 clang::DeclarationName lhs_decl_name = lhs_named_decl->getDeclName();
2341 clang::DeclarationName rhs_decl_name = rhs_named_decl->getDeclName();
2342 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2343 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2344 return false;
2345 } else
Greg Claytona2721472011-06-25 00:44:06 +00002346 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002347 } else
2348 return false;
Greg Claytona2721472011-06-25 00:44:06 +00002349
Kate Stoneb9c1b512016-09-06 20:57:50 +00002350 //--------------------------------------------------------------
2351 // We know that the decl context kinds all match, so now we need
2352 // to make sure the names match as well
2353 //--------------------------------------------------------------
2354 lhs_decl_ctx = lhs_decl->getDeclContext();
2355 rhs_decl_ctx = rhs_decl->getDeclContext();
2356 while (1) {
2357 switch (lhs_decl_ctx->getDeclKind()) {
2358 case clang::Decl::TranslationUnit:
2359 // We don't care about the translation unit names
2360 return true;
2361 default: {
2362 clang::NamedDecl *lhs_named_decl =
2363 llvm::dyn_cast<clang::NamedDecl>(lhs_decl_ctx);
2364 clang::NamedDecl *rhs_named_decl =
2365 llvm::dyn_cast<clang::NamedDecl>(rhs_decl_ctx);
2366 if (lhs_named_decl && rhs_named_decl) {
2367 clang::DeclarationName lhs_decl_name =
2368 lhs_named_decl->getDeclName();
2369 clang::DeclarationName rhs_decl_name =
2370 rhs_named_decl->getDeclName();
2371 if (lhs_decl_name.getNameKind() == rhs_decl_name.getNameKind()) {
2372 if (lhs_decl_name.getAsString() != rhs_decl_name.getAsString())
2373 return false;
2374 } else
2375 return false;
2376 } else
2377 return false;
2378 } break;
2379 }
2380 lhs_decl_ctx = lhs_decl_ctx->getParent();
2381 rhs_decl_ctx = rhs_decl_ctx->getParent();
Greg Claytond8d4a572015-08-11 21:38:15 +00002382 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002383 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002384 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002385 }
2386 return false;
2387}
2388bool ClangASTContext::GetCompleteDecl(clang::ASTContext *ast,
2389 clang::Decl *decl) {
2390 if (!decl)
Greg Claytond8d4a572015-08-11 21:38:15 +00002391 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002392
Kate Stoneb9c1b512016-09-06 20:57:50 +00002393 ExternalASTSource *ast_source = ast->getExternalSource();
Greg Claytond8d4a572015-08-11 21:38:15 +00002394
Kate Stoneb9c1b512016-09-06 20:57:50 +00002395 if (!ast_source)
Greg Claytond8d4a572015-08-11 21:38:15 +00002396 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002397
2398 if (clang::TagDecl *tag_decl = llvm::dyn_cast<clang::TagDecl>(decl)) {
2399 if (tag_decl->isCompleteDefinition())
2400 return true;
2401
2402 if (!tag_decl->hasExternalLexicalStorage())
2403 return false;
2404
2405 ast_source->CompleteType(tag_decl);
2406
2407 return !tag_decl->getTypeForDecl()->isIncompleteType();
2408 } else if (clang::ObjCInterfaceDecl *objc_interface_decl =
2409 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl)) {
2410 if (objc_interface_decl->getDefinition())
2411 return true;
2412
2413 if (!objc_interface_decl->hasExternalLexicalStorage())
2414 return false;
2415
2416 ast_source->CompleteType(objc_interface_decl);
2417
2418 return !objc_interface_decl->getTypeForDecl()->isIncompleteType();
2419 } else {
2420 return false;
2421 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002422}
2423
Kate Stoneb9c1b512016-09-06 20:57:50 +00002424void ClangASTContext::SetMetadataAsUserID(const void *object,
2425 user_id_t user_id) {
2426 ClangASTMetadata meta_data;
2427 meta_data.SetUserID(user_id);
2428 SetMetadata(object, meta_data);
Greg Clayton99558cc42015-08-24 23:46:31 +00002429}
2430
Kate Stoneb9c1b512016-09-06 20:57:50 +00002431void ClangASTContext::SetMetadata(clang::ASTContext *ast, const void *object,
2432 ClangASTMetadata &metadata) {
2433 ClangExternalASTSourceCommon *external_source =
2434 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2435
2436 if (external_source)
2437 external_source->SetMetadata(object, metadata);
2438}
2439
2440ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
2441 const void *object) {
2442 ClangExternalASTSourceCommon *external_source =
2443 ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
2444
2445 if (external_source && external_source->HasMetadata(object))
2446 return external_source->GetMetadata(object);
2447 else
Greg Claytond8d4a572015-08-11 21:38:15 +00002448 return nullptr;
2449}
2450
Kate Stoneb9c1b512016-09-06 20:57:50 +00002451clang::DeclContext *
2452ClangASTContext::GetAsDeclContext(clang::CXXMethodDecl *cxx_method_decl) {
2453 return llvm::dyn_cast<clang::DeclContext>(cxx_method_decl);
2454}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002455
Kate Stoneb9c1b512016-09-06 20:57:50 +00002456clang::DeclContext *
2457ClangASTContext::GetAsDeclContext(clang::ObjCMethodDecl *objc_method_decl) {
2458 return llvm::dyn_cast<clang::DeclContext>(objc_method_decl);
2459}
Greg Claytone6b36cd2015-12-08 01:02:08 +00002460
Kate Stoneb9c1b512016-09-06 20:57:50 +00002461bool ClangASTContext::SetTagTypeKind(clang::QualType tag_qual_type,
2462 int kind) const {
2463 const clang::Type *clang_type = tag_qual_type.getTypePtr();
2464 if (clang_type) {
2465 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(clang_type);
2466 if (tag_type) {
2467 clang::TagDecl *tag_decl =
2468 llvm::dyn_cast<clang::TagDecl>(tag_type->getDecl());
2469 if (tag_decl) {
2470 tag_decl->setTagKind((clang::TagDecl::TagKind)kind);
2471 return true;
2472 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002473 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002474 }
2475 return false;
2476}
2477
2478bool ClangASTContext::SetDefaultAccessForRecordFields(
2479 clang::RecordDecl *record_decl, int default_accessibility,
2480 int *assigned_accessibilities, size_t num_assigned_accessibilities) {
2481 if (record_decl) {
2482 uint32_t field_idx;
2483 clang::RecordDecl::field_iterator field, field_end;
2484 for (field = record_decl->field_begin(),
2485 field_end = record_decl->field_end(), field_idx = 0;
2486 field != field_end; ++field, ++field_idx) {
2487 // If no accessibility was assigned, assign the correct one
2488 if (field_idx < num_assigned_accessibilities &&
2489 assigned_accessibilities[field_idx] == clang::AS_none)
2490 field->setAccess((clang::AccessSpecifier)default_accessibility);
2491 }
Greg Claytond8d4a572015-08-11 21:38:15 +00002492 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002493 }
2494 return false;
2495}
2496
2497clang::DeclContext *
2498ClangASTContext::GetDeclContextForType(const CompilerType &type) {
2499 return GetDeclContextForType(ClangUtil::GetQualType(type));
2500}
2501
2502clang::DeclContext *
2503ClangASTContext::GetDeclContextForType(clang::QualType type) {
2504 if (type.isNull())
2505 return nullptr;
2506
2507 clang::QualType qual_type = type.getCanonicalType();
2508 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2509 switch (type_class) {
2510 case clang::Type::ObjCInterface:
2511 return llvm::cast<clang::ObjCObjectType>(qual_type.getTypePtr())
2512 ->getInterface();
2513 case clang::Type::ObjCObjectPointer:
2514 return GetDeclContextForType(
2515 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
2516 ->getPointeeType());
2517 case clang::Type::Record:
2518 return llvm::cast<clang::RecordType>(qual_type)->getDecl();
2519 case clang::Type::Enum:
2520 return llvm::cast<clang::EnumType>(qual_type)->getDecl();
2521 case clang::Type::Typedef:
2522 return GetDeclContextForType(llvm::cast<clang::TypedefType>(qual_type)
2523 ->getDecl()
2524 ->getUnderlyingType());
2525 case clang::Type::Auto:
2526 return GetDeclContextForType(
2527 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
2528 case clang::Type::Elaborated:
2529 return GetDeclContextForType(
2530 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
2531 case clang::Type::Paren:
2532 return GetDeclContextForType(
2533 llvm::cast<clang::ParenType>(qual_type)->desugar());
2534 default:
2535 break;
2536 }
2537 // No DeclContext in this type...
2538 return nullptr;
2539}
2540
2541static bool GetCompleteQualType(clang::ASTContext *ast,
2542 clang::QualType qual_type,
2543 bool allow_completion = true) {
2544 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2545 switch (type_class) {
2546 case clang::Type::ConstantArray:
2547 case clang::Type::IncompleteArray:
2548 case clang::Type::VariableArray: {
2549 const clang::ArrayType *array_type =
2550 llvm::dyn_cast<clang::ArrayType>(qual_type.getTypePtr());
2551
2552 if (array_type)
2553 return GetCompleteQualType(ast, array_type->getElementType(),
2554 allow_completion);
2555 } break;
2556 case clang::Type::Record: {
2557 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2558 if (cxx_record_decl) {
2559 if (cxx_record_decl->hasExternalLexicalStorage()) {
2560 const bool is_complete = cxx_record_decl->isCompleteDefinition();
2561 const bool fields_loaded =
2562 cxx_record_decl->hasLoadedFieldsFromExternalStorage();
2563 if (is_complete && fields_loaded)
2564 return true;
2565
2566 if (!allow_completion)
2567 return false;
2568
2569 // Call the field_begin() accessor to for it to use the external source
2570 // to load the fields...
2571 clang::ExternalASTSource *external_ast_source =
2572 ast->getExternalSource();
2573 if (external_ast_source) {
2574 external_ast_source->CompleteType(cxx_record_decl);
2575 if (cxx_record_decl->isCompleteDefinition()) {
2576 cxx_record_decl->field_begin();
2577 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
2578 }
2579 }
2580 }
2581 }
2582 const clang::TagType *tag_type =
2583 llvm::cast<clang::TagType>(qual_type.getTypePtr());
2584 return !tag_type->isIncompleteType();
2585 } break;
2586
2587 case clang::Type::Enum: {
2588 const clang::TagType *tag_type =
2589 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
2590 if (tag_type) {
2591 clang::TagDecl *tag_decl = tag_type->getDecl();
2592 if (tag_decl) {
2593 if (tag_decl->getDefinition())
2594 return true;
2595
2596 if (!allow_completion)
2597 return false;
2598
2599 if (tag_decl->hasExternalLexicalStorage()) {
2600 if (ast) {
2601 clang::ExternalASTSource *external_ast_source =
2602 ast->getExternalSource();
2603 if (external_ast_source) {
2604 external_ast_source->CompleteType(tag_decl);
2605 return !tag_type->isIncompleteType();
2606 }
2607 }
2608 }
2609 return false;
2610 }
2611 }
2612
2613 } break;
2614 case clang::Type::ObjCObject:
2615 case clang::Type::ObjCInterface: {
2616 const clang::ObjCObjectType *objc_class_type =
2617 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
2618 if (objc_class_type) {
2619 clang::ObjCInterfaceDecl *class_interface_decl =
2620 objc_class_type->getInterface();
2621 // We currently can't complete objective C types through the newly added
2622 // ASTContext
2623 // because it only supports TagDecl objects right now...
2624 if (class_interface_decl) {
2625 if (class_interface_decl->getDefinition())
2626 return true;
2627
2628 if (!allow_completion)
2629 return false;
2630
2631 if (class_interface_decl->hasExternalLexicalStorage()) {
2632 if (ast) {
2633 clang::ExternalASTSource *external_ast_source =
2634 ast->getExternalSource();
2635 if (external_ast_source) {
2636 external_ast_source->CompleteType(class_interface_decl);
2637 return !objc_class_type->isIncompleteType();
2638 }
2639 }
2640 }
2641 return false;
2642 }
2643 }
2644 } break;
2645
2646 case clang::Type::Typedef:
2647 return GetCompleteQualType(ast, llvm::cast<clang::TypedefType>(qual_type)
2648 ->getDecl()
2649 ->getUnderlyingType(),
2650 allow_completion);
2651
2652 case clang::Type::Auto:
2653 return GetCompleteQualType(
2654 ast, llvm::cast<clang::AutoType>(qual_type)->getDeducedType(),
2655 allow_completion);
2656
2657 case clang::Type::Elaborated:
2658 return GetCompleteQualType(
2659 ast, llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType(),
2660 allow_completion);
2661
2662 case clang::Type::Paren:
2663 return GetCompleteQualType(
2664 ast, llvm::cast<clang::ParenType>(qual_type)->desugar(),
2665 allow_completion);
2666
2667 case clang::Type::Attributed:
2668 return GetCompleteQualType(
2669 ast, llvm::cast<clang::AttributedType>(qual_type)->getModifiedType(),
2670 allow_completion);
2671
2672 default:
2673 break;
2674 }
2675
2676 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00002677}
2678
2679static clang::ObjCIvarDecl::AccessControl
Kate Stoneb9c1b512016-09-06 20:57:50 +00002680ConvertAccessTypeToObjCIvarAccessControl(AccessType access) {
2681 switch (access) {
2682 case eAccessNone:
Greg Claytond8d4a572015-08-11 21:38:15 +00002683 return clang::ObjCIvarDecl::None;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002684 case eAccessPublic:
2685 return clang::ObjCIvarDecl::Public;
2686 case eAccessPrivate:
2687 return clang::ObjCIvarDecl::Private;
2688 case eAccessProtected:
2689 return clang::ObjCIvarDecl::Protected;
2690 case eAccessPackage:
2691 return clang::ObjCIvarDecl::Package;
2692 }
2693 return clang::ObjCIvarDecl::None;
Greg Claytond8d4a572015-08-11 21:38:15 +00002694}
2695
Greg Claytond8d4a572015-08-11 21:38:15 +00002696//----------------------------------------------------------------------
2697// Tests
2698//----------------------------------------------------------------------
2699
Kate Stoneb9c1b512016-09-06 20:57:50 +00002700bool ClangASTContext::IsAggregateType(lldb::opaque_compiler_type_t type) {
2701 clang::QualType qual_type(GetCanonicalQualType(type));
2702
2703 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2704 switch (type_class) {
2705 case clang::Type::IncompleteArray:
2706 case clang::Type::VariableArray:
2707 case clang::Type::ConstantArray:
2708 case clang::Type::ExtVector:
2709 case clang::Type::Vector:
2710 case clang::Type::Record:
2711 case clang::Type::ObjCObject:
2712 case clang::Type::ObjCInterface:
2713 return true;
2714 case clang::Type::Auto:
2715 return IsAggregateType(llvm::cast<clang::AutoType>(qual_type)
2716 ->getDeducedType()
2717 .getAsOpaquePtr());
2718 case clang::Type::Elaborated:
2719 return IsAggregateType(llvm::cast<clang::ElaboratedType>(qual_type)
2720 ->getNamedType()
2721 .getAsOpaquePtr());
2722 case clang::Type::Typedef:
2723 return IsAggregateType(llvm::cast<clang::TypedefType>(qual_type)
2724 ->getDecl()
2725 ->getUnderlyingType()
2726 .getAsOpaquePtr());
2727 case clang::Type::Paren:
2728 return IsAggregateType(
2729 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2730 default:
2731 break;
2732 }
2733 // The clang type does have a value
2734 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002735}
2736
Kate Stoneb9c1b512016-09-06 20:57:50 +00002737bool ClangASTContext::IsAnonymousType(lldb::opaque_compiler_type_t type) {
2738 clang::QualType qual_type(GetCanonicalQualType(type));
2739
2740 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2741 switch (type_class) {
2742 case clang::Type::Record: {
2743 if (const clang::RecordType *record_type =
2744 llvm::dyn_cast_or_null<clang::RecordType>(
2745 qual_type.getTypePtrOrNull())) {
2746 if (const clang::RecordDecl *record_decl = record_type->getDecl()) {
2747 return record_decl->isAnonymousStructOrUnion();
2748 }
Enrico Granata7123e2b2015-11-07 02:06:57 +00002749 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002750 break;
2751 }
2752 case clang::Type::Auto:
2753 return IsAnonymousType(llvm::cast<clang::AutoType>(qual_type)
2754 ->getDeducedType()
2755 .getAsOpaquePtr());
2756 case clang::Type::Elaborated:
2757 return IsAnonymousType(llvm::cast<clang::ElaboratedType>(qual_type)
2758 ->getNamedType()
2759 .getAsOpaquePtr());
2760 case clang::Type::Typedef:
2761 return IsAnonymousType(llvm::cast<clang::TypedefType>(qual_type)
2762 ->getDecl()
2763 ->getUnderlyingType()
2764 .getAsOpaquePtr());
2765 case clang::Type::Paren:
2766 return IsAnonymousType(
2767 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
2768 default:
2769 break;
2770 }
2771 // The clang type does have a value
2772 return false;
Enrico Granata7123e2b2015-11-07 02:06:57 +00002773}
2774
Kate Stoneb9c1b512016-09-06 20:57:50 +00002775bool ClangASTContext::IsArrayType(lldb::opaque_compiler_type_t type,
2776 CompilerType *element_type_ptr,
2777 uint64_t *size, bool *is_incomplete) {
2778 clang::QualType qual_type(GetCanonicalQualType(type));
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002779
Kate Stoneb9c1b512016-09-06 20:57:50 +00002780 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2781 switch (type_class) {
2782 default:
2783 break;
Tamas Berghammer69d0b332015-10-09 12:43:08 +00002784
Kate Stoneb9c1b512016-09-06 20:57:50 +00002785 case clang::Type::ConstantArray:
Greg Claytond8d4a572015-08-11 21:38:15 +00002786 if (element_type_ptr)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002787 element_type_ptr->SetCompilerType(
2788 getASTContext(),
2789 llvm::cast<clang::ConstantArrayType>(qual_type)->getElementType());
Greg Claytond8d4a572015-08-11 21:38:15 +00002790 if (size)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002791 *size = llvm::cast<clang::ConstantArrayType>(qual_type)
2792 ->getSize()
2793 .getLimitedValue(ULLONG_MAX);
Greg Claytond8d4a572015-08-11 21:38:15 +00002794 if (is_incomplete)
Kate Stoneb9c1b512016-09-06 20:57:50 +00002795 *is_incomplete = false;
2796 return true;
2797
2798 case clang::Type::IncompleteArray:
2799 if (element_type_ptr)
2800 element_type_ptr->SetCompilerType(
2801 getASTContext(),
2802 llvm::cast<clang::IncompleteArrayType>(qual_type)->getElementType());
2803 if (size)
2804 *size = 0;
2805 if (is_incomplete)
2806 *is_incomplete = true;
2807 return true;
2808
2809 case clang::Type::VariableArray:
2810 if (element_type_ptr)
2811 element_type_ptr->SetCompilerType(
2812 getASTContext(),
2813 llvm::cast<clang::VariableArrayType>(qual_type)->getElementType());
2814 if (size)
2815 *size = 0;
2816 if (is_incomplete)
2817 *is_incomplete = false;
2818 return true;
2819
2820 case clang::Type::DependentSizedArray:
2821 if (element_type_ptr)
2822 element_type_ptr->SetCompilerType(
2823 getASTContext(), llvm::cast<clang::DependentSizedArrayType>(qual_type)
2824 ->getElementType());
2825 if (size)
2826 *size = 0;
2827 if (is_incomplete)
2828 *is_incomplete = false;
2829 return true;
2830
2831 case clang::Type::Typedef:
2832 return IsArrayType(llvm::cast<clang::TypedefType>(qual_type)
2833 ->getDecl()
2834 ->getUnderlyingType()
2835 .getAsOpaquePtr(),
2836 element_type_ptr, size, is_incomplete);
2837 case clang::Type::Auto:
2838 return IsArrayType(llvm::cast<clang::AutoType>(qual_type)
2839 ->getDeducedType()
2840 .getAsOpaquePtr(),
2841 element_type_ptr, size, is_incomplete);
2842 case clang::Type::Elaborated:
2843 return IsArrayType(llvm::cast<clang::ElaboratedType>(qual_type)
2844 ->getNamedType()
2845 .getAsOpaquePtr(),
2846 element_type_ptr, size, is_incomplete);
2847 case clang::Type::Paren:
2848 return IsArrayType(
2849 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2850 element_type_ptr, size, is_incomplete);
2851 }
2852 if (element_type_ptr)
2853 element_type_ptr->Clear();
2854 if (size)
2855 *size = 0;
2856 if (is_incomplete)
2857 *is_incomplete = false;
2858 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00002859}
2860
Kate Stoneb9c1b512016-09-06 20:57:50 +00002861bool ClangASTContext::IsVectorType(lldb::opaque_compiler_type_t type,
2862 CompilerType *element_type, uint64_t *size) {
2863 clang::QualType qual_type(GetCanonicalQualType(type));
2864
2865 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2866 switch (type_class) {
2867 case clang::Type::Vector: {
2868 const clang::VectorType *vector_type =
2869 qual_type->getAs<clang::VectorType>();
2870 if (vector_type) {
2871 if (size)
2872 *size = vector_type->getNumElements();
2873 if (element_type)
2874 *element_type =
2875 CompilerType(getASTContext(), vector_type->getElementType());
2876 }
2877 return true;
2878 } break;
2879 case clang::Type::ExtVector: {
2880 const clang::ExtVectorType *ext_vector_type =
2881 qual_type->getAs<clang::ExtVectorType>();
2882 if (ext_vector_type) {
2883 if (size)
2884 *size = ext_vector_type->getNumElements();
2885 if (element_type)
2886 *element_type =
2887 CompilerType(getASTContext(), ext_vector_type->getElementType());
2888 }
2889 return true;
2890 }
2891 default:
2892 break;
2893 }
2894 return false;
2895}
2896
2897bool ClangASTContext::IsRuntimeGeneratedType(
2898 lldb::opaque_compiler_type_t type) {
2899 clang::DeclContext *decl_ctx = ClangASTContext::GetASTContext(getASTContext())
2900 ->GetDeclContextForType(GetQualType(type));
2901 if (!decl_ctx)
2902 return false;
2903
2904 if (!llvm::isa<clang::ObjCInterfaceDecl>(decl_ctx))
2905 return false;
2906
2907 clang::ObjCInterfaceDecl *result_iface_decl =
2908 llvm::dyn_cast<clang::ObjCInterfaceDecl>(decl_ctx);
2909
2910 ClangASTMetadata *ast_metadata =
2911 ClangASTContext::GetMetadata(getASTContext(), result_iface_decl);
2912 if (!ast_metadata)
2913 return false;
2914 return (ast_metadata->GetISAPtr() != 0);
2915}
2916
2917bool ClangASTContext::IsCharType(lldb::opaque_compiler_type_t type) {
2918 return GetQualType(type).getUnqualifiedType()->isCharType();
2919}
2920
2921bool ClangASTContext::IsCompleteType(lldb::opaque_compiler_type_t type) {
2922 const bool allow_completion = false;
2923 return GetCompleteQualType(getASTContext(), GetQualType(type),
2924 allow_completion);
2925}
2926
2927bool ClangASTContext::IsConst(lldb::opaque_compiler_type_t type) {
2928 return GetQualType(type).isConstQualified();
2929}
2930
2931bool ClangASTContext::IsCStringType(lldb::opaque_compiler_type_t type,
2932 uint32_t &length) {
2933 CompilerType pointee_or_element_clang_type;
2934 length = 0;
2935 Flags type_flags(GetTypeInfo(type, &pointee_or_element_clang_type));
2936
2937 if (!pointee_or_element_clang_type.IsValid())
2938 return false;
2939
2940 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer)) {
2941 if (pointee_or_element_clang_type.IsCharType()) {
2942 if (type_flags.Test(eTypeIsArray)) {
2943 // We know the size of the array and it could be a C string
2944 // since it is an array of characters
2945 length = llvm::cast<clang::ConstantArrayType>(
2946 GetCanonicalQualType(type).getTypePtr())
2947 ->getSize()
2948 .getLimitedValue();
2949 }
2950 return true;
2951 }
2952 }
2953 return false;
2954}
2955
2956bool ClangASTContext::IsFunctionType(lldb::opaque_compiler_type_t type,
2957 bool *is_variadic_ptr) {
2958 if (type) {
2959 clang::QualType qual_type(GetCanonicalQualType(type));
2960
2961 if (qual_type->isFunctionType()) {
2962 if (is_variadic_ptr) {
2963 const clang::FunctionProtoType *function_proto_type =
2964 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
2965 if (function_proto_type)
2966 *is_variadic_ptr = function_proto_type->isVariadic();
2967 else
2968 *is_variadic_ptr = false;
2969 }
2970 return true;
2971 }
2972
Greg Claytond8d4a572015-08-11 21:38:15 +00002973 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00002974 switch (type_class) {
2975 default:
2976 break;
2977 case clang::Type::Typedef:
2978 return IsFunctionType(llvm::cast<clang::TypedefType>(qual_type)
2979 ->getDecl()
2980 ->getUnderlyingType()
2981 .getAsOpaquePtr(),
2982 nullptr);
2983 case clang::Type::Auto:
2984 return IsFunctionType(llvm::cast<clang::AutoType>(qual_type)
2985 ->getDeducedType()
2986 .getAsOpaquePtr(),
2987 nullptr);
2988 case clang::Type::Elaborated:
2989 return IsFunctionType(llvm::cast<clang::ElaboratedType>(qual_type)
2990 ->getNamedType()
2991 .getAsOpaquePtr(),
2992 nullptr);
2993 case clang::Type::Paren:
2994 return IsFunctionType(
2995 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
2996 nullptr);
2997 case clang::Type::LValueReference:
2998 case clang::Type::RValueReference: {
2999 const clang::ReferenceType *reference_type =
3000 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3001 if (reference_type)
3002 return IsFunctionType(reference_type->getPointeeType().getAsOpaquePtr(),
3003 nullptr);
3004 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00003005 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003006 }
3007 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003008}
3009
3010// Used to detect "Homogeneous Floating-point Aggregates"
3011uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003012ClangASTContext::IsHomogeneousAggregate(lldb::opaque_compiler_type_t type,
3013 CompilerType *base_type_ptr) {
3014 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003015 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003016
3017 clang::QualType qual_type(GetCanonicalQualType(type));
3018 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3019 switch (type_class) {
3020 case clang::Type::Record:
3021 if (GetCompleteType(type)) {
3022 const clang::CXXRecordDecl *cxx_record_decl =
3023 qual_type->getAsCXXRecordDecl();
3024 if (cxx_record_decl) {
3025 if (cxx_record_decl->getNumBases() || cxx_record_decl->isDynamicClass())
3026 return 0;
3027 }
3028 const clang::RecordType *record_type =
3029 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3030 if (record_type) {
3031 const clang::RecordDecl *record_decl = record_type->getDecl();
3032 if (record_decl) {
3033 // We are looking for a structure that contains only floating point
3034 // types
3035 clang::RecordDecl::field_iterator field_pos,
3036 field_end = record_decl->field_end();
3037 uint32_t num_fields = 0;
3038 bool is_hva = false;
3039 bool is_hfa = false;
3040 clang::QualType base_qual_type;
3041 uint64_t base_bitwidth = 0;
3042 for (field_pos = record_decl->field_begin(); field_pos != field_end;
3043 ++field_pos) {
3044 clang::QualType field_qual_type = field_pos->getType();
3045 uint64_t field_bitwidth = getASTContext()->getTypeSize(qual_type);
3046 if (field_qual_type->isFloatingType()) {
3047 if (field_qual_type->isComplexType())
3048 return 0;
3049 else {
3050 if (num_fields == 0)
3051 base_qual_type = field_qual_type;
3052 else {
3053 if (is_hva)
3054 return 0;
3055 is_hfa = true;
3056 if (field_qual_type.getTypePtr() !=
3057 base_qual_type.getTypePtr())
3058 return 0;
3059 }
3060 }
3061 } else if (field_qual_type->isVectorType() ||
3062 field_qual_type->isExtVectorType()) {
3063 if (num_fields == 0) {
3064 base_qual_type = field_qual_type;
3065 base_bitwidth = field_bitwidth;
3066 } else {
3067 if (is_hfa)
3068 return 0;
3069 is_hva = true;
3070 if (base_bitwidth != field_bitwidth)
3071 return 0;
3072 if (field_qual_type.getTypePtr() != base_qual_type.getTypePtr())
3073 return 0;
3074 }
3075 } else
3076 return 0;
3077 ++num_fields;
3078 }
3079 if (base_type_ptr)
3080 *base_type_ptr = CompilerType(getASTContext(), base_qual_type);
3081 return num_fields;
3082 }
3083 }
3084 }
3085 break;
3086
3087 case clang::Type::Typedef:
3088 return IsHomogeneousAggregate(llvm::cast<clang::TypedefType>(qual_type)
3089 ->getDecl()
3090 ->getUnderlyingType()
3091 .getAsOpaquePtr(),
3092 base_type_ptr);
3093
3094 case clang::Type::Auto:
3095 return IsHomogeneousAggregate(llvm::cast<clang::AutoType>(qual_type)
3096 ->getDeducedType()
3097 .getAsOpaquePtr(),
3098 base_type_ptr);
3099
3100 case clang::Type::Elaborated:
3101 return IsHomogeneousAggregate(llvm::cast<clang::ElaboratedType>(qual_type)
3102 ->getNamedType()
3103 .getAsOpaquePtr(),
3104 base_type_ptr);
3105 default:
3106 break;
3107 }
3108 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003109}
3110
Kate Stoneb9c1b512016-09-06 20:57:50 +00003111size_t ClangASTContext::GetNumberOfFunctionArguments(
3112 lldb::opaque_compiler_type_t type) {
3113 if (type) {
3114 clang::QualType qual_type(GetCanonicalQualType(type));
3115 const clang::FunctionProtoType *func =
3116 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3117 if (func)
3118 return func->getNumParams();
3119 }
3120 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00003121}
3122
Greg Claytona1e5dc82015-08-11 22:53:00 +00003123CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00003124ClangASTContext::GetFunctionArgumentAtIndex(lldb::opaque_compiler_type_t type,
3125 const size_t index) {
3126 if (type) {
Greg Claytond8d4a572015-08-11 21:38:15 +00003127 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00003128 const clang::FunctionProtoType *func =
3129 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
3130 if (func) {
3131 if (index < func->getNumParams())
3132 return CompilerType(getASTContext(), func->getParamType(index));
Greg Claytond8d4a572015-08-11 21:38:15 +00003133 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003134 }
3135 return CompilerType();
3136}
3137
3138bool ClangASTContext::IsFunctionPointerType(lldb::opaque_compiler_type_t type) {
3139 if (type) {
3140 clang::QualType qual_type(GetCanonicalQualType(type));
3141
3142 if (qual_type->isFunctionPointerType())
3143 return true;
3144
3145 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3146 switch (type_class) {
3147 default:
3148 break;
3149 case clang::Type::Typedef:
3150 return IsFunctionPointerType(llvm::cast<clang::TypedefType>(qual_type)
3151 ->getDecl()
3152 ->getUnderlyingType()
3153 .getAsOpaquePtr());
3154 case clang::Type::Auto:
3155 return IsFunctionPointerType(llvm::cast<clang::AutoType>(qual_type)
3156 ->getDeducedType()
3157 .getAsOpaquePtr());
3158 case clang::Type::Elaborated:
3159 return IsFunctionPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3160 ->getNamedType()
3161 .getAsOpaquePtr());
3162 case clang::Type::Paren:
3163 return IsFunctionPointerType(
3164 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
3165
3166 case clang::Type::LValueReference:
3167 case clang::Type::RValueReference: {
3168 const clang::ReferenceType *reference_type =
3169 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3170 if (reference_type)
3171 return IsFunctionPointerType(
3172 reference_type->getPointeeType().getAsOpaquePtr());
3173 } break;
3174 }
3175 }
3176 return false;
3177}
3178
3179bool ClangASTContext::IsBlockPointerType(
3180 lldb::opaque_compiler_type_t type,
3181 CompilerType *function_pointer_type_ptr) {
3182 if (type) {
3183 clang::QualType qual_type(GetCanonicalQualType(type));
3184
3185 if (qual_type->isBlockPointerType()) {
3186 if (function_pointer_type_ptr) {
3187 const clang::BlockPointerType *block_pointer_type =
3188 qual_type->getAs<clang::BlockPointerType>();
3189 QualType pointee_type = block_pointer_type->getPointeeType();
3190 QualType function_pointer_type = m_ast_ap->getPointerType(pointee_type);
3191 *function_pointer_type_ptr =
3192 CompilerType(getASTContext(), function_pointer_type);
3193 }
3194 return true;
3195 }
3196
3197 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3198 switch (type_class) {
3199 default:
3200 break;
3201 case clang::Type::Typedef:
3202 return IsBlockPointerType(llvm::cast<clang::TypedefType>(qual_type)
3203 ->getDecl()
3204 ->getUnderlyingType()
3205 .getAsOpaquePtr(),
3206 function_pointer_type_ptr);
3207 case clang::Type::Auto:
3208 return IsBlockPointerType(llvm::cast<clang::AutoType>(qual_type)
3209 ->getDeducedType()
3210 .getAsOpaquePtr(),
3211 function_pointer_type_ptr);
3212 case clang::Type::Elaborated:
3213 return IsBlockPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3214 ->getNamedType()
3215 .getAsOpaquePtr(),
3216 function_pointer_type_ptr);
3217 case clang::Type::Paren:
3218 return IsBlockPointerType(
3219 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3220 function_pointer_type_ptr);
3221
3222 case clang::Type::LValueReference:
3223 case clang::Type::RValueReference: {
3224 const clang::ReferenceType *reference_type =
3225 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
3226 if (reference_type)
3227 return IsBlockPointerType(
3228 reference_type->getPointeeType().getAsOpaquePtr(),
3229 function_pointer_type_ptr);
3230 } break;
3231 }
3232 }
3233 return false;
3234}
3235
3236bool ClangASTContext::IsIntegerType(lldb::opaque_compiler_type_t type,
3237 bool &is_signed) {
3238 if (!type)
3239 return false;
3240
3241 clang::QualType qual_type(GetCanonicalQualType(type));
3242 const clang::BuiltinType *builtin_type =
3243 llvm::dyn_cast<clang::BuiltinType>(qual_type->getCanonicalTypeInternal());
3244
3245 if (builtin_type) {
3246 if (builtin_type->isInteger()) {
3247 is_signed = builtin_type->isSignedInteger();
3248 return true;
3249 }
3250 }
3251
3252 return false;
3253}
3254
3255bool ClangASTContext::IsEnumerationType(lldb::opaque_compiler_type_t type,
3256 bool &is_signed) {
3257 if (type) {
3258 const clang::EnumType *enum_type = llvm::dyn_cast<clang::EnumType>(
3259 GetCanonicalQualType(type)->getCanonicalTypeInternal());
3260
3261 if (enum_type) {
3262 IsIntegerType(enum_type->getDecl()->getIntegerType().getAsOpaquePtr(),
3263 is_signed);
3264 return true;
3265 }
3266 }
3267
3268 return false;
3269}
3270
3271bool ClangASTContext::IsPointerType(lldb::opaque_compiler_type_t type,
3272 CompilerType *pointee_type) {
3273 if (type) {
3274 clang::QualType qual_type(GetCanonicalQualType(type));
3275 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3276 switch (type_class) {
3277 case clang::Type::Builtin:
3278 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3279 default:
3280 break;
3281 case clang::BuiltinType::ObjCId:
3282 case clang::BuiltinType::ObjCClass:
3283 return true;
3284 }
3285 return false;
3286 case clang::Type::ObjCObjectPointer:
3287 if (pointee_type)
3288 pointee_type->SetCompilerType(
3289 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3290 ->getPointeeType());
3291 return true;
3292 case clang::Type::BlockPointer:
3293 if (pointee_type)
3294 pointee_type->SetCompilerType(
3295 getASTContext(),
3296 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3297 return true;
3298 case clang::Type::Pointer:
3299 if (pointee_type)
3300 pointee_type->SetCompilerType(
3301 getASTContext(),
3302 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3303 return true;
3304 case clang::Type::MemberPointer:
3305 if (pointee_type)
3306 pointee_type->SetCompilerType(
3307 getASTContext(),
3308 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3309 return true;
3310 case clang::Type::Typedef:
3311 return IsPointerType(llvm::cast<clang::TypedefType>(qual_type)
3312 ->getDecl()
3313 ->getUnderlyingType()
3314 .getAsOpaquePtr(),
3315 pointee_type);
3316 case clang::Type::Auto:
3317 return IsPointerType(llvm::cast<clang::AutoType>(qual_type)
3318 ->getDeducedType()
3319 .getAsOpaquePtr(),
3320 pointee_type);
3321 case clang::Type::Elaborated:
3322 return IsPointerType(llvm::cast<clang::ElaboratedType>(qual_type)
3323 ->getNamedType()
3324 .getAsOpaquePtr(),
3325 pointee_type);
3326 case clang::Type::Paren:
3327 return IsPointerType(
3328 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3329 pointee_type);
3330 default:
3331 break;
3332 }
3333 }
3334 if (pointee_type)
3335 pointee_type->Clear();
3336 return false;
3337}
3338
3339bool ClangASTContext::IsPointerOrReferenceType(
3340 lldb::opaque_compiler_type_t type, CompilerType *pointee_type) {
3341 if (type) {
3342 clang::QualType qual_type(GetCanonicalQualType(type));
3343 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3344 switch (type_class) {
3345 case clang::Type::Builtin:
3346 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
3347 default:
3348 break;
3349 case clang::BuiltinType::ObjCId:
3350 case clang::BuiltinType::ObjCClass:
3351 return true;
3352 }
3353 return false;
3354 case clang::Type::ObjCObjectPointer:
3355 if (pointee_type)
3356 pointee_type->SetCompilerType(
3357 getASTContext(), llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3358 ->getPointeeType());
3359 return true;
3360 case clang::Type::BlockPointer:
3361 if (pointee_type)
3362 pointee_type->SetCompilerType(
3363 getASTContext(),
3364 llvm::cast<clang::BlockPointerType>(qual_type)->getPointeeType());
3365 return true;
3366 case clang::Type::Pointer:
3367 if (pointee_type)
3368 pointee_type->SetCompilerType(
3369 getASTContext(),
3370 llvm::cast<clang::PointerType>(qual_type)->getPointeeType());
3371 return true;
3372 case clang::Type::MemberPointer:
3373 if (pointee_type)
3374 pointee_type->SetCompilerType(
3375 getASTContext(),
3376 llvm::cast<clang::MemberPointerType>(qual_type)->getPointeeType());
3377 return true;
3378 case clang::Type::LValueReference:
3379 if (pointee_type)
3380 pointee_type->SetCompilerType(
3381 getASTContext(),
3382 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3383 return true;
3384 case clang::Type::RValueReference:
3385 if (pointee_type)
3386 pointee_type->SetCompilerType(
3387 getASTContext(),
3388 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3389 return true;
3390 case clang::Type::Typedef:
3391 return IsPointerOrReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3392 ->getDecl()
3393 ->getUnderlyingType()
3394 .getAsOpaquePtr(),
3395 pointee_type);
3396 case clang::Type::Auto:
3397 return IsPointerOrReferenceType(llvm::cast<clang::AutoType>(qual_type)
3398 ->getDeducedType()
3399 .getAsOpaquePtr(),
3400 pointee_type);
3401 case clang::Type::Elaborated:
3402 return IsPointerOrReferenceType(
3403 llvm::cast<clang::ElaboratedType>(qual_type)
3404 ->getNamedType()
3405 .getAsOpaquePtr(),
3406 pointee_type);
3407 case clang::Type::Paren:
3408 return IsPointerOrReferenceType(
3409 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3410 pointee_type);
3411 default:
3412 break;
3413 }
3414 }
3415 if (pointee_type)
3416 pointee_type->Clear();
3417 return false;
3418}
3419
3420bool ClangASTContext::IsReferenceType(lldb::opaque_compiler_type_t type,
3421 CompilerType *pointee_type,
3422 bool *is_rvalue) {
3423 if (type) {
3424 clang::QualType qual_type(GetCanonicalQualType(type));
3425 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3426
3427 switch (type_class) {
3428 case clang::Type::LValueReference:
3429 if (pointee_type)
3430 pointee_type->SetCompilerType(
3431 getASTContext(),
3432 llvm::cast<clang::LValueReferenceType>(qual_type)->desugar());
3433 if (is_rvalue)
3434 *is_rvalue = false;
3435 return true;
3436 case clang::Type::RValueReference:
3437 if (pointee_type)
3438 pointee_type->SetCompilerType(
3439 getASTContext(),
3440 llvm::cast<clang::RValueReferenceType>(qual_type)->desugar());
3441 if (is_rvalue)
3442 *is_rvalue = true;
3443 return true;
3444 case clang::Type::Typedef:
3445 return IsReferenceType(llvm::cast<clang::TypedefType>(qual_type)
3446 ->getDecl()
3447 ->getUnderlyingType()
3448 .getAsOpaquePtr(),
3449 pointee_type, is_rvalue);
3450 case clang::Type::Auto:
3451 return IsReferenceType(llvm::cast<clang::AutoType>(qual_type)
3452 ->getDeducedType()
3453 .getAsOpaquePtr(),
3454 pointee_type, is_rvalue);
3455 case clang::Type::Elaborated:
3456 return IsReferenceType(llvm::cast<clang::ElaboratedType>(qual_type)
3457 ->getNamedType()
3458 .getAsOpaquePtr(),
3459 pointee_type, is_rvalue);
3460 case clang::Type::Paren:
3461 return IsReferenceType(
3462 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3463 pointee_type, is_rvalue);
3464
3465 default:
3466 break;
3467 }
3468 }
3469 if (pointee_type)
3470 pointee_type->Clear();
3471 return false;
3472}
3473
3474bool ClangASTContext::IsFloatingPointType(lldb::opaque_compiler_type_t type,
3475 uint32_t &count, bool &is_complex) {
3476 if (type) {
3477 clang::QualType qual_type(GetCanonicalQualType(type));
3478
3479 if (const clang::BuiltinType *BT = llvm::dyn_cast<clang::BuiltinType>(
3480 qual_type->getCanonicalTypeInternal())) {
3481 clang::BuiltinType::Kind kind = BT->getKind();
3482 if (kind >= clang::BuiltinType::Float &&
3483 kind <= clang::BuiltinType::LongDouble) {
3484 count = 1;
3485 is_complex = false;
3486 return true;
3487 }
3488 } else if (const clang::ComplexType *CT =
3489 llvm::dyn_cast<clang::ComplexType>(
3490 qual_type->getCanonicalTypeInternal())) {
3491 if (IsFloatingPointType(CT->getElementType().getAsOpaquePtr(), count,
3492 is_complex)) {
3493 count = 2;
3494 is_complex = true;
3495 return true;
3496 }
3497 } else if (const clang::VectorType *VT = llvm::dyn_cast<clang::VectorType>(
3498 qual_type->getCanonicalTypeInternal())) {
3499 if (IsFloatingPointType(VT->getElementType().getAsOpaquePtr(), count,
3500 is_complex)) {
3501 count = VT->getNumElements();
3502 is_complex = false;
3503 return true;
3504 }
3505 }
3506 }
3507 count = 0;
3508 is_complex = false;
3509 return false;
3510}
3511
3512bool ClangASTContext::IsDefined(lldb::opaque_compiler_type_t type) {
3513 if (!type)
3514 return false;
3515
3516 clang::QualType qual_type(GetQualType(type));
3517 const clang::TagType *tag_type =
3518 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
3519 if (tag_type) {
3520 clang::TagDecl *tag_decl = tag_type->getDecl();
3521 if (tag_decl)
3522 return tag_decl->isCompleteDefinition();
3523 return false;
3524 } else {
3525 const clang::ObjCObjectType *objc_class_type =
3526 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3527 if (objc_class_type) {
3528 clang::ObjCInterfaceDecl *class_interface_decl =
3529 objc_class_type->getInterface();
3530 if (class_interface_decl)
3531 return class_interface_decl->getDefinition() != nullptr;
3532 return false;
3533 }
3534 }
3535 return true;
3536}
3537
3538bool ClangASTContext::IsObjCClassType(const CompilerType &type) {
3539 if (type) {
3540 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3541
3542 const clang::ObjCObjectPointerType *obj_pointer_type =
3543 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3544
3545 if (obj_pointer_type)
3546 return obj_pointer_type->isObjCClassType();
3547 }
3548 return false;
3549}
3550
3551bool ClangASTContext::IsObjCObjectOrInterfaceType(const CompilerType &type) {
3552 if (ClangUtil::IsClangType(type))
3553 return ClangUtil::GetCanonicalQualType(type)->isObjCObjectOrInterfaceType();
3554 return false;
3555}
3556
3557bool ClangASTContext::IsClassType(lldb::opaque_compiler_type_t type) {
3558 if (!type)
3559 return false;
3560 clang::QualType qual_type(GetCanonicalQualType(type));
3561 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3562 return (type_class == clang::Type::Record);
3563}
3564
3565bool ClangASTContext::IsEnumType(lldb::opaque_compiler_type_t type) {
3566 if (!type)
3567 return false;
3568 clang::QualType qual_type(GetCanonicalQualType(type));
3569 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3570 return (type_class == clang::Type::Enum);
3571}
3572
3573bool ClangASTContext::IsPolymorphicClass(lldb::opaque_compiler_type_t type) {
3574 if (type) {
3575 clang::QualType qual_type(GetCanonicalQualType(type));
3576 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3577 switch (type_class) {
3578 case clang::Type::Record:
3579 if (GetCompleteType(type)) {
3580 const clang::RecordType *record_type =
3581 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
3582 const clang::RecordDecl *record_decl = record_type->getDecl();
3583 if (record_decl) {
3584 const clang::CXXRecordDecl *cxx_record_decl =
3585 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
3586 if (cxx_record_decl)
3587 return cxx_record_decl->isPolymorphic();
Greg Claytond8d4a572015-08-11 21:38:15 +00003588 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003589 }
3590 break;
3591
3592 default:
3593 break;
3594 }
3595 }
3596 return false;
3597}
3598
3599bool ClangASTContext::IsPossibleDynamicType(lldb::opaque_compiler_type_t type,
3600 CompilerType *dynamic_pointee_type,
3601 bool check_cplusplus,
3602 bool check_objc) {
3603 clang::QualType pointee_qual_type;
3604 if (type) {
3605 clang::QualType qual_type(GetCanonicalQualType(type));
3606 bool success = false;
3607 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3608 switch (type_class) {
3609 case clang::Type::Builtin:
3610 if (check_objc &&
3611 llvm::cast<clang::BuiltinType>(qual_type)->getKind() ==
3612 clang::BuiltinType::ObjCId) {
3613 if (dynamic_pointee_type)
3614 dynamic_pointee_type->SetCompilerType(this, type);
3615 return true;
3616 }
3617 break;
3618
3619 case clang::Type::ObjCObjectPointer:
3620 if (check_objc) {
3621 if (auto objc_pointee_type =
3622 qual_type->getPointeeType().getTypePtrOrNull()) {
3623 if (auto objc_object_type =
3624 llvm::dyn_cast_or_null<clang::ObjCObjectType>(
3625 objc_pointee_type)) {
3626 if (objc_object_type->isObjCClass())
3627 return false;
3628 }
3629 }
3630 if (dynamic_pointee_type)
3631 dynamic_pointee_type->SetCompilerType(
3632 getASTContext(),
3633 llvm::cast<clang::ObjCObjectPointerType>(qual_type)
3634 ->getPointeeType());
3635 return true;
3636 }
3637 break;
3638
3639 case clang::Type::Pointer:
3640 pointee_qual_type =
3641 llvm::cast<clang::PointerType>(qual_type)->getPointeeType();
3642 success = true;
3643 break;
3644
3645 case clang::Type::LValueReference:
3646 case clang::Type::RValueReference:
3647 pointee_qual_type =
3648 llvm::cast<clang::ReferenceType>(qual_type)->getPointeeType();
3649 success = true;
3650 break;
3651
3652 case clang::Type::Typedef:
3653 return IsPossibleDynamicType(llvm::cast<clang::TypedefType>(qual_type)
3654 ->getDecl()
3655 ->getUnderlyingType()
3656 .getAsOpaquePtr(),
3657 dynamic_pointee_type, check_cplusplus,
3658 check_objc);
3659
3660 case clang::Type::Auto:
3661 return IsPossibleDynamicType(llvm::cast<clang::AutoType>(qual_type)
3662 ->getDeducedType()
3663 .getAsOpaquePtr(),
3664 dynamic_pointee_type, check_cplusplus,
3665 check_objc);
3666
3667 case clang::Type::Elaborated:
3668 return IsPossibleDynamicType(llvm::cast<clang::ElaboratedType>(qual_type)
3669 ->getNamedType()
3670 .getAsOpaquePtr(),
3671 dynamic_pointee_type, check_cplusplus,
3672 check_objc);
3673
3674 case clang::Type::Paren:
3675 return IsPossibleDynamicType(
3676 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
3677 dynamic_pointee_type, check_cplusplus, check_objc);
3678 default:
3679 break;
3680 }
3681
3682 if (success) {
3683 // Check to make sure what we are pointing too is a possible dynamic C++
3684 // type
3685 // We currently accept any "void *" (in case we have a class that has been
3686 // watered down to an opaque pointer) and virtual C++ classes.
3687 const clang::Type::TypeClass pointee_type_class =
3688 pointee_qual_type.getCanonicalType()->getTypeClass();
3689 switch (pointee_type_class) {
3690 case clang::Type::Builtin:
3691 switch (llvm::cast<clang::BuiltinType>(pointee_qual_type)->getKind()) {
3692 case clang::BuiltinType::UnknownAny:
3693 case clang::BuiltinType::Void:
3694 if (dynamic_pointee_type)
3695 dynamic_pointee_type->SetCompilerType(getASTContext(),
3696 pointee_qual_type);
3697 return true;
3698 default:
3699 break;
3700 }
3701 break;
3702
3703 case clang::Type::Record:
3704 if (check_cplusplus) {
3705 clang::CXXRecordDecl *cxx_record_decl =
3706 pointee_qual_type->getAsCXXRecordDecl();
3707 if (cxx_record_decl) {
3708 bool is_complete = cxx_record_decl->isCompleteDefinition();
3709
3710 if (is_complete)
3711 success = cxx_record_decl->isDynamicClass();
3712 else {
3713 ClangASTMetadata *metadata = ClangASTContext::GetMetadata(
3714 getASTContext(), cxx_record_decl);
3715 if (metadata)
3716 success = metadata->GetIsDynamicCXXType();
3717 else {
3718 is_complete = CompilerType(getASTContext(), pointee_qual_type)
3719 .GetCompleteType();
3720 if (is_complete)
3721 success = cxx_record_decl->isDynamicClass();
3722 else
3723 success = false;
3724 }
3725 }
3726
3727 if (success) {
3728 if (dynamic_pointee_type)
3729 dynamic_pointee_type->SetCompilerType(getASTContext(),
3730 pointee_qual_type);
3731 return true;
3732 }
3733 }
3734 }
3735 break;
3736
3737 case clang::Type::ObjCObject:
3738 case clang::Type::ObjCInterface:
3739 if (check_objc) {
3740 if (dynamic_pointee_type)
3741 dynamic_pointee_type->SetCompilerType(getASTContext(),
3742 pointee_qual_type);
3743 return true;
3744 }
3745 break;
3746
3747 default:
3748 break;
3749 }
3750 }
3751 }
3752 if (dynamic_pointee_type)
3753 dynamic_pointee_type->Clear();
3754 return false;
3755}
3756
3757bool ClangASTContext::IsScalarType(lldb::opaque_compiler_type_t type) {
3758 if (!type)
3759 return false;
3760
3761 return (GetTypeInfo(type, nullptr) & eTypeIsScalar) != 0;
3762}
3763
3764bool ClangASTContext::IsTypedefType(lldb::opaque_compiler_type_t type) {
3765 if (!type)
3766 return false;
3767 return GetQualType(type)->getTypeClass() == clang::Type::Typedef;
3768}
3769
3770bool ClangASTContext::IsVoidType(lldb::opaque_compiler_type_t type) {
3771 if (!type)
3772 return false;
3773 return GetCanonicalQualType(type)->isVoidType();
3774}
3775
3776bool ClangASTContext::SupportsLanguage(lldb::LanguageType language) {
3777 return ClangASTContextSupportsLanguage(language);
3778}
3779
3780bool ClangASTContext::GetCXXClassName(const CompilerType &type,
3781 std::string &class_name) {
3782 if (type) {
3783 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3784 if (!qual_type.isNull()) {
3785 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3786 if (cxx_record_decl) {
3787 class_name.assign(cxx_record_decl->getIdentifier()->getNameStart());
3788 return true;
3789 }
3790 }
3791 }
3792 class_name.clear();
3793 return false;
3794}
3795
3796bool ClangASTContext::IsCXXClassType(const CompilerType &type) {
3797 if (!type)
3798 return false;
3799
3800 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3801 if (!qual_type.isNull() && qual_type->getAsCXXRecordDecl() != nullptr)
3802 return true;
3803 return false;
3804}
3805
3806bool ClangASTContext::IsBeingDefined(lldb::opaque_compiler_type_t type) {
3807 if (!type)
3808 return false;
3809 clang::QualType qual_type(GetCanonicalQualType(type));
3810 const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type);
3811 if (tag_type)
3812 return tag_type->isBeingDefined();
3813 return false;
3814}
3815
3816bool ClangASTContext::IsObjCObjectPointerType(const CompilerType &type,
3817 CompilerType *class_type_ptr) {
3818 if (!type)
3819 return false;
3820
3821 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3822
3823 if (!qual_type.isNull() && qual_type->isObjCObjectPointerType()) {
3824 if (class_type_ptr) {
3825 if (!qual_type->isObjCClassType() && !qual_type->isObjCIdType()) {
3826 const clang::ObjCObjectPointerType *obj_pointer_type =
3827 llvm::dyn_cast<clang::ObjCObjectPointerType>(qual_type);
3828 if (obj_pointer_type == nullptr)
3829 class_type_ptr->Clear();
3830 else
3831 class_type_ptr->SetCompilerType(
3832 type.GetTypeSystem(),
3833 clang::QualType(obj_pointer_type->getInterfaceType(), 0)
3834 .getAsOpaquePtr());
3835 }
Greg Claytond8d4a572015-08-11 21:38:15 +00003836 }
3837 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003838 }
3839 if (class_type_ptr)
3840 class_type_ptr->Clear();
3841 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003842}
3843
Kate Stoneb9c1b512016-09-06 20:57:50 +00003844bool ClangASTContext::GetObjCClassName(const CompilerType &type,
3845 std::string &class_name) {
3846 if (!type)
3847 return false;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00003848
Kate Stoneb9c1b512016-09-06 20:57:50 +00003849 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
3850
3851 const clang::ObjCObjectType *object_type =
3852 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
3853 if (object_type) {
3854 const clang::ObjCInterfaceDecl *interface = object_type->getInterface();
3855 if (interface) {
3856 class_name = interface->getNameAsString();
3857 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00003858 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003859 }
3860 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00003861}
3862
Greg Claytond8d4a572015-08-11 21:38:15 +00003863//----------------------------------------------------------------------
3864// Type Completion
3865//----------------------------------------------------------------------
3866
Kate Stoneb9c1b512016-09-06 20:57:50 +00003867bool ClangASTContext::GetCompleteType(lldb::opaque_compiler_type_t type) {
3868 if (!type)
3869 return false;
3870 const bool allow_completion = true;
3871 return GetCompleteQualType(getASTContext(), GetQualType(type),
3872 allow_completion);
Greg Claytond8d4a572015-08-11 21:38:15 +00003873}
3874
Kate Stoneb9c1b512016-09-06 20:57:50 +00003875ConstString ClangASTContext::GetTypeName(lldb::opaque_compiler_type_t type) {
3876 std::string type_name;
3877 if (type) {
3878 clang::PrintingPolicy printing_policy(getASTContext()->getPrintingPolicy());
3879 clang::QualType qual_type(GetQualType(type));
3880 printing_policy.SuppressTagKeyword = true;
3881 const clang::TypedefType *typedef_type =
3882 qual_type->getAs<clang::TypedefType>();
3883 if (typedef_type) {
3884 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
3885 type_name = typedef_decl->getQualifiedNameAsString();
3886 } else {
3887 type_name = qual_type.getAsString(printing_policy);
Greg Claytond8d4a572015-08-11 21:38:15 +00003888 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003889 }
3890 return ConstString(type_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00003891}
3892
3893uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00003894ClangASTContext::GetTypeInfo(lldb::opaque_compiler_type_t type,
3895 CompilerType *pointee_or_element_clang_type) {
3896 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00003897 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003898
3899 if (pointee_or_element_clang_type)
3900 pointee_or_element_clang_type->Clear();
3901
3902 clang::QualType qual_type(GetQualType(type));
3903
3904 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3905 switch (type_class) {
3906 case clang::Type::Builtin: {
3907 const clang::BuiltinType *builtin_type = llvm::dyn_cast<clang::BuiltinType>(
3908 qual_type->getCanonicalTypeInternal());
3909
3910 uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
3911 switch (builtin_type->getKind()) {
3912 case clang::BuiltinType::ObjCId:
3913 case clang::BuiltinType::ObjCClass:
3914 if (pointee_or_element_clang_type)
3915 pointee_or_element_clang_type->SetCompilerType(
3916 getASTContext(), getASTContext()->ObjCBuiltinClassTy);
3917 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3918 break;
3919
3920 case clang::BuiltinType::ObjCSel:
3921 if (pointee_or_element_clang_type)
3922 pointee_or_element_clang_type->SetCompilerType(getASTContext(),
3923 getASTContext()->CharTy);
3924 builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
3925 break;
3926
3927 case clang::BuiltinType::Bool:
3928 case clang::BuiltinType::Char_U:
3929 case clang::BuiltinType::UChar:
3930 case clang::BuiltinType::WChar_U:
3931 case clang::BuiltinType::Char16:
3932 case clang::BuiltinType::Char32:
3933 case clang::BuiltinType::UShort:
3934 case clang::BuiltinType::UInt:
3935 case clang::BuiltinType::ULong:
3936 case clang::BuiltinType::ULongLong:
3937 case clang::BuiltinType::UInt128:
3938 case clang::BuiltinType::Char_S:
3939 case clang::BuiltinType::SChar:
3940 case clang::BuiltinType::WChar_S:
3941 case clang::BuiltinType::Short:
3942 case clang::BuiltinType::Int:
3943 case clang::BuiltinType::Long:
3944 case clang::BuiltinType::LongLong:
3945 case clang::BuiltinType::Int128:
3946 case clang::BuiltinType::Float:
3947 case clang::BuiltinType::Double:
3948 case clang::BuiltinType::LongDouble:
3949 builtin_type_flags |= eTypeIsScalar;
3950 if (builtin_type->isInteger()) {
3951 builtin_type_flags |= eTypeIsInteger;
3952 if (builtin_type->isSignedInteger())
3953 builtin_type_flags |= eTypeIsSigned;
3954 } else if (builtin_type->isFloatingPoint())
3955 builtin_type_flags |= eTypeIsFloat;
3956 break;
3957 default:
3958 break;
3959 }
3960 return builtin_type_flags;
3961 }
3962
3963 case clang::Type::BlockPointer:
3964 if (pointee_or_element_clang_type)
3965 pointee_or_element_clang_type->SetCompilerType(
3966 getASTContext(), qual_type->getPointeeType());
3967 return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
3968
3969 case clang::Type::Complex: {
3970 uint32_t complex_type_flags =
3971 eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
3972 const clang::ComplexType *complex_type = llvm::dyn_cast<clang::ComplexType>(
3973 qual_type->getCanonicalTypeInternal());
3974 if (complex_type) {
3975 clang::QualType complex_element_type(complex_type->getElementType());
3976 if (complex_element_type->isIntegerType())
3977 complex_type_flags |= eTypeIsFloat;
3978 else if (complex_element_type->isFloatingType())
3979 complex_type_flags |= eTypeIsInteger;
3980 }
3981 return complex_type_flags;
3982 } break;
3983
3984 case clang::Type::ConstantArray:
3985 case clang::Type::DependentSizedArray:
3986 case clang::Type::IncompleteArray:
3987 case clang::Type::VariableArray:
3988 if (pointee_or_element_clang_type)
3989 pointee_or_element_clang_type->SetCompilerType(
3990 getASTContext(), llvm::cast<clang::ArrayType>(qual_type.getTypePtr())
3991 ->getElementType());
3992 return eTypeHasChildren | eTypeIsArray;
3993
3994 case clang::Type::DependentName:
3995 return 0;
3996 case clang::Type::DependentSizedExtVector:
3997 return eTypeHasChildren | eTypeIsVector;
3998 case clang::Type::DependentTemplateSpecialization:
3999 return eTypeIsTemplate;
4000 case clang::Type::Decltype:
4001 return 0;
4002
4003 case clang::Type::Enum:
4004 if (pointee_or_element_clang_type)
4005 pointee_or_element_clang_type->SetCompilerType(
4006 getASTContext(),
4007 llvm::cast<clang::EnumType>(qual_type)->getDecl()->getIntegerType());
4008 return eTypeIsEnumeration | eTypeHasValue;
4009
4010 case clang::Type::Auto:
4011 return CompilerType(
4012 getASTContext(),
4013 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4014 .GetTypeInfo(pointee_or_element_clang_type);
4015 case clang::Type::Elaborated:
4016 return CompilerType(
4017 getASTContext(),
4018 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4019 .GetTypeInfo(pointee_or_element_clang_type);
4020 case clang::Type::Paren:
4021 return CompilerType(getASTContext(),
4022 llvm::cast<clang::ParenType>(qual_type)->desugar())
4023 .GetTypeInfo(pointee_or_element_clang_type);
4024
4025 case clang::Type::FunctionProto:
4026 return eTypeIsFuncPrototype | eTypeHasValue;
4027 case clang::Type::FunctionNoProto:
4028 return eTypeIsFuncPrototype | eTypeHasValue;
4029 case clang::Type::InjectedClassName:
4030 return 0;
4031
4032 case clang::Type::LValueReference:
4033 case clang::Type::RValueReference:
4034 if (pointee_or_element_clang_type)
4035 pointee_or_element_clang_type->SetCompilerType(
4036 getASTContext(),
4037 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr())
4038 ->getPointeeType());
4039 return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
4040
4041 case clang::Type::MemberPointer:
4042 return eTypeIsPointer | eTypeIsMember | eTypeHasValue;
4043
4044 case clang::Type::ObjCObjectPointer:
4045 if (pointee_or_element_clang_type)
4046 pointee_or_element_clang_type->SetCompilerType(
4047 getASTContext(), qual_type->getPointeeType());
4048 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer |
4049 eTypeHasValue;
4050
4051 case clang::Type::ObjCObject:
4052 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4053 case clang::Type::ObjCInterface:
4054 return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
4055
4056 case clang::Type::Pointer:
4057 if (pointee_or_element_clang_type)
4058 pointee_or_element_clang_type->SetCompilerType(
4059 getASTContext(), qual_type->getPointeeType());
4060 return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
4061
4062 case clang::Type::Record:
4063 if (qual_type->getAsCXXRecordDecl())
4064 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
4065 else
4066 return eTypeHasChildren | eTypeIsStructUnion;
4067 break;
4068 case clang::Type::SubstTemplateTypeParm:
4069 return eTypeIsTemplate;
4070 case clang::Type::TemplateTypeParm:
4071 return eTypeIsTemplate;
4072 case clang::Type::TemplateSpecialization:
4073 return eTypeIsTemplate;
4074
4075 case clang::Type::Typedef:
4076 return eTypeIsTypedef |
4077 CompilerType(getASTContext(),
4078 llvm::cast<clang::TypedefType>(qual_type)
4079 ->getDecl()
4080 ->getUnderlyingType())
4081 .GetTypeInfo(pointee_or_element_clang_type);
4082 case clang::Type::TypeOfExpr:
4083 return 0;
4084 case clang::Type::TypeOf:
4085 return 0;
4086 case clang::Type::UnresolvedUsing:
4087 return 0;
4088
4089 case clang::Type::ExtVector:
4090 case clang::Type::Vector: {
4091 uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
4092 const clang::VectorType *vector_type = llvm::dyn_cast<clang::VectorType>(
4093 qual_type->getCanonicalTypeInternal());
4094 if (vector_type) {
4095 if (vector_type->isIntegerType())
4096 vector_type_flags |= eTypeIsFloat;
4097 else if (vector_type->isFloatingType())
4098 vector_type_flags |= eTypeIsInteger;
4099 }
4100 return vector_type_flags;
4101 }
4102 default:
4103 return 0;
4104 }
4105 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004106}
4107
Greg Claytond8d4a572015-08-11 21:38:15 +00004108lldb::LanguageType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004109ClangASTContext::GetMinimumLanguage(lldb::opaque_compiler_type_t type) {
4110 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00004111 return lldb::eLanguageTypeC;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004112
4113 // If the type is a reference, then resolve it to what it refers to first:
4114 clang::QualType qual_type(GetCanonicalQualType(type).getNonReferenceType());
4115 if (qual_type->isAnyPointerType()) {
4116 if (qual_type->isObjCObjectPointerType())
4117 return lldb::eLanguageTypeObjC;
4118
4119 clang::QualType pointee_type(qual_type->getPointeeType());
4120 if (pointee_type->getPointeeCXXRecordDecl() != nullptr)
4121 return lldb::eLanguageTypeC_plus_plus;
4122 if (pointee_type->isObjCObjectOrInterfaceType())
4123 return lldb::eLanguageTypeObjC;
4124 if (pointee_type->isObjCClassType())
4125 return lldb::eLanguageTypeObjC;
4126 if (pointee_type.getTypePtr() ==
4127 getASTContext()->ObjCBuiltinIdTy.getTypePtr())
4128 return lldb::eLanguageTypeObjC;
4129 } else {
4130 if (qual_type->isObjCObjectOrInterfaceType())
4131 return lldb::eLanguageTypeObjC;
4132 if (qual_type->getAsCXXRecordDecl())
4133 return lldb::eLanguageTypeC_plus_plus;
4134 switch (qual_type->getTypeClass()) {
4135 default:
4136 break;
4137 case clang::Type::Builtin:
4138 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4139 default:
4140 case clang::BuiltinType::Void:
4141 case clang::BuiltinType::Bool:
4142 case clang::BuiltinType::Char_U:
4143 case clang::BuiltinType::UChar:
4144 case clang::BuiltinType::WChar_U:
4145 case clang::BuiltinType::Char16:
4146 case clang::BuiltinType::Char32:
4147 case clang::BuiltinType::UShort:
4148 case clang::BuiltinType::UInt:
4149 case clang::BuiltinType::ULong:
4150 case clang::BuiltinType::ULongLong:
4151 case clang::BuiltinType::UInt128:
4152 case clang::BuiltinType::Char_S:
4153 case clang::BuiltinType::SChar:
4154 case clang::BuiltinType::WChar_S:
4155 case clang::BuiltinType::Short:
4156 case clang::BuiltinType::Int:
4157 case clang::BuiltinType::Long:
4158 case clang::BuiltinType::LongLong:
4159 case clang::BuiltinType::Int128:
4160 case clang::BuiltinType::Float:
4161 case clang::BuiltinType::Double:
4162 case clang::BuiltinType::LongDouble:
4163 break;
4164
4165 case clang::BuiltinType::NullPtr:
4166 return eLanguageTypeC_plus_plus;
4167
4168 case clang::BuiltinType::ObjCId:
4169 case clang::BuiltinType::ObjCClass:
4170 case clang::BuiltinType::ObjCSel:
4171 return eLanguageTypeObjC;
4172
4173 case clang::BuiltinType::Dependent:
4174 case clang::BuiltinType::Overload:
4175 case clang::BuiltinType::BoundMember:
4176 case clang::BuiltinType::UnknownAny:
4177 break;
4178 }
4179 break;
4180 case clang::Type::Typedef:
4181 return CompilerType(getASTContext(),
4182 llvm::cast<clang::TypedefType>(qual_type)
4183 ->getDecl()
4184 ->getUnderlyingType())
4185 .GetMinimumLanguage();
4186 }
4187 }
4188 return lldb::eLanguageTypeC;
Greg Claytond8d4a572015-08-11 21:38:15 +00004189}
4190
4191lldb::TypeClass
Kate Stoneb9c1b512016-09-06 20:57:50 +00004192ClangASTContext::GetTypeClass(lldb::opaque_compiler_type_t type) {
4193 if (!type)
4194 return lldb::eTypeClassInvalid;
4195
4196 clang::QualType qual_type(GetQualType(type));
4197
4198 switch (qual_type->getTypeClass()) {
4199 case clang::Type::UnaryTransform:
4200 break;
4201 case clang::Type::FunctionNoProto:
4202 return lldb::eTypeClassFunction;
4203 case clang::Type::FunctionProto:
4204 return lldb::eTypeClassFunction;
4205 case clang::Type::IncompleteArray:
4206 return lldb::eTypeClassArray;
4207 case clang::Type::VariableArray:
4208 return lldb::eTypeClassArray;
4209 case clang::Type::ConstantArray:
4210 return lldb::eTypeClassArray;
4211 case clang::Type::DependentSizedArray:
4212 return lldb::eTypeClassArray;
4213 case clang::Type::DependentSizedExtVector:
4214 return lldb::eTypeClassVector;
4215 case clang::Type::ExtVector:
4216 return lldb::eTypeClassVector;
4217 case clang::Type::Vector:
4218 return lldb::eTypeClassVector;
4219 case clang::Type::Builtin:
4220 return lldb::eTypeClassBuiltin;
4221 case clang::Type::ObjCObjectPointer:
4222 return lldb::eTypeClassObjCObjectPointer;
4223 case clang::Type::BlockPointer:
4224 return lldb::eTypeClassBlockPointer;
4225 case clang::Type::Pointer:
4226 return lldb::eTypeClassPointer;
4227 case clang::Type::LValueReference:
4228 return lldb::eTypeClassReference;
4229 case clang::Type::RValueReference:
4230 return lldb::eTypeClassReference;
4231 case clang::Type::MemberPointer:
4232 return lldb::eTypeClassMemberPointer;
4233 case clang::Type::Complex:
4234 if (qual_type->isComplexType())
4235 return lldb::eTypeClassComplexFloat;
4236 else
4237 return lldb::eTypeClassComplexInteger;
4238 case clang::Type::ObjCObject:
4239 return lldb::eTypeClassObjCObject;
4240 case clang::Type::ObjCInterface:
4241 return lldb::eTypeClassObjCInterface;
4242 case clang::Type::Record: {
4243 const clang::RecordType *record_type =
4244 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4245 const clang::RecordDecl *record_decl = record_type->getDecl();
4246 if (record_decl->isUnion())
4247 return lldb::eTypeClassUnion;
4248 else if (record_decl->isStruct())
4249 return lldb::eTypeClassStruct;
4250 else
4251 return lldb::eTypeClassClass;
4252 } break;
4253 case clang::Type::Enum:
4254 return lldb::eTypeClassEnumeration;
4255 case clang::Type::Typedef:
4256 return lldb::eTypeClassTypedef;
4257 case clang::Type::UnresolvedUsing:
4258 break;
4259 case clang::Type::Paren:
4260 return CompilerType(getASTContext(),
4261 llvm::cast<clang::ParenType>(qual_type)->desugar())
4262 .GetTypeClass();
4263 case clang::Type::Auto:
4264 return CompilerType(
4265 getASTContext(),
4266 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4267 .GetTypeClass();
4268 case clang::Type::Elaborated:
4269 return CompilerType(
4270 getASTContext(),
4271 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4272 .GetTypeClass();
4273
4274 case clang::Type::Attributed:
4275 break;
4276 case clang::Type::TemplateTypeParm:
4277 break;
4278 case clang::Type::SubstTemplateTypeParm:
4279 break;
4280 case clang::Type::SubstTemplateTypeParmPack:
4281 break;
4282 case clang::Type::InjectedClassName:
4283 break;
4284 case clang::Type::DependentName:
4285 break;
4286 case clang::Type::DependentTemplateSpecialization:
4287 break;
4288 case clang::Type::PackExpansion:
4289 break;
4290
4291 case clang::Type::TypeOfExpr:
4292 break;
4293 case clang::Type::TypeOf:
4294 break;
4295 case clang::Type::Decltype:
4296 break;
4297 case clang::Type::TemplateSpecialization:
4298 break;
Pavel Labath4f19fce22017-02-17 13:39:50 +00004299 case clang::Type::DeducedTemplateSpecialization:
4300 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004301 case clang::Type::Atomic:
4302 break;
4303 case clang::Type::Pipe:
4304 break;
4305
4306 // pointer type decayed from an array or function type.
4307 case clang::Type::Decayed:
4308 break;
4309 case clang::Type::Adjusted:
4310 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00004311 case clang::Type::ObjCTypeParam:
4312 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004313 }
4314 // We don't know hot to display this type...
4315 return lldb::eTypeClassOther;
Greg Claytond8d4a572015-08-11 21:38:15 +00004316}
4317
Kate Stoneb9c1b512016-09-06 20:57:50 +00004318unsigned ClangASTContext::GetTypeQualifiers(lldb::opaque_compiler_type_t type) {
4319 if (type)
4320 return GetQualType(type).getQualifiers().getCVRQualifiers();
4321 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004322}
4323
4324//----------------------------------------------------------------------
4325// Creating related types
4326//----------------------------------------------------------------------
4327
Greg Claytona1e5dc82015-08-11 22:53:00 +00004328CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004329ClangASTContext::GetArrayElementType(lldb::opaque_compiler_type_t type,
4330 uint64_t *stride) {
4331 if (type) {
4332 clang::QualType qual_type(GetCanonicalQualType(type));
4333
4334 const clang::Type *array_eletype =
4335 qual_type.getTypePtr()->getArrayElementTypeNoTypeQual();
4336
4337 if (!array_eletype)
4338 return CompilerType();
4339
4340 CompilerType element_type(getASTContext(),
4341 array_eletype->getCanonicalTypeUnqualified());
4342
4343 // TODO: the real stride will be >= this value.. find the real one!
4344 if (stride)
4345 *stride = element_type.GetByteSize(nullptr);
4346
4347 return element_type;
4348 }
4349 return CompilerType();
4350}
4351
4352CompilerType ClangASTContext::GetArrayType(lldb::opaque_compiler_type_t type,
4353 uint64_t size) {
4354 if (type) {
4355 clang::QualType qual_type(GetCanonicalQualType(type));
4356 if (clang::ASTContext *ast_ctx = getASTContext()) {
4357 if (size != 0)
4358 return CompilerType(
4359 ast_ctx, ast_ctx->getConstantArrayType(
4360 qual_type, llvm::APInt(64, size),
4361 clang::ArrayType::ArraySizeModifier::Normal, 0));
4362 else
4363 return CompilerType(
4364 ast_ctx,
4365 ast_ctx->getIncompleteArrayType(
4366 qual_type, clang::ArrayType::ArraySizeModifier::Normal, 0));
Greg Claytond8d4a572015-08-11 21:38:15 +00004367 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004368 }
4369
4370 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004371}
4372
Greg Claytona1e5dc82015-08-11 22:53:00 +00004373CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004374ClangASTContext::GetCanonicalType(lldb::opaque_compiler_type_t type) {
4375 if (type)
4376 return CompilerType(getASTContext(), GetCanonicalQualType(type));
4377 return CompilerType();
4378}
4379
4380static clang::QualType GetFullyUnqualifiedType_Impl(clang::ASTContext *ast,
4381 clang::QualType qual_type) {
4382 if (qual_type->isPointerType())
4383 qual_type = ast->getPointerType(
4384 GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
4385 else
4386 qual_type = qual_type.getUnqualifiedType();
4387 qual_type.removeLocalConst();
4388 qual_type.removeLocalRestrict();
4389 qual_type.removeLocalVolatile();
4390 return qual_type;
Enrico Granata639392f2016-08-30 20:39:58 +00004391}
4392
4393CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004394ClangASTContext::GetFullyUnqualifiedType(lldb::opaque_compiler_type_t type) {
4395 if (type)
4396 return CompilerType(
4397 getASTContext(),
4398 GetFullyUnqualifiedType_Impl(getASTContext(), GetQualType(type)));
4399 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004400}
4401
Kate Stoneb9c1b512016-09-06 20:57:50 +00004402int ClangASTContext::GetFunctionArgumentCount(
4403 lldb::opaque_compiler_type_t type) {
4404 if (type) {
4405 const clang::FunctionProtoType *func =
4406 llvm::dyn_cast<clang::FunctionProtoType>(GetCanonicalQualType(type));
4407 if (func)
4408 return func->getNumParams();
4409 }
4410 return -1;
4411}
4412
4413CompilerType ClangASTContext::GetFunctionArgumentTypeAtIndex(
4414 lldb::opaque_compiler_type_t type, size_t idx) {
4415 if (type) {
4416 const clang::FunctionProtoType *func =
4417 llvm::dyn_cast<clang::FunctionProtoType>(GetQualType(type));
4418 if (func) {
4419 const uint32_t num_args = func->getNumParams();
4420 if (idx < num_args)
4421 return CompilerType(getASTContext(), func->getParamType(idx));
4422 }
4423 }
4424 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004425}
4426
Greg Claytona1e5dc82015-08-11 22:53:00 +00004427CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004428ClangASTContext::GetFunctionReturnType(lldb::opaque_compiler_type_t type) {
4429 if (type) {
4430 clang::QualType qual_type(GetQualType(type));
4431 const clang::FunctionProtoType *func =
4432 llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
4433 if (func)
4434 return CompilerType(getASTContext(), func->getReturnType());
4435 }
4436 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004437}
4438
4439size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00004440ClangASTContext::GetNumMemberFunctions(lldb::opaque_compiler_type_t type) {
4441 size_t num_functions = 0;
4442 if (type) {
4443 clang::QualType qual_type(GetCanonicalQualType(type));
4444 switch (qual_type->getTypeClass()) {
4445 case clang::Type::Record:
4446 if (GetCompleteQualType(getASTContext(), qual_type)) {
4447 const clang::RecordType *record_type =
4448 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4449 const clang::RecordDecl *record_decl = record_type->getDecl();
4450 assert(record_decl);
4451 const clang::CXXRecordDecl *cxx_record_decl =
4452 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4453 if (cxx_record_decl)
4454 num_functions = std::distance(cxx_record_decl->method_begin(),
4455 cxx_record_decl->method_end());
4456 }
4457 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004458
Sean Callananf9c622a2016-09-30 18:44:43 +00004459 case clang::Type::ObjCObjectPointer: {
4460 const clang::ObjCObjectPointerType *objc_class_type =
4461 qual_type->getAsObjCInterfacePointerType();
4462 const clang::ObjCInterfaceType *objc_interface_type =
4463 objc_class_type->getInterfaceType();
4464 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004465 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4466 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004467 clang::ObjCInterfaceDecl *class_interface_decl =
4468 objc_interface_type->getDecl();
4469 if (class_interface_decl) {
4470 num_functions = std::distance(class_interface_decl->meth_begin(),
4471 class_interface_decl->meth_end());
Greg Claytond8d4a572015-08-11 21:38:15 +00004472 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004473 }
4474 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004475 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004476
4477 case clang::Type::ObjCObject:
4478 case clang::Type::ObjCInterface:
4479 if (GetCompleteType(type)) {
4480 const clang::ObjCObjectType *objc_class_type =
4481 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4482 if (objc_class_type) {
4483 clang::ObjCInterfaceDecl *class_interface_decl =
4484 objc_class_type->getInterface();
4485 if (class_interface_decl)
4486 num_functions = std::distance(class_interface_decl->meth_begin(),
4487 class_interface_decl->meth_end());
4488 }
4489 }
4490 break;
4491
4492 case clang::Type::Typedef:
4493 return CompilerType(getASTContext(),
4494 llvm::cast<clang::TypedefType>(qual_type)
4495 ->getDecl()
4496 ->getUnderlyingType())
4497 .GetNumMemberFunctions();
4498
4499 case clang::Type::Auto:
4500 return CompilerType(
4501 getASTContext(),
4502 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
4503 .GetNumMemberFunctions();
4504
4505 case clang::Type::Elaborated:
4506 return CompilerType(
4507 getASTContext(),
4508 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
4509 .GetNumMemberFunctions();
4510
4511 case clang::Type::Paren:
4512 return CompilerType(getASTContext(),
4513 llvm::cast<clang::ParenType>(qual_type)->desugar())
4514 .GetNumMemberFunctions();
4515
4516 default:
4517 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004518 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004519 }
4520 return num_functions;
Greg Claytond8d4a572015-08-11 21:38:15 +00004521}
4522
4523TypeMemberFunctionImpl
Kate Stoneb9c1b512016-09-06 20:57:50 +00004524ClangASTContext::GetMemberFunctionAtIndex(lldb::opaque_compiler_type_t type,
4525 size_t idx) {
4526 std::string name;
4527 MemberFunctionKind kind(MemberFunctionKind::eMemberFunctionKindUnknown);
4528 CompilerType clang_type;
4529 CompilerDecl clang_decl;
4530 if (type) {
4531 clang::QualType qual_type(GetCanonicalQualType(type));
4532 switch (qual_type->getTypeClass()) {
4533 case clang::Type::Record:
4534 if (GetCompleteQualType(getASTContext(), qual_type)) {
4535 const clang::RecordType *record_type =
4536 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
4537 const clang::RecordDecl *record_decl = record_type->getDecl();
4538 assert(record_decl);
4539 const clang::CXXRecordDecl *cxx_record_decl =
4540 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
4541 if (cxx_record_decl) {
4542 auto method_iter = cxx_record_decl->method_begin();
4543 auto method_end = cxx_record_decl->method_end();
4544 if (idx <
4545 static_cast<size_t>(std::distance(method_iter, method_end))) {
4546 std::advance(method_iter, idx);
4547 clang::CXXMethodDecl *cxx_method_decl =
4548 method_iter->getCanonicalDecl();
4549 if (cxx_method_decl) {
4550 name = cxx_method_decl->getDeclName().getAsString();
4551 if (cxx_method_decl->isStatic())
4552 kind = lldb::eMemberFunctionKindStaticMethod;
4553 else if (llvm::isa<clang::CXXConstructorDecl>(cxx_method_decl))
4554 kind = lldb::eMemberFunctionKindConstructor;
4555 else if (llvm::isa<clang::CXXDestructorDecl>(cxx_method_decl))
4556 kind = lldb::eMemberFunctionKindDestructor;
4557 else
4558 kind = lldb::eMemberFunctionKindInstanceMethod;
4559 clang_type = CompilerType(
4560 this, cxx_method_decl->getType().getAsOpaquePtr());
4561 clang_decl = CompilerDecl(this, cxx_method_decl);
4562 }
4563 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004564 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004565 }
4566 break;
Greg Claytond8d4a572015-08-11 21:38:15 +00004567
Sean Callananf9c622a2016-09-30 18:44:43 +00004568 case clang::Type::ObjCObjectPointer: {
4569 const clang::ObjCObjectPointerType *objc_class_type =
4570 qual_type->getAsObjCInterfacePointerType();
4571 const clang::ObjCInterfaceType *objc_interface_type =
4572 objc_class_type->getInterfaceType();
4573 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00004574 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
4575 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00004576 clang::ObjCInterfaceDecl *class_interface_decl =
4577 objc_interface_type->getDecl();
4578 if (class_interface_decl) {
4579 auto method_iter = class_interface_decl->meth_begin();
4580 auto method_end = class_interface_decl->meth_end();
4581 if (idx <
4582 static_cast<size_t>(std::distance(method_iter, method_end))) {
4583 std::advance(method_iter, idx);
4584 clang::ObjCMethodDecl *objc_method_decl =
4585 method_iter->getCanonicalDecl();
4586 if (objc_method_decl) {
4587 clang_decl = CompilerDecl(this, objc_method_decl);
4588 name = objc_method_decl->getSelector().getAsString();
4589 if (objc_method_decl->isClassMethod())
4590 kind = lldb::eMemberFunctionKindStaticMethod;
4591 else
4592 kind = lldb::eMemberFunctionKindInstanceMethod;
Kate Stoneb9c1b512016-09-06 20:57:50 +00004593 }
4594 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004595 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004596 }
4597 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00004598 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004599
Kate Stoneb9c1b512016-09-06 20:57:50 +00004600 case clang::Type::ObjCObject:
4601 case clang::Type::ObjCInterface:
4602 if (GetCompleteType(type)) {
4603 const clang::ObjCObjectType *objc_class_type =
4604 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
4605 if (objc_class_type) {
4606 clang::ObjCInterfaceDecl *class_interface_decl =
4607 objc_class_type->getInterface();
4608 if (class_interface_decl) {
4609 auto method_iter = class_interface_decl->meth_begin();
4610 auto method_end = class_interface_decl->meth_end();
4611 if (idx <
4612 static_cast<size_t>(std::distance(method_iter, method_end))) {
4613 std::advance(method_iter, idx);
4614 clang::ObjCMethodDecl *objc_method_decl =
4615 method_iter->getCanonicalDecl();
4616 if (objc_method_decl) {
4617 clang_decl = CompilerDecl(this, objc_method_decl);
4618 name = objc_method_decl->getSelector().getAsString();
4619 if (objc_method_decl->isClassMethod())
4620 kind = lldb::eMemberFunctionKindStaticMethod;
4621 else
4622 kind = lldb::eMemberFunctionKindInstanceMethod;
4623 }
4624 }
4625 }
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004626 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004627 }
4628 break;
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004629
Kate Stoneb9c1b512016-09-06 20:57:50 +00004630 case clang::Type::Typedef:
4631 return GetMemberFunctionAtIndex(llvm::cast<clang::TypedefType>(qual_type)
4632 ->getDecl()
4633 ->getUnderlyingType()
4634 .getAsOpaquePtr(),
4635 idx);
Ewan Crawford27fc7a72016-03-15 09:50:16 +00004636
Kate Stoneb9c1b512016-09-06 20:57:50 +00004637 case clang::Type::Auto:
4638 return GetMemberFunctionAtIndex(llvm::cast<clang::AutoType>(qual_type)
4639 ->getDeducedType()
4640 .getAsOpaquePtr(),
4641 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004642
Kate Stoneb9c1b512016-09-06 20:57:50 +00004643 case clang::Type::Elaborated:
4644 return GetMemberFunctionAtIndex(
4645 llvm::cast<clang::ElaboratedType>(qual_type)
4646 ->getNamedType()
4647 .getAsOpaquePtr(),
4648 idx);
Greg Clayton56939cb2015-09-17 22:23:34 +00004649
Kate Stoneb9c1b512016-09-06 20:57:50 +00004650 case clang::Type::Paren:
4651 return GetMemberFunctionAtIndex(
4652 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
4653 idx);
4654
4655 default:
4656 break;
Greg Clayton56939cb2015-09-17 22:23:34 +00004657 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004658 }
Greg Clayton56939cb2015-09-17 22:23:34 +00004659
Kate Stoneb9c1b512016-09-06 20:57:50 +00004660 if (kind == eMemberFunctionKindUnknown)
4661 return TypeMemberFunctionImpl();
4662 else
4663 return TypeMemberFunctionImpl(clang_type, clang_decl, name, kind);
Greg Clayton56939cb2015-09-17 22:23:34 +00004664}
4665
Greg Claytona1e5dc82015-08-11 22:53:00 +00004666CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00004667ClangASTContext::GetNonReferenceType(lldb::opaque_compiler_type_t type) {
4668 if (type)
4669 return CompilerType(getASTContext(),
4670 GetQualType(type).getNonReferenceType());
4671 return CompilerType();
4672}
4673
4674CompilerType ClangASTContext::CreateTypedefType(
4675 const CompilerType &type, const char *typedef_name,
4676 const CompilerDeclContext &compiler_decl_ctx) {
4677 if (type && typedef_name && typedef_name[0]) {
4678 ClangASTContext *ast =
4679 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
4680 if (!ast)
4681 return CompilerType();
4682 clang::ASTContext *clang_ast = ast->getASTContext();
4683 clang::QualType qual_type(ClangUtil::GetQualType(type));
4684
4685 clang::DeclContext *decl_ctx =
4686 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4687 if (decl_ctx == nullptr)
4688 decl_ctx = ast->getASTContext()->getTranslationUnitDecl();
4689
4690 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4691 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4692 &clang_ast->Idents.get(typedef_name),
4693 clang_ast->getTrivialTypeSourceInfo(qual_type));
4694
4695 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4696
4697 // Get a uniqued clang::QualType for the typedef decl type
4698 return CompilerType(clang_ast, clang_ast->getTypedefType(decl));
4699 }
4700 return CompilerType();
4701}
4702
4703CompilerType
4704ClangASTContext::GetPointeeType(lldb::opaque_compiler_type_t type) {
4705 if (type) {
4706 clang::QualType qual_type(GetQualType(type));
4707 return CompilerType(getASTContext(),
4708 qual_type.getTypePtr()->getPointeeType());
4709 }
4710 return CompilerType();
4711}
4712
4713CompilerType
4714ClangASTContext::GetPointerType(lldb::opaque_compiler_type_t type) {
4715 if (type) {
4716 clang::QualType qual_type(GetQualType(type));
4717
4718 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
4719 switch (type_class) {
4720 case clang::Type::ObjCObject:
4721 case clang::Type::ObjCInterface:
4722 return CompilerType(getASTContext(),
4723 getASTContext()->getObjCObjectPointerType(qual_type));
4724
4725 default:
4726 return CompilerType(getASTContext(),
4727 getASTContext()->getPointerType(qual_type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004728 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004729 }
4730 return CompilerType();
4731}
4732
4733CompilerType
4734ClangASTContext::GetLValueReferenceType(lldb::opaque_compiler_type_t type) {
4735 if (type)
4736 return CompilerType(this, getASTContext()
4737 ->getLValueReferenceType(GetQualType(type))
4738 .getAsOpaquePtr());
4739 else
Greg Claytona1e5dc82015-08-11 22:53:00 +00004740 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00004741}
4742
Kate Stoneb9c1b512016-09-06 20:57:50 +00004743CompilerType
4744ClangASTContext::GetRValueReferenceType(lldb::opaque_compiler_type_t type) {
4745 if (type)
4746 return CompilerType(this, getASTContext()
4747 ->getRValueReferenceType(GetQualType(type))
4748 .getAsOpaquePtr());
4749 else
4750 return CompilerType();
4751}
4752
4753CompilerType
4754ClangASTContext::AddConstModifier(lldb::opaque_compiler_type_t type) {
4755 if (type) {
4756 clang::QualType result(GetQualType(type));
4757 result.addConst();
4758 return CompilerType(this, result.getAsOpaquePtr());
4759 }
4760 return CompilerType();
4761}
4762
4763CompilerType
4764ClangASTContext::AddVolatileModifier(lldb::opaque_compiler_type_t type) {
4765 if (type) {
4766 clang::QualType result(GetQualType(type));
4767 result.addVolatile();
4768 return CompilerType(this, result.getAsOpaquePtr());
4769 }
4770 return CompilerType();
4771}
4772
4773CompilerType
4774ClangASTContext::AddRestrictModifier(lldb::opaque_compiler_type_t type) {
4775 if (type) {
4776 clang::QualType result(GetQualType(type));
4777 result.addRestrict();
4778 return CompilerType(this, result.getAsOpaquePtr());
4779 }
4780 return CompilerType();
4781}
4782
4783CompilerType
4784ClangASTContext::CreateTypedef(lldb::opaque_compiler_type_t type,
4785 const char *typedef_name,
4786 const CompilerDeclContext &compiler_decl_ctx) {
4787 if (type) {
4788 clang::ASTContext *clang_ast = getASTContext();
4789 clang::QualType qual_type(GetQualType(type));
4790
4791 clang::DeclContext *decl_ctx =
4792 ClangASTContext::DeclContextGetAsDeclContext(compiler_decl_ctx);
4793 if (decl_ctx == nullptr)
4794 decl_ctx = getASTContext()->getTranslationUnitDecl();
4795
4796 clang::TypedefDecl *decl = clang::TypedefDecl::Create(
4797 *clang_ast, decl_ctx, clang::SourceLocation(), clang::SourceLocation(),
4798 &clang_ast->Idents.get(typedef_name),
4799 clang_ast->getTrivialTypeSourceInfo(qual_type));
4800
4801 clang::TagDecl *tdecl = nullptr;
4802 if (!qual_type.isNull()) {
4803 if (const clang::RecordType *rt = qual_type->getAs<clang::RecordType>())
4804 tdecl = rt->getDecl();
4805 if (const clang::EnumType *et = qual_type->getAs<clang::EnumType>())
4806 tdecl = et->getDecl();
4807 }
4808
4809 // Check whether this declaration is an anonymous struct, union, or enum,
4810 // hidden behind a typedef. If so, we
4811 // try to check whether we have a typedef tag to attach to the original
4812 // record declaration
4813 if (tdecl && !tdecl->getIdentifier() && !tdecl->getTypedefNameForAnonDecl())
4814 tdecl->setTypedefNameForAnonDecl(decl);
4815
4816 decl->setAccess(clang::AS_public); // TODO respect proper access specifier
4817
4818 // Get a uniqued clang::QualType for the typedef decl type
4819 return CompilerType(this, clang_ast->getTypedefType(decl).getAsOpaquePtr());
4820 }
4821 return CompilerType();
4822}
4823
4824CompilerType
4825ClangASTContext::GetTypedefedType(lldb::opaque_compiler_type_t type) {
4826 if (type) {
4827 const clang::TypedefType *typedef_type =
4828 llvm::dyn_cast<clang::TypedefType>(GetQualType(type));
4829 if (typedef_type)
4830 return CompilerType(getASTContext(),
4831 typedef_type->getDecl()->getUnderlyingType());
4832 }
4833 return CompilerType();
4834}
Greg Claytond8d4a572015-08-11 21:38:15 +00004835
4836//----------------------------------------------------------------------
4837// Create related types using the current type's AST
4838//----------------------------------------------------------------------
4839
Kate Stoneb9c1b512016-09-06 20:57:50 +00004840CompilerType ClangASTContext::GetBasicTypeFromAST(lldb::BasicType basic_type) {
4841 return ClangASTContext::GetBasicType(getASTContext(), basic_type);
Greg Claytond8d4a572015-08-11 21:38:15 +00004842}
4843//----------------------------------------------------------------------
4844// Exploring the type
4845//----------------------------------------------------------------------
4846
Kate Stoneb9c1b512016-09-06 20:57:50 +00004847uint64_t ClangASTContext::GetBitSize(lldb::opaque_compiler_type_t type,
4848 ExecutionContextScope *exe_scope) {
4849 if (GetCompleteType(type)) {
Greg Claytond8d4a572015-08-11 21:38:15 +00004850 clang::QualType qual_type(GetCanonicalQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00004851 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
Kate Stoneb9c1b512016-09-06 20:57:50 +00004852 switch (type_class) {
4853 case clang::Type::Record:
4854 if (GetCompleteType(type))
4855 return getASTContext()->getTypeSize(qual_type);
4856 else
4857 return 0;
4858 break;
Enrico Granata36f51e42015-12-18 22:41:25 +00004859
Kate Stoneb9c1b512016-09-06 20:57:50 +00004860 case clang::Type::ObjCInterface:
4861 case clang::Type::ObjCObject: {
4862 ExecutionContext exe_ctx(exe_scope);
4863 Process *process = exe_ctx.GetProcessPtr();
4864 if (process) {
4865 ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
4866 if (objc_runtime) {
4867 uint64_t bit_size = 0;
4868 if (objc_runtime->GetTypeBitSize(
4869 CompilerType(getASTContext(), qual_type), bit_size))
4870 return bit_size;
4871 }
4872 } else {
4873 static bool g_printed = false;
4874 if (!g_printed) {
4875 StreamString s;
4876 DumpTypeDescription(type, &s);
4877
4878 llvm::outs() << "warning: trying to determine the size of type ";
4879 llvm::outs() << s.GetString() << "\n";
4880 llvm::outs() << "without a valid ExecutionContext. this is not "
4881 "reliable. please file a bug against LLDB.\n";
4882 llvm::outs() << "backtrace:\n";
4883 llvm::sys::PrintStackTrace(llvm::outs());
4884 llvm::outs() << "\n";
4885 g_printed = true;
4886 }
4887 }
Greg Claytond8d4a572015-08-11 21:38:15 +00004888 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00004889 LLVM_FALLTHROUGH;
4890 default:
4891 const uint32_t bit_size = getASTContext()->getTypeSize(qual_type);
4892 if (bit_size == 0) {
4893 if (qual_type->isIncompleteArrayType())
4894 return getASTContext()->getTypeSize(
4895 qual_type->getArrayElementTypeNoTypeQual()
4896 ->getCanonicalTypeUnqualified());
4897 }
4898 if (qual_type->isObjCObjectOrInterfaceType())
4899 return bit_size +
4900 getASTContext()->getTypeSize(
4901 getASTContext()->ObjCBuiltinClassTy);
4902 return bit_size;
4903 }
4904 }
4905 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00004906}
4907
Kate Stoneb9c1b512016-09-06 20:57:50 +00004908size_t ClangASTContext::GetTypeBitAlign(lldb::opaque_compiler_type_t type) {
4909 if (GetCompleteType(type))
4910 return getASTContext()->getTypeAlign(GetQualType(type));
4911 return 0;
4912}
4913
4914lldb::Encoding ClangASTContext::GetEncoding(lldb::opaque_compiler_type_t type,
4915 uint64_t &count) {
4916 if (!type)
4917 return lldb::eEncodingInvalid;
4918
4919 count = 1;
4920 clang::QualType qual_type(GetCanonicalQualType(type));
4921
4922 switch (qual_type->getTypeClass()) {
4923 case clang::Type::UnaryTransform:
4924 break;
4925
4926 case clang::Type::FunctionNoProto:
4927 case clang::Type::FunctionProto:
4928 break;
4929
4930 case clang::Type::IncompleteArray:
4931 case clang::Type::VariableArray:
4932 break;
4933
4934 case clang::Type::ConstantArray:
4935 break;
4936
4937 case clang::Type::ExtVector:
4938 case clang::Type::Vector:
4939 // TODO: Set this to more than one???
4940 break;
4941
4942 case clang::Type::Builtin:
4943 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
4944 case clang::BuiltinType::Void:
4945 break;
4946
4947 case clang::BuiltinType::Bool:
4948 case clang::BuiltinType::Char_S:
4949 case clang::BuiltinType::SChar:
4950 case clang::BuiltinType::WChar_S:
4951 case clang::BuiltinType::Char16:
4952 case clang::BuiltinType::Char32:
4953 case clang::BuiltinType::Short:
4954 case clang::BuiltinType::Int:
4955 case clang::BuiltinType::Long:
4956 case clang::BuiltinType::LongLong:
4957 case clang::BuiltinType::Int128:
4958 return lldb::eEncodingSint;
4959
4960 case clang::BuiltinType::Char_U:
4961 case clang::BuiltinType::UChar:
4962 case clang::BuiltinType::WChar_U:
4963 case clang::BuiltinType::UShort:
4964 case clang::BuiltinType::UInt:
4965 case clang::BuiltinType::ULong:
4966 case clang::BuiltinType::ULongLong:
4967 case clang::BuiltinType::UInt128:
4968 return lldb::eEncodingUint;
4969
4970 case clang::BuiltinType::Half:
4971 case clang::BuiltinType::Float:
4972 case clang::BuiltinType::Float128:
4973 case clang::BuiltinType::Double:
4974 case clang::BuiltinType::LongDouble:
4975 return lldb::eEncodingIEEE754;
4976
4977 case clang::BuiltinType::ObjCClass:
4978 case clang::BuiltinType::ObjCId:
4979 case clang::BuiltinType::ObjCSel:
4980 return lldb::eEncodingUint;
4981
4982 case clang::BuiltinType::NullPtr:
4983 return lldb::eEncodingUint;
4984
4985 case clang::BuiltinType::Kind::ARCUnbridgedCast:
4986 case clang::BuiltinType::Kind::BoundMember:
4987 case clang::BuiltinType::Kind::BuiltinFn:
4988 case clang::BuiltinType::Kind::Dependent:
4989 case clang::BuiltinType::Kind::OCLClkEvent:
4990 case clang::BuiltinType::Kind::OCLEvent:
4991 case clang::BuiltinType::Kind::OCLImage1dRO:
4992 case clang::BuiltinType::Kind::OCLImage1dWO:
4993 case clang::BuiltinType::Kind::OCLImage1dRW:
4994 case clang::BuiltinType::Kind::OCLImage1dArrayRO:
4995 case clang::BuiltinType::Kind::OCLImage1dArrayWO:
4996 case clang::BuiltinType::Kind::OCLImage1dArrayRW:
4997 case clang::BuiltinType::Kind::OCLImage1dBufferRO:
4998 case clang::BuiltinType::Kind::OCLImage1dBufferWO:
4999 case clang::BuiltinType::Kind::OCLImage1dBufferRW:
5000 case clang::BuiltinType::Kind::OCLImage2dRO:
5001 case clang::BuiltinType::Kind::OCLImage2dWO:
5002 case clang::BuiltinType::Kind::OCLImage2dRW:
5003 case clang::BuiltinType::Kind::OCLImage2dArrayRO:
5004 case clang::BuiltinType::Kind::OCLImage2dArrayWO:
5005 case clang::BuiltinType::Kind::OCLImage2dArrayRW:
5006 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRO:
5007 case clang::BuiltinType::Kind::OCLImage2dArrayDepthWO:
5008 case clang::BuiltinType::Kind::OCLImage2dArrayDepthRW:
5009 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARO:
5010 case clang::BuiltinType::Kind::OCLImage2dArrayMSAAWO:
5011 case clang::BuiltinType::Kind::OCLImage2dArrayMSAARW:
5012 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRO:
5013 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthWO:
5014 case clang::BuiltinType::Kind::OCLImage2dArrayMSAADepthRW:
5015 case clang::BuiltinType::Kind::OCLImage2dDepthRO:
5016 case clang::BuiltinType::Kind::OCLImage2dDepthWO:
5017 case clang::BuiltinType::Kind::OCLImage2dDepthRW:
5018 case clang::BuiltinType::Kind::OCLImage2dMSAARO:
5019 case clang::BuiltinType::Kind::OCLImage2dMSAAWO:
5020 case clang::BuiltinType::Kind::OCLImage2dMSAARW:
5021 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRO:
5022 case clang::BuiltinType::Kind::OCLImage2dMSAADepthWO:
5023 case clang::BuiltinType::Kind::OCLImage2dMSAADepthRW:
5024 case clang::BuiltinType::Kind::OCLImage3dRO:
5025 case clang::BuiltinType::Kind::OCLImage3dWO:
5026 case clang::BuiltinType::Kind::OCLImage3dRW:
5027 case clang::BuiltinType::Kind::OCLQueue:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005028 case clang::BuiltinType::Kind::OCLReserveID:
5029 case clang::BuiltinType::Kind::OCLSampler:
5030 case clang::BuiltinType::Kind::OMPArraySection:
5031 case clang::BuiltinType::Kind::Overload:
5032 case clang::BuiltinType::Kind::PseudoObject:
5033 case clang::BuiltinType::Kind::UnknownAny:
5034 break;
5035 }
5036 break;
5037 // All pointer types are represented as unsigned integer encodings.
5038 // We may nee to add a eEncodingPointer if we ever need to know the
5039 // difference
5040 case clang::Type::ObjCObjectPointer:
5041 case clang::Type::BlockPointer:
5042 case clang::Type::Pointer:
5043 case clang::Type::LValueReference:
5044 case clang::Type::RValueReference:
5045 case clang::Type::MemberPointer:
5046 return lldb::eEncodingUint;
5047 case clang::Type::Complex: {
5048 lldb::Encoding encoding = lldb::eEncodingIEEE754;
5049 if (qual_type->isComplexType())
5050 encoding = lldb::eEncodingIEEE754;
5051 else {
5052 const clang::ComplexType *complex_type =
5053 qual_type->getAsComplexIntegerType();
5054 if (complex_type)
5055 encoding = CompilerType(getASTContext(), complex_type->getElementType())
5056 .GetEncoding(count);
5057 else
5058 encoding = lldb::eEncodingSint;
5059 }
5060 count = 2;
5061 return encoding;
5062 }
5063
5064 case clang::Type::ObjCInterface:
5065 break;
5066 case clang::Type::Record:
5067 break;
5068 case clang::Type::Enum:
5069 return lldb::eEncodingSint;
5070 case clang::Type::Typedef:
5071 return CompilerType(getASTContext(),
5072 llvm::cast<clang::TypedefType>(qual_type)
5073 ->getDecl()
5074 ->getUnderlyingType())
5075 .GetEncoding(count);
5076
5077 case clang::Type::Auto:
5078 return CompilerType(
5079 getASTContext(),
5080 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5081 .GetEncoding(count);
5082
5083 case clang::Type::Elaborated:
5084 return CompilerType(
5085 getASTContext(),
5086 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5087 .GetEncoding(count);
5088
5089 case clang::Type::Paren:
5090 return CompilerType(getASTContext(),
5091 llvm::cast<clang::ParenType>(qual_type)->desugar())
5092 .GetEncoding(count);
5093
5094 case clang::Type::DependentSizedArray:
5095 case clang::Type::DependentSizedExtVector:
5096 case clang::Type::UnresolvedUsing:
5097 case clang::Type::Attributed:
5098 case clang::Type::TemplateTypeParm:
5099 case clang::Type::SubstTemplateTypeParm:
5100 case clang::Type::SubstTemplateTypeParmPack:
5101 case clang::Type::InjectedClassName:
5102 case clang::Type::DependentName:
5103 case clang::Type::DependentTemplateSpecialization:
5104 case clang::Type::PackExpansion:
5105 case clang::Type::ObjCObject:
5106
5107 case clang::Type::TypeOfExpr:
5108 case clang::Type::TypeOf:
5109 case clang::Type::Decltype:
5110 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005111 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005112 case clang::Type::Atomic:
5113 case clang::Type::Adjusted:
5114 case clang::Type::Pipe:
5115 break;
5116
5117 // pointer type decayed from an array or function type.
5118 case clang::Type::Decayed:
5119 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005120 case clang::Type::ObjCTypeParam:
5121 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005122 }
5123 count = 0;
5124 return lldb::eEncodingInvalid;
5125}
5126
5127lldb::Format ClangASTContext::GetFormat(lldb::opaque_compiler_type_t type) {
5128 if (!type)
5129 return lldb::eFormatDefault;
5130
5131 clang::QualType qual_type(GetCanonicalQualType(type));
5132
5133 switch (qual_type->getTypeClass()) {
5134 case clang::Type::UnaryTransform:
5135 break;
5136
5137 case clang::Type::FunctionNoProto:
5138 case clang::Type::FunctionProto:
5139 break;
5140
5141 case clang::Type::IncompleteArray:
5142 case clang::Type::VariableArray:
5143 break;
5144
5145 case clang::Type::ConstantArray:
5146 return lldb::eFormatVoid; // no value
5147
5148 case clang::Type::ExtVector:
5149 case clang::Type::Vector:
5150 break;
5151
5152 case clang::Type::Builtin:
5153 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5154 // default: assert(0 && "Unknown builtin type!");
5155 case clang::BuiltinType::UnknownAny:
5156 case clang::BuiltinType::Void:
5157 case clang::BuiltinType::BoundMember:
5158 break;
5159
5160 case clang::BuiltinType::Bool:
5161 return lldb::eFormatBoolean;
5162 case clang::BuiltinType::Char_S:
5163 case clang::BuiltinType::SChar:
5164 case clang::BuiltinType::WChar_S:
5165 case clang::BuiltinType::Char_U:
5166 case clang::BuiltinType::UChar:
5167 case clang::BuiltinType::WChar_U:
5168 return lldb::eFormatChar;
5169 case clang::BuiltinType::Char16:
5170 return lldb::eFormatUnicode16;
5171 case clang::BuiltinType::Char32:
5172 return lldb::eFormatUnicode32;
5173 case clang::BuiltinType::UShort:
5174 return lldb::eFormatUnsigned;
5175 case clang::BuiltinType::Short:
5176 return lldb::eFormatDecimal;
5177 case clang::BuiltinType::UInt:
5178 return lldb::eFormatUnsigned;
5179 case clang::BuiltinType::Int:
5180 return lldb::eFormatDecimal;
5181 case clang::BuiltinType::ULong:
5182 return lldb::eFormatUnsigned;
5183 case clang::BuiltinType::Long:
5184 return lldb::eFormatDecimal;
5185 case clang::BuiltinType::ULongLong:
5186 return lldb::eFormatUnsigned;
5187 case clang::BuiltinType::LongLong:
5188 return lldb::eFormatDecimal;
5189 case clang::BuiltinType::UInt128:
5190 return lldb::eFormatUnsigned;
5191 case clang::BuiltinType::Int128:
5192 return lldb::eFormatDecimal;
5193 case clang::BuiltinType::Half:
5194 case clang::BuiltinType::Float:
5195 case clang::BuiltinType::Double:
5196 case clang::BuiltinType::LongDouble:
5197 return lldb::eFormatFloat;
5198 default:
5199 return lldb::eFormatHex;
5200 }
5201 break;
5202 case clang::Type::ObjCObjectPointer:
5203 return lldb::eFormatHex;
5204 case clang::Type::BlockPointer:
5205 return lldb::eFormatHex;
5206 case clang::Type::Pointer:
5207 return lldb::eFormatHex;
5208 case clang::Type::LValueReference:
5209 case clang::Type::RValueReference:
5210 return lldb::eFormatHex;
5211 case clang::Type::MemberPointer:
5212 break;
5213 case clang::Type::Complex: {
5214 if (qual_type->isComplexType())
5215 return lldb::eFormatComplex;
5216 else
5217 return lldb::eFormatComplexInteger;
5218 }
5219 case clang::Type::ObjCInterface:
5220 break;
5221 case clang::Type::Record:
5222 break;
5223 case clang::Type::Enum:
5224 return lldb::eFormatEnum;
5225 case clang::Type::Typedef:
5226 return CompilerType(getASTContext(),
5227 llvm::cast<clang::TypedefType>(qual_type)
5228 ->getDecl()
5229 ->getUnderlyingType())
5230 .GetFormat();
5231 case clang::Type::Auto:
5232 return CompilerType(getASTContext(),
5233 llvm::cast<clang::AutoType>(qual_type)->desugar())
5234 .GetFormat();
5235 case clang::Type::Paren:
5236 return CompilerType(getASTContext(),
5237 llvm::cast<clang::ParenType>(qual_type)->desugar())
5238 .GetFormat();
5239 case clang::Type::Elaborated:
5240 return CompilerType(
5241 getASTContext(),
5242 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5243 .GetFormat();
5244 case clang::Type::DependentSizedArray:
5245 case clang::Type::DependentSizedExtVector:
5246 case clang::Type::UnresolvedUsing:
5247 case clang::Type::Attributed:
5248 case clang::Type::TemplateTypeParm:
5249 case clang::Type::SubstTemplateTypeParm:
5250 case clang::Type::SubstTemplateTypeParmPack:
5251 case clang::Type::InjectedClassName:
5252 case clang::Type::DependentName:
5253 case clang::Type::DependentTemplateSpecialization:
5254 case clang::Type::PackExpansion:
5255 case clang::Type::ObjCObject:
5256
5257 case clang::Type::TypeOfExpr:
5258 case clang::Type::TypeOf:
5259 case clang::Type::Decltype:
5260 case clang::Type::TemplateSpecialization:
Pavel Labath4f19fce22017-02-17 13:39:50 +00005261 case clang::Type::DeducedTemplateSpecialization:
Kate Stoneb9c1b512016-09-06 20:57:50 +00005262 case clang::Type::Atomic:
5263 case clang::Type::Adjusted:
5264 case clang::Type::Pipe:
5265 break;
5266
5267 // pointer type decayed from an array or function type.
5268 case clang::Type::Decayed:
5269 break;
Zachary Turner5a8ad4592016-10-05 17:07:34 +00005270 case clang::Type::ObjCTypeParam:
5271 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00005272 }
5273 // We don't know hot to display this type...
5274 return lldb::eFormatBytes;
5275}
5276
5277static bool ObjCDeclHasIVars(clang::ObjCInterfaceDecl *class_interface_decl,
5278 bool check_superclass) {
5279 while (class_interface_decl) {
5280 if (class_interface_decl->ivar_size() > 0)
5281 return true;
5282
5283 if (check_superclass)
5284 class_interface_decl = class_interface_decl->getSuperClass();
5285 else
5286 break;
5287 }
5288 return false;
5289}
5290
5291uint32_t ClangASTContext::GetNumChildren(lldb::opaque_compiler_type_t type,
5292 bool omit_empty_base_classes) {
5293 if (!type)
5294 return 0;
5295
5296 uint32_t num_children = 0;
5297 clang::QualType qual_type(GetQualType(type));
5298 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5299 switch (type_class) {
5300 case clang::Type::Builtin:
5301 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5302 case clang::BuiltinType::ObjCId: // child is Class
5303 case clang::BuiltinType::ObjCClass: // child is Class
5304 num_children = 1;
5305 break;
5306
5307 default:
5308 break;
5309 }
5310 break;
5311
5312 case clang::Type::Complex:
5313 return 0;
5314
5315 case clang::Type::Record:
5316 if (GetCompleteQualType(getASTContext(), qual_type)) {
5317 const clang::RecordType *record_type =
5318 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5319 const clang::RecordDecl *record_decl = record_type->getDecl();
5320 assert(record_decl);
5321 const clang::CXXRecordDecl *cxx_record_decl =
5322 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
5323 if (cxx_record_decl) {
5324 if (omit_empty_base_classes) {
5325 // Check each base classes to see if it or any of its
5326 // base classes contain any fields. This can help
5327 // limit the noise in variable views by not having to
5328 // show base classes that contain no members.
5329 clang::CXXRecordDecl::base_class_const_iterator base_class,
5330 base_class_end;
5331 for (base_class = cxx_record_decl->bases_begin(),
5332 base_class_end = cxx_record_decl->bases_end();
5333 base_class != base_class_end; ++base_class) {
5334 const clang::CXXRecordDecl *base_class_decl =
5335 llvm::cast<clang::CXXRecordDecl>(
5336 base_class->getType()
5337 ->getAs<clang::RecordType>()
5338 ->getDecl());
5339
5340 // Skip empty base classes
5341 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
5342 continue;
5343
5344 num_children++;
5345 }
5346 } else {
5347 // Include all base classes
5348 num_children += cxx_record_decl->getNumBases();
5349 }
5350 }
5351 clang::RecordDecl::field_iterator field, field_end;
5352 for (field = record_decl->field_begin(),
5353 field_end = record_decl->field_end();
5354 field != field_end; ++field)
5355 ++num_children;
5356 }
5357 break;
5358
5359 case clang::Type::ObjCObject:
5360 case clang::Type::ObjCInterface:
5361 if (GetCompleteQualType(getASTContext(), qual_type)) {
5362 const clang::ObjCObjectType *objc_class_type =
5363 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5364 assert(objc_class_type);
5365 if (objc_class_type) {
5366 clang::ObjCInterfaceDecl *class_interface_decl =
5367 objc_class_type->getInterface();
5368
5369 if (class_interface_decl) {
5370
5371 clang::ObjCInterfaceDecl *superclass_interface_decl =
5372 class_interface_decl->getSuperClass();
5373 if (superclass_interface_decl) {
5374 if (omit_empty_base_classes) {
5375 if (ObjCDeclHasIVars(superclass_interface_decl, true))
5376 ++num_children;
5377 } else
5378 ++num_children;
5379 }
5380
5381 num_children += class_interface_decl->ivar_size();
5382 }
5383 }
5384 }
5385 break;
5386
5387 case clang::Type::ObjCObjectPointer: {
5388 const clang::ObjCObjectPointerType *pointer_type =
5389 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr());
5390 clang::QualType pointee_type = pointer_type->getPointeeType();
5391 uint32_t num_pointee_children =
5392 CompilerType(getASTContext(), pointee_type)
5393 .GetNumChildren(omit_empty_base_classes);
5394 // If this type points to a simple type, then it has 1 child
5395 if (num_pointee_children == 0)
5396 num_children = 1;
5397 else
5398 num_children = num_pointee_children;
5399 } break;
5400
5401 case clang::Type::Vector:
5402 case clang::Type::ExtVector:
5403 num_children =
5404 llvm::cast<clang::VectorType>(qual_type.getTypePtr())->getNumElements();
5405 break;
5406
5407 case clang::Type::ConstantArray:
5408 num_children = llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr())
5409 ->getSize()
5410 .getLimitedValue();
5411 break;
5412
5413 case clang::Type::Pointer: {
5414 const clang::PointerType *pointer_type =
5415 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
5416 clang::QualType pointee_type(pointer_type->getPointeeType());
5417 uint32_t num_pointee_children =
5418 CompilerType(getASTContext(), pointee_type)
5419 .GetNumChildren(omit_empty_base_classes);
5420 if (num_pointee_children == 0) {
5421 // We have a pointer to a pointee type that claims it has no children.
5422 // We will want to look at
5423 num_children = GetNumPointeeChildren(pointee_type);
5424 } else
5425 num_children = num_pointee_children;
5426 } break;
5427
5428 case clang::Type::LValueReference:
5429 case clang::Type::RValueReference: {
5430 const clang::ReferenceType *reference_type =
5431 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
5432 clang::QualType pointee_type = reference_type->getPointeeType();
5433 uint32_t num_pointee_children =
5434 CompilerType(getASTContext(), pointee_type)
5435 .GetNumChildren(omit_empty_base_classes);
5436 // If this type points to a simple type, then it has 1 child
5437 if (num_pointee_children == 0)
5438 num_children = 1;
5439 else
5440 num_children = num_pointee_children;
5441 } break;
5442
5443 case clang::Type::Typedef:
5444 num_children =
5445 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5446 ->getDecl()
5447 ->getUnderlyingType())
5448 .GetNumChildren(omit_empty_base_classes);
5449 break;
5450
5451 case clang::Type::Auto:
5452 num_children =
5453 CompilerType(getASTContext(),
5454 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5455 .GetNumChildren(omit_empty_base_classes);
5456 break;
5457
5458 case clang::Type::Elaborated:
5459 num_children =
5460 CompilerType(
5461 getASTContext(),
5462 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5463 .GetNumChildren(omit_empty_base_classes);
5464 break;
5465
5466 case clang::Type::Paren:
5467 num_children =
5468 CompilerType(getASTContext(),
5469 llvm::cast<clang::ParenType>(qual_type)->desugar())
5470 .GetNumChildren(omit_empty_base_classes);
5471 break;
5472 default:
5473 break;
5474 }
5475 return num_children;
5476}
5477
5478CompilerType ClangASTContext::GetBuiltinTypeByName(const ConstString &name) {
5479 return GetBasicType(GetBasicTypeEnumeration(name));
Greg Clayton56939cb2015-09-17 22:23:34 +00005480}
5481
Greg Claytond8d4a572015-08-11 21:38:15 +00005482lldb::BasicType
Kate Stoneb9c1b512016-09-06 20:57:50 +00005483ClangASTContext::GetBasicTypeEnumeration(lldb::opaque_compiler_type_t type) {
5484 if (type) {
5485 clang::QualType qual_type(GetQualType(type));
5486 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5487 if (type_class == clang::Type::Builtin) {
5488 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
5489 case clang::BuiltinType::Void:
5490 return eBasicTypeVoid;
5491 case clang::BuiltinType::Bool:
5492 return eBasicTypeBool;
5493 case clang::BuiltinType::Char_S:
5494 return eBasicTypeSignedChar;
5495 case clang::BuiltinType::Char_U:
5496 return eBasicTypeUnsignedChar;
5497 case clang::BuiltinType::Char16:
5498 return eBasicTypeChar16;
5499 case clang::BuiltinType::Char32:
5500 return eBasicTypeChar32;
5501 case clang::BuiltinType::UChar:
5502 return eBasicTypeUnsignedChar;
5503 case clang::BuiltinType::SChar:
5504 return eBasicTypeSignedChar;
5505 case clang::BuiltinType::WChar_S:
5506 return eBasicTypeSignedWChar;
5507 case clang::BuiltinType::WChar_U:
5508 return eBasicTypeUnsignedWChar;
5509 case clang::BuiltinType::Short:
5510 return eBasicTypeShort;
5511 case clang::BuiltinType::UShort:
5512 return eBasicTypeUnsignedShort;
5513 case clang::BuiltinType::Int:
5514 return eBasicTypeInt;
5515 case clang::BuiltinType::UInt:
5516 return eBasicTypeUnsignedInt;
5517 case clang::BuiltinType::Long:
5518 return eBasicTypeLong;
5519 case clang::BuiltinType::ULong:
5520 return eBasicTypeUnsignedLong;
5521 case clang::BuiltinType::LongLong:
5522 return eBasicTypeLongLong;
5523 case clang::BuiltinType::ULongLong:
5524 return eBasicTypeUnsignedLongLong;
5525 case clang::BuiltinType::Int128:
5526 return eBasicTypeInt128;
5527 case clang::BuiltinType::UInt128:
5528 return eBasicTypeUnsignedInt128;
5529
5530 case clang::BuiltinType::Half:
5531 return eBasicTypeHalf;
5532 case clang::BuiltinType::Float:
5533 return eBasicTypeFloat;
5534 case clang::BuiltinType::Double:
5535 return eBasicTypeDouble;
5536 case clang::BuiltinType::LongDouble:
5537 return eBasicTypeLongDouble;
5538
5539 case clang::BuiltinType::NullPtr:
5540 return eBasicTypeNullPtr;
5541 case clang::BuiltinType::ObjCId:
5542 return eBasicTypeObjCID;
5543 case clang::BuiltinType::ObjCClass:
5544 return eBasicTypeObjCClass;
5545 case clang::BuiltinType::ObjCSel:
5546 return eBasicTypeObjCSel;
5547 default:
5548 return eBasicTypeOther;
5549 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005550 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005551 }
5552 return eBasicTypeInvalid;
Greg Claytond8d4a572015-08-11 21:38:15 +00005553}
5554
Kate Stoneb9c1b512016-09-06 20:57:50 +00005555void ClangASTContext::ForEachEnumerator(
5556 lldb::opaque_compiler_type_t type,
5557 std::function<bool(const CompilerType &integer_type,
5558 const ConstString &name,
5559 const llvm::APSInt &value)> const &callback) {
5560 const clang::EnumType *enum_type =
5561 llvm::dyn_cast<clang::EnumType>(GetCanonicalQualType(type));
5562 if (enum_type) {
5563 const clang::EnumDecl *enum_decl = enum_type->getDecl();
5564 if (enum_decl) {
5565 CompilerType integer_type(this,
5566 enum_decl->getIntegerType().getAsOpaquePtr());
Greg Clayton99558cc42015-08-24 23:46:31 +00005567
Kate Stoneb9c1b512016-09-06 20:57:50 +00005568 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5569 for (enum_pos = enum_decl->enumerator_begin(),
5570 enum_end_pos = enum_decl->enumerator_end();
5571 enum_pos != enum_end_pos; ++enum_pos) {
5572 ConstString name(enum_pos->getNameAsString().c_str());
5573 if (!callback(integer_type, name, enum_pos->getInitVal()))
5574 break;
5575 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005576 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005577 }
Greg Clayton99558cc42015-08-24 23:46:31 +00005578}
5579
Greg Claytond8d4a572015-08-11 21:38:15 +00005580#pragma mark Aggregate Types
5581
Kate Stoneb9c1b512016-09-06 20:57:50 +00005582uint32_t ClangASTContext::GetNumFields(lldb::opaque_compiler_type_t type) {
5583 if (!type)
5584 return 0;
Enrico Granata36f51e42015-12-18 22:41:25 +00005585
Kate Stoneb9c1b512016-09-06 20:57:50 +00005586 uint32_t count = 0;
5587 clang::QualType qual_type(GetCanonicalQualType(type));
5588 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5589 switch (type_class) {
5590 case clang::Type::Record:
5591 if (GetCompleteType(type)) {
5592 const clang::RecordType *record_type =
5593 llvm::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
5594 if (record_type) {
5595 clang::RecordDecl *record_decl = record_type->getDecl();
5596 if (record_decl) {
5597 uint32_t field_idx = 0;
5598 clang::RecordDecl::field_iterator field, field_end;
5599 for (field = record_decl->field_begin(),
5600 field_end = record_decl->field_end();
5601 field != field_end; ++field)
5602 ++field_idx;
5603 count = field_idx;
5604 }
5605 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005606 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005607 break;
5608
5609 case clang::Type::Typedef:
5610 count =
5611 CompilerType(getASTContext(), llvm::cast<clang::TypedefType>(qual_type)
5612 ->getDecl()
5613 ->getUnderlyingType())
5614 .GetNumFields();
5615 break;
5616
5617 case clang::Type::Auto:
5618 count =
5619 CompilerType(getASTContext(),
5620 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5621 .GetNumFields();
5622 break;
5623
5624 case clang::Type::Elaborated:
5625 count = CompilerType(
5626 getASTContext(),
5627 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5628 .GetNumFields();
5629 break;
5630
5631 case clang::Type::Paren:
5632 count = CompilerType(getASTContext(),
5633 llvm::cast<clang::ParenType>(qual_type)->desugar())
5634 .GetNumFields();
5635 break;
5636
Sean Callananf9c622a2016-09-30 18:44:43 +00005637 case clang::Type::ObjCObjectPointer: {
5638 const clang::ObjCObjectPointerType *objc_class_type =
5639 qual_type->getAsObjCInterfacePointerType();
5640 const clang::ObjCInterfaceType *objc_interface_type =
5641 objc_class_type->getInterfaceType();
5642 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005643 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5644 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005645 clang::ObjCInterfaceDecl *class_interface_decl =
5646 objc_interface_type->getDecl();
5647 if (class_interface_decl) {
5648 count = class_interface_decl->ivar_size();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005649 }
5650 }
5651 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005652 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005653
5654 case clang::Type::ObjCObject:
5655 case clang::Type::ObjCInterface:
5656 if (GetCompleteType(type)) {
5657 const clang::ObjCObjectType *objc_class_type =
5658 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5659 if (objc_class_type) {
5660 clang::ObjCInterfaceDecl *class_interface_decl =
5661 objc_class_type->getInterface();
5662
5663 if (class_interface_decl)
5664 count = class_interface_decl->ivar_size();
5665 }
5666 }
5667 break;
5668
5669 default:
5670 break;
5671 }
5672 return count;
Greg Claytond8d4a572015-08-11 21:38:15 +00005673}
5674
Bruce Mitchener48ea9002015-09-23 00:18:24 +00005675static lldb::opaque_compiler_type_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005676GetObjCFieldAtIndex(clang::ASTContext *ast,
5677 clang::ObjCInterfaceDecl *class_interface_decl, size_t idx,
5678 std::string &name, uint64_t *bit_offset_ptr,
5679 uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) {
5680 if (class_interface_decl) {
5681 if (idx < (class_interface_decl->ivar_size())) {
5682 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
5683 ivar_end = class_interface_decl->ivar_end();
5684 uint32_t ivar_idx = 0;
5685
5686 for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end;
5687 ++ivar_pos, ++ivar_idx) {
5688 if (ivar_idx == idx) {
5689 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
5690
5691 clang::QualType ivar_qual_type(ivar_decl->getType());
5692
5693 name.assign(ivar_decl->getNameAsString());
5694
5695 if (bit_offset_ptr) {
5696 const clang::ASTRecordLayout &interface_layout =
5697 ast->getASTObjCInterfaceLayout(class_interface_decl);
5698 *bit_offset_ptr = interface_layout.getFieldOffset(ivar_idx);
5699 }
5700
5701 const bool is_bitfield = ivar_pos->isBitField();
5702
5703 if (bitfield_bit_size_ptr) {
5704 *bitfield_bit_size_ptr = 0;
5705
5706 if (is_bitfield && ast) {
5707 clang::Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
5708 llvm::APSInt bitfield_apsint;
5709 if (bitfield_bit_size_expr &&
5710 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5711 *ast)) {
5712 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5713 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005714 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005715 }
5716 if (is_bitfield_ptr)
5717 *is_bitfield_ptr = is_bitfield;
5718
5719 return ivar_qual_type.getAsOpaquePtr();
Greg Claytond8d4a572015-08-11 21:38:15 +00005720 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005721 }
Greg Claytond8d4a572015-08-11 21:38:15 +00005722 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005723 }
5724 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00005725}
5726
Kate Stoneb9c1b512016-09-06 20:57:50 +00005727CompilerType ClangASTContext::GetFieldAtIndex(lldb::opaque_compiler_type_t type,
5728 size_t idx, std::string &name,
5729 uint64_t *bit_offset_ptr,
5730 uint32_t *bitfield_bit_size_ptr,
5731 bool *is_bitfield_ptr) {
5732 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00005733 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00005734
5735 clang::QualType qual_type(GetCanonicalQualType(type));
5736 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5737 switch (type_class) {
5738 case clang::Type::Record:
5739 if (GetCompleteType(type)) {
5740 const clang::RecordType *record_type =
5741 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
5742 const clang::RecordDecl *record_decl = record_type->getDecl();
5743 uint32_t field_idx = 0;
5744 clang::RecordDecl::field_iterator field, field_end;
5745 for (field = record_decl->field_begin(),
5746 field_end = record_decl->field_end();
5747 field != field_end; ++field, ++field_idx) {
5748 if (idx == field_idx) {
5749 // Print the member type if requested
5750 // Print the member name and equal sign
5751 name.assign(field->getNameAsString());
5752
5753 // Figure out the type byte size (field_type_info.first) and
5754 // alignment (field_type_info.second) from the AST context.
5755 if (bit_offset_ptr) {
5756 const clang::ASTRecordLayout &record_layout =
5757 getASTContext()->getASTRecordLayout(record_decl);
5758 *bit_offset_ptr = record_layout.getFieldOffset(field_idx);
5759 }
5760
5761 const bool is_bitfield = field->isBitField();
5762
5763 if (bitfield_bit_size_ptr) {
5764 *bitfield_bit_size_ptr = 0;
5765
5766 if (is_bitfield) {
5767 clang::Expr *bitfield_bit_size_expr = field->getBitWidth();
5768 llvm::APSInt bitfield_apsint;
5769 if (bitfield_bit_size_expr &&
5770 bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint,
5771 *getASTContext())) {
5772 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
5773 }
5774 }
5775 }
5776 if (is_bitfield_ptr)
5777 *is_bitfield_ptr = is_bitfield;
5778
5779 return CompilerType(getASTContext(), field->getType());
5780 }
5781 }
5782 }
5783 break;
5784
Sean Callananf9c622a2016-09-30 18:44:43 +00005785 case clang::Type::ObjCObjectPointer: {
5786 const clang::ObjCObjectPointerType *objc_class_type =
5787 qual_type->getAsObjCInterfacePointerType();
5788 const clang::ObjCInterfaceType *objc_interface_type =
5789 objc_class_type->getInterfaceType();
5790 if (objc_interface_type &&
Davide Italiano52ffb532017-04-17 18:24:18 +00005791 GetCompleteType(static_cast<lldb::opaque_compiler_type_t>(
5792 const_cast<clang::ObjCInterfaceType *>(objc_interface_type)))) {
Sean Callananf9c622a2016-09-30 18:44:43 +00005793 clang::ObjCInterfaceDecl *class_interface_decl =
5794 objc_interface_type->getDecl();
5795 if (class_interface_decl) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00005796 return CompilerType(
5797 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5798 idx, name, bit_offset_ptr,
5799 bitfield_bit_size_ptr, is_bitfield_ptr));
5800 }
5801 }
5802 break;
Sean Callananf9c622a2016-09-30 18:44:43 +00005803 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005804
5805 case clang::Type::ObjCObject:
5806 case clang::Type::ObjCInterface:
5807 if (GetCompleteType(type)) {
5808 const clang::ObjCObjectType *objc_class_type =
5809 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
5810 assert(objc_class_type);
5811 if (objc_class_type) {
5812 clang::ObjCInterfaceDecl *class_interface_decl =
5813 objc_class_type->getInterface();
5814 return CompilerType(
5815 this, GetObjCFieldAtIndex(getASTContext(), class_interface_decl,
5816 idx, name, bit_offset_ptr,
5817 bitfield_bit_size_ptr, is_bitfield_ptr));
5818 }
5819 }
5820 break;
5821
5822 case clang::Type::Typedef:
5823 return CompilerType(getASTContext(),
5824 llvm::cast<clang::TypedefType>(qual_type)
5825 ->getDecl()
5826 ->getUnderlyingType())
5827 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5828 is_bitfield_ptr);
5829
5830 case clang::Type::Auto:
5831 return CompilerType(
5832 getASTContext(),
5833 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
5834 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5835 is_bitfield_ptr);
5836
5837 case clang::Type::Elaborated:
5838 return CompilerType(
5839 getASTContext(),
5840 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
5841 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5842 is_bitfield_ptr);
5843
5844 case clang::Type::Paren:
5845 return CompilerType(getASTContext(),
5846 llvm::cast<clang::ParenType>(qual_type)->desugar())
5847 .GetFieldAtIndex(idx, name, bit_offset_ptr, bitfield_bit_size_ptr,
5848 is_bitfield_ptr);
5849
5850 default:
5851 break;
5852 }
5853 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00005854}
5855
Greg Clayton99558cc42015-08-24 23:46:31 +00005856uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005857ClangASTContext::GetNumDirectBaseClasses(lldb::opaque_compiler_type_t type) {
5858 uint32_t count = 0;
5859 clang::QualType qual_type(GetCanonicalQualType(type));
5860 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5861 switch (type_class) {
5862 case clang::Type::Record:
5863 if (GetCompleteType(type)) {
5864 const clang::CXXRecordDecl *cxx_record_decl =
5865 qual_type->getAsCXXRecordDecl();
5866 if (cxx_record_decl)
5867 count = cxx_record_decl->getNumBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00005868 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005869 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00005870
Kate Stoneb9c1b512016-09-06 20:57:50 +00005871 case clang::Type::ObjCObjectPointer:
5872 count = GetPointeeType(type).GetNumDirectBaseClasses();
5873 break;
5874
5875 case clang::Type::ObjCObject:
5876 if (GetCompleteType(type)) {
5877 const clang::ObjCObjectType *objc_class_type =
5878 qual_type->getAsObjCQualifiedInterfaceType();
5879 if (objc_class_type) {
5880 clang::ObjCInterfaceDecl *class_interface_decl =
5881 objc_class_type->getInterface();
5882
5883 if (class_interface_decl && class_interface_decl->getSuperClass())
5884 count = 1;
5885 }
5886 }
5887 break;
5888 case clang::Type::ObjCInterface:
5889 if (GetCompleteType(type)) {
5890 const clang::ObjCInterfaceType *objc_interface_type =
5891 qual_type->getAs<clang::ObjCInterfaceType>();
5892 if (objc_interface_type) {
5893 clang::ObjCInterfaceDecl *class_interface_decl =
5894 objc_interface_type->getInterface();
5895
5896 if (class_interface_decl && class_interface_decl->getSuperClass())
5897 count = 1;
5898 }
5899 }
5900 break;
5901
5902 case clang::Type::Typedef:
5903 count = GetNumDirectBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5904 ->getDecl()
5905 ->getUnderlyingType()
5906 .getAsOpaquePtr());
5907 break;
5908
5909 case clang::Type::Auto:
5910 count = GetNumDirectBaseClasses(llvm::cast<clang::AutoType>(qual_type)
5911 ->getDeducedType()
5912 .getAsOpaquePtr());
5913 break;
5914
5915 case clang::Type::Elaborated:
5916 count = GetNumDirectBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
5917 ->getNamedType()
5918 .getAsOpaquePtr());
5919 break;
5920
5921 case clang::Type::Paren:
5922 return GetNumDirectBaseClasses(
5923 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5924
5925 default:
5926 break;
5927 }
5928 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00005929}
5930
5931uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00005932ClangASTContext::GetNumVirtualBaseClasses(lldb::opaque_compiler_type_t type) {
5933 uint32_t count = 0;
5934 clang::QualType qual_type(GetCanonicalQualType(type));
5935 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5936 switch (type_class) {
5937 case clang::Type::Record:
5938 if (GetCompleteType(type)) {
5939 const clang::CXXRecordDecl *cxx_record_decl =
5940 qual_type->getAsCXXRecordDecl();
5941 if (cxx_record_decl)
5942 count = cxx_record_decl->getNumVBases();
Greg Clayton99558cc42015-08-24 23:46:31 +00005943 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00005944 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00005945
Kate Stoneb9c1b512016-09-06 20:57:50 +00005946 case clang::Type::Typedef:
5947 count = GetNumVirtualBaseClasses(llvm::cast<clang::TypedefType>(qual_type)
5948 ->getDecl()
5949 ->getUnderlyingType()
5950 .getAsOpaquePtr());
5951 break;
5952
5953 case clang::Type::Auto:
5954 count = GetNumVirtualBaseClasses(llvm::cast<clang::AutoType>(qual_type)
5955 ->getDeducedType()
5956 .getAsOpaquePtr());
5957 break;
5958
5959 case clang::Type::Elaborated:
5960 count =
5961 GetNumVirtualBaseClasses(llvm::cast<clang::ElaboratedType>(qual_type)
5962 ->getNamedType()
5963 .getAsOpaquePtr());
5964 break;
5965
5966 case clang::Type::Paren:
5967 count = GetNumVirtualBaseClasses(
5968 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr());
5969 break;
5970
5971 default:
5972 break;
5973 }
5974 return count;
Greg Clayton99558cc42015-08-24 23:46:31 +00005975}
5976
Kate Stoneb9c1b512016-09-06 20:57:50 +00005977CompilerType ClangASTContext::GetDirectBaseClassAtIndex(
5978 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
5979 clang::QualType qual_type(GetCanonicalQualType(type));
5980 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5981 switch (type_class) {
5982 case clang::Type::Record:
5983 if (GetCompleteType(type)) {
5984 const clang::CXXRecordDecl *cxx_record_decl =
5985 qual_type->getAsCXXRecordDecl();
5986 if (cxx_record_decl) {
5987 uint32_t curr_idx = 0;
5988 clang::CXXRecordDecl::base_class_const_iterator base_class,
5989 base_class_end;
5990 for (base_class = cxx_record_decl->bases_begin(),
5991 base_class_end = cxx_record_decl->bases_end();
5992 base_class != base_class_end; ++base_class, ++curr_idx) {
5993 if (curr_idx == idx) {
5994 if (bit_offset_ptr) {
5995 const clang::ASTRecordLayout &record_layout =
5996 getASTContext()->getASTRecordLayout(cxx_record_decl);
5997 const clang::CXXRecordDecl *base_class_decl =
5998 llvm::cast<clang::CXXRecordDecl>(
5999 base_class->getType()
6000 ->getAs<clang::RecordType>()
6001 ->getDecl());
6002 if (base_class->isVirtual())
6003 *bit_offset_ptr =
6004 record_layout.getVBaseClassOffset(base_class_decl)
6005 .getQuantity() *
6006 8;
6007 else
6008 *bit_offset_ptr =
6009 record_layout.getBaseClassOffset(base_class_decl)
6010 .getQuantity() *
6011 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006012 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006013 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6014 }
6015 }
6016 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006017 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006018 break;
6019
6020 case clang::Type::ObjCObjectPointer:
6021 return GetPointeeType(type).GetDirectBaseClassAtIndex(idx, bit_offset_ptr);
6022
6023 case clang::Type::ObjCObject:
6024 if (idx == 0 && GetCompleteType(type)) {
6025 const clang::ObjCObjectType *objc_class_type =
6026 qual_type->getAsObjCQualifiedInterfaceType();
6027 if (objc_class_type) {
6028 clang::ObjCInterfaceDecl *class_interface_decl =
6029 objc_class_type->getInterface();
6030
6031 if (class_interface_decl) {
6032 clang::ObjCInterfaceDecl *superclass_interface_decl =
6033 class_interface_decl->getSuperClass();
6034 if (superclass_interface_decl) {
6035 if (bit_offset_ptr)
6036 *bit_offset_ptr = 0;
6037 return CompilerType(getASTContext(),
6038 getASTContext()->getObjCInterfaceType(
6039 superclass_interface_decl));
6040 }
6041 }
6042 }
6043 }
6044 break;
6045 case clang::Type::ObjCInterface:
6046 if (idx == 0 && GetCompleteType(type)) {
6047 const clang::ObjCObjectType *objc_interface_type =
6048 qual_type->getAs<clang::ObjCInterfaceType>();
6049 if (objc_interface_type) {
6050 clang::ObjCInterfaceDecl *class_interface_decl =
6051 objc_interface_type->getInterface();
6052
6053 if (class_interface_decl) {
6054 clang::ObjCInterfaceDecl *superclass_interface_decl =
6055 class_interface_decl->getSuperClass();
6056 if (superclass_interface_decl) {
6057 if (bit_offset_ptr)
6058 *bit_offset_ptr = 0;
6059 return CompilerType(getASTContext(),
6060 getASTContext()->getObjCInterfaceType(
6061 superclass_interface_decl));
6062 }
6063 }
6064 }
6065 }
6066 break;
6067
6068 case clang::Type::Typedef:
6069 return GetDirectBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6070 ->getDecl()
6071 ->getUnderlyingType()
6072 .getAsOpaquePtr(),
6073 idx, bit_offset_ptr);
6074
6075 case clang::Type::Auto:
6076 return GetDirectBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6077 ->getDeducedType()
6078 .getAsOpaquePtr(),
6079 idx, bit_offset_ptr);
6080
6081 case clang::Type::Elaborated:
6082 return GetDirectBaseClassAtIndex(
6083 llvm::cast<clang::ElaboratedType>(qual_type)
6084 ->getNamedType()
6085 .getAsOpaquePtr(),
6086 idx, bit_offset_ptr);
6087
6088 case clang::Type::Paren:
6089 return GetDirectBaseClassAtIndex(
6090 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6091 idx, bit_offset_ptr);
6092
6093 default:
6094 break;
6095 }
6096 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006097}
6098
Kate Stoneb9c1b512016-09-06 20:57:50 +00006099CompilerType ClangASTContext::GetVirtualBaseClassAtIndex(
6100 lldb::opaque_compiler_type_t type, size_t idx, uint32_t *bit_offset_ptr) {
6101 clang::QualType qual_type(GetCanonicalQualType(type));
6102 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6103 switch (type_class) {
6104 case clang::Type::Record:
6105 if (GetCompleteType(type)) {
6106 const clang::CXXRecordDecl *cxx_record_decl =
6107 qual_type->getAsCXXRecordDecl();
6108 if (cxx_record_decl) {
6109 uint32_t curr_idx = 0;
6110 clang::CXXRecordDecl::base_class_const_iterator base_class,
6111 base_class_end;
6112 for (base_class = cxx_record_decl->vbases_begin(),
6113 base_class_end = cxx_record_decl->vbases_end();
6114 base_class != base_class_end; ++base_class, ++curr_idx) {
6115 if (curr_idx == idx) {
6116 if (bit_offset_ptr) {
6117 const clang::ASTRecordLayout &record_layout =
6118 getASTContext()->getASTRecordLayout(cxx_record_decl);
6119 const clang::CXXRecordDecl *base_class_decl =
6120 llvm::cast<clang::CXXRecordDecl>(
6121 base_class->getType()
6122 ->getAs<clang::RecordType>()
6123 ->getDecl());
6124 *bit_offset_ptr =
6125 record_layout.getVBaseClassOffset(base_class_decl)
6126 .getQuantity() *
6127 8;
Greg Clayton99558cc42015-08-24 23:46:31 +00006128 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006129 return CompilerType(this, base_class->getType().getAsOpaquePtr());
6130 }
6131 }
6132 }
Greg Clayton99558cc42015-08-24 23:46:31 +00006133 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006134 break;
Greg Clayton99558cc42015-08-24 23:46:31 +00006135
Kate Stoneb9c1b512016-09-06 20:57:50 +00006136 case clang::Type::Typedef:
6137 return GetVirtualBaseClassAtIndex(llvm::cast<clang::TypedefType>(qual_type)
6138 ->getDecl()
6139 ->getUnderlyingType()
6140 .getAsOpaquePtr(),
6141 idx, bit_offset_ptr);
6142
6143 case clang::Type::Auto:
6144 return GetVirtualBaseClassAtIndex(llvm::cast<clang::AutoType>(qual_type)
6145 ->getDeducedType()
6146 .getAsOpaquePtr(),
6147 idx, bit_offset_ptr);
6148
6149 case clang::Type::Elaborated:
6150 return GetVirtualBaseClassAtIndex(
6151 llvm::cast<clang::ElaboratedType>(qual_type)
6152 ->getNamedType()
6153 .getAsOpaquePtr(),
6154 idx, bit_offset_ptr);
6155
6156 case clang::Type::Paren:
6157 return GetVirtualBaseClassAtIndex(
6158 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
6159 idx, bit_offset_ptr);
6160
6161 default:
6162 break;
6163 }
6164 return CompilerType();
Greg Clayton99558cc42015-08-24 23:46:31 +00006165}
6166
Greg Claytond8d4a572015-08-11 21:38:15 +00006167// If a pointer to a pointee type (the clang_type arg) says that it has no
6168// children, then we either need to trust it, or override it and return a
6169// different result. For example, an "int *" has one child that is an integer,
6170// but a function pointer doesn't have any children. Likewise if a Record type
6171// claims it has no children, then there really is nothing to show.
Kate Stoneb9c1b512016-09-06 20:57:50 +00006172uint32_t ClangASTContext::GetNumPointeeChildren(clang::QualType type) {
6173 if (type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00006174 return 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006175
6176 clang::QualType qual_type(type.getCanonicalType());
6177 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6178 switch (type_class) {
6179 case clang::Type::Builtin:
6180 switch (llvm::cast<clang::BuiltinType>(qual_type)->getKind()) {
6181 case clang::BuiltinType::UnknownAny:
6182 case clang::BuiltinType::Void:
6183 case clang::BuiltinType::NullPtr:
6184 case clang::BuiltinType::OCLEvent:
6185 case clang::BuiltinType::OCLImage1dRO:
6186 case clang::BuiltinType::OCLImage1dWO:
6187 case clang::BuiltinType::OCLImage1dRW:
6188 case clang::BuiltinType::OCLImage1dArrayRO:
6189 case clang::BuiltinType::OCLImage1dArrayWO:
6190 case clang::BuiltinType::OCLImage1dArrayRW:
6191 case clang::BuiltinType::OCLImage1dBufferRO:
6192 case clang::BuiltinType::OCLImage1dBufferWO:
6193 case clang::BuiltinType::OCLImage1dBufferRW:
6194 case clang::BuiltinType::OCLImage2dRO:
6195 case clang::BuiltinType::OCLImage2dWO:
6196 case clang::BuiltinType::OCLImage2dRW:
6197 case clang::BuiltinType::OCLImage2dArrayRO:
6198 case clang::BuiltinType::OCLImage2dArrayWO:
6199 case clang::BuiltinType::OCLImage2dArrayRW:
6200 case clang::BuiltinType::OCLImage3dRO:
6201 case clang::BuiltinType::OCLImage3dWO:
6202 case clang::BuiltinType::OCLImage3dRW:
6203 case clang::BuiltinType::OCLSampler:
6204 return 0;
6205 case clang::BuiltinType::Bool:
6206 case clang::BuiltinType::Char_U:
6207 case clang::BuiltinType::UChar:
6208 case clang::BuiltinType::WChar_U:
6209 case clang::BuiltinType::Char16:
6210 case clang::BuiltinType::Char32:
6211 case clang::BuiltinType::UShort:
6212 case clang::BuiltinType::UInt:
6213 case clang::BuiltinType::ULong:
6214 case clang::BuiltinType::ULongLong:
6215 case clang::BuiltinType::UInt128:
6216 case clang::BuiltinType::Char_S:
6217 case clang::BuiltinType::SChar:
6218 case clang::BuiltinType::WChar_S:
6219 case clang::BuiltinType::Short:
6220 case clang::BuiltinType::Int:
6221 case clang::BuiltinType::Long:
6222 case clang::BuiltinType::LongLong:
6223 case clang::BuiltinType::Int128:
6224 case clang::BuiltinType::Float:
6225 case clang::BuiltinType::Double:
6226 case clang::BuiltinType::LongDouble:
6227 case clang::BuiltinType::Dependent:
6228 case clang::BuiltinType::Overload:
6229 case clang::BuiltinType::ObjCId:
6230 case clang::BuiltinType::ObjCClass:
6231 case clang::BuiltinType::ObjCSel:
6232 case clang::BuiltinType::BoundMember:
6233 case clang::BuiltinType::Half:
6234 case clang::BuiltinType::ARCUnbridgedCast:
6235 case clang::BuiltinType::PseudoObject:
6236 case clang::BuiltinType::BuiltinFn:
6237 case clang::BuiltinType::OMPArraySection:
6238 return 1;
6239 default:
6240 return 0;
6241 }
6242 break;
6243
6244 case clang::Type::Complex:
6245 return 1;
6246 case clang::Type::Pointer:
6247 return 1;
6248 case clang::Type::BlockPointer:
6249 return 0; // If block pointers don't have debug info, then no children for
6250 // them
6251 case clang::Type::LValueReference:
6252 return 1;
6253 case clang::Type::RValueReference:
6254 return 1;
6255 case clang::Type::MemberPointer:
6256 return 0;
6257 case clang::Type::ConstantArray:
6258 return 0;
6259 case clang::Type::IncompleteArray:
6260 return 0;
6261 case clang::Type::VariableArray:
6262 return 0;
6263 case clang::Type::DependentSizedArray:
6264 return 0;
6265 case clang::Type::DependentSizedExtVector:
6266 return 0;
6267 case clang::Type::Vector:
6268 return 0;
6269 case clang::Type::ExtVector:
6270 return 0;
6271 case clang::Type::FunctionProto:
6272 return 0; // When we function pointers, they have no children...
6273 case clang::Type::FunctionNoProto:
6274 return 0; // When we function pointers, they have no children...
6275 case clang::Type::UnresolvedUsing:
6276 return 0;
6277 case clang::Type::Paren:
6278 return GetNumPointeeChildren(
6279 llvm::cast<clang::ParenType>(qual_type)->desugar());
6280 case clang::Type::Typedef:
6281 return GetNumPointeeChildren(llvm::cast<clang::TypedefType>(qual_type)
6282 ->getDecl()
6283 ->getUnderlyingType());
6284 case clang::Type::Auto:
6285 return GetNumPointeeChildren(
6286 llvm::cast<clang::AutoType>(qual_type)->getDeducedType());
6287 case clang::Type::Elaborated:
6288 return GetNumPointeeChildren(
6289 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType());
6290 case clang::Type::TypeOfExpr:
6291 return 0;
6292 case clang::Type::TypeOf:
6293 return 0;
6294 case clang::Type::Decltype:
6295 return 0;
6296 case clang::Type::Record:
6297 return 0;
6298 case clang::Type::Enum:
6299 return 1;
6300 case clang::Type::TemplateTypeParm:
6301 return 1;
6302 case clang::Type::SubstTemplateTypeParm:
6303 return 1;
6304 case clang::Type::TemplateSpecialization:
6305 return 1;
6306 case clang::Type::InjectedClassName:
6307 return 0;
6308 case clang::Type::DependentName:
6309 return 1;
6310 case clang::Type::DependentTemplateSpecialization:
6311 return 1;
6312 case clang::Type::ObjCObject:
6313 return 0;
6314 case clang::Type::ObjCInterface:
6315 return 0;
6316 case clang::Type::ObjCObjectPointer:
6317 return 1;
6318 default:
6319 break;
6320 }
6321 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006322}
6323
Kate Stoneb9c1b512016-09-06 20:57:50 +00006324CompilerType ClangASTContext::GetChildCompilerTypeAtIndex(
6325 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, size_t idx,
6326 bool transparent_pointers, bool omit_empty_base_classes,
6327 bool ignore_array_bounds, std::string &child_name,
6328 uint32_t &child_byte_size, int32_t &child_byte_offset,
6329 uint32_t &child_bitfield_bit_size, uint32_t &child_bitfield_bit_offset,
6330 bool &child_is_base_class, bool &child_is_deref_of_parent,
6331 ValueObject *valobj, uint64_t &language_flags) {
6332 if (!type)
Greg Claytona1e5dc82015-08-11 22:53:00 +00006333 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006334
Kate Stoneb9c1b512016-09-06 20:57:50 +00006335 clang::QualType parent_qual_type(GetCanonicalQualType(type));
6336 const clang::Type::TypeClass parent_type_class =
6337 parent_qual_type->getTypeClass();
6338 child_bitfield_bit_size = 0;
6339 child_bitfield_bit_offset = 0;
6340 child_is_base_class = false;
6341 language_flags = 0;
6342
6343 const bool idx_is_valid = idx < GetNumChildren(type, omit_empty_base_classes);
6344 uint32_t bit_offset;
6345 switch (parent_type_class) {
6346 case clang::Type::Builtin:
6347 if (idx_is_valid) {
6348 switch (llvm::cast<clang::BuiltinType>(parent_qual_type)->getKind()) {
6349 case clang::BuiltinType::ObjCId:
6350 case clang::BuiltinType::ObjCClass:
6351 child_name = "isa";
6352 child_byte_size =
6353 getASTContext()->getTypeSize(getASTContext()->ObjCBuiltinClassTy) /
6354 CHAR_BIT;
6355 return CompilerType(getASTContext(),
6356 getASTContext()->ObjCBuiltinClassTy);
6357
6358 default:
6359 break;
6360 }
6361 }
6362 break;
6363
6364 case clang::Type::Record:
6365 if (idx_is_valid && GetCompleteType(type)) {
6366 const clang::RecordType *record_type =
6367 llvm::cast<clang::RecordType>(parent_qual_type.getTypePtr());
6368 const clang::RecordDecl *record_decl = record_type->getDecl();
6369 assert(record_decl);
6370 const clang::ASTRecordLayout &record_layout =
6371 getASTContext()->getASTRecordLayout(record_decl);
6372 uint32_t child_idx = 0;
6373
6374 const clang::CXXRecordDecl *cxx_record_decl =
6375 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6376 if (cxx_record_decl) {
6377 // We might have base classes to print out first
6378 clang::CXXRecordDecl::base_class_const_iterator base_class,
6379 base_class_end;
6380 for (base_class = cxx_record_decl->bases_begin(),
6381 base_class_end = cxx_record_decl->bases_end();
6382 base_class != base_class_end; ++base_class) {
6383 const clang::CXXRecordDecl *base_class_decl = nullptr;
6384
6385 // Skip empty base classes
6386 if (omit_empty_base_classes) {
6387 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6388 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6389 if (ClangASTContext::RecordHasFields(base_class_decl) == false)
6390 continue;
6391 }
6392
6393 if (idx == child_idx) {
6394 if (base_class_decl == nullptr)
6395 base_class_decl = llvm::cast<clang::CXXRecordDecl>(
6396 base_class->getType()->getAs<clang::RecordType>()->getDecl());
6397
6398 if (base_class->isVirtual()) {
6399 bool handled = false;
6400 if (valobj) {
6401 Error err;
6402 AddressType addr_type = eAddressTypeInvalid;
6403 lldb::addr_t vtable_ptr_addr =
6404 valobj->GetCPPVTableAddress(addr_type);
6405
6406 if (vtable_ptr_addr != LLDB_INVALID_ADDRESS &&
6407 addr_type == eAddressTypeLoad) {
6408
6409 ExecutionContext exe_ctx(valobj->GetExecutionContextRef());
6410 Process *process = exe_ctx.GetProcessPtr();
6411 if (process) {
6412 clang::VTableContextBase *vtable_ctx =
6413 getASTContext()->getVTableContext();
6414 if (vtable_ctx) {
6415 if (vtable_ctx->isMicrosoft()) {
6416 clang::MicrosoftVTableContext *msoft_vtable_ctx =
6417 static_cast<clang::MicrosoftVTableContext *>(
6418 vtable_ctx);
6419
6420 if (vtable_ptr_addr) {
6421 const lldb::addr_t vbtable_ptr_addr =
6422 vtable_ptr_addr +
6423 record_layout.getVBPtrOffset().getQuantity();
6424
6425 const lldb::addr_t vbtable_ptr =
6426 process->ReadPointerFromMemory(vbtable_ptr_addr,
6427 err);
6428 if (vbtable_ptr != LLDB_INVALID_ADDRESS) {
6429 // Get the index into the virtual base table. The
6430 // index is the index in uint32_t from vbtable_ptr
6431 const unsigned vbtable_index =
6432 msoft_vtable_ctx->getVBTableIndex(
6433 cxx_record_decl, base_class_decl);
6434 const lldb::addr_t base_offset_addr =
6435 vbtable_ptr + vbtable_index * 4;
6436 const uint32_t base_offset =
6437 process->ReadUnsignedIntegerFromMemory(
6438 base_offset_addr, 4, UINT32_MAX, err);
6439 if (base_offset != UINT32_MAX) {
6440 handled = true;
6441 bit_offset = base_offset * 8;
6442 }
6443 }
6444 }
6445 } else {
6446 clang::ItaniumVTableContext *itanium_vtable_ctx =
6447 static_cast<clang::ItaniumVTableContext *>(
6448 vtable_ctx);
6449 if (vtable_ptr_addr) {
6450 const lldb::addr_t vtable_ptr =
6451 process->ReadPointerFromMemory(vtable_ptr_addr,
6452 err);
6453 if (vtable_ptr != LLDB_INVALID_ADDRESS) {
6454 clang::CharUnits base_offset_offset =
6455 itanium_vtable_ctx->getVirtualBaseOffsetOffset(
6456 cxx_record_decl, base_class_decl);
6457 const lldb::addr_t base_offset_addr =
6458 vtable_ptr + base_offset_offset.getQuantity();
6459 const uint32_t base_offset_size =
6460 process->GetAddressByteSize();
6461 const uint64_t base_offset =
6462 process->ReadUnsignedIntegerFromMemory(
6463 base_offset_addr, base_offset_size,
6464 UINT32_MAX, err);
6465 if (base_offset < UINT32_MAX) {
6466 handled = true;
6467 bit_offset = base_offset * 8;
6468 }
6469 }
6470 }
6471 }
6472 }
6473 }
6474 }
6475 }
6476 if (!handled)
6477 bit_offset = record_layout.getVBaseClassOffset(base_class_decl)
6478 .getQuantity() *
6479 8;
6480 } else
6481 bit_offset = record_layout.getBaseClassOffset(base_class_decl)
6482 .getQuantity() *
6483 8;
6484
6485 // Base classes should be a multiple of 8 bits in size
6486 child_byte_offset = bit_offset / 8;
6487 CompilerType base_class_clang_type(getASTContext(),
6488 base_class->getType());
6489 child_name = base_class_clang_type.GetTypeName().AsCString("");
6490 uint64_t base_class_clang_type_bit_size =
6491 base_class_clang_type.GetBitSize(
6492 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6493
6494 // Base classes bit sizes should be a multiple of 8 bits in size
6495 assert(base_class_clang_type_bit_size % 8 == 0);
6496 child_byte_size = base_class_clang_type_bit_size / 8;
6497 child_is_base_class = true;
6498 return base_class_clang_type;
6499 }
6500 // We don't increment the child index in the for loop since we might
6501 // be skipping empty base classes
6502 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006503 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006504 }
6505 // Make sure index is in range...
6506 uint32_t field_idx = 0;
6507 clang::RecordDecl::field_iterator field, field_end;
6508 for (field = record_decl->field_begin(),
6509 field_end = record_decl->field_end();
6510 field != field_end; ++field, ++field_idx, ++child_idx) {
6511 if (idx == child_idx) {
6512 // Print the member type if requested
6513 // Print the member name and equal sign
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006514 child_name.assign(field->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006515
6516 // Figure out the type byte size (field_type_info.first) and
6517 // alignment (field_type_info.second) from the AST context.
6518 CompilerType field_clang_type(getASTContext(), field->getType());
6519 assert(field_idx < record_layout.getFieldCount());
6520 child_byte_size = field_clang_type.GetByteSize(
6521 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6522 const uint32_t child_bit_size = child_byte_size * 8;
6523
6524 // Figure out the field offset within the current struct/union/class
6525 // type
6526 bit_offset = record_layout.getFieldOffset(field_idx);
6527 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
6528 child_bitfield_bit_size)) {
6529 child_bitfield_bit_offset = bit_offset % child_bit_size;
6530 const uint32_t child_bit_offset =
6531 bit_offset - child_bitfield_bit_offset;
6532 child_byte_offset = child_bit_offset / 8;
6533 } else {
6534 child_byte_offset = bit_offset / 8;
6535 }
6536
6537 return field_clang_type;
6538 }
6539 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006540 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006541 break;
6542
6543 case clang::Type::ObjCObject:
6544 case clang::Type::ObjCInterface:
6545 if (idx_is_valid && GetCompleteType(type)) {
6546 const clang::ObjCObjectType *objc_class_type =
6547 llvm::dyn_cast<clang::ObjCObjectType>(parent_qual_type.getTypePtr());
6548 assert(objc_class_type);
6549 if (objc_class_type) {
6550 uint32_t child_idx = 0;
6551 clang::ObjCInterfaceDecl *class_interface_decl =
6552 objc_class_type->getInterface();
6553
6554 if (class_interface_decl) {
6555
6556 const clang::ASTRecordLayout &interface_layout =
6557 getASTContext()->getASTObjCInterfaceLayout(class_interface_decl);
6558 clang::ObjCInterfaceDecl *superclass_interface_decl =
6559 class_interface_decl->getSuperClass();
6560 if (superclass_interface_decl) {
6561 if (omit_empty_base_classes) {
6562 CompilerType base_class_clang_type(
6563 getASTContext(), getASTContext()->getObjCInterfaceType(
6564 superclass_interface_decl));
6565 if (base_class_clang_type.GetNumChildren(
6566 omit_empty_base_classes) > 0) {
6567 if (idx == 0) {
6568 clang::QualType ivar_qual_type(
6569 getASTContext()->getObjCInterfaceType(
6570 superclass_interface_decl));
6571
6572 child_name.assign(
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006573 superclass_interface_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006574
6575 clang::TypeInfo ivar_type_info =
6576 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6577
6578 child_byte_size = ivar_type_info.Width / 8;
6579 child_byte_offset = 0;
6580 child_is_base_class = true;
6581
6582 return CompilerType(getASTContext(), ivar_qual_type);
6583 }
6584
6585 ++child_idx;
6586 }
6587 } else
6588 ++child_idx;
6589 }
6590
6591 const uint32_t superclass_idx = child_idx;
6592
6593 if (idx < (child_idx + class_interface_decl->ivar_size())) {
6594 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
6595 ivar_end = class_interface_decl->ivar_end();
6596
6597 for (ivar_pos = class_interface_decl->ivar_begin();
6598 ivar_pos != ivar_end; ++ivar_pos) {
6599 if (child_idx == idx) {
6600 clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
6601
6602 clang::QualType ivar_qual_type(ivar_decl->getType());
6603
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00006604 child_name.assign(ivar_decl->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00006605
6606 clang::TypeInfo ivar_type_info =
6607 getASTContext()->getTypeInfo(ivar_qual_type.getTypePtr());
6608
6609 child_byte_size = ivar_type_info.Width / 8;
6610
6611 // Figure out the field offset within the current
6612 // struct/union/class type
6613 // For ObjC objects, we can't trust the bit offset we get from
6614 // the Clang AST, since
6615 // that doesn't account for the space taken up by unbacked
6616 // properties, or from
6617 // the changing size of base classes that are newer than this
6618 // class.
6619 // So if we have a process around that we can ask about this
6620 // object, do so.
6621 child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
6622 Process *process = nullptr;
6623 if (exe_ctx)
6624 process = exe_ctx->GetProcessPtr();
6625 if (process) {
6626 ObjCLanguageRuntime *objc_runtime =
6627 process->GetObjCLanguageRuntime();
6628 if (objc_runtime != nullptr) {
6629 CompilerType parent_ast_type(getASTContext(),
6630 parent_qual_type);
6631 child_byte_offset = objc_runtime->GetByteOffsetForIvar(
6632 parent_ast_type, ivar_decl->getNameAsString().c_str());
6633 }
6634 }
6635
6636 // Setting this to UINT32_MAX to make sure we don't compute it
6637 // twice...
6638 bit_offset = UINT32_MAX;
6639
6640 if (child_byte_offset ==
6641 static_cast<int32_t>(LLDB_INVALID_IVAR_OFFSET)) {
6642 bit_offset = interface_layout.getFieldOffset(child_idx -
6643 superclass_idx);
6644 child_byte_offset = bit_offset / 8;
6645 }
6646
6647 // Note, the ObjC Ivar Byte offset is just that, it doesn't
6648 // account for the bit offset
6649 // of a bitfield within its containing object. So regardless of
6650 // where we get the byte
6651 // offset from, we still need to get the bit offset for
6652 // bitfields from the layout.
6653
6654 if (ClangASTContext::FieldIsBitfield(getASTContext(), ivar_decl,
6655 child_bitfield_bit_size)) {
6656 if (bit_offset == UINT32_MAX)
6657 bit_offset = interface_layout.getFieldOffset(
6658 child_idx - superclass_idx);
6659
6660 child_bitfield_bit_offset = bit_offset % 8;
6661 }
6662 return CompilerType(getASTContext(), ivar_qual_type);
6663 }
6664 ++child_idx;
6665 }
6666 }
6667 }
6668 }
6669 }
6670 break;
6671
6672 case clang::Type::ObjCObjectPointer:
6673 if (idx_is_valid) {
6674 CompilerType pointee_clang_type(GetPointeeType(type));
6675
6676 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6677 child_is_deref_of_parent = false;
6678 bool tmp_child_is_deref_of_parent = false;
6679 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6680 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6681 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6682 child_bitfield_bit_size, child_bitfield_bit_offset,
6683 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6684 language_flags);
6685 } else {
6686 child_is_deref_of_parent = true;
6687 const char *parent_name =
6688 valobj ? valobj->GetName().GetCString() : NULL;
6689 if (parent_name) {
6690 child_name.assign(1, '*');
6691 child_name += parent_name;
6692 }
6693
6694 // We have a pointer to an simple type
6695 if (idx == 0 && pointee_clang_type.GetCompleteType()) {
6696 child_byte_size = pointee_clang_type.GetByteSize(
6697 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6698 child_byte_offset = 0;
6699 return pointee_clang_type;
6700 }
6701 }
6702 }
6703 break;
6704
6705 case clang::Type::Vector:
6706 case clang::Type::ExtVector:
6707 if (idx_is_valid) {
6708 const clang::VectorType *array =
6709 llvm::cast<clang::VectorType>(parent_qual_type.getTypePtr());
6710 if (array) {
6711 CompilerType element_type(getASTContext(), array->getElementType());
6712 if (element_type.GetCompleteType()) {
6713 char element_name[64];
6714 ::snprintf(element_name, sizeof(element_name), "[%" PRIu64 "]",
6715 static_cast<uint64_t>(idx));
6716 child_name.assign(element_name);
6717 child_byte_size = element_type.GetByteSize(
6718 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6719 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6720 return element_type;
6721 }
6722 }
6723 }
6724 break;
6725
6726 case clang::Type::ConstantArray:
6727 case clang::Type::IncompleteArray:
6728 if (ignore_array_bounds || idx_is_valid) {
6729 const clang::ArrayType *array = GetQualType(type)->getAsArrayTypeUnsafe();
6730 if (array) {
6731 CompilerType element_type(getASTContext(), array->getElementType());
6732 if (element_type.GetCompleteType()) {
Zachary Turner827d5d72016-12-16 04:27:00 +00006733 child_name = llvm::formatv("[{0}]", idx);
Kate Stoneb9c1b512016-09-06 20:57:50 +00006734 child_byte_size = element_type.GetByteSize(
6735 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6736 child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
6737 return element_type;
6738 }
6739 }
6740 }
6741 break;
6742
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006743 case clang::Type::Pointer: {
6744 CompilerType pointee_clang_type(GetPointeeType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00006745
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006746 // Don't dereference "void *" pointers
6747 if (pointee_clang_type.IsVoidType())
6748 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00006749
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006750 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6751 child_is_deref_of_parent = false;
6752 bool tmp_child_is_deref_of_parent = false;
6753 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6754 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6755 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6756 child_bitfield_bit_size, child_bitfield_bit_offset,
6757 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6758 language_flags);
6759 } else {
6760 child_is_deref_of_parent = true;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006761
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006762 const char *parent_name =
6763 valobj ? valobj->GetName().GetCString() : NULL;
6764 if (parent_name) {
6765 child_name.assign(1, '*');
6766 child_name += parent_name;
6767 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006768
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006769 // We have a pointer to an simple type
6770 if (idx == 0) {
6771 child_byte_size = pointee_clang_type.GetByteSize(
6772 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6773 child_byte_offset = 0;
6774 return pointee_clang_type;
Kate Stoneb9c1b512016-09-06 20:57:50 +00006775 }
6776 }
6777 break;
Tamas Berghammer1c62e032017-01-07 16:39:07 +00006778 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006779
6780 case clang::Type::LValueReference:
6781 case clang::Type::RValueReference:
6782 if (idx_is_valid) {
6783 const clang::ReferenceType *reference_type =
6784 llvm::cast<clang::ReferenceType>(parent_qual_type.getTypePtr());
6785 CompilerType pointee_clang_type(getASTContext(),
6786 reference_type->getPointeeType());
6787 if (transparent_pointers && pointee_clang_type.IsAggregateType()) {
6788 child_is_deref_of_parent = false;
6789 bool tmp_child_is_deref_of_parent = false;
6790 return pointee_clang_type.GetChildCompilerTypeAtIndex(
6791 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6792 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6793 child_bitfield_bit_size, child_bitfield_bit_offset,
6794 child_is_base_class, tmp_child_is_deref_of_parent, valobj,
6795 language_flags);
6796 } else {
6797 const char *parent_name =
6798 valobj ? valobj->GetName().GetCString() : NULL;
6799 if (parent_name) {
6800 child_name.assign(1, '&');
6801 child_name += parent_name;
6802 }
6803
6804 // We have a pointer to an simple type
6805 if (idx == 0) {
6806 child_byte_size = pointee_clang_type.GetByteSize(
6807 exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL);
6808 child_byte_offset = 0;
6809 return pointee_clang_type;
6810 }
6811 }
6812 }
6813 break;
6814
6815 case clang::Type::Typedef: {
6816 CompilerType typedefed_clang_type(
6817 getASTContext(), llvm::cast<clang::TypedefType>(parent_qual_type)
6818 ->getDecl()
6819 ->getUnderlyingType());
6820 return typedefed_clang_type.GetChildCompilerTypeAtIndex(
6821 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6822 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6823 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6824 child_is_deref_of_parent, valobj, language_flags);
6825 } break;
6826
6827 case clang::Type::Auto: {
6828 CompilerType elaborated_clang_type(
6829 getASTContext(),
6830 llvm::cast<clang::AutoType>(parent_qual_type)->getDeducedType());
6831 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6832 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6833 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6834 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6835 child_is_deref_of_parent, valobj, language_flags);
6836 }
6837
6838 case clang::Type::Elaborated: {
6839 CompilerType elaborated_clang_type(
6840 getASTContext(),
6841 llvm::cast<clang::ElaboratedType>(parent_qual_type)->getNamedType());
6842 return elaborated_clang_type.GetChildCompilerTypeAtIndex(
6843 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6844 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6845 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6846 child_is_deref_of_parent, valobj, language_flags);
6847 }
6848
6849 case clang::Type::Paren: {
6850 CompilerType paren_clang_type(
6851 getASTContext(),
6852 llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
6853 return paren_clang_type.GetChildCompilerTypeAtIndex(
6854 exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
6855 ignore_array_bounds, child_name, child_byte_size, child_byte_offset,
6856 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
6857 child_is_deref_of_parent, valobj, language_flags);
6858 }
6859
6860 default:
6861 break;
6862 }
6863 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00006864}
6865
Kate Stoneb9c1b512016-09-06 20:57:50 +00006866static uint32_t GetIndexForRecordBase(const clang::RecordDecl *record_decl,
6867 const clang::CXXBaseSpecifier *base_spec,
6868 bool omit_empty_base_classes) {
6869 uint32_t child_idx = 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00006870
Kate Stoneb9c1b512016-09-06 20:57:50 +00006871 const clang::CXXRecordDecl *cxx_record_decl =
6872 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6873
6874 // const char *super_name = record_decl->getNameAsCString();
6875 // const char *base_name =
6876 // base_spec->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString();
6877 // printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
6878 //
6879 if (cxx_record_decl) {
6880 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
6881 for (base_class = cxx_record_decl->bases_begin(),
6882 base_class_end = cxx_record_decl->bases_end();
6883 base_class != base_class_end; ++base_class) {
6884 if (omit_empty_base_classes) {
6885 if (BaseSpecifierIsEmpty(base_class))
6886 continue;
6887 }
6888
6889 // printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n",
6890 // super_name, base_name,
6891 // child_idx,
6892 // base_class->getType()->getAs<clang::RecordType>()->getDecl()->getNameAsCString());
6893 //
6894 //
6895 if (base_class == base_spec)
6896 return child_idx;
6897 ++child_idx;
Greg Claytond8d4a572015-08-11 21:38:15 +00006898 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006899 }
6900
6901 return UINT32_MAX;
6902}
6903
6904static uint32_t GetIndexForRecordChild(const clang::RecordDecl *record_decl,
6905 clang::NamedDecl *canonical_decl,
6906 bool omit_empty_base_classes) {
6907 uint32_t child_idx = ClangASTContext::GetNumBaseClasses(
6908 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl),
6909 omit_empty_base_classes);
6910
6911 clang::RecordDecl::field_iterator field, field_end;
6912 for (field = record_decl->field_begin(), field_end = record_decl->field_end();
6913 field != field_end; ++field, ++child_idx) {
6914 if (field->getCanonicalDecl() == canonical_decl)
6915 return child_idx;
6916 }
6917
6918 return UINT32_MAX;
Greg Claytond8d4a572015-08-11 21:38:15 +00006919}
6920
6921// Look for a child member (doesn't include base classes, but it does include
6922// their members) in the type hierarchy. Returns an index path into "clang_type"
6923// on how to reach the appropriate member.
6924//
6925// class A
6926// {
6927// public:
6928// int m_a;
6929// int m_b;
6930// };
6931//
6932// class B
6933// {
6934// };
6935//
6936// class C :
6937// public B,
6938// public A
6939// {
6940// };
6941//
6942// If we have a clang type that describes "class C", and we wanted to looked
6943// "m_b" in it:
6944//
Kate Stoneb9c1b512016-09-06 20:57:50 +00006945// With omit_empty_base_classes == false we would get an integer array back
6946// with:
Greg Claytond8d4a572015-08-11 21:38:15 +00006947// { 1, 1 }
6948// The first index 1 is the child index for "class A" within class C
6949// The second index 1 is the child index for "m_b" within class A
6950//
6951// With omit_empty_base_classes == true we would get an integer array back with:
6952// { 0, 1 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00006953// The first index 0 is the child index for "class A" within class C (since
6954// class B doesn't have any members it doesn't count)
Greg Claytond8d4a572015-08-11 21:38:15 +00006955// The second index 1 is the child index for "m_b" within class A
6956
Kate Stoneb9c1b512016-09-06 20:57:50 +00006957size_t ClangASTContext::GetIndexOfChildMemberWithName(
6958 lldb::opaque_compiler_type_t type, const char *name,
6959 bool omit_empty_base_classes, std::vector<uint32_t> &child_indexes) {
6960 if (type && name && name[0]) {
6961 clang::QualType qual_type(GetCanonicalQualType(type));
6962 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6963 switch (type_class) {
6964 case clang::Type::Record:
6965 if (GetCompleteType(type)) {
6966 const clang::RecordType *record_type =
6967 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
6968 const clang::RecordDecl *record_decl = record_type->getDecl();
Enrico Granata36f51e42015-12-18 22:41:25 +00006969
Kate Stoneb9c1b512016-09-06 20:57:50 +00006970 assert(record_decl);
6971 uint32_t child_idx = 0;
6972
6973 const clang::CXXRecordDecl *cxx_record_decl =
6974 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
6975
6976 // Try and find a field that matches NAME
6977 clang::RecordDecl::field_iterator field, field_end;
6978 llvm::StringRef name_sref(name);
6979 for (field = record_decl->field_begin(),
6980 field_end = record_decl->field_end();
6981 field != field_end; ++field, ++child_idx) {
6982 llvm::StringRef field_name = field->getName();
6983 if (field_name.empty()) {
6984 CompilerType field_type(getASTContext(), field->getType());
6985 child_indexes.push_back(child_idx);
6986 if (field_type.GetIndexOfChildMemberWithName(
6987 name, omit_empty_base_classes, child_indexes))
6988 return child_indexes.size();
6989 child_indexes.pop_back();
6990
6991 } else if (field_name.equals(name_sref)) {
6992 // We have to add on the number of base classes to this index!
6993 child_indexes.push_back(
6994 child_idx + ClangASTContext::GetNumBaseClasses(
6995 cxx_record_decl, omit_empty_base_classes));
6996 return child_indexes.size();
6997 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006998 }
Greg Claytond8d4a572015-08-11 21:38:15 +00006999
Kate Stoneb9c1b512016-09-06 20:57:50 +00007000 if (cxx_record_decl) {
7001 const clang::RecordDecl *parent_record_decl = cxx_record_decl;
7002
7003 // printf ("parent = %s\n", parent_record_decl->getNameAsCString());
7004
7005 // const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
7006 // Didn't find things easily, lets let clang do its thang...
7007 clang::IdentifierInfo &ident_ref =
7008 getASTContext()->Idents.get(name_sref);
7009 clang::DeclarationName decl_name(&ident_ref);
7010
7011 clang::CXXBasePaths paths;
7012 if (cxx_record_decl->lookupInBases(
7013 [decl_name](const clang::CXXBaseSpecifier *specifier,
7014 clang::CXXBasePath &path) {
7015 return clang::CXXRecordDecl::FindOrdinaryMember(
7016 specifier, path, decl_name);
7017 },
7018 paths)) {
7019 clang::CXXBasePaths::const_paths_iterator path,
7020 path_end = paths.end();
7021 for (path = paths.begin(); path != path_end; ++path) {
7022 const size_t num_path_elements = path->size();
7023 for (size_t e = 0; e < num_path_elements; ++e) {
7024 clang::CXXBasePathElement elem = (*path)[e];
7025
7026 child_idx = GetIndexForRecordBase(parent_record_decl, elem.Base,
7027 omit_empty_base_classes);
7028 if (child_idx == UINT32_MAX) {
7029 child_indexes.clear();
7030 return 0;
7031 } else {
7032 child_indexes.push_back(child_idx);
7033 parent_record_decl = llvm::cast<clang::RecordDecl>(
7034 elem.Base->getType()
7035 ->getAs<clang::RecordType>()
7036 ->getDecl());
7037 }
7038 }
7039 for (clang::NamedDecl *path_decl : path->Decls) {
7040 child_idx = GetIndexForRecordChild(
7041 parent_record_decl, path_decl, omit_empty_base_classes);
7042 if (child_idx == UINT32_MAX) {
7043 child_indexes.clear();
7044 return 0;
7045 } else {
7046 child_indexes.push_back(child_idx);
7047 }
7048 }
7049 }
7050 return child_indexes.size();
7051 }
7052 }
7053 }
7054 break;
7055
7056 case clang::Type::ObjCObject:
7057 case clang::Type::ObjCInterface:
7058 if (GetCompleteType(type)) {
7059 llvm::StringRef name_sref(name);
7060 const clang::ObjCObjectType *objc_class_type =
7061 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7062 assert(objc_class_type);
7063 if (objc_class_type) {
7064 uint32_t child_idx = 0;
7065 clang::ObjCInterfaceDecl *class_interface_decl =
7066 objc_class_type->getInterface();
7067
7068 if (class_interface_decl) {
7069 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7070 ivar_end = class_interface_decl->ivar_end();
7071 clang::ObjCInterfaceDecl *superclass_interface_decl =
7072 class_interface_decl->getSuperClass();
7073
7074 for (ivar_pos = class_interface_decl->ivar_begin();
7075 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7076 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7077
7078 if (ivar_decl->getName().equals(name_sref)) {
7079 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7080 (omit_empty_base_classes &&
7081 ObjCDeclHasIVars(superclass_interface_decl, true)))
7082 ++child_idx;
7083
7084 child_indexes.push_back(child_idx);
7085 return child_indexes.size();
7086 }
7087 }
7088
7089 if (superclass_interface_decl) {
7090 // The super class index is always zero for ObjC classes,
7091 // so we push it onto the child indexes in case we find
7092 // an ivar in our superclass...
7093 child_indexes.push_back(0);
7094
7095 CompilerType superclass_clang_type(
7096 getASTContext(), getASTContext()->getObjCInterfaceType(
7097 superclass_interface_decl));
7098 if (superclass_clang_type.GetIndexOfChildMemberWithName(
7099 name, omit_empty_base_classes, child_indexes)) {
7100 // We did find an ivar in a superclass so just
7101 // return the results!
7102 return child_indexes.size();
7103 }
7104
7105 // We didn't find an ivar matching "name" in our
7106 // superclass, pop the superclass zero index that
7107 // we pushed on above.
7108 child_indexes.pop_back();
7109 }
7110 }
7111 }
7112 }
7113 break;
7114
7115 case clang::Type::ObjCObjectPointer: {
7116 CompilerType objc_object_clang_type(
7117 getASTContext(),
7118 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7119 ->getPointeeType());
7120 return objc_object_clang_type.GetIndexOfChildMemberWithName(
7121 name, omit_empty_base_classes, child_indexes);
7122 } break;
7123
7124 case clang::Type::ConstantArray: {
7125 // const clang::ConstantArrayType *array =
7126 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7127 // const uint64_t element_count =
7128 // array->getSize().getLimitedValue();
7129 //
7130 // if (idx < element_count)
7131 // {
7132 // std::pair<uint64_t, unsigned> field_type_info =
7133 // ast->getTypeInfo(array->getElementType());
7134 //
7135 // char element_name[32];
7136 // ::snprintf (element_name, sizeof (element_name),
7137 // "%s[%u]", parent_name ? parent_name : "", idx);
7138 //
7139 // child_name.assign(element_name);
7140 // assert(field_type_info.first % 8 == 0);
7141 // child_byte_size = field_type_info.first / 8;
7142 // child_byte_offset = idx * child_byte_size;
7143 // return array->getElementType().getAsOpaquePtr();
7144 // }
7145 } break;
7146
7147 // case clang::Type::MemberPointerType:
7148 // {
7149 // MemberPointerType *mem_ptr_type =
7150 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7151 // clang::QualType pointee_type =
7152 // mem_ptr_type->getPointeeType();
7153 //
7154 // if (ClangASTContext::IsAggregateType
7155 // (pointee_type.getAsOpaquePtr()))
7156 // {
7157 // return GetIndexOfChildWithName (ast,
7158 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7159 // name);
7160 // }
7161 // }
7162 // break;
7163 //
7164 case clang::Type::LValueReference:
7165 case clang::Type::RValueReference: {
7166 const clang::ReferenceType *reference_type =
7167 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7168 clang::QualType pointee_type(reference_type->getPointeeType());
7169 CompilerType pointee_clang_type(getASTContext(), pointee_type);
7170
7171 if (pointee_clang_type.IsAggregateType()) {
7172 return pointee_clang_type.GetIndexOfChildMemberWithName(
7173 name, omit_empty_base_classes, child_indexes);
7174 }
7175 } break;
7176
7177 case clang::Type::Pointer: {
7178 CompilerType pointee_clang_type(GetPointeeType(type));
7179
7180 if (pointee_clang_type.IsAggregateType()) {
7181 return pointee_clang_type.GetIndexOfChildMemberWithName(
7182 name, omit_empty_base_classes, child_indexes);
7183 }
7184 } break;
7185
7186 case clang::Type::Typedef:
7187 return CompilerType(getASTContext(),
7188 llvm::cast<clang::TypedefType>(qual_type)
7189 ->getDecl()
7190 ->getUnderlyingType())
7191 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7192 child_indexes);
7193
7194 case clang::Type::Auto:
7195 return CompilerType(
7196 getASTContext(),
7197 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7198 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7199 child_indexes);
7200
7201 case clang::Type::Elaborated:
7202 return CompilerType(
7203 getASTContext(),
7204 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7205 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7206 child_indexes);
7207
7208 case clang::Type::Paren:
7209 return CompilerType(getASTContext(),
7210 llvm::cast<clang::ParenType>(qual_type)->desugar())
7211 .GetIndexOfChildMemberWithName(name, omit_empty_base_classes,
7212 child_indexes);
7213
7214 default:
7215 break;
7216 }
7217 }
7218 return 0;
7219}
Greg Claytond8d4a572015-08-11 21:38:15 +00007220
7221// Get the index of the child of "clang_type" whose name matches. This function
7222// doesn't descend into the children, but only looks one level deep and name
7223// matches can include base class names.
7224
7225uint32_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007226ClangASTContext::GetIndexOfChildWithName(lldb::opaque_compiler_type_t type,
7227 const char *name,
7228 bool omit_empty_base_classes) {
7229 if (type && name && name[0]) {
7230 clang::QualType qual_type(GetCanonicalQualType(type));
Enrico Granata36f51e42015-12-18 22:41:25 +00007231
Kate Stoneb9c1b512016-09-06 20:57:50 +00007232 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7233
7234 switch (type_class) {
7235 case clang::Type::Record:
7236 if (GetCompleteType(type)) {
7237 const clang::RecordType *record_type =
7238 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
7239 const clang::RecordDecl *record_decl = record_type->getDecl();
7240
7241 assert(record_decl);
7242 uint32_t child_idx = 0;
7243
7244 const clang::CXXRecordDecl *cxx_record_decl =
7245 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
7246
7247 if (cxx_record_decl) {
7248 clang::CXXRecordDecl::base_class_const_iterator base_class,
7249 base_class_end;
7250 for (base_class = cxx_record_decl->bases_begin(),
7251 base_class_end = cxx_record_decl->bases_end();
7252 base_class != base_class_end; ++base_class) {
7253 // Skip empty base classes
7254 clang::CXXRecordDecl *base_class_decl =
7255 llvm::cast<clang::CXXRecordDecl>(
7256 base_class->getType()
7257 ->getAs<clang::RecordType>()
7258 ->getDecl());
7259 if (omit_empty_base_classes &&
7260 ClangASTContext::RecordHasFields(base_class_decl) == false)
7261 continue;
7262
7263 CompilerType base_class_clang_type(getASTContext(),
7264 base_class->getType());
7265 std::string base_class_type_name(
7266 base_class_clang_type.GetTypeName().AsCString(""));
7267 if (base_class_type_name.compare(name) == 0)
7268 return child_idx;
7269 ++child_idx;
7270 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007271 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007272
Kate Stoneb9c1b512016-09-06 20:57:50 +00007273 // Try and find a field that matches NAME
7274 clang::RecordDecl::field_iterator field, field_end;
7275 llvm::StringRef name_sref(name);
7276 for (field = record_decl->field_begin(),
7277 field_end = record_decl->field_end();
7278 field != field_end; ++field, ++child_idx) {
7279 if (field->getName().equals(name_sref))
7280 return child_idx;
7281 }
7282 }
7283 break;
7284
7285 case clang::Type::ObjCObject:
7286 case clang::Type::ObjCInterface:
7287 if (GetCompleteType(type)) {
7288 llvm::StringRef name_sref(name);
7289 const clang::ObjCObjectType *objc_class_type =
7290 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
7291 assert(objc_class_type);
7292 if (objc_class_type) {
7293 uint32_t child_idx = 0;
7294 clang::ObjCInterfaceDecl *class_interface_decl =
7295 objc_class_type->getInterface();
7296
7297 if (class_interface_decl) {
7298 clang::ObjCInterfaceDecl::ivar_iterator ivar_pos,
7299 ivar_end = class_interface_decl->ivar_end();
7300 clang::ObjCInterfaceDecl *superclass_interface_decl =
7301 class_interface_decl->getSuperClass();
7302
7303 for (ivar_pos = class_interface_decl->ivar_begin();
7304 ivar_pos != ivar_end; ++ivar_pos, ++child_idx) {
7305 const clang::ObjCIvarDecl *ivar_decl = *ivar_pos;
7306
7307 if (ivar_decl->getName().equals(name_sref)) {
7308 if ((!omit_empty_base_classes && superclass_interface_decl) ||
7309 (omit_empty_base_classes &&
7310 ObjCDeclHasIVars(superclass_interface_decl, true)))
7311 ++child_idx;
7312
7313 return child_idx;
7314 }
7315 }
7316
7317 if (superclass_interface_decl) {
7318 if (superclass_interface_decl->getName().equals(name_sref))
7319 return 0;
7320 }
7321 }
7322 }
7323 }
7324 break;
7325
7326 case clang::Type::ObjCObjectPointer: {
7327 CompilerType pointee_clang_type(
7328 getASTContext(),
7329 llvm::cast<clang::ObjCObjectPointerType>(qual_type.getTypePtr())
7330 ->getPointeeType());
7331 return pointee_clang_type.GetIndexOfChildWithName(
7332 name, omit_empty_base_classes);
7333 } break;
7334
7335 case clang::Type::ConstantArray: {
7336 // const clang::ConstantArrayType *array =
7337 // llvm::cast<clang::ConstantArrayType>(parent_qual_type.getTypePtr());
7338 // const uint64_t element_count =
7339 // array->getSize().getLimitedValue();
7340 //
7341 // if (idx < element_count)
7342 // {
7343 // std::pair<uint64_t, unsigned> field_type_info =
7344 // ast->getTypeInfo(array->getElementType());
7345 //
7346 // char element_name[32];
7347 // ::snprintf (element_name, sizeof (element_name),
7348 // "%s[%u]", parent_name ? parent_name : "", idx);
7349 //
7350 // child_name.assign(element_name);
7351 // assert(field_type_info.first % 8 == 0);
7352 // child_byte_size = field_type_info.first / 8;
7353 // child_byte_offset = idx * child_byte_size;
7354 // return array->getElementType().getAsOpaquePtr();
7355 // }
7356 } break;
7357
7358 // case clang::Type::MemberPointerType:
7359 // {
7360 // MemberPointerType *mem_ptr_type =
7361 // llvm::cast<MemberPointerType>(qual_type.getTypePtr());
7362 // clang::QualType pointee_type =
7363 // mem_ptr_type->getPointeeType();
7364 //
7365 // if (ClangASTContext::IsAggregateType
7366 // (pointee_type.getAsOpaquePtr()))
7367 // {
7368 // return GetIndexOfChildWithName (ast,
7369 // mem_ptr_type->getPointeeType().getAsOpaquePtr(),
7370 // name);
7371 // }
7372 // }
7373 // break;
7374 //
7375 case clang::Type::LValueReference:
7376 case clang::Type::RValueReference: {
7377 const clang::ReferenceType *reference_type =
7378 llvm::cast<clang::ReferenceType>(qual_type.getTypePtr());
7379 CompilerType pointee_type(getASTContext(),
7380 reference_type->getPointeeType());
7381
7382 if (pointee_type.IsAggregateType()) {
7383 return pointee_type.GetIndexOfChildWithName(name,
7384 omit_empty_base_classes);
7385 }
7386 } break;
7387
7388 case clang::Type::Pointer: {
7389 const clang::PointerType *pointer_type =
7390 llvm::cast<clang::PointerType>(qual_type.getTypePtr());
7391 CompilerType pointee_type(getASTContext(),
7392 pointer_type->getPointeeType());
7393
7394 if (pointee_type.IsAggregateType()) {
7395 return pointee_type.GetIndexOfChildWithName(name,
7396 omit_empty_base_classes);
7397 } else {
7398 // if (parent_name)
7399 // {
7400 // child_name.assign(1, '*');
7401 // child_name += parent_name;
7402 // }
7403 //
7404 // // We have a pointer to an simple type
7405 // if (idx == 0)
7406 // {
7407 // std::pair<uint64_t, unsigned> clang_type_info
7408 // = ast->getTypeInfo(pointee_type);
7409 // assert(clang_type_info.first % 8 == 0);
7410 // child_byte_size = clang_type_info.first / 8;
7411 // child_byte_offset = 0;
7412 // return pointee_type.getAsOpaquePtr();
7413 // }
7414 }
7415 } break;
7416
7417 case clang::Type::Auto:
7418 return CompilerType(
7419 getASTContext(),
7420 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
7421 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7422
7423 case clang::Type::Elaborated:
7424 return CompilerType(
7425 getASTContext(),
7426 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
7427 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7428
7429 case clang::Type::Paren:
7430 return CompilerType(getASTContext(),
7431 llvm::cast<clang::ParenType>(qual_type)->desugar())
7432 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7433
7434 case clang::Type::Typedef:
7435 return CompilerType(getASTContext(),
7436 llvm::cast<clang::TypedefType>(qual_type)
7437 ->getDecl()
7438 ->getUnderlyingType())
7439 .GetIndexOfChildWithName(name, omit_empty_base_classes);
7440
7441 default:
7442 break;
7443 }
7444 }
7445 return UINT32_MAX;
7446}
Greg Claytond8d4a572015-08-11 21:38:15 +00007447
7448size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00007449ClangASTContext::GetNumTemplateArguments(lldb::opaque_compiler_type_t type) {
7450 if (!type)
Greg Claytond8d4a572015-08-11 21:38:15 +00007451 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007452
Kate Stoneb9c1b512016-09-06 20:57:50 +00007453 clang::QualType qual_type(GetCanonicalQualType(type));
7454 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7455 switch (type_class) {
7456 case clang::Type::Record:
7457 if (GetCompleteType(type)) {
7458 const clang::CXXRecordDecl *cxx_record_decl =
7459 qual_type->getAsCXXRecordDecl();
7460 if (cxx_record_decl) {
7461 const clang::ClassTemplateSpecializationDecl *template_decl =
7462 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7463 cxx_record_decl);
7464 if (template_decl)
7465 return template_decl->getTemplateArgs().size();
7466 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007467 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007468 break;
7469
7470 case clang::Type::Typedef:
7471 return (CompilerType(getASTContext(),
7472 llvm::cast<clang::TypedefType>(qual_type)
7473 ->getDecl()
7474 ->getUnderlyingType()))
7475 .GetNumTemplateArguments();
7476
7477 case clang::Type::Auto:
7478 return (CompilerType(
7479 getASTContext(),
7480 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7481 .GetNumTemplateArguments();
7482
7483 case clang::Type::Elaborated:
7484 return (CompilerType(
7485 getASTContext(),
7486 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7487 .GetNumTemplateArguments();
7488
7489 case clang::Type::Paren:
7490 return (CompilerType(getASTContext(),
7491 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7492 .GetNumTemplateArguments();
7493
7494 default:
7495 break;
7496 }
7497
7498 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00007499}
7500
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007501CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00007502ClangASTContext::GetTemplateArgument(lldb::opaque_compiler_type_t type,
7503 size_t arg_idx,
7504 lldb::TemplateArgumentKind &kind) {
7505 if (!type)
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007506 return CompilerType();
Kate Stoneb9c1b512016-09-06 20:57:50 +00007507
7508 clang::QualType qual_type(GetCanonicalQualType(type));
7509 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
7510 switch (type_class) {
7511 case clang::Type::Record:
7512 if (GetCompleteType(type)) {
7513 const clang::CXXRecordDecl *cxx_record_decl =
7514 qual_type->getAsCXXRecordDecl();
7515 if (cxx_record_decl) {
7516 const clang::ClassTemplateSpecializationDecl *template_decl =
7517 llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(
7518 cxx_record_decl);
7519 if (template_decl &&
7520 arg_idx < template_decl->getTemplateArgs().size()) {
7521 const clang::TemplateArgument &template_arg =
7522 template_decl->getTemplateArgs()[arg_idx];
7523 switch (template_arg.getKind()) {
7524 case clang::TemplateArgument::Null:
7525 kind = eTemplateArgumentKindNull;
7526 return CompilerType();
7527
7528 case clang::TemplateArgument::Type:
7529 kind = eTemplateArgumentKindType;
7530 return CompilerType(getASTContext(), template_arg.getAsType());
7531
7532 case clang::TemplateArgument::Declaration:
7533 kind = eTemplateArgumentKindDeclaration;
7534 return CompilerType();
7535
7536 case clang::TemplateArgument::Integral:
7537 kind = eTemplateArgumentKindIntegral;
7538 return CompilerType(getASTContext(),
7539 template_arg.getIntegralType());
7540
7541 case clang::TemplateArgument::Template:
7542 kind = eTemplateArgumentKindTemplate;
7543 return CompilerType();
7544
7545 case clang::TemplateArgument::TemplateExpansion:
7546 kind = eTemplateArgumentKindTemplateExpansion;
7547 return CompilerType();
7548
7549 case clang::TemplateArgument::Expression:
7550 kind = eTemplateArgumentKindExpression;
7551 return CompilerType();
7552
7553 case clang::TemplateArgument::Pack:
7554 kind = eTemplateArgumentKindPack;
7555 return CompilerType();
7556
7557 default:
David Blaikiea322f362017-01-06 00:38:06 +00007558 llvm_unreachable("Unhandled clang::TemplateArgument::ArgKind");
Kate Stoneb9c1b512016-09-06 20:57:50 +00007559 }
7560 }
7561 }
7562 }
7563 break;
7564
7565 case clang::Type::Typedef:
7566 return (CompilerType(getASTContext(),
7567 llvm::cast<clang::TypedefType>(qual_type)
7568 ->getDecl()
7569 ->getUnderlyingType()))
7570 .GetTemplateArgument(arg_idx, kind);
7571
7572 case clang::Type::Auto:
7573 return (CompilerType(
7574 getASTContext(),
7575 llvm::cast<clang::AutoType>(qual_type)->getDeducedType()))
7576 .GetTemplateArgument(arg_idx, kind);
7577
7578 case clang::Type::Elaborated:
7579 return (CompilerType(
7580 getASTContext(),
7581 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType()))
7582 .GetTemplateArgument(arg_idx, kind);
7583
7584 case clang::Type::Paren:
7585 return (CompilerType(getASTContext(),
7586 llvm::cast<clang::ParenType>(qual_type)->desugar()))
7587 .GetTemplateArgument(arg_idx, kind);
7588
7589 default:
7590 break;
7591 }
7592 kind = eTemplateArgumentKindNull;
7593 return CompilerType();
Enrico Granatac6bf2e22015-09-23 01:39:46 +00007594}
7595
Kate Stoneb9c1b512016-09-06 20:57:50 +00007596CompilerType ClangASTContext::GetTypeForFormatters(void *type) {
7597 if (type)
7598 return ClangUtil::RemoveFastQualifiers(CompilerType(this, type));
7599 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00007600}
7601
Kate Stoneb9c1b512016-09-06 20:57:50 +00007602clang::EnumDecl *ClangASTContext::GetAsEnumDecl(const CompilerType &type) {
7603 const clang::EnumType *enutype =
7604 llvm::dyn_cast<clang::EnumType>(ClangUtil::GetCanonicalQualType(type));
7605 if (enutype)
7606 return enutype->getDecl();
7607 return NULL;
7608}
7609
7610clang::RecordDecl *ClangASTContext::GetAsRecordDecl(const CompilerType &type) {
7611 const clang::RecordType *record_type =
7612 llvm::dyn_cast<clang::RecordType>(ClangUtil::GetCanonicalQualType(type));
7613 if (record_type)
7614 return record_type->getDecl();
7615 return nullptr;
7616}
7617
7618clang::TagDecl *ClangASTContext::GetAsTagDecl(const CompilerType &type) {
7619 clang::QualType qual_type = ClangUtil::GetCanonicalQualType(type);
7620 if (qual_type.isNull())
Greg Claytond8d4a572015-08-11 21:38:15 +00007621 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007622 else
7623 return qual_type->getAsTagDecl();
Greg Claytone6b36cd2015-12-08 01:02:08 +00007624}
7625
Greg Claytond8d4a572015-08-11 21:38:15 +00007626clang::CXXRecordDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007627ClangASTContext::GetAsCXXRecordDecl(lldb::opaque_compiler_type_t type) {
7628 return GetCanonicalQualType(type)->getAsCXXRecordDecl();
Greg Claytond8d4a572015-08-11 21:38:15 +00007629}
7630
7631clang::ObjCInterfaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00007632ClangASTContext::GetAsObjCInterfaceDecl(const CompilerType &type) {
7633 const clang::ObjCObjectType *objc_class_type =
7634 llvm::dyn_cast<clang::ObjCObjectType>(
7635 ClangUtil::GetCanonicalQualType(type));
7636 if (objc_class_type)
7637 return objc_class_type->getInterface();
7638 return nullptr;
7639}
7640
7641clang::FieldDecl *ClangASTContext::AddFieldToRecordType(
7642 const CompilerType &type, const char *name,
7643 const CompilerType &field_clang_type, AccessType access,
7644 uint32_t bitfield_bit_size) {
7645 if (!type.IsValid() || !field_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00007646 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00007647 ClangASTContext *ast =
7648 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
7649 if (!ast)
7650 return nullptr;
7651 clang::ASTContext *clang_ast = ast->getASTContext();
7652
7653 clang::FieldDecl *field = nullptr;
7654
7655 clang::Expr *bit_width = nullptr;
7656 if (bitfield_bit_size != 0) {
7657 llvm::APInt bitfield_bit_size_apint(
7658 clang_ast->getTypeSize(clang_ast->IntTy), bitfield_bit_size);
7659 bit_width = new (*clang_ast)
7660 clang::IntegerLiteral(*clang_ast, bitfield_bit_size_apint,
7661 clang_ast->IntTy, clang::SourceLocation());
7662 }
7663
7664 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7665 if (record_decl) {
7666 field = clang::FieldDecl::Create(
7667 *clang_ast, record_decl, clang::SourceLocation(),
7668 clang::SourceLocation(),
7669 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7670 ClangUtil::GetQualType(field_clang_type), // Field type
7671 nullptr, // TInfo *
7672 bit_width, // BitWidth
7673 false, // Mutable
7674 clang::ICIS_NoInit); // HasInit
7675
7676 if (!name) {
7677 // Determine whether this field corresponds to an anonymous
7678 // struct or union.
7679 if (const clang::TagType *TagT =
7680 field->getType()->getAs<clang::TagType>()) {
7681 if (clang::RecordDecl *Rec =
7682 llvm::dyn_cast<clang::RecordDecl>(TagT->getDecl()))
7683 if (!Rec->getDeclName()) {
7684 Rec->setAnonymousStructOrUnion(true);
7685 field->setImplicit();
7686 }
7687 }
7688 }
7689
7690 if (field) {
7691 field->setAccess(
7692 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7693
7694 record_decl->addDecl(field);
7695
7696#ifdef LLDB_CONFIGURATION_DEBUG
7697 VerifyDecl(field);
7698#endif
7699 }
7700 } else {
7701 clang::ObjCInterfaceDecl *class_interface_decl =
7702 ast->GetAsObjCInterfaceDecl(type);
7703
7704 if (class_interface_decl) {
7705 const bool is_synthesized = false;
7706
7707 field_clang_type.GetCompleteType();
7708
7709 field = clang::ObjCIvarDecl::Create(
7710 *clang_ast, class_interface_decl, clang::SourceLocation(),
7711 clang::SourceLocation(),
7712 name ? &clang_ast->Idents.get(name) : nullptr, // Identifier
7713 ClangUtil::GetQualType(field_clang_type), // Field type
7714 nullptr, // TypeSourceInfo *
7715 ConvertAccessTypeToObjCIvarAccessControl(access), bit_width,
7716 is_synthesized);
7717
7718 if (field) {
7719 class_interface_decl->addDecl(field);
7720
7721#ifdef LLDB_CONFIGURATION_DEBUG
7722 VerifyDecl(field);
7723#endif
7724 }
7725 }
7726 }
7727 return field;
Greg Claytond8d4a572015-08-11 21:38:15 +00007728}
7729
Kate Stoneb9c1b512016-09-06 20:57:50 +00007730void ClangASTContext::BuildIndirectFields(const CompilerType &type) {
7731 if (!type)
7732 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007733
Kate Stoneb9c1b512016-09-06 20:57:50 +00007734 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7735 if (!ast)
7736 return;
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007737
Kate Stoneb9c1b512016-09-06 20:57:50 +00007738 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007739
Kate Stoneb9c1b512016-09-06 20:57:50 +00007740 if (!record_decl)
7741 return;
7742
7743 typedef llvm::SmallVector<clang::IndirectFieldDecl *, 1> IndirectFieldVector;
7744
7745 IndirectFieldVector indirect_fields;
7746 clang::RecordDecl::field_iterator field_pos;
7747 clang::RecordDecl::field_iterator field_end_pos = record_decl->field_end();
7748 clang::RecordDecl::field_iterator last_field_pos = field_end_pos;
7749 for (field_pos = record_decl->field_begin(); field_pos != field_end_pos;
7750 last_field_pos = field_pos++) {
7751 if (field_pos->isAnonymousStructOrUnion()) {
7752 clang::QualType field_qual_type = field_pos->getType();
7753
7754 const clang::RecordType *field_record_type =
7755 field_qual_type->getAs<clang::RecordType>();
7756
7757 if (!field_record_type)
7758 continue;
7759
7760 clang::RecordDecl *field_record_decl = field_record_type->getDecl();
7761
7762 if (!field_record_decl)
7763 continue;
7764
7765 for (clang::RecordDecl::decl_iterator
7766 di = field_record_decl->decls_begin(),
7767 de = field_record_decl->decls_end();
7768 di != de; ++di) {
7769 if (clang::FieldDecl *nested_field_decl =
7770 llvm::dyn_cast<clang::FieldDecl>(*di)) {
7771 clang::NamedDecl **chain =
7772 new (*ast->getASTContext()) clang::NamedDecl *[2];
7773 chain[0] = *field_pos;
7774 chain[1] = nested_field_decl;
7775 clang::IndirectFieldDecl *indirect_field =
7776 clang::IndirectFieldDecl::Create(
7777 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7778 nested_field_decl->getIdentifier(),
7779 nested_field_decl->getType(), {chain, 2});
7780
7781 indirect_field->setImplicit();
7782
7783 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7784 field_pos->getAccess(), nested_field_decl->getAccess()));
7785
7786 indirect_fields.push_back(indirect_field);
7787 } else if (clang::IndirectFieldDecl *nested_indirect_field_decl =
7788 llvm::dyn_cast<clang::IndirectFieldDecl>(*di)) {
7789 size_t nested_chain_size =
7790 nested_indirect_field_decl->getChainingSize();
7791 clang::NamedDecl **chain = new (*ast->getASTContext())
7792 clang::NamedDecl *[nested_chain_size + 1];
7793 chain[0] = *field_pos;
7794
7795 int chain_index = 1;
7796 for (clang::IndirectFieldDecl::chain_iterator
7797 nci = nested_indirect_field_decl->chain_begin(),
7798 nce = nested_indirect_field_decl->chain_end();
7799 nci < nce; ++nci) {
7800 chain[chain_index] = *nci;
7801 chain_index++;
7802 }
7803
7804 clang::IndirectFieldDecl *indirect_field =
7805 clang::IndirectFieldDecl::Create(
7806 *ast->getASTContext(), record_decl, clang::SourceLocation(),
7807 nested_indirect_field_decl->getIdentifier(),
7808 nested_indirect_field_decl->getType(),
7809 {chain, nested_chain_size + 1});
7810
7811 indirect_field->setImplicit();
7812
7813 indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(
7814 field_pos->getAccess(), nested_indirect_field_decl->getAccess()));
7815
7816 indirect_fields.push_back(indirect_field);
Greg Claytond8d4a572015-08-11 21:38:15 +00007817 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007818 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007819 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007820 }
7821
7822 // Check the last field to see if it has an incomplete array type as its
7823 // last member and if it does, the tell the record decl about it
7824 if (last_field_pos != field_end_pos) {
7825 if (last_field_pos->getType()->isIncompleteArrayType())
7826 record_decl->hasFlexibleArrayMember();
7827 }
7828
7829 for (IndirectFieldVector::iterator ifi = indirect_fields.begin(),
7830 ife = indirect_fields.end();
7831 ifi < ife; ++ifi) {
7832 record_decl->addDecl(*ifi);
7833 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007834}
7835
Kate Stoneb9c1b512016-09-06 20:57:50 +00007836void ClangASTContext::SetIsPacked(const CompilerType &type) {
7837 if (type) {
7838 ClangASTContext *ast =
7839 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7840 if (ast) {
7841 clang::RecordDecl *record_decl = GetAsRecordDecl(type);
7842
7843 if (!record_decl)
Greg Claytonf73034f2015-09-08 18:15:05 +00007844 return;
7845
Kate Stoneb9c1b512016-09-06 20:57:50 +00007846 record_decl->addAttr(
7847 clang::PackedAttr::CreateImplicit(*ast->getASTContext()));
Greg Claytond8d4a572015-08-11 21:38:15 +00007848 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007849 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007850}
7851
Kate Stoneb9c1b512016-09-06 20:57:50 +00007852clang::VarDecl *ClangASTContext::AddVariableToRecordType(
7853 const CompilerType &type, const char *name, const CompilerType &var_type,
7854 AccessType access) {
7855 clang::VarDecl *var_decl = nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00007856
Kate Stoneb9c1b512016-09-06 20:57:50 +00007857 if (!type.IsValid() || !var_type.IsValid())
7858 return nullptr;
7859 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
7860 if (!ast)
7861 return nullptr;
7862
7863 clang::RecordDecl *record_decl = ast->GetAsRecordDecl(type);
7864 if (record_decl) {
7865 var_decl = clang::VarDecl::Create(
7866 *ast->getASTContext(), // ASTContext &
7867 record_decl, // DeclContext *
7868 clang::SourceLocation(), // clang::SourceLocation StartLoc
7869 clang::SourceLocation(), // clang::SourceLocation IdLoc
7870 name ? &ast->getASTContext()->Idents.get(name)
7871 : nullptr, // clang::IdentifierInfo *
7872 ClangUtil::GetQualType(var_type), // Variable clang::QualType
7873 nullptr, // TypeSourceInfo *
7874 clang::SC_Static); // StorageClass
7875 if (var_decl) {
7876 var_decl->setAccess(
7877 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access));
7878 record_decl->addDecl(var_decl);
7879
Greg Claytond8d4a572015-08-11 21:38:15 +00007880#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00007881 VerifyDecl(var_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00007882#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00007883 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007884 }
7885 return var_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00007886}
7887
Kate Stoneb9c1b512016-09-06 20:57:50 +00007888clang::CXXMethodDecl *ClangASTContext::AddMethodToCXXRecordType(
7889 lldb::opaque_compiler_type_t type, const char *name,
7890 const CompilerType &method_clang_type, lldb::AccessType access,
7891 bool is_virtual, bool is_static, bool is_inline, bool is_explicit,
7892 bool is_attr_used, bool is_artificial) {
7893 if (!type || !method_clang_type.IsValid() || name == nullptr ||
7894 name[0] == '\0')
7895 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00007896
Kate Stoneb9c1b512016-09-06 20:57:50 +00007897 clang::QualType record_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007898
Kate Stoneb9c1b512016-09-06 20:57:50 +00007899 clang::CXXRecordDecl *cxx_record_decl =
7900 record_qual_type->getAsCXXRecordDecl();
Zachary Turnerd133f6a2016-03-28 22:53:41 +00007901
Kate Stoneb9c1b512016-09-06 20:57:50 +00007902 if (cxx_record_decl == nullptr)
7903 return nullptr;
7904
7905 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
7906
7907 clang::CXXMethodDecl *cxx_method_decl = nullptr;
7908
7909 clang::DeclarationName decl_name(&getASTContext()->Idents.get(name));
7910
7911 const clang::FunctionType *function_type =
7912 llvm::dyn_cast<clang::FunctionType>(method_qual_type.getTypePtr());
7913
7914 if (function_type == nullptr)
7915 return nullptr;
7916
7917 const clang::FunctionProtoType *method_function_prototype(
7918 llvm::dyn_cast<clang::FunctionProtoType>(function_type));
7919
7920 if (!method_function_prototype)
7921 return nullptr;
7922
7923 unsigned int num_params = method_function_prototype->getNumParams();
7924
7925 clang::CXXDestructorDecl *cxx_dtor_decl(nullptr);
7926 clang::CXXConstructorDecl *cxx_ctor_decl(nullptr);
7927
7928 if (is_artificial)
7929 return nullptr; // skip everything artificial
7930
7931 if (name[0] == '~') {
7932 cxx_dtor_decl = clang::CXXDestructorDecl::Create(
7933 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7934 clang::DeclarationNameInfo(
7935 getASTContext()->DeclarationNames.getCXXDestructorName(
7936 getASTContext()->getCanonicalType(record_qual_type)),
7937 clang::SourceLocation()),
7938 method_qual_type, nullptr, is_inline, is_artificial);
7939 cxx_method_decl = cxx_dtor_decl;
7940 } else if (decl_name == cxx_record_decl->getDeclName()) {
7941 cxx_ctor_decl = clang::CXXConstructorDecl::Create(
7942 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7943 clang::DeclarationNameInfo(
7944 getASTContext()->DeclarationNames.getCXXConstructorName(
7945 getASTContext()->getCanonicalType(record_qual_type)),
7946 clang::SourceLocation()),
7947 method_qual_type,
7948 nullptr, // TypeSourceInfo *
7949 is_explicit, is_inline, is_artificial, false /*is_constexpr*/);
7950 cxx_method_decl = cxx_ctor_decl;
7951 } else {
7952 clang::StorageClass SC = is_static ? clang::SC_Static : clang::SC_None;
7953 clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
7954
7955 if (IsOperator(name, op_kind)) {
7956 if (op_kind != clang::NUM_OVERLOADED_OPERATORS) {
7957 // Check the number of operator parameters. Sometimes we have
7958 // seen bad DWARF that doesn't correctly describe operators and
7959 // if we try to create a method and add it to the class, clang
7960 // will assert and crash, so we need to make sure things are
7961 // acceptable.
7962 const bool is_method = true;
7963 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount(
7964 is_method, op_kind, num_params))
7965 return nullptr;
7966 cxx_method_decl = clang::CXXMethodDecl::Create(
7967 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7968 clang::DeclarationNameInfo(
7969 getASTContext()->DeclarationNames.getCXXOperatorName(op_kind),
7970 clang::SourceLocation()),
7971 method_qual_type,
7972 nullptr, // TypeSourceInfo *
7973 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
7974 } else if (num_params == 0) {
7975 // Conversion operators don't take params...
7976 cxx_method_decl = clang::CXXConversionDecl::Create(
7977 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7978 clang::DeclarationNameInfo(
7979 getASTContext()->DeclarationNames.getCXXConversionFunctionName(
7980 getASTContext()->getCanonicalType(
7981 function_type->getReturnType())),
7982 clang::SourceLocation()),
7983 method_qual_type,
7984 nullptr, // TypeSourceInfo *
7985 is_inline, is_explicit, false /*is_constexpr*/,
7986 clang::SourceLocation());
7987 }
Greg Claytond8d4a572015-08-11 21:38:15 +00007988 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007989
7990 if (cxx_method_decl == nullptr) {
7991 cxx_method_decl = clang::CXXMethodDecl::Create(
7992 *getASTContext(), cxx_record_decl, clang::SourceLocation(),
7993 clang::DeclarationNameInfo(decl_name, clang::SourceLocation()),
7994 method_qual_type,
7995 nullptr, // TypeSourceInfo *
7996 SC, is_inline, false /*is_constexpr*/, clang::SourceLocation());
Greg Claytond8d4a572015-08-11 21:38:15 +00007997 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00007998 }
7999
8000 clang::AccessSpecifier access_specifier =
8001 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access);
8002
8003 cxx_method_decl->setAccess(access_specifier);
8004 cxx_method_decl->setVirtualAsWritten(is_virtual);
8005
8006 if (is_attr_used)
8007 cxx_method_decl->addAttr(clang::UsedAttr::CreateImplicit(*getASTContext()));
8008
8009 // Populate the method decl with parameter decls
8010
8011 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8012
8013 for (unsigned param_index = 0; param_index < num_params; ++param_index) {
8014 params.push_back(clang::ParmVarDecl::Create(
8015 *getASTContext(), cxx_method_decl, clang::SourceLocation(),
8016 clang::SourceLocation(),
8017 nullptr, // anonymous
8018 method_function_prototype->getParamType(param_index), nullptr,
8019 clang::SC_None, nullptr));
8020 }
8021
8022 cxx_method_decl->setParams(llvm::ArrayRef<clang::ParmVarDecl *>(params));
8023
8024 cxx_record_decl->addDecl(cxx_method_decl);
8025
8026 // Sometimes the debug info will mention a constructor (default/copy/move),
8027 // destructor, or assignment operator (copy/move) but there won't be any
8028 // version of this in the code. So we check if the function was artificially
8029 // generated and if it is trivial and this lets the compiler/backend know
8030 // that it can inline the IR for these when it needs to and we can avoid a
8031 // "missing function" error when running expressions.
8032
8033 if (is_artificial) {
8034 if (cxx_ctor_decl && ((cxx_ctor_decl->isDefaultConstructor() &&
8035 cxx_record_decl->hasTrivialDefaultConstructor()) ||
8036 (cxx_ctor_decl->isCopyConstructor() &&
8037 cxx_record_decl->hasTrivialCopyConstructor()) ||
8038 (cxx_ctor_decl->isMoveConstructor() &&
8039 cxx_record_decl->hasTrivialMoveConstructor()))) {
8040 cxx_ctor_decl->setDefaulted();
8041 cxx_ctor_decl->setTrivial(true);
8042 } else if (cxx_dtor_decl) {
8043 if (cxx_record_decl->hasTrivialDestructor()) {
8044 cxx_dtor_decl->setDefaulted();
8045 cxx_dtor_decl->setTrivial(true);
8046 }
8047 } else if ((cxx_method_decl->isCopyAssignmentOperator() &&
8048 cxx_record_decl->hasTrivialCopyAssignment()) ||
8049 (cxx_method_decl->isMoveAssignmentOperator() &&
8050 cxx_record_decl->hasTrivialMoveAssignment())) {
8051 cxx_method_decl->setDefaulted();
8052 cxx_method_decl->setTrivial(true);
Greg Claytond8d4a572015-08-11 21:38:15 +00008053 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008054 }
8055
Greg Claytond8d4a572015-08-11 21:38:15 +00008056#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008057 VerifyDecl(cxx_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008058#endif
Greg Claytond8d4a572015-08-11 21:38:15 +00008059
Kate Stoneb9c1b512016-09-06 20:57:50 +00008060 // printf ("decl->isPolymorphic() = %i\n",
8061 // cxx_record_decl->isPolymorphic());
8062 // printf ("decl->isAggregate() = %i\n",
8063 // cxx_record_decl->isAggregate());
8064 // printf ("decl->isPOD() = %i\n",
8065 // cxx_record_decl->isPOD());
8066 // printf ("decl->isEmpty() = %i\n",
8067 // cxx_record_decl->isEmpty());
8068 // printf ("decl->isAbstract() = %i\n",
8069 // cxx_record_decl->isAbstract());
8070 // printf ("decl->hasTrivialConstructor() = %i\n",
8071 // cxx_record_decl->hasTrivialConstructor());
8072 // printf ("decl->hasTrivialCopyConstructor() = %i\n",
8073 // cxx_record_decl->hasTrivialCopyConstructor());
8074 // printf ("decl->hasTrivialCopyAssignment() = %i\n",
8075 // cxx_record_decl->hasTrivialCopyAssignment());
8076 // printf ("decl->hasTrivialDestructor() = %i\n",
8077 // cxx_record_decl->hasTrivialDestructor());
8078 return cxx_method_decl;
8079}
Greg Claytond8d4a572015-08-11 21:38:15 +00008080
8081#pragma mark C++ Base Classes
8082
8083clang::CXXBaseSpecifier *
Kate Stoneb9c1b512016-09-06 20:57:50 +00008084ClangASTContext::CreateBaseClassSpecifier(lldb::opaque_compiler_type_t type,
8085 AccessType access, bool is_virtual,
8086 bool base_of_class) {
8087 if (type)
8088 return new clang::CXXBaseSpecifier(
8089 clang::SourceRange(), is_virtual, base_of_class,
8090 ClangASTContext::ConvertAccessTypeToAccessSpecifier(access),
8091 getASTContext()->getTrivialTypeSourceInfo(GetQualType(type)),
8092 clang::SourceLocation());
8093 return nullptr;
8094}
8095
8096void ClangASTContext::DeleteBaseClassSpecifiers(
8097 clang::CXXBaseSpecifier **base_classes, unsigned num_base_classes) {
8098 for (unsigned i = 0; i < num_base_classes; ++i) {
8099 delete base_classes[i];
8100 base_classes[i] = nullptr;
8101 }
8102}
8103
8104bool ClangASTContext::SetBaseClassesForClassType(
8105 lldb::opaque_compiler_type_t type,
8106 clang::CXXBaseSpecifier const *const *base_classes,
8107 unsigned num_base_classes) {
8108 if (type) {
8109 clang::CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl(type);
8110 if (cxx_record_decl) {
8111 cxx_record_decl->setBases(base_classes, num_base_classes);
8112 return true;
8113 }
8114 }
8115 return false;
8116}
8117
8118bool ClangASTContext::SetObjCSuperClass(
8119 const CompilerType &type, const CompilerType &superclass_clang_type) {
8120 ClangASTContext *ast =
8121 llvm::dyn_cast_or_null<ClangASTContext>(type.GetTypeSystem());
8122 if (!ast)
8123 return false;
8124 clang::ASTContext *clang_ast = ast->getASTContext();
8125
8126 if (type && superclass_clang_type.IsValid() &&
8127 superclass_clang_type.GetTypeSystem() == type.GetTypeSystem()) {
8128 clang::ObjCInterfaceDecl *class_interface_decl =
8129 GetAsObjCInterfaceDecl(type);
8130 clang::ObjCInterfaceDecl *super_interface_decl =
8131 GetAsObjCInterfaceDecl(superclass_clang_type);
8132 if (class_interface_decl && super_interface_decl) {
8133 class_interface_decl->setSuperClass(clang_ast->getTrivialTypeSourceInfo(
8134 clang_ast->getObjCInterfaceType(super_interface_decl)));
8135 return true;
8136 }
8137 }
8138 return false;
8139}
8140
8141bool ClangASTContext::AddObjCClassProperty(
8142 const CompilerType &type, const char *property_name,
8143 const CompilerType &property_clang_type, clang::ObjCIvarDecl *ivar_decl,
8144 const char *property_setter_name, const char *property_getter_name,
8145 uint32_t property_attributes, ClangASTMetadata *metadata) {
8146 if (!type || !property_clang_type.IsValid() || property_name == nullptr ||
8147 property_name[0] == '\0')
8148 return false;
8149 ClangASTContext *ast = llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8150 if (!ast)
8151 return false;
8152 clang::ASTContext *clang_ast = ast->getASTContext();
8153
8154 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8155
8156 if (class_interface_decl) {
8157 CompilerType property_clang_type_to_access;
8158
8159 if (property_clang_type.IsValid())
8160 property_clang_type_to_access = property_clang_type;
8161 else if (ivar_decl)
8162 property_clang_type_to_access =
8163 CompilerType(clang_ast, ivar_decl->getType());
8164
8165 if (class_interface_decl && property_clang_type_to_access.IsValid()) {
8166 clang::TypeSourceInfo *prop_type_source;
8167 if (ivar_decl)
8168 prop_type_source =
8169 clang_ast->getTrivialTypeSourceInfo(ivar_decl->getType());
8170 else
8171 prop_type_source = clang_ast->getTrivialTypeSourceInfo(
8172 ClangUtil::GetQualType(property_clang_type));
8173
8174 clang::ObjCPropertyDecl *property_decl = clang::ObjCPropertyDecl::Create(
8175 *clang_ast, class_interface_decl,
8176 clang::SourceLocation(), // Source Location
8177 &clang_ast->Idents.get(property_name),
8178 clang::SourceLocation(), // Source Location for AT
8179 clang::SourceLocation(), // Source location for (
8180 ivar_decl ? ivar_decl->getType()
8181 : ClangUtil::GetQualType(property_clang_type),
8182 prop_type_source);
8183
8184 if (property_decl) {
8185 if (metadata)
8186 ClangASTContext::SetMetadata(clang_ast, property_decl, *metadata);
8187
8188 class_interface_decl->addDecl(property_decl);
8189
8190 clang::Selector setter_sel, getter_sel;
8191
8192 if (property_setter_name != nullptr) {
8193 std::string property_setter_no_colon(
8194 property_setter_name, strlen(property_setter_name) - 1);
8195 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008196 &clang_ast->Idents.get(property_setter_no_colon);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008197 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8198 } else if (!(property_attributes & DW_APPLE_PROPERTY_readonly)) {
8199 std::string setter_sel_string("set");
8200 setter_sel_string.push_back(::toupper(property_name[0]));
8201 setter_sel_string.append(&property_name[1]);
8202 clang::IdentifierInfo *setter_ident =
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00008203 &clang_ast->Idents.get(setter_sel_string);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008204 setter_sel = clang_ast->Selectors.getSelector(1, &setter_ident);
8205 }
8206 property_decl->setSetterName(setter_sel);
8207 property_decl->setPropertyAttributes(
8208 clang::ObjCPropertyDecl::OBJC_PR_setter);
8209
8210 if (property_getter_name != nullptr) {
8211 clang::IdentifierInfo *getter_ident =
8212 &clang_ast->Idents.get(property_getter_name);
8213 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8214 } else {
8215 clang::IdentifierInfo *getter_ident =
8216 &clang_ast->Idents.get(property_name);
8217 getter_sel = clang_ast->Selectors.getSelector(0, &getter_ident);
8218 }
8219 property_decl->setGetterName(getter_sel);
8220 property_decl->setPropertyAttributes(
8221 clang::ObjCPropertyDecl::OBJC_PR_getter);
8222
8223 if (ivar_decl)
8224 property_decl->setPropertyIvarDecl(ivar_decl);
8225
8226 if (property_attributes & DW_APPLE_PROPERTY_readonly)
8227 property_decl->setPropertyAttributes(
8228 clang::ObjCPropertyDecl::OBJC_PR_readonly);
8229 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
8230 property_decl->setPropertyAttributes(
8231 clang::ObjCPropertyDecl::OBJC_PR_readwrite);
8232 if (property_attributes & DW_APPLE_PROPERTY_assign)
8233 property_decl->setPropertyAttributes(
8234 clang::ObjCPropertyDecl::OBJC_PR_assign);
8235 if (property_attributes & DW_APPLE_PROPERTY_retain)
8236 property_decl->setPropertyAttributes(
8237 clang::ObjCPropertyDecl::OBJC_PR_retain);
8238 if (property_attributes & DW_APPLE_PROPERTY_copy)
8239 property_decl->setPropertyAttributes(
8240 clang::ObjCPropertyDecl::OBJC_PR_copy);
8241 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
8242 property_decl->setPropertyAttributes(
8243 clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
8244 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_nullability)
8245 property_decl->setPropertyAttributes(
8246 clang::ObjCPropertyDecl::OBJC_PR_nullability);
8247 if (property_attributes &
8248 clang::ObjCPropertyDecl::OBJC_PR_null_resettable)
8249 property_decl->setPropertyAttributes(
8250 clang::ObjCPropertyDecl::OBJC_PR_null_resettable);
8251 if (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class)
8252 property_decl->setPropertyAttributes(
8253 clang::ObjCPropertyDecl::OBJC_PR_class);
8254
8255 const bool isInstance =
8256 (property_attributes & clang::ObjCPropertyDecl::OBJC_PR_class) == 0;
8257
8258 if (!getter_sel.isNull() &&
8259 !(isInstance
8260 ? class_interface_decl->lookupInstanceMethod(getter_sel)
8261 : class_interface_decl->lookupClassMethod(getter_sel))) {
8262 const bool isVariadic = false;
8263 const bool isSynthesized = false;
8264 const bool isImplicitlyDeclared = true;
8265 const bool isDefined = false;
8266 const clang::ObjCMethodDecl::ImplementationControl impControl =
8267 clang::ObjCMethodDecl::None;
8268 const bool HasRelatedResultType = false;
8269
8270 clang::ObjCMethodDecl *getter = clang::ObjCMethodDecl::Create(
8271 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8272 getter_sel, ClangUtil::GetQualType(property_clang_type_to_access),
8273 nullptr, class_interface_decl, isInstance, isVariadic,
8274 isSynthesized, isImplicitlyDeclared, isDefined, impControl,
8275 HasRelatedResultType);
8276
8277 if (getter && metadata)
8278 ClangASTContext::SetMetadata(clang_ast, getter, *metadata);
8279
8280 if (getter) {
8281 getter->setMethodParams(*clang_ast,
8282 llvm::ArrayRef<clang::ParmVarDecl *>(),
8283 llvm::ArrayRef<clang::SourceLocation>());
8284
8285 class_interface_decl->addDecl(getter);
8286 }
8287 }
8288
8289 if (!setter_sel.isNull() &&
8290 !(isInstance
8291 ? class_interface_decl->lookupInstanceMethod(setter_sel)
8292 : class_interface_decl->lookupClassMethod(setter_sel))) {
8293 clang::QualType result_type = clang_ast->VoidTy;
8294 const bool isVariadic = false;
8295 const bool isSynthesized = false;
8296 const bool isImplicitlyDeclared = true;
8297 const bool isDefined = false;
8298 const clang::ObjCMethodDecl::ImplementationControl impControl =
8299 clang::ObjCMethodDecl::None;
8300 const bool HasRelatedResultType = false;
8301
8302 clang::ObjCMethodDecl *setter = clang::ObjCMethodDecl::Create(
8303 *clang_ast, clang::SourceLocation(), clang::SourceLocation(),
8304 setter_sel, result_type, nullptr, class_interface_decl,
8305 isInstance, isVariadic, isSynthesized, isImplicitlyDeclared,
8306 isDefined, impControl, HasRelatedResultType);
8307
8308 if (setter && metadata)
8309 ClangASTContext::SetMetadata(clang_ast, setter, *metadata);
8310
8311 llvm::SmallVector<clang::ParmVarDecl *, 1> params;
8312
8313 params.push_back(clang::ParmVarDecl::Create(
8314 *clang_ast, setter, clang::SourceLocation(),
8315 clang::SourceLocation(),
8316 nullptr, // anonymous
8317 ClangUtil::GetQualType(property_clang_type_to_access), nullptr,
8318 clang::SC_Auto, nullptr));
8319
8320 if (setter) {
8321 setter->setMethodParams(
8322 *clang_ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8323 llvm::ArrayRef<clang::SourceLocation>());
8324
8325 class_interface_decl->addDecl(setter);
8326 }
8327 }
8328
8329 return true;
8330 }
8331 }
8332 }
8333 return false;
8334}
8335
8336bool ClangASTContext::IsObjCClassTypeAndHasIVars(const CompilerType &type,
8337 bool check_superclass) {
8338 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8339 if (class_interface_decl)
8340 return ObjCDeclHasIVars(class_interface_decl, check_superclass);
8341 return false;
8342}
8343
8344clang::ObjCMethodDecl *ClangASTContext::AddMethodToObjCObjectType(
8345 const CompilerType &type,
8346 const char *name, // the full symbol name as seen in the symbol table
8347 // (lldb::opaque_compiler_type_t type, "-[NString
8348 // stringWithCString:]")
8349 const CompilerType &method_clang_type, lldb::AccessType access,
8350 bool is_artificial, bool is_variadic) {
8351 if (!type || !method_clang_type.IsValid())
Greg Claytond8d4a572015-08-11 21:38:15 +00008352 return nullptr;
Greg Claytond8d4a572015-08-11 21:38:15 +00008353
Kate Stoneb9c1b512016-09-06 20:57:50 +00008354 clang::ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl(type);
8355
8356 if (class_interface_decl == nullptr)
8357 return nullptr;
8358 ClangASTContext *lldb_ast =
8359 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8360 if (lldb_ast == nullptr)
8361 return nullptr;
8362 clang::ASTContext *ast = lldb_ast->getASTContext();
8363
8364 const char *selector_start = ::strchr(name, ' ');
8365 if (selector_start == nullptr)
8366 return nullptr;
8367
8368 selector_start++;
8369 llvm::SmallVector<clang::IdentifierInfo *, 12> selector_idents;
8370
8371 size_t len = 0;
8372 const char *start;
8373 // printf ("name = '%s'\n", name);
8374
8375 unsigned num_selectors_with_args = 0;
8376 for (start = selector_start; start && *start != '\0' && *start != ']';
8377 start += len) {
8378 len = ::strcspn(start, ":]");
8379 bool has_arg = (start[len] == ':');
8380 if (has_arg)
8381 ++num_selectors_with_args;
8382 selector_idents.push_back(&ast->Idents.get(llvm::StringRef(start, len)));
8383 if (has_arg)
8384 len += 1;
8385 }
8386
8387 if (selector_idents.size() == 0)
8388 return nullptr;
8389
8390 clang::Selector method_selector = ast->Selectors.getSelector(
8391 num_selectors_with_args ? selector_idents.size() : 0,
8392 selector_idents.data());
8393
8394 clang::QualType method_qual_type(ClangUtil::GetQualType(method_clang_type));
8395
8396 // Populate the method decl with parameter decls
8397 const clang::Type *method_type(method_qual_type.getTypePtr());
8398
8399 if (method_type == nullptr)
8400 return nullptr;
8401
8402 const clang::FunctionProtoType *method_function_prototype(
8403 llvm::dyn_cast<clang::FunctionProtoType>(method_type));
8404
8405 if (!method_function_prototype)
8406 return nullptr;
8407
8408 bool is_synthesized = false;
8409 bool is_defined = false;
8410 clang::ObjCMethodDecl::ImplementationControl imp_control =
8411 clang::ObjCMethodDecl::None;
8412
8413 const unsigned num_args = method_function_prototype->getNumParams();
8414
8415 if (num_args != num_selectors_with_args)
8416 return nullptr; // some debug information is corrupt. We are not going to
8417 // deal with it.
8418
8419 clang::ObjCMethodDecl *objc_method_decl = clang::ObjCMethodDecl::Create(
8420 *ast,
8421 clang::SourceLocation(), // beginLoc,
8422 clang::SourceLocation(), // endLoc,
8423 method_selector, method_function_prototype->getReturnType(),
8424 nullptr, // TypeSourceInfo *ResultTInfo,
8425 ClangASTContext::GetASTContext(ast)->GetDeclContextForType(
8426 ClangUtil::GetQualType(type)),
8427 name[0] == '-', is_variadic, is_synthesized,
8428 true, // is_implicitly_declared; we force this to true because we don't
8429 // have source locations
8430 is_defined, imp_control, false /*has_related_result_type*/);
8431
8432 if (objc_method_decl == nullptr)
8433 return nullptr;
8434
8435 if (num_args > 0) {
8436 llvm::SmallVector<clang::ParmVarDecl *, 12> params;
8437
8438 for (unsigned param_index = 0; param_index < num_args; ++param_index) {
8439 params.push_back(clang::ParmVarDecl::Create(
8440 *ast, objc_method_decl, clang::SourceLocation(),
8441 clang::SourceLocation(),
8442 nullptr, // anonymous
8443 method_function_prototype->getParamType(param_index), nullptr,
8444 clang::SC_Auto, nullptr));
Greg Claytond8d4a572015-08-11 21:38:15 +00008445 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008446
Kate Stoneb9c1b512016-09-06 20:57:50 +00008447 objc_method_decl->setMethodParams(
8448 *ast, llvm::ArrayRef<clang::ParmVarDecl *>(params),
8449 llvm::ArrayRef<clang::SourceLocation>());
8450 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008451
Kate Stoneb9c1b512016-09-06 20:57:50 +00008452 class_interface_decl->addDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008453
Greg Claytond8d4a572015-08-11 21:38:15 +00008454#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +00008455 VerifyDecl(objc_method_decl);
Greg Claytond8d4a572015-08-11 21:38:15 +00008456#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00008457
8458 return objc_method_decl;
Greg Claytond8d4a572015-08-11 21:38:15 +00008459}
8460
Kate Stoneb9c1b512016-09-06 20:57:50 +00008461bool ClangASTContext::GetHasExternalStorage(const CompilerType &type) {
8462 if (ClangUtil::IsClangType(type))
Greg Claytone6b36cd2015-12-08 01:02:08 +00008463 return false;
Greg Claytone6b36cd2015-12-08 01:02:08 +00008464
Kate Stoneb9c1b512016-09-06 20:57:50 +00008465 clang::QualType qual_type(ClangUtil::GetCanonicalQualType(type));
Greg Claytone6b36cd2015-12-08 01:02:08 +00008466
Kate Stoneb9c1b512016-09-06 20:57:50 +00008467 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8468 switch (type_class) {
8469 case clang::Type::Record: {
8470 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8471 if (cxx_record_decl)
8472 return cxx_record_decl->hasExternalLexicalStorage() ||
8473 cxx_record_decl->hasExternalVisibleStorage();
8474 } break;
Enrico Granata36f51e42015-12-18 22:41:25 +00008475
Kate Stoneb9c1b512016-09-06 20:57:50 +00008476 case clang::Type::Enum: {
8477 clang::EnumDecl *enum_decl =
8478 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8479 if (enum_decl)
8480 return enum_decl->hasExternalLexicalStorage() ||
8481 enum_decl->hasExternalVisibleStorage();
8482 } break;
8483
8484 case clang::Type::ObjCObject:
8485 case clang::Type::ObjCInterface: {
8486 const clang::ObjCObjectType *objc_class_type =
8487 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8488 assert(objc_class_type);
8489 if (objc_class_type) {
8490 clang::ObjCInterfaceDecl *class_interface_decl =
8491 objc_class_type->getInterface();
8492
8493 if (class_interface_decl)
8494 return class_interface_decl->hasExternalLexicalStorage() ||
8495 class_interface_decl->hasExternalVisibleStorage();
Greg Claytond8d4a572015-08-11 21:38:15 +00008496 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008497 } break;
8498
8499 case clang::Type::Typedef:
8500 return GetHasExternalStorage(CompilerType(
8501 type.GetTypeSystem(), llvm::cast<clang::TypedefType>(qual_type)
8502 ->getDecl()
8503 ->getUnderlyingType()
8504 .getAsOpaquePtr()));
8505
8506 case clang::Type::Auto:
8507 return GetHasExternalStorage(CompilerType(
8508 type.GetTypeSystem(), llvm::cast<clang::AutoType>(qual_type)
8509 ->getDeducedType()
8510 .getAsOpaquePtr()));
8511
8512 case clang::Type::Elaborated:
8513 return GetHasExternalStorage(CompilerType(
8514 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
8515 ->getNamedType()
8516 .getAsOpaquePtr()));
8517
8518 case clang::Type::Paren:
8519 return GetHasExternalStorage(CompilerType(
8520 type.GetTypeSystem(),
8521 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
8522
8523 default:
8524 break;
8525 }
8526 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008527}
8528
Kate Stoneb9c1b512016-09-06 20:57:50 +00008529bool ClangASTContext::SetHasExternalStorage(lldb::opaque_compiler_type_t type,
8530 bool has_extern) {
8531 if (!type)
8532 return false;
8533
8534 clang::QualType qual_type(GetCanonicalQualType(type));
8535
8536 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
8537 switch (type_class) {
8538 case clang::Type::Record: {
8539 clang::CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
8540 if (cxx_record_decl) {
8541 cxx_record_decl->setHasExternalLexicalStorage(has_extern);
8542 cxx_record_decl->setHasExternalVisibleStorage(has_extern);
8543 return true;
8544 }
8545 } break;
8546
8547 case clang::Type::Enum: {
8548 clang::EnumDecl *enum_decl =
8549 llvm::cast<clang::EnumType>(qual_type)->getDecl();
8550 if (enum_decl) {
8551 enum_decl->setHasExternalLexicalStorage(has_extern);
8552 enum_decl->setHasExternalVisibleStorage(has_extern);
8553 return true;
8554 }
8555 } break;
8556
8557 case clang::Type::ObjCObject:
8558 case clang::Type::ObjCInterface: {
8559 const clang::ObjCObjectType *objc_class_type =
8560 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
8561 assert(objc_class_type);
8562 if (objc_class_type) {
8563 clang::ObjCInterfaceDecl *class_interface_decl =
8564 objc_class_type->getInterface();
8565
8566 if (class_interface_decl) {
8567 class_interface_decl->setHasExternalLexicalStorage(has_extern);
8568 class_interface_decl->setHasExternalVisibleStorage(has_extern);
8569 return true;
8570 }
8571 }
8572 } break;
8573
8574 case clang::Type::Typedef:
8575 return SetHasExternalStorage(llvm::cast<clang::TypedefType>(qual_type)
8576 ->getDecl()
8577 ->getUnderlyingType()
8578 .getAsOpaquePtr(),
8579 has_extern);
8580
8581 case clang::Type::Auto:
8582 return SetHasExternalStorage(llvm::cast<clang::AutoType>(qual_type)
8583 ->getDeducedType()
8584 .getAsOpaquePtr(),
8585 has_extern);
8586
8587 case clang::Type::Elaborated:
8588 return SetHasExternalStorage(llvm::cast<clang::ElaboratedType>(qual_type)
8589 ->getNamedType()
8590 .getAsOpaquePtr(),
8591 has_extern);
8592
8593 case clang::Type::Paren:
8594 return SetHasExternalStorage(
8595 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr(),
8596 has_extern);
8597
8598 default:
8599 break;
8600 }
8601 return false;
8602}
Greg Claytond8d4a572015-08-11 21:38:15 +00008603
8604#pragma mark TagDecl
8605
Kate Stoneb9c1b512016-09-06 20:57:50 +00008606bool ClangASTContext::StartTagDeclarationDefinition(const CompilerType &type) {
8607 clang::QualType qual_type(ClangUtil::GetQualType(type));
8608 if (!qual_type.isNull()) {
8609 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8610 if (tag_type) {
8611 clang::TagDecl *tag_decl = tag_type->getDecl();
8612 if (tag_decl) {
8613 tag_decl->startDefinition();
8614 return true;
8615 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008616 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008617
8618 const clang::ObjCObjectType *object_type =
8619 qual_type->getAs<clang::ObjCObjectType>();
8620 if (object_type) {
8621 clang::ObjCInterfaceDecl *interface_decl = object_type->getInterface();
8622 if (interface_decl) {
8623 interface_decl->startDefinition();
8624 return true;
8625 }
8626 }
8627 }
8628 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008629}
8630
Kate Stoneb9c1b512016-09-06 20:57:50 +00008631bool ClangASTContext::CompleteTagDeclarationDefinition(
8632 const CompilerType &type) {
8633 clang::QualType qual_type(ClangUtil::GetQualType(type));
8634 if (!qual_type.isNull()) {
8635 // Make sure we use the same methodology as
8636 // ClangASTContext::StartTagDeclarationDefinition()
8637 // as to how we start/end the definition. Previously we were calling
8638 const clang::TagType *tag_type = qual_type->getAs<clang::TagType>();
8639 if (tag_type) {
8640 clang::TagDecl *tag_decl = tag_type->getDecl();
8641 if (tag_decl) {
8642 clang::CXXRecordDecl *cxx_record_decl =
8643 llvm::dyn_cast_or_null<clang::CXXRecordDecl>(tag_decl);
8644
8645 if (cxx_record_decl) {
8646 if (!cxx_record_decl->isCompleteDefinition())
8647 cxx_record_decl->completeDefinition();
8648 cxx_record_decl->setHasLoadedFieldsFromExternalStorage(true);
8649 cxx_record_decl->setHasExternalLexicalStorage(false);
8650 cxx_record_decl->setHasExternalVisibleStorage(false);
8651 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008652 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008653 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008654 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008655
8656 const clang::EnumType *enutype = qual_type->getAs<clang::EnumType>();
8657
8658 if (enutype) {
8659 clang::EnumDecl *enum_decl = enutype->getDecl();
8660
8661 if (enum_decl) {
8662 if (!enum_decl->isCompleteDefinition()) {
8663 ClangASTContext *lldb_ast =
8664 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8665 if (lldb_ast == nullptr)
8666 return false;
8667 clang::ASTContext *ast = lldb_ast->getASTContext();
8668
8669 /// TODO This really needs to be fixed.
8670
8671 QualType integer_type(enum_decl->getIntegerType());
8672 if (!integer_type.isNull()) {
8673 unsigned NumPositiveBits = 1;
8674 unsigned NumNegativeBits = 0;
8675
8676 clang::QualType promotion_qual_type;
8677 // If the enum integer type is less than an integer in bit width,
8678 // then we must promote it to an integer size.
8679 if (ast->getTypeSize(enum_decl->getIntegerType()) <
8680 ast->getTypeSize(ast->IntTy)) {
8681 if (enum_decl->getIntegerType()->isSignedIntegerType())
8682 promotion_qual_type = ast->IntTy;
8683 else
8684 promotion_qual_type = ast->UnsignedIntTy;
8685 } else
8686 promotion_qual_type = enum_decl->getIntegerType();
8687
8688 enum_decl->completeDefinition(enum_decl->getIntegerType(),
8689 promotion_qual_type, NumPositiveBits,
8690 NumNegativeBits);
8691 }
8692 }
8693 return true;
8694 }
8695 }
8696 }
8697 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008698}
8699
Kate Stoneb9c1b512016-09-06 20:57:50 +00008700bool ClangASTContext::AddEnumerationValueToEnumerationType(
8701 lldb::opaque_compiler_type_t type,
8702 const CompilerType &enumerator_clang_type, const Declaration &decl,
8703 const char *name, int64_t enum_value, uint32_t enum_value_bit_size) {
8704 if (type && enumerator_clang_type.IsValid() && name && name[0]) {
8705 clang::QualType enum_qual_type(GetCanonicalQualType(type));
Zachary Turnerd133f6a2016-03-28 22:53:41 +00008706
Kate Stoneb9c1b512016-09-06 20:57:50 +00008707 bool is_signed = false;
8708 enumerator_clang_type.IsIntegerType(is_signed);
Greg Claytond8d4a572015-08-11 21:38:15 +00008709 const clang::Type *clang_type = enum_qual_type.getTypePtr();
Kate Stoneb9c1b512016-09-06 20:57:50 +00008710 if (clang_type) {
8711 const clang::EnumType *enutype =
8712 llvm::dyn_cast<clang::EnumType>(clang_type);
8713
8714 if (enutype) {
8715 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
8716 enum_llvm_apsint = enum_value;
8717 clang::EnumConstantDecl *enumerator_decl =
8718 clang::EnumConstantDecl::Create(
8719 *getASTContext(), enutype->getDecl(), clang::SourceLocation(),
8720 name ? &getASTContext()->Idents.get(name)
8721 : nullptr, // Identifier
8722 ClangUtil::GetQualType(enumerator_clang_type),
8723 nullptr, enum_llvm_apsint);
8724
8725 if (enumerator_decl) {
8726 enutype->getDecl()->addDecl(enumerator_decl);
8727
8728#ifdef LLDB_CONFIGURATION_DEBUG
8729 VerifyDecl(enumerator_decl);
8730#endif
8731
8732 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00008733 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008734 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008735 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008736 }
8737 return false;
Greg Claytond8d4a572015-08-11 21:38:15 +00008738}
8739
Greg Claytona1e5dc82015-08-11 22:53:00 +00008740CompilerType
Kate Stoneb9c1b512016-09-06 20:57:50 +00008741ClangASTContext::GetEnumerationIntegerType(lldb::opaque_compiler_type_t type) {
8742 clang::QualType enum_qual_type(GetCanonicalQualType(type));
8743 const clang::Type *clang_type = enum_qual_type.getTypePtr();
8744 if (clang_type) {
8745 const clang::EnumType *enutype =
8746 llvm::dyn_cast<clang::EnumType>(clang_type);
8747 if (enutype) {
8748 clang::EnumDecl *enum_decl = enutype->getDecl();
8749 if (enum_decl)
8750 return CompilerType(getASTContext(), enum_decl->getIntegerType());
Greg Claytond8d4a572015-08-11 21:38:15 +00008751 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008752 }
8753 return CompilerType();
Greg Claytond8d4a572015-08-11 21:38:15 +00008754}
8755
Kate Stoneb9c1b512016-09-06 20:57:50 +00008756CompilerType
8757ClangASTContext::CreateMemberPointerType(const CompilerType &type,
8758 const CompilerType &pointee_type) {
8759 if (type && pointee_type.IsValid() &&
8760 type.GetTypeSystem() == pointee_type.GetTypeSystem()) {
8761 ClangASTContext *ast =
8762 llvm::dyn_cast<ClangASTContext>(type.GetTypeSystem());
8763 if (!ast)
8764 return CompilerType();
8765 return CompilerType(ast->getASTContext(),
8766 ast->getASTContext()->getMemberPointerType(
8767 ClangUtil::GetQualType(pointee_type),
8768 ClangUtil::GetQualType(type).getTypePtr()));
8769 }
8770 return CompilerType();
8771}
Greg Claytond8d4a572015-08-11 21:38:15 +00008772
8773size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +00008774ClangASTContext::ConvertStringToFloatValue(lldb::opaque_compiler_type_t type,
8775 const char *s, uint8_t *dst,
8776 size_t dst_size) {
8777 if (type) {
8778 clang::QualType qual_type(GetCanonicalQualType(type));
8779 uint32_t count = 0;
8780 bool is_complex = false;
8781 if (IsFloatingPointType(type, count, is_complex)) {
8782 // TODO: handle complex and vector types
8783 if (count != 1)
8784 return false;
8785
8786 llvm::StringRef s_sref(s);
8787 llvm::APFloat ap_float(getASTContext()->getFloatTypeSemantics(qual_type),
8788 s_sref);
8789
8790 const uint64_t bit_size = getASTContext()->getTypeSize(qual_type);
8791 const uint64_t byte_size = bit_size / 8;
8792 if (dst_size >= byte_size) {
8793 Scalar scalar = ap_float.bitcastToAPInt().zextOrTrunc(
8794 llvm::NextPowerOf2(byte_size) * 8);
8795 lldb_private::Error get_data_error;
8796 if (scalar.GetAsMemoryData(dst, byte_size,
8797 lldb_private::endian::InlHostByteOrder(),
8798 get_data_error))
8799 return byte_size;
8800 }
Greg Claytond8d4a572015-08-11 21:38:15 +00008801 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00008802 }
8803 return 0;
Greg Claytond8d4a572015-08-11 21:38:15 +00008804}
8805
Greg Claytond8d4a572015-08-11 21:38:15 +00008806//----------------------------------------------------------------------
8807// Dumping types
8808//----------------------------------------------------------------------
8809#define DEPTH_INCREMENT 2
8810
Kate Stoneb9c1b512016-09-06 20:57:50 +00008811void ClangASTContext::DumpValue(
8812 lldb::opaque_compiler_type_t type, ExecutionContext *exe_ctx, Stream *s,
Zachary Turner29cb8682017-03-03 20:57:05 +00008813 lldb::Format format, const DataExtractor &data,
Kate Stoneb9c1b512016-09-06 20:57:50 +00008814 lldb::offset_t data_byte_offset, size_t data_byte_size,
8815 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset, bool show_types,
8816 bool show_summary, bool verbose, uint32_t depth) {
8817 if (!type)
8818 return;
8819
8820 clang::QualType qual_type(GetQualType(type));
8821 switch (qual_type->getTypeClass()) {
8822 case clang::Type::Record:
8823 if (GetCompleteType(type)) {
8824 const clang::RecordType *record_type =
8825 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
8826 const clang::RecordDecl *record_decl = record_type->getDecl();
8827 assert(record_decl);
8828 uint32_t field_bit_offset = 0;
8829 uint32_t field_byte_offset = 0;
8830 const clang::ASTRecordLayout &record_layout =
8831 getASTContext()->getASTRecordLayout(record_decl);
8832 uint32_t child_idx = 0;
8833
8834 const clang::CXXRecordDecl *cxx_record_decl =
8835 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
8836 if (cxx_record_decl) {
8837 // We might have base classes to print out first
8838 clang::CXXRecordDecl::base_class_const_iterator base_class,
8839 base_class_end;
8840 for (base_class = cxx_record_decl->bases_begin(),
8841 base_class_end = cxx_record_decl->bases_end();
8842 base_class != base_class_end; ++base_class) {
8843 const clang::CXXRecordDecl *base_class_decl =
8844 llvm::cast<clang::CXXRecordDecl>(
8845 base_class->getType()->getAs<clang::RecordType>()->getDecl());
8846
8847 // Skip empty base classes
8848 if (verbose == false &&
8849 ClangASTContext::RecordHasFields(base_class_decl) == false)
8850 continue;
8851
8852 if (base_class->isVirtual())
8853 field_bit_offset =
8854 record_layout.getVBaseClassOffset(base_class_decl)
8855 .getQuantity() *
8856 8;
8857 else
8858 field_bit_offset = record_layout.getBaseClassOffset(base_class_decl)
8859 .getQuantity() *
8860 8;
8861 field_byte_offset = field_bit_offset / 8;
8862 assert(field_bit_offset % 8 == 0);
8863 if (child_idx == 0)
8864 s->PutChar('{');
8865 else
8866 s->PutChar(',');
8867
8868 clang::QualType base_class_qual_type = base_class->getType();
8869 std::string base_class_type_name(base_class_qual_type.getAsString());
8870
8871 // Indent and print the base class type name
Zachary Turner827d5d72016-12-16 04:27:00 +00008872 s->Format("\n{0}{1}", llvm::fmt_repeat(" ", depth + DEPTH_INCREMENT),
8873 base_class_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00008874
8875 clang::TypeInfo base_class_type_info =
8876 getASTContext()->getTypeInfo(base_class_qual_type);
8877
8878 // Dump the value of the member
8879 CompilerType base_clang_type(getASTContext(), base_class_qual_type);
8880 base_clang_type.DumpValue(
8881 exe_ctx,
8882 s, // Stream to dump to
8883 base_clang_type
8884 .GetFormat(), // The format with which to display the member
8885 data, // Data buffer containing all bytes for this type
8886 data_byte_offset + field_byte_offset, // Offset into "data" where
8887 // to grab value from
8888 base_class_type_info.Width / 8, // Size of this type in bytes
8889 0, // Bitfield bit size
8890 0, // Bitfield bit offset
8891 show_types, // Boolean indicating if we should show the variable
8892 // types
8893 show_summary, // Boolean indicating if we should show a summary
8894 // for the current type
8895 verbose, // Verbose output?
8896 depth + DEPTH_INCREMENT); // Scope depth for any types that have
8897 // children
8898
8899 ++child_idx;
8900 }
8901 }
8902 uint32_t field_idx = 0;
8903 clang::RecordDecl::field_iterator field, field_end;
8904 for (field = record_decl->field_begin(),
8905 field_end = record_decl->field_end();
8906 field != field_end; ++field, ++field_idx, ++child_idx) {
8907 // Print the starting squiggly bracket (if this is the
8908 // first member) or comma (for member 2 and beyond) for
8909 // the struct/union/class member.
8910 if (child_idx == 0)
8911 s->PutChar('{');
8912 else
8913 s->PutChar(',');
8914
8915 // Indent
8916 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
8917
8918 clang::QualType field_type = field->getType();
8919 // Print the member type if requested
8920 // Figure out the type byte size (field_type_info.first) and
8921 // alignment (field_type_info.second) from the AST context.
8922 clang::TypeInfo field_type_info =
8923 getASTContext()->getTypeInfo(field_type);
8924 assert(field_idx < record_layout.getFieldCount());
8925 // Figure out the field offset within the current struct/union/class
8926 // type
8927 field_bit_offset = record_layout.getFieldOffset(field_idx);
8928 field_byte_offset = field_bit_offset / 8;
8929 uint32_t field_bitfield_bit_size = 0;
8930 uint32_t field_bitfield_bit_offset = 0;
8931 if (ClangASTContext::FieldIsBitfield(getASTContext(), *field,
8932 field_bitfield_bit_size))
8933 field_bitfield_bit_offset = field_bit_offset % 8;
8934
8935 if (show_types) {
8936 std::string field_type_name(field_type.getAsString());
8937 if (field_bitfield_bit_size > 0)
8938 s->Printf("(%s:%u) ", field_type_name.c_str(),
8939 field_bitfield_bit_size);
8940 else
8941 s->Printf("(%s) ", field_type_name.c_str());
8942 }
8943 // Print the member name and equal sign
8944 s->Printf("%s = ", field->getNameAsString().c_str());
8945
8946 // Dump the value of the member
8947 CompilerType field_clang_type(getASTContext(), field_type);
8948 field_clang_type.DumpValue(
8949 exe_ctx,
8950 s, // Stream to dump to
8951 field_clang_type
8952 .GetFormat(), // The format with which to display the member
8953 data, // Data buffer containing all bytes for this type
8954 data_byte_offset + field_byte_offset, // Offset into "data" where to
8955 // grab value from
8956 field_type_info.Width / 8, // Size of this type in bytes
8957 field_bitfield_bit_size, // Bitfield bit size
8958 field_bitfield_bit_offset, // Bitfield bit offset
8959 show_types, // Boolean indicating if we should show the variable
8960 // types
8961 show_summary, // Boolean indicating if we should show a summary for
8962 // the current type
8963 verbose, // Verbose output?
8964 depth + DEPTH_INCREMENT); // Scope depth for any types that have
8965 // children
8966 }
8967
8968 // Indent the trailing squiggly bracket
8969 if (child_idx > 0)
8970 s->Printf("\n%*s}", depth, "");
8971 }
8972 return;
8973
8974 case clang::Type::Enum:
8975 if (GetCompleteType(type)) {
8976 const clang::EnumType *enutype =
8977 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
8978 const clang::EnumDecl *enum_decl = enutype->getDecl();
8979 assert(enum_decl);
8980 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
8981 lldb::offset_t offset = data_byte_offset;
8982 const int64_t enum_value = data.GetMaxU64Bitfield(
8983 &offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
8984 for (enum_pos = enum_decl->enumerator_begin(),
8985 enum_end_pos = enum_decl->enumerator_end();
8986 enum_pos != enum_end_pos; ++enum_pos) {
8987 if (enum_pos->getInitVal() == enum_value) {
8988 s->Printf("%s", enum_pos->getNameAsString().c_str());
8989 return;
8990 }
8991 }
8992 // If we have gotten here we didn't get find the enumerator in the
8993 // enum decl, so just print the integer.
8994 s->Printf("%" PRIi64, enum_value);
8995 }
8996 return;
8997
8998 case clang::Type::ConstantArray: {
8999 const clang::ConstantArrayType *array =
9000 llvm::cast<clang::ConstantArrayType>(qual_type.getTypePtr());
9001 bool is_array_of_characters = false;
9002 clang::QualType element_qual_type = array->getElementType();
9003
9004 const clang::Type *canonical_type =
9005 element_qual_type->getCanonicalTypeInternal().getTypePtr();
9006 if (canonical_type)
9007 is_array_of_characters = canonical_type->isCharType();
9008
9009 const uint64_t element_count = array->getSize().getLimitedValue();
9010
9011 clang::TypeInfo field_type_info =
9012 getASTContext()->getTypeInfo(element_qual_type);
9013
9014 uint32_t element_idx = 0;
9015 uint32_t element_offset = 0;
9016 uint64_t element_byte_size = field_type_info.Width / 8;
9017 uint32_t element_stride = element_byte_size;
9018
9019 if (is_array_of_characters) {
9020 s->PutChar('"');
Zachary Turner29cb8682017-03-03 20:57:05 +00009021 DumpDataExtractor(data, s, data_byte_offset, lldb::eFormatChar,
9022 element_byte_size, element_count, UINT32_MAX,
9023 LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009024 s->PutChar('"');
9025 return;
9026 } else {
9027 CompilerType element_clang_type(getASTContext(), element_qual_type);
9028 lldb::Format element_format = element_clang_type.GetFormat();
9029
9030 for (element_idx = 0; element_idx < element_count; ++element_idx) {
9031 // Print the starting squiggly bracket (if this is the
9032 // first member) or comman (for member 2 and beyong) for
9033 // the struct/union/class member.
9034 if (element_idx == 0)
9035 s->PutChar('{');
9036 else
9037 s->PutChar(',');
9038
9039 // Indent and print the index
9040 s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
9041
9042 // Figure out the field offset within the current struct/union/class
9043 // type
9044 element_offset = element_idx * element_stride;
9045
9046 // Dump the value of the member
9047 element_clang_type.DumpValue(
9048 exe_ctx,
9049 s, // Stream to dump to
9050 element_format, // The format with which to display the element
9051 data, // Data buffer containing all bytes for this type
9052 data_byte_offset +
9053 element_offset, // Offset into "data" where to grab value from
9054 element_byte_size, // Size of this type in bytes
9055 0, // Bitfield bit size
9056 0, // Bitfield bit offset
9057 show_types, // Boolean indicating if we should show the variable
9058 // types
9059 show_summary, // Boolean indicating if we should show a summary for
9060 // the current type
9061 verbose, // Verbose output?
9062 depth + DEPTH_INCREMENT); // Scope depth for any types that have
9063 // children
9064 }
9065
9066 // Indent the trailing squiggly bracket
9067 if (element_idx > 0)
9068 s->Printf("\n%*s}", depth, "");
9069 }
9070 }
9071 return;
9072
9073 case clang::Type::Typedef: {
9074 clang::QualType typedef_qual_type =
9075 llvm::cast<clang::TypedefType>(qual_type)
9076 ->getDecl()
9077 ->getUnderlyingType();
9078
9079 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9080 lldb::Format typedef_format = typedef_clang_type.GetFormat();
9081 clang::TypeInfo typedef_type_info =
9082 getASTContext()->getTypeInfo(typedef_qual_type);
9083 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9084
9085 return typedef_clang_type.DumpValue(
9086 exe_ctx,
9087 s, // Stream to dump to
9088 typedef_format, // The format with which to display the element
9089 data, // Data buffer containing all bytes for this type
9090 data_byte_offset, // Offset into "data" where to grab value from
9091 typedef_byte_size, // Size of this type in bytes
9092 bitfield_bit_size, // Bitfield bit size
9093 bitfield_bit_offset, // Bitfield bit offset
9094 show_types, // Boolean indicating if we should show the variable types
9095 show_summary, // Boolean indicating if we should show a summary for the
9096 // current type
9097 verbose, // Verbose output?
9098 depth); // Scope depth for any types that have children
9099 } break;
9100
9101 case clang::Type::Auto: {
9102 clang::QualType elaborated_qual_type =
9103 llvm::cast<clang::AutoType>(qual_type)->getDeducedType();
9104 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9105 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9106 clang::TypeInfo elaborated_type_info =
9107 getASTContext()->getTypeInfo(elaborated_qual_type);
9108 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9109
9110 return elaborated_clang_type.DumpValue(
9111 exe_ctx,
9112 s, // Stream to dump to
9113 elaborated_format, // The format with which to display the element
9114 data, // Data buffer containing all bytes for this type
9115 data_byte_offset, // Offset into "data" where to grab value from
9116 elaborated_byte_size, // Size of this type in bytes
9117 bitfield_bit_size, // Bitfield bit size
9118 bitfield_bit_offset, // Bitfield bit offset
9119 show_types, // Boolean indicating if we should show the variable types
9120 show_summary, // Boolean indicating if we should show a summary for the
9121 // current type
9122 verbose, // Verbose output?
9123 depth); // Scope depth for any types that have children
9124 } break;
9125
9126 case clang::Type::Elaborated: {
9127 clang::QualType elaborated_qual_type =
9128 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType();
9129 CompilerType elaborated_clang_type(getASTContext(), elaborated_qual_type);
9130 lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
9131 clang::TypeInfo elaborated_type_info =
9132 getASTContext()->getTypeInfo(elaborated_qual_type);
9133 uint64_t elaborated_byte_size = elaborated_type_info.Width / 8;
9134
9135 return elaborated_clang_type.DumpValue(
9136 exe_ctx,
9137 s, // Stream to dump to
9138 elaborated_format, // The format with which to display the element
9139 data, // Data buffer containing all bytes for this type
9140 data_byte_offset, // Offset into "data" where to grab value from
9141 elaborated_byte_size, // Size of this type in bytes
9142 bitfield_bit_size, // Bitfield bit size
9143 bitfield_bit_offset, // Bitfield bit offset
9144 show_types, // Boolean indicating if we should show the variable types
9145 show_summary, // Boolean indicating if we should show a summary for the
9146 // current type
9147 verbose, // Verbose output?
9148 depth); // Scope depth for any types that have children
9149 } break;
9150
9151 case clang::Type::Paren: {
9152 clang::QualType desugar_qual_type =
9153 llvm::cast<clang::ParenType>(qual_type)->desugar();
9154 CompilerType desugar_clang_type(getASTContext(), desugar_qual_type);
9155
9156 lldb::Format desugar_format = desugar_clang_type.GetFormat();
9157 clang::TypeInfo desugar_type_info =
9158 getASTContext()->getTypeInfo(desugar_qual_type);
9159 uint64_t desugar_byte_size = desugar_type_info.Width / 8;
9160
9161 return desugar_clang_type.DumpValue(
9162 exe_ctx,
9163 s, // Stream to dump to
9164 desugar_format, // The format with which to display the element
9165 data, // Data buffer containing all bytes for this type
9166 data_byte_offset, // Offset into "data" where to grab value from
9167 desugar_byte_size, // Size of this type in bytes
9168 bitfield_bit_size, // Bitfield bit size
9169 bitfield_bit_offset, // Bitfield bit offset
9170 show_types, // Boolean indicating if we should show the variable types
9171 show_summary, // Boolean indicating if we should show a summary for the
9172 // current type
9173 verbose, // Verbose output?
9174 depth); // Scope depth for any types that have children
9175 } break;
9176
9177 default:
9178 // We are down to a scalar type that we just need to display.
Zachary Turner29cb8682017-03-03 20:57:05 +00009179 DumpDataExtractor(data, s, data_byte_offset, format, data_byte_size, 1,
9180 UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size,
9181 bitfield_bit_offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009182
9183 if (show_summary)
9184 DumpSummary(type, exe_ctx, s, data, data_byte_offset, data_byte_size);
9185 break;
9186 }
9187}
9188
9189bool ClangASTContext::DumpTypeValue(
9190 lldb::opaque_compiler_type_t type, Stream *s, lldb::Format format,
Zachary Turner29cb8682017-03-03 20:57:05 +00009191 const DataExtractor &data, lldb::offset_t byte_offset, size_t byte_size,
9192 uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009193 ExecutionContextScope *exe_scope) {
9194 if (!type)
9195 return false;
9196 if (IsAggregateType(type)) {
9197 return false;
9198 } else {
Greg Claytond8d4a572015-08-11 21:38:15 +00009199 clang::QualType qual_type(GetQualType(type));
Kate Stoneb9c1b512016-09-06 20:57:50 +00009200
9201 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9202 switch (type_class) {
9203 case clang::Type::Typedef: {
9204 clang::QualType typedef_qual_type =
9205 llvm::cast<clang::TypedefType>(qual_type)
9206 ->getDecl()
9207 ->getUnderlyingType();
9208 CompilerType typedef_clang_type(getASTContext(), typedef_qual_type);
9209 if (format == eFormatDefault)
9210 format = typedef_clang_type.GetFormat();
9211 clang::TypeInfo typedef_type_info =
9212 getASTContext()->getTypeInfo(typedef_qual_type);
9213 uint64_t typedef_byte_size = typedef_type_info.Width / 8;
9214
9215 return typedef_clang_type.DumpTypeValue(
9216 s,
9217 format, // The format with which to display the element
9218 data, // Data buffer containing all bytes for this type
9219 byte_offset, // Offset into "data" where to grab value from
9220 typedef_byte_size, // Size of this type in bytes
9221 bitfield_bit_size, // Size in bits of a bitfield value, if zero don't
9222 // treat as a bitfield
9223 bitfield_bit_offset, // Offset in bits of a bitfield value if
9224 // bitfield_bit_size != 0
9225 exe_scope);
9226 } break;
9227
9228 case clang::Type::Enum:
9229 // If our format is enum or default, show the enumeration value as
9230 // its enumeration string value, else just display it as requested.
9231 if ((format == eFormatEnum || format == eFormatDefault) &&
9232 GetCompleteType(type)) {
9233 const clang::EnumType *enutype =
9234 llvm::cast<clang::EnumType>(qual_type.getTypePtr());
9235 const clang::EnumDecl *enum_decl = enutype->getDecl();
9236 assert(enum_decl);
9237 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
9238 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
9239 lldb::offset_t offset = byte_offset;
9240 if (is_signed) {
9241 const int64_t enum_svalue = data.GetMaxS64Bitfield(
9242 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9243 for (enum_pos = enum_decl->enumerator_begin(),
9244 enum_end_pos = enum_decl->enumerator_end();
9245 enum_pos != enum_end_pos; ++enum_pos) {
9246 if (enum_pos->getInitVal().getSExtValue() == enum_svalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009247 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009248 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009249 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009250 }
9251 // If we have gotten here we didn't get find the enumerator in the
9252 // enum decl, so just print the integer.
9253 s->Printf("%" PRIi64, enum_svalue);
9254 } else {
9255 const uint64_t enum_uvalue = data.GetMaxU64Bitfield(
9256 &offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
9257 for (enum_pos = enum_decl->enumerator_begin(),
9258 enum_end_pos = enum_decl->enumerator_end();
9259 enum_pos != enum_end_pos; ++enum_pos) {
9260 if (enum_pos->getInitVal().getZExtValue() == enum_uvalue) {
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009261 s->PutCString(enum_pos->getNameAsString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00009262 return true;
Greg Claytond8d4a572015-08-11 21:38:15 +00009263 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009264 }
9265 // If we have gotten here we didn't get find the enumerator in the
9266 // enum decl, so just print the integer.
9267 s->Printf("%" PRIu64, enum_uvalue);
Greg Claytond8d4a572015-08-11 21:38:15 +00009268 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009269 return true;
9270 }
9271 // format was not enum, just fall through and dump the value as
9272 // requested....
9273 LLVM_FALLTHROUGH;
9274
9275 default:
9276 // We are down to a scalar type that we just need to display.
9277 {
9278 uint32_t item_count = 1;
9279 // A few formats, we might need to modify our size and count for
9280 // depending
9281 // on how we are trying to display the value...
9282 switch (format) {
Greg Claytond8d4a572015-08-11 21:38:15 +00009283 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +00009284 case eFormatBoolean:
9285 case eFormatBinary:
9286 case eFormatComplex:
9287 case eFormatCString: // NULL terminated C strings
9288 case eFormatDecimal:
9289 case eFormatEnum:
9290 case eFormatHex:
9291 case eFormatHexUppercase:
9292 case eFormatFloat:
9293 case eFormatOctal:
9294 case eFormatOSType:
9295 case eFormatUnsigned:
9296 case eFormatPointer:
9297 case eFormatVectorOfChar:
9298 case eFormatVectorOfSInt8:
9299 case eFormatVectorOfUInt8:
9300 case eFormatVectorOfSInt16:
9301 case eFormatVectorOfUInt16:
9302 case eFormatVectorOfSInt32:
9303 case eFormatVectorOfUInt32:
9304 case eFormatVectorOfSInt64:
9305 case eFormatVectorOfUInt64:
9306 case eFormatVectorOfFloat32:
9307 case eFormatVectorOfFloat64:
9308 case eFormatVectorOfUInt128:
9309 break;
9310
9311 case eFormatChar:
9312 case eFormatCharPrintable:
9313 case eFormatCharArray:
9314 case eFormatBytes:
9315 case eFormatBytesWithASCII:
9316 item_count = byte_size;
9317 byte_size = 1;
9318 break;
9319
9320 case eFormatUnicode16:
9321 item_count = byte_size / 2;
9322 byte_size = 2;
9323 break;
9324
9325 case eFormatUnicode32:
9326 item_count = byte_size / 4;
9327 byte_size = 4;
9328 break;
9329 }
Zachary Turner29cb8682017-03-03 20:57:05 +00009330 return DumpDataExtractor(data, s, byte_offset, format, byte_size,
9331 item_count, UINT32_MAX, LLDB_INVALID_ADDRESS,
9332 bitfield_bit_size, bitfield_bit_offset,
9333 exe_scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009334 }
9335 break;
9336 }
9337 }
9338 return 0;
9339}
9340
9341void ClangASTContext::DumpSummary(lldb::opaque_compiler_type_t type,
9342 ExecutionContext *exe_ctx, Stream *s,
9343 const lldb_private::DataExtractor &data,
9344 lldb::offset_t data_byte_offset,
9345 size_t data_byte_size) {
9346 uint32_t length = 0;
9347 if (IsCStringType(type, length)) {
9348 if (exe_ctx) {
9349 Process *process = exe_ctx->GetProcessPtr();
9350 if (process) {
9351 lldb::offset_t offset = data_byte_offset;
9352 lldb::addr_t pointer_address = data.GetMaxU64(&offset, data_byte_size);
9353 std::vector<uint8_t> buf;
9354 if (length > 0)
9355 buf.resize(length);
9356 else
9357 buf.resize(256);
9358
Zachary Turner29cb8682017-03-03 20:57:05 +00009359 DataExtractor cstr_data(&buf.front(), buf.size(),
9360 process->GetByteOrder(), 4);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009361 buf.back() = '\0';
9362 size_t bytes_read;
9363 size_t total_cstr_len = 0;
9364 Error error;
9365 while ((bytes_read = process->ReadMemory(pointer_address, &buf.front(),
9366 buf.size(), error)) > 0) {
9367 const size_t len = strlen((const char *)&buf.front());
9368 if (len == 0)
Greg Claytond8d4a572015-08-11 21:38:15 +00009369 break;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009370 if (total_cstr_len == 0)
9371 s->PutCString(" \"");
Zachary Turner29cb8682017-03-03 20:57:05 +00009372 DumpDataExtractor(cstr_data, s, 0, lldb::eFormatChar, 1, len,
9373 UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009374 total_cstr_len += len;
9375 if (len < buf.size())
9376 break;
9377 pointer_address += total_cstr_len;
Greg Claytond8d4a572015-08-11 21:38:15 +00009378 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009379 if (total_cstr_len > 0)
9380 s->PutChar('"');
9381 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009382 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009383 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009384}
9385
Kate Stoneb9c1b512016-09-06 20:57:50 +00009386void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type) {
9387 StreamFile s(stdout, false);
9388 DumpTypeDescription(type, &s);
9389 ClangASTMetadata *metadata =
9390 ClangASTContext::GetMetadata(getASTContext(), type);
9391 if (metadata) {
9392 metadata->Dump(&s);
9393 }
9394}
Greg Claytond8d4a572015-08-11 21:38:15 +00009395
Kate Stoneb9c1b512016-09-06 20:57:50 +00009396void ClangASTContext::DumpTypeDescription(lldb::opaque_compiler_type_t type,
9397 Stream *s) {
9398 if (type) {
9399 clang::QualType qual_type(GetQualType(type));
Greg Claytond8d4a572015-08-11 21:38:15 +00009400
Kate Stoneb9c1b512016-09-06 20:57:50 +00009401 llvm::SmallVector<char, 1024> buf;
9402 llvm::raw_svector_ostream llvm_ostrm(buf);
9403
9404 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9405 switch (type_class) {
9406 case clang::Type::ObjCObject:
9407 case clang::Type::ObjCInterface: {
9408 GetCompleteType(type);
9409
9410 const clang::ObjCObjectType *objc_class_type =
9411 llvm::dyn_cast<clang::ObjCObjectType>(qual_type.getTypePtr());
9412 assert(objc_class_type);
9413 if (objc_class_type) {
9414 clang::ObjCInterfaceDecl *class_interface_decl =
9415 objc_class_type->getInterface();
9416 if (class_interface_decl) {
9417 clang::PrintingPolicy policy = getASTContext()->getPrintingPolicy();
9418 class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
Greg Claytond8d4a572015-08-11 21:38:15 +00009419 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009420 }
9421 } break;
Greg Claytond8d4a572015-08-11 21:38:15 +00009422
Kate Stoneb9c1b512016-09-06 20:57:50 +00009423 case clang::Type::Typedef: {
9424 const clang::TypedefType *typedef_type =
9425 qual_type->getAs<clang::TypedefType>();
9426 if (typedef_type) {
9427 const clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
9428 std::string clang_typedef_name(
9429 typedef_decl->getQualifiedNameAsString());
9430 if (!clang_typedef_name.empty()) {
9431 s->PutCString("typedef ");
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009432 s->PutCString(clang_typedef_name);
Greg Claytond8d4a572015-08-11 21:38:15 +00009433 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009434 }
9435 } break;
9436
9437 case clang::Type::Auto:
9438 CompilerType(getASTContext(),
9439 llvm::cast<clang::AutoType>(qual_type)->getDeducedType())
9440 .DumpTypeDescription(s);
9441 return;
9442
9443 case clang::Type::Elaborated:
9444 CompilerType(getASTContext(),
9445 llvm::cast<clang::ElaboratedType>(qual_type)->getNamedType())
9446 .DumpTypeDescription(s);
9447 return;
9448
9449 case clang::Type::Paren:
9450 CompilerType(getASTContext(),
9451 llvm::cast<clang::ParenType>(qual_type)->desugar())
9452 .DumpTypeDescription(s);
9453 return;
9454
9455 case clang::Type::Record: {
9456 GetCompleteType(type);
9457
9458 const clang::RecordType *record_type =
9459 llvm::cast<clang::RecordType>(qual_type.getTypePtr());
9460 const clang::RecordDecl *record_decl = record_type->getDecl();
9461 const clang::CXXRecordDecl *cxx_record_decl =
9462 llvm::dyn_cast<clang::CXXRecordDecl>(record_decl);
9463
9464 if (cxx_record_decl)
9465 cxx_record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9466 s->GetIndentLevel());
9467 else
9468 record_decl->print(llvm_ostrm, getASTContext()->getPrintingPolicy(),
9469 s->GetIndentLevel());
9470 } break;
9471
9472 default: {
9473 const clang::TagType *tag_type =
9474 llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr());
9475 if (tag_type) {
9476 clang::TagDecl *tag_decl = tag_type->getDecl();
9477 if (tag_decl)
9478 tag_decl->print(llvm_ostrm, 0);
9479 } else {
9480 std::string clang_type_name(qual_type.getAsString());
9481 if (!clang_type_name.empty())
Malcolm Parsons771ef6d2016-11-02 20:34:10 +00009482 s->PutCString(clang_type_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +00009483 }
Greg Claytond8d4a572015-08-11 21:38:15 +00009484 }
Greg Claytone6b36cd2015-12-08 01:02:08 +00009485 }
9486
Kate Stoneb9c1b512016-09-06 20:57:50 +00009487 if (buf.size() > 0) {
9488 s->Write(buf.data(), buf.size());
Greg Clayton8b4edba2015-08-14 20:02:05 +00009489 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009490 }
Greg Clayton8b4edba2015-08-14 20:02:05 +00009491}
9492
Kate Stoneb9c1b512016-09-06 20:57:50 +00009493void ClangASTContext::DumpTypeName(const CompilerType &type) {
9494 if (ClangUtil::IsClangType(type)) {
9495 clang::QualType qual_type(
9496 ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
9497
9498 const clang::Type::TypeClass type_class = qual_type->getTypeClass();
9499 switch (type_class) {
9500 case clang::Type::Record: {
9501 const clang::CXXRecordDecl *cxx_record_decl =
9502 qual_type->getAsCXXRecordDecl();
9503 if (cxx_record_decl)
9504 printf("class %s", cxx_record_decl->getName().str().c_str());
9505 } break;
9506
9507 case clang::Type::Enum: {
9508 clang::EnumDecl *enum_decl =
9509 llvm::cast<clang::EnumType>(qual_type)->getDecl();
9510 if (enum_decl) {
9511 printf("enum %s", enum_decl->getName().str().c_str());
9512 }
9513 } break;
9514
9515 case clang::Type::ObjCObject:
9516 case clang::Type::ObjCInterface: {
9517 const clang::ObjCObjectType *objc_class_type =
9518 llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
9519 if (objc_class_type) {
9520 clang::ObjCInterfaceDecl *class_interface_decl =
9521 objc_class_type->getInterface();
9522 // We currently can't complete objective C types through the newly added
9523 // ASTContext
9524 // because it only supports TagDecl objects right now...
9525 if (class_interface_decl)
9526 printf("@class %s", class_interface_decl->getName().str().c_str());
9527 }
9528 } break;
9529
9530 case clang::Type::Typedef:
9531 printf("typedef %s", llvm::cast<clang::TypedefType>(qual_type)
9532 ->getDecl()
9533 ->getName()
9534 .str()
9535 .c_str());
9536 break;
9537
9538 case clang::Type::Auto:
9539 printf("auto ");
9540 return DumpTypeName(CompilerType(type.GetTypeSystem(),
9541 llvm::cast<clang::AutoType>(qual_type)
9542 ->getDeducedType()
9543 .getAsOpaquePtr()));
9544
9545 case clang::Type::Elaborated:
9546 printf("elaborated ");
9547 return DumpTypeName(CompilerType(
9548 type.GetTypeSystem(), llvm::cast<clang::ElaboratedType>(qual_type)
9549 ->getNamedType()
9550 .getAsOpaquePtr()));
9551
9552 case clang::Type::Paren:
9553 printf("paren ");
9554 return DumpTypeName(CompilerType(
9555 type.GetTypeSystem(),
9556 llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
9557
9558 default:
9559 printf("ClangASTContext::DumpTypeName() type_class = %u", type_class);
9560 break;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009561 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009562 }
Greg Clayton6dc8d582015-08-18 22:32:36 +00009563}
9564
Kate Stoneb9c1b512016-09-06 20:57:50 +00009565clang::ClassTemplateDecl *ClangASTContext::ParseClassTemplateDecl(
9566 clang::DeclContext *decl_ctx, lldb::AccessType access_type,
9567 const char *parent_name, int tag_decl_kind,
9568 const ClangASTContext::TemplateParameterInfos &template_param_infos) {
9569 if (template_param_infos.IsValid()) {
9570 std::string template_basename(parent_name);
9571 template_basename.erase(template_basename.find('<'));
9572
9573 return CreateClassTemplateDecl(decl_ctx, access_type,
9574 template_basename.c_str(), tag_decl_kind,
9575 template_param_infos);
9576 }
9577 return NULL;
Greg Clayton6dc8d582015-08-18 22:32:36 +00009578}
Greg Clayton8b4edba2015-08-14 20:02:05 +00009579
Kate Stoneb9c1b512016-09-06 20:57:50 +00009580void ClangASTContext::CompleteTagDecl(void *baton, clang::TagDecl *decl) {
9581 ClangASTContext *ast = (ClangASTContext *)baton;
9582 SymbolFile *sym_file = ast->GetSymbolFile();
9583 if (sym_file) {
9584 CompilerType clang_type = GetTypeForDecl(decl);
9585 if (clang_type)
9586 sym_file->CompleteType(clang_type);
9587 }
Greg Clayton261ac3f2015-08-28 01:01:03 +00009588}
9589
Kate Stoneb9c1b512016-09-06 20:57:50 +00009590void ClangASTContext::CompleteObjCInterfaceDecl(
9591 void *baton, clang::ObjCInterfaceDecl *decl) {
9592 ClangASTContext *ast = (ClangASTContext *)baton;
9593 SymbolFile *sym_file = ast->GetSymbolFile();
9594 if (sym_file) {
9595 CompilerType clang_type = GetTypeForDecl(decl);
9596 if (clang_type)
9597 sym_file->CompleteType(clang_type);
9598 }
Zachary Turner42dff792016-04-15 00:21:26 +00009599}
Greg Clayton261ac3f2015-08-28 01:01:03 +00009600
Kate Stoneb9c1b512016-09-06 20:57:50 +00009601DWARFASTParser *ClangASTContext::GetDWARFParser() {
9602 if (!m_dwarf_ast_parser_ap)
9603 m_dwarf_ast_parser_ap.reset(new DWARFASTParserClang(*this));
9604 return m_dwarf_ast_parser_ap.get();
9605}
9606
9607PDBASTParser *ClangASTContext::GetPDBParser() {
9608 if (!m_pdb_ast_parser_ap)
9609 m_pdb_ast_parser_ap.reset(new PDBASTParser(*this));
9610 return m_pdb_ast_parser_ap.get();
9611}
9612
9613bool ClangASTContext::LayoutRecordType(
9614 void *baton, const clang::RecordDecl *record_decl, uint64_t &bit_size,
9615 uint64_t &alignment,
9616 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
9617 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9618 &base_offsets,
9619 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
9620 &vbase_offsets) {
9621 ClangASTContext *ast = (ClangASTContext *)baton;
9622 DWARFASTParserClang *dwarf_ast_parser =
9623 (DWARFASTParserClang *)ast->GetDWARFParser();
9624 return dwarf_ast_parser->GetClangASTImporter().LayoutRecordType(
9625 record_decl, bit_size, alignment, field_offsets, base_offsets,
9626 vbase_offsets);
Greg Clayton8b4edba2015-08-14 20:02:05 +00009627}
9628
Greg Clayton99558cc42015-08-24 23:46:31 +00009629//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009630// CompilerDecl override functions
9631//----------------------------------------------------------------------
Paul Hermand628cbb2015-09-15 23:44:17 +00009632
Kate Stoneb9c1b512016-09-06 20:57:50 +00009633ConstString ClangASTContext::DeclGetName(void *opaque_decl) {
9634 if (opaque_decl) {
9635 clang::NamedDecl *nd =
9636 llvm::dyn_cast<NamedDecl>((clang::Decl *)opaque_decl);
9637 if (nd != nullptr)
9638 return ConstString(nd->getDeclName().getAsString());
9639 }
9640 return ConstString();
Paul Hermand628cbb2015-09-15 23:44:17 +00009641}
9642
Kate Stoneb9c1b512016-09-06 20:57:50 +00009643ConstString ClangASTContext::DeclGetMangledName(void *opaque_decl) {
9644 if (opaque_decl) {
9645 clang::NamedDecl *nd =
9646 llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)opaque_decl);
9647 if (nd != nullptr && !llvm::isa<clang::ObjCMethodDecl>(nd)) {
9648 clang::MangleContext *mc = getMangleContext();
9649 if (mc && mc->shouldMangleCXXName(nd)) {
9650 llvm::SmallVector<char, 1024> buf;
9651 llvm::raw_svector_ostream llvm_ostrm(buf);
9652 if (llvm::isa<clang::CXXConstructorDecl>(nd)) {
9653 mc->mangleCXXCtor(llvm::dyn_cast<clang::CXXConstructorDecl>(nd),
9654 Ctor_Complete, llvm_ostrm);
9655 } else if (llvm::isa<clang::CXXDestructorDecl>(nd)) {
9656 mc->mangleCXXDtor(llvm::dyn_cast<clang::CXXDestructorDecl>(nd),
9657 Dtor_Complete, llvm_ostrm);
9658 } else {
9659 mc->mangleName(nd, llvm_ostrm);
Greg Claytonfe689042015-11-10 17:47:04 +00009660 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009661 if (buf.size() > 0)
9662 return ConstString(buf.data(), buf.size());
9663 }
Greg Claytonfe689042015-11-10 17:47:04 +00009664 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009665 }
9666 return ConstString();
Greg Claytonfe689042015-11-10 17:47:04 +00009667}
9668
Kate Stoneb9c1b512016-09-06 20:57:50 +00009669CompilerDeclContext ClangASTContext::DeclGetDeclContext(void *opaque_decl) {
9670 if (opaque_decl)
9671 return CompilerDeclContext(this,
9672 ((clang::Decl *)opaque_decl)->getDeclContext());
9673 else
9674 return CompilerDeclContext();
Greg Claytonfe689042015-11-10 17:47:04 +00009675}
9676
Kate Stoneb9c1b512016-09-06 20:57:50 +00009677CompilerType ClangASTContext::DeclGetFunctionReturnType(void *opaque_decl) {
9678 if (clang::FunctionDecl *func_decl =
9679 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9680 return CompilerType(this, func_decl->getReturnType().getAsOpaquePtr());
9681 if (clang::ObjCMethodDecl *objc_method =
9682 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9683 return CompilerType(this, objc_method->getReturnType().getAsOpaquePtr());
9684 else
Greg Claytonfe689042015-11-10 17:47:04 +00009685 return CompilerType();
9686}
9687
Kate Stoneb9c1b512016-09-06 20:57:50 +00009688size_t ClangASTContext::DeclGetFunctionNumArguments(void *opaque_decl) {
9689 if (clang::FunctionDecl *func_decl =
9690 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl))
9691 return func_decl->param_size();
9692 if (clang::ObjCMethodDecl *objc_method =
9693 llvm::dyn_cast<clang::ObjCMethodDecl>((clang::Decl *)opaque_decl))
9694 return objc_method->param_size();
9695 else
9696 return 0;
9697}
9698
9699CompilerType ClangASTContext::DeclGetFunctionArgumentType(void *opaque_decl,
9700 size_t idx) {
9701 if (clang::FunctionDecl *func_decl =
9702 llvm::dyn_cast<clang::FunctionDecl>((clang::Decl *)opaque_decl)) {
9703 if (idx < func_decl->param_size()) {
9704 ParmVarDecl *var_decl = func_decl->getParamDecl(idx);
9705 if (var_decl)
9706 return CompilerType(this, var_decl->getOriginalType().getAsOpaquePtr());
9707 }
9708 } else if (clang::ObjCMethodDecl *objc_method =
9709 llvm::dyn_cast<clang::ObjCMethodDecl>(
9710 (clang::Decl *)opaque_decl)) {
9711 if (idx < objc_method->param_size())
9712 return CompilerType(
9713 this,
9714 objc_method->parameters()[idx]->getOriginalType().getAsOpaquePtr());
9715 }
9716 return CompilerType();
9717}
9718
Paul Hermand628cbb2015-09-15 23:44:17 +00009719//----------------------------------------------------------------------
Greg Clayton99558cc42015-08-24 23:46:31 +00009720// CompilerDeclContext functions
9721//----------------------------------------------------------------------
9722
Kate Stoneb9c1b512016-09-06 20:57:50 +00009723std::vector<CompilerDecl> ClangASTContext::DeclContextFindDeclByName(
9724 void *opaque_decl_ctx, ConstString name, const bool ignore_using_decls) {
9725 std::vector<CompilerDecl> found_decls;
9726 if (opaque_decl_ctx) {
9727 DeclContext *root_decl_ctx = (DeclContext *)opaque_decl_ctx;
9728 std::set<DeclContext *> searched;
9729 std::multimap<DeclContext *, DeclContext *> search_queue;
9730 SymbolFile *symbol_file = GetSymbolFile();
Paul Hermand628cbb2015-09-15 23:44:17 +00009731
Kate Stoneb9c1b512016-09-06 20:57:50 +00009732 for (clang::DeclContext *decl_context = root_decl_ctx;
9733 decl_context != nullptr && found_decls.empty();
9734 decl_context = decl_context->getParent()) {
9735 search_queue.insert(std::make_pair(decl_context, decl_context));
Paul Hermand628cbb2015-09-15 23:44:17 +00009736
Kate Stoneb9c1b512016-09-06 20:57:50 +00009737 for (auto it = search_queue.find(decl_context); it != search_queue.end();
9738 it++) {
9739 if (!searched.insert(it->second).second)
9740 continue;
9741 symbol_file->ParseDeclsForContext(
9742 CompilerDeclContext(this, it->second));
Paul Hermanea188fc2015-09-16 18:48:30 +00009743
Kate Stoneb9c1b512016-09-06 20:57:50 +00009744 for (clang::Decl *child : it->second->decls()) {
9745 if (clang::UsingDirectiveDecl *ud =
9746 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9747 if (ignore_using_decls)
9748 continue;
9749 clang::DeclContext *from = ud->getCommonAncestor();
9750 if (searched.find(ud->getNominatedNamespace()) == searched.end())
9751 search_queue.insert(
9752 std::make_pair(from, ud->getNominatedNamespace()));
9753 } else if (clang::UsingDecl *ud =
9754 llvm::dyn_cast<clang::UsingDecl>(child)) {
9755 if (ignore_using_decls)
9756 continue;
9757 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9758 clang::Decl *target = usd->getTargetDecl();
9759 if (clang::NamedDecl *nd =
9760 llvm::dyn_cast<clang::NamedDecl>(target)) {
9761 IdentifierInfo *ii = nd->getIdentifier();
9762 if (ii != nullptr &&
9763 ii->getName().equals(name.AsCString(nullptr)))
9764 found_decls.push_back(CompilerDecl(this, nd));
9765 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009766 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009767 } else if (clang::NamedDecl *nd =
9768 llvm::dyn_cast<clang::NamedDecl>(child)) {
9769 IdentifierInfo *ii = nd->getIdentifier();
9770 if (ii != nullptr && ii->getName().equals(name.AsCString(nullptr)))
9771 found_decls.push_back(CompilerDecl(this, nd));
9772 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009773 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009774 }
Paul Hermand628cbb2015-09-15 23:44:17 +00009775 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009776 }
9777 return found_decls;
Paul Hermand628cbb2015-09-15 23:44:17 +00009778}
9779
Dawn Perchikb5925782015-12-12 19:31:41 +00009780// Look for child_decl_ctx's lookup scope in frame_decl_ctx and its parents,
Kate Stoneb9c1b512016-09-06 20:57:50 +00009781// and return the number of levels it took to find it, or
9782// LLDB_INVALID_DECL_LEVEL
9783// if not found. If the decl was imported via a using declaration, its name
9784// and/or
9785// type, if set, will be used to check that the decl found in the scope is a
9786// match.
Dawn Perchikb5925782015-12-12 19:31:41 +00009787//
Kate Stoneb9c1b512016-09-06 20:57:50 +00009788// The optional name is required by languages (like C++) to handle using
9789// declarations
Dawn Perchikb5925782015-12-12 19:31:41 +00009790// like:
9791//
9792// void poo();
9793// namespace ns {
9794// void foo();
9795// void goo();
9796// }
9797// void bar() {
9798// using ns::foo;
9799// // CountDeclLevels returns 0 for 'foo', 1 for 'poo', and
9800// // LLDB_INVALID_DECL_LEVEL for 'goo'.
9801// }
9802//
9803// The optional type is useful in the case that there's a specific overload
9804// that we're looking for that might otherwise be shadowed, like:
9805//
9806// void foo(int);
9807// namespace ns {
9808// void foo();
9809// }
9810// void bar() {
9811// using ns::foo;
9812// // CountDeclLevels returns 0 for { 'foo', void() },
9813// // 1 for { 'foo', void(int) }, and
9814// // LLDB_INVALID_DECL_LEVEL for { 'foo', void(int, int) }.
9815// }
9816//
9817// NOTE: Because file statics are at the TranslationUnit along with globals, a
Kate Stoneb9c1b512016-09-06 20:57:50 +00009818// function at file scope will return the same level as a function at global
9819// scope.
9820// Ideally we'd like to treat the file scope as an additional scope just below
9821// the
9822// global scope. More work needs to be done to recognise that, if the decl
9823// we're
9824// trying to look up is static, we should compare its source file with that of
9825// the
Dawn Perchikb5925782015-12-12 19:31:41 +00009826// current scope and return a lower number for it.
Kate Stoneb9c1b512016-09-06 20:57:50 +00009827uint32_t ClangASTContext::CountDeclLevels(clang::DeclContext *frame_decl_ctx,
9828 clang::DeclContext *child_decl_ctx,
9829 ConstString *child_name,
9830 CompilerType *child_type) {
9831 if (frame_decl_ctx) {
9832 std::set<DeclContext *> searched;
9833 std::multimap<DeclContext *, DeclContext *> search_queue;
9834 SymbolFile *symbol_file = GetSymbolFile();
Dawn Perchikb5925782015-12-12 19:31:41 +00009835
Kate Stoneb9c1b512016-09-06 20:57:50 +00009836 // Get the lookup scope for the decl we're trying to find.
9837 clang::DeclContext *parent_decl_ctx = child_decl_ctx->getParent();
Dawn Perchikb5925782015-12-12 19:31:41 +00009838
Kate Stoneb9c1b512016-09-06 20:57:50 +00009839 // Look for it in our scope's decl context and its parents.
9840 uint32_t level = 0;
9841 for (clang::DeclContext *decl_ctx = frame_decl_ctx; decl_ctx != nullptr;
9842 decl_ctx = decl_ctx->getParent()) {
9843 if (!decl_ctx->isLookupContext())
9844 continue;
9845 if (decl_ctx == parent_decl_ctx)
9846 // Found it!
9847 return level;
9848 search_queue.insert(std::make_pair(decl_ctx, decl_ctx));
9849 for (auto it = search_queue.find(decl_ctx); it != search_queue.end();
9850 it++) {
9851 if (searched.find(it->second) != searched.end())
9852 continue;
9853
9854 // Currently DWARF has one shared translation unit for all Decls at top
9855 // level, so this
9856 // would erroneously find using statements anywhere. So don't look at
9857 // the top-level
9858 // translation unit.
9859 // TODO fix this and add a testcase that depends on it.
9860
9861 if (llvm::isa<clang::TranslationUnitDecl>(it->second))
9862 continue;
9863
9864 searched.insert(it->second);
9865 symbol_file->ParseDeclsForContext(
9866 CompilerDeclContext(this, it->second));
9867
9868 for (clang::Decl *child : it->second->decls()) {
9869 if (clang::UsingDirectiveDecl *ud =
9870 llvm::dyn_cast<clang::UsingDirectiveDecl>(child)) {
9871 clang::DeclContext *ns = ud->getNominatedNamespace();
9872 if (ns == parent_decl_ctx)
9873 // Found it!
9874 return level;
9875 clang::DeclContext *from = ud->getCommonAncestor();
9876 if (searched.find(ns) == searched.end())
9877 search_queue.insert(std::make_pair(from, ns));
9878 } else if (child_name) {
9879 if (clang::UsingDecl *ud =
9880 llvm::dyn_cast<clang::UsingDecl>(child)) {
9881 for (clang::UsingShadowDecl *usd : ud->shadows()) {
9882 clang::Decl *target = usd->getTargetDecl();
9883 clang::NamedDecl *nd = llvm::dyn_cast<clang::NamedDecl>(target);
9884 if (!nd)
9885 continue;
9886 // Check names.
9887 IdentifierInfo *ii = nd->getIdentifier();
9888 if (ii == nullptr ||
9889 !ii->getName().equals(child_name->AsCString(nullptr)))
9890 continue;
9891 // Check types, if one was provided.
9892 if (child_type) {
9893 CompilerType clang_type = ClangASTContext::GetTypeForDecl(nd);
9894 if (!AreTypesSame(clang_type, *child_type,
9895 /*ignore_qualifiers=*/true))
9896 continue;
9897 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009898 // Found it!
9899 return level;
Kate Stoneb9c1b512016-09-06 20:57:50 +00009900 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009901 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009902 }
Dawn Perchikb5925782015-12-12 19:31:41 +00009903 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009904 }
9905 ++level;
Dawn Perchikb5925782015-12-12 19:31:41 +00009906 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00009907 }
9908 return LLDB_INVALID_DECL_LEVEL;
Dawn Perchikb5925782015-12-12 19:31:41 +00009909}
9910
Kate Stoneb9c1b512016-09-06 20:57:50 +00009911bool ClangASTContext::DeclContextIsStructUnionOrClass(void *opaque_decl_ctx) {
9912 if (opaque_decl_ctx)
9913 return ((clang::DeclContext *)opaque_decl_ctx)->isRecord();
9914 else
Greg Clayton99558cc42015-08-24 23:46:31 +00009915 return false;
9916}
9917
Kate Stoneb9c1b512016-09-06 20:57:50 +00009918ConstString ClangASTContext::DeclContextGetName(void *opaque_decl_ctx) {
9919 if (opaque_decl_ctx) {
9920 clang::NamedDecl *named_decl =
9921 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9922 if (named_decl)
9923 return ConstString(named_decl->getName());
9924 }
9925 return ConstString();
Greg Clayton99558cc42015-08-24 23:46:31 +00009926}
9927
Kate Stoneb9c1b512016-09-06 20:57:50 +00009928ConstString
9929ClangASTContext::DeclContextGetScopeQualifiedName(void *opaque_decl_ctx) {
9930 if (opaque_decl_ctx) {
9931 clang::NamedDecl *named_decl =
9932 llvm::dyn_cast<clang::NamedDecl>((clang::DeclContext *)opaque_decl_ctx);
9933 if (named_decl)
9934 return ConstString(
9935 llvm::StringRef(named_decl->getQualifiedNameAsString()));
9936 }
9937 return ConstString();
9938}
9939
9940bool ClangASTContext::DeclContextIsClassMethod(
9941 void *opaque_decl_ctx, lldb::LanguageType *language_ptr,
9942 bool *is_instance_method_ptr, ConstString *language_object_name_ptr) {
9943 if (opaque_decl_ctx) {
9944 clang::DeclContext *decl_ctx = (clang::DeclContext *)opaque_decl_ctx;
9945 if (ObjCMethodDecl *objc_method =
9946 llvm::dyn_cast<clang::ObjCMethodDecl>(decl_ctx)) {
9947 if (is_instance_method_ptr)
9948 *is_instance_method_ptr = objc_method->isInstanceMethod();
9949 if (language_ptr)
9950 *language_ptr = eLanguageTypeObjC;
9951 if (language_object_name_ptr)
9952 language_object_name_ptr->SetCString("self");
9953 return true;
9954 } else if (CXXMethodDecl *cxx_method =
9955 llvm::dyn_cast<clang::CXXMethodDecl>(decl_ctx)) {
9956 if (is_instance_method_ptr)
9957 *is_instance_method_ptr = cxx_method->isInstance();
9958 if (language_ptr)
9959 *language_ptr = eLanguageTypeC_plus_plus;
9960 if (language_object_name_ptr)
9961 language_object_name_ptr->SetCString("this");
9962 return true;
9963 } else if (clang::FunctionDecl *function_decl =
9964 llvm::dyn_cast<clang::FunctionDecl>(decl_ctx)) {
9965 ClangASTMetadata *metadata =
9966 GetMetadata(&decl_ctx->getParentASTContext(), function_decl);
9967 if (metadata && metadata->HasObjectPtr()) {
9968 if (is_instance_method_ptr)
9969 *is_instance_method_ptr = true;
9970 if (language_ptr)
9971 *language_ptr = eLanguageTypeObjC;
9972 if (language_object_name_ptr)
9973 language_object_name_ptr->SetCString(metadata->GetObjectPtrName());
9974 return true;
9975 }
9976 }
9977 }
9978 return false;
9979}
9980
9981clang::DeclContext *
9982ClangASTContext::DeclContextGetAsDeclContext(const CompilerDeclContext &dc) {
9983 if (dc.IsClang())
9984 return (clang::DeclContext *)dc.GetOpaqueDeclContext();
9985 return nullptr;
9986}
Greg Clayton99558cc42015-08-24 23:46:31 +00009987
9988ObjCMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00009989ClangASTContext::DeclContextGetAsObjCMethodDecl(const CompilerDeclContext &dc) {
9990 if (dc.IsClang())
9991 return llvm::dyn_cast<clang::ObjCMethodDecl>(
9992 (clang::DeclContext *)dc.GetOpaqueDeclContext());
9993 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +00009994}
9995
9996CXXMethodDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +00009997ClangASTContext::DeclContextGetAsCXXMethodDecl(const CompilerDeclContext &dc) {
9998 if (dc.IsClang())
9999 return llvm::dyn_cast<clang::CXXMethodDecl>(
10000 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10001 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010002}
10003
10004clang::FunctionDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010005ClangASTContext::DeclContextGetAsFunctionDecl(const CompilerDeclContext &dc) {
10006 if (dc.IsClang())
10007 return llvm::dyn_cast<clang::FunctionDecl>(
10008 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10009 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010010}
10011
10012clang::NamespaceDecl *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010013ClangASTContext::DeclContextGetAsNamespaceDecl(const CompilerDeclContext &dc) {
10014 if (dc.IsClang())
10015 return llvm::dyn_cast<clang::NamespaceDecl>(
10016 (clang::DeclContext *)dc.GetOpaqueDeclContext());
10017 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010018}
10019
10020ClangASTMetadata *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010021ClangASTContext::DeclContextGetMetaData(const CompilerDeclContext &dc,
10022 const void *object) {
10023 clang::ASTContext *ast = DeclContextGetClangASTContext(dc);
10024 if (ast)
10025 return ClangASTContext::GetMetadata(ast, object);
10026 return nullptr;
Greg Clayton99558cc42015-08-24 23:46:31 +000010027}
10028
10029clang::ASTContext *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010030ClangASTContext::DeclContextGetClangASTContext(const CompilerDeclContext &dc) {
10031 ClangASTContext *ast =
10032 llvm::dyn_cast_or_null<ClangASTContext>(dc.GetTypeSystem());
10033 if (ast)
10034 return ast->getASTContext();
10035 return nullptr;
10036}
10037
10038ClangASTContextForExpressions::ClangASTContextForExpressions(Target &target)
10039 : ClangASTContext(target.GetArchitecture().GetTriple().getTriple().c_str()),
10040 m_target_wp(target.shared_from_this()),
10041 m_persistent_variables(new ClangPersistentVariables) {}
10042
10043UserExpression *ClangASTContextForExpressions::GetUserExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010044 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010045 Expression::ResultType desired_type,
10046 const EvaluateExpressionOptions &options) {
10047 TargetSP target_sp = m_target_wp.lock();
10048 if (!target_sp)
Greg Clayton99558cc42015-08-24 23:46:31 +000010049 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +000010050
Zachary Turnerc5d7df92016-11-08 04:52:16 +000010051 return new ClangUserExpression(*target_sp.get(), expr, prefix, language,
Kate Stoneb9c1b512016-09-06 20:57:50 +000010052 desired_type, options);
Greg Clayton8b4edba2015-08-14 20:02:05 +000010053}
10054
Kate Stoneb9c1b512016-09-06 20:57:50 +000010055FunctionCaller *ClangASTContextForExpressions::GetFunctionCaller(
10056 const CompilerType &return_type, const Address &function_address,
10057 const ValueList &arg_value_list, const char *name) {
10058 TargetSP target_sp = m_target_wp.lock();
10059 if (!target_sp)
10060 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010061
Kate Stoneb9c1b512016-09-06 20:57:50 +000010062 Process *process = target_sp->GetProcessSP().get();
10063 if (!process)
10064 return nullptr;
Jim Ingham151c0322015-09-15 21:13:50 +000010065
Kate Stoneb9c1b512016-09-06 20:57:50 +000010066 return new ClangFunctionCaller(*process, return_type, function_address,
10067 arg_value_list, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010068}
10069
10070UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010071ClangASTContextForExpressions::GetUtilityFunction(const char *text,
10072 const char *name) {
10073 TargetSP target_sp = m_target_wp.lock();
10074 if (!target_sp)
10075 return nullptr;
10076
10077 return new ClangUtilityFunction(*target_sp.get(), text, name);
Jim Ingham151c0322015-09-15 21:13:50 +000010078}
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010079
10080PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +000010081ClangASTContextForExpressions::GetPersistentExpressionState() {
10082 return m_persistent_variables.get();
Sean Callanan8f1f9a12015-09-30 19:57:57 +000010083}