-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathNamespaceDeclaration.cs
More file actions
76 lines (61 loc) · 2.77 KB
/
NamespaceDeclaration.cs
File metadata and controls
76 lines (61 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Semmle.Extraction.CSharp.Entities
{
internal class NamespaceDeclaration : CachedEntity<BaseNamespaceDeclarationSyntax>
{
private readonly NamespaceDeclaration parent;
private readonly BaseNamespaceDeclarationSyntax node;
public NamespaceDeclaration(Context cx, BaseNamespaceDeclarationSyntax node, NamespaceDeclaration parent)
: base(cx, node)
{
this.node = node;
this.parent = parent;
}
public override void WriteId(EscapingTextWriter trapFile)
{
trapFile.WriteSubId(Context.CreateLocation(ReportingLocation));
trapFile.Write(";namespacedeclaration");
}
public override void Populate(TextWriter trapFile)
{
var @namespace = (INamespaceSymbol?)Context.GetModel(node).GetSymbolInfo(node.Name).Symbol;
if (@namespace is null)
{
throw new InternalError(node, "Namespace symbol not found");
}
var ns = Namespace.Create(Context, @namespace);
trapFile.namespace_declarations(this, ns);
var visitor = new Populators.TypeOrNamespaceVisitor(Context, trapFile, this);
foreach (var member in node.Members.Cast<CSharpSyntaxNode>().Concat(node.Usings))
{
member.Accept(visitor);
}
if (parent is not null)
{
trapFile.parent_namespace_declaration(this, parent);
}
if (OnlyScaffold)
{
return;
}
WriteLocationToTrap(trapFile.namespace_declaration_location, this, Context.CreateLocation(node.Name.GetLocation()));
}
public static NamespaceDeclaration Create(Context cx, BaseNamespaceDeclarationSyntax decl, NamespaceDeclaration parent)
{
var init = (decl, parent);
return NamespaceDeclarationFactory.Instance.CreateEntity(cx, decl, init);
}
private class NamespaceDeclarationFactory : CachedEntityFactory<(BaseNamespaceDeclarationSyntax decl, NamespaceDeclaration parent), NamespaceDeclaration>
{
public static readonly NamespaceDeclarationFactory Instance = new NamespaceDeclarationFactory();
public override NamespaceDeclaration Create(Context cx, (BaseNamespaceDeclarationSyntax decl, NamespaceDeclaration parent) init) =>
new NamespaceDeclaration(cx, init.decl, init.parent);
}
public override Microsoft.CodeAnalysis.Location ReportingLocation => node.Name.GetLocation();
public override bool NeedsPopulation => true;
}
}