private static bool CheckForSimpleExpressions(BindingContext context, ExpressionObsolete expression, ref string result, Type desugared) { return(CheckFloatSyntax(desugared, expression, ref result) || CheckForEnumValue(context, desugared, expression, ref result) || CheckForChar(context, desugared, ref result) || CheckForString(context, desugared, ref result)); }
public string VisitExpression(ExpressionObsolete expr) { switch (expr.Class) { case StatementClass.Call: var callExpr = (CallExprObsolete)expr; switch (callExpr.Declaration.GenerationKind) { case GenerationKind.Generate: var args = string.Join(", ", callExpr.Arguments.Select(VisitExpression)); return($"{typePrinter.VisitDeclaration(callExpr.Declaration.Namespace)}.{callExpr.Declaration.Name}({args})"); case GenerationKind.Internal: // a non-ctor can only be internal if it's been converted to a property var property = ((Class)callExpr.Declaration.Namespace).Properties.First( p => p.GetMethod == callExpr.Declaration); return($"{typePrinter.VisitDeclaration(callExpr.Declaration.Namespace)}.{property.Name}"); default: return(expr.String); } case StatementClass.DeclarationReference: if (expr.Declaration is Variable || expr.Declaration is Enumeration.Item) { return(expr.Declaration.Visit(typePrinter).Type); } goto default; case StatementClass.BinaryOperator: var binaryOperator = (BinaryOperatorObsolete)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 = (CXXConstructExprObsolete)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); } }
public static bool?PrintExpression(BindingContext context, Function function, Type type, ExpressionObsolete expression, bool allowDefaultLiteral, ref string result) { var desugared = type.Desugar(); if (desugared.IsAddress() && !desugared.IsPrimitiveTypeConvertibleToRef() && (expression.String == "0" || expression.String == "nullptr")) { result = desugared.GetPointee()?.Desugar() is FunctionType ? "null" : (allowDefaultLiteral ? "default" : $"default({desugared})"); return(true); } // constants are obtained through dynamic calls at present so they are not compile-time values in target languages if (expression.Declaration is Variable || (!context.Options.MarshalCharAsManagedChar && desugared.IsPrimitiveType(PrimitiveType.UChar))) { return(null); } if (desugared.IsPrimitiveTypeConvertibleToRef()) { var method = function as Method; if (method != null && method.IsConstructor) { result = string.Empty; return(false); } return(null); } if (CheckForDefaultPointer(context, desugared, ref result)) { return(true); } if (expression.Class == StatementClass.Call) { if (expression.Declaration.Ignore) { result = null; return(false); } return(null); } var defaultConstruct = CheckForDefaultConstruct(context, desugared, expression, ref result); if (defaultConstruct != false) { return(defaultConstruct); } return(CheckForSimpleExpressions(context, expression, ref result, desugared)); }
private static bool?CheckForDefaultConstruct(BindingContext context, Type desugared, ExpressionObsolete expression, ref string result) { var type = desugared.GetFinalPointee() ?? desugared; Class decl; if (!type.TryGetClass(out decl)) { return(false); } var ctor = expression as CXXConstructExprObsolete; var typePrinter = new CSharpTypePrinter(context); typePrinter.PushMarshalKind(MarshalKind.DefaultExpression); var typePrinterResult = type.Visit(typePrinter).Type; TypeMap typeMap; if (context.TypeMaps.FindTypeMap(type, out typeMap)) { var typePrinterContext = new TypePrinterContext() { Kind = typePrinter.Kind, MarshalKind = typePrinter.MarshalKind, Type = type }; var typeInSignature = typeMap.CSharpSignatureType(typePrinterContext) .SkipPointerRefs().Desugar(); Enumeration @enum; if (typeInSignature.TryGetEnum(out @enum)) { if (ctor != null && (ctor.Arguments.Count == 0 || HasSingleZeroArgExpression((Function)ctor.Declaration))) { result = "0"; return(true); } return(false); } if (ctor != null && typePrinterResult == "string" && ctor.Arguments.Count == 0) { result = "\"\""; return(true); } } if (ctor == null) { CheckForSimpleExpressions(context, expression, ref result, desugared); return(decl.IsValueType ? (bool?)false : null); } var method = (Method)expression.Declaration; var expressionSupported = decl.IsValueType && method.Parameters.Count == 0; if (expression.String.Contains('(') || expression.String.StartsWith("{")) { var argsBuilder = new StringBuilder("new "); argsBuilder.Append(typePrinterResult); argsBuilder.Append('('); for (var i = 0; i < ctor.Arguments.Count; i++) { var argument = ctor.Arguments[i]; var argResult = argument.String; expressionSupported &= PrintExpression(context, method, method.Parameters[i].Type.Desugar(), argument, allowDefaultLiteral: false, ref argResult) ?? false; argsBuilder.Append(argResult); if (i < ctor.Arguments.Count - 1) { argsBuilder.Append(", "); } } argsBuilder.Append(')'); result = argsBuilder.ToString(); } return(expressionSupported ? true : (bool?)null); }
private bool CheckForSimpleExpressions(ExpressionObsolete expression, ref string result, Type desugared) { return(CheckFloatSyntax(desugared, expression, ref result) || CheckForEnumValue(desugared, expression, ref result) || CheckForDefaultChar(desugared, ref result)); }
public static string CSharpValue(this ExpressionObsolete value, CSharpExpressionPrinter printer) { return value.Visit(printer); }