Skip to content

Add _pointerBitWidth platform condition #41534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems pointless if it's not possible to use other pointer sizes on platforms like Android armv7.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand. In what cases would you want other pointer sizes on armv7?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My point is that isn't arch(arm) here redundant with _pointerBitWidth(_32), ie the former always implies the latter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, but the whole point of this test is to check that all of these are properly set. If any of them wasn't, the test would fail.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so the goal is to just lump together all compile-time properties of this platform, even though some of them are redundant so nobody would ever use some combos of those checks together, got it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite. It is to ensure that if we are compiling for armv7-unknown-linux-androideabi, all of the expected conditions hold. We have these tests for all targets.

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
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
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