DefineParameter() public method

public DefineParameter ( int iSequence, System attributes, string strParamName ) : System.Reflection.Emit.ParameterBuilder
iSequence int
attributes System
strParamName string
return System.Reflection.Emit.ParameterBuilder
Esempio n. 1
0
 public static void DefineParams(ConstructorBuilder constructorBuilder, IEnumerable<RppParameterInfo> constructorParams)
 {
     int index = 1;
     foreach (var param in constructorParams)
     {
         constructorBuilder.DefineParameter(index++, ParameterAttributes.None, RppClass.StringConstructorArgName(param.Name));
     }
 }
 internal void Compile(ConstructorBuilder c)
 {
     if (Builder == null)
     {
         int ofs = (c.IsStatic ? 0 : 1);
         Builder = c.DefineParameter(this.Index + ofs, this.Attributes, this.Name);
     }
 }
        private static void DefineParameterNames(
            ConstructorBuilder constructor, 
            ConstructorMetadata constructorMetadata)
        {
            for (var i = 0; i < constructorMetadata.Parameters.Length; i++)
            {
                var parameter = constructorMetadata.Parameters[i];

                constructor.DefineParameter(parameter.Sequence, ParameterAttributes.None, parameter.Name);
            }
        }
Esempio n. 4
0
        public void Execute()
        {
            constructorBuilder = builder().TypeBuilder.DefineConstructor(
                MethodAttributes.Public |
                MethodAttributes.SpecialName |
                MethodAttributes.RTSpecialName,
                CallingConventions.Standard,
                this.types().ToArray());

            int counter = 1;
            foreach(Type type in this.types())
            {
                constructorBuilder.DefineParameter(counter, ParameterAttributes.None, type.Name + "_" + counter);
                counter++;
            }
        }
		internal ConstructorEmitter(AbstractTypeEmitter maintype, params ArgumentReference[] arguments)
		{
			this.maintype = maintype;

			var args = ArgumentsUtil.InitializeAndConvert(arguments);

			builder = maintype.TypeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, args);
			// if we don't copy the parameter attributes, the default binder will fail
			// when trying to resolve constructors from the passed argument values.
            for (int i = 0; i < args.Length; ++i)
            {
                var arg = arguments[i];
                var paramBuilder = builder.DefineParameter(i + 1, arg.ParameterAttributes, "");
                if (arg.DefaultValue != DBNull.Value)
                    paramBuilder.SetConstant(arg.DefaultValue);
            }
		}
 private void NegTestCaseVerificationHelper(
     ConstructorBuilder constructor,
     int sequence,
     ParameterAttributes attribute,
     string paramName,
     Type desiredException)
 {
     Assert.Throws(desiredException, () => { constructor.DefineParameter(sequence, attribute, paramName); });
 }
Esempio n. 7
0
 // TODO: use in Python's OverrideConstructor:
 public static ParameterBuilder DefineParameterCopy(ConstructorBuilder builder, int paramIndex, ParameterInfo info) {
     var result = builder.DefineParameter(1 + paramIndex, info.Attributes, info.Name);
     CopyParameterAttributes(info, result);
     return result;
 }
Esempio n. 8
0
		public virtual void ApplyAttributes (MethodBuilder mb, ConstructorBuilder cb, int index, PredefinedAttributes pa)
		{
			if (builder != null)
				throw new InternalErrorException ("builder already exists");

			if (mb == null)
				builder = cb.DefineParameter (index, Attributes, Name);
			else
				builder = mb.DefineParameter (index, Attributes, Name);

			if (OptAttributes != null)
				OptAttributes.Emit ();

			if (HasDefaultValue) {
				//
				// Emit constant values for true constants only, the other
				// constant-like expressions will rely on default value expression
				//
				Constant c = default_expr as Constant;
				if (c != null) {
					if (default_expr.Type == TypeManager.decimal_type) {
						builder.SetCustomAttribute (Const.CreateDecimalConstantAttribute (c, pa));
					} else {
						builder.SetConstant (c.GetTypedValue ());
					}
				}
			}

			if (parameter_type != null) {
				if (parameter_type == InternalType.Dynamic) {
					pa.Dynamic.EmitAttribute (builder);
				} else if (parameter_type.HasDynamicElement) {
					pa.Dynamic.EmitAttribute (builder, parameter_type);
				}
			}
		}
Esempio n. 9
0
 public static void RegisterBuilders(this IReadOnlyList<ParameterStructure> prm, ConstructorBuilder builder, bool isInstance)
 {
     for (var i = 0; i < prm.Count; ++i)
     {
         var p = prm[i];
         var pb = builder.DefineParameter(i + 1, p.Attributes, p.Name);
         p.RegisterBuilder(pb, isInstance);
     }
 }
Esempio n. 10
0
		public virtual void ApplyAttributes (MethodBuilder mb, ConstructorBuilder cb, int index)
		{
			if (mb == null)
				builder = cb.DefineParameter (index, Attributes, Name);
			else
				builder = mb.DefineParameter (index, Attributes, Name);

			if (OptAttributes != null)
				OptAttributes.Emit ();

			if (HasDefaultValue) {
				//
				// Emit constant values for true constants only, the other
				// constant-like expressions will rely on default value expression
				//
				Constant c = default_expr as Constant;
				if (c != null) {
					if (default_expr.Type == TypeManager.decimal_type) {
						builder.SetCustomAttribute (Const.CreateDecimalConstantAttribute (c));
					} else {
						builder.SetConstant (c.GetTypedValue ());
					}
				}
			}

			if (parameter_type == InternalType.Dynamic) {
				PredefinedAttributes.Get.Dynamic.EmitAttribute (builder);
			} else {
				var trans_flags = TypeManager.HasDynamicTypeUsed (parameter_type);
				if (trans_flags != null) {
					var pa = PredefinedAttributes.Get.DynamicTransform;
					if (pa.Constructor != null || pa.ResolveConstructor (Location, ArrayContainer.MakeType (TypeManager.bool_type))) {
						builder.SetCustomAttribute (
							new CustomAttributeBuilder (pa.Constructor, new object [] { trans_flags }));
					}
				}
			}
		}
Esempio n. 11
0
        private void DefineParameter(MethodBuilder methodBuilder, ConstructorBuilder constructorBuilder, Cci.IParameterDefinition paramDef)
        {
            // No explicit param row is needed if param has no flags (other than optionally IN),
            // no name and no references to the param row, such as CustomAttribute, Constant, or FieldMarshall
            var attributes = paramDef.GetAttributes(_context);
            var defaultValue = paramDef.GetDefaultValue(_context);

            if (defaultValue != null ||
                paramDef.IsOptional ||
                paramDef.IsOut ||
                paramDef.IsMarshalledExplicitly ||
                attributes.Any() ||
                paramDef.Name.Length > 0)
            {
                int index = paramDef is Cci.ReturnValueParameter ? 0 : paramDef.Index + 1;
                ParameterAttributes attrs = (ParameterAttributes)Cci.MetadataWriter.GetParameterFlags(paramDef);

                ParameterBuilder paramBuilder = (methodBuilder != null) ?
                    methodBuilder.DefineParameter(index, attrs, paramDef.Name) :
                    constructorBuilder.DefineParameter(index, attrs, paramDef.Name);

                if (defaultValue != null)
                {
                    object rawValue = defaultValue.Value;
                    if (rawValue == null)
                    {
                        var paramTypeRef = paramDef.GetType(_context);
                        if (paramTypeRef.IsValueType)
                        {
                            SetParameterDefaultStructValue(paramBuilder);
                        }
                        else
                        {
                            paramBuilder.SetConstant(null);
                        }
                    }
                    else
                    {
                        // TODO (tomat): Ref.Emit has too strict checks on the constant type. While it is ok to emit value,
                        // e.g. of type Int16 for parameter of type Int32 to metadata, Ref.Emit checks if these types are Type.IsAssignableFrom.
                        // To make this work we need to convert.
                        //
                        // We also need to support Nullable<T>.
                        if (rawValue.GetType().IsPrimitive)
                        {
                            // parameter type has already been resolved once when defining the method, so just retrive it:
                            Type paramType = ResolveType(paramDef.GetType(_context));

                            if (paramType.IsGenericType && paramType.GetGenericTypeDefinition() == typeof(Nullable<>))
                            {
                                paramType = paramType.GetGenericArguments()[0];
                            }

                            if (paramType.IsEnum)
                            {
                                // If emitting the enum, it isn't "created" as this stage so Enum.GetUnderlyingType() will throw
                                // Otherwise, if the enum is already defined, we should use Enum.GetUnderlyingType() to get the correct type
                                paramType = paramType is TypeBuilder ? paramType.UnderlyingSystemType : Enum.GetUnderlyingType(paramType);
                            }

                            rawValue = Convert.ChangeType(rawValue, paramType, System.Globalization.CultureInfo.InvariantCulture);
                        }

                        paramBuilder.SetConstant(rawValue);
                    }
                }

                if (paramDef.IsMarshalledExplicitly)
                {
                    // FieldMarshal
                    var marshallingInformation = paramDef.MarshallingInformation;

                    if (marshallingInformation != null)
                    {
                        paramBuilder.SetCustomAttribute(GetMarshalAsAttribute(marshallingInformation));
                    }
                    else
                    {
                        Debug.Assert(!paramDef.MarshallingDescriptor.IsDefaultOrEmpty);
                        // TODO:
                        throw new NotImplementedException();
                    }
                }

                EmitCustomAttributes(paramBuilder, attributes);
            }
        }
Esempio n. 12
0
		/// <summary>
		/// Sets attributes of generated override/implement/export stub parameters.
		/// </summary>
		/// <param name="stub">The stub constructor builder.
		/// </param>
		/// <param name="formalParams">Formal parameters of the implementing PHP method.</param>
		/// <param name="templateParams">Parameters of the overload being overriden/implemented/exported.</param>
		public static void DefineStubParameters(ConstructorBuilder/*!*/ stub,
			PHP.Core.AST.FormalParam[] formalParams, ParameterInfo[]/*!*/ templateParams)
		{
			for (int i = 0; i < templateParams.Length; i++)
			{
				string name;

				// take the overriding parameter name if available
				if (formalParams != null && i < formalParams.Length) name = formalParams[i].Name.ToString();
				else name = templateParams[i].Name;

				stub.DefineParameter(i + 1, templateParams[i].Attributes, name);
			}
		}
Esempio n. 13
0
		public virtual void ApplyAttributes (MethodBuilder mb, ConstructorBuilder cb, int index)
		{
			if (mb == null)
				builder = cb.DefineParameter (index, Attributes, Name);
			else
				builder = mb.DefineParameter (index, Attributes, Name);

			if (OptAttributes != null)
				OptAttributes.Emit ();
		}
Esempio n. 14
0
        private void ConvertConstructorParameters(ICommonMethodNode value, ConstructorBuilder methb)
        {
            ParameterBuilder pb = null;
            IParameterNode[] parameters = value.parameters;
            for (int i = 0; i < parameters.Length; i++)
            {
                object default_value = null;
                if (parameters[i].default_value != null)
                    default_value = helper.GetConstantForExpression(parameters[i].default_value);
                ParameterAttributes pa = ParameterAttributes.None;
                if (parameters[i].parameter_type == parameter_type.var)
                    pa = ParameterAttributes.Retval;

                if (default_value != null)
                    pa |= ParameterAttributes.Optional;
                pb = methb.DefineParameter(i + 1, pa, parameters[i].name);
                if (default_value != null)
                    pb.SetConstant(default_value);
                helper.AddParameter(parameters[i], pb);
                if (parameters[i].is_params)
                    pb.SetCustomAttribute(TypeFactory.ParamArrayAttributeConstructor, new byte[] { 0x1, 0x0, 0x0, 0x0 });
            }
        }
Esempio n. 15
0
 		public override bool Define ()
		{
			if (IsGeneric) {
				foreach (TypeParameter type_param in TypeParameters) {
					if (!type_param.Resolve (this))
						return false;
				}

				foreach (TypeParameter type_param in TypeParameters) {
					if (!type_param.DefineType (this))
						return false;
				}
			}

			member_cache = new MemberCache (TypeManager.multicast_delegate_type, this);

			// FIXME: POSSIBLY make this static, as it is always constant
			//
			Type [] const_arg_types = new Type [2];
			const_arg_types [0] = TypeManager.object_type;
			const_arg_types [1] = TypeManager.intptr_type;

			const MethodAttributes ctor_mattr = MethodAttributes.RTSpecialName | MethodAttributes.SpecialName |
				MethodAttributes.HideBySig | MethodAttributes.Public;

			ConstructorBuilder = TypeBuilder.DefineConstructor (ctor_mattr,
									    CallingConventions.Standard,
									    const_arg_types);

			ConstructorBuilder.DefineParameter (1, ParameterAttributes.None, "object");
			ConstructorBuilder.DefineParameter (2, ParameterAttributes.None, "method");
			//
			// HACK because System.Reflection.Emit is lame
			//
			IParameterData [] fixed_pars = new IParameterData [] {
				new ParameterData ("object", Parameter.Modifier.NONE),
				new ParameterData ("method", Parameter.Modifier.NONE)
			};

			AParametersCollection const_parameters = new ParametersImported (
				fixed_pars,
				new Type[] { TypeManager.object_type, TypeManager.intptr_type });
			
			TypeManager.RegisterMethod (ConstructorBuilder, const_parameters);
			member_cache.AddMember (ConstructorBuilder, this);
			
			ConstructorBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);

			//
			// Here the various methods like Invoke, BeginInvoke etc are defined
			//
			// First, call the `out of band' special method for
			// defining recursively any types we need:
			
			if (!Parameters.Resolve (this))
				return false;

			//
			// Invoke method
			//

			// Check accessibility
			foreach (Type partype in Parameters.Types){
				if (!IsAccessibleAs (partype)) {
					Report.SymbolRelatedToPreviousError (partype);
					Report.Error (59, Location,
						      "Inconsistent accessibility: parameter type `{0}' is less accessible than delegate `{1}'",
						      TypeManager.CSharpName (partype),
						      GetSignatureForError ());
					return false;
				}
			}
			
			ReturnType = ReturnType.ResolveAsTypeTerminal (this, false);
			if (ReturnType == null)
				return false;

			ret_type = ReturnType.Type;
            
			if (!IsAccessibleAs (ret_type)) {
				Report.SymbolRelatedToPreviousError (ret_type);
				Report.Error (58, Location,
					      "Inconsistent accessibility: return type `" +
					      TypeManager.CSharpName (ret_type) + "' is less " +
					      "accessible than delegate `" + GetSignatureForError () + "'");
				return false;
			}

			CheckProtectedModifier ();

			if (RootContext.StdLib && TypeManager.IsSpecialType (ret_type)) {
				Method.Error1599 (Location, ret_type, Report);
				return false;
			}

			TypeManager.CheckTypeVariance (ret_type, Variance.Covariant, this);

			//
			// We don't have to check any others because they are all
			// guaranteed to be accessible - they are standard types.
			//
			
  			CallingConventions cc = Parameters.CallingConvention;

 			InvokeBuilder = TypeBuilder.DefineMethod ("Invoke", 
 								  mattr,		     
 								  cc,
 								  ret_type,		     
 								  Parameters.GetEmitTypes ());
			
			InvokeBuilder.SetImplementationFlags (MethodImplAttributes.Runtime);

			TypeManager.RegisterMethod (InvokeBuilder, Parameters);
			member_cache.AddMember (InvokeBuilder, this);

			//
			// Don't emit async method for compiler generated delegates (e.g. dynamic site containers)
			//
			if (TypeManager.iasyncresult_type != null && TypeManager.asynccallback_type != null && !IsCompilerGenerated) {
				DefineAsyncMethods (cc);
			}

			return true;
		}
Esempio n. 16
0
		public virtual void ApplyAttributes (MethodBuilder mb, ConstructorBuilder cb, int index)
		{
			if (mb == null)
				builder = cb.DefineParameter (index, Attributes, Name);
			else
				builder = mb.DefineParameter (index, Attributes, Name);

			if (OptAttributes != null)
				OptAttributes.Emit ();

			if (HasDefaultValue) {
				//
				// Emit constant values for true constants only, the other
				// constant-like expressions will rely on default value expression
				//
				Constant c = default_expr as Constant;
				if (c != null) {
					if (default_expr.Type == TypeManager.decimal_type) {
						builder.SetCustomAttribute (Const.CreateDecimalConstantAttribute (c));
					} else {
						builder.SetConstant (c.GetValue ());
					}
				}
			}

			if (TypeManager.IsDynamicType (parameter_type))
				PredefinedAttributes.Get.Dynamic.EmitAttribute (builder, Location);
		}