Skip to content

Commit

Permalink
[Rgen] Fix a bug in how the target flags are managed.
Browse files Browse the repository at this point in the history
We cannot use an  or a & we need to provide a list, that leater is
tested against a flag that has been either | or &.
  • Loading branch information
mandel-macaque committed Jan 26, 2025
1 parent a083d73 commit 010103d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,16 @@ public void Initialize (IncrementalGeneratorInitializationContext context)
}

static string [] GetFlagsForTarget (Dictionary<string, (string AttributeFullName, AttributeTargets Targets)> flags,
AttributeTargets targets)
=> flags.Where (kv => kv.Value.Targets.HasFlag (targets))
AttributeTargets[] targets)
=> flags.Where (kv => targets.Any (t => kv.Value.Targets.HasFlag (t)))
.Select (kv => kv.Key)
.ToArray ();

static (string AttributeFullName, string AttributeName, BindingAttributeData Data)[] GetAttributesForTarget (
static (string AttributeFullName, string AttributeName, BindingAttributeData Data) [] GetAttributesForTarget (
Dictionary<string, (string AttributeFullName, string AttributeName, BindingAttributeData Data)> dataAttribute,
AttributeTargets targets)
=> dataAttribute.Where (kv => kv.Value.Data.Target.HasFlag (targets))
AttributeTargets [] targets)
// return all the attributes that have at least one of the targets
=> dataAttribute.Where (kv => targets.Any (t => kv.Value.Data.Target.HasFlag (t)))
.Select (kv => kv.Value)
.ToArray ();

Expand Down Expand Up @@ -167,7 +168,7 @@ static void WriteDataModelExtension (TabbedStringBuilder sb, string dataModel, s
static void GenerateModelExtension (TabbedStringBuilder sb, string dataModel,
Dictionary<string, (string AttributeFullName, AttributeTargets Targets)> flags,
Dictionary<string, (string AttributeFullName, string AttributeName, BindingAttributeData Data)> attributes,
AttributeTargets targets,
AttributeTargets[] targets,
SourceProductionContext context)
{
var methodFlags = GetFlagsForTarget (flags, targets);
Expand Down Expand Up @@ -238,13 +239,13 @@ void GenerateCode (SourceProductionContext context, Compilation compilation,

#pragma warning disable format
// generate the extra methods for the data model, group the fields by the model type based on the target
var models = new (string Model, AttributeTargets Target) [] {
("EnumMember", AttributeTargets.Field),
("Parameter", AttributeTargets.Parameter),
("Property", AttributeTargets.Property),
("Method", AttributeTargets.Method),
("Binding", AttributeTargets.Interface),
("TypeInfo", AttributeTargets.Parameter)
var models = new (string Model, AttributeTargets[] Targets) [] {
("EnumMember", [AttributeTargets.Field]),
("Parameter", [AttributeTargets.Parameter]),
("Property", [AttributeTargets.Property]),
("Method", [AttributeTargets.Method]),
("Binding", [AttributeTargets.Interface, AttributeTargets.Class, AttributeTargets.Enum, AttributeTargets.Struct]),
("TypeInfo", [AttributeTargets.Parameter])
};
#pragma warning restore format

Expand Down
2 changes: 2 additions & 0 deletions src/rgen/Microsoft.Macios.Transformer/AttributesNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ static class AttributesNames {
/// </summary>
[BindingFlag (AttributeTargets.Class | AttributeTargets.Interface)]
public const string ModelAttribute = "Foundation.ModelAttribute";
[BindingAttribute(typeof(NativeData), AttributeTargets.Enum)]
public const string NativeAttribute = "ObjCRuntime.NativeAttribute";
[BindingAttribute(typeof(NativeData), AttributeTargets.Enum | AttributeTargets.Struct)]
public const string NativeNameAttribute = "ObjCRuntime.NativeNameAttribute";

/// <summary>
Expand Down

0 comments on commit 010103d

Please sign in to comment.