Browse Source

Added global:: prefix to all System.IntPtr references in the C# code generator.

Without this qualification it is possible for the generated code to have namespace clashes with nested System namespaces in the code being wrapped.
pull/15/head
Bright Twin 12 years ago
parent
commit
7c19f5f7db
  1. 4
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  2. 12
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  3. 10
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs

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

@ -201,7 +201,7 @@ namespace CppSharp.Generators.CSharp
string instance = Context.ReturnVarName; string instance = Context.ReturnVarName;
if (ctx.Kind == CSharpMarshalKind.NativeField) if (ctx.Kind == CSharpMarshalKind.NativeField)
{ {
instance = string.Format("new System.IntPtr(&{0})", instance); instance = string.Format("new global::System.IntPtr(&{0})", instance);
} }
Context.Return.Write("new {0}({1})", QualifiedIdentifier(@class), Context.Return.Write("new {0}({1})", QualifiedIdentifier(@class),
@ -306,7 +306,7 @@ namespace CppSharp.Generators.CSharp
Helpers.SafeIdentifier(Context.Parameter.Name)); Helpers.SafeIdentifier(Context.Parameter.Name));
} }
Context.Return.Write("new System.IntPtr(&{0})", Context.Return.Write("new global::System.IntPtr(&{0})",
Helpers.GeneratedIdentifier(Context.ArgName)); Helpers.GeneratedIdentifier(Context.ArgName));
return true; return true;
} }

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

@ -323,7 +323,7 @@ namespace CppSharp.Generators.CSharp
if (ShouldGenerateClassNativeField(@class)) if (ShouldGenerateClassNativeField(@class))
{ {
PushBlock(CSharpBlockKind.Field); PushBlock(CSharpBlockKind.Field);
WriteLine("public System.IntPtr {0} {{ get; protected set; }}", WriteLine("public global::System.IntPtr {0} {{ get; protected set; }}",
Helpers.InstanceIdentifier); Helpers.InstanceIdentifier);
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
@ -1031,7 +1031,7 @@ namespace CppSharp.Generators.CSharp
PushBlock(CSharpBlockKind.Method); PushBlock(CSharpBlockKind.Method);
WriteLine("internal {0}({1}.Internal* native)", SafeIdentifier(@class.Name), WriteLine("internal {0}({1}.Internal* native)", SafeIdentifier(@class.Name),
@class.Name); @class.Name);
WriteLineIndent(": this(new System.IntPtr(native))"); WriteLineIndent(": this(new global::System.IntPtr(native))");
WriteStartBraceIndent(); WriteStartBraceIndent();
WriteCloseBraceIndent(); WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
@ -1045,7 +1045,7 @@ namespace CppSharp.Generators.CSharp
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
PushBlock(CSharpBlockKind.Method); PushBlock(CSharpBlockKind.Method);
WriteLine("internal {0}(System.IntPtr native)", SafeIdentifier(@class.Name)); WriteLine("internal {0}(global::System.IntPtr native)", SafeIdentifier(@class.Name));
var hasBaseClass = @class.HasBaseClass && @class.BaseClass.IsRefType; var hasBaseClass = @class.HasBaseClass && @class.BaseClass.IsRefType;
if (hasBaseClass) if (hasBaseClass)
@ -1325,7 +1325,7 @@ namespace CppSharp.Generators.CSharp
if (needsInstance) if (needsInstance)
{ {
names.Insert(0, needsFixedThis ? string.Format("new System.IntPtr(&{0})", names.Insert(0, needsFixedThis ? string.Format("new global::System.IntPtr(&{0})",
GeneratedIdentifier("instance")) : Helpers.InstanceIdentifier); GeneratedIdentifier("instance")) : Helpers.InstanceIdentifier);
} }
@ -1803,10 +1803,10 @@ namespace CppSharp.Generators.CSharp
var method = function as Method; var method = function as Method;
if (method != null && !method.IsStatic) if (method != null && !method.IsStatic)
{ {
@params.Add("System.IntPtr instance"); @params.Add("global::System.IntPtr instance");
if (method.IsConstructor && Options.IsMicrosoftAbi) if (method.IsConstructor && Options.IsMicrosoftAbi)
retType = "System.IntPtr"; retType = "global::System.IntPtr";
} }
for (var i = 0; i < function.Parameters.Count; ++i) for (var i = 0; i < function.Parameters.Count; ++i)

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

@ -172,17 +172,17 @@ namespace CppSharp.Generators.CSharp
var isManagedContext = ContextKind == CSharpTypePrinterContextKind.Managed; var isManagedContext = ContextKind == CSharpTypePrinterContextKind.Managed;
if (IsConstCharString(pointer)) if (IsConstCharString(pointer))
return isManagedContext ? "string" : "System.IntPtr"; return isManagedContext ? "string" : "global::System.IntPtr";
PrimitiveType primitive; PrimitiveType primitive;
if (pointee.Desugar().IsPrimitiveType(out primitive)) if (pointee.Desugar().IsPrimitiveType(out primitive))
return "System.IntPtr"; return "global::System.IntPtr";
Class @class; Class @class;
if (pointee.IsTagDecl(out @class) if (pointee.IsTagDecl(out @class)
&& ContextKind == CSharpTypePrinterContextKind.Native) && ContextKind == CSharpTypePrinterContextKind.Native)
{ {
return "System.IntPtr"; return "global::System.IntPtr";
} }
return pointee.Visit(this, quals); return pointee.Visit(this, quals);
@ -225,7 +225,7 @@ namespace CppSharp.Generators.CSharp
if (decl.Type.IsPointerTo<FunctionType>(out func)) if (decl.Type.IsPointerTo<FunctionType>(out func))
{ {
if (ContextKind == CSharpTypePrinterContextKind.Native) if (ContextKind == CSharpTypePrinterContextKind.Native)
return "System.IntPtr"; return "global::System.IntPtr";
// TODO: Use SafeIdentifier() // TODO: Use SafeIdentifier()
return VisitDeclaration(decl); return VisitDeclaration(decl);
} }
@ -314,7 +314,7 @@ namespace CppSharp.Generators.CSharp
case PrimitiveType.UInt64: return "ulong"; case PrimitiveType.UInt64: return "ulong";
case PrimitiveType.Float: return "float"; case PrimitiveType.Float: return "float";
case PrimitiveType.Double: return "double"; case PrimitiveType.Double: return "double";
case PrimitiveType.IntPtr: return "System.IntPtr"; case PrimitiveType.IntPtr: return "global::System.IntPtr";
} }
throw new NotSupportedException(); throw new NotSupportedException();

Loading…
Cancel
Save