1//===-- Property.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/lldb-python.h"
11
12#include "lldb/Interpreter/Property.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
18#include "lldb/Core/UserSettingsController.h"
19#include "lldb/Interpreter/Args.h"
20#include "lldb/Interpreter/CommandInterpreter.h"
21#include "lldb/Interpreter/OptionValues.h"
22
23using namespace lldb;
24using namespace lldb_private;
25
26Property::Property (const PropertyDefinition &definition) :
27    m_name (definition.name),
28    m_description (definition.description),
29    m_value_sp (),
30    m_is_global (definition.global)
31{
32    switch (definition.type)
33    {
34        case OptionValue::eTypeInvalid:
35        case OptionValue::eTypeProperties:
36            break;
37        case OptionValue::eTypeArch:
38            // "definition.default_uint_value" is not used
39            // "definition.default_cstr_value" as a string value that represents the default string value for the architecture/triple
40            m_value_sp.reset (new OptionValueArch(definition.default_cstr_value));
41            break;
42
43        case OptionValue::eTypeArgs:
44            // "definition.default_uint_value" is always a OptionValue::Type
45            m_value_sp.reset (new OptionValueArgs());
46            break;
47
48        case OptionValue::eTypeArray:
49            // "definition.default_uint_value" is always a OptionValue::Type
50            m_value_sp.reset (new OptionValueArray(OptionValue::ConvertTypeToMask((OptionValue::Type)definition.default_uint_value)));
51            break;
52
53        case OptionValue::eTypeBoolean:
54            // "definition.default_uint_value" is the default boolean value if
55            // "definition.default_cstr_value" is NULL, otherwise interpret
56            // "definition.default_cstr_value" as a string value that represents the default
57            // value.
58            if (definition.default_cstr_value)
59                m_value_sp.reset (new OptionValueBoolean(Args::StringToBoolean (definition.default_cstr_value, false, NULL)));
60            else
61                m_value_sp.reset (new OptionValueBoolean(definition.default_uint_value != 0));
62            break;
63
64        case OptionValue::eTypeDictionary:
65            // "definition.default_uint_value" is always a OptionValue::Type
66            m_value_sp.reset (new OptionValueDictionary(OptionValue::ConvertTypeToMask((OptionValue::Type)definition.default_uint_value)));
67            break;
68
69        case OptionValue::eTypeEnum:
70            // "definition.default_uint_value" is the default enumeration value if
71            // "definition.default_cstr_value" is NULL, otherwise interpret
72            // "definition.default_cstr_value" as a string value that represents the default
73            // value.
74        {
75            OptionValueEnumeration *enum_value = new OptionValueEnumeration(definition.enum_values, definition.default_uint_value);
76            m_value_sp.reset (enum_value);
77            if (definition.default_cstr_value)
78            {
79                if (enum_value->SetValueFromCString(definition.default_cstr_value).Success())
80                {
81                    enum_value->SetDefaultValue(enum_value->GetCurrentValue());
82                    // Call Clear() since we don't want the value to appear as
83                    // having been set since we called SetValueFromCString() above.
84                    // Clear will set the current value to the default and clear
85                    // the boolean that says that the value has been set.
86                    enum_value->Clear();
87                }
88            }
89        }
90            break;
91
92        case OptionValue::eTypeFileSpec:
93            // "definition.default_uint_value" represents if the "definition.default_cstr_value" should
94            // be resolved or not
95            m_value_sp.reset (new OptionValueFileSpec(FileSpec(definition.default_cstr_value, definition.default_uint_value != 0)));
96            break;
97
98        case OptionValue::eTypeFileSpecList:
99            // "definition.default_uint_value" is not used for a OptionValue::eTypeFileSpecList
100            m_value_sp.reset (new OptionValueFileSpecList());
101            break;
102
103        case OptionValue::eTypeFormat:
104            // "definition.default_uint_value" is the default format enumeration value if
105            // "definition.default_cstr_value" is NULL, otherwise interpret
106            // "definition.default_cstr_value" as a string value that represents the default
107            // value.
108        {
109            Format new_format = eFormatInvalid;
110            if (definition.default_cstr_value)
111                Args::StringToFormat (definition.default_cstr_value, new_format, NULL);
112            else
113                new_format = (Format)definition.default_uint_value;
114            m_value_sp.reset (new OptionValueFormat(new_format));
115        }
116            break;
117
118        case OptionValue::eTypePathMap:
119            // "definition.default_uint_value" tells us if notifications should occur for
120            // path mappings
121            m_value_sp.reset (new OptionValuePathMappings(definition.default_uint_value != 0));
122            break;
123
124        case OptionValue::eTypeRegex:
125            // "definition.default_uint_value" is used to the regular expression flags
126            // "definition.default_cstr_value" the default regular expression value
127            // value.
128            m_value_sp.reset (new OptionValueRegex(definition.default_cstr_value, definition.default_uint_value));
129            break;
130
131        case OptionValue::eTypeSInt64:
132            // "definition.default_uint_value" is the default integer value if
133            // "definition.default_cstr_value" is NULL, otherwise interpret
134            // "definition.default_cstr_value" as a string value that represents the default
135            // value.
136            m_value_sp.reset (new OptionValueSInt64(definition.default_cstr_value ? Args::StringToSInt64 (definition.default_cstr_value) : definition.default_uint_value));
137            break;
138
139        case OptionValue::eTypeUInt64:
140            // "definition.default_uint_value" is the default unsigned integer value if
141            // "definition.default_cstr_value" is NULL, otherwise interpret
142            // "definition.default_cstr_value" as a string value that represents the default
143            // value.
144            m_value_sp.reset (new OptionValueUInt64(definition.default_cstr_value ? Args::StringToUInt64 (definition.default_cstr_value) : definition.default_uint_value));
145            break;
146
147        case OptionValue::eTypeUUID:
148            // "definition.default_uint_value" is not used for a OptionValue::eTypeUUID
149            // "definition.default_cstr_value" can contain a default UUID value
150        {
151            UUID uuid;
152            if (definition.default_cstr_value)
153                uuid.SetFromCString (definition.default_cstr_value);
154            m_value_sp.reset (new OptionValueUUID(uuid));
155        }
156            break;
157
158        case OptionValue::eTypeString:
159            // "definition.default_uint_value" can contain the string option flags OR'ed together
160            // "definition.default_cstr_value" can contain a default string value
161            {
162                OptionValueString *string_value = new OptionValueString(definition.default_cstr_value);
163                if (definition.default_uint_value != 0)
164                    string_value->GetOptions().Reset(definition.default_uint_value);
165                m_value_sp.reset (string_value);
166            }
167            break;
168    }
169}
170
171Property::Property (const ConstString &name,
172                    const ConstString &desc,
173                    bool is_global,
174                    const lldb::OptionValueSP &value_sp) :
175    m_name (name),
176    m_description (desc),
177    m_value_sp (value_sp),
178    m_is_global (is_global)
179{
180}
181
182bool
183Property::DumpQualifiedName(Stream &strm) const
184{
185    if (m_name)
186    {
187        if (m_value_sp->DumpQualifiedName(strm))
188            strm.PutChar('.');
189        strm << m_name;
190        return true;
191    }
192    return false;
193}
194
195
196void
197Property::Dump (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask) const
198{
199    if (m_value_sp)
200    {
201        const bool dump_desc = dump_mask & OptionValue::eDumpOptionDescription;
202        const bool transparent = m_value_sp->ValueIsTransparent ();
203        if (dump_desc || !transparent)
204        {
205            if ((dump_mask & OptionValue::eDumpOptionName) && m_name)
206            {
207                DumpQualifiedName(strm);
208                if (dump_mask & ~OptionValue::eDumpOptionName)
209                    strm.PutChar(' ');
210            }
211        }
212        if (dump_desc)
213        {
214            const char *desc = GetDescription();
215            if (desc)
216                strm.Printf ("-- %s", desc);
217
218            if (transparent && (dump_mask == (OptionValue::eDumpOptionName | OptionValue::eDumpOptionDescription)))
219                strm.EOL();
220        }
221        m_value_sp->DumpValue(exe_ctx, strm, dump_mask);
222    }
223}
224
225
226void
227Property::DumpDescription (CommandInterpreter &interpreter,
228                           Stream &strm,
229                           uint32_t output_width,
230                           bool display_qualified_name) const
231{
232    if (m_value_sp)
233    {
234        const char *desc = GetDescription();
235
236        if (desc)
237        {
238            StreamString qualified_name;
239            const OptionValueProperties *sub_properties = m_value_sp->GetAsProperties();
240            if (sub_properties)
241            {
242                strm.EOL();
243
244                if (m_value_sp->DumpQualifiedName(qualified_name))
245                    strm.Printf("'%s' variables:\n\n", qualified_name.GetString().c_str());
246                sub_properties->DumpAllDescriptions(interpreter, strm);
247            }
248            else
249            {
250                if (desc)
251                {
252                    if (display_qualified_name)
253                    {
254                        StreamString qualified_name;
255                        DumpQualifiedName(qualified_name);
256                        interpreter.OutputFormattedHelpText (strm,
257                                                             qualified_name.GetString().c_str(),
258                                                             "--",
259                                                             desc,
260                                                             output_width);
261                    }
262                    else
263                    {
264                        interpreter.OutputFormattedHelpText (strm,
265                                                             m_name.GetCString(),
266                                                             "--",
267                                                             desc,
268                                                             output_width);
269                    }
270                }
271            }
272        }
273    }
274}
275
276