Browse Source

Moved GeneratedIdentifier to the base Generator so that the former is available to all back-ends.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/46/head
Dimitar Dobrev 12 years ago
parent
commit
5208fafef7
  1. 2
      src/Generator/Generators/CLI/CLIMarshal.cs
  2. 12
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  3. 41
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  4. 5
      src/Generator/Generators/Generator.cs
  5. 3
      src/Generator/Passes/CheckOperatorsOverloads.cs

2
src/Generator/Generators/CLI/CLIMarshal.cs

@ -180,7 +180,7 @@ namespace CppSharp.Generators.CLI @@ -180,7 +180,7 @@ namespace CppSharp.Generators.CLI
if (@class.IsRefType)
{
var name = Helpers.GeneratedIdentifier(Context.ReturnVarName);
var name = Generator.GeneratedIdentifier(Context.ReturnVarName);
Context.SupportBefore.WriteLine("auto {0} = new ::{1}({2});", name,
@class.QualifiedOriginalName, Context.ReturnVarName);
instance = name;

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

@ -185,7 +185,7 @@ namespace CppSharp.Generators.CSharp @@ -185,7 +185,7 @@ namespace CppSharp.Generators.CSharp
FunctionType function;
if (decl.Type.IsPointerTo(out function))
{
var ptrName = Helpers.GeneratedIdentifier("ptr") +
var ptrName = Generator.GeneratedIdentifier("ptr") +
Context.ParameterIndex;
Context.SupportBefore.WriteLine("var {0} = {1};", ptrName,
@ -206,7 +206,7 @@ namespace CppSharp.Generators.CSharp @@ -206,7 +206,7 @@ namespace CppSharp.Generators.CSharp
string instance = Context.ReturnVarName;
if (ctx.Kind == CSharpMarshalKind.NativeField)
{
string copy = Helpers.GeneratedIdentifier("copy");
string copy = Generator.GeneratedIdentifier("copy");
Context.SupportBefore.WriteLine(
"var {0} = new global::System.IntPtr(&{1});", copy, instance);
instance = copy;
@ -214,7 +214,7 @@ namespace CppSharp.Generators.CSharp @@ -214,7 +214,7 @@ namespace CppSharp.Generators.CSharp
if (@class.IsRefType)
{
var instanceName = Helpers.GeneratedIdentifier("instance");
var instanceName = Generator.GeneratedIdentifier("instance");
// Allocate memory for a new native object and call the ctor.
Context.SupportBefore.WriteLine("var {0} = Marshal.AllocHGlobal({1});",
@ -366,17 +366,17 @@ namespace CppSharp.Generators.CSharp @@ -366,17 +366,17 @@ namespace CppSharp.Generators.CSharp
if (Context.Parameter.Usage == ParameterUsage.Out)
{
Context.SupportBefore.WriteLine("var {0} = new {1}.Internal();",
Helpers.GeneratedIdentifier(Context.ArgName), @class.Name);
Generator.GeneratedIdentifier(Context.ArgName), @class.Name);
}
else
{
Context.SupportBefore.WriteLine("var {0} = {1}.ToInternal();",
Helpers.GeneratedIdentifier(Context.ArgName),
Generator.GeneratedIdentifier(Context.ArgName),
Helpers.SafeIdentifier(Context.Parameter.Name));
}
Context.Return.Write("new global::System.IntPtr(&{0})",
Helpers.GeneratedIdentifier(Context.ArgName));
Generator.GeneratedIdentifier(Context.ArgName));
return true;
}

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

@ -26,11 +26,6 @@ namespace CppSharp.Generators.CSharp @@ -26,11 +26,6 @@ namespace CppSharp.Generators.CSharp
"void", "partial", "yield", "where"
};
public static string GeneratedIdentifier(string id)
{
return "__" + id;
}
public static string SafeIdentifier(string id)
{
id = new string(((IEnumerable<char>)id)
@ -59,7 +54,7 @@ namespace CppSharp.Generators.CSharp @@ -59,7 +54,7 @@ namespace CppSharp.Generators.CSharp
public static string InstanceIdentifier
{
get { return GeneratedIdentifier("Instance"); }
get { return Generator.GeneratedIdentifier("Instance"); }
}
}
@ -124,7 +119,7 @@ namespace CppSharp.Generators.CSharp @@ -124,7 +119,7 @@ namespace CppSharp.Generators.CSharp
public static string GeneratedIdentifier(string id)
{
return Helpers.GeneratedIdentifier(id);
return Generator.GeneratedIdentifier(id);
}
public static string SafeIdentifier(string id)
@ -513,7 +508,7 @@ namespace CppSharp.Generators.CSharp @@ -513,7 +508,7 @@ namespace CppSharp.Generators.CSharp
if (ASTUtils.CheckIgnoreField(field)) continue;
var nativeField = string.Format("{0}->{1}",
Helpers.GeneratedIdentifier("ptr"), field.OriginalName);
Generator.GeneratedIdentifier("ptr"), field.OriginalName);
var ctx = new CSharpMarshalContext(Driver)
{
@ -535,7 +530,7 @@ namespace CppSharp.Generators.CSharp @@ -535,7 +530,7 @@ namespace CppSharp.Generators.CSharp
private void GenerateStructInternalMarshaling(Class @class)
{
var marshalVar = Helpers.GeneratedIdentifier("native");
var marshalVar = Generator.GeneratedIdentifier("native");
WriteLine("var {0} = new {1}.Internal();", marshalVar, QualifiedIdentifier(@class));
GenerateStructInternalMarshalingFields(@class, marshalVar);
@ -746,7 +741,7 @@ namespace CppSharp.Generators.CSharp @@ -746,7 +741,7 @@ namespace CppSharp.Generators.CSharp
var field = decl as Field;
WriteLine("var {0} = (Internal*){1}.ToPointer();",
Helpers.GeneratedIdentifier("ptr"), Helpers.InstanceIdentifier);
Generator.GeneratedIdentifier("ptr"), Helpers.InstanceIdentifier);
var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
param.Visit(marshal);
@ -754,7 +749,7 @@ namespace CppSharp.Generators.CSharp @@ -754,7 +749,7 @@ namespace CppSharp.Generators.CSharp
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
Write(marshal.Context.SupportBefore);
Write("{0}->{1} = {2}", Helpers.GeneratedIdentifier("ptr"),
Write("{0}->{1} = {2}", Generator.GeneratedIdentifier("ptr"),
Helpers.SafeIdentifier(field.OriginalName), marshal.Context.Return);
WriteLine(";");
@ -781,13 +776,13 @@ namespace CppSharp.Generators.CSharp @@ -781,13 +776,13 @@ namespace CppSharp.Generators.CSharp
var field = decl as Field;
WriteLine("var {0} = (Internal*){1}.ToPointer();",
Helpers.GeneratedIdentifier("ptr"), Helpers.InstanceIdentifier);
Generator.GeneratedIdentifier("ptr"), Helpers.InstanceIdentifier);
var ctx = new CSharpMarshalContext(Driver)
{
Kind = CSharpMarshalKind.NativeField,
ArgName = decl.Name,
ReturnVarName = string.Format("{0}->{1}", Helpers.GeneratedIdentifier("ptr"),
ReturnVarName = string.Format("{0}->{1}", Generator.GeneratedIdentifier("ptr"),
Helpers.SafeIdentifier(field.OriginalName)),
ReturnType = decl.QualifiedType
};
@ -810,7 +805,7 @@ namespace CppSharp.Generators.CSharp @@ -810,7 +805,7 @@ namespace CppSharp.Generators.CSharp
var location = string.Format("CppSharp.SymbolResolver.ResolveSymbol(\"{0}\", \"{1}\")",
libSymbol.Item1, libSymbol.Item2);
WriteLine("var {0} = ({1}*){2};", Helpers.GeneratedIdentifier("ptr"),
WriteLine("var {0} = ({1}*){2};", Generator.GeneratedIdentifier("ptr"),
@var.Type, location);
TypePrinter.PopContext();
@ -818,7 +813,7 @@ namespace CppSharp.Generators.CSharp @@ -818,7 +813,7 @@ namespace CppSharp.Generators.CSharp
var ctx = new CSharpMarshalContext(Driver)
{
ArgName = decl.Name,
ReturnVarName = "*" + Helpers.GeneratedIdentifier("ptr"),
ReturnVarName = "*" + Generator.GeneratedIdentifier("ptr"),
ReturnType = new QualifiedType(var.Type)
};
@ -1185,7 +1180,7 @@ namespace CppSharp.Generators.CSharp @@ -1185,7 +1180,7 @@ namespace CppSharp.Generators.CSharp
var args = TypePrinter.VisitParameters(@event.Parameters, hasNames: true);
TypePrinter.PopContext();
delegateInstance = Helpers.GeneratedIdentifier(@event.OriginalName);
delegateInstance = Generator.GeneratedIdentifier(@event.OriginalName);
delegateName = delegateInstance + "Delegate";
delegateRaise = delegateInstance + "RaiseInstance";
@ -1221,7 +1216,7 @@ namespace CppSharp.Generators.CSharp @@ -1221,7 +1216,7 @@ namespace CppSharp.Generators.CSharp
WriteLine("{0} = new {1}(_{2}Raise);", delegateRaise, delegateName, @event.Name);
WriteLine("var {0} = Marshal.GetFunctionPointerForDelegate({1}).ToPointer();",
Helpers.GeneratedIdentifier("ptr"), delegateInstance);
Generator.GeneratedIdentifier("ptr"), delegateInstance);
// Call type map here.
@ -1377,13 +1372,13 @@ namespace CppSharp.Generators.CSharp @@ -1377,13 +1372,13 @@ namespace CppSharp.Generators.CSharp
{
WriteLine("{0} = native;", Helpers.InstanceIdentifier);
if (Options.GenerateVirtualTables && @class.IsDynamic)
WriteLine("SetupVTables({0});", Helpers.GeneratedIdentifier("Instance"));
WriteLine("SetupVTables({0});", Generator.GeneratedIdentifier("Instance"));
}
}
else
{
WriteLine("var {0} = (Internal*){1}.ToPointer();",
Helpers.GeneratedIdentifier("ptr"), "native");
Generator.GeneratedIdentifier("ptr"), "native");
GenerateStructMarshalingFields(@class);
}
@ -1402,7 +1397,7 @@ namespace CppSharp.Generators.CSharp @@ -1402,7 +1397,7 @@ namespace CppSharp.Generators.CSharp
PushBlock(CSharpBlockKind.Method);
WriteLine("internal void FromInternal(Internal* native)");
WriteStartBraceIndent();
WriteLine("var {0} = {1};", Helpers.GeneratedIdentifier("ptr"), "native");
WriteLine("var {0} = {1};", Generator.GeneratedIdentifier("ptr"), "native");
GenerateStructMarshalingFields(@class);
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
@ -1581,7 +1576,7 @@ namespace CppSharp.Generators.CSharp @@ -1581,7 +1576,7 @@ namespace CppSharp.Generators.CSharp
GenerateFunctionParams(@params);
WriteLine(");");
if (Options.GenerateVirtualTables && @class.IsDynamic)
WriteLine("SetupVTables({0});", Helpers.GeneratedIdentifier("Instance"));
WriteLine("SetupVTables({0});", Generator.GeneratedIdentifier("Instance"));
}
public void GenerateInternalFunctionCall(Function function,
@ -1669,7 +1664,7 @@ namespace CppSharp.Generators.CSharp @@ -1669,7 +1664,7 @@ namespace CppSharp.Generators.CSharp
//WriteLine("fixed({0}* {1} = &this)", @class.QualifiedName,
// GeneratedIdentifier("instance"));
//WriteStartBraceIndent();
WriteLine("var {0} = ToInternal();", Helpers.GeneratedIdentifier("instance"));
WriteLine("var {0} = ToInternal();", Generator.GeneratedIdentifier("instance"));
}
if (needsReturn && !function.HasHiddenStructParameter)
@ -1697,7 +1692,7 @@ namespace CppSharp.Generators.CSharp @@ -1697,7 +1692,7 @@ namespace CppSharp.Generators.CSharp
if (needsFixedThis)
{
// WriteCloseBraceIndent();
WriteLine("FromInternal(&{0});", Helpers.GeneratedIdentifier("instance"));
WriteLine("FromInternal(&{0});", Generator.GeneratedIdentifier("instance"));
}
if (needsReturn)

5
src/Generator/Generators/Generator.cs

@ -98,5 +98,10 @@ namespace CppSharp.Generators @@ -98,5 +98,10 @@ namespace CppSharp.Generators
/// Generates the outputs for a given translation unit.
/// </summary>
public abstract List<Template> Generate(TranslationUnit unit);
public static string GeneratedIdentifier(string id)
{
return "__" + id;
}
}
}

3
src/Generator/Passes/CheckOperatorsOverloads.cs

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
using System.Linq;
using CppSharp.AST;
using CppSharp.Generators;
using CppSharp.Generators.CSharp;
namespace CppSharp.Passes
@ -64,7 +65,7 @@ namespace CppSharp.Passes @@ -64,7 +65,7 @@ namespace CppSharp.Passes
@operator.Parameters.Insert(0, new Parameter
{
Name = Helpers.GeneratedIdentifier("op"),
Name = Generator.GeneratedIdentifier("op"),
QualifiedType = new QualifiedType(type),
Kind = ParameterKind.OperatorParameter
});

Loading…
Cancel
Save