-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathUserCompoundAssignmentInvocation.cs
More file actions
40 lines (34 loc) · 1.54 KB
/
UserCompoundAssignmentInvocation.cs
File metadata and controls
40 lines (34 loc) · 1.54 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
using System.IO;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Semmle.Extraction.Kinds;
namespace Semmle.Extraction.CSharp.Entities.Expressions
{
/// <summary>
/// Represents a user-defined compound assignment operator such as `a += b` where `a` is of a type that defines an `operator +=`.
/// In this case, we don't want to desugar it into `a = a + b`, but instead extract the operator call directly as it should
/// be considered an instance method call on `a` with `b` as an argument.
/// </summary>
internal class UserCompoundAssignmentInvocation : Expression<AssignmentExpressionSyntax>
{
private readonly ExpressionNodeInfo info;
protected UserCompoundAssignmentInvocation(ExpressionNodeInfo info)
: base(info.SetKind(ExprKind.OPERATOR_INVOCATION))
{
this.info = info;
}
public static Expression Create(ExpressionNodeInfo info) => new UserCompoundAssignmentInvocation(info).TryPopulate();
protected override void PopulateExpression(TextWriter trapFile)
{
Create(Context, Syntax.Left, this, -1);
Create(Context, Syntax.Right, this, 0);
var target = info.GetTargetSymbol(Context);
if (target is null)
{
Context.ModelError(Syntax, "Unable to resolve target method for user-defined compound assignment operator");
return;
}
var targetKey = Method.Create(Context, target);
trapFile.expr_call(this, targetKey);
}
}
}