Skip to content
Merged
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 @@ -17,7 +17,7 @@

namespace Microsoft.TypeSpec.Generator.ClientModel.Providers
{
public class BinaryContentHelperDefinition : TypeProvider
public class BinaryContentHelperDefinition : InternalHelperProvider
{
private const string _fromEnumerableName = "FromEnumerable";
private const string _fromDictionaryName = "FromDictionary";
Expand All @@ -30,9 +30,6 @@ public class BinaryContentHelperDefinition : TypeProvider

protected override string BuildName() => "BinaryContentHelper";

protected override TypeSignatureModifiers BuildDeclarationModifiers()
=> TypeSignatureModifiers.Internal | TypeSignatureModifiers.Static | TypeSignatureModifiers.Partial | TypeSignatureModifiers.Class;

protected override string BuildRelativeFilePath() => Path.Combine("src", "Generated", "Internal", $"{Name}.cs");

protected override MethodProvider[] BuildMethods()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Microsoft.TypeSpec.Generator.ClientModel.Providers
{
internal class CancellationTokenExtensionsDefinition : TypeProvider
internal class CancellationTokenExtensionsDefinition : InternalHelperProvider
{
private ParameterProvider _cancellationTokenParam;

Expand All @@ -21,11 +21,6 @@ public CancellationTokenExtensionsDefinition()
_cancellationTokenParam = new ParameterProvider("cancellationToken", FormattableStringHelpers.Empty, typeof(CancellationToken));
}

protected override TypeSignatureModifiers BuildDeclarationModifiers()
{
return TypeSignatureModifiers.Internal | TypeSignatureModifiers.Static;
}

protected override string BuildRelativeFilePath() => Path.Combine("src", "Generated", "Internal", $"{Name}.cs");

protected override string BuildName() => "CancellationTokenExtensions";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Microsoft.TypeSpec.Generator.ClientModel.Providers
{
internal class ClientPipelineExtensionsDefinition : TypeProvider
internal class ClientPipelineExtensionsDefinition : InternalHelperProvider
{
private ParameterProvider _pipelineParam;
private ParameterProvider _messageParam;
Expand All @@ -32,11 +32,6 @@ public ClientPipelineExtensionsDefinition()
_options = _requestOptionsParam.ToApi<HttpRequestOptionsApi>();
}

protected override TypeSignatureModifiers BuildDeclarationModifiers()
{
return TypeSignatureModifiers.Internal | TypeSignatureModifiers.Static;
}

protected override string BuildRelativeFilePath() => Path.Combine("src", "Generated", "Internal", $"{Name}.cs");

protected override string BuildName() => "ClientPipelineExtensions";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace Microsoft.TypeSpec.Generator.ClientModel.Providers
{
internal sealed class ClientUriBuilderDefinition : TypeProvider
internal sealed class ClientUriBuilderDefinition : InternalHelperProvider
{
private const string ToUriMethodName = "ToUri";
private const string ResetMethodName = "Reset";
Expand Down Expand Up @@ -63,10 +63,7 @@ public ClientUriBuilderDefinition()
_pathLengthField = new(FieldModifiers.Private, typeof(int), "_pathLength", this);
}

protected override TypeSignatureModifiers BuildDeclarationModifiers()
{
return TypeSignatureModifiers.Internal;
}
protected override TypeSignatureModifiers BuildDeclarationModifiers() => TypeSignatureModifiers.Internal;

protected override string BuildRelativeFilePath() => Path.Combine("src", "Generated", "Internal", $"{Name}.cs");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Microsoft.TypeSpec.Generator.ClientModel.Providers
{
internal class ErrorResultDefinition : TypeProvider
internal class ErrorResultDefinition : InternalHelperProvider
{
private class ErrorResultTemplate<T> { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#pragma warning disable SCME0004 // FileBinaryContent is evaluation-only.
namespace Microsoft.TypeSpec.Generator.ClientModel.Providers
{
public sealed partial class ModelSerializationExtensionsDefinition : TypeProvider
public sealed partial class ModelSerializationExtensionsDefinition : InternalHelperProvider
{
public const string WireOptionsFieldName = "WireOptions";
public const string JsonDocumentOptionsFieldName = "JsonDocumentOptions";
Expand Down Expand Up @@ -96,11 +96,6 @@ public ModelSerializationExtensionsDefinition()
enclosingType: this);
}

protected override TypeSignatureModifiers BuildDeclarationModifiers()
{
return TypeSignatureModifiers.Internal | TypeSignatureModifiers.Static;
}

internal FieldProvider WireOptionsField { get; }
private readonly FieldProvider _jsonDocumentOptionsField;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace Microsoft.TypeSpec.Generator.ClientModel.Providers
{
public class MultiPartFormDataBinaryContentDefinition : TypeProvider
public class MultiPartFormDataBinaryContentDefinition : InternalHelperProvider
{
private readonly ScopedApi<MultipartFormDataContent> _multipartContentExpression;
private readonly FieldProvider _multipartContentField;
Expand Down Expand Up @@ -77,7 +77,8 @@ public MultiPartFormDataBinaryContentDefinition()

protected override string BuildName() => $"MultiPartFormData{_requestContentType.Name}";

protected override TypeSignatureModifiers BuildDeclarationModifiers() => TypeSignatureModifiers.Class | TypeSignatureModifiers.Internal;
protected override TypeSignatureModifiers BuildDeclarationModifiers()
=> TypeSignatureModifiers.Internal | TypeSignatureModifiers.Class;

protected override CSharpType BuildBaseType() => _requestContentType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Microsoft.TypeSpec.Generator.ClientModel.Providers
{
internal class MultipartFormDataHelperDefinition : TypeProvider
internal class MultipartFormDataHelperDefinition : InternalHelperProvider
{
private const string _fromEnumerableName = "FromEnumerable";
private const string _fromDictionaryName = "FromDictionary";
Expand All @@ -24,9 +24,6 @@ internal class MultipartFormDataHelperDefinition : TypeProvider

protected override string BuildName() => "MultipartFormDataHelper";

protected override TypeSignatureModifiers BuildDeclarationModifiers()
=> TypeSignatureModifiers.Internal | TypeSignatureModifiers.Static | TypeSignatureModifiers.Partial | TypeSignatureModifiers.Class;

protected override string BuildRelativeFilePath() => Path.Combine("src", "Generated", "Internal", $"{Name}.cs");

protected override MethodProvider[] BuildMethods()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace Microsoft.TypeSpec.Generator.ClientModel.Providers
{
internal class PipelineRequestHeadersExtensionsDefinition : TypeProvider
internal class PipelineRequestHeadersExtensionsDefinition : InternalHelperProvider
{
private const string _setDelimited = "SetDelimited";
private const string _addWithPrefix = "Add";
Expand All @@ -25,11 +25,6 @@ public PipelineRequestHeadersExtensionsDefinition()
_pipelineRequestHeadersParam = new ParameterProvider("headers", FormattableStringHelpers.Empty, typeof(PipelineRequestHeaders));
}
private readonly CSharpType _t = typeof(IEnumerable<>).GetGenericArguments()[0];
protected override TypeSignatureModifiers BuildDeclarationModifiers()
{
return TypeSignatureModifiers.Internal | TypeSignatureModifiers.Static;
}

protected override string BuildRelativeFilePath() => Path.Combine("src", "Generated", "Internal", $"{Name}.cs");

protected override string BuildName() => "PipelineRequestHeadersExtensions";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace Microsoft.TypeSpec.Generator.ClientModel.Providers
{
internal sealed class TypeFormattersDefinition : TypeProvider
internal sealed class TypeFormattersDefinition : InternalHelperProvider
{
private readonly ValueExpression _invariantCultureExpression = new MemberExpression(typeof(CultureInfo), nameof(CultureInfo.InvariantCulture));
private const string ToStringMethodName = "ToString";
Expand All @@ -33,11 +33,6 @@ internal TypeFormattersDefinition()
_defaultNumberFormatField = new(FieldModifiers.Public | FieldModifiers.Const, typeof(string), "DefaultNumberFormat", this, initializationValue: Literal("G"));
}

protected override TypeSignatureModifiers BuildDeclarationModifiers()
{
return TypeSignatureModifiers.Internal | TypeSignatureModifiers.Static;
}

protected override string BuildRelativeFilePath() => Path.Combine("src", "Generated", "Internal", $"{Name}.cs");

protected override string BuildName() => "TypeFormatters";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace Microsoft.TypeSpec.Generator.ClientModel.Providers
{
public class Utf8JsonBinaryContentDefinition : TypeProvider
public class Utf8JsonBinaryContentDefinition : InternalHelperProvider
{
private const string _jsonWriterName = "JsonWriter";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ public class ScmCodeModelGenerator : CodeModelGenerator

public override ScmTypeFactory TypeFactory { get; }

internal ModelSerializationExtensionsDefinition ModelSerializationExtensionsDefinition { get; } =
new ModelSerializationExtensionsDefinition();
internal ModelSerializationExtensionsDefinition ModelSerializationExtensionsDefinition
=> _modelSerializationExtensionsDefinition ??= new ModelSerializationExtensionsDefinition();
private ModelSerializationExtensionsDefinition? _modelSerializationExtensionsDefinition;

internal SerializationFormatDefinition SerializationFormatDefinition { get; } =
new SerializationFormatDefinition();
Expand All @@ -50,6 +51,7 @@ protected override void Configure()
AddMetadataReference(MetadataReference.CreateFromFile(typeof(BinaryData).Assembly.Location));
AddMetadataReference(MetadataReference.CreateFromFile(typeof(JsonSerializer).Assembly.Location));
AddTypeToKeep(ModelReaderWriterContextDefinition.s_name, isRoot: false);
AddTypeToKeep(SerializationFormatDefinition, isRoot: false);
}

public override async Task WriteAdditionalFiles(string outputPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ private async Task<TypeSymbols> GetTypeSymbolsAsync(Compilation compilation,
// we do not add the model factory and additionalNonRootTypeSymbols to the declared symbol list
// so that it will never be included in any process of internalization or removal
if (SymbolEqualityComparer.Default.Equals(symbol, _modelFactorySymbol)
|| _additionalNonRootTypeNames.Contains(symbol.Name)
|| _additionalNonRootTypeNames.Contains(symbol.GetFullyQualifiedName()))
|| IsAdditionalNonRootType(symbol))
{
continue;
}
Expand All @@ -110,6 +109,24 @@ private async Task<TypeSymbols> GetTypeSymbolsAsync(Compilation compilation,
kv => kv.Value.ToHashSet<INamedTypeSymbol>(SymbolEqualityComparer.Default)));
}

private bool IsAdditionalNonRootType(INamedTypeSymbol symbol)
{
if (_additionalNonRootTypeNames.Contains(symbol.Name))
{
return true;
}

var fullyQualifiedName = symbol.GetFullyQualifiedName();
Comment thread
jorgerangel-msft marked this conversation as resolved.
if (_additionalNonRootTypeNames.Contains(fullyQualifiedName))
{
return true;
}

var genericArityIndex = fullyQualifiedName.IndexOf('`');
return genericArityIndex > 0
&& _additionalNonRootTypeNames.Contains(fullyQualifiedName[..genericArityIndex]);
}

protected virtual bool ShouldIncludeDocument(Document document) =>
!GeneratedCodeWorkspace.IsGeneratedTestDocument(document);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace Microsoft.TypeSpec.Generator.Providers
{
internal class ArgumentDefinition : TypeProvider
internal class ArgumentDefinition : InternalHelperProvider
{
private class Template<T> { }

Expand All @@ -34,11 +34,6 @@ public ArgumentDefinition()
_nullableT = _t.WithNullable(true);
}

protected override TypeSignatureModifiers BuildDeclarationModifiers()
{
return TypeSignatureModifiers.Internal | TypeSignatureModifiers.Static;
}

private MethodSignature GetSignature(
string name,
IReadOnlyList<ParameterProvider> parameters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Microsoft.TypeSpec.Generator.Providers
{
internal sealed class ChangeTrackingDictionaryDefinition : TypeProvider
internal sealed class ChangeTrackingDictionaryDefinition : InternalHelperProvider
{
private class ChangeTrackingDictionaryTemplate<TKey, TValue> { }
private readonly CSharpType _tKey = typeof(ChangeTrackingDictionaryTemplate<,>).GetGenericArguments()[0];
Expand Down Expand Up @@ -48,10 +48,7 @@ public ChangeTrackingDictionaryDefinition()

protected override WhereExpression BuildWhereClause() => Where.NotNull(_tKey);

protected override TypeSignatureModifiers BuildDeclarationModifiers()
{
return TypeSignatureModifiers.Internal;
}
protected override TypeSignatureModifiers BuildDeclarationModifiers() => TypeSignatureModifiers.Internal;

protected override string BuildRelativeFilePath() => Path.Combine("src", "Generated", "Internal", $"{Name}.cs");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Microsoft.TypeSpec.Generator.Providers
{
internal sealed class ChangeTrackingListDefinition : TypeProvider
internal sealed class ChangeTrackingListDefinition : InternalHelperProvider
{
private class ChangeTrackingListTemplate<T> { }

Expand Down Expand Up @@ -46,10 +46,7 @@ public ChangeTrackingListDefinition()
EnsureList = new(This.Invoke(_ensureListSignature));
}

protected override TypeSignatureModifiers BuildDeclarationModifiers()
{
return TypeSignatureModifiers.Internal;
}
protected override TypeSignatureModifiers BuildDeclarationModifiers() => TypeSignatureModifiers.Internal;

protected override string BuildRelativeFilePath() => Path.Combine("src", "Generated", "Internal", $"{Name}.cs");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using Microsoft.TypeSpec.Generator.Input;
using Microsoft.TypeSpec.Generator.Primitives;

namespace Microsoft.TypeSpec.Generator.Providers
{
/// <summary>
/// A base provider for generator-owned internal helper types that must not be removed during post-processing.
/// </summary>
public abstract class InternalHelperProvider : TypeProvider
{
protected InternalHelperProvider(InputType? inputType = default) : base(inputType)
{
CodeModelGenerator.Instance.AddTypeToKeep(this, isRoot: false);
}

protected override TypeSignatureModifiers BuildDeclarationModifiers()
=> TypeSignatureModifiers.Internal | TypeSignatureModifiers.Static;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Microsoft.TypeSpec.Generator.Providers
{
public class OptionalDefinition : TypeProvider
public class OptionalDefinition : InternalHelperProvider
{
private class ListTemplate<T> { }

Expand All @@ -29,11 +29,6 @@ public OptionalDefinition()
_tValue = _genericChangeTrackingDictionary.Arguments[1];
}

protected override TypeSignatureModifiers BuildDeclarationModifiers()
{
return TypeSignatureModifiers.Internal | TypeSignatureModifiers.Static;
}

protected override string BuildRelativeFilePath() => Path.Combine("src", "Generated", "Internal", $"{Name}.cs");

protected override string BuildName() => "Optional";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ namespace Microsoft.TypeSpec.Generator
{
public class TypeFactory
{
private ChangeTrackingListDefinition ChangeTrackingListProvider { get; } = new();
private ChangeTrackingListDefinition ChangeTrackingListProvider
=> _changeTrackingListProvider ??= new();
private ChangeTrackingListDefinition? _changeTrackingListProvider;

private ChangeTrackingDictionaryDefinition ChangeTrackingDictionaryProvider { get; } = new();
private ChangeTrackingDictionaryDefinition ChangeTrackingDictionaryProvider
=> _changeTrackingDictionaryProvider ??= new();
private ChangeTrackingDictionaryDefinition? _changeTrackingDictionaryProvider;

private Dictionary<InputModelType, ModelProvider?> InputTypeToModelProvider { get; } = [];

Expand Down
Loading
Loading