public override (IType ReturnType, bool IsConstant) Invocation(IExpression[] args, SourceRange source, DiagnosticsReport diagnostics)
            {
                var returnType = new IntType(source);

                if (args.Length != 1)
                {
                    diagnostics.AddError($"Expected 1 argument, found {args.Length}", source);
                }

                if (args.Length < 1)
                {
                    return(returnType, false);
                }

                var isConstant = false;
                var arg        = args[0];

                if (arg.Type is IArrayType)
                {
                    if (!arg.IsLValue)
                    {
                        Debug.Assert(false, "There is no way for an array to be non-lvalue");
                    }
                }
                else if (arg.Type is TypeNameType typeName)
                {
                    if (typeName.TypeDeclaration is not EnumDeclaration)
                    {
                        diagnostics.AddError($"Argument 1: cannot pass non-enum type '{typeName.TypeDeclaration.Name}' to COUNT_OF parameter", arg.Source);
                    }
                    else
                    {
                        isConstant = true;
                    }
                }
                else if (arg.Type is not ErrorType)
                {
                    diagnostics.AddError($"Argument 1: cannot pass '{arg.Type}' to COUNT_OF parameter, expected array reference or enum type name", arg.Source);
                }

                return(returnType, isConstant);
            }
            public override (IType ReturnType, bool IsConstant) Invocation(IExpression[] args, SourceRange source, DiagnosticsReport diagnostics)
            {
                var returnType = new IntType(source);

                if (args.Length != 1)
                {
                    diagnostics.AddError($"Expected 1 argument, found {args.Length}", source);
                }

                if (args.Length < 1)
                {
                    return(returnType, false);
                }

                var arg = args[0];

                if (arg.Type is not(EnumType or ErrorType))
                {
                    diagnostics.AddError($"Argument 1: cannot pass '{arg.Type}' to ENUM_TO_INT parameter, expected enum value", arg.Source);
                }

                return(returnType, arg.IsConstant);
            }
Пример #3
0
            public override (IType ReturnType, bool IsConstant) Invocation(IExpression[] args, SourceRange source, DiagnosticsReport diagnostics)
            {
                if (args.Length != 2)
                {
                    var err = new ErrorType(source, diagnostics, $"Expected 2 arguments, found {args.Length}");
                    if (args.Length < 1)
                    {
                        return(err, false);
                    }
                }

                IType returnType;
                var   arg1 = args[0];

                if (arg1.Type is TypeNameType typeName)
                {
                    if (typeName.TypeDeclaration is EnumDeclaration enumDecl)
                    {
                        returnType = enumDecl.CreateType(source);
                    }
                    else
                    {
                        returnType = new ErrorType(arg1.Source, diagnostics, $"Argument 1: cannot pass non-enum type '{typeName.TypeDeclaration.Name}' to INT_TO_ENUM first parameter");
                    }
                }
                else if (arg1.Type is ErrorType)
                {
                    returnType = arg1.Type;
                }
                else
                {
                    returnType = new ErrorType(arg1.Source, diagnostics, $"Argument 1: cannot pass '{arg1.Type}' to INT_TO_ENUM first parameter, expected enum type name");
                }

                var isConstant = false;

                if (args.Length >= 2)
                {
                    var arg2 = args[1];
                    isConstant = arg2.IsConstant;
                    var param2Ty = new IntType(arg2.Source);
                    if (!param2Ty.CanAssign(arg2.Type !, arg2.IsLValue))
                    {
                        diagnostics.AddError($"Argument  2: cannot pass '{arg2.Type}' as second parameter '{TypePrinter.ToString(param2Ty, "value", false)}'", arg2.Source);
                    }
                }

                return(returnType, isConstant);
            }