Skip to content

Commit 7b3c819

Browse files
committed
Merge branch 'net10.0' into fix-31167-II
2 parents ac91bbc + 2522ae4 commit 7b3c819

File tree

62 files changed

+2196
-610
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+2196
-610
lines changed

eng/automation/LocProject.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@
9797
"SourceFile": "src\\Controls\\src\\SourceGen\\MauiGResources.resx",
9898
"LclFile": "loc\\{Lang}\\src\\Controls\\src\\SourceGen\\MauiGResources.resx.lcl",
9999
"CopyOption": "LangIDOnName",
100-
"OutputPath": "src\\Controls\\src\\SourceGen\\"
100+
"OutputPath": "src\\Controls\\src\\SourceGen\\xlf\\"
101101
}
102102
],
103103
"LssFiles": [],
104104
"CloneLanguageSet": ""
105105
}
106106
]
107-
}
107+
}

src/Controls/src/Build.Tasks/CompiledConverters/BindablePropertyConverter.cs

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,6 @@ public IEnumerable<Instruction> ConvertFromString(string value, ILContext contex
2525
yield return Instruction.Create(OpCodes.Ldsfld, bpRef);
2626
}
2727

28-
static bool IsOfAnyType(XmlType xmlType, params string[] types)
29-
{
30-
if (types == null || types.Length == 0)
31-
return false;
32-
if (xmlType == null)
33-
return false;
34-
if (xmlType.NamespaceUri != XamlParser.MauiUri && xmlType.NamespaceUri != XamlParser.MauiGlobalUri)
35-
return false;
36-
if (types.Contains(xmlType.Name))
37-
return true;
38-
return false;
39-
}
40-
4128
public FieldReference GetBindablePropertyFieldReference(string value, ILContext context, ModuleDefinition module, BaseNode node)
4229
{
4330
FieldReference bpRef = null;
@@ -48,18 +35,18 @@ public FieldReference GetBindablePropertyFieldReference(string value, ILContext
4835
if (parts.Length == 1)
4936
{
5037
var parent = node.Parent?.Parent as IElementNode ?? (node.Parent?.Parent as IListNode)?.Parent as IElementNode;
51-
if (IsOfAnyType((node.Parent as ElementNode)?.XmlType, nameof(Setter), nameof(PropertyCondition)))
38+
if ((node.Parent as ElementNode)?.XmlType is XmlType xt && xt.IsOfAnyType(nameof(Setter), nameof(PropertyCondition)))
5239
{
53-
if (IsOfAnyType(parent.XmlType, nameof(Trigger), nameof(DataTrigger), nameof(MultiTrigger), nameof(Style)))
40+
if (parent.XmlType.IsOfAnyType(nameof(Trigger), nameof(DataTrigger), nameof(MultiTrigger), nameof(Style)))
5441
{
5542
typeName = GetTargetTypeName(parent);
5643
}
57-
else if (IsOfAnyType(parent.XmlType, nameof(VisualState)))
44+
else if (parent.XmlType.IsOfAnyType(nameof(VisualState)))
5845
{
5946
typeName = FindTypeNameForVisualState(parent, node, context);
6047
}
6148
}
62-
else if (IsOfAnyType((node.Parent as ElementNode)?.XmlType, nameof(Trigger)))
49+
else if ((node.Parent as ElementNode)?.XmlType is XmlType xt1 && xt1.IsOfAnyType(nameof(Trigger)))
6350
{
6451
typeName = GetTargetTypeName(node.Parent);
6552
}
@@ -99,18 +86,18 @@ static XmlType FindTypeNameForVisualState(IElementNode parent, IXmlLineInfo line
9986

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

10592
//3. if the VSG is in a VSGL, skip that as it could be implicit
106-
if (target.Parent is ListNode
107-
|| IsOfAnyType((target.Parent as IElementNode)?.XmlType, nameof(VisualStateGroupList)))
93+
if ( target.Parent is ListNode
94+
|| target.Parent is IElementNode { XmlType: XmlType xt } && xt.IsOfAnyType(nameof(VisualStateGroupList)))
10895
target = target.Parent.Parent as IElementNode;
10996
else
11097
target = target.Parent as IElementNode;
11198

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

122109
public static FieldReference GetBindablePropertyFieldReference(XamlCache cache, TypeReference typeRef, string propertyName, ModuleDefinition module)
123110
{
124-
TypeReference declaringTypeReference;
125-
FieldReference bpRef = typeRef.GetField(cache, fd => fd.Name == $"{propertyName}Property" && fd.IsStatic && fd.IsPublic, out declaringTypeReference);
111+
FieldReference bpRef = typeRef.GetField(cache, fd => fd.Name == $"{propertyName}Property" && fd.IsStatic && fd.IsPublic, out TypeReference declaringTypeReference);
126112
if (bpRef != null)
127113
{
128114
bpRef = module.ImportReference(bpRef.ResolveGenericParameters(declaringTypeReference));

src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ public void Visit(ElementNode node, INode parentNode)
165165
if (adderRef.ReturnType.FullName != "System.Void")
166166
Context.IL.Emit(Pop);
167167
}
168-
169168
else
170169
throw new BuildException(BuildExceptionCode.ContentPropertyAttributeMissing, node, null, ((IElementNode)parentNode).XmlType.Name);
171170
}

src/Controls/src/Core/Page/Page.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ public Task DisplayAlert(string title, string message, string cancel, FlowDirect
350350
/// <inheritdoc cref="DisplayAlertAsync(string, string, string, string, FlowDirection)"/>
351351
[Obsolete("Use DisplayAlertAsync instead")]
352352
public Task<bool> DisplayAlert(string title, string message, string accept, string cancel, FlowDirection flowDirection)
353-
=> DisplayAlertAsync(title, message, accept, null, flowDirection);
353+
=> DisplayAlertAsync(title, message, accept, cancel, flowDirection);
354354

355355
/// <inheritdoc cref="DisplayAlertAsync(string, string, string, string, FlowDirection)"/>
356356
public Task DisplayAlertAsync(string title, string message, string cancel)

src/Controls/src/Core/ResourceDictionary.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using System.Linq;
1111
using System.Reflection;
1212
using System.Runtime.CompilerServices;
13-
13+
using System.Runtime.ExceptionServices;
1414
using Microsoft.Maui.Controls.Internals;
1515

1616
namespace Microsoft.Maui.Controls
@@ -46,7 +46,19 @@ public Uri Source
4646
public void SetAndCreateSource<T>(Uri value)
4747
where T : ResourceDictionary, new()
4848
{
49-
var instance = s_instances.GetValue(typeof(T), static _ => new T());
49+
var instance = s_instances.GetValue(typeof(T), static _ =>
50+
{
51+
try
52+
{
53+
return new T();
54+
}
55+
catch (TargetInvocationException tie) when (tie.InnerException is not null)
56+
{
57+
ExceptionDispatchInfo.Capture(tie.InnerException).Throw();
58+
throw;
59+
}
60+
});
61+
5062
SetSource(value, instance);
5163
}
5264

@@ -66,7 +78,18 @@ public void SetAndLoadSource(Uri value, string resourcePath, Assembly assembly,
6678

6779
internal static ResourceDictionary GetOrCreateInstance([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] Type type)
6880
{
69-
return s_instances.GetValue(type, _ => (ResourceDictionary)Activator.CreateInstance(type));
81+
return s_instances.GetValue(type, _ =>
82+
{
83+
try
84+
{
85+
return (ResourceDictionary)Activator.CreateInstance(type);
86+
}
87+
catch (TargetInvocationException tie) when (tie.InnerException is not null)
88+
{
89+
ExceptionDispatchInfo.Capture(tie.InnerException).Throw();
90+
throw;
91+
}
92+
});
7093
}
7194

7295
internal void SetSource(Uri source, ResourceDictionary sourceInstance)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using Microsoft.CodeAnalysis.Diagnostics;
3+
4+
namespace Microsoft.Maui.Controls.SourceGen;
5+
6+
public static class AnalyzerConfigOptionsExtensions
7+
{
8+
public static bool IsEnabled(this AnalyzerConfigOptions options, string key)
9+
=> options.TryGetValue(key, out var value) && string.Compare(value, "enable", StringComparison.OrdinalIgnoreCase) == 0;
10+
11+
public static bool IsDisabled(this AnalyzerConfigOptions options, string key)
12+
=> options.TryGetValue(key, out var value) && string.Compare(value, "disable", StringComparison.OrdinalIgnoreCase) == 0;
13+
14+
public static bool IsTrue(this AnalyzerConfigOptions options, string key)
15+
=> options.TryGetValue(key, out var value) && string.Compare(value, "true", StringComparison.OrdinalIgnoreCase) == 0;
16+
17+
public static bool IsFalse(this AnalyzerConfigOptions options, string key)
18+
=> options.TryGetValue(key, out var value) && string.Compare(value, "false", StringComparison.OrdinalIgnoreCase) == 0;
19+
20+
public static string GetValueOrDefault(this AnalyzerConfigOptions options, string key, string defaultValue)
21+
=> options.TryGetValue(key, out var value) ? value : defaultValue;
22+
23+
public static string? GetValueOrNull(this AnalyzerConfigOptions options, string key)
24+
=> options.TryGetValue(key, out var value) ? value : null;
25+
}

src/Controls/src/SourceGen/CompiledBindingMarkup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public CompiledBindingMarkup(IElementNode node, string path, LocalVariable bindi
2626
}
2727

2828
private Location GetLocation(INode node)
29-
=> LocationHelpers.LocationCreate(_context.FilePath!, (IXmlLineInfo)node, "x:DataType");
29+
=> LocationHelpers.LocationCreate(_context.ProjectItem.RelativePath!, (IXmlLineInfo)node, "x:DataType");
3030

3131
public bool TryCompileBinding(ITypeSymbol sourceType, bool isTemplateBinding, out string? newBindingExpression)
3232
{

src/Controls/src/SourceGen/Controls.SourceGen.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<Compile Include="..\Xaml\MarkupExpressionParser.cs" Link="MarkupExpressionParser.cs" />
3434
<Compile Include="..\Xaml\PruneIgnoredNodesVisitor.cs" Link="PruneIgnoredNodesVisitor.cs" />
3535
<Compile Include="..\Xaml\RemoveDuplicateDesignNodes.cs" Link="RemoveDuplicateDesignNodes.cs" />
36+
<Compile Include="..\Xaml\SimplifyOnPlatformVisitor.cs" Link="SimplifyOnPlatformVisitor.cs" />
3637
<Compile Include="..\Xaml\SimplifyTypeExtensionVisitor.cs" Link="SimplifyTypeExtensionVisitor.cs" />
3738
<Compile Include="..\Xaml\TypeArgumentsParser.cs" Link="TypeArgumentsParser.cs" />
3839
<Compile Include="..\Xaml\XamlInflator.cs" Link="XamlInflator.cs" />
@@ -42,6 +43,7 @@
4243
<Compile Include="..\Xaml\XamlParser.cs" Link="XamlParser.Namespaces.cs" />
4344
<Compile Include="..\Xaml\XamlParser.Namespaces.cs" Link="XamlParser.Namespaces.cs" />
4445
<Compile Include="..\Xaml\XmlName.cs" Link="XmlName.cs" />
46+
<Compile Include="..\Xaml\XmlType.cs" Link="XmlType.cs" />
4547
<Compile Include="..\Xaml\XmlnsHelper.cs" Link="XmlnsHelper.cs" />
4648
<Compile Include="..\Xaml\XmlTypeXamlExtensions.cs" Link="XmlTypeXamlExtensions.cs" />
4749
<Compile Include="..\..\..\Core\src\Services\Crc64.cs">

src/Controls/src/SourceGen/GeneratorHelpers.cs

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -36,71 +36,16 @@ public static string EscapeIdentifier(string identifier)
3636
: $"@{identifier}";
3737
}
3838

39-
public static ProjectItem? ComputeProjectItem((AdditionalText, AnalyzerConfigOptionsProvider) tuple, CancellationToken cancellationToken)
39+
public static ProjectItem? ComputeProjectItem((AdditionalText additionalText, AnalyzerConfigOptionsProvider optionsProvider) tuple, CancellationToken cancellationToken)
4040
{
41-
var (additionalText, optionsProvider) = tuple;
42-
var fileOptions = optionsProvider.GetOptions(additionalText);
43-
if (!fileOptions.TryGetValue("build_metadata.additionalfiles.GenKind", out string? kind) || kind is null)
41+
if (cancellationToken.IsCancellationRequested)
4442
return null;
4543

46-
fileOptions.TryGetValue("build_metadata.additionalfiles.TargetPath", out var targetPath);
47-
fileOptions.TryGetValue("build_metadata.additionalfiles.ManifestResourceName", out var manifestResourceName);
48-
fileOptions.TryGetValue("build_metadata.additionalfiles.RelativePath", out var relativePath);
49-
fileOptions.TryGetValue("build_property.targetframework", out var targetFramework);
50-
fileOptions.TryGetValue("build_property.Configuration", out var configuration);
51-
52-
bool enableDiagnostics = false;
53-
if (fileOptions.TryGetValue("build_property.EnableMauiXamlDiagnostics", out var enDiag) && string.Compare(enDiag, "true", StringComparison.OrdinalIgnoreCase) == 0)
54-
enableDiagnostics = true;
55-
if (fileOptions.TryGetValue("build_property.additionalfiles.EnableDiagnostics", out enDiag) && string.Compare(enDiag, "true", StringComparison.OrdinalIgnoreCase) == 0)
56-
enableDiagnostics = true;
57-
if (fileOptions.TryGetValue("build_property.additionalfiles.EnableDiagnostics", out enDiag) && string.Compare(enDiag, "false", StringComparison.OrdinalIgnoreCase) == 0)
58-
enableDiagnostics = false;
59-
60-
var xamlinflator = 0;
61-
if (fileOptions.TryGetValue("build_metadata.additionalfiles.Inflator", out var inflator) && !string.IsNullOrEmpty(inflator))
62-
{
63-
var parts = inflator!.Split(',');
64-
for (int i = 0; i < parts.Length; i++)
65-
{
66-
var trimmed = parts[i].Trim();
67-
if (!Enum.TryParse<XamlInflator>(trimmed, true, out var xinfl))
68-
throw new InvalidOperationException($"Invalid inflator '{trimmed}' for {additionalText.Path}.");
69-
xamlinflator |= (int)xinfl;
70-
}
71-
}
72-
73-
var enableLineInfo = true;
74-
if (fileOptions.TryGetValue("build_property.MauiXamlLineInfo", out var lineInfo) && string.Compare(lineInfo, "disable", StringComparison.OrdinalIgnoreCase) == 0)
75-
enableLineInfo = false;
76-
if (fileOptions.TryGetValue("build_metadata.additionalfiles.LineInfo", out lineInfo) && string.Compare(lineInfo, "enable", StringComparison.OrdinalIgnoreCase) == 0)
77-
enableLineInfo = true;
78-
if (fileOptions.TryGetValue("build_metadata.additionalfiles.LineInfo", out lineInfo) && string.Compare(lineInfo, "disable", StringComparison.OrdinalIgnoreCase) == 0)
79-
enableLineInfo = false;
80-
81-
string noWarn = "";
82-
if (fileOptions.TryGetValue("build_property.MauiXamlNoWarn", out var noWarnValue))
83-
noWarn = noWarnValue;
84-
if (fileOptions.TryGetValue("build_metadata.additionalfiles.NoWarn", out noWarnValue))
85-
noWarn = noWarnValue;
86-
87-
return new ProjectItem
88-
{
89-
AdditionalText = additionalText,
90-
TargetPath = targetPath,
91-
RelativePath = relativePath,
92-
ManifestResourceName = manifestResourceName,
93-
Kind = kind,
94-
Inflator = (XamlInflator)xamlinflator,
95-
EnableLineInfo = enableLineInfo,
96-
EnableDiagnostics = enableDiagnostics,
97-
NoWarn = noWarn,
98-
TargetFramework = targetFramework,
99-
Configuration = configuration!,
100-
};
44+
var projectItem = new ProjectItem(tuple.additionalText, tuple.optionsProvider.GetOptions(tuple.additionalText));
45+
return projectItem.Kind == "None" ? null : projectItem;
10146
}
10247

103-
public static XamlProjectItemForIC? ComputeXamlProjectItemForIC((ProjectItem?, AssemblyCaches) itemAdnCaches, CancellationToken cancellationToken)
48+
public static XamlProjectItemForIC? ComputeXamlProjectItemForIC((ProjectItem?, AssemblyCaches) itemAdnCaches, CancellationToken cancellationToken)
10449
{
10550
var (projectItem, assemblyCaches) = itemAdnCaches;
10651
var text = projectItem?.AdditionalText.GetText(cancellationToken);

src/Controls/src/SourceGen/InitializeComponentCodeWriter.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,7 @@ PrePost newblock() =>
8686
var methodName = genSwitch ? "InitializeComponentSourceGen" : "InitializeComponent";
8787
codeWriter.WriteLine($"private partial void {methodName}()");
8888
xamlItem.Root!.XmlType.TryResolveTypeSymbol(null, compilation, xmlnsCache, out var baseType);
89-
var sgcontext = new SourceGenContext(codeWriter, compilation, sourceProductionContext, xmlnsCache, typeCache, rootType!, baseType)
90-
{
91-
FilePath = xamlItem.ProjectItem.RelativePath,
92-
EnableLineInfo = xamlItem.ProjectItem.EnableLineInfo,
93-
EnableDiagnostics = xamlItem.ProjectItem.EnableDiagnostics,
94-
};
89+
var sgcontext = new SourceGenContext(codeWriter, compilation, sourceProductionContext, xmlnsCache, typeCache, rootType!, baseType, xamlItem.ProjectItem);
9590
using (newblock())
9691
{
9792
Visit(root, sgcontext);
@@ -123,6 +118,8 @@ static void Visit(RootNode rootnode, SourceGenContext visitorContext, bool useDe
123118
if (useDesignProperties)
124119
rootnode.Accept(new RemoveDuplicateDesignNodes(), null);
125120
rootnode.Accept(new SimplifyTypeExtensionVisitor(), null);
121+
if (!string.IsNullOrEmpty(visitorContext.ProjectItem.TargetFramework))
122+
rootnode.Accept(new SimplifyOnPlatformVisitor(visitorContext.ProjectItem.TargetFramework), null);
126123
rootnode.Accept(new CreateValuesVisitor(visitorContext), null);
127124
rootnode.Accept(new SetNamescopesAndRegisterNamesVisitor(visitorContext), null); //set namescopes for {x:Reference} and FindByName
128125
rootnode.Accept(new SetFieldsForXNamesVisitor(visitorContext), null);

0 commit comments

Comments
 (0)