Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
hadashiA committed Jun 14, 2024
1 parent 4b07342 commit 1b2789f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
36 changes: 16 additions & 20 deletions VContainer.SourceGenerator/Analyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -78,15 +83,6 @@ public IEnumerable<TypeMeta> 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)
{
Expand All @@ -108,4 +104,4 @@ public IEnumerable<TypeMeta> Analyze(ReferenceSymbols referenceSymbols, Cancella
}
}
}
}
}
6 changes: 4 additions & 2 deletions VContainer.SourceGenerator/Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 12 additions & 7 deletions VContainer.SourceGenerator/VContainerIncrementalSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(">", "_");
Expand All @@ -130,6 +137,4 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
}
});
}


}

0 comments on commit 1b2789f

Please sign in to comment.