1 //===-- ClangUserExpression.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 "lldb/Host/Config.h"
10 
11 #include <cstdio>
12 #if HAVE_SYS_TYPES_H
13 #include <sys/types.h>
14 #endif
15 
16 #include <cstdlib>
17 #include <map>
18 #include <string>
19 
20 #include "ClangUserExpression.h"
21 
22 #include "ASTResultSynthesizer.h"
23 #include "ClangASTMetadata.h"
24 #include "ClangDiagnostic.h"
25 #include "ClangExpressionDeclMap.h"
26 #include "ClangExpressionParser.h"
27 #include "ClangModulesDeclVendor.h"
28 #include "ClangPersistentVariables.h"
29 #include "CppModuleConfiguration.h"
30 
31 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
32 #include "lldb/Core/Debugger.h"
33 #include "lldb/Core/Module.h"
34 #include "lldb/Core/StreamFile.h"
35 #include "lldb/Core/ValueObjectConstResult.h"
36 #include "lldb/Expression/ExpressionSourceCode.h"
37 #include "lldb/Expression/IRExecutionUnit.h"
38 #include "lldb/Expression/IRInterpreter.h"
39 #include "lldb/Expression/Materializer.h"
40 #include "lldb/Host/HostInfo.h"
41 #include "lldb/Symbol/Block.h"
42 #include "lldb/Symbol/CompileUnit.h"
43 #include "lldb/Symbol/Function.h"
44 #include "lldb/Symbol/ObjectFile.h"
45 #include "lldb/Symbol/SymbolFile.h"
46 #include "lldb/Symbol/SymbolVendor.h"
47 #include "lldb/Symbol/Type.h"
48 #include "lldb/Symbol/VariableList.h"
49 #include "lldb/Target/ExecutionContext.h"
50 #include "lldb/Target/Process.h"
51 #include "lldb/Target/StackFrame.h"
52 #include "lldb/Target/Target.h"
53 #include "lldb/Target/ThreadPlan.h"
54 #include "lldb/Target/ThreadPlanCallUserExpression.h"
55 #include "lldb/Utility/ConstString.h"
56 #include "lldb/Utility/Log.h"
57 #include "lldb/Utility/StreamString.h"
58 
59 #include "clang/AST/DeclCXX.h"
60 #include "clang/AST/DeclObjC.h"
61 
62 #include "llvm/ADT/ScopeExit.h"
63 
64 using namespace lldb_private;
65 
66 char ClangUserExpression::ID;
67 
68 ClangUserExpression::ClangUserExpression(
69     ExecutionContextScope &exe_scope, llvm::StringRef expr,
70     llvm::StringRef prefix, lldb::LanguageType language,
71     ResultType desired_type, const EvaluateExpressionOptions &options,
72     ValueObject *ctx_obj)
73     : LLVMUserExpression(exe_scope, expr, prefix, language, desired_type,
74                          options),
75       m_type_system_helper(*m_target_wp.lock(), options.GetExecutionPolicy() ==
76                                                     eExecutionPolicyTopLevel),
77       m_result_delegate(exe_scope.CalculateTarget()), m_ctx_obj(ctx_obj) {
78   switch (m_language) {
79   case lldb::eLanguageTypeC_plus_plus:
80     m_allow_cxx = true;
81     break;
82   case lldb::eLanguageTypeObjC:
83     m_allow_objc = true;
84     break;
85   case lldb::eLanguageTypeObjC_plus_plus:
86   default:
87     m_allow_cxx = true;
88     m_allow_objc = true;
89     break;
90   }
91 }
92 
93 ClangUserExpression::~ClangUserExpression() = default;
94 
95 void ClangUserExpression::ScanContext(ExecutionContext &exe_ctx, Status &err) {
96   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
97 
98   LLDB_LOGF(log, "ClangUserExpression::ScanContext()");
99 
100   m_target = exe_ctx.GetTargetPtr();
101 
102   if (!(m_allow_cxx || m_allow_objc)) {
103     LLDB_LOGF(log, "  [CUE::SC] Settings inhibit C++ and Objective-C");
104     return;
105   }
106 
107   StackFrame *frame = exe_ctx.GetFramePtr();
108   if (frame == nullptr) {
109     LLDB_LOGF(log, "  [CUE::SC] Null stack frame");
110     return;
111   }
112 
113   SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
114                                                   lldb::eSymbolContextBlock);
115 
116   if (!sym_ctx.function) {
117     LLDB_LOGF(log, "  [CUE::SC] Null function");
118     return;
119   }
120 
121   // Find the block that defines the function represented by "sym_ctx"
122   Block *function_block = sym_ctx.GetFunctionBlock();
123 
124   if (!function_block) {
125     LLDB_LOGF(log, "  [CUE::SC] Null function block");
126     return;
127   }
128 
129   CompilerDeclContext decl_context = function_block->GetDeclContext();
130 
131   if (!decl_context) {
132     LLDB_LOGF(log, "  [CUE::SC] Null decl context");
133     return;
134   }
135 
136   if (m_ctx_obj) {
137     switch (m_ctx_obj->GetObjectRuntimeLanguage()) {
138     case lldb::eLanguageTypeC:
139     case lldb::eLanguageTypeC89:
140     case lldb::eLanguageTypeC99:
141     case lldb::eLanguageTypeC11:
142     case lldb::eLanguageTypeC_plus_plus:
143     case lldb::eLanguageTypeC_plus_plus_03:
144     case lldb::eLanguageTypeC_plus_plus_11:
145     case lldb::eLanguageTypeC_plus_plus_14:
146       m_in_cplusplus_method = true;
147       break;
148     case lldb::eLanguageTypeObjC:
149     case lldb::eLanguageTypeObjC_plus_plus:
150       m_in_objectivec_method = true;
151       break;
152     default:
153       break;
154     }
155     m_needs_object_ptr = true;
156   } else if (clang::CXXMethodDecl *method_decl =
157           TypeSystemClang::DeclContextGetAsCXXMethodDecl(decl_context)) {
158     if (m_allow_cxx && method_decl->isInstance()) {
159       if (m_enforce_valid_object) {
160         lldb::VariableListSP variable_list_sp(
161             function_block->GetBlockVariableList(true));
162 
163         const char *thisErrorString = "Stopped in a C++ method, but 'this' "
164                                       "isn't available; pretending we are in a "
165                                       "generic context";
166 
167         if (!variable_list_sp) {
168           err.SetErrorString(thisErrorString);
169           return;
170         }
171 
172         lldb::VariableSP this_var_sp(
173             variable_list_sp->FindVariable(ConstString("this")));
174 
175         if (!this_var_sp || !this_var_sp->IsInScope(frame) ||
176             !this_var_sp->LocationIsValidForFrame(frame)) {
177           err.SetErrorString(thisErrorString);
178           return;
179         }
180       }
181 
182       m_in_cplusplus_method = true;
183       m_needs_object_ptr = true;
184     }
185   } else if (clang::ObjCMethodDecl *method_decl =
186                  TypeSystemClang::DeclContextGetAsObjCMethodDecl(
187                      decl_context)) {
188     if (m_allow_objc) {
189       if (m_enforce_valid_object) {
190         lldb::VariableListSP variable_list_sp(
191             function_block->GetBlockVariableList(true));
192 
193         const char *selfErrorString = "Stopped in an Objective-C method, but "
194                                       "'self' isn't available; pretending we "
195                                       "are in a generic context";
196 
197         if (!variable_list_sp) {
198           err.SetErrorString(selfErrorString);
199           return;
200         }
201 
202         lldb::VariableSP self_variable_sp =
203             variable_list_sp->FindVariable(ConstString("self"));
204 
205         if (!self_variable_sp || !self_variable_sp->IsInScope(frame) ||
206             !self_variable_sp->LocationIsValidForFrame(frame)) {
207           err.SetErrorString(selfErrorString);
208           return;
209         }
210       }
211 
212       m_in_objectivec_method = true;
213       m_needs_object_ptr = true;
214 
215       if (!method_decl->isInstanceMethod())
216         m_in_static_method = true;
217     }
218   } else if (clang::FunctionDecl *function_decl =
219                  TypeSystemClang::DeclContextGetAsFunctionDecl(decl_context)) {
220     // We might also have a function that said in the debug information that it
221     // captured an object pointer.  The best way to deal with getting to the
222     // ivars at present is by pretending that this is a method of a class in
223     // whatever runtime the debug info says the object pointer belongs to.  Do
224     // that here.
225 
226     ClangASTMetadata *metadata =
227         TypeSystemClang::DeclContextGetMetaData(decl_context, function_decl);
228     if (metadata && metadata->HasObjectPtr()) {
229       lldb::LanguageType language = metadata->GetObjectPtrLanguage();
230       if (language == lldb::eLanguageTypeC_plus_plus) {
231         if (m_enforce_valid_object) {
232           lldb::VariableListSP variable_list_sp(
233               function_block->GetBlockVariableList(true));
234 
235           const char *thisErrorString = "Stopped in a context claiming to "
236                                         "capture a C++ object pointer, but "
237                                         "'this' isn't available; pretending we "
238                                         "are in a generic context";
239 
240           if (!variable_list_sp) {
241             err.SetErrorString(thisErrorString);
242             return;
243           }
244 
245           lldb::VariableSP this_var_sp(
246               variable_list_sp->FindVariable(ConstString("this")));
247 
248           if (!this_var_sp || !this_var_sp->IsInScope(frame) ||
249               !this_var_sp->LocationIsValidForFrame(frame)) {
250             err.SetErrorString(thisErrorString);
251             return;
252           }
253         }
254 
255         m_in_cplusplus_method = true;
256         m_needs_object_ptr = true;
257       } else if (language == lldb::eLanguageTypeObjC) {
258         if (m_enforce_valid_object) {
259           lldb::VariableListSP variable_list_sp(
260               function_block->GetBlockVariableList(true));
261 
262           const char *selfErrorString =
263               "Stopped in a context claiming to capture an Objective-C object "
264               "pointer, but 'self' isn't available; pretending we are in a "
265               "generic context";
266 
267           if (!variable_list_sp) {
268             err.SetErrorString(selfErrorString);
269             return;
270           }
271 
272           lldb::VariableSP self_variable_sp =
273               variable_list_sp->FindVariable(ConstString("self"));
274 
275           if (!self_variable_sp || !self_variable_sp->IsInScope(frame) ||
276               !self_variable_sp->LocationIsValidForFrame(frame)) {
277             err.SetErrorString(selfErrorString);
278             return;
279           }
280 
281           Type *self_type = self_variable_sp->GetType();
282 
283           if (!self_type) {
284             err.SetErrorString(selfErrorString);
285             return;
286           }
287 
288           CompilerType self_clang_type = self_type->GetForwardCompilerType();
289 
290           if (!self_clang_type) {
291             err.SetErrorString(selfErrorString);
292             return;
293           }
294 
295           if (TypeSystemClang::IsObjCClassType(self_clang_type)) {
296             return;
297           } else if (TypeSystemClang::IsObjCObjectPointerType(
298                          self_clang_type)) {
299             m_in_objectivec_method = true;
300             m_needs_object_ptr = true;
301           } else {
302             err.SetErrorString(selfErrorString);
303             return;
304           }
305         } else {
306           m_in_objectivec_method = true;
307           m_needs_object_ptr = true;
308         }
309       }
310     }
311   }
312 }
313 
314 // This is a really nasty hack, meant to fix Objective-C expressions of the
315 // form (int)[myArray count].  Right now, because the type information for
316 // count is not available, [myArray count] returns id, which can't be directly
317 // cast to int without causing a clang error.
318 static void ApplyObjcCastHack(std::string &expr) {
319   const std::string from = "(int)[";
320   const std::string to = "(int)(long long)[";
321 
322   size_t offset;
323 
324   while ((offset = expr.find(from)) != expr.npos)
325     expr.replace(offset, from.size(), to);
326 }
327 
328 bool ClangUserExpression::SetupPersistentState(DiagnosticManager &diagnostic_manager,
329                                  ExecutionContext &exe_ctx) {
330   if (Target *target = exe_ctx.GetTargetPtr()) {
331     if (PersistentExpressionState *persistent_state =
332             target->GetPersistentExpressionStateForLanguage(
333                 lldb::eLanguageTypeC)) {
334       m_clang_state = llvm::cast<ClangPersistentVariables>(persistent_state);
335       m_result_delegate.RegisterPersistentState(persistent_state);
336     } else {
337       diagnostic_manager.PutString(
338           eDiagnosticSeverityError,
339           "couldn't start parsing (no persistent data)");
340       return false;
341     }
342   } else {
343     diagnostic_manager.PutString(eDiagnosticSeverityError,
344                                  "error: couldn't start parsing (no target)");
345     return false;
346   }
347   return true;
348 }
349 
350 static void SetupDeclVendor(ExecutionContext &exe_ctx, Target *target,
351                             DiagnosticManager &diagnostic_manager) {
352   if (!target->GetEnableAutoImportClangModules())
353     return;
354 
355   auto *persistent_state = llvm::cast<ClangPersistentVariables>(
356       target->GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC));
357   if (!persistent_state)
358     return;
359 
360   std::shared_ptr<ClangModulesDeclVendor> decl_vendor =
361       persistent_state->GetClangModulesDeclVendor();
362   if (!decl_vendor)
363     return;
364 
365   StackFrame *frame = exe_ctx.GetFramePtr();
366   if (!frame)
367     return;
368 
369   Block *block = frame->GetFrameBlock();
370   if (!block)
371     return;
372   SymbolContext sc;
373 
374   block->CalculateSymbolContext(&sc);
375 
376   if (!sc.comp_unit)
377     return;
378   StreamString error_stream;
379 
380   ClangModulesDeclVendor::ModuleVector modules_for_macros =
381       persistent_state->GetHandLoadedClangModules();
382   if (decl_vendor->AddModulesForCompileUnit(*sc.comp_unit, modules_for_macros,
383                                             error_stream))
384     return;
385 
386   // Failed to load some modules, so emit the error stream as a diagnostic.
387   if (!error_stream.Empty()) {
388     // The error stream already contains several Clang diagnostics that might
389     // be either errors or warnings, so just print them all as one remark
390     // diagnostic to prevent that the message starts with "error: error:".
391     diagnostic_manager.PutString(eDiagnosticSeverityRemark,
392                                  error_stream.GetString());
393     return;
394   }
395 
396   diagnostic_manager.PutString(eDiagnosticSeverityError,
397                                "Unknown error while loading modules needed for "
398                                "current compilation unit.");
399 }
400 
401 ClangExpressionSourceCode::WrapKind ClangUserExpression::GetWrapKind() const {
402   assert(m_options.GetExecutionPolicy() != eExecutionPolicyTopLevel &&
403          "Top level expressions aren't wrapped.");
404   using Kind = ClangExpressionSourceCode::WrapKind;
405   if (m_in_cplusplus_method)
406     return Kind::CppMemberFunction;
407   else if (m_in_objectivec_method) {
408     if (m_in_static_method)
409       return Kind::ObjCStaticMethod;
410     return Kind::ObjCInstanceMethod;
411   }
412   // Not in any kind of 'special' function, so just wrap it in a normal C
413   // function.
414   return Kind::Function;
415 }
416 
417 void ClangUserExpression::CreateSourceCode(
418     DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
419     std::vector<std::string> modules_to_import, bool for_completion) {
420 
421   std::string prefix = m_expr_prefix;
422 
423   if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
424     m_transformed_text = m_expr_text;
425   } else {
426     m_source_code.reset(ClangExpressionSourceCode::CreateWrapped(
427         m_filename, prefix, m_expr_text, GetWrapKind()));
428 
429     if (!m_source_code->GetText(m_transformed_text, exe_ctx, !m_ctx_obj,
430                                 for_completion, modules_to_import)) {
431       diagnostic_manager.PutString(eDiagnosticSeverityError,
432                                    "couldn't construct expression body");
433       return;
434     }
435 
436     // Find and store the start position of the original code inside the
437     // transformed code. We need this later for the code completion.
438     std::size_t original_start;
439     std::size_t original_end;
440     bool found_bounds = m_source_code->GetOriginalBodyBounds(
441         m_transformed_text, original_start, original_end);
442     if (found_bounds)
443       m_user_expression_start_pos = original_start;
444   }
445 }
446 
447 static bool SupportsCxxModuleImport(lldb::LanguageType language) {
448   switch (language) {
449   case lldb::eLanguageTypeC_plus_plus:
450   case lldb::eLanguageTypeC_plus_plus_03:
451   case lldb::eLanguageTypeC_plus_plus_11:
452   case lldb::eLanguageTypeC_plus_plus_14:
453   case lldb::eLanguageTypeObjC_plus_plus:
454     return true;
455   default:
456     return false;
457   }
458 }
459 
460 /// Utility method that puts a message into the expression log and
461 /// returns an invalid module configuration.
462 static CppModuleConfiguration LogConfigError(const std::string &msg) {
463   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
464   LLDB_LOG(log, "[C++ module config] {0}", msg);
465   return CppModuleConfiguration();
466 }
467 
468 CppModuleConfiguration GetModuleConfig(lldb::LanguageType language,
469                                        ExecutionContext &exe_ctx) {
470   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
471 
472   // Don't do anything if this is not a C++ module configuration.
473   if (!SupportsCxxModuleImport(language))
474     return LogConfigError("Language doesn't support C++ modules");
475 
476   Target *target = exe_ctx.GetTargetPtr();
477   if (!target)
478     return LogConfigError("No target");
479 
480   StackFrame *frame = exe_ctx.GetFramePtr();
481   if (!frame)
482     return LogConfigError("No frame");
483 
484   Block *block = frame->GetFrameBlock();
485   if (!block)
486     return LogConfigError("No block");
487 
488   SymbolContext sc;
489   block->CalculateSymbolContext(&sc);
490   if (!sc.comp_unit)
491     return LogConfigError("Couldn't calculate symbol context");
492 
493   // Build a list of files we need to analyze to build the configuration.
494   FileSpecList files;
495   for (const FileSpec &f : sc.comp_unit->GetSupportFiles())
496     files.AppendIfUnique(f);
497   // We also need to look at external modules in the case of -gmodules as they
498   // contain the support files for libc++ and the C library.
499   llvm::DenseSet<SymbolFile *> visited_symbol_files;
500   sc.comp_unit->ForEachExternalModule(
501       visited_symbol_files, [&files](Module &module) {
502         for (std::size_t i = 0; i < module.GetNumCompileUnits(); ++i) {
503           const FileSpecList &support_files =
504               module.GetCompileUnitAtIndex(i)->GetSupportFiles();
505           for (const FileSpec &f : support_files) {
506             files.AppendIfUnique(f);
507           }
508         }
509         return false;
510       });
511 
512   LLDB_LOG(log, "[C++ module config] Found {0} support files to analyze",
513            files.GetSize());
514   if (log && log->GetVerbose()) {
515     for (const FileSpec &f : files)
516       LLDB_LOGV(log, "[C++ module config] Analyzing support file: {0}",
517                 f.GetPath());
518   }
519 
520   // Try to create a configuration from the files. If there is no valid
521   // configuration possible with the files, this just returns an invalid
522   // configuration.
523   return CppModuleConfiguration(files);
524 }
525 
526 bool ClangUserExpression::PrepareForParsing(
527     DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
528     bool for_completion) {
529   InstallContext(exe_ctx);
530 
531   if (!SetupPersistentState(diagnostic_manager, exe_ctx))
532     return false;
533 
534   Status err;
535   ScanContext(exe_ctx, err);
536 
537   if (!err.Success()) {
538     diagnostic_manager.PutString(eDiagnosticSeverityWarning, err.AsCString());
539   }
540 
541   ////////////////////////////////////
542   // Generate the expression
543   //
544 
545   ApplyObjcCastHack(m_expr_text);
546 
547   SetupDeclVendor(exe_ctx, m_target, diagnostic_manager);
548 
549   m_filename = m_clang_state->GetNextExprFileName();
550 
551   if (m_target->GetImportStdModule() == eImportStdModuleTrue)
552     SetupCppModuleImports(exe_ctx);
553 
554   CreateSourceCode(diagnostic_manager, exe_ctx, m_imported_cpp_modules,
555                    for_completion);
556   return true;
557 }
558 
559 bool ClangUserExpression::TryParse(
560     DiagnosticManager &diagnostic_manager, ExecutionContextScope *exe_scope,
561     ExecutionContext &exe_ctx, lldb_private::ExecutionPolicy execution_policy,
562     bool keep_result_in_memory, bool generate_debug_info) {
563   m_materializer_up = std::make_unique<Materializer>();
564 
565   ResetDeclMap(exe_ctx, m_result_delegate, keep_result_in_memory);
566 
567   auto on_exit = llvm::make_scope_exit([this]() { ResetDeclMap(); });
568 
569   if (!DeclMap()->WillParse(exe_ctx, GetMaterializer())) {
570     diagnostic_manager.PutString(
571         eDiagnosticSeverityError,
572         "current process state is unsuitable for expression parsing");
573     return false;
574   }
575 
576   if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
577     DeclMap()->SetLookupsEnabled(true);
578   }
579 
580   m_parser = std::make_unique<ClangExpressionParser>(
581       exe_scope, *this, generate_debug_info, m_include_directories, m_filename);
582 
583   unsigned num_errors = m_parser->Parse(diagnostic_manager);
584 
585   // Check here for FixItHints.  If there are any try to apply the fixits and
586   // set the fixed text in m_fixed_text before returning an error.
587   if (num_errors) {
588     if (diagnostic_manager.HasFixIts()) {
589       if (m_parser->RewriteExpression(diagnostic_manager)) {
590         size_t fixed_start;
591         size_t fixed_end;
592         m_fixed_text = diagnostic_manager.GetFixedExpression();
593         // Retrieve the original expression in case we don't have a top level
594         // expression (which has no surrounding source code).
595         if (m_source_code && m_source_code->GetOriginalBodyBounds(
596                                  m_fixed_text, fixed_start, fixed_end))
597           m_fixed_text =
598               m_fixed_text.substr(fixed_start, fixed_end - fixed_start);
599       }
600     }
601     return false;
602   }
603 
604   //////////////////////////////////////////////////////////////////////////////
605   // Prepare the output of the parser for execution, evaluating it statically
606   // if possible
607   //
608 
609   {
610     Status jit_error = m_parser->PrepareForExecution(
611         m_jit_start_addr, m_jit_end_addr, m_execution_unit_sp, exe_ctx,
612         m_can_interpret, execution_policy);
613 
614     if (!jit_error.Success()) {
615       const char *error_cstr = jit_error.AsCString();
616       if (error_cstr && error_cstr[0])
617         diagnostic_manager.PutString(eDiagnosticSeverityError, error_cstr);
618       else
619         diagnostic_manager.PutString(eDiagnosticSeverityError,
620                                      "expression can't be interpreted or run");
621       return false;
622     }
623   }
624   return true;
625 }
626 
627 void ClangUserExpression::SetupCppModuleImports(ExecutionContext &exe_ctx) {
628   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
629 
630   CppModuleConfiguration module_config = GetModuleConfig(m_language, exe_ctx);
631   m_imported_cpp_modules = module_config.GetImportedModules();
632   m_include_directories = module_config.GetIncludeDirs();
633 
634   LLDB_LOG(log, "List of imported modules in expression: {0}",
635            llvm::make_range(m_imported_cpp_modules.begin(),
636                             m_imported_cpp_modules.end()));
637   LLDB_LOG(log, "List of include directories gathered for modules: {0}",
638            llvm::make_range(m_include_directories.begin(),
639                             m_include_directories.end()));
640 }
641 
642 static bool shouldRetryWithCppModule(Target &target, ExecutionPolicy exe_policy) {
643   // Top-level expression don't yet support importing C++ modules.
644   if (exe_policy == ExecutionPolicy::eExecutionPolicyTopLevel)
645     return false;
646   return target.GetImportStdModule() == eImportStdModuleFallback;
647 }
648 
649 bool ClangUserExpression::Parse(DiagnosticManager &diagnostic_manager,
650                                 ExecutionContext &exe_ctx,
651                                 lldb_private::ExecutionPolicy execution_policy,
652                                 bool keep_result_in_memory,
653                                 bool generate_debug_info) {
654   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
655 
656   if (!PrepareForParsing(diagnostic_manager, exe_ctx, /*for_completion*/ false))
657     return false;
658 
659   LLDB_LOGF(log, "Parsing the following code:\n%s", m_transformed_text.c_str());
660 
661   ////////////////////////////////////
662   // Set up the target and compiler
663   //
664 
665   Target *target = exe_ctx.GetTargetPtr();
666 
667   if (!target) {
668     diagnostic_manager.PutString(eDiagnosticSeverityError, "invalid target");
669     return false;
670   }
671 
672   //////////////////////////
673   // Parse the expression
674   //
675 
676   Process *process = exe_ctx.GetProcessPtr();
677   ExecutionContextScope *exe_scope = process;
678 
679   if (!exe_scope)
680     exe_scope = exe_ctx.GetTargetPtr();
681 
682   bool parse_success = TryParse(diagnostic_manager, exe_scope, exe_ctx,
683                                 execution_policy, keep_result_in_memory,
684                                 generate_debug_info);
685   // If the expression failed to parse, check if retrying parsing with a loaded
686   // C++ module is possible.
687   if (!parse_success && shouldRetryWithCppModule(*target, execution_policy)) {
688     // Load the loaded C++ modules.
689     SetupCppModuleImports(exe_ctx);
690     // If we did load any modules, then retry parsing.
691     if (!m_imported_cpp_modules.empty()) {
692       // The module imports are injected into the source code wrapper,
693       // so recreate those.
694       CreateSourceCode(diagnostic_manager, exe_ctx, m_imported_cpp_modules,
695                        /*for_completion*/ false);
696       // Clear the error diagnostics from the previous parse attempt.
697       diagnostic_manager.Clear();
698       parse_success = TryParse(diagnostic_manager, exe_scope, exe_ctx,
699                                execution_policy, keep_result_in_memory,
700                                generate_debug_info);
701     }
702   }
703   if (!parse_success)
704     return false;
705 
706   if (exe_ctx.GetProcessPtr() && execution_policy == eExecutionPolicyTopLevel) {
707     Status static_init_error =
708         m_parser->RunStaticInitializers(m_execution_unit_sp, exe_ctx);
709 
710     if (!static_init_error.Success()) {
711       const char *error_cstr = static_init_error.AsCString();
712       if (error_cstr && error_cstr[0])
713         diagnostic_manager.Printf(eDiagnosticSeverityError,
714                                   "%s\n",
715                                   error_cstr);
716       else
717         diagnostic_manager.PutString(eDiagnosticSeverityError,
718                                      "couldn't run static initializers\n");
719       return false;
720     }
721   }
722 
723   if (m_execution_unit_sp) {
724     bool register_execution_unit = false;
725 
726     if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
727       register_execution_unit = true;
728     }
729 
730     // If there is more than one external function in the execution unit, it
731     // needs to keep living even if it's not top level, because the result
732     // could refer to that function.
733 
734     if (m_execution_unit_sp->GetJittedFunctions().size() > 1) {
735       register_execution_unit = true;
736     }
737 
738     if (register_execution_unit) {
739       if (auto *persistent_state =
740               exe_ctx.GetTargetPtr()->GetPersistentExpressionStateForLanguage(
741                   m_language))
742         persistent_state->RegisterExecutionUnit(m_execution_unit_sp);
743     }
744   }
745 
746   if (generate_debug_info) {
747     lldb::ModuleSP jit_module_sp(m_execution_unit_sp->GetJITModule());
748 
749     if (jit_module_sp) {
750       ConstString const_func_name(FunctionName());
751       FileSpec jit_file;
752       jit_file.GetFilename() = const_func_name;
753       jit_module_sp->SetFileSpecAndObjectName(jit_file, ConstString());
754       m_jit_module_wp = jit_module_sp;
755       target->GetImages().Append(jit_module_sp);
756     }
757   }
758 
759   if (process && m_jit_start_addr != LLDB_INVALID_ADDRESS)
760     m_jit_process_wp = lldb::ProcessWP(process->shared_from_this());
761   return true;
762 }
763 
764 /// Converts an absolute position inside a given code string into
765 /// a column/line pair.
766 ///
767 /// \param[in] abs_pos
768 ///     A absolute position in the code string that we want to convert
769 ///     to a column/line pair.
770 ///
771 /// \param[in] code
772 ///     A multi-line string usually representing source code.
773 ///
774 /// \param[out] line
775 ///     The line in the code that contains the given absolute position.
776 ///     The first line in the string is indexed as 1.
777 ///
778 /// \param[out] column
779 ///     The column in the line that contains the absolute position.
780 ///     The first character in a line is indexed as 0.
781 static void AbsPosToLineColumnPos(size_t abs_pos, llvm::StringRef code,
782                                   unsigned &line, unsigned &column) {
783   // Reset to code position to beginning of the file.
784   line = 0;
785   column = 0;
786 
787   assert(abs_pos <= code.size() && "Absolute position outside code string?");
788 
789   // We have to walk up to the position and count lines/columns.
790   for (std::size_t i = 0; i < abs_pos; ++i) {
791     // If we hit a line break, we go back to column 0 and enter a new line.
792     // We only handle \n because that's what we internally use to make new
793     // lines for our temporary code strings.
794     if (code[i] == '\n') {
795       ++line;
796       column = 0;
797       continue;
798     }
799     ++column;
800   }
801 }
802 
803 bool ClangUserExpression::Complete(ExecutionContext &exe_ctx,
804                                    CompletionRequest &request,
805                                    unsigned complete_pos) {
806   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
807 
808   // We don't want any visible feedback when completing an expression. Mostly
809   // because the results we get from an incomplete invocation are probably not
810   // correct.
811   DiagnosticManager diagnostic_manager;
812 
813   if (!PrepareForParsing(diagnostic_manager, exe_ctx, /*for_completion*/ true))
814     return false;
815 
816   LLDB_LOGF(log, "Parsing the following code:\n%s", m_transformed_text.c_str());
817 
818   //////////////////////////
819   // Parse the expression
820   //
821 
822   m_materializer_up = std::make_unique<Materializer>();
823 
824   ResetDeclMap(exe_ctx, m_result_delegate, /*keep result in memory*/ true);
825 
826   auto on_exit = llvm::make_scope_exit([this]() { ResetDeclMap(); });
827 
828   if (!DeclMap()->WillParse(exe_ctx, GetMaterializer())) {
829     diagnostic_manager.PutString(
830         eDiagnosticSeverityError,
831         "current process state is unsuitable for expression parsing");
832 
833     return false;
834   }
835 
836   if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
837     DeclMap()->SetLookupsEnabled(true);
838   }
839 
840   Process *process = exe_ctx.GetProcessPtr();
841   ExecutionContextScope *exe_scope = process;
842 
843   if (!exe_scope)
844     exe_scope = exe_ctx.GetTargetPtr();
845 
846   ClangExpressionParser parser(exe_scope, *this, false);
847 
848   // We have to find the source code location where the user text is inside
849   // the transformed expression code. When creating the transformed text, we
850   // already stored the absolute position in the m_transformed_text string. The
851   // only thing left to do is to transform it into the line:column format that
852   // Clang expects.
853 
854   // The line and column of the user expression inside the transformed source
855   // code.
856   unsigned user_expr_line, user_expr_column;
857   if (m_user_expression_start_pos.hasValue())
858     AbsPosToLineColumnPos(*m_user_expression_start_pos, m_transformed_text,
859                           user_expr_line, user_expr_column);
860   else
861     return false;
862 
863   // The actual column where we have to complete is the start column of the
864   // user expression + the offset inside the user code that we were given.
865   const unsigned completion_column = user_expr_column + complete_pos;
866   parser.Complete(request, user_expr_line, completion_column, complete_pos);
867 
868   return true;
869 }
870 
871 bool ClangUserExpression::AddArguments(ExecutionContext &exe_ctx,
872                                        std::vector<lldb::addr_t> &args,
873                                        lldb::addr_t struct_address,
874                                        DiagnosticManager &diagnostic_manager) {
875   lldb::addr_t object_ptr = LLDB_INVALID_ADDRESS;
876   lldb::addr_t cmd_ptr = LLDB_INVALID_ADDRESS;
877 
878   if (m_needs_object_ptr) {
879     lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP();
880     if (!frame_sp)
881       return true;
882 
883     ConstString object_name;
884 
885     if (m_in_cplusplus_method) {
886       object_name.SetCString("this");
887     } else if (m_in_objectivec_method) {
888       object_name.SetCString("self");
889     } else {
890       diagnostic_manager.PutString(
891           eDiagnosticSeverityError,
892           "need object pointer but don't know the language");
893       return false;
894     }
895 
896     Status object_ptr_error;
897 
898     if (m_ctx_obj) {
899       AddressType address_type;
900       object_ptr = m_ctx_obj->GetAddressOf(false, &address_type);
901       if (object_ptr == LLDB_INVALID_ADDRESS ||
902           address_type != eAddressTypeLoad)
903         object_ptr_error.SetErrorString("Can't get context object's "
904                                         "debuggee address");
905     } else
906       object_ptr = GetObjectPointer(frame_sp, object_name, object_ptr_error);
907 
908     if (!object_ptr_error.Success()) {
909       exe_ctx.GetTargetRef().GetDebugger().GetAsyncOutputStream()->Printf(
910           "warning: `%s' is not accessible (substituting 0)\n",
911           object_name.AsCString());
912       object_ptr = 0;
913     }
914 
915     if (m_in_objectivec_method) {
916       ConstString cmd_name("_cmd");
917 
918       cmd_ptr = GetObjectPointer(frame_sp, cmd_name, object_ptr_error);
919 
920       if (!object_ptr_error.Success()) {
921         diagnostic_manager.Printf(
922             eDiagnosticSeverityWarning,
923             "couldn't get cmd pointer (substituting NULL): %s",
924             object_ptr_error.AsCString());
925         cmd_ptr = 0;
926       }
927     }
928 
929     args.push_back(object_ptr);
930 
931     if (m_in_objectivec_method)
932       args.push_back(cmd_ptr);
933 
934     args.push_back(struct_address);
935   } else {
936     args.push_back(struct_address);
937   }
938   return true;
939 }
940 
941 lldb::ExpressionVariableSP ClangUserExpression::GetResultAfterDematerialization(
942     ExecutionContextScope *exe_scope) {
943   return m_result_delegate.GetVariable();
944 }
945 
946 void ClangUserExpression::ClangUserExpressionHelper::ResetDeclMap(
947     ExecutionContext &exe_ctx,
948     Materializer::PersistentVariableDelegate &delegate,
949     bool keep_result_in_memory,
950     ValueObject *ctx_obj) {
951   std::shared_ptr<ClangASTImporter> ast_importer;
952   auto *state = exe_ctx.GetTargetSP()->GetPersistentExpressionStateForLanguage(
953       lldb::eLanguageTypeC);
954   if (state) {
955     auto *persistent_vars = llvm::cast<ClangPersistentVariables>(state);
956     ast_importer = persistent_vars->GetClangASTImporter();
957   }
958   m_expr_decl_map_up = std::make_unique<ClangExpressionDeclMap>(
959       keep_result_in_memory, &delegate, exe_ctx.GetTargetSP(), ast_importer,
960       ctx_obj);
961 }
962 
963 clang::ASTConsumer *
964 ClangUserExpression::ClangUserExpressionHelper::ASTTransformer(
965     clang::ASTConsumer *passthrough) {
966   m_result_synthesizer_up = std::make_unique<ASTResultSynthesizer>(
967       passthrough, m_top_level, m_target);
968 
969   return m_result_synthesizer_up.get();
970 }
971 
972 void ClangUserExpression::ClangUserExpressionHelper::CommitPersistentDecls() {
973   if (m_result_synthesizer_up) {
974     m_result_synthesizer_up->CommitPersistentDecls();
975   }
976 }
977 
978 ConstString ClangUserExpression::ResultDelegate::GetName() {
979   return m_persistent_state->GetNextPersistentVariableName(false);
980 }
981 
982 void ClangUserExpression::ResultDelegate::DidDematerialize(
983     lldb::ExpressionVariableSP &variable) {
984   m_variable = variable;
985 }
986 
987 void ClangUserExpression::ResultDelegate::RegisterPersistentState(
988     PersistentExpressionState *persistent_state) {
989   m_persistent_state = persistent_state;
990 }
991 
992 lldb::ExpressionVariableSP &ClangUserExpression::ResultDelegate::GetVariable() {
993   return m_variable;
994 }
995