1 //===-- Language.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-private.h"
11 #include "lldb/Core/Language.h"
12 #include "lldb/Core/Stream.h"
13 #include <string.h>
14
15 using namespace lldb;
16 using namespace lldb_private;
17
18 #define ENUM_TO_DCSTREAM(x) case x: s->PutCString(#x); return
19
20 struct LanguageStrings
21 {
22 const char * names[3];
23 };
24
25 static LanguageStrings
26 g_languages[] =
27 {
28 { { "unknown" , NULL , NULL } },
29 { { "c89" , NULL , "ISO C:1989" } },
30 { { NULL , NULL , "K&R C" } },
31 { { "ada83" , "Ada83" , "ISO Ada:1983" } },
32 { { "c++" , "cxx" , "ISO C++:1998" } },
33 { { "cobol74" , "Cobol74" , "ISO Cobol:1974" } },
34 { { "cobol" , "Cobol85" , "ISO Cobol:1985." } },
35 { { "f77" , "Fortran77" , "ISO Fortran 77." } },
36 { { "f90" , "Fortran90" , "ISO Fortran 90" } },
37 { { "pascal" , "Pascal83" , "ISO Pascal:1983" } },
38 { { "modula2" , "Modula2" , "ISO Modula-2:1996" } },
39 { { "java" , NULL , "Java" } },
40 { { "c" , "C99" , "ISO C:1999" } },
41 { { "ada" , "Ada95" , "ISO Ada:1995" } },
42 { { "f95" , "Fortran95" , "ISO Fortran 95" } },
43 { { "PLI" , NULL , "ANSI PL/I:1976" } },
44 { { "objc" , NULL , "Objective-C" } },
45 { { "objc++" , NULL , "Objective-C++" } },
46 { { "upc" , NULL , "Unified Parallel C" } },
47 { { "d" , NULL , "D" } },
48 { { "python" , NULL , "Python" } }
49 };
50
51 static const size_t
52 g_num_languages = sizeof(g_languages)/sizeof(LanguageStrings);
53
Language(LanguageType language)54 Language::Language(LanguageType language) :
55 m_language (language)
56 {
57 }
58
~Language()59 Language::~Language()
60 {
61 }
62
63 LanguageType
GetLanguage() const64 Language::GetLanguage() const
65 {
66 return m_language;
67 }
68
69 void
Clear()70 Language::Clear ()
71 {
72 m_language = eLanguageTypeUnknown;
73 }
74
75 void
SetLanguage(LanguageType language)76 Language::SetLanguage(LanguageType language)
77 {
78 m_language = language;
79 }
80
81 bool
SetLanguageFromCString(const char * language_cstr)82 Language::SetLanguageFromCString(const char *language_cstr)
83 {
84 size_t i, desc_idx;
85 const char *name;
86
87 // First check the most common name for the languages
88 for (desc_idx=lldb::eDescriptionLevelBrief; desc_idx<kNumDescriptionLevels; ++desc_idx)
89 {
90 for (i=0; i<g_num_languages; ++i)
91 {
92 name = g_languages[i].names[desc_idx];
93 if (name == NULL)
94 continue;
95
96 if (::strcasecmp (language_cstr, name) == 0)
97 {
98 m_language = (LanguageType)i;
99 return true;
100 }
101 }
102 }
103
104 m_language = eLanguageTypeUnknown;
105 return false;
106 }
107
108
109 const char *
AsCString(lldb::DescriptionLevel level) const110 Language::AsCString (lldb::DescriptionLevel level) const
111 {
112 if (m_language < g_num_languages && level < kNumDescriptionLevels)
113 {
114 const char *name = g_languages[m_language].names[level];
115 if (name)
116 return name;
117 else if (level + 1 < kNumDescriptionLevels)
118 return AsCString ((lldb::DescriptionLevel)(level + 1));
119 else
120 return NULL;
121 }
122 return NULL;
123 }
124
125 void
Dump(Stream * s) const126 Language::Dump(Stream *s) const
127 {
128 GetDescription(s, lldb::eDescriptionLevelVerbose);
129 }
130
131 void
GetDescription(Stream * s,lldb::DescriptionLevel level) const132 Language::GetDescription (Stream *s, lldb::DescriptionLevel level) const
133 {
134 const char *lang_cstr = AsCString(level);
135
136 if (lang_cstr)
137 s->PutCString(lang_cstr);
138 else
139 s->Printf("Language(language = 0x%4.4x)", m_language);
140 }
141
142
143
144
145 Stream&
operator <<(Stream & s,const Language & language)146 lldb_private::operator << (Stream& s, const Language& language)
147 {
148 language.Dump(&s);
149 return s;
150 }
151
152