Skip to content

Commit d46e2d5

Browse files
authored
Merge pull request #267 from Cysharp/hadashiA/remove-default-value
Remove default value detection
2 parents e36762e + 3f24349 commit d46e2d5

File tree

4 files changed

+6
-157
lines changed

4 files changed

+6
-157
lines changed

src/MemoryPack.Generator/MemoryPackGenerator.Emitter.cs

+2-75
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using Microsoft.CodeAnalysis;
22
using Microsoft.CodeAnalysis.CSharp;
33
using Microsoft.CodeAnalysis.CSharp.Syntax;
4-
using System;
5-
using System.Collections.Generic;
64
using System.Diagnostics;
75
using System.Text;
86

@@ -46,7 +44,7 @@ static void Generate(TypeDeclarationSyntax syntax, Compilation compilation, stri
4644
}
4745
var unionFormatter = (unionSymbol != null);
4846

49-
var typeMeta = new TypeMeta(semanticModel, typeSymbol, reference);
47+
var typeMeta = new TypeMeta(typeSymbol, reference);
5048
if (unionFormatter)
5149
{
5250
// replace original symbol
@@ -516,7 +514,7 @@ private string EmitDeserializeBody()
516514
{
517515
{{(IsValueType ? "" : "if (value == null)")}}
518516
{
519-
{{Members.Where(x => x.Symbol != null).Select(x => $" __{x.Name} = {x.DefaultValueExpression};").NewLine()}}
517+
{{Members.Where(x => x.Symbol != null).Select(x => $" __{x.Name} = default!;").NewLine()}}
520518
}
521519
{{(IsValueType ? "#if false" : " else")}}
522520
{
@@ -1366,75 +1364,4 @@ public string EmitReadRefDeserialize(int i, bool requireDeltaCheck)
13661364
return $"{pre}reader.ReadValue(ref __{Name});";
13671365
}
13681366
}
1369-
1370-
string EmitConstantValue(object? constantValue)
1371-
{
1372-
if (constantValue != null)
1373-
{
1374-
return constantValue switch
1375-
{
1376-
string x => $"\"{x}\"",
1377-
char x => $"'{x}'",
1378-
float x => $"{x}f",
1379-
decimal x => $"{x}M",
1380-
bool x => x ? "true" : "false",
1381-
_ => constantValue.ToString()
1382-
};
1383-
}
1384-
return "default!";
1385-
}
1386-
1387-
string EmitExpression(ExpressionSyntax expression)
1388-
{
1389-
switch (expression.Kind())
1390-
{
1391-
case SyntaxKind.SimpleMemberAccessExpression:
1392-
{
1393-
var memberAccess = (MemberAccessExpressionSyntax)expression;
1394-
var memberSymbol = semanticModel.GetSymbolInfo(memberAccess.Name).Symbol;
1395-
if (memberSymbol is INamedTypeSymbol { TypeKind: TypeKind.Enum } namedTypeSymbol)
1396-
{
1397-
return $"{GetTypeFullName(namedTypeSymbol)}.{memberAccess.Name}";
1398-
}
1399-
if (memberSymbol is IFieldSymbol { Type.TypeKind: TypeKind.Enum } fieldSymbol)
1400-
{
1401-
return $"{GetTypeFullName(fieldSymbol.Type)}.{fieldSymbol.Name}";
1402-
}
1403-
break;
1404-
}
1405-
1406-
case SyntaxKind.ObjectCreationExpression:
1407-
{
1408-
var objectCreation = (ObjectCreationExpressionSyntax)expression;
1409-
var symbolInfo = semanticModel.GetSymbolInfo(objectCreation.Type);
1410-
if (symbolInfo.Symbol is INamedTypeSymbol x)
1411-
{
1412-
var arguments = string.Join(", ",
1413-
objectCreation.ArgumentList?.Arguments.Select(arg =>
1414-
EmitExpression(arg.Expression)) ?? Enumerable.Empty<string>());
1415-
return $"new {GetTypeFullName(x)}({arguments})";
1416-
}
1417-
break;
1418-
}
1419-
1420-
case SyntaxKind.TupleExpression:
1421-
var tupleExpression = (TupleExpressionSyntax)expression;
1422-
var tupleElements = string.Join(", ",
1423-
tupleExpression.Arguments.Select(arg => EmitExpression(arg.Expression)));
1424-
return $"({tupleElements})";
1425-
}
1426-
return expression.ToString();
1427-
}
1428-
1429-
static string GetTypeFullName(ITypeSymbol typeSymbol)
1430-
{
1431-
if (typeSymbol.ContainingType is { } containingType)
1432-
{
1433-
// nested type
1434-
var containingTypeFullName = containingType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
1435-
var typeName = typeSymbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat);
1436-
return $"{containingTypeFullName}.{typeName}";
1437-
}
1438-
return typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
1439-
}
14401367
}

src/MemoryPack.Generator/MemoryPackGenerator.Parser.cs

+3-39
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public partial class TypeMeta
6666
public (ushort Tag, INamedTypeSymbol Type)[] UnionTags { get; }
6767
public bool IsUseEmptyConstructor => Constructor == null || Constructor.Parameters.IsEmpty;
6868

69-
public TypeMeta(SemanticModel semanticModel, INamedTypeSymbol symbol, ReferenceSymbols reference)
69+
public TypeMeta(INamedTypeSymbol symbol, ReferenceSymbols reference)
7070
{
7171
this.reference = reference;
7272
this.Symbol = symbol;
@@ -104,7 +104,7 @@ public TypeMeta(SemanticModel semanticModel, INamedTypeSymbol symbol, ReferenceS
104104
}
105105
return true;
106106
})
107-
.Select((x, i) => new MemberMeta(semanticModel, x, Constructor, reference, i))
107+
.Select((x, i) => new MemberMeta(x, Constructor, reference, i))
108108
.OrderBy(x => x.Order)
109109
.ToArray();
110110

@@ -615,8 +615,6 @@ partial class MemberMeta
615615
public int Order { get; }
616616
public bool HasExplicitOrder { get; }
617617
public MemberKind Kind { get; }
618-
public string DefaultValueExpression { get; } = "default!";
619-
readonly SemanticModel semanticModel;
620618

621619
MemberMeta(int order)
622620
{
@@ -627,9 +625,8 @@ partial class MemberMeta
627625
this.Kind = MemberKind.Blank;
628626
}
629627

630-
public MemberMeta(SemanticModel semanticModel, ISymbol symbol, IMethodSymbol? constructor, ReferenceSymbols references, int sequentialOrder)
628+
public MemberMeta(ISymbol symbol, IMethodSymbol? constructor, ReferenceSymbols references, int sequentialOrder)
631629
{
632-
this.semanticModel = semanticModel;
633630
this.Symbol = symbol;
634631
this.Name = symbol.Name;
635632
this.Order = sequentialOrder;
@@ -649,10 +646,6 @@ public MemberMeta(SemanticModel semanticModel, ISymbol symbol, IMethodSymbol? co
649646
{
650647
this.IsConstructorParameter = constructor.TryGetConstructorParameter(symbol, out var constructorParameter);
651648
this.ConstructorParameterName = constructorParameter?.Name;
652-
if (constructorParameter?.HasExplicitDefaultValue == true)
653-
{
654-
DefaultValueExpression = EmitConstantValue(constructorParameter.ExplicitDefaultValue);
655-
}
656649
}
657650
else
658651
{
@@ -670,25 +663,6 @@ public MemberMeta(SemanticModel semanticModel, ISymbol symbol, IMethodSymbol? co
670663
#endif
671664
;
672665
MemberType = f.Type;
673-
674-
// Detect default value
675-
foreach (var syntaxReference in f.DeclaringSyntaxReferences)
676-
{
677-
var syntax = syntaxReference.GetSyntax();
678-
if (syntax is FieldDeclarationSyntax { Declaration.Variables: { Count: > 0 } variables })
679-
{
680-
if (variables.First().Initializer is { } initializer)
681-
{
682-
DefaultValueExpression = EmitExpression(initializer.Value);
683-
break;
684-
}
685-
}
686-
if (syntax is VariableDeclaratorSyntax { Initializer: { } initializer2 })
687-
{
688-
DefaultValueExpression = EmitExpression(initializer2.Value);
689-
break;
690-
}
691-
}
692666
}
693667
else if (symbol is IPropertySymbol p)
694668
{
@@ -701,16 +675,6 @@ public MemberMeta(SemanticModel semanticModel, ISymbol symbol, IMethodSymbol? co
701675
#endif
702676
&& (p.SetMethod != null && !p.SetMethod.IsInitOnly);
703677
MemberType = p.Type;
704-
705-
// Detect default value
706-
foreach (var syntaxReference in p.DeclaringSyntaxReferences)
707-
{
708-
if (syntaxReference.GetSyntax() is PropertyDeclarationSyntax { Initializer: { } initializer })
709-
{
710-
DefaultValueExpression = EmitExpression(initializer.Value);
711-
break;
712-
}
713-
}
714678
}
715679
else
716680
{

src/MemoryPack.Generator/MemoryPackGenerator.TypeScript.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ partial class MemoryPackGenerator
3838
return null;
3939
}
4040

41-
var typeMeta = new TypeMeta(semanticModel, typeSymbol, reference);
41+
var typeMeta = new TypeMeta(typeSymbol, reference);
4242

4343
if (typeMeta.GenerateType is not (GenerateType.Object or GenerateType.Union))
4444
{

tests/MemoryPack.Tests/DefaultValueTest.cs

-42
This file was deleted.

0 commit comments

Comments
 (0)