示例#1
0
 public PredefinedMember(ModuleContainer module, BuiltinTypeSpec type, string name, params TypeSpec[] types)
     : this(module, type, MemberFilter.Method(name, 0, ParametersCompiled.CreateFullyResolved(types), null))
 {
 }
示例#2
0
文件: nullable.cs 项目: ztone/mono
 public static MethodSpec GetGetValueOrDefault(TypeSpec nullableType)
 {
     return((MethodSpec)MemberCache.FindMember(nullableType,
                                               MemberFilter.Method("GetValueOrDefault", 0, ParametersCompiled.EmptyReadOnlyParameters, null), BindingRestriction.None));
 }
示例#3
0
        public PredefinedMembers(ModuleContainer module)
        {
            var types  = module.PredefinedTypes;
            var atypes = module.PredefinedAttributes;
            var btypes = module.Compiler.BuiltinTypes;

            ActivatorCreateInstance = new PredefinedMember <MethodSpec> (module, types.Activator,
                                                                         MemberFilter.Method("CreateInstance", 1, ParametersCompiled.EmptyReadOnlyParameters, null));

            DecimalCtor = new PredefinedMember <MethodSpec> (module, btypes.Decimal,
                                                             MemberFilter.Constructor(ParametersCompiled.CreateFullyResolved(
                                                                                          btypes.Int, btypes.Int, btypes.Int, btypes.Bool, btypes.Byte)));

            DecimalCtorInt = new PredefinedMember <MethodSpec> (module, btypes.Decimal,
                                                                MemberFilter.Constructor(ParametersCompiled.CreateFullyResolved(btypes.Int)));

            DecimalCtorLong = new PredefinedMember <MethodSpec> (module, btypes.Decimal,
                                                                 MemberFilter.Constructor(ParametersCompiled.CreateFullyResolved(btypes.Long)));

            DecimalConstantAttributeCtor = new PredefinedMember <MethodSpec> (module, atypes.DecimalConstant,
                                                                              MemberFilter.Constructor(ParametersCompiled.CreateFullyResolved(
                                                                                                           btypes.Byte, btypes.Byte, btypes.UInt, btypes.UInt, btypes.UInt)));

            DefaultMemberAttributeCtor = new PredefinedMember <MethodSpec> (module, atypes.DefaultMember,
                                                                            MemberFilter.Constructor(ParametersCompiled.CreateFullyResolved(btypes.String)));

            DelegateCombine = new PredefinedMember <MethodSpec> (module, btypes.Delegate, "Combine", btypes.Delegate, btypes.Delegate);
            DelegateRemove  = new PredefinedMember <MethodSpec> (module, btypes.Delegate, "Remove", btypes.Delegate, btypes.Delegate);

            DelegateEqual = new PredefinedMember <MethodSpec> (module, btypes.Delegate,
                                                               new MemberFilter(Operator.GetMetadataName(Operator.OpType.Equality), 0, MemberKind.Operator, null, btypes.Bool));

            DelegateInequal = new PredefinedMember <MethodSpec> (module, btypes.Delegate,
                                                                 new MemberFilter(Operator.GetMetadataName(Operator.OpType.Inequality), 0, MemberKind.Operator, null, btypes.Bool));

            DynamicAttributeCtor = new PredefinedMember <MethodSpec> (module, atypes.Dynamic,
                                                                      MemberFilter.Constructor(ParametersCompiled.CreateFullyResolved(
                                                                                                   ArrayContainer.MakeType(module, btypes.Bool))));

            FieldInfoGetFieldFromHandle = new PredefinedMember <MethodSpec> (module, types.FieldInfo,
                                                                             "GetFieldFromHandle", MemberKind.Method, types.RuntimeFieldHandle);

            FieldInfoGetFieldFromHandle2 = new PredefinedMember <MethodSpec> (module, types.FieldInfo,
                                                                              "GetFieldFromHandle", MemberKind.Method, types.RuntimeFieldHandle, new PredefinedType(btypes.RuntimeTypeHandle));

            FixedBufferAttributeCtor = new PredefinedMember <MethodSpec> (module, atypes.FixedBuffer,
                                                                          MemberFilter.Constructor(ParametersCompiled.CreateFullyResolved(btypes.Type, btypes.Int)));

            IDisposableDispose = new PredefinedMember <MethodSpec> (module, btypes.IDisposable, "Dispose", TypeSpec.EmptyTypes);

            IEnumerableGetEnumerator = new PredefinedMember <MethodSpec> (module, btypes.IEnumerable,
                                                                          "GetEnumerator", TypeSpec.EmptyTypes);

            InterlockedCompareExchange = new PredefinedMember <MethodSpec> (module, types.Interlocked,
                                                                            MemberFilter.Method("CompareExchange", 0,
                                                                                                new ParametersImported(
                                                                                                    new[] {
                new ParameterData(null, Parameter.Modifier.REF),
                new ParameterData(null, Parameter.Modifier.NONE),
                new ParameterData(null, Parameter.Modifier.NONE)
            },
                                                                                                    new[] {
                btypes.Int, btypes.Int, btypes.Int
            },
                                                                                                    false),
                                                                                                btypes.Int));

            InterlockedCompareExchange_T = new PredefinedMember <MethodSpec> (module, types.Interlocked,
                                                                              MemberFilter.Method("CompareExchange", 1,
                                                                                                  new ParametersImported(
                                                                                                      new[] {
                new ParameterData(null, Parameter.Modifier.REF),
                new ParameterData(null, Parameter.Modifier.NONE),
                new ParameterData(null, Parameter.Modifier.NONE)
            },
                                                                                                      new[] {
                new TypeParameterSpec(0, null, SpecialConstraint.None, Variance.None, null),
                new TypeParameterSpec(0, null, SpecialConstraint.None, Variance.None, null),
                new TypeParameterSpec(0, null, SpecialConstraint.None, Variance.None, null),
            }, false),
                                                                                                  null));

            MethodInfoGetMethodFromHandle = new PredefinedMember <MethodSpec> (module, types.MethodBase,
                                                                               "GetMethodFromHandle", MemberKind.Method, types.RuntimeMethodHandle);

            MethodInfoGetMethodFromHandle2 = new PredefinedMember <MethodSpec> (module, types.MethodBase,
                                                                                "GetMethodFromHandle", MemberKind.Method, types.RuntimeMethodHandle, new PredefinedType(btypes.RuntimeTypeHandle));

            MonitorEnter = new PredefinedMember <MethodSpec> (module, types.Monitor, "Enter", btypes.Object);

            MonitorEnter_v4 = new PredefinedMember <MethodSpec> (module, types.Monitor,
                                                                 MemberFilter.Method("Enter", 0,
                                                                                     new ParametersImported(new[] {
                new ParameterData(null, Parameter.Modifier.NONE),
                new ParameterData(null, Parameter.Modifier.REF)
            },
                                                                                                            new[] {
                btypes.Object, btypes.Bool
            }, false), null));

            MonitorExit = new PredefinedMember <MethodSpec> (module, types.Monitor, "Exit", btypes.Object);

            RuntimeCompatibilityWrapNonExceptionThrows = new PredefinedMember <PropertySpec> (module, atypes.RuntimeCompatibility,
                                                                                              MemberFilter.Property("WrapNonExceptionThrows", btypes.Bool));

            RuntimeHelpersInitializeArray = new PredefinedMember <MethodSpec> (module, types.RuntimeHelpers,
                                                                               "InitializeArray", btypes.Array, btypes.RuntimeFieldHandle);

            RuntimeHelpersOffsetToStringData = new PredefinedMember <PropertySpec> (module, types.RuntimeHelpers,
                                                                                    MemberFilter.Property("OffsetToStringData", btypes.Int));

            SecurityActionRequestMinimum = new PredefinedMember <ConstSpec> (module, types.SecurityAction, "RequestMinimum",
                                                                             MemberKind.Field, types.SecurityAction);

            StringEmpty = new PredefinedMember <FieldSpec> (module, btypes.String, MemberFilter.Field("Empty", btypes.String));

            StringEqual = new PredefinedMember <MethodSpec> (module, btypes.String,
                                                             new MemberFilter(Operator.GetMetadataName(Operator.OpType.Equality), 0, MemberKind.Operator, null, btypes.Bool));

            StringInequal = new PredefinedMember <MethodSpec> (module, btypes.String,
                                                               new MemberFilter(Operator.GetMetadataName(Operator.OpType.Inequality), 0, MemberKind.Operator, null, btypes.Bool));

            StructLayoutAttributeCtor = new PredefinedMember <MethodSpec> (module, atypes.StructLayout,
                                                                           MemberFilter.Constructor(ParametersCompiled.CreateFullyResolved(btypes.Short)));

            StructLayoutCharSet = new PredefinedMember <FieldSpec> (module, atypes.StructLayout, "CharSet",
                                                                    MemberKind.Field, types.CharSet);

            StructLayoutPack = new PredefinedMember <FieldSpec> (module, atypes.StructLayout,
                                                                 MemberFilter.Field("Pack", btypes.Int));

            StructLayoutSize = new PredefinedMember <FieldSpec> (module, atypes.StructLayout,
                                                                 MemberFilter.Field("Size", btypes.Int));

            TypeGetTypeFromHandle = new PredefinedMember <MethodSpec> (module, btypes.Type, "GetTypeFromHandle", btypes.RuntimeTypeHandle);
        }
示例#4
0
        public override bool Resolve(BlockContext bc)
        {
            if (bc.CurrentBlock is Linq.QueryBlock)
            {
                bc.Report.Error(1995, loc,
                                "The `await' operator may only be used in a query expression within the first collection expression of the initial `from' clause or within the collection expression of a `join' clause");
                return(false);
            }

            if (!base.Resolve(bc))
            {
                return(false);
            }

            Arguments args = new Arguments(0);

            type = expr.Type;

            //
            // The await expression is of dynamic type
            //
            if (type.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
            {
                result_type = type;
                expr        = new Invocation(new MemberAccess(expr, "GetAwaiter"), args).Resolve(bc);
                return(true);
            }

            //
            // Check whether the expression is awaitable
            //
            Expression ama = new AwaitableMemberAccess(expr).Resolve(bc);

            if (ama == null)
            {
                return(false);
            }

            var errors_printer = new SessionReportPrinter();
            var old            = bc.Report.SetPrinter(errors_printer);

            ama = new Invocation(ama, args).Resolve(bc);
            bc.Report.SetPrinter(old);

            if (errors_printer.ErrorsCount > 0 || !MemberAccess.IsValidDotExpression(ama.Type))
            {
                bc.Report.Error(1986, expr.Location,
                                "The `await' operand type `{0}' must have suitable GetAwaiter method",
                                expr.Type.GetSignatureForError());

                return(false);
            }

            var awaiter_type = ama.Type;

            expr = ama;

            //
            // Predefined: bool IsCompleted { get; }
            //
            is_completed = MemberCache.FindMember(awaiter_type, MemberFilter.Property("IsCompleted", bc.Module.Compiler.BuiltinTypes.Bool),
                                                  BindingRestriction.InstanceOnly) as PropertySpec;

            if (is_completed == null || !is_completed.HasGet)
            {
                Error_WrongAwaiterPattern(bc, awaiter_type);
                return(false);
            }

            //
            // Predefined: GetResult ()
            //
            // The method return type is also result type of await expression
            //
            get_result = MemberCache.FindMember(awaiter_type, MemberFilter.Method("GetResult", 0,
                                                                                  ParametersCompiled.EmptyReadOnlyParameters, null),
                                                BindingRestriction.InstanceOnly) as MethodSpec;

            if (get_result == null)
            {
                Error_WrongAwaiterPattern(bc, awaiter_type);
                return(false);
            }

            //
            // Predefined: INotifyCompletion.OnCompleted (System.Action)
            //
            var nc = bc.Module.PredefinedTypes.INotifyCompletion;

            if (nc.Define() && !awaiter_type.ImplementsInterface(nc.TypeSpec, false))
            {
                bc.Report.Error(4027, loc, "The awaiter type `{0}' must implement interface `{1}'",
                                awaiter_type.GetSignatureForError(), nc.GetSignatureForError());
                return(false);
            }

            result_type = get_result.ReturnType;

            return(true);
        }
示例#5
0
        public override bool Resolve(BlockContext bc)
        {
            if (!base.Resolve(bc))
            {
                return(false);
            }

            type = expr.Type;

            //
            // The task result is of dynamic type
            //
            if (expr.Type.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
            {
                throw new NotImplementedException("dynamic await");
            }

            //
            // Check whether the expression is awaitable
            //
            Expression ama = new AwaitableMemberAccess(expr).Resolve(bc);

            if (ama == null)
            {
                return(false);
            }

            Arguments args = new Arguments(0);

            var errors_printer = new SessionReportPrinter();
            var old            = bc.Report.SetPrinter(errors_printer);

            ama = new Invocation(ama, args).Resolve(bc);

            if (errors_printer.ErrorsCount > 0 || !MemberAccess.IsValidDotExpression(ama.Type))
            {
                bc.Report.SetPrinter(old);
                Error_WrongGetAwaiter(bc, loc, expr.Type);
                return(false);
            }

            var awaiter_type = ama.Type;

            awaiter = ((AsyncTaskStorey)machine_initializer.Storey).AddAwaiter(awaiter_type, loc);
            expr    = ama;

            //
            // Predefined: bool IsCompleted { get; }
            //
            var is_completed_ma = new MemberAccess(expr, "IsCompleted").Resolve(bc);

            if (is_completed_ma != null)
            {
                is_completed = is_completed_ma as PropertyExpr;
                if (is_completed != null && is_completed.Type.BuiltinType == BuiltinTypeSpec.Type.Bool && is_completed.IsInstance && is_completed.Getter != null)
                {
                    // valid
                }
                else
                {
                    bc.Report.SetPrinter(old);
                    Error_WrongAwaiterPattern(bc, awaiter_type);
                    return(false);
                }
            }

            bc.Report.SetPrinter(old);

            if (errors_printer.ErrorsCount > 0)
            {
                Error_WrongAwaiterPattern(bc, awaiter_type);
                return(false);
            }

            //
            // Predefined: OnCompleted (Action)
            //
            if (bc.Module.PredefinedTypes.Action.Define())
            {
                on_completed = MemberCache.FindMember(awaiter_type, MemberFilter.Method("OnCompleted", 0,
                                                                                        ParametersCompiled.CreateFullyResolved(bc.Module.PredefinedTypes.Action.TypeSpec), bc.Module.Compiler.BuiltinTypes.Void),
                                                      BindingRestriction.InstanceOnly) as MethodSpec;

                if (on_completed == null)
                {
                    Error_WrongAwaiterPattern(bc, awaiter_type);
                    return(false);
                }
            }

            //
            // Predefined: GetResult ()
            //
            // The method return type is also result type of await expression
            //
            get_result = MemberCache.FindMember(awaiter_type, MemberFilter.Method("GetResult", 0,
                                                                                  ParametersCompiled.EmptyReadOnlyParameters, null),
                                                BindingRestriction.InstanceOnly) as MethodSpec;

            if (get_result == null)
            {
                Error_WrongAwaiterPattern(bc, awaiter_type);
                return(false);
            }

            return(true);
        }
示例#6
0
        public override bool Resolve(BlockContext bc)
        {
            if (!base.Resolve(bc))
            {
                return(false);
            }

            Arguments args = new Arguments(0);

            type = expr.Type;

            //
            // The await expression is of dynamic type
            //
            if (type.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
            {
                result_type = type;

                awaiter = ((AsyncTaskStorey)machine_initializer.Storey).AddAwaiter(type, loc);

                expr = new Invocation(new MemberAccess(expr, "GetAwaiter"), args).Resolve(bc);
                return(true);
            }

            //
            // Check whether the expression is awaitable
            //
            Expression ama = new AwaitableMemberAccess(expr).Resolve(bc);

            if (ama == null)
            {
                return(false);
            }

            var errors_printer = new SessionReportPrinter();
            var old            = bc.Report.SetPrinter(errors_printer);

            ama = new Invocation(ama, args).Resolve(bc);
            bc.Report.SetPrinter(old);

            if (errors_printer.ErrorsCount > 0 || !MemberAccess.IsValidDotExpression(ama.Type))
            {
                bc.Report.Error(1986, expr.Location,
                                "The `await' operand type `{0}' must have suitable GetAwaiter method",
                                expr.Type.GetSignatureForError());

                return(false);
            }

            var awaiter_type = ama.Type;

            awaiter = ((AsyncTaskStorey)machine_initializer.Storey).AddAwaiter(awaiter_type, loc);

            expr = ama;

            //
            // Predefined: bool IsCompleted { get; }
            //
            is_completed = MemberCache.FindMember(awaiter_type, MemberFilter.Property("IsCompleted", bc.Module.Compiler.BuiltinTypes.Bool),
                                                  BindingRestriction.InstanceOnly) as PropertySpec;

            if (is_completed == null || !is_completed.HasGet)
            {
                Error_WrongAwaiterPattern(bc, awaiter_type);
                return(false);
            }

            //
            // Predefined: OnCompleted (Action)
            //
            if (bc.Module.PredefinedTypes.Action.Define())
            {
                on_completed = MemberCache.FindMember(awaiter_type, MemberFilter.Method("OnCompleted", 0,
                                                                                        ParametersCompiled.CreateFullyResolved(bc.Module.PredefinedTypes.Action.TypeSpec), bc.Module.Compiler.BuiltinTypes.Void),
                                                      BindingRestriction.InstanceOnly) as MethodSpec;

                if (on_completed == null)
                {
                    Error_WrongAwaiterPattern(bc, awaiter_type);
                    return(false);
                }
            }

            //
            // Predefined: GetResult ()
            //
            // The method return type is also result type of await expression
            //
            get_result = MemberCache.FindMember(awaiter_type, MemberFilter.Method("GetResult", 0,
                                                                                  ParametersCompiled.EmptyReadOnlyParameters, null),
                                                BindingRestriction.InstanceOnly) as MethodSpec;

            if (get_result == null)
            {
                Error_WrongAwaiterPattern(bc, awaiter_type);
                return(false);
            }

            result_type = get_result.ReturnType;

            return(true);
        }
示例#7
0
        //
        // Returns the method specification for a method named `name' defined
        // in type `t' which takes arguments of types `args'
        //
        public static MethodSpec GetPredefinedMethod(TypeSpec t, string name, Location loc, params TypeSpec [] args)
        {
            var pc = ParametersCompiled.CreateFullyResolved(args);

            return(GetPredefinedMethod(t, MemberFilter.Method(name, 0, pc, null), false, loc));
        }