Ejemplo n.º 1
0
		public override LiteralExpression VisitLiteralValue(LiteralExpression expression)
		{
			if (expression.GetValue() == null)
				_writer.WriteNull();
			else
			{
				switch (expression.PrimitiveType)
				{
					case PrimitiveType.None:
						if (expression.ExpressionType != typeof(DateTime))
							goto case PrimitiveType.Object;
						_writer.WriteLiteral(expression.AsDateTime);
						break;

					case PrimitiveType.Object:
						_writer.WriteLiteral(expression.GetValue());
						break;

					case PrimitiveType.Boolean:
						_writer.WriteLiteral(expression.AsBoolean);
						break;

					case PrimitiveType.Byte:
					case PrimitiveType.SByte:
					case PrimitiveType.Int16:
					case PrimitiveType.UInt16:
					case PrimitiveType.UInt32:
					case PrimitiveType.UInt64:
						_writer.WriteLiteral(expression.AsInt64, expression.ExpressionType);
						break;

					case PrimitiveType.Int32:
					case PrimitiveType.Int64:
						_writer.WriteLiteral(expression.AsInt64);
						break;

					case PrimitiveType.Char:
						_writer.WriteLiteral(expression.AsString[0]);
						break;

					case PrimitiveType.String:
						_writer.WriteLiteral(expression.AsString);
						break;

					case PrimitiveType.Single:
					case PrimitiveType.Decimal:
						_writer.WriteLiteral(expression.AsDouble, expression.ExpressionType);
						break;

					case PrimitiveType.Double:
						_writer.WriteLiteral(expression.AsDouble);
						break;

					default:
						throw ExceptionBuilder.UnhandledCaseLabel(expression.PrimitiveType);
				}
			}

			return expression;
		}
Ejemplo n.º 2
0
		private static bool VisitLiteralValue(LiteralExpression node1, LiteralExpression node2)
		{
			return node2 != null &&
			       Equals(node1.GetValue(), node2.GetValue());
		}
Ejemplo n.º 3
0
		public virtual LiteralExpression VisitLiteralValue(LiteralExpression expression)
		{
			return expression;
		}
Ejemplo n.º 4
0
		public override LiteralExpression VisitLiteralValue(LiteralExpression expression)
		{
			_ilEmitContext.AddParameter(expression, expression.GetValue(), expression.ExpressionType);
			return expression;
		}
Ejemplo n.º 5
0
		public override LiteralExpression VisitLiteralValue(LiteralExpression expression)
		{
			ILParameterDeclaration ilParameterDeclaration = _ilEmitContext.GetParameters(expression)[0];
			EmitLoadParameter(ilParameterDeclaration);

			return expression;
		}
Ejemplo n.º 6
0
		public override LiteralExpression VisitLiteralValue(LiteralExpression expression)
		{
			_xmlWriter.WriteStartElement("literalExpression");

			_xmlWriter.WriteStartElement("value");
			WriteTypeAttribute(expression.ExpressionType);

			object value = expression.GetValue();
			if (NullHelper.IsNull(value))
				_xmlWriter.WriteAttributeString("value", "<null>");
			else
				_xmlWriter.WriteAttributeString("value", value.ToString());
			_xmlWriter.WriteEndElement();

			_xmlWriter.WriteEndElement();

			return expression;
		}
Ejemplo n.º 7
0
        public override ExpressionNode VisitCaseExpression(CaseExpression expression)
        {
            // Ensure all nested expressions are fully resolved.

            base.VisitCaseExpression(expression);

            for (int i = 0; i < expression.WhenExpressions.Length; i++)
            {
                if (expression.WhenExpressions[i].ExpressionType == null || expression.ThenExpressions[i].ExpressionType == null)
                {
                    return(expression);
                }
            }

            if (expression.ElseExpression != null && expression.ElseExpression.ExpressionType == null)
            {
                return(expression);
            }

            // Ok, all nested expressions could be fully resolved. Lets validate the CASE expression.

            // The semantic of CASE says that if no expression incl. ELSE does match the result is NULL.
            // So having an ELSE expression that returns NULL is quite redundant.

            LiteralExpression elseAsLiteral = expression.ElseExpression as LiteralExpression;

            if (elseAsLiteral != null && elseAsLiteral.IsNullValue)
            {
                expression.ElseExpression = null;
            }

            // All WHEN expressions must evaluate to bool.

            foreach (ExpressionNode whenExpression in expression.WhenExpressions)
            {
                if (whenExpression.ExpressionType != typeof(bool) && whenExpression.ExpressionType != typeof(DBNull))
                {
                    _errorReporter.WhenMustEvaluateToBoolIfCaseInputIsOmitted(whenExpression);
                }
            }

            // Check that all result expression incl. else share a common type.
            //
            // To do this and to support good error reporting we first try to find
            // the best common type. Any needed conversions or type errors are
            // ignored.

            Type commonResultType = expression.ThenExpressions[0].ExpressionType;

            for (int i = 1; i < expression.ThenExpressions.Length; i++)
            {
                commonResultType = _binder.ChooseBetterTypeConversion(commonResultType, expression.ThenExpressions[i].ExpressionType);
            }

            if (expression.ElseExpression != null)
            {
                commonResultType = _binder.ChooseBetterTypeConversion(expression.ElseExpression.ExpressionType, commonResultType);
            }

            // Now we know that commonResultType is the best type for all result expressions.
            // Insert cast nodes for all expressions that have a different type but are
            // implicit convertible and report errors for all expressions that not convertible.

            if (expression.ElseExpression != null)
            {
                expression.ElseExpression = _binder.ConvertExpressionIfRequired(expression.ElseExpression, commonResultType);
            }

            for (int i = 0; i < expression.ThenExpressions.Length; i++)
            {
                expression.ThenExpressions[i] = _binder.ConvertExpressionIfRequired(expression.ThenExpressions[i], commonResultType);
            }

            expression.ResultType = commonResultType;

            return(expression);
        }