forked from neo-project/neo-devpack-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpression.cs
283 lines (271 loc) · 12 KB
/
Expression.cs
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright (C) 2015-2023 The Neo Project.
//
// The Neo.Compiler.CSharp is free software distributed under the MIT
// software license, see the accompanying file LICENSE in the main directory
// of the project or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.
extern alias scfx;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Neo.SmartContract.Native;
using Neo.VM;
using System.Numerics;
using Neo.Cryptography.ECC;
using Neo.IO;
using Neo.Wallets;
namespace Neo.Compiler;
partial class MethodConvert
{
private void ConvertExpression(SemanticModel model, ExpressionSyntax syntax, SyntaxNode? syntaxNode = null)
{
using var sequence = InsertSequencePoint(syntax);
Optional<object?> constant = model.GetConstantValue(syntax);
if (constant.HasValue)
{
var value = constant.Value;
ITypeSymbol? typeSymbol = null;
if (syntaxNode is VariableDeclaratorSyntax variableDeclarator)
{
var declaration = variableDeclarator.Parent as VariableDeclarationSyntax;
if (declaration != null)
{
typeSymbol = ModelExtensions.GetTypeInfo(model, declaration.Type).Type;
}
}
else if (syntaxNode is PropertyDeclarationSyntax propertyDeclaration)
{
typeSymbol = ModelExtensions.GetTypeInfo(model, propertyDeclaration.Type).Type;
}
if (typeSymbol != null)
{
string fullName = typeSymbol.ToDisplayString();
switch (fullName)
{
case "Neo.SmartContract.Framework.UInt160":
var strValue = (string)value;
value = (UInt160.TryParse(strValue, out var hash)
? hash
: strValue.ToScriptHash(_context.Options.AddressVersion)).ToArray();
break;
case "Neo.SmartContract.Framework.UInt256":
strValue = (string)value;
value = strValue.HexToBytes(true);
if (((byte[])value).Length != 32)
throw new CompilationException(syntax, DiagnosticId.InvalidInitialValue, "Invalid UInt256 literal");
break;
case "Neo.SmartContract.Framework.ECPoint":
strValue = (string)value;
value = ECPoint.Parse(strValue, ECCurve.Secp256r1).EncodePoint(true);
break;
case "Neo.SmartContract.Framework.ByteArray":
strValue = (string)value;
value = strValue.HexToBytes(true);
break;
}
}
Push(value);
return;
}
switch (syntax)
{
//Convert an expression that creates an object of an anonymous type.
//Example: new { Amount = 108, Message = "Hello" };
case AnonymousObjectCreationExpressionSyntax expression:
ConvertAnonymousObjectCreationExpression(model, expression);
break;
//Convert an expression that creates an array.
//Example: new int[4] { 5, 6, 7, 8};
case ArrayCreationExpressionSyntax expression:
ConvertArrayCreationExpression(model, expression);
break;
//Convert an assignment expression.
//Example: new int[4] { 5, 6, 7, 8};
case AssignmentExpressionSyntax expression:
ConvertAssignmentExpression(model, expression);
break;
case BaseObjectCreationExpressionSyntax expression:
ConvertObjectCreationExpression(model, expression);
break;
case BinaryExpressionSyntax expression:
ConvertBinaryExpression(model, expression);
break;
//Converts a cast expression.
//Example: (ECPoint)ByteString.Empty;
case CastExpressionSyntax expression:
ConvertCastExpression(model, expression);
break;
//Converts a checked expression.
//Example: checked(temp * 2);
case CheckedExpressionSyntax expression:
ConvertCheckedExpression(model, expression);
break;
//Converts a null-conditional access expression.
//Example: people?.Name; array?[i];
case ConditionalAccessExpressionSyntax expression:
ConvertConditionalAccessExpression(model, expression);
break;
//Converts a null-conditional access expression.
//Example: var x = a > b ? a : b;
case ConditionalExpressionSyntax expression:
ConvertConditionalExpression(model, expression);
break;
//Converts an array element or indexer access ([]) expression.
//Example: array[i];
case ElementAccessExpressionSyntax expression:
ConvertElementAccessExpression(model, expression);
break;
// ????
case ElementBindingExpressionSyntax expression:
ConvertElementBindingExpression(model, expression);
break;
//Converts an identifier name expression.
//Example: int a = 1;
case IdentifierNameSyntax expression:
ConvertIdentifierNameExpression(model, expression);
break;
//Converts an implicit array creation expression.
//Example: var authorNames = new[] {"Shilpa", "Soniya", "Shivi", "Ritika"};
case ImplicitArrayCreationExpressionSyntax expression:
ConvertImplicitArrayCreationExpression(model, expression);
break;
//Converts an initialValue attribute
//Example: [InitialValue("NVg7LjGcUSrgxgjX3zEgqaksfMaiS8Z6e1"]
case InitializerExpressionSyntax expression:
ConvertInitializerExpression(model, expression);
break;
//Converts an interpolated string expression;
//Example: $"Hello, {name}"
case InterpolatedStringExpressionSyntax expression:
ConvertInterpolatedStringExpression(model, expression);
break;
//Converts Invocation, include method invocation, event invocation and delegate invocation
//Example: Runtime.Log("hello");
case InvocationExpressionSyntax expression:
ConvertInvocationExpression(model, expression);
break;
//Converts an 'is' pattern expression;
//Example: if (obj is string)
case IsPatternExpressionSyntax expression:
ConvertIsPatternExpression(model, expression);
break;
//Converts a member access expression.
//Example: Ledger.CurrentHash
case MemberAccessExpressionSyntax expression:
ConvertMemberAccessExpression(model, expression);
break;
// ????
case MemberBindingExpressionSyntax expression:
ConvertMemberBindingExpression(model, expression);
break;
//TODO
case ParenthesizedExpressionSyntax expression:
ConvertExpression(model, expression.Expression);
break;
//Converts postfix operator.
//Example: i++, i--
case PostfixUnaryExpressionSyntax expression:
ConvertPostfixUnaryExpression(model, expression);
break;
//Converts prefix operator.
//Example: ++i, --i, !flag
case PrefixUnaryExpressionSyntax expression:
ConvertPrefixUnaryExpression(model, expression);
break;
//Converts a switch expression.
//Example: day switch { 1 => "Monday", 2 => "Tuesday", 3 => "Wednesday", 4 => "Thursday", 5 => "Friday", 6 => "Saturday", 7 => "Sunday", _ => "Unknown" };
case SwitchExpressionSyntax expression:
ConvertSwitchExpression(model, expression);
break;
case BaseExpressionSyntax:
case ThisExpressionSyntax:
AddInstruction(OpCode.LDARG0);
break;
case ThrowExpressionSyntax expression:
Throw(model, expression.Expression);
break;
//Converts a tuple type expression.
//Example: (string, int) t1 = ("chris", 3);
case TupleExpressionSyntax expression:
ConvertTupleExpression(model, expression);
break;
default:
throw new CompilationException(syntax, DiagnosticId.SyntaxNotSupported, $"Unsupported syntax: {syntax}");
}
}
private void EnsureIntegerInRange(ITypeSymbol type)
{
if (type.Name == "BigInteger") return;
var (minValue, maxValue, mask) = type.Name switch
{
"SByte" => ((BigInteger)sbyte.MinValue, (BigInteger)sbyte.MaxValue, (BigInteger)0xff),
"Int16" => (short.MinValue, short.MaxValue, 0xffff),
"Int32" => (int.MinValue, int.MaxValue, 0xffffffff),
"Int64" => (long.MinValue, long.MaxValue, 0xffffffffffffffff),
"Byte" => (byte.MinValue, byte.MaxValue, 0xff),
"UInt16" => (ushort.MinValue, ushort.MaxValue, 0xffff),
"UInt32" => (uint.MinValue, uint.MaxValue, 0xffffffff),
"UInt64" => (ulong.MinValue, ulong.MaxValue, 0xffffffffffffffff),
_ => throw new CompilationException(DiagnosticId.SyntaxNotSupported, $"Unsupported type: {type}")
};
JumpTarget checkUpperBoundTarget = new(), adjustTarget = new(), endTarget = new();
AddInstruction(OpCode.DUP);
Push(minValue);
Jump(OpCode.JMPGE_L, checkUpperBoundTarget);
if (_checkedStack.Peek())
AddInstruction(OpCode.THROW);
else
Jump(OpCode.JMP_L, adjustTarget);
checkUpperBoundTarget.Instruction = AddInstruction(OpCode.DUP);
Push(maxValue);
Jump(OpCode.JMPLE_L, endTarget);
if (_checkedStack.Peek())
{
AddInstruction(OpCode.THROW);
}
else
{
adjustTarget.Instruction = Push(mask);
AddInstruction(OpCode.AND);
if (minValue < 0)
{
AddInstruction(OpCode.DUP);
Push(maxValue);
Jump(OpCode.JMPLE_L, endTarget);
Push(mask + 1);
AddInstruction(OpCode.SUB);
}
}
endTarget.Instruction = AddInstruction(OpCode.NOP);
}
private void ConvertObjectToString(SemanticModel model, ExpressionSyntax expression)
{
ITypeSymbol? type = ModelExtensions.GetTypeInfo(model, expression).Type;
switch (type?.ToString())
{
case "sbyte":
case "byte":
case "short":
case "ushort":
case "int":
case "uint":
case "long":
case "ulong":
case "System.Numerics.BigInteger":
ConvertExpression(model, expression);
Call(NativeContract.StdLib.Hash, "itoa", 1, true);
break;
case "string":
case "Neo.SmartContract.Framework.ECPoint":
case "Neo.SmartContract.Framework.ByteString":
case "Neo.SmartContract.Framework.UInt160":
case "Neo.SmartContract.Framework.UInt256":
ConvertExpression(model, expression);
break;
default:
throw new CompilationException(expression, DiagnosticId.InvalidToStringType, $"Unsupported interpolation: {expression}");
}
}
}