Browse Source

Applied Helpers.SafeIdentifier in the name-cleaning pass in order to get correct names in type maps.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/187/head
Dimitar Dobrev 12 years ago
parent
commit
85e56de487
  1. 1
      src/Generator/Driver.cs
  2. 26
      src/Generator/Generators/CLI/CLIHeadersTemplate.cs
  3. 8
      src/Generator/Generators/CLI/CLISourcesTemplate.cs
  4. 6
      src/Generator/Generators/CLI/CLITextTemplate.cs
  5. 19
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  6. 78
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  7. 2
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  8. 13
      src/Generator/Passes/CleanInvalidDeclNamesPass.cs

1
src/Generator/Driver.cs

@ -248,7 +248,6 @@ namespace CppSharp
TranslationUnitPasses.AddPass(new CleanUnitPass(Options)); TranslationUnitPasses.AddPass(new CleanUnitPass(Options));
TranslationUnitPasses.AddPass(new SortDeclarationsPass()); TranslationUnitPasses.AddPass(new SortDeclarationsPass());
TranslationUnitPasses.AddPass(new ResolveIncompleteDeclsPass()); TranslationUnitPasses.AddPass(new ResolveIncompleteDeclsPass());
TranslationUnitPasses.AddPass(new CleanInvalidDeclNamesPass());
TranslationUnitPasses.AddPass(new CheckIgnoredDeclsPass()); TranslationUnitPasses.AddPass(new CheckIgnoredDeclsPass());
if (Options.IsCSharpGenerator) if (Options.IsCSharpGenerator)

26
src/Generator/Generators/CLI/CLIHeadersTemplate.cs

@ -190,7 +190,7 @@ namespace CppSharp.Generators.CLI
PushBlock(CLIBlockKind.Namespace, @namespace); PushBlock(CLIBlockKind.Namespace, @namespace);
WriteLine("namespace {0}", isTopLevel WriteLine("namespace {0}", isTopLevel
? Options.OutputNamespace ? Options.OutputNamespace
: SafeIdentifier(@namespace.Name)); : @namespace.Name);
WriteStartBraceIndent(); WriteStartBraceIndent();
} }
@ -218,7 +218,7 @@ namespace CppSharp.Generators.CLI
{ {
PushBlock(CLIBlockKind.FunctionsClass); PushBlock(CLIBlockKind.FunctionsClass);
WriteLine("public ref class {0}{1}", SafeIdentifier(Options.OutputNamespace), WriteLine("public ref class {0}{1}", Options.OutputNamespace,
TranslationUnit.FileNameWithoutExtension); TranslationUnit.FileNameWithoutExtension);
WriteLine("{"); WriteLine("{");
WriteLine("public:"); WriteLine("public:");
@ -348,7 +348,7 @@ namespace CppSharp.Generators.CLI
NewLine(); NewLine();
WriteLine("{0} {1}({2});", retType, SafeIdentifier(function.Name), WriteLine("{0} {1}({2});", retType, function.Name,
GenerateParametersList(function.Parameters)); GenerateParametersList(function.Parameters));
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
@ -363,8 +363,8 @@ namespace CppSharp.Generators.CLI
PushIndent(); PushIndent();
// Output a default constructor that takes the native pointer. // Output a default constructor that takes the native pointer.
WriteLine("{0}({1} native);", SafeIdentifier(@class.Name), nativeType); WriteLine("{0}({1} native);", @class.Name, nativeType);
WriteLine("{0}({1} native);", SafeIdentifier(@class.Name), "System::IntPtr"); WriteLine("{0}({1} native);", @class.Name, "System::IntPtr");
foreach (var ctor in @class.Constructors) foreach (var ctor in @class.Constructors)
{ {
@ -441,7 +441,7 @@ namespace CppSharp.Generators.CLI
if (@class.IsUnion) if (@class.IsUnion)
WriteLine("[System::Runtime::InteropServices::FieldOffset({0})]", WriteLine("[System::Runtime::InteropServices::FieldOffset({0})]",
field.Offset); field.Offset);
WriteLine("{0} {1};", field.Type, SafeIdentifier(field.Name)); WriteLine("{0} {1};", field.Type, field.Name);
PopBlock(NewLineKind.Never); PopBlock(NewLineKind.Never);
} }
@ -558,7 +558,7 @@ namespace CppSharp.Generators.CLI
Write(@class.IsValueType ? "value struct " : "ref class "); Write(@class.IsValueType ? "value struct " : "ref class ");
Write("{0}", SafeIdentifier(@class.Name)); Write("{0}", @class.Name);
if (@class.IsOpaque) if (@class.IsOpaque)
{ {
@ -652,7 +652,7 @@ namespace CppSharp.Generators.CLI
method.OperatorKind == CXXOperatorKind.Conversion) method.OperatorKind == CXXOperatorKind.Conversion)
Write("{0}(", GetMethodName(method)); Write("{0}(", GetMethodName(method));
else else
Write("{0} {1}(", method.ReturnType, SafeIdentifier(method.Name)); Write("{0} {1}(", method.ReturnType, method.Name);
GenerateMethodParameters(method); GenerateMethodParameters(method);
@ -682,7 +682,7 @@ namespace CppSharp.Generators.CLI
WriteLine("{0}{1};", WriteLine("{0}{1};",
!insideClass ? "public " : "", !insideClass ? "public " : "",
string.Format(TypePrinter.VisitDelegate(function), string.Format(TypePrinter.VisitDelegate(function),
SafeIdentifier(typedef.Name))); typedef.Name));
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
return true; return true;
@ -701,7 +701,7 @@ namespace CppSharp.Generators.CLI
GenerateDeclarationCommon(function); GenerateDeclarationCommon(function);
var retType = function.ReturnType.ToString(); var retType = function.ReturnType.ToString();
Write("static {0} {1}(", retType, SafeIdentifier(function.Name)); Write("static {0} {1}(", retType, function.Name);
Write(GenerateParametersList(function.Parameters)); Write(GenerateParametersList(function.Parameters));
@ -727,7 +727,7 @@ namespace CppSharp.Generators.CLI
if (@enum.Namespace is Namespace) if (@enum.Namespace is Namespace)
Write("public "); Write("public ");
Write("enum struct {0}", SafeIdentifier(@enum.Name)); Write("enum struct {0}", @enum.Name);
var typeName = TypePrinter.VisitPrimitiveType(@enum.BuiltinType.Type, var typeName = TypePrinter.VisitPrimitiveType(@enum.BuiltinType.Type,
new TypeQualifiers()); new TypeQualifiers());
@ -746,10 +746,10 @@ namespace CppSharp.Generators.CLI
GenerateInlineSummary(item.Comment); GenerateInlineSummary(item.Comment);
if (item.ExplicitValue) if (item.ExplicitValue)
Write(String.Format("{0} = {1}", SafeIdentifier(item.Name), Write(String.Format("{0} = {1}", item.Name,
@enum.GetItemValueAsString(item))); @enum.GetItemValueAsString(item)));
else else
Write(String.Format("{0}", SafeIdentifier(item.Name))); Write(String.Format("{0}", item.Name));
if (item != @enum.Items.Last()) if (item != @enum.Items.Last())
WriteLine(","); WriteLine(",");

8
src/Generator/Generators/CLI/CLISourcesTemplate.cs

@ -278,7 +278,7 @@ namespace CppSharp.Generators.CLI
WriteLine("generic<{0}>", typeNames); WriteLine("generic<{0}>", typeNames);
WriteLine("{0} {1}::{2}({3})", retType, WriteLine("{0} {1}::{2}({3})", retType,
QualifiedIdentifier(function.Namespace), SafeIdentifier(function.Name), QualifiedIdentifier(function.Namespace), function.Name,
GenerateParametersList(function.Parameters)); GenerateParametersList(function.Parameters));
WriteStartBraceIndent(); WriteStartBraceIndent();
@ -553,7 +553,7 @@ namespace CppSharp.Generators.CLI
private void GenerateClassConstructor(Class @class, bool isIntPtr) private void GenerateClassConstructor(Class @class, bool isIntPtr)
{ {
Write("{0}::{1}(", QualifiedIdentifier(@class), SafeIdentifier(@class.Name)); Write("{0}::{1}(", QualifiedIdentifier(@class), @class.Name);
var nativeType = string.Format("::{0}*", @class.QualifiedOriginalName); var nativeType = string.Format("::{0}*", @class.QualifiedOriginalName);
WriteLine("{0} native)", isIntPtr ? "System::IntPtr" : nativeType); WriteLine("{0} native)", isIntPtr ? "System::IntPtr" : nativeType);
@ -661,7 +661,7 @@ namespace CppSharp.Generators.CLI
Write("{0}::{1}(", QualifiedIdentifier(@class), GetMethodName(method)); Write("{0}::{1}(", QualifiedIdentifier(@class), GetMethodName(method));
else else
Write("{0} {1}::{2}(", method.ReturnType, QualifiedIdentifier(@class), Write("{0} {1}::{2}(", method.ReturnType, QualifiedIdentifier(@class),
SafeIdentifier(method.Name)); method.Name);
GenerateMethodParameters(method); GenerateMethodParameters(method);
@ -784,7 +784,7 @@ namespace CppSharp.Generators.CLI
Options.OutputNamespace, TranslationUnit.FileNameWithoutExtension); Options.OutputNamespace, TranslationUnit.FileNameWithoutExtension);
Write("{0} {1}::{2}(", function.ReturnType, classSig, Write("{0} {1}::{2}(", function.ReturnType, classSig,
SafeIdentifier(function.Name)); function.Name);
for (var i = 0; i < function.Parameters.Count; ++i) for (var i = 0; i < function.Parameters.Count; ++i)
{ {

6
src/Generator/Generators/CLI/CLITextTemplate.cs

@ -89,15 +89,15 @@ namespace CppSharp.Generators.CLI
public string GetMethodName(Method method) public string GetMethodName(Method method)
{ {
if (method.OperatorKind == CXXOperatorKind.Conversion) if (method.OperatorKind == CXXOperatorKind.Conversion)
return SafeIdentifier("operator " + method.ConversionType); return "operator " + method.ConversionType;
if (method.IsConstructor || method.IsDestructor) if (method.IsConstructor || method.IsDestructor)
{ {
var @class = (Class) method.Namespace; var @class = (Class) method.Namespace;
return SafeIdentifier(@class.Name); return @class.Name;
} }
return SafeIdentifier(method.Name); return method.Name;
} }
public void GenerateDeclarationCommon(Declaration decl) public void GenerateDeclarationCommon(Declaration decl)

19
src/Generator/Generators/CSharp/CSharpMarshal.cs

@ -418,10 +418,8 @@ namespace CppSharp.Generators.CSharp
pointee.IsPrimitiveType(PrimitiveType.WideChar)) && pointee.IsPrimitiveType(PrimitiveType.WideChar)) &&
pointer.QualifiedPointee.Qualifiers.IsConst) pointer.QualifiedPointee.Qualifiers.IsConst)
{ {
Context.Return.Write(MarshalStringToUnmanaged( Context.Return.Write(MarshalStringToUnmanaged(Context.Parameter.Name));
Helpers.SafeIdentifier(Context.Parameter.Name))); CSharpContext.Cleanup.WriteLine("Marshal.FreeHGlobal({0});", Context.ArgName);
CSharpContext.Cleanup.WriteLine("Marshal.FreeHGlobal({0});",
Helpers.SafeIdentifier(Context.ArgName));
return true; return true;
} }
@ -443,7 +441,7 @@ namespace CppSharp.Generators.CSharp
{ {
Context.SupportBefore.WriteLine("var {0} = {1}.ToInternal();", Context.SupportBefore.WriteLine("var {0} = {1}.ToInternal();",
Generator.GeneratedIdentifier(Context.ArgName), Generator.GeneratedIdentifier(Context.ArgName),
Helpers.SafeIdentifier(Context.Parameter.Name)); Context.Parameter.Name);
} }
Context.Return.Write("new global::System.IntPtr(&{0})", Context.Return.Write("new global::System.IntPtr(&{0})",
@ -464,8 +462,7 @@ namespace CppSharp.Generators.CSharp
{ {
var typeName = Type.TypePrinterDelegate(pointee); var typeName = Type.TypePrinterDelegate(pointee);
Context.SupportBefore.WriteLine("{0} _{1};", typeName, Context.SupportBefore.WriteLine("{0} _{1};", typeName, param.Name);
Helpers.SafeIdentifier(param.Name));
Context.Return.Write("&_{0}", param.Name); Context.Return.Write("&_{0}", param.Name);
} }
@ -549,7 +546,7 @@ namespace CppSharp.Generators.CSharp
if (@class.IsValueType) if (@class.IsValueType)
{ {
MarshalValueClass(@class); MarshalValueClass();
} }
else else
{ {
@ -570,7 +567,7 @@ namespace CppSharp.Generators.CSharp
return; return;
} }
string param = Helpers.SafeIdentifier(Context.Parameter.Name); string param = Context.Parameter.Name;
Type type = Context.Parameter.Type.Desugar(); Type type = Context.Parameter.Type.Desugar();
if (type.IsAddress()) if (type.IsAddress())
{ {
@ -590,9 +587,9 @@ namespace CppSharp.Generators.CSharp
qualifiedIdentifier, Helpers.InstanceIdentifier); qualifiedIdentifier, Helpers.InstanceIdentifier);
} }
private void MarshalValueClass(Class @class) private void MarshalValueClass()
{ {
Context.Return.Write("{0}.ToInternal()", Helpers.SafeIdentifier(Context.Parameter.Name)); Context.Return.Write("{0}.ToInternal()", Context.Parameter.Name);
} }
public override bool VisitFieldDecl(Field field) public override bool VisitFieldDecl(Field field)

78
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -37,8 +37,9 @@ namespace CppSharp.Generators.CSharp
public static string SafeIdentifier(string id) public static string SafeIdentifier(string id)
{ {
id = new string(id.Select(c => char.IsLetterOrDigit(c) ? c : '_').ToArray()); if (id.All(char.IsLetterOrDigit))
return Keywords.Contains(id) ? "@" + id : id; return Keywords.Contains(id) ? "@" + id : id;
return new string(id.Select(c => char.IsLetterOrDigit(c) ? c : '_').ToArray());
} }
public static string ToCSharpCallConv(CallingConvention convention) public static string ToCSharpCallConv(CallingConvention convention)
@ -146,11 +147,6 @@ namespace CppSharp.Generators.CSharp
return Generator.GeneratedIdentifier(id); return Generator.GeneratedIdentifier(id);
} }
public static string SafeIdentifier(string id)
{
return Helpers.SafeIdentifier(id);
}
#endregion #endregion
public override void Process() public override void Process()
@ -170,7 +166,7 @@ namespace CppSharp.Generators.CSharp
if (Options.GenerateLibraryNamespace) if (Options.GenerateLibraryNamespace)
{ {
PushBlock(CSharpBlockKind.Namespace); PushBlock(CSharpBlockKind.Namespace);
WriteLine("namespace {0}", SafeIdentifier(Driver.Options.OutputNamespace)); WriteLine("namespace {0}", Driver.Options.OutputNamespace);
WriteStartBraceIndent(); WriteStartBraceIndent();
} }
@ -242,7 +238,7 @@ namespace CppSharp.Generators.CSharp
if (context.HasFunctions) if (context.HasFunctions)
{ {
PushBlock(CSharpBlockKind.Functions); PushBlock(CSharpBlockKind.Functions);
WriteLine("public unsafe partial class {0}{1}", SafeIdentifier(Options.OutputNamespace), WriteLine("public unsafe partial class {0}{1}", Options.OutputNamespace,
TranslationUnit.FileNameWithoutExtension); TranslationUnit.FileNameWithoutExtension);
WriteStartBraceIndent(); WriteStartBraceIndent();
@ -497,7 +493,7 @@ namespace CppSharp.Generators.CSharp
GenerateClassInternalHead(@class); GenerateClassInternalHead(@class);
WriteStartBraceIndent(); WriteStartBraceIndent();
var typePrinter = TypePrinter as CSharpTypePrinter; var typePrinter = TypePrinter;
typePrinter.PushContext(CSharpTypePrinterContextKind.Native); typePrinter.PushContext(CSharpTypePrinterContextKind.Native);
GenerateClassInternalsFields(@class); GenerateClassInternalsFields(@class);
@ -593,9 +589,7 @@ namespace CppSharp.Generators.CSharp
continue; continue;
var typeName = param.CSharpType(TypePrinter); var typeName = param.CSharpType(TypePrinter);
string paramName = param.IsSynthetized ? @params.Add(string.Format("{0} {1}", typeName, param.Name));
GeneratedIdentifier(param.Name) : SafeIdentifier(param.Name);
@params.Add(string.Format("{0} {1}", typeName, paramName));
} }
if (method != null && method.IsConstructor) if (method != null && method.IsConstructor)
@ -636,7 +630,8 @@ namespace CppSharp.Generators.CSharp
if (property.Ignore || property.Field == null) continue; if (property.Ignore || property.Field == null) continue;
var nativeField = string.Format("{0}->{1}", var nativeField = string.Format("{0}->{1}",
Generator.GeneratedIdentifier("ptr"), property.Field.OriginalName); Generator.GeneratedIdentifier("ptr"),
Helpers.SafeIdentifier(property.Field.OriginalName));
var ctx = new CSharpMarshalContext(Driver) var ctx = new CSharpMarshalContext(Driver)
{ {
@ -693,7 +688,8 @@ namespace CppSharp.Generators.CSharp
private void GenerateStructInternalMarshalingProperty(Property property, string marshalVar) private void GenerateStructInternalMarshalingProperty(Property property, string marshalVar)
{ {
var nativeField = string.Format("{0}->{1}", var nativeField = string.Format("{0}->{1}",
Generator.GeneratedIdentifier("ptr"), property.Field.OriginalName); Generator.GeneratedIdentifier("ptr"),
Helpers.SafeIdentifier(property.Field.OriginalName));
var marshalCtx = new CSharpMarshalContext(Driver) var marshalCtx = new CSharpMarshalContext(Driver)
{ {
ArgName = property.Name, ArgName = property.Name,
@ -721,7 +717,7 @@ namespace CppSharp.Generators.CSharp
if (marshal.Context.Return.StringBuilder.Length > 0) if (marshal.Context.Return.StringBuilder.Length > 0)
{ {
WriteLine("{0}.{1} = {2};", marshalVar, WriteLine("{0}.{1} = {2};", marshalVar,
property.OriginalName, marshal.Context.Return); Helpers.SafeIdentifier(property.OriginalName), marshal.Context.Return);
} }
if (isRef) if (isRef)
@ -761,7 +757,7 @@ namespace CppSharp.Generators.CSharp
Write("partial "); Write("partial ");
Write(@class.IsInterface ? "interface " : (@class.IsValueType ? "struct " : "class ")); Write(@class.IsInterface ? "interface " : (@class.IsValueType ? "struct " : "class "));
Write("{0}", SafeIdentifier(@class.Name)); Write("{0}", @class.Name);
var bases = new List<string>(); var bases = new List<string>();
@ -809,7 +805,7 @@ namespace CppSharp.Generators.CSharp
private void GenerateClassInternalsField(Field field) private void GenerateClassInternalsField(Field field)
{ {
var safeIdentifier = SafeIdentifier(field.OriginalName); var safeIdentifier = Helpers.SafeIdentifier(field.OriginalName);
PushBlock(CSharpBlockKind.Field); PushBlock(CSharpBlockKind.Field);
@ -844,7 +840,7 @@ namespace CppSharp.Generators.CSharp
WriteLine("[FieldOffset({0})]", field.Offset); WriteLine("[FieldOffset({0})]", field.Offset);
WriteLine("{0} {1} {2};", @public ? "public" : "private", WriteLine("{0} {1} {2};", @public ? "public" : "private",
field.Type, SafeIdentifier(field.Name)); field.Type, field.Name);
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
@ -961,7 +957,8 @@ namespace CppSharp.Generators.CSharp
var marshal = new CSharpMarshalManagedToNativePrinter(ctx); var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
ctx.ReturnVarName = string.Format("{0}->{1}", ctx.ReturnVarName = string.Format("{0}->{1}",
Generator.GeneratedIdentifier("ptr"), Helpers.SafeIdentifier(field.OriginalName)); Generator.GeneratedIdentifier("ptr"),
Helpers.SafeIdentifier(field.OriginalName));
param.Visit(marshal); param.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore)) if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
@ -1282,14 +1279,14 @@ namespace CppSharp.Generators.CSharp
private string GetPropertyName(Property prop) private string GetPropertyName(Property prop)
{ {
return prop.Parameters.Count == 0 ? SafeIdentifier(prop.Name) return prop.Parameters.Count == 0 ? prop.Name
: string.Format("this[{0}]", FormatMethodParameters(prop.Parameters)); : string.Format("this[{0}]", FormatMethodParameters(prop.Parameters));
} }
private void GenerateVariable(Class @class, Type type, Variable variable) private void GenerateVariable(Class @class, Type type, Variable variable)
{ {
PushBlock(CSharpBlockKind.Variable); PushBlock(CSharpBlockKind.Variable);
WriteLine("public static {0} {1}", type, SafeIdentifier(variable.Name)); WriteLine("public static {0} {1}", type, variable.Name);
WriteStartBraceIndent(); WriteStartBraceIndent();
GeneratePropertyGetter(variable, @class); GeneratePropertyGetter(variable, @class);
@ -1535,7 +1532,7 @@ namespace CppSharp.Generators.CSharp
var ctx = new CSharpMarshalContext(Driver) var ctx = new CSharpMarshalContext(Driver)
{ {
ReturnType = param.QualifiedType, ReturnType = param.QualifiedType,
ReturnVarName = SafeIdentifier(param.Name) ReturnVarName = param.Name
}; };
var marshal = new CSharpMarshalNativeToManagedPrinter(ctx) { VarSuffix = i }; var marshal = new CSharpMarshalNativeToManagedPrinter(ctx) { VarSuffix = i };
@ -1556,7 +1553,7 @@ namespace CppSharp.Generators.CSharp
Method m = ((Class) method.Namespace).GetMethodByName(method.Name); Method m = ((Class) method.Namespace).GetMethodByName(method.Name);
if (m.IsGenerated) if (m.IsGenerated)
{ {
WriteLine("target.{0}({1});", SafeIdentifier(method.Name), string.Join(", ", marshals)); WriteLine("target.{0}({1});", method.Name, string.Join(", ", marshals));
} }
else else
{ {
@ -1597,12 +1594,12 @@ namespace CppSharp.Generators.CSharp
{ {
property = ((Class) method.Namespace).Properties.First( property = ((Class) method.Namespace).Properties.First(
p => p.SetMethod == method); p => p.SetMethod == method);
WriteLine("target.{0} = {1};", SafeIdentifier(property.Name), WriteLine("target.{0} = {1};", property.Name,
string.Join(", ", marshals)); string.Join(", ", marshals));
} }
else else
{ {
WriteLine("target.{0};", SafeIdentifier(property.Name)); WriteLine("target.{0};", property.Name);
} }
} }
@ -1707,7 +1704,7 @@ namespace CppSharp.Generators.CSharp
NewLine(); NewLine();
WriteLine("{0} {1};", @event.Type, delegateInstance); WriteLine("{0} {1};", @event.Type, delegateInstance);
WriteLine("public event {0} {1}", @event.Type, Helpers.SafeIdentifier(@event.Name)); WriteLine("public event {0} {1}", @event.Type, @event.Name);
WriteStartBraceIndent(); WriteStartBraceIndent();
GenerateEventAdd(@event); GenerateEventAdd(@event);
@ -1773,7 +1770,7 @@ namespace CppSharp.Generators.CSharp
{ {
var ctx = new CSharpMarshalContext(Driver) var ctx = new CSharpMarshalContext(Driver)
{ {
ReturnVarName = Helpers.SafeIdentifier(param.Name), ReturnVarName = param.Name,
ReturnType = param.QualifiedType ReturnType = param.QualifiedType
}; };
@ -1884,7 +1881,7 @@ namespace CppSharp.Generators.CSharp
{ {
PushBlock(CSharpBlockKind.Method); PushBlock(CSharpBlockKind.Method);
string className = @class.Name; string className = @class.Name;
string safeIdentifier = SafeIdentifier(className); string safeIdentifier = className;
if (@class.Access == AccessSpecifier.Private && className.EndsWith("Internal")) if (@class.Access == AccessSpecifier.Private && className.EndsWith("Internal"))
{ {
className = className.Substring(0, className = className.Substring(0,
@ -2230,7 +2227,7 @@ namespace CppSharp.Generators.CSharp
&& !string.IsNullOrWhiteSpace(param.Context.ArgumentPrefix)) && !string.IsNullOrWhiteSpace(param.Context.ArgumentPrefix))
name += param.Context.ArgumentPrefix; name += param.Context.ArgumentPrefix;
name += Helpers.SafeIdentifier(param.Name); name += param.Name;
names.Add(name); names.Add(name);
} }
@ -2454,7 +2451,7 @@ namespace CppSharp.Generators.CSharp
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore)) if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore); Write(marshal.Context.SupportBefore);
WriteLine("var {0} = {1};", SafeIdentifier(argName), marshal.Context.Return); WriteLine("var {0} = {1};", argName, marshal.Context.Return);
return paramMarshal; return paramMarshal;
} }
@ -2479,7 +2476,7 @@ namespace CppSharp.Generators.CSharp
where param.Kind != ParameterKind.IndirectReturnType where param.Kind != ParameterKind.IndirectReturnType
let typeName = param.CSharpType(TypePrinter) let typeName = param.CSharpType(TypePrinter)
select string.Format("{0}{1} {2}", GetParameterUsage(param.Usage), select string.Format("{0}{1} {2}", GetParameterUsage(param.Usage),
typeName, SafeIdentifier(param.Name))); typeName, param.Name));
} }
#endregion #endregion
@ -2498,7 +2495,7 @@ namespace CppSharp.Generators.CSharp
|| typedef.Type.IsPointerTo(out tag)) || typedef.Type.IsPointerTo(out tag))
{ {
PushBlock(CSharpBlockKind.Typedef); PushBlock(CSharpBlockKind.Typedef);
WriteLine("public class " + SafeIdentifier(typedef.Name) + @" { }"); WriteLine("public class " + typedef.Name + @" { }");
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
else if (typedef.Type.IsPointerTo(out functionType)) else if (typedef.Type.IsPointerTo(out functionType))
@ -2510,7 +2507,7 @@ namespace CppSharp.Generators.CSharp
WriteLine("{0}unsafe {1};", WriteLine("{0}unsafe {1};",
Helpers.GetAccess(typedef.Access), Helpers.GetAccess(typedef.Access),
string.Format(TypePrinter.VisitDelegate(functionType).Type, string.Format(TypePrinter.VisitDelegate(functionType).Type,
SafeIdentifier(typedef.Name))); typedef.Name));
TypePrinter.PopContext(); TypePrinter.PopContext();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
@ -2528,7 +2525,7 @@ namespace CppSharp.Generators.CSharp
if (@enum.IsFlags) if (@enum.IsFlags)
WriteLine("[Flags]"); WriteLine("[Flags]");
Write("public enum {0}", SafeIdentifier(@enum.Name)); Write("public enum {0}", @enum.Name);
var typeName = TypePrinter.VisitPrimitiveType(@enum.BuiltinType.Type, var typeName = TypePrinter.VisitPrimitiveType(@enum.BuiltinType.Type,
new TypeQualifiers()); new TypeQualifiers());
@ -2546,8 +2543,8 @@ namespace CppSharp.Generators.CSharp
var value = @enum.GetItemValueAsString(item); var value = @enum.GetItemValueAsString(item);
Write(item.ExplicitValue Write(item.ExplicitValue
? string.Format("{0} = {1}", SafeIdentifier(item.Name), value) ? string.Format("{0} = {1}", item.Name, value)
: string.Format("{0}", SafeIdentifier(item.Name))); : string.Format("{0}", item.Name));
if (i < @enum.Items.Count - 1) if (i < @enum.Items.Count - 1)
Write(","); Write(",");
@ -2572,7 +2569,7 @@ namespace CppSharp.Generators.CSharp
if (function.IsOperator) if (function.IsOperator)
return Operators.GetOperatorIdentifier(function.OperatorKind); return Operators.GetOperatorIdentifier(function.OperatorKind);
return SafeIdentifier(function.Name); return function.Name;
} }
public static string GetFunctionNativeIdentifier(Function function) public static string GetFunctionNativeIdentifier(Function function)
@ -2592,7 +2589,7 @@ namespace CppSharp.Generators.CSharp
functionName = GetMethodIdentifier(method); functionName = GetMethodIdentifier(method);
} }
var identifier = SafeIdentifier(functionName); var identifier = functionName;
if (function.IsOperator) if (function.IsOperator)
identifier = "Operator" + function.OperatorKind; identifier = "Operator" + function.OperatorKind;
@ -2672,10 +2669,7 @@ namespace CppSharp.Generators.CSharp
var typeName = param.CSharpType(typePrinter); var typeName = param.CSharpType(typePrinter);
var paramName = param.IsSynthetized ? @params.Add(string.Format("{0} {1}", typeName, param.Name));
GeneratedIdentifier(param.Name) : SafeIdentifier(param.Name);
@params.Add(string.Format("{0} {1}", typeName, paramName));
} }
if (method != null && method.IsConstructor) if (method != null && method.IsConstructor)

2
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -508,7 +508,7 @@ namespace CppSharp.Generators.CSharp
public CSharpTypePrinterResult VisitParameter(Parameter arg, bool hasName) public CSharpTypePrinterResult VisitParameter(Parameter arg, bool hasName)
{ {
var type = arg.Type.Visit(this, arg.QualifiedType.Qualifiers); var type = arg.Type.Visit(this, arg.QualifiedType.Qualifiers);
var name = Helpers.SafeIdentifier(arg.Name); var name = arg.Name;
if (hasName && !string.IsNullOrEmpty(name)) if (hasName && !string.IsNullOrEmpty(name))
return string.Format("{0} {1}", type, name); return string.Format("{0} {1}", type, name);

13
src/Generator/Passes/CleanInvalidDeclNamesPass.cs

@ -1,6 +1,8 @@
using System; using System;
using System.Linq; using System.Linq;
using CppSharp.AST; using CppSharp.AST;
using CppSharp.Generators.CLI;
using CppSharp.Generators.CSharp;
namespace CppSharp.Passes namespace CppSharp.Passes
{ {
@ -14,13 +16,13 @@ namespace CppSharp.Passes
if (string.IsNullOrWhiteSpace(name)) if (string.IsNullOrWhiteSpace(name))
return string.Format("_{0}", uniqueName++); return string.Format("_{0}", uniqueName++);
var firstChar = name.FirstOrDefault();
// Clean up the item name if the first digit is not a valid name. // Clean up the item name if the first digit is not a valid name.
if (char.IsNumber(firstChar)) if (char.IsNumber(name[0]))
return '_' + name; return '_' + name;
return name; if (Driver.Options.IsCLIGenerator)
return CLITextTemplate.SafeIdentifier(name);
return Helpers.SafeIdentifier(name);
} }
public override bool VisitDeclaration(Declaration decl) public override bool VisitDeclaration(Declaration decl)
@ -33,10 +35,13 @@ namespace CppSharp.Passes
// types with empty names are assumed to be private // types with empty names are assumed to be private
if (decl is Class && string.IsNullOrWhiteSpace(decl.Name)) if (decl is Class && string.IsNullOrWhiteSpace(decl.Name))
{ {
decl.Name = "_";
decl.ExplicityIgnored = true; decl.ExplicityIgnored = true;
return false; return false;
} }
Function function = decl as Function;
if (function == null || !function.IsOperator)
decl.Name = CheckName(decl.Name); decl.Name = CheckName(decl.Name);
StringHelpers.CleanupText(ref decl.DebugText); StringHelpers.CleanupText(ref decl.DebugText);

Loading…
Cancel
Save