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"
14using namespace clang;
15
16StringRef 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::CIR:
25 return "ClangIR";
26 case Language::C:
27 return "C";
28 case Language::CXX:
29 return "C++";
30 case Language::ObjC:
31 return "Objective-C";
32 case Language::ObjCXX:
33 return "Objective-C++";
34 case Language::OpenCL:
35 return "OpenCL";
36 case Language::OpenCLCXX:
37 return "OpenCLC++";
38 case Language::CUDA:
39 return "CUDA";
40 case Language::RenderScript:
41 return "RenderScript";
42 case Language::HIP:
43 return "HIP";
44 case Language::HLSL:
45 return "HLSL";
46 }
47
48 llvm_unreachable("unhandled language kind");
49}
50
51#define LANGSTANDARD(id, name, lang, desc, features) \
52 static const LangStandard Lang_##id = {name, desc, features, Language::lang};
53#include "clang/Basic/LangStandards.def"
54
55const LangStandard &LangStandard::getLangStandardForKind(Kind K) {
56 switch (K) {
57 case lang_unspecified:
58 llvm::report_fatal_error(reason: "getLangStandardForKind() on unspecified kind");
59#define LANGSTANDARD(id, name, lang, desc, features) \
60 case lang_##id: return Lang_##id;
61#include "clang/Basic/LangStandards.def"
62 }
63 llvm_unreachable("Invalid language kind!");
64}
65
66LangStandard::Kind LangStandard::getLangKind(StringRef Name) {
67 return llvm::StringSwitch<Kind>(Name)
68#define LANGSTANDARD(id, name, lang, desc, features) .Case(name, lang_##id)
69#define LANGSTANDARD_ALIAS(id, alias) .Case(alias, lang_##id)
70#include "clang/Basic/LangStandards.def"
71 .Default(Value: lang_unspecified);
72}
73
74LangStandard::Kind LangStandard::getHLSLLangKind(StringRef Name) {
75 return llvm::StringSwitch<LangStandard::Kind>(Name)
76 .Case(S: "2016", Value: LangStandard::lang_hlsl2016)
77 .Case(S: "2017", Value: LangStandard::lang_hlsl2017)
78 .Case(S: "2018", Value: LangStandard::lang_hlsl2018)
79 .Case(S: "2021", Value: LangStandard::lang_hlsl2021)
80 .Case(S: "202x", Value: LangStandard::lang_hlsl202x)
81 .Default(Value: LangStandard::lang_unspecified);
82}
83
84const LangStandard *LangStandard::getLangStandardForName(StringRef Name) {
85 Kind K = getLangKind(Name);
86 if (K == lang_unspecified)
87 return nullptr;
88
89 return &getLangStandardForKind(K);
90}
91
92LangStandard::Kind clang::getDefaultLanguageStandard(clang::Language Lang,
93 const llvm::Triple &T) {
94 switch (Lang) {
95 case Language::Unknown:
96 case Language::LLVM_IR:
97 case Language::CIR:
98 llvm_unreachable("Invalid input kind!");
99 case Language::OpenCL:
100 return LangStandard::lang_opencl12;
101 case Language::OpenCLCXX:
102 return LangStandard::lang_openclcpp10;
103 case Language::Asm:
104 case Language::C:
105 // The PS4 uses C99 as the default C standard.
106 if (T.isPS4())
107 return LangStandard::lang_gnu99;
108 return LangStandard::lang_gnu17;
109 case Language::ObjC:
110 return LangStandard::lang_gnu11;
111 case Language::CXX:
112 case Language::ObjCXX:
113 case Language::CUDA:
114 case Language::HIP:
115 return LangStandard::lang_gnucxx17;
116 case Language::RenderScript:
117 return LangStandard::lang_c99;
118 case Language::HLSL:
119 return LangStandard::lang_hlsl2021;
120 }
121 llvm_unreachable("unhandled Language kind!");
122}
123

source code of clang/lib/Basic/LangStandards.cpp