Skip to content

Commit e3ed6c2

Browse files
committed
refactor StaticInitializer into it's own class
1 parent 23e28ae commit e3ed6c2

File tree

12 files changed

+112
-37
lines changed

12 files changed

+112
-37
lines changed

javascript/extractor/src/com/semmle/jcorn/Parser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3219,7 +3219,7 @@ protected Node parseClass(Position startLoc, boolean isStatement) {
32193219
Expression superClass = this.parseClassSuper();
32203220
Position bodyStartLoc = this.startLoc;
32213221
boolean hadConstructor = false;
3222-
List<Node> body = new ArrayList<>();
3222+
List<MemberDefinition<?>> body = new ArrayList<>();
32233223
this.expect(TokenType.braceL);
32243224
while (!this.eat(TokenType.braceR)) {
32253225
if (this.eat(TokenType.semi)) continue;

javascript/extractor/src/com/semmle/js/ast/ClassBody.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44

55
/** The body of a {@linkplain ClassDeclaration} or {@linkplain ClassExpression}. */
66
public class ClassBody extends Node {
7-
private final List<Node> body; // either MemberDefinition or BlockStatement (static initialization blocks)
7+
private final List<MemberDefinition<?>> body;
88

9-
public ClassBody(SourceLocation loc, List<Node> body) {
9+
public ClassBody(SourceLocation loc, List<MemberDefinition<?>> body) {
1010
super("ClassBody", loc);
1111
this.body = body;
1212
}
1313

14-
public List<Node> getBody() {
14+
public List<MemberDefinition<?>> getBody() {
1515
return body;
1616
}
1717

18-
public void addMember(Node md) {
18+
public void addMember(MemberDefinition<?> md) {
1919
body.add(md);
2020
}
2121

javascript/extractor/src/com/semmle/js/ast/DefaultVisitor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,4 +782,9 @@ public R visit(XMLDotDotExpression nd, C c) {
782782
public R visit(GeneratedCodeExpr nd, C c) {
783783
return visit((Expression) nd, c);
784784
}
785+
786+
@Override
787+
public R visit(StaticInitializer nd, C c) {
788+
return visit((MemberDefinition<Expression>) nd, c);
789+
}
785790
}

javascript/extractor/src/com/semmle/js/ast/NodeCopier.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,11 @@ public INode visit(XMLDotDotExpression nd, Void c) {
897897

898898
@Override
899899
public INode visit(GeneratedCodeExpr nd, Void c) {
900-
return new GeneratedCodeExpr(visit(nd.getLoc()), nd.getOpeningDelimiter(), nd.getClosingDelimiter(), nd.getBody());
900+
return new GeneratedCodeExpr(visit(nd.getLoc()), nd.getOpeningDelimiter(), nd.getClosingDelimiter(), nd.getBody());
901+
}
902+
903+
@Override
904+
public INode visit(StaticInitializer nd, Void c) {
905+
return new StaticInitializer(visit(nd.getLoc()), copy(nd.getBody()));
901906
}
902907
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.semmle.js.ast;
2+
3+
/**
4+
* A static initializer block in a class.
5+
* E.g.
6+
* ```TypeScript
7+
* class Foo {
8+
* static bar : number;
9+
* static {
10+
* Foo.bar = 42;
11+
* }
12+
* }
13+
*/
14+
public class StaticInitializer extends MemberDefinition<Expression> {
15+
private final BlockStatement body;
16+
17+
public StaticInitializer(SourceLocation loc, BlockStatement body) {
18+
super("StaticInitializer", loc, DeclarationFlags.static_, null, null);
19+
this.body = body;
20+
}
21+
22+
/**
23+
* Gets the body of this static initializer.
24+
* @return The body of this static initializer.
25+
*/
26+
public BlockStatement getBody() {
27+
return body;
28+
}
29+
30+
@Override
31+
public boolean isConcrete() {
32+
return false;
33+
}
34+
35+
36+
@Override
37+
public <C, R> R accept(Visitor<C, R> v, C c) {
38+
return v.visit(this, c);
39+
}
40+
}

javascript/extractor/src/com/semmle/js/ast/Visitor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,6 @@ public interface Visitor<C, R> {
315315
public R visit(XMLDotDotExpression nd, C c);
316316

317317
public R visit(GeneratedCodeExpr generatedCodeExpr, C c);
318+
319+
public R visit(StaticInitializer nd, C c);
318320
}

javascript/extractor/src/com/semmle/js/extractor/ASTExtractor.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
import com.semmle.js.ast.SourceLocation;
8181
import com.semmle.js.ast.SpreadElement;
8282
import com.semmle.js.ast.Statement;
83+
import com.semmle.js.ast.StaticInitializer;
8384
import com.semmle.js.ast.Super;
8485
import com.semmle.js.ast.SwitchCase;
8586
import com.semmle.js.ast.SwitchStatement;
@@ -1613,6 +1614,8 @@ public Label visit(MemberDefinition<?> nd, Context c) {
16131614
int kind;
16141615
if (nd instanceof MethodDefinition) {
16151616
kind = getMethodKind((MethodDefinition) nd);
1617+
} else if (nd instanceof StaticInitializer) {
1618+
kind = 10;
16161619
} else {
16171620
kind = getFieldKind((FieldDefinition) nd);
16181621
}
@@ -1643,6 +1646,11 @@ public Label visit(MemberDefinition<?> nd, Context c) {
16431646
}
16441647
}
16451648

1649+
if (nd instanceof StaticInitializer) {
1650+
StaticInitializer si = (StaticInitializer) nd;
1651+
visit(si.getBody(), methkey, 3, IdContext.VAR_BIND);
1652+
}
1653+
16461654
if (nd instanceof FieldDefinition) {
16471655
FieldDefinition field = (FieldDefinition) nd;
16481656
if (field.isParameterField() && constructorKey != null) {

javascript/extractor/src/com/semmle/js/extractor/CFGExtractor.java

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import com.semmle.js.ast.SourceLocation;
6666
import com.semmle.js.ast.SpreadElement;
6767
import com.semmle.js.ast.Statement;
68+
import com.semmle.js.ast.StaticInitializer;
6869
import com.semmle.js.ast.Super;
6970
import com.semmle.js.ast.SwitchCase;
7071
import com.semmle.js.ast.SwitchStatement;
@@ -454,7 +455,7 @@ public Node visit(ClassExpression nd, Void v) {
454455

455456
@Override
456457
public Node visit(ClassBody nd, Void v) {
457-
for (Node m : nd.getBody()) {
458+
for (MemberDefinition<?> m : nd.getBody()) {
458459
if (m instanceof MethodDefinition) {
459460
Node first = m.accept(this, v);
460461
if (first != null) return first;
@@ -1163,11 +1164,8 @@ public Void visit(ClassExpression nd, SuccessorInfo i) {
11631164
private Map<Expression, AClass> constructor2Class = new LinkedHashMap<>();
11641165

11651166
private Void visit(Node nd, AClass ac, SuccessorInfo i) {
1166-
for (Node m : ac.getBody().getBody()) {
1167-
if (m instanceof MemberDefinition) {
1168-
MemberDefinition md = (MemberDefinition) m;
1169-
if (md.isConstructor() && md.isConcrete()) constructor2Class.put(md.getValue(), ac);
1170-
}
1167+
for (MemberDefinition<?> md : ac.getBody().getBody()) {
1168+
if (md.isConstructor() && md.isConcrete()) constructor2Class.put(md.getValue(), ac);
11711169
}
11721170
visitSequence(ac.getId(), ac.getSuperClass(), ac.getBody(), nd);
11731171
writeSuccessors(nd, visitSequence(getStaticInitializers(ac.getBody()), getDecoratorsOfClass(ac), i.getAllSuccessors()));
@@ -1207,18 +1205,15 @@ private List<Node> getDecoratorsOfClass(AClass ac) {
12071205
List<Node> staticDecorators = new ArrayList<>();
12081206
List<Node> constructorParameterDecorators = new ArrayList<>();
12091207
List<Node> classDecorators = (List<Node>)(List<?>)ac.getDecorators();
1210-
for (Node memberNode : ac.getBody().getBody()) {
1211-
if (memberNode instanceof MemberDefinition) {
1212-
MemberDefinition<?> member = (MemberDefinition<?>) memberNode;
1213-
if (!member.isConcrete()) continue;
1214-
List<Node> decorators = getMemberDecorators(member);
1215-
if (member.isConstructor()) {
1216-
constructorParameterDecorators.addAll(decorators);
1217-
} else if (member.isStatic()) {
1218-
staticDecorators.addAll(decorators);
1219-
} else {
1220-
instanceDecorators.addAll(decorators);
1221-
}
1208+
for (MemberDefinition<?> member : ac.getBody().getBody()) {
1209+
if (!member.isConcrete()) continue;
1210+
List<Node> decorators = getMemberDecorators(member);
1211+
if (member.isConstructor()) {
1212+
constructorParameterDecorators.addAll(decorators);
1213+
} else if (member.isStatic()) {
1214+
staticDecorators.addAll(decorators);
1215+
} else {
1216+
instanceDecorators.addAll(decorators);
12221217
}
12231218
}
12241219
List<Node> result = new ArrayList<>();
@@ -1619,8 +1614,8 @@ public Void visit(ClassBody nd, SuccessorInfo i) {
16191614

16201615
private List<MemberDefinition<?>> getMethods(ClassBody nd) {
16211616
List<MemberDefinition<?>> mds = new ArrayList<>();
1622-
for (Node md : nd.getBody()) {
1623-
if (md instanceof MethodDefinition) mds.add((MemberDefinition<?>)md);
1617+
for (MemberDefinition<?> md : nd.getBody()) {
1618+
if (md instanceof MethodDefinition) mds.add(md);
16241619
}
16251620
return mds;
16261621
}
@@ -1630,18 +1625,16 @@ private List<MemberDefinition<?>> getMethods(ClassBody nd) {
16301625
*/
16311626
private List<Node> getStaticInitializers(ClassBody nd) {
16321627
List<Node> nodes = new ArrayList<>();
1633-
for (Node node : nd.getBody()) {
1628+
for (MemberDefinition<?> node : nd.getBody()) {
16341629
if (node instanceof FieldDefinition && ((FieldDefinition)node).isStatic()) nodes.add(node);
1635-
if (node instanceof BlockStatement) nodes.add(node);
1630+
if (node instanceof StaticInitializer) nodes.add(((StaticInitializer)node).getBody());
16361631
}
16371632
return nodes;
16381633
}
16391634

16401635
private List<FieldDefinition> getConcreteInstanceFields(ClassBody nd) {
16411636
List<FieldDefinition> fds = new ArrayList<>();
1642-
for (Node node : nd.getBody()) {
1643-
if (!(node instanceof MemberDefinition)) continue;
1644-
MemberDefinition<?> md = (MemberDefinition<?>)node;
1637+
for (MemberDefinition<?> md : nd.getBody()) {
16451638
if (md instanceof FieldDefinition && !md.isStatic() && md.isConcrete())
16461639
fds.add((FieldDefinition) md);
16471640
}

javascript/extractor/src/com/semmle/ts/extractor/TypeScriptASTConverter.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
import com.semmle.js.ast.SourceLocation;
8383
import com.semmle.js.ast.SpreadElement;
8484
import com.semmle.js.ast.Statement;
85+
import com.semmle.js.ast.StaticInitializer;
8586
import com.semmle.js.ast.Super;
8687
import com.semmle.js.ast.SwitchCase;
8788
import com.semmle.js.ast.SwitchStatement;
@@ -155,6 +156,8 @@
155156
import com.semmle.util.collections.CollectionUtil;
156157
import com.semmle.util.data.IntList;
157158

159+
import jdk.internal.org.objectweb.asm.commons.StaticInitMerger;
160+
158161
/**
159162
* Utility class for converting a <a
160163
* href="https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API">TypeScript AST
@@ -869,7 +872,8 @@ private Node convertBinaryExpression(JsonObject node, SourceLocation loc) throws
869872
}
870873

871874
private Node convertStaticInitializerBlock(JsonObject node, SourceLocation loc) throws ParseError {
872-
return new BlockStatement(loc, convertChildren(node.get("body").getAsJsonObject(), "statements"));
875+
BlockStatement body = new BlockStatement(loc, convertChildren(node.get("body").getAsJsonObject(), "statements"));
876+
return new StaticInitializer(loc, body);
873877
}
874878

875879
private Node convertBlock(JsonObject node, SourceLocation loc) throws ParseError {

javascript/ql/lib/semmle/javascript/Classes.qll

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,9 @@ class ClassDefinition extends @class_definition, ClassOrInterface, AST::ValueNod
262262
/**
263263
* Gets a static initializer of this class, if any.
264264
*/
265-
BlockStmt getAStaticInitializerBlock() { result.getParent() = this }
265+
BlockStmt getAStaticInitializerBlock() {
266+
exists(StaticInitializer init | init.getDeclaringClass() = this | result = init.getBody())
267+
}
266268
}
267269

268270
/**
@@ -1139,6 +1141,15 @@ class ParameterField extends FieldDeclaration, @parameter_field {
11391141
override TypeAnnotation getTypeAnnotation() { result = getParameter().getTypeAnnotation() }
11401142
}
11411143

1144+
/**
1145+
* A static initializer in a class.
1146+
*/
1147+
class StaticInitializer extends MemberDefinition, @static_initializer {
1148+
BlockStmt getBody() { result.getParent() = this }
1149+
1150+
override Expr getNameExpr() { none() }
1151+
}
1152+
11421153
/**
11431154
* A call signature declared in an interface.
11441155
*

0 commit comments

Comments
 (0)