public AvaloniaAttachedInstanceProperty(XamlAstNamePropertyReference prop, TransformerConfiguration config, IXamlType declaringType, IXamlType type, IXamlType avaloniaPropertyType, IXamlType avaloniaObject, IXamlField field) : base(prop, prop.Name, declaringType, null) { _config = config; _declaringType = declaringType; _avaloniaPropertyType = avaloniaPropertyType; // XamlIl doesn't support generic methods yet if (_avaloniaPropertyType.GenericArguments?.Count > 0) { _avaloniaPropertyType = _avaloniaPropertyType.BaseType; } _avaloniaObject = avaloniaObject; _field = field; PropertyType = type; Setters.Add(new SetterMethod(this)); Getter = new GetterMethod(this); }
public XamlIlAvaloniaProperty(XamlAstClrProperty original, IXamlField field, AvaloniaXamlIlWellKnownTypes types) : base(original, original.Name, original.DeclaringType, original.Getter, original.Setters) { var assignBinding = original.CustomAttributes.Any(ca => ca.Type.Equals(types.AssignBindingAttribute)); AvaloniaProperty = field; CustomAttributes = original.CustomAttributes; if (!assignBinding) { Setters.Insert(0, new BindingSetter(types, original.DeclaringType, field)); } // Styled and attached properties can be set with a BindingPriority when they're // assigned in a ControlTemplate. if (field.FieldType.GenericTypeDefinition == types.StyledPropertyT || field.FieldType.GenericTypeDefinition == types.AvaloniaAttachedPropertyT) { var propertyType = field.FieldType.GenericArguments[0]; Setters.Insert(0, new SetValueWithPrioritySetter(types, original.DeclaringType, field, propertyType)); if (!assignBinding) { Setters.Insert(1, new BindingWithPrioritySetter(types, original.DeclaringType, field)); } } Setters.Insert(0, new UnsetValueSetter(types, original.DeclaringType, field)); }
public XamlIlAvaloniaPropertyFieldNode(AvaloniaXamlIlWellKnownTypes types, IXamlLineInfo lineInfo, IXamlField field) : base(lineInfo) { _field = field; AvaloniaPropertyType = XamlIlAvaloniaPropertyHelper.GetAvaloniaPropertyType(field, types, lineInfo); }
public static void EmitFieldLiteral(IXamlField field, IXamlILEmitter codeGen) { var ftype = field.FieldType.IsEnum ? field.FieldType.GetEnumUnderlyingType() : field.FieldType; if (ftype.Name == "UInt64" || ftype.Name == "Int64") { codeGen.Emit(OpCodes.Ldc_I8, TypeSystemHelpers.ConvertLiteralToLong(field.GetLiteralValue())); } else if (ftype.Name == "Double") { codeGen.Emit(OpCodes.Ldc_R8, (double)field.GetLiteralValue()); } else if (ftype.Name == "Single") { codeGen.Emit(OpCodes.Ldc_R4, (float)field.GetLiteralValue()); } else if (ftype.Name == "String") { codeGen.Emit(OpCodes.Ldstr, (string)field.GetLiteralValue()); } else { codeGen.Emit(OpCodes.Ldc_I4, TypeSystemHelpers.ConvertLiteralToInt(field.GetLiteralValue())); } }
public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node) { if (node is XamlAstClrProperty prop) { if (prop.DeclaringType.Assembly == context.GetWinUITypes().WinUIControlsAssembly) { IXamlField propertyIndexMaybe = context.GetWinUITypes().XamlPropertyIndex.Fields.FirstOrDefault(f => f.Name == $"{prop.DeclaringType.Name}_{prop.Name}"); if (propertyIndexMaybe != null) { prop.Setters.Insert(0, new XamlDirectSetter(context.GetWinUITypes(), prop.Getter.ReturnType, prop.DeclaringType, propertyIndexMaybe)); foreach (var adder in XamlTransformHelpers.FindPossibleAdders(context, prop.Getter.ReturnType)) { if (adder.Parameters.Count == 1) { prop.Setters.Add(new XamlDirectAdderSetter(context.GetWinUITypes(), adder.Parameters[0], prop.DeclaringType, propertyIndexMaybe)); } } return(prop); } IXamlField eventIndexMaybe = context.GetWinUITypes().XamlEventIndex.Fields.FirstOrDefault(f => f.Name == $"{prop.DeclaringType.Name}_{prop.Name}"); if (eventIndexMaybe != null) { prop.Setters.Insert(0, new XamlDirectEventSetter(context.GetWinUITypes(), prop.Setters[0].Parameters[0], prop.DeclaringType, eventIndexMaybe)); return(prop); } } } return(node); }
public XamlIlAttacchedPropertyEqualsSelector(XamlIlSelectorNode previous, IXamlField propertyFiled, IXamlAstValueNode value) : base(previous) { PropertyFiled = propertyFiled; Value = value; }
public AvaloniaPropertyCustomSetter(AvaloniaXamlIlWellKnownTypes types, IXamlType declaringType, IXamlField avaloniaProperty) { Types = types; AvaloniaProperty = avaloniaProperty; TargetType = declaringType; }
public XamlDirectEventSetter(WellKnownWinUITypes winUITypes, IXamlType propertyType, IXamlType targetType, IXamlField eventIndex) { Parameters = new[] { propertyType }; WinUITypes = winUITypes; TargetType = targetType; EventIndex = eventIndex; }
public XamlDirectAdderSetter(WellKnownWinUITypes winUITypes, IXamlType propertyType, IXamlType targetType, IXamlField propertyIndex) { Parameters = new[] { propertyType }; WinUITypes = winUITypes; TargetType = targetType; PropertyIndex = propertyIndex; }
public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node) { if (node is XamlValueWithManipulationNode valWithManip && TryGetNewObjectNodeForXamlDirect(valWithManip, out var newObj) && newObj.Arguments.Count == 0) { if (!(valWithManip.Manipulation is XamlObjectInitializationNode init) || !(init.Manipulation is XamlManipulationGroupNode manipGrp)) { throw new XamlParseException( "Unable to find the object initialization node inside object creation.", node); } IXamlType objType = newObj.Type.GetClrType(); if (objType.Assembly != context.GetWinUITypes().WinUIControlsAssembly) { // Only built-in controls have XamlDirect support. // Remove the XamlDirect setters since XamlDirect isn't worth using once the object // is created and return. RemoveXamlDirectSettersFromAssignments(manipGrp); return(node); } IXamlField typeIndex = context.GetWinUITypes().XamlTypeIndex.GetAllFields().FirstOrDefault(fld => fld.Name == objType.Name); if (typeIndex is null) { // We didn't find a matching type index, so we can't use XamlDirect. // Remove the XamlDirect setters since XamlDirect isn't worth using once the object // is created and return. RemoveXamlDirectSettersFromAssignments(manipGrp); return(node); } IXamlAstValueNode constructedObjNode = new XamlDirectNewObjectNode(newObj, context.GetWinUITypes().IXamlDirectObject, typeIndex); var xamlDirectAssignmentsNode = new XamlManipulationGroupNode(constructedObjNode); var standardAssignmentsNode = new XamlManipulationGroupNode(constructedObjNode); ExtractXamlDirectAssignments(manipGrp, xamlDirectAssignmentsNode, standardAssignmentsNode); if (xamlDirectAssignmentsNode.Children.Count != 0) { constructedObjNode = new XamlValueWithManipulationNode(constructedObjNode, constructedObjNode, xamlDirectAssignmentsNode); } if (standardAssignmentsNode.Children.Count == 0) { node = new XamlObjectFromDirectObjectNode(constructedObjNode, newObj.Type); } else { valWithManip.Value = new XamlObjectFromDirectObjectNode(constructedObjNode, newObj.Type); init.Manipulation = standardAssignmentsNode; } } return(node);
public static object GetLiteralFieldConstantValue(IXamlField field) { var value = field.GetLiteralValue(); //This code is needed for SRE backend that returns an actual enum instead of just int if (value.GetType().IsEnum) { value = Convert.ChangeType(value, value.GetType().GetEnumUnderlyingType()); } return(value); }
public XamlIlAvaloniaProperty(XamlAstClrProperty original, IXamlField field, AvaloniaXamlIlWellKnownTypes types) : base(original, original.Name, original.DeclaringType, original.Getter, original.Setters) { AvaloniaProperty = field; CustomAttributes = original.CustomAttributes; if (!original.CustomAttributes.Any(ca => ca.Type.Equals(types.AssignBindingAttribute))) { Setters.Insert(0, new BindingSetter(types, original.DeclaringType, field)); } Setters.Insert(0, new UnsetValueSetter(types, original.DeclaringType, field)); }
protected AvaloniaPropertyCustomSetter( AvaloniaXamlIlWellKnownTypes types, IXamlType declaringType, IXamlField avaloniaProperty, bool allowNull) { Types = types; AvaloniaProperty = avaloniaProperty; TargetType = declaringType; BinderParameters = new PropertySetterBinderParameters { AllowXNull = allowNull, AllowRuntimeNull = allowNull }; }
public static IXamlType GetAvaloniaPropertyType(IXamlField field, AvaloniaXamlIlWellKnownTypes types, IXamlLineInfo lineInfo) { var avaloniaPropertyType = field.FieldType; while (avaloniaPropertyType != null) { if (avaloniaPropertyType.GenericTypeDefinition?.Equals(types.AvaloniaPropertyT) == true) { return(avaloniaPropertyType.GenericArguments[0]); } avaloniaPropertyType = avaloniaPropertyType.BaseType; } throw new XamlX.XamlParseException( $"{field.Name}'s type {field.FieldType} doesn't inherit from AvaloniaProperty<T>, make sure to use typed properties", lineInfo); }
public bool Equals(IXamlField other) => ((SreField)other)?.Field.Equals(Field) == true;
public IXamlILEmitter Emit(OpCode code, IXamlField field) { Record(code, field); _inner.Emit(code, field); return(this); }
public bool Equals(IXamlField other) => other is CecilField cf && cf.Field == Field;
public static XamlConstantNode GetLiteralFieldConstantNode(IXamlField field, IXamlLineInfo info) => new XamlConstantNode(info, field.FieldType, GetLiteralFieldConstantValue(field));
public XamlIlAvaloniaPropertyPropertyPathElementNode(IXamlField field, IXamlType propertyType) { _field = field; Type = propertyType; }
private XamlILContextDefinition(IXamlTypeBuilder <IXamlILEmitter> parentBuilder, IXamlTypeSystem typeSystem, XamlLanguageTypeMappings mappings, XamlLanguageEmitMappings <IXamlILEmitter, XamlILNodeEmitResult> emitMappings) { var so = typeSystem.GetType("System.Object"); var builder = parentBuilder.DefineSubType(so, "Context", true); builder.DefineGenericParameters(new[] { new KeyValuePair <string, XamlGenericParameterConstraint>("TTarget", new XamlGenericParameterConstraint { IsClass = true }) }); var rootObjectField = builder.DefineField(builder.GenericParameters[0], "RootObject", true, false); var intermediateRootObjectField = builder.DefineField(so, XamlRuntimeContextDefintion.IntermediateRootObjectFieldName, true, false); _parentServiceProviderField = builder.DefineField(mappings.ServiceProvider, "_sp", false, false); if (mappings.InnerServiceProviderFactoryMethod != null) { _innerServiceProviderField = builder.DefineField(mappings.ServiceProvider, "_innerSp", false, false); } var staticProvidersField = builder.DefineField(typeSystem.GetType("System.Object").MakeArrayType(1), "_staticProviders", false, false); var systemType = typeSystem.GetType("System.Type"); var systemUri = typeSystem.GetType("System.Uri"); var systemString = typeSystem.GetType("System.String"); var getServiceInterfaceMethod = mappings.ServiceProvider.FindMethod("GetService", so, false, systemType); var ownServices = new List <IXamlType>(); var ctorCallbacks = new List <Action <IXamlILEmitter> >(); if (mappings.RootObjectProvider != null) { builder.AddInterfaceImplementation(mappings.RootObjectProvider); var rootGen = ImplementInterfacePropertyGetter(builder, mappings.RootObjectProvider, XamlRuntimeContextDefintion.RootObjectFieldName) .Generator; var tryParent = rootGen.DefineLabel(); var fail = rootGen.DefineLabel(); var parentRootProvider = rootGen.DefineLocal(mappings.RootObjectProvider); rootGen // if(RootObject!=null) return RootObject; .LdThisFld(rootObjectField) .Box(rootObjectField.FieldType) .Brfalse(tryParent) .LdThisFld(rootObjectField) .Box(rootObjectField.FieldType) .Ret() // if(_sp == null) goto fail; .MarkLabel(tryParent) .LdThisFld(_parentServiceProviderField) .Brfalse(fail) // parentProv = (IRootObjectProvider)_sp.GetService(typeof(IRootObjectProvider)); .LdThisFld(_parentServiceProviderField) .Ldtype(mappings.RootObjectProvider) .EmitCall(getServiceInterfaceMethod) .Castclass(mappings.RootObjectProvider) .Stloc(parentRootProvider) // if(parentProv == null) goto fail; .Ldloc(parentRootProvider) .Brfalse(fail) // return parentProv.Root; .Ldloc(parentRootProvider) .EmitCall(mappings.RootObjectProvider.FindMethod(m => m.Name == "get_RootObject")) .Ret() // fail: .MarkLabel(fail) .Ldnull() .Ret(); if (mappings.RootObjectProviderIntermediateRootPropertyName != null) { ImplementInterfacePropertyGetter(builder, mappings.RootObjectProvider, mappings.RootObjectProviderIntermediateRootPropertyName) .Generator .LdThisFld(intermediateRootObjectField) .Ret(); } ownServices.Add(mappings.RootObjectProvider); } if (mappings.ParentStackProvider != null) { builder.AddInterfaceImplementation(mappings.ParentStackProvider); var objectListType = typeSystem.GetType("System.Collections.Generic.List`1") .MakeGenericType(new[] { typeSystem.GetType("System.Object") }); ParentListField = builder.DefineField(objectListType, XamlRuntimeContextDefintion.ParentListFieldName, true, false); var enumerator = EmitParentEnumerable(typeSystem, parentBuilder, mappings); CreateCallbacks.Add(enumerator.createCallback); var parentStackEnumerableField = builder.DefineField( typeSystem.GetType("System.Collections.Generic.IEnumerable`1").MakeGenericType(new[] { so }), "_parentStackEnumerable", false, false); ImplementInterfacePropertyGetter(builder, mappings.ParentStackProvider, "Parents") .Generator.LdThisFld(parentStackEnumerableField).Ret(); ctorCallbacks.Add(g => g .Emit(OpCodes.Ldarg_0) .Emit(OpCodes.Newobj, objectListType.FindConstructor(new List <IXamlType>())) .Emit(OpCodes.Stfld, ParentListField) .Emit(OpCodes.Ldarg_0) .LdThisFld(ParentListField) .LdThisFld(_parentServiceProviderField) .Emit(OpCodes.Newobj, enumerator.ctor) .Emit(OpCodes.Stfld, parentStackEnumerableField)); ownServices.Add(mappings.ParentStackProvider); } ownServices.Add(EmitTypeDescriptorContextStub(typeSystem, builder, mappings)); if (mappings.ProvideValueTarget != null) { builder.AddInterfaceImplementation(mappings.ProvideValueTarget); PropertyTargetObject = builder.DefineField(so, XamlRuntimeContextDefintion.ProvideTargetObjectName, true, false); PropertyTargetProperty = builder.DefineField(so, XamlRuntimeContextDefintion.ProvideTargetPropertyName, true, false); ImplementInterfacePropertyGetter(builder, mappings.ProvideValueTarget, "TargetObject") .Generator.LdThisFld(PropertyTargetObject).Ret(); ImplementInterfacePropertyGetter(builder, mappings.ProvideValueTarget, "TargetProperty") .Generator.LdThisFld(PropertyTargetProperty).Ret(); ownServices.Add(mappings.ProvideValueTarget); } IXamlField baseUriField = null; if (mappings.UriContextProvider != null) { baseUriField = builder.DefineField(systemUri, "_baseUri", false, false); builder.AddInterfaceImplementation(mappings.UriContextProvider); var getter = builder.DefineMethod(systemUri, new IXamlType[0], "get_BaseUri", true, false, true); var setter = builder.DefineMethod(typeSystem.GetType("System.Void"), new[] { systemUri }, "set_BaseUri", true, false, true); getter.Generator .LdThisFld(baseUriField) .Ret(); setter.Generator .Ldarg_0() .Ldarg(1) .Stfld(baseUriField) .Ret(); builder.DefineProperty(systemUri, "BaseUri", setter, getter); ownServices.Add(mappings.UriContextProvider); } builder.AddInterfaceImplementation(mappings.ServiceProvider); var getServiceMethod = builder.DefineMethod(so, new[] { systemType }, "GetService", true, false, true); ownServices = ownServices.Where(s => s != null).ToList(); if (_innerServiceProviderField != null) { var next = getServiceMethod.Generator.DefineLabel(); var innerResult = getServiceMethod.Generator.DefineLocal(so); getServiceMethod.Generator //if(_inner == null) goto next; .LdThisFld(_innerServiceProviderField) .Brfalse(next) // var innerRes = _inner.GetService(type); .LdThisFld(_innerServiceProviderField) .Ldarg(1) .EmitCall(getServiceInterfaceMethod) .Stloc(innerResult) // if(innerRes == null) goto next; .Ldloc(innerResult) .Brfalse(next) // return innerRes .Ldloc(innerResult) .Ret() .MarkLabel(next); } var compare = systemType.FindMethod("Equals", typeSystem.GetType("System.Boolean"), false, systemType); var isAssignableFrom = systemType.FindMethod("IsAssignableFrom", typeSystem.GetType("System.Boolean"), false, systemType); var fromHandle = systemType.Methods.First(m => m.Name == "GetTypeFromHandle"); var getTypeFromObject = so.Methods.First(m => m.Name == "GetType" && m.Parameters.Count == 0); if (ownServices.Count != 0) { for (var c = 0; c < ownServices.Count; c++) { var next = getServiceMethod.Generator.DefineLabel(); getServiceMethod.Generator .Emit(OpCodes.Ldtoken, ownServices[c]) .EmitCall(fromHandle) .Emit(OpCodes.Ldarg_1) .Emit(OpCodes.Callvirt, compare) .Emit(OpCodes.Brfalse, next) .Emit(OpCodes.Ldarg_0) .Emit(OpCodes.Ret) .MarkLabel(next); } } var staticProviderIndex = getServiceMethod.Generator.DefineLocal(typeSystem.GetType("System.Int32")); var staticProviderNext = getServiceMethod.Generator.DefineLabel(); var staticProviderFailed = getServiceMethod.Generator.DefineLabel(); var staticProviderEnd = getServiceMethod.Generator.DefineLabel(); var staticProviderElement = getServiceMethod.Generator.DefineLocal(so); getServiceMethod.Generator //start: if(_staticProviders == null) goto: end .LdThisFld(staticProvidersField) .Brfalse(staticProviderEnd) // var c = 0 .Ldc_I4(0) .Stloc(staticProviderIndex) // next: .MarkLabel(staticProviderNext) // if(c >= _staticProviders.Length) goto: end .Ldloc(staticProviderIndex) .LdThisFld(staticProvidersField) .Ldlen() .Bge(staticProviderEnd) // var obj = _staticProviders[c] .LdThisFld(staticProvidersField) .Ldloc(staticProviderIndex) .Ldelem_ref() // dup .Stloc(staticProviderElement) .Ldarg(1) .Ldloc(staticProviderElement) // if(obj.GetType().Equals(arg1)) return obj; else goto failed; .EmitCall(getTypeFromObject) .EmitCall(isAssignableFrom) .Brfalse(staticProviderFailed) .Ldloc(staticProviderElement) .Ret() // failed: .MarkLabel(staticProviderFailed) // c++ .Ldloc(staticProviderIndex) .Ldc_I4(1) .Add() .Stloc(staticProviderIndex) // goto: start .Br(staticProviderNext) // end: .MarkLabel(staticProviderEnd); var noParentProvider = getServiceMethod.Generator.DefineLabel(); getServiceMethod.Generator .LdThisFld(_parentServiceProviderField) .Brfalse(noParentProvider) .LdThisFld(_parentServiceProviderField) .Ldarg(1) .EmitCall(getServiceInterfaceMethod) .Emit(OpCodes.Ret) .MarkLabel(noParentProvider) .Ldnull() .Ret(); var ctor = builder.DefineConstructor(false, mappings.ServiceProvider, staticProvidersField.FieldType, systemString); ctor.Generator .Emit(OpCodes.Ldarg_0) .Emit(OpCodes.Call, so.Constructors.First()) .Emit(OpCodes.Ldarg_0) .Emit(OpCodes.Ldarg_1) .Emit(OpCodes.Stfld, _parentServiceProviderField) .Emit(OpCodes.Ldarg_0) .Emit(OpCodes.Ldarg_2) .Emit(OpCodes.Stfld, staticProvidersField); if (baseUriField != null) { var noUri = ctor.Generator.DefineLabel(); ctor.Generator .Emit(OpCodes.Ldarg_3) .Brfalse(noUri) .Emit(OpCodes.Ldarg_0) .Emit(OpCodes.Ldarg_3) .Newobj(systemUri.FindConstructor(new List <IXamlType> { typeSystem.GetType("System.String") })) .Emit(OpCodes.Stfld, baseUriField) .MarkLabel(noUri); } foreach (var feature in ctorCallbacks) { feature(ctor.Generator); } emitMappings.ContextTypeBuilderCallback?.Invoke(builder, ctor.Generator); // We are calling this last to ensure that our own services are ready if (_innerServiceProviderField != null) { ctor.Generator // _innerSp = InnerServiceProviderFactory(this) .Ldarg_0() .Ldarg_0() .EmitCall(mappings.InnerServiceProviderFactoryMethod) .Stfld(_innerServiceProviderField); } ctor.Generator.Emit(OpCodes.Ret); Constructor = ctor; CreateCallbacks.Add(() => { parentBuilder.CreateType(); }); if (ParentListField != null) { EmitPushPopParent(builder, typeSystem); } CreateAllTypes(); ContextType = builder.CreateType(); }
public IXamlILEmitter Emit(SreOpCode code, IXamlField field) { return(Emit(Instruction.Create(Dic[code], Import(((CecilField)field).Field)))); }
public SetValueWithPrioritySetter(AvaloniaXamlIlWellKnownTypes types, IXamlType declaringType, IXamlField avaloniaProperty, IXamlType propertyType) : base(types, declaringType, avaloniaProperty, propertyType.AcceptsNull()) { Parameters = new[] { types.BindingPriority, propertyType }; }
public static IXamlILEmitter Stsfld(this IXamlILEmitter emitter, IXamlField field) => emitter.Emit(OpCodes.Stsfld, field);
public IXamlILEmitter Emit(OpCode code, IXamlField field) { _ilg.Emit(code, ((SreField)field).Field); return(this); }
public static IXamlILEmitter LdThisFld(this IXamlILEmitter emitter, IXamlField field) => emitter.Ldarg_0().Emit(OpCodes.Ldfld, field);
public UnsetValueSetter(AvaloniaXamlIlWellKnownTypes types, IXamlType declaringType, IXamlField avaloniaProperty) : base(types, declaringType, avaloniaProperty) { Parameters = new[] { types.UnsetValueType }; }
public BindingSetter(AvaloniaXamlIlWellKnownTypes types, IXamlType declaringType, IXamlField avaloniaProperty) : base(types, declaringType, avaloniaProperty) { Parameters = new[] { types.IBinding }; }
public BindingWithPrioritySetter(AvaloniaXamlIlWellKnownTypes types, IXamlType declaringType, IXamlField avaloniaProperty) : base(types, declaringType, avaloniaProperty, false) { Parameters = new[] { types.BindingPriority, types.IBinding }; }