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/Target/ExecutionContext.h"
14 #include "lldb/Target/Platform.h"
15 #include "lldb/Target/Target.h"
16 
17 using namespace lldb_private;
18 
19 const char *
20 ExpressionSourceCode::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)
31 typedef __INT8_TYPE__ int8_t;
32 typedef __UINT8_TYPE__ uint8_t;
33 typedef __INT16_TYPE__ int16_t;
34 typedef __UINT16_TYPE__ uint16_t;
35 typedef __INT32_TYPE__ int32_t;
36 typedef __UINT32_TYPE__ uint32_t;
37 typedef __INT64_TYPE__ int64_t;
38 typedef __UINT64_TYPE__ uint64_t;
39 typedef __INTPTR_TYPE__ intptr_t;
40 typedef __UINTPTR_TYPE__ uintptr_t;
41 typedef __SIZE_TYPE__ size_t;
42 typedef __PTRDIFF_TYPE__ ptrdiff_t;
43 typedef unsigned short unichar;
44 )";
45 
46 
47 bool ExpressionSourceCode::GetText (std::string &text, lldb::LanguageType wrapping_language, bool const_object, bool static_method, ExecutionContext &exe_ctx) const
48 {
49     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     {
54         if (target->GetArchitecture().GetMachine() == llvm::Triple::aarch64)
55         {
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 
70     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"
90                                "%s                             \n"
91                                "%s                             \n"
92                                "void                           \n"
93                                "%s(void *$__lldb_arg)          \n"
94                                "{                              \n"
95                                "    %s;                        \n"
96                                "}                              \n",
97                                g_expression_prefix,
98                                target_specific_defines,
99                                m_prefix.c_str(),
100                                m_name.c_str(),
101                                m_body.c_str());
102             break;
103         case lldb::eLanguageTypeC_plus_plus:
104             wrap_stream.Printf("%s                                     \n"
105                                "%s                                     \n"
106                                "%s                                     \n"
107                                "void                                   \n"
108                                "$__lldb_class::%s(void *$__lldb_arg) %s\n"
109                                "{                                      \n"
110                                "    %s;                                \n"
111                                "}                                      \n",
112                                g_expression_prefix,
113                                target_specific_defines,
114                                m_prefix.c_str(),
115                                m_name.c_str(),
116                                (const_object ? "const" : ""),
117                                m_body.c_str());
118             break;
119         case lldb::eLanguageTypeObjC:
120             if (static_method)
121             {
122                 wrap_stream.Printf("%s                                                      \n"
123                                    "%s                                                      \n"
124                                    "%s                                                      \n"
125                                    "@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",
134                                    g_expression_prefix,
135                                    target_specific_defines,
136                                    m_prefix.c_str(),
137                                    m_name.c_str(),
138                                    m_name.c_str(),
139                                    m_body.c_str());
140             }
141             else
142             {
143                 wrap_stream.Printf("%s                                                     \n"
144                                    "%s                                                     \n"
145                                    "%s                                                     \n"
146                                    "@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",
155                                    g_expression_prefix,
156                                    target_specific_defines,
157                                    m_prefix.c_str(),
158                                    m_name.c_str(),
159                                    m_name.c_str(),
160                                    m_body.c_str());
161             }
162             break;
163         }
164 
165         text = wrap_stream.GetString();
166     }
167     else
168     {
169         text.append(m_body);
170     }
171 
172     return true;
173 }
174