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
14using namespace lldb_private;
15
16const char *
17ExpressionSourceCode::g_expression_prefix = R"(
18#undef NULL
19#undef Nil
20#undef nil
21#undef YES
22#undef NO
23#define NULL (__null)
24#define Nil (__null)
25#define nil (__null)
26#define YES ((BOOL)1)
27#define NO ((BOOL)0)
28typedef signed char BOOL;
29typedef signed __INT8_TYPE__ int8_t;
30typedef unsigned __INT8_TYPE__ uint8_t;
31typedef signed __INT16_TYPE__ int16_t;
32typedef unsigned __INT16_TYPE__ uint16_t;
33typedef signed __INT32_TYPE__ int32_t;
34typedef unsigned __INT32_TYPE__ uint32_t;
35typedef signed __INT64_TYPE__ int64_t;
36typedef unsigned __INT64_TYPE__ uint64_t;
37typedef signed __INTPTR_TYPE__ intptr_t;
38typedef unsigned __INTPTR_TYPE__ uintptr_t;
39typedef __SIZE_TYPE__ size_t;
40typedef __PTRDIFF_TYPE__ ptrdiff_t;
41typedef unsigned short unichar;
42)";
43
44
45bool ExpressionSourceCode::GetText (std::string &text, lldb::LanguageType wrapping_language, bool const_object, bool static_method) const
46{
47    if (m_wrap)
48    {
49        switch (wrapping_language)
50        {
51        default:
52            return false;
53        case lldb::eLanguageTypeC:
54        case lldb::eLanguageTypeC_plus_plus:
55        case lldb::eLanguageTypeObjC:
56            break;
57        }
58
59        StreamString wrap_stream;
60
61        switch (wrapping_language)
62        {
63        default:
64            break;
65        case lldb::eLanguageTypeC:
66            wrap_stream.Printf("%s                             \n"
67                               "%s                             \n"
68                               "void                           \n"
69                               "%s(void *$__lldb_arg)          \n"
70                               "{                              \n"
71                               "    %s;                        \n"
72                               "}                              \n",
73                               g_expression_prefix,
74                               m_prefix.c_str(),
75                               m_name.c_str(),
76                               m_body.c_str());
77            break;
78        case lldb::eLanguageTypeC_plus_plus:
79            wrap_stream.Printf("%s                                     \n"
80                               "%s                                     \n"
81                               "void                                   \n"
82                               "$__lldb_class::%s(void *$__lldb_arg) %s\n"
83                               "{                                      \n"
84                               "    %s;                                \n"
85                               "}                                      \n",
86                               g_expression_prefix,
87                               m_prefix.c_str(),
88                               m_name.c_str(),
89                               (const_object ? "const" : ""),
90                               m_body.c_str());
91            break;
92        case lldb::eLanguageTypeObjC:
93            if (static_method)
94            {
95                wrap_stream.Printf("%s                                                      \n"
96                                   "%s                                                      \n"
97                                   "@interface $__lldb_objc_class ($__lldb_category)        \n"
98                                   "+(void)%s:(void *)$__lldb_arg;                          \n"
99                                   "@end                                                    \n"
100                                   "@implementation $__lldb_objc_class ($__lldb_category)   \n"
101                                   "+(void)%s:(void *)$__lldb_arg                           \n"
102                                   "{                                                       \n"
103                                   "    %s;                                                 \n"
104                                   "}                                                       \n"
105                                   "@end                                                    \n",
106                                   g_expression_prefix,
107                                   m_prefix.c_str(),
108                                   m_name.c_str(),
109                                   m_name.c_str(),
110                                   m_body.c_str());
111            }
112            else
113            {
114                wrap_stream.Printf("%s                                                     \n"
115                                   "%s                                                     \n"
116                                   "@interface $__lldb_objc_class ($__lldb_category)       \n"
117                                   "-(void)%s:(void *)$__lldb_arg;                         \n"
118                                   "@end                                                   \n"
119                                   "@implementation $__lldb_objc_class ($__lldb_category)  \n"
120                                   "-(void)%s:(void *)$__lldb_arg                          \n"
121                                   "{                                                      \n"
122                                   "    %s;                                                \n"
123                                   "}                                                      \n"
124                                   "@end                                                   \n",
125                                   g_expression_prefix,
126                                   m_prefix.c_str(),
127                                   m_name.c_str(),
128                                   m_name.c_str(),
129                                   m_body.c_str());
130            }
131            break;
132        }
133
134        text = wrap_stream.GetString();
135    }
136    else
137    {
138        text.append(m_body);
139    }
140
141    return true;
142}
143