Skip to content

Commit eb8c226

Browse files
committed
C#: Add support for explicit return types in the extractor.
1 parent ae62704 commit eb8c226

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Lambda.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ private void VisitParameter(ParameterSyntax p)
2222
Parameter.Create(Context, symbol, this);
2323
}
2424

25-
private Lambda(ExpressionNodeInfo info, CSharpSyntaxNode body, IEnumerable<ParameterSyntax> @params)
25+
private Lambda(ExpressionNodeInfo info, CSharpSyntaxNode body, IEnumerable<ParameterSyntax> @params, TypeSyntax? @return)
2626
: base(info)
2727
{
2828
if (Context.GetModel(info.Node).GetSymbolInfo(info.Node).Symbol is IMethodSymbol symbol)
@@ -40,6 +40,13 @@ private Lambda(ExpressionNodeInfo info, CSharpSyntaxNode body, IEnumerable<Param
4040
foreach (var param in @params)
4141
VisitParameter(param);
4242

43+
if (@return is not null)
44+
{
45+
var symbol = Context.GetType(@return);
46+
var type = Entities.Type.Create(Context, symbol);
47+
var trapFile = Context.TrapWriter.Writer;
48+
trapFile.lambda_expr_return_type(this, type.TypeRef);
49+
}
4350
if (body is ExpressionSyntax exprBody)
4451
Create(Context, exprBody, this, 0);
4552
else if (body is BlockSyntax blockBody)
@@ -50,17 +57,17 @@ private Lambda(ExpressionNodeInfo info, CSharpSyntaxNode body, IEnumerable<Param
5057
}
5158

5259
private Lambda(ExpressionNodeInfo info, ParenthesizedLambdaExpressionSyntax node)
53-
: this(info.SetKind(ExprKind.LAMBDA), node.Body, node.ParameterList.Parameters) { }
60+
: this(info.SetKind(ExprKind.LAMBDA), node.Body, node.ParameterList.Parameters, node.ReturnType) { }
5461

5562
public static Lambda Create(ExpressionNodeInfo info, ParenthesizedLambdaExpressionSyntax node) => new Lambda(info, node);
5663

5764
private Lambda(ExpressionNodeInfo info, SimpleLambdaExpressionSyntax node)
58-
: this(info.SetKind(ExprKind.LAMBDA), node.Body, Enumerators.Singleton(node.Parameter)) { }
65+
: this(info.SetKind(ExprKind.LAMBDA), node.Body, Enumerators.Singleton(node.Parameter), null) { }
5966

6067
public static Lambda Create(ExpressionNodeInfo info, SimpleLambdaExpressionSyntax node) => new Lambda(info, node);
6168

6269
private Lambda(ExpressionNodeInfo info, AnonymousMethodExpressionSyntax node) :
63-
this(info.SetKind(ExprKind.ANONYMOUS_METHOD), node.Body, node.ParameterList is null ? Enumerable.Empty<ParameterSyntax>() : node.ParameterList.Parameters)
70+
this(info.SetKind(ExprKind.ANONYMOUS_METHOD), node.Body, node.ParameterList is null ? Enumerable.Empty<ParameterSyntax>() : node.ParameterList.Parameters, null)
6471
{ }
6572

6673
public static Lambda Create(ExpressionNodeInfo info, AnonymousMethodExpressionSyntax node) => new Lambda(info, node);

csharp/extractor/Semmle.Extraction.CSharp/Tuples.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ internal static void indexer_location(this TextWriter trapFile, Indexer indexer,
215215
internal static void indexers(this TextWriter trapFile, Indexer propKey, string name, Type declaringType, Type memberType, Indexer unboundProperty) =>
216216
trapFile.WriteTuple("indexers", propKey, name, declaringType, memberType, unboundProperty);
217217

218+
internal static void lambda_expr_return_type(this TextWriter trapFile, Lambda expr, Type returnType) =>
219+
trapFile.WriteTuple("lambda_expr_return_type", expr, returnType);
220+
218221
internal static void local_function_stmts(this TextWriter trapFile, Entities.Statements.LocalFunction fnStmt, LocalFunction fn) =>
219222
trapFile.WriteTuple("local_function_stmts", fnStmt, fn);
220223

0 commit comments

Comments
 (0)