forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLangOptions.cpp
162 lines (142 loc) · 4.87 KB
/
LangOptions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//===--- LangOptions.cpp - Language & configuration options ---------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines the LangOptions class, which provides various
// language and configuration flags.
//
//===----------------------------------------------------------------------===//
#include "swift/Basic/LangOptions.h"
#include "swift/Basic/Range.h"
#include "swift/Config.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/raw_ostream.h"
using namespace swift;
static const StringRef SupportedConditionalCompilationOSs[] = {
"OSX",
"tvOS",
"watchOS",
"iOS",
"Linux",
"FreeBSD",
"Windows",
"Android"
};
static const StringRef SupportedConditionalCompilationArches[] = {
"arm",
"arm64",
"i386",
"x86_64",
"powerpc64",
"powerpc64le"
};
bool LangOptions::isPlatformConditionOSSupported(StringRef OSName) {
auto foundIt = std::find(std::begin(SupportedConditionalCompilationOSs),
std::end(SupportedConditionalCompilationOSs),
OSName);
return foundIt != std::end(SupportedConditionalCompilationOSs);
}
bool
LangOptions::isPlatformConditionArchSupported(StringRef ArchName) {
auto foundIt = std::find(std::begin(SupportedConditionalCompilationArches),
std::end(SupportedConditionalCompilationArches),
ArchName);
return foundIt != std::end(SupportedConditionalCompilationArches);
}
StringRef
LangOptions::getPlatformConditionValue(StringRef Name) const {
// Last one wins.
for (auto &Opt : reversed(PlatformConditionValues)) {
if (Opt.first == Name)
return Opt.second;
}
return StringRef();
}
bool LangOptions::isCustomConditionalCompilationFlagSet(StringRef Name) const {
return std::find(CustomConditionalCompilationFlags.begin(),
CustomConditionalCompilationFlags.end(), Name)
!= CustomConditionalCompilationFlags.end();
}
std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
clearAllPlatformConditionValues();
if (triple.getOS() == llvm::Triple::Darwin &&
triple.getVendor() == llvm::Triple::Apple) {
// Rewrite darwinX.Y triples to macosx10.X'.Y ones.
// It affects code generation on our platform.
llvm::SmallString<16> osxBuf;
llvm::raw_svector_ostream osx(osxBuf);
osx << llvm::Triple::getOSTypeName(llvm::Triple::MacOSX);
unsigned major, minor, micro;
triple.getMacOSXVersion(major, minor, micro);
osx << major << "." << minor;
if (micro != 0)
osx << "." << micro;
triple.setOSName(osx.str());
}
Target = std::move(triple);
bool UnsupportedOS = false;
// Set the "os" platform condition.
if (Target.isMacOSX())
addPlatformConditionValue("os", "OSX");
else if (triple.isTvOS())
addPlatformConditionValue("os", "tvOS");
else if (triple.isWatchOS())
addPlatformConditionValue("os", "watchOS");
else if (triple.isiOS())
addPlatformConditionValue("os", "iOS");
else if (triple.isAndroid())
addPlatformConditionValue("os", "Android");
else if (triple.isOSLinux())
addPlatformConditionValue("os", "Linux");
else if (triple.isOSFreeBSD())
addPlatformConditionValue("os", "FreeBSD");
else if (triple.isOSWindows())
addPlatformConditionValue("os", "Windows");
else {
UnsupportedOS = true;
}
bool UnsupportedArch = false;
// Set the "arch" platform condition.
switch (Target.getArch()) {
case llvm::Triple::ArchType::arm:
case llvm::Triple::ArchType::thumb:
addPlatformConditionValue("arch", "arm");
break;
case llvm::Triple::ArchType::aarch64:
addPlatformConditionValue("arch", "arm64");
break;
case llvm::Triple::ArchType::ppc64:
addPlatformConditionValue("arch", "powerpc64");
break;
case llvm::Triple::ArchType::ppc64le:
addPlatformConditionValue("arch", "powerpc64le");
break;
case llvm::Triple::ArchType::x86:
addPlatformConditionValue("arch", "i386");
break;
case llvm::Triple::ArchType::x86_64:
addPlatformConditionValue("arch", "x86_64");
break;
default:
UnsupportedArch = true;
}
if (UnsupportedOS || UnsupportedArch)
return { UnsupportedOS, UnsupportedArch };
// Set the "runtime" platform condition.
if (EnableObjCInterop)
addPlatformConditionValue("_runtime", "_ObjC");
else
addPlatformConditionValue("_runtime", "_Native");
// If you add anything to this list, change the default size of
// PlatformConditionValues to not require an extra allocation
// in the common case.
return { false, false };
}