public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
        {
            if (node is XamlAstObjectNode binding && binding.Type.GetClrType().Equals(context.GetAvaloniaTypes().CompiledBindingExtension))
            {
                var convertedNode = ConvertLongFormPropertiesToBindingExpressionNode(context, binding);
                var foundPath     = false;

                if (binding.Arguments.Count > 0 && binding.Arguments[0] is XamlAstTextNode bindingPathText)
                {
                    var reader = new CharacterReader(bindingPathText.Text.AsSpan());
                    var(nodes, _) = BindingExpressionGrammar.Parse(ref reader);

                    if (convertedNode != null)
                    {
                        nodes.Insert(nodes.TakeWhile(x => x is BindingExpressionGrammar.ITransformNode).Count(), convertedNode);
                    }

                    if (nodes.Count == 1 && nodes[0] is BindingExpressionGrammar.EmptyExpressionNode)
                    {
                        binding.Arguments.RemoveAt(0);
                    }
                    else
                    {
                        binding.Arguments[0] = new ParsedBindingPathNode(bindingPathText, context.GetAvaloniaTypes().CompiledBindingPath, nodes);
                        foundPath            = true;
                    }
                }

                if (!foundPath)
                {
                    var bindingPathAssignment = binding.Children.OfType <XamlAstXamlPropertyValueNode>()
                                                .FirstOrDefault(v => v.Property.GetClrProperty().Name == "Path");

                    if (bindingPathAssignment != null && bindingPathAssignment.Values[0] is XamlAstTextNode pathValue)
                    {
                        var reader = new CharacterReader(pathValue.Text.AsSpan());
                        var(nodes, _) = BindingExpressionGrammar.Parse(ref reader);

                        if (nodes.Count == 1 && nodes[0] is BindingExpressionGrammar.EmptyExpressionNode)
                        {
                            bindingPathAssignment.Values.RemoveAt(0);
                        }
                        else
                        {
                            if (convertedNode != null)
                            {
                                nodes.Insert(nodes.TakeWhile(x => x is BindingExpressionGrammar.ITransformNode).Count(), convertedNode);
                            }

                            bindingPathAssignment.Values[0] = new ParsedBindingPathNode(pathValue, context.GetAvaloniaTypes().CompiledBindingPath, nodes);
                        }
                    }
                }
            }

            return(node);
        }
Exemple #2
0
        public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
        {
            if (node is AvaloniaXamlIlTargetTypeMetadataNode targetType)
            {
                return(targetType.Value);
            }

            return(node);
        }
Exemple #3
0
 public static WellKnownWinUITypes GetWinUITypes(this AstTransformationContext ctx)
 {
     if (ctx.TryGetItem <WellKnownWinUITypes>(out var rv))
     {
         return(rv);
     }
     ctx.SetItem(rv = new WellKnownWinUITypes(ctx.Configuration));
     return(rv);
 }
 public static AvaloniaXamlIlWellKnownTypes GetAvaloniaTypes(this AstTransformationContext ctx)
 {
     if (ctx.TryGetItem <AvaloniaXamlIlWellKnownTypes>(out var rv))
     {
         return(rv);
     }
     ctx.SetItem(rv = new AvaloniaXamlIlWellKnownTypes(ctx.Configuration));
     return(rv);
 }
Exemple #5
0
 public static RXamlWellKnownTypes GetRobustTypes(this AstTransformationContext ctx)
 {
     if (ctx.TryGetItem <RXamlWellKnownTypes>(out var rv))
     {
         return(rv);
     }
     ctx.SetItem(rv = new RXamlWellKnownTypes(ctx.Configuration));
     return(rv);
 }
            public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
            {
                if (node is XamlPropertyAssignmentNode pa)
                {
                    if (pa.Property.Name == "Name" &&
                        pa.Property.DeclaringType.FullName == "Robust.Client.UserInterface.Control")
                    {
                        if (context.ParentNodes().FirstOrDefault() is XamlManipulationGroupNode mg &&
                            mg.Children.OfType <RobustNameScopeRegistrationXamlIlNode>().Any())
                        {
                            return(node);
                        }

                        IXamlAstValueNode value = null;
                        for (var c = 0; c < pa.Values.Count; c++)
                        {
                            if (pa.Values[c].Type.GetClrType().Equals(context.Configuration.WellKnownTypes.String))
                            {
                                value = pa.Values[c];
                                if (!(value is XamlAstTextNode))
                                {
                                    var local = new XamlAstCompilerLocalNode(value);
                                    // Wrap original in local initialization
                                    pa.Values[c] = new XamlAstLocalInitializationNodeEmitter(value, value, local);
                                    // Use local
                                    value = local;
                                }

                                break;
                            }
                        }

                        if (value != null)
                        {
                            var objectType = context.ParentNodes().OfType <XamlAstConstructableObjectNode>().FirstOrDefault()?.Type.GetClrType();
                            return(new XamlManipulationGroupNode(pa)
                            {
                                Children =
                                {
                                    pa,
                                    new RobustNameScopeRegistrationXamlIlNode(value, objectType)
                                }
                            });
                        }
                    }

                    /*else if (pa.Property.CustomAttributes.Select(attr => attr.Type).Intersect(context.Configuration.TypeMappings.DeferredContentPropertyAttributes).Any())
                     * {
                     *  pa.Values[pa.Values.Count - 1] =
                     *      new NestedScopeMetadataNode(pa.Values[pa.Values.Count - 1]);
                     * }*/
                }

                return(node);
            }
Exemple #7
0
 public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
 {
     if (node is XamlAstObjectNode objectNode &&
         objectNode.Type is XamlAstXmlTypeReference typeReference &&
         (typeReference.Name == "DataTemplate" ||
          typeReference.Name == "ControlTemplate"))
     {
         objectNode.Children.Clear();
     }
     return(node);
 }
Exemple #8
0
        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 IXamlType UpdateCompiledBindingExtension(AstTransformationContext context, XamlAstConstructableObjectNode binding, Func <IXamlType> startTypeResolver, IXamlType selfType)
        {
            IXamlType bindingResultType = null;

            if (binding.Arguments.Count > 0 && binding.Arguments[0] is ParsedBindingPathNode bindingPath)
            {
                var transformed = TransformBindingPath(
                    context,
                    bindingPath,
                    startTypeResolver,
                    selfType,
                    bindingPath.Path);

                transformed = TransformForTargetTyping(transformed, context);

                bindingResultType    = transformed.BindingResultType;
                binding.Arguments[0] = transformed;
            }
            else
            {
                var bindingPathAssignment = binding.Children.OfType <XamlPropertyAssignmentNode>()
                                            .FirstOrDefault(v => v.Property.Name == "Path");

                if (bindingPathAssignment is null)
                {
                    return(startTypeResolver());
                }

                if (bindingPathAssignment.Values[0] is XamlIlBindingPathNode pathNode)
                {
                    bindingResultType = pathNode.BindingResultType;
                }
                else if (bindingPathAssignment.Values[0] is ParsedBindingPathNode bindingPathNode)
                {
                    var transformed = TransformBindingPath(
                        context,
                        bindingPathNode,
                        startTypeResolver,
                        selfType,
                        bindingPathNode.Path);

                    transformed = TransformForTargetTyping(transformed, context);

                    bindingResultType = transformed.BindingResultType;
                    bindingPathAssignment.Values[0] = transformed;
                }
                else
                {
                    throw new InvalidOperationException("Invalid state of Path property");
                }
            }

            return(bindingResultType);
        }
Exemple #10
0
        public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
        {
            if (node is XamlAstClrProperty prop && prop.Getter != null)
            {
                foreach (var adder in XamlTransformHelpers.FindPossibleAdders(context, prop.Getter.ReturnType))
                {
                    prop.Setters.Add(new AdderSetter(prop.Getter, adder));
                }
            }

            return(node);
        }
Exemple #11
0
        public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
        {
            if (node is XamlAstObjectNode ni)
            {
                XamlAstXamlPropertyValueNode propertyNode = null;

                for (var c = ni.Children.Count - 1; c >= 0; c--)
                {
                    var child = ni.Children[c];
                    if (child is IXamlAstValueNode valueNode)
                    {
                        if (propertyNode == null)
                        {
                            var contentProperty = context.Configuration.FindContentProperty(ni.Type.GetClrType());
                            if (contentProperty != null)
                            {
                                propertyNode = new XamlAstXamlPropertyValueNode(ni,
                                                                                new XamlAstClrProperty(ni, contentProperty, context.Configuration),
                                                                                Array.Empty <IXamlAstValueNode>());
                            }
                            else
                            {
                                var adders = XamlTransformHelpers.FindPossibleAdders(context, ni.Type.GetClrType());
                                if (adders.Count == 0)
                                {
                                    throw new XamlParseException(
                                              $"No Content property or any Add methods found for type {ni.Type.GetClrType().GetFqn()}",
                                              child);
                                }
                                propertyNode = new XamlAstXamlPropertyValueNode(ni, new XamlAstClrProperty(ni,
                                                                                                           "Content", ni.Type.GetClrType(), null,
                                                                                                           adders.Select(a => new XamlDirectCallPropertySetter(a)
                                {
                                    BinderParameters = { AllowMultiple = true }
                                })),
                                                                                Array.Empty <IXamlAstValueNode>());
                            }
                        }
                        // We are going in reverse order, so insert at the beginning
                        propertyNode.Values.Insert(0, valueNode);
                        ni.Children.RemoveAt(c);
                    }
                }

                if (propertyNode != null)
                {
                    ni.Children.Add(propertyNode);
                }
            }

            return(node);
        }
        public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
        {
            if (node is XamlAstXmlTypeReference xmlref)
            {
                var resolved = ResolveType(context, xmlref, context.StrictMode);
                if (resolved == null)
                {
                    return(node);
                }
                return(resolved);
            }

            return(node);
        }
 public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
 {
     if (!context.ParentNodes().Any() &&
         node is XamlValueWithManipulationNode mnode)
     {
         mnode.Manipulation = new XamlManipulationGroupNode(mnode,
                                                            new[]
         {
             mnode.Manipulation,
             new HandleRootObjectScopeNode(mnode)
         });
     }
     return(node);
 }
        public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
        {
            // By the time this transform runs, either all setters will be XamlDirect setters
            // or none will be.
            if (node is XamlPropertyAssignmentNode assign &&
                assign.PossibleSetters[0] is IXamlDirectSetter)
            {
                foreach (var setter in assign.PossibleSetters)
                {
                    IXamlDirectSetter directSetter = (IXamlDirectSetter)setter;

                    // Up to this point, the type of the setter parameter matches the actual property type
                    // so that the compiler correctly resolves the setter as a possible setter.
                    // For the emit phase, we need to change the setter type to match the actual type
                    // that the emitted code expects.
                    // Here we change the setter type from a DependencyObject-derived type to IXamlDirectObject
                    // so that the emitters know that the actual expected parameter type is IXamlDirectObject.
                    // Additionally, if we are setting a property that is type object with a DependencyObject-derived object,
                    // we need to use the IXamlDirectObject setter.
                    if (!(directSetter is XamlDirectEventSetter))
                    {
                        if (context.GetWinUITypes().DependencyObject.IsAssignableFrom(directSetter.Parameters[0]))
                        {
                            directSetter.ChangeEmitSetterType(context.GetWinUITypes().IXamlDirectObject);
                        }
                        else if (directSetter.Parameters[0] == context.Configuration.WellKnownTypes.Object)
                        {
                            if (context.GetWinUITypes().DependencyObject.IsAssignableFrom(assign.Values[0].Type.GetClrType()))
                            {
                                directSetter.ChangeEmitSetterType(context.GetWinUITypes().IXamlDirectObject);
                            }
                        }
                    }
                }

                for (int i = 0; i < assign.Values.Count; i++)
                {
                    if (assign.Values[i] is XamlObjectFromDirectObjectNode objFromDirect)
                    {
                        assign.Values[i] = objFromDirect.Value;
                    }
                    else if (assign.Values[i] is XamlValueWithManipulationNode || assign.PossibleSetters[0].Parameters[0] == context.GetWinUITypes().IXamlDirectObject)
                    {
                        assign.Values[i] = new XamlDirectObjectFromObjectNode(assign.Values[0], context.GetWinUITypes().IXamlDirectObject);
                    }
                }
            }
            return(node);
        }
Exemple #15
0
            public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
            {
                if (node is XamlAstObjectNode astNode)
                {
                    astNode.Children.RemoveAll(n =>
                                               n is XamlAstXmlDirective dir &&
                                               dir.Namespace == XamlNamespaces.Xaml2006 &&
                                               (dir.Name == "Class" ||
                                                dir.Name == "Precompile" ||
                                                dir.Name == "FieldModifier" ||
                                                dir.Name == "ClassModifier"));
                }

                return(node);
            }
Exemple #16
0
        public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
        {
            if (node is XamlAstConstructableObjectNode obj)
            {
                return(new XamlValueWithManipulationNode(obj,
                                                         new XamlAstNewClrObjectNode(obj, obj.Type.GetClrTypeReference(), obj.Constructor, obj.Arguments),
                                                         new XamlObjectInitializationNode(obj,
                                                                                          new XamlManipulationGroupNode(obj)
                {
                    Children = obj.Children.Cast <IXamlAstManipulationNode>().ToList()
                }, obj.Type.GetClrType())));
            }

            return(node);
        }
        public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
        {
            if (context.ParentNodes().FirstOrDefault() is AvaloniaXamlIlCompileBindingsNode)
            {
                return(node);
            }

            if (node is XamlAstObjectNode obj)
            {
                foreach (var item in obj.Children)
                {
                    if (item is XamlAstXmlDirective directive)
                    {
                        if (directive.Namespace == XamlNamespaces.Xaml2006 &&
                            directive.Name == "CompileBindings" &&
                            directive.Values.Count == 1)
                        {
                            if (!(directive.Values[0] is XamlAstTextNode text &&
                                  bool.TryParse(text.Text, out var compileBindings)))
                            {
                                throw new XamlParseException("The value of x:CompileBindings must be a literal boolean value.", directive.Values[0]);
                            }

                            obj.Children.Remove(directive);

                            return(new AvaloniaXamlIlCompileBindingsNode(obj, compileBindings));
                        }
                    }
                }
            }

            // Convert the <Binding> tag to either a CompiledBinding or ReflectionBinding tag.

            if (node is XamlAstXmlTypeReference tref &&
                tref.Name == "Binding" &&
                tref.XmlNamespace == "https://github.com/avaloniaui")
            {
                tref.IsMarkupExtension = true;

                var compileBindings = context.ParentNodes()
                                      .OfType <AvaloniaXamlIlCompileBindingsNode>()
                                      .FirstOrDefault()
                                      ?.CompileBindings ?? CompileBindingsByDefault;

                tref.Name = compileBindings ? "CompiledBinding" : "ReflectionBinding";
            }
            return(node);
        }
 public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
 {
     if (node is XamlAstClrProperty prop)
     {
         IXamlProperty dependencyPropertyProp = prop.DeclaringType.Properties
                                                .FirstOrDefault(p => p.Getter?.IsStatic == true &&
                                                                p.Name == prop.Name + "Property" &&
                                                                p.PropertyType == context.GetWinUITypes().DependencyProperty);
         if (dependencyPropertyProp is null)
         {
             return(node);
         }
         return(new DependencyProperty(prop, dependencyPropertyProp, context.GetWinUITypes()));
     }
     return(node);
 }
Exemple #19
0
        public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
        {
            if (node is XamlAstClrProperty prop)
            {
                var n     = prop.Name + "Property";
                var field =
                    prop.DeclaringType.Fields
                    .FirstOrDefault(f => f.Name == n);
                if (field != null)
                {
                    return(new XamlIlAvaloniaProperty(prop, field, context.GetAvaloniaTypes()));
                }
            }

            return(node);
        }
Exemple #20
0
        public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
        {
            if (!(node is XamlAstXamlPropertyValueNode propertyValueNode))
            {
                return(node);
            }

            if (!(propertyValueNode.Property is XamlAstClrProperty clrProperty))
            {
                return(node);
            }

            IEnumerable <IXamlCustomAttribute> attributes = propertyValueNode.Property.GetClrProperty().CustomAttributes;

            if (propertyValueNode.Property is XamlAstClrProperty referenceNode &&
                referenceNode.Getter != null)
            {
                attributes = attributes.Concat(referenceNode.Getter.CustomAttributes);
            }

            if (attributes.All(attribute => attribute.Type.FullName != "Avalonia.Controls.ResolveByNameAttribute"))
            {
                return(node);
            }

            if (propertyValueNode.Values.Count != 1 || !(propertyValueNode.Values.First() is XamlAstTextNode))
            {
                return(node);
            }

            var newNode = new XamlAstObjectNode(
                propertyValueNode.Values[0],
                new XamlAstClrTypeReference(propertyValueNode.Values[0],
                                            context.GetAvaloniaTypes().ResolveByNameExtension, true))
            {
                Arguments = new List <IXamlAstValueNode> {
                    propertyValueNode.Values[0]
                }
            };

            if (XamlTransformHelpers.TryConvertMarkupExtension(context, newNode, out var extensionNode))
            {
                propertyValueNode.Values[0] = extensionNode;
            }

            return(node);
        }
        /// <summary>
        /// Converts x:DataType directives to regular DataType assignments if property with Avalonia.Metadata.DataTypeAttribute exists.
        /// </summary>
        /// <returns></returns>
        public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
        {
            if (node is XamlAstObjectNode on)
            {
                for (var c = 0; c < on.Children.Count; c++)
                {
                    var ch = on.Children[c];
                    if (ch is XamlAstXmlDirective {
                        Namespace: XamlNamespaces.Xaml2006, Name: DataTypePropertyName
                    } d)
                    {
                        if (on.Children.OfType <XamlAstXamlPropertyValueNode>()
                            .Any(p => ((XamlAstNamePropertyReference)p.Property)?.Name == DataTypePropertyName))
                        {
                            // Break iteration if any DataType property was already set by user code.
                            break;
                        }

                        var templateDataTypeAttribute = context.GetAvaloniaTypes().DataTypeAttribute;

                        var clrType = (on.Type as XamlAstClrTypeReference)?.Type;
                        if (clrType is null)
                        {
                            break;
                        }

                        // Technically it's possible to map "x:DataType" to a property with [DataType] attribute regardless of its name,
                        // but we go explicitly strict here and check the name as well.
                        var(declaringType, dataTypeProperty) = GetAllProperties(clrType)
                                                               .FirstOrDefault(t => t.property.Name == DataTypePropertyName && t.property.CustomAttributes
                                                                               .Any(a => a.Type == templateDataTypeAttribute));

                        if (dataTypeProperty is not null)
                        {
                            on.Children[c] = new XamlAstXamlPropertyValueNode(d,
                                                                              new XamlAstNamePropertyReference(d,
                                                                                                               new XamlAstClrTypeReference(ch, declaringType, false), dataTypeProperty.Name,
                                                                                                               on.Type),
                                                                              d.Values);
                        }
                    }
                }
            }

            return(node);
        }
        public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
        {
            if (node is IXamlAstValueNode vn)
            {
                if (context.ParentNodes().FirstOrDefault() is XamlMarkupExtensionNode)
                {
                    return(node);
                }

                if (XamlTransformHelpers.TryConvertMarkupExtension(context, vn, out var rv))
                {
                    return(rv);
                }
            }

            return(node);
        }
        public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
        {
            if (!(node is XamlAstObjectNode on &&
                  on.Type.GetClrType().FullName == "Avalonia.Styling.Setter"))
            {
                return(node);
            }

            var targetTypeNode = context.ParentNodes()
                                 .OfType <AvaloniaXamlIlTargetTypeMetadataNode>()
                                 .FirstOrDefault(x => x.ScopeType == AvaloniaXamlIlTargetTypeMetadataNode.ScopeTypes.Style) ??
                                 throw new XamlParseException("Can not find parent Style Selector or ControlTemplate TargetType", node);

            IXamlType propType = null;
            var       property = @on.Children.OfType <XamlAstXamlPropertyValueNode>()
                                 .FirstOrDefault(x => x.Property.GetClrProperty().Name == "Property");

            if (property != null)
            {
                var propertyName = property.Values.OfType <XamlAstTextNode>().FirstOrDefault()?.Text;
                if (propertyName == null)
                {
                    throw new XamlParseException("Setter.Property must be a string", node);
                }


                var avaloniaPropertyNode = XamlIlAvaloniaPropertyHelper.CreateNode(context, propertyName,
                                                                                   new XamlAstClrTypeReference(targetTypeNode, targetTypeNode.TargetType.GetClrType(), false), property.Values[0]);
                property.Values = new List <IXamlAstValueNode> {
                    avaloniaPropertyNode
                };
                propType = avaloniaPropertyNode.AvaloniaPropertyType;
            }
            else
            {
                var propertyPath = on.Children.OfType <XamlAstXamlPropertyValueNode>()
                                   .FirstOrDefault(x => x.Property.GetClrProperty().Name == "PropertyPath");
                if (propertyPath == null)
                {
                    throw new XamlX.XamlParseException("Setter without a property or property path is not valid", node);
                }
                if (propertyPath.Values[0] is IXamlIlPropertyPathNode ppn &&
                    ppn.PropertyType != null)
                {
                    propType = ppn.PropertyType;
                }
        public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
        {
            if (node is XamlAstConstructableObjectNode binding && binding.Type.GetClrType().Equals(context.GetAvaloniaTypes().CompiledBindingExtension))
            {
                IXamlType startType;
                var       parentDataContextNode = context.ParentNodes().OfType <AvaloniaXamlIlDataContextTypeMetadataNode>().FirstOrDefault();
                if (parentDataContextNode is null)
                {
                    throw new XamlX.XamlParseException("Cannot parse a compiled binding without an explicit x:DataType directive to give a starting data type for bindings.", binding);
                }

                startType = parentDataContextNode.DataContextType;

                XamlIlBindingPathHelper.UpdateCompiledBindingExtension(context, binding, startType);
            }

            return(node);
        }
        public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
        {
            if (node is XamlAstObjectNode no)
            {
                foreach (var d in no.Children.OfType <XamlAstXmlDirective>().ToList())
                {
                    if (d.Namespace == XamlNamespaces.Xaml2006)
                    {
                        if (d.Name == "Precompile" || d.Name == "Class")
                        {
                            no.Children.Remove(d);
                        }
                    }
                }
            }

            return(node);
        }
Exemple #26
0
        public static IXamlIlAvaloniaPropertyNode CreateNode(AstTransformationContext context,
                                                             string propertyName, IXamlAstTypeReference selectorTypeReference, IXamlLineInfo lineInfo)
        {
            XamlAstNamePropertyReference forgedReference;

            var parser = new PropertyParser();

            var parsedPropertyName = parser.Parse(new CharacterReader(propertyName.AsSpan()));

            if (parsedPropertyName.owner == null)
            {
                forgedReference = new XamlAstNamePropertyReference(lineInfo, selectorTypeReference,
                                                                   propertyName, selectorTypeReference);
            }
            else
            {
                var xmlOwner = parsedPropertyName.ns;
                if (xmlOwner != null)
                {
                    xmlOwner += ":";
                }
                xmlOwner += parsedPropertyName.owner;

                var tref = TypeReferenceResolver.ResolveType(context, xmlOwner, false, lineInfo, true);

                var propertyFieldName = parsedPropertyName.name + "Property";
                var found             = tref.Type.GetAllFields()
                                        .FirstOrDefault(f => f.IsStatic && f.IsPublic && f.Name == propertyFieldName);
                if (found == null)
                {
                    throw new XamlX.XamlParseException(
                              $"Unable to find {propertyFieldName} field on type {tref.Type.GetFullName()}", lineInfo);
                }
                return(new XamlIlAvaloniaPropertyFieldNode(context.GetAvaloniaTypes(), lineInfo, found));
            }

            var clrProperty =
                ((XamlAstClrProperty) new PropertyReferenceResolver().Transform(context,
                                                                                forgedReference));

            return(new XamlIlAvaloniaPropertyNode(lineInfo,
                                                  context.Configuration.TypeSystem.GetType("Avalonia.AvaloniaProperty"),
                                                  clrProperty));
        }
        public static XamlAstClrTypeReference ResolveType(AstTransformationContext context,
                                                          string xmlName, bool isMarkupExtension, IXamlLineInfo lineInfo,
                                                          bool strict)
        {
            var pair = xmlName.Split(new[] { ':' }, 2);

            var(shortNs, name) = pair.Length == 1 ? ("", pair[0]) : (pair[0], pair[1]);
            if (!context.NamespaceAliases.TryGetValue(shortNs, out var xmlns))
            {
                if (strict)
                {
                    throw new XamlParseException(
                              $"Unable to resolve type namespace alias {shortNs}", lineInfo);
                }
                return(null);
            }

            return(ResolveType(context, xmlns, name, isMarkupExtension, new List <XamlAstXmlTypeReference>(), lineInfo, strict));
        }
Exemple #28
0
        public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
        {
            if (node is XamlAstXamlPropertyValueNode propertyNode)
            {
                var childNodes = propertyNode.Values;
                WhitespaceNormalization.Apply(
                    childNodes,
                    context.Configuration
                    );

                var property = propertyNode.Property.GetClrProperty();
                if (!WantsWhitespaceOnlyElements(context.Configuration, property))
                {
                    WhitespaceNormalization.RemoveWhitespaceNodes(childNodes);
                }
            }

            return(node);
        }
Exemple #29
0
        public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
        {
            if (node is XamlAstObjectNode on && on.Arguments.Count == 0)
            {
                var ctors = on.Type.GetClrType().Constructors;
                if (!ctors.Any(c => c.IsPublic && !c.IsStatic && c.Parameters.Count == 0))
                {
                    var sp = context.Configuration.TypeMappings.ServiceProvider;
                    if (ctors.Any(c =>
                                  c.IsPublic && !c.IsStatic && c.Parameters.Count == 1 && c.Parameters[0]
                                  .Equals(sp)))
                    {
                        on.Arguments.Add(new InjectServiceProviderNode(sp, on));
                    }
                }
            }

            return(node);
        }
Exemple #30
0
 public IXamlAstNode Transform(AstTransformationContext context, IXamlAstNode node)
 {
     if (node is XamlAstNamePropertyReference prop &&
         prop.TargetType is XamlAstClrTypeReference targetRef &&
         prop.DeclaringType is XamlAstClrTypeReference declaringRef)
     {
         var types = context.GetAvaloniaTypes();
         if (types.StyledElement.IsAssignableFrom(targetRef.Type) &&
             types.Classes.Equals(declaringRef.Type))
         {
             return(new XamlAstClrProperty(node, "class:" + prop.Name, types.Classes,
                                           null)
             {
                 Setters = { new ClassValueSetter(types, prop.Name), new ClassBindingSetter(types, prop.Name) }
             });
         }
     }
     return(node);
 }