private Maybe<Expression> GenerateNullableMethodCall(Type type, Expression instance, int errorPos, string id, TokenId nextToken)
{
if (IsNullableType(type))
{
var expression = TryParseMemberAccess(type.GetGenericArguments()[0], Expression.Property(instance, "Value"), nextToken, errorPos, id);
return expression.Select(e => new
{
expression = e,
protectedExpression = !IsNullableType(e.Type) && e.Type.IsValueType
? Expression.Convert(e, typeof(Nullable<>).MakeGenericType(e.Type))
: e
}).Select(notNull =>
{
var condition = (Expression) Expression.Condition(Expression.Equal(instance, Expression.Constant(null, type)),
Expression.Constant(null, notNull.protectedExpression.Type), notNull.protectedExpression);
// attach raw expression to condition to get it name in GetConditionalName()
ExtensionsProvider.SetValue(condition, notNull.expression);
return condition;
}
).ToMaybe();
}
return Maybe.Nothing;
}