-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Add #objectFormat compilation conditional (SE-0492) #80212
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -120,6 +120,10 @@ struct CompilerBuildConfiguration: BuildConfiguration { | |
| staticBuildConfiguration.targetPointerBitWidth | ||
| } | ||
|
|
||
| func isActiveTargetObjectFormat(name: String) throws -> Bool { | ||
| try staticBuildConfiguration.isActiveTargetObjectFormat(name: name) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't really matter, but I would expect this never to throw, because it's just querying the static build configuration |
||
| } | ||
|
|
||
| var targetAtomicBitWidths: [Int] { | ||
| staticBuildConfiguration.targetAtomicBitWidths | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -141,13 +141,20 @@ static const SupportedConditionalValue SupportedConditionalCompilationHasAtomicB | |
| "_128" | ||
| }; | ||
|
|
||
| static const SupportedConditionalValue SupportedConditionalCompilationObjectFileFormats[] = { | ||
| "MachO", | ||
| "ELF", | ||
| "COFF", | ||
| "Wasm", | ||
| }; | ||
|
|
||
| static const PlatformConditionKind AllPublicPlatformConditionKinds[] = { | ||
| #define PLATFORM_CONDITION(LABEL, IDENTIFIER) PlatformConditionKind::LABEL, | ||
| #define PLATFORM_CONDITION_(LABEL, IDENTIFIER) | ||
| #include "swift/AST/PlatformConditionKinds.def" | ||
| }; | ||
|
|
||
| ArrayRef<SupportedConditionalValue> getSupportedConditionalCompilationValues(const PlatformConditionKind &Kind) { | ||
| static ArrayRef<SupportedConditionalValue> getSupportedConditionalCompilationValues(const PlatformConditionKind &Kind) { | ||
| switch (Kind) { | ||
| case PlatformConditionKind::OS: | ||
| return SupportedConditionalCompilationOSs; | ||
|
|
@@ -167,12 +174,14 @@ ArrayRef<SupportedConditionalValue> getSupportedConditionalCompilationValues(con | |
| return SupportedConditionalCompilationPtrAuthSchemes; | ||
| case PlatformConditionKind::HasAtomicBitWidth: | ||
| return SupportedConditionalCompilationHasAtomicBitWidths; | ||
| case PlatformConditionKind::ObjectFileFormat: | ||
| return SupportedConditionalCompilationObjectFileFormats; | ||
| } | ||
| llvm_unreachable("Unhandled PlatformConditionKind in switch"); | ||
| } | ||
|
|
||
| PlatformConditionKind suggestedPlatformConditionKind(PlatformConditionKind Kind, const StringRef &V, | ||
| std::vector<StringRef> &suggestedValues) { | ||
| static PlatformConditionKind suggestedPlatformConditionKind(PlatformConditionKind Kind, const StringRef &V, | ||
| std::vector<StringRef> &suggestedValues) { | ||
| std::string lower = V.lower(); | ||
| for (const PlatformConditionKind& candidateKind : AllPublicPlatformConditionKinds) { | ||
| if (candidateKind != Kind) { | ||
|
|
@@ -191,8 +200,8 @@ PlatformConditionKind suggestedPlatformConditionKind(PlatformConditionKind Kind, | |
| return Kind; | ||
| } | ||
|
|
||
| bool isMatching(PlatformConditionKind Kind, const StringRef &V, | ||
| PlatformConditionKind &suggestedKind, std::vector<StringRef> &suggestions) { | ||
| static bool isMatching(PlatformConditionKind Kind, const StringRef &V, | ||
| PlatformConditionKind &suggestedKind, std::vector<StringRef> &suggestions) { | ||
| // Compare against known values, ignoring case to avoid penalizing | ||
| // characters with incorrect case. | ||
| unsigned minDistance = std::numeric_limits<unsigned>::max(); | ||
|
|
@@ -231,6 +240,7 @@ checkPlatformConditionSupported(PlatformConditionKind Kind, StringRef Value, | |
| case PlatformConditionKind::TargetEnvironment: | ||
| case PlatformConditionKind::PtrAuth: | ||
| case PlatformConditionKind::HasAtomicBitWidth: | ||
| case PlatformConditionKind::ObjectFileFormat: | ||
| return isMatching(Kind, Value, suggestedKind, suggestedValues); | ||
| case PlatformConditionKind::CanImport: | ||
| // All importable names are valid. | ||
|
|
@@ -649,6 +659,17 @@ std::pair<bool, bool> LangOptions::setTarget(llvm::Triple triple) { | |
| addPlatformConditionValue(PlatformConditionKind::PointerBitWidth, "_64"); | ||
| } | ||
|
|
||
| // Set the "objectFormat" platform condition. | ||
| if (Target.isOSBinFormatMachO()) { | ||
| addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "MachO"); | ||
| } else if (Target.isOSBinFormatELF()) { | ||
| addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "ELF"); | ||
| } else if (Target.isOSBinFormatCOFF()) { | ||
| addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "COFF"); | ||
| } else if (Target.isOSBinFormatWasm()) { | ||
| addPlatformConditionValue(PlatformConditionKind::ObjectFileFormat, "Wasm"); | ||
| } | ||
|
||
|
|
||
| // Set the "runtime" platform condition. | ||
| addPlatformConditionValue(PlatformConditionKind::Runtime, | ||
| EnableObjCInterop ? "_ObjC" : "_Native"); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // RUN: %swift -typecheck %s -verify -target arm64-apple-none-macho -parse-stdlib | ||
| // RUN: %swift -typecheck %s -verify -target arm64-apple-none-elf -parse-stdlib | ||
| // RUN: %swift -typecheck %s -verify -target wasm32-unknown-wasi -parse-stdlib | ||
| // RUN: %swift -typecheck %s -verify -target x86_64-unknown-windows-msvc -parse-stdlib | ||
| // RUN: %swift-ide-test -test-input-complete -source-filename=%s -target arm64-apple-macos | ||
|
|
||
| #if objectFormat(MachO) || objectFormat(ELF) || objectFormat(Wasm) || objectFormat(COFF) | ||
|
||
| class C {} | ||
| var x = C() | ||
| #endif | ||
| var y = x | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for the underscore here?