Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- DWARFASTParserClang.cpp -------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include <cstdlib>
10
11
#include "DWARFASTParserClang.h"
12
#include "DWARFDebugInfo.h"
13
#include "DWARFDeclContext.h"
14
#include "DWARFDefines.h"
15
#include "SymbolFileDWARF.h"
16
#include "SymbolFileDWARFDebugMap.h"
17
#include "SymbolFileDWARFDwo.h"
18
#include "UniqueDWARFASTType.h"
19
20
#include "Plugins/ExpressionParser/Clang/ClangASTImporter.h"
21
#include "Plugins/ExpressionParser/Clang/ClangASTMetadata.h"
22
#include "Plugins/ExpressionParser/Clang/ClangUtil.h"
23
#include "Plugins/Language/ObjC/ObjCLanguage.h"
24
#include "lldb/Core/Module.h"
25
#include "lldb/Core/Value.h"
26
#include "lldb/Host/Host.h"
27
#include "lldb/Symbol/CompileUnit.h"
28
#include "lldb/Symbol/Function.h"
29
#include "lldb/Symbol/ObjectFile.h"
30
#include "lldb/Symbol/SymbolFile.h"
31
#include "lldb/Symbol/TypeList.h"
32
#include "lldb/Symbol/TypeMap.h"
33
#include "lldb/Target/Language.h"
34
#include "lldb/Utility/LLDBAssert.h"
35
#include "lldb/Utility/Log.h"
36
#include "lldb/Utility/StreamString.h"
37
38
#include "llvm/Demangle/Demangle.h"
39
40
#include "clang/AST/CXXInheritance.h"
41
#include "clang/AST/DeclCXX.h"
42
#include "clang/AST/DeclObjC.h"
43
#include "clang/AST/DeclTemplate.h"
44
45
#include <map>
46
#include <memory>
47
#include <vector>
48
49
//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
50
51
#ifdef ENABLE_DEBUG_PRINTF
52
#include <cstdio>
53
#define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
54
#else
55
#define DEBUG_PRINTF(fmt, ...)
56
#endif
57
58
using namespace lldb;
59
using namespace lldb_private;
60
DWARFASTParserClang::DWARFASTParserClang(TypeSystemClang &ast)
61
2.92k
    : m_ast(ast), m_die_to_decl_ctx(), m_decl_ctx_to_die() {}
62
63
2.81k
DWARFASTParserClang::~DWARFASTParserClang() = default;
64
65
46.8k
static AccessType DW_ACCESS_to_AccessType(uint32_t dwarf_accessibility) {
66
46.8k
  switch (dwarf_accessibility) {
67
42.7k
  case DW_ACCESS_public:
68
42.7k
    return eAccessPublic;
69
1.27k
  case DW_ACCESS_private:
70
1.27k
    return eAccessPrivate;
71
2.84k
  case DW_ACCESS_protected:
72
2.84k
    return eAccessProtected;
73
0
  default:
74
0
    break;
75
46.8k
  }
76
0
  return eAccessNone;
77
46.8k
}
78
79
141k
static bool DeclKindIsCXXClass(clang::Decl::Kind decl_kind) {
80
141k
  switch (decl_kind) {
81
5.12k
  case clang::Decl::CXXRecord:
82
124k
  case clang::Decl::ClassTemplateSpecialization:
83
124k
    return true;
84
17.3k
  default:
85
17.3k
    break;
86
141k
  }
87
17.3k
  return false;
88
141k
}
89
90
91
94.6k
ClangASTImporter &DWARFASTParserClang::GetClangASTImporter() {
92
94.6k
  if (!m_clang_ast_importer_up) {
93
1.77k
    m_clang_ast_importer_up = std::make_unique<ClangASTImporter>();
94
1.77k
  }
95
94.6k
  return *m_clang_ast_importer_up;
96
94.6k
}
97
98
/// Detect a forward declaration that is nested in a DW_TAG_module.
99
60.3k
static bool IsClangModuleFwdDecl(const DWARFDIE &Die) {
100
60.3k
  if (!Die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0))
101
60.3k
    return false;
102
2
  auto Parent = Die.GetParent();
103
2
  while (Parent.IsValid()) {
104
2
    if (Parent.Tag() == DW_TAG_module)
105
2
      return true;
106
0
    Parent = Parent.GetParent();
107
0
  }
108
0
  return false;
109
2
}
110
111
3.37k
static DWARFDIE GetContainingClangModuleDIE(const DWARFDIE &die) {
112
3.37k
  if (die.IsValid()) {
113
3.37k
    DWARFDIE top_module_die;
114
    // Now make sure this DIE is scoped in a DW_TAG_module tag and return true
115
    // if so
116
13.4k
    for (DWARFDIE parent = die.GetParent(); parent.IsValid();
117
13.4k
         
parent = parent.GetParent()10.0k
) {
118
13.4k
      const dw_tag_t tag = parent.Tag();
119
13.4k
      if (tag == DW_TAG_module)
120
3.59k
        top_module_die = parent;
121
9.82k
      else if (tag == DW_TAG_compile_unit || 
tag == DW_TAG_partial_unit6.44k
)
122
3.37k
        break;
123
13.4k
    }
124
125
3.37k
    return top_module_die;
126
3.37k
  }
127
0
  return DWARFDIE();
128
3.37k
}
129
130
3.37k
static lldb::ModuleSP GetContainingClangModule(const DWARFDIE &die) {
131
3.37k
  if (die.IsValid()) {
132
3.37k
    DWARFDIE clang_module_die = GetContainingClangModuleDIE(die);
133
134
3.37k
    if (clang_module_die) {
135
1.40k
      const char *module_name = clang_module_die.GetName();
136
1.40k
      if (module_name)
137
1.40k
        return die.GetDWARF()->GetExternalModule(
138
1.40k
            lldb_private::ConstString(module_name));
139
1.40k
    }
140
3.37k
  }
141
1.97k
  return lldb::ModuleSP();
142
3.37k
}
143
144
TypeSP DWARFASTParserClang::ParseTypeFromClangModule(const SymbolContext &sc,
145
                                                     const DWARFDIE &die,
146
3.37k
                                                     Log *log) {
147
3.37k
  ModuleSP clang_module_sp = GetContainingClangModule(die);
148
3.37k
  if (!clang_module_sp)
149
2.15k
    return TypeSP();
150
151
  // If this type comes from a Clang module, recursively look in the
152
  // DWARF section of the .pcm file in the module cache. Clang
153
  // generates DWO skeleton units as breadcrumbs to find them.
154
1.21k
  llvm::SmallVector<CompilerContext, 4> decl_context;
155
1.21k
  die.GetDeclContext(decl_context);
156
1.21k
  TypeMap pcm_types;
157
158
  // The type in the Clang module must have the same language as the current CU.
159
1.21k
  LanguageSet languages;
160
1.21k
  languages.Insert(SymbolFileDWARF::GetLanguageFamily(*die.GetCU()));
161
1.21k
  llvm::DenseSet<SymbolFile *> searched_symbol_files;
162
1.21k
  clang_module_sp->GetSymbolFile()->FindTypes(decl_context, languages,
163
1.21k
                                              searched_symbol_files, pcm_types);
164
1.21k
  if (pcm_types.Empty()) {
165
    // Since this type is defined in one of the Clang modules imported
166
    // by this symbol file, search all of them. Instead of calling
167
    // sym_file->FindTypes(), which would return this again, go straight
168
    // to the imported modules.
169
554
    auto &sym_file = die.GetCU()->GetSymbolFileDWARF();
170
171
    // Well-formed clang modules never form cycles; guard against corrupted
172
    // ones by inserting the current file.
173
554
    searched_symbol_files.insert(&sym_file);
174
554
    sym_file.ForEachExternalModule(
175
554
        *sc.comp_unit, searched_symbol_files, [&](Module &module) {
176
0
          module.GetSymbolFile()->FindTypes(decl_context, languages,
177
0
                                            searched_symbol_files, pcm_types);
178
0
          return pcm_types.GetSize();
179
0
        });
180
554
  }
181
182
1.21k
  if (!pcm_types.GetSize())
183
554
    return TypeSP();
184
185
  // We found a real definition for this type in the Clang module, so lets use
186
  // it and cache the fact that we found a complete type for this die.
187
665
  TypeSP pcm_type_sp = pcm_types.GetTypeAtIndex(0);
188
665
  if (!pcm_type_sp)
189
0
    return TypeSP();
190
191
665
  lldb_private::CompilerType pcm_type = pcm_type_sp->GetForwardCompilerType();
192
665
  lldb_private::CompilerType type =
193
665
      GetClangASTImporter().CopyType(m_ast, pcm_type);
194
195
665
  if (!type)
196
0
    return TypeSP();
197
198
  // Under normal operation pcm_type is a shallow forward declaration
199
  // that gets completed later. This is necessary to support cyclic
200
  // data structures. If, however, pcm_type is already complete (for
201
  // example, because it was loaded for a different target before),
202
  // the definition needs to be imported right away, too.
203
  // Type::ResolveClangType() effectively ignores the ResolveState
204
  // inside type_sp and only looks at IsDefined(), so it never calls
205
  // ClangASTImporter::ASTImporterDelegate::ImportDefinitionTo(),
206
  // which does extra work for Objective-C classes. This would result
207
  // in only the forward declaration to be visible.
208
665
  if (pcm_type.IsDefined())
209
303
    GetClangASTImporter().RequireCompleteType(ClangUtil::GetQualType(type));
210
211
665
  SymbolFileDWARF *dwarf = die.GetDWARF();
212
665
  TypeSP type_sp(new Type(die.GetID(), dwarf, pcm_type_sp->GetName(),
213
665
                          pcm_type_sp->GetByteSize(nullptr), nullptr,
214
665
                          LLDB_INVALID_UID, Type::eEncodingInvalid,
215
665
                          &pcm_type_sp->GetDeclaration(), type,
216
665
                          Type::ResolveState::Forward,
217
665
                          TypePayloadClang(GetOwningClangModule(die))));
218
219
665
  dwarf->GetTypeList().Insert(type_sp);
220
665
  dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
221
665
  clang::TagDecl *tag_decl = TypeSystemClang::GetAsTagDecl(type);
222
665
  if (tag_decl) {
223
340
    LinkDeclContextToDIE(tag_decl, die);
224
340
  } else {
225
325
    clang::DeclContext *defn_decl_ctx = GetCachedClangDeclContextForDIE(die);
226
325
    if (defn_decl_ctx)
227
0
      LinkDeclContextToDIE(defn_decl_ctx, die);
228
325
  }
229
230
665
  return type_sp;
231
665
}
232
233
54
static void ForcefullyCompleteType(CompilerType type) {
234
54
  bool started = TypeSystemClang::StartTagDeclarationDefinition(type);
235
54
  lldbassert(started && "Unable to start a class type definition.");
236
54
  TypeSystemClang::CompleteTagDeclarationDefinition(type);
237
54
  const clang::TagDecl *td = ClangUtil::GetAsTagDecl(type);
238
54
  auto &ts = llvm::cast<TypeSystemClang>(*type.GetTypeSystem());
239
54
  ts.GetMetadata(td)->SetIsForcefullyCompleted();
240
54
}
241
242
/// Complete a type from debug info, or mark it as forcefully completed if
243
/// there is no definition of the type in the current Module. Call this function
244
/// in contexts where the usual C++ rules require a type to be complete (base
245
/// class, member, etc.).
246
11.8k
static void RequireCompleteType(CompilerType type) {
247
  // Technically, enums can be incomplete too, but we don't handle those as they
248
  // are emitted even under -flimit-debug-info.
249
11.8k
  if (!TypeSystemClang::IsCXXClassType(type))
250
6.94k
    return;
251
252
4.89k
  if (type.GetCompleteType())
253
4.84k
    return;
254
255
  // No complete definition in this module.  Mark the class as complete to
256
  // satisfy local ast invariants, but make a note of the fact that
257
  // it is not _really_ complete so we can later search for a definition in a
258
  // different module.
259
  // Since we provide layout assistance, layouts of types containing this class
260
  // will be correct even if we  are not able to find the definition elsewhere.
261
52
  ForcefullyCompleteType(type);
262
52
}
263
264
/// This function serves a similar purpose as RequireCompleteType above, but it
265
/// avoids completing the type if it is not immediately necessary. It only
266
/// ensures we _can_ complete the type later.
267
static void PrepareContextToReceiveMembers(TypeSystemClang &ast,
268
                                           ClangASTImporter &ast_importer,
269
                                           clang::DeclContext *decl_ctx,
270
                                           DWARFDIE die,
271
29.6k
                                           const char *type_name_cstr) {
272
29.6k
  auto *tag_decl_ctx = clang::dyn_cast<clang::TagDecl>(decl_ctx);
273
29.6k
  if (!tag_decl_ctx)
274
16.6k
    return; // Non-tag context are always ready.
275
276
  // We have already completed the type, or we have found its definition and are
277
  // ready to complete it later (cf. ParseStructureLikeDIE).
278
13.0k
  if (tag_decl_ctx->isCompleteDefinition() || 
tag_decl_ctx->isBeingDefined()12.7k
)
279
13.0k
    return;
280
281
  // We reach this point of the tag was present in the debug info as a
282
  // declaration only. If it was imported from another AST context (in the
283
  // gmodules case), we can complete the type by doing a full import.
284
285
  // If this type was not imported from an external AST, there's nothing to do.
286
3
  CompilerType type = ast.GetTypeForDecl(tag_decl_ctx);
287
3
  if (type && ast_importer.CanImport(type)) {
288
1
    auto qual_type = ClangUtil::GetQualType(type);
289
1
    if (ast_importer.RequireCompleteType(qual_type))
290
1
      return;
291
0
    die.GetDWARF()->GetObjectFile()->GetModule()->ReportError(
292
0
        "Unable to complete the Decl context for DIE '%s' at offset "
293
0
        "0x%8.8x.\nPlease file a bug report.",
294
0
        type_name_cstr ? type_name_cstr : "", die.GetOffset());
295
0
  }
296
297
  // We don't have a type definition and/or the import failed. We must
298
  // forcefully complete the type to avoid crashes.
299
2
  ForcefullyCompleteType(type);
300
2
}
301
302
138k
ParsedDWARFTypeAttributes::ParsedDWARFTypeAttributes(const DWARFDIE &die) {
303
138k
  DWARFAttributes attributes;
304
138k
  size_t num_attributes = die.GetAttributes(attributes);
305
794k
  for (size_t i = 0; i < num_attributes; 
++i656k
) {
306
656k
    dw_attr_t attr = attributes.AttributeAtIndex(i);
307
656k
    DWARFFormValue form_value;
308
656k
    if (!attributes.ExtractFormValueAtIndex(i, form_value))
309
0
      continue;
310
656k
    switch (attr) {
311
35
    case DW_AT_abstract_origin:
312
35
      abstract_origin = form_value;
313
35
      break;
314
315
44.0k
    case DW_AT_accessibility:
316
44.0k
      accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
317
44.0k
      break;
318
319
826
    case DW_AT_artificial:
320
826
      is_artificial = form_value.Boolean();
321
826
      break;
322
323
0
    case DW_AT_bit_stride:
324
0
      bit_stride = form_value.Unsigned();
325
0
      break;
326
327
18.0k
    case DW_AT_byte_size:
328
18.0k
      byte_size = form_value.Unsigned();
329
18.0k
      break;
330
331
0
    case DW_AT_byte_stride:
332
0
      byte_stride = form_value.Unsigned();
333
0
      break;
334
335
10.3k
    case DW_AT_calling_convention:
336
10.3k
      calling_convention = form_value.Unsigned();
337
10.3k
      break;
338
339
634
    case DW_AT_containing_type:
340
634
      containing_type = form_value;
341
634
      break;
342
343
93.0k
    case DW_AT_decl_file:
344
      // die.GetCU() can differ if DW_AT_specification uses DW_FORM_ref_addr.
345
93.0k
      decl.SetFile(
346
93.0k
          attributes.CompileUnitAtIndex(i)->GetFile(form_value.Unsigned()));
347
93.0k
      break;
348
93.0k
    case DW_AT_decl_line:
349
93.0k
      decl.SetLine(form_value.Unsigned());
350
93.0k
      break;
351
0
    case DW_AT_decl_column:
352
0
      decl.SetColumn(form_value.Unsigned());
353
0
      break;
354
355
64.6k
    case DW_AT_declaration:
356
64.6k
      is_forward_declaration = form_value.Boolean();
357
64.6k
      break;
358
359
5.65k
    case DW_AT_encoding:
360
5.65k
      encoding = form_value.Unsigned();
361
5.65k
      break;
362
363
42
    case DW_AT_enum_class:
364
42
      is_scoped_enum = form_value.Boolean();
365
42
      break;
366
367
1.59k
    case DW_AT_explicit:
368
1.59k
      is_explicit = form_value.Boolean();
369
1.59k
      break;
370
371
64.9k
    case DW_AT_external:
372
64.9k
      if (form_value.Unsigned())
373
64.9k
        storage = clang::SC_Extern;
374
64.9k
      break;
375
376
61
    case DW_AT_inline:
377
61
      is_inline = form_value.Boolean();
378
61
      break;
379
380
51.3k
    case DW_AT_linkage_name:
381
51.3k
    case DW_AT_MIPS_linkage_name:
382
51.3k
      mangled_name = form_value.AsCString();
383
51.3k
      break;
384
385
101k
    case DW_AT_name:
386
101k
      name.SetCString(form_value.AsCString());
387
101k
      break;
388
389
848
    case DW_AT_object_pointer:
390
848
      object_pointer = form_value.Reference();
391
848
      break;
392
393
12
    case DW_AT_signature:
394
12
      signature = form_value;
395
12
      break;
396
397
134
    case DW_AT_specification:
398
134
      specification = form_value;
399
134
      break;
400
401
90.6k
    case DW_AT_type:
402
90.6k
      type = form_value;
403
90.6k
      break;
404
405
288
    case DW_AT_virtuality:
406
288
      is_virtual = form_value.Boolean();
407
288
      break;
408
409
292
    case DW_AT_APPLE_objc_complete_type:
410
292
      is_complete_objc_class = form_value.Signed();
411
292
      break;
412
413
42
    case DW_AT_APPLE_objc_direct:
414
42
      is_objc_direct_call = true;
415
42
      break;
416
417
1.41k
    case DW_AT_APPLE_runtime_class:
418
1.41k
      class_language = (LanguageType)form_value.Signed();
419
1.41k
      break;
420
421
43
    case DW_AT_GNU_vector:
422
43
      is_vector = form_value.Boolean();
423
43
      break;
424
216
    case DW_AT_export_symbols:
425
216
      exports_symbols = form_value.Boolean();
426
216
      break;
427
656k
    }
428
656k
  }
429
138k
}
430
431
0
static std::string GetUnitName(const DWARFDIE &die) {
432
0
  if (DWARFUnit *unit = die.GetCU())
433
0
    return unit->GetAbsolutePath().GetPath();
434
0
  return "<missing DWARF unit path>";
435
0
}
436
437
TypeSP DWARFASTParserClang::ParseTypeFromDWARF(const SymbolContext &sc,
438
                                               const DWARFDIE &die,
439
138k
                                               bool *type_is_new_ptr) {
440
138k
  if (type_is_new_ptr)
441
105
    *type_is_new_ptr = false;
442
443
138k
  if (!die)
444
0
    return nullptr;
445
446
138k
  Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
447
138k
                                        DWARF_LOG_LOOKUPS));
448
449
138k
  SymbolFileDWARF *dwarf = die.GetDWARF();
450
138k
  if (log) {
451
6
    DWARFDIE context_die;
452
6
    clang::DeclContext *context =
453
6
        GetClangDeclContextContainingDIE(die, &context_die);
454
455
6
    dwarf->GetObjectFile()->GetModule()->LogMessage(
456
6
        log,
457
6
        "DWARFASTParserClang::ParseTypeFromDWARF "
458
6
        "(die = 0x%8.8x, decl_ctx = %p (die 0x%8.8x)) %s name = '%s')",
459
6
        die.GetOffset(), static_cast<void *>(context), context_die.GetOffset(),
460
6
        die.GetTagAsCString(), die.GetName());
461
6
  }
462
463
138k
  Type *type_ptr = dwarf->GetDIEToType().lookup(die.GetDIE());
464
138k
  if (type_ptr == DIE_IS_BEING_PARSED)
465
0
    return nullptr;
466
138k
  if (type_ptr)
467
2
    return type_ptr->shared_from_this();
468
  // Set a bit that lets us know that we are currently parsing this
469
138k
  dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED;
470
471
138k
  ParsedDWARFTypeAttributes attrs(die);
472
473
138k
  if (DWARFDIE signature_die = attrs.signature.Reference()) {
474
9
    if (TypeSP type_sp =
475
9
            ParseTypeFromDWARF(sc, signature_die, type_is_new_ptr)) {
476
9
      dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
477
9
      if (clang::DeclContext *decl_ctx =
478
9
              GetCachedClangDeclContextForDIE(signature_die))
479
9
        LinkDeclContextToDIE(decl_ctx, die);
480
9
      return type_sp;
481
9
    }
482
0
    return nullptr;
483
9
  }
484
485
138k
  if (type_is_new_ptr)
486
99
    *type_is_new_ptr = true;
487
488
138k
  const dw_tag_t tag = die.Tag();
489
490
138k
  TypeSP type_sp;
491
492
138k
  switch (tag) {
493
15.8k
  case DW_TAG_typedef:
494
21.4k
  case DW_TAG_base_type:
495
37.1k
  case DW_TAG_pointer_type:
496
46.7k
  case DW_TAG_reference_type:
497
48.6k
  case DW_TAG_rvalue_reference_type:
498
56.2k
  case DW_TAG_const_type:
499
56.2k
  case DW_TAG_restrict_type:
500
56.2k
  case DW_TAG_volatile_type:
501
56.2k
  case DW_TAG_atomic_type:
502
56.4k
  case DW_TAG_unspecified_type: {
503
56.4k
    type_sp = ParseTypeModifier(sc, die, attrs);
504
56.4k
    break;
505
56.2k
  }
506
507
9.35k
  case DW_TAG_structure_type:
508
9.74k
  case DW_TAG_union_type:
509
14.4k
  case DW_TAG_class_type: {
510
14.4k
    type_sp = ParseStructureLikeDIE(sc, die, attrs);
511
14.4k
    break;
512
9.74k
  }
513
514
103
  case DW_TAG_enumeration_type: {
515
103
    type_sp = ParseEnum(sc, die, attrs);
516
103
    break;
517
9.74k
  }
518
519
15
  case DW_TAG_inlined_subroutine:
520
65.8k
  case DW_TAG_subprogram:
521
66.2k
  case DW_TAG_subroutine_type: {
522
66.2k
    type_sp = ParseSubroutine(die, attrs);
523
66.2k
    break;
524
65.8k
  }
525
921
  case DW_TAG_array_type: {
526
921
    type_sp = ParseArrayType(die, attrs);
527
921
    break;
528
65.8k
  }
529
6
  case DW_TAG_ptr_to_member_type: {
530
6
    type_sp = ParsePointerToMemberType(die, attrs);
531
6
    break;
532
65.8k
  }
533
0
  default:
534
0
    dwarf->GetObjectFile()->GetModule()->ReportError(
535
0
        "{0x%8.8x}: unhandled type tag 0x%4.4x (%s), please file a bug and "
536
0
        "attach the file at the start of this error message",
537
0
        die.GetOffset(), tag, DW_TAG_value_to_name(tag));
538
0
    break;
539
138k
  }
540
541
  // TODO: We should consider making the switch above exhaustive to simplify
542
  // control flow in ParseTypeFromDWARF. Then, we could simply replace this
543
  // return statement with a call to llvm_unreachable.
544
138k
  return UpdateSymbolContextScopeForType(sc, die, type_sp);
545
138k
}
546
547
lldb::TypeSP
548
DWARFASTParserClang::ParseTypeModifier(const SymbolContext &sc,
549
                                       const DWARFDIE &die,
550
56.4k
                                       ParsedDWARFTypeAttributes &attrs) {
551
56.4k
  Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
552
56.4k
                                        DWARF_LOG_LOOKUPS));
553
56.4k
  SymbolFileDWARF *dwarf = die.GetDWARF();
554
56.4k
  const dw_tag_t tag = die.Tag();
555
56.4k
  LanguageType cu_language = SymbolFileDWARF::GetLanguage(*die.GetCU());
556
56.4k
  Type::ResolveState resolve_state = Type::ResolveState::Unresolved;
557
56.4k
  Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
558
56.4k
  TypeSP type_sp;
559
56.4k
  CompilerType clang_type;
560
561
56.4k
  if (tag == DW_TAG_typedef) {
562
    // DeclContext will be populated when the clang type is materialized in
563
    // Type::ResolveCompilerType.
564
15.8k
    PrepareContextToReceiveMembers(
565
15.8k
        m_ast, GetClangASTImporter(),
566
15.8k
        GetClangDeclContextContainingDIE(die, nullptr), die,
567
15.8k
        attrs.name.GetCString());
568
569
15.8k
    if (attrs.type.IsValid()) {
570
      // Try to parse a typedef from the (DWARF embedded in the) Clang
571
      // module file first as modules can contain typedef'ed
572
      // structures that have no names like:
573
      //
574
      //  typedef struct { int a; } Foo;
575
      //
576
      // In this case we will have a structure with no name and a
577
      // typedef named "Foo" that points to this unnamed
578
      // structure. The name in the typedef is the only identifier for
579
      // the struct, so always try to get typedefs from Clang modules
580
      // if possible.
581
      //
582
      // The type_sp returned will be empty if the typedef doesn't
583
      // exist in a module file, so it is cheap to call this function
584
      // just to check.
585
      //
586
      // If we don't do this we end up creating a TypeSP that says
587
      // this is a typedef to type 0x123 (the DW_AT_type value would
588
      // be 0x123 in the DW_TAG_typedef), and this is the unnamed
589
      // structure type. We will have a hard time tracking down an
590
      // unnammed structure type in the module debug info, so we make
591
      // sure we don't get into this situation by always resolving
592
      // typedefs from the module.
593
15.7k
      const DWARFDIE encoding_die = attrs.type.Reference();
594
595
      // First make sure that the die that this is typedef'ed to _is_
596
      // just a declaration (DW_AT_declaration == 1), not a full
597
      // definition since template types can't be represented in
598
      // modules since only concrete instances of templates are ever
599
      // emitted and modules won't contain those
600
15.7k
      if (encoding_die &&
601
15.7k
          encoding_die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) == 1) {
602
1.24k
        type_sp = ParseTypeFromClangModule(sc, die, log);
603
1.24k
        if (type_sp)
604
171
          return type_sp;
605
1.24k
      }
606
15.7k
    }
607
15.8k
  }
608
609
56.2k
  DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\") type => 0x%8.8lx\n", die.GetID(),
610
56.2k
               DW_TAG_value_to_name(tag), type_name_cstr,
611
56.2k
               encoding_uid.Reference());
612
613
56.2k
  switch (tag) {
614
0
  default:
615
0
    break;
616
617
147
  case DW_TAG_unspecified_type:
618
147
    if (attrs.name == "nullptr_t" || attrs.name == "decltype(nullptr)") {
619
144
      resolve_state = Type::ResolveState::Full;
620
144
      clang_type = m_ast.GetBasicType(eBasicTypeNullPtr);
621
144
      break;
622
144
    }
623
    // Fall through to base type below in case we can handle the type
624
    // there...
625
147
    
LLVM_FALLTHROUGH3
;3
626
627
5.65k
  case DW_TAG_base_type:
628
5.65k
    resolve_state = Type::ResolveState::Full;
629
5.65k
    clang_type = m_ast.GetBuiltinTypeForDWARFEncodingAndBitSize(
630
5.65k
        attrs.name.GetStringRef(), attrs.encoding,
631
5.65k
        attrs.byte_size.getValueOr(0) * 8);
632
5.65k
    break;
633
634
15.7k
  case DW_TAG_pointer_type:
635
15.7k
    encoding_data_type = Type::eEncodingIsPointerUID;
636
15.7k
    break;
637
9.54k
  case DW_TAG_reference_type:
638
9.54k
    encoding_data_type = Type::eEncodingIsLValueReferenceUID;
639
9.54k
    break;
640
1.89k
  case DW_TAG_rvalue_reference_type:
641
1.89k
    encoding_data_type = Type::eEncodingIsRValueReferenceUID;
642
1.89k
    break;
643
15.6k
  case DW_TAG_typedef:
644
15.6k
    encoding_data_type = Type::eEncodingIsTypedefUID;
645
15.6k
    break;
646
7.60k
  case DW_TAG_const_type:
647
7.60k
    encoding_data_type = Type::eEncodingIsConstUID;
648
7.60k
    break;
649
0
  case DW_TAG_restrict_type:
650
0
    encoding_data_type = Type::eEncodingIsRestrictUID;
651
0
    break;
652
52
  case DW_TAG_volatile_type:
653
52
    encoding_data_type = Type::eEncodingIsVolatileUID;
654
52
    break;
655
2
  case DW_TAG_atomic_type:
656
2
    encoding_data_type = Type::eEncodingIsAtomicUID;
657
2
    break;
658
56.2k
  }
659
660
56.2k
  if (!clang_type && 
(50.4k
encoding_data_type == Type::eEncodingIsPointerUID50.4k
||
661
50.4k
                      
encoding_data_type == Type::eEncodingIsTypedefUID34.7k
)) {
662
31.3k
    if (tag == DW_TAG_pointer_type) {
663
15.7k
      DWARFDIE target_die = die.GetReferencedDIE(DW_AT_type);
664
665
15.7k
      if (target_die.GetAttributeValueAsUnsigned(DW_AT_APPLE_block, 0)) {
666
        // Blocks have a __FuncPtr inside them which is a pointer to a
667
        // function of the proper type.
668
669
68
        for (DWARFDIE child_die : target_die.children()) {
670
68
          if (!strcmp(child_die.GetAttributeValueAsString(DW_AT_name, ""),
671
68
                      "__FuncPtr")) {
672
17
            DWARFDIE function_pointer_type =
673
17
                child_die.GetReferencedDIE(DW_AT_type);
674
675
17
            if (function_pointer_type) {
676
17
              DWARFDIE function_type =
677
17
                  function_pointer_type.GetReferencedDIE(DW_AT_type);
678
679
17
              bool function_type_is_new_pointer;
680
17
              TypeSP lldb_function_type_sp = ParseTypeFromDWARF(
681
17
                  sc, function_type, &function_type_is_new_pointer);
682
683
17
              if (lldb_function_type_sp) {
684
17
                clang_type = m_ast.CreateBlockPointerType(
685
17
                    lldb_function_type_sp->GetForwardCompilerType());
686
17
                encoding_data_type = Type::eEncodingIsUID;
687
17
                attrs.type.Clear();
688
17
                resolve_state = Type::ResolveState::Full;
689
17
              }
690
17
            }
691
692
17
            break;
693
17
          }
694
68
        }
695
17
      }
696
15.7k
    }
697
698
31.3k
    if (cu_language == eLanguageTypeObjC ||
699
31.3k
        
cu_language == eLanguageTypeObjC_plus_plus28.4k
) {
700
3.49k
      if (attrs.name) {
701
1.54k
        if (attrs.name == "id") {
702
171
          if (log)
703
0
            dwarf->GetObjectFile()->GetModule()->LogMessage(
704
0
                log,
705
0
                "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' "
706
0
                "is Objective-C 'id' built-in type.",
707
0
                die.GetOffset(), die.GetTagAsCString(), die.GetName());
708
171
          clang_type = m_ast.GetBasicType(eBasicTypeObjCID);
709
171
          encoding_data_type = Type::eEncodingIsUID;
710
171
          attrs.type.Clear();
711
171
          resolve_state = Type::ResolveState::Full;
712
1.37k
        } else if (attrs.name == "Class") {
713
266
          if (log)
714
0
            dwarf->GetObjectFile()->GetModule()->LogMessage(
715
0
                log,
716
0
                "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' "
717
0
                "is Objective-C 'Class' built-in type.",
718
0
                die.GetOffset(), die.GetTagAsCString(), die.GetName());
719
266
          clang_type = m_ast.GetBasicType(eBasicTypeObjCClass);
720
266
          encoding_data_type = Type::eEncodingIsUID;
721
266
          attrs.type.Clear();
722
266
          resolve_state = Type::ResolveState::Full;
723
1.11k
        } else if (attrs.name == "SEL") {
724
67
          if (log)
725
0
            dwarf->GetObjectFile()->GetModule()->LogMessage(
726
0
                log,
727
0
                "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' "
728
0
                "is Objective-C 'selector' built-in type.",
729
0
                die.GetOffset(), die.GetTagAsCString(), die.GetName());
730
67
          clang_type = m_ast.GetBasicType(eBasicTypeObjCSel);
731
67
          encoding_data_type = Type::eEncodingIsUID;
732
67
          attrs.type.Clear();
733
67
          resolve_state = Type::ResolveState::Full;
734
67
        }
735
1.94k
      } else if (encoding_data_type == Type::eEncodingIsPointerUID &&
736
1.94k
                 
attrs.type.IsValid()1.94k
) {
737
        // Clang sometimes erroneously emits id as objc_object*.  In that
738
        // case we fix up the type to "id".
739
740
1.90k
        const DWARFDIE encoding_die = attrs.type.Reference();
741
742
1.90k
        if (encoding_die && encoding_die.Tag() == DW_TAG_structure_type) {
743
1.15k
          llvm::StringRef struct_name = encoding_die.GetName();
744
1.15k
          if (struct_name == "objc_object") {
745
0
            if (log)
746
0
              dwarf->GetObjectFile()->GetModule()->LogMessage(
747
0
                  log,
748
0
                  "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s "
749
0
                  "'%s' is 'objc_object*', which we overrode to "
750
0
                  "'id'.",
751
0
                  die.GetOffset(), die.GetTagAsCString(), die.GetName());
752
0
            clang_type = m_ast.GetBasicType(eBasicTypeObjCID);
753
0
            encoding_data_type = Type::eEncodingIsUID;
754
0
            attrs.type.Clear();
755
0
            resolve_state = Type::ResolveState::Full;
756
0
          }
757
1.15k
        }
758
1.90k
      }
759
3.49k
    }
760
31.3k
  }
761
762
56.2k
  type_sp = std::make_shared<Type>(
763
56.2k
      die.GetID(), dwarf, attrs.name, attrs.byte_size, nullptr,
764
56.2k
      dwarf->GetUID(attrs.type.Reference()), encoding_data_type, &attrs.decl,
765
56.2k
      clang_type, resolve_state, TypePayloadClang(GetOwningClangModule(die)));
766
767
56.2k
  dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
768
56.2k
  return type_sp;
769
56.2k
}
770
771
TypeSP DWARFASTParserClang::ParseEnum(const SymbolContext &sc,
772
                                      const DWARFDIE &die,
773
103
                                      ParsedDWARFTypeAttributes &attrs) {
774
103
  Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
775
103
                                        DWARF_LOG_LOOKUPS));
776
103
  SymbolFileDWARF *dwarf = die.GetDWARF();
777
103
  const dw_tag_t tag = die.Tag();
778
103
  TypeSP type_sp;
779
780
103
  if (attrs.is_forward_declaration) {
781
9
    type_sp = ParseTypeFromClangModule(sc, die, log);
782
9
    if (type_sp)
783
4
      return type_sp;
784
785
5
    DWARFDeclContext die_decl_ctx = SymbolFileDWARF::GetDWARFDeclContext(die);
786
787
5
    type_sp = dwarf->FindDefinitionTypeForDWARFDeclContext(die_decl_ctx);
788
789
5
    if (!type_sp) {
790
5
      SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
791
5
      if (debug_map_symfile) {
792
        // We weren't able to find a full declaration in this DWARF,
793
        // see if we have a declaration anywhere else...
794
2
        type_sp = debug_map_symfile->FindDefinitionTypeForDWARFDeclContext(
795
2
            die_decl_ctx);
796
2
      }
797
5
    }
798
799
5
    if (type_sp) {
800
0
      if (log) {
801
0
        dwarf->GetObjectFile()->GetModule()->LogMessage(
802
0
            log,
803
0
            "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a "
804
0
            "forward declaration, complete type is 0x%8.8" PRIx64,
805
0
            static_cast<void *>(this), die.GetOffset(),
806
0
            DW_TAG_value_to_name(tag), attrs.name.GetCString(),
807
0
            type_sp->GetID());
808
0
      }
809
810
      // We found a real definition for this type elsewhere so lets use
811
      // it and cache the fact that we found a complete type for this
812
      // die
813
0
      dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
814
0
      clang::DeclContext *defn_decl_ctx =
815
0
          GetCachedClangDeclContextForDIE(dwarf->GetDIE(type_sp->GetID()));
816
0
      if (defn_decl_ctx)
817
0
        LinkDeclContextToDIE(defn_decl_ctx, die);
818
0
      return type_sp;
819
0
    }
820
5
  }
821
99
  DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
822
99
               DW_TAG_value_to_name(tag), type_name_cstr);
823
824
99
  CompilerType enumerator_clang_type;
825
99
  CompilerType clang_type;
826
99
  clang_type.SetCompilerType(
827
99
      &m_ast, dwarf->GetForwardDeclDieToClangType().lookup(die.GetDIE()));
828
99
  if (!clang_type) {
829
99
    if (attrs.type.IsValid()) {
830
94
      Type *enumerator_type =
831
94
          dwarf->ResolveTypeUID(attrs.type.Reference(), true);
832
94
      if (enumerator_type)
833
94
        enumerator_clang_type = enumerator_type->GetFullCompilerType();
834
94
    }
835
836
99
    if (!enumerator_clang_type) {
837
5
      if (attrs.byte_size) {
838
0
        enumerator_clang_type = m_ast.GetBuiltinTypeForDWARFEncodingAndBitSize(
839
0
            "", DW_ATE_signed, *attrs.byte_size * 8);
840
5
      } else {
841
5
        enumerator_clang_type = m_ast.GetBasicType(eBasicTypeInt);
842
5
      }
843
5
    }
844
845
99
    clang_type = m_ast.CreateEnumerationType(
846
99
        attrs.name.GetStringRef(),
847
99
        GetClangDeclContextContainingDIE(die, nullptr),
848
99
        GetOwningClangModule(die), attrs.decl, enumerator_clang_type,
849
99
        attrs.is_scoped_enum);
850
99
  } else {
851
0
    enumerator_clang_type = m_ast.GetEnumerationIntegerType(clang_type);
852
0
  }
853
854
99
  LinkDeclContextToDIE(TypeSystemClang::GetDeclContextForType(clang_type), die);
855
856
99
  type_sp = std::make_shared<Type>(
857
99
      die.GetID(), dwarf, attrs.name, attrs.byte_size, nullptr,
858
99
      dwarf->GetUID(attrs.type.Reference()), Type::eEncodingIsUID, &attrs.decl,
859
99
      clang_type, Type::ResolveState::Forward,
860
99
      TypePayloadClang(GetOwningClangModule(die)));
861
862
99
  if (TypeSystemClang::StartTagDeclarationDefinition(clang_type)) {
863
99
    if (die.HasChildren()) {
864
94
      bool is_signed = false;
865
94
      enumerator_clang_type.IsIntegerType(is_signed);
866
94
      ParseChildEnumerators(clang_type, is_signed,
867
94
                            type_sp->GetByteSize(nullptr).getValueOr(0), die);
868
94
    }
869
99
    TypeSystemClang::CompleteTagDeclarationDefinition(clang_type);
870
99
  } else {
871
0
    dwarf->GetObjectFile()->GetModule()->ReportError(
872
0
        "DWARF DIE at 0x%8.8x named \"%s\" was not able to start its "
873
0
        "definition.\nPlease file a bug and attach the file at the "
874
0
        "start of this error message",
875
0
        die.GetOffset(), attrs.name.GetCString());
876
0
  }
877
99
  return type_sp;
878
103
}
879
880
static clang::CallingConv
881
66.2k
ConvertDWARFCallingConventionToClang(const ParsedDWARFTypeAttributes &attrs) {
882
66.2k
  switch (attrs.calling_convention) {
883
66.1k
  case llvm::dwarf::DW_CC_normal:
884
66.1k
    return clang::CC_C;
885
1
  case llvm::dwarf::DW_CC_BORLAND_stdcall:
886
1
    return clang::CC_X86StdCall;
887
1
  case llvm::dwarf::DW_CC_BORLAND_msfastcall:
888
1
    return clang::CC_X86FastCall;
889
2
  case llvm::dwarf::DW_CC_LLVM_vectorcall:
890
2
    return clang::CC_X86VectorCall;
891
1
  case llvm::dwarf::DW_CC_BORLAND_pascal:
892
1
    return clang::CC_X86Pascal;
893
2
  case llvm::dwarf::DW_CC_LLVM_Win64:
894
2
    return clang::CC_Win64;
895
1
  case llvm::dwarf::DW_CC_LLVM_X86_64SysV:
896
1
    return clang::CC_X86_64SysV;
897
2
  case llvm::dwarf::DW_CC_LLVM_X86RegCall:
898
2
    return clang::CC_X86RegCall;
899
1
  default:
900
1
    break;
901
66.2k
  }
902
903
1
  Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
904
1
                                        DWARF_LOG_LOOKUPS));
905
1
  LLDB_LOG(log, "Unsupported DW_AT_calling_convention value: {0}",
906
1
           attrs.calling_convention);
907
  // Use the default calling convention as a fallback.
908
1
  return clang::CC_C;
909
66.2k
}
910
911
TypeSP DWARFASTParserClang::ParseSubroutine(const DWARFDIE &die,
912
66.2k
                           ParsedDWARFTypeAttributes &attrs) {
913
66.2k
  Log *log(LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION |
914
66.2k
                                        DWARF_LOG_LOOKUPS));
915
916
66.2k
  SymbolFileDWARF *dwarf = die.GetDWARF();
917
66.2k
  const dw_tag_t tag = die.Tag();
918
919
66.2k
  bool is_variadic = false;
920
66.2k
  bool is_static = false;
921
66.2k
  bool has_template_params = false;
922
923
66.2k
  unsigned type_quals = 0;
924
925
66.2k
  std::string object_pointer_name;
926
66.2k
  if (attrs.object_pointer) {
927
848
    const char *object_pointer_name_cstr = attrs.object_pointer.GetName();
928
848
    if (object_pointer_name_cstr)
929
848
      object_pointer_name = object_pointer_name_cstr;
930
848
  }
931
932
66.2k
  DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
933
66.2k
               DW_TAG_value_to_name(tag), type_name_cstr);
934
935
66.2k
  CompilerType return_clang_type;
936
66.2k
  Type *func_type = nullptr;
937
938
66.2k
  if (attrs.type.IsValid())
939
39.5k
    func_type = dwarf->ResolveTypeUID(attrs.type.Reference(), true);
940
941
66.2k
  if (func_type)
942
39.5k
    return_clang_type = func_type->GetForwardCompilerType();
943
26.6k
  else
944
26.6k
    return_clang_type = m_ast.GetBasicType(eBasicTypeVoid);
945
946
66.2k
  std::vector<CompilerType> function_param_types;
947
66.2k
  std::vector<clang::ParmVarDecl *> function_param_decls;
948
949
  // Parse the function children for the parameters
950
951
66.2k
  DWARFDIE decl_ctx_die;
952
66.2k
  clang::DeclContext *containing_decl_ctx =
953
66.2k
      GetClangDeclContextContainingDIE(die, &decl_ctx_die);
954
66.2k
  const clang::Decl::Kind containing_decl_kind =
955
66.2k
      containing_decl_ctx->getDeclKind();
956
957
66.2k
  bool is_cxx_method = DeclKindIsCXXClass(containing_decl_kind);
958
  // Start off static. This will be set to false in
959
  // ParseChildParameters(...) if we find a "this" parameters as the
960
  // first parameter
961
66.2k
  if (is_cxx_method) {
962
62.6k
    is_static = true;
963
62.6k
  }
964
965
66.2k
  if (die.HasChildren()) {
966
65.6k
    bool skip_artificial = true;
967
65.6k
    ParseChildParameters(containing_decl_ctx, die, skip_artificial, is_static,
968
65.6k
                         is_variadic, has_template_params,
969
65.6k
                         function_param_types, function_param_decls,
970
65.6k
                         type_quals);
971
65.6k
  }
972
973
66.2k
  bool ignore_containing_context = false;
974
  // Check for templatized class member functions. If we had any
975
  // DW_TAG_template_type_parameter or DW_TAG_template_value_parameter
976
  // the DW_TAG_subprogram DIE, then we can't let this become a method in
977
  // a class. Why? Because templatized functions are only emitted if one
978
  // of the templatized methods is used in the current compile unit and
979
  // we will end up with classes that may or may not include these member
980
  // functions and this means one class won't match another class
981
  // definition and it affects our ability to use a class in the clang
982
  // expression parser. So for the greater good, we currently must not
983
  // allow any template member functions in a class definition.
984
66.2k
  if (is_cxx_method && 
has_template_params62.6k
) {
985
2.27k
    ignore_containing_context = true;
986
2.27k
    is_cxx_method = false;
987
2.27k
  }
988
989
66.2k
  clang::CallingConv calling_convention =
990
66.2k
      ConvertDWARFCallingConventionToClang(attrs);
991
992
  // clang_type will get the function prototype clang type after this
993
  // call
994
66.2k
  CompilerType clang_type = m_ast.CreateFunctionType(
995
66.2k
      return_clang_type, function_param_types.data(),
996
66.2k
      function_param_types.size(), is_variadic, type_quals, calling_convention);
997
998
66.2k
  if (attrs.name) {
999
65.8k
    bool type_handled = false;
1000
65.8k
    if (tag == DW_TAG_subprogram || 
tag == DW_TAG_inlined_subroutine15
) {
1001
65.8k
      ObjCLanguage::MethodName objc_method(attrs.name.GetStringRef(), true);
1002
65.8k
      if (objc_method.IsValid(true)) {
1003
757
        CompilerType class_opaque_type;
1004
757
        ConstString class_name(objc_method.GetClassName());
1005
757
        if (class_name) {
1006
757
          TypeSP complete_objc_class_type_sp(
1007
757
              dwarf->FindCompleteObjCDefinitionTypeForDIE(DWARFDIE(),
1008
757
                                                          class_name, false));
1009
1010
757
          if (complete_objc_class_type_sp) {
1011
757
            CompilerType type_clang_forward_type =
1012
757
                complete_objc_class_type_sp->GetForwardCompilerType();
1013
757
            if (TypeSystemClang::IsObjCObjectOrInterfaceType(
1014
757
                    type_clang_forward_type))
1015
757
              class_opaque_type = type_clang_forward_type;
1016
757
          }
1017
757
        }
1018
1019
757
        if (class_opaque_type) {
1020
          // If accessibility isn't set to anything valid, assume public
1021
          // for now...
1022
757
          if (attrs.accessibility == eAccessNone)
1023
757
            attrs.accessibility = eAccessPublic;
1024
1025
757
          clang::ObjCMethodDecl *objc_method_decl =
1026
757
              m_ast.AddMethodToObjCObjectType(
1027
757
                  class_opaque_type, attrs.name.GetCString(), clang_type,
1028
757
                  attrs.accessibility, attrs.is_artificial, is_variadic,
1029
757
                  attrs.is_objc_direct_call);
1030
757
          type_handled = objc_method_decl != nullptr;
1031
757
          if (type_handled) {
1032
757
            LinkDeclContextToDIE(objc_method_decl, die);
1033
757
            m_ast.SetMetadataAsUserID(objc_method_decl, die.GetID());
1034
757
          } else {
1035
0
            dwarf->GetObjectFile()->GetModule()->ReportError(
1036
0
                "{0x%8.8x}: invalid Objective-C method 0x%4.4x (%s), "
1037
0
                "please file a bug and attach the file at the start of "
1038
0
                "this error message",
1039
0
                die.GetOffset(), tag, DW_TAG_value_to_name(tag));
1040
0
          }
1041
757
        }
1042
65.1k
      } else if (is_cxx_method) {
1043
        // Look at the parent of this DIE and see if is is a class or
1044
        // struct and see if this is actually a C++ method
1045
60.3k
        Type *class_type = dwarf->ResolveType(decl_ctx_die);
1046
60.3k
        if (class_type) {
1047
60.3k
          bool alternate_defn = false;
1048
60.3k
          if (class_type->GetID() != decl_ctx_die.GetID() ||
1049
60.3k
              
IsClangModuleFwdDecl(decl_ctx_die)60.3k
) {
1050
4
            alternate_defn = true;
1051
1052
            // We uniqued the parent class of this function to another
1053
            // class so we now need to associate all dies under
1054
            // "decl_ctx_die" to DIEs in the DIE for "class_type"...
1055
4
            DWARFDIE class_type_die = dwarf->GetDIE(class_type->GetID());
1056
1057
4
            if (class_type_die) {
1058
4
              std::vector<DWARFDIE> failures;
1059
1060
4
              CopyUniqueClassMethodTypes(decl_ctx_die, class_type_die,
1061
4
                                         class_type, failures);
1062
1063
              // FIXME do something with these failures that's
1064
              // smarter than just dropping them on the ground.
1065
              // Unfortunately classes don't like having stuff added
1066
              // to them after their definitions are complete...
1067
1068
4
              Type *type_ptr = dwarf->GetDIEToType()[die.GetDIE()];
1069
4
              if (type_ptr && type_ptr != DIE_IS_BEING_PARSED) {
1070
0
                return type_ptr->shared_from_this();
1071
0
              }
1072
4
            }
1073
4
          }
1074
1075
60.3k
          if (attrs.specification.IsValid()) {
1076
            // We have a specification which we are going to base our
1077
            // function prototype off of, so we need this type to be
1078
            // completed so that the m_die_to_decl_ctx for the method in
1079
            // the specification has a valid clang decl context.
1080
98
            class_type->GetForwardCompilerType();
1081
            // If we have a specification, then the function type should
1082
            // have been made with the specification and not with this
1083
            // die.
1084
98
            DWARFDIE spec_die = attrs.specification.Reference();
1085
98
            clang::DeclContext *spec_clang_decl_ctx =
1086
98
                GetClangDeclContextForDIE(spec_die);
1087
98
            if (spec_clang_decl_ctx) {
1088
98
              LinkDeclContextToDIE(spec_clang_decl_ctx, die);
1089
98
            } else {
1090
0
              dwarf->GetObjectFile()->GetModule()->ReportWarning(
1091
0
                  "0x%8.8" PRIx64 ": DW_AT_specification(0x%8.8x"
1092
0
                  ") has no decl\n",
1093
0
                  die.GetID(), spec_die.GetOffset());
1094
0
            }
1095
98
            type_handled = true;
1096
60.2k
          } else if (attrs.abstract_origin.IsValid()) {
1097
            // We have a specification which we are going to base our
1098
            // function prototype off of, so we need this type to be
1099
            // completed so that the m_die_to_decl_ctx for the method in
1100
            // the abstract origin has a valid clang decl context.
1101
0
            class_type->GetForwardCompilerType();
1102
1103
0
            DWARFDIE abs_die = attrs.abstract_origin.Reference();
1104
0
            clang::DeclContext *abs_clang_decl_ctx =
1105
0
                GetClangDeclContextForDIE(abs_die);
1106
0
            if (abs_clang_decl_ctx) {
1107
0
              LinkDeclContextToDIE(abs_clang_decl_ctx, die);
1108
0
            } else {
1109
0
              dwarf->GetObjectFile()->GetModule()->ReportWarning(
1110
0
                  "0x%8.8" PRIx64 ": DW_AT_abstract_origin(0x%8.8x"
1111
0
                  ") has no decl\n",
1112
0
                  die.GetID(), abs_die.GetOffset());
1113
0
            }
1114
0
            type_handled = true;
1115
60.2k
          } else {
1116
60.2k
            CompilerType class_opaque_type =
1117
60.2k
                class_type->GetForwardCompilerType();
1118
60.2k
            if (TypeSystemClang::IsCXXClassType(class_opaque_type)) {
1119
60.2k
              if (class_opaque_type.IsBeingDefined() || 
alternate_defn2
) {
1120
60.2k
                if (!is_static && 
!die.HasChildren()58.4k
) {
1121
                  // We have a C++ member function with no children (this
1122
                  // pointer!) and clang will get mad if we try and make
1123
                  // a function that isn't well formed in the DWARF, so
1124
                  // we will just skip it...
1125
0
                  type_handled = true;
1126
60.2k
                } else {
1127
60.2k
                  bool add_method = true;
1128
60.2k
                  if (alternate_defn) {
1129
                    // If an alternate definition for the class exists,
1130
                    // then add the method only if an equivalent is not
1131
                    // already present.
1132
2
                    clang::CXXRecordDecl *record_decl =
1133
2
                        m_ast.GetAsCXXRecordDecl(
1134
2
                            class_opaque_type.GetOpaqueQualType());
1135
2
                    if (record_decl) {
1136
2
                      for (auto method_iter = record_decl->method_begin();
1137
2
                           method_iter != record_decl->method_end();
1138
2
                           
method_iter++0
) {
1139
2
                        clang::CXXMethodDecl *method_decl = *method_iter;
1140
2
                        if (method_decl->getNameInfo().getAsString() ==
1141
2
                            attrs.name.GetStringRef()) {
1142
2
                          if (method_decl->getType() ==
1143
2
                              ClangUtil::GetQualType(clang_type)) {
1144
2
                            add_method = false;
1145
2
                            LinkDeclContextToDIE(method_decl, die);
1146
2
                            type_handled = true;
1147
1148
2
                            break;
1149
2
                          }
1150
2
                        }
1151
2
                      }
1152
2
                    }
1153
2
                  }
1154
1155
60.2k
                  if (add_method) {
1156
60.2k
                    llvm::PrettyStackTraceFormat stack_trace(
1157
60.2k
                        "SymbolFileDWARF::ParseType() is adding a method "
1158
60.2k
                        "%s to class %s in DIE 0x%8.8" PRIx64 " from %s",
1159
60.2k
                        attrs.name.GetCString(),
1160
60.2k
                        class_type->GetName().GetCString(), die.GetID(),
1161
60.2k
                        dwarf->GetObjectFile()
1162
60.2k
                            ->GetFileSpec()
1163
60.2k
                            .GetPath()
1164
60.2k
                            .c_str());
1165
1166
60.2k
                    const bool is_attr_used = false;
1167
                    // Neither GCC 4.2 nor clang++ currently set a valid
1168
                    // accessibility in the DWARF for C++ methods...
1169
                    // Default to public for now...
1170
60.2k
                    if (attrs.accessibility == eAccessNone)
1171
17.7k
                      attrs.accessibility = eAccessPublic;
1172
1173
60.2k
                    clang::CXXMethodDecl *cxx_method_decl =
1174
60.2k
                        m_ast.AddMethodToCXXRecordType(
1175
60.2k
                            class_opaque_type.GetOpaqueQualType(),
1176
60.2k
                            attrs.name.GetCString(), attrs.mangled_name,
1177
60.2k
                            clang_type, attrs.accessibility, attrs.is_virtual,
1178
60.2k
                            is_static, attrs.is_inline, attrs.is_explicit,
1179
60.2k
                            is_attr_used, attrs.is_artificial);
1180
1181
60.2k
                    type_handled = cxx_method_decl != nullptr;
1182
                    // Artificial methods are always handled even when we
1183
                    // don't create a new declaration for them.
1184
60.2k
                    type_handled |= attrs.is_artificial;
1185
1186
60.2k
                    if (cxx_method_decl) {
1187
59.7k
                      LinkDeclContextToDIE(cxx_method_decl, die);
1188
1189
59.7k
                      ClangASTMetadata metadata;
1190
59.7k
                      metadata.SetUserID(die.GetID());
1191
1192
59.7k
                      if (!object_pointer_name.empty()) {
1193
0
                        metadata.SetObjectPtrName(
1194
0
                            object_pointer_name.c_str());
1195
0
                        LLDB_LOGF(log,
1196
0
                                  "Setting object pointer name: %s on method "
1197
0
                                  "object %p.\n",
1198
0
                                  object_pointer_name.c_str(),
1199
0
                                  static_cast<void *>(cxx_method_decl));
1200
0
                      }
1201
59.7k
                      m_ast.SetMetadata(cxx_method_decl, metadata);
1202
59.7k
                    } else {
1203
552
                      ignore_containing_context = true;
1204
552
                    }
1205
60.2k
                  }
1206
60.2k
                }
1207
60.2k
              } else {
1208
                // We were asked to parse the type for a method in a
1209
                // class, yet the class hasn't been asked to complete
1210
                // itself through the clang::ExternalASTSource protocol,
1211
                // so we need to just have the class complete itself and
1212
                // do things the right way, then our
1213
                // DIE should then have an entry in the
1214
                // dwarf->GetDIEToType() map. First
1215
                // we need to modify the dwarf->GetDIEToType() so it
1216
                // doesn't think we are trying to parse this DIE
1217
                // anymore...
1218
0
                dwarf->GetDIEToType()[die.GetDIE()] = NULL;
1219
1220
                // Now we get the full type to force our class type to
1221
                // complete itself using the clang::ExternalASTSource
1222
                // protocol which will parse all base classes and all
1223
                // methods (including the method for this DIE).
1224
0
                class_type->GetFullCompilerType();
1225
1226
                // The type for this DIE should have been filled in the
1227
                // function call above
1228
0
                Type *type_ptr = dwarf->GetDIEToType()[die.GetDIE()];
1229
0
                if (type_ptr && type_ptr != DIE_IS_BEING_PARSED) {
1230
0
                  return type_ptr->shared_from_this();
1231
0
                }
1232
1233
                // FIXME This is fixing some even uglier behavior but we
1234
                // really need to
1235
                // uniq the methods of each class as well as the class
1236
                // itself. <rdar://problem/11240464>
1237
0
                type_handled = true;
1238
0
              }
1239
60.2k
            }
1240
60.2k
          }
1241
60.3k
        }
1242
60.3k
      }
1243
65.8k
    }
1244
1245
65.8k
    if (!type_handled) {
1246
4.75k
      clang::FunctionDecl *function_decl = nullptr;
1247
4.75k
      clang::FunctionDecl *template_function_decl = nullptr;
1248
1249
4.75k
      if (attrs.abstract_origin.IsValid()) {
1250
32
        DWARFDIE abs_die = attrs.abstract_origin.Reference();
1251
1252
32
        if (dwarf->ResolveType(abs_die)) {
1253
32
          function_decl = llvm::dyn_cast_or_null<clang::FunctionDecl>(
1254
32
              GetCachedClangDeclContextForDIE(abs_die));
1255
1256
32
          if (function_decl) {
1257
32
            LinkDeclContextToDIE(function_decl, die);
1258
32
          }
1259
32
        }
1260
32
      }
1261
1262
4.75k
      if (!function_decl) {
1263
4.71k
        char *name_buf = nullptr;
1264
4.71k
        llvm::StringRef name = attrs.name.GetStringRef();
1265
1266
        // We currently generate function templates with template parameters in
1267
        // their name. In order to get closer to the AST that clang generates
1268
        // we want to strip these from the name when creating the AST.
1269
4.71k
        if (attrs.mangled_name) {
1270
1.89k
          llvm::ItaniumPartialDemangler D;
1271
1.89k
          if (!D.partialDemangle(attrs.mangled_name)) {
1272
1.81k
            name_buf = D.getFunctionBaseName(nullptr, nullptr);
1273
1.81k
            name = name_buf;
1274
1.81k
          }
1275
1.89k
        }
1276
1277
        // We just have a function that isn't part of a class
1278
4.71k
        function_decl = m_ast.CreateFunctionDeclaration(
1279
4.71k
            ignore_containing_context ? 
m_ast.GetTranslationUnitDecl()2.27k
1280
4.71k
                                      : 
containing_decl_ctx2.44k
,
1281
4.71k
            GetOwningClangModule(die), name, clang_type, attrs.storage,
1282
4.71k
            attrs.is_inline);
1283
4.71k
        std::free(name_buf);
1284
1285
4.71k
        if (has_template_params) {
1286
2.30k
          TypeSystemClang::TemplateParameterInfos template_param_infos;
1287
2.30k
          ParseTemplateParameterInfos(die, template_param_infos);
1288
2.30k
          template_function_decl = m_ast.CreateFunctionDeclaration(
1289
2.30k
              ignore_containing_context ? 
m_ast.GetTranslationUnitDecl()2.27k
1290
2.30k
                                        : 
containing_decl_ctx26
,
1291
2.30k
              GetOwningClangModule(die), attrs.name.GetStringRef(), clang_type,
1292
2.30k
              attrs.storage, attrs.is_inline);
1293
2.30k
          clang::FunctionTemplateDecl *func_template_decl =
1294
2.30k
              m_ast.CreateFunctionTemplateDecl(
1295
2.30k
                  containing_decl_ctx, GetOwningClangModule(die),
1296
2.30k
                  template_function_decl, template_param_infos);
1297
2.30k
          m_ast.CreateFunctionTemplateSpecializationInfo(
1298
2.30k
              template_function_decl, func_template_decl, template_param_infos);
1299
2.30k
        }
1300
1301
4.71k
        lldbassert(function_decl);
1302
1303
4.71k
        if (function_decl) {
1304
4.71k
          LinkDeclContextToDIE(function_decl, die);
1305
1306
4.71k
          if (!function_param_decls.empty()) {
1307
3.52k
            m_ast.SetFunctionParameters(function_decl, function_param_decls);
1308
3.52k
            if (template_function_decl)
1309
2.21k
              m_ast.SetFunctionParameters(template_function_decl,
1310
2.21k
                                          function_param_decls);
1311
3.52k
          }
1312
1313
4.71k
          ClangASTMetadata metadata;
1314
4.71k
          metadata.SetUserID(die.GetID());
1315
1316
4.71k
          if (!object_pointer_name.empty()) {
1317
17
            metadata.SetObjectPtrName(object_pointer_name.c_str());
1318
17
            LLDB_LOGF(log,
1319
17
                      "Setting object pointer name: %s on function "
1320
17
                      "object %p.",
1321
17
                      object_pointer_name.c_str(),
1322
17
                      static_cast<void *>(function_decl));
1323
17
          }
1324
4.71k
          m_ast.SetMetadata(function_decl, metadata);
1325
4.71k
        }
1326
4.71k
      }
1327
4.75k
    }
1328
65.8k
  }
1329
66.2k
  return std::make_shared<Type>(
1330
66.2k
      die.GetID(), dwarf, attrs.name, llvm::None, nullptr, LLDB_INVALID_UID,
1331
66.2k
      Type::eEncodingIsUID, &attrs.decl, clang_type, Type::ResolveState::Full);
1332
66.2k
}
1333
1334
TypeSP DWARFASTParserClang::ParseArrayType(const DWARFDIE &die,
1335
921
                                           ParsedDWARFTypeAttributes &attrs) {
1336
921
  SymbolFileDWARF *dwarf = die.GetDWARF();
1337
1338
921
  DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
1339
921
               DW_TAG_value_to_name(tag), type_name_cstr);
1340
1341
921
  DWARFDIE type_die = attrs.type.Reference();
1342
921
  Type *element_type = dwarf->ResolveTypeUID(type_die, true);
1343
1344
921
  if (!element_type)
1345
0
    return nullptr;
1346
1347
921
  llvm::Optional<SymbolFile::ArrayInfo> array_info = ParseChildArrayInfo(die);
1348
921
  if (array_info) {
1349
921
    attrs.byte_stride = array_info->byte_stride;
1350
921
    attrs.bit_stride = array_info->bit_stride;
1351
921
  }
1352
921
  if (attrs.byte_stride == 0 && attrs.bit_stride == 0)
1353
921
    attrs.byte_stride = element_type->GetByteSize(nullptr).getValueOr(0);
1354
921
  CompilerType array_element_type = element_type->GetForwardCompilerType();
1355
921
  RequireCompleteType(array_element_type);
1356
1357
921
  uint64_t array_element_bit_stride =
1358
921
      attrs.byte_stride * 8 + attrs.bit_stride;
1359
921
  CompilerType clang_type;
1360
921
  if (array_info && array_info->element_orders.size() > 0) {
1361
921
    uint64_t num_elements = 0;
1362
921
    auto end = array_info->element_orders.rend();
1363
1.85k
    for (auto pos = array_info->element_orders.rbegin(); pos != end; 
++pos933
) {
1364
933
      num_elements = *pos;
1365
933
      clang_type = m_ast.CreateArrayType(array_element_type, num_elements,
1366
933
                                         attrs.is_vector);
1367
933
      array_element_type = clang_type;
1368
933
      array_element_bit_stride = num_elements
1369
933
                                     ? 
array_element_bit_stride * num_elements903
1370
933
                                     : 
array_element_bit_stride30
;
1371
933
    }
1372
921
  } else {
1373
0
    clang_type =
1374
0
        m_ast.CreateArrayType(array_element_type, 0, attrs.is_vector);
1375
0
  }
1376
921
  ConstString empty_name;
1377
921
  TypeSP type_sp = std::make_shared<Type>(
1378
921
      die.GetID(), dwarf, empty_name, array_element_bit_stride / 8, nullptr,
1379
921
      dwarf->GetUID(type_die), Type::eEncodingIsUID, &attrs.decl, clang_type,
1380
921
      Type::ResolveState::Full);
1381
921
  type_sp->SetEncodingType(element_type);
1382
921
  const clang::Type *type = ClangUtil::GetQualType(clang_type).getTypePtr();
1383
921
  m_ast.SetMetadataAsUserID(type, die.GetID());
1384
921
  return type_sp;
1385
921
}
1386
1387
TypeSP DWARFASTParserClang::ParsePointerToMemberType(
1388
6
    const DWARFDIE &die, const ParsedDWARFTypeAttributes &attrs) {
1389
6
  SymbolFileDWARF *dwarf = die.GetDWARF();
1390
6
  Type *pointee_type = dwarf->ResolveTypeUID(attrs.type.Reference(), true);
1391
6
  Type *class_type =
1392
6
      dwarf->ResolveTypeUID(attrs.containing_type.Reference(), true);
1393
1394
6
  CompilerType pointee_clang_type = pointee_type->GetForwardCompilerType();
1395
6
  CompilerType class_clang_type = class_type->GetForwardCompilerType();
1396
1397
6
  CompilerType clang_type = TypeSystemClang::CreateMemberPointerType(
1398
6
      class_clang_type, pointee_clang_type);
1399
1400
6
  if (llvm::Optional<uint64_t> clang_type_size =
1401
6
          clang_type.GetByteSize(nullptr)) {
1402
6
    return std::make_shared<Type>(die.GetID(), dwarf, attrs.name,
1403
6
                                  *clang_type_size, nullptr, LLDB_INVALID_UID,
1404
6
                                  Type::eEncodingIsUID, nullptr, clang_type,
1405
6
                                  Type::ResolveState::Forward);
1406
6
  }
1407
0
  return nullptr;
1408
6
}
1409
1410
void DWARFASTParserClang::ParseInheritance(
1411
    const DWARFDIE &die, const DWARFDIE &parent_die,
1412
    const CompilerType class_clang_type, const AccessType default_accessibility,
1413
    const lldb::ModuleSP &module_sp,
1414
    std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> &base_classes,
1415
3.10k
    ClangASTImporter::LayoutInfo &layout_info) {
1416
1417
3.10k
  TypeSystemClang *ast =
1418
3.10k
      llvm::dyn_cast_or_null<TypeSystemClang>(class_clang_type.GetTypeSystem());
1419
3.10k
  if (ast == nullptr)
1420
0
    return;
1421
1422
  // TODO: implement DW_TAG_inheritance type parsing.
1423
3.10k
  DWARFAttributes attributes;
1424
3.10k
  const size_t num_attributes = die.GetAttributes(attributes);
1425
3.10k
  if (num_attributes == 0)
1426
0
    return;
1427
1428
3.10k
  DWARFFormValue encoding_form;
1429
3.10k
  AccessType accessibility = default_accessibility;
1430
3.10k
  bool is_virtual = false;
1431
3.10k
  bool is_base_of_class = true;
1432
3.10k
  off_t member_byte_offset = 0;
1433
1434
9.94k
  for (uint32_t i = 0; i < num_attributes; 
++i6.84k
) {
1435
6.84k
    const dw_attr_t attr = attributes.AttributeAtIndex(i);
1436
6.84k
    DWARFFormValue form_value;
1437
6.84k
    if (attributes.ExtractFormValueAtIndex(i, form_value)) {
1438
6.84k
      switch (attr) {
1439
3.10k
      case DW_AT_type:
1440
3.10k
        encoding_form = form_value;
1441
3.10k
        break;
1442
3.10k
      case DW_AT_data_member_location:
1443
3.10k
        if (form_value.BlockData()) {
1444
7
          Value initialValue(0);
1445
7
          Value memberOffset(0);
1446
7
          const DWARFDataExtractor &debug_info_data = die.GetData();
1447
7
          uint32_t block_length = form_value.Unsigned();
1448
7
          uint32_t block_offset =
1449
7
              form_value.BlockData() - debug_info_data.GetDataStart();
1450
7
          if (DWARFExpression::Evaluate(
1451
7
                  nullptr, nullptr, module_sp,
1452
7
                  DataExtractor(debug_info_data, block_offset, block_length),
1453
7
                  die.GetCU(), eRegisterKindDWARF, &initialValue, nullptr,
1454
7
                  memberOffset, nullptr)) {
1455
0
            member_byte_offset = memberOffset.ResolveValue(nullptr).UInt();
1456
0
          }
1457
3.09k
        } else {
1458
          // With DWARF 3 and later, if the value is an integer constant,
1459
          // this form value is the offset in bytes from the beginning of
1460
          // the containing entity.
1461
3.09k
          member_byte_offset = form_value.Unsigned();
1462
3.09k
        }
1463
3.10k
        break;
1464
1465
628
      case DW_AT_accessibility:
1466
628
        accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
1467
628
        break;
1468
1469
7
      case DW_AT_virtuality:
1470
7
        is_virtual = form_value.Boolean();
1471
7
        break;
1472
1473
0
      default:
1474
0
        break;
1475
6.84k
      }
1476
6.84k
    }
1477
6.84k
  }
1478
1479
3.10k
  Type *base_class_type = die.ResolveTypeUID(encoding_form.Reference());
1480
3.10k
  if (base_class_type == nullptr) {
1481
0
    module_sp->ReportError("0x%8.8x: DW_TAG_inheritance failed to "
1482
0
                           "resolve the base class at 0x%8.8x"
1483
0
                           " from enclosing type 0x%8.8x. \nPlease file "
1484
0
                           "a bug and attach the file at the start of "
1485
0
                           "this error message",
1486
0
                           die.GetOffset(),
1487
0
                           encoding_form.Reference().GetOffset(),
1488
0
                           parent_die.GetOffset());
1489
0
    return;
1490
0
  }
1491
1492
3.10k
  CompilerType base_class_clang_type = base_class_type->GetFullCompilerType();
1493
3.10k
  assert(base_class_clang_type);
1494
3.10k
  if (TypeSystemClang::IsObjCObjectOrInterfaceType(class_clang_type)) {
1495
435
    ast->SetObjCSuperClass(class_clang_type, base_class_clang_type);
1496
435
    return;
1497
435
  }
1498
2.66k
  std::unique_ptr<clang::CXXBaseSpecifier> result =
1499
2.66k
      ast->CreateBaseClassSpecifier(base_class_clang_type.GetOpaqueQualType(),
1500
2.66k
                                    accessibility, is_virtual,
1501
2.66k
                                    is_base_of_class);
1502
2.66k
  if (!result)
1503
0
    return;
1504
1505
2.66k
  base_classes.push_back(std::move(result));
1506
1507
2.66k
  if (is_virtual) {
1508
    // Do not specify any offset for virtual inheritance. The DWARF
1509
    // produced by clang doesn't give us a constant offset, but gives
1510
    // us a DWARF expressions that requires an actual object in memory.
1511
    // the DW_AT_data_member_location for a virtual base class looks
1512
    // like:
1513
    //      DW_AT_data_member_location( DW_OP_dup, DW_OP_deref,
1514
    //      DW_OP_constu(0x00000018), DW_OP_minus, DW_OP_deref,
1515
    //      DW_OP_plus )
1516
    // Given this, there is really no valid response we can give to
1517
    // clang for virtual base class offsets, and this should eventually
1518
    // be removed from LayoutRecordType() in the external
1519
    // AST source in clang.
1520
2.66k
  } else {
1521
2.66k
    layout_info.base_offsets.insert(std::make_pair(
1522
2.66k
        ast->GetAsCXXRecordDecl(base_class_clang_type.GetOpaqueQualType()),
1523
2.66k
        clang::CharUnits::fromQuantity(member_byte_offset)));
1524
2.66k
  }
1525
2.66k
}
1526
1527
TypeSP DWARFASTParserClang::UpdateSymbolContextScopeForType(
1528
138k
    const SymbolContext &sc, const DWARFDIE &die, TypeSP type_sp) {
1529
138k
  if (!type_sp)
1530
0
    return type_sp;
1531
1532
138k
  SymbolFileDWARF *dwarf = die.GetDWARF();
1533
138k
  DWARFDIE sc_parent_die = SymbolFileDWARF::GetParentSymbolContextDIE(die);
1534
138k
  dw_tag_t sc_parent_tag = sc_parent_die.Tag();
1535
1536
138k
  SymbolContextScope *symbol_context_scope = nullptr;
1537
138k
  if (sc_parent_tag == DW_TAG_compile_unit ||
1538
138k
      
sc_parent_tag == DW_TAG_partial_unit151
) {
1539
137k
    symbol_context_scope = sc.comp_unit;
1540
137k
  } else 
if (151
sc.function != nullptr151
&&
sc_parent_die18
) {
1541
18
    symbol_context_scope =
1542
18
        sc.function->GetBlock(true).FindBlockByID(sc_parent_die.GetID());
1543
18
    if (symbol_context_scope == nullptr)
1544
0
      symbol_context_scope = sc.function;
1545
133
  } else {
1546
133
    symbol_context_scope = sc.module_sp.get();
1547
133
  }
1548
1549
138k
  if (symbol_context_scope != nullptr)
1550
138k
    type_sp->SetSymbolContextScope(symbol_context_scope);
1551
1552
138k
  dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
1553
138k
  return type_sp;
1554
138k
}
1555
1556
TypeSP
1557
DWARFASTParserClang::ParseStructureLikeDIE(const SymbolContext &sc,
1558
                                           const DWARFDIE &die,
1559
14.4k
                                           ParsedDWARFTypeAttributes &attrs) {
1560
14.4k
  TypeSP type_sp;
1561
14.4k
  CompilerType clang_type;
1562
14.4k
  const dw_tag_t tag = die.Tag();
1563
14.4k
  SymbolFileDWARF *dwarf = die.GetDWARF();
1564
14.4k
  LanguageType cu_language = SymbolFileDWARF::GetLanguage(*die.GetCU());
1565
14.4k
  Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_TYPE_COMPLETION |
1566
14.4k
                                          DWARF_LOG_LOOKUPS);
1567
1568
  // UniqueDWARFASTType is large, so don't create a local variables on the
1569
  // stack, put it on the heap. This function is often called recursively and
1570
  // clang isn't good at sharing the stack space for variables in different
1571
  // blocks.
1572
14.4k
  auto unique_ast_entry_up = std::make_unique<UniqueDWARFASTType>();
1573
1574
14.4k
  ConstString unique_typename(attrs.name);
1575
14.4k
  Declaration unique_decl(attrs.decl);
1576
1577
14.4k
  if (attrs.name) {
1578
14.0k
    if (Language::LanguageIsCPlusPlus(cu_language)) {
1579
      // For C++, we rely solely upon the one definition rule that says
1580
      // only one thing can exist at a given decl context. We ignore the
1581
      // file and line that things are declared on.
1582
11.8k
      std::string qualified_name;
1583
11.8k
      if (die.GetQualifiedName(qualified_name))
1584
11.8k
        unique_typename = ConstString(qualified_name);
1585
11.8k
      unique_decl.Clear();
1586
11.8k
    }
1587
1588
14.0k
    if (dwarf->GetUniqueDWARFASTTypeMap().Find(
1589
14.0k
            unique_typename, die, unique_decl, attrs.byte_size.getValueOr(-1),
1590
14.0k
            *unique_ast_entry_up)) {
1591
31
      type_sp = unique_ast_entry_up->m_type_sp;
1592
31
      if (type_sp) {
1593
31
        dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
1594
31
        LinkDeclContextToDIE(
1595
31
            GetCachedClangDeclContextForDIE(unique_ast_entry_up->m_die), die);
1596
31
        return type_sp;
1597
31
      }
1598
31
    }
1599
14.0k
  }
1600
1601
14.4k
  DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(),
1602
14.4k
               DW_TAG_value_to_name(tag), type_name_cstr);
1603
1604
14.4k
  int tag_decl_kind = -1;
1605
14.4k
  AccessType default_accessibility = eAccessNone;
1606
14.4k
  if (tag == DW_TAG_structure_type) {
1607
9.33k
    tag_decl_kind = clang::TTK_Struct;
1608
9.33k
    default_accessibility = eAccessPublic;
1609
9.33k
  } else 
if (5.08k
tag == DW_TAG_union_type5.08k
) {
1610
389
    tag_decl_kind = clang::TTK_Union;
1611
389
    default_accessibility = eAccessPublic;
1612
4.69k
  } else if (tag == DW_TAG_class_type) {
1613
4.69k
    tag_decl_kind = clang::TTK_Class;
1614
4.69k
    default_accessibility = eAccessPrivate;
1615
4.69k
  }
1616
1617
14.4k
  if (attrs.byte_size && 
*attrs.byte_size == 012.2k
&&
attrs.name16
&&
1618
14.4k
      
!die.HasChildren()13
&&
cu_language == eLanguageTypeObjC3
) {
1619
    // Work around an issue with clang at the moment where forward
1620
    // declarations for objective C classes are emitted as:
1621
    //  DW_TAG_structure_type [2]
1622
    //  DW_AT_name( "ForwardObjcClass" )
1623
    //  DW_AT_byte_size( 0x00 )
1624
    //  DW_AT_decl_file( "..." )
1625
    //  DW_AT_decl_line( 1 )
1626
    //
1627
    // Note that there is no DW_AT_declaration and there are no children,
1628
    // and the byte size is zero.
1629
0
    attrs.is_forward_declaration = true;
1630
0
  }
1631
1632
14.4k
  if (attrs.class_language == eLanguageTypeObjC ||
1633
14.4k
      
attrs.class_language == eLanguageTypeObjC_plus_plus13.1k
) {
1634
1.40k
    if (!attrs.is_complete_objc_class &&
1635
1.40k
        
die.Supports_DW_AT_APPLE_objc_complete_type()1.10k
) {
1636
      // We have a valid eSymbolTypeObjCClass class symbol whose name
1637
      // matches the current objective C class that we are trying to find
1638
      // and this DIE isn't the complete definition (we checked
1639
      // is_complete_objc_class above and know it is false), so the real
1640
      // definition is in here somewhere
1641
1.10k
      type_sp =
1642
1.10k
          dwarf->FindCompleteObjCDefinitionTypeForDIE(die, attrs.name, true);
1643
1644
1.10k
      if (!type_sp) {
1645
1.09k
        SymbolFileDWARFDebugMap *debug_map_symfile =
1646
1.09k
            dwarf->GetDebugMapSymfile();
1647
1.09k
        if (debug_map_symfile) {
1648
          // We weren't able to find a full declaration in this DWARF,
1649
          // see if we have a declaration anywhere else...
1650
370
          type_sp = debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE(
1651
370
              die, attrs.name, true);
1652
370
        }
1653
1.09k
      }
1654
1655
1.10k
      if (type_sp) {
1656
47
        if (log) {
1657
0
          dwarf->GetObjectFile()->GetModule()->LogMessage(
1658
0
              log,
1659
0
              "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an "
1660
0
              "incomplete objc type, complete type is 0x%8.8" PRIx64,
1661
0
              static_cast<void *>(this), die.GetOffset(),
1662
0
              DW_TAG_value_to_name(tag), attrs.name.GetCString(),
1663
0
              type_sp->GetID());
1664
0
        }
1665
1666
        // We found a real definition for this type elsewhere so lets use
1667
        // it and cache the fact that we found a complete type for this
1668
        // die
1669
47
        dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
1670
47
        return type_sp;
1671
47
      }
1672
1.10k
    }
1673
1.40k
  }
1674
1675
14.3k
  if (attrs.is_forward_declaration) {
1676
    // We have a forward declaration to a type and we need to try and
1677
    // find a full declaration. We look in the current type index just in
1678
    // case we have a forward declaration followed by an actual
1679
    // declarations in the DWARF. If this fails, we need to look
1680
    // elsewhere...
1681
2.12k
    if (log) {
1682
0
      dwarf->GetObjectFile()->GetModule()->LogMessage(
1683
0
          log,
1684
0
          "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a "
1685
0
          "forward declaration, trying to find complete type",
1686
0
          static_cast<void *>(this), die.GetOffset(), DW_TAG_value_to_name(tag),
1687
0
          attrs.name.GetCString());
1688
0
    }
1689
1690
    // See if the type comes from a Clang module and if so, track down
1691
    // that type.
1692
2.12k
    type_sp = ParseTypeFromClangModule(sc, die, log);
1693
2.12k
    if (type_sp)
1694
490
      return type_sp;
1695
1696
1.63k
    DWARFDeclContext die_decl_ctx = SymbolFileDWARF::GetDWARFDeclContext(die);
1697
1698
    // type_sp = FindDefinitionTypeForDIE (dwarf_cu, die,
1699
    // type_name_const_str);
1700
1.63k
    type_sp = dwarf->FindDefinitionTypeForDWARFDeclContext(die_decl_ctx);
1701
1702
1.63k
    if (!type_sp) {
1703
1.62k
      SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
1704
1.62k
      if (debug_map_symfile) {
1705
        // We weren't able to find a full declaration in this DWARF, see
1706
        // if we have a declaration anywhere else...
1707
925
        type_sp = debug_map_symfile->FindDefinitionTypeForDWARFDeclContext(
1708
925
            die_decl_ctx);
1709
925
      }
1710
1.62k
    }
1711
1712
1.63k
    if (type_sp) {
1713
21
      if (log) {
1714
0
        dwarf->GetObjectFile()->GetModule()->LogMessage(
1715
0
            log,
1716
0
            "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a "
1717
0
            "forward declaration, complete type is 0x%8.8" PRIx64,
1718
0
            static_cast<void *>(this), die.GetOffset(),
1719
0
            DW_TAG_value_to_name(tag), attrs.name.GetCString(),
1720
0
            type_sp->GetID());
1721
0
      }
1722
1723
      // We found a real definition for this type elsewhere so lets use
1724
      // it and cache the fact that we found a complete type for this die
1725
21
      dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
1726
21
      clang::DeclContext *defn_decl_ctx =
1727
21
          GetCachedClangDeclContextForDIE(dwarf->GetDIE(type_sp->GetID()));
1728
21
      if (defn_decl_ctx)
1729
21
        LinkDeclContextToDIE(defn_decl_ctx, die);
1730
21
      return type_sp;
1731
21
    }
1732
1.63k
  }
1733
13.8k
  assert(tag_decl_kind != -1);
1734
0
  (void)tag_decl_kind;
1735
13.8k
  bool clang_type_was_created = false;
1736
13.8k
  clang_type.SetCompilerType(
1737
13.8k
      &m_ast, dwarf->GetForwardDeclDieToClangType().lookup(die.GetDIE()));
1738
13.8k
  if (!clang_type) {
1739
13.8k
    clang::DeclContext *decl_ctx =
1740
13.8k
        GetClangDeclContextContainingDIE(die, nullptr);
1741
1742
13.8k
    PrepareContextToReceiveMembers(m_ast, GetClangASTImporter(), decl_ctx, die,
1743
13.8k
                                   attrs.name.GetCString());
1744
1745
13.8k
    if (attrs.accessibility == eAccessNone && 
decl_ctx13.8k
) {
1746
      // Check the decl context that contains this class/struct/union. If
1747
      // it is a class we must give it an accessibility.
1748
13.8k
      const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
1749
13.8k
      if (DeclKindIsCXXClass(containing_decl_kind))
1750
865
        attrs.accessibility = default_accessibility;
1751
13.8k
    }
1752
1753
13.8k
    ClangASTMetadata metadata;
1754
13.8k
    metadata.SetUserID(die.GetID());
1755
13.8k
    metadata.SetIsDynamicCXXType(dwarf->ClassOrStructIsVirtual(die));
1756
1757
13.8k
    if (attrs.name.GetStringRef().contains('<')) {
1758
8.49k
      TypeSystemClang::TemplateParameterInfos template_param_infos;
1759
8.49k
      if (ParseTemplateParameterInfos(die, template_param_infos)) {
1760
8.49k
        clang::ClassTemplateDecl *class_template_decl =
1761
8.49k
            m_ast.ParseClassTemplateDecl(
1762
8.49k
                decl_ctx, GetOwningClangModule(die), attrs.accessibility,
1763
8.49k
                attrs.name.GetCString(), tag_decl_kind, template_param_infos);
1764
8.49k
        if (!class_template_decl) {
1765
0
          if (log) {
1766
0
            dwarf->GetObjectFile()->GetModule()->LogMessage(
1767
0
                log,
1768
0
                "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" "
1769
0
                "clang::ClassTemplateDecl failed to return a decl.",
1770
0
                static_cast<void *>(this), die.GetOffset(),
1771
0
                DW_TAG_value_to_name(tag), attrs.name.GetCString());
1772
0
          }
1773
0
          return TypeSP();
1774
0
        }
1775
1776
8.49k
        clang::ClassTemplateSpecializationDecl *class_specialization_decl =
1777
8.49k
            m_ast.CreateClassTemplateSpecializationDecl(
1778
8.49k
                decl_ctx, GetOwningClangModule(die), class_template_decl,
1779
8.49k
                tag_decl_kind, template_param_infos);
1780
8.49k
        clang_type = m_ast.CreateClassTemplateSpecializationType(
1781
8.49k
            class_specialization_decl);
1782
8.49k
        clang_type_was_created = true;
1783
1784
8.49k
        m_ast.SetMetadata(class_template_decl, metadata);
1785
8.49k
        m_ast.SetMetadata(class_specialization_decl, metadata);
1786
8.49k
      }
1787
8.49k
    }
1788
1789
13.8k
    if (!clang_type_was_created) {
1790
5.35k
      clang_type_was_created = true;
1791
5.35k
      clang_type = m_ast.CreateRecordType(
1792
5.35k
          decl_ctx, GetOwningClangModule(die), attrs.accessibility,
1793
5.35k
          attrs.name.GetCString(), tag_decl_kind, attrs.class_language,
1794
5.35k
          &metadata, attrs.exports_symbols);
1795
5.35k
    }
1796
13.8k
  }
1797
1798
  // Store a forward declaration to this class type in case any
1799
  // parameters in any class methods need it for the clang types for
1800
  // function prototypes.
1801
13.8k
  LinkDeclContextToDIE(m_ast.GetDeclContextForType(clang_type), die);
1802
13.8k
  type_sp = std::make_shared<Type>(
1803
13.8k
      die.GetID(), dwarf, attrs.name, attrs.byte_size, nullptr,
1804
13.8k
      LLDB_INVALID_UID, Type::eEncodingIsUID, &attrs.decl, clang_type,
1805
13.8k
      Type::ResolveState::Forward,
1806
13.8k
      TypePayloadClang(OptionalClangModuleID(), attrs.is_complete_objc_class));
1807
1808
  // Add our type to the unique type map so we don't end up creating many
1809
  // copies of the same type over and over in the ASTContext for our
1810
  // module
1811
13.8k
  unique_ast_entry_up->m_type_sp = type_sp;
1812
13.8k
  unique_ast_entry_up->m_die = die;
1813
13.8k
  unique_ast_entry_up->m_declaration = unique_decl;
1814
13.8k
  unique_ast_entry_up->m_byte_size = attrs.byte_size.getValueOr(0);
1815
13.8k
  dwarf->GetUniqueDWARFASTTypeMap().Insert(unique_typename,
1816
13.8k
                                           *unique_ast_entry_up);
1817
1818
13.8k
  if (!attrs.is_forward_declaration) {
1819
    // Always start the definition for a class type so that if the class
1820
    // has child classes or types that require the class to be created
1821
    // for use as their decl contexts the class will be ready to accept
1822
    // these child definitions.
1823
12.2k
    if (!die.HasChildren()) {
1824
      // No children for this struct/union/class, lets finish it
1825
409
      if (TypeSystemClang::StartTagDeclarationDefinition(clang_type)) {
1826
409
        TypeSystemClang::CompleteTagDeclarationDefinition(clang_type);
1827
409
      } else {
1828
0
        dwarf->GetObjectFile()->GetModule()->ReportError(
1829
0
            "DWARF DIE at 0x%8.8x named \"%s\" was not able to start its "
1830
0
            "definition.\nPlease file a bug and attach the file at the "
1831
0
            "start of this error message",
1832
0
            die.GetOffset(), attrs.name.GetCString());
1833
0
      }
1834
1835
      // If the byte size of the record is specified then overwrite the size
1836
      // that would be computed by Clang. This is only needed as LLDB's
1837
      // TypeSystemClang is always in C++ mode, but some compilers such as
1838
      // GCC and Clang give empty structs a size of 0 in C mode (in contrast to
1839
      // the size of 1 for empty structs that would be computed in C++ mode).
1840
409
      if (attrs.byte_size) {
1841
409
        clang::RecordDecl *record_decl =
1842
409
            TypeSystemClang::GetAsRecordDecl(clang_type);
1843
409
        if (record_decl) {
1844
409
          ClangASTImporter::LayoutInfo layout;
1845
409
          layout.bit_size = *attrs.byte_size * 8;
1846
409
          GetClangASTImporter().SetRecordLayout(record_decl, layout);
1847
409
        }
1848
409
      }
1849
11.8k
    } else if (clang_type_was_created) {
1850
      // Start the definition if the class is not objective C since the
1851
      // underlying decls respond to isCompleteDefinition(). Objective
1852
      // C decls don't respond to isCompleteDefinition() so we can't
1853
      // start the declaration definition right away. For C++
1854
      // class/union/structs we want to start the definition in case the
1855
      // class is needed as the declaration context for a contained class
1856
      // or type without the need to complete that type..
1857
1858
11.8k
      if (attrs.class_language != eLanguageTypeObjC &&
1859
11.8k
          
attrs.class_language != eLanguageTypeObjC_plus_plus10.5k
)
1860
10.4k
        TypeSystemClang::StartTagDeclarationDefinition(clang_type);
1861
1862
      // Leave this as a forward declaration until we need to know the
1863
      // details of the type. lldb_private::Type will automatically call
1864
      // the SymbolFile virtual function
1865
      // "SymbolFileDWARF::CompleteType(Type *)" When the definition
1866
      // needs to be defined.
1867
11.8k
      assert(!dwarf->GetForwardDeclClangTypeToDie().count(
1868
11.8k
                 ClangUtil::RemoveFastQualifiers(clang_type)
1869
11.8k
                     .GetOpaqueQualType()) &&
1870
11.8k
             "Type already in the forward declaration map!");
1871
      // Can't assume m_ast.GetSymbolFile() is actually a
1872
      // SymbolFileDWARF, it can be a SymbolFileDWARFDebugMap for Apple
1873
      // binaries.
1874
0
      dwarf->GetForwardDeclDieToClangType()[die.GetDIE()] =
1875
11.8k
          clang_type.GetOpaqueQualType();
1876
11.8k
      dwarf->GetForwardDeclClangTypeToDie().try_emplace(
1877
11.8k
          ClangUtil::RemoveFastQualifiers(clang_type).GetOpaqueQualType(),
1878
11.8k
          *die.GetDIERef());
1879
11.8k
      m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), true);
1880
11.8k
    }
1881
12.2k
  }
1882
1883
  // If we made a clang type, set the trivial abi if applicable: We only
1884
  // do this for pass by value - which implies the Trivial ABI. There
1885
  // isn't a way to assert that something that would normally be pass by
1886
  // value is pass by reference, so we ignore that attribute if set.
1887
13.8k
  if (attrs.calling_convention == llvm::dwarf::DW_CC_pass_by_value) {
1888
8.52k
    clang::CXXRecordDecl *record_decl =
1889
8.52k
        m_ast.GetAsCXXRecordDecl(clang_type.GetOpaqueQualType());
1890
8.52k
    if (record_decl && record_decl->getDefinition()) {
1891
8.52k
      record_decl->setHasTrivialSpecialMemberForCall();
1892
8.52k
    }
1893
8.52k
  }
1894
1895
13.8k
  if (attrs.calling_convention == llvm::dwarf::DW_CC_pass_by_reference) {
1896
1.83k
    clang::CXXRecordDecl *record_decl =
1897
1.83k
        m_ast.GetAsCXXRecordDecl(clang_type.GetOpaqueQualType());
1898
1.83k
    if (record_decl)
1899
1.83k
      record_decl->setArgPassingRestrictions(
1900
1.83k
          clang::RecordDecl::APK_CannotPassInRegs);
1901
1.83k
  }
1902
13.8k
  return type_sp;
1903
13.8k
}
1904
1905
// DWARF parsing functions
1906
1907
class DWARFASTParserClang::DelayedAddObjCClassProperty {
1908
public:
1909
  DelayedAddObjCClassProperty(
1910
      const CompilerType &class_opaque_type, const char *property_name,
1911
      const CompilerType &property_opaque_type, // The property type is only
1912
                                                // required if you don't have an
1913
                                                // ivar decl
1914
      const char *property_setter_name, const char *property_getter_name,
1915
      uint32_t property_attributes, const ClangASTMetadata *metadata)
1916
      : m_class_opaque_type(class_opaque_type), m_property_name(property_name),
1917
        m_property_opaque_type(property_opaque_type),
1918
        m_property_setter_name(property_setter_name),
1919
        m_property_getter_name(property_getter_name),
1920
570
        m_property_attributes(property_attributes) {
1921
570
    if (metadata != nullptr) {
1922
570
      m_metadata_up = std::make_unique<ClangASTMetadata>();
1923
570
      *m_metadata_up = *metadata;
1924
570
    }
1925
570
  }
1926
1927
1.01k
  DelayedAddObjCClassProperty(const DelayedAddObjCClassProperty &rhs) {
1928
1.01k
    *this = rhs;
1929
1.01k
  }
1930
1931
  DelayedAddObjCClassProperty &
1932
1.01k
  operator=(const DelayedAddObjCClassProperty &rhs) {
1933
1.01k
    m_class_opaque_type = rhs.m_class_opaque_type;
1934
1.01k
    m_property_name = rhs.m_property_name;
1935
1.01k
    m_property_opaque_type = rhs.m_property_opaque_type;
1936
1.01k
    m_property_setter_name = rhs.m_property_setter_name;
1937
1.01k
    m_property_getter_name = rhs.m_property_getter_name;
1938
1.01k
    m_property_attributes = rhs.m_property_attributes;
1939
1940
1.01k
    if (rhs.m_metadata_up) {
1941
1.01k
      m_metadata_up = std::make_unique<ClangASTMetadata>();
1942
1.01k
      *m_metadata_up = *rhs.m_metadata_up;
1943
1.01k
    }
1944
1.01k
    return *this;
1945
1.01k
  }
1946
1947
570
  bool Finalize() {
1948
570
    return TypeSystemClang::AddObjCClassProperty(
1949
570
        m_class_opaque_type, m_property_name, m_property_opaque_type,
1950
570
        /*ivar_decl=*/nullptr, m_property_setter_name, m_property_getter_name,
1951
570
        m_property_attributes, m_metadata_up.get());
1952
570
  }
1953
1954
private:
1955
  CompilerType m_class_opaque_type;
1956
  const char *m_property_name;
1957
  CompilerType m_property_opaque_type;
1958
  const char *m_property_setter_name;
1959
  const char *m_property_getter_name;
1960
  uint32_t m_property_attributes;
1961
  std::unique_ptr<ClangASTMetadata> m_metadata_up;
1962
};
1963
1964
bool DWARFASTParserClang::ParseTemplateDIE(
1965
    const DWARFDIE &die,
1966
19.2k
    TypeSystemClang::TemplateParameterInfos &template_param_infos) {
1967
19.2k
  const dw_tag_t tag = die.Tag();
1968
19.2k
  bool is_template_template_argument = false;
1969
1970
19.2k
  switch (tag) {
1971
1.26k
  case DW_TAG_GNU_template_parameter_pack: {
1972
1.26k
    template_param_infos.packed_args =
1973
1.26k
        std::make_unique<TypeSystemClang::TemplateParameterInfos>();
1974
1.63k
    for (DWARFDIE child_die : die.children()) {
1975
1.63k
      if (!ParseTemplateDIE(child_die, *template_param_infos.packed_args))
1976
0
        return false;
1977
1.63k
    }
1978
1.26k
    if (const char *name = die.GetName()) {
1979
1.18k
      template_param_infos.pack_name = name;
1980
1.18k
    }
1981
1.26k
    return true;
1982
1.26k
  }
1983
35
  case DW_TAG_GNU_template_template_param:
1984
35
    is_template_template_argument = true;
1985
35
    LLVM_FALLTHROUGH;
1986
14.1k
  case DW_TAG_template_type_parameter:
1987
17.9k
  case DW_TAG_template_value_parameter: {
1988
17.9k
    DWARFAttributes attributes;
1989
17.9k
    const size_t num_attributes = die.GetAttributes(attributes);
1990
17.9k
    const char *name = nullptr;
1991
17.9k
    const char *template_name = nullptr;
1992
17.9k
    CompilerType clang_type;
1993
17.9k
    uint64_t uval64 = 0;
1994
17.9k
    bool uval64_valid = false;
1995
17.9k
    if (num_attributes > 0) {
1996
17.2k
      DWARFFormValue form_value;
1997
53.0k
      for (size_t i = 0; i < num_attributes; 
++i35.8k
) {
1998
35.8k
        const dw_attr_t attr = attributes.AttributeAtIndex(i);
1999
2000
35.8k
        switch (attr) {
2001
14.8k
        case DW_AT_name:
2002
14.8k
          if (attributes.ExtractFormValueAtIndex(i, form_value))
2003
14.8k
            name = form_value.AsCString();
2004
14.8k
          break;
2005
2006
35
        case DW_AT_GNU_template_name:
2007
35
          if (attributes.ExtractFormValueAtIndex(i, form_value))
2008
35
            template_name = form_value.AsCString();
2009
35
          break;
2010
2011
17.1k
        case DW_AT_type:
2012
17.1k
          if (attributes.ExtractFormValueAtIndex(i, form_value)) {
2013
17.1k
            Type *lldb_type = die.ResolveTypeUID(form_value.Reference());
2014
17.1k
            if (lldb_type)
2015
17.1k
              clang_type = lldb_type->GetForwardCompilerType();
2016
17.1k
          }
2017
17.1k
          break;
2018
2019
3.81k
        case DW_AT_const_value:
2020
3.81k
          if (attributes.ExtractFormValueAtIndex(i, form_value)) {
2021
3.81k
            uval64_valid = true;
2022
3.81k
            uval64 = form_value.Unsigned();
2023
3.81k
          }
2024
3.81k
          break;
2025
0
        default:
2026
0
          break;
2027
35.8k
        }
2028
35.8k
      }
2029
2030
17.2k
      clang::ASTContext &ast = m_ast.getASTContext();
2031
17.2k
      if (!clang_type)
2032
111
        clang_type = m_ast.GetBasicType(eBasicTypeVoid);
2033
2034
17.2k
      if (!is_template_template_argument) {
2035
17.2k
        bool is_signed = false;
2036
17.2k
        if (name && 
name[0]14.8k
)
2037
14.8k
          template_param_infos.names.push_back(name);
2038
2.35k
        else
2039
2.35k
          template_param_infos.names.push_back(nullptr);
2040
2041
        // Get the signed value for any integer or enumeration if available
2042
17.2k
        clang_type.IsIntegerOrEnumerationType(is_signed);
2043
2044
17.2k
        if (tag == DW_TAG_template_value_parameter && 
uval64_valid3.81k
) {
2045
3.81k
          llvm::Optional<uint64_t> size = clang_type.GetBitSize(nullptr);
2046
3.81k
          if (!size)
2047
0
            return false;
2048
3.81k
          llvm::APInt apint(*size, uval64, is_signed);
2049
3.81k
          template_param_infos.args.push_back(
2050
3.81k
              clang::TemplateArgument(ast, llvm::APSInt(apint, !is_signed),
2051
3.81k
                                      ClangUtil::GetQualType(clang_type)));
2052
13.3k
        } else {
2053
13.3k
          template_param_infos.args.push_back(
2054
13.3k
              clang::TemplateArgument(ClangUtil::GetQualType(clang_type)));
2055
13.3k
        }
2056
17.2k
      } else {
2057
35
        auto *tplt_type = m_ast.CreateTemplateTemplateParmDecl(template_name);
2058
35
        template_param_infos.names.push_back(name);
2059
35
        template_param_infos.args.push_back(
2060
35
            clang::TemplateArgument(clang::TemplateName(tplt_type)));
2061
35
      }
2062
17.2k
    }
2063
17.9k
  }
2064
17.9k
    return true;
2065
2066
0
  default:
2067
0
    break;
2068
19.2k
  }
2069
0
  return false;
2070
19.2k
}
2071
2072
bool DWARFASTParserClang::ParseTemplateParameterInfos(
2073
    const DWARFDIE &parent_die,
2074
10.8k
    TypeSystemClang::TemplateParameterInfos &template_param_infos) {
2075
2076
10.8k
  if (!parent_die)
2077
0
    return false;
2078
2079
139k
  
for (DWARFDIE die : parent_die.children())10.8k
{
2080
139k
    const dw_tag_t tag = die.Tag();
2081
2082
139k
    switch (tag) {
2083
12.6k
    case DW_TAG_template_type_parameter:
2084
16.3k
    case DW_TAG_template_value_parameter:
2085
17.5k
    case DW_TAG_GNU_template_parameter_pack:
2086
17.5k
    case DW_TAG_GNU_template_template_param:
2087
17.5k
      ParseTemplateDIE(die, template_param_infos);
2088
17.5k
      break;
2089
2090
122k
    default:
2091
122k
      break;
2092
139k
    }
2093
139k
  }
2094
10.8k
  return template_param_infos.args.size() == template_param_infos.names.size();
2095
10.8k
}
2096
2097
bool DWARFASTParserClang::CompleteRecordType(const DWARFDIE &die,
2098
                                             lldb_private::Type *type,
2099
7.73k
                                             CompilerType &clang_type) {
2100
7.73k
  const dw_tag_t tag = die.Tag();
2101
7.73k
  SymbolFileDWARF *dwarf = die.GetDWARF();
2102
2103
7.73k
  ClangASTImporter::LayoutInfo layout_info;
2104
2105
7.73k
  if (die.HasChildren()) {
2106
7.73k
    const bool type_is_objc_object_or_interface =
2107
7.73k
        TypeSystemClang::IsObjCObjectOrInterfaceType(clang_type);
2108
7.73k
    if (type_is_objc_object_or_interface) {
2109
      // For objective C we don't start the definition when the class is
2110
      // created.
2111
696
      TypeSystemClang::StartTagDeclarationDefinition(clang_type);
2112
696
    }
2113
2114
7.73k
    AccessType default_accessibility = eAccessNone;
2115
7.73k
    if (tag == DW_TAG_structure_type) {
2116
5.16k
      default_accessibility = eAccessPublic;
2117
5.16k
    } else 
if (2.57k
tag == DW_TAG_union_type2.57k
) {
2118
386
      default_accessibility = eAccessPublic;
2119
2.19k
    } else if (tag == DW_TAG_class_type) {
2120
2.19k
      default_accessibility = eAccessPrivate;
2121
2.19k
    }
2122
2123
7.73k
    std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases;
2124
    // Parse members and base classes first
2125
7.73k
    std::vector<DWARFDIE> member_function_dies;
2126
2127
7.73k
    DelayedPropertyList delayed_properties;
2128
7.73k
    ParseChildMembers(die, clang_type, bases, member_function_dies,
2129
7.73k
                      delayed_properties, default_accessibility, layout_info);
2130
2131
    // Now parse any methods if there were any...
2132
7.73k
    for (const DWARFDIE &die : member_function_dies)
2133
62.5k
      dwarf->ResolveType(die);
2134
2135
7.73k
    if (type_is_objc_object_or_interface) {
2136
696
      ConstString class_name(clang_type.GetTypeName());
2137
696
      if (class_name) {
2138
733
        dwarf->GetObjCMethods(class_name, [&](DWARFDIE method_die) {
2139
733
          method_die.ResolveType();
2140
733
          return true;
2141
733
        });
2142
2143
696
        for (DelayedAddObjCClassProperty &property : delayed_properties)
2144
570
          property.Finalize();
2145
696
      }
2146
696
    }
2147
2148
7.73k
    if (!bases.empty()) {
2149
      // Make sure all base classes refer to complete types and not forward
2150
      // declarations. If we don't do this, clang will crash with an
2151
      // assertion in the call to clang_type.TransferBaseClasses()
2152
2.66k
      for (const auto &base_class : bases) {
2153
2.66k
        clang::TypeSourceInfo *type_source_info =
2154
2.66k
            base_class->getTypeSourceInfo();
2155
2.66k
        if (type_source_info)
2156
2.66k
          RequireCompleteType(m_ast.GetType(type_source_info->getType()));
2157
2.66k
      }
2158
2159
2.06k
      m_ast.TransferBaseClasses(clang_type.GetOpaqueQualType(),
2160
2.06k
                                std::move(bases));
2161
2.06k
    }
2162
7.73k
  }
2163
2164
7.73k
  m_ast.AddMethodOverridesForCXXRecordType(clang_type.GetOpaqueQualType());
2165
7.73k
  TypeSystemClang::BuildIndirectFields(clang_type);
2166
7.73k
  TypeSystemClang::CompleteTagDeclarationDefinition(clang_type);
2167
2168
7.73k
  if (!layout_info.field_offsets.empty() || 
!layout_info.base_offsets.empty()3.43k
||
2169
7.73k
      
!layout_info.vbase_offsets.empty()1.67k
) {
2170
6.06k
    if (type)
2171
6.06k
      layout_info.bit_size = type->GetByteSize(nullptr).getValueOr(0) * 8;
2172
6.06k
    if (layout_info.bit_size == 0)
2173
9
      layout_info.bit_size =
2174
9
          die.GetAttributeValueAsUnsigned(DW_AT_byte_size, 0) * 8;
2175
2176
6.06k
    clang::CXXRecordDecl *record_decl =
2177
6.06k
        m_ast.GetAsCXXRecordDecl(clang_type.GetOpaqueQualType());
2178
6.06k
    if (record_decl)
2179
5.57k
      GetClangASTImporter().SetRecordLayout(record_decl, layout_info);
2180
6.06k
  }
2181
2182
7.73k
  return (bool)clang_type;
2183
7.73k
}
2184
2185
bool DWARFASTParserClang::CompleteEnumType(const DWARFDIE &die,
2186
                                           lldb_private::Type *type,
2187
0
                                           CompilerType &clang_type) {
2188
0
  if (TypeSystemClang::StartTagDeclarationDefinition(clang_type)) {
2189
0
    if (die.HasChildren()) {
2190
0
      bool is_signed = false;
2191
0
      clang_type.IsIntegerType(is_signed);
2192
0
      ParseChildEnumerators(clang_type, is_signed,
2193
0
                            type->GetByteSize(nullptr).getValueOr(0), die);
2194
0
    }
2195
0
    TypeSystemClang::CompleteTagDeclarationDefinition(clang_type);
2196
0
  }
2197
0
  return (bool)clang_type;
2198
0
}
2199
2200
bool DWARFASTParserClang::CompleteTypeFromDWARF(const DWARFDIE &die,
2201
                                                lldb_private::Type *type,
2202
7.73k
                                                CompilerType &clang_type) {
2203
7.73k
  SymbolFileDWARF *dwarf = die.GetDWARF();
2204
2205
7.73k
  std::lock_guard<std::recursive_mutex> guard(
2206
7.73k
      dwarf->GetObjectFile()->GetModule()->GetMutex());
2207
2208
  // Disable external storage for this type so we don't get anymore
2209
  // clang::ExternalASTSource queries for this type.
2210
7.73k
  m_ast.SetHasExternalStorage(clang_type.GetOpaqueQualType(), false);
2211
2212
7.73k
  if (!die)
2213
0
    return false;
2214
2215
7.73k
  const dw_tag_t tag = die.Tag();
2216
2217
7.73k
  Log *log =
2218
7.73k
      nullptr; // (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
2219
7.73k
  if (log)
2220
0
    dwarf->GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
2221
0
        log, "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
2222
0
        die.GetID(), die.GetTagAsCString(), type->GetName().AsCString());
2223
7.73k
  assert(clang_type);
2224
0
  DWARFAttributes attributes;
2225
7.73k
  switch (tag) {
2226
5.16k
  case DW_TAG_structure_type:
2227
5.54k
  case DW_TAG_union_type:
2228
7.73k
  case DW_TAG_class_type:
2229
7.73k
    return CompleteRecordType(die, type, clang_type);
2230
0
  case DW_TAG_enumeration_type:
2231
0
    return CompleteEnumType(die, type, clang_type);
2232
0
  default:
2233
0
    assert(false && "not a forward clang type decl!");
2234
0
    break;
2235
7.73k
  }
2236
2237
0
  return false;
2238
7.73k
}
2239
2240
void DWARFASTParserClang::EnsureAllDIEsInDeclContextHaveBeenParsed(
2241
25.6k
    lldb_private::CompilerDeclContext decl_context) {
2242
25.6k
  auto opaque_decl_ctx =
2243
25.6k
      (clang::DeclContext *)decl_context.GetOpaqueDeclContext();
2244
25.6k
  for (auto it = m_decl_ctx_to_die.find(opaque_decl_ctx);
2245
28.4k
       it != m_decl_ctx_to_die.end() && 
it->first == opaque_decl_ctx4.02k
;
2246
25.6k
       
it = m_decl_ctx_to_die.erase(it)2.81k
)
2247
2.81k
    for (DWARFDIE decl : it->second.children())
2248
64.5k
      GetClangDeclForDIE(decl);
2249
25.6k
}
2250
2251
401k
CompilerDecl DWARFASTParserClang::GetDeclForUIDFromDWARF(const DWARFDIE &die) {
2252
401k
  clang::Decl *clang_decl = GetClangDeclForDIE(die);
2253
401k
  if (clang_decl != nullptr)
2254
401k
    return m_ast.GetCompilerDecl(clang_decl);
2255
422
  return CompilerDecl();
2256
401k
}
2257
2258
CompilerDeclContext
2259
147k
DWARFASTParserClang::GetDeclContextForUIDFromDWARF(const DWARFDIE &die) {
2260
147k
  clang::DeclContext *clang_decl_ctx = GetClangDeclContextForDIE(die);
2261
147k
  if (clang_decl_ctx)
2262
146k
    return m_ast.CreateDeclContext(clang_decl_ctx);
2263
552
  return CompilerDeclContext();
2264
147k
}
2265
2266
CompilerDeclContext
2267
44.8k
DWARFASTParserClang::GetDeclContextContainingUIDFromDWARF(const DWARFDIE &die) {
2268
44.8k
  clang::DeclContext *clang_decl_ctx =
2269
44.8k
      GetClangDeclContextContainingDIE(die, nullptr);
2270
44.8k
  if (clang_decl_ctx)
2271
44.8k
    return m_ast.CreateDeclContext(clang_decl_ctx);
2272
0
  return CompilerDeclContext();
2273
44.8k
}
2274
2275
size_t DWARFASTParserClang::ParseChildEnumerators(
2276
    lldb_private::CompilerType &clang_type, bool is_signed,
2277
94
    uint32_t enumerator_byte_size, const DWARFDIE &parent_die) {
2278
94
  if (!parent_die)
2279
0
    return 0;
2280
2281
94
  size_t enumerators_added = 0;
2282
2283
367
  for (DWARFDIE die : parent_die.children()) {
2284
367
    const dw_tag_t tag = die.Tag();
2285
367
    if (tag == DW_TAG_enumerator) {
2286
367
      DWARFAttributes attributes;
2287
367
      const size_t num_child_attributes = die.GetAttributes(attributes);
2288
367
      if (num_child_attributes > 0) {
2289
367
        const char *name = nullptr;
2290
367
        bool got_value = false;
2291
367
        int64_t enum_value = 0;
2292
367
        Declaration decl;
2293
2294
367
        uint32_t i;
2295
1.10k
        for (i = 0; i < num_child_attributes; 
++i734
) {
2296
734
          const dw_attr_t attr = attributes.AttributeAtIndex(i);
2297
734
          DWARFFormValue form_value;
2298
734
          if (attributes.ExtractFormValueAtIndex(i, form_value)) {
2299
734
            switch (attr) {
2300
367
            case DW_AT_const_value:
2301
367
              got_value = true;
2302
367
              if (is_signed)
2303
126
                enum_value = form_value.Signed();
2304
241
              else
2305
241
                enum_value = form_value.Unsigned();
2306
367
              break;
2307
2308
367
            case DW_AT_name:
2309
367
              name = form_value.AsCString();
2310
367
              break;
2311
2312
0
            case DW_AT_description:
2313
0
            default:
2314
0
            case DW_AT_decl_file:
2315
0
              decl.SetFile(attributes.CompileUnitAtIndex(i)->GetFile(
2316
0
                  form_value.Unsigned()));
2317
0
              break;
2318
0
            case DW_AT_decl_line:
2319
0
              decl.SetLine(form_value.Unsigned());
2320
0
              break;
2321
0
            case DW_AT_decl_column:
2322
0
              decl.SetColumn(form_value.Unsigned());
2323
0
              break;
2324
0
            case DW_AT_sibling:
2325
0
              break;
2326
734
            }
2327
734
          }
2328
734
        }
2329
2330
367
        if (name && name[0] && got_value) {
2331
367
          m_ast.AddEnumerationValueToEnumerationType(
2332
367
              clang_type, decl, name, enum_value, enumerator_byte_size * 8);
2333
367
          ++enumerators_added;
2334
367
        }
2335
367
      }
2336
367
    }
2337
367
  }
2338
94
  return enumerators_added;
2339
94
}
2340
2341
Function *
2342
DWARFASTParserClang::ParseFunctionFromDWARF(CompileUnit &comp_unit,
2343
                                            const DWARFDIE &die,
2344
12.8k
                                            const AddressRange &func_range) {
2345
12.8k
  assert(func_range.GetBaseAddress().IsValid());
2346
0
  DWARFRangeList func_ranges;
2347
12.8k
  const char *name = nullptr;
2348
12.8k
  const char *mangled = nullptr;
2349
12.8k
  int decl_file = 0;
2350
12.8k
  int decl_line = 0;
2351
12.8k
  int decl_column = 0;
2352
12.8k
  int call_file = 0;
2353
12.8k
  int call_line = 0;
2354
12.8k
  int call_column = 0;
2355
12.8k
  DWARFExpression frame_base;
2356
2357
12.8k
  const dw_tag_t tag = die.Tag();
2358
2359
12.8k
  if (tag != DW_TAG_subprogram)
2360
0
    return nullptr;
2361
2362
12.8k
  if (die.GetDIENamesAndRanges(name, mangled, func_ranges, decl_file, decl_line,
2363
12.8k
                               decl_column, call_file, call_line, call_column,
2364
12.8k
                               &frame_base)) {
2365
12.8k
    Mangled func_name;
2366
12.8k
    if (mangled)
2367
9.65k
      func_name.SetValue(ConstString(mangled), true);
2368
3.15k
    else if ((die.GetParent().Tag() == DW_TAG_compile_unit ||
2369
3.15k
              
die.GetParent().Tag() == DW_TAG_partial_unit0
) &&
2370
3.15k
             Language::LanguageIsCPlusPlus(
2371
3.15k
                 SymbolFileDWARF::GetLanguage(*die.GetCU())) &&
2372
3.15k
             !Language::LanguageIsObjC(
2373
1.39k
                 SymbolFileDWARF::GetLanguage(*die.GetCU())) &&
2374
3.15k
             
name1.32k
&&
strcmp(name, "main") != 01.32k
) {
2375
      // If the mangled name is not present in the DWARF, generate the
2376
      // demangled name using the decl context. We skip if the function is
2377
      // "main" as its name is never mangled.
2378
28
      bool is_static = false;
2379
28
      bool is_variadic = false;
2380
28
      bool has_template_params = false;
2381
28
      unsigned type_quals = 0;
2382
28
      std::vector<CompilerType> param_types;
2383
28
      std::vector<clang::ParmVarDecl *> param_decls;
2384
28
      StreamString sstr;
2385
2386
28
      DWARFDeclContext decl_ctx = SymbolFileDWARF::GetDWARFDeclContext(die);
2387
28
      sstr << decl_ctx.GetQualifiedName();
2388
2389
28
      clang::DeclContext *containing_decl_ctx =
2390
28
          GetClangDeclContextContainingDIE(die, nullptr);
2391
28
      ParseChildParameters(containing_decl_ctx, die, true, is_static,
2392
28
                           is_variadic, has_template_params, param_types,
2393
28
                           param_decls, type_quals);
2394
28
      sstr << "(";
2395
28
      for (size_t i = 0; i < param_types.size(); 
i++0
) {
2396
0
        if (i > 0)
2397
0
          sstr << ", ";
2398
0
        sstr << param_types[i].GetTypeName();
2399
0
      }
2400
28
      if (is_variadic)
2401
0
        sstr << ", ...";
2402
28
      sstr << ")";
2403
28
      if (type_quals & clang::Qualifiers::Const)
2404
0
        sstr << " const";
2405
2406
28
      func_name.SetValue(ConstString(sstr.GetString()), false);
2407
28
    } else
2408
3.12k
      func_name.SetValue(ConstString(name), false);
2409
2410
12.8k
    FunctionSP func_sp;
2411
12.8k
    std::unique_ptr<Declaration> decl_up;
2412
12.8k
    if (decl_file != 0 || 
decl_line != 035
||
decl_column != 035
)
2413
12.7k
      decl_up = std::make_unique<Declaration>(die.GetCU()->GetFile(decl_file),
2414
12.7k
                                              decl_line, decl_column);
2415
2416
12.8k
    SymbolFileDWARF *dwarf = die.GetDWARF();
2417
    // Supply the type _only_ if it has already been parsed
2418
12.8k
    Type *func_type = dwarf->GetDIEToType().lookup(die.GetDIE());
2419
2420
12.8k
    assert(func_type == nullptr || func_type != DIE_IS_BEING_PARSED);
2421
2422
0
    const user_id_t func_user_id = die.GetID();
2423
12.8k
    func_sp =
2424
12.8k
        std::make_shared<Function>(&comp_unit,
2425
12.8k
                                   func_user_id, // UserID is the DIE offset
2426
12.8k
                                   func_user_id, func_name, func_type,
2427
12.8k
                                   func_range); // first address range
2428
2429
12.8k
    if (func_sp.get() != nullptr) {
2430
12.8k
      if (frame_base.IsValid())
2431
12.7k
        func_sp->GetFrameBaseExpression() = frame_base;
2432
12.8k
      comp_unit.AddFunction(func_sp);
2433
12.8k
      return func_sp.get();
2434
12.8k
    }
2435
12.8k
  }
2436
0
  return nullptr;
2437
12.8k
}
2438
2439
namespace {
2440
/// Parsed form of all attributes that are relevant for parsing type members.
2441
struct MemberAttributes {
2442
  explicit MemberAttributes(const DWARFDIE &die, const DWARFDIE &parent_die,
2443
                            ModuleSP module_sp);
2444
  const char *name = nullptr;
2445
  /// Indicates how many bits into the word (according to the host endianness)
2446
  /// the low-order bit of the field starts. Can be negative.
2447
  int64_t bit_offset = 0;
2448
  /// Indicates the size of the field in bits.
2449
  size_t bit_size = 0;
2450
  uint64_t data_bit_offset = UINT64_MAX;
2451
  AccessType accessibility = eAccessNone;
2452
  llvm::Optional<uint64_t> byte_size;
2453
  DWARFFormValue encoding_form;
2454
  /// Indicates the byte offset of the word from the base address of the
2455
  /// structure.
2456
  uint32_t member_byte_offset;
2457
  bool is_artificial = false;
2458
  /// On DW_TAG_members, this means the member is static.
2459
  bool is_external = false;
2460
};
2461
2462
/// Parsed form of all attributes that are relevant for parsing Objective-C
2463
/// properties.
2464
struct PropertyAttributes {
2465
  explicit PropertyAttributes(const DWARFDIE &die);
2466
  const char *prop_name = nullptr;
2467
  const char *prop_getter_name = nullptr;
2468
  const char *prop_setter_name = nullptr;
2469
  /// \see clang::ObjCPropertyAttribute
2470
  uint32_t prop_attributes = 0;
2471
};
2472
} // namespace
2473
2474
MemberAttributes::MemberAttributes(const DWARFDIE &die,
2475
                                   const DWARFDIE &parent_die,
2476
9.39k
                                   ModuleSP module_sp) {
2477
9.39k
  member_byte_offset = (parent_die.Tag() == DW_TAG_union_type) ? 
0878
: UINT32_MAX;
2478
2479
9.39k
  DWARFAttributes attributes;
2480
9.39k
  const size_t num_attributes = die.GetAttributes(attributes);
2481
59.2k
  for (std::size_t i = 0; i < num_attributes; 
++i49.9k
) {
2482
49.9k
    const dw_attr_t attr = attributes.AttributeAtIndex(i);
2483
49.9k
    DWARFFormValue form_value;
2484
49.9k
    if (attributes.ExtractFormValueAtIndex(i, form_value)) {
2485
49.9k
      switch (attr) {
2486
8.58k
      case DW_AT_name:
2487
8.58k
        name = form_value.AsCString();
2488
8.58k
        break;
2489
9.39k
      case DW_AT_type:
2490
9.39k
        encoding_form = form_value;
2491
9.39k
        break;
2492
0
      case DW_AT_bit_offset:
2493
0
        bit_offset = form_value.Signed();
2494
0
        break;
2495
234
      case DW_AT_bit_size:
2496
234
        bit_size = form_value.Unsigned();
2497
234
        break;
2498
0
      case DW_AT_byte_size:
2499
0
        byte_size = form_value.Unsigned();
2500
0
        break;
2501
234
      case DW_AT_data_bit_offset:
2502
234
        data_bit_offset = form_value.Unsigned();
2503
234
        break;
2504
8.13k
      case DW_AT_data_member_location:
2505
8.13k
        if (form_value.BlockData()) {
2506
0
          Value initialValue(0);
2507
0
          Value memberOffset(0);
2508
0
          const DWARFDataExtractor &debug_info_data = die.GetData();
2509
0
          uint32_t block_length = form_value.Unsigned();
2510
0
          uint32_t block_offset =
2511
0
              form_value.BlockData() - debug_info_data.GetDataStart();
2512
0
          if (DWARFExpression::Evaluate(
2513
0
                  nullptr, // ExecutionContext *
2514
0
                  nullptr, // RegisterContext *
2515
0
                  module_sp,
2516
0
                  DataExtractor(debug_info_data, block_offset, block_length),
2517
0
                  die.GetCU(), eRegisterKindDWARF, &initialValue, nullptr,
2518
0
                  memberOffset, nullptr)) {
2519
0
            member_byte_offset = memberOffset.ResolveValue(nullptr).UInt();
2520
0
          }
2521
8.13k
        } else {
2522
          // With DWARF 3 and later, if the value is an integer constant,
2523
          // this form value is the offset in bytes from the beginning of
2524
          // the containing entity.
2525
8.13k
          member_byte_offset = form_value.Unsigned();
2526
8.13k
        }
2527
8.13k
        break;
2528
2529
2.20k
      case DW_AT_accessibility:
2530
2.20k
        accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
2531
2.20k
        break;
2532
115
      case DW_AT_artificial:
2533
115
        is_artificial = form_value.Boolean();
2534
115
        break;
2535
461
      case DW_AT_external:
2536
461
        is_external = form_value.Boolean();
2537
461
        break;
2538
20.5k
      default:
2539
20.5k
        break;
2540
49.9k
      }
2541
49.9k
    }
2542
49.9k
  }
2543
2544
  // Clang has a DWARF generation bug where sometimes it represents
2545
  // fields that are references with bad byte size and bit size/offset
2546
  // information such as:
2547
  //
2548
  //  DW_AT_byte_size( 0x00 )
2549
  //  DW_AT_bit_size( 0x40 )
2550
  //  DW_AT_bit_offset( 0xffffffffffffffc0 )
2551
  //
2552
  // So check the bit offset to make sure it is sane, and if the values
2553
  // are not sane, remove them. If we don't do this then we will end up
2554
  // with a crash if we try to use this type in an expression when clang
2555
  // becomes unhappy with its recycled debug info.
2556
9.39k
  if (byte_size.getValueOr(0) == 0 && bit_offset < 0) {
2557
0
    bit_size = 0;
2558
0
    bit_offset = 0;
2559
0
  }
2560
9.39k
}
2561
2562
570
PropertyAttributes::PropertyAttributes(const DWARFDIE &die) {
2563
2564
570
  DWARFAttributes attributes;
2565
570
  const size_t num_attributes = die.GetAttributes(attributes);
2566
3.42k
  for (size_t i = 0; i < num_attributes; 
++i2.85k
) {
2567
2.85k
    const dw_attr_t attr = attributes.AttributeAtIndex(i);
2568
2.85k
    DWARFFormValue form_value;
2569
2.85k
    if (attributes.ExtractFormValueAtIndex(i, form_value)) {
2570
2.85k
      switch (attr) {
2571
570
      case DW_AT_APPLE_property_name:
2572
570
        prop_name = form_value.AsCString();
2573
570
        break;
2574
5
      case DW_AT_APPLE_property_getter:
2575
5
        prop_getter_name = form_value.AsCString();
2576
5
        break;
2577
3
      case DW_AT_APPLE_property_setter:
2578
3
        prop_setter_name = form_value.AsCString();
2579
3
        break;
2580
570
      case DW_AT_APPLE_property_attribute:
2581
570
        prop_attributes = form_value.Unsigned();
2582
570
        break;
2583
1.71k
      default:
2584
1.71k
        break;
2585
2.85k
      }
2586
2.85k
    }
2587
2.85k
  }
2588
2589
570
  if (!prop_name)
2590
0
    return;
2591
570
  ConstString fixed_setter;
2592
2593
  // Check if the property getter/setter were provided as full names.
2594
  // We want basenames, so we extract them.
2595
570
  if (prop_getter_name && 
prop_getter_name[0] == '-'5
) {
2596
0
    ObjCLanguage::MethodName prop_getter_method(prop_getter_name, true);
2597
0
    prop_getter_name = prop_getter_method.GetSelector().GetCString();
2598
0
  }
2599
2600
570
  if (prop_setter_name && 
prop_setter_name[0] == '-'3
) {
2601
0
    ObjCLanguage::MethodName prop_setter_method(prop_setter_name, true);
2602
0
    prop_setter_name = prop_setter_method.GetSelector().GetCString();
2603
0
  }
2604
2605
  // If the names haven't been provided, they need to be filled in.
2606
570
  if (!prop_getter_name)
2607
565
    prop_getter_name = prop_name;
2608
570
  if (!prop_setter_name && 
prop_name[0]567
&&
2609
570
      
!(prop_attributes & DW_APPLE_PROPERTY_readonly)567
) {
2610
129
    StreamString ss;
2611
2612
129
    ss.Printf("set%c%s:", toupper(prop_name[0]), &prop_name[1]);
2613
2614
129
    fixed_setter.SetString(ss.GetString());
2615
129
    prop_setter_name = fixed_setter.GetCString();
2616
129
  }
2617
570
}
2618
2619
void DWARFASTParserClang::ParseObjCProperty(
2620
    const DWARFDIE &die, const DWARFDIE &parent_die,
2621
    const lldb_private::CompilerType &class_clang_type,
2622
570
    DelayedPropertyList &delayed_properties) {
2623
  // This function can only parse DW_TAG_APPLE_property.
2624
570
  assert(die.Tag() == DW_TAG_APPLE_property);
2625
2626
0
  ModuleSP module_sp = parent_die.GetDWARF()->GetObjectFile()->GetModule();
2627
2628
570
  const MemberAttributes attrs(die, parent_die, module_sp);
2629
570
  const PropertyAttributes propAttrs(die);
2630
2631
570
  if (!propAttrs.prop_name) {
2632
0
    module_sp->ReportError(
2633
0
        "0x%8.8" PRIx64 ": DW_TAG_APPLE_property has no name.", die.GetID());
2634
0
    return;
2635
0
  }
2636
2637
570
  Type *member_type = die.ResolveTypeUID(attrs.encoding_form.Reference());
2638
570
  if (!member_type) {
2639
0
    module_sp->ReportError("0x%8.8" PRIx64
2640
0
                           ": DW_TAG_APPLE_property '%s' refers to type 0x%8.8x"
2641
0
                           " which was unable to be parsed",
2642
0
                           die.GetID(), propAttrs.prop_name,
2643
0
                           attrs.encoding_form.Reference().GetOffset());
2644
0
    return;
2645
0
  }
2646
2647
570
  ClangASTMetadata metadata;
2648
570
  metadata.SetUserID(die.GetID());
2649
570
  delayed_properties.push_back(DelayedAddObjCClassProperty(
2650
570
      class_clang_type, propAttrs.prop_name,
2651
570
      member_type->GetLayoutCompilerType(), propAttrs.prop_setter_name,
2652
570
      propAttrs.prop_getter_name, propAttrs.prop_attributes, &metadata));
2653
570
}
2654
2655
void DWARFASTParserClang::ParseSingleMember(
2656
    const DWARFDIE &die, const DWARFDIE &parent_die,
2657
    const lldb_private::CompilerType &class_clang_type,
2658
    lldb::AccessType default_accessibility,
2659
    lldb_private::ClangASTImporter::LayoutInfo &layout_info,
2660
8.82k
    FieldInfo &last_field_info) {
2661
  // This function can only parse DW_TAG_member.
2662
8.82k
  assert(die.Tag() == DW_TAG_member);
2663
2664
0
  ModuleSP module_sp = parent_die.GetDWARF()->GetObjectFile()->GetModule();
2665
8.82k
  const dw_tag_t tag = die.Tag();
2666
  // Get the parent byte size so we can verify any members will fit
2667
8.82k
  const uint64_t parent_byte_size =
2668
8.82k
      parent_die.GetAttributeValueAsUnsigned(DW_AT_byte_size, UINT64_MAX);
2669
8.82k
  const uint64_t parent_bit_size =
2670
8.82k
      parent_byte_size == UINT64_MAX ? UINT64_MAX : 
parent_byte_size * 88.82k
;
2671
2672
  // FIXME: Remove the workarounds below and make this const.
2673
8.82k
  MemberAttributes attrs(die, parent_die, module_sp);
2674
2675
8.82k
  const bool class_is_objc_object_or_interface =
2676
8.82k
      TypeSystemClang::IsObjCObjectOrInterfaceType(class_clang_type);
2677
2678
  // FIXME: Make Clang ignore Objective-C accessibility for expressions
2679
8.82k
  if (class_is_objc_object_or_interface)
2680
713
    attrs.accessibility = eAccessNone;
2681
2682
  // Handle static members
2683
8.82k
  if (attrs.is_external && 
attrs.member_byte_offset == UINT32_MAX461
) {
2684
461
    Type *var_type = die.ResolveTypeUID(attrs.encoding_form.Reference());
2685
2686
461
    if (var_type) {
2687
461
      if (attrs.accessibility == eAccessNone)
2688
268
        attrs.accessibility = eAccessPublic;
2689
461
      TypeSystemClang::AddVariableToRecordType(
2690
461
          class_clang_type, attrs.name, var_type->GetForwardCompilerType(),
2691
461
          attrs.accessibility);
2692
461
    }
2693
461
    return;
2694
461
  }
2695
2696
8.36k
  Type *member_type = die.ResolveTypeUID(attrs.encoding_form.Reference());
2697
8.36k
  if (!member_type) {
2698
0
    if (attrs.name)
2699
0
      module_sp->ReportError(
2700
0
          "0x%8.8" PRIx64 ": DW_TAG_member '%s' refers to type 0x%8.8x"
2701
0
          " which was unable to be parsed",
2702
0
          die.GetID(), attrs.name, attrs.encoding_form.Reference().GetOffset());
2703
0
    else
2704
0
      module_sp->ReportError(
2705
0
          "0x%8.8" PRIx64 ": DW_TAG_member refers to type 0x%8.8x"
2706
0
          " which was unable to be parsed",
2707
0
          die.GetID(), attrs.encoding_form.Reference().GetOffset());
2708
0
    return;
2709
0
  }
2710
2711
8.36k
  const uint64_t character_width = 8;
2712
8.36k
  const uint64_t word_width = 32;
2713
8.36k
  CompilerType member_clang_type = member_type->GetLayoutCompilerType();
2714
2715
8.36k
  if (attrs.accessibility == eAccessNone)
2716
7.07k
    attrs.accessibility = default_accessibility;
2717
2718
8.36k
  uint64_t field_bit_offset = (attrs.member_byte_offset == UINT32_MAX
2719
8.36k
                                   ? 
0231
2720
8.36k
                                   : 
(attrs.member_byte_offset * 8)8.13k
);
2721
2722
8.36k
  if (attrs.bit_size > 0) {
2723
234
    FieldInfo this_field_info;
2724
234
    this_field_info.bit_offset = field_bit_offset;
2725
234
    this_field_info.bit_size = attrs.bit_size;
2726
2727
234
    if (attrs.data_bit_offset != UINT64_MAX) {
2728
234
      this_field_info.bit_offset = attrs.data_bit_offset;
2729
234
    } else {
2730
0
      if (!attrs.byte_size)
2731
0
        attrs.byte_size = member_type->GetByteSize(nullptr);
2732
2733
0
      ObjectFile *objfile = die.GetDWARF()->GetObjectFile();
2734
0
      if (objfile->GetByteOrder() == eByteOrderLittle) {
2735
0
        this_field_info.bit_offset += attrs.byte_size.getValueOr(0) * 8;
2736
0
        this_field_info.bit_offset -= (attrs.bit_offset + attrs.bit_size);
2737
0
      } else {
2738
0
        this_field_info.bit_offset += attrs.bit_offset;
2739
0
      }
2740
0
    }
2741
2742
    // The ObjC runtime knows the byte offset but we still need to provide
2743
    // the bit-offset in the layout. It just means something different then
2744
    // what it does in C and C++. So we skip this check for ObjC types.
2745
    //
2746
    // We also skip this for fields of a union since they will all have a
2747
    // zero offset.
2748
234
    if (!TypeSystemClang::IsObjCObjectOrInterfaceType(class_clang_type) &&
2749
234
        
!(213
parent_die.Tag() == DW_TAG_union_type213
&&
2750
213
          
this_field_info.bit_offset == 03
) &&
2751
234
        
(210
(this_field_info.bit_offset >= parent_bit_size)210
||
2752
210
         (last_field_info.IsBitfield() &&
2753
210
          !last_field_info.NextBitfieldOffsetIsValid(
2754
162
              this_field_info.bit_offset)))) {
2755
0
      ObjectFile *objfile = die.GetDWARF()->GetObjectFile();
2756
0
      objfile->GetModule()->ReportWarning(
2757
0
          "0x%8.8" PRIx64 ": %s bitfield named \"%s\" has invalid "
2758
0
          "bit offset (0x%8.8" PRIx64
2759
0
          ") member will be ignored. Please file a bug against the "
2760
0
          "compiler and include the preprocessed output for %s\n",
2761
0
          die.GetID(), DW_TAG_value_to_name(tag), attrs.name,
2762
0
          this_field_info.bit_offset, GetUnitName(parent_die).c_str());
2763
0
      return;
2764
0
    }
2765
2766
    // Update the field bit offset we will report for layout
2767
234
    field_bit_offset = this_field_info.bit_offset;
2768
2769
    // Objective-C has invalid DW_AT_bit_offset values in older
2770
    // versions of clang, so we have to be careful and only insert
2771
    // unnamed bitfields if we have a new enough clang.
2772
234
    bool detect_unnamed_bitfields = true;
2773
2774
234
    if (class_is_objc_object_or_interface)
2775
21
      detect_unnamed_bitfields =
2776
21
          die.GetCU()->Supports_unnamed_objc_bitfields();
2777
2778
234
    if (detect_unnamed_bitfields) {
2779
213
      llvm::Optional<FieldInfo> unnamed_field_info;
2780
213
      uint64_t last_field_end = 0;
2781
2782
213
      last_field_end = last_field_info.bit_offset + last_field_info.bit_size;
2783
2784
213
      if (!last_field_info.IsBitfield()) {
2785
        // The last field was not a bit-field...
2786
        // but if it did take up the entire word then we need to extend
2787
        // last_field_end so the bit-field does not step into the last
2788
        // fields padding.
2789
50
        if (last_field_end != 0 && 
((last_field_end % word_width) != 0)12
)
2790
4
          last_field_end += word_width - (last_field_end % word_width);
2791
50
      }
2792
2793
      // If we have a gap between the last_field_end and the current
2794
      // field we have an unnamed bit-field.
2795
      // If we have a base class, we assume there is no unnamed
2796
      // bit-field if this is the first field since the gap can be
2797
      // attributed to the members from the base class. This assumption
2798
      // is not correct if the first field of the derived class is
2799
      // indeed an unnamed bit-field. We currently do not have the
2800
      // machinary to track the offset of the last field of classes we
2801
      // have seen before, so we are not handling this case.
2802
213
      if (this_field_info.bit_offset != last_field_end &&
2803
213
          
this_field_info.bit_offset > last_field_end31
&&
2804
213
          
!(27
last_field_info.bit_offset == 027
&&
2805
27
            
last_field_info.bit_size == 020
&&
2806
27
            
layout_info.base_offsets.size() != 011
)) {
2807
25
        unnamed_field_info = FieldInfo{};
2808
25
        unnamed_field_info->bit_size =
2809
25
            this_field_info.bit_offset - last_field_end;
2810
25
        unnamed_field_info->bit_offset = last_field_end;
2811
25
      }
2812
2813
213
      if (unnamed_field_info) {
2814
25
        clang::FieldDecl *unnamed_bitfield_decl =
2815
25
            TypeSystemClang::AddFieldToRecordType(
2816
25
                class_clang_type, llvm::StringRef(),
2817
25
                m_ast.GetBuiltinTypeForEncodingAndBitSize(eEncodingSint,
2818
25
                                                          word_width),
2819
25
                attrs.accessibility, unnamed_field_info->bit_size);
2820
2821
25
        layout_info.field_offsets.insert(std::make_pair(
2822
25
            unnamed_bitfield_decl, unnamed_field_info->bit_offset));
2823
25
      }
2824
213
    }
2825
2826
234
    last_field_info = this_field_info;
2827
234
    last_field_info.SetIsBitfield(true);
2828
8.13k
  } else {
2829
8.13k
    last_field_info.bit_offset = field_bit_offset;
2830
2831
8.13k
    if (llvm::Optional<uint64_t> clang_type_size =
2832
8.13k
            member_type->GetByteSize(nullptr)) {
2833
8.11k
      last_field_info.bit_size = *clang_type_size * character_width;
2834
8.11k
    }
2835
2836
8.13k
    last_field_info.SetIsBitfield(false);
2837
8.13k
  }
2838
2839
  // Don't turn artificial members such as vtable pointers into real FieldDecls
2840
  // in our AST. Clang will re-create those articial members and they would
2841
  // otherwise just overlap in the layout with the FieldDecls we add here.
2842
  // This needs to be done after updating FieldInfo which keeps track of where
2843
  // field start/end so we don't later try to fill the the space of this
2844
  // artificial member with (unnamed bitfield) padding.
2845
  // FIXME: This check should verify that this is indeed an artificial member
2846
  // we are supposed to ignore.
2847
8.36k
  if (attrs.is_artificial)
2848
115
    return;
2849
2850
8.25k
  if (!member_clang_type.IsCompleteType())
2851
18
    member_clang_type.GetCompleteType();
2852
2853
8.25k
  {
2854
    // Older versions of clang emit array[0] and array[1] in the
2855
    // same way (<rdar://problem/12566646>). If the current field
2856
    // is at the end of the structure, then there is definitely no
2857
    // room for extra elements and we override the type to
2858
    // array[0].
2859
2860
8.25k
    CompilerType member_array_element_type;
2861
8.25k
    uint64_t member_array_size;
2862
8.25k
    bool member_array_is_incomplete;
2863
2864
8.25k
    if (member_clang_type.IsArrayType(&member_array_element_type,
2865
8.25k
                                      &member_array_size,
2866
8.25k
                                      &member_array_is_incomplete) &&
2867
8.25k
        
!member_array_is_incomplete333
) {
2868
318
      uint64_t parent_byte_size =
2869
318
          parent_die.GetAttributeValueAsUnsigned(DW_AT_byte_size, UINT64_MAX);
2870
2871
318
      if (attrs.member_byte_offset >= parent_byte_size) {
2872
0
        if (member_array_size != 1 &&
2873
0
            (member_array_size != 0 ||
2874
0
             attrs.member_byte_offset > parent_byte_size)) {
2875
0
          module_sp->ReportError(
2876
0
              "0x%8.8" PRIx64 ": DW_TAG_member '%s' refers to type 0x%8.8x"
2877
0
              " which extends beyond the bounds of 0x%8.8" PRIx64,
2878
0
              die.GetID(), attrs.name,
2879
0
              attrs.encoding_form.Reference().GetOffset(),
2880
0
              parent_die.GetID());
2881
0
        }
2882
2883
0
        member_clang_type =
2884
0
            m_ast.CreateArrayType(member_array_element_type, 0, false);
2885
0
      }
2886
318
    }
2887
8.25k
  }
2888
2889
8.25k
  RequireCompleteType(member_clang_type);
2890
2891
8.25k
  clang::FieldDecl *field_decl = TypeSystemClang::AddFieldToRecordType(
2892
8.25k
      class_clang_type, attrs.name, member_clang_type, attrs.accessibility,
2893
8.25k
      attrs.bit_size);
2894
2895
8.25k
  m_ast.SetMetadataAsUserID(field_decl, die.GetID());
2896
2897
8.25k
  layout_info.field_offsets.insert(
2898
8.25k
      std::make_pair(field_decl, field_bit_offset));
2899
8.25k
}
2900
2901
bool DWARFASTParserClang::ParseChildMembers(
2902
    const DWARFDIE &parent_die, CompilerType &class_clang_type,
2903
    std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> &base_classes,
2904
    std::vector<DWARFDIE> &member_function_dies,
2905
    DelayedPropertyList &delayed_properties,
2906
    const AccessType default_accessibility,
2907
7.73k
    ClangASTImporter::LayoutInfo &layout_info) {
2908
7.73k
  if (!parent_die)
2909
0
    return false;
2910
2911
7.73k
  FieldInfo last_field_info;
2912
2913
7.73k
  ModuleSP module_sp = parent_die.GetDWARF()->GetObjectFile()->GetModule();
2914
7.73k
  TypeSystemClang *ast =
2915
7.73k
      llvm::dyn_cast_or_null<TypeSystemClang>(class_clang_type.GetTypeSystem());
2916
7.73k
  if (ast == nullptr)
2917
0
    return false;
2918
2919
96.8k
  
for (DWARFDIE die : parent_die.children())7.73k
{
2920
96.8k
    dw_tag_t tag = die.Tag();
2921
2922
96.8k
    switch (tag) {
2923
570
    case DW_TAG_APPLE_property:
2924
570
      ParseObjCProperty(die, parent_die, class_clang_type, delayed_properties);
2925
570
      break;
2926
2927
8.82k
    case DW_TAG_member:
2928
8.82k
      ParseSingleMember(die, parent_die, class_clang_type,
2929
8.82k
                        default_accessibility, layout_info, last_field_info);
2930
8.82k
      break;
2931
2932
62.5k
    case DW_TAG_subprogram:
2933
      // Let the type parsing code handle this one for us.
2934
62.5k
      member_function_dies.push_back(die);
2935
62.5k
      break;
2936
2937
3.10k
    case DW_TAG_inheritance:
2938
3.10k
      ParseInheritance(die, parent_die, class_clang_type, default_accessibility,
2939
3.10k
                       module_sp, base_classes, layout_info);
2940
3.10k
      break;
2941
2942
21.7k
    default:
2943
21.7k
      break;
2944
96.8k
    }
2945
96.8k
  }
2946
2947
7.73k
  return true;
2948
7.73k
}
2949
2950
size_t DWARFASTParserClang::ParseChildParameters(
2951
    clang::DeclContext *containing_decl_ctx, const DWARFDIE &parent_die,
2952
    bool skip_artificial, bool &is_static, bool &is_variadic,
2953
    bool &has_template_params, std::vector<CompilerType> &function_param_types,
2954
    std::vector<clang::ParmVarDecl *> &function_param_decls,
2955
65.6k
    unsigned &type_quals) {
2956
65.6k
  if (!parent_die)
2957
0
    return 0;
2958
2959
65.6k
  size_t arg_idx = 0;
2960
142k
  for (DWARFDIE die : parent_die.children()) {
2961
142k
    const dw_tag_t tag = die.Tag();
2962
142k
    switch (tag) {
2963
128k
    case DW_TAG_formal_parameter: {
2964
128k
      DWARFAttributes attributes;
2965
128k
      const size_t num_attributes = die.GetAttributes(attributes);
2966
128k
      if (num_attributes > 0) {
2967
128k
        const char *name = nullptr;
2968
128k
        DWARFFormValue param_type_die_form;
2969
128k
        bool is_artificial = false;
2970
        // one of None, Auto, Register, Extern, Static, PrivateExtern
2971
2972
128k
        clang::StorageClass storage = clang::SC_None;
2973
128k
        uint32_t i;
2974
332k
        for (i = 0; i < num_attributes; 
++i204k
) {
2975
204k
          const dw_attr_t attr = attributes.AttributeAtIndex(i);
2976
204k
          DWARFFormValue form_value;
2977
204k
          if (attributes.ExtractFormValueAtIndex(i, form_value)) {
2978
204k
            switch (attr) {
2979
4.24k
            case DW_AT_name:
2980
4.24k
              name = form_value.AsCString();
2981
4.24k
              break;
2982
128k
            case DW_AT_type:
2983
128k
              param_type_die_form = form_value;
2984
128k
              break;
2985
62.2k
            case DW_AT_artificial:
2986
62.2k
              is_artificial = form_value.Boolean();
2987
62.2k
              break;
2988
4.26k
            case DW_AT_location:
2989
4.26k
            case DW_AT_const_value:
2990
4.26k
            case DW_AT_default_value:
2991
4.26k
            case DW_AT_description:
2992
4.26k
            case DW_AT_endianity:
2993
4.26k
            case DW_AT_is_optional:
2994
4.26k
            case DW_AT_segment:
2995
4.26k
            case DW_AT_variable_parameter:
2996
9.36k
            default:
2997
9.38k
            case DW_AT_abstract_origin:
2998
9.38k
            case DW_AT_sibling:
2999
9.38k
              break;
3000
204k
            }
3001
204k
          }
3002
204k
        }
3003
3004
128k
        bool skip = false;
3005
128k
        if (skip_artificial && is_artificial) {
3006
          // In order to determine if a C++ member function is "const" we
3007
          // have to look at the const-ness of "this"...
3008
62.2k
          if (arg_idx == 0 &&
3009
62.2k
              
DeclKindIsCXXClass(containing_decl_ctx->getDeclKind())61.4k
&&
3010
              // Often times compilers omit the "this" name for the
3011
              // specification DIEs, so we can't rely upon the name being in
3012
              // the formal parameter DIE...
3013
62.2k
              
(60.6k
name == nullptr60.6k
||
::strcmp(name, "this") == 0106
)) {
3014
60.6k
            Type *this_type =
3015
60.6k
                die.ResolveTypeUID(param_type_die_form.Reference());
3016
60.6k
            if (this_type) {
3017
60.6k
              uint32_t encoding_mask = this_type->GetEncodingMask();
3018
60.6k
              if (encoding_mask & Type::eEncodingIsPointerUID) {
3019
60.6k
                is_static = false;
3020
3021
60.6k
                if (encoding_mask & (1u << Type::eEncodingIsConstUID))
3022
18.7k
                  type_quals |= clang::Qualifiers::Const;
3023
60.6k
                if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
3024
192
                  type_quals |= clang::Qualifiers::Volatile;
3025
60.6k
              }
3026
60.6k
            }
3027
60.6k
          }
3028
62.2k
          skip = true;
3029
62.2k
        }
3030
3031
128k
        if (!skip) {
3032
66.2k
          Type *type = die.ResolveTypeUID(param_type_die_form.Reference());
3033
66.2k
          if (type) {
3034
66.2k
            function_param_types.push_back(type->GetForwardCompilerType());
3035
3036
66.2k
            clang::ParmVarDecl *param_var_decl =
3037
66.2k
                m_ast.CreateParameterDeclaration(
3038
66.2k
                    containing_decl_ctx, GetOwningClangModule(die), name,
3039
66.2k
                    type->GetForwardCompilerType(), storage);
3040
66.2k
            assert(param_var_decl);
3041
0
            function_param_decls.push_back(param_var_decl);
3042
3043
66.2k
            m_ast.SetMetadataAsUserID(param_var_decl, die.GetID());
3044
66.2k
          }
3045
66.2k
        }
3046
128k
      }
3047
128k
      arg_idx++;
3048
128k
    } break;
3049
3050
29
    case DW_TAG_unspecified_parameters:
3051
29
      is_variadic = true;
3052
29
      break;
3053
3054
3.01k
    case DW_TAG_template_type_parameter:
3055
3.31k
    case DW_TAG_template_value_parameter:
3056
4.27k
    case DW_TAG_GNU_template_parameter_pack:
3057
      // The one caller of this was never using the template_param_infos, and
3058
      // the local variable was taking up a large amount of stack space in
3059
      // SymbolFileDWARF::ParseType() so this was removed. If we ever need the
3060
      // template params back, we can add them back.
3061
      // ParseTemplateDIE (dwarf_cu, die, template_param_infos);
3062
4.27k
      has_template_params = true;
3063
4.27k
      break;
3064
3065
9.38k
    default:
3066
9.38k
      break;
3067
142k
    }
3068
142k
  }
3069
65.6k
  return arg_idx;
3070
65.6k
}
3071
3072
llvm::Optional<SymbolFile::ArrayInfo>
3073
DWARFASTParser::ParseChildArrayInfo(const DWARFDIE &parent_die,
3074
960
                                    const ExecutionContext *exe_ctx) {
3075
960
  SymbolFile::ArrayInfo array_info;
3076
960
  if (!parent_die)
3077
0
    return llvm::None;
3078
3079
972
  
for (DWARFDIE die : parent_die.children())960
{
3080
972
    const dw_tag_t tag = die.Tag();
3081
972
    if (tag != DW_TAG_subrange_type)
3082
0
      continue;
3083
3084
972
    DWARFAttributes attributes;
3085
972
    const size_t num_child_attributes = die.GetAttributes(attributes);
3086
972
    if (num_child_attributes > 0) {
3087
972
      uint64_t num_elements = 0;
3088
972
      uint64_t lower_bound = 0;
3089
972
      uint64_t upper_bound = 0;
3090
972
      bool upper_bound_valid = false;
3091
972
      uint32_t i;
3092
2.90k
      for (i = 0; i < num_child_attributes; 
++i1.92k
) {
3093
1.92k
        const dw_attr_t attr = attributes.AttributeAtIndex(i);
3094
1.92k
        DWARFFormValue form_value;
3095
1.92k
        if (attributes.ExtractFormValueAtIndex(i, form_value)) {
3096
1.92k
          switch (attr) {
3097
0
          case DW_AT_name:
3098
0
            break;
3099
3100
957
          case DW_AT_count:
3101
957
            if (DWARFDIE var_die = die.GetReferencedDIE(DW_AT_count)) {
3102
36
              if (var_die.Tag() == DW_TAG_variable)
3103
36
                if (exe_ctx) {
3104
30
                  if (auto frame = exe_ctx->GetFrameSP()) {
3105
30
                    Status error;
3106
30
                    lldb::VariableSP var_sp;
3107
30
                    auto valobj_sp = frame->GetValueForVariableExpressionPath(
3108
30
                        var_die.GetName(), eNoDynamicValues, 0, var_sp,
3109
30
                        error);
3110
30
                    if (valobj_sp) {
3111
30
                      num_elements = valobj_sp->GetValueAsUnsigned(0);
3112
30
                      break;
3113
30
                    }
3114
30
                  }
3115
30
                }
3116
36
            } else
3117
921
              num_elements = form_value.Unsigned();
3118
927
            break;
3119
3120
927
          case DW_AT_bit_stride:
3121
0
            array_info.bit_stride = form_value.Unsigned();
3122
0
            break;
3123
3124
0
          case DW_AT_byte_stride:
3125
0
            array_info.byte_stride = form_value.Unsigned();
3126
0
            break;
3127
3128
0
          case DW_AT_lower_bound:
3129
0
            lower_bound = form_value.Unsigned();
3130
0
            break;
3131
3132
0
          case DW_AT_upper_bound:
3133
0
            upper_bound_valid = true;
3134
0
            upper_bound = form_value.Unsigned();
3135
0
            break;
3136
3137
0
          default:
3138
0
          case DW_AT_abstract_origin:
3139
0
          case DW_AT_accessibility:
3140
0
          case DW_AT_allocated:
3141
0
          case DW_AT_associated:
3142
0
          case DW_AT_data_location:
3143
0
          case DW_AT_declaration:
3144
0
          case DW_AT_description:
3145
0
          case DW_AT_sibling:
3146
0
          case DW_AT_threads_scaled:
3147
972
          case DW_AT_type:
3148
972
          case DW_AT_visibility:
3149
972
            break;
3150
1.92k
          }
3151
1.92k
        }
3152
1.92k
      }
3153
3154
972
      if (num_elements == 0) {
3155
39
        if (upper_bound_valid && 
upper_bound >= lower_bound0
)
3156
0
          num_elements = upper_bound - lower_bound + 1;
3157
39
      }
3158
3159
972
      array_info.element_orders.push_back(num_elements);
3160
972
    }
3161
972
  }
3162
960
  return array_info;
3163
960
}
3164
3165
7.82k
Type *DWARFASTParserClang::GetTypeForDIE(const DWARFDIE &die) {
3166
7.82k
  if (die) {
3167
7.82k
    SymbolFileDWARF *dwarf = die.GetDWARF();
3168
7.82k
    DWARFAttributes attributes;
3169
7.82k
    const size_t num_attributes = die.GetAttributes(attributes);
3170
7.82k
    if (num_attributes > 0) {
3171
7.82k
      DWARFFormValue type_die_form;
3172
36.9k
      for (size_t i = 0; i < num_attributes; 
++i29.1k
) {
3173
36.9k
        dw_attr_t attr = attributes.AttributeAtIndex(i);
3174
36.9k
        DWARFFormValue form_value;
3175
3176
36.9k
        if (attr == DW_AT_type &&
3177
36.9k
            
attributes.ExtractFormValueAtIndex(i, form_value)7.82k
)
3178
7.82k
          return dwarf->ResolveTypeUID(form_value.Reference(), true);
3179
36.9k
      }
3180
7.82k
    }
3181
7.82k
  }
3182
3183
0
  return nullptr;
3184
7.82k
}
3185
3186
466k
clang::Decl *DWARFASTParserClang::GetClangDeclForDIE(const DWARFDIE &die) {
3187
466k
  if (!die)
3188
0
    return nullptr;
3189
3190
466k
  switch (die.Tag()) {
3191
367k
  case DW_TAG_variable:
3192
367k
  case DW_TAG_constant:
3193
413k
  case DW_TAG_formal_parameter:
3194
413k
  case DW_TAG_imported_declaration:
3195
413k
  case DW_TAG_imported_module:
3196
413k
    break;
3197
52.8k
  default:
3198
52.8k
    return nullptr;
3199
466k
  }
3200
3201
413k
  DIEToDeclMap::iterator cache_pos = m_die_to_decl.find(die.GetDIE());
3202
413k
  if (cache_pos != m_die_to_decl.end())
3203
405k
    return cache_pos->second;
3204
3205
8.22k
  if (DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification)) {
3206
24
    clang::Decl *decl = GetClangDeclForDIE(spec_die);
3207
24
    m_die_to_decl[die.GetDIE()] = decl;
3208
24
    return decl;
3209
24
  }
3210
3211
8.19k
  if (DWARFDIE abstract_origin_die =
3212
8.19k
          die.GetReferencedDIE(DW_AT_abstract_origin)) {
3213
21
    clang::Decl *decl = GetClangDeclForDIE(abstract_origin_die);
3214
21
    m_die_to_decl[die.GetDIE()] = decl;
3215
21
    return decl;
3216
21
  }
3217
3218
8.17k
  clang::Decl *decl = nullptr;
3219
8.17k
  switch (die.Tag()) {
3220
6.70k
  case DW_TAG_variable:
3221
6.70k
  case DW_TAG_constant:
3222
7.82k
  case DW_TAG_formal_parameter: {
3223
7.82k
    SymbolFileDWARF *dwarf = die.GetDWARF();
3224
7.82k
    Type *type = GetTypeForDIE(die);
3225
7.82k
    if (dwarf && type) {
3226
7.82k
      const char *name = die.GetName();
3227
7.82k
      clang::DeclContext *decl_context =
3228
7.82k
          TypeSystemClang::DeclContextGetAsDeclContext(
3229
7.82k
              dwarf->GetDeclContextContainingUID(die.GetID()));
3230
7.82k
      decl = m_ast.CreateVariableDeclaration(
3231
7.82k
          decl_context, GetOwningClangModule(die), name,
3232
7.82k
          ClangUtil::GetQualType(type->GetForwardCompilerType()));
3233
7.82k
    }
3234
7.82k
    break;
3235
6.70k
  }
3236
301
  case DW_TAG_imported_declaration: {
3237
301
    SymbolFileDWARF *dwarf = die.GetDWARF();
3238
301
    DWARFDIE imported_uid = die.GetAttributeValueAsReferenceDIE(DW_AT_import);
3239
301
    if (imported_uid) {
3240
301
      CompilerDecl imported_decl = SymbolFileDWARF::GetDecl(imported_uid);
3241
301
      if (imported_decl) {
3242
15
        clang::DeclContext *decl_context =
3243
15
            TypeSystemClang::DeclContextGetAsDeclContext(
3244
15
                dwarf->GetDeclContextContainingUID(die.GetID()));
3245
15
        if (clang::NamedDecl *clang_imported_decl =
3246
15
                llvm::dyn_cast<clang::NamedDecl>(
3247
15
                    (clang::Decl *)imported_decl.GetOpaqueDecl()))
3248
15
          decl = m_ast.CreateUsingDeclaration(
3249
15
              decl_context, OptionalClangModuleID(), clang_imported_decl);
3250
15
      }
3251
301
    }
3252
301
    break;
3253
6.70k
  }
3254
48
  case DW_TAG_imported_module: {
3255
48
    SymbolFileDWARF *dwarf = die.GetDWARF();
3256
48
    DWARFDIE imported_uid = die.GetAttributeValueAsReferenceDIE(DW_AT_import);
3257
3258
48
    if (imported_uid) {
3259
48
      CompilerDeclContext imported_decl_ctx =
3260
48
          SymbolFileDWARF::GetDeclContext(imported_uid);
3261
48
      if (imported_decl_ctx) {
3262
48
        clang::DeclContext *decl_context =
3263
48
            TypeSystemClang::DeclContextGetAsDeclContext(
3264
48
                dwarf->GetDeclContextContainingUID(die.GetID()));
3265
48
        if (clang::NamespaceDecl *ns_decl =
3266
48
                TypeSystemClang::DeclContextGetAsNamespaceDecl(
3267
48
                    imported_decl_ctx))
3268
48
          decl = m_ast.CreateUsingDirectiveDeclaration(
3269
48
              decl_context, OptionalClangModuleID(), ns_decl);
3270
48
      }
3271
48
    }
3272
48
    break;
3273
6.70k
  }
3274
0
  default:
3275
0
    break;
3276
8.17k
  }
3277
3278
8.17k
  m_die_to_decl[die.GetDIE()] = decl;
3279
3280
8.17k
  return decl;
3281
8.17k
}
3282
3283
clang::DeclContext *
3284
290k
DWARFASTParserClang::GetClangDeclContextForDIE(const DWARFDIE &die) {
3285
290k
  if (die) {
3286
290k
    clang::DeclContext *decl_ctx = GetCachedClangDeclContextForDIE(die);
3287
290k
    if (decl_ctx)
3288
282k
      return decl_ctx;
3289
3290
8.48k
    bool try_parsing_type = true;
3291
8.48k
    switch (die.Tag()) {
3292
2.56k
    case DW_TAG_compile_unit:
3293
2.56k
    case DW_TAG_partial_unit:
3294
2.56k
      decl_ctx = m_ast.GetTranslationUnitDecl();
3295
2.56k
      try_parsing_type = false;
3296
2.56k
      break;
3297
3298
2.58k
    case DW_TAG_namespace:
3299
2.58k
      decl_ctx = ResolveNamespaceDIE(die);
3300
2.58k
      try_parsing_type = false;
3301
2.58k
      break;
3302
3303
106
    case DW_TAG_lexical_block:
3304
106
      decl_ctx = GetDeclContextForBlock(die);
3305
106
      try_parsing_type = false;
3306
106
      break;
3307
3308
3.23k
    default:
3309
3.23k
      break;
3310
8.48k
    }
3311
3312
8.48k
    if (decl_ctx == nullptr && 
try_parsing_type3.23k
) {
3313
3.23k
      Type *type = die.GetDWARF()->ResolveType(die);
3314
3.23k
      if (type)
3315
3.23k
        decl_ctx = GetCachedClangDeclContextForDIE(die);
3316
3.23k
    }
3317
3318
8.48k
    if (decl_ctx) {
3319
7.93k
      LinkDeclContextToDIE(decl_ctx, die);
3320
7.93k
      return decl_ctx;
3321
7.93k
    }
3322
8.48k
  }
3323
552
  return nullptr;
3324
290k
}
3325
3326
OptionalClangModuleID
3327
169k
DWARFASTParserClang::GetOwningClangModule(const DWARFDIE &die) {
3328
169k
  if (!die.IsValid())
3329
0
    return {};
3330
3331
651k
  
for (DWARFDIE parent = die.GetParent(); 169k
parent.IsValid();
3332
527k
       
parent = parent.GetParent()482k
) {
3333
527k
    const dw_tag_t tag = parent.Tag();
3334
527k
    if (tag == DW_TAG_module) {
3335
45.2k
      DWARFDIE module_die = parent;
3336
45.2k
      auto it = m_die_to_module.find(module_die.GetDIE());
3337
45.2k
      if (it != m_die_to_module.end())
3338
41.6k
        return it->second;
3339
3.58k
      const char *name =
3340
3.58k
          module_die.GetAttributeValueAsString(DW_AT_name, nullptr);
3341
3.58k
      if (!name)
3342
0
        return {};
3343
3344
3.58k
      OptionalClangModuleID id =
3345
3.58k
          m_ast.GetOrCreateClangModule(name, GetOwningClangModule(module_die));
3346
3.58k
      m_die_to_module.insert({module_die.GetDIE(), id});
3347
3.58k
      return id;
3348
3.58k
    }
3349
527k
  }
3350
123k
  return {};
3351
169k
}
3352
3353
221
static bool IsSubroutine(const DWARFDIE &die) {
3354
221
  switch (die.Tag()) {
3355
103
  case DW_TAG_subprogram:
3356
109
  case DW_TAG_inlined_subroutine:
3357
109
    return true;
3358
112
  default:
3359
112
    return false;
3360
221
  }
3361
221
}
3362
3363
106
static DWARFDIE GetContainingFunctionWithAbstractOrigin(const DWARFDIE &die) {
3364
215
  for (DWARFDIE candidate = die; candidate; 
candidate = candidate.GetParent()109
) {
3365
215
    if (IsSubroutine(candidate)) {
3366
106
      if (candidate.GetReferencedDIE(DW_AT_abstract_origin)) {
3367
3
        return candidate;
3368
103
      } else {
3369
103
        return DWARFDIE();
3370
103
      }
3371
106
    }
3372
215
  }
3373
0
  assert(0 && "Shouldn't call GetContainingFunctionWithAbstractOrigin on "
3374
0
              "something not in a function");
3375
0
  return DWARFDIE();
3376
106
}
3377
3378
3
static DWARFDIE FindAnyChildWithAbstractOrigin(const DWARFDIE &context) {
3379
3
  for (DWARFDIE candidate : context.children()) {
3380
3
    if (candidate.GetReferencedDIE(DW_AT_abstract_origin)) {
3381
3
      return candidate;
3382
3
    }
3383
3
  }
3384
0
  return DWARFDIE();
3385
3
}
3386
3387
static DWARFDIE FindFirstChildWithAbstractOrigin(const DWARFDIE &block,
3388
3
                                                 const DWARFDIE &function) {
3389
3
  assert(IsSubroutine(function));
3390
3
  for (DWARFDIE context = block; context != function.GetParent();
3391
3
       
context = context.GetParent()0
) {
3392
3
    assert(!IsSubroutine(context) || context == function);
3393
3
    if (DWARFDIE child = FindAnyChildWithAbstractOrigin(context)) {
3394
3
      return child;
3395
3
    }
3396
3
  }
3397
0
  return DWARFDIE();
3398
3
}
3399
3400
clang::DeclContext *
3401
106
DWARFASTParserClang::GetDeclContextForBlock(const DWARFDIE &die) {
3402
106
  assert(die.Tag() == DW_TAG_lexical_block);
3403
0
  DWARFDIE containing_function_with_abstract_origin =
3404
106
      GetContainingFunctionWithAbstractOrigin(die);
3405
106
  if (!containing_function_with_abstract_origin) {
3406
103
    return (clang::DeclContext *)ResolveBlockDIE(die);
3407
103
  }
3408
3
  DWARFDIE child = FindFirstChildWithAbstractOrigin(
3409
3
      die, containing_function_with_abstract_origin);
3410
3
  CompilerDeclContext decl_context =
3411
3
      GetDeclContextContainingUIDFromDWARF(child);
3412
3
  return (clang::DeclContext *)decl_context.GetOpaqueDeclContext();
3413
106
}
3414
3415
103
clang::BlockDecl *DWARFASTParserClang::ResolveBlockDIE(const DWARFDIE &die) {
3416
103
  if (die && die.Tag() == DW_TAG_lexical_block) {
3417
103
    clang::BlockDecl *decl =
3418
103
        llvm::cast_or_null<clang::BlockDecl>(m_die_to_decl_ctx[die.GetDIE()]);
3419
3420
103
    if (!decl) {
3421
103
      DWARFDIE decl_context_die;
3422
103
      clang::DeclContext *decl_context =
3423
103
          GetClangDeclContextContainingDIE(die, &decl_context_die);
3424
103
      decl =
3425
103
          m_ast.CreateBlockDeclaration(decl_context, GetOwningClangModule(die));
3426
3427
103
      if (decl)
3428
103
        LinkDeclContextToDIE((clang::DeclContext *)decl, die);
3429
103
    }
3430
3431
103
    return decl;
3432
103
  }
3433
0
  return nullptr;
3434
103
}
3435
3436
clang::NamespaceDecl *
3437
2.58k
DWARFASTParserClang::ResolveNamespaceDIE(const DWARFDIE &die) {
3438
2.58k
  if (die && die.Tag() == DW_TAG_namespace) {
3439
    // See if we already parsed this namespace DIE and associated it with a
3440
    // uniqued namespace declaration
3441
2.58k
    clang::NamespaceDecl *namespace_decl =
3442
2.58k
        static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die.GetDIE()]);
3443
2.58k
    if (namespace_decl)
3444
0
      return namespace_decl;
3445
2.58k
    else {
3446
2.58k
      const char *namespace_name = die.GetName();
3447
2.58k
      clang::DeclContext *containing_decl_ctx =
3448
2.58k
          GetClangDeclContextContainingDIE(die, nullptr);
3449
2.58k
      bool is_inline =
3450
2.58k
          die.GetAttributeValueAsUnsigned(DW_AT_export_symbols, 0) != 0;
3451
3452
2.58k
      namespace_decl = m_ast.GetUniqueNamespaceDeclaration(
3453
2.58k
          namespace_name, containing_decl_ctx, GetOwningClangModule(die),
3454
2.58k
          is_inline);
3455
2.58k
      Log *log =
3456
2.58k
          nullptr; // (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
3457
2.58k
      if (log) {
3458
0
        SymbolFileDWARF *dwarf = die.GetDWARF();
3459
0
        if (namespace_name) {
3460
0
          dwarf->GetObjectFile()->GetModule()->LogMessage(
3461
0
              log,
3462
0
              "ASTContext => %p: 0x%8.8" PRIx64
3463
0
              ": DW_TAG_namespace with DW_AT_name(\"%s\") => "
3464
0
              "clang::NamespaceDecl *%p (original = %p)",
3465
0
              static_cast<void *>(&m_ast.getASTContext()), die.GetID(),
3466
0
              namespace_name, static_cast<void *>(namespace_decl),
3467
0
              static_cast<void *>(namespace_decl->getOriginalNamespace()));
3468
0
        } else {
3469
0
          dwarf->GetObjectFile()->GetModule()->LogMessage(
3470
0
              log,
3471
0
              "ASTContext => %p: 0x%8.8" PRIx64
3472
0
              ": DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p "
3473
0
              "(original = %p)",
3474
0
              static_cast<void *>(&m_ast.getASTContext()), die.GetID(),
3475
0
              static_cast<void *>(namespace_decl),
3476
0
              static_cast<void *>(namespace_decl->getOriginalNamespace()));
3477
0
        }
3478
0
      }
3479
3480
2.58k
      if (namespace_decl)
3481
2.58k
        LinkDeclContextToDIE((clang::DeclContext *)namespace_decl, die);
3482
2.58k
      return namespace_decl;
3483
2.58k
    }
3484
2.58k
  }
3485
0
  return nullptr;
3486
2.58k
}
3487
3488
clang::DeclContext *DWARFASTParserClang::GetClangDeclContextContainingDIE(
3489
143k
    const DWARFDIE &die, DWARFDIE *decl_ctx_die_copy) {
3490
143k
  SymbolFileDWARF *dwarf = die.GetDWARF();
3491
3492
143k
  DWARFDIE decl_ctx_die = dwarf->GetDeclContextDIEContainingDIE(die);
3493
3494
143k
  if (decl_ctx_die_copy)
3495
66.3k
    *decl_ctx_die_copy = decl_ctx_die;
3496
3497
143k
  if (decl_ctx_die) {
3498
143k
    clang::DeclContext *clang_decl_ctx =
3499
143k
        GetClangDeclContextForDIE(decl_ctx_die);
3500
143k
    if (clang_decl_ctx)
3501
143k
      return clang_decl_ctx;
3502
143k
  }
3503
9
  return m_ast.GetTranslationUnitDecl();
3504
143k
}
3505
3506
clang::DeclContext *
3507
294k
DWARFASTParserClang::GetCachedClangDeclContextForDIE(const DWARFDIE &die) {
3508
294k
  if (die) {
3509
294k
    DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find(die.GetDIE());
3510
294k
    if (pos != m_die_to_decl_ctx.end())
3511
285k
      return pos->second;
3512
294k
  }
3513
9.36k
  return nullptr;
3514
294k
}
3515
3516
void DWARFASTParserClang::LinkDeclContextToDIE(clang::DeclContext *decl_ctx,
3517
90.2k
                                               const DWARFDIE &die) {
3518
90.2k
  m_die_to_decl_ctx[die.GetDIE()] = decl_ctx;
3519
  // There can be many DIEs for a single decl context
3520
  // m_decl_ctx_to_die[decl_ctx].insert(die.GetDIE());
3521
90.2k
  m_decl_ctx_to_die.insert(std::make_pair(decl_ctx, die));
3522
90.2k
}
3523
3524
bool DWARFASTParserClang::CopyUniqueClassMethodTypes(
3525
    const DWARFDIE &src_class_die, const DWARFDIE &dst_class_die,
3526
4
    lldb_private::Type *class_type, std::vector<DWARFDIE> &failures) {
3527
4
  if (!class_type || !src_class_die || !dst_class_die)
3528
0
    return false;
3529
4
  if (src_class_die.Tag() != dst_class_die.Tag())
3530
0
    return false;
3531
3532
  // We need to complete the class type so we can get all of the method types
3533
  // parsed so we can then unique those types to their equivalent counterparts
3534
  // in "dst_cu" and "dst_class_die"
3535
4
  class_type->GetFullCompilerType();
3536
3537
4
  DWARFDIE src_die;
3538
4
  DWARFDIE dst_die;
3539
4
  UniqueCStringMap<DWARFDIE> src_name_to_die;
3540
4
  UniqueCStringMap<DWARFDIE> dst_name_to_die;
3541
4
  UniqueCStringMap<DWARFDIE> src_name_to_die_artificial;
3542
4
  UniqueCStringMap<DWARFDIE> dst_name_to_die_artificial;
3543
8
  for (src_die = src_class_die.GetFirstChild(); src_die.IsValid();
3544
4
       src_die = src_die.GetSibling()) {
3545
4
    if (src_die.Tag() == DW_TAG_subprogram) {
3546
      // Make sure this is a declaration and not a concrete instance by looking
3547
      // for DW_AT_declaration set to 1. Sometimes concrete function instances
3548
      // are placed inside the class definitions and shouldn't be included in
3549
      // the list of things are are tracking here.
3550
4
      if (src_die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) == 1) {
3551
4
        const char *src_name = src_die.GetMangledName();
3552
4
        if (src_name) {
3553
4
          ConstString src_const_name(src_name);
3554
4
          if (src_die.GetAttributeValueAsUnsigned(DW_AT_artificial, 0))
3555
0
            src_name_to_die_artificial.Append(src_const_name, src_die);
3556
4
          else
3557
4
            src_name_to_die.Append(src_const_name, src_die);
3558
4
        }
3559
4
      }
3560
4
    }
3561
4
  }
3562
8
  for (dst_die = dst_class_die.GetFirstChild(); dst_die.IsValid();
3563
4
       dst_die = dst_die.GetSibling()) {
3564
4
    if (dst_die.Tag() == DW_TAG_subprogram) {
3565
      // Make sure this is a declaration and not a concrete instance by looking
3566
      // for DW_AT_declaration set to 1. Sometimes concrete function instances
3567
      // are placed inside the class definitions and shouldn't be included in
3568
      // the list of things are are tracking here.
3569
4
      if (dst_die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) == 1) {
3570
4
        const char *dst_name = dst_die.GetMangledName();
3571
4
        if (dst_name) {
3572
4
          ConstString dst_const_name(dst_name);
3573
4
          if (dst_die.GetAttributeValueAsUnsigned(DW_AT_artificial, 0))
3574
0
            dst_name_to_die_artificial.Append(dst_const_name, dst_die);
3575
4
          else
3576
4
            dst_name_to_die.Append(dst_const_name, dst_die);
3577
4
        }
3578
4
      }
3579
4
    }
3580
4
  }
3581
4
  const uint32_t src_size = src_name_to_die.GetSize();
3582
4
  const uint32_t dst_size = dst_name_to_die.GetSize();
3583
4
  Log *log = nullptr; // (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO |
3584
                      // DWARF_LOG_TYPE_COMPLETION));
3585
3586
  // Is everything kosher so we can go through the members at top speed?
3587
4
  bool fast_path = true;
3588
3589
4
  if (src_size != dst_size) {
3590
0
    if (src_size != 0 && dst_size != 0) {
3591
0
      LLDB_LOGF(log,
3592
0
                "warning: trying to unique class DIE 0x%8.8x to 0x%8.8x, "
3593
0
                "but they didn't have the same size (src=%d, dst=%d)",
3594
0
                src_class_die.GetOffset(), dst_class_die.GetOffset(), src_size,
3595
0
                dst_size);
3596
0
    }
3597
3598
0
    fast_path = false;
3599
0
  }
3600
3601
4
  uint32_t idx;
3602
3603
4
  if (fast_path) {
3604
8
    for (idx = 0; idx < src_size; 
++idx4
) {
3605
4
      src_die = src_name_to_die.GetValueAtIndexUnchecked(idx);
3606
4
      dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
3607
3608
4
      if (src_die.Tag() != dst_die.Tag()) {
3609
0
        LLDB_LOGF(log,
3610
0
                  "warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, "
3611
0
                  "but 0x%8.8x (%s) tags didn't match 0x%8.8x (%s)",
3612
0
                  src_class_die.GetOffset(), dst_class_die.GetOffset(),
3613
0
                  src_die.GetOffset(), src_die.GetTagAsCString(),
3614
0
                  dst_die.GetOffset(), dst_die.GetTagAsCString());
3615
0
        fast_path = false;
3616
0
      }
3617
3618
4
      const char *src_name = src_die.GetMangledName();
3619
4
      const char *dst_name = dst_die.GetMangledName();
3620
3621
      // Make sure the names match
3622
4
      if (src_name == dst_name || 
(strcmp(src_name, dst_name) == 0)2
)
3623
2
        continue;
3624
3625
2
      LLDB_LOGF(log,
3626
2
                "warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, "
3627
2
                "but 0x%8.8x (%s) names didn't match 0x%8.8x (%s)",
3628
2
                src_class_die.GetOffset(), dst_class_die.GetOffset(),
3629
2
                src_die.GetOffset(), src_name, dst_die.GetOffset(), dst_name);
3630
3631
2
      fast_path = false;
3632
2
    }
3633
4
  }
3634
3635
4
  DWARFASTParserClang *src_dwarf_ast_parser =
3636
4
      static_cast<DWARFASTParserClang *>(
3637
4
          SymbolFileDWARF::GetDWARFParser(*src_die.GetCU()));
3638
4
  DWARFASTParserClang *dst_dwarf_ast_parser =
3639
4
      static_cast<DWARFASTParserClang *>(
3640
4
          SymbolFileDWARF::GetDWARFParser(*dst_die.GetCU()));
3641
3642
  // Now do the work of linking the DeclContexts and Types.
3643
4
  if (fast_path) {
3644
    // We can do this quickly.  Just run across the tables index-for-index
3645
    // since we know each node has matching names and tags.
3646
4
    for (idx = 0; idx < src_size; 
++idx2
) {
3647
2
      src_die = src_name_to_die.GetValueAtIndexUnchecked(idx);
3648
2
      dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
3649
3650
2
      clang::DeclContext *src_decl_ctx =
3651
2
          src_dwarf_ast_parser->m_die_to_decl_ctx[src_die.GetDIE()];
3652
2
      if (src_decl_ctx) {
3653
0
        LLDB_LOGF(log, "uniquing decl context %p from 0x%8.8x for 0x%8.8x",
3654
0
                  static_cast<void *>(src_decl_ctx), src_die.GetOffset(),
3655
0
                  dst_die.GetOffset());
3656
0
        dst_dwarf_ast_parser->LinkDeclContextToDIE(src_decl_ctx, dst_die);
3657
2
      } else {
3658
2
        LLDB_LOGF(log,
3659
2
                  "warning: tried to unique decl context from 0x%8.8x for "
3660
2
                  "0x%8.8x, but none was found",
3661
2
                  src_die.GetOffset(), dst_die.GetOffset());
3662
2
      }
3663
3664
2
      Type *src_child_type =
3665
2
          dst_die.GetDWARF()->GetDIEToType()[src_die.GetDIE()];
3666
2
      if (src_child_type) {
3667
1
        LLDB_LOGF(log,
3668
1
                  "uniquing type %p (uid=0x%" PRIx64
3669
1
                  ") from 0x%8.8x for 0x%8.8x",
3670
1
                  static_cast<void *>(src_child_type), src_child_type->GetID(),
3671
1
                  src_die.GetOffset(), dst_die.GetOffset());
3672
1
        dst_die.GetDWARF()->GetDIEToType()[dst_die.GetDIE()] = src_child_type;
3673
1
      } else {
3674
1
        LLDB_LOGF(log,
3675
1
                  "warning: tried to unique lldb_private::Type from "
3676
1
                  "0x%8.8x for 0x%8.8x, but none was found",
3677
1
                  src_die.GetOffset(), dst_die.GetOffset());
3678
1
      }
3679
2
    }
3680
2
  } else {
3681
    // We must do this slowly.  For each member of the destination, look up a
3682
    // member in the source with the same name, check its tag, and unique them
3683
    // if everything matches up.  Report failures.
3684
3685
2
    if (!src_name_to_die.IsEmpty() && !dst_name_to_die.IsEmpty()) {
3686
2
      src_name_to_die.Sort();
3687
3688
4
      for (idx = 0; idx < dst_size; 
++idx2
) {
3689
2
        ConstString dst_name = dst_name_to_die.GetCStringAtIndex(idx);
3690
2
        dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
3691
2
        src_die = src_name_to_die.Find(dst_name, DWARFDIE());
3692
3693
2
        if (src_die && 
(src_die.Tag() == dst_die.Tag())0
) {
3694
0
          clang::DeclContext *src_decl_ctx =
3695
0
              src_dwarf_ast_parser->m_die_to_decl_ctx[src_die.GetDIE()];
3696
0
          if (src_decl_ctx) {
3697
0
            LLDB_LOGF(log, "uniquing decl context %p from 0x%8.8x for 0x%8.8x",
3698
0
                      static_cast<void *>(src_decl_ctx), src_die.GetOffset(),
3699
0
                      dst_die.GetOffset());
3700
0
            dst_dwarf_ast_parser->LinkDeclContextToDIE(src_decl_ctx, dst_die);
3701
0
          } else {
3702
0
            LLDB_LOGF(log,
3703
0
                      "warning: tried to unique decl context from 0x%8.8x "
3704
0
                      "for 0x%8.8x, but none was found",
3705
0
                      src_die.GetOffset(), dst_die.GetOffset());
3706
0
          }
3707
3708
0
          Type *src_child_type =
3709
0
              dst_die.GetDWARF()->GetDIEToType()[src_die.GetDIE()];
3710
0
          if (src_child_type) {
3711
0
            LLDB_LOGF(
3712
0
                log,
3713
0
                "uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
3714
0
                static_cast<void *>(src_child_type), src_child_type->GetID(),
3715
0
                src_die.GetOffset(), dst_die.GetOffset());
3716
0
            dst_die.GetDWARF()->GetDIEToType()[dst_die.GetDIE()] =
3717
0
                src_child_type;
3718
0
          } else {
3719
0
            LLDB_LOGF(log,
3720
0
                      "warning: tried to unique lldb_private::Type from "
3721
0
                      "0x%8.8x for 0x%8.8x, but none was found",
3722
0
                      src_die.GetOffset(), dst_die.GetOffset());
3723
0
          }
3724
2
        } else {
3725
2
          LLDB_LOGF(log, "warning: couldn't find a match for 0x%8.8x",
3726
2
                    dst_die.GetOffset());
3727
3728
2
          failures.push_back(dst_die);
3729
2
        }
3730
2
      }
3731
2
    }
3732
2
  }
3733
3734
4
  const uint32_t src_size_artificial = src_name_to_die_artificial.GetSize();
3735
4
  const uint32_t dst_size_artificial = dst_name_to_die_artificial.GetSize();
3736
3737
4
  if (src_size_artificial && 
dst_size_artificial0
) {
3738
0
    dst_name_to_die_artificial.Sort();
3739
3740
0
    for (idx = 0; idx < src_size_artificial; ++idx) {
3741
0
      ConstString src_name_artificial =
3742
0
          src_name_to_die_artificial.GetCStringAtIndex(idx);
3743
0
      src_die = src_name_to_die_artificial.GetValueAtIndexUnchecked(idx);
3744
0
      dst_die =
3745
0
          dst_name_to_die_artificial.Find(src_name_artificial, DWARFDIE());
3746
3747
0
      if (dst_die) {
3748
        // Both classes have the artificial types, link them
3749
0
        clang::DeclContext *src_decl_ctx =
3750
0
            src_dwarf_ast_parser->m_die_to_decl_ctx[src_die.GetDIE()];
3751
0
        if (src_decl_ctx) {
3752
0
          LLDB_LOGF(log, "uniquing decl context %p from 0x%8.8x for 0x%8.8x",
3753
0
                    static_cast<void *>(src_decl_ctx), src_die.GetOffset(),
3754
0
                    dst_die.GetOffset());
3755
0
          dst_dwarf_ast_parser->LinkDeclContextToDIE(src_decl_ctx, dst_die);
3756
0
        } else {
3757
0
          LLDB_LOGF(log,
3758
0
                    "warning: tried to unique decl context from 0x%8.8x "
3759
0
                    "for 0x%8.8x, but none was found",
3760
0
                    src_die.GetOffset(), dst_die.GetOffset());
3761
0
        }
3762
3763
0
        Type *src_child_type =
3764
0
            dst_die.GetDWARF()->GetDIEToType()[src_die.GetDIE()];
3765
0
        if (src_child_type) {
3766
0
          LLDB_LOGF(
3767
0
              log,
3768
0
              "uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
3769
0
              static_cast<void *>(src_child_type), src_child_type->GetID(),
3770
0
              src_die.GetOffset(), dst_die.GetOffset());
3771
0
          dst_die.GetDWARF()->GetDIEToType()[dst_die.GetDIE()] = src_child_type;
3772
0
        } else {
3773
0
          LLDB_LOGF(log,
3774
0
                    "warning: tried to unique lldb_private::Type from "
3775
0
                    "0x%8.8x for 0x%8.8x, but none was found",
3776
0
                    src_die.GetOffset(), dst_die.GetOffset());
3777
0
        }
3778
0
      }
3779
0
    }
3780
0
  }
3781
3782
4
  if (dst_size_artificial) {
3783
0
    for (idx = 0; idx < dst_size_artificial; ++idx) {
3784
0
      ConstString dst_name_artificial =
3785
0
          dst_name_to_die_artificial.GetCStringAtIndex(idx);
3786
0
      dst_die = dst_name_to_die_artificial.GetValueAtIndexUnchecked(idx);
3787
0
      LLDB_LOGF(log,
3788
0
                "warning: need to create artificial method for 0x%8.8x for "
3789
0
                "method '%s'",
3790
0
                dst_die.GetOffset(), dst_name_artificial.GetCString());
3791
3792
0
      failures.push_back(dst_die);
3793
0
    }
3794
0
  }
3795
3796
4
  return !failures.empty();
3797
4
}