Coverage Report

Created: 2022-01-22 13:19

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- ClangExpressionDeclMap.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 "ClangExpressionDeclMap.h"
10
11
#include "ClangASTSource.h"
12
#include "ClangModulesDeclVendor.h"
13
#include "ClangPersistentVariables.h"
14
#include "ClangUtil.h"
15
16
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
17
#include "lldb/Core/Address.h"
18
#include "lldb/Core/Module.h"
19
#include "lldb/Core/ModuleSpec.h"
20
#include "lldb/Core/ValueObjectConstResult.h"
21
#include "lldb/Core/ValueObjectVariable.h"
22
#include "lldb/Expression/DiagnosticManager.h"
23
#include "lldb/Expression/Materializer.h"
24
#include "lldb/Symbol/CompileUnit.h"
25
#include "lldb/Symbol/CompilerDecl.h"
26
#include "lldb/Symbol/CompilerDeclContext.h"
27
#include "lldb/Symbol/Function.h"
28
#include "lldb/Symbol/ObjectFile.h"
29
#include "lldb/Symbol/SymbolContext.h"
30
#include "lldb/Symbol/SymbolFile.h"
31
#include "lldb/Symbol/SymbolVendor.h"
32
#include "lldb/Symbol/Type.h"
33
#include "lldb/Symbol/TypeList.h"
34
#include "lldb/Symbol/Variable.h"
35
#include "lldb/Symbol/VariableList.h"
36
#include "lldb/Target/ExecutionContext.h"
37
#include "lldb/Target/Process.h"
38
#include "lldb/Target/RegisterContext.h"
39
#include "lldb/Target/StackFrame.h"
40
#include "lldb/Target/Target.h"
41
#include "lldb/Target/Thread.h"
42
#include "lldb/Utility/Endian.h"
43
#include "lldb/Utility/Log.h"
44
#include "lldb/Utility/RegisterValue.h"
45
#include "lldb/Utility/Status.h"
46
#include "lldb/lldb-private.h"
47
#include "clang/AST/ASTConsumer.h"
48
#include "clang/AST/ASTContext.h"
49
#include "clang/AST/ASTImporter.h"
50
#include "clang/AST/Decl.h"
51
#include "clang/AST/DeclarationName.h"
52
#include "clang/AST/RecursiveASTVisitor.h"
53
54
#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
55
#include "Plugins/LanguageRuntime/CPlusPlus/CPPLanguageRuntime.h"
56
#include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h"
57
58
using namespace lldb;
59
using namespace lldb_private;
60
using namespace clang;
61
62
static const char *g_lldb_local_vars_namespace_cstr = "$__lldb_local_vars";
63
64
ClangExpressionDeclMap::ClangExpressionDeclMap(
65
    bool keep_result_in_memory,
66
    Materializer::PersistentVariableDelegate *result_delegate,
67
    const lldb::TargetSP &target,
68
    const std::shared_ptr<ClangASTImporter> &importer, ValueObject *ctx_obj)
69
    : ClangASTSource(target, importer), m_found_entities(), m_struct_members(),
70
      m_keep_result_in_memory(keep_result_in_memory),
71
      m_result_delegate(result_delegate), m_ctx_obj(ctx_obj), m_parser_vars(),
72
12.7k
      m_struct_vars() {
73
12.7k
  EnableStructVars();
74
12.7k
}
75
76
12.7k
ClangExpressionDeclMap::~ClangExpressionDeclMap() {
77
  // Note: The model is now that the parser's AST context and all associated
78
  //   data does not vanish until the expression has been executed.  This means
79
  //   that valuable lookup data (like namespaces) doesn't vanish, but
80
81
12.7k
  DidParse();
82
12.7k
  DisableStructVars();
83
12.7k
}
84
85
bool ClangExpressionDeclMap::WillParse(ExecutionContext &exe_ctx,
86
12.7k
                                       Materializer *materializer) {
87
12.7k
  EnableParserVars();
88
12.7k
  m_parser_vars->m_exe_ctx = exe_ctx;
89
90
12.7k
  Target *target = exe_ctx.GetTargetPtr();
91
12.7k
  if (exe_ctx.GetFramePtr())
92
10.0k
    m_parser_vars->m_sym_ctx =
93
10.0k
        exe_ctx.GetFramePtr()->GetSymbolContext(lldb::eSymbolContextEverything);
94
2.76k
  else if (exe_ctx.GetThreadPtr() &&
95
2.76k
           
exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0)2.39k
)
96
2.39k
    m_parser_vars->m_sym_ctx =
97
2.39k
        exe_ctx.GetThreadPtr()->GetStackFrameAtIndex(0)->GetSymbolContext(
98
2.39k
            lldb::eSymbolContextEverything);
99
378
  else if (exe_ctx.GetProcessPtr()) {
100
14
    m_parser_vars->m_sym_ctx.Clear(true);
101
14
    m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
102
364
  } else if (target) {
103
364
    m_parser_vars->m_sym_ctx.Clear(true);
104
364
    m_parser_vars->m_sym_ctx.target_sp = exe_ctx.GetTargetSP();
105
364
  }
106
107
12.7k
  if (target) {
108
12.7k
    m_parser_vars->m_persistent_vars = llvm::cast<ClangPersistentVariables>(
109
12.7k
        target->GetPersistentExpressionStateForLanguage(eLanguageTypeC));
110
111
12.7k
    if (!ScratchTypeSystemClang::GetForTarget(*target))
112
0
      return false;
113
12.7k
  }
114
115
12.7k
  m_parser_vars->m_target_info = GetTargetInfo();
116
12.7k
  m_parser_vars->m_materializer = materializer;
117
118
12.7k
  return true;
119
12.7k
}
120
121
void ClangExpressionDeclMap::InstallCodeGenerator(
122
12.7k
    clang::ASTConsumer *code_gen) {
123
12.7k
  assert(m_parser_vars);
124
0
  m_parser_vars->m_code_gen = code_gen;
125
12.7k
}
126
127
void ClangExpressionDeclMap::InstallDiagnosticManager(
128
12.7k
    DiagnosticManager &diag_manager) {
129
12.7k
  assert(m_parser_vars);
130
0
  m_parser_vars->m_diagnostics = &diag_manager;
131
12.7k
}
132
133
16.1k
void ClangExpressionDeclMap::DidParse() {
134
16.1k
  if (m_parser_vars && 
m_parser_vars->m_persistent_vars12.7k
) {
135
12.7k
    for (size_t entity_index = 0, num_entities = m_found_entities.GetSize();
136
40.5k
         entity_index < num_entities; 
++entity_index27.8k
) {
137
27.8k
      ExpressionVariableSP var_sp(
138
27.8k
          m_found_entities.GetVariableAtIndex(entity_index));
139
27.8k
      if (var_sp)
140
27.8k
        llvm::cast<ClangExpressionVariable>(var_sp.get())
141
27.8k
            ->DisableParserVars(GetParserID());
142
27.8k
    }
143
144
12.7k
    for (size_t pvar_index = 0,
145
12.7k
                num_pvars = m_parser_vars->m_persistent_vars->GetSize();
146
347k
         pvar_index < num_pvars; 
++pvar_index334k
) {
147
334k
      ExpressionVariableSP pvar_sp(
148
334k
          m_parser_vars->m_persistent_vars->GetVariableAtIndex(pvar_index));
149
334k
      if (ClangExpressionVariable *clang_var =
150
334k
              llvm::dyn_cast<ClangExpressionVariable>(pvar_sp.get()))
151
334k
        clang_var->DisableParserVars(GetParserID());
152
334k
    }
153
154
12.7k
    DisableParserVars();
155
12.7k
  }
156
16.1k
}
157
158
// Interface for IRForTarget
159
160
12.7k
ClangExpressionDeclMap::TargetInfo ClangExpressionDeclMap::GetTargetInfo() {
161
12.7k
  assert(m_parser_vars.get());
162
163
0
  TargetInfo ret;
164
165
12.7k
  ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
166
167
12.7k
  Process *process = exe_ctx.GetProcessPtr();
168
12.7k
  if (process) {
169
12.4k
    ret.byte_order = process->GetByteOrder();
170
12.4k
    ret.address_byte_size = process->GetAddressByteSize();
171
12.4k
  } else {
172
364
    Target *target = exe_ctx.GetTargetPtr();
173
364
    if (target) {
174
364
      ret.byte_order = target->GetArchitecture().GetByteOrder();
175
364
      ret.address_byte_size = target->GetArchitecture().GetAddressByteSize();
176
364
    }
177
364
  }
178
179
12.7k
  return ret;
180
12.7k
}
181
182
TypeFromUser ClangExpressionDeclMap::DeportType(TypeSystemClang &target,
183
                                                TypeSystemClang &source,
184
8.78k
                                                TypeFromParser parser_type) {
185
8.78k
  assert(&target == GetScratchContext(*m_target));
186
0
  assert((TypeSystem *)&source == parser_type.GetTypeSystem());
187
0
  assert(&source.getASTContext() == m_ast_context);
188
189
0
  return TypeFromUser(m_ast_importer_sp->DeportType(target, parser_type));
190
8.78k
}
191
192
bool ClangExpressionDeclMap::AddPersistentVariable(const NamedDecl *decl,
193
                                                   ConstString name,
194
                                                   TypeFromParser parser_type,
195
                                                   bool is_result,
196
8.78k
                                                   bool is_lvalue) {
197
8.78k
  assert(m_parser_vars.get());
198
199
0
  TypeSystemClang *ast =
200
8.78k
      llvm::dyn_cast_or_null<TypeSystemClang>(parser_type.GetTypeSystem());
201
8.78k
  if (ast == nullptr)
202
0
    return false;
203
204
  // Check if we already declared a persistent variable with the same name.
205
8.78k
  if (lldb::ExpressionVariableSP conflicting_var =
206
8.78k
          m_parser_vars->m_persistent_vars->GetVariable(name)) {
207
6
    std::string msg = llvm::formatv("redefinition of persistent variable '{0}'",
208
6
                                    name).str();
209
6
    m_parser_vars->m_diagnostics->AddDiagnostic(
210
6
        msg, DiagnosticSeverity::eDiagnosticSeverityError,
211
6
        DiagnosticOrigin::eDiagnosticOriginLLDB);
212
6
    return false;
213
6
  }
214
215
8.78k
  if (m_parser_vars->m_materializer && is_result) {
216
8.75k
    Status err;
217
218
8.75k
    ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
219
8.75k
    Target *target = exe_ctx.GetTargetPtr();
220
8.75k
    if (target == nullptr)
221
0
      return false;
222
223
8.75k
    auto *clang_ast_context = GetScratchContext(*target);
224
8.75k
    if (!clang_ast_context)
225
0
      return false;
226
227
8.75k
    TypeFromUser user_type = DeportType(*clang_ast_context, *ast, parser_type);
228
229
8.75k
    uint32_t offset = m_parser_vars->m_materializer->AddResultVariable(
230
8.75k
        user_type, is_lvalue, m_keep_result_in_memory, m_result_delegate, err);
231
232
8.75k
    ClangExpressionVariable *var = new ClangExpressionVariable(
233
8.75k
        exe_ctx.GetBestExecutionContextScope(), name, user_type,
234
8.75k
        m_parser_vars->m_target_info.byte_order,
235
8.75k
        m_parser_vars->m_target_info.address_byte_size);
236
237
8.75k
    m_found_entities.AddNewlyConstructedVariable(var);
238
239
8.75k
    var->EnableParserVars(GetParserID());
240
241
8.75k
    ClangExpressionVariable::ParserVars *parser_vars =
242
8.75k
        var->GetParserVars(GetParserID());
243
244
8.75k
    parser_vars->m_named_decl = decl;
245
246
8.75k
    var->EnableJITVars(GetParserID());
247
248
8.75k
    ClangExpressionVariable::JITVars *jit_vars = var->GetJITVars(GetParserID());
249
250
8.75k
    jit_vars->m_offset = offset;
251
252
8.75k
    return true;
253
8.75k
  }
254
255
30
  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
256
30
  ExecutionContext &exe_ctx = m_parser_vars->m_exe_ctx;
257
30
  Target *target = exe_ctx.GetTargetPtr();
258
30
  if (target == nullptr)
259
0
    return false;
260
261
30
  TypeSystemClang *context = GetScratchContext(*target);
262
30
  if (!context)
263
0
    return false;
264
265
30
  TypeFromUser user_type = DeportType(*context, *ast, parser_type);
266
267
30
  if (!user_type.GetOpaqueQualType()) {
268
0
    LLDB_LOG(log, "Persistent variable's type wasn't copied successfully");
269
0
    return false;
270
0
  }
271
272
30
  if (!m_parser_vars->m_target_info.IsValid())
273
0
    return false;
274
275
30
  if (!m_parser_vars->m_persistent_vars)
276
0
    return false;
277
278
30
  ClangExpressionVariable *var = llvm::cast<ClangExpressionVariable>(
279
30
      m_parser_vars->m_persistent_vars
280
30
          ->CreatePersistentVariable(
281
30
              exe_ctx.GetBestExecutionContextScope(), name, user_type,
282
30
              m_parser_vars->m_target_info.byte_order,
283
30
              m_parser_vars->m_target_info.address_byte_size)
284
30
          .get());
285
286
30
  if (!var)
287
0
    return false;
288
289
30
  var->m_frozen_sp->SetHasCompleteType();
290
291
30
  if (is_result)
292
0
    var->m_flags |= ClangExpressionVariable::EVNeedsFreezeDry;
293
30
  else
294
30
    var->m_flags |=
295
30
        ClangExpressionVariable::EVKeepInTarget; // explicitly-declared
296
                                                 // persistent variables should
297
                                                 // persist
298
299
30
  if (is_lvalue) {
300
0
    var->m_flags |= ClangExpressionVariable::EVIsProgramReference;
301
30
  } else {
302
30
    var->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
303
30
    var->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
304
30
  }
305
306
30
  if (m_keep_result_in_memory) {
307
30
    var->m_flags |= ClangExpressionVariable::EVKeepInTarget;
308
30
  }
309
310
30
  LLDB_LOG(log, "Created persistent variable with flags {0:x}", var->m_flags);
311
312
30
  var->EnableParserVars(GetParserID());
313
314
30
  ClangExpressionVariable::ParserVars *parser_vars =
315
30
      var->GetParserVars(GetParserID());
316
317
30
  parser_vars->m_named_decl = decl;
318
319
30
  return true;
320
30
}
321
322
bool ClangExpressionDeclMap::AddValueToStruct(const NamedDecl *decl,
323
                                              ConstString name,
324
                                              llvm::Value *value, size_t size,
325
18.0k
                                              lldb::offset_t alignment) {
326
18.0k
  assert(m_struct_vars.get());
327
0
  assert(m_parser_vars.get());
328
329
0
  bool is_persistent_variable = false;
330
331
18.0k
  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
332
333
18.0k
  m_struct_vars->m_struct_laid_out = false;
334
335
18.0k
  if (ClangExpressionVariable::FindVariableInList(m_struct_members, decl,
336
18.0k
                                                  GetParserID()))
337
29
    return true;
338
339
17.9k
  ClangExpressionVariable *var(ClangExpressionVariable::FindVariableInList(
340
17.9k
      m_found_entities, decl, GetParserID()));
341
342
17.9k
  if (!var && 
m_parser_vars->m_persistent_vars1.40k
) {
343
1.40k
    var = ClangExpressionVariable::FindVariableInList(
344
1.40k
        *m_parser_vars->m_persistent_vars, decl, GetParserID());
345
1.40k
    is_persistent_variable = true;
346
1.40k
  }
347
348
17.9k
  if (!var)
349
1.23k
    return false;
350
351
16.7k
  LLDB_LOG(log, "Adding value for (NamedDecl*){0} [{1} - {2}] to the structure",
352
16.7k
           decl, name, var->GetName());
353
354
  // We know entity->m_parser_vars is valid because we used a parser variable
355
  // to find it
356
357
16.7k
  ClangExpressionVariable::ParserVars *parser_vars =
358
16.7k
      llvm::cast<ClangExpressionVariable>(var)->GetParserVars(GetParserID());
359
360
16.7k
  parser_vars->m_llvm_value = value;
361
362
16.7k
  if (ClangExpressionVariable::JITVars *jit_vars =
363
16.7k
          llvm::cast<ClangExpressionVariable>(var)->GetJITVars(GetParserID())) {
364
    // We already laid this out; do not touch
365
366
8.75k
    LLDB_LOG(log, "Already placed at {0:x}", jit_vars->m_offset);
367
8.75k
  }
368
369
16.7k
  llvm::cast<ClangExpressionVariable>(var)->EnableJITVars(GetParserID());
370
371
16.7k
  ClangExpressionVariable::JITVars *jit_vars =
372
16.7k
      llvm::cast<ClangExpressionVariable>(var)->GetJITVars(GetParserID());
373
374
16.7k
  jit_vars->m_alignment = alignment;
375
16.7k
  jit_vars->m_size = size;
376
377
16.7k
  m_struct_members.AddVariable(var->shared_from_this());
378
379
16.7k
  if (m_parser_vars->m_materializer) {
380
16.7k
    uint32_t offset = 0;
381
382
16.7k
    Status err;
383
384
16.7k
    if (is_persistent_variable) {
385
165
      ExpressionVariableSP var_sp(var->shared_from_this());
386
165
      offset = m_parser_vars->m_materializer->AddPersistentVariable(
387
165
          var_sp, nullptr, err);
388
16.5k
    } else {
389
16.5k
      if (const lldb_private::Symbol *sym = parser_vars->m_lldb_sym)
390
5
        offset = m_parser_vars->m_materializer->AddSymbol(*sym, err);
391
16.5k
      else if (const RegisterInfo *reg_info = var->GetRegisterInfo())
392
33
        offset = m_parser_vars->m_materializer->AddRegister(*reg_info, err);
393
16.5k
      else if (parser_vars->m_lldb_var)
394
7.79k
        offset = m_parser_vars->m_materializer->AddVariable(
395
7.79k
            parser_vars->m_lldb_var, err);
396
16.5k
    }
397
398
16.7k
    if (!err.Success())
399
0
      return false;
400
401
16.7k
    LLDB_LOG(log, "Placed at {0:x}", offset);
402
403
16.7k
    jit_vars->m_offset =
404
16.7k
        offset; // TODO DoStructLayout() should not change this.
405
16.7k
  }
406
407
16.7k
  return true;
408
16.7k
}
409
410
8.92k
bool ClangExpressionDeclMap::DoStructLayout() {
411
8.92k
  assert(m_struct_vars.get());
412
413
8.92k
  if (m_struct_vars->m_struct_laid_out)
414
0
    return true;
415
416
8.92k
  if (!m_parser_vars->m_materializer)
417
0
    return false;
418
419
8.92k
  m_struct_vars->m_struct_alignment =
420
8.92k
      m_parser_vars->m_materializer->GetStructAlignment();
421
8.92k
  m_struct_vars->m_struct_size =
422
8.92k
      m_parser_vars->m_materializer->GetStructByteSize();
423
8.92k
  m_struct_vars->m_struct_laid_out = true;
424
8.92k
  return true;
425
8.92k
}
426
427
bool ClangExpressionDeclMap::GetStructInfo(uint32_t &num_elements, size_t &size,
428
8.92k
                                           lldb::offset_t &alignment) {
429
8.92k
  assert(m_struct_vars.get());
430
431
8.92k
  if (!m_struct_vars->m_struct_laid_out)
432
0
    return false;
433
434
8.92k
  num_elements = m_struct_members.GetSize();
435
8.92k
  size = m_struct_vars->m_struct_size;
436
8.92k
  alignment = m_struct_vars->m_struct_alignment;
437
438
8.92k
  return true;
439
8.92k
}
440
441
bool ClangExpressionDeclMap::GetStructElement(const NamedDecl *&decl,
442
                                              llvm::Value *&value,
443
                                              lldb::offset_t &offset,
444
                                              ConstString &name,
445
16.7k
                                              uint32_t index) {
446
16.7k
  assert(m_struct_vars.get());
447
448
16.7k
  if (!m_struct_vars->m_struct_laid_out)
449
0
    return false;
450
451
16.7k
  if (index >= m_struct_members.GetSize())
452
0
    return false;
453
454
16.7k
  ExpressionVariableSP member_sp(m_struct_members.GetVariableAtIndex(index));
455
456
16.7k
  if (!member_sp)
457
0
    return false;
458
459
16.7k
  ClangExpressionVariable::ParserVars *parser_vars =
460
16.7k
      llvm::cast<ClangExpressionVariable>(member_sp.get())
461
16.7k
          ->GetParserVars(GetParserID());
462
16.7k
  ClangExpressionVariable::JITVars *jit_vars =
463
16.7k
      llvm::cast<ClangExpressionVariable>(member_sp.get())
464
16.7k
          ->GetJITVars(GetParserID());
465
466
16.7k
  if (!parser_vars || !jit_vars || !member_sp->GetValueObject())
467
0
    return false;
468
469
16.7k
  decl = parser_vars->m_named_decl;
470
16.7k
  value = parser_vars->m_llvm_value;
471
16.7k
  offset = jit_vars->m_offset;
472
16.7k
  name = member_sp->GetName();
473
474
16.7k
  return true;
475
16.7k
}
476
477
bool ClangExpressionDeclMap::GetFunctionInfo(const NamedDecl *decl,
478
0
                                             uint64_t &ptr) {
479
0
  ClangExpressionVariable *entity(ClangExpressionVariable::FindVariableInList(
480
0
      m_found_entities, decl, GetParserID()));
481
482
0
  if (!entity)
483
0
    return false;
484
485
  // We know m_parser_vars is valid since we searched for the variable by its
486
  // NamedDecl
487
488
0
  ClangExpressionVariable::ParserVars *parser_vars =
489
0
      entity->GetParserVars(GetParserID());
490
491
0
  ptr = parser_vars->m_lldb_value.GetScalar().ULongLong();
492
493
0
  return true;
494
0
}
495
496
addr_t ClangExpressionDeclMap::GetSymbolAddress(Target &target,
497
                                                Process *process,
498
                                                ConstString name,
499
                                                lldb::SymbolType symbol_type,
500
185
                                                lldb_private::Module *module) {
501
185
  SymbolContextList sc_list;
502
503
185
  if (module)
504
0
    module->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
505
185
  else
506
185
    target.GetImages().FindSymbolsWithNameAndType(name, symbol_type, sc_list);
507
508
185
  const uint32_t num_matches = sc_list.GetSize();
509
185
  addr_t symbol_load_addr = LLDB_INVALID_ADDRESS;
510
511
185
  for (uint32_t i = 0;
512
375
       i < num_matches &&
513
375
       
(190
symbol_load_addr == 0190
||
symbol_load_addr == 190
LLDB_INVALID_ADDRESS190
);
514
190
       i++) {
515
190
    SymbolContext sym_ctx;
516
190
    sc_list.GetContextAtIndex(i, sym_ctx);
517
518
190
    const Address sym_address = sym_ctx.symbol->GetAddress();
519
520
190
    if (!sym_address.IsValid())
521
8
      continue;
522
523
182
    switch (sym_ctx.symbol->GetType()) {
524
0
    case eSymbolTypeCode:
525
0
    case eSymbolTypeTrampoline:
526
0
      symbol_load_addr = sym_address.GetCallableLoadAddress(&target);
527
0
      break;
528
529
0
    case eSymbolTypeResolver:
530
0
      symbol_load_addr = sym_address.GetCallableLoadAddress(&target, true);
531
0
      break;
532
533
0
    case eSymbolTypeReExported: {
534
0
      ConstString reexport_name = sym_ctx.symbol->GetReExportedSymbolName();
535
0
      if (reexport_name) {
536
0
        ModuleSP reexport_module_sp;
537
0
        ModuleSpec reexport_module_spec;
538
0
        reexport_module_spec.GetPlatformFileSpec() =
539
0
            sym_ctx.symbol->GetReExportedSymbolSharedLibrary();
540
0
        if (reexport_module_spec.GetPlatformFileSpec()) {
541
0
          reexport_module_sp =
542
0
              target.GetImages().FindFirstModule(reexport_module_spec);
543
0
          if (!reexport_module_sp) {
544
0
            reexport_module_spec.GetPlatformFileSpec().GetDirectory().Clear();
545
0
            reexport_module_sp =
546
0
                target.GetImages().FindFirstModule(reexport_module_spec);
547
0
          }
548
0
        }
549
0
        symbol_load_addr = GetSymbolAddress(
550
0
            target, process, sym_ctx.symbol->GetReExportedSymbolName(),
551
0
            symbol_type, reexport_module_sp.get());
552
0
      }
553
0
    } break;
554
555
0
    case eSymbolTypeData:
556
0
    case eSymbolTypeRuntime:
557
0
    case eSymbolTypeVariable:
558
0
    case eSymbolTypeLocal:
559
0
    case eSymbolTypeParam:
560
0
    case eSymbolTypeInvalid:
561
0
    case eSymbolTypeAbsolute:
562
0
    case eSymbolTypeException:
563
0
    case eSymbolTypeSourceFile:
564
0
    case eSymbolTypeHeaderFile:
565
0
    case eSymbolTypeObjectFile:
566
0
    case eSymbolTypeCommonBlock:
567
0
    case eSymbolTypeBlock:
568
0
    case eSymbolTypeVariableType:
569
0
    case eSymbolTypeLineEntry:
570
0
    case eSymbolTypeLineHeader:
571
0
    case eSymbolTypeScopeBegin:
572
0
    case eSymbolTypeScopeEnd:
573
0
    case eSymbolTypeAdditional:
574
0
    case eSymbolTypeCompiler:
575
0
    case eSymbolTypeInstrumentation:
576
0
    case eSymbolTypeUndefined:
577
89
    case eSymbolTypeObjCClass:
578
89
    case eSymbolTypeObjCMetaClass:
579
182
    case eSymbolTypeObjCIVar:
580
182
      symbol_load_addr = sym_address.GetLoadAddress(&target);
581
182
      break;
582
182
    }
583
182
  }
584
585
185
  if (symbol_load_addr == LLDB_INVALID_ADDRESS && 
process3
) {
586
3
    ObjCLanguageRuntime *runtime = ObjCLanguageRuntime::Get(*process);
587
588
3
    if (runtime) {
589
3
      symbol_load_addr = runtime->LookupRuntimeSymbol(name);
590
3
    }
591
3
  }
592
593
185
  return symbol_load_addr;
594
185
}
595
596
addr_t ClangExpressionDeclMap::GetSymbolAddress(ConstString name,
597
185
                                                lldb::SymbolType symbol_type) {
598
185
  assert(m_parser_vars.get());
599
600
185
  if (!m_parser_vars->m_exe_ctx.GetTargetPtr())
601
0
    return false;
602
603
185
  return GetSymbolAddress(m_parser_vars->m_exe_ctx.GetTargetRef(),
604
185
                          m_parser_vars->m_exe_ctx.GetProcessPtr(), name,
605
185
                          symbol_type);
606
185
}
607
608
lldb::VariableSP ClangExpressionDeclMap::FindGlobalVariable(
609
    Target &target, ModuleSP &module, ConstString name,
610
4.10k
    const CompilerDeclContext &namespace_decl) {
611
4.10k
  VariableList vars;
612
613
4.10k
  if (module && 
namespace_decl443
)
614
443
    module->FindGlobalVariables(name, namespace_decl, -1, vars);
615
3.66k
  else
616
3.66k
    target.GetImages().FindGlobalVariables(name, -1, vars);
617
618
4.10k
  if (vars.GetSize() == 0)
619
3.77k
    return VariableSP();
620
332
  return vars.GetVariableAtIndex(0);
621
4.10k
}
622
623
0
TypeSystemClang *ClangExpressionDeclMap::GetTypeSystemClang() {
624
0
  StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
625
0
  if (frame == nullptr)
626
0
    return nullptr;
627
628
0
  SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
629
0
                                                  lldb::eSymbolContextBlock);
630
0
  if (sym_ctx.block == nullptr)
631
0
    return nullptr;
632
633
0
  CompilerDeclContext frame_decl_context = sym_ctx.block->GetDeclContext();
634
0
  if (!frame_decl_context)
635
0
    return nullptr;
636
637
0
  return llvm::dyn_cast_or_null<TypeSystemClang>(
638
0
      frame_decl_context.GetTypeSystem());
639
0
}
640
641
// Interface for ClangASTSource
642
643
void ClangExpressionDeclMap::FindExternalVisibleDecls(
644
63.8k
    NameSearchContext &context) {
645
63.8k
  assert(m_ast_context);
646
647
0
  const ConstString name(context.m_decl_name.getAsString().c_str());
648
649
63.8k
  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
650
651
63.8k
  if (log) {
652
75
    if (!context.m_decl_context)
653
0
      LLDB_LOG(log,
654
75
               "ClangExpressionDeclMap::FindExternalVisibleDecls for "
655
75
               "'{0}' in a NULL DeclContext",
656
75
               name);
657
75
    else if (const NamedDecl *context_named_decl =
658
75
                 dyn_cast<NamedDecl>(context.m_decl_context))
659
15
      LLDB_LOG(log,
660
75
               "ClangExpressionDeclMap::FindExternalVisibleDecls for "
661
75
               "'{0}' in '{1}'",
662
75
               name, context_named_decl->getNameAsString());
663
60
    else
664
60
      LLDB_LOG(log,
665
75
               "ClangExpressionDeclMap::FindExternalVisibleDecls for "
666
75
               "'{0}' in a '{1}'",
667
75
               name, context.m_decl_context->getDeclKindName());
668
75
  }
669
670
63.8k
  if (const NamespaceDecl *namespace_context =
671
63.8k
          dyn_cast<NamespaceDecl>(context.m_decl_context)) {
672
10.8k
    if (namespace_context->getName().str() ==
673
10.8k
        std::string(g_lldb_local_vars_namespace_cstr)) {
674
8.93k
      CompilerDeclContext compiler_decl_ctx =
675
8.93k
          m_clang_ast_context->CreateDeclContext(
676
8.93k
              const_cast<clang::DeclContext *>(context.m_decl_context));
677
8.93k
      FindExternalVisibleDecls(context, lldb::ModuleSP(), compiler_decl_ctx);
678
8.93k
      return;
679
8.93k
    }
680
681
1.88k
    ClangASTImporter::NamespaceMapSP namespace_map =
682
1.88k
        m_ast_importer_sp->GetNamespaceMap(namespace_context);
683
684
1.88k
    if (!namespace_map)
685
1.54k
      return;
686
687
342
    LLDB_LOGV(log, "  CEDM::FEVD Inspecting (NamespaceMap*){0:x} ({1} entries)",
688
342
              namespace_map.get(), namespace_map->size());
689
690
443
    for (ClangASTImporter::NamespaceMapItem &n : *namespace_map) {
691
443
      LLDB_LOG(log, "  CEDM::FEVD Searching namespace {0} in module {1}",
692
443
               n.second.GetName(), n.first->GetFileSpec().GetFilename());
693
694
443
      FindExternalVisibleDecls(context, n.first, n.second);
695
443
    }
696
53.0k
  } else if (isa<TranslationUnitDecl>(context.m_decl_context)) {
697
42.7k
    CompilerDeclContext namespace_decl;
698
699
42.7k
    LLDB_LOG(log, "  CEDM::FEVD Searching the root namespace");
700
701
42.7k
    FindExternalVisibleDecls(context, lldb::ModuleSP(), namespace_decl);
702
42.7k
  }
703
704
53.4k
  ClangASTSource::FindExternalVisibleDecls(context);
705
53.4k
}
706
707
void ClangExpressionDeclMap::MaybeRegisterFunctionBody(
708
15
    FunctionDecl *copied_function_decl) {
709
15
  if (copied_function_decl->getBody() && 
m_parser_vars->m_code_gen11
) {
710
11
    clang::DeclGroupRef decl_group_ref(copied_function_decl);
711
11
    m_parser_vars->m_code_gen->HandleTopLevelDecl(decl_group_ref);
712
11
  }
713
15
}
714
715
42.0k
clang::NamedDecl *ClangExpressionDeclMap::GetPersistentDecl(ConstString name) {
716
42.0k
  if (!m_parser_vars)
717
0
    return nullptr;
718
42.0k
  Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
719
42.0k
  if (!target)
720
0
    return nullptr;
721
722
42.0k
  ScratchTypeSystemClang::GetForTarget(*target);
723
724
42.0k
  if (!m_parser_vars->m_persistent_vars)
725
0
    return nullptr;
726
42.0k
  return m_parser_vars->m_persistent_vars->GetPersistentDecl(name);
727
42.0k
}
728
729
void ClangExpressionDeclMap::SearchPersistenDecls(NameSearchContext &context,
730
42.0k
                                                  const ConstString name) {
731
42.0k
  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
732
733
42.0k
  NamedDecl *persistent_decl = GetPersistentDecl(name);
734
735
42.0k
  if (!persistent_decl)
736
42.0k
    return;
737
738
56
  Decl *parser_persistent_decl = CopyDecl(persistent_decl);
739
740
56
  if (!parser_persistent_decl)
741
0
    return;
742
743
56
  NamedDecl *parser_named_decl = dyn_cast<NamedDecl>(parser_persistent_decl);
744
745
56
  if (!parser_named_decl)
746
0
    return;
747
748
56
  if (clang::FunctionDecl *parser_function_decl =
749
56
          llvm::dyn_cast<clang::FunctionDecl>(parser_named_decl)) {
750
8
    MaybeRegisterFunctionBody(parser_function_decl);
751
8
  }
752
753
56
  LLDB_LOG(log, "  CEDM::FEVD Found persistent decl {0}", name);
754
755
56
  context.AddNamedDecl(parser_named_decl);
756
56
}
757
758
143
void ClangExpressionDeclMap::LookUpLldbClass(NameSearchContext &context) {
759
143
  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
760
761
143
  StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
762
143
  SymbolContext sym_ctx;
763
143
  if (frame != nullptr)
764
143
    sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
765
143
                                      lldb::eSymbolContextBlock);
766
767
143
  if (m_ctx_obj) {
768
24
    Status status;
769
24
    lldb::ValueObjectSP ctx_obj_ptr = m_ctx_obj->AddressOf(status);
770
24
    if (!ctx_obj_ptr || 
status.Fail()21
)
771
3
      return;
772
773
21
    AddContextClassType(context, TypeFromUser(m_ctx_obj->GetCompilerType()));
774
775
21
    m_struct_vars->m_object_pointer_type =
776
21
        TypeFromUser(ctx_obj_ptr->GetCompilerType());
777
778
21
    return;
779
24
  }
780
781
  // Clang is looking for the type of "this"
782
783
119
  if (frame == nullptr)
784
0
    return;
785
786
  // Find the block that defines the function represented by "sym_ctx"
787
119
  Block *function_block = sym_ctx.GetFunctionBlock();
788
789
119
  if (!function_block)
790
0
    return;
791
792
119
  CompilerDeclContext function_decl_ctx = function_block->GetDeclContext();
793
794
119
  if (!function_decl_ctx)
795
0
    return;
796
797
119
  clang::CXXMethodDecl *method_decl =
798
119
      TypeSystemClang::DeclContextGetAsCXXMethodDecl(function_decl_ctx);
799
800
119
  if (method_decl) {
801
113
    clang::CXXRecordDecl *class_decl = method_decl->getParent();
802
803
113
    QualType class_qual_type(class_decl->getTypeForDecl(), 0);
804
805
113
    TypeFromUser class_user_type(class_qual_type.getAsOpaquePtr(),
806
113
                                 function_decl_ctx.GetTypeSystem());
807
808
113
    LLDB_LOG(log, "  CEDM::FEVD Adding type for $__lldb_class: {1}",
809
113
             class_qual_type.getAsString());
810
811
113
    AddContextClassType(context, class_user_type);
812
813
113
    if (method_decl->isInstance()) {
814
      // self is a pointer to the object
815
816
113
      QualType class_pointer_type =
817
113
          method_decl->getASTContext().getPointerType(class_qual_type);
818
819
113
      TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(),
820
113
                                  function_decl_ctx.GetTypeSystem());
821
822
113
      m_struct_vars->m_object_pointer_type = self_user_type;
823
113
    }
824
113
    return;
825
113
  }
826
827
  // This branch will get hit if we are executing code in the context of
828
  // a function that claims to have an object pointer (through
829
  // DW_AT_object_pointer?) but is not formally a method of the class.
830
  // In that case, just look up the "this" variable in the current scope
831
  // and use its type.
832
  // FIXME: This code is formally correct, but clang doesn't currently
833
  // emit DW_AT_object_pointer
834
  // for C++ so it hasn't actually been tested.
835
836
6
  VariableList *vars = frame->GetVariableList(false);
837
838
6
  lldb::VariableSP this_var = vars->FindVariable(ConstString("this"));
839
840
6
  if (this_var && this_var->IsInScope(frame) &&
841
6
      this_var->LocationIsValidForFrame(frame)) {
842
6
    Type *this_type = this_var->GetType();
843
844
6
    if (!this_type)
845
0
      return;
846
847
6
    TypeFromUser pointee_type =
848
6
        this_type->GetForwardCompilerType().GetPointeeType();
849
850
6
    LLDB_LOG(log, "  FEVD Adding type for $__lldb_class: {1}",
851
6
             ClangUtil::GetQualType(pointee_type).getAsString());
852
853
6
    AddContextClassType(context, pointee_type);
854
6
    TypeFromUser this_user_type(this_type->GetFullCompilerType());
855
6
    m_struct_vars->m_object_pointer_type = this_user_type;
856
6
  }
857
6
}
858
859
116
void ClangExpressionDeclMap::LookUpLldbObjCClass(NameSearchContext &context) {
860
116
  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
861
862
116
  StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
863
864
116
  if (m_ctx_obj) {
865
15
    Status status;
866
15
    lldb::ValueObjectSP ctx_obj_ptr = m_ctx_obj->AddressOf(status);
867
15
    if (!ctx_obj_ptr || status.Fail())
868
0
      return;
869
870
15
    AddOneType(context, TypeFromUser(m_ctx_obj->GetCompilerType()));
871
872
15
    m_struct_vars->m_object_pointer_type =
873
15
        TypeFromUser(ctx_obj_ptr->GetCompilerType());
874
875
15
    return;
876
15
  }
877
878
  // Clang is looking for the type of "*self"
879
880
101
  if (!frame)
881
0
    return;
882
883
101
  SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
884
101
                                                  lldb::eSymbolContextBlock);
885
886
  // Find the block that defines the function represented by "sym_ctx"
887
101
  Block *function_block = sym_ctx.GetFunctionBlock();
888
889
101
  if (!function_block)
890
0
    return;
891
892
101
  CompilerDeclContext function_decl_ctx = function_block->GetDeclContext();
893
894
101
  if (!function_decl_ctx)
895
0
    return;
896
897
101
  clang::ObjCMethodDecl *method_decl =
898
101
      TypeSystemClang::DeclContextGetAsObjCMethodDecl(function_decl_ctx);
899
900
101
  if (method_decl) {
901
95
    ObjCInterfaceDecl *self_interface = method_decl->getClassInterface();
902
903
95
    if (!self_interface)
904
0
      return;
905
906
95
    const clang::Type *interface_type = self_interface->getTypeForDecl();
907
908
95
    if (!interface_type)
909
0
      return; // This is unlikely, but we have seen crashes where this
910
              // occurred
911
912
95
    TypeFromUser class_user_type(QualType(interface_type, 0).getAsOpaquePtr(),
913
95
                                 function_decl_ctx.GetTypeSystem());
914
915
95
    LLDB_LOG(log, "  FEVD[{0}] Adding type for $__lldb_objc_class: {1}",
916
95
             ClangUtil::ToString(interface_type));
917
918
95
    AddOneType(context, class_user_type);
919
920
95
    if (method_decl->isInstanceMethod()) {
921
      // self is a pointer to the object
922
923
80
      QualType class_pointer_type =
924
80
          method_decl->getASTContext().getObjCObjectPointerType(
925
80
              QualType(interface_type, 0));
926
927
80
      TypeFromUser self_user_type(class_pointer_type.getAsOpaquePtr(),
928
80
                                  function_decl_ctx.GetTypeSystem());
929
930
80
      m_struct_vars->m_object_pointer_type = self_user_type;
931
80
    } else {
932
      // self is a Class pointer
933
15
      QualType class_type = method_decl->getASTContext().getObjCClassType();
934
935
15
      TypeFromUser self_user_type(class_type.getAsOpaquePtr(),
936
15
                                  function_decl_ctx.GetTypeSystem());
937
938
15
      m_struct_vars->m_object_pointer_type = self_user_type;
939
15
    }
940
941
95
    return;
942
95
  }
943
  // This branch will get hit if we are executing code in the context of
944
  // a function that claims to have an object pointer (through
945
  // DW_AT_object_pointer?) but is not formally a method of the class.
946
  // In that case, just look up the "self" variable in the current scope
947
  // and use its type.
948
949
6
  VariableList *vars = frame->GetVariableList(false);
950
951
6
  lldb::VariableSP self_var = vars->FindVariable(ConstString("self"));
952
953
6
  if (!self_var)
954
0
    return;
955
6
  if (!self_var->IsInScope(frame))
956
0
    return;
957
6
  if (!self_var->LocationIsValidForFrame(frame))
958
0
    return;
959
960
6
  Type *self_type = self_var->GetType();
961
962
6
  if (!self_type)
963
0
    return;
964
965
6
  CompilerType self_clang_type = self_type->GetFullCompilerType();
966
967
6
  if (TypeSystemClang::IsObjCClassType(self_clang_type)) {
968
0
    return;
969
0
  }
970
6
  if (!TypeSystemClang::IsObjCObjectPointerType(self_clang_type))
971
0
    return;
972
6
  self_clang_type = self_clang_type.GetPointeeType();
973
974
6
  if (!self_clang_type)
975
0
    return;
976
977
6
  LLDB_LOG(log, "  FEVD[{0}] Adding type for $__lldb_objc_class: {1}",
978
6
           ClangUtil::ToString(self_type->GetFullCompilerType()));
979
980
6
  TypeFromUser class_user_type(self_clang_type);
981
982
6
  AddOneType(context, class_user_type);
983
984
6
  TypeFromUser self_user_type(self_type->GetFullCompilerType());
985
986
6
  m_struct_vars->m_object_pointer_type = self_user_type;
987
6
}
988
989
void ClangExpressionDeclMap::LookupLocalVarNamespace(
990
7.23k
    SymbolContext &sym_ctx, NameSearchContext &name_context) {
991
7.23k
  if (sym_ctx.block == nullptr)
992
0
    return;
993
994
7.23k
  CompilerDeclContext frame_decl_context = sym_ctx.block->GetDeclContext();
995
7.23k
  if (!frame_decl_context)
996
0
    return;
997
998
7.23k
  TypeSystemClang *frame_ast = llvm::dyn_cast_or_null<TypeSystemClang>(
999
7.23k
      frame_decl_context.GetTypeSystem());
1000
7.23k
  if (!frame_ast)
1001
0
    return;
1002
1003
7.23k
  clang::NamespaceDecl *namespace_decl =
1004
7.23k
      m_clang_ast_context->GetUniqueNamespaceDeclaration(
1005
7.23k
          g_lldb_local_vars_namespace_cstr, nullptr, OptionalClangModuleID());
1006
7.23k
  if (!namespace_decl)
1007
0
    return;
1008
1009
7.23k
  name_context.AddNamedDecl(namespace_decl);
1010
7.23k
  clang::DeclContext *ctxt = clang::Decl::castToDeclContext(namespace_decl);
1011
7.23k
  ctxt->setHasExternalVisibleStorage(true);
1012
7.23k
  name_context.m_found_local_vars_nsp = true;
1013
7.23k
}
1014
1015
void ClangExpressionDeclMap::LookupInModulesDeclVendor(
1016
3.34k
    NameSearchContext &context, ConstString name) {
1017
3.34k
  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1018
1019
3.34k
  if (!m_target)
1020
1
    return;
1021
1022
3.34k
  std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor =
1023
3.34k
      GetClangModulesDeclVendor();
1024
3.34k
  if (!modules_decl_vendor)
1025
4
    return;
1026
1027
3.33k
  bool append = false;
1028
3.33k
  uint32_t max_matches = 1;
1029
3.33k
  std::vector<clang::NamedDecl *> decls;
1030
1031
3.33k
  if (!modules_decl_vendor->FindDecls(name, append, max_matches, decls))
1032
3.24k
    return;
1033
1034
89
  assert(!decls.empty() && "FindDecls returned true but no decls?");
1035
0
  clang::NamedDecl *const decl_from_modules = decls[0];
1036
1037
89
  LLDB_LOG(log,
1038
89
           "  CAS::FEVD Matching decl found for "
1039
89
           "\"{1}\" in the modules",
1040
89
           name);
1041
1042
89
  clang::Decl *copied_decl = CopyDecl(decl_from_modules);
1043
89
  if (!copied_decl) {
1044
0
    LLDB_LOG(log, "  CAS::FEVD - Couldn't export a "
1045
0
                  "declaration from the modules");
1046
0
    return;
1047
0
  }
1048
1049
89
  if (auto copied_function = dyn_cast<clang::FunctionDecl>(copied_decl)) {
1050
7
    MaybeRegisterFunctionBody(copied_function);
1051
1052
7
    context.AddNamedDecl(copied_function);
1053
1054
7
    context.m_found_function_with_type_info = true;
1055
7
    context.m_found_function = true;
1056
82
  } else if (auto copied_var = dyn_cast<clang::VarDecl>(copied_decl)) {
1057
4
    context.AddNamedDecl(copied_var);
1058
4
    context.m_found_variable = true;
1059
4
  }
1060
89
}
1061
1062
bool ClangExpressionDeclMap::LookupLocalVariable(
1063
    NameSearchContext &context, ConstString name, SymbolContext &sym_ctx,
1064
21.3k
    const CompilerDeclContext &namespace_decl) {
1065
21.3k
  if (sym_ctx.block == nullptr)
1066
9
    return false;
1067
1068
21.3k
  CompilerDeclContext decl_context = sym_ctx.block->GetDeclContext();
1069
21.3k
  if (!decl_context)
1070
0
    return false;
1071
1072
  // Make sure that the variables are parsed so that we have the
1073
  // declarations.
1074
21.3k
  StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
1075
21.3k
  VariableListSP vars = frame->GetInScopeVariableList(true);
1076
288k
  for (size_t i = 0; i < vars->GetSize(); 
i++266k
)
1077
266k
    vars->GetVariableAtIndex(i)->GetDecl();
1078
1079
  // Search for declarations matching the name. Do not include imported
1080
  // decls in the search if we are looking for decls in the artificial
1081
  // namespace $__lldb_local_vars.
1082
21.3k
  std::vector<CompilerDecl> found_decls =
1083
21.3k
      decl_context.FindDeclByName(name, namespace_decl.IsValid());
1084
1085
21.3k
  VariableSP var;
1086
21.3k
  bool variable_found = false;
1087
21.3k
  for (CompilerDecl decl : found_decls) {
1088
135k
    for (size_t vi = 0, ve = vars->GetSize(); vi != ve; 
++vi116k
) {
1089
134k
      VariableSP candidate_var = vars->GetVariableAtIndex(vi);
1090
134k
      if (candidate_var->GetDecl() == decl) {
1091
18.2k
        var = candidate_var;
1092
18.2k
        break;
1093
18.2k
      }
1094
134k
    }
1095
1096
19.1k
    if (var && 
!variable_found18.2k
) {
1097
18.1k
      variable_found = true;
1098
18.1k
      ValueObjectSP valobj = ValueObjectVariable::Create(frame, var);
1099
18.1k
      AddOneVariable(context, var, valobj);
1100
18.1k
      context.m_found_variable = true;
1101
18.1k
    }
1102
19.1k
  }
1103
21.3k
  return variable_found;
1104
21.3k
}
1105
1106
/// Structure to hold the info needed when comparing function
1107
/// declarations.
1108
namespace {
1109
struct FuncDeclInfo {
1110
  ConstString m_name;
1111
  CompilerType m_copied_type;
1112
  uint32_t m_decl_lvl;
1113
  SymbolContext m_sym_ctx;
1114
};
1115
} // namespace
1116
1117
SymbolContextList ClangExpressionDeclMap::SearchFunctionsInSymbolContexts(
1118
    const SymbolContextList &sc_list,
1119
281
    const CompilerDeclContext &frame_decl_context) {
1120
  // First, symplify things by looping through the symbol contexts to
1121
  // remove unwanted functions and separate out the functions we want to
1122
  // compare and prune into a separate list. Cache the info needed about
1123
  // the function declarations in a vector for efficiency.
1124
281
  uint32_t num_indices = sc_list.GetSize();
1125
281
  SymbolContextList sc_sym_list;
1126
281
  std::vector<FuncDeclInfo> decl_infos;
1127
281
  decl_infos.reserve(num_indices);
1128
281
  clang::DeclContext *frame_decl_ctx =
1129
281
      (clang::DeclContext *)frame_decl_context.GetOpaqueDeclContext();
1130
281
  TypeSystemClang *ast = llvm::dyn_cast_or_null<TypeSystemClang>(
1131
281
      frame_decl_context.GetTypeSystem());
1132
1133
1.45k
  for (uint32_t index = 0; index < num_indices; 
++index1.17k
) {
1134
1.17k
    FuncDeclInfo fdi;
1135
1.17k
    SymbolContext sym_ctx;
1136
1.17k
    sc_list.GetContextAtIndex(index, sym_ctx);
1137
1138
    // We don't know enough about symbols to compare them, but we should
1139
    // keep them in the list.
1140
1.17k
    Function *function = sym_ctx.function;
1141
1.17k
    if (!function) {
1142
724
      sc_sym_list.Append(sym_ctx);
1143
724
      continue;
1144
724
    }
1145
    // Filter out functions without declaration contexts, as well as
1146
    // class/instance methods, since they'll be skipped in the code that
1147
    // follows anyway.
1148
450
    CompilerDeclContext func_decl_context = function->GetDeclContext();
1149
450
    if (!func_decl_context ||
1150
450
        func_decl_context.IsClassMethod(nullptr, nullptr, nullptr))
1151
0
      continue;
1152
    // We can only prune functions for which we can copy the type.
1153
450
    CompilerType func_clang_type = function->GetType()->GetFullCompilerType();
1154
450
    CompilerType copied_func_type = GuardedCopyType(func_clang_type);
1155
450
    if (!copied_func_type) {
1156
0
      sc_sym_list.Append(sym_ctx);
1157
0
      continue;
1158
0
    }
1159
1160
450
    fdi.m_sym_ctx = sym_ctx;
1161
450
    fdi.m_name = function->GetName();
1162
450
    fdi.m_copied_type = copied_func_type;
1163
450
    fdi.m_decl_lvl = LLDB_INVALID_DECL_LEVEL;
1164
450
    if (fdi.m_copied_type && func_decl_context) {
1165
      // Call CountDeclLevels to get the number of parent scopes we have
1166
      // to look through before we find the function declaration. When
1167
      // comparing functions of the same type, the one with a lower count
1168
      // will be closer to us in the lookup scope and shadows the other.
1169
450
      clang::DeclContext *func_decl_ctx =
1170
450
          (clang::DeclContext *)func_decl_context.GetOpaqueDeclContext();
1171
450
      fdi.m_decl_lvl = ast->CountDeclLevels(frame_decl_ctx, func_decl_ctx,
1172
450
                                            &fdi.m_name, &fdi.m_copied_type);
1173
450
    }
1174
450
    decl_infos.emplace_back(fdi);
1175
450
  }
1176
1177
  // Loop through the functions in our cache looking for matching types,
1178
  // then compare their scope levels to see which is closer.
1179
281
  std::multimap<CompilerType, const FuncDeclInfo *> matches;
1180
450
  for (const FuncDeclInfo &fdi : decl_infos) {
1181
450
    const CompilerType t = fdi.m_copied_type;
1182
450
    auto q = matches.find(t);
1183
450
    if (q != matches.end()) {
1184
174
      if (q->second->m_decl_lvl > fdi.m_decl_lvl)
1185
        // This function is closer; remove the old set.
1186
36
        matches.erase(t);
1187
138
      else if (q->second->m_decl_lvl < fdi.m_decl_lvl)
1188
        // The functions in our set are closer - skip this one.
1189
90
        continue;
1190
174
    }
1191
360
    matches.insert(std::make_pair(t, &fdi));
1192
360
  }
1193
1194
  // Loop through our matches and add their symbol contexts to our list.
1195
281
  SymbolContextList sc_func_list;
1196
281
  for (const auto &q : matches)
1197
306
    sc_func_list.Append(q.second->m_sym_ctx);
1198
1199
  // Rejoin the lists with the functions in front.
1200
281
  sc_func_list.Append(sc_sym_list);
1201
281
  return sc_func_list;
1202
281
}
1203
1204
void ClangExpressionDeclMap::LookupFunction(
1205
    NameSearchContext &context, lldb::ModuleSP module_sp, ConstString name,
1206
3.77k
    const CompilerDeclContext &namespace_decl) {
1207
3.77k
  if (!m_parser_vars)
1208
1
    return;
1209
1210
3.77k
  Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1211
1212
3.77k
  std::vector<clang::NamedDecl *> decls_from_modules;
1213
1214
3.77k
  if (target) {
1215
3.77k
    if (std::shared_ptr<ClangModulesDeclVendor> decl_vendor =
1216
3.77k
            GetClangModulesDeclVendor()) {
1217
3.76k
      decl_vendor->FindDecls(name, false, UINT32_MAX, decls_from_modules);
1218
3.76k
    }
1219
3.77k
  }
1220
1221
3.77k
  SymbolContextList sc_list;
1222
3.77k
  if (namespace_decl && 
module_sp422
) {
1223
422
    ModuleFunctionSearchOptions function_options;
1224
422
    function_options.include_inlines = false;
1225
422
    function_options.include_symbols = false;
1226
1227
422
    module_sp->FindFunctions(name, namespace_decl, eFunctionNameTypeBase,
1228
422
                             function_options, sc_list);
1229
3.34k
  } else if (target && !namespace_decl) {
1230
3.34k
    ModuleFunctionSearchOptions function_options;
1231
3.34k
    function_options.include_inlines = false;
1232
3.34k
    function_options.include_symbols = true;
1233
1234
    // TODO Fix FindFunctions so that it doesn't return
1235
    //   instance methods for eFunctionNameTypeBase.
1236
1237
3.34k
    target->GetImages().FindFunctions(
1238
3.34k
        name, eFunctionNameTypeFull | eFunctionNameTypeBase, function_options,
1239
3.34k
        sc_list);
1240
3.34k
  }
1241
1242
  // If we found more than one function, see if we can use the frame's decl
1243
  // context to remove functions that are shadowed by other functions which
1244
  // match in type but are nearer in scope.
1245
  //
1246
  // AddOneFunction will not add a function whose type has already been
1247
  // added, so if there's another function in the list with a matching type,
1248
  // check to see if their decl context is a parent of the current frame's or
1249
  // was imported via a and using statement, and pick the best match
1250
  // according to lookup rules.
1251
3.77k
  if (sc_list.GetSize() > 1) {
1252
    // Collect some info about our frame's context.
1253
284
    StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
1254
284
    SymbolContext frame_sym_ctx;
1255
284
    if (frame != nullptr)
1256
284
      frame_sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
1257
284
                                              lldb::eSymbolContextBlock);
1258
284
    CompilerDeclContext frame_decl_context =
1259
284
        frame_sym_ctx.block != nullptr ? 
frame_sym_ctx.block->GetDeclContext()281
1260
284
                                       : 
CompilerDeclContext()3
;
1261
1262
    // We can't do this without a compiler decl context for our frame.
1263
284
    if (frame_decl_context) {
1264
281
      sc_list = SearchFunctionsInSymbolContexts(sc_list, frame_decl_context);
1265
281
    }
1266
284
  }
1267
1268
3.77k
  if (sc_list.GetSize()) {
1269
721
    Symbol *extern_symbol = nullptr;
1270
721
    Symbol *non_extern_symbol = nullptr;
1271
1272
721
    for (uint32_t index = 0, num_indices = sc_list.GetSize();
1273
2.19k
         index < num_indices; 
++index1.47k
) {
1274
1.47k
      SymbolContext sym_ctx;
1275
1.47k
      sc_list.GetContextAtIndex(index, sym_ctx);
1276
1277
1.47k
      if (sym_ctx.function) {
1278
583
        CompilerDeclContext decl_ctx = sym_ctx.function->GetDeclContext();
1279
1280
583
        if (!decl_ctx)
1281
0
          continue;
1282
1283
        // Filter out class/instance methods.
1284
583
        if (decl_ctx.IsClassMethod(nullptr, nullptr, nullptr))
1285
0
          continue;
1286
1287
583
        AddOneFunction(context, sym_ctx.function, nullptr);
1288
583
        context.m_found_function_with_type_info = true;
1289
583
        context.m_found_function = true;
1290
890
      } else if (sym_ctx.symbol) {
1291
890
        if (sym_ctx.symbol->GetType() == eSymbolTypeReExported && 
target11
) {
1292
11
          sym_ctx.symbol = sym_ctx.symbol->ResolveReExportedSymbol(*target);
1293
11
          if (sym_ctx.symbol == nullptr)
1294
0
            continue;
1295
11
        }
1296
1297
890
        if (sym_ctx.symbol->IsExternal())
1298
542
          extern_symbol = sym_ctx.symbol;
1299
348
        else
1300
348
          non_extern_symbol = sym_ctx.symbol;
1301
890
      }
1302
1.47k
    }
1303
1304
721
    if (!context.m_found_function_with_type_info) {
1305
306
      for (clang::NamedDecl *decl : decls_from_modules) {
1306
14
        if (llvm::isa<clang::FunctionDecl>(decl)) {
1307
14
          clang::NamedDecl *copied_decl =
1308
14
              llvm::cast_or_null<FunctionDecl>(CopyDecl(decl));
1309
14
          if (copied_decl) {
1310
14
            context.AddNamedDecl(copied_decl);
1311
14
            context.m_found_function_with_type_info = true;
1312
14
          }
1313
14
        }
1314
14
      }
1315
306
    }
1316
1317
721
    if (!context.m_found_function_with_type_info) {
1318
292
      if (extern_symbol) {
1319
176
        AddOneFunction(context, nullptr, extern_symbol);
1320
176
        context.m_found_function = true;
1321
176
      } else 
if (116
non_extern_symbol116
) {
1322
116
        AddOneFunction(context, nullptr, non_extern_symbol);
1323
116
        context.m_found_function = true;
1324
116
      }
1325
292
    }
1326
721
  }
1327
3.77k
}
1328
1329
void ClangExpressionDeclMap::FindExternalVisibleDecls(
1330
    NameSearchContext &context, lldb::ModuleSP module_sp,
1331
52.1k
    const CompilerDeclContext &namespace_decl) {
1332
52.1k
  assert(m_ast_context);
1333
1334
52.1k
  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1335
1336
52.1k
  const ConstString name(context.m_decl_name.getAsString().c_str());
1337
52.1k
  if (IgnoreName(name, false))
1338
692
    return;
1339
1340
  // Only look for functions by name out in our symbols if the function doesn't
1341
  // start with our phony prefix of '$'
1342
1343
51.4k
  Target *target = nullptr;
1344
51.4k
  StackFrame *frame = nullptr;
1345
51.4k
  SymbolContext sym_ctx;
1346
51.4k
  if (m_parser_vars) {
1347
51.4k
    target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1348
51.4k
    frame = m_parser_vars->m_exe_ctx.GetFramePtr();
1349
51.4k
  }
1350
51.4k
  if (frame != nullptr)
1351
50.2k
    sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
1352
50.2k
                                      lldb::eSymbolContextBlock);
1353
1354
  // Try the persistent decls, which take precedence over all else.
1355
51.4k
  if (!namespace_decl)
1356
42.0k
    SearchPersistenDecls(context, name);
1357
1358
51.4k
  if (name.GetStringRef().startswith("$") && 
!namespace_decl29.1k
) {
1359
29.1k
    if (name == "$__lldb_class") {
1360
143
      LookUpLldbClass(context);
1361
143
      return;
1362
143
    }
1363
1364
28.9k
    if (name == "$__lldb_objc_class") {
1365
116
      LookUpLldbObjCClass(context);
1366
116
      return;
1367
116
    }
1368
28.8k
    if (name == g_lldb_local_vars_namespace_cstr) {
1369
7.23k
      LookupLocalVarNamespace(sym_ctx, context);
1370
7.23k
      return;
1371
7.23k
    }
1372
1373
    // any other $__lldb names should be weeded out now
1374
21.6k
    if (name.GetStringRef().startswith("$__lldb"))
1375
20.8k
      return;
1376
1377
    // No ParserVars means we can't do register or variable lookup.
1378
779
    if (!m_parser_vars || 
!m_parser_vars->m_persistent_vars778
)
1379
1
      return;
1380
1381
778
    ExpressionVariableSP pvar_sp(
1382
778
        m_parser_vars->m_persistent_vars->GetVariable(name));
1383
1384
778
    if (pvar_sp) {
1385
144
      AddOneVariable(context, pvar_sp);
1386
144
      return;
1387
144
    }
1388
1389
634
    assert(name.GetStringRef().startswith("$"));
1390
0
    llvm::StringRef reg_name = name.GetStringRef().substr(1);
1391
1392
634
    if (m_parser_vars->m_exe_ctx.GetRegisterContext()) {
1393
632
      const RegisterInfo *reg_info(
1394
632
          m_parser_vars->m_exe_ctx.GetRegisterContext()->GetRegisterInfoByName(
1395
632
              reg_name));
1396
1397
632
      if (reg_info) {
1398
33
        LLDB_LOG(log, "  CEDM::FEVD Found register {0}", reg_info->name);
1399
1400
33
        AddOneRegister(context, reg_info);
1401
33
      }
1402
632
    }
1403
634
    return;
1404
778
  }
1405
1406
22.3k
  bool local_var_lookup = !namespace_decl || (namespace_decl.GetName() ==
1407
9.37k
                                              g_lldb_local_vars_namespace_cstr);
1408
22.3k
  if (frame && 
local_var_lookup21.8k
)
1409
21.3k
    if (LookupLocalVariable(context, name, sym_ctx, namespace_decl))
1410
18.1k
      return;
1411
1412
4.10k
  if (target) {
1413
4.10k
    ValueObjectSP valobj;
1414
4.10k
    VariableSP var;
1415
4.10k
    var = FindGlobalVariable(*target, module_sp, name, namespace_decl);
1416
1417
4.10k
    if (var) {
1418
332
      valobj = ValueObjectVariable::Create(target, var);
1419
332
      AddOneVariable(context, var, valobj);
1420
332
      context.m_found_variable = true;
1421
332
      return;
1422
332
    }
1423
4.10k
  }
1424
1425
3.77k
  LookupFunction(context, module_sp, name, namespace_decl);
1426
1427
  // Try the modules next.
1428
3.77k
  if (!context.m_found_function_with_type_info)
1429
3.34k
    LookupInModulesDeclVendor(context, name);
1430
1431
3.77k
  if (target && 
!context.m_found_variable3.77k
&&
!namespace_decl3.76k
) {
1432
    // We couldn't find a non-symbol variable for this.  Now we'll hunt for a
1433
    // generic data symbol, and -- if it is found -- treat it as a variable.
1434
3.34k
    Status error;
1435
1436
3.34k
    const Symbol *data_symbol =
1437
3.34k
        m_parser_vars->m_sym_ctx.FindBestGlobalDataSymbol(name, error);
1438
1439
3.34k
    if (!error.Success()) {
1440
2
      const unsigned diag_id =
1441
2
          m_ast_context->getDiagnostics().getCustomDiagID(
1442
2
              clang::DiagnosticsEngine::Level::Error, "%0");
1443
2
      m_ast_context->getDiagnostics().Report(diag_id) << error.AsCString();
1444
2
    }
1445
1446
3.34k
    if (data_symbol) {
1447
156
      std::string warning("got name from symbols: ");
1448
156
      warning.append(name.AsCString());
1449
156
      const unsigned diag_id =
1450
156
          m_ast_context->getDiagnostics().getCustomDiagID(
1451
156
              clang::DiagnosticsEngine::Level::Warning, "%0");
1452
156
      m_ast_context->getDiagnostics().Report(diag_id) << warning.c_str();
1453
156
      AddOneGenericVariable(context, *data_symbol);
1454
156
      context.m_found_variable = true;
1455
156
    }
1456
3.34k
  }
1457
3.77k
}
1458
1459
bool ClangExpressionDeclMap::GetVariableValue(VariableSP &var,
1460
                                              lldb_private::Value &var_location,
1461
                                              TypeFromUser *user_type,
1462
18.5k
                                              TypeFromParser *parser_type) {
1463
18.5k
  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1464
1465
18.5k
  Type *var_type = var->GetType();
1466
1467
18.5k
  if (!var_type) {
1468
0
    LLDB_LOG(log, "Skipped a definition because it has no type");
1469
0
    return false;
1470
0
  }
1471
1472
18.5k
  CompilerType var_clang_type = var_type->GetFullCompilerType();
1473
1474
18.5k
  if (!var_clang_type) {
1475
0
    LLDB_LOG(log, "Skipped a definition because it has no Clang type");
1476
0
    return false;
1477
0
  }
1478
1479
18.5k
  TypeSystemClang *clang_ast = llvm::dyn_cast_or_null<TypeSystemClang>(
1480
18.5k
      var_type->GetForwardCompilerType().GetTypeSystem());
1481
1482
18.5k
  if (!clang_ast) {
1483
0
    LLDB_LOG(log, "Skipped a definition because it has no Clang AST");
1484
0
    return false;
1485
0
  }
1486
1487
18.5k
  DWARFExpression &var_location_expr = var->LocationExpression();
1488
1489
18.5k
  Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1490
18.5k
  Status err;
1491
1492
18.5k
  if (var->GetLocationIsConstantValueData()) {
1493
25
    DataExtractor const_value_extractor;
1494
1495
25
    if (var_location_expr.GetExpressionData(const_value_extractor)) {
1496
25
      var_location = Value(const_value_extractor.GetDataStart(),
1497
25
                           const_value_extractor.GetByteSize());
1498
25
      var_location.SetValueType(Value::ValueType::HostAddress);
1499
25
    } else {
1500
0
      LLDB_LOG(log, "Error evaluating constant variable: {0}", err.AsCString());
1501
0
      return false;
1502
0
    }
1503
25
  }
1504
1505
18.5k
  CompilerType type_to_use = GuardedCopyType(var_clang_type);
1506
1507
18.5k
  if (!type_to_use) {
1508
0
    LLDB_LOG(log,
1509
0
             "Couldn't copy a variable's type into the parser's AST context");
1510
1511
0
    return false;
1512
0
  }
1513
1514
18.5k
  if (parser_type)
1515
18.5k
    *parser_type = TypeFromParser(type_to_use);
1516
1517
18.5k
  if (var_location.GetContextType() == Value::ContextType::Invalid)
1518
18.5k
    var_location.SetCompilerType(type_to_use);
1519
1520
18.5k
  if (var_location.GetValueType() == Value::ValueType::FileAddress) {
1521
0
    SymbolContext var_sc;
1522
0
    var->CalculateSymbolContext(&var_sc);
1523
1524
0
    if (!var_sc.module_sp)
1525
0
      return false;
1526
1527
0
    Address so_addr(var_location.GetScalar().ULongLong(),
1528
0
                    var_sc.module_sp->GetSectionList());
1529
1530
0
    lldb::addr_t load_addr = so_addr.GetLoadAddress(target);
1531
1532
0
    if (load_addr != LLDB_INVALID_ADDRESS) {
1533
0
      var_location.GetScalar() = load_addr;
1534
0
      var_location.SetValueType(Value::ValueType::LoadAddress);
1535
0
    }
1536
0
  }
1537
1538
18.5k
  if (user_type)
1539
18.5k
    *user_type = TypeFromUser(var_clang_type);
1540
1541
18.5k
  return true;
1542
18.5k
}
1543
1544
void ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
1545
                                            VariableSP var,
1546
18.5k
                                            ValueObjectSP valobj) {
1547
18.5k
  assert(m_parser_vars.get());
1548
1549
18.5k
  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1550
1551
18.5k
  TypeFromUser ut;
1552
18.5k
  TypeFromParser pt;
1553
18.5k
  Value var_location;
1554
1555
18.5k
  if (!GetVariableValue(var, var_location, &ut, &pt))
1556
0
    return;
1557
1558
18.5k
  clang::QualType parser_opaque_type =
1559
18.5k
      QualType::getFromOpaquePtr(pt.GetOpaqueQualType());
1560
1561
18.5k
  if (parser_opaque_type.isNull())
1562
0
    return;
1563
1564
18.5k
  if (const clang::Type *parser_type = parser_opaque_type.getTypePtr()) {
1565
18.5k
    if (const TagType *tag_type = dyn_cast<TagType>(parser_type))
1566
6.35k
      CompleteType(tag_type->getDecl());
1567
18.5k
    if (const ObjCObjectPointerType *objc_object_ptr_type =
1568
18.5k
            dyn_cast<ObjCObjectPointerType>(parser_type))
1569
587
      CompleteType(objc_object_ptr_type->getInterfaceDecl());
1570
18.5k
  }
1571
1572
18.5k
  bool is_reference = pt.IsReferenceType();
1573
1574
18.5k
  NamedDecl *var_decl = nullptr;
1575
18.5k
  if (is_reference)
1576
1.65k
    var_decl = context.AddVarDecl(pt);
1577
16.8k
  else
1578
16.8k
    var_decl = context.AddVarDecl(pt.GetLValueReferenceType());
1579
1580
18.5k
  std::string decl_name(context.m_decl_name.getAsString());
1581
18.5k
  ConstString entity_name(decl_name.c_str());
1582
18.5k
  ClangExpressionVariable *entity(new ClangExpressionVariable(valobj));
1583
18.5k
  m_found_entities.AddNewlyConstructedVariable(entity);
1584
1585
18.5k
  assert(entity);
1586
0
  entity->EnableParserVars(GetParserID());
1587
18.5k
  ClangExpressionVariable::ParserVars *parser_vars =
1588
18.5k
      entity->GetParserVars(GetParserID());
1589
18.5k
  parser_vars->m_named_decl = var_decl;
1590
18.5k
  parser_vars->m_llvm_value = nullptr;
1591
18.5k
  parser_vars->m_lldb_value = var_location;
1592
18.5k
  parser_vars->m_lldb_var = var;
1593
1594
18.5k
  if (is_reference)
1595
1.65k
    entity->m_flags |= ClangExpressionVariable::EVTypeIsReference;
1596
1597
18.5k
  LLDB_LOG(log, "  CEDM::FEVD Found variable {1}, returned\n{2} (original {3})",
1598
18.5k
           decl_name, ClangUtil::DumpDecl(var_decl), ClangUtil::ToString(ut));
1599
18.5k
}
1600
1601
void ClangExpressionDeclMap::AddOneVariable(NameSearchContext &context,
1602
144
                                            ExpressionVariableSP &pvar_sp) {
1603
144
  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1604
1605
144
  TypeFromUser user_type(
1606
144
      llvm::cast<ClangExpressionVariable>(pvar_sp.get())->GetTypeFromUser());
1607
1608
144
  TypeFromParser parser_type(GuardedCopyType(user_type));
1609
1610
144
  if (!parser_type.GetOpaqueQualType()) {
1611
0
    LLDB_LOG(log, "  CEDM::FEVD Couldn't import type for pvar {0}",
1612
0
             pvar_sp->GetName());
1613
0
    return;
1614
0
  }
1615
1616
144
  NamedDecl *var_decl =
1617
144
      context.AddVarDecl(parser_type.GetLValueReferenceType());
1618
1619
144
  llvm::cast<ClangExpressionVariable>(pvar_sp.get())
1620
144
      ->EnableParserVars(GetParserID());
1621
144
  ClangExpressionVariable::ParserVars *parser_vars =
1622
144
      llvm::cast<ClangExpressionVariable>(pvar_sp.get())
1623
144
          ->GetParserVars(GetParserID());
1624
144
  parser_vars->m_named_decl = var_decl;
1625
144
  parser_vars->m_llvm_value = nullptr;
1626
144
  parser_vars->m_lldb_value.Clear();
1627
1628
144
  LLDB_LOG(log, "  CEDM::FEVD Added pvar {1}, returned\n{2}",
1629
144
           pvar_sp->GetName(), ClangUtil::DumpDecl(var_decl));
1630
144
}
1631
1632
void ClangExpressionDeclMap::AddOneGenericVariable(NameSearchContext &context,
1633
156
                                                   const Symbol &symbol) {
1634
156
  assert(m_parser_vars.get());
1635
1636
156
  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1637
1638
156
  Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1639
1640
156
  if (target == nullptr)
1641
0
    return;
1642
1643
156
  TypeSystemClang *scratch_ast_context = GetScratchContext(*target);
1644
156
  if (!scratch_ast_context)
1645
0
    return;
1646
1647
156
  TypeFromUser user_type(scratch_ast_context->GetBasicType(eBasicTypeVoid)
1648
156
                             .GetPointerType()
1649
156
                             .GetLValueReferenceType());
1650
156
  TypeFromParser parser_type(m_clang_ast_context->GetBasicType(eBasicTypeVoid)
1651
156
                                 .GetPointerType()
1652
156
                                 .GetLValueReferenceType());
1653
156
  NamedDecl *var_decl = context.AddVarDecl(parser_type);
1654
1655
156
  std::string decl_name(context.m_decl_name.getAsString());
1656
156
  ConstString entity_name(decl_name.c_str());
1657
156
  ClangExpressionVariable *entity(new ClangExpressionVariable(
1658
156
      m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(), entity_name,
1659
156
      user_type, m_parser_vars->m_target_info.byte_order,
1660
156
      m_parser_vars->m_target_info.address_byte_size));
1661
156
  m_found_entities.AddNewlyConstructedVariable(entity);
1662
1663
156
  entity->EnableParserVars(GetParserID());
1664
156
  ClangExpressionVariable::ParserVars *parser_vars =
1665
156
      entity->GetParserVars(GetParserID());
1666
1667
156
  const Address symbol_address = symbol.GetAddress();
1668
156
  lldb::addr_t symbol_load_addr = symbol_address.GetLoadAddress(target);
1669
1670
  // parser_vars->m_lldb_value.SetContext(Value::ContextType::ClangType,
1671
  // user_type.GetOpaqueQualType());
1672
156
  parser_vars->m_lldb_value.SetCompilerType(user_type);
1673
156
  parser_vars->m_lldb_value.GetScalar() = symbol_load_addr;
1674
156
  parser_vars->m_lldb_value.SetValueType(Value::ValueType::LoadAddress);
1675
1676
156
  parser_vars->m_named_decl = var_decl;
1677
156
  parser_vars->m_llvm_value = nullptr;
1678
156
  parser_vars->m_lldb_sym = &symbol;
1679
1680
156
  LLDB_LOG(log, "  CEDM::FEVD Found variable {1}, returned\n{2}", decl_name,
1681
156
           ClangUtil::DumpDecl(var_decl));
1682
156
}
1683
1684
void ClangExpressionDeclMap::AddOneRegister(NameSearchContext &context,
1685
33
                                            const RegisterInfo *reg_info) {
1686
33
  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1687
1688
33
  CompilerType clang_type =
1689
33
      m_clang_ast_context->GetBuiltinTypeForEncodingAndBitSize(
1690
33
          reg_info->encoding, reg_info->byte_size * 8);
1691
1692
33
  if (!clang_type) {
1693
0
    LLDB_LOG(log, "  Tried to add a type for {0}, but couldn't get one",
1694
0
             context.m_decl_name.getAsString());
1695
0
    return;
1696
0
  }
1697
1698
33
  TypeFromParser parser_clang_type(clang_type);
1699
1700
33
  NamedDecl *var_decl = context.AddVarDecl(parser_clang_type);
1701
1702
33
  ClangExpressionVariable *entity(new ClangExpressionVariable(
1703
33
      m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1704
33
      m_parser_vars->m_target_info.byte_order,
1705
33
      m_parser_vars->m_target_info.address_byte_size));
1706
33
  m_found_entities.AddNewlyConstructedVariable(entity);
1707
1708
33
  std::string decl_name(context.m_decl_name.getAsString());
1709
33
  entity->SetName(ConstString(decl_name.c_str()));
1710
33
  entity->SetRegisterInfo(reg_info);
1711
33
  entity->EnableParserVars(GetParserID());
1712
33
  ClangExpressionVariable::ParserVars *parser_vars =
1713
33
      entity->GetParserVars(GetParserID());
1714
33
  parser_vars->m_named_decl = var_decl;
1715
33
  parser_vars->m_llvm_value = nullptr;
1716
33
  parser_vars->m_lldb_value.Clear();
1717
33
  entity->m_flags |= ClangExpressionVariable::EVBareRegister;
1718
1719
33
  LLDB_LOG(log, "  CEDM::FEVD Added register {1}, returned\n{2}",
1720
33
           context.m_decl_name.getAsString(), ClangUtil::DumpDecl(var_decl));
1721
33
}
1722
1723
void ClangExpressionDeclMap::AddOneFunction(NameSearchContext &context,
1724
                                            Function *function,
1725
875
                                            Symbol *symbol) {
1726
875
  assert(m_parser_vars.get());
1727
1728
875
  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1729
1730
875
  NamedDecl *function_decl = nullptr;
1731
875
  Address fun_address;
1732
875
  CompilerType function_clang_type;
1733
1734
875
  bool is_indirect_function = false;
1735
1736
875
  if (function) {
1737
583
    Type *function_type = function->GetType();
1738
1739
583
    const auto lang = function->GetCompileUnit()->GetLanguage();
1740
583
    const auto name = function->GetMangled().GetMangledName().AsCString();
1741
583
    const bool extern_c = (Language::LanguageIsC(lang) &&
1742
583
                           
!CPlusPlusLanguage::IsCPPMangledName(name)41
) ||
1743
583
                          
(542
Language::LanguageIsObjC(lang)542
&&
1744
542
                           
!Language::LanguageIsCPlusPlus(lang)1
);
1745
1746
583
    if (!extern_c) {
1747
541
      TypeSystem *type_system = function->GetDeclContext().GetTypeSystem();
1748
541
      if (llvm::isa<TypeSystemClang>(type_system)) {
1749
541
        clang::DeclContext *src_decl_context =
1750
541
            (clang::DeclContext *)function->GetDeclContext()
1751
541
                .GetOpaqueDeclContext();
1752
541
        clang::FunctionDecl *src_function_decl =
1753
541
            llvm::dyn_cast_or_null<clang::FunctionDecl>(src_decl_context);
1754
541
        if (src_function_decl &&
1755
541
            src_function_decl->getTemplateSpecializationInfo()) {
1756
0
          clang::FunctionTemplateDecl *function_template =
1757
0
              src_function_decl->getTemplateSpecializationInfo()->getTemplate();
1758
0
          clang::FunctionTemplateDecl *copied_function_template =
1759
0
              llvm::dyn_cast_or_null<clang::FunctionTemplateDecl>(
1760
0
                  CopyDecl(function_template));
1761
0
          if (copied_function_template) {
1762
0
            if (log) {
1763
0
              StreamString ss;
1764
1765
0
              function->DumpSymbolContext(&ss);
1766
1767
0
              LLDB_LOG(log,
1768
0
                       "  CEDM::FEVD Imported decl for function template"
1769
0
                       " {1} (description {2}), returned\n{3}",
1770
0
                       copied_function_template->getNameAsString(),
1771
0
                       ss.GetData(),
1772
0
                       ClangUtil::DumpDecl(copied_function_template));
1773
0
            }
1774
1775
0
            context.AddNamedDecl(copied_function_template);
1776
0
          }
1777
541
        } else if (src_function_decl) {
1778
541
          if (clang::FunctionDecl *copied_function_decl =
1779
541
                  llvm::dyn_cast_or_null<clang::FunctionDecl>(
1780
541
                      CopyDecl(src_function_decl))) {
1781
541
            if (log) {
1782
0
              StreamString ss;
1783
1784
0
              function->DumpSymbolContext(&ss);
1785
1786
0
              LLDB_LOG(log,
1787
0
                       "  CEDM::FEVD Imported decl for function {1} "
1788
0
                       "(description {2}), returned\n{3}",
1789
0
                       copied_function_decl->getNameAsString(), ss.GetData(),
1790
0
                       ClangUtil::DumpDecl(copied_function_decl));
1791
0
            }
1792
1793
541
            context.AddNamedDecl(copied_function_decl);
1794
541
            return;
1795
541
          } else {
1796
0
            LLDB_LOG(log, "  Failed to import the function decl for '{0}'",
1797
0
                     src_function_decl->getName());
1798
0
          }
1799
541
        }
1800
541
      }
1801
541
    }
1802
1803
42
    if (!function_type) {
1804
0
      LLDB_LOG(log, "  Skipped a function because it has no type");
1805
0
      return;
1806
0
    }
1807
1808
42
    function_clang_type = function_type->GetFullCompilerType();
1809
1810
42
    if (!function_clang_type) {
1811
0
      LLDB_LOG(log, "  Skipped a function because it has no Clang type");
1812
0
      return;
1813
0
    }
1814
1815
42
    fun_address = function->GetAddressRange().GetBaseAddress();
1816
1817
42
    CompilerType copied_function_type = GuardedCopyType(function_clang_type);
1818
42
    if (copied_function_type) {
1819
42
      function_decl = context.AddFunDecl(copied_function_type, extern_c);
1820
1821
42
      if (!function_decl) {
1822
0
        LLDB_LOG(log, "  Failed to create a function decl for '{0}' ({1:x})",
1823
0
                 function_type->GetName(), function_type->GetID());
1824
1825
0
        return;
1826
0
      }
1827
42
    } else {
1828
      // We failed to copy the type we found
1829
0
      LLDB_LOG(log,
1830
0
               "  Failed to import the function type '{0}' ({1:x})"
1831
0
               " into the expression parser AST contenxt",
1832
0
               function_type->GetName(), function_type->GetID());
1833
1834
0
      return;
1835
0
    }
1836
292
  } else if (symbol) {
1837
292
    fun_address = symbol->GetAddress();
1838
292
    function_decl = context.AddGenericFunDecl();
1839
292
    is_indirect_function = symbol->IsIndirect();
1840
292
  } else {
1841
0
    LLDB_LOG(log, "  AddOneFunction called with no function and no symbol");
1842
0
    return;
1843
0
  }
1844
1845
334
  Target *target = m_parser_vars->m_exe_ctx.GetTargetPtr();
1846
1847
334
  lldb::addr_t load_addr =
1848
334
      fun_address.GetCallableLoadAddress(target, is_indirect_function);
1849
1850
334
  ClangExpressionVariable *entity(new ClangExpressionVariable(
1851
334
      m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1852
334
      m_parser_vars->m_target_info.byte_order,
1853
334
      m_parser_vars->m_target_info.address_byte_size));
1854
334
  m_found_entities.AddNewlyConstructedVariable(entity);
1855
1856
334
  std::string decl_name(context.m_decl_name.getAsString());
1857
334
  entity->SetName(ConstString(decl_name.c_str()));
1858
334
  entity->SetCompilerType(function_clang_type);
1859
334
  entity->EnableParserVars(GetParserID());
1860
1861
334
  ClangExpressionVariable::ParserVars *parser_vars =
1862
334
      entity->GetParserVars(GetParserID());
1863
1864
334
  if (load_addr != LLDB_INVALID_ADDRESS) {
1865
334
    parser_vars->m_lldb_value.SetValueType(Value::ValueType::LoadAddress);
1866
334
    parser_vars->m_lldb_value.GetScalar() = load_addr;
1867
334
  } else {
1868
    // We have to try finding a file address.
1869
1870
0
    lldb::addr_t file_addr = fun_address.GetFileAddress();
1871
1872
0
    parser_vars->m_lldb_value.SetValueType(Value::ValueType::FileAddress);
1873
0
    parser_vars->m_lldb_value.GetScalar() = file_addr;
1874
0
  }
1875
1876
334
  parser_vars->m_named_decl = function_decl;
1877
334
  parser_vars->m_llvm_value = nullptr;
1878
1879
334
  if (log) {
1880
0
    StreamString ss;
1881
1882
0
    fun_address.Dump(&ss,
1883
0
                     m_parser_vars->m_exe_ctx.GetBestExecutionContextScope(),
1884
0
                     Address::DumpStyleResolvedDescription);
1885
1886
0
    LLDB_LOG(log,
1887
0
             "  CEDM::FEVD Found {1} function {2} (description {3}), "
1888
0
             "returned\n{4}",
1889
0
             (function ? "specific" : "generic"), decl_name, ss.GetData(),
1890
0
             ClangUtil::DumpDecl(function_decl));
1891
0
  }
1892
334
}
1893
1894
void ClangExpressionDeclMap::AddContextClassType(NameSearchContext &context,
1895
140
                                                 const TypeFromUser &ut) {
1896
140
  CompilerType copied_clang_type = GuardedCopyType(ut);
1897
1898
140
  Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1899
1900
140
  if (!copied_clang_type) {
1901
0
    LLDB_LOG(log,
1902
0
             "ClangExpressionDeclMap::AddThisType - Couldn't import the type");
1903
1904
0
    return;
1905
0
  }
1906
1907
140
  if (copied_clang_type.IsAggregateType() &&
1908
140
      copied_clang_type.GetCompleteType()) {
1909
140
    CompilerType void_clang_type =
1910
140
        m_clang_ast_context->GetBasicType(eBasicTypeVoid);
1911
140
    CompilerType void_ptr_clang_type = void_clang_type.GetPointerType();
1912
1913
140
    CompilerType method_type = m_clang_ast_context->CreateFunctionType(
1914
140
        void_clang_type, &void_ptr_clang_type, 1, false, 0);
1915
1916
140
    const bool is_virtual = false;
1917
140
    const bool is_static = false;
1918
140
    const bool is_inline = false;
1919
140
    const bool is_explicit = false;
1920
140
    const bool is_attr_used = true;
1921
140
    const bool is_artificial = false;
1922
1923
140
    CXXMethodDecl *method_decl = m_clang_ast_context->AddMethodToCXXRecordType(
1924
140
        copied_clang_type.GetOpaqueQualType(), "$__lldb_expr", nullptr,
1925
140
        method_type, lldb::eAccessPublic, is_virtual, is_static, is_inline,
1926
140
        is_explicit, is_attr_used, is_artificial);
1927
1928
140
    LLDB_LOG(log,
1929
140
             "  CEDM::AddThisType Added function $__lldb_expr "
1930
140
             "(description {0}) for this type\n{1}",
1931
140
             ClangUtil::ToString(copied_clang_type),
1932
140
             ClangUtil::DumpDecl(method_decl));
1933
140
  }
1934
1935
140
  if (!copied_clang_type.IsValid())
1936
0
    return;
1937
1938
140
  TypeSourceInfo *type_source_info = m_ast_context->getTrivialTypeSourceInfo(
1939
140
      QualType::getFromOpaquePtr(copied_clang_type.GetOpaqueQualType()));
1940
1941
140
  if (!type_source_info)
1942
0
    return;
1943
1944
  // Construct a typedef type because if "*this" is a templated type we can't
1945
  // just return ClassTemplateSpecializationDecls in response to name queries.
1946
  // Using a typedef makes this much more robust.
1947
1948
140
  TypedefDecl *typedef_decl = TypedefDecl::Create(
1949
140
      *m_ast_context, m_ast_context->getTranslationUnitDecl(), SourceLocation(),
1950
140
      SourceLocation(), context.m_decl_name.getAsIdentifierInfo(),
1951
140
      type_source_info);
1952
1953
140
  if (!typedef_decl)
1954
0
    return;
1955
1956
140
  context.AddNamedDecl(typedef_decl);
1957
140
}
1958
1959
void ClangExpressionDeclMap::AddOneType(NameSearchContext &context,
1960
116
                                        const TypeFromUser &ut) {
1961
116
  CompilerType copied_clang_type = GuardedCopyType(ut);
1962
1963
116
  if (!copied_clang_type) {
1964
0
    Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1965
1966
0
    LLDB_LOG(log,
1967
0
             "ClangExpressionDeclMap::AddOneType - Couldn't import the type");
1968
1969
0
    return;
1970
0
  }
1971
1972
116
  context.AddTypeDecl(copied_clang_type);
1973
116
}