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.
97 lines
3.7 KiB
97 lines
3.7 KiB
using System; |
|
using System.Collections.Generic; |
|
using System.Collections.Immutable; |
|
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 Relation, INamedTypeSymbol Target)> RelationsToAdd { get; } = new(); |
|
public List<(INamedTypeSymbol Component, ImmutableArray<TypedConstant> Arguments)> ComponentsToAdd { get; } = new(); |
|
|
|
public virtual bool HasEntitiesToAdd => (EntitiesToAdd.Count > 0) |
|
|| (RelationsToAdd.Count > 0) |
|
|| (ComponentsToAdd.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); |
|
} |
|
|
|
foreach (var attr in Symbol.GetAttributes()) { |
|
// Add entities and relationships specified using [Add<...>] attributes. |
|
for (var attrType = attr.AttributeClass; attrType != null; attrType = attrType.BaseType) { |
|
var attrName = RelevantSymbolReceiver.ToRelevantAttributeName(attrType); |
|
if (attrName is "Add" or "Set") { |
|
|
|
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) continue; |
|
|
|
switch (attrName) { |
|
case "Add": |
|
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"); |
|
} |
|
break; |
|
|
|
case "Set": |
|
var component = (INamedTypeSymbol)attrType.TypeArguments.Single(); |
|
var arguments = attr.ConstructorArguments.Single().Values; |
|
// TODO: Verify arguments actually match a constructor. |
|
// Right now this will just error in the generated code. Probably good enough? |
|
ComponentsToAdd.Add((component, arguments)); |
|
break; |
|
|
|
default: throw new InvalidOperationException( |
|
"Invalid relevant attribute name"); |
|
} |
|
} |
|
} |
|
} |
|
} |
|
}
|
|
|