Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,6 @@ public IEnumerable<Instruction> ConvertFromString(string value, ILContext contex
yield return Instruction.Create(OpCodes.Ldsfld, bpRef);
}

static bool IsOfAnyType(XmlType xmlType, params string[] types)
{
if (types == null || types.Length == 0)
return false;
if (xmlType == null)
return false;
if (xmlType.NamespaceUri != XamlParser.MauiUri && xmlType.NamespaceUri != XamlParser.MauiGlobalUri)
return false;
if (types.Contains(xmlType.Name))
return true;
return false;
}

public FieldReference GetBindablePropertyFieldReference(string value, ILContext context, ModuleDefinition module, BaseNode node)
{
FieldReference bpRef = null;
Expand All @@ -48,18 +35,18 @@ public FieldReference GetBindablePropertyFieldReference(string value, ILContext
if (parts.Length == 1)
{
var parent = node.Parent?.Parent as IElementNode ?? (node.Parent?.Parent as IListNode)?.Parent as IElementNode;
if (IsOfAnyType((node.Parent as ElementNode)?.XmlType, nameof(Setter), nameof(PropertyCondition)))
if ((node.Parent as ElementNode)?.XmlType is XmlType xt && xt.IsOfAnyType(nameof(Setter), nameof(PropertyCondition)))
{
if (IsOfAnyType(parent.XmlType, nameof(Trigger), nameof(DataTrigger), nameof(MultiTrigger), nameof(Style)))
if (parent.XmlType.IsOfAnyType(nameof(Trigger), nameof(DataTrigger), nameof(MultiTrigger), nameof(Style)))
{
typeName = GetTargetTypeName(parent);
}
else if (IsOfAnyType(parent.XmlType, nameof(VisualState)))
else if (parent.XmlType.IsOfAnyType(nameof(VisualState)))
{
typeName = FindTypeNameForVisualState(parent, node, context);
}
}
else if (IsOfAnyType((node.Parent as ElementNode)?.XmlType, nameof(Trigger)))
else if ((node.Parent as ElementNode)?.XmlType is XmlType xt1 && xt1.IsOfAnyType(nameof(Trigger)))
{
typeName = GetTargetTypeName(node.Parent);
}
Expand Down Expand Up @@ -99,18 +86,18 @@ static XmlType FindTypeNameForVisualState(IElementNode parent, IXmlLineInfo line

//2. check that the VS is in a VSG
// if (!(parent.Parent is IElementNode target) || target.XmlType.NamespaceUri != XamlParser.MauiUri || target.XmlType.Name != nameof(VisualStateGroup))
if (!(parent.Parent is IElementNode target) || !IsOfAnyType(target.XmlType, nameof(VisualStateGroup)))
if (!(parent.Parent is IElementNode target) || !target.XmlType.IsOfAnyType(nameof(VisualStateGroup)))
throw new XamlParseException($"Expected {nameof(VisualStateGroup)} but found {parent.Parent}", lineInfo);

//3. if the VSG is in a VSGL, skip that as it could be implicit
if (target.Parent is ListNode
|| IsOfAnyType((target.Parent as IElementNode)?.XmlType, nameof(VisualStateGroupList)))
if ( target.Parent is ListNode
|| target.Parent is IElementNode { XmlType: XmlType xt } && xt.IsOfAnyType(nameof(VisualStateGroupList)))
target = target.Parent.Parent as IElementNode;
else
target = target.Parent as IElementNode;

//4. target is now a Setter in a Style, or a VE
if (IsOfAnyType(target.XmlType, nameof(Setter)))
if (target.XmlType.IsOfAnyType(nameof(Setter)))
{
var targetType = ((target?.Parent as IElementNode)?.Properties[new XmlName("", "TargetType")] as ValueNode)?.Value as string;
return TypeArgumentsParser.ParseSingle(targetType, parent.NamespaceResolver, lineInfo);
Expand All @@ -121,8 +108,7 @@ static XmlType FindTypeNameForVisualState(IElementNode parent, IXmlLineInfo line

public static FieldReference GetBindablePropertyFieldReference(XamlCache cache, TypeReference typeRef, string propertyName, ModuleDefinition module)
{
TypeReference declaringTypeReference;
FieldReference bpRef = typeRef.GetField(cache, fd => fd.Name == $"{propertyName}Property" && fd.IsStatic && fd.IsPublic, out declaringTypeReference);
FieldReference bpRef = typeRef.GetField(cache, fd => fd.Name == $"{propertyName}Property" && fd.IsStatic && fd.IsPublic, out TypeReference declaringTypeReference);
if (bpRef != null)
{
bpRef = module.ImportReference(bpRef.ResolveGenericParameters(declaringTypeReference));
Expand Down
2 changes: 2 additions & 0 deletions src/Controls/src/SourceGen/Controls.SourceGen.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<Compile Include="..\Xaml\MarkupExpressionParser.cs" Link="MarkupExpressionParser.cs" />
<Compile Include="..\Xaml\PruneIgnoredNodesVisitor.cs" Link="PruneIgnoredNodesVisitor.cs" />
<Compile Include="..\Xaml\RemoveDuplicateDesignNodes.cs" Link="RemoveDuplicateDesignNodes.cs" />
<Compile Include="..\Xaml\SimplifyOnPlatformVisitor.cs" Link="SimplifyOnPlatformVisitor.cs" />
<Compile Include="..\Xaml\SimplifyTypeExtensionVisitor.cs" Link="SimplifyTypeExtensionVisitor.cs" />
<Compile Include="..\Xaml\TypeArgumentsParser.cs" Link="TypeArgumentsParser.cs" />
<Compile Include="..\Xaml\XamlInflator.cs" Link="XamlInflator.cs" />
Expand All @@ -42,6 +43,7 @@
<Compile Include="..\Xaml\XamlParser.cs" Link="XamlParser.Namespaces.cs" />
<Compile Include="..\Xaml\XamlParser.Namespaces.cs" Link="XamlParser.Namespaces.cs" />
<Compile Include="..\Xaml\XmlName.cs" Link="XmlName.cs" />
<Compile Include="..\Xaml\XmlType.cs" Link="XmlType.cs" />
<Compile Include="..\Xaml\XmlnsHelper.cs" Link="XmlnsHelper.cs" />
<Compile Include="..\Xaml\XmlTypeXamlExtensions.cs" Link="XmlTypeXamlExtensions.cs" />
<Compile Include="..\..\..\Core\src\Services\Crc64.cs">
Expand Down
3 changes: 3 additions & 0 deletions src/Controls/src/SourceGen/InitializeComponentCodeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ PrePost newblock() =>
FilePath = xamlItem.ProjectItem.RelativePath,
EnableLineInfo = xamlItem.ProjectItem.EnableLineInfo,
EnableDiagnostics = xamlItem.ProjectItem.EnableDiagnostics,
TargetFramework = xamlItem.ProjectItem.TargetFramework ?? "",
};
using (newblock())
{
Expand Down Expand Up @@ -123,6 +124,8 @@ static void Visit(RootNode rootnode, SourceGenContext visitorContext, bool useDe
if (useDesignProperties)
rootnode.Accept(new RemoveDuplicateDesignNodes(), null);
rootnode.Accept(new SimplifyTypeExtensionVisitor(), null);
if (!string.IsNullOrEmpty(visitorContext.TargetFramework))
rootnode.Accept(new SimplifyOnPlatformVisitor(visitorContext.TargetFramework), null);
rootnode.Accept(new CreateValuesVisitor(visitorContext), null);
rootnode.Accept(new SetNamescopesAndRegisterNamesVisitor(visitorContext), null); //set namescopes for {x:Reference} and FindByName
rootnode.Accept(new SetFieldsForXNamesVisitor(visitorContext), null);
Expand Down
33 changes: 9 additions & 24 deletions src/Controls/src/SourceGen/NodeSGExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -551,19 +551,6 @@ public static void RegisterSourceInfo(this INode node, SourceGenContext context,
writer.Indent--;
}

static bool IsOfAnyType(XmlType xmlType, params string[] types)
{
if (types == null || types.Length == 0)
return false;
if (xmlType == null)
return false;
if (xmlType.NamespaceUri != XamlParser.MauiUri && xmlType.NamespaceUri != XamlParser.MauiGlobalUri)
return false;
if (types.Contains(xmlType.Name))
return true;
return false;
}

public static IFieldSymbol GetBindableProperty(this ValueNode node, SourceGenContext context)
{
static ITypeSymbol? GetTargetTypeSymbol(INode node, SourceGenContext context)
Expand All @@ -584,14 +571,14 @@ public static IFieldSymbol GetBindableProperty(this ValueNode node, SourceGenCon
{
ITypeSymbol? typeSymbol = null;
var parent = node.Parent?.Parent as IElementNode ?? (node.Parent?.Parent as IListNode)?.Parent as IElementNode;
if (IsOfAnyType((node.Parent as ElementNode)?.XmlType!, "Setter", "PropertyCondition"))
if ((node.Parent as ElementNode)!.XmlType!.IsOfAnyType( "Setter", "PropertyCondition"))
{
if (IsOfAnyType(parent!.XmlType, "Trigger", "DataTrigger", "MultiTrigger", "Style"))
if (parent!.XmlType.IsOfAnyType("Trigger", "DataTrigger", "MultiTrigger", "Style"))
typeSymbol = GetTargetTypeSymbol(parent, context);
else if (IsOfAnyType(parent.XmlType, "VisualState"))
else if (parent.XmlType.IsOfAnyType("VisualState"))
typeSymbol = FindTypeSymbolForVisualState(parent, context, node);
}
else if (IsOfAnyType((node.Parent as ElementNode)?.XmlType!, "Trigger"))
else if ((node.Parent as ElementNode)!.XmlType!.IsOfAnyType("Trigger"))
typeSymbol = GetTargetTypeSymbol(node.Parent!, context);

var propertyName = parts[0];
Expand All @@ -615,19 +602,19 @@ public static IFieldSymbol GetBindableProperty(this ValueNode node, SourceGenCon
//1. parent is VisualState, don't check that

//2. check that the VS is in a VSG
if (!(parent.Parent is IElementNode target) || !IsOfAnyType(target.XmlType, "VisualStateGroup"))
if (!(parent.Parent is IElementNode target) || !target.XmlType.IsOfAnyType("VisualStateGroup"))
throw new Exception($"Expected VisualStateGroup but found {parent.Parent}");

//3. if the VSG is in a VSGL, skip that as it could be implicit
if (target.Parent is ListNode
|| IsOfAnyType((target.Parent as IElementNode)?.XmlType!, "VisualStateGroupList"))
if ( target.Parent is ListNode
|| (target.Parent as IElementNode)!.XmlType!.IsOfAnyType( "VisualStateGroupList"))
target = (IElementNode)target.Parent.Parent;
else
target = (IElementNode)target.Parent;

XmlType? typeName = null;
//4. target is now a Setter in a Style, or a VE
if (IsOfAnyType(target.XmlType, "Setter"))
if (target.XmlType.IsOfAnyType("Setter"))
{
var targetType = ((target?.Parent as IElementNode)?.Properties[new XmlName("", "TargetType")] as ValueNode)?.Value as string;
typeName = TypeArgumentsParser.ParseSingle(targetType, parent.NamespaceResolver, lineInfo);
Expand All @@ -639,7 +626,5 @@ public static IFieldSymbol GetBindableProperty(this ValueNode node, SourceGenCon
}

public static bool RepresentsType(this INode node, string namespaceUri, string name)
{
return node is IElementNode elementNode && elementNode.XmlType.RepresentsType(namespaceUri, name);
}
=> node is IElementNode elementNode && elementNode.XmlType.RepresentsType(namespaceUri, name);
}
1 change: 1 addition & 0 deletions src/Controls/src/SourceGen/SourceGenContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class SourceGenContext(IndentedTextWriter writer, Compilation compilation, Sourc
public IList<string> LocalMethods { get; } = new List<string>();
public bool EnableLineInfo { get; set; }
public bool EnableDiagnostics { get; internal set; }
public string TargetFramework { get; internal set; } = "";

public void AddLocalMethod(string code)
{
Expand Down
Loading
Loading