forked from SharpGenTools/SharpGenTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldCodeGenerator.cs
More file actions
171 lines (156 loc) · 7.01 KB
/
Copy pathFieldCodeGenerator.cs
File metadata and controls
171 lines (156 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System.Collections.Generic;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using SharpGen.Model;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace SharpGen.Generator;
internal sealed partial class FieldCodeGenerator : MemberMultiCodeGeneratorBase<CsField>
{
private readonly bool explicitLayout;
public FieldCodeGenerator(Ioc ioc, bool explicitLayout) : base(ioc)
{
this.explicitLayout = explicitLayout;
}
public override IEnumerable<MemberDeclarationSyntax> GenerateCode(CsField csElement)
{
if (csElement.IsBoolToInt && !csElement.IsArray)
{
yield return GenerateBackingField(csElement, csElement.MarshalType);
yield return GenerateProperty(
csElement, PredefinedType(Token(SyntaxKind.BoolKeyword)),
GeneratorHelpers.GenerateIntToBoolConversion,
(_, value) => GeneratorHelpers.CastExpression(
ParseTypeName(csElement.MarshalType.QualifiedName),
GeneratorHelpers.GenerateBoolToIntConversion(value)
)
);
}
else if (csElement.IsArray && !csElement.IsString)
{
var elementType = ParseTypeName(csElement.PublicType.QualifiedName);
yield return GenerateBackingField(csElement, csElement.PublicType, isArray: true);
yield return GenerateProperty(
csElement, ArrayType(elementType, SingletonList(ArrayRankSpecifier())),
value => AssignmentExpression(
SyntaxKind.CoalesceAssignmentExpression,
value,
ObjectCreationExpression(
ArrayType(
elementType,
SingletonList(
ArrayRankSpecifier(
SingletonSeparatedList<ExpressionSyntax>(
LiteralExpression(
SyntaxKind.NumericLiteralExpression,
Literal(csElement.ArrayDimensionValue)
)
)
)
)
)
)
),
null
);
}
else if (csElement.IsBitField)
{
PropertyValueGetTransform getterTransform;
PropertyValueSetTransform setterTransform;
TypeSyntax propertyType;
if (csElement.IsBoolBitField)
{
getterTransform = GeneratorHelpers.GenerateIntToBoolConversion;
setterTransform = (_, value) => GeneratorHelpers.GenerateBoolToIntConversion(value);
propertyType = PredefinedType(Token(SyntaxKind.BoolKeyword));
}
else
{
getterTransform = valueExpression => GeneratorHelpers.CastExpression(
ParseTypeName(csElement.PublicType.QualifiedName),
valueExpression
);
setterTransform = null;
propertyType = ParseTypeName(csElement.PublicType.QualifiedName);
}
yield return GenerateBackingField(csElement, csElement.PublicType);
var bitMask = LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(csElement.BitMask));
var bitOffset = LiteralExpression(
SyntaxKind.NumericLiteralExpression, Literal(csElement.BitOffset)
);
yield return GenerateProperty(
csElement, propertyType,
Compose(
getterTransform,
value => BinaryExpression(
SyntaxKind.BitwiseAndExpression,
GeneratorHelpers.WrapInParentheses(
BinaryExpression(SyntaxKind.RightShiftExpression, value, bitOffset)
),
bitMask
)
),
Compose(
(oldValue, value) => GeneratorHelpers.CastExpression(
ParseTypeName(csElement.PublicType.QualifiedName),
BinaryExpression(
SyntaxKind.BitwiseOrExpression,
GeneratorHelpers.WrapInParentheses(
BinaryExpression(
SyntaxKind.BitwiseAndExpression,
oldValue,
PrefixUnaryExpression(
SyntaxKind.BitwiseNotExpression,
GeneratorHelpers.WrapInParentheses(
BinaryExpression(SyntaxKind.LeftShiftExpression, bitMask, bitOffset)
)
)
)
),
GeneratorHelpers.WrapInParentheses(
BinaryExpression(
SyntaxKind.LeftShiftExpression,
GeneratorHelpers.WrapInParentheses(
BinaryExpression(SyntaxKind.BitwiseAndExpression, value, bitMask)
),
bitOffset
)
)
)
),
setterTransform
)
);
}
else
{
yield return GenerateBackingField(
csElement, csElement.PublicType, propertyBacking: false, document: true
);
}
}
private MemberDeclarationSyntax GenerateBackingField(CsField field, CsTypeBase backingType,
bool isArray = false, bool propertyBacking = true,
bool document = false)
{
var elementType = ParseTypeName(backingType.QualifiedName);
var fieldDecl = FieldDeclaration(
VariableDeclaration(
isArray
? ArrayType(elementType, SingletonList(ArrayRankSpecifier()))
: elementType,
SingletonSeparatedList(
VariableDeclarator(propertyBacking ? field.IntermediateMarshalName : field.Name)
)
)
)
.WithModifiers(
propertyBacking
? TokenList(Token(SyntaxKind.InternalKeyword))
: field.VisibilityTokenList
);
if (explicitLayout)
fieldDecl = AddFieldOffsetAttribute(fieldDecl, field.Offset);
return document ? AddDocumentationTrivia(fieldDecl, field) : fieldDecl;
}
}