4 using SiliconStudio.Shaders.Ast;
5 using SiliconStudio.Shaders.Parser;
7 namespace SiliconStudio.Shaders.Analysis
19 Visit((
Node)expression);
21 var unaryType = expression.TypeInference.TargetType;
22 var inputType = expression.Expression.TypeInference.TargetType;
23 if (unaryType == null || inputType == null)
35 Visit((
Node)expression);
37 var leftType = expression.Left.TypeInference.TargetType;
38 var rightType = expression.Right.TypeInference.TargetType;
39 var returnType = expression.TypeInference.ExpectedType ?? expression.TypeInference.TargetType;
41 bool isNumericOperator =
true;
43 switch (expression.Operator)
45 case BinaryOperator.LogicalAnd:
46 case BinaryOperator.LogicalOr:
47 isNumericOperator =
false;
48 returnType = GetBinaryImplicitConversionType(expression.
Span, leftType, rightType,
true);
49 expression.TypeInference.TargetType = returnType;
51 case BinaryOperator.Less:
52 case BinaryOperator.LessEqual:
53 case BinaryOperator.Greater:
54 case BinaryOperator.GreaterEqual:
55 case BinaryOperator.Equality:
56 case BinaryOperator.Inequality:
57 isNumericOperator =
false;
58 returnType = GetBinaryImplicitConversionType(expression.
Span, leftType, rightType,
false);
60 TypeBase resultType = ScalarType.Bool;
63 resultType =
new VectorType(
ScalarType.
Bool, ((VectorType)returnType).Dimension);
67 var matrixType = (MatrixType)returnType;
68 resultType =
new MatrixType(
ScalarType.
Bool, matrixType.RowCount, matrixType.ColumnCount);
70 expression.TypeInference.TargetType = resultType;
74 if (returnType != null)
78 var typeToCheck = leftType ?? rightType;
79 if (typeToCheck != null)
80 return ConvertExpressionToBool(expression, typeToCheck);
84 if (!isNumericOperator || CastHelper.NeedConvertForBinary(leftType, returnType))
85 expression.Left = Cast(leftType, returnType, expression.Left);
86 if (!isNumericOperator || CastHelper.NeedConvertForBinary(rightType, returnType))
87 expression.Right = Cast(rightType, returnType, expression.Right);
104 Visit((
Node)ifStatement);
106 var conditionType = ifStatement.Condition.TypeInference.TargetType;
109 ifStatement.Condition = ConvertExpressionToBool(ifStatement.Condition, conditionType);
117 Visit((
Node)conditionalExpression);
119 var leftType = conditionalExpression.Left.TypeInference.TargetType;
120 var rightType = conditionalExpression.Right.TypeInference.TargetType;
124 conditionalExpression.Left = Cast(leftType, rightType, conditionalExpression.Left);
128 conditionalExpression.Right = Cast(rightType, leftType, conditionalExpression.Right);
135 if (fromType != null && toType != null)
137 if (fromType != toType)
140 castMethod.Arguments.Add(expression);
141 expression = castMethod;
142 expression.TypeInference.TargetType = toType;
153 Visit((
Node)returnStatement);
155 if (returnStatement.Value != null)
157 var expressionType = returnStatement.Value.TypeInference.TargetType;
158 if (expressionType != null)
159 returnStatement.Value = Cast(expressionType, returnStatement.Value.TypeInference.ExpectedType ?? expressionType, returnStatement.Value);
167 Visit((
Node)variable);
169 if (variable.InitialValue != null)
171 var expressionType = variable.InitialValue.TypeInference.TargetType;
174 variable.InitialValue = Cast(expressionType, variable.Type.ResolveType(), variable.InitialValue);
183 Visit((
Node)expression);
185 var expressionType = expression.Target.TypeInference.TargetType;
186 var targetType = expression.Target.TypeInference.ExpectedType ?? expressionType;
187 expression.Value = Cast(expression.Value.TypeInference.TargetType, targetType, expression.Value);
194 Visit((
Node)expression);
196 var indexerType = expression.Index.TypeInference.TargetType;
197 if (indexerType != null)
199 var baseType = TypeBase.GetBaseType(indexerType);
201 expression.Index = Cast(indexerType,
ScalarType.
Int, expression.Index);
209 Visit((
Node)expression);
212 for (
int i = 0; i < expression.Arguments.Count; ++i)
214 var argument = expression.Arguments[i];
215 var targetType = argument.TypeInference.TargetType;
216 if (targetType != null && !(targetType is
ObjectType))
217 expression.Arguments[i] = Cast(targetType, argument.TypeInference.ExpectedType ?? targetType, argument);
222 public override void Run()
Base class for all vector types
static readonly ScalarType Bool
Scalar bool.
Describes a binary expression.
static readonly ScalarType Int
Scalar int.
UnaryOperator
Unary operator used in all binary expressions (except assignment expression).
CastAnalysis(ParsingResult result)
SourceSpan Span
Gets or sets the source span.
void Visit(AssignmentExpression expression)
Shader Shader
Gets or sets the shader.
virtual void Visit(IfStatement ifStatement)
static readonly ScalarType Float
Sclar float.
A reference to a variable.
Expression Visit(BinaryExpression expression)
void Visit(IndexerExpression expression)
Expression Visit(UnaryExpression expression)
void Visit(MethodInvocationExpression expression)
static readonly ScalarType Double
Scalar double.
void Visit(Variable variable)
override void Run()
Runs this instance.
void Visit(ReturnStatement returnStatement)
void Visit(ConditionalExpression conditionalExpression)