public MethodCore (DeclSpace parent, GenericMethod generic, FullNamedExpression type, Modifiers mod, Modifiers allowed_mod, MemberName name, Attributes attrs, ParametersCompiled parameters) : base (parent, generic, type, mod, allowed_mod, name, attrs) { this.parameters = parameters; }
public Accessor(ToplevelBlock b, Modifiers mod, Attributes attrs, ParametersCompiled p, Location loc) { Block = b; Attributes = attrs; Location = loc; Parameters = p; ModFlags = ModifiersExtensions.Check (AllowedModifiers, mod, 0, loc, RootContext.ToplevelTypes.Compiler.Report); }
public Delegate(TypeContainer parent, FullNamedExpression type, Modifiers mod_flags, MemberName name, ParametersCompiled param_list, Attributes attrs) : base(parent, name, attrs, MemberKind.Delegate) { this.ReturnType = type; ModFlags = ModifiersExtensions.Check (AllowedModifiers, mod_flags, IsTopLevel ? Modifiers.INTERNAL : Modifiers.PRIVATE, name.Location, Report); parameters = param_list; spec = new TypeSpec (Kind, null, this, null, ModFlags | Modifiers.SEALED); }
public Delegate (NamespaceEntry ns, DeclSpace parent, FullNamedExpression type, int mod_flags, MemberName name, ParametersCompiled param_list, Attributes attrs) : base (ns, parent, name, attrs, Kind.Delegate) { this.ReturnType = type; ModFlags = Modifiers.Check (AllowedModifiers, mod_flags, IsTopLevel ? Modifiers.INTERNAL : Modifiers.PRIVATE, name.Location, Report); Parameters = param_list; }
public AnonymousMethodBody (ParametersCompiled parameters, ParametersBlock block, TypeSpec return_type, TypeSpec delegate_type, Location loc) : base (block, return_type, loc) { this.type = delegate_type; this.parameters = parameters; }
protected virtual AnonymousMethodBody CompatibleMethodFactory (TypeSpec return_type, TypeSpec delegate_type, ParametersCompiled p, ParametersBlock b) { return new AnonymousMethodBody (p, b, return_type, delegate_type, loc); }
public LambdaMethod (ParametersCompiled parameters, ParametersBlock block, TypeSpec return_type, TypeSpec delegate_type, Location loc) : base (parameters, block, return_type, delegate_type, loc) { }
public static Method Create (TypeDefinition parent, FullNamedExpression returnType, Modifiers mod, MemberName name, ParametersCompiled parameters, Attributes attrs) { var m = new Method (parent, returnType, mod, name, parameters, attrs); if ((mod & Modifiers.PARTIAL) != 0) { const Modifiers invalid_partial_mod = Modifiers.AccessibilityMask | Modifiers.ABSTRACT | Modifiers.EXTERN | Modifiers.NEW | Modifiers.OVERRIDE | Modifiers.SEALED | Modifiers.VIRTUAL; if ((mod & invalid_partial_mod) != 0) { m.Report.Error (750, m.Location, "A partial method cannot define access modifier or any of abstract, extern, new, override, sealed, or virtual modifiers"); mod &= ~invalid_partial_mod; } if ((parent.ModFlags & Modifiers.PARTIAL) == 0) { m.Report.Error (751, m.Location, "A partial method must be declared within a partial class or partial struct"); } } if ((mod & Modifiers.STATIC) == 0 && parameters.HasExtensionMethodType) { m.Report.Error (1105, m.Location, "`{0}': Extension methods must be declared static", m.GetSignatureForError ()); } return m; }
public Method (TypeDefinition parent, FullNamedExpression return_type, Modifiers mod, MemberName name, ParametersCompiled parameters, Attributes attrs) : base (parent, return_type, mod, parent.PartialContainer.Kind == MemberKind.Interface ? AllowedModifiersInterface : parent.PartialContainer.Kind == MemberKind.Struct ? AllowedModifiersStruct | Modifiers.ASYNC : AllowedModifiersClass | Modifiers.ASYNC, name, attrs, parameters) { }
Method GenerateNumberMatcher() { var loc = Location; var parameters = ParametersCompiled.CreateFullyResolved( new [] { new Parameter(new TypeExpression(Compiler.BuiltinTypes.Object, loc), "obj", 0, null, loc), new Parameter(new TypeExpression(Compiler.BuiltinTypes.Object, loc), "value", 0, null, loc), new Parameter(new TypeExpression(Compiler.BuiltinTypes.Bool, loc), "enumType", 0, null, loc), }, new [] { Compiler.BuiltinTypes.Object, Compiler.BuiltinTypes.Object, Compiler.BuiltinTypes.Bool }); var m = new Method(this, new TypeExpression(Compiler.BuiltinTypes.Bool, loc), Modifiers.PUBLIC | Modifiers.STATIC | Modifiers.DEBUGGER_HIDDEN, new MemberName("NumberMatcher", loc), parameters, null); parameters [0].Resolve(m, 0); parameters [1].Resolve(m, 1); parameters [2].Resolve(m, 2); ToplevelBlock top_block = new ToplevelBlock(Compiler, parameters, loc); m.Block = top_block; // // if (enumType) // return Equals (obj, value); // var equals_args = new Arguments(2); equals_args.Add(new Argument(top_block.GetParameterReference(0, loc))); equals_args.Add(new Argument(top_block.GetParameterReference(1, loc))); var if_type = new If( top_block.GetParameterReference(2, loc), new Return(new Invocation(new SimpleName("Equals", loc), equals_args), loc), loc); top_block.AddStatement(if_type); // // if (obj is Enum || obj == null) // return false; // var if_enum = new If( new Binary(Binary.Operator.LogicalOr, new Is(top_block.GetParameterReference(0, loc), new TypeExpression(Compiler.BuiltinTypes.Enum, loc), loc), new Binary(Binary.Operator.Equality, top_block.GetParameterReference(0, loc), new NullLiteral(loc))), new Return(new BoolLiteral(Compiler.BuiltinTypes, false, loc), loc), loc); top_block.AddStatement(if_enum); var system_convert = new MemberAccess(new QualifiedAliasMember("global", "System", loc), "Convert", loc); var expl_block = new ExplicitBlock(top_block, loc, loc); // // var converted = System.Convert.ChangeType (obj, System.Convert.GetTypeCode (value)); // var lv_converted = LocalVariable.CreateCompilerGenerated(Compiler.BuiltinTypes.Object, top_block, loc); var arguments_gettypecode = new Arguments(1); arguments_gettypecode.Add(new Argument(top_block.GetParameterReference(1, loc))); var gettypecode = new Invocation(new MemberAccess(system_convert, "GetTypeCode", loc), arguments_gettypecode); var arguments_changetype = new Arguments(1); arguments_changetype.Add(new Argument(top_block.GetParameterReference(0, loc))); arguments_changetype.Add(new Argument(gettypecode)); var changetype = new Invocation(new MemberAccess(system_convert, "ChangeType", loc), arguments_changetype); expl_block.AddStatement(new StatementExpression(new SimpleAssign(new LocalVariableReference(lv_converted, loc), changetype, loc))); // // return converted.Equals (value) // var equals_arguments = new Arguments(1); equals_arguments.Add(new Argument(top_block.GetParameterReference(1, loc))); var equals_invocation = new Invocation(new MemberAccess(new LocalVariableReference(lv_converted, loc), "Equals"), equals_arguments); expl_block.AddStatement(new Return(equals_invocation, loc)); var catch_block = new ExplicitBlock(top_block, loc, loc); catch_block.AddStatement(new Return(new BoolLiteral(Compiler.BuiltinTypes, false, loc), loc)); top_block.AddStatement(new TryCatch(expl_block, new List <Catch> () { new Catch(catch_block, loc) }, loc, false)); m.Define(); m.PrepareEmit(); AddMember(m); return(m); }
public InteractiveMethod(TypeDefinition parent, FullNamedExpression returnType, Modifiers mod, ParametersCompiled parameters) : base(parent, returnType, mod, new MemberName("Host"), parameters, null) { }
public Constructor (TypeDefinition parent, string name, Modifiers mod, Attributes attrs, ParametersCompiled args, Location loc) : base (parent, null, mod, AllowedModifiers, new MemberName (name, loc), attrs, args) { }
// // Returns the ConstructorInfo for "args" // public static MethodSpec GetPredefinedConstructor(TypeSpec t, Location loc, params TypeSpec [] args) { var pc = ParametersCompiled.CreateFullyResolved(args); return(GetPredefinedMember(t, MemberFilter.Constructor(pc), false, loc) as MethodSpec); }
public T Get() { if (member != null) { return(member); } if (declaring_type == null) { if (!declaring_type_predefined.Define()) { return(null); } declaring_type = declaring_type_predefined.TypeSpec; } if (parameters_predefined != null) { TypeSpec[] types = new TypeSpec [parameters_predefined.Length]; for (int i = 0; i < types.Length; ++i) { var p = parameters_predefined [i]; if (!p.Define()) { return(null); } types[i] = p.TypeSpec; } if (filter.Kind == MemberKind.Field) { filter = new MemberFilter(filter.Name, filter.Arity, filter.Kind, null, types [0]); } else { filter = new MemberFilter(filter.Name, filter.Arity, filter.Kind, ParametersCompiled.CreateFullyResolved(types), filter.MemberType); } } member = MemberCache.FindMember(declaring_type, filter, BindingRestriction.DeclaredOnly) as T; if (member == null) { return(null); } if (!member.IsAccessible(module)) { return(null); } return(member); }
public PredefinedMember(ModuleContainer module, BuiltinTypeSpec type, string name, params TypeSpec[] types) : this(module, type, MemberFilter.Method(name, 0, ParametersCompiled.CreateFullyResolved(types), null)) { }
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); }
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); }
public Destructor (TypeDefinition parent, Modifiers mod, ParametersCompiled parameters, Attributes attrs, Location l) : base (parent, null, mod, AllowedModifiers, new MemberName (MetadataName, l), attrs, parameters) { ModFlags &= ~Modifiers.PRIVATE; ModFlags |= Modifiers.PROTECTED | Modifiers.OVERRIDE; }
protected void DefineBuilders (MemberKind kind, ParametersCompiled parameters) { PropertyBuilder = Parent.TypeBuilder.DefineProperty ( GetFullName (MemberName), PropertyAttributes.None, #if !BOOTSTRAP_BASIC // Requires trunk version mscorlib IsStatic ? 0 : CallingConventions.HasThis, #endif MemberType.GetMetaInfo (), null, null, parameters.GetMetaInfo (), null, null); PropertySpec spec; if (kind == MemberKind.Indexer) spec = new IndexerSpec (Parent.Definition, this, MemberType, parameters, PropertyBuilder, ModFlags); else spec = new PropertySpec (kind, Parent.Definition, this, MemberType, PropertyBuilder, ModFlags); if (Get != null) { spec.Get = Get.Spec; var method = Get.Spec.GetMetaInfo () as MethodBuilder; if (method != null) { PropertyBuilder.SetGetMethod (method); Parent.MemberCache.AddMember (this, method.Name, Get.Spec); } } else { CheckMissingAccessor (kind, parameters, true); } if (Set != null) { spec.Set = Set.Spec; var method = Set.Spec.GetMetaInfo () as MethodBuilder; if (method != null) { PropertyBuilder.SetSetMethod (method); Parent.MemberCache.AddMember (this, method.Name, Set.Spec); } } else { CheckMissingAccessor (kind, parameters, false); } Parent.MemberCache.AddMember (this, PropertyBuilder.Name, spec); }
public MethodCore (TypeDefinition parent, FullNamedExpression type, Modifiers mod, Modifiers allowed_mod, MemberName name, Attributes attrs, ParametersCompiled parameters) : base (parent, type, mod, allowed_mod, name, attrs) { this.parameters = parameters; }
CompiledMethod CompileBlock (Class host, Undo undo, Report Report) { #if STATIC throw new NotSupportedException (); #else string current_debug_name = "eval-" + count + ".dll"; ++count; AssemblyDefinitionDynamic assembly; AssemblyBuilderAccess access; if (Environment.GetEnvironmentVariable ("SAVE") != null) { access = AssemblyBuilderAccess.RunAndSave; assembly = new AssemblyDefinitionDynamic (module, current_debug_name, current_debug_name); assembly.Importer = importer; } else { #if NET_4_0 access = AssemblyBuilderAccess.RunAndCollect; #else access = AssemblyBuilderAccess.Run; #endif assembly = new AssemblyDefinitionDynamic (module, current_debug_name); } assembly.Create (AppDomain.CurrentDomain, access); Method expression_method; if (host != null) { var base_class_imported = importer.ImportType (base_class); var baseclass_list = new List<FullNamedExpression> (1) { new TypeExpression (base_class_imported, host.Location) }; host.SetBaseTypes (baseclass_list); expression_method = (Method) host.Members[0]; if ((expression_method.ModFlags & Modifiers.ASYNC) != 0) { // // Host method is async. When WaitOnTask is set we wrap it with wait // // void AsyncWait (ref object $retval) { // $retval = Host(); // ((Task)$retval).Wait(); // When WaitOnTask is set // } // var p = new ParametersCompiled ( new Parameter (new TypeExpression (module.Compiler.BuiltinTypes.Object, Location.Null), "$retval", Parameter.Modifier.REF, null, Location.Null) ); var method = new Method(host, new TypeExpression(module.Compiler.BuiltinTypes.Void, Location.Null), Modifiers.PUBLIC | Modifiers.STATIC, new MemberName("AsyncWait"), p, null); method.Block = new ToplevelBlock(method.Compiler, p, Location.Null); method.Block.AddStatement(new StatementExpression (new SimpleAssign( new SimpleName(p [0].Name, Location.Null), new Invocation(new SimpleName(expression_method.MemberName.Name, Location.Null), new Arguments(0)), Location.Null), Location.Null)); if (WaitOnTask) { var task = new Cast (expression_method.TypeExpression, new SimpleName (p [0].Name, Location.Null), Location.Null); method.Block.AddStatement (new StatementExpression (new Invocation ( new MemberAccess (task, "Wait", Location.Null), new Arguments (0)), Location.Null)); } host.AddMember(method); expression_method = method; } host.CreateContainer(); host.DefineContainer(); host.Define(); } else { expression_method = null; } module.CreateContainer (); // Disable module and source file re-definition checks module.EnableRedefinition (); source_file.EnableRedefinition (); module.Define (); if (Report.Errors != 0){ if (undo != null) undo.ExecuteUndo (); return null; } if (host != null){ host.PrepareEmit (); host.EmitContainer (); } module.EmitContainer (); if (Report.Errors != 0){ if (undo != null) undo.ExecuteUndo (); return null; } module.CloseContainer (); if (host != null) host.CloseContainer (); if (access == AssemblyBuilderAccess.RunAndSave) assembly.Save (); if (host == null) return null; // // Unlike Mono, .NET requires that the MethodInfo is fetched, it cant // work from MethodBuilders. Retarded, I know. // var tt = assembly.Builder.GetType (host.TypeBuilder.Name); var mi = tt.GetMethod (expression_method.MemberName.Name); // // We need to then go from FieldBuilder to FieldInfo // or reflection gets confused (it basically gets confused, and variables override each // other). // foreach (var member in host.Members) { var field = member as Field; if (field == null) continue; var fi = tt.GetField (field.Name); Tuple<FieldSpec, FieldInfo> old; // If a previous value was set, nullify it, so that we do // not leak memory if (fields.TryGetValue (field.Name, out old)) { if (old.Item1.MemberType.IsStruct) { // // TODO: Clear fields for structs // } else { try { old.Item2.SetValue (null, null); } catch { } } } fields[field.Name] = Tuple.Create (field.Spec, fi); } return (CompiledMethod) System.Delegate.CreateDelegate (typeof (CompiledMethod), mi); #endif }
// // 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)); }
// // Creates full MethodBuilder for the method // public MethodBuilder DefineMethodBuilder (TypeDefinition container, ParametersCompiled param) { DefineMethodBuilder (container); builder.SetReturnType (method.ReturnType.GetMetaInfo ()); builder.SetParameters (param.GetMetaInfo ()); return builder; }
public static ParametersCompiled MergeGenerated (CompilerContext ctx, ParametersCompiled userParams, bool checkConflicts, Parameter compilerParams, TypeSpec compilerTypes) { return MergeGenerated (ctx, userParams, checkConflicts, new Parameter [] { compilerParams }, new TypeSpec [] { compilerTypes }); }
public Operator (TypeDefinition parent, OpType type, FullNamedExpression ret_type, Modifiers mod_flags, ParametersCompiled parameters, ToplevelBlock block, Attributes attrs, Location loc) : base (parent, ret_type, mod_flags, AllowedModifiers, new MemberName (GetMetadataName (type), loc), attrs, parameters) { OperatorType = type; Block = block; }
// // Use this method when you merge compiler generated parameters with user parameters // public static ParametersCompiled MergeGenerated (CompilerContext ctx, ParametersCompiled userParams, bool checkConflicts, Parameter[] compilerParams, TypeSpec[] compilerTypes) { Parameter[] all_params = new Parameter [userParams.Count + compilerParams.Length]; userParams.FixedParameters.CopyTo(all_params, 0); TypeSpec [] all_types; if (userParams.types != null) { all_types = new TypeSpec [all_params.Length]; userParams.Types.CopyTo (all_types, 0); } else { all_types = null; } int last_filled = userParams.Count; int index = 0; foreach (Parameter p in compilerParams) { for (int i = 0; i < last_filled; ++i) { while (p.Name == all_params [i].Name) { if (checkConflicts && i < userParams.Count) { ctx.Report.Error (316, userParams[i].Location, "The parameter name `{0}' conflicts with a compiler generated name", p.Name); } p.Name = '_' + p.Name; } } all_params [last_filled] = p; if (all_types != null) all_types [last_filled] = compilerTypes [index++]; ++last_filled; } ParametersCompiled parameters = new ParametersCompiled (all_params, all_types); parameters.has_params = userParams.has_params; return parameters; }
protected MethodOrOperator (TypeDefinition parent, FullNamedExpression type, Modifiers mod, Modifiers allowed_mod, MemberName name, Attributes attrs, ParametersCompiled parameters) : base (parent, type, mod, allowed_mod, name, attrs, parameters) { }
protected AEventAccessor (Event method, string prefix, Attributes attrs, Location loc) : base (method, prefix, attrs, loc) { this.method = method; this.ModFlags = method.ModFlags; this.parameters = ParametersCompiled.CreateImplicitParameter (method.TypeExpression, loc); }
protected Method (TypeDefinition parent, FullNamedExpression return_type, Modifiers mod, Modifiers amod, MemberName name, ParametersCompiled parameters, Attributes attrs) : base (parent, return_type, mod, amod, name, attrs, parameters) { }
public SetIndexerMethod (PropertyBase property, Modifiers modifiers, ParametersCompiled parameters, Attributes attrs, Location loc) : base (property, modifiers, parameters, attrs, loc) { }
protected override AnonymousMethodBody CompatibleMethodFactory (TypeSpec returnType, TypeSpec delegateType, ParametersCompiled p, ParametersBlock b) { return new LambdaMethod (p, b, returnType, delegateType, loc); }
public Indexer (TypeDefinition parent, FullNamedExpression type, MemberName name, Modifiers mod, ParametersCompiled parameters, Attributes attrs) : base (parent, type, mod, parent.PartialContainer.Kind == MemberKind.Interface ? AllowedInterfaceModifiers : AllowedModifiers, name, attrs) { this.parameters = parameters; }
protected bool DefineParameters (ParametersCompiled parameters) { if (!parameters.Resolve (this)) return false; bool error = false; for (int i = 0; i < parameters.Count; ++i) { Parameter p = parameters [i]; if (p.HasDefaultValue && (IsExplicitImpl || this is Operator || (this is Indexer && parameters.Count == 1))) p.Warning_UselessOptionalParameter (Report); if (p.CheckAccessibility (this)) continue; TypeSpec t = parameters.Types [i]; Report.SymbolRelatedToPreviousError (t); if (this is Indexer) Report.Error (55, Location, "Inconsistent accessibility: parameter type `{0}' is less accessible than indexer `{1}'", t.GetSignatureForError (), GetSignatureForError ()); else if (this is Operator) Report.Error (57, Location, "Inconsistent accessibility: parameter type `{0}' is less accessible than operator `{1}'", t.GetSignatureForError (), GetSignatureForError ()); else Report.Error (51, Location, "Inconsistent accessibility: parameter type `{0}' is less accessible than method `{1}'", t.GetSignatureForError (), GetSignatureForError ()); error = true; } return !error; }
public SetMethod (PropertyBase method, Modifiers modifiers, ParametersCompiled parameters, Attributes attrs, Location loc) : base (method, Prefix, modifiers, attrs, loc) { this.parameters = parameters; }
public AnonymousMethodMethod (TypeDefinition parent, AnonymousExpression am, AnonymousMethodStorey storey, TypeExpr return_type, Modifiers mod, MemberName name, ParametersCompiled parameters) : base (parent, return_type, mod | Modifiers.COMPILER_GENERATED, name, parameters, null) { this.AnonymousMethod = am; this.Storey = storey; Parent.PartialContainer.Members.Add (this); Block = new ToplevelBlock (am.block, parameters); }
void CheckMissingAccessor (MemberKind kind, ParametersCompiled parameters, bool get) { if (IsExplicitImpl) { MemberFilter filter; if (kind == MemberKind.Indexer) filter = new MemberFilter (MemberCache.IndexerNameAlias, 0, kind, parameters, null); else filter = new MemberFilter (MemberName.Name, 0, kind, null, null); var implementing = MemberCache.FindMember (InterfaceType, filter, BindingRestriction.DeclaredOnly) as PropertySpec; if (implementing == null) return; var accessor = get ? implementing.Get : implementing.Set; if (accessor != null) { Report.SymbolRelatedToPreviousError (accessor); Report.Error (551, Location, "Explicit interface implementation `{0}' is missing accessor `{1}'", GetSignatureForError (), accessor.GetSignatureForError ()); } } }
public static AnonymousTypeClass Create (TypeContainer parent, IList<AnonymousTypeParameter> parameters, Location loc) { string name = ClassNamePrefix + parent.Module.CounterAnonymousTypes++; ParametersCompiled all_parameters; TypeParameters tparams = null; SimpleName[] t_args; if (parameters.Count == 0) { all_parameters = ParametersCompiled.EmptyReadOnlyParameters; t_args = null; } else { t_args = new SimpleName[parameters.Count]; tparams = new TypeParameters (); Parameter[] ctor_params = new Parameter[parameters.Count]; for (int i = 0; i < parameters.Count; ++i) { AnonymousTypeParameter p = parameters[i]; for (int ii = 0; ii < i; ++ii) { if (parameters[ii].Name == p.Name) { parent.Compiler.Report.Error (833, parameters[ii].Location, "`{0}': An anonymous type cannot have multiple properties with the same name", p.Name); p = new AnonymousTypeParameter (null, "$" + i.ToString (), p.Location); parameters[i] = p; break; } } t_args[i] = new SimpleName ("<" + p.Name + ">__T", p.Location); tparams.Add (new TypeParameter (i, new MemberName (t_args[i].Name, p.Location), null, null, Variance.None)); ctor_params[i] = new Parameter (t_args[i], p.Name, Parameter.Modifier.NONE, null, p.Location); } all_parameters = new ParametersCompiled (ctor_params); } // // Create generic anonymous type host with generic arguments // named upon properties names // AnonymousTypeClass a_type = new AnonymousTypeClass (parent.Module, new MemberName (name, tparams, loc), parameters, loc); Constructor c = new Constructor (a_type, name, Modifiers.PUBLIC | Modifiers.DEBUGGER_HIDDEN, null, all_parameters, loc); c.Block = new ToplevelBlock (parent.Module.Compiler, c.ParameterInfo, loc); // // Create fields and constructor body with field initialization // bool error = false; for (int i = 0; i < parameters.Count; ++i) { AnonymousTypeParameter p = parameters [i]; Field f = new Field (a_type, t_args [i], Modifiers.PRIVATE | Modifiers.READONLY | Modifiers.DEBUGGER_HIDDEN, new MemberName ("<" + p.Name + ">", p.Location), null); if (!a_type.AddField (f)) { error = true; continue; } c.Block.AddStatement (new StatementExpression ( new SimpleAssign (new MemberAccess (new This (p.Location), f.Name), c.Block.GetParameterReference (i, p.Location)))); ToplevelBlock get_block = new ToplevelBlock (parent.Module.Compiler, p.Location); get_block.AddStatement (new Return ( new MemberAccess (new This (p.Location), f.Name), p.Location)); Property prop = new Property (a_type, t_args [i], Modifiers.PUBLIC, new MemberName (p.Name, p.Location), null); prop.Get = new Property.GetMethod (prop, 0, null, p.Location); prop.Get.Block = get_block; a_type.AddMember (prop); } if (error) return null; a_type.AddConstructor (c); return a_type; }
/// <remarks> /// If a method in Type `t' (or null to look in all interfaces /// and the base abstract class) with name `Name', return type `ret_type' and /// arguments `args' implements an interface, this method will /// return the MethodInfo that this method implements. /// /// If `name' is null, we operate solely on the method's signature. This is for /// instance used when implementing indexers. /// /// The `Operation op' controls whether to lookup, clear the pending bit, or clear /// all the methods with the given signature. /// /// The `MethodInfo need_proxy' is used when we're implementing an interface's /// indexer in a class. If the new indexer's IndexerName does not match the one /// that was used in the interface, then we always need to create a proxy for it. /// /// </remarks> public MethodSpec InterfaceMethod(MemberName name, TypeSpec iType, MethodData method, Operation op, out MethodSpec ambiguousCandidate, ref bool optional) { ambiguousCandidate = null; if (pending_implementations == null) { return(null); } TypeSpec ret_type = method.method.ReturnType; ParametersCompiled args = method.method.ParameterInfo; bool is_indexer = method.method is Indexer.SetIndexerMethod || method.method is Indexer.GetIndexerMethod; MethodSpec m; foreach (TypeAndMethods tm in pending_implementations) { if (!(iType == null || tm.type == iType)) { continue; } int method_count = tm.methods.Count; for (int i = 0; i < method_count; i++) { m = tm.methods [i]; if (m == null) { continue; } if (is_indexer) { if (!m.IsAccessor || m.Parameters.IsEmpty) { continue; } } else { if (name.Name != m.Name) { continue; } if (m.Arity != name.Arity) { continue; } } if (!TypeSpecComparer.Override.IsEqual(m.Parameters, args)) { continue; } if (!TypeSpecComparer.Override.IsEqual(m.ReturnType, ret_type)) { tm.found[i] = method; continue; } // // `need_proxy' is not null when we're implementing an // interface indexer and this is Clear(One/All) operation. // // If `name' is null, then we do a match solely based on the // signature and not on the name (this is done in the Lookup // for an interface indexer). // if (op != Operation.Lookup) { if (m.IsAccessor != method.method.IsAccessor) { continue; } // If `t != null', then this is an explicitly interface // implementation and we can always clear the method. // `need_proxy' is not null if we're implementing an // interface indexer. In this case, we need to create // a proxy if the implementation's IndexerName doesn't // match the IndexerName in the interface. if (m.DeclaringType.IsInterface && iType == null && name.Name != m.Name) // TODO: This is very expensive comparison { tm.need_proxy[i] = method.method.Spec; } else { tm.methods[i] = null; } } else { tm.found [i] = method; optional = tm.optional; } if (op == Operation.Lookup && name.ExplicitInterface != null && ambiguousCandidate == null) { ambiguousCandidate = m; continue; } // // Lookups and ClearOne return // if (op != Operation.ClearAll) { return(m); } } // If a specific type was requested, we can stop now. if (tm.type == iType) { break; } } m = ambiguousCandidate; ambiguousCandidate = null; return(m); }