You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.8 KiB
76 lines
2.8 KiB
using System; |
|
using System.Collections.Generic; |
|
using System.Linq; |
|
using Microsoft.CodeAnalysis; |
|
|
|
namespace gaemstone.SourceGen.Structure; |
|
|
|
public abstract class BaseEntityInfo : BaseInfo |
|
{ |
|
public new TypeEntityInfo? Parent { get => (TypeEntityInfo?)base.Parent; set => base.Parent = value; } |
|
|
|
public string? EntityPath { get; } |
|
public string? EntitySymbol { get; } |
|
|
|
public List<INamedTypeSymbol> EntitiesToAdd { get; } = new(); |
|
public List<(INamedTypeSymbol, INamedTypeSymbol)> RelationsToAdd { get; } = new(); |
|
public bool HasEntitiesToAdd => (EntitiesToAdd.Count > 0) || (RelationsToAdd.Count > 0); |
|
|
|
public BaseEntityInfo(ISymbol symbol) |
|
: base(symbol) |
|
{ |
|
// TODO: Validate that these only contain valid characters. |
|
EntityPath = Get("Path")?.ConstructorArguments.FirstOrDefault().Value as string; |
|
EntitySymbol = (Get("Symbol") is AttributeData symbolAttr) |
|
// If [Symbol] is present, use the given custom symbol (if given), .. |
|
? (symbolAttr.ConstructorArguments.FirstOrDefault().Value as string) |
|
?? EntityPath?.Split('/')[^1] // .. otherwise default to the name in [Path], .. |
|
?? Name // .. or just use the default: The symbol's name. |
|
: null; |
|
} |
|
|
|
protected override IEnumerable<Diagnostic> ValidateSelf() |
|
{ |
|
if (this is ModuleEntityInfo) { |
|
// If this entity is a module, it must not be nested. |
|
if (Symbol.ContainingType != null) yield return Diagnostic.Create( |
|
Descriptors.ModuleMustNotBeNested, Location); |
|
} else { |
|
// Otherwise, it must occur within a module |
|
if (Parent is not ModuleEntityInfo) yield return Diagnostic.Create( |
|
Descriptors.EntityMustBeInModule, Location); |
|
} |
|
|
|
// Add entities and relationships specified using [Add<...>] attributes. |
|
foreach (var attr in Symbol.GetAttributes()) { |
|
for (var attrType = attr.AttributeClass; attrType != null; attrType = attrType.BaseType) { |
|
if (RelevantSymbolReceiver.ToRelevantAttributeName(attrType) != "Add") continue; |
|
|
|
var allTypeArgumentsValid = true; |
|
for (var i = 0; i < attrType.TypeArguments.Length; i++) { |
|
var arg = attrType.TypeArguments[i]; |
|
var param = attrType.TypeParameters[i]; |
|
if (arg is not INamedTypeSymbol) { |
|
yield return Diagnostic.Create( |
|
Descriptors.InvalidTypeArgument, param.Locations.Single()); |
|
allTypeArgumentsValid = false; |
|
} |
|
// TODO: Make sure entities being added have appropriate attributes as well. |
|
} |
|
|
|
if (allTypeArgumentsValid) { |
|
switch (attrType.TypeArguments) { |
|
case [ INamedTypeSymbol entity ]: |
|
EntitiesToAdd.Add(entity); |
|
break; |
|
case [ INamedTypeSymbol relation, INamedTypeSymbol target ]: |
|
RelationsToAdd.Add((relation, target)); |
|
break; |
|
default: throw new InvalidOperationException( |
|
"Type argument pattern matching failed"); |
|
} |
|
} |
|
} |
|
} |
|
} |
|
}
|
|
|