Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 1 | //===-- ClangUserExpression.cpp -------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // C Includes |
| 11 | #include <stdio.h> |
| 12 | #if HAVE_SYS_TYPES_H |
| 13 | # include <sys/types.h> |
| 14 | #endif |
| 15 | |
| 16 | // C++ Includes |
| 17 | |
| 18 | #include "lldb/Core/ConstString.h" |
| 19 | #include "lldb/Core/Stream.h" |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 20 | #include "lldb/Expression/ClangExpressionDeclMap.h" |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 21 | #include "lldb/Expression/ClangExpressionParser.h" |
| 22 | #include "lldb/Expression/ClangUtilityFunction.h" |
| 23 | #include "lldb/Host/Host.h" |
| 24 | #include "lldb/Target/ExecutionContext.h" |
| 25 | #include "lldb/Target/Target.h" |
| 26 | |
| 27 | using namespace lldb_private; |
| 28 | |
| 29 | //------------------------------------------------------------------ |
| 30 | /// Constructor |
| 31 | /// |
| 32 | /// @param[in] text |
| 33 | /// The text of the function. Must be a full translation unit. |
| 34 | /// |
| 35 | /// @param[in] name |
| 36 | /// The name of the function, as used in the text. |
| 37 | //------------------------------------------------------------------ |
| 38 | ClangUtilityFunction::ClangUtilityFunction (const char *text, |
| 39 | const char *name) : |
| 40 | m_function_text(text), |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 41 | m_function_name(name), |
| 42 | m_jit_begin(LLDB_INVALID_ADDRESS), |
| 43 | m_jit_end(LLDB_INVALID_ADDRESS) |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
Greg Clayton | 6a5aa8a | 2010-09-24 23:07:41 +0000 | [diff] [blame] | 47 | ClangUtilityFunction::~ClangUtilityFunction () |
| 48 | { |
| 49 | } |
| 50 | |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 51 | //------------------------------------------------------------------ |
| 52 | /// Install the utility function into a process |
| 53 | /// |
| 54 | /// @param[in] error_stream |
| 55 | /// A stream to print parse errors and warnings to. |
| 56 | /// |
| 57 | /// @param[in] exe_ctx |
| 58 | /// The execution context to install the utility function to. |
| 59 | /// |
| 60 | /// @return |
| 61 | /// True on success (no errors); false otherwise. |
| 62 | //------------------------------------------------------------------ |
| 63 | bool |
| 64 | ClangUtilityFunction::Install (Stream &error_stream, |
| 65 | ExecutionContext &exe_ctx) |
Sean Callanan | f18d91c | 2010-09-01 00:58:00 +0000 | [diff] [blame] | 66 | { |
| 67 | if (m_jit_begin != LLDB_INVALID_ADDRESS) |
| 68 | { |
| 69 | error_stream.PutCString("error: already installed\n"); |
| 70 | return false; |
| 71 | } |
| 72 | |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 73 | //////////////////////////////////// |
| 74 | // Set up the target and compiler |
| 75 | // |
| 76 | |
| 77 | Target *target = exe_ctx.target; |
| 78 | |
| 79 | if (!target) |
| 80 | { |
| 81 | error_stream.PutCString ("error: invalid target\n"); |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | ConstString target_triple; |
| 86 | |
| 87 | target->GetTargetTriple (target_triple); |
| 88 | |
| 89 | if (!target_triple) |
| 90 | target_triple = Host::GetTargetTriple (); |
| 91 | |
| 92 | if (!target_triple) |
| 93 | { |
| 94 | error_stream.PutCString ("error: invalid target triple\n"); |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | ////////////////////////// |
| 99 | // Parse the expression |
| 100 | // |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 101 | |
Sean Callanan | aa301c4 | 2010-12-03 01:38:59 +0000 | [diff] [blame^] | 102 | m_expr_decl_map.reset(new ClangExpressionDeclMap()); |
| 103 | |
| 104 | m_expr_decl_map->WillParse(exe_ctx); |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 105 | |
| 106 | ClangExpressionParser parser(target_triple.GetCString(), *this); |
| 107 | |
| 108 | unsigned num_errors = parser.Parse (error_stream); |
| 109 | |
| 110 | if (num_errors) |
| 111 | { |
| 112 | error_stream.Printf ("error: %d errors parsing expression\n", num_errors); |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 113 | |
| 114 | m_expr_decl_map.reset(); |
| 115 | |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 116 | return false; |
| 117 | } |
| 118 | |
| 119 | ////////////////////////////////// |
| 120 | // JIT the output of the parser |
| 121 | // |
| 122 | |
| 123 | Error jit_error = parser.MakeJIT (m_jit_begin, m_jit_end, exe_ctx); |
| 124 | |
Sean Callanan | aa301c4 | 2010-12-03 01:38:59 +0000 | [diff] [blame^] | 125 | m_expr_decl_map->DidParse(); |
| 126 | |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 127 | m_expr_decl_map.reset(); |
| 128 | |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 129 | if (jit_error.Success()) |
| 130 | { |
| 131 | return true; |
| 132 | } |
| 133 | else |
| 134 | { |
| 135 | error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors); |
| 136 | return false; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | |