示例#1
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);
            }