Restored the renaming of enums.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
This commit is contained in:
Dimitar Dobrev 2017-03-10 21:00:08 +02:00
Родитель 028eac9d69
Коммит fe85a33ba1
8 изменённых файлов: 39 добавлений и 58 удалений

Просмотреть файл

@ -221,7 +221,6 @@ namespace CppSharp
public void SetupPasses(Driver driver)
{
driver.Context.TranslationUnitPasses.RenameDeclsUpperCase(RenameTargets.Any);
driver.Context.TranslationUnitPasses.AddPass(new FunctionToInstanceMethodPass());
driver.Context.TranslationUnitPasses.AddPass(new MarshalPrimitivePointersAsRefTypePass());
}
@ -232,10 +231,6 @@ namespace CppSharp
public void Postprocess(Driver driver, ASTContext ctx)
{
new CaseRenamePass(
RenameTargets.Function | RenameTargets.Method | RenameTargets.Property | RenameTargets.Delegate |
RenameTargets.Field | RenameTargets.Variable,
RenameCasePattern.UpperCamelCase).VisitASTContext(driver.Context.ASTContext);
}
public void Run()

Просмотреть файл

@ -338,9 +338,7 @@ namespace CppSharp
if (Options.GeneratorKind == GeneratorKind.CLI ||
Options.GeneratorKind == GeneratorKind.CSharp)
TranslationUnitPasses.RenameDeclsUpperCase(RenameTargets.Function |
RenameTargets.Method | RenameTargets.Property | RenameTargets.Delegate |
RenameTargets.Field | RenameTargets.Variable | RenameTargets.Class);
TranslationUnitPasses.RenameDeclsUpperCase(RenameTargets.Any &~ RenameTargets.Parameter);
}
public void ProcessCode()

Просмотреть файл

@ -1,5 +1,6 @@
using System.Linq;
using CppSharp.AST;
using CppSharp.AST.Extensions;
using CppSharp.Types;
using Type = CppSharp.AST.Type;
@ -20,6 +21,17 @@ namespace CppSharp.Generators.CSharp
this.typePrinter = typePrinter;
}
public string VisitParameter(Parameter parameter)
{
var expression = VisitExpression(parameter.DefaultArgument);
var desugared = parameter.Type.Desugar();
if (desugared.IsPrimitiveType() &&
(parameter.DefaultArgument.Declaration != null ||
parameter.DefaultArgument.Class == StatementClass.BinaryOperator))
return $"({desugared.Visit(typePrinter)}) {expression}";
return expression;
}
public string VisitExpression(Expression expr)
{
switch (expr.Class)
@ -47,6 +59,24 @@ namespace CppSharp.Generators.CSharp
if (expr.Declaration is Variable || expr.Declaration is Enumeration.Item)
return expr.Declaration.Visit(typePrinter).Type;
goto default;
case StatementClass.BinaryOperator:
var binaryOperator = (BinaryOperator) expr;
var lhsResult = binaryOperator.LHS.String;
if (binaryOperator.LHS.Declaration is Enumeration.Item)
lhsResult = binaryOperator.LHS.Declaration.Visit(typePrinter).Type;
var rhsResult = binaryOperator.RHS.String;
if (binaryOperator.RHS.Declaration is Enumeration.Item)
rhsResult = binaryOperator.RHS.Declaration.Visit(typePrinter).Type;
return $"{lhsResult} {binaryOperator.OpcodeStr} {rhsResult}";
case StatementClass.ConstructorReference:
var constructorExpr = (CXXConstructExpr) expr;
if (constructorExpr.Arguments.Count == 1 &&
constructorExpr.Arguments[0].Declaration is Enumeration.Item)
return constructorExpr.Arguments[0].Declaration.Visit(typePrinter).Type;
goto default;
default:
return expr.String;
}

Просмотреть файл

@ -2191,7 +2191,7 @@ namespace CppSharp.Generators.CSharp
string.Join(", ",
method.Parameters.Where(
p => p.Kind == ParameterKind.Regular).Select(
p => p.Ignore ? ExpressionPrinter.VisitExpression(p.DefaultArgument) : p.Name)));
p => p.Ignore ? ExpressionPrinter.VisitParameter(p) : p.Name)));
}
if (method.IsPure)
@ -2282,7 +2282,7 @@ namespace CppSharp.Generators.CSharp
return p.Type.IsPointerToPrimitiveType() && p.Usage == ParameterUsage.InOut && p.HasDefaultValue
? "ref param" + index++
: (( p.Type.TryGetClass(out @class) && @class.IsInterface) ? "param" + index++
: ExpressionPrinter.VisitExpression(p.DefaultArgument));
: ExpressionPrinter.VisitParameter(p));
}
private void GenerateOverloadCall(Function function)
@ -2305,7 +2305,7 @@ namespace CppSharp.Generators.CSharp
parameter.HasDefaultValue)
{
WriteLine("var param{0} = ({1}) {2};", j++, @class.OriginalClass.OriginalName,
ExpressionPrinter.VisitExpression(parameter.DefaultArgument));
ExpressionPrinter.VisitParameter(parameter));
}
}
@ -2921,7 +2921,7 @@ namespace CppSharp.Generators.CSharp
select string.Format("{0}{1} {2}", GetParameterUsage(param.Usage),
typeName, param.Name +
(param.DefaultArgument == null || !Options.GenerateDefaultValuesForArguments ?
string.Empty : " = " + ExpressionPrinter.VisitExpression(param.DefaultArgument))));
string.Empty : " = " + ExpressionPrinter.VisitParameter(param))));
}
#endregion

Просмотреть файл

@ -106,7 +106,6 @@ namespace CppSharp.Passes
private bool CheckForSimpleExpressions(Expression expression, ref string result, Type desugared)
{
return CheckFloatSyntax(desugared, expression, ref result) ||
CheckForBinaryOperator(desugared, expression, ref result) ||
CheckForEnumValue(desugared, expression, ref result) ||
CheckForDefaultChar(desugared, ref result);
}
@ -205,16 +204,6 @@ namespace CppSharp.Passes
argsBuilder.Append(')');
result = argsBuilder.ToString();
}
else
{
if (method.Parameters.Count > 0)
{
var paramType = method.Parameters[0].Type.SkipPointerRefs().Desugar();
Enumeration @enum;
if (paramType.TryGetEnum(out @enum))
result = TranslateEnumExpression(method, paramType, expression.String);
}
}
return expressionSupported ? true : (bool?) null;
}
@ -244,46 +233,12 @@ namespace CppSharp.Passes
return false;
}
private bool CheckForBinaryOperator(Type desugared, Expression expression,
ref string result)
{
if (expression.Class != StatementClass.BinaryOperator)
return false;
var binaryOperator = (BinaryOperator) expression;
var lhsResult = binaryOperator.LHS.String;
CheckForEnumValue(desugared, binaryOperator.LHS, ref lhsResult);
var rhsResult = binaryOperator.RHS.String;
CheckForEnumValue(desugared, binaryOperator.RHS, ref rhsResult);
result = string.Format("{0} {1} {2}", lhsResult,
binaryOperator.OpcodeStr, rhsResult);
return true;
}
private bool CheckForEnumValue(Type desugared, Statement statement,
ref string result)
{
var enumItem = statement.Declaration as Enumeration.Item;
if (enumItem != null)
{
if (desugared.IsPrimitiveType())
{
statement.Declaration = null;
result = string.Format("(int) {0}.{1}",
new CSharpTypePrinter(Context).VisitEnumDecl(
(Enumeration) enumItem.Namespace), enumItem.Name);
}
else
{
result = string.Format("{0}.{1}",
new CSharpTypePrinter(Context).VisitEnumDecl(
(Enumeration) enumItem.Namespace), enumItem.Name);
}
return true;
}
var call = statement.Declaration as Function;
if (call != null && statement.String != "0")

Просмотреть файл

@ -37,6 +37,8 @@ public class CommonTests : GeneratorTestFixture
using (var hasProtectedEnum = new HasProtectedEnum())
{
}
EnumWithUnderscores e = 0;
e.GetHashCode();
#pragma warning restore 0168
#pragma warning restore 0219

Просмотреть файл

@ -1218,3 +1218,5 @@ DLL_API void hasPointerParam(Foo* foo, int i);
DLL_API void hasPointerParam(const Foo& foo);
enum EmptyEnum { };
enum __enum_with_underscores { };

Просмотреть файл

@ -14,7 +14,6 @@ namespace CppSharp.Tests
public override void SetupPasses(Driver driver)
{
driver.Context.TranslationUnitPasses.RenameDeclsUpperCase(RenameTargets.Any);
driver.Context.TranslationUnitPasses.AddPass(new FunctionToInstanceMethodPass());
}