Inheritance: MethodCore, IMethodData
Ejemplo n.º 1
0
		protected override Expression DoResolve (ResolveContext ec)
		{
			constructor_method = Delegate.GetConstructor (type);

			var invoke_method = Delegate.GetInvokeMethod (type);

			Arguments arguments = CreateDelegateMethodArguments (ec, invoke_method.Parameters, invoke_method.Parameters.Types, loc);
			method_group = method_group.OverloadResolve (ec, ref arguments, this, OverloadResolver.Restrictions.CovariantDelegate);
			if (method_group == null)
				return null;

			var delegate_method = method_group.BestCandidate;
			
			if (delegate_method.DeclaringType.IsNullableType) {
				ec.Report.Error (1728, loc, "Cannot create delegate from method `{0}' because it is a member of System.Nullable<T> type",
					delegate_method.GetSignatureForError ());
				return null;
			}		
			
			if (!AllowSpecialMethodsInvocation)
				Invocation.IsSpecialMethodInvocation (ec, delegate_method, loc);

			ExtensionMethodGroupExpr emg = method_group as ExtensionMethodGroupExpr;
			if (emg != null) {
				method_group.InstanceExpression = emg.ExtensionExpression;
				TypeSpec e_type = emg.ExtensionExpression.Type;
				if (TypeSpec.IsValueType (e_type)) {
					ec.Report.Error (1113, loc, "Extension method `{0}' of value type `{1}' cannot be used to create delegates",
						delegate_method.GetSignatureForError (), e_type.GetSignatureForError ());
				}
			}

			TypeSpec rt = delegate_method.ReturnType;
			if (rt.BuiltinType == BuiltinTypeSpec.Type.Dynamic)
				rt = ec.BuiltinTypes.Object;

			if (!Delegate.IsTypeCovariant (ec, rt, invoke_method.ReturnType)) {
				Expression ret_expr = new TypeExpression (delegate_method.ReturnType, loc);
				Error_ConversionFailed (ec, delegate_method, ret_expr);
			}

			if (delegate_method.IsConditionallyExcluded (ec, loc)) {
				ec.Report.SymbolRelatedToPreviousError (delegate_method);
				MethodOrOperator m = delegate_method.MemberDefinition as MethodOrOperator;
				if (m != null && m.IsPartialDefinition) {
					ec.Report.Error (762, loc, "Cannot create delegate from partial method declaration `{0}'",
						delegate_method.GetSignatureForError ());
				} else {
					ec.Report.Error (1618, loc, "Cannot create delegate with `{0}' because it has a Conditional attribute",
						TypeManager.CSharpSignature (delegate_method));
				}
			}

			var expr = method_group.InstanceExpression;
			if (expr != null && (expr.Type.IsGenericParameter || !TypeSpec.IsReferenceType (expr.Type)))
				method_group.InstanceExpression = new BoxedCast (expr, ec.BuiltinTypes.Object);

			eclass = ExprClass.Value;
			return this;
		}
Ejemplo n.º 2
0
 public PartialMethodDefinitionInfo(MethodOrOperator mc)
 {
     this.mc = mc;
     if ((mc.ModFlags & Modifiers.STATIC) != 0)
     {
         attrs = MethodAttributes.Static;
     }
 }
Ejemplo n.º 3
0
        static CompiledMethod CompileBlock(Class host, Undo undo, Report Report)
        {
            AssemblyDefinitionDynamic assembly;

            if (Environment.GetEnvironmentVariable("SAVE") != null)
            {
                assembly          = new AssemblyDefinitionDynamic(RootContext.ToplevelTypes, current_debug_name, current_debug_name);
                assembly.Importer = loader.Importer;
            }
            else
            {
                assembly = new AssemblyDefinitionDynamic(RootContext.ToplevelTypes, current_debug_name);
            }

            assembly.Create(AppDomain.CurrentDomain, AssemblyBuilderAccess.RunAndSave);
            RootContext.ToplevelTypes.SetDeclaringAssembly(assembly);
            RootContext.ToplevelTypes.Define();

            if (Report.Errors != 0)
            {
                undo.ExecuteUndo();
                return(null);
            }

            TypeBuilder   tb = null;
            MethodBuilder mb = null;

            if (host != null)
            {
                tb = host.TypeBuilder;
                mb = null;
                foreach (MemberCore member in host.Methods)
                {
                    if (member.Name != "Host")
                    {
                        continue;
                    }

                    MethodOrOperator method = (MethodOrOperator)member;
                    mb = method.MethodBuilder;
                    break;
                }

                if (mb == null)
                {
                    throw new Exception("Internal error: did not find the method builder for the generated method");
                }
            }

            RootContext.ToplevelTypes.Emit();
            if (Report.Errors != 0)
            {
                undo.ExecuteUndo();
                return(null);
            }

            RootContext.ToplevelTypes.CloseType();

            if (Environment.GetEnvironmentVariable("SAVE") != null)
            {
                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(tb.Name);
            MethodInfo mi = tt.GetMethod(mb.Name);

            // Pull the FieldInfos from the type, and keep track of them
            foreach (Field field in queued_fields)
            {
                FieldInfo 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);
                }
                else
                {
                    fields.Add(field.Name, Tuple.Create(field.Spec, fi));
                }
            }
            //types.Add (tb);

            queued_fields.Clear();

            return((CompiledMethod)System.Delegate.CreateDelegate(typeof(CompiledMethod), mi));
        }
Ejemplo n.º 4
0
        public static string [] GetCompletions(string input, out string prefix)
        {
            prefix = "";
            if (input == null || input.Length == 0)
            {
                return(null);
            }

            lock (evaluator_lock)
            {
                if (!inited)
                {
                    Init();
                }

                bool         partial_input;
                CSharpParser parser = ParseString(ParseMode.GetCompletions, input, out partial_input);
                if (parser == null)
                {
                    if (CSharpParser.yacc_verbose_flag != 0)
                    {
                        Console.WriteLine("DEBUG: No completions available");
                    }
                    return(null);
                }

                Class parser_result = parser.InteractiveResult as Class;

                if (parser_result == null)
                {
                    if (CSharpParser.yacc_verbose_flag != 0)
                    {
                        Console.WriteLine("Do not know how to cope with !Class yet");
                    }
                    return(null);
                }

                try
                {
                    var a = new AssemblyDefinitionDynamic(RootContext.ToplevelTypes, "temp");
                    a.Create(AppDomain.CurrentDomain, AssemblyBuilderAccess.Run);
                    RootContext.ToplevelTypes.SetDeclaringAssembly(a);
                    RootContext.ToplevelTypes.Define();
                    if (ctx.Report.Errors != 0)
                    {
                        return(null);
                    }

                    MethodOrOperator method = null;
                    foreach (MemberCore member in parser_result.Methods)
                    {
                        if (member.Name != "Host")
                        {
                            continue;
                        }

                        method = (MethodOrOperator)member;
                        break;
                    }
                    if (method == null)
                    {
                        throw new InternalErrorException("did not find the the Host method");
                    }

                    BlockContext bc = new BlockContext(method, method.Block, method.ReturnType);

                    try
                    {
                        method.Block.Resolve(null, bc, method);
                    }
                    catch (CompletionResult cr)
                    {
                        prefix = cr.BaseText;
                        return(cr.Result);
                    }
                }
                finally
                {
                    parser.undo.ExecuteUndo();
                }
            }
            return(null);
        }
Ejemplo n.º 5
0
		public void AddMethod (MethodOrOperator method)
		{
			if (!AddToContainer (method, method.MemberName.Basename))
				return;
			
			if (methods == null)
				methods = new List<MemberCore> ();

			AddMemberToList (method, methods);
		}
Ejemplo n.º 6
0
		public void AddMethod (MethodOrOperator method)
		{
			orderedAllMembers.Add (method);
			if (!AddToContainer (method, method.MemberName.Basename))
				return;
			
			if (methods == null)
				methods = new List<MemberCore> ();

			if (method.MemberName.Left != null) 
				AddMemberToList (method, methods, true);
			else 
				AddMemberToList (method, methods, false);
		}
Ejemplo n.º 7
0
        public override Expression DoResolve(ResolveContext ec)
        {
            constructor_method = Delegate.GetConstructor(ec.Compiler, ec.CurrentType, type);

            MethodInfo invoke_method = Delegate.GetInvokeMethod(ec.Compiler, ec.CurrentType, type);

            method_group.DelegateType       = type;
            method_group.CustomErrorHandler = this;

            Arguments arguments = CreateDelegateMethodArguments(TypeManager.GetParameterData(invoke_method), loc);

            method_group = method_group.OverloadResolve(ec, ref arguments, false, loc);
            if (method_group == null)
            {
                return(null);
            }

            delegate_method = (MethodInfo)method_group;

            if (TypeManager.IsNullableType(delegate_method.DeclaringType))
            {
                ec.Report.Error(1728, loc, "Cannot create delegate from method `{0}' because it is a member of System.Nullable<T> type",
                                TypeManager.GetFullNameSignature(delegate_method));
                return(null);
            }

            Invocation.IsSpecialMethodInvocation(ec, delegate_method, loc);

            ExtensionMethodGroupExpr emg = method_group as ExtensionMethodGroupExpr;

            if (emg != null)
            {
                delegate_instance_expression = emg.ExtensionExpression;
                Type e_type = delegate_instance_expression.Type;
                if (TypeManager.IsValueType(e_type))
                {
                    ec.Report.Error(1113, loc, "Extension method `{0}' of value type `{1}' cannot be used to create delegates",
                                    TypeManager.CSharpSignature(delegate_method), TypeManager.CSharpName(e_type));
                }
            }

            Type       rt       = TypeManager.TypeToCoreType(delegate_method.ReturnType);
            Expression ret_expr = new TypeExpression(rt, loc);

            if (!Delegate.IsTypeCovariant(ret_expr, (TypeManager.TypeToCoreType(invoke_method.ReturnType))))
            {
                Error_ConversionFailed(ec, delegate_method, ret_expr);
            }

            if (Invocation.IsMethodExcluded(delegate_method, loc))
            {
                ec.Report.SymbolRelatedToPreviousError(delegate_method);
                MethodOrOperator m = TypeManager.GetMethod(delegate_method) as MethodOrOperator;
                if (m != null && m.IsPartialDefinition)
                {
                    ec.Report.Error(762, loc, "Cannot create delegate from partial method declaration `{0}'",
                                    TypeManager.CSharpSignature(delegate_method));
                }
                else
                {
                    ec.Report.Error(1618, loc, "Cannot create delegate with `{0}' because it has a Conditional attribute",
                                    TypeManager.CSharpSignature(delegate_method));
                }
            }

            DoResolveInstanceExpression(ec);
            eclass = ExprClass.Value;
            return(this);
        }
Ejemplo n.º 8
0
        static CompiledMethod CompileBlock(Class host, Undo undo)
        {
            RootContext.ResolveTree();
            if (Report.Errors != 0)
            {
                undo.ExecuteUndo();
                return(null);
            }

            RootContext.PopulateTypes();

            if (Report.Errors != 0)
            {
                undo.ExecuteUndo();
                return(null);
            }

            TypeBuilder   tb = null;
            MethodBuilder mb = null;

            if (host != null)
            {
                tb = host.TypeBuilder;
                mb = null;
                foreach (MemberCore member in host.Methods)
                {
                    if (member.Name != "Host")
                    {
                        continue;
                    }

                    MethodOrOperator method = (MethodOrOperator)member;
                    mb = method.MethodBuilder;
                    break;
                }

                if (mb == null)
                {
                    throw new Exception("Internal error: did not find the method builder for the generated method");
                }
            }

            RootContext.EmitCode();
            if (Report.Errors != 0)
            {
                return(null);
            }

            RootContext.CloseTypes();

            if (Environment.GetEnvironmentVariable("SAVE") != null)
            {
                CodeGen.Save(current_debug_name, false);
            }

            if (host == null)
            {
                return(null);
            }

            //
            // Unlike Mono, .NET requires that the MethodInfo is fetched, it cant
            // work from MethodBuilders.   Retarded, I know.
            //
            Type       tt = CodeGen.Assembly.Builder.GetType(tb.Name);
            MethodInfo mi = tt.GetMethod(mb.Name);

            // Pull the FieldInfos from the type, and keep track of them
            foreach (Field field in queued_fields)
            {
                FieldInfo fi = tt.GetField(field.Name);

                FieldInfo old = (FieldInfo)fields [field.Name];

                // If a previous value was set, nullify it, so that we do
                // not leak memory
                if (old != null)
                {
                    if (old.FieldType.IsValueType)
                    {
                        //
                        // TODO: Clear fields for structs
                        //
                    }
                    else
                    {
                        try {
                            old.SetValue(null, null);
                        } catch {
                        }
                    }
                }

                fields [field.Name] = fi;
            }
            //types.Add (tb);

            queued_fields.Clear();

            return((CompiledMethod)System.Delegate.CreateDelegate(typeof(CompiledMethod), mi));
        }
Ejemplo n.º 9
0
		public PartialMethodDefinitionInfo (MethodOrOperator mc)
		{
			this.mc = mc;
			if ((mc.ModFlags & Modifiers.STATIC) != 0)
				attrs = MethodAttributes.Static;
		}
Ejemplo n.º 10
0
		/// <summary>
		///   Define and resolve the type parameters.
		///   We're called from Method.Define().
		/// </summary>
		public bool Define (MethodOrOperator m)
		{
			TypeParameterName[] names = MemberName.TypeArguments.GetDeclarations ();
			string[] snames = new string [names.Length];
			for (int i = 0; i < names.Length; i++) {
				string type_argument_name = names[i].Name;
				int idx = parameters.GetParameterIndexByName (type_argument_name);
				if (idx >= 0) {
					Block b = m.Block;
					if (b == null)
						b = new Block (null);

					b.Error_AlreadyDeclaredTypeParameter (parameters [i].Location,
						type_argument_name, "method parameter");
				}
				
				snames[i] = type_argument_name;
			}

			GenericTypeParameterBuilder[] gen_params = m.MethodBuilder.DefineGenericParameters (snames);
			for (int i = 0; i < TypeParameters.Length; i++)
				TypeParameters [i].Define (gen_params [i]);

			if (!Define ())
				return false;

			for (int i = 0; i < TypeParameters.Length; i++) {
				if (!TypeParameters [i].ResolveType (this))
					return false;
			}

			return true;
		}
Ejemplo n.º 11
0
		public void RemoveMethod(MethodOrOperator method){
		   RemoveFromContainer(method.Name);
		   RemoveFromMemberList(method,methods);
		}
Ejemplo n.º 12
0
Archivo: generic.cs Proyecto: ikvm/mono
		/// <summary>
		///   Define and resolve the type parameters.
		///   We're called from Method.Define().
		/// </summary>
		public bool Define (MethodOrOperator m)
		{
			TypeParameterName[] names = MemberName.TypeArguments.GetDeclarations ();
			string[] snames = new string [names.Length];
			for (int i = 0; i < names.Length; i++) {
				string type_argument_name = names[i].Name;
				int idx = parameters.GetParameterIndexByName (type_argument_name);

				if (idx >= 0) {
					var b = m.Block;
					if (b == null)
						b = new ToplevelBlock (Compiler, Location);

					b.Error_AlreadyDeclaredTypeParameter (parameters [i].Location,
						type_argument_name, "method parameter");
				}

				if (m.Block != null) {
					var ikv = m.Block.GetKnownVariable (type_argument_name);
					if (ikv != null)
						ikv.Block.Error_AlreadyDeclaredTypeParameter (ikv.Location, type_argument_name, "local variable");
				}
				
				snames[i] = type_argument_name;
			}

			GenericTypeParameterBuilder[] gen_params = m.MethodBuilder.DefineGenericParameters (snames);
			for (int i = 0; i < TypeParameters.Length; i++)
				TypeParameters [i].Define (gen_params [i], null);

			return true;
		}
Ejemplo n.º 13
0
		public void AddMethod (MethodOrOperator method)
		{
			if (!AddToContainer (method, method.MemberName.Basename))
				return;
			
			if (methods == null)
				methods = new MemberCoreArrayList ();

			if (method.MemberName.Left != null) 
				AddMemberToList (method, methods, true);
			else 
				AddMemberToList (method, methods, false);
		}