blob: 9a42510d0a24a0f88469b322097f0faba35cce4b [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"
Sean Callanan68f85e72015-04-30 21:49:58 +000013#include "lldb/Expression/ClangModulesDeclVendor.h"
14#include "lldb/Expression/ClangPersistentVariables.h"
15#include "lldb/Symbol/Block.h"
Jason Molendaa3329782014-03-29 18:54:20 +000016#include "lldb/Target/ExecutionContext.h"
17#include "lldb/Target/Platform.h"
Sean Callanan68f85e72015-04-30 21:49:58 +000018#include "lldb/Target/StackFrame.h"
Jason Molendaa3329782014-03-29 18:54:20 +000019#include "lldb/Target/Target.h"
Sean Callanan9bc83842011-09-26 18:45:31 +000020
21using namespace lldb_private;
22
Greg Clayton399107a2013-02-13 23:57:48 +000023const char *
24ExpressionSourceCode::g_expression_prefix = R"(
Sean Callanan68f85e72015-04-30 21:49:58 +000025#ifndef NULL
Greg Clayton399107a2013-02-13 23:57:48 +000026#define NULL (__null)
Sean Callanan68f85e72015-04-30 21:49:58 +000027#endif
28#ifndef Nil
Greg Clayton399107a2013-02-13 23:57:48 +000029#define Nil (__null)
Sean Callanan68f85e72015-04-30 21:49:58 +000030#endif
31#ifndef nil
Greg Clayton399107a2013-02-13 23:57:48 +000032#define nil (__null)
Sean Callanan68f85e72015-04-30 21:49:58 +000033#endif
34#ifndef YES
Greg Clayton399107a2013-02-13 23:57:48 +000035#define YES ((BOOL)1)
Sean Callanan68f85e72015-04-30 21:49:58 +000036#endif
37#ifndef NO
Greg Clayton399107a2013-02-13 23:57:48 +000038#define NO ((BOOL)0)
Sean Callanan68f85e72015-04-30 21:49:58 +000039#endif
Reid Kleckner56d861f2014-07-29 21:59:33 +000040typedef __INT8_TYPE__ int8_t;
41typedef __UINT8_TYPE__ uint8_t;
42typedef __INT16_TYPE__ int16_t;
43typedef __UINT16_TYPE__ uint16_t;
44typedef __INT32_TYPE__ int32_t;
45typedef __UINT32_TYPE__ uint32_t;
46typedef __INT64_TYPE__ int64_t;
47typedef __UINT64_TYPE__ uint64_t;
48typedef __INTPTR_TYPE__ intptr_t;
49typedef __UINTPTR_TYPE__ uintptr_t;
Greg Clayton399107a2013-02-13 23:57:48 +000050typedef __SIZE_TYPE__ size_t;
51typedef __PTRDIFF_TYPE__ ptrdiff_t;
52typedef unsigned short unichar;
Kate Stonecc391a02015-01-21 23:39:16 +000053extern "C"
54{
55 int printf(const char * __restrict, ...);
56}
Greg Clayton399107a2013-02-13 23:57:48 +000057)";
Sean Callanana199e822012-09-13 23:35:30 +000058
59
Jason Molendaa3329782014-03-29 18:54:20 +000060bool 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 +000061{
Jason Molendaa3329782014-03-29 18:54:20 +000062 const char *target_specific_defines = "typedef signed char BOOL;\n";
Sean Callanan68f85e72015-04-30 21:49:58 +000063 std::string module_macros;
Jason Molendaa3329782014-03-29 18:54:20 +000064
65 if (Target *target = exe_ctx.GetTargetPtr())
66 {
Todd Fialad8eaa172014-07-23 14:37:35 +000067 if (target->GetArchitecture().GetMachine() == llvm::Triple::aarch64)
Jason Molendaa3329782014-03-29 18:54:20 +000068 {
69 target_specific_defines = "typedef bool BOOL;\n";
70 }
71 if (target->GetArchitecture().GetMachine() == llvm::Triple::x86_64)
72 {
73 if (lldb::PlatformSP platform_sp = target->GetPlatform())
74 {
Sean Callanan68f85e72015-04-30 21:49:58 +000075 static ConstString g_platform_ios_simulator ("ios-simulator");
Jason Molendaa3329782014-03-29 18:54:20 +000076 if (platform_sp->GetPluginName() == g_platform_ios_simulator)
77 {
78 target_specific_defines = "typedef bool BOOL;\n";
79 }
80 }
81 }
Sean Callanan68f85e72015-04-30 21:49:58 +000082
83 if (ClangModulesDeclVendor *decl_vendor = target->GetClangModulesDeclVendor())
84 {
85 const ClangModulesDeclVendor::ModuleVector &hand_imported_modules = target->GetPersistentVariables().GetHandLoadedClangModules();
86 ClangModulesDeclVendor::ModuleVector modules_for_macros;
87
88 for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules)
89 {
90 modules_for_macros.push_back(module);
91 }
92
93 if (target->GetEnableAutoImportClangModules())
94 {
95 if (StackFrame *frame = exe_ctx.GetFramePtr())
96 {
97 if (Block *block = frame->GetFrameBlock())
98 {
99 SymbolContext sc;
100
101 block->CalculateSymbolContext(&sc);
102
103 if (sc.comp_unit)
104 {
105 StreamString error_stream;
106
107 decl_vendor->AddModulesForCompileUnit(*sc.comp_unit, modules_for_macros, error_stream);
108 }
109 }
110 }
111 }
112
113 decl_vendor->ForEachMacro(modules_for_macros, [&module_macros] (const std::string &expansion) -> bool {
114 module_macros.append(expansion);
115 module_macros.append("\n");
116 return false;
117 });
118 }
119
Jason Molendaa3329782014-03-29 18:54:20 +0000120 }
121
Sean Callanan9bc83842011-09-26 18:45:31 +0000122 if (m_wrap)
123 {
124 switch (wrapping_language)
125 {
126 default:
127 return false;
128 case lldb::eLanguageTypeC:
129 case lldb::eLanguageTypeC_plus_plus:
130 case lldb::eLanguageTypeObjC:
131 break;
132 }
133
134 StreamString wrap_stream;
135
Sean Callanan68f85e72015-04-30 21:49:58 +0000136 wrap_stream.Printf("%s\n%s\n%s\n%s\n",
137 module_macros.c_str(),
138 g_expression_prefix,
139 target_specific_defines,
140 m_prefix.c_str());
141
Sean Callanan9bc83842011-09-26 18:45:31 +0000142 switch (wrapping_language)
143 {
144 default:
145 break;
146 case lldb::eLanguageTypeC:
Sean Callanan68f85e72015-04-30 21:49:58 +0000147 wrap_stream.Printf("void \n"
Sean Callanan9bc83842011-09-26 18:45:31 +0000148 "%s(void *$__lldb_arg) \n"
149 "{ \n"
150 " %s; \n"
151 "} \n",
Sean Callanan9bc83842011-09-26 18:45:31 +0000152 m_name.c_str(),
153 m_body.c_str());
154 break;
155 case lldb::eLanguageTypeC_plus_plus:
Sean Callanan68f85e72015-04-30 21:49:58 +0000156 wrap_stream.Printf("void \n"
Sean Callanan9bc83842011-09-26 18:45:31 +0000157 "$__lldb_class::%s(void *$__lldb_arg) %s\n"
158 "{ \n"
159 " %s; \n"
160 "} \n",
Sean Callanan9bc83842011-09-26 18:45:31 +0000161 m_name.c_str(),
162 (const_object ? "const" : ""),
163 m_body.c_str());
164 break;
165 case lldb::eLanguageTypeObjC:
Sean Callanand5c17ed2011-11-15 02:11:17 +0000166 if (static_method)
167 {
Sean Callanan68f85e72015-04-30 21:49:58 +0000168 wrap_stream.Printf("@interface $__lldb_objc_class ($__lldb_category) \n"
Sean Callanana199e822012-09-13 23:35:30 +0000169 "+(void)%s:(void *)$__lldb_arg; \n"
170 "@end \n"
171 "@implementation $__lldb_objc_class ($__lldb_category) \n"
172 "+(void)%s:(void *)$__lldb_arg \n"
173 "{ \n"
174 " %s; \n"
175 "} \n"
176 "@end \n",
Sean Callanana199e822012-09-13 23:35:30 +0000177 m_name.c_str(),
178 m_name.c_str(),
179 m_body.c_str());
Sean Callanand5c17ed2011-11-15 02:11:17 +0000180 }
181 else
182 {
Sean Callanan68f85e72015-04-30 21:49:58 +0000183 wrap_stream.Printf("@interface $__lldb_objc_class ($__lldb_category) \n"
Sean Callanand5c17ed2011-11-15 02:11:17 +0000184 "-(void)%s:(void *)$__lldb_arg; \n"
185 "@end \n"
186 "@implementation $__lldb_objc_class ($__lldb_category) \n"
187 "-(void)%s:(void *)$__lldb_arg \n"
188 "{ \n"
189 " %s; \n"
190 "} \n"
191 "@end \n",
Sean Callanand5c17ed2011-11-15 02:11:17 +0000192 m_name.c_str(),
193 m_name.c_str(),
194 m_body.c_str());
195 }
Sean Callanan9bc83842011-09-26 18:45:31 +0000196 break;
197 }
198
199 text = wrap_stream.GetString();
200 }
201 else
202 {
203 text.append(m_body);
204 }
205
206 return true;
207}