-
Notifications
You must be signed in to change notification settings - Fork 122
Port authoring generator to CsWinRT 3.0 #2231
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
base: user/sergiopedri/attributes
Are you sure you want to change the base?
Port authoring generator to CsWinRT 3.0 #2231
Conversation
Introduce an assembly-level WindowsRuntimeAuthoringAssemblyExportsTypeAttribute used by the CsWinRT generator to record the Type that contains the managed GetActivationFactory method for authoring scenarios. The sealed attribute stores a Type (ExportsType), is marked Obsolete with diagnostic metadata, and EditorBrowsableState.Never to indicate it is not intended for direct use. Includes XML docs and license header.
Introduce IndentedTextWriter (src/Authoring/WinRT.SourceGenerator2/Helpers) — a portable helper for building indented source text using pooled buffers and C# interpolated string handlers. Ported from ComputeSharp and MIT-licensed, it provides indentation management (default 4 spaces), block scoping (Block IDisposable to emit braces and adjust indent), multiline-safe Write/WriteLine, conditional WriteIf/WriteLineIf overloads, and custom interpolated-string handlers (WriteInterpolatedStringHandler and WriteIfInterpolatedStringHandler). Includes ToStringAndClear and Dispose semantics to trim/clear the underlying buffer.
Add documentation and images to the cswinrt solution by including Folder/File entries for /Docs/, /Docs/cswinrtgen/, /Docs/diagnostics/, and /Docs/images/. This exposes markdown docs (usage, authoring, interop, versioning, etc.), cswinrtgen docs, diagnostics, and related images in the solution explorer so the docs are available alongside the code.
Clarify and extend the WinRT interop DLL specification: add a rule to derive namespaces for nested types from the outermost declaring type; document that types with a [WindowsRuntimeMetadata] attribute use the attribute's WinRT metadata name as the assembly identifier when not in a well-known assembly; add native-sized integer primitives (nint, nuint); and remove #WinUI2 and #Win2D from the list of compact well-known assembly identifiers. These changes improve handling of nested types, metadata-backed WinRT types, and native-sized integer support.
Add C# 'scoped' to ReadOnlySpan<char> and scoped ref interpolated-string handler parameters, and mark several Write/WriteIf/WriteLine overloads as readonly. Replace direct instance WriteLine calls with Unsafe.AsRef(in this).WriteLine() to allow invocation from readonly contexts. Rename file from IndentedInterpolatedStringHandler.cs to IndentedTextWriter.cs. These changes clarify span lifetimes and prevent span escaping in the indented text writer.
Drop the IIncrementalGenerator interface from the TypeMapAssemblyTargetGenerator class declaration. The class remains partial and retains its generation methods; this change decouples the type from the incremental generator interface in preparation for refactoring or alternative registration.
Introduce IndentedTextWriterExtensions containing WriteGeneratedAttributes to emit [GeneratedCode] (with the extension assembly version) and optional [DebuggerNonUserCode] / [ExcludeFromCodeCoverage] attributes. Supports fully-qualified or short type names and an option to exclude non-user-code attributes. Ported from ComputeSharp (MIT) for use in source generators to mark generated files.
Introduce StringExtensions with an EscapeIdentifierName extension for string used by the source generator. The method returns "_" for null/empty, preserves already valid identifiers, and otherwise replaces invalid identifier characters with '_' using Microsoft.CodeAnalysis.CSharp.SyntaxFacts. It builds the result efficiently with DefaultInterpolatedStringHandler and is intended to produce safe identifier names (e.g., for assembly or symbol names).
Introduce Write and WriteLine overloads that accept a DefaultInterpolatedStringHandler and an isMultiline flag. The new methods forward handler.Text to the existing Write/WriteLine implementations, clear the handler afterwards, and include an UnconditionalSuppressMessage on Write to suppress CA1822 since the handler call implicitly passes 'this'. This enables more efficient handling of interpolated strings when writing to the IndentedTextWriter.
Introduce IsAccessibleFromCompilationAssembly(Compilation) to ISymbolExtensions to check whether an ISymbol is accessible from a given compilation's assembly (uses compilation.IsSymbolAccessibleWithin). Also add XML doc param for the symbol parameter to improve documentation.
Introduce an incremental source generator to emit managed and native export types for authoring scenarios. Adds AuthoringExportTypesGenerator (split into core, Execute and Helpers), models for generator options and info (AuthoringExportTypesOptions, AuthoringManagedExportsInfo, AuthoringNativeExportsInfo), and generation logic that produces ManagedExports.g.cs and NativeExports.g.cs. Adds AnalyzerConfigOptionsExtension methods to read CsWinRTComponent and CsWinRTMergeReferencedActivationFactories MSBuild properties and uses them (along with PublishAot) to control emission and merging of referenced activation factories. Includes helper to discover dependent assembly export type names when merging activation factories.
Remove the unused 'using System.Linq;' directive from TypeMapAssemblyTargetGenerator.cs to clean up the code and avoid analyzer/compiler warnings. No functional changes.
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.
Pull request overview
Ports the authoring source generator to the CsWinRT 3.0 design and updates surrounding infrastructure/docs to support the new generated exports model (managed + native) and related metadata.
Changes:
- Add a new authoring exports incremental generator (
AuthoringExportTypesGenerator) with supporting models/helpers/extensions. - Introduce a new runtime attribute (
WindowsRuntimeAuthoringAssemblyExportsTypeAttribute) for locating generated managed exports types across assemblies. - Update solution structure and documentation/specs to reflect the new design (eg. nested type namespace mangling, new primitive types).
Reviewed changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| src/cswinrt.slnx | Adds docs (and images) into the solution layout. |
| src/WinRT.Runtime2/InteropServices/Attributes/WindowsRuntimeAuthoringAssemblyExportsTypeAttribute.cs | New runtime attribute for identifying the managed exports type in authoring scenarios. |
| src/Authoring/WinRT.SourceGenerator2/TypeMapAssemblyTargetGenerator.cs | No functional change in diff hunk; retains generator entrypoint. |
| src/Authoring/WinRT.SourceGenerator2/TypeMapAssemblyTargetGenerator.Execute.cs | Adjusts partial class declaration (separating implementation details). |
| src/Authoring/WinRT.SourceGenerator2/Models/AuthoringNativeExportsInfo.cs | Adds model for native export generation inputs. |
| src/Authoring/WinRT.SourceGenerator2/Models/AuthoringManagedExportsInfo.cs | Adds model for managed export generation inputs. |
| src/Authoring/WinRT.SourceGenerator2/Models/AuthoringExportTypesOptions.cs | Adds options model and emission gating logic. |
| src/Authoring/WinRT.SourceGenerator2/Helpers/IndentedTextWriter.cs | Adds codegen writer helper (ported). |
| src/Authoring/WinRT.SourceGenerator2/Extensions/StringExtensions.cs | Adds identifier escaping helper for codegen. |
| src/Authoring/WinRT.SourceGenerator2/Extensions/IndentedTextWriterExtensions.cs | Adds helpers to emit standard generated-code attributes. |
| src/Authoring/WinRT.SourceGenerator2/Extensions/ISymbolExtensions.cs | Adds accessibility helper for symbol checks used during generation. |
| src/Authoring/WinRT.SourceGenerator2/Extensions/AnalyzerConfigOptionsExtensions.cs | Adds MSBuild property accessors for authoring generator options. |
| src/Authoring/WinRT.SourceGenerator2/AuthoringExportTypesGenerator.cs | New incremental generator entrypoint wiring options + outputs. |
| src/Authoring/WinRT.SourceGenerator2/AuthoringExportTypesGenerator.Helpers.cs | Adds helper to discover referenced assemblies’ exports types via attribute metadata. |
| src/Authoring/WinRT.SourceGenerator2/AuthoringExportTypesGenerator.Execute.cs | Implements managed/native exports emission and referenced activation factory merging. |
| docs/winrt-interop-dll-spec.md | Spec updates: nested types, nint/nuint, WindowsRuntimeMetadata assembly naming behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/Authoring/WinRT.SourceGenerator2/AuthoringExportTypesGenerator.Execute.cs
Outdated
Show resolved
Hide resolved
src/Authoring/WinRT.SourceGenerator2/AuthoringExportTypesGenerator.Execute.cs
Outdated
Show resolved
Hide resolved
src/Authoring/WinRT.SourceGenerator2/AuthoringExportTypesGenerator.Execute.cs
Outdated
Show resolved
Hide resolved
src/Authoring/WinRT.SourceGenerator2/AuthoringExportTypesGenerator.Execute.cs
Outdated
Show resolved
Hide resolved
src/Authoring/WinRT.SourceGenerator2/Helpers/IndentedTextWriter.cs
Outdated
Show resolved
Hide resolved
src/Authoring/WinRT.SourceGenerator2/Extensions/StringExtensions.cs
Outdated
Show resolved
Hide resolved
src/Authoring/WinRT.SourceGenerator2/AuthoringExportTypesGenerator.Execute.cs
Show resolved
Hide resolved
src/Authoring/WinRT.SourceGenerator2/AuthoringExportTypesGenerator.Execute.cs
Outdated
Show resolved
Hide resolved
src/Authoring/WinRT.SourceGenerator2/AuthoringExportTypesGenerator.Execute.cs
Show resolved
Hide resolved
Correct several mistakes in the source generator output and helper comments: - AuthoringExportTypesGenerator.Execute.cs: return the referenced activation factory instead of just calling it; add missing semicolon for "void* activationFactory"; fix null-check and assignment to use the actual result variable (result instead of obj); remove an extraneous generated block insertion; make the extern WindowsGetStringRawBuffer signature non-partial. - IndentedTextWriter.cs: remove a stray character in the XML doc closing tag. These changes fix compilation/runtime issues in generated code and clean up a documentation typo.
Add pragma to suppress IDE0046 and refactor string-escaping logic to avoid allocating the interpolated-string handler for already-valid identifiers. When escaping, prefix a leading '_' if the first char can't start an identifier, replace invalid identifier chars with '_', and detect keywords to prefix an underscore so the result is a valid C# identifier.
Title. This also includes the changes to generated code to work with the new design.