Skip to content

Commit 63e10a0

Browse files
drexinxwu
authored andcommitted
Add _pointerBitWidth platform condition (swiftlang#41534)
* Add _pointerBitWidth platform condition * Fixes after rebasing --------- Co-authored-by: Xiaodi Wu <[email protected]>
1 parent 42b8e4f commit 63e10a0

25 files changed

+66
-27
lines changed

include/swift/AST/PlatformConditionKinds.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ PLATFORM_CONDITION(Arch, "arch")
3131
/// The active endianness target (big or little)
3232
PLATFORM_CONDITION_(Endianness, "endian")
3333

34+
/// The active arch target pointer bit width (_32 or _64)
35+
PLATFORM_CONDITION_(PointerBitWidth, "pointerBitWidth")
36+
3437
/// Runtime support (_ObjC or _Native)
3538
PLATFORM_CONDITION_(Runtime, "runtime")
3639

lib/Basic/LangOptions.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ static const SupportedConditionalValue SupportedConditionalCompilationEndianness
8484
"big"
8585
};
8686

87+
static const SupportedConditionalValue SupportedConditionalCompilationPointerBitWidths[] = {
88+
"_32",
89+
"_64"
90+
};
91+
8792
static const SupportedConditionalValue SupportedConditionalCompilationRuntimes[] = {
8893
"_ObjC",
8994
"_Native",
@@ -114,6 +119,8 @@ ArrayRef<SupportedConditionalValue> getSupportedConditionalCompilationValues(con
114119
return SupportedConditionalCompilationArches;
115120
case PlatformConditionKind::Endianness:
116121
return SupportedConditionalCompilationEndianness;
122+
case PlatformConditionKind::PointerBitWidth:
123+
return SupportedConditionalCompilationPointerBitWidths;
117124
case PlatformConditionKind::Runtime:
118125
return SupportedConditionalCompilationRuntimes;
119126
case PlatformConditionKind::CanImport:
@@ -181,6 +188,7 @@ checkPlatformConditionSupported(PlatformConditionKind Kind, StringRef Value,
181188
case PlatformConditionKind::OS:
182189
case PlatformConditionKind::Arch:
183190
case PlatformConditionKind::Endianness:
191+
case PlatformConditionKind::PointerBitWidth:
184192
case PlatformConditionKind::Runtime:
185193
case PlatformConditionKind::TargetEnvironment:
186194
case PlatformConditionKind::PtrAuth:
@@ -405,6 +413,25 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
405413
break;
406414
}
407415

416+
// Set the "_pointerBitWidth" platform condition.
417+
switch (Target.getArch()) {
418+
default: llvm_unreachable("undefined architecture pointer bit width");
419+
case llvm::Triple::ArchType::arm:
420+
case llvm::Triple::ArchType::thumb:
421+
case llvm::Triple::ArchType::aarch64_32:
422+
case llvm::Triple::ArchType::x86:
423+
case llvm::Triple::ArchType::wasm32:
424+
addPlatformConditionValue(PlatformConditionKind::PointerBitWidth, "_32");
425+
break;
426+
case llvm::Triple::ArchType::aarch64:
427+
case llvm::Triple::ArchType::ppc64:
428+
case llvm::Triple::ArchType::ppc64le:
429+
case llvm::Triple::ArchType::x86_64:
430+
case llvm::Triple::ArchType::systemz:
431+
addPlatformConditionValue(PlatformConditionKind::PointerBitWidth, "_64");
432+
break;
433+
}
434+
408435
// Set the "runtime" platform condition.
409436
addPlatformConditionValue(PlatformConditionKind::Runtime,
410437
EnableObjCInterop ? "_ObjC" : "_Native");

lib/Parse/ParseIfConfig.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ class ValidateIfConfigCondition :
378378
return E;
379379
}
380380

381-
// ( 'os' | 'arch' | '_endian' | '_runtime' ) '(' identifier ')''
381+
// ( 'os' | 'arch' | '_endian' | '_pointerBitWidth' | '_runtime' ) '(' identifier ')''
382382
auto Kind = getPlatformConditionKind(*KindName);
383383
if (!Kind.has_value()) {
384384
D.diagnose(E->getLoc(), diag::unsupported_platform_condition_expression);
@@ -412,6 +412,8 @@ class ValidateIfConfigCondition :
412412
DiagName = "architecture"; break;
413413
case PlatformConditionKind::Endianness:
414414
DiagName = "endianness"; break;
415+
case PlatformConditionKind::PointerBitWidth:
416+
DiagName = "pointer bit width"; break;
415417
case PlatformConditionKind::CanImport:
416418
DiagName = "import conditional"; break;
417419
case PlatformConditionKind::TargetEnvironment:

test/Parse/ConditionalCompilation/aarch64AndroidTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(arm64) && os(Android) && _runtime(_Native) && _endian(little)
10+
#if arch(arm64) && os(Android) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_64)
1111
class C {}
1212
var x = C()
1313
#endif

test/Parse/ConditionalCompilation/arm64AppleTVOSTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(arm64) && os(tvOS) && _runtime(_ObjC) && _endian(little)
10+
#if arch(arm64) && os(tvOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_64)
1111
class C {}
1212
var x = C()
1313
#endif

test/Parse/ConditionalCompilation/arm64IOSTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(arm64) && os(iOS) && _runtime(_ObjC) && _endian(little)
10+
#if arch(arm64) && os(iOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_64)
1111
class C {}
1212
var x = C()
1313
#endif

test/Parse/ConditionalCompilation/armAndroidTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(arm) && os(Android) && _runtime(_Native) && _endian(little)
10+
#if arch(arm) && os(Android) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_32)
1111
class C {}
1212
var x = C()
1313
#endif

test/Parse/ConditionalCompilation/armIOSTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(arm) && os(iOS) && _runtime(_ObjC) && _endian(little)
10+
#if arch(arm) && os(iOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32)
1111
class C {}
1212
var x = C()
1313
#endif

test/Parse/ConditionalCompilation/armWatchOSTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(arm) && os(watchOS) && _runtime(_ObjC) && _endian(little)
10+
#if arch(arm) && os(watchOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32)
1111
class C {}
1212
var x = C()
1313
#endif

test/Parse/ConditionalCompilation/i386AppleTVOSTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(i386) && os(tvOS) && _runtime(_ObjC) && _endian(little)
10+
#if arch(i386) && os(tvOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32)
1111
class C {}
1212
var x = C()
1313
#endif

test/Parse/ConditionalCompilation/i386IOSTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(i386) && os(iOS) && _runtime(_ObjC) && _endian(little)
10+
#if arch(i386) && os(iOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32)
1111
class C {}
1212
var x = C()
1313
#endif

test/Parse/ConditionalCompilation/i386WatchOSTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(i386) && os(watchOS) && _runtime(_ObjC) && _endian(little)
10+
#if arch(i386) && os(watchOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32)
1111
class C {}
1212
var x = C()
1313
#endif

test/Parse/ConditionalCompilation/identifierName.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
func f2(
66
FOO: Int,
77
swift: Int, _compiler_version: Int,
8-
os: Int, arch: Int, _endian: Int, _runtime: Int,
8+
os: Int, arch: Int, _endian: Int, _pointerBitWidth: Int, _runtime: Int,
99
targetEnvironment: Int,
1010
arm: Int, i386: Int, macOS: Int, OSX: Int, Linux: Int,
1111
big: Int, little: Int,
12+
_32: Int, _64: Int,
1213
_ObjC: Int, _Native: Int,
1314
simulator: Int
1415
) {
@@ -21,6 +22,8 @@ func f2(
2122
_ = arch + i386 + arm
2223
#elseif _endian(big) && _endian(little)
2324
_ = _endian + big + little
25+
#elseif _pointerBitWidth(_32) && _pointerBitWidth(_64)
26+
_ = _pointerBitWidth + _32 + _64
2427
#elseif _runtime(_ObjC) && _runtime(_Native)
2528
_ = _runtime + _ObjC + _Native
2629
#elseif targetEnvironment(simulator)
@@ -34,10 +37,11 @@ func f2(
3437
func f2() {
3538
let
3639
FOO = 1, swift = 1, _compiler_version = 1,
37-
os = 1, arch = 1, _endian = 1, _runtime = 1,
40+
os = 1, arch = 1, _endian = 1, _pointerBitWidth = 1, _runtime = 1,
3841
targetEnvironment = 1,
3942
arm = 1, i386 = 1, macOS = 1, OSX = 1, Linux = 1,
4043
big = 1, little = 1,
44+
_32 = 1, _64 = 1,
4145
_ObjC = 1, _Native = 1,
4246
simulator = 1
4347

@@ -49,6 +53,8 @@ func f2() {
4953
_ = arch + i386 + arm
5054
#elseif _endian(big) && _endian(little)
5155
_ = _endian + big + little
56+
#elseif _pointerBitWidth(_32) && _pointerBitWidth(_64)
57+
_ = _pointerBitWidth + _32 + _64
5258
#elseif _runtime(_ObjC) && _runtime(_Native)
5359
_ = _runtime + _ObjC + _Native
5460
#elseif targetEnvironment(simulator)
@@ -62,17 +68,19 @@ func f2() {
6268
struct S {
6369
let
6470
FOO = 1, swift = 1, _compiler_version = 1,
65-
os = 1, arch = 1, _endian = 1, _runtime = 1,
71+
os = 1, arch = 1, _endian = 1, _pointerBitWidth = 1, _runtime = 1,
6672
targetEnvironment = 1,
6773
arm = 1, i386 = 1, macOS = 1, OSX = 1, Linux = 1,
6874
big = 1, little = 1,
75+
_32 = 1, _64 = 1,
6976
_ObjC = 1, _Native = 1,
7077
simulator = 1
7178

7279
#if FOO
7380
#elseif os(macOS) && os(OSX) && os(Linux)
7481
#elseif arch(i386) && arch(arm)
7582
#elseif _endian(big) && _endian(little)
83+
#elseif _pointerBitWidth(_32) && _pointerBitWidth(_64)
7684
#elseif _runtime(_ObjC) && _runtime(_Native)
7785
#elseif targetEnvironment(simulator)
7886
#elseif swift(>=1.0) && _compiler_version("4.*.0")

test/Parse/ConditionalCompilation/powerpc64LinuxTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %swift -typecheck %s -verify -target powerpc64-unknown-linux-gnu -disable-objc-interop -parse-stdlib
22
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target powerpc64-unknown-linux-gnu
33

4-
#if arch(powerpc64) && os(Linux) && _runtime(_Native) && _endian(big)
4+
#if arch(powerpc64) && os(Linux) && _runtime(_Native) && _endian(big) && _pointerBitWidth(_64)
55
class C {}
66
var x = C()
77
#endif

test/Parse/ConditionalCompilation/powerpc64leLinuxTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %swift -typecheck %s -verify -target powerpc64le-unknown-linux-gnu -disable-objc-interop -parse-stdlib
22
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target powerpc64le-unknown-linux-gnu
33

4-
#if arch(powerpc64le) && os(Linux) && _runtime(_Native) && _endian(little)
4+
#if arch(powerpc64le) && os(Linux) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_64)
55
class C {}
66
var x = C()
77
#endif

test/Parse/ConditionalCompilation/s390xLinuxTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %swift -typecheck %s -verify -target s390x-unknown-linux-gnu -disable-objc-interop -parse-stdlib
22
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target s390x-unknown-linux-gnu
33

4-
#if arch(s390x) && os(Linux) && _runtime(_Native) && _endian(big)
4+
#if arch(s390x) && os(Linux) && _runtime(_Native) && _endian(big) && _pointerBitWidth(_64)
55
class C {}
66
var x = C()
77
#endif

test/Parse/ConditionalCompilation/wasm32Target.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %swift -typecheck %s -verify -target wasm32-unknown-wasi -disable-objc-interop -parse-stdlib
22
// RUN: %swift-ide-test -test-input-complete -source-filename %s -target wasm32-unknown-wasi
33

4-
#if arch(wasm32) && os(WASI) && _runtime(_Native) && _endian(little)
4+
#if arch(wasm32) && os(WASI) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_32)
55
class C {}
66
var x = C()
77
#endif

test/Parse/ConditionalCompilation/x64AppleTVOSTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(x86_64) && os(tvOS) && _runtime(_ObjC) && _endian(little)
10+
#if arch(x86_64) && os(tvOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_64)
1111
class C {}
1212
var x = C()
1313
#endif

test/Parse/ConditionalCompilation/x64CygwinTarget.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// RUN: %swift -typecheck %s -verify -target x86_64-unknown-windows-cygnus -disable-objc-interop -parse-stdlib
22
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target x86_64-unknown-windows-cygnus
3-
4-
#if arch(x86_64) && os(Cygwin) && _runtime(_Native) && _endian(little)
3+
#if arch(x86_64) && os(Cygwin) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_64)
54
class C {}
65
var x = C()
76
#endif

test/Parse/ConditionalCompilation/x64FreeBSDTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %swift -typecheck %s -verify -target x86_64-unknown-freebsd10 -disable-objc-interop -parse-stdlib
22
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target x86_64-unknown-freebsd10
33

4-
#if arch(x86_64) && os(FreeBSD) && _runtime(_Native) && _endian(little)
4+
#if arch(x86_64) && os(FreeBSD) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_64)
55
class C {}
66
var x = C()
77
#endif

test/Parse/ConditionalCompilation/x64IOSTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(x86_64) && os(iOS) && _runtime(_ObjC) && _endian(little)
10+
#if arch(x86_64) && os(iOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_64)
1111
class C {}
1212
var x = C()
1313
#endif

test/Parse/ConditionalCompilation/x64LinuxTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %swift -typecheck %s -verify -target x86_64-unknown-linux-gnu -disable-objc-interop -parse-stdlib
22
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target x86_64-unknown-linux-gnu
33

4-
#if arch(x86_64) && os(Linux) && _runtime(_Native) && _endian(little)
4+
#if arch(x86_64) && os(Linux) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_64)
55
class C {}
66
var x = C()
77
#endif

test/Parse/ConditionalCompilation/x64OSXTarget.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// RUN: %swift -typecheck %s -verify -target x86_64-apple-macosx10.9 -parse-stdlib
22
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target x86_64-apple-macosx10.9
33

4-
#if arch(x86_64) && os(OSX) && _runtime(_ObjC) && _endian(little)
4+
#if arch(x86_64) && os(OSX) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_64)
55
class C {}
66
var x = C()
77
#endif
88
var y = x
99

1010

11-
#if arch(x86_64) && os(macOS) && _runtime(_ObjC) && _endian(little)
11+
#if arch(x86_64) && os(macOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_64)
1212
class CC {}
1313
var xx = CC()
1414
#endif

test/Parse/ConditionalCompilation/x64WindowsTarget.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %swift -typecheck %s -verify -target x86_64-unknown-windows-msvc -disable-objc-interop -parse-stdlib
22
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target x86_64-unknown-windows-msvc
33

4-
#if arch(x86_64) && os(Windows) && _runtime(_Native) && _endian(little)
4+
#if arch(x86_64) && os(Windows) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_64)
55
class C {}
66
var x = C()
77
#endif

test/Parse/ConditionalCompilation/x86_64PS4Target.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
let i: Int = "Hello"
88
#endif
99

10-
#if arch(x86_64) && os(PS4) && _runtime(_Native) && _endian(little)
10+
#if arch(x86_64) && os(PS4) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_64)
1111
class C {}
1212
var x = C()
1313
#endif

0 commit comments

Comments
 (0)