1ac7ddfbfSEd Maste //===-- ExpressionSourceCode.cpp --------------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste //                     The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste 
10ac7ddfbfSEd Maste #include "lldb/Expression/ExpressionSourceCode.h"
11ac7ddfbfSEd Maste 
12ac7ddfbfSEd Maste #include "lldb/Core/StreamString.h"
130127ef0fSEd Maste #include "lldb/Target/ExecutionContext.h"
140127ef0fSEd Maste #include "lldb/Target/Platform.h"
150127ef0fSEd Maste #include "lldb/Target/Target.h"
16ac7ddfbfSEd Maste 
17ac7ddfbfSEd Maste using namespace lldb_private;
18ac7ddfbfSEd Maste 
19ac7ddfbfSEd Maste const char *
20ac7ddfbfSEd Maste ExpressionSourceCode::g_expression_prefix = R"(
21ac7ddfbfSEd Maste #undef NULL
22ac7ddfbfSEd Maste #undef Nil
23ac7ddfbfSEd Maste #undef nil
24ac7ddfbfSEd Maste #undef YES
25ac7ddfbfSEd Maste #undef NO
26ac7ddfbfSEd Maste #define NULL (__null)
27ac7ddfbfSEd Maste #define Nil (__null)
28ac7ddfbfSEd Maste #define nil (__null)
29ac7ddfbfSEd Maste #define YES ((BOOL)1)
30ac7ddfbfSEd Maste #define NO ((BOOL)0)
310127ef0fSEd Maste typedef __INT8_TYPE__ int8_t;
320127ef0fSEd Maste typedef __UINT8_TYPE__ uint8_t;
330127ef0fSEd Maste typedef __INT16_TYPE__ int16_t;
340127ef0fSEd Maste typedef __UINT16_TYPE__ uint16_t;
350127ef0fSEd Maste typedef __INT32_TYPE__ int32_t;
360127ef0fSEd Maste typedef __UINT32_TYPE__ uint32_t;
370127ef0fSEd Maste typedef __INT64_TYPE__ int64_t;
380127ef0fSEd Maste typedef __UINT64_TYPE__ uint64_t;
390127ef0fSEd Maste typedef __INTPTR_TYPE__ intptr_t;
400127ef0fSEd Maste typedef __UINTPTR_TYPE__ uintptr_t;
41ac7ddfbfSEd Maste typedef __SIZE_TYPE__ size_t;
42ac7ddfbfSEd Maste typedef __PTRDIFF_TYPE__ ptrdiff_t;
43ac7ddfbfSEd Maste typedef unsigned short unichar;
44ac7ddfbfSEd Maste )";
45ac7ddfbfSEd Maste 
46ac7ddfbfSEd Maste 
470127ef0fSEd Maste bool ExpressionSourceCode::GetText (std::string &text, lldb::LanguageType wrapping_language, bool const_object, bool static_method, ExecutionContext &exe_ctx) const
48ac7ddfbfSEd Maste {
490127ef0fSEd Maste     const char *target_specific_defines = "typedef signed char BOOL;\n";
500127ef0fSEd Maste     static ConstString g_platform_ios_simulator ("PlatformiOSSimulator");
510127ef0fSEd Maste 
520127ef0fSEd Maste     if (Target *target = exe_ctx.GetTargetPtr())
530127ef0fSEd Maste     {
540127ef0fSEd Maste         if (target->GetArchitecture().GetMachine() == llvm::Triple::aarch64)
550127ef0fSEd Maste         {
560127ef0fSEd Maste             target_specific_defines = "typedef bool BOOL;\n";
570127ef0fSEd Maste         }
580127ef0fSEd Maste         if (target->GetArchitecture().GetMachine() == llvm::Triple::x86_64)
590127ef0fSEd Maste         {
600127ef0fSEd Maste             if (lldb::PlatformSP platform_sp = target->GetPlatform())
610127ef0fSEd Maste             {
620127ef0fSEd Maste                 if (platform_sp->GetPluginName() == g_platform_ios_simulator)
630127ef0fSEd Maste                 {
640127ef0fSEd Maste                     target_specific_defines = "typedef bool BOOL;\n";
650127ef0fSEd Maste                 }
660127ef0fSEd Maste             }
670127ef0fSEd Maste         }
680127ef0fSEd Maste     }
690127ef0fSEd Maste 
70ac7ddfbfSEd Maste     if (m_wrap)
71ac7ddfbfSEd Maste     {
72ac7ddfbfSEd Maste         switch (wrapping_language)
73ac7ddfbfSEd Maste         {
74ac7ddfbfSEd Maste         default:
75ac7ddfbfSEd Maste             return false;
76ac7ddfbfSEd Maste         case lldb::eLanguageTypeC:
77ac7ddfbfSEd Maste         case lldb::eLanguageTypeC_plus_plus:
78ac7ddfbfSEd Maste         case lldb::eLanguageTypeObjC:
79ac7ddfbfSEd Maste             break;
80ac7ddfbfSEd Maste         }
81ac7ddfbfSEd Maste 
82ac7ddfbfSEd Maste         StreamString wrap_stream;
83ac7ddfbfSEd Maste 
84ac7ddfbfSEd Maste         switch (wrapping_language)
85ac7ddfbfSEd Maste         {
86ac7ddfbfSEd Maste         default:
87ac7ddfbfSEd Maste             break;
88ac7ddfbfSEd Maste         case lldb::eLanguageTypeC:
89ac7ddfbfSEd Maste             wrap_stream.Printf("%s                             \n"
90ac7ddfbfSEd Maste                                "%s                             \n"
910127ef0fSEd Maste                                "%s                             \n"
92ac7ddfbfSEd Maste                                "void                           \n"
93ac7ddfbfSEd Maste                                "%s(void *$__lldb_arg)          \n"
94ac7ddfbfSEd Maste                                "{                              \n"
95ac7ddfbfSEd Maste                                "    %s;                        \n"
96ac7ddfbfSEd Maste                                "}                              \n",
97ac7ddfbfSEd Maste                                g_expression_prefix,
980127ef0fSEd Maste                                target_specific_defines,
99ac7ddfbfSEd Maste                                m_prefix.c_str(),
100ac7ddfbfSEd Maste                                m_name.c_str(),
101ac7ddfbfSEd Maste                                m_body.c_str());
102ac7ddfbfSEd Maste             break;
103ac7ddfbfSEd Maste         case lldb::eLanguageTypeC_plus_plus:
104ac7ddfbfSEd Maste             wrap_stream.Printf("%s                                     \n"
105ac7ddfbfSEd Maste                                "%s                                     \n"
1060127ef0fSEd Maste                                "%s                                     \n"
107ac7ddfbfSEd Maste                                "void                                   \n"
108ac7ddfbfSEd Maste                                "$__lldb_class::%s(void *$__lldb_arg) %s\n"
109ac7ddfbfSEd Maste                                "{                                      \n"
110ac7ddfbfSEd Maste                                "    %s;                                \n"
111ac7ddfbfSEd Maste                                "}                                      \n",
112ac7ddfbfSEd Maste                                g_expression_prefix,
1130127ef0fSEd Maste                                target_specific_defines,
114ac7ddfbfSEd Maste                                m_prefix.c_str(),
115ac7ddfbfSEd Maste                                m_name.c_str(),
116ac7ddfbfSEd Maste                                (const_object ? "const" : ""),
117ac7ddfbfSEd Maste                                m_body.c_str());
118ac7ddfbfSEd Maste             break;
119ac7ddfbfSEd Maste         case lldb::eLanguageTypeObjC:
120ac7ddfbfSEd Maste             if (static_method)
121ac7ddfbfSEd Maste             {
122ac7ddfbfSEd Maste                 wrap_stream.Printf("%s                                                      \n"
123ac7ddfbfSEd Maste                                    "%s                                                      \n"
1240127ef0fSEd Maste                                    "%s                                                      \n"
125ac7ddfbfSEd Maste                                    "@interface $__lldb_objc_class ($__lldb_category)        \n"
126ac7ddfbfSEd Maste                                    "+(void)%s:(void *)$__lldb_arg;                          \n"
127ac7ddfbfSEd Maste                                    "@end                                                    \n"
128ac7ddfbfSEd Maste                                    "@implementation $__lldb_objc_class ($__lldb_category)   \n"
129ac7ddfbfSEd Maste                                    "+(void)%s:(void *)$__lldb_arg                           \n"
130ac7ddfbfSEd Maste                                    "{                                                       \n"
131ac7ddfbfSEd Maste                                    "    %s;                                                 \n"
132ac7ddfbfSEd Maste                                    "}                                                       \n"
133ac7ddfbfSEd Maste                                    "@end                                                    \n",
134ac7ddfbfSEd Maste                                    g_expression_prefix,
1350127ef0fSEd Maste                                    target_specific_defines,
136ac7ddfbfSEd Maste                                    m_prefix.c_str(),
137ac7ddfbfSEd Maste                                    m_name.c_str(),
138ac7ddfbfSEd Maste                                    m_name.c_str(),
139ac7ddfbfSEd Maste                                    m_body.c_str());
140ac7ddfbfSEd Maste             }
141ac7ddfbfSEd Maste             else
142ac7ddfbfSEd Maste             {
143ac7ddfbfSEd Maste                 wrap_stream.Printf("%s                                                     \n"
144ac7ddfbfSEd Maste                                    "%s                                                     \n"
1450127ef0fSEd Maste                                    "%s                                                     \n"
146ac7ddfbfSEd Maste                                    "@interface $__lldb_objc_class ($__lldb_category)       \n"
147ac7ddfbfSEd Maste                                    "-(void)%s:(void *)$__lldb_arg;                         \n"
148ac7ddfbfSEd Maste                                    "@end                                                   \n"
149ac7ddfbfSEd Maste                                    "@implementation $__lldb_objc_class ($__lldb_category)  \n"
150ac7ddfbfSEd Maste                                    "-(void)%s:(void *)$__lldb_arg                          \n"
151ac7ddfbfSEd Maste                                    "{                                                      \n"
152ac7ddfbfSEd Maste                                    "    %s;                                                \n"
153ac7ddfbfSEd Maste                                    "}                                                      \n"
154ac7ddfbfSEd Maste                                    "@end                                                   \n",
155ac7ddfbfSEd Maste                                    g_expression_prefix,
1560127ef0fSEd Maste                                    target_specific_defines,
157ac7ddfbfSEd Maste                                    m_prefix.c_str(),
158ac7ddfbfSEd Maste                                    m_name.c_str(),
159ac7ddfbfSEd Maste                                    m_name.c_str(),
160ac7ddfbfSEd Maste                                    m_body.c_str());
161ac7ddfbfSEd Maste             }
162ac7ddfbfSEd Maste             break;
163ac7ddfbfSEd Maste         }
164ac7ddfbfSEd Maste 
165ac7ddfbfSEd Maste         text = wrap_stream.GetString();
166ac7ddfbfSEd Maste     }
167ac7ddfbfSEd Maste     else
168ac7ddfbfSEd Maste     {
169ac7ddfbfSEd Maste         text.append(m_body);
170ac7ddfbfSEd Maste     }
171ac7ddfbfSEd Maste 
172ac7ddfbfSEd Maste     return true;
173ac7ddfbfSEd Maste }
174