| 1 |
using System.Collections.Generic; |
| 2 |
using System.Linq; |
| 3 |
using System.Text; |
| 4 |
using Microsoft.CodeAnalysis; |
| 5 |
using Microsoft.CodeAnalysis.CSharp; |
| 6 |
using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 7 |
using Microsoft.CodeAnalysis.Text; |
| 8 |
|
| 9 |
namespace Godot.SourceGenerators |
| 10 |
{ |
| 11 |
[Generator] |
| 12 |
public class ScriptPropertyDefValGenerator : ISourceGenerator |
| 13 |
{ |
| 14 |
public void Initialize(GeneratorInitializationContext context) |
| 15 |
{ |
| 16 |
} |
| 17 |
|
| 18 |
public void Execute(GeneratorExecutionContext context) |
| 19 |
{ |
| 20 |
if (context.IsGodotSourceGeneratorDisabled("ScriptPropertyDefVal")) |
| 21 |
return; |
| 22 |
|
| 23 |
INamedTypeSymbol[] godotClasses = context |
| 24 |
.Compilation.SyntaxTrees |
| 25 |
.SelectMany(tree => |
| 26 |
tree.GetRoot().DescendantNodes() |
| 27 |
.OfType<ClassDeclarationSyntax>() |
| 28 |
.SelectGodotScriptClasses(context.Compilation) |
| 29 |
|
| 30 |
.Where(x => |
| 31 |
{ |
| 32 |
if (x.cds.IsPartial()) |
| 33 |
{ |
| 34 |
if (x.cds.IsNested() && !x.cds.AreAllOuterTypesPartial(out _)) |
| 35 |
{ |
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
return true; |
| 40 |
} |
| 41 |
|
| 42 |
return false; |
| 43 |
}) |
| 44 |
.Select(x => x.symbol) |
| 45 |
) |
| 46 |
.Distinct<INamedTypeSymbol>(SymbolEqualityComparer.Default) |
| 47 |
.ToArray(); |
| 48 |
|
| 49 |
if (godotClasses.Length > 0) |
| 50 |
{ |
| 51 |
var typeCache = new MarshalUtils.TypeCache(context.Compilation); |
| 52 |
|
| 53 |
foreach (var godotClass in godotClasses) |
| 54 |
{ |
| 55 |
VisitGodotScriptClass(context, typeCache, godotClass); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
private static void VisitGodotScriptClass( |
| 61 |
GeneratorExecutionContext context, |
| 62 |
MarshalUtils.TypeCache typeCache, |
| 63 |
INamedTypeSymbol symbol |
| 64 |
) |
| 65 |
{ |
| 66 |
INamespaceSymbol namespaceSymbol = symbol.ContainingNamespace; |
| 67 |
string classNs = namespaceSymbol is { IsGlobalNamespace: false } |
| 68 |
? namespaceSymbol.FullQualifiedNameOmitGlobal() |
| 69 |
: string.Empty; |
| 70 |
bool hasNamespace = classNs.Length != 0; |
| 71 |
|
| 72 |
bool isNode = symbol.InheritsFrom("GodotSharp", GodotClasses.Node); |
| 73 |
|
| 74 |
bool isInnerClass = symbol.ContainingType != null; |
| 75 |
|
| 76 |
string uniqueHint = symbol.FullQualifiedNameOmitGlobal().SanitizeQualifiedNameForUniqueHint() |
| 77 |
+ "_ScriptPropertyDefVal.generated"; |
| 78 |
|
| 79 |
var source = new StringBuilder(); |
| 80 |
|
| 81 |
if (hasNamespace) |
| 82 |
{ |
| 83 |
source.Append("namespace "); |
| 84 |
source.Append(classNs); |
| 85 |
source.Append(" {\n\n"); |
| 86 |
} |
| 87 |
|
| 88 |
if (isInnerClass) |
| 89 |
{ |
| 90 |
var containingType = symbol.ContainingType; |
| 91 |
AppendPartialContainingTypeDeclarations(containingType); |
| 92 |
|
| 93 |
void AppendPartialContainingTypeDeclarations(INamedTypeSymbol? containingType) |
| 94 |
{ |
| 95 |
if (containingType == null) |
| 96 |
return; |
| 97 |
|
| 98 |
AppendPartialContainingTypeDeclarations(containingType.ContainingType); |
| 99 |
|
| 100 |
source.Append("partial "); |
| 101 |
source.Append(containingType.GetDeclarationKeyword()); |
| 102 |
source.Append(" "); |
| 103 |
source.Append(containingType.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)); |
| 104 |
source.Append("\n{\n"); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
source.Append("partial class "); |
| 109 |
source.Append(symbol.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)); |
| 110 |
source.Append("\n{\n"); |
| 111 |
|
| 112 |
var exportedMembers = new List<ExportedPropertyMetadata>(); |
| 113 |
|
| 114 |
var members = symbol.GetMembers(); |
| 115 |
|
| 116 |
var exportedProperties = members |
| 117 |
.Where(s => s.Kind == SymbolKind.Property) |
| 118 |
.Cast<IPropertySymbol>() |
| 119 |
.Where(s => s.GetAttributes() |
| 120 |
.Any(a => a.AttributeClass?.IsGodotExportAttribute() ?? false)) |
| 121 |
.ToArray(); |
| 122 |
|
| 123 |
var exportedFields = members |
| 124 |
.Where(s => s.Kind == SymbolKind.Field && !s.IsImplicitlyDeclared) |
| 125 |
.Cast<IFieldSymbol>() |
| 126 |
.Where(s => s.GetAttributes() |
| 127 |
.Any(a => a.AttributeClass?.IsGodotExportAttribute() ?? false)) |
| 128 |
.ToArray(); |
| 129 |
|
| 130 |
foreach (var property in exportedProperties) |
| 131 |
{ |
| 132 |
if (property.IsStatic) |
| 133 |
{ |
| 134 |
context.ReportDiagnostic(Diagnostic.Create( |
| 135 |
Common.ExportedMemberIsStaticRule, |
| 136 |
property.Locations.FirstLocationWithSourceTreeOrDefault(), |
| 137 |
property.ToDisplayString() |
| 138 |
)); |
| 139 |
continue; |
| 140 |
} |
| 141 |
|
| 142 |
if (property.IsIndexer) |
| 143 |
{ |
| 144 |
context.ReportDiagnostic(Diagnostic.Create( |
| 145 |
Common.ExportedMemberIsIndexerRule, |
| 146 |
property.Locations.FirstLocationWithSourceTreeOrDefault(), |
| 147 |
property.ToDisplayString() |
| 148 |
)); |
| 149 |
continue; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
if (property.IsWriteOnly) |
| 157 |
{ |
| 158 |
context.ReportDiagnostic(Diagnostic.Create( |
| 159 |
Common.ExportedPropertyIsWriteOnlyRule, |
| 160 |
property.Locations.FirstLocationWithSourceTreeOrDefault(), |
| 161 |
property.ToDisplayString() |
| 162 |
)); |
| 163 |
continue; |
| 164 |
} |
| 165 |
|
| 166 |
if (property.IsReadOnly || property.SetMethod!.IsInitOnly) |
| 167 |
{ |
| 168 |
context.ReportDiagnostic(Diagnostic.Create( |
| 169 |
Common.ExportedMemberIsReadOnlyRule, |
| 170 |
property.Locations.FirstLocationWithSourceTreeOrDefault(), |
| 171 |
property.ToDisplayString() |
| 172 |
)); |
| 173 |
continue; |
| 174 |
} |
| 175 |
|
| 176 |
if (property.ExplicitInterfaceImplementations.Length > 0) |
| 177 |
{ |
| 178 |
context.ReportDiagnostic(Diagnostic.Create( |
| 179 |
Common.ExportedMemberIsExplicitInterfaceImplementationRule, |
| 180 |
property.Locations.FirstLocationWithSourceTreeOrDefault(), |
| 181 |
property.ToDisplayString() |
| 182 |
)); |
| 183 |
continue; |
| 184 |
} |
| 185 |
|
| 186 |
var propertyType = property.Type; |
| 187 |
var marshalType = MarshalUtils.ConvertManagedTypeToMarshalType(propertyType, typeCache); |
| 188 |
|
| 189 |
if (marshalType == null) |
| 190 |
{ |
| 191 |
context.ReportDiagnostic(Diagnostic.Create( |
| 192 |
Common.ExportedMemberTypeIsNotSupportedRule, |
| 193 |
property.Locations.FirstLocationWithSourceTreeOrDefault(), |
| 194 |
property.ToDisplayString() |
| 195 |
)); |
| 196 |
continue; |
| 197 |
} |
| 198 |
|
| 199 |
if (!isNode && MemberHasNodeType(propertyType, marshalType.Value)) |
| 200 |
{ |
| 201 |
context.ReportDiagnostic(Diagnostic.Create( |
| 202 |
Common.OnlyNodesShouldExportNodesRule, |
| 203 |
property.Locations.FirstLocationWithSourceTreeOrDefault() |
| 204 |
)); |
| 205 |
continue; |
| 206 |
} |
| 207 |
|
| 208 |
var propertyDeclarationSyntax = property.DeclaringSyntaxReferences |
| 209 |
.Select(r => r.GetSyntax() as PropertyDeclarationSyntax).FirstOrDefault(); |
| 210 |
|
| 211 |
|
| 212 |
string? value = null; |
| 213 |
if (propertyDeclarationSyntax != null) |
| 214 |
{ |
| 215 |
if (propertyDeclarationSyntax.Initializer != null) |
| 216 |
{ |
| 217 |
var sm = context.Compilation.GetSemanticModel(propertyDeclarationSyntax.Initializer.SyntaxTree); |
| 218 |
var initializerValue = propertyDeclarationSyntax.Initializer.Value; |
| 219 |
if (!IsStaticallyResolvable(initializerValue, sm)) |
| 220 |
value = "default"; |
| 221 |
else |
| 222 |
value = propertyDeclarationSyntax.Initializer.Value.FullQualifiedSyntax(sm); |
| 223 |
} |
| 224 |
else |
| 225 |
{ |
| 226 |
var propertyGet = propertyDeclarationSyntax.AccessorList?.Accessors |
| 227 |
.FirstOrDefault(a => a.Keyword.IsKind(SyntaxKind.GetKeyword)); |
| 228 |
if (propertyGet != null) |
| 229 |
{ |
| 230 |
if (propertyGet.ExpressionBody != null) |
| 231 |
{ |
| 232 |
if (propertyGet.ExpressionBody.Expression is IdentifierNameSyntax identifierNameSyntax) |
| 233 |
{ |
| 234 |
var sm = context.Compilation.GetSemanticModel(identifierNameSyntax.SyntaxTree); |
| 235 |
var fieldSymbol = sm.GetSymbolInfo(identifierNameSyntax).Symbol as IFieldSymbol; |
| 236 |
EqualsValueClauseSyntax? initializer = fieldSymbol?.DeclaringSyntaxReferences |
| 237 |
.Select(r => r.GetSyntax()) |
| 238 |
.OfType<VariableDeclaratorSyntax>() |
| 239 |
.Select(s => s.Initializer) |
| 240 |
.FirstOrDefault(i => i != null); |
| 241 |
|
| 242 |
if (initializer != null) |
| 243 |
{ |
| 244 |
sm = context.Compilation.GetSemanticModel(initializer.SyntaxTree); |
| 245 |
value = initializer.Value.FullQualifiedSyntax(sm); |
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
else |
| 250 |
{ |
| 251 |
var returns = propertyGet.DescendantNodes().OfType<ReturnStatementSyntax>(); |
| 252 |
if (returns.Count() == 1) |
| 253 |
{ |
| 254 |
|
| 255 |
var returnStatementSyntax = returns.Single(); |
| 256 |
if (returnStatementSyntax.Expression is IdentifierNameSyntax identifierNameSyntax) |
| 257 |
{ |
| 258 |
var sm = context.Compilation.GetSemanticModel(identifierNameSyntax.SyntaxTree); |
| 259 |
var fieldSymbol = sm.GetSymbolInfo(identifierNameSyntax).Symbol as IFieldSymbol; |
| 260 |
EqualsValueClauseSyntax? initializer = fieldSymbol?.DeclaringSyntaxReferences |
| 261 |
.Select(r => r.GetSyntax()) |
| 262 |
.OfType<VariableDeclaratorSyntax>() |
| 263 |
.Select(s => s.Initializer) |
| 264 |
.FirstOrDefault(i => i != null); |
| 265 |
|
| 266 |
if (initializer != null) |
| 267 |
{ |
| 268 |
sm = context.Compilation.GetSemanticModel(initializer.SyntaxTree); |
| 269 |
value = initializer.Value.FullQualifiedSyntax(sm); |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
exportedMembers.Add(new ExportedPropertyMetadata( |
| 279 |
property.Name, marshalType.Value, propertyType, value)); |
| 280 |
} |
| 281 |
|
| 282 |
foreach (var field in exportedFields) |
| 283 |
{ |
| 284 |
if (field.IsStatic) |
| 285 |
{ |
| 286 |
context.ReportDiagnostic(Diagnostic.Create( |
| 287 |
Common.ExportedMemberIsStaticRule, |
| 288 |
field.Locations.FirstLocationWithSourceTreeOrDefault(), |
| 289 |
field.ToDisplayString() |
| 290 |
)); |
| 291 |
continue; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
if (field.IsReadOnly) |
| 297 |
{ |
| 298 |
context.ReportDiagnostic(Diagnostic.Create( |
| 299 |
Common.ExportedMemberIsReadOnlyRule, |
| 300 |
field.Locations.FirstLocationWithSourceTreeOrDefault(), |
| 301 |
field.ToDisplayString() |
| 302 |
)); |
| 303 |
continue; |
| 304 |
} |
| 305 |
|
| 306 |
var fieldType = field.Type; |
| 307 |
var marshalType = MarshalUtils.ConvertManagedTypeToMarshalType(fieldType, typeCache); |
| 308 |
|
| 309 |
if (marshalType == null) |
| 310 |
{ |
| 311 |
context.ReportDiagnostic(Diagnostic.Create( |
| 312 |
Common.ExportedMemberTypeIsNotSupportedRule, |
| 313 |
field.Locations.FirstLocationWithSourceTreeOrDefault(), |
| 314 |
field.ToDisplayString() |
| 315 |
)); |
| 316 |
continue; |
| 317 |
} |
| 318 |
|
| 319 |
if (!isNode && MemberHasNodeType(fieldType, marshalType.Value)) |
| 320 |
{ |
| 321 |
context.ReportDiagnostic(Diagnostic.Create( |
| 322 |
Common.OnlyNodesShouldExportNodesRule, |
| 323 |
field.Locations.FirstLocationWithSourceTreeOrDefault() |
| 324 |
)); |
| 325 |
continue; |
| 326 |
} |
| 327 |
|
| 328 |
EqualsValueClauseSyntax? initializer = field.DeclaringSyntaxReferences |
| 329 |
.Select(r => r.GetSyntax()) |
| 330 |
.OfType<VariableDeclaratorSyntax>() |
| 331 |
.Select(s => s.Initializer) |
| 332 |
.FirstOrDefault(i => i != null); |
| 333 |
|
| 334 |
|
| 335 |
string? value = null; |
| 336 |
if (initializer != null) |
| 337 |
{ |
| 338 |
var sm = context.Compilation.GetSemanticModel(initializer.SyntaxTree); |
| 339 |
var initializerValue = initializer.Value; |
| 340 |
if (!IsStaticallyResolvable(initializerValue, sm)) |
| 341 |
value = "default"; |
| 342 |
else |
| 343 |
value = initializer.Value.FullQualifiedSyntax(sm); |
| 344 |
} |
| 345 |
|
| 346 |
exportedMembers.Add(new ExportedPropertyMetadata( |
| 347 |
field.Name, marshalType.Value, fieldType, value)); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
if (exportedMembers.Count > 0) |
| 353 |
{ |
| 354 |
source.Append("#pragma warning disable CS0109 // Disable warning about redundant 'new' keyword\n"); |
| 355 |
|
| 356 |
const string DictionaryType = |
| 357 |
"global::System.Collections.Generic.Dictionary<global::Godot.StringName, global::Godot.Variant>"; |
| 358 |
|
| 359 |
source.Append("#if TOOLS\n"); |
| 360 |
|
| 361 |
source.Append(" /// <summary>\n") |
| 362 |
.Append(" /// Get the default values for all properties declared in this class.\n") |
| 363 |
.Append(" /// This method is used by Godot to determine the value that will be\n") |
| 364 |
.Append(" /// used by the inspector when resetting properties.\n") |
| 365 |
.Append(" /// Do not call this method.\n") |
| 366 |
.Append(" /// </summary>\n"); |
| 367 |
|
| 368 |
source.Append(" [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]\n"); |
| 369 |
|
| 370 |
source.Append(" internal new static "); |
| 371 |
source.Append(DictionaryType); |
| 372 |
source.Append(" GetGodotPropertyDefaultValues()\n {\n"); |
| 373 |
|
| 374 |
source.Append(" var values = new "); |
| 375 |
source.Append(DictionaryType); |
| 376 |
source.Append("("); |
| 377 |
source.Append(exportedMembers.Count); |
| 378 |
source.Append(");\n"); |
| 379 |
|
| 380 |
foreach (var exportedMember in exportedMembers) |
| 381 |
{ |
| 382 |
string defaultValueLocalName = string.Concat("__", exportedMember.Name, "_default_value"); |
| 383 |
|
| 384 |
source.Append(" "); |
| 385 |
source.Append(exportedMember.TypeSymbol.FullQualifiedNameIncludeGlobal()); |
| 386 |
source.Append(" "); |
| 387 |
source.Append(defaultValueLocalName); |
| 388 |
source.Append(" = "); |
| 389 |
source.Append(exportedMember.Value ?? "default"); |
| 390 |
source.Append(";\n"); |
| 391 |
source.Append(" values.Add(PropertyName.@"); |
| 392 |
source.Append(exportedMember.Name); |
| 393 |
source.Append(", "); |
| 394 |
source.AppendManagedToVariantExpr(defaultValueLocalName, |
| 395 |
exportedMember.TypeSymbol, exportedMember.Type); |
| 396 |
source.Append(");\n"); |
| 397 |
} |
| 398 |
|
| 399 |
source.Append(" return values;\n"); |
| 400 |
source.Append(" }\n"); |
| 401 |
|
| 402 |
source.Append("#endif // TOOLS\n"); |
| 403 |
|
| 404 |
source.Append("#pragma warning restore CS0109\n"); |
| 405 |
} |
| 406 |
|
| 407 |
source.Append("}\n"); |
| 408 |
|
| 409 |
if (isInnerClass) |
| 410 |
{ |
| 411 |
var containingType = symbol.ContainingType; |
| 412 |
|
| 413 |
while (containingType != null) |
| 414 |
{ |
| 415 |
source.Append("}\n"); |
| 416 |
|
| 417 |
containingType = containingType.ContainingType; |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
if (hasNamespace) |
| 422 |
{ |
| 423 |
source.Append("\n}\n"); |
| 424 |
} |
| 425 |
|
| 426 |
context.AddSource(uniqueHint, SourceText.From(source.ToString(), Encoding.UTF8)); |
| 427 |
} |
| 428 |
|
| 429 |
private static bool IsStaticallyResolvable(ExpressionSyntax expression, SemanticModel semanticModel) |
| 430 |
{ |
| 431 |
|
| 432 |
foreach (SyntaxNode descendant in expression.DescendantNodesAndSelf()) |
| 433 |
{ |
| 434 |
|
| 435 |
if (semanticModel.GetConstantValue(descendant).HasValue) |
| 436 |
{ |
| 437 |
continue; |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
SymbolInfo symbolInfo = semanticModel.GetSymbolInfo(descendant); |
| 442 |
if (symbolInfo.Symbol is ISymbol symbol) |
| 443 |
{ |
| 444 |
if (symbol.Kind is SymbolKind.Local or SymbolKind.Parameter) |
| 445 |
{ |
| 446 |
return false; |
| 447 |
} |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
return true; |
| 453 |
} |
| 454 |
|
| 455 |
private static bool MemberHasNodeType(ITypeSymbol memberType, MarshalType marshalType) |
| 456 |
{ |
| 457 |
if (marshalType == MarshalType.GodotObjectOrDerived) |
| 458 |
{ |
| 459 |
return memberType.InheritsFrom("GodotSharp", GodotClasses.Node); |
| 460 |
} |
| 461 |
if (marshalType == MarshalType.GodotObjectOrDerivedArray) |
| 462 |
{ |
| 463 |
var elementType = ((IArrayTypeSymbol)memberType).ElementType; |
| 464 |
return elementType.InheritsFrom("GodotSharp", GodotClasses.Node); |
| 465 |
} |
| 466 |
if (memberType is INamedTypeSymbol { IsGenericType: true } genericType) |
| 467 |
{ |
| 468 |
return genericType.TypeArguments |
| 469 |
.Any(static typeArgument |
| 470 |
=> typeArgument.InheritsFrom("GodotSharp", GodotClasses.Node)); |
| 471 |
} |
| 472 |
|
| 473 |
return false; |
| 474 |
} |
| 475 |
|
| 476 |
private struct ExportedPropertyMetadata |
| 477 |
{ |
| 478 |
public ExportedPropertyMetadata(string name, MarshalType type, ITypeSymbol typeSymbol, string? value) |
| 479 |
{ |
| 480 |
Name = name; |
| 481 |
Type = type; |
| 482 |
TypeSymbol = typeSymbol; |
| 483 |
Value = value; |
| 484 |
} |
| 485 |
|
| 486 |
public string Name { get; } |
| 487 |
public MarshalType Type { get; } |
| 488 |
public ITypeSymbol TypeSymbol { get; } |
| 489 |
public string? Value { get; } |
| 490 |
} |
| 491 |
} |
| 492 |
} |