Skip to content

Commit feb25d6

Browse files
Globals: Some optimizations on the functions
1 parent 6a8582f commit feb25d6

File tree

1 file changed

+29
-32
lines changed

1 file changed

+29
-32
lines changed

EppNet-SourceGen/Source/Globals.cs

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
using System.Reflection;
1818
using System.Threading;
1919

20+
using static System.Reflection.Metadata.Ecma335.MethodBodyStreamEncoder;
21+
2022
namespace EppNet.SourceGen
2123
{
2224

@@ -119,27 +121,19 @@ public static class Globals
119121
public static bool HasAttribute(ClassDeclarationSyntax classNode, string attrName)
120122
{
121123
foreach (var attrList in classNode.AttributeLists)
122-
{
123124
foreach (var attr in attrList.Attributes)
124-
{
125-
if (attr.Name.ToString() == attrName)
125+
if (attr.Name is IdentifierNameSyntax id && id.Identifier.Text == attrName)
126126
return true;
127-
}
128-
}
129127

130128
return false;
131129
}
132130

133131
public static bool HasAttribute(MethodDeclarationSyntax methodNode, string attrName)
134132
{
135133
foreach (var attrList in methodNode.AttributeLists)
136-
{
137134
foreach (var attr in attrList.Attributes)
138-
{
139-
if (attr.Name.ToString() == attrName)
135+
if (attr.Name is IdentifierNameSyntax id && id.Identifier.Text == attrName)
140136
return true;
141-
}
142-
}
143137

144138
return false;
145139
}
@@ -161,7 +155,7 @@ public static (bool, bool) IsValidTypeName(TypeSyntax typeSyntax, SemanticModel
161155
{
162156
if (SupportedTypes.Contains(typeName))
163157
return (true, false);
164-
else if (resolverDict?.ContainsKey(typeName) == true)
158+
else if (resolverDict is not null && resolverDict.ContainsKey(typeName))
165159
return (true, false);
166160
else
167161
{
@@ -195,9 +189,7 @@ public static (NetworkParameterTypeModel?, TypeSyntax) TryCreateParameterModel(T
195189
NetworkParameterTypeModel? model = null;
196190
ITypeSymbol typeSymbol = semModel.GetTypeInfo(type, cancelToken).Type;
197191

198-
cancelToken.ThrowIfCancellationRequested();
199-
200-
if (typeSymbol == null)
192+
if (cancelToken.IsCancellationRequested || typeSymbol == null)
201193
return (model, null);
202194

203195
if (typeSymbol.TypeKind == TypeKind.Enum)
@@ -214,7 +206,7 @@ public static (NetworkParameterTypeModel?, TypeSyntax) TryCreateParameterModel(T
214206

215207
if (type is GenericNameSyntax genericName)
216208
{
217-
string baseTypeName = $"{typeSymbol.ContainingNamespace.ToDisplayString()}.{genericName.Identifier.Text}";
209+
string baseTypeName = typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
218210
(bool isValidType, bool isNetObj) = IsValidTypeName(type, semModel, baseTypeName, resolverDict);
219211

220212
if (!isValidType)
@@ -253,7 +245,7 @@ public static (NetworkParameterTypeModel?, TypeSyntax) TryCreateParameterModel(T
253245
}
254246
else
255247
{
256-
string fullTypeName = $"{typeSymbol.ContainingNamespace.ToDisplayString()}.{typeSymbol.Name}";
248+
string fullTypeName = typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
257249
(bool isValidType, bool isNetObj) = IsValidTypeName(type, semModel, fullTypeName, resolverDict);
258250

259251
if (!isValidType)
@@ -285,9 +277,8 @@ private static (int, AttributeData) GetDistribution(INamedTypeSymbol symbol)
285277

286278
var namedArg = attrData!.NamedArguments.FirstOrDefault(arg => arg.Key == "Dist");
287279

288-
if (!namedArg.Equals(default(KeyValuePair<string, TypedConstant>)) &&
289-
namedArg.Value.Value is int namedDistType)
290-
distType = namedDistType;
280+
if (attrData.NamedArguments.FirstOrDefault(arg => arg.Key == "Dist") is { Value.Value: int namedDistType })
281+
return (namedDistType, attrData);
291282

292283
return (distType, attrData);
293284
}
@@ -304,7 +295,8 @@ public static (NetworkObjectModel?, List<AnalysisDiagnostic>) TryCreateNetObject
304295
NetworkObjectModel? model = null;
305296
List<AnalysisDiagnostic> errors = [];
306297

307-
cancelToken.ThrowIfCancellationRequested();
298+
if (cancelToken.IsCancellationRequested)
299+
return (model, errors);
308300

309301
if (node is not ClassDeclarationSyntax classNode)
310302
errors.Add(new AnalysisDiagnostic(DescNetObjError, node, "Network Objects must be classes!"));
@@ -338,13 +330,12 @@ public static (NetworkObjectModel?, List<AnalysisDiagnostic>) TryCreateNetObject
338330
// Let's ascend the hierarchy
339331
while (true)
340332
{
341-
cancelToken.ThrowIfCancellationRequested();
342333
int tDistType = GetDistribution(tSymbol).Item1;
343334

344335
if (tDistType != -1)
345336
{
346337
// This is a network object
347-
string baseClassName = $"{tSymbol.ContainingNamespace.ToDisplayString()}.{tSymbol.Name}";
338+
string baseClassName = tSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
348339
baseNetObjs.Add(baseClassName);
349340

350341
AttributeSyntax context = distData.Item2.ApplicationSyntaxReference?.GetSyntax() as AttributeSyntax;
@@ -361,6 +352,9 @@ public static (NetworkObjectModel?, List<AnalysisDiagnostic>) TryCreateNetObject
361352
break;
362353
}
363354

355+
if (cancelToken.IsCancellationRequested)
356+
return (model, errors);
357+
364358
// Step 4: Let's locate network methods
365359
(EquatableDictionary<string, EquatableHashSet<NetworkMethodModel>> myMethods, List<AnalysisDiagnostic> methodErrors) =
366360
TryAndLocateNetMethods(classNode, semModel, resolverDict, cancelToken);
@@ -377,11 +371,9 @@ public static (NetworkObjectModel?, List<AnalysisDiagnostic>) TryCreateNetObject
377371
public static (NetworkMethodModel?, List<AnalysisDiagnostic>) TryCreateNetMethod(MethodDeclarationSyntax methodNode,
378372
SemanticModel semModel, IDictionary<string, string> resolverDict, CancellationToken cancelToken = default)
379373
{
380-
cancelToken.ThrowIfCancellationRequested();
381-
382374
// Ensure the method has our attribute. If it doesn't...
383375
// we do not care
384-
if (methodNode == null || !HasAttribute(methodNode, NetMethodAttr))
376+
if (cancelToken.IsCancellationRequested || methodNode == null || !HasAttribute(methodNode, NetMethodAttr))
385377
return (null, null);
386378

387379
List<AnalysisDiagnostic> errors = [];
@@ -425,17 +417,14 @@ public static (NetworkMethodModel?, List<AnalysisDiagnostic>) TryCreateNetMethod
425417

426418
foreach (ParameterSyntax paramNode in methodNode.ParameterList.Parameters)
427419
{
428-
// Don't get too far without polling token
429-
cancelToken.ThrowIfCancellationRequested();
430-
431420
TypeSyntax type = paramNode.Type;
432421

433422
(NetworkParameterTypeModel? typeModel, TypeSyntax typeArg) = TryCreateParameterModel(type, semModel, resolverDict, cancelToken);
434423

435424
if (!typeModel.HasValue)
436425
{
437426
ITypeSymbol typeSymbol = semModel.GetTypeInfo(typeArg, cancelToken).Type;
438-
string typeName = $"{typeSymbol.ContainingNamespace.ToDisplayString()}.{typeSymbol.Name}";
427+
string typeName = typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
439428

440429
errors.Add(new AnalysisDiagnostic(DescNetMethodError, typeArg,
441430
$"\"{typeName}\" is not a valid network type or network object. Do you have a resolver?", methodNode.GetLocation()));
@@ -446,6 +435,9 @@ public static (NetworkMethodModel?, List<AnalysisDiagnostic>) TryCreateNetMethod
446435

447436
NetworkMethodModel? model = null;
448437

438+
if (cancelToken.IsCancellationRequested)
439+
return (model, errors);
440+
449441
if (errors.Count == 0)
450442
{
451443
// If we didn't encounter any errors, let's create the model
@@ -459,13 +451,17 @@ public static (EquatableDictionary<string, EquatableHashSet<NetworkMethodModel>>
459451
SemanticModel semModel, IDictionary<string, string> resolverDict, CancellationToken cancelToken = default)
460452
{
461453

462-
cancelToken.ThrowIfCancellationRequested();
454+
if (cancelToken.IsCancellationRequested)
455+
return (null, null);
463456

464457
EquatableDictionary<string, EquatableHashSet<NetworkMethodModel>> methodsDict = new();
465458
List<AnalysisDiagnostic> allDiags = [];
466459

467-
foreach (MethodDeclarationSyntax methodNode in classNode.Members.OfType<MethodDeclarationSyntax>())
460+
foreach (var member in classNode.Members)
468461
{
462+
if (member is not MethodDeclarationSyntax methodNode)
463+
continue;
464+
469465
(NetworkMethodModel? model, List<AnalysisDiagnostic> diags) =
470466
TryCreateNetMethod(methodNode, semModel, resolverDict, cancelToken);
471467

@@ -493,7 +489,8 @@ public static (ResolverModel?, List<AnalysisDiagnostic>) TryCreateResolver(CShar
493489
ResolverModel? model = null;
494490
List<AnalysisDiagnostic> errors = [];
495491

496-
cancelToken.ThrowIfCancellationRequested();
492+
if (cancelToken.IsCancellationRequested)
493+
return (model, errors);
497494

498495
if (node is not ClassDeclarationSyntax classNode)
499496
{

0 commit comments

Comments
 (0)