Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public static string CreateValidIdentifier (string identifier, bool useEncodedRe
return validIdentifier.Replace (normalizedIdentifier, "_");
}

public static bool IsValidIdentifier (string identifier)
{
return !validIdentifier.IsMatch (identifier);
}

// Makes uglier but unique identifiers by encoding each invalid character with its character value
static string EncodeReplacement (Match match) => $"_x{(ushort) match.Value [0]}_";
}
Expand Down
24 changes: 17 additions & 7 deletions tools/generator/CodeGenerationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.CSharp;

using Java.Interop.Tools.JavaCallableWrappers;

using Xamarin.Android.Binder;
using Xamarin.AndroidTools.AnnotationSupport;

Expand Down Expand Up @@ -97,21 +99,29 @@ public string GetOutputName (string s)
return GetOutputName (s.Substring (0, idx)) + '<' + String.Join (", ", typeParams.ToArray ()) + '>';
}

CSharpCodeProvider code_provider = new CSharpCodeProvider ();

public string GetSafeIdentifier (string name)
{
if (string.IsNullOrEmpty (name))
return name;

// NOTE: "partial" differs in behavior on macOS vs. Windows, Windows reports "partial" as a valid identifier
// This check ensures the same output on both platforms
if (name == "partial")
return name;
switch (name) {
case "partial": return name;
// `this` isn't in TypeNameUtilities.reserved_keywords; special-case.
case "this": return "this_";
}

// In the ideal world, it should not be applied twice.
// Sadly that is not true in reality, so we need to exclude non-symbols
// when replacing the argument name with a valid identifier.
// (ReturnValue.ToNative() takes an argument which could be either an expression or mere symbol.)
if (name.LastOrDefault () != ')' && !name.Contains ('.'))
name = code_provider.IsValidIdentifier (name) ? name : name + '_';
if (name [name.Length-1] != ')' && !name.Contains ('.') && !name.StartsWith ("@")) {
if (!IdentifierValidator.IsValidIdentifier (name) ||
Array.BinarySearch (TypeNameUtilities.reserved_keywords, name) >= 0) {
name = name + "_";
}
}
return name.Replace ('$', '_');
}

Expand Down
2 changes: 1 addition & 1 deletion tools/generator/Utilities/TypeNameUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static class TypeNameUtilities
{
// These must be sorted for BinarySearch to work
// Missing "this" because it's handled elsewhere as "this_"
static string [] reserved_keywords = new [] {
internal static string [] reserved_keywords = new [] {
"abstract", "as", "base", "bool", "break", "byte", "callback", "case", "catch", "char", "checked", "class", "const",
"continue", "decimal", "default", "delegate", "do", "double", "else", "enum", "event", "explicit", "extern", "false",
"finally", "fixed", "float", "for", "foreach", "goto", "if", "implicit", "in", "int", "interface", "internal",
Expand Down
2 changes: 1 addition & 1 deletion tools/generator/generator.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<TargetFrameworks>net472;netcoreapp3.1</TargetFrameworks>
<OutputType>Exe</OutputType>
<DefineConstants>$(DefineConstants);GENERATOR;HAVE_CECIL;JCW_ONLY_TYPE_NAMES</DefineConstants>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
Expand Down