From 1b2789f4431476d66b39fc25133656bdff80a0b0 Mon Sep 17 00:00:00 2001 From: hadashiA Date: Fri, 14 Jun 2024 17:28:42 +0900 Subject: [PATCH] wip --- VContainer.SourceGenerator/Analyzer.cs | 36 +++++++++---------- VContainer.SourceGenerator/Emitter.cs | 6 ++-- .../VContainerIncrementalSourceGenerator.cs | 19 ++++++---- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/VContainer.SourceGenerator/Analyzer.cs b/VContainer.SourceGenerator/Analyzer.cs index 80c42dd4..a630bad6 100644 --- a/VContainer.SourceGenerator/Analyzer.cs +++ b/VContainer.SourceGenerator/Analyzer.cs @@ -17,6 +17,20 @@ static class Analyzer { return null; } + if (typeSymbol.TypeKind is TypeKind.Interface or TypeKind.Struct or TypeKind.Enum) + { + return null; + } + + if (typeSymbol.IsAbstract || typeSymbol.IsStatic) + { + return null; + } + + if (typeSymbol.ContainingModule.Name is "VContainer" or "VContainer.Standalone") + { + return null; + } foreach (var baseTypeSymbol in typeSymbol.GetAllBaseTypes()) { @@ -40,16 +54,7 @@ static class Analyzer } } - var typeMeta = new TypeMeta(typeSymbol, referenceSymbols, syntax); - if (typeMeta.ExplictInjectConstructors.Count <= 0 && - typeMeta.InjectFields.Count <= 0 && - typeMeta.InjectProperties.Count <= 0 && - typeMeta.InjectMethods.Count <= 0) - { - return null; - } - - return typeMeta; + return new TypeMeta(typeSymbol, referenceSymbols, syntax); } } @@ -78,15 +83,6 @@ public IEnumerable Analyze(ReferenceSymbols referenceSymbols, Cancella { foreach (var typeArgument in methodSymbol.TypeArguments) { - if (typeArgument.TypeKind is TypeKind.Interface or TypeKind.Struct) - { - continue; - } - if (typeArgument.IsAbstract) - { - continue; - } - var typeMeta = Analyzer.AnalyzeTypeSymbol(typeArgument.ContainingType, referenceSymbols, cancellation: cancellation); if (typeMeta != null) { @@ -108,4 +104,4 @@ public IEnumerable Analyze(ReferenceSymbols referenceSymbols, Cancella } } } -} +} \ No newline at end of file diff --git a/VContainer.SourceGenerator/Emitter.cs b/VContainer.SourceGenerator/Emitter.cs index 69dfa462..cc19ce6e 100644 --- a/VContainer.SourceGenerator/Emitter.cs +++ b/VContainer.SourceGenerator/Emitter.cs @@ -61,9 +61,11 @@ public static bool TryEmitGeneratedInjector( .Replace(">", "_"); var generateTypeName = $"{typeName}GeneratedInjector"; - using (codeWriter.BeginBlockScope("[Preserve]")) + + codeWriter.AppendLine("[Preserve]"); + using (codeWriter.BeginBlockScope($"class {generateTypeName} : IInjector")) { - codeWriter.AppendLine($"class {generateTypeName} : IInjector"); + codeWriter.AppendLine(); if (!TryEmitCreateInstanceMethod(typeMeta, codeWriter, references, in context)) { return false; diff --git a/VContainer.SourceGenerator/VContainerIncrementalSourceGenerator.cs b/VContainer.SourceGenerator/VContainerIncrementalSourceGenerator.cs index 522d6f76..cd400edc 100644 --- a/VContainer.SourceGenerator/VContainerIncrementalSourceGenerator.cs +++ b/VContainer.SourceGenerator/VContainerIncrementalSourceGenerator.cs @@ -28,7 +28,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) foreach (var referencedAssemblyName in compilation.ReferencedAssemblyNames) { - if (referencedAssemblyName.Name == "VContainer") + if (referencedAssemblyName.Name.StartsWith("VContainer")) return true; } return false; @@ -111,16 +111,23 @@ public void Initialize(IncrementalGeneratorInitializationContext context) var typeMetas = typeDeclarationCandidates .Select(x => x.Analyze(references)) - .Where(x => x != null); + .Where(x => x != null && + (x.ExplictInjectConstructors.Count > 0 || + x.InjectFields.Count > 0 || + x.InjectProperties.Count > 0 || + x.InjectMethods.Count > 0)); var typeMetasFromRegister = registerInvocationCandidates .SelectMany(x => x.Analyze(references)); - foreach (var typeMeta in typeMetas.Concat(typeMetasFromRegister)) + foreach (var typeMeta in typeMetas + .Concat(typeMetasFromRegister) + .Where(x => x != null) + .DistinctBy(x => x!.Symbol, SymbolEqualityComparer.Default)) { - if (typeMeta != null && Emitter.TryEmitGeneratedInjector(typeMeta, codeWriter, references, in sourceProductionContext)) + if (Emitter.TryEmitGeneratedInjector(typeMeta!, codeWriter, references, in sourceProductionContext)) { - var fullType = typeMeta.FullTypeName + var fullType = typeMeta!.FullTypeName .Replace("global::", "") .Replace("<", "_") .Replace(">", "_"); @@ -130,6 +137,4 @@ public void Initialize(IncrementalGeneratorInitializationContext context) } }); } - - }