Browse Source

Minor refactoring fixes.

pull/1301/head
João Matos 6 years ago committed by João Matos
parent
commit
c19df0cc64
  1. 2
      src/AST/Template.cs
  2. 2
      src/Generator/Driver.cs
  3. 6
      src/Generator/Generators/C/CCodeGenerator.cs
  4. 6
      src/Generator/Generators/CLI/CLIMarshal.cs
  5. 27
      src/Generator/Generators/CLI/CLISources.cs
  6. 1
      src/Generator/Passes/CheckAbiParameters.cs
  7. 1
      src/Generator/Passes/CheckAmbiguousFunctions.cs

2
src/AST/Template.cs

@ -310,7 +310,7 @@ namespace CppSharp.AST
public override string ToString() public override string ToString()
{ {
var args = string.Join(", ", Arguments.Select(a => a.ToString())); var args = string.Join(", ", Arguments.Select(a => a.ToString()));
return string.Format("{0}<{1}> [{2}]", OriginalName, args, SpecializationKind); return $"{OriginalName}<{args}> [{SpecializationKind}]";
} }
} }

2
src/Generator/Driver.cs

@ -322,7 +322,7 @@ namespace CppSharp
foreach (var template in output.Outputs) foreach (var template in output.Outputs)
{ {
var fileRelativePath = string.Format("{0}.{1}", fileBase, template.FileExtension); var fileRelativePath = $"{fileBase}.{template.FileExtension}";
var file = Path.Combine(outputPath, fileRelativePath); var file = Path.Combine(outputPath, fileRelativePath);
File.WriteAllText(file, template.Generate()); File.WriteAllText(file, template.Generate());

6
src/Generator/Generators/C/CCodeGenerator.cs

@ -20,8 +20,8 @@ namespace CppSharp.Generators.C
public override string ToString() public override string ToString()
{ {
return string.Format(Kind == IncludeKind.Angled ? return Kind == IncludeKind.Angled ?
"#include <{0}>" : "#include \"{0}\"", File); $"#include <{File}>" : $"#include \"{File}\"";
} }
} }
@ -151,7 +151,7 @@ namespace CppSharp.Generators.C
if (!string.IsNullOrWhiteSpace(enumName) && useTypedefEnum) if (!string.IsNullOrWhiteSpace(enumName) && useTypedefEnum)
WriteLine($"}} {enumName};"); WriteLine($"}} {enumName};");
else else
WriteLine($"}};"); WriteLine("};");
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);

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

@ -288,9 +288,9 @@ namespace CppSharp.Generators.CLI
public string QualifiedIdentifier(Declaration decl) public string QualifiedIdentifier(Declaration decl)
{ {
if (!string.IsNullOrEmpty(decl.TranslationUnit.Module.OutputNamespace)) if (!string.IsNullOrEmpty(decl.TranslationUnit.Module.OutputNamespace))
return string.Format("{0}::{1}", decl.TranslationUnit.Module.OutputNamespace, return $"{decl.TranslationUnit.Module.OutputNamespace}::{decl.QualifiedName}";
decl.QualifiedName);
return string.Format("{0}", decl.QualifiedName); return decl.QualifiedName;
} }
public void WriteClassInstance(Class @class, string instance) public void WriteClassInstance(Class @class, string instance)

27
src/Generator/Generators/CLI/CLISources.cs

@ -150,7 +150,7 @@ namespace CppSharp.Generators.CLI
WriteLine("void {0}::{1}::set(System::IntPtr object)", WriteLine("void {0}::{1}::set(System::IntPtr object)",
qualifiedIdentifier, Helpers.InstanceIdentifier); qualifiedIdentifier, Helpers.InstanceIdentifier);
WriteOpenBraceAndIndent(); WriteOpenBraceAndIndent();
var nativeType = string.Format("::{0}*", @class.QualifiedOriginalName); var nativeType = $"::{@class.QualifiedOriginalName}*";
WriteLine("NativePtr = ({0})object.ToPointer();", nativeType); WriteLine("NativePtr = ({0})object.ToPointer();", nativeType);
UnindentAndWriteCloseBrace(); UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
@ -356,11 +356,11 @@ namespace CppSharp.Generators.CLI
var args = new List<string>(); var args = new List<string>();
var isIndexer = indexParameter != null; var isIndexer = indexParameter != null;
if (isIndexer) if (isIndexer)
args.Add(string.Format("{0} {1}", indexParameter.Type, indexParameter.Name)); args.Add($"{indexParameter.Type} {indexParameter.Name}");
var function = decl as Function; var function = decl as Function;
var argName = function != null && !isIndexer ? function.Parameters[0].Name : "value"; var argName = function != null && !isIndexer ? function.Parameters[0].Name : "value";
args.Add(string.Format("{0} {1}", type, argName)); args.Add($"{type} {argName}");
WriteLine("void {0}::{1}::set({2})", QualifiedIdentifier(@class), WriteLine("void {0}::{1}::set({2})", QualifiedIdentifier(@class),
name, string.Join(", ", args)); name, string.Join(", ", args));
@ -468,18 +468,17 @@ namespace CppSharp.Generators.CLI
{ {
if (@class.IsValueType && decl is Field) if (@class.IsValueType && decl is Field)
{ {
WriteLine("return {0};", decl.Name); WriteLine($"return {decl.Name};");
UnindentAndWriteCloseBrace(); UnindentAndWriteCloseBrace();
NewLine(); NewLine();
return; return;
} }
string variable; string variable;
if (decl is Variable) if (decl is Variable)
variable = string.Format("::{0}::{1}", variable = $"::{@class.QualifiedOriginalName}::{decl.OriginalName}";
@class.QualifiedOriginalName, decl.OriginalName);
else else
variable = string.Format("((::{0}*)NativePtr)->{1}", variable = $"((::{@class.QualifiedOriginalName}*)NativePtr)->{decl.OriginalName}";
@class.QualifiedOriginalName, decl.OriginalName);
var ctx = new MarshalContext(Context, CurrentIndentation) var ctx = new MarshalContext(Context, CurrentIndentation)
{ {
@ -488,13 +487,14 @@ namespace CppSharp.Generators.CLI
ReturnType = decl.QualifiedType ReturnType = decl.QualifiedType
}; };
ctx.PushMarshalKind(MarshalKind.NativeField); ctx.PushMarshalKind(MarshalKind.NativeField);
var marshal = new CLIMarshalNativeToManagedPrinter(ctx); var marshal = new CLIMarshalNativeToManagedPrinter(ctx);
decl.Visit(marshal); decl.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.Before)) if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Write(marshal.Context.Before); Write(marshal.Context.Before);
WriteLine("return {0};", marshal.Context.Return); WriteLine($"return {marshal.Context.Return};");
} }
@ -864,7 +864,7 @@ namespace CppSharp.Generators.CLI
{ {
if (!property.IsDeclared || property.Field == null) continue; if (!property.IsDeclared || property.Field == null) continue;
var varName = string.Format("_native.{0}", property.Field.OriginalName); var varName = $"_native.{property.Field.OriginalName}";
var ctx = new MarshalContext(Context, CurrentIndentation) var ctx = new MarshalContext(Context, CurrentIndentation)
{ {
@ -991,12 +991,12 @@ namespace CppSharp.Generators.CLI
{ {
if (IsNativeFunctionOrStaticMethod(function)) if (IsNativeFunctionOrStaticMethod(function))
{ {
Write("::{0}(", function.QualifiedOriginalName); Write($"::{function.QualifiedOriginalName}(");
} }
else else
{ {
if (isValueType) if (isValueType)
Write("{0}.", valueMarshalName); Write($"{valueMarshalName}.");
else if (IsNativeMethod(function)) else if (IsNativeMethod(function))
Write("((::{0}*)NativePtr)->", @class.QualifiedOriginalName); Write("((::{0}*)NativePtr)->", @class.QualifiedOriginalName);
Write("{0}(", function.OriginalName); Write("{0}(", function.OriginalName);
@ -1175,8 +1175,7 @@ namespace CppSharp.Generators.CLI
effectiveParam.Visit(marshal); effectiveParam.Visit(marshal);
if (string.IsNullOrEmpty(marshal.Context.Return)) if (string.IsNullOrEmpty(marshal.Context.Return))
throw new Exception(string.Format("Cannot marshal argument of function '{0}'", throw new Exception($"Cannot marshal argument of function '{function.QualifiedOriginalName}'");
function.QualifiedOriginalName));
if (isRef) if (isRef)
{ {

1
src/Generator/Passes/CheckAbiParameters.cs

@ -1,5 +1,4 @@
using CppSharp.AST; using CppSharp.AST;
using CppSharp.AST.Extensions;
using System.Linq; using System.Linq;
namespace CppSharp.Passes namespace CppSharp.Passes

1
src/Generator/Passes/CheckAmbiguousFunctions.cs

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using CppSharp.AST; using CppSharp.AST;
using CppSharp.AST.Extensions; using CppSharp.AST.Extensions;
using CppSharp.Types;
using CppSharp.Generators; using CppSharp.Generators;
namespace CppSharp.Passes namespace CppSharp.Passes

Loading…
Cancel
Save