88
99namespace Semmle . Extraction . CSharp . Entities
1010{
11- internal class NamespaceDeclaration : FreshEntity
11+ class NamespaceDeclaration : CachedEntity < NamespaceDeclarationSyntax >
1212 {
1313 private readonly NamespaceDeclaration parent ;
1414 private readonly NamespaceDeclarationSyntax node ;
1515
1616 public NamespaceDeclaration ( Context cx , NamespaceDeclarationSyntax node , NamespaceDeclaration parent )
17- : base ( cx )
17+ : base ( cx , node )
1818 {
1919 this . node = node ;
2020 this . parent = parent ;
21- TryPopulate ( ) ;
2221 }
2322
24- protected override void Populate ( TextWriter trapFile )
23+ public override void WriteId ( TextWriter trapFile )
2524 {
26- var @namespace = ( INamespaceSymbol ) cx . GetModel ( node ) . GetSymbolInfo ( node . Name ) . Symbol ;
27- var ns = Namespace . Create ( cx , @namespace ) ;
25+ trapFile . WriteSubId ( Context . Create ( ReportingLocation ) ) ;
26+ trapFile . Write ( ";namespacedeclaration" ) ;
27+ }
28+
29+ public override void Populate ( TextWriter trapFile )
30+ {
31+ var @namespace = ( INamespaceSymbol ) Context . GetModel ( node ) . GetSymbolInfo ( node . Name ) . Symbol ;
32+ var ns = Namespace . Create ( Context , @namespace ) ;
2833 trapFile . namespace_declarations ( this , ns ) ;
29- trapFile . namespace_declaration_location ( this , cx . Create ( node . Name . GetLocation ( ) ) ) ;
34+ trapFile . namespace_declaration_location ( this , Context . Create ( node . Name . GetLocation ( ) ) ) ;
3035
31- var visitor = new Populators . TypeOrNamespaceVisitor ( cx , trapFile , this ) ;
36+ var visitor = new Populators . TypeOrNamespaceVisitor ( Context , trapFile , this ) ;
3237
3338 foreach ( var member in node . Members . Cast < CSharpSyntaxNode > ( ) . Concat ( node . Usings ) )
3439 {
@@ -41,8 +46,24 @@ protected override void Populate(TextWriter trapFile)
4146 }
4247 }
4348
44- public static NamespaceDeclaration Create ( Context cx , NamespaceDeclarationSyntax decl , NamespaceDeclaration parent ) => new NamespaceDeclaration ( cx , decl , parent ) ;
49+ public static NamespaceDeclaration Create ( Context cx , NamespaceDeclarationSyntax decl , NamespaceDeclaration parent )
50+ {
51+ var init = ( decl , parent ) ;
52+ return NamespaceDeclarationFactory . Instance . CreateEntity ( cx , init , init ) ;
53+ }
54+
55+ class NamespaceDeclarationFactory : ICachedEntityFactory < ( NamespaceDeclarationSyntax decl , NamespaceDeclaration parent ) , NamespaceDeclaration >
56+ {
57+ public static readonly NamespaceDeclarationFactory Instance = new NamespaceDeclarationFactory ( ) ;
58+
59+ public NamespaceDeclaration Create ( Context cx , ( NamespaceDeclarationSyntax decl , NamespaceDeclaration parent ) init ) =>
60+ new NamespaceDeclaration ( cx , init . decl , init . parent ) ;
61+ }
4562
4663 public override TrapStackBehaviour TrapStackBehaviour => TrapStackBehaviour . NoLabel ;
64+
65+ public override Microsoft . CodeAnalysis . Location ReportingLocation => node . Name . GetLocation ( ) ;
66+
67+ public override bool NeedsPopulation => true ;
4768 }
4869}
0 commit comments