Skip to content

Commit

Permalink
Merge pull request #69088 from raulsntos/dotnet/global
Browse files Browse the repository at this point in the history
C#: Add `global::` namespace to generated source
  • Loading branch information
neikeq authored Nov 26, 2022
2 parents c7ceb94 + b9e1ca1 commit 50badbd
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static void ReportNonPartialGodotScriptClass(
{
string message =
"Missing partial modifier on declaration of type '" +
$"{symbol.FullQualifiedName()}' which is a subclass of '{GodotClasses.Object}'";
$"{symbol.FullQualifiedNameOmitGlobal()}' which is a subclass of '{GodotClasses.Object}'";

string description = $"{message}. Subclasses of '{GodotClasses.Object}' " +
"must be declared with the partial modifier.";
Expand All @@ -41,7 +41,7 @@ TypeDeclarationSyntax outerTypeDeclSyntax
.GetDeclaredSymbol(outerTypeDeclSyntax);

string fullQualifiedName = outerSymbol is INamedTypeSymbol namedTypeSymbol ?
namedTypeSymbol.FullQualifiedName() :
namedTypeSymbol.FullQualifiedNameOmitGlobal() :
"type not found";

string message =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,39 +149,46 @@ public static string GetDeclarationKeyword(this INamedTypeSymbol namedTypeSymbol
};
}

private static SymbolDisplayFormat FullyQualifiedFormatOmitGlobal { get; } =
SymbolDisplayFormat.FullyQualifiedFormat
.WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted);

public static string FullQualifiedName(this ITypeSymbol symbol)
=> symbol.ToDisplayString(NullableFlowState.NotNull, FullyQualifiedFormatOmitGlobal);

public static string NameWithTypeParameters(this INamedTypeSymbol symbol)
{
return symbol.IsGenericType ?
string.Concat(symbol.Name, "<", string.Join(", ", symbol.TypeParameters), ">") :
symbol.Name;
}

public static string FullQualifiedName(this INamespaceSymbol namespaceSymbol)
private static SymbolDisplayFormat FullyQualifiedFormatOmitGlobal { get; } =
SymbolDisplayFormat.FullyQualifiedFormat
.WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted);

private static SymbolDisplayFormat FullyQualifiedFormatIncludeGlobal { get; } =
SymbolDisplayFormat.FullyQualifiedFormat
.WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Included);

public static string FullQualifiedNameOmitGlobal(this ITypeSymbol symbol)
=> symbol.ToDisplayString(NullableFlowState.NotNull, FullyQualifiedFormatOmitGlobal);

public static string FullQualifiedNameOmitGlobal(this INamespaceSymbol namespaceSymbol)
=> namespaceSymbol.ToDisplayString(FullyQualifiedFormatOmitGlobal);

public static string FullQualifiedName(this ISymbol symbol)
=> symbol.ToDisplayString(FullyQualifiedFormatOmitGlobal);
public static string FullQualifiedNameIncludeGlobal(this ITypeSymbol symbol)
=> symbol.ToDisplayString(NullableFlowState.NotNull, FullyQualifiedFormatIncludeGlobal);

public static string FullQualifiedNameIncludeGlobal(this INamespaceSymbol namespaceSymbol)
=> namespaceSymbol.ToDisplayString(FullyQualifiedFormatIncludeGlobal);

public static string FullQualifiedSyntax(this SyntaxNode node, SemanticModel sm)
{
StringBuilder sb = new();
FullQualifiedSyntax_(node, sm, sb, true);
FullQualifiedSyntax(node, sm, sb, true);
return sb.ToString();
}

private static void FullQualifiedSyntax_(SyntaxNode node, SemanticModel sm, StringBuilder sb, bool isFirstNode)
private static void FullQualifiedSyntax(SyntaxNode node, SemanticModel sm, StringBuilder sb, bool isFirstNode)
{
if (node is NameSyntax ns && isFirstNode)
{
SymbolInfo nameInfo = sm.GetSymbolInfo(ns);
sb.Append(nameInfo.Symbol?.FullQualifiedName() ?? ns.ToString());
sb.Append(nameInfo.Symbol?.ToDisplayString(FullyQualifiedFormatIncludeGlobal) ?? ns.ToString());
return;
}

Expand All @@ -195,7 +202,7 @@ private static void FullQualifiedSyntax_(SyntaxNode node, SemanticModel sm, Stri

if (child.IsNode)
{
FullQualifiedSyntax_(child.AsNode()!, sm, sb, isFirstNode: innerIsFirstNode);
FullQualifiedSyntax(child.AsNode()!, sm, sb, isFirstNode: innerIsFirstNode);
innerIsFirstNode = false;
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ INamedTypeSymbol GetTypeByMetadataNameOrThrow(string fullyQualifiedMetadataName)
_ => null
};
case "Collections"
when type.ContainingNamespace?.FullQualifiedName() == "Godot.Collections":
when type.ContainingNamespace?.FullQualifiedNameOmitGlobal() == "Godot.Collections":
return type switch
{
{ Name: "Dictionary" } =>
Expand Down Expand Up @@ -367,7 +367,7 @@ public static StringBuilder AppendNativeVariantToManagedExpr(this StringBuilder
MarshalType.SignalInfo =>
source.Append(VariantUtils, ".ConvertToSignalInfo(", inputExpr, ")"),
MarshalType.Enum =>
source.Append("(", typeSymbol.FullQualifiedName(),
source.Append("(", typeSymbol.FullQualifiedNameIncludeGlobal(),
")", VariantUtils, ".ConvertToInt32(", inputExpr, ")"),
MarshalType.ByteArray =>
source.Append(VariantUtils, ".ConvertAsPackedByteArrayToSystemArray(", inputExpr, ")"),
Expand All @@ -389,7 +389,7 @@ public static StringBuilder AppendNativeVariantToManagedExpr(this StringBuilder
source.Append(VariantUtils, ".ConvertAsPackedColorArrayToSystemArray(", inputExpr, ")"),
MarshalType.GodotObjectOrDerivedArray =>
source.Append(VariantUtils, ".ConvertToSystemArrayOfGodotObject<",
((IArrayTypeSymbol)typeSymbol).ElementType.FullQualifiedName(), ">(", inputExpr, ")"),
((IArrayTypeSymbol)typeSymbol).ElementType.FullQualifiedNameIncludeGlobal(), ">(", inputExpr, ")"),
MarshalType.SystemArrayOfStringName =>
source.Append(VariantUtils, ".ConvertToSystemArrayOfStringName(", inputExpr, ")"),
MarshalType.SystemArrayOfNodePath =>
Expand All @@ -399,7 +399,7 @@ public static StringBuilder AppendNativeVariantToManagedExpr(this StringBuilder
MarshalType.Variant =>
source.Append("global::Godot.Variant.CreateCopyingBorrowed(", inputExpr, ")"),
MarshalType.GodotObjectOrDerived =>
source.Append("(", typeSymbol.FullQualifiedName(),
source.Append("(", typeSymbol.FullQualifiedNameIncludeGlobal(),
")", VariantUtils, ".ConvertToGodotObject(", inputExpr, ")"),
MarshalType.StringName =>
source.Append(VariantUtils, ".ConvertToStringNameObject(", inputExpr, ")"),
Expand All @@ -413,11 +413,11 @@ public static StringBuilder AppendNativeVariantToManagedExpr(this StringBuilder
source.Append(VariantUtils, ".ConvertToArrayObject(", inputExpr, ")"),
MarshalType.GodotGenericDictionary =>
source.Append(VariantUtils, ".ConvertToDictionaryObject<",
((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedName(), ", ",
((INamedTypeSymbol)typeSymbol).TypeArguments[1].FullQualifiedName(), ">(", inputExpr, ")"),
((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedNameIncludeGlobal(), ", ",
((INamedTypeSymbol)typeSymbol).TypeArguments[1].FullQualifiedNameIncludeGlobal(), ">(", inputExpr, ")"),
MarshalType.GodotGenericArray =>
source.Append(VariantUtils, ".ConvertToArrayObject<",
((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedName(), ">(", inputExpr, ")"),
((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedNameIncludeGlobal(), ">(", inputExpr, ")"),
_ => throw new ArgumentOutOfRangeException(nameof(marshalType), marshalType,
"Received unexpected marshal type")
};
Expand Down Expand Up @@ -578,7 +578,7 @@ public static StringBuilder AppendVariantToManagedExpr(this StringBuilder source
MarshalType.Callable => source.Append(inputExpr, ".AsCallable()"),
MarshalType.SignalInfo => source.Append(inputExpr, ".AsSignalInfo()"),
MarshalType.Enum =>
source.Append("(", typeSymbol.FullQualifiedName(), ")", inputExpr, ".AsInt64()"),
source.Append("(", typeSymbol.FullQualifiedNameIncludeGlobal(), ")", inputExpr, ".AsInt64()"),
MarshalType.ByteArray => source.Append(inputExpr, ".AsByteArray()"),
MarshalType.Int32Array => source.Append(inputExpr, ".AsInt32Array()"),
MarshalType.Int64Array => source.Append(inputExpr, ".AsInt64Array()"),
Expand All @@ -589,23 +589,23 @@ public static StringBuilder AppendVariantToManagedExpr(this StringBuilder source
MarshalType.Vector3Array => source.Append(inputExpr, ".AsVector3Array()"),
MarshalType.ColorArray => source.Append(inputExpr, ".AsColorArray()"),
MarshalType.GodotObjectOrDerivedArray => source.Append(inputExpr, ".AsGodotObjectArray<",
((IArrayTypeSymbol)typeSymbol).ElementType.FullQualifiedName(), ">()"),
((IArrayTypeSymbol)typeSymbol).ElementType.FullQualifiedNameIncludeGlobal(), ">()"),
MarshalType.SystemArrayOfStringName => source.Append(inputExpr, ".AsSystemArrayOfStringName()"),
MarshalType.SystemArrayOfNodePath => source.Append(inputExpr, ".AsSystemArrayOfNodePath()"),
MarshalType.SystemArrayOfRID => source.Append(inputExpr, ".AsSystemArrayOfRID()"),
MarshalType.Variant => source.Append(inputExpr),
MarshalType.GodotObjectOrDerived => source.Append("(",
typeSymbol.FullQualifiedName(), ")", inputExpr, ".AsGodotObject()"),
typeSymbol.FullQualifiedNameIncludeGlobal(), ")", inputExpr, ".AsGodotObject()"),
MarshalType.StringName => source.Append(inputExpr, ".AsStringName()"),
MarshalType.NodePath => source.Append(inputExpr, ".AsNodePath()"),
MarshalType.RID => source.Append(inputExpr, ".AsRID()"),
MarshalType.GodotDictionary => source.Append(inputExpr, ".AsGodotDictionary()"),
MarshalType.GodotArray => source.Append(inputExpr, ".AsGodotArray()"),
MarshalType.GodotGenericDictionary => source.Append(inputExpr, ".AsGodotDictionary<",
((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedName(), ", ",
((INamedTypeSymbol)typeSymbol).TypeArguments[1].FullQualifiedName(), ">()"),
((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedNameIncludeGlobal(), ", ",
((INamedTypeSymbol)typeSymbol).TypeArguments[1].FullQualifiedNameIncludeGlobal(), ">()"),
MarshalType.GodotGenericArray => source.Append(inputExpr, ".AsGodotArray<",
((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedName(), ">()"),
((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedNameIncludeGlobal(), ">()"),
_ => throw new ArgumentOutOfRangeException(nameof(marshalType), marshalType,
"Received unexpected marshal type")
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ INamedTypeSymbol symbol
{
INamespaceSymbol namespaceSymbol = symbol.ContainingNamespace;
string classNs = namespaceSymbol != null && !namespaceSymbol.IsGlobalNamespace ?
namespaceSymbol.FullQualifiedName() :
namespaceSymbol.FullQualifiedNameOmitGlobal() :
string.Empty;
bool hasNamespace = classNs.Length != 0;

bool isInnerClass = symbol.ContainingType != null;

string uniqueHint = symbol.FullQualifiedName().SanitizeQualifiedNameForUniqueHint()
string uniqueHint = symbol.FullQualifiedNameOmitGlobal().SanitizeQualifiedNameForUniqueHint()
+ "_ScriptMethods.generated";

var source = new StringBuilder();
Expand Down Expand Up @@ -135,7 +135,7 @@ INamedTypeSymbol symbol

source.Append("#pragma warning disable CS0109 // Disable warning about redundant 'new' keyword\n");

source.Append($" public new class MethodName : {symbol.BaseType.FullQualifiedName()}.MethodName {{\n");
source.Append($" public new class MethodName : {symbol.BaseType.FullQualifiedNameIncludeGlobal()}.MethodName {{\n");

// Generate cached StringNames for methods and properties, for fast lookup

Expand All @@ -146,7 +146,7 @@ INamedTypeSymbol symbol

foreach (string methodName in distinctMethodNames)
{
source.Append(" public new static readonly StringName ");
source.Append(" public new static readonly global::Godot.StringName ");
source.Append(methodName);
source.Append(" = \"");
source.Append(methodName);
Expand All @@ -159,7 +159,7 @@ INamedTypeSymbol symbol

if (godotClassMethods.Length > 0)
{
const string listType = "System.Collections.Generic.List<global::Godot.Bridge.MethodInfo>";
const string listType = "global::System.Collections.Generic.List<global::Godot.Bridge.MethodInfo>";

source.Append(" internal new static ")
.Append(listType)
Expand Down Expand Up @@ -248,7 +248,7 @@ private static void AppendMethodInfo(StringBuilder source, MethodInfo methodInfo

AppendPropertyInfo(source, methodInfo.ReturnVal);

source.Append(", flags: (Godot.MethodFlags)")
source.Append(", flags: (global::Godot.MethodFlags)")
.Append((int)methodInfo.Flags)
.Append(", arguments: ");

Expand Down Expand Up @@ -276,15 +276,15 @@ private static void AppendMethodInfo(StringBuilder source, MethodInfo methodInfo

private static void AppendPropertyInfo(StringBuilder source, PropertyInfo propertyInfo)
{
source.Append("new(type: (Godot.Variant.Type)")
source.Append("new(type: (global::Godot.Variant.Type)")
.Append((int)propertyInfo.Type)
.Append(", name: \"")
.Append(propertyInfo.Name)
.Append("\", hint: (Godot.PropertyHint)")
.Append("\", hint: (global::Godot.PropertyHint)")
.Append((int)propertyInfo.Hint)
.Append(", hintString: \"")
.Append(propertyInfo.HintString)
.Append("\", usage: (Godot.PropertyUsageFlags)")
.Append("\", usage: (global::Godot.PropertyUsageFlags)")
.Append((int)propertyInfo.Usage)
.Append(", exported: ")
.Append(propertyInfo.Exported ? "true" : "false")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ IEnumerable<ClassDeclarationSyntax> classDeclarations

INamespaceSymbol namespaceSymbol = symbol.ContainingNamespace;
string classNs = namespaceSymbol != null && !namespaceSymbol.IsGlobalNamespace ?
namespaceSymbol.FullQualifiedName() :
namespaceSymbol.FullQualifiedNameOmitGlobal() :
string.Empty;
bool hasNamespace = classNs.Length != 0;

string uniqueHint = symbol.FullQualifiedName().SanitizeQualifiedNameForUniqueHint()
string uniqueHint = symbol.FullQualifiedNameOmitGlobal().SanitizeQualifiedNameForUniqueHint()
+ "_ScriptPath.generated";

var source = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ INamedTypeSymbol symbol
{
INamespaceSymbol namespaceSymbol = symbol.ContainingNamespace;
string classNs = namespaceSymbol != null && !namespaceSymbol.IsGlobalNamespace ?
namespaceSymbol.FullQualifiedName() :
namespaceSymbol.FullQualifiedNameOmitGlobal() :
string.Empty;
bool hasNamespace = classNs.Length != 0;

bool isInnerClass = symbol.ContainingType != null;

string uniqueHint = symbol.FullQualifiedName().SanitizeQualifiedNameForUniqueHint()
string uniqueHint = symbol.FullQualifiedNameOmitGlobal().SanitizeQualifiedNameForUniqueHint()
+ "_ScriptProperties.generated";

var source = new StringBuilder();
Expand Down Expand Up @@ -124,14 +124,14 @@ INamedTypeSymbol symbol

source.Append("#pragma warning disable CS0109 // Disable warning about redundant 'new' keyword\n");

source.Append($" public new class PropertyName : {symbol.BaseType.FullQualifiedName()}.PropertyName {{\n");
source.Append($" public new class PropertyName : {symbol.BaseType.FullQualifiedNameIncludeGlobal()}.PropertyName {{\n");

// Generate cached StringNames for methods and properties, for fast lookup

foreach (var property in godotClassProperties)
{
string propertyName = property.PropertySymbol.Name;
source.Append(" public new static readonly StringName ");
source.Append(" public new static readonly global::Godot.StringName ");
source.Append(propertyName);
source.Append(" = \"");
source.Append(propertyName);
Expand All @@ -141,7 +141,7 @@ INamedTypeSymbol symbol
foreach (var field in godotClassFields)
{
string fieldName = field.FieldSymbol.Name;
source.Append(" public new static readonly StringName ");
source.Append(" public new static readonly global::Godot.StringName ");
source.Append(fieldName);
source.Append(" = \"");
source.Append(fieldName);
Expand Down Expand Up @@ -216,7 +216,7 @@ INamedTypeSymbol symbol

// Generate GetGodotPropertyList

string dictionaryType = "System.Collections.Generic.List<global::Godot.Bridge.PropertyInfo>";
string dictionaryType = "global::System.Collections.Generic.List<global::Godot.Bridge.PropertyInfo>";

source.Append(" internal new static ")
.Append(dictionaryType)
Expand Down Expand Up @@ -340,15 +340,15 @@ private static void AppendGroupingPropertyInfo(StringBuilder source, PropertyInf

private static void AppendPropertyInfo(StringBuilder source, PropertyInfo propertyInfo)
{
source.Append(" properties.Add(new(type: (Godot.Variant.Type)")
source.Append(" properties.Add(new(type: (global::Godot.Variant.Type)")
.Append((int)propertyInfo.Type)
.Append(", name: PropertyName.")
.Append(propertyInfo.Name)
.Append(", hint: (Godot.PropertyHint)")
.Append(", hint: (global::Godot.PropertyHint)")
.Append((int)propertyInfo.Hint)
.Append(", hintString: \"")
.Append(propertyInfo.HintString)
.Append("\", usage: (Godot.PropertyUsageFlags)")
.Append("\", usage: (global::Godot.PropertyUsageFlags)")
.Append((int)propertyInfo.Usage)
.Append(", exported: ")
.Append(propertyInfo.Exported ? "true" : "false")
Expand Down
Loading

0 comments on commit 50badbd

Please sign in to comment.