public XamlIlDirectCallPropertySetter(IXamlIlMethod method, IXamlIlType type) { _method = method; _type = type; Parameters = new[] { type }; TargetType = method.ThisOrFirstParameter(); }
public XamlIlAvaloniaPropertyNode(IXamlIlLineInfo lineInfo, IXamlIlType type, XamlIlAstClrProperty property) : base(lineInfo) { Type = new XamlIlAstClrTypeReference(this, type, false); Property = property; AvaloniaPropertyType = Property.Getter?.ReturnType ?? Property.Setters.First().Parameters[0]; }
public static IXamlIlMethod FindMethod(this IXamlIlType type, Func <IXamlIlMethod, bool> criteria) { foreach (var m in type.Methods) { if (criteria(m)) { return(m); } } var bres = type.BaseType?.FindMethod(criteria); if (bres != null) { return(bres); } foreach (var iface in type.Interfaces) { foreach (var m in iface.Methods) { if (criteria(m)) { return(m); } } } return(null); }
public static IXamlIlConstructor FindConstructor(this IXamlIlType type, List <IXamlIlType> args = null) { if (args == null) { args = new List <IXamlIlType>(); } foreach (var ctor in type.Constructors.Where(c => c.IsPublic && !c.IsStatic && c.Parameters.Count == args.Count)) { var mismatch = false; for (var c = 0; c < args.Count; c++) { mismatch = !ctor.Parameters[c].IsAssignableFrom(args[c]); if (mismatch) { break; } } if (!mismatch) { return(ctor); } } return(null); }
public XamlIlSelectorNode(XamlIlSelectorNode previous, IXamlIlLineInfo info = null, IXamlIlType selectorType = null) : base(info ?? previous) { Previous = previous; Type = selectorType == null ? previous.Type : new XamlIlAstClrTypeReference(this, selectorType, false); }
public static IEnumerable <IXamlIlMethod> FindMethods(this IXamlIlType type, Func <IXamlIlMethod, bool> criteria) { foreach (var m in type.Methods) { if (criteria(m)) { yield return(m); } } var bt = type.BaseType; if (bt != null) { foreach (var bm in bt.FindMethods(criteria)) { yield return(bm); } } foreach (var iface in type.Interfaces) { foreach (var m in iface.Methods) { if (criteria(m)) { yield return(m); } } } }
public bool IsAssignableFrom(IXamlIlType type) { if (!type.IsValueType && type == XamlIlPseudoType.Null) { return(true); } if (type.IsValueType && type.GenericTypeDefinition?.FullName == "System.Nullable`1") { return(true); } if (FullName == "System.Object" && type.IsInterface) { return(true); } var baseType = type; while (baseType != null) { if (baseType.Equals(this)) { return(true); } baseType = baseType.BaseType; } if (IsInterface && type.GetAllInterfaces().Any(IsAssignableFrom)) { return(true); } return(false); }
public XamlIlContext(IXamlIlType definition, IXamlIlType constructedType, string baseUri, List <IXamlIlField> staticProviders) : this(definition, constructedType, (context, codegen) => { if (staticProviders?.Count > 0) { var so = codegen.TypeSystem.GetType("System.Object"); codegen.Ldc_I4(staticProviders.Count) .Newarr(so); for (var c = 0; c < staticProviders.Count; c++) { codegen .Dup() .Ldc_I4(c) .Ldsfld(staticProviders[c]) .Castclass(so) .Stelem_ref(); } } else { codegen.Ldnull(); } codegen.Ldstr(baseUri) .Newobj(context.Constructor); }) { }
public AttributeResolver(IXamlIlTypeSystem typeSystem, XamlIlLanguageTypeMappings mappings) { _typeConverterAttribute = mappings.TypeConverterAttributes.First(); void AddType(IXamlIlType type, IXamlIlType conv) => _converters.Add(new KeyValuePair <IXamlIlType, IXamlIlType>(type, conv)); void Add(string type, string conv) => AddType(typeSystem.GetType(type), typeSystem.GetType(conv)); Add("Avalonia.Media.Imaging.IBitmap", "Avalonia.Markup.Xaml.Converters.BitmapTypeConverter"); var ilist = typeSystem.GetType("System.Collections.Generic.IList`1"); AddType(ilist.MakeGenericType(typeSystem.GetType("Avalonia.Point")), typeSystem.GetType("Avalonia.Markup.Xaml.Converters.PointsListTypeConverter")); Add("Avalonia.Controls.Templates.IMemberSelector", "Avalonia.Markup.Xaml.Converters.MemberSelectorTypeConverter"); Add("Avalonia.Controls.WindowIcon", "Avalonia.Markup.Xaml.Converters.IconTypeConverter"); Add("System.Globalization.CultureInfo", "System.ComponentModel.CultureInfoConverter"); Add("System.Uri", "Avalonia.Markup.Xaml.Converters.AvaloniaUriTypeConverter"); Add("System.TimeSpan", "Avalonia.Markup.Xaml.Converters.TimeSpanTypeConverter"); Add("Avalonia.Media.FontFamily", "Avalonia.Markup.Xaml.Converters.FontFamilyTypeConverter"); _avaloniaList = typeSystem.GetType("Avalonia.Collections.AvaloniaList`1"); _avaloniaListConverter = typeSystem.GetType("Avalonia.Collections.AvaloniaListConverter`1"); }
public XamlIlLoadMethodDelegateNode(IXamlIlLineInfo lineInfo, IXamlIlAstValueNode value, IXamlIlType delegateType, IXamlIlMethod method) : base(lineInfo, value) { DelegateType = delegateType; Method = method; Type = new XamlIlAstClrTypeReference(value, DelegateType); }
public static IXamlIlEmitter Ldtype(this IXamlIlEmitter emitter, IXamlIlType type) { var conv = emitter.TypeSystem.GetType("System.Type") .FindMethod(m => m.IsStatic && m.IsPublic && m.Name == "GetTypeFromHandle"); return(emitter.Ldtoken(type).EmitCall(conv)); }
public static bool CustomValueConverter(XamlIlAstTransformationContext context, IXamlIlAstValueNode node, IXamlIlType type, out IXamlIlAstValueNode result) { if (type.FullName == "System.TimeSpan" && node is XamlIlAstTextNode tn && !tn.Text.Contains(":")) { var seconds = double.Parse(tn.Text, CultureInfo.InvariantCulture); result = new XamlIlStaticOrTargetedReturnMethodCallNode(tn, type.FindMethod("FromSeconds", type, false, context.Configuration.WellKnownTypes.Double), new[] { new XamlIlConstantNode(tn, context.Configuration.WellKnownTypes.Double, seconds) }); return(true); } if (type.FullName == "Avalonia.AvaloniaProperty") { var scope = context.ParentNodes().OfType <AvaloniaXamlIlTargetTypeMetadataNode>().FirstOrDefault(); if (scope == null) { throw new XamlIlLoadException("Unable to find the parent scope for AvaloniaProperty lookup", node); } if (!(node is XamlIlAstTextNode text)) { throw new XamlIlLoadException("Property should be a text node", node); } result = XamlIlAvaloniaPropertyHelper.CreateNode(context, text.Text, scope.TargetType, text); return(true); } result = null; return(false); }
public XamlIlTypeExtensionNode(IXamlIlLineInfo lineInfo, IXamlIlAstTypeReference value, IXamlIlType systemType) : base(lineInfo) { _systemType = systemType; Type = new XamlIlAstClrTypeReference(this, systemType); Value = value; }
public XamlIlObjectInitializationNode(IXamlIlLineInfo lineInfo, IXamlIlAstManipulationNode manipulation, IXamlIlType type) : base(lineInfo) { Manipulation = manipulation; Type = type; }
public AvaloniaAttachedInstanceProperty(XamlIlAstNamePropertyReference prop, XamlIlTransformerConfiguration config, IXamlIlType declaringType, IXamlIlType type, IXamlIlType avaloniaPropertyType, IXamlIlType avaloniaObject, IXamlIlField 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 XamlIlContext(IXamlIlType definition, IXamlIlType constructedType, XamlIlLanguageTypeMappings mappings, Action <XamlIlContext, IXamlIlEmitter> factory) { ContextType = definition.MakeGenericType(constructedType); IXamlIlField Get(string s) => ContextType.Fields.FirstOrDefault(f => f.Name == s); IXamlIlMethod GetMethod(string s) => ContextType.Methods.FirstOrDefault(f => f.Name == s); RootObjectField = Get(XamlIlContextDefinition.RootObjectFieldName); IntermediateRootObjectField = Get(XamlIlContextDefinition.IntermediateRootObjectFieldName); ParentListField = Get(XamlIlContextDefinition.ParentListFieldName); PropertyTargetObject = Get(XamlIlContextDefinition.ProvideTargetObjectName); PropertyTargetProperty = Get(XamlIlContextDefinition.ProvideTargetPropertyName); PushParentMethod = GetMethod(XamlIlContextDefinition.PushParentMethodName); PopParentMethod = GetMethod(XamlIlContextDefinition.PopParentMethodName); Constructor = ContextType.Constructors.First(); Factory = il => factory(this, il); if (mappings.ContextFactoryCallback != null) { Factory = il => { factory(this, il); mappings.ContextFactoryCallback(this, il); } } ; }
static object LoadSreCore(string xaml, Assembly localAssembly, object rootInstance, Uri uri, bool isDesignMode) { InitializeSre(); var asm = localAssembly == null ? null : _sreTypeSystem.GetAssembly(localAssembly); var compiler = new AvaloniaXamlIlCompiler(new XamlIlTransformerConfiguration(_sreTypeSystem, asm, _sreMappings, _sreXmlns, AvaloniaXamlIlLanguage.CustomValueConverter), _sreContextType) { EnableIlVerification = true }; var tb = _sreBuilder.DefineType("Builder_" + Guid.NewGuid().ToString("N") + "_" + uri); IXamlIlType overrideType = null; if (rootInstance != null) { overrideType = _sreTypeSystem.GetType(rootInstance.GetType()); } compiler.IsDesignMode = isDesignMode; compiler.ParseAndCompile(xaml, uri?.ToString(), null, _sreTypeSystem.CreateTypeBuilder(tb), overrideType); var created = tb.CreateTypeInfo(); return(LoadOrPopulate(created, rootInstance)); }
public void Compile(XamlIlDocument doc, IXamlIlType contextType, IXamlIlMethodBuilder populateMethod, IXamlIlMethodBuilder buildMethod, IXamlIlTypeBuilder namespaceInfoBuilder, Func <string, IXamlIlType, IXamlIlTypeBuilder> createClosure, string baseUri, IFileSource fileSource) { var rootGrp = (XamlIlValueWithManipulationNode)doc.Root; var staticProviders = new List <IXamlIlField>(); if (namespaceInfoBuilder != null) { staticProviders.Add( XamlIlNamespaceInfoHelper.EmitNamespaceInfoProvider(_configuration, namespaceInfoBuilder, doc)); } var context = new XamlIlContext(contextType, rootGrp.Type.GetClrType(), _configuration.TypeMappings, baseUri, staticProviders); CompilePopulate(fileSource, rootGrp.Manipulation, createClosure, populateMethod.Generator, context); if (buildMethod != null) { CompileBuild(fileSource, rootGrp.Value, null, buildMethod.Generator, context, populateMethod); } namespaceInfoBuilder?.CreateType(); }
public bool IsAssignableFrom(IXamlIlType type) { if (_isAssignableFromCache.TryGetValue(type, out var cached)) { return(cached); } return(_isAssignableFromCache[type] = IsAssignableFromCore(type)); }
public IXamlIlField DefineField(IXamlIlType type, string name, bool isPublic, bool isStatic) { var f = _tb.DefineField(name, ((SreType)type).Type, (isPublic ? FieldAttributes.Public : FieldAttributes.Private) | (isStatic ? FieldAttributes.Static : default(FieldAttributes))); return(new SreField(_system, f)); }
public static bool IsDirectlyAssignableFrom(this IXamlIlType type, IXamlIlType other) { if (type.IsValueType || other.IsValueType) { return(type.Equals(other)); } return(type.IsAssignableFrom(other)); }
public XamlIlAstNewClrObjectNode(IXamlIlLineInfo lineInfo, IXamlIlType type, IXamlIlConstructor ctor, List <IXamlIlAstValueNode> arguments) : base(lineInfo) { Type = new XamlIlAstClrTypeReference(lineInfo, type); Constructor = ctor; Arguments = arguments; }
public AvaloniaPropertyCustomSetter(AvaloniaXamlIlWellKnownTypes types, IXamlIlType declaringType, IXamlIlField avaloniaProperty) { Types = types; AvaloniaProperty = avaloniaProperty; TargetType = declaringType; }
public IXamlIlTypeBuilder DefineSubType(IXamlIlType baseType, string name, bool isPublic) { var td = new TypeDefinition("", name, isPublic ? TypeAttributes.NestedPublic : TypeAttributes.NestedPrivate, GetReference(baseType)); Definition.NestedTypes.Add(td); return(new CecilTypeBuilder(TypeSystem, (CecilAssembly)Assembly, td)); }
public SreCustomAttribute(CustomAttributeData data, IXamlIlType type) { Type = type; _data = data; Parameters = data.ConstructorArguments.Select(p => p.Value).ToList(); Properties = data.NamedArguments?.ToDictionary(x => x.MemberName, x => x.TypedValue.Value) ?? new Dictionary <string, object>(); }
public IXamlIlLocal DefineLocal(IXamlIlType type) { var rv = _inner.DefineLocal(type); _locals[rv] = new LocalInfo { Number = _locals.Count, Type = type }; return(rv); }
public XamlIlConstantNode(IXamlIlLineInfo lineInfo, IXamlIlType type, object constant) : base(lineInfo) { if (!constant.GetType().IsPrimitive) { throw new ArgumentException($"Don't know how to emit {constant.GetType()} constant"); } Constant = constant; Type = new XamlIlAstClrTypeReference(lineInfo, type); }
public IXamlIlLocal DefineLocal(IXamlIlType type) { var r = Import(((ITypeReference)type).Reference); var def = new VariableDefinition(r); _body.Variables.Add(def); return(new CecilLocal { Variable = def }); }
public static bool IsNullable(this IXamlIlType type) { var def = type.GenericTypeDefinition; if (def == null) { return(false); } return(def.Namespace == "System" && def.Name == "Nullable`1"); }
public static IXamlIlMethod GetMethod(this IXamlIlType type, FindMethodMethodSignature signature) { var found = FindMethod(type, signature); if (found == null) { throw new XamlIlTypeSystemException($"Method with signature {signature} is not found on type {type.GetFqn()}"); } return(found); }