Skip to content

Commit

Permalink
Add _pointerBitWidth platform condition (swiftlang#41534)
Browse files Browse the repository at this point in the history
* Add _pointerBitWidth platform condition

* Fixes after rebasing

---------

Co-authored-by: Xiaodi Wu <[email protected]>
  • Loading branch information
2 people authored and etcwilde committed Apr 3, 2023
1 parent 42b8e4f commit 63e10a0
Show file tree
Hide file tree
Showing 25 changed files with 66 additions and 27 deletions.
3 changes: 3 additions & 0 deletions include/swift/AST/PlatformConditionKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ PLATFORM_CONDITION(Arch, "arch")
/// The active endianness target (big or little)
PLATFORM_CONDITION_(Endianness, "endian")

/// The active arch target pointer bit width (_32 or _64)
PLATFORM_CONDITION_(PointerBitWidth, "pointerBitWidth")

/// Runtime support (_ObjC or _Native)
PLATFORM_CONDITION_(Runtime, "runtime")

Expand Down
27 changes: 27 additions & 0 deletions lib/Basic/LangOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ static const SupportedConditionalValue SupportedConditionalCompilationEndianness
"big"
};

static const SupportedConditionalValue SupportedConditionalCompilationPointerBitWidths[] = {
"_32",
"_64"
};

static const SupportedConditionalValue SupportedConditionalCompilationRuntimes[] = {
"_ObjC",
"_Native",
Expand Down Expand Up @@ -114,6 +119,8 @@ ArrayRef<SupportedConditionalValue> getSupportedConditionalCompilationValues(con
return SupportedConditionalCompilationArches;
case PlatformConditionKind::Endianness:
return SupportedConditionalCompilationEndianness;
case PlatformConditionKind::PointerBitWidth:
return SupportedConditionalCompilationPointerBitWidths;
case PlatformConditionKind::Runtime:
return SupportedConditionalCompilationRuntimes;
case PlatformConditionKind::CanImport:
Expand Down Expand Up @@ -181,6 +188,7 @@ checkPlatformConditionSupported(PlatformConditionKind Kind, StringRef Value,
case PlatformConditionKind::OS:
case PlatformConditionKind::Arch:
case PlatformConditionKind::Endianness:
case PlatformConditionKind::PointerBitWidth:
case PlatformConditionKind::Runtime:
case PlatformConditionKind::TargetEnvironment:
case PlatformConditionKind::PtrAuth:
Expand Down Expand Up @@ -405,6 +413,25 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
break;
}

// Set the "_pointerBitWidth" platform condition.
switch (Target.getArch()) {
default: llvm_unreachable("undefined architecture pointer bit width");
case llvm::Triple::ArchType::arm:
case llvm::Triple::ArchType::thumb:
case llvm::Triple::ArchType::aarch64_32:
case llvm::Triple::ArchType::x86:
case llvm::Triple::ArchType::wasm32:
addPlatformConditionValue(PlatformConditionKind::PointerBitWidth, "_32");
break;
case llvm::Triple::ArchType::aarch64:
case llvm::Triple::ArchType::ppc64:
case llvm::Triple::ArchType::ppc64le:
case llvm::Triple::ArchType::x86_64:
case llvm::Triple::ArchType::systemz:
addPlatformConditionValue(PlatformConditionKind::PointerBitWidth, "_64");
break;
}

// Set the "runtime" platform condition.
addPlatformConditionValue(PlatformConditionKind::Runtime,
EnableObjCInterop ? "_ObjC" : "_Native");
Expand Down
4 changes: 3 additions & 1 deletion lib/Parse/ParseIfConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ class ValidateIfConfigCondition :
return E;
}

// ( 'os' | 'arch' | '_endian' | '_runtime' ) '(' identifier ')''
// ( 'os' | 'arch' | '_endian' | '_pointerBitWidth' | '_runtime' ) '(' identifier ')''
auto Kind = getPlatformConditionKind(*KindName);
if (!Kind.has_value()) {
D.diagnose(E->getLoc(), diag::unsupported_platform_condition_expression);
Expand Down Expand Up @@ -412,6 +412,8 @@ class ValidateIfConfigCondition :
DiagName = "architecture"; break;
case PlatformConditionKind::Endianness:
DiagName = "endianness"; break;
case PlatformConditionKind::PointerBitWidth:
DiagName = "pointer bit width"; break;
case PlatformConditionKind::CanImport:
DiagName = "import conditional"; break;
case PlatformConditionKind::TargetEnvironment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let i: Int = "Hello"
#endif

#if arch(arm64) && os(Android) && _runtime(_Native) && _endian(little)
#if arch(arm64) && os(Android) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_64)
class C {}
var x = C()
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let i: Int = "Hello"
#endif

#if arch(arm64) && os(tvOS) && _runtime(_ObjC) && _endian(little)
#if arch(arm64) && os(tvOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_64)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/arm64IOSTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let i: Int = "Hello"
#endif

#if arch(arm64) && os(iOS) && _runtime(_ObjC) && _endian(little)
#if arch(arm64) && os(iOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_64)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/armAndroidTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let i: Int = "Hello"
#endif

#if arch(arm) && os(Android) && _runtime(_Native) && _endian(little)
#if arch(arm) && os(Android) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_32)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/armIOSTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let i: Int = "Hello"
#endif

#if arch(arm) && os(iOS) && _runtime(_ObjC) && _endian(little)
#if arch(arm) && os(iOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/armWatchOSTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let i: Int = "Hello"
#endif

#if arch(arm) && os(watchOS) && _runtime(_ObjC) && _endian(little)
#if arch(arm) && os(watchOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32)
class C {}
var x = C()
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let i: Int = "Hello"
#endif

#if arch(i386) && os(tvOS) && _runtime(_ObjC) && _endian(little)
#if arch(i386) && os(tvOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/i386IOSTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let i: Int = "Hello"
#endif

#if arch(i386) && os(iOS) && _runtime(_ObjC) && _endian(little)
#if arch(i386) && os(iOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/i386WatchOSTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let i: Int = "Hello"
#endif

#if arch(i386) && os(watchOS) && _runtime(_ObjC) && _endian(little)
#if arch(i386) && os(watchOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_32)
class C {}
var x = C()
#endif
Expand Down
14 changes: 11 additions & 3 deletions test/Parse/ConditionalCompilation/identifierName.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
func f2(
FOO: Int,
swift: Int, _compiler_version: Int,
os: Int, arch: Int, _endian: Int, _runtime: Int,
os: Int, arch: Int, _endian: Int, _pointerBitWidth: Int, _runtime: Int,
targetEnvironment: Int,
arm: Int, i386: Int, macOS: Int, OSX: Int, Linux: Int,
big: Int, little: Int,
_32: Int, _64: Int,
_ObjC: Int, _Native: Int,
simulator: Int
) {
Expand All @@ -21,6 +22,8 @@ func f2(
_ = arch + i386 + arm
#elseif _endian(big) && _endian(little)
_ = _endian + big + little
#elseif _pointerBitWidth(_32) && _pointerBitWidth(_64)
_ = _pointerBitWidth + _32 + _64
#elseif _runtime(_ObjC) && _runtime(_Native)
_ = _runtime + _ObjC + _Native
#elseif targetEnvironment(simulator)
Expand All @@ -34,10 +37,11 @@ func f2(
func f2() {
let
FOO = 1, swift = 1, _compiler_version = 1,
os = 1, arch = 1, _endian = 1, _runtime = 1,
os = 1, arch = 1, _endian = 1, _pointerBitWidth = 1, _runtime = 1,
targetEnvironment = 1,
arm = 1, i386 = 1, macOS = 1, OSX = 1, Linux = 1,
big = 1, little = 1,
_32 = 1, _64 = 1,
_ObjC = 1, _Native = 1,
simulator = 1

Expand All @@ -49,6 +53,8 @@ func f2() {
_ = arch + i386 + arm
#elseif _endian(big) && _endian(little)
_ = _endian + big + little
#elseif _pointerBitWidth(_32) && _pointerBitWidth(_64)
_ = _pointerBitWidth + _32 + _64
#elseif _runtime(_ObjC) && _runtime(_Native)
_ = _runtime + _ObjC + _Native
#elseif targetEnvironment(simulator)
Expand All @@ -62,17 +68,19 @@ func f2() {
struct S {
let
FOO = 1, swift = 1, _compiler_version = 1,
os = 1, arch = 1, _endian = 1, _runtime = 1,
os = 1, arch = 1, _endian = 1, _pointerBitWidth = 1, _runtime = 1,
targetEnvironment = 1,
arm = 1, i386 = 1, macOS = 1, OSX = 1, Linux = 1,
big = 1, little = 1,
_32 = 1, _64 = 1,
_ObjC = 1, _Native = 1,
simulator = 1

#if FOO
#elseif os(macOS) && os(OSX) && os(Linux)
#elseif arch(i386) && arch(arm)
#elseif _endian(big) && _endian(little)
#elseif _pointerBitWidth(_32) && _pointerBitWidth(_64)
#elseif _runtime(_ObjC) && _runtime(_Native)
#elseif targetEnvironment(simulator)
#elseif swift(>=1.0) && _compiler_version("4.*.0")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %swift -typecheck %s -verify -target powerpc64-unknown-linux-gnu -disable-objc-interop -parse-stdlib
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target powerpc64-unknown-linux-gnu

#if arch(powerpc64) && os(Linux) && _runtime(_Native) && _endian(big)
#if arch(powerpc64) && os(Linux) && _runtime(_Native) && _endian(big) && _pointerBitWidth(_64)
class C {}
var x = C()
#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %swift -typecheck %s -verify -target powerpc64le-unknown-linux-gnu -disable-objc-interop -parse-stdlib
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target powerpc64le-unknown-linux-gnu

#if arch(powerpc64le) && os(Linux) && _runtime(_Native) && _endian(little)
#if arch(powerpc64le) && os(Linux) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_64)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/s390xLinuxTarget.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %swift -typecheck %s -verify -target s390x-unknown-linux-gnu -disable-objc-interop -parse-stdlib
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target s390x-unknown-linux-gnu

#if arch(s390x) && os(Linux) && _runtime(_Native) && _endian(big)
#if arch(s390x) && os(Linux) && _runtime(_Native) && _endian(big) && _pointerBitWidth(_64)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/wasm32Target.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %swift -typecheck %s -verify -target wasm32-unknown-wasi -disable-objc-interop -parse-stdlib
// RUN: %swift-ide-test -test-input-complete -source-filename %s -target wasm32-unknown-wasi

#if arch(wasm32) && os(WASI) && _runtime(_Native) && _endian(little)
#if arch(wasm32) && os(WASI) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_32)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/x64AppleTVOSTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let i: Int = "Hello"
#endif

#if arch(x86_64) && os(tvOS) && _runtime(_ObjC) && _endian(little)
#if arch(x86_64) && os(tvOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_64)
class C {}
var x = C()
#endif
Expand Down
3 changes: 1 addition & 2 deletions test/Parse/ConditionalCompilation/x64CygwinTarget.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// RUN: %swift -typecheck %s -verify -target x86_64-unknown-windows-cygnus -disable-objc-interop -parse-stdlib
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target x86_64-unknown-windows-cygnus

#if arch(x86_64) && os(Cygwin) && _runtime(_Native) && _endian(little)
#if arch(x86_64) && os(Cygwin) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_64)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/x64FreeBSDTarget.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %swift -typecheck %s -verify -target x86_64-unknown-freebsd10 -disable-objc-interop -parse-stdlib
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target x86_64-unknown-freebsd10

#if arch(x86_64) && os(FreeBSD) && _runtime(_Native) && _endian(little)
#if arch(x86_64) && os(FreeBSD) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_64)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/x64IOSTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let i: Int = "Hello"
#endif

#if arch(x86_64) && os(iOS) && _runtime(_ObjC) && _endian(little)
#if arch(x86_64) && os(iOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_64)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/x64LinuxTarget.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %swift -typecheck %s -verify -target x86_64-unknown-linux-gnu -disable-objc-interop -parse-stdlib
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target x86_64-unknown-linux-gnu

#if arch(x86_64) && os(Linux) && _runtime(_Native) && _endian(little)
#if arch(x86_64) && os(Linux) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_64)
class C {}
var x = C()
#endif
Expand Down
4 changes: 2 additions & 2 deletions test/Parse/ConditionalCompilation/x64OSXTarget.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// RUN: %swift -typecheck %s -verify -target x86_64-apple-macosx10.9 -parse-stdlib
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target x86_64-apple-macosx10.9

#if arch(x86_64) && os(OSX) && _runtime(_ObjC) && _endian(little)
#if arch(x86_64) && os(OSX) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_64)
class C {}
var x = C()
#endif
var y = x


#if arch(x86_64) && os(macOS) && _runtime(_ObjC) && _endian(little)
#if arch(x86_64) && os(macOS) && _runtime(_ObjC) && _endian(little) && _pointerBitWidth(_64)
class CC {}
var xx = CC()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/x64WindowsTarget.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %swift -typecheck %s -verify -target x86_64-unknown-windows-msvc -disable-objc-interop -parse-stdlib
// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target x86_64-unknown-windows-msvc

#if arch(x86_64) && os(Windows) && _runtime(_Native) && _endian(little)
#if arch(x86_64) && os(Windows) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_64)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/x86_64PS4Target.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let i: Int = "Hello"
#endif

#if arch(x86_64) && os(PS4) && _runtime(_Native) && _endian(little)
#if arch(x86_64) && os(PS4) && _runtime(_Native) && _endian(little) && _pointerBitWidth(_64)
class C {}
var x = C()
#endif
Expand Down

0 comments on commit 63e10a0

Please sign in to comment.