Skip to content
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

[WIP] [Parse] Add _pointer_bit_width platform condition #14413

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions include/swift/Basic/LangOptions.h
Expand Up @@ -41,13 +41,15 @@ namespace swift {
OS,
/// The active arch target (x86_64, i386, arm, arm64, etc.)
Arch,
/// The active endianness target (big or little)
/// The active arch target endianness (big or little)
Endianness,
/// The active arch target pointer bit width (_32 or _64)
PointerBitWidth,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Cf. __POINTER_WIDTH__ in Clang, getArchPointerBitWidth in LLVM, target_pointer_width in Rust.

/// Runtime support (_ObjC or _Native)
Runtime,
/// Conditional import of module
CanImport,
/// Target Environment (currently just 'simulator' or absent)
/// Target environment (currently just 'simulator' or absent)
TargetEnvironment,
};

Expand Down Expand Up @@ -360,7 +362,7 @@ namespace swift {
}

private:
llvm::SmallVector<std::pair<PlatformConditionKind, std::string>, 5>
llvm::SmallVector<std::pair<PlatformConditionKind, std::string>, 6>
PlatformConditionValues;
llvm::SmallVector<std::string, 2> CustomConditionalCompilationFlags;
};
Expand Down
36 changes: 36 additions & 0 deletions lib/Basic/LangOptions.cpp
Expand Up @@ -56,6 +56,11 @@ static const StringRef SupportedConditionalCompilationEndianness[] = {
"big"
};

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

static const StringRef SupportedConditionalCompilationRuntimes[] = {
"_ObjC",
"_Native",
Expand Down Expand Up @@ -101,6 +106,9 @@ checkPlatformConditionSupported(PlatformConditionKind Kind, StringRef Value,
case PlatformConditionKind::Endianness:
return contains(SupportedConditionalCompilationEndianness, Value,
suggestions);
case PlatformConditionKind::PointerBitWidth:
return contains(SupportedConditionalCompilationPointerBitWidths, Value,
suggestions);
case PlatformConditionKind::Runtime:
return contains(SupportedConditionalCompilationRuntimes, Value,
suggestions);
Expand Down Expand Up @@ -256,6 +264,34 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) {
llvm_unreachable("undefined architecture endianness");
}

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

// Set the "runtime" platform condition.
if (EnableObjCInterop)
addPlatformConditionValue(PlatformConditionKind::Runtime, "_ObjC");
Expand Down
5 changes: 4 additions & 1 deletion lib/Parse/ParseIfConfig.cpp
Expand Up @@ -40,6 +40,7 @@ Optional<PlatformConditionKind> getPlatformConditionKind(StringRef Name) {
.Case("os", PlatformConditionKind::OS)
.Case("arch", PlatformConditionKind::Arch)
.Case("_endian", PlatformConditionKind::Endianness)
.Case("_pointer_bit_width", PlatformConditionKind::PointerBitWidth)
.Case("_runtime", PlatformConditionKind::Runtime)
.Case("canImport", PlatformConditionKind::CanImport)
.Case("targetEnvironment", PlatformConditionKind::TargetEnvironment)
Expand Down Expand Up @@ -296,7 +297,7 @@ class ValidateIfConfigCondition :
return E;
}

// ( 'os' | 'arch' | '_endian' | '_runtime' | 'canImport') '(' identifier ')''
// ( 'os' | 'arch' | '_endian' | '_pointer_bit_width' | '_runtime' | 'canImport') '(' identifier ')''
auto Kind = getPlatformConditionKind(*KindName);
if (!Kind.hasValue()) {
D.diagnose(E->getLoc(), diag::unsupported_platform_condition_expression);
Expand Down Expand Up @@ -329,6 +330,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
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) && _pointer_bit_width(_64)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/arm64IOSTarget.swift
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) && _pointer_bit_width(_64)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/armAndroidTarget.swift
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) && _pointer_bit_width(_32)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/armIOSTarget.swift
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) && _pointer_bit_width(_32)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/armWatchOSTarget.swift
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) && _pointer_bit_width(_32)
class C {}
var x = C()
#endif
Expand Down
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) && _pointer_bit_width(_32)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/i386IOSTarget.swift
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) && _pointer_bit_width(_32)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/i386WatchOSTarget.swift
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) && _pointer_bit_width(_32)
class C {}
var x = C()
#endif
Expand Down
14 changes: 11 additions & 3 deletions test/Parse/ConditionalCompilation/identifierName.swift
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, _pointer_bit_width: 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 _pointer_bit_width(_32) && _pointer_bit_width(_64)
_ = _pointer_bit_width + _32 + _64
#elseif _runtime(_ObjC) && _runtime(_Native)
_ = _runtime + _ObjC + _Native
#elseif swift(>=1.0) && _compiler_version("3.*.0")
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, _pointer_bit_width = 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 _pointer_bit_width(_32) && _pointer_bit_width(_64)
_ = _pointer_bit_width + _32 + _64
#elseif _runtime(_ObjC) && _runtime(_Native)
_ = _runtime + _ObjC + _Native
#elseif swift(>=1.0) && _compiler_version("3.*.0")
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, _pointer_bit_width = 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 _pointer_bit_width(_32) && _pointer_bit_width(_64)
#elseif _runtime(_ObjC) && _runtime(_Native)
#elseif swift(>=1.0) && _compiler_version("3.*.0")
#elseif targetEnvironment(simulator)
Expand Down
@@ -1,7 +1,7 @@
// RUN: %swift -typecheck %s -verify -D FOO -D BAR -target powerpc64-unknown-linux-gnu -disable-objc-interop -D FOO -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) && _pointer_bit_width(_64)
class C {}
var x = C()
#endif
Expand Down
@@ -1,7 +1,7 @@
// RUN: %swift -typecheck %s -verify -D FOO -D BAR -target powerpc64le-unknown-linux-gnu -disable-objc-interop -D FOO -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) && _pointer_bit_width(_64)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/s390xLinuxTarget.swift
@@ -1,7 +1,7 @@
// RUN: %swift -typecheck %s -verify -D FOO -D BAR -target s390x-unknown-linux-gnu -disable-objc-interop -D FOO -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) && _pointer_bit_width(_64)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/x64AppleTVOSTarget.swift
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) && _pointer_bit_width(_64)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/x64CygwinTarget.swift
@@ -1,6 +1,6 @@
// RUN: %swift -parse %s -verify -D FOO -D BAR -target x86_64-unknown-windows-cygnus -disable-objc-interop -D FOO -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)
#if arch(x86_64) && os(Cygwin) && _runtime(_Native) && _pointer_bit_width(_64)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/x64FreeBSDTarget.swift
@@ -1,7 +1,7 @@
// RUN: %swift -typecheck %s -verify -D FOO -D BAR -target x86_64-unknown-freebsd10 -disable-objc-interop -D FOO -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) && _pointer_bit_width(_64)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/x64IOSTarget.swift
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) && _pointer_bit_width(_64)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/x64LinuxTarget.swift
@@ -1,7 +1,7 @@
// RUN: %swift -typecheck %s -verify -D FOO -D BAR -target x86_64-unknown-linux-gnu -disable-objc-interop -D FOO -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) && _pointer_bit_width(_64)
class C {}
var x = C()
#endif
Expand Down
4 changes: 2 additions & 2 deletions test/Parse/ConditionalCompilation/x64OSXTarget.swift
@@ -1,14 +1,14 @@
// RUN: %swift -typecheck %s -verify -D FOO -D BAR -target x86_64-apple-macosx10.9 -D FOO -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) && _pointer_bit_width(_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) && _pointer_bit_width(_64)
class CC {}
var xx = CC()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/x64WindowsTarget.swift
@@ -1,7 +1,7 @@
// RUN: %swift -typecheck %s -verify -D FOO -D BAR -target x86_64-unknown-windows-msvc -disable-objc-interop -D FOO -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) && _pointer_bit_width(_64)
class C {}
var x = C()
#endif
Expand Down
2 changes: 1 addition & 1 deletion test/Parse/ConditionalCompilation/x86_64PS4Target.swift
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) && _pointer_bit_width(_64)
class C {}
var x = C()
#endif
Expand Down