Skip to content

Commit 2346d20

Browse files
chenzhitongcschuchardt88
authored andcommitted
comment for Expression (neo-project#980)
1 parent a78dd28 commit 2346d20

34 files changed

+820
-4
lines changed

src/Neo.Compiler.CSharp/MethodConvert/Expression/AnonymousObjectCreationExpression.cs

+15
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ namespace Neo.Compiler;
1818

1919
partial class MethodConvert
2020
{
21+
/// <summary>
22+
/// Methods for converting the creation of an object of an anonymous type into a series of instructions.
23+
/// </summary>
24+
/// <param name="model">The semantic model providing context and information about the anonymous object creation.</param>
25+
/// <param name="expression">The syntax representation of the anonymous object creation statement being converted.</param>
26+
/// <remarks>
27+
/// Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first.
28+
/// The type name is generated by the compiler and is not available at the source code level.
29+
/// The type of each property is inferred by the compiler.
30+
/// </remarks>
31+
/// <example>
32+
/// The following example shows an anonymous type that is initialized with two properties named Amount and Message.
33+
/// <c>var v = new { Amount = 108, Message = "Hello" };</c>
34+
/// </example>
35+
/// <seealso href="https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/anonymous-types">Anonymous types</seealso>
2136
private void ConvertAnonymousObjectCreationExpression(SemanticModel model, AnonymousObjectCreationExpressionSyntax expression)
2237
{
2338
AddInstruction(OpCode.NEWARRAY0);

src/Neo.Compiler.CSharp/MethodConvert/Expression/ArrayCreationExpression.cs

+19
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,25 @@ namespace Neo.Compiler;
1919

2020
partial class MethodConvert
2121
{
22+
/// <summary>
23+
/// Converts the code for constructing arrays and initializing arrays into OpCodes.
24+
/// This method includes analyzing the array length, array type, array dimension and initial data.
25+
/// </summary>
26+
/// <param name="model">The semantic model providing context and information about the array creation.</param>
27+
/// <param name="expression">The syntax representation of the array creation statement being converted.</param>
28+
/// <exception cref="CompilationException">Only one-dimensional arrays are supported, otherwise an exception is thrown.</exception>
29+
/// <remarks>
30+
/// When the array is initialized to null, this code converts it to "array length" + OpCode.NEWBUFFER (only for byte[]) or OpCode.NEWARRAY_T.
31+
/// When the array is not initialized to null, this code converts the initialized constants one by one in reverse order, then adds the "array length" and OpCode.PACK
32+
/// </remarks>
33+
/// <example>
34+
/// Example of a array creation syntax:
35+
/// <c>var array = new byte[4];</c>
36+
/// The compilation result of the example code is: OpCode.PUSH4, OpCode.NEWBUFFER
37+
/// <c>var array = new int[4] { 5, 6, 7, 8};</c>
38+
/// The compilation result of the example code is: OpCode.PUSH8, OpCode.PUSH7, OpCode.PUSH6, OpCode.PUSH5, OpCode.PUSH4, OpCode.PACK
39+
/// </example>
40+
/// <seealso href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/arrays">Arrays</seealso>
2241
private void ConvertArrayCreationExpression(SemanticModel model, ArrayCreationExpressionSyntax expression)
2342
{
2443
ArrayRankSpecifierSyntax specifier = expression.Type.RankSpecifiers[0];

src/Neo.Compiler.CSharp/MethodConvert/Expression/AssignmentExpression.CoalesceAssignment.cs

+26
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,32 @@ namespace Neo.Compiler;
2121

2222
partial class MethodConvert
2323
{
24+
/// <summary>
25+
/// Converts the code for null-coalescing assignment expression into OpCodes.
26+
/// The null-coalescing assignment operator ??= assigns the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null.
27+
/// The ??= operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null.
28+
/// Null-coalescing assignment expressions are a new feature introduced in C# 8.0(Released September, 2019).
29+
/// </summary>
30+
/// <param name="model">The semantic model providing context and information about coalesce assignment expression.</param>
31+
/// <param name="expression">The syntax representation of the coalesce assignment expression statement being converted.</param>
32+
/// <exception cref="CompilationException">Thrown when the syntax is not supported.</exception>
33+
/// <example>
34+
/// <code>
35+
/// public class Cat
36+
/// {
37+
/// public string Name { get; set; }
38+
/// }
39+
/// </code>
40+
/// <code>
41+
/// Cat nullableCat = null;
42+
/// Cat nonNullableCat = new() { Name = "Mimi" };
43+
/// nullableCat ??= nonNullableCat;
44+
/// Runtime.Log("Nullable cat: " + nullableCat.Name);
45+
/// </code>
46+
/// <c>nullableCat ??= nonNullableCat;</c> this line is evaluated as
47+
/// <c>nullableCat = nullableCat ?? nonNullableCat;</c> is evaluated as <c>if (nullableCat == null) nullableCat = nonNullableCat;</c>
48+
/// </example>
49+
/// <seealso href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator">?? and ??= operators - the null-coalescing operators</seealso>
2450
private void ConvertCoalesceAssignmentExpression(SemanticModel model, AssignmentExpressionSyntax expression)
2551
{
2652
switch (expression.Left)

src/Neo.Compiler.CSharp/MethodConvert/Expression/AssignmentExpression.ComplexAssignment.cs

+28
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,34 @@ namespace Neo.Compiler;
2121

2222
partial class MethodConvert
2323
{
24+
/// <summary>
25+
/// Converts the code for complex assignment (or compound assignment) expression into OpCodes.
26+
/// </summary>
27+
/// <param name="model">The semantic model providing context and information about complex assignment expression.</param>
28+
/// <param name="expression">The syntax representation of the complex assignment expression statement being converted.</param>
29+
/// <exception cref="CompilationException">Thrown when the syntax is not supported.</exception>
30+
/// <remarks>
31+
/// For a binary operator op, a compound assignment expression of the form "x op= y" is equivalent to "x = x op y" except that x is only evaluated once.
32+
/// </remarks>
33+
/// <example>
34+
/// The following example demonstrates the usage of compound assignment with arithmetic operators:
35+
/// The corresponding code branch is "ConvertComplexAssignmentExpression"
36+
/// <code>
37+
/// int a = 5;
38+
/// a += 9;
39+
/// Runtime.Log(a.ToString());
40+
/// a -= 4;
41+
/// Runtime.Log(a.ToString());
42+
/// a *= 2;
43+
/// Runtime.Log(a.ToString());
44+
/// a /= 4;
45+
/// Runtime.Log(a.ToString());
46+
/// a %= 3;
47+
/// Runtime.Log(a.ToString());
48+
/// </code>
49+
/// output: 14, 10, 20, 5, 2
50+
/// </example>
51+
/// <seealso href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/assignment-operator#compound-assignment">Compound assignment</seealso>
2452
private void ConvertComplexAssignmentExpression(SemanticModel model, AssignmentExpressionSyntax expression)
2553
{
2654
ITypeSymbol type = model.GetTypeInfo(expression).Type!;

src/Neo.Compiler.CSharp/MethodConvert/Expression/AssignmentExpression.SimpleAssignment.cs

+17-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,23 @@ namespace Neo.Compiler;
2121

2222
partial class MethodConvert
2323
{
24-
24+
/// <summary>
25+
/// Converts the code for simple assignment expression into OpCodes.
26+
/// The assignment operator = assigns the value of its right-hand operand to a variable,
27+
/// a property, or an indexer element given by its left-hand operand.
28+
/// </summary>
29+
/// <param name="model">The semantic model providing context and information about simple assignment expression.</param>
30+
/// <param name="expression">The syntax representation of the simple assignment expression statement being converted.</param>
31+
/// <exception cref="CompilationException">Thrown when the syntax is not supported.</exception>
32+
/// <remarks>
33+
/// The result of an assignment expression is the value assigned to the left-hand operand.
34+
/// The type of the right-hand operand must be the same as the type of the left-hand operand or implicitly convertible to it.
35+
/// </remarks>
36+
/// <example>
37+
/// The assignment operator = is right-associative, that is, an expression of the form
38+
/// <c>a = b = c</c> is evaluated as <c>a = (b = c)</c>
39+
/// </example>
40+
/// <seealso href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/assignment-operator">Assignment operators</seealso>
2541
private void ConvertSimpleAssignmentExpression(SemanticModel model, AssignmentExpressionSyntax expression)
2642
{
2743
ConvertExpression(model, expression.Right);

src/Neo.Compiler.CSharp/MethodConvert/Expression/AssignmentExpression.cs

+25
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,31 @@ namespace Neo.Compiler;
1717

1818
partial class MethodConvert
1919
{
20+
/// <summary>
21+
/// Converts the code for assignment expression into OpCodes.
22+
/// Include assignment operator (=), null-coalescing assignment operator (??=) and compound assignment(+=, -=, *=, /= ... )
23+
/// </summary>
24+
/// <param name="model">The semantic model providing context and information about assignment expression.</param>
25+
/// <param name="expression">The syntax representation of the assignment expression statement being converted.</param>
26+
/// <example>
27+
/// The following code covers three branches. If you want to see the example code for only one of the branches,
28+
/// you can look at the comments of the corresponding method.
29+
/// <code>
30+
/// public class Cat
31+
/// {
32+
/// public string Name { get; set; }
33+
/// }
34+
/// </code>
35+
/// <code>
36+
/// Cat nullableCat = null;
37+
/// Cat nonNullableCat = new() { Name = "Mimi" };
38+
/// nullableCat ??= nonNullableCat;
39+
/// var logInfo = "Nullable cat: ";
40+
/// logInfo += nullableCat.Name;
41+
/// Runtime.Log(log);
42+
/// </code>
43+
/// </example>
44+
/// <seealso cref="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/assignment-operator"/>
2045
private void ConvertAssignmentExpression(SemanticModel model, AssignmentExpressionSyntax expression)
2146
{
2247
switch (expression.OperatorToken.ValueText)

src/Neo.Compiler.CSharp/MethodConvert/Expression/BinaryExpression.cs

+26
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,32 @@ namespace Neo.Compiler;
1919

2020
partial class MethodConvert
2121
{
22+
/// <summary>
23+
/// The conditional logical OR operator ||, also known as the "short-circuiting" logical OR operator, computes the logical OR of its operands.
24+
/// The result of x || y is true if either x or y evaluates to true.
25+
/// Otherwise, the result is false. If x evaluates to true, y isn't evaluated.
26+
///
27+
/// The conditional logical AND operator &&, also known as the "short-circuiting" logical AND operator, computes the logical AND of its operands.
28+
/// The result of x && y is true if both x and y evaluate to true.
29+
/// Otherwise, the result is false. If x evaluates to false, y isn't evaluated.
30+
///
31+
/// The is operator checks if the run-time type of an expression result is compatible with a given type. The is operator also tests an expression result against a pattern.
32+
///
33+
/// The as operator explicitly converts the result of an expression to a given reference or nullable value type. If the conversion isn't possible, the as operator returns null. Unlike a cast expression, the as operator never throws an exception.
34+
///
35+
/// The null-coalescing operator ?? returns the value of its left-hand operand if it isn't null;
36+
/// otherwise, it evaluates the right-hand operand and returns its result.
37+
/// The ?? operator doesn't evaluate its right-hand operand if the left-hand operand evaluates to non-null.
38+
/// </summary>
39+
/// <param name="model">The semantic model providing context and information about binary expression.</param>
40+
/// <param name="expression">The syntax representation of the binary expression statement being converted.</param>
41+
/// <exception cref="CompilationException">If an unsupported operator is encountered</exception>
42+
/// <seealso href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/boolean-logical-operators">Boolean logical operators - AND, OR</seealso>
43+
/// <seealso href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/type-testing-and-cast">Type-testing operators and cast expressions - is, as</seealso>
44+
/// <seealso href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator">?? operators - the null-coalescing operators</seealso>
45+
/// <seealso href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/bitwise-and-shift-operators">Bitwise and shift operators</seealso>
46+
/// <seealso href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/arithmetic-operators">Arithmetic operators</seealso>
47+
/// <seealso href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/boolean-logical-operators">Boolean logical operators - AND, OR, NOT, XOR</seealso>
2248
private void ConvertBinaryExpression(SemanticModel model, BinaryExpressionSyntax expression)
2349
{
2450
switch (expression.OperatorToken.ValueText)

src/Neo.Compiler.CSharp/MethodConvert/Expression/CastExpression.cs

+22
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,28 @@ namespace Neo.Compiler;
1919

2020
partial class MethodConvert
2121
{
22+
/// <summary>
23+
/// This method converts a cast expression to OpCodes.
24+
/// A cast expression of the form (T)E performs an explicit conversion of the result of expression E to type T.
25+
/// If no explicit conversion exists from the type of E to type T, a compile-time error occurs.
26+
/// </summary>
27+
/// <param name="model">The semantic model providing context and information about cast expression.</param>
28+
/// <param name="expression">The syntax representation of the cast expression statement being converted.</param>
29+
/// <remarks>
30+
/// This method determines the source type and the target type of the cast expression.
31+
/// If the cast can be resolved to a method symbol, it calls the corresponding method.
32+
/// Otherwise, it generates OpCodes based on the types involved in the cast operation.
33+
/// </remarks>
34+
/// <example>
35+
/// This code is cast a ByteString type to an ECPoint type,
36+
/// where the source type is ByteString and the target type is ECPoint.
37+
/// <code>
38+
/// ByteString bytes = ByteString.Empty;
39+
/// ECPoint point = (ECPoint)bytes;
40+
/// Runtime.Log(point.ToString());
41+
/// </code>
42+
/// </example>
43+
/// <seealso href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/type-testing-and-cast#cast-expression">Cast expression</seealso>
2244
private void ConvertCastExpression(SemanticModel model, CastExpressionSyntax expression)
2345
{
2446
ITypeSymbol sType = model.GetTypeInfo(expression.Expression).Type!;

src/Neo.Compiler.CSharp/MethodConvert/Expression/CheckedExpression.cs

+33
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,39 @@ namespace Neo.Compiler;
1818

1919
partial class MethodConvert
2020
{
21+
/// <summary>
22+
/// The checked and unchecked statements specify the overflow-checking context for integral-type arithmetic operations and conversions.
23+
/// When integer arithmetic overflow occurs, the overflow-checking context defines what happens.
24+
/// In a checked context, a System.OverflowException is thrown;
25+
/// if overflow happens in a constant expression, a compile-time error occurs.
26+
/// </summary>
27+
/// <param name="model">The semantic model providing context and information about checked and unchecked statement.</param>
28+
/// <param name="expression">The syntax representation of the checked and unchecked statement being converted.</param>
29+
/// <example>
30+
/// Use the checked keyword to qualify the result of the temp*2 calculation and use a try catch to handle the overflow if it occurs.
31+
/// <code>
32+
/// try
33+
/// {
34+
/// int temp = int.MaxValue;
35+
/// int a = checked(temp * 2);
36+
/// }
37+
/// catch (OverflowException)
38+
/// {
39+
/// Runtime.Log("Overflow");
40+
/// }
41+
/// </code>
42+
/// </example>
43+
/// <remarks>
44+
/// This code is not called when the checked keyword modifies a block of statements, for example.
45+
/// <code>
46+
/// checked
47+
/// {
48+
/// int a = temp * 2;
49+
/// }
50+
/// </code>
51+
/// For a checked statement, see <see cref="ConvertCheckedStatement(SemanticModel, CheckedStatementSyntax)"/>
52+
/// </remarks>
53+
/// <seealso href="https://learn.microsoft.com/zh-cn/dotnet/csharp/language-reference/operators/arithmetic-operators#integer-arithmetic-overflow">Integer arithmetic overflow</seealso>
2154
private void ConvertCheckedExpression(SemanticModel model, CheckedExpressionSyntax expression)
2255
{
2356
_checkedStack.Push(expression.Keyword.IsKind(SyntaxKind.CheckedKeyword));

src/Neo.Compiler.CSharp/MethodConvert/Expression/ConditionalAccessExpression.cs

+30
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,36 @@ namespace Neo.Compiler;
1919

2020
partial class MethodConvert
2121
{
22+
/// <summary>
23+
/// This method converts a null-conditional access expression to OpCodes.
24+
/// </summary>
25+
/// /// <param name="model">The semantic model providing context and information about null-conditional access expression.</param>
26+
/// <param name="expression">The syntax representation of the null-conditional access expression statement being converted.</param>
27+
/// <remarks>
28+
/// The method evaluates the expression and checks if it is null.
29+
/// If the expression is not null, it converts the 'WhenNotNull' part of the expression.
30+
/// If the resulting type of the expression is 'System.Void', it handles the case differently by dropping the result.
31+
/// A null-conditional operator applies a member access (?.) or element access (?[]) operation to its operand only if that operand evaluates to non-null;
32+
/// otherwise, it returns null.
33+
/// It will jump to <see cref="ConvertMemberBindingExpression"/> and <see cref="ConvertElementBindingExpression"/> to handle the case where the variable or array is not null.
34+
/// </remarks>
35+
/// <example>
36+
/// If Block is not null, get the block's timestamp; otherwise, it returns null.
37+
/// <code>
38+
/// var block = Ledger.GetBlock(10000);
39+
/// var timestamp = block?.Timestamp;
40+
/// Runtime.Log(timestamp.ToString());
41+
/// </code>
42+
/// If array is not null, get the array's element; otherwise, it returns null.
43+
/// <code>
44+
/// var a = Ledger.GetBlock(10000);
45+
/// var b = Ledger.GetBlock(10001);
46+
/// var array = new[] { a, b };
47+
/// var firstItem = array?[0];
48+
/// Runtime.Log(firstItem?.Timestamp.ToString());
49+
/// </code>
50+
/// </example>
51+
/// <seealso href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-">Null-conditional operators ?. and ?[]</seealso>
2252
private void ConvertConditionalAccessExpression(SemanticModel model, ConditionalAccessExpressionSyntax expression)
2353
{
2454
ITypeSymbol type = model.GetTypeInfo(expression).Type!;

src/Neo.Compiler.CSharp/MethodConvert/Expression/ConditionalExpression.cs

+17
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@ namespace Neo.Compiler;
1818

1919
partial class MethodConvert
2020
{
21+
/// <summary>
22+
/// This method converts a ternary conditional expression to OpCodes.
23+
/// </summary>
24+
/// /// <param name="model">The semantic model providing context and information about ternary conditional expression.</param>
25+
/// <param name="expression">The syntax representation of the ternary conditional expression statement being converted.</param>
26+
/// <example>
27+
/// The conditional operator ?:, also known as the ternary conditional operator,
28+
/// evaluates a Boolean expression and returns the result of one of the two expressions,
29+
/// depending on whether the Boolean expression evaluates to true or false, as the following example shows:
30+
/// <code>
31+
/// var index = 10000;
32+
/// var current = Ledger.CurrentIndex;
33+
/// var state = current > index ? "start" : "stop";
34+
/// Runtime.Log(state.ToString());
35+
/// </code>
36+
/// </example>
37+
/// <seealso href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator">?: operator - the ternary conditional operator</seealso>
2138
private void ConvertConditionalExpression(SemanticModel model, ConditionalExpressionSyntax expression)
2239
{
2340
JumpTarget falseTarget = new();

0 commit comments

Comments
 (0)