Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

namespace Microsoft.TypeSpec.Generator.Expressions
Expand All @@ -14,26 +15,43 @@ public sealed record LiteralExpression(object? Literal) : ValueExpression
{
internal override void Write(CodeWriter writer)
{
writer.AppendRaw(Literal switch
writer.AppendRaw(Format(Literal) ?? throw new NotImplementedException());
}

/// <summary>
/// Creates a <see cref="LiteralExpression"/> when <paramref name="value"/> maps to a renderable literal.
/// </summary>
internal static bool TryCreate(object? value, [NotNullWhen(true)] out LiteralExpression? literal)
{
if (Format(value) is null)
{
null => "null",
string s => SyntaxFactory.Literal(s).ToString(),
int i => SyntaxFactory.Literal(i).ToString(),
uint ui => SyntaxFactory.Literal(ui).ToString(),
long l => SyntaxFactory.Literal(l).ToString(),
ulong ul => SyntaxFactory.Literal(ul).ToString(),
byte b => SyntaxFactory.Literal((int)b).ToString(),
sbyte sb => SyntaxFactory.Literal((int)sb).ToString(),
short s => SyntaxFactory.Literal((int)s).ToString(),
ushort us => SyntaxFactory.Literal((uint)us).ToString(),
decimal d => SyntaxFactory.Literal(d).ToString(),
double d => SyntaxFactory.Literal(d).ToString(),
float f => SyntaxFactory.Literal(f).ToString(),
char c => SyntaxFactory.Literal(c).ToString(),
bool b => b ? "true" : "false",
BinaryData bd => bd.ToArray().Length == 0 ? "new byte[] { }" : SyntaxFactory.Literal(bd.ToString()).ToString(),
_ => throw new NotImplementedException()
});
literal = null;
return false;
}

literal = new LiteralExpression(value);
return true;
}

private static string? Format(object? literal) => literal switch
{
null => "null",
string s => SyntaxFactory.Literal(s).ToString(),
int i => SyntaxFactory.Literal(i).ToString(),
uint ui => SyntaxFactory.Literal(ui).ToString(),
long l => SyntaxFactory.Literal(l).ToString(),
ulong ul => SyntaxFactory.Literal(ul).ToString(),
byte b => SyntaxFactory.Literal((int)b).ToString(),
sbyte sb => SyntaxFactory.Literal((int)sb).ToString(),
short s => SyntaxFactory.Literal((int)s).ToString(),
ushort us => SyntaxFactory.Literal((uint)us).ToString(),
decimal d => SyntaxFactory.Literal(d).ToString(),
double d => SyntaxFactory.Literal(d).ToString(),
float f => SyntaxFactory.Literal(f).ToString(),
char c => SyntaxFactory.Literal(c).ToString(),
bool b => b ? "true" : "false",
BinaryData bd => bd.ToArray().Length == 0 ? "new byte[] { }" : SyntaxFactory.Literal(bd.ToString()).ToString(),
_ => null
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ protected internal virtual FinallyExpression VisitFinallyExpression(FinallyExpre
/// </summary>
/// <param name="property">The original <see cref="PropertyProvider"/>.</param>
/// <returns>Null if it should be removed otherwise the modified version of the <see cref="PropertyProvider"/>.</returns>
protected virtual PropertyProvider? VisitProperty(PropertyProvider property)
protected internal virtual PropertyProvider? VisitProperty(PropertyProvider property)
Comment thread
jorgerangel-msft marked this conversation as resolved.
{
return property;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ .. _assemblyMetadataReferences.Value.Concat(CodeModelGenerator.Instance.Addition
project = project
.AddMetadataReferences(metadataReferences)
.WithCompilationOptions(new CSharpCompilationOptions(
OutputKind.DynamicallyLinkedLibrary, metadataReferenceResolver: _metadataReferenceResolver.Value, nullableContextOptions: NullableContextOptions.Disable));
OutputKind.DynamicallyLinkedLibrary, metadataReferenceResolver: _metadataReferenceResolver.Value, nullableContextOptions: NullableContextOptions.Disable)
.WithMetadataImportOptions(MetadataImportOptions.All));
return await project.GetCompilationAsync();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected override string BuildNamespace() => string.IsNullOrEmpty(_inputType?.N
protected static string RemoveUnderscores(string name) => name.Replace("_", string.Empty);

private HashSet<string>? _customMemberNames;
private HashSet<string> CustomMemberNames => _customMemberNames ??= new HashSet<string>(
private protected HashSet<string> CustomMemberNames => _customMemberNames ??= new HashSet<string>(
GetCustomMemberNames(),
StringComparer.OrdinalIgnoreCase);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using Microsoft.TypeSpec.Generator.EmitterRpc;
using Microsoft.TypeSpec.Generator.Expressions;
using Microsoft.TypeSpec.Generator.Input;
using Microsoft.TypeSpec.Generator.Input.Extensions;
Expand Down Expand Up @@ -274,5 +276,81 @@ protected override TypeProvider[] BuildSerializationProviders()
return CodeModelGenerator.Instance.TypeFactory.CreateSerializations(_inputType, this).ToArray();
}
protected override bool GetIsEnum() => true;

protected internal override IReadOnlyList<EnumTypeMember>? BuildEnumValuesForBackCompatibility(IReadOnlyList<EnumTypeMember> currentValues)
{
var lastContractProperties = LastContractView?.Properties;
if (lastContractProperties == null || lastContractProperties.Count == 0)
{
return null;
}

var currentNames = new HashSet<string>(currentValues.Select(v => v.Name), StringComparer.OrdinalIgnoreCase);
var lastContractValueFields = new Dictionary<string, FieldProvider>(StringComparer.Ordinal);
foreach (var field in LastContractView!.Fields)
{
lastContractValueFields.TryAdd(field.Name, field);
}

List<EnumTypeMember>? restoredMembers = null;
foreach (var property in lastContractProperties)
{
// Members that still exist in the current spec or are provided by custom code are left untouched.
if (currentNames.Contains(property.Name) || CustomMemberNames.Contains(property.Name))
{
continue;
}

// Honor an intentional removal recorded in the ApiCompat baseline.
if (CodeModelGenerator.Instance.SourceInputModel?.ApiCompatBaseline.IsMemberSuppressed(Type.FullyQualifiedName, property.Name, 0) == true)
{
CodeModelGenerator.Instance.Emitter.Debug(
$"Skipping re-add of enum member '{Name}.{property.Name}'; the removal is accepted in the ApiCompat baseline.",
BackCompatibilityChangeCategory.BaselineAcceptedRemovalSkipped);
continue;
}

if (TryResurrectRemovedMember(property, lastContractValueFields, out var resurrectedMember))
{
(restoredMembers ??= []).Add(resurrectedMember);
CodeModelGenerator.Instance.Emitter.Debug(
$"Re-added enum member '{property.Name}' to enum '{Name}' to preserve a member from the last contract.",
BackCompatibilityChangeCategory.EnumMemberAddedFromLastContract);
}
}

if (restoredMembers == null)
{
return null;
}

// Preserve the current spec order and append the restored members at the end.
return [.. currentValues, .. restoredMembers];
}

private bool TryResurrectRemovedMember(
PropertyProvider lastContractProperty,
IReadOnlyDictionary<string, FieldProvider> lastContractValueFields,
[NotNullWhen(true)] out EnumTypeMember? member)
{
member = null;

// The wire value lives in the private const `<Member>Value` field.
var valueFieldName = $"{lastContractProperty.Name}Value";
Comment thread
jorgerangel-msft marked this conversation as resolved.
if (!lastContractValueFields.TryGetValue(valueFieldName, out var valueField)
|| valueField.InitializationValue is not LiteralExpression { Literal: { } literalValue })
{
return false;
}

var field = new FieldProvider(
FieldModifiers.Private | FieldModifiers.Const,
EnumUnderlyingType,
valueFieldName,
this,
initializationValue: Literal(literalValue));
member = new EnumTypeMember(lastContractProperty.Name, field, literalValue);
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -247,18 +247,13 @@ protected internal override PropertyProvider[] BuildProperties()
return null;
}

private static ValueExpression? GetFieldInitializer(IFieldSymbol fieldSymbol)
private static LiteralExpression? GetFieldInitializer(IFieldSymbol fieldSymbol)
{
if (fieldSymbol.ContainingType?.TypeKind == TypeKind.Enum)
{
if (fieldSymbol.HasConstantValue && fieldSymbol.ConstantValue != null)
{
return Literal(fieldSymbol.ConstantValue);
}
return null;
}

return null;
return fieldSymbol.HasConstantValue &&
fieldSymbol.ConstantValue != null &&
LiteralExpression.TryCreate(fieldSymbol.ConstantValue, out var initializer)
? initializer
: null;
}

private static string? GetOriginalName(ISymbol symbol)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,8 @@ internal void ProcessTypeForBackCompatibility()

IReadOnlyList<EnumTypeMember>? updatedEnumValues = null;
IEnumerable<FieldProvider>? newFields = null;
if (this is EnumProvider)
IEnumerable<PropertyProvider>? newProperties = null;
if (this is EnumProvider enumProvider)
{
var hasFields = LastContractView?.Fields != null && LastContractView.Fields.Count > 0;
if (hasFields)
Expand All @@ -848,21 +849,48 @@ internal void ProcessTypeForBackCompatibility()
updatedEnumValues = newEnumValues;
}

newFields = filteredFields;
// Sync the enum values before rebuilding the member collections from them.
if (updatedEnumValues != null)
{
_enumValues = updatedEnumValues;
}

if (enumProvider.IsExtensible)
{
// Extensible enums carry an extra backing `_value` field and surface members
// as properties, so rebuild both from the updated members. Reuse the
// already-visited field and property instances for members that still exist so
// any visitor mutations are preserved and only the restored members are
// (re)visited below.
var existingFields = new Dictionary<string, FieldProvider>(StringComparer.Ordinal);
foreach (var field in Fields)
{
existingFields.TryAdd(field.Name, field);
}
newFields = ApplyCustomizationFilter(
BuildFields().Select(f => existingFields.TryGetValue(f.Name, out var existing) ? existing : f));

var existingProperties = new Dictionary<string, PropertyProvider>(StringComparer.Ordinal);
foreach (var property in Properties)
{
existingProperties.TryAdd(property.Name, property);
}
newProperties = ApplyCustomizationFilter(
Comment thread
jorgerangel-msft marked this conversation as resolved.
BuildProperties().Select(p => existingProperties.TryGetValue(p.Name, out var existing) ? existing : p));
}
else
{
newFields = filteredFields;
}
}
}
}

var newMethods = hasMethods ? BuildMethodsForBackCompatibility(Methods) : null;
var newConstructors = hasConstructors ? BuildConstructorsForBackCompatibility(Constructors) : null;

if (newFields != null || newMethods != null || newConstructors != null)
if (newFields != null || newProperties != null || newMethods != null || newConstructors != null)
{
if (updatedEnumValues != null)
{
_enumValues = updatedEnumValues;
}
Comment thread
jorgerangel-msft marked this conversation as resolved.

// Back-compatibility processing intentionally runs after the library visitor pass so
// that the contract comparison uses the final, post-visitor member signatures (otherwise
// we could incorrectly decide whether a back-compat member is needed). As a result, any
Expand All @@ -877,12 +905,16 @@ internal void ProcessTypeForBackCompatibility()
{
newConstructors = VisitNewMembers(newConstructors, Constructors, static (member, visitor) => visitor.VisitConstructor(member));
}
if (newProperties != null)
{
newProperties = VisitNewMembers(newProperties, Properties, static (member, visitor) => visitor.VisitProperty(member));
}
if (newFields != null)
{
newFields = VisitNewMembers(newFields, Fields, static (member, visitor) => visitor.VisitField(member));
}

Update(fields: newFields, methods: newMethods, constructors: newConstructors);
Update(fields: newFields, properties: newProperties, methods: newMethods, constructors: newConstructors);
}

// Providers whose attributes depend on final generation decisions build their attributes at write
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ private class TestFilterVisitor : LibraryVisitor
return constructor;
}

protected override PropertyProvider? VisitProperty(PropertyProvider property)
protected internal override PropertyProvider? VisitProperty(PropertyProvider property)
{
if (property.Name == "TestProperty")
{
Expand Down
Loading
Loading