Skip to content

Windows: expand the typed constants to cover broad usage#89575

Open
compnerd wants to merge 15 commits into
swiftlang:mainfrom
compnerd:constants
Open

Windows: expand the typed constants to cover broad usage#89575
compnerd wants to merge 15 commits into
swiftlang:mainfrom
compnerd:constants

Conversation

@compnerd

@compnerd compnerd commented May 30, 2026

Copy link
Copy Markdown
Member

This pull request introduces support for .apinotes type annotations for Windows SDK macros, allowing Swift to import Windows C macros with more accurate types as specified in APINotes files. The changes include updating the build system to install the new WinSDK.apinotes file, enhancing the ClangImporter to use macro type overrides from APINotes, and cleaning up redundant Swift definitions for Windows constants now handled via APINotes. Test infrastructure and cases are also updated to verify the new behavior.

Build system and VFS updates:

  • Added WinSDK.apinotes to the list of files installed and mapped in the Windows overlay and VFS configuration, ensuring it is available for the importer at build and runtime. [1] [2] [3] [4] [5]

ClangImporter enhancements for macro type overrides:

  • Modified ClangImporter::Implementation::importMacro and related functions to accept and propagate the Clang IdentifierInfo pointer, enabling lookup of APINotes type information for macros. Introduced logic to override macro types using APINotes, and refactored macro importing code to support this. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17]

  • Updated file mapping logic to redirect WinSDK.apinotes for Windows overlays, ensuring the file is found by the importer.

Swift standard library cleanup:

  • Removed hardcoded Swift definitions for many Windows SDK constants from WinSDK.swift that are now imported directly from the SDK using APINotes type information. [1] [2] [3]

Test infrastructure and coverage:

  • Added new macro type annotations to test APINotes files, corresponding macro definitions to C headers, and Swift test cases to verify correct import and type checking of macros with APINotes-specified types. [1] [2] [3]

@compnerd

Copy link
Copy Markdown
Member Author

@swift-ci please test Windows platform

@compnerd

Copy link
Copy Markdown
Member Author

CC: @al45tair @carlpeto

@compnerd

compnerd commented May 31, 2026

Copy link
Copy Markdown
Member Author

@compnerd

Copy link
Copy Markdown
Member Author

@compnerd

Copy link
Copy Markdown
Member Author

1 similar comment
@compnerd

Copy link
Copy Markdown
Member Author

@compnerd

Copy link
Copy Markdown
Member Author

@compnerd

Copy link
Copy Markdown
Member Author

1 similar comment
@compnerd

Copy link
Copy Markdown
Member Author

@compnerd compnerd requested a review from shahmishal as a code owner June 8, 2026 16:55
@compnerd

compnerd commented Jun 8, 2026

Copy link
Copy Markdown
Member Author

Please test with following PRs:
swiftlang/llvm-project#13093

@swift-ci please test Windows platform

@compnerd

compnerd commented Jun 8, 2026

Copy link
Copy Markdown
Member Author

Please test with following PRs:
swiftlang/llvm-project#13093
apple/swift-system#308

@swift-ci please test Windows platform

@compnerd

compnerd commented Jun 9, 2026

Copy link
Copy Markdown
Member Author

Please test with following PRs:
apple/swift-system#308

@swift-ci please test Windows platform

@compnerd

compnerd commented Jun 9, 2026

Copy link
Copy Markdown
Member Author

Please test with following PRs:
apple/swift-system#308
swiftlang/swift-package-manager#10190

@swift-ci please test Windows platform

@compnerd

Copy link
Copy Markdown
Member Author

Please test with following PRs:
apple/swift-system#308
swiftlang/swift-package-manager#10190
swiftlang/sourcekit-lsp#2689

@swift-ci please test Windows platform

@compnerd

Copy link
Copy Markdown
Member Author

Please test with following PRs:
swiftlang/swift-package-manager#10190

@swift-ci please test Windows platform

@compnerd

Copy link
Copy Markdown
Member Author

Please test with following PRs:
apple/swift-system#308
swiftlang/swift-package-manager#10190
swiftlang/sourcekit-lsp#2689

@swift-ci please test Windows platform

compnerd added 14 commits June 11, 2026 14:52
Macro constants do not have Clang declarations, so APINotes `Globals`
metadata cannot be applied through the normal declaration processing
path. When importing a macro constant, preserve the macro's
`IdentifierInfo` and owning Clang module, and ask Clang Sema whether
APINotes provides a replacement type for that macro name.

The owning module is needed for module APINotes sidecars such as
`WinSDK.apinotes`; location-only lookup can otherwise fall back to
directory `APINotes.apinotes` lookup and miss typed macro annotations.

Thread the macro identifier through the importer so recursive macro
aliases continue to use the originally imported macro name for APINotes
lookup while still resolving through to the target macro value.

Add APINotes coverage for typed macro constants, including an alias
whose target has a different APINotes type. This verifies both that the
alias keeps its own APINotes type and that it materializes the resolved
target value.
The macro importer records C-style cast types when importing macro
constants, but it did not apply the integer conversion to the evaluated
constant value. This matters for Windows-style definitions such as
`((DWORD)-1)`: the imported Swift declaration has an unsigned type, but
the underlying APValue still represents the signed literal `-1`, which
can produce an overflow warning when the constant is materialized as
`DWORD`.

Apply the unsigned integer cast to imported macro values before creating
the Swift constant. This truncates or extends the APSInt to the cast
type's width and marks it unsigned, matching C's modulo conversion for
unsigned integer casts.

Keep the existing cast acceptance behavior intact: builtin casts and
`id` casts remain the supported forms. The new value adjustment is
deliberately limited to non-bool unsigned integer casts so that `_Bool`
truth-value conversion and other builtin cast behavior are not changed.

Add coverage for builtin unsigned casts and unsigned typedef casts,
including a verify test that catches the previous overflow diagnostic.
Add a WinSDK.apinotes sidecar and project it through the Windows SDK VFS
overlay next to the virtual WinSDK module map. Install and resync the file
with the existing Windows module-map overlay assets so both the runtimes
overlay and the legacy stdlib VFS path expose it to ClangImporter.

Seed the APINotes file with the WinSDK macro constants that have historically
needed typed Swift overlay declarations. Remove the Swift wrappers for macro
bodies that the importer can already synthesize as constants, letting APINotes
provide the canonical WinSDK typedef spellings for those names.

Keep the Swift wrappers for constants whose macro bodies still defeat value
import, such as nested pointer casts, helper macros, and aggregate expressions.
Those wrappers remain the value-synthesis path until the macro importer can
evaluate those forms directly.

Add a Windows-only typecheck test that verifies representative APINotes-backed
constants keep their typed WinSDK spellings.
This migrates the various constants used in swift-inspect into the
APINotes and cleans up the use in the project.
This migrates the Foundation set of typed aliases to the APINotes.
This integrates the typed mapping set from swift-platform-core
(https://github.com/compnerd/swift-platform-core).
This adds additional constants used by SwiftPM.
This integrates the typed constants that swift-tools-support-core was
holding onto.
This integrates remaining constants that have been encountered by Dia.
Add additional constants that were being carried by Swift Testing.
Migrate the annotations for the constants used by swift-system.
Migrate constants used by swift-subprocess.
Bump the swift-argument-parser pin to allow building with the
canonically typed constants.
Bump the swift-system pin to allow building with the canonically typed
constants.
@compnerd

Copy link
Copy Markdown
Member Author

Please test with following PRs:
swiftlang/sourcekit-lsp#2689

@swift-ci please test Windows platform

Migrate constants used by swift-backtrace.
@compnerd

Copy link
Copy Markdown
Member Author

@swift-ci please test Windows platform

@compnerd

Copy link
Copy Markdown
Member Author

@swift-ci please smoke test

1 similar comment
@compnerd

Copy link
Copy Markdown
Member Author

@swift-ci please smoke test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants