Пример #1
0
 /// <summary>
 /// Check if convert is necessary
 /// </summary>
 /// <param name="leftType">Type of the left.</param>
 /// <param name="rightType">Type of the right.</param>
 /// <returns>True if a cast is necessary between this two types</returns>
 public static bool NeedConvertForBinary(TypeBase leftType, TypeBase rightType)
 {
     return leftType != null && rightType != null && leftType != rightType
            &&
            !((leftType is ScalarType && (rightType is VectorType || rightType is MatrixType) && TypeBase.GetBaseType(rightType) == leftType)
              || (rightType is ScalarType && (leftType is VectorType || leftType is MatrixType) && TypeBase.GetBaseType(leftType) == rightType));
 }
Пример #2
0
 protected void Visit(TypeBase typeBase)
 {
     typeBase.TypeInference.Declaration = null;
     typeBase.TypeInference.TargetType = null;
     typeBase.TypeInference.ExpectedType = null;
     Visit((Node)typeBase);
 }
Пример #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Variable"/> class.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="name">The name.</param>
 /// <param name="initialValue">The initial value.</param>
 public Variable(TypeBase type, string name, Expression initialValue = null)
 {
     Type = type;
     Name = new Identifier(name);
     InitialValue = initialValue;
     Attributes = new List<AttributeBase>();
     Qualifiers = Qualifier.None;
 }
        private void Visit(TypeBase typeBase)
        {
            Visit((Node)typeBase);

            if (sourceManager.IsClassExists(typeBase.Name.Text))
            {
                FoundClasses.Add(typeBase.Name.Text);
            }
            else if (typeBase is ShaderTypeName)
            {
                // Special case for ShaderTypeName as we must generate an error if it is not found
                log.Error(XenkoMessageCode.ErrorClassNotFound, typeBase.Span, typeBase.Name.Text);
            }
        }
        /// <summary>
        /// Gets the type of the binary implicit conversion.
        /// </summary>
        /// <param name="span">The span.</param>
        /// <param name="left">The left.</param>
        /// <param name="right">The right.</param>
        /// <returns>The implicit conversion between between to two types</returns>
        protected override TypeBase GetDivideImplicitConversionType(SourceSpan span, TypeBase left, TypeBase right)
        {
            if (left.ResolveType().IsStreamsType() || right.ResolveType().IsStreamsType())
                return StreamsType.Streams;

            return base.GetDivideImplicitConversionType(span, left, right);
        }
Пример #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MethodDefinition"/> class.
 /// </summary>
 /// <param name="returntype">The returntype.</param>
 /// <param name="name">The name.</param>
 public MethodDefinition(TypeBase returntype, string name) : this()
 {
     ReturnType = returntype;
     Name = new Identifier(name);
     declaration = this;
 }
Пример #7
0
        public static int GetDimensionSize(TypeBase typeDeclaration, int dimension)
        {
            if (typeDeclaration is VectorType)
            {
                if (dimension != 1) return 1;
                return ((VectorType)typeDeclaration).Dimension;
            }

            if (typeDeclaration is MatrixType)
            {
                var matrixType = (MatrixType)typeDeclaration;
                return dimension == 0 ? matrixType.RowCount : matrixType.ColumnCount;
            }

            return 1;
        }
Пример #8
0
 /// <summary>
 /// Determines whether the specified type is a float/half/double.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <returns>
 ///   <c>true</c> if the specified type is float/half/double; otherwise, <c>false</c>.
 /// </returns>
 public static bool IsFloat(TypeBase type)
 {
     return(type == Float || type == Double || type == Half);
 }
Пример #9
0
        private void ChangeVariableType(Variable samplerVariable, TypeBase newType)
        {
            if (samplerVariable != null)
            {
                samplerVariable.Type = newType;
                if (samplerVariable is Parameter)
                {
                    return;
                }

                var variableInitialValue = samplerVariable.InitialValue as VariableReferenceExpression;
                if (variableInitialValue != null)
                {
                    this.ChangeVariableType(variableInitialValue.TypeInference.Declaration as Variable, newType);
                }
            }
        }
 protected static bool IsTextureType(TypeBase type)
 {
     // TODO we should improve AST type system
     return type is TextureType || (type is GenericBaseType && type.Name.Text.Contains("Texture"));
 }
Пример #11
0
 /// <summary>
 /// Checks the constraint.
 /// </summary>
 /// <param name="parameterType">Type of the parameter.</param>
 /// <param name="typeToCheck">The type to check.</param>
 /// <returns></returns>
 public bool CheckConstraint(GenericParameterType parameterType, TypeBase typeToCheck)
 {
     foreach (var genericParameterConstraint in ParameterConstraints)
     {
         if (genericParameterConstraint.Name == parameterType.Name)
         {
             return genericParameterConstraint.Constraint(typeToCheck);
         }
     }
     return false;
 }
Пример #12
0
        /// <summary>
        /// Gets the type of the binary implicit conversion.
        /// </summary>
        /// <param name="span">The span.</param>
        /// <param name="left">The left.</param>
        /// <param name="right">The right.</param>
        /// <returns>The implicit conversion between between to two types</returns>
        protected override TypeBase GetMultiplyImplicitConversionType(SourceSpan span, TypeBase left, TypeBase right)
        {
            if (left.ResolveType() is StreamsType || right.ResolveType() is StreamsType)
                return StreamsType.Streams;

            return base.GetMultiplyImplicitConversionType(span, left, right);
        }
Пример #13
0
 public static bool HasDimensions(TypeBase typeDeclaration)
 {
     return((typeDeclaration is ScalarType) || (typeDeclaration is VectorType) || (typeDeclaration is MatrixType));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="TypeReferenceExpression"/> class.
 /// </summary>
 /// <param name="type">The type.</param>
 public TypeReferenceExpression(TypeBase type)
 {
     Type = type;
 }
Пример #15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Parameter"/> class.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="name">The name.</param>
 /// <param name="initialValue">The initial value.</param>
 public Parameter(TypeBase type, string name = null, Expression initialValue = null)
     : base(type, name, initialValue)
 {
 }
Пример #16
0
 /// <summary>
 /// Determines whether the specified type is an integer.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <returns>
 ///   <c>true</c> if the specified type is an integer; otherwise, <c>false</c>.
 /// </returns>
 public static bool IsInteger(TypeBase type)
 {
     return(type == Int || type == UInt);
 }
        /// <summary>
        /// Finds the declaration inside the compositions and calls the base method too
        /// </summary>
        /// <param name="typeBase">the type of the object</param>
        /// <param name="memberName">the name of its member</param>
        /// <returns>a collection of all possible members</returns>
        protected override IEnumerable<IDeclaration> FindDeclarationsFromObject(TypeBase typeBase, string memberName)
        {
            if (typeBase == null)
            {
                yield break;
            }

            // look if it is a composition
            // the typebase is unique for each extern so there is no need to look for the right class
            //var mixin = compositionsVirtualTables.FirstOrDefault(x => ReferenceEquals(x.Key.ResolveType(), typeBase.ResolveType())).Value;

            var mixin = moduleMixins.FirstOrDefault(x => x.MixinName == typeBase.Name.Text);

            if (mixin != null)
            {
                foreach (var member in mixin.VirtualTable.Variables.Where(x => x.Variable.Name.Text == memberName))
                    yield return member.Variable;

                foreach (var member in mixin.VirtualTable.Methods.Where(x => x.Method.Name.Text == memberName))
                    yield return member.Method;

                if (mixin.MixinName == memberName)
                    yield return mixin.Shader;

                foreach (var dep in mixin.InheritanceList.Where(x => x.MixinName == memberName).Select(x => x.Shader))
                    yield return dep;
            }
            else
            {
                foreach (var item in base.FindDeclarationsFromObject(typeBase.ResolveType(), memberName))
                    yield return item;
            }
        }
Пример #18
0
        /// <summary>
        /// Gets the type of the binary implicit conversion.
        /// </summary>
        /// <param name="span">The span.</param>
        /// <param name="left">The left.</param>
        /// <param name="right">The right.</param>
        /// <param name="isBinaryOperator">if set to <c>true</c> [is binary operator].</param>
        /// <returns>
        /// The implicit conversion between between to two types
        /// </returns>
        protected virtual TypeBase GetBinaryImplicitConversionType(SourceSpan span, TypeBase left, TypeBase right, bool isBinaryOperator)
        {
            var result = CastHelper.GetBinaryImplicitConversionType(left, right, isBinaryOperator);

            if (result == null)
                Error(MessageCode.ErrorBinaryTypeDeduction, span, left, right);

            return result;
        }
Пример #19
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MethodDefinition"/> class.
 /// </summary>
 /// <param name="returntype">The returntype.</param>
 /// <param name="name">The name.</param>
 public MethodDefinition(TypeBase returntype, string name) : this()
 {
     ReturnType  = returntype;
     Name        = new Identifier(name);
     declaration = this;
 }
Пример #20
0
        /// <summary>
        /// Gets the type of the binary implicit conversion.
        /// </summary>
        /// <param name="span">The span.</param>
        /// <param name="left">The left.</param>
        /// <param name="right">The right.</param>
        /// <returns>The implicit conversion between between to two types</returns>
        protected virtual TypeBase GetDivideImplicitConversionType(SourceSpan span, TypeBase left, TypeBase right)
        {
            var result = CastHelper.GetDivideImplicitConversionType(left.ResolveType(), right.ResolveType());

            if (result == null)
                Error(MessageCode.ErrorBinaryTypeDeduction, span, left, right);

            return result;
        }
Пример #21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Parameter"/> class.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="name">The name.</param>
 /// <param name="initialValue">The initial value.</param>
 public Parameter(TypeBase type, string name = null, Expression initialValue = null)
     : base(type, name, initialValue)
 {
 }
Пример #22
0
 public static bool IsInputOutputStream(TypeBase type)
 {
     var streamType = type as StreamsType;
     if (streamType == null)
     {
         return false;
     }
     return streamType.IsInputOutput;
 }
 protected static bool IsBufferType(TypeBase type)
 {
     // TODO we should improve AST type system
     return type is GenericBaseType && type.Name.Text.Contains("Buffer");
 }
Пример #24
0
 public static TypeBase GetBaseType(TypeBase type)
 {
     if (type is MatrixType) return ((MatrixType)type).Type;
     if (type is VectorType) return ((VectorType)type).Type;
     return type;
 }
Пример #25
0
        /// <summary>
        /// Gets the type of the binary implicit scalar conversion.
        /// </summary>
        /// <param name="span">The span.</param>
        /// <param name="left">The left.</param>
        /// <param name="right">The right.</param>
        /// <returns>
        /// The implicit conversion between the two scalar types
        /// </returns>
        protected ScalarType GetBinaryImplicitScalarConversionType(SourceSpan span, TypeBase left, TypeBase right)
        {
            var result = CastHelper.GetBinaryImplicitScalarConversionType(left, right);

            if (result == null)
                Error(MessageCode.ErrorScalarTypeConversion, span, left, right);
            return result;
        }
Пример #26
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TypeName"/> class.
 /// </summary>
 /// <param name="typeBase">The type base.</param>
 public TypeName(TypeBase typeBase)
     : base(typeBase.Name)
 {
     TypeInference.TargetType = typeBase;
 }
Пример #27
0
        /// <summary>
        /// Creates a type based on a new base type. If type is a matrix or vector, then the base type is changed to match the newBaseType.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="newBaseType">New type of the base.</param>
        /// <returns>A new type</returns>
        public static TypeBase CreateWithBaseType(TypeBase type, ScalarType newBaseType)
        {
            if (type is MatrixType)
                return new MatrixType(newBaseType, ((MatrixType)type).RowCount, ((MatrixType)type).ColumnCount);

            if (type is VectorType)
                return new VectorType(newBaseType, ((VectorType)type).Dimension);

            return newBaseType;
        }
Пример #28
0
 public void Visit(TypeBase typeReference)
 {
     Visit((Node)typeReference);
     AddReference(GetDeclarationContainer(), (Node)typeReference.TypeInference.Declaration);
 }
Пример #29
0
 public static bool HasDimensions(TypeBase typeDeclaration)
 {
     return (typeDeclaration is ScalarType) || (typeDeclaration is VectorType) || (typeDeclaration is MatrixType);
 }
Пример #30
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TypeName"/> class.
 /// </summary>
 /// <param name="typeBase">The type base.</param>
 public TypeName(TypeBase typeBase)
     : base(typeBase.Name)
 {
     TypeInference.TargetType = typeBase;
 }
Пример #31
0
        /// <summary>
        /// Equalses the specified other.
        /// </summary>
        /// <param name="other">
        /// The other.
        /// </param>
        /// <returns>
        /// <c>true</c> if the specified <see cref="TypeBase"/> is equal to this instance; otherwise, <c>false</c>.
        /// </returns>
        public bool Equals(TypeBase other)
        {
            if (ReferenceEquals(null, other))
            {
                return false;
            }

            if (ReferenceEquals(this, other))
            {
                return true;
            }

            return Equals(other.Name, Name);
        }
Пример #32
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TypeReferenceExpression"/> class.
 /// </summary>
 /// <param name="type">The type.</param>
 public TypeReferenceExpression(TypeBase type)
 {
     Type = type;
 }
        /// <summary>
        /// Gets the type of the binary implicit conversion.
        /// </summary>
        /// <param name="span">The span.</param>
        /// <param name="left">The left.</param>
        /// <param name="right">The right.</param>
        /// <param name="isBinaryOperator">if set to <c>true</c> [is binary operator].</param>
        /// <returns>
        /// The implicit conversion between between to two types
        /// </returns>
        protected override TypeBase GetBinaryImplicitConversionType(SourceSpan span, TypeBase left, TypeBase right, bool isBinaryOperator)
        {
            if (left.ResolveType().IsStreamsType() || right.ResolveType().IsStreamsType())
                return StreamsType.Streams;

            return base.GetBinaryImplicitConversionType(span, left, right, isBinaryOperator);
        }
Пример #34
0
        private Expression Cast(TypeBase fromType, TypeBase toType, Expression expression)
        {
            if (fromType != null && toType != null)
            {
                if (fromType != toType)
                {
                    var castMethod = new MethodInvocationExpression(new TypeReferenceExpression(toType));
                    castMethod.Arguments.Add(expression);
                    expression = castMethod;
                    expression.TypeInference.TargetType = toType;
                }
            }

            return expression;
        }
 /// <summary>
 /// Find the base type in case of array
 /// </summary>
 /// <param name="typeBase">the type to explore</param>
 /// <returns>the base type</returns>
 private static TypeBase FindFinalType(TypeBase typeBase)
 {
     if (typeBase is ArrayType)
         return FindFinalType((typeBase as ArrayType).Type);
     return typeBase.ResolveType();
 }
Пример #36
0
 private Expression ConvertExpressionToBool(Expression expression, TypeBase typeToCheck)
 {
     if (typeToCheck != ScalarType.Bool)
         return new MethodInvocationExpression(new TypeReferenceExpression(ScalarType.Bool), expression);
     return expression;
 }
        /// <summary>
        /// Tests the arguments of the method - check streams type here
        /// </summary>
        /// <param name="argTypeBase">the argument typebase</param>
        /// <param name="expectedTypeBase">the expected typebase</param>
        /// <param name="argType">the argument type</param>
        /// <param name="expectedType">the expected type</param>
        /// <param name="score">the score of the overload</param>
        /// <returns>true if the overload is correct, false otherwise</returns>
        protected override bool TestMethodInvocationArgument(TypeBase argTypeBase, TypeBase expectedTypeBase, TypeBase argType, TypeBase expectedType, ref int score)
        {
            if (argTypeBase == StreamsType.Streams && expectedTypeBase == StreamsType.Output) // streams and output are the same
                return true;
            if (argTypeBase.IsStreamsType() && expectedType.IsStreamsType())
                return argTypeBase == expectedType;

            return base.TestMethodInvocationArgument(argTypeBase, expectedTypeBase, argType, expectedType, ref score);
        }
Пример #38
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ArrayType"/> class.
 /// </summary>
 /// <param name="type">The type.</param>
 /// <param name="dimensions">The dimensions.</param>
 public ArrayType(TypeBase type, params Expression[] dimensions) : base("$array")
 {
     Type       = type;
     Dimensions = new List <Expression>();
     Dimensions.AddRange(dimensions);
 }