xref: /llvm-project/lldb/source/Expression/ExpressionParser.cpp (revision 84fdfb9ca63ee4304b486d7e85545ee4e1a46f5d)
1bfd263a3SMichael Buch //===-- ExpressionParser.cpp ----------------------------------------------===//
2bfd263a3SMichael Buch //
3bfd263a3SMichael Buch // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4bfd263a3SMichael Buch // See https://llvm.org/LICENSE.txt for license information.
5bfd263a3SMichael Buch // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bfd263a3SMichael Buch //
7bfd263a3SMichael Buch //===----------------------------------------------------------------------===//
8bfd263a3SMichael Buch 
9bfd263a3SMichael Buch #include "lldb/Expression/ExpressionParser.h"
10bfd263a3SMichael Buch #include "lldb/Expression/DiagnosticManager.h"
11bfd263a3SMichael Buch #include "lldb/Expression/IRExecutionUnit.h"
12bfd263a3SMichael Buch #include "lldb/Target/ExecutionContext.h"
13bfd263a3SMichael Buch #include "lldb/Target/ThreadPlanCallFunction.h"
14bfd263a3SMichael Buch 
15bfd263a3SMichael Buch using namespace lldb;
16bfd263a3SMichael Buch using namespace lldb_private;
17bfd263a3SMichael Buch 
18bfd263a3SMichael Buch Status ExpressionParser::PrepareForExecution(
19bfd263a3SMichael Buch     addr_t &func_addr, addr_t &func_end,
20bfd263a3SMichael Buch     std::shared_ptr<IRExecutionUnit> &execution_unit_sp,
21bfd263a3SMichael Buch     ExecutionContext &exe_ctx, bool &can_interpret,
22bfd263a3SMichael Buch     ExecutionPolicy execution_policy) {
23bfd263a3SMichael Buch   Status status =
24bfd263a3SMichael Buch       DoPrepareForExecution(func_addr, func_end, execution_unit_sp, exe_ctx,
25bfd263a3SMichael Buch                             can_interpret, execution_policy);
26bfd263a3SMichael Buch   if (status.Success() && exe_ctx.GetProcessPtr() && exe_ctx.HasThreadScope())
27bfd263a3SMichael Buch     status = RunStaticInitializers(execution_unit_sp, exe_ctx);
28bfd263a3SMichael Buch 
29bfd263a3SMichael Buch   return status;
30bfd263a3SMichael Buch }
31bfd263a3SMichael Buch 
32bfd263a3SMichael Buch Status
33bfd263a3SMichael Buch ExpressionParser::RunStaticInitializers(IRExecutionUnitSP &execution_unit_sp,
34bfd263a3SMichael Buch                                         ExecutionContext &exe_ctx) {
35bfd263a3SMichael Buch   Status err;
36bfd263a3SMichael Buch 
37bfd263a3SMichael Buch   if (!execution_unit_sp.get()) {
380642cd76SAdrian Prantl     err = Status::FromErrorString(
39bfd263a3SMichael Buch         "can't run static initializers for a NULL execution unit");
40bfd263a3SMichael Buch     return err;
41bfd263a3SMichael Buch   }
42bfd263a3SMichael Buch 
43bfd263a3SMichael Buch   if (!exe_ctx.HasThreadScope()) {
440642cd76SAdrian Prantl     err = Status::FromErrorString(
450642cd76SAdrian Prantl         "can't run static initializers without a thread");
46bfd263a3SMichael Buch     return err;
47bfd263a3SMichael Buch   }
48bfd263a3SMichael Buch 
49bfd263a3SMichael Buch   std::vector<addr_t> static_initializers;
50bfd263a3SMichael Buch 
51bfd263a3SMichael Buch   execution_unit_sp->GetStaticInitializers(static_initializers);
52bfd263a3SMichael Buch 
53bfd263a3SMichael Buch   for (addr_t static_initializer : static_initializers) {
54bfd263a3SMichael Buch     EvaluateExpressionOptions options;
55bfd263a3SMichael Buch 
56bfd263a3SMichael Buch     ThreadPlanSP call_static_initializer(new ThreadPlanCallFunction(
57bfd263a3SMichael Buch         exe_ctx.GetThreadRef(), Address(static_initializer), CompilerType(),
58bfd263a3SMichael Buch         llvm::ArrayRef<addr_t>(), options));
59bfd263a3SMichael Buch 
60bfd263a3SMichael Buch     DiagnosticManager execution_errors;
61bfd263a3SMichael Buch     ExpressionResults results =
62bfd263a3SMichael Buch         exe_ctx.GetThreadRef().GetProcess()->RunThreadPlan(
63bfd263a3SMichael Buch             exe_ctx, call_static_initializer, options, execution_errors);
64bfd263a3SMichael Buch 
65bfd263a3SMichael Buch     if (results != eExpressionCompleted) {
66*84fdfb9cSAdrian Prantl       err = Status::FromError(execution_errors.GetAsError(
67*84fdfb9cSAdrian Prantl           lldb::eExpressionSetupError, "couldn't run static initializer:"));
68bfd263a3SMichael Buch       return err;
69bfd263a3SMichael Buch     }
70bfd263a3SMichael Buch   }
71bfd263a3SMichael Buch 
72bfd263a3SMichael Buch   return err;
73bfd263a3SMichael Buch }
74