1 //===-- 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"
13 #include "lldb/Expression/ClangModulesDeclVendor.h"
14 #include "lldb/Expression/ClangPersistentVariables.h"
15 #include "lldb/Symbol/Block.h"
16 #include "lldb/Target/ExecutionContext.h"
17 #include "lldb/Target/Platform.h"
18 #include "lldb/Target/StackFrame.h"
19 #include "lldb/Target/Target.h"
20
21 using namespace lldb_private;
22
23 const char *
24 ExpressionSourceCode::g_expression_prefix = R"(
25 #ifndef NULL
26 #define NULL (__null)
27 #endif
28 #ifndef Nil
29 #define Nil (__null)
30 #endif
31 #ifndef nil
32 #define nil (__null)
33 #endif
34 #ifndef YES
35 #define YES ((BOOL)1)
36 #endif
37 #ifndef NO
38 #define NO ((BOOL)0)
39 #endif
40 typedef __INT8_TYPE__ int8_t;
41 typedef __UINT8_TYPE__ uint8_t;
42 typedef __INT16_TYPE__ int16_t;
43 typedef __UINT16_TYPE__ uint16_t;
44 typedef __INT32_TYPE__ int32_t;
45 typedef __UINT32_TYPE__ uint32_t;
46 typedef __INT64_TYPE__ int64_t;
47 typedef __UINT64_TYPE__ uint64_t;
48 typedef __INTPTR_TYPE__ intptr_t;
49 typedef __UINTPTR_TYPE__ uintptr_t;
50 typedef __SIZE_TYPE__ size_t;
51 typedef __PTRDIFF_TYPE__ ptrdiff_t;
52 typedef unsigned short unichar;
53 extern "C"
54 {
55 int printf(const char * __restrict, ...);
56 }
57 )";
58
59
GetText(std::string & text,lldb::LanguageType wrapping_language,bool const_object,bool static_method,ExecutionContext & exe_ctx) const60 bool ExpressionSourceCode::GetText (std::string &text, lldb::LanguageType wrapping_language, bool const_object, bool static_method, ExecutionContext &exe_ctx) const
61 {
62 const char *target_specific_defines = "typedef signed char BOOL;\n";
63 std::string module_macros;
64
65 if (Target *target = exe_ctx.GetTargetPtr())
66 {
67 if (target->GetArchitecture().GetMachine() == llvm::Triple::aarch64)
68 {
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 {
75 static ConstString g_platform_ios_simulator ("ios-simulator");
76 if (platform_sp->GetPluginName() == g_platform_ios_simulator)
77 {
78 target_specific_defines = "typedef bool BOOL;\n";
79 }
80 }
81 }
82
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
120 }
121
122 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
136 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
142 switch (wrapping_language)
143 {
144 default:
145 break;
146 case lldb::eLanguageTypeC:
147 wrap_stream.Printf("void \n"
148 "%s(void *$__lldb_arg) \n"
149 "{ \n"
150 " %s; \n"
151 "} \n",
152 m_name.c_str(),
153 m_body.c_str());
154 break;
155 case lldb::eLanguageTypeC_plus_plus:
156 wrap_stream.Printf("void \n"
157 "$__lldb_class::%s(void *$__lldb_arg) %s\n"
158 "{ \n"
159 " %s; \n"
160 "} \n",
161 m_name.c_str(),
162 (const_object ? "const" : ""),
163 m_body.c_str());
164 break;
165 case lldb::eLanguageTypeObjC:
166 if (static_method)
167 {
168 wrap_stream.Printf("@interface $__lldb_objc_class ($__lldb_category) \n"
169 "+(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",
177 m_name.c_str(),
178 m_name.c_str(),
179 m_body.c_str());
180 }
181 else
182 {
183 wrap_stream.Printf("@interface $__lldb_objc_class ($__lldb_category) \n"
184 "-(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",
192 m_name.c_str(),
193 m_name.c_str(),
194 m_body.c_str());
195 }
196 break;
197 }
198
199 text = wrap_stream.GetString();
200 }
201 else
202 {
203 text.append(m_body);
204 }
205
206 return true;
207 }
208