Exemplo n.º 1
0
        internal void Emit(CodeGenerator /*!*/ codeGen, IPhpCustomAttributeProvider /*!*/ target)
        {
            if (attributes == null)
            {
                return;
            }

            foreach (CustomAttribute attribute in attributes)
            {
                attribute.Emit(codeGen, target);
            }
        }
Exemplo n.º 2
0
        internal void Analyze(Analyzer /*!*/ analyzer, IPhpCustomAttributeProvider /*!*/ target)
        {
            if (attributes == null)
            {
                return;
            }

            bool duplicate_found = false;

            foreach (CustomAttribute attribute in attributes)
            {
                attribute.Analyze(analyzer, target, ref duplicate_found);
            }
        }
Exemplo n.º 3
0
            /// <summary>
            /// Analyses attribute parameters, resolves the constructor, the fields, and the properties
            /// assigned in the parameters.
            /// </summary>
            public void Analyze(Analyzer /*!*/ analyzer, IPhpCustomAttributeProvider /*!*/ target, ref bool duplicateFound)
            {
                Debug.Assert(type != null);

                this.target = target;

                // check selector:
                if (((int)target.AcceptsTargets & (int)node.TargetSelector) == 0)
                {
                    analyzer.ErrorSink.Add(Errors.InvalidAttributeTargetSelector, analyzer.SourceUnit, node.Span,
                                           node.TargetSelector.ToString().ToLower(System.Globalization.CultureInfo.InvariantCulture));
                }

                // resolve constructor for the regular attributes:
                if (!ResolveConstructor(analyzer))
                {
                    return;
                }

                // evaluate actual arguments:
                bool all_evaluable;

                AnalyzeActualArguments(analyzer, out all_evaluable);

                // check usage:
                if (!CheckAttributeUsage(analyzer, ref duplicateFound))
                {
                    return;
                }

                if (!all_evaluable)
                {
                    return;
                }

                // process special and pseudo-attributes:
                ApplySpecialAttributes(analyzer);
            }
Exemplo n.º 4
0
 public static void Emit(this CustomAttribute /*!*/ node, CodeGenerator /*!*/ codeGen, IPhpCustomAttributeProvider /*!*/ target, bool force)
 {
     node.Compiler().Emit(codeGen, target, force);
 }
Exemplo n.º 5
0
 public static void Analyze(this CustomAttribute /*!*/ node, Analyzer /*!*/ analyzer, IPhpCustomAttributeProvider /*!*/ target, ref bool duplicateFound)
 {
     node.Compiler().Analyze(analyzer, target, ref duplicateFound);
 }
Exemplo n.º 6
0
		internal void Analyze(Analyzer/*!*/ analyzer, IPhpCustomAttributeProvider/*!*/ target)
		{
			if (attributes == null) return;

			bool duplicate_found = false;

			foreach (CustomAttribute attribute in attributes)
				attribute.Analyze(analyzer, target, ref duplicate_found);
		}
Exemplo n.º 7
0
 public static void Analyze(this CustomAttributes /*!*/ node, Analyzer /*!*/ analyzer, IPhpCustomAttributeProvider /*!*/ target)
 {
     node.Compiler().Analyze(node, analyzer, target);
 }
Exemplo n.º 8
0
 public void Emit(CodeGenerator /*!*/ codeGen, IPhpCustomAttributeProvider /*!*/ target)
 {
     Emit(codeGen, target, false);
 }
Exemplo n.º 9
0
 public void Emit(CodeGenerator/*!*/ codeGen, IPhpCustomAttributeProvider/*!*/ target)
 {
     Emit(codeGen, target, false);
 }
Exemplo n.º 10
0
            /// <summary>
            /// Analyses attribute parameters, resolves the constructor, the fields, and the properties 
            /// assigned in the parameters.
            /// </summary>
            public void Analyze(Analyzer/*!*/ analyzer, IPhpCustomAttributeProvider/*!*/ target, ref bool duplicateFound)
            {
                Debug.Assert(type != null);

                this.target = target;

                // check selector:
                if (((int)target.AcceptsTargets & (int)node.TargetSelector) == 0)
                {
                    analyzer.ErrorSink.Add(Errors.InvalidAttributeTargetSelector, analyzer.SourceUnit, node.Position,
                        node.TargetSelector.ToString().ToLower(System.Globalization.CultureInfo.InvariantCulture));
                }

                // resolve constructor for the regular attributes:
                if (!ResolveConstructor(analyzer))
                    return;

                // evaluate actual arguments:
                bool all_evaluable;
                AnalyzeActualArguments(analyzer, out all_evaluable);

                // check usage:
                if (!CheckAttributeUsage(analyzer, ref duplicateFound))
                    return;

                if (!all_evaluable)
                    return;

                // process special and pseudo-attributes:
                ApplySpecialAttributes(analyzer);
            }
Exemplo n.º 11
0
            public void Emit(CustomAttributes/*!*/node, CodeGenerator/*!*/ codeGenerator, IPhpCustomAttributeProvider/*!*/ target)
            {
                if (node.Attributes == null) return;

                foreach (CustomAttribute attribute in node.Attributes)
                    attribute.Emit(codeGenerator, target);
            }
Exemplo n.º 12
0
            public void Analyze(CustomAttributes/*!*/node, Analyzer/*!*/ analyzer, IPhpCustomAttributeProvider/*!*/ target)
            {
                if (node.Attributes == null) return;

                bool duplicate_found = false;

                foreach (CustomAttribute attribute in node.Attributes)
                    attribute.Analyze(analyzer, target, ref duplicate_found);
            }
Exemplo n.º 13
0
 public static void Emit(this CustomAttribute/*!*/node, CodeGenerator/*!*/ codeGen, IPhpCustomAttributeProvider/*!*/ target, bool force)
 {
     node.Compiler().Emit(codeGen, target, force);
 }
Exemplo n.º 14
0
 public static void Analyze(this CustomAttribute/*!*/node, Analyzer/*!*/ analyzer, IPhpCustomAttributeProvider/*!*/ target, ref bool duplicateFound)
 {
     node.Compiler().Analyze(analyzer, target, ref duplicateFound);
 }
Exemplo n.º 15
0
		internal void Emit(CodeGenerator/*!*/ codeGen, IPhpCustomAttributeProvider/*!*/ target)
		{
			if (attributes == null) return;

			foreach (CustomAttribute attribute in attributes)
				attribute.Emit(codeGen, target);
		}
Exemplo n.º 16
0
            public void Emit(CustomAttributes /*!*/ node, CodeGenerator /*!*/ codeGenerator, IPhpCustomAttributeProvider /*!*/ target)
            {
                if (node.Attributes == null)
                {
                    return;
                }

                foreach (CustomAttribute attribute in node.Attributes)
                {
                    attribute.Emit(codeGenerator, target);
                }
            }
Exemplo n.º 17
0
            public void Emit(CodeGenerator/*!*/ codeGen, IPhpCustomAttributeProvider/*!*/ target, bool force)
            {
                // skip attributes that are not emitted:
                if (!isEmitted && !force) return;

                ConstructorInfo real_ctor;

                // TODO: type conversions (in analysis during overload resolution?)

                Type[] real_ctor_parameter_types = new Type[node.CallSignature.Parameters.Count];

                if (type is ClrType)
                {
                    real_ctor = (ConstructorInfo)((ClrMethod.Overload)overload).Method;
                    var real_params = real_ctor.GetParameters();

                    for (int i = 0; i < real_ctor_parameter_types.Length; i++)
                        real_ctor_parameter_types[i] = real_params[i].ParameterType;
                }
                else
                {
                    Debug.Assert(type is PhpType);
                    real_ctor = ((PhpType)type).ClrConstructorInfos[0];

                    // Do not try to call GetParameters(), all parameters are of type object,
                    // GetParameters() of not baked PhpType throws.
                    for (int i = 0; i < real_ctor_parameter_types.Length; i++)
                        real_ctor_parameter_types[i] = PHP.Core.Emit.Types.Object[0];
                }

                // ctor args:
                object[] ctor_args = new object[node.CallSignature.Parameters.Count];

                for (int i = 0; i < node.CallSignature.Parameters.Count; i++)
                {
                    Expression expr = node.CallSignature.Parameters[i].Expression;

                    TypeOfEx type_of = expr as TypeOfEx;
                    if (type_of != null)
                    {
                        ctor_args[i] = type_of.ClassNameRef.ResolvedType().RealType;
                    }
                    else
                    {
                        ctor_args[i] = ConvertToClr.ObjectToType(expr.GetValue(), real_ctor_parameter_types[i]);
                    }
                }

                List<FieldInfo> fields = new List<FieldInfo>();
                List<PropertyInfo> properties = new List<PropertyInfo>();
                List<object> field_values = new List<object>();
                List<object> prop_values = new List<object>();

                foreach (NamedActualParam param in node.NamedParameters)
                {
                    object value = param.Expression.GetValue();

                    MemberInfo real_member = param.GetProperty().RealMember;
                    FieldInfo real_field;
                    PropertyInfo real_property;

                    if ((real_property = real_member as PropertyInfo) != null ||    // regular CLR property
                        (param.GetProperty() is PhpField && (real_property = ((PhpField)param.GetProperty()).ExportedProperty) != null))  // or PHP property (real field below is PhpReference, we have to use its export stub)
                    {
                        properties.Add(real_property);
                        prop_values.Add(ConvertToClr.ObjectToType(value, real_property.PropertyType));
                    }
                    else if ((real_field = real_member as FieldInfo) != null)
                    {
                        fields.Add(real_field);
                        field_values.Add(ConvertToClr.ObjectToType(value, real_field.FieldType));
                    }
                    else
                    {
                        Debug.Fail("Cannot resolve attribute named parameter!");
                    }
                }

                CustomAttributeBuilder builder = new CustomAttributeBuilder(real_ctor, ctor_args,
                    properties.ToArray(), prop_values.ToArray(), fields.ToArray(), field_values.ToArray());

                switch (node.TargetSelector)
                {
                    case CustomAttribute.TargetSelectors.Assembly:
                    case CustomAttribute.TargetSelectors.Module:
                        codeGen.CompilationUnit.ModuleBuilder.AssemblyBuilder.EmitCustomAttribute(builder, node.TargetSelector);
                        break;

                    default:
                        target.EmitCustomAttribute(builder, node.TargetSelector);
                        break;
                }
            }
Exemplo n.º 18
0
 public static void Analyze(this CustomAttributes/*!*/node, Analyzer/*!*/ analyzer, IPhpCustomAttributeProvider/*!*/ target)
 {
     node.Compiler().Analyze(node, analyzer, target);
 }
Exemplo n.º 19
0
            public void Emit(CodeGenerator /*!*/ codeGen, IPhpCustomAttributeProvider /*!*/ target, bool force)
            {
                // skip attributes that are not emitted:
                if (!isEmitted && !force)
                {
                    return;
                }

                ConstructorInfo real_ctor;

                // TODO: type conversions (in analysis during overload resolution?)

                var parameters = node.CallSignature.Parameters;

                Type[] real_ctor_parameter_types = new Type[parameters.Length];

                if (type is ClrType)
                {
                    real_ctor = (ConstructorInfo)((ClrMethod.Overload)overload).Method;
                    var real_params = real_ctor.GetParameters();

                    for (int i = 0; i < real_ctor_parameter_types.Length; i++)
                    {
                        real_ctor_parameter_types[i] = real_params[i].ParameterType;
                    }
                }
                else
                {
                    Debug.Assert(type is PhpType);
                    real_ctor = ((PhpType)type).ClrConstructorInfos[0];

                    // Do not try to call GetParameters(), all parameters are of type object,
                    // GetParameters() of not baked PhpType throws.
                    for (int i = 0; i < real_ctor_parameter_types.Length; i++)
                    {
                        real_ctor_parameter_types[i] = PHP.Core.Emit.Types.Object[0];
                    }
                }

                // ctor args:
                object[] ctor_args = new object[parameters.Length];

                for (int i = 0; i < parameters.Length; i++)
                {
                    Expression expr = parameters[i].Expression;

                    TypeOfEx type_of = expr as TypeOfEx;
                    if (type_of != null)
                    {
                        ctor_args[i] = type_of.ClassNameRef.ResolvedType().RealType;
                    }
                    else
                    {
                        ctor_args[i] = ConvertToClr.ObjectToType(expr.GetValue(), real_ctor_parameter_types[i]);
                    }
                }

                List <FieldInfo>    fields       = new List <FieldInfo>();
                List <PropertyInfo> properties   = new List <PropertyInfo>();
                List <object>       field_values = new List <object>();
                List <object>       prop_values  = new List <object>();

                foreach (NamedActualParam param in node.NamedParameters)
                {
                    object value = param.Expression.GetValue();

                    MemberInfo   real_member = param.GetProperty().RealMember;
                    FieldInfo    real_field;
                    PropertyInfo real_property;

                    if ((real_property = real_member as PropertyInfo) != null ||                                                         // regular CLR property
                        (param.GetProperty() is PhpField && (real_property = ((PhpField)param.GetProperty()).ExportedProperty) != null)) // or PHP property (real field below is PhpReference, we have to use its export stub)
                    {
                        properties.Add(real_property);
                        prop_values.Add(ConvertToClr.ObjectToType(value, real_property.PropertyType));
                    }
                    else if ((real_field = real_member as FieldInfo) != null)
                    {
                        fields.Add(real_field);
                        field_values.Add(ConvertToClr.ObjectToType(value, real_field.FieldType));
                    }
                    else
                    {
                        Debug.Fail("Cannot resolve attribute named parameter!");
                    }
                }

                CustomAttributeBuilder builder = new CustomAttributeBuilder(real_ctor, ctor_args,
                                                                            properties.ToArray(), prop_values.ToArray(), fields.ToArray(), field_values.ToArray());

                switch (node.TargetSelector)
                {
                case CustomAttribute.TargetSelectors.Assembly:
                case CustomAttribute.TargetSelectors.Module:
                    codeGen.CompilationUnit.ModuleBuilder.AssemblyBuilder.EmitCustomAttribute(builder, node.TargetSelector);
                    break;

                default:
                    target.EmitCustomAttribute(builder, node.TargetSelector);
                    break;
                }
            }
Exemplo n.º 20
0
 public static void Emit(this CustomAttributes/*!*/node, CodeGenerator/*!*/ codeGenerator, IPhpCustomAttributeProvider/*!*/ target)
 {
     node.Compiler().Emit(node, codeGenerator, target);
 }
Exemplo n.º 21
0
 public static void Emit(this CustomAttributes /*!*/ node, CodeGenerator /*!*/ codeGenerator, IPhpCustomAttributeProvider /*!*/ target)
 {
     node.Compiler().Emit(node, codeGenerator, target);
 }
Exemplo n.º 22
0
		internal void Emit(CodeGenerator/*!*/ codeGen, IPhpCustomAttributeProvider/*!*/ target, bool force)
		{
			// skip attributes that are not emitted:
			if (!isEmitted && !force) return;

			ConstructorInfo real_ctor;

			// TODO: type conversions (in analysis during overload resolution?)

			if (type is ClrType)
			{
				real_ctor = (ConstructorInfo)((ClrMethod.Overload)overload).Method;
			}
			else
			{
				Debug.Assert(type is PhpType);
				real_ctor = ((PhpType)type).ClrConstructorInfos[0];
			}

			// ctor args:
			object[] ctor_args = new object[callSignature.Parameters.Count];
			
			for (int i = 0; i < callSignature.Parameters.Count; i++)
			{
				Expression expr = callSignature.Parameters[i].Expression;
				
				TypeOfEx type_of = expr as TypeOfEx;
				if (type_of != null)
				{
					ctor_args[i] = type_of.ClassNameRef.ResolvedType.RealType;
				}
				else
				{
                    ctor_args[i] = ConvertToClr.ObjectToType( expr.Value, real_ctor.GetParameters()[i].ParameterType );
				}
			}	

			List<FieldInfo> fields = new List<FieldInfo>();
			List<PropertyInfo> properties = new List<PropertyInfo>();
			List<object> field_values = new List<object>();
			List<object> prop_values = new List<object>();

			foreach (NamedActualParam param in namedParameters)
			{
				object value = param.Expression.Value;

				MemberInfo real_member = param.Property.RealMember;
				FieldInfo real_field = real_member as FieldInfo;

				if (real_field != null)
				{
					fields.Add(real_field);
                    field_values.Add(ConvertToClr.ObjectToType(value, real_field.FieldType));
				}
				else
				{
                    PropertyInfo real_property = real_member as PropertyInfo;
                    Debug.Assert(real_property != null);

                    properties.Add(real_property);
                    field_values.Add(ConvertToClr.ObjectToType(value, real_property.PropertyType));
				}
			}

			CustomAttributeBuilder builder = new CustomAttributeBuilder(real_ctor, ctor_args,
				properties.ToArray(), prop_values.ToArray(), fields.ToArray(), field_values.ToArray());

			switch (targetSelector)
			{
				case TargetSelectors.Assembly:
				case TargetSelectors.Module:
					codeGen.CompilationUnit.ModuleBuilder.AssemblyBuilder.EmitCustomAttribute(builder, targetSelector);
					break;

				default:
					target.EmitCustomAttribute(builder, targetSelector);
					break;
			}
		}