blob: 080562e51e91a4ad70cc91145f1e5dc43c64031d [file] [log] [blame]
Sean Callanan9bc83842011-09-26 18:45:31 +00001//===-- ExpressionSourceCode.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#include "lldb/Expression/ExpressionSourceCode.h"
11
12#include "lldb/Core/StreamString.h"
Jason Molendaa3329782014-03-29 18:54:20 +000013#include "lldb/Target/ExecutionContext.h"
14#include "lldb/Target/Platform.h"
15#include "lldb/Target/Target.h"
Sean Callanan9bc83842011-09-26 18:45:31 +000016
17using namespace lldb_private;
18
Greg Clayton399107a2013-02-13 23:57:48 +000019const char *
20ExpressionSourceCode::g_expression_prefix = R"(
21#undef NULL
22#undef Nil
23#undef nil
24#undef YES
25#undef NO
26#define NULL (__null)
27#define Nil (__null)
28#define nil (__null)
29#define YES ((BOOL)1)
30#define NO ((BOOL)0)
Reid Kleckner56d861f2014-07-29 21:59:33 +000031typedef __INT8_TYPE__ int8_t;
32typedef __UINT8_TYPE__ uint8_t;
33typedef __INT16_TYPE__ int16_t;
34typedef __UINT16_TYPE__ uint16_t;
35typedef __INT32_TYPE__ int32_t;
36typedef __UINT32_TYPE__ uint32_t;
37typedef __INT64_TYPE__ int64_t;
38typedef __UINT64_TYPE__ uint64_t;
39typedef __INTPTR_TYPE__ intptr_t;
40typedef __UINTPTR_TYPE__ uintptr_t;
Greg Clayton399107a2013-02-13 23:57:48 +000041typedef __SIZE_TYPE__ size_t;
42typedef __PTRDIFF_TYPE__ ptrdiff_t;
43typedef unsigned short unichar;
44)";
Sean Callanana199e822012-09-13 23:35:30 +000045
46
Jason Molendaa3329782014-03-29 18:54:20 +000047bool ExpressionSourceCode::GetText (std::string &text, lldb::LanguageType wrapping_language, bool const_object, bool static_method, ExecutionContext &exe_ctx) const
Sean Callanan9bc83842011-09-26 18:45:31 +000048{
Jason Molendaa3329782014-03-29 18:54:20 +000049 const char *target_specific_defines = "typedef signed char BOOL;\n";
50 static ConstString g_platform_ios_simulator ("PlatformiOSSimulator");
51
52 if (Target *target = exe_ctx.GetTargetPtr())
53 {
Todd Fialad8eaa172014-07-23 14:37:35 +000054 if (target->GetArchitecture().GetMachine() == llvm::Triple::aarch64)
Jason Molendaa3329782014-03-29 18:54:20 +000055 {
56 target_specific_defines = "typedef bool BOOL;\n";
57 }
58 if (target->GetArchitecture().GetMachine() == llvm::Triple::x86_64)
59 {
60 if (lldb::PlatformSP platform_sp = target->GetPlatform())
61 {
62 if (platform_sp->GetPluginName() == g_platform_ios_simulator)
63 {
64 target_specific_defines = "typedef bool BOOL;\n";
65 }
66 }
67 }
68 }
69
Sean Callanan9bc83842011-09-26 18:45:31 +000070 if (m_wrap)
71 {
72 switch (wrapping_language)
73 {
74 default:
75 return false;
76 case lldb::eLanguageTypeC:
77 case lldb::eLanguageTypeC_plus_plus:
78 case lldb::eLanguageTypeObjC:
79 break;
80 }
81
82 StreamString wrap_stream;
83
84 switch (wrapping_language)
85 {
86 default:
87 break;
88 case lldb::eLanguageTypeC:
89 wrap_stream.Printf("%s \n"
Sean Callanana199e822012-09-13 23:35:30 +000090 "%s \n"
Jason Molendaa3329782014-03-29 18:54:20 +000091 "%s \n"
Sean Callanan9bc83842011-09-26 18:45:31 +000092 "void \n"
93 "%s(void *$__lldb_arg) \n"
94 "{ \n"
95 " %s; \n"
96 "} \n",
Greg Clayton399107a2013-02-13 23:57:48 +000097 g_expression_prefix,
Jason Molendaa3329782014-03-29 18:54:20 +000098 target_specific_defines,
Greg Claytonddf976a2013-07-03 00:07:54 +000099 m_prefix.c_str(),
Sean Callanan9bc83842011-09-26 18:45:31 +0000100 m_name.c_str(),
101 m_body.c_str());
102 break;
103 case lldb::eLanguageTypeC_plus_plus:
104 wrap_stream.Printf("%s \n"
Sean Callanana199e822012-09-13 23:35:30 +0000105 "%s \n"
Jason Molendaa3329782014-03-29 18:54:20 +0000106 "%s \n"
Sean Callanan9bc83842011-09-26 18:45:31 +0000107 "void \n"
108 "$__lldb_class::%s(void *$__lldb_arg) %s\n"
109 "{ \n"
110 " %s; \n"
111 "} \n",
Greg Clayton399107a2013-02-13 23:57:48 +0000112 g_expression_prefix,
Jason Molendaa3329782014-03-29 18:54:20 +0000113 target_specific_defines,
Greg Claytonddf976a2013-07-03 00:07:54 +0000114 m_prefix.c_str(),
Sean Callanan9bc83842011-09-26 18:45:31 +0000115 m_name.c_str(),
116 (const_object ? "const" : ""),
117 m_body.c_str());
118 break;
119 case lldb::eLanguageTypeObjC:
Sean Callanand5c17ed2011-11-15 02:11:17 +0000120 if (static_method)
121 {
122 wrap_stream.Printf("%s \n"
Sean Callanana199e822012-09-13 23:35:30 +0000123 "%s \n"
Jason Molendaa3329782014-03-29 18:54:20 +0000124 "%s \n"
Sean Callanana199e822012-09-13 23:35:30 +0000125 "@interface $__lldb_objc_class ($__lldb_category) \n"
126 "+(void)%s:(void *)$__lldb_arg; \n"
127 "@end \n"
128 "@implementation $__lldb_objc_class ($__lldb_category) \n"
129 "+(void)%s:(void *)$__lldb_arg \n"
130 "{ \n"
131 " %s; \n"
132 "} \n"
133 "@end \n",
Greg Clayton399107a2013-02-13 23:57:48 +0000134 g_expression_prefix,
Jason Molendaa3329782014-03-29 18:54:20 +0000135 target_specific_defines,
Greg Claytonddf976a2013-07-03 00:07:54 +0000136 m_prefix.c_str(),
Sean Callanana199e822012-09-13 23:35:30 +0000137 m_name.c_str(),
138 m_name.c_str(),
139 m_body.c_str());
Sean Callanand5c17ed2011-11-15 02:11:17 +0000140 }
141 else
142 {
Sean Callanan26e184d2012-05-09 21:27:03 +0000143 wrap_stream.Printf("%s \n"
Sean Callanana199e822012-09-13 23:35:30 +0000144 "%s \n"
Jason Molendaa3329782014-03-29 18:54:20 +0000145 "%s \n"
Sean Callanand5c17ed2011-11-15 02:11:17 +0000146 "@interface $__lldb_objc_class ($__lldb_category) \n"
147 "-(void)%s:(void *)$__lldb_arg; \n"
148 "@end \n"
149 "@implementation $__lldb_objc_class ($__lldb_category) \n"
150 "-(void)%s:(void *)$__lldb_arg \n"
151 "{ \n"
152 " %s; \n"
153 "} \n"
154 "@end \n",
Greg Clayton399107a2013-02-13 23:57:48 +0000155 g_expression_prefix,
Jason Molendaa3329782014-03-29 18:54:20 +0000156 target_specific_defines,
Greg Claytonddf976a2013-07-03 00:07:54 +0000157 m_prefix.c_str(),
Sean Callanand5c17ed2011-11-15 02:11:17 +0000158 m_name.c_str(),
159 m_name.c_str(),
160 m_body.c_str());
161 }
Sean Callanan9bc83842011-09-26 18:45:31 +0000162 break;
163 }
164
165 text = wrap_stream.GetString();
166 }
167 else
168 {
169 text.append(m_body);
170 }
171
172 return true;
173}