|
1 | 1 | using Microsoft.CodeAnalysis; |
| 2 | +using Microsoft.CodeAnalysis.CSharp; |
| 3 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
2 | 4 | using StackMemoryCollections.Generators.Primitive; |
3 | 5 | using StackMemoryCollections.Helpers; |
4 | 6 | using System; |
5 | 7 | using System.Collections.Generic; |
| 8 | +using System.Collections.Immutable; |
6 | 9 | using System.Linq; |
7 | 10 |
|
8 | 11 | namespace StackMemoryCollections |
@@ -34,72 +37,49 @@ internal class AttributeProcessor |
34 | 37 | private readonly Dictionary<string, Model.TypeInfo> _typeInfos = new Dictionary<string, Model.TypeInfo>(); |
35 | 38 |
|
36 | 39 | public void FillAllTypes( |
37 | | - INamespaceOrTypeSymbol symbol |
| 40 | + Compilation compilation, |
| 41 | + ImmutableArray<TypeDeclarationSyntax> types |
38 | 42 | ) |
39 | 43 | { |
40 | | - var queue = new Queue<INamespaceOrTypeSymbol>(); |
41 | | - queue.Enqueue(symbol); |
42 | | - |
43 | | - while (queue.Count != 0) |
| 44 | + foreach (var typeDeclaration in types ) |
44 | 45 | { |
45 | | - var current = queue.Dequeue(); |
46 | | - if (current is INamedTypeSymbol type && |
47 | | - !type.IsAbstract && |
48 | | - !type.IsGenericType && |
49 | | - !type.IsStatic && |
50 | | - (type.IsValueType || type.IsReferenceType)) |
51 | | - { |
52 | | - var attributes = type.GetAttributes(); |
53 | | - var hasHelper = attributes.Any(wh => wh.AttributeClass.Name == "GenerateHelperAttribute"); |
54 | | - var hasStack = attributes.Any(wh => wh.AttributeClass.Name == "GenerateStackAttribute"); |
55 | | - var hasQueue = attributes.Any(wh => wh.AttributeClass.Name == "GenerateQueueAttribute"); |
56 | | - var hasList = attributes.Any(wh => wh.AttributeClass.Name == "GenerateListAttribute"); |
57 | | - var hasDictionary = attributes.Any(wh => wh.AttributeClass.Name == "GenerateDictionaryAttribute"); |
58 | | - var hasWrapper = attributes.Any(wh => wh.AttributeClass.Name == "GenerateWrapperAttribute"); |
59 | | - |
60 | | - if (hasHelper || hasStack || hasQueue || hasList || hasDictionary || hasWrapper) |
61 | | - { |
62 | | - _typeHelpers.Add(type); |
| 46 | + var type = compilation.GetSemanticModel(typeDeclaration.SyntaxTree).GetDeclaredSymbol(typeDeclaration); |
63 | 47 |
|
64 | | - if (hasStack) |
65 | | - { |
66 | | - _typeGeneratedStack.Add(type); |
67 | | - } |
| 48 | + var attributes = type.GetAttributes(); |
| 49 | + var hasHelper = attributes.Any(wh => wh.AttributeClass.Name == "GenerateHelperAttribute"); |
| 50 | + var hasStack = attributes.Any(wh => wh.AttributeClass.Name == "GenerateStackAttribute"); |
| 51 | + var hasQueue = attributes.Any(wh => wh.AttributeClass.Name == "GenerateQueueAttribute"); |
| 52 | + var hasList = attributes.Any(wh => wh.AttributeClass.Name == "GenerateListAttribute"); |
| 53 | + var hasDictionary = attributes.Any(wh => wh.AttributeClass.Name == "GenerateDictionaryAttribute"); |
| 54 | + var hasWrapper = attributes.Any(wh => wh.AttributeClass.Name == "GenerateWrapperAttribute"); |
68 | 55 |
|
69 | | - if (hasList) |
70 | | - { |
71 | | - _typeGeneratedList.Add(type); |
72 | | - } |
| 56 | + if (hasHelper || hasStack || hasQueue || hasList || hasDictionary || hasWrapper) |
| 57 | + { |
| 58 | + _typeHelpers.Add(type); |
73 | 59 |
|
74 | | - if (hasQueue) |
75 | | - { |
76 | | - _typeGeneratedQueue.Add(type); |
77 | | - } |
| 60 | + if (hasStack) |
| 61 | + { |
| 62 | + _typeGeneratedStack.Add(type); |
| 63 | + } |
78 | 64 |
|
79 | | - if (hasDictionary) |
80 | | - { |
81 | | - _typeGeneratedDictionary.Add(type); |
82 | | - } |
| 65 | + if (hasList) |
| 66 | + { |
| 67 | + _typeGeneratedList.Add(type); |
| 68 | + } |
83 | 69 |
|
84 | | - if (hasWrapper) |
85 | | - { |
86 | | - _typeWrappers.Add(type); |
87 | | - } |
| 70 | + if (hasQueue) |
| 71 | + { |
| 72 | + _typeGeneratedQueue.Add(type); |
88 | 73 | } |
89 | | - } |
90 | | - else if (current is INamespaceSymbol namespaceSymbol) |
91 | | - { |
92 | | - if (!_containCollections && namespaceSymbol.Name == "StackMemoryCollections") |
| 74 | + |
| 75 | + if (hasDictionary) |
93 | 76 | { |
94 | | - _containCollections = true; |
| 77 | + _typeGeneratedDictionary.Add(type); |
95 | 78 | } |
96 | | - } |
97 | 79 |
|
98 | | - foreach (var child in current.GetMembers()) |
99 | | - { |
100 | | - if (child is INamespaceOrTypeSymbol symbolChild) |
| 80 | + if (hasWrapper) |
101 | 81 | { |
102 | | - queue.Enqueue(symbolChild); |
| 82 | + _typeWrappers.Add(type); |
103 | 83 | } |
104 | 84 | } |
105 | 85 | } |
@@ -413,7 +393,7 @@ private bool HelperMustGenerated(in System.Collections.Immutable.ImmutableArray< |
413 | 393 | ); |
414 | 394 | } |
415 | 395 |
|
416 | | - public void Generate(GeneratorExecutionContext context) |
| 396 | + public void Generate(SourceProductionContext context) |
417 | 397 | { |
418 | 398 | if (!_containCollections) |
419 | 399 | { |
|
0 commit comments