xref: /freebsd/contrib/llvm-project/clang/lib/Basic/LangStandards.cpp (revision c57c26179033f64c2011a2d2a904ee3fa62e826a)
1 //===--- LangStandards.cpp - Language Standard Definitions ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "clang/Basic/LangStandard.h"
10 #include "clang/Config/config.h"
11 #include "llvm/ADT/StringSwitch.h"
12 #include "llvm/Support/ErrorHandling.h"
13 #include "llvm/TargetParser/Triple.h"
14 using namespace clang;
15 
16 StringRef clang::languageToString(Language L) {
17   switch (L) {
18   case Language::Unknown:
19     return "Unknown";
20   case Language::Asm:
21     return "Asm";
22   case Language::LLVM_IR:
23     return "LLVM IR";
24   case Language::C:
25     return "C";
26   case Language::CXX:
27     return "C++";
28   case Language::ObjC:
29     return "Objective-C";
30   case Language::ObjCXX:
31     return "Objective-C++";
32   case Language::OpenCL:
33     return "OpenCL";
34   case Language::OpenCLCXX:
35     return "OpenCLC++";
36   case Language::CUDA:
37     return "CUDA";
38   case Language::RenderScript:
39     return "RenderScript";
40   case Language::HIP:
41     return "HIP";
42   case Language::HLSL:
43     return "HLSL";
44   }
45 
46   llvm_unreachable("unhandled language kind");
47 }
48 
49 #define LANGSTANDARD(id, name, lang, desc, features)                           \
50   static const LangStandard Lang_##id = {name, desc, features, Language::lang};
51 #include "clang/Basic/LangStandards.def"
52 
53 const LangStandard &LangStandard::getLangStandardForKind(Kind K) {
54   switch (K) {
55   case lang_unspecified:
56     llvm::report_fatal_error("getLangStandardForKind() on unspecified kind");
57 #define LANGSTANDARD(id, name, lang, desc, features) \
58     case lang_##id: return Lang_##id;
59 #include "clang/Basic/LangStandards.def"
60   }
61   llvm_unreachable("Invalid language kind!");
62 }
63 
64 LangStandard::Kind LangStandard::getLangKind(StringRef Name) {
65   return llvm::StringSwitch<Kind>(Name)
66 #define LANGSTANDARD(id, name, lang, desc, features) .Case(name, lang_##id)
67 #define LANGSTANDARD_ALIAS(id, alias) .Case(alias, lang_##id)
68 #include "clang/Basic/LangStandards.def"
69       .Default(lang_unspecified);
70 }
71 
72 const LangStandard *LangStandard::getLangStandardForName(StringRef Name) {
73   Kind K = getLangKind(Name);
74   if (K == lang_unspecified)
75     return nullptr;
76 
77   return &getLangStandardForKind(K);
78 }
79 
80 LangStandard::Kind clang::getDefaultLanguageStandard(clang::Language Lang,
81                                                      const llvm::Triple &T) {
82   switch (Lang) {
83   case Language::Unknown:
84   case Language::LLVM_IR:
85     llvm_unreachable("Invalid input kind!");
86   case Language::OpenCL:
87     return LangStandard::lang_opencl12;
88   case Language::OpenCLCXX:
89     return LangStandard::lang_openclcpp10;
90   case Language::Asm:
91   case Language::C:
92     // The PS4 uses C99 as the default C standard.
93     if (T.isPS4())
94       return LangStandard::lang_gnu99;
95     return LangStandard::lang_gnu17;
96   case Language::ObjC:
97     return LangStandard::lang_gnu11;
98   case Language::CXX:
99   case Language::ObjCXX:
100   case Language::CUDA:
101   case Language::HIP:
102     return LangStandard::lang_gnucxx17;
103   case Language::RenderScript:
104     return LangStandard::lang_c99;
105   case Language::HLSL:
106     return LangStandard::lang_hlsl2021;
107   }
108   llvm_unreachable("unhandled Language kind!");
109 }
110