public XamlIlDirectCallPropertySetter(IXamlIlMethod method, IXamlIlType type)
 {
     _method    = method;
     _type      = type;
     Parameters = new[] { type };
     TargetType = method.ThisOrFirstParameter();
 }
Пример #2
0
 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];
 }
Пример #3
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);
        }
Пример #4
0
        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);
 }
Пример #6
0
        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);
                    }
                }
            }
        }
Пример #7
0
            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);
            }
Пример #8
0
        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);
        })
        {
        }
Пример #9
0
            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");
            }
Пример #10
0
 public XamlIlLoadMethodDelegateNode(IXamlIlLineInfo lineInfo, IXamlIlAstValueNode value,
                                     IXamlIlType delegateType, IXamlIlMethod method) : base(lineInfo, value)
 {
     DelegateType = delegateType;
     Method       = method;
     Type         = new XamlIlAstClrTypeReference(value, DelegateType);
 }
Пример #11
0
        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));
        }
Пример #12
0
        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);
        }
Пример #13
0
 public XamlIlTypeExtensionNode(IXamlIlLineInfo lineInfo, IXamlIlAstTypeReference value,
                                IXamlIlType systemType) : base(lineInfo)
 {
     _systemType = systemType;
     Type        = new XamlIlAstClrTypeReference(this, systemType);
     Value       = value;
 }
Пример #14
0
 public XamlIlObjectInitializationNode(IXamlIlLineInfo lineInfo,
                                       IXamlIlAstManipulationNode manipulation, IXamlIlType type)
     : base(lineInfo)
 {
     Manipulation = manipulation;
     Type         = type;
 }
Пример #15
0
            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);
            }
Пример #16
0
        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);
                }
            }
            ;
        }
Пример #17
0
        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));
        }
Пример #18
0
        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();
        }
Пример #19
0
 public bool IsAssignableFrom(IXamlIlType type)
 {
     if (_isAssignableFromCache.TryGetValue(type, out var cached))
     {
         return(cached);
     }
     return(_isAssignableFromCache[type] = IsAssignableFromCore(type));
 }
Пример #20
0
            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));
            }
Пример #21
0
 public static bool IsDirectlyAssignableFrom(this IXamlIlType type, IXamlIlType other)
 {
     if (type.IsValueType || other.IsValueType)
     {
         return(type.Equals(other));
     }
     return(type.IsAssignableFrom(other));
 }
Пример #22
0
 public XamlIlAstNewClrObjectNode(IXamlIlLineInfo lineInfo,
                                  IXamlIlType type, IXamlIlConstructor ctor,
                                  List <IXamlIlAstValueNode> arguments) : base(lineInfo)
 {
     Type        = new XamlIlAstClrTypeReference(lineInfo, type);
     Constructor = ctor;
     Arguments   = arguments;
 }
Пример #23
0
 public AvaloniaPropertyCustomSetter(AvaloniaXamlIlWellKnownTypes types,
                                     IXamlIlType declaringType,
                                     IXamlIlField avaloniaProperty)
 {
     Types            = types;
     AvaloniaProperty = avaloniaProperty;
     TargetType       = declaringType;
 }
Пример #24
0
            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));
            }
Пример #25
0
 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>();
 }
Пример #26
0
        public IXamlIlLocal DefineLocal(IXamlIlType type)
        {
            var rv = _inner.DefineLocal(type);

            _locals[rv] = new LocalInfo {
                Number = _locals.Count, Type = type
            };
            return(rv);
        }
Пример #27
0
 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);
 }
Пример #28
0
            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
                });
            }
Пример #29
0
        public static bool IsNullable(this IXamlIlType type)
        {
            var def = type.GenericTypeDefinition;

            if (def == null)
            {
                return(false);
            }
            return(def.Namespace == "System" && def.Name == "Nullable`1");
        }
Пример #30
0
        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);
        }