xref: /NextBSD/contrib/llvm/tools/lldb/source/Core/Language.cpp (revision 84d351007654069f9643c8e4b4802a7f5f08ee42)
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 "llvm/ADT/STLExtras.h"
14 #include <string.h>
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
19 #define ENUM_TO_DCSTREAM(x) case x: s->PutCString(#x); return
20 
21 struct LanguageStrings
22 {
23     const char * names[3];
24 };
25 
26 static LanguageStrings
27 g_languages[] =
28 {
29     { { "unknown" , NULL          , NULL                  } },
30     { { "c89"     , NULL          , "ISO C:1989"          } },
31     { { NULL      , NULL          , "K&R C"               } },
32     { { "ada83"   , "Ada83"       , "ISO Ada:1983"        } },
33     { { "c++"     , "cxx"         , "ISO C++:1998"        } },
34     { { "cobol74" , "Cobol74"     , "ISO Cobol:1974"      } },
35     { { "cobol"   , "Cobol85"     , "ISO Cobol:1985."     } },
36     { { "f77"     , "Fortran77"   , "ISO Fortran 77."     } },
37     { { "f90"     , "Fortran90"   , "ISO Fortran 90"      } },
38     { { "pascal"  , "Pascal83"    , "ISO Pascal:1983"     } },
39     { { "modula2" , "Modula2"     , "ISO Modula-2:1996"   } },
40     { { "java"    , NULL          , "Java"                } },
41     { { "c"       , "C99"         , "ISO C:1999"          } },
42     { { "ada"     , "Ada95"       , "ISO Ada:1995"        } },
43     { { "f95"     , "Fortran95"   , "ISO Fortran 95"      } },
44     { { "PLI"     , NULL          , "ANSI PL/I:1976"      } },
45     { { "objc"    , NULL          , "Objective-C"         } },
46     { { "objc++"  , NULL          , "Objective-C++"       } },
47     { { "upc"     , NULL          , "Unified Parallel C"  } },
48     { { "d"       , NULL          , "D"                   } },
49     { { "python"  , NULL          , "Python"              } },
50     { { "opencl"  , "OpenCL"          , "OpenCL"           } },
51     { { "go"      , "Go"              , "Go"               } },
52     { { "modula3" , "Modula3"         , "Modula 3"         } },
53     { { "haskell" , "Haskell"         , "Haskell"          } },
54     { { "c++03"   , "C_plus_plus_03"  , "ISO C++:2003"     } },
55     { { "c++11"   , "C_plus_plus_11"  , "ISO C++:2011"     } },
56     { { "ocaml"   , "OCaml"           , "OCaml"            } },
57     { { "rust"    , "Rust"            , "Rust"             } },
58     { { "c11"     , "C11"             , "ISO C:2011"       } },
59     { { "swift"   , "Swift"           , "Swift"            } },
60     { { "julia"   , "Julia"           , "Julia"            } },
61     { { "dylan"   , "Dylan"           , "Dylan"            } },
62     { { "c++14"   , "C_plus_plus_14"  , "ISO C++:2014"     } },
63     { { "f03"     , "Fortran03"       , "ISO Fortran 2003" } },
64     { { "f08"     , "Fortran08"       , "ISO Fortran 2008" } },
65     // Vendor Extensions
66     { { "mipsassem"    , "Mips_Assembler" , "Mips Assembler" } },
67     { { "renderscript" , "RenderScript"   , "RenderScript"   } }
68 };
69 
70 static const size_t g_num_languages = llvm::array_lengthof(g_languages);
71 
Language(LanguageType language)72 Language::Language(LanguageType language) :
73     m_language (language)
74 {
75 }
76 
~Language()77 Language::~Language()
78 {
79 }
80 
81 LanguageType
GetLanguage() const82 Language::GetLanguage() const
83 {
84     return m_language;
85 }
86 
87 void
Clear()88 Language::Clear ()
89 {
90     m_language = eLanguageTypeUnknown;
91 }
92 
93 void
SetLanguage(LanguageType language)94 Language::SetLanguage(LanguageType language)
95 {
96     m_language = language;
97 }
98 
99 bool
SetLanguageFromCString(const char * language_cstr)100 Language::SetLanguageFromCString(const char *language_cstr)
101 {
102     size_t i, desc_idx;
103     const char *name;
104 
105     // First check the most common name for the languages
106     for (desc_idx=lldb::eDescriptionLevelBrief; desc_idx<kNumDescriptionLevels; ++desc_idx)
107     {
108         for (i=0; i<g_num_languages; ++i)
109         {
110             name = g_languages[i].names[desc_idx];
111             if (name == NULL)
112                 continue;
113 
114             if (::strcasecmp (language_cstr, name) == 0)
115             {
116                 m_language = (LanguageType)i;
117                 return true;
118             }
119         }
120     }
121 
122     m_language = eLanguageTypeUnknown;
123     return false;
124 }
125 
126 
127 const char *
AsCString(lldb::DescriptionLevel level) const128 Language::AsCString (lldb::DescriptionLevel level) const
129 {
130     if (m_language < g_num_languages && level < kNumDescriptionLevels)
131     {
132         const char *name = g_languages[m_language].names[level];
133         if (name)
134             return name;
135         else if (level + 1 < kNumDescriptionLevels)
136             return AsCString ((lldb::DescriptionLevel)(level + 1));
137         else
138             return NULL;
139     }
140     return NULL;
141 }
142 
143 void
Dump(Stream * s) const144 Language::Dump(Stream *s) const
145 {
146     GetDescription(s, lldb::eDescriptionLevelVerbose);
147 }
148 
149 void
GetDescription(Stream * s,lldb::DescriptionLevel level) const150 Language::GetDescription (Stream *s, lldb::DescriptionLevel level) const
151 {
152     const char *lang_cstr = AsCString(level);
153 
154     if (lang_cstr)
155         s->PutCString(lang_cstr);
156     else
157         s->Printf("Language(language = 0x%4.4x)", m_language);
158 }
159 
160 
161 
162 
163 Stream&
operator <<(Stream & s,const Language & language)164 lldb_private::operator << (Stream& s, const Language& language)
165 {
166     language.Dump(&s);
167     return s;
168 }
169 
170