Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed codegen type name overlap by displaying global typenames #560

Merged
merged 1 commit into from
May 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 20 additions & 10 deletions api/AltV.Net.Async.CodeGen/AsyncEntityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@

namespace AltV.Net.Async.CodeGen
{
internal static class TypeSymbolExtensions
{
internal static string GetGlobalName(this ITypeSymbol symbol)
{
return symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
}
}

internal class SyntaxTreeReceiver : ISyntaxReceiver
{
public readonly List<ClassDeclarationSyntax> Classes = new();
Expand All @@ -35,7 +43,7 @@ private static string Indent(string text, int n = 1)

private static string FormatAttributes(IEnumerable<AttributeData> attributes)
{
return string.Join(" ", attributes.Select(a => $"[{a}]"));
return string.Join(" ", attributes.Select(a => $"[global::{a}]"));
}

private const string AttributeText = @"
Expand Down Expand Up @@ -216,7 +224,7 @@ public void Execute(GeneratorExecutionContext context)
propertyValue += $"\n set {setter} ";
else if (property.SetMethod is not null)
propertyValue +=
@$"init => throw new System.MemberAccessException(""Manual construction of Async class is prohibited. Property {property.Name} is marked as init-only and therefore cannot be set on the async entity.""); ";
@$"init => throw new global::System.MemberAccessException(""Manual construction of Async class is prohibited. Property {property.Name} is marked as init-only and therefore cannot be set on the async entity.""); ";

var attributes = classProperty.GetAttributes().Where(a => !a.Equals(propertyAttribute)).ToArray();
var formattedAttributes =
Expand All @@ -227,7 +235,7 @@ public void Execute(GeneratorExecutionContext context)
? "\n"
: "";
members.Add(formattedAttributes +
$"public {@new}{property.Type} {member.Name} {{ {propertyValue}{newline}}}");
$"public {@new}{property.Type.GetGlobalName()} {member.Name} {{ {propertyValue}{newline}}}");
}

break;
Expand Down Expand Up @@ -258,18 +266,20 @@ public void Execute(GeneratorExecutionContext context)

var parameterAttrs = methodParameter.GetAttributes();
// ToString of methodParameter gives ref/out/in and type, without a name
argumentsList[index] =
(parameterAttrs.Length == 0
? ""
: FormatAttributes(methodParameter.GetAttributes()) + " ") + methodParameter +
" " + methodParameter.Name;

var modifier = methodParameter.RefKind switch
{
RefKind.Out => "out ",
RefKind.Ref => "ref ",
RefKind.In => "in ",
_ => ""
};

argumentsList[index] =
(parameterAttrs.Length == 0
? ""
: FormatAttributes(methodParameter.GetAttributes()) + " ") + modifier + methodParameter.Type.GetGlobalName() +
" " + methodParameter.Name;
callArgumentsList[index] = modifier + methodParameter.Name;
}

Expand All @@ -281,7 +291,7 @@ public void Execute(GeneratorExecutionContext context)
if (typeParameter.HasValueTypeConstraint) constraintDeclarations.Add("struct");
if (typeParameter.HasUnmanagedTypeConstraint) constraintDeclarations.Add("unmanaged");
if (typeParameter.HasNotNullConstraint) constraintDeclarations.Add("notnull");
constraintDeclarations.AddRange(typeParameter.ConstraintTypes.Select(e => e.ToString()));
constraintDeclarations.AddRange(typeParameter.ConstraintTypes.Select(e => e.GetGlobalName()));
if (typeParameter.HasConstructorConstraint) constraintDeclarations.Add("new()");

if (constraintDeclarations.Count == 0) continue;
Expand Down Expand Up @@ -323,7 +333,7 @@ public void Execute(GeneratorExecutionContext context)
}

members.Add(formattedAttributes +
$"public {@new}{classMethod.ReturnType} {name}{genericTypes}({arguments}) {constraintClauses}\n{{\n{Indent(methodValue)}\n}}");
$"public {@new}{classMethod.ReturnType.GetGlobalName()} {name}{genericTypes}({arguments}) {constraintClauses}\n{{\n{Indent(methodValue)}\n}}");

break;
}
Expand Down