forked from neo-project/neo-devpack-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvocationExpression.cs
114 lines (107 loc) · 4.94 KB
/
InvocationExpression.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
// 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;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Neo.SmartContract;
using Neo.VM;
using System.Linq;
namespace Neo.Compiler;
partial class MethodConvert
{
/// <summary>
/// Converts Invocation, include method invocation, event invocation and delegate invocation to OpCodes.
/// </summary>
/// <param name="model">The semantic model providing context and information about invocation expression.</param>
/// <param name="expression">The syntax representation of the invocation expression statement being converted.</param>
private void ConvertInvocationExpression(SemanticModel model, InvocationExpressionSyntax expression)
{
ArgumentSyntax[] arguments = expression.ArgumentList.Arguments.ToArray();
ISymbol symbol = model.GetSymbolInfo(expression.Expression).Symbol!;
switch (symbol)
{
case IEventSymbol @event:
ConvertEventInvocationExpression(model, @event, arguments);
break;
case IMethodSymbol method:
ConvertMethodInvocationExpression(model, method, expression.Expression, arguments);
break;
default:
ConvertDelegateInvocationExpression(model, expression.Expression, arguments);
break;
}
}
/// <summary>
/// Convert the event invocation expression to OpCodes.
/// </summary>
/// <param name="model">The semantic model providing context and information about event invocation expression.</param>
/// <param name="symbol">Symbol of the event</param>
/// <param name="arguments">Arguments of the event</param>
/// <example><see href="https://github.com/neo-project/neo-devpack-dotnet/blob/master/examples/Example.SmartContract.Event/Event.cs"/></example>
private void ConvertEventInvocationExpression(SemanticModel model, IEventSymbol symbol, ArgumentSyntax[] arguments)
{
AddInstruction(OpCode.NEWARRAY0);
foreach (ArgumentSyntax argument in arguments)
{
AddInstruction(OpCode.DUP);
ConvertExpression(model, argument.Expression);
AddInstruction(OpCode.APPEND);
}
Push(symbol.GetDisplayName());
Call(ApplicationEngine.System_Runtime_Notify);
}
/// <summary>
/// Convert the method invocation expression to OpCodes.
/// </summary>
/// <param name="model">The semantic model providing context and information about method invocation expression.</param>
/// <param name="symbol">Symbol of the method</param>
/// <param name="expression">The syntax representation of the method invocation expression statement being converted.</param>
/// <param name="arguments">Arguments of the method</param>
/// <example>
/// <c>Runtime.Log("hello World!");</c>
/// </example>
private void ConvertMethodInvocationExpression(SemanticModel model, IMethodSymbol symbol, ExpressionSyntax expression, ArgumentSyntax[] arguments)
{
switch (expression)
{
case IdentifierNameSyntax:
Call(model, symbol, null, arguments);
break;
case MemberAccessExpressionSyntax syntax:
if (symbol.IsStatic)
Call(model, symbol, null, arguments);
else
Call(model, symbol, syntax.Expression, arguments);
break;
case MemberBindingExpressionSyntax:
Call(model, symbol, true, arguments);
break;
default:
throw new CompilationException(expression, DiagnosticId.SyntaxNotSupported, $"Unsupported expression: {expression}");
}
}
/// <summary>
/// Convert the delegate invocation expression to OpCodes.
/// </summary>
/// <param name="model">The semantic model providing context and information about delegate invocation expression.</param>
/// <param name="expression">The syntax representation of the delegate invocation expression statement being converted.</param>
/// <param name="arguments">Arguments of the delegate</param>
/// <example>
/// TODO
/// </example>
private void ConvertDelegateInvocationExpression(SemanticModel model, ExpressionSyntax expression, ArgumentSyntax[] arguments)
{
INamedTypeSymbol type = (INamedTypeSymbol)model.GetTypeInfo(expression).Type!;
PrepareArgumentsForMethod(model, type.DelegateInvokeMethod!, arguments);
ConvertExpression(model, expression);
AddInstruction(OpCode.CALLA);
}
}