Skip to content

Commit 1830eea

Browse files
committed
C#: Cleanup TypeMentions in AST printing
1 parent 444b100 commit 1830eea

File tree

1 file changed

+19
-33
lines changed

1 file changed

+19
-33
lines changed

csharp/ql/src/semmle/code/csharp/PrintAst.qll

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,13 @@ private predicate isInterestingBaseType(ValueOrRefType base) {
120120
*/
121121
private newtype TPrintAstNode =
122122
TElementNode(Element element) { shouldPrint(element, _) } or
123-
TTopLevelTypeMentionNode(TypeMention typeMention) {
124-
shouldPrint(typeMention.getTarget(), _) and
125-
not isNotNeeded(typeMention.getTarget().getParent*()) and
126-
not typeMention.getTarget().getParent*() instanceof TypeParameter
127-
} or
128-
TNestedTypeMentionNode(TypeMention typeMention) {
129-
shouldPrint(typeMention.getParent+().getTarget(), _) and
130-
not isNotNeeded(typeMention.getParent+().getTarget().getParent*()) and
131-
not typeMention.getParent+().getTarget().getParent*() instanceof TypeParameter
123+
TTypeMentionNode(TypeMention typeMention) {
124+
exists(Element target |
125+
target = typeMention.getParent*().getTarget() and
126+
shouldPrint(target, _) and
127+
not isNotNeeded(target.getParent*()) and
128+
not target.getParent*() instanceof TypeParameter
129+
)
132130
} or
133131
TParametersNode(Parameterizable parameterizable) {
134132
shouldPrint(parameterizable, _) and
@@ -246,9 +244,11 @@ abstract class ElementNode extends PrintAstNode, TElementNode {
246244
/**
247245
* A node representing an AST node with an underlying `TypeMention`.
248246
*/
249-
abstract class TypeMentionNode extends PrintAstNode {
247+
class TypeMentionNode extends PrintAstNode, TTypeMentionNode {
250248
TypeMention typeMention;
251249

250+
TypeMentionNode() { this = TTypeMentionNode(typeMention) }
251+
252252
override string toString() {
253253
result = "[TypeMention] " + typeMention.getType().toStringWithTypes()
254254
}
@@ -265,7 +265,7 @@ abstract class TypeMentionNode extends PrintAstNode {
265265
*/
266266
final Element getTarget() { result = typeMention.getTarget() }
267267

268-
override NestedTypeMentionNode getChild(int childIndex) {
268+
override TypeMentionNode getChild(int childIndex) {
269269
result.getTypeMention() =
270270
rank[childIndex](TypeMention t, Location l |
271271
t.getParent() = typeMention and
@@ -276,20 +276,6 @@ abstract class TypeMentionNode extends PrintAstNode {
276276
}
277277
}
278278

279-
/**
280-
* A node representing a top-level `TypeMention`, which has a target `Element`.
281-
*/
282-
class TopLevelTypeMentionNode extends TypeMentionNode, TTopLevelTypeMentionNode {
283-
TopLevelTypeMentionNode() { this = TTopLevelTypeMentionNode(typeMention) }
284-
}
285-
286-
/**
287-
* A node representing a nested `TypeMention`, whose parent is also a `TypeMention`.
288-
*/
289-
class NestedTypeMentionNode extends TypeMentionNode, TNestedTypeMentionNode {
290-
NestedTypeMentionNode() { this = TNestedTypeMentionNode(typeMention) }
291-
}
292-
293279
/**
294280
* A node representing a `ControlFlowElement` (`Expr` or `Stmt`).
295281
*/
@@ -321,7 +307,7 @@ class ControlFlowElementNode extends ElementNode {
321307
or
322308
not exists(controlFlowElement.getAChild()) and childIndex = 0
323309
) and
324-
result.(TopLevelTypeMentionNode).getTarget() = controlFlowElement
310+
result.(TypeMentionNode).getTarget() = controlFlowElement
325311
or
326312
result.(ElementNode).getElement() = controlFlowElement.getChild(childIndex)
327313
}
@@ -353,7 +339,7 @@ final class AssignmentNode extends ControlFlowElementNode {
353339

354340
override PrintAstNode getChild(int childIndex) {
355341
childIndex = -1 and
356-
result.(TopLevelTypeMentionNode).getTarget() = controlFlowElement
342+
result.(TypeMentionNode).getTarget() = controlFlowElement
357343
or
358344
childIndex = 0 and
359345
result.(ElementNode).getElement() = assignment.getLValue()
@@ -394,7 +380,7 @@ final class CallableNode extends ElementNode {
394380

395381
override PrintAstNode getChild(int childIndex) {
396382
childIndex = -1 and
397-
result.(TopLevelTypeMentionNode).getTarget() = callable
383+
result.(TypeMentionNode).getTarget() = callable
398384
or
399385
childIndex = 0 and
400386
result.(AttributesNode).getAttributable() = callable
@@ -426,7 +412,7 @@ final class DeclarationWithAccessorsNode extends ElementNode {
426412

427413
override PrintAstNode getChild(int childIndex) {
428414
childIndex = -1 and
429-
result.(TopLevelTypeMentionNode).getTarget() = declaration
415+
result.(TypeMentionNode).getTarget() = declaration
430416
or
431417
childIndex = 0 and
432418
result.(AttributesNode).getAttributable() = declaration
@@ -462,7 +448,7 @@ final class FieldNode extends ElementNode {
462448

463449
override PrintAstNode getChild(int childIndex) {
464450
childIndex = -1 and
465-
result.(TopLevelTypeMentionNode).getTarget() = field
451+
result.(TypeMentionNode).getTarget() = field
466452
or
467453
childIndex = 0 and
468454
result.(AttributesNode).getAttributable() = field
@@ -504,7 +490,7 @@ final class ParameterNode extends ElementNode {
504490

505491
override PrintAstNode getChild(int childIndex) {
506492
childIndex = -1 and
507-
result.(TopLevelTypeMentionNode).getTarget() = param
493+
result.(TypeMentionNode).getTarget() = param
508494
or
509495
childIndex = 0 and
510496
result.(AttributesNode).getAttributable() = param
@@ -531,7 +517,7 @@ final class AttributeNode extends ElementNode {
531517
or
532518
not exists(attr.getAChild()) and childIndex = 0
533519
) and
534-
result.(TopLevelTypeMentionNode).getTarget() = attr
520+
result.(TypeMentionNode).getTarget() = attr
535521
or
536522
result.(ElementNode).getElement() = attr.getChild(childIndex)
537523
}
@@ -695,7 +681,7 @@ final class BaseTypesNode extends PrintAstNode, TBaseTypesNode {
695681

696682
override Location getLocation() { none() }
697683

698-
override TopLevelTypeMentionNode getChild(int childIndex) {
684+
override TypeMentionNode getChild(int childIndex) {
699685
childIndex = 0 and
700686
result.getTypeMention().getType() = valueOrRefType.getBaseClass() and
701687
result.getTarget() = valueOrRefType

0 commit comments

Comments
 (0)