Skip to content

Commit 8b967f9

Browse files
committed
C#: Re-factor BuildNamedTypeId.
1 parent 39e7bba commit 8b967f9

File tree

1 file changed

+38
-33
lines changed

1 file changed

+38
-33
lines changed

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

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -286,53 +286,58 @@ private static void BuildFunctionPointerTypeId(this IFunctionPointerTypeSymbol f
286286
public static IEnumerable<IFieldSymbol?> GetTupleElementsMaybeNull(this INamedTypeSymbol type) =>
287287
type.TupleElements;
288288

289-
private static void BuildNamedTypeId(this INamedTypeSymbol named, Context cx, EscapingTextWriter trapFile, ISymbol symbolBeingDefined, bool constructUnderlyingTupleType)
289+
private static void AddContaining(INamedTypeSymbol named, Context cx, EscapingTextWriter trapFile, ISymbol symbolBeingDefined)
290290
{
291-
if (!constructUnderlyingTupleType && named.IsTupleType)
291+
if (named.ContainingType is not null)
292292
{
293-
trapFile.Write('(');
294-
trapFile.BuildList(",", named.GetTupleElementsMaybeNull(),
295-
(i, f) =>
296-
{
297-
if (f is null)
298-
{
299-
trapFile.Write($"null({i})");
300-
}
301-
else
302-
{
303-
trapFile.Write((f.CorrespondingTupleField ?? f).Name);
304-
trapFile.Write(":");
305-
f.Type.BuildOrWriteId(cx, trapFile, symbolBeingDefined, constructUnderlyingTupleType: false);
306-
}
307-
}
308-
);
309-
trapFile.Write(")");
310-
return;
293+
named.ContainingType.BuildOrWriteId(cx, trapFile, symbolBeingDefined, constructUnderlyingTupleType: false);
294+
trapFile.Write('.');
311295
}
312-
313-
void AddContaining()
296+
else if (named.ContainingNamespace is not null)
314297
{
315-
if (named.ContainingType is not null)
316-
{
317-
named.ContainingType.BuildOrWriteId(cx, trapFile, symbolBeingDefined, constructUnderlyingTupleType: false);
318-
trapFile.Write('.');
319-
}
320-
else if (named.ContainingNamespace is not null)
298+
if (cx.ShouldAddAssemblyTrapPrefix && named.ContainingAssembly is not null)
299+
BuildAssembly(named.ContainingAssembly, trapFile);
300+
named.ContainingNamespace.BuildNamespace(cx, trapFile);
301+
}
302+
}
303+
304+
private static void BuildTupleId(INamedTypeSymbol named, Context cx, EscapingTextWriter trapFile, ISymbol symbolBeingDefined)
305+
{
306+
trapFile.Write('(');
307+
trapFile.BuildList(",", named.GetTupleElementsMaybeNull(),
308+
(i, f) =>
321309
{
322-
if (cx.ShouldAddAssemblyTrapPrefix && named.ContainingAssembly is not null)
323-
BuildAssembly(named.ContainingAssembly, trapFile);
324-
named.ContainingNamespace.BuildNamespace(cx, trapFile);
310+
if (f is null)
311+
{
312+
trapFile.Write($"null({i})");
313+
}
314+
else
315+
{
316+
trapFile.Write((f.CorrespondingTupleField ?? f).Name);
317+
trapFile.Write(":");
318+
f.Type.BuildOrWriteId(cx, trapFile, symbolBeingDefined, constructUnderlyingTupleType: false);
319+
}
325320
}
321+
);
322+
trapFile.Write(")");
323+
}
324+
325+
private static void BuildNamedTypeId(this INamedTypeSymbol named, Context cx, EscapingTextWriter trapFile, ISymbol symbolBeingDefined, bool constructUnderlyingTupleType)
326+
{
327+
if (!constructUnderlyingTupleType && named.IsTupleType)
328+
{
329+
BuildTupleId(named, cx, trapFile, symbolBeingDefined);
330+
return;
326331
}
327332

328333
if (named.TypeParameters.IsEmpty)
329334
{
330-
AddContaining();
335+
AddContaining(named, cx, trapFile, symbolBeingDefined);
331336
trapFile.Write(named.Name);
332337
}
333338
else if (named.IsReallyUnbound())
334339
{
335-
AddContaining();
340+
AddContaining(named, cx, trapFile, symbolBeingDefined);
336341
trapFile.Write(named.Name);
337342
trapFile.Write("`");
338343
trapFile.Write(named.TypeParameters.Length);

0 commit comments

Comments
 (0)