Skip to content

Commit 9bb1691

Browse files
committed
feat: introduce OperationBodySyntaxDescriptor and related construction plan for operation body syntax metadata handling
1 parent 2ca4125 commit 9bb1691

2 files changed

Lines changed: 110 additions & 0 deletions

File tree

src/MLIR.Generators/Emitters/AssemblyFormatEmitter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ internal static class AssemblyFormatEmitter
99
public static void Emit(StringBuilder builder, OperationModel operation, OperationBodySyntaxMetadata bodySyntaxMetadata)
1010
{
1111
var className = DialectGeneratorNaming.GetOperationClassName(operation);
12+
var syntaxDescriptor = OperationBodySyntaxDescriptor.Describe(bodySyntaxMetadata);
1213
builder.AppendLine("public sealed class " + className + "AssemblyFormat : IOperationAssemblyFormat");
1314
builder.AppendLine("{");
1415
builder.AppendLine(" public bool TryParse(SyntaxToken nameToken, IReadOnlyList<SyntaxToken> resultTokens, IReadOnlyList<SyntaxToken> resultCommaTokens, SyntaxToken? equalsToken, OperationParsingContext context, out OperationBodySyntax? body)");
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
namespace MLIR.Generators.Emitters;
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using MLIR.ODS.Model;
6+
7+
internal sealed class OperationBodySyntaxConstructionPlan
8+
{
9+
public OperationBodySyntaxConstructionPlan(
10+
IReadOnlyList<string> regionFields,
11+
IReadOnlyDictionary<string, string> attributeFields,
12+
string? attrDictField,
13+
string? attrDictWithKeywordField,
14+
string? propDictField,
15+
IReadOnlyDictionary<string, string> operandFields,
16+
IReadOnlyDictionary<string, string> resultFields,
17+
string? typeField,
18+
string? successorsField,
19+
string? operandsField)
20+
{
21+
RegionFields = regionFields;
22+
AttributeFields = attributeFields;
23+
AttrDictField = attrDictField;
24+
AttrDictWithKeywordField = attrDictWithKeywordField;
25+
PropDictField = propDictField;
26+
OperandFields = operandFields;
27+
ResultFields = resultFields;
28+
TypeField = typeField;
29+
SuccessorsField = successorsField;
30+
OperandsField = operandsField;
31+
}
32+
33+
public IReadOnlyList<string> RegionFields { get; }
34+
public IReadOnlyDictionary<string, string> AttributeFields { get; }
35+
public string? AttrDictField { get; }
36+
public string? AttrDictWithKeywordField { get; }
37+
public string? PropDictField { get; }
38+
public IReadOnlyDictionary<string, string> OperandFields { get; }
39+
public IReadOnlyDictionary<string, string> ResultFields { get; }
40+
public string? TypeField { get; }
41+
public string? SuccessorsField { get; }
42+
public string? OperandsField { get; }
43+
}
44+
45+
internal static class OperationBodySyntaxDescriptor
46+
{
47+
public static OperationBodySyntaxConstructionPlan Describe(OperationBodySyntaxMetadata metadata)
48+
{
49+
var regionFields = new List<string>();
50+
var attributeFields = new Dictionary<string, string>(StringComparer.Ordinal);
51+
var operandFields = new Dictionary<string, string>(StringComparer.Ordinal);
52+
var resultFields = new Dictionary<string, string>(StringComparer.Ordinal);
53+
string? attrDictField = null;
54+
string? attrDictWithKeywordField = null;
55+
string? propDictField = null;
56+
string? successorsField = null;
57+
string? operandsField = null;
58+
string? typeField = null;
59+
60+
foreach (var component in metadata.ComponentFields)
61+
{
62+
switch (component.Kind)
63+
{
64+
case BodyComponentKind.Regions:
65+
regionFields.Add(component.FieldName);
66+
break;
67+
case BodyComponentKind.Attribute:
68+
attributeFields[component.ComponentName] = component.FieldName;
69+
break;
70+
case BodyComponentKind.Operand:
71+
operandFields[component.ComponentName] = component.FieldName;
72+
break;
73+
case BodyComponentKind.Result:
74+
resultFields[component.ComponentName] = component.FieldName;
75+
break;
76+
case BodyComponentKind.AttrDict:
77+
attrDictField ??= component.FieldName;
78+
break;
79+
case BodyComponentKind.AttrDictWithKeyword:
80+
attrDictWithKeywordField ??= component.FieldName;
81+
break;
82+
case BodyComponentKind.PropDict:
83+
propDictField ??= component.FieldName;
84+
break;
85+
case BodyComponentKind.Successors:
86+
successorsField ??= component.FieldName;
87+
break;
88+
case BodyComponentKind.Operands:
89+
operandsField ??= component.FieldName;
90+
break;
91+
case BodyComponentKind.Type:
92+
typeField ??= component.FieldName;
93+
break;
94+
}
95+
}
96+
97+
return new OperationBodySyntaxConstructionPlan(
98+
regionFields,
99+
attributeFields,
100+
attrDictField,
101+
attrDictWithKeywordField,
102+
propDictField,
103+
operandFields,
104+
resultFields,
105+
typeField,
106+
successorsField,
107+
operandsField);
108+
}
109+
}

0 commit comments

Comments
 (0)