173844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton//===-- OptionValueEnumeration.cpp ------------------------------*- C++ -*-===//
273844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton//
373844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton//                     The LLVM Compiler Infrastructure
473844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton//
573844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton// This file is distributed under the University of Illinois Open Source
673844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton// License. See LICENSE.TXT for details.
773844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton//
873844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton//===----------------------------------------------------------------------===//
973844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton
1073844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton#include "lldb/Interpreter/OptionValueEnumeration.h"
1173844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton
1273844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton// C Includes
1373844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton// C++ Includes
1473844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton// Other libraries and framework includes
1573844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton// Project includes
169f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton#include "lldb/Core/StringList.h"
1773844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton
1873844aa19a7360b662e2be710fc3c969d6c86606Greg Claytonusing namespace lldb;
1973844aa19a7360b662e2be710fc3c969d6c86606Greg Claytonusing namespace lldb_private;
2073844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton
2173844aa19a7360b662e2be710fc3c969d6c86606Greg ClaytonOptionValueEnumeration::OptionValueEnumeration (const OptionEnumValueElement *enumerators,
2273844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                                                enum_type value) :
2373844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton    OptionValue(),
2473844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton    m_current_value (value),
2573844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton    m_default_value (value),
2673844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton    m_enumerations ()
2773844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton{
2873844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton    SetEnumerations(enumerators);
2973844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton}
3073844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton
3173844aa19a7360b662e2be710fc3c969d6c86606Greg ClaytonOptionValueEnumeration::~OptionValueEnumeration()
3273844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton{
3373844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton}
3473844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton
3573844aa19a7360b662e2be710fc3c969d6c86606Greg Claytonvoid
3673844aa19a7360b662e2be710fc3c969d6c86606Greg ClaytonOptionValueEnumeration::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)
3773844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton{
3873844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton    if (dump_mask & eDumpOptionType)
3973844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton        strm.Printf ("(%s)", GetTypeAsCString ());
4073844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton    if (dump_mask & eDumpOptionValue)
4173844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton    {
4273844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton        if (dump_mask & eDumpOptionType)
4373844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton            strm.PutCString (" = ");
4473844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton        const size_t count = m_enumerations.GetSize ();
4573844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton        for (size_t i=0; i<count; ++i)
4673844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton        {
4773844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton            if (m_enumerations.GetValueAtIndexUnchecked(i).value == m_current_value)
4873844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton            {
4973844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                strm.PutCString(m_enumerations.GetCStringAtIndex(i));
5073844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                return;
5173844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton            }
5273844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton        }
535f35a4be95aed0e5b2cb36f7d785bcbfc67284aeDaniel Malea        strm.Printf("%" PRIu64, (uint64_t)m_current_value);
5473844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton    }
5573844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton}
5673844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton
5773844aa19a7360b662e2be710fc3c969d6c86606Greg ClaytonError
5873844aa19a7360b662e2be710fc3c969d6c86606Greg ClaytonOptionValueEnumeration::SetValueFromCString (const char *value, VarSetOperationType op)
5973844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton{
6073844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton    Error error;
6173844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton    switch (op)
6273844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton    {
6373844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton        case eVarSetOperationClear:
6473844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton            Clear ();
6573844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton            break;
6673844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton
6773844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton        case eVarSetOperationReplace:
6873844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton        case eVarSetOperationAssign:
6973844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton            if (value && value[0])
7073844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton            {
7173844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                ConstString const_enumerator_name(value);
7273844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                const EnumerationMapEntry *enumerator_entry = m_enumerations.FindFirstValueForName (const_enumerator_name.GetCString());
7373844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                if (enumerator_entry)
7473844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                {
7573844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                    m_current_value = enumerator_entry->value.value;
7673844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                }
7773844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                else
7873844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                {
7973844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                    StreamString error_strm;
8073844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                    error_strm.Printf("invalid enumeration value '%s'", value);
8173844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                    const size_t count = m_enumerations.GetSize ();
8273844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                    if (count)
8373844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                    {
8473844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                        error_strm.Printf(", valid values are: %s", m_enumerations.GetCStringAtIndex(0));
8573844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                        for (size_t i=1; i<count; ++i)
8673844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                        {
8773844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                            error_strm.Printf (", %s", m_enumerations.GetCStringAtIndex(i));
8873844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                        }
8973844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                    }
9073844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                    error.SetErrorString(error_strm.GetData());
9173844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                }
9273844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton            }
9373844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton            else
9473844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton            {
9573844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton                error.SetErrorString("invalid enumeration value");
9673844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton            }
9773844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton            break;
9873844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton
9973844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton        case eVarSetOperationInsertBefore:
10073844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton        case eVarSetOperationInsertAfter:
10173844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton        case eVarSetOperationRemove:
10273844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton        case eVarSetOperationAppend:
10373844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton        case eVarSetOperationInvalid:
10473844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton            error = OptionValue::SetValueFromCString (value, op);
10573844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton            break;
10673844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton    }
10773844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton    return error;
10873844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton}
10973844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton
11073844aa19a7360b662e2be710fc3c969d6c86606Greg Claytonvoid
11173844aa19a7360b662e2be710fc3c969d6c86606Greg ClaytonOptionValueEnumeration::SetEnumerations (const OptionEnumValueElement *enumerators)
11273844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton{
11373844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton    m_enumerations.Clear();
11473844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton    if (enumerators)
11573844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton    {
11673844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton        for (size_t i=0; enumerators[i].string_value != NULL; ++i)
11773844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton        {
11873844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton            ConstString const_enumerator_name(enumerators[i].string_value);
11973844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton            EnumeratorInfo enumerator_info = { enumerators[i].value, enumerators[i].usage };
12073844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton            m_enumerations.Append (const_enumerator_name.GetCString(), enumerator_info);
12173844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton        }
12273844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton        m_enumerations.Sort();
12373844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton    }
12473844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton}
12573844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton
12673844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton
12773844aa19a7360b662e2be710fc3c969d6c86606Greg Claytonlldb::OptionValueSP
12873844aa19a7360b662e2be710fc3c969d6c86606Greg ClaytonOptionValueEnumeration::DeepCopy () const
12973844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton{
13073844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton    return OptionValueSP(new OptionValueEnumeration(*this));
13173844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton}
13273844aa19a7360b662e2be710fc3c969d6c86606Greg Clayton
1339f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Claytonsize_t
1349f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg ClaytonOptionValueEnumeration::AutoComplete (CommandInterpreter &interpreter,
1359f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton                                      const char *s,
1369f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton                                      int match_start_point,
1379f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton                                      int max_return_elements,
1389f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton                                      bool &word_complete,
1399f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton                                      StringList &matches)
1409f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton{
1419f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton    word_complete = false;
1429f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton    matches.Clear();
1439f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton
1449f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton    const uint32_t num_enumerators = m_enumerations.GetSize();
1459f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton    if (s && s[0])
1469f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton    {
1479f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton        const size_t s_len = strlen(s);
1489f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton        for (size_t i=0; i<num_enumerators; ++i)
1499f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton        {
1509f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton            const char *name = m_enumerations.GetCStringAtIndex(i);
1519f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton            if (::strncmp(s, name, s_len) == 0)
1529f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton                matches.AppendString(name);
1539f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton        }
1549f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton    }
1559f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton    else
1569f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton    {
1579f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton        // only suggest "true" or "false" by default
1589f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton        for (size_t i=0; i<num_enumerators; ++i)
1599f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton            matches.AppendString(m_enumerations.GetCStringAtIndex(i));
1609f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton    }
1619f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton    return matches.GetSize();
1629f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton}
1639f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton
1649f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton
1659f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton
1669f282851b07ff6daee37cd3b1a3fbc43ef11da29Greg Clayton
167