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.
 
 

43 lines
1.3 KiB

using System.Collections.Generic;
using System.Linq;
using gaemstone.SourceGen.Utility;
using Microsoft.CodeAnalysis;
namespace gaemstone.SourceGen.Structure;
public abstract class BaseInfo
{
public ISymbol Symbol { get; }
public BaseEntityInfo? Parent { get; set; }
public string Name => Symbol.Name;
public string FullName => Symbol.GetFullName();
public string Namespace => Symbol.GetNamespace()!;
public Location Location => Symbol.Locations.First();
public List<Diagnostic> Diagnostics { get; } = new();
public bool IsErrored => Diagnostics.Any(d => d.Severity == DiagnosticSeverity.Error);
public BaseInfo(ISymbol symbol) => Symbol = symbol;
public void Validate()
{
// All of the children are validated before the parent is,
// in case their state affects the parent in some way.
foreach (var child in GetChildren())
child.Validate();
foreach (var diag in ValidateSelf())
Diagnostics.Add(diag);
}
protected virtual IEnumerable<BaseInfo> GetChildren() => Enumerable.Empty<BaseInfo>();
protected abstract IEnumerable<Diagnostic> ValidateSelf();
protected bool Has(string name)
=> Get(name) != null;
protected AttributeData? Get(string name)
=> Symbol.GetAttributes().FirstOrDefault(attr =>
RelevantSymbolReceiver.ToRelevantAttributeName(attr.AttributeClass!) == name);
}