@Override public boolean equals(Object o) { if (! (o instanceof CAstNode)) return false; if (kind != ((CAstNode)o).getKind()) return false; if (((CAstNode)o).getChildCount() != cs.length) return false; for(int i = 0; i < cs.length; i++) if (! cs[i].equals(((CAstNode)o).getChild(i))) return false; return true; } }
@Override public boolean equals(Object o) { if (! (o instanceof CAstNode)) return false; if (kind != ((CAstNode)o).getKind()) return false; if (((CAstNode)o).getChildCount() != cs.length) return false; for(int i = 0; i < cs.length; i++) if (! cs[i].equals(((CAstNode)o).getChild(i))) return false; return true; } }
private static int[] gatherArrayDims(WalkContext c, CAstNode n) { int numDims = n.getChildCount() - 2; int[] dims = new int[numDims]; for (int i = 0; i < numDims; i++) dims[i] = c.getValue(n.getChild(i + 2)); return dims; }
private static int[] gatherArrayDims(WalkContext c, CAstNode n) { int numDims = n.getChildCount() - 2; int[] dims = new int[numDims]; for (int i = 0; i < numDims; i++) dims[i] = c.getValue(n.getChild(i + 2)); return dims; }
/** * Visit children of a node starting at a given index. * @param n the parent node of the nodes to process * @param start the starting index of the nodes to process * @param context a visitor-specific context */ public final void visitChildren(CAstNode n, int start, C context, CAstVisitor<C> visitor) { int end = n.getChildCount(); for (int i = start; i < end; i++) visitor.visit(n.getChild(i), context, visitor); } /**
private static int getCoveringChildIndex(CAstNode parent, int start, CAstNode child) { for(int i=start;i<parent.getChildCount();++i) if(NodePos.inSubtree(child, parent.getChild(i))) return i; return -1; }
@Override public boolean matches(CAstNode node) { if(node == null || node.getKind() != kind || node.getChildCount() != children.length) return false; for(int i=0;i<children.length;++i) if(!children[i].matches(node.getChild(i))) return false; return true; } }
@Override protected void leaveBlockExprAssignOp(CAstNode n, CAstNode v, CAstNode a, boolean pre, WalkContext c, CAstVisitor<WalkContext> visitor) { /* empty */ c.setValue(n, c.getValue(n.getChild(n.getChildCount() - 1))); }
@Override public boolean matches(CAstNode node) { if(node == null || node.getKind() != kind || node.getChildCount() != children.length) return false; for(int i=0;i<children.length;++i) if(!children[i].matches(node.getChild(i))) return false; return true; } }
private boolean usesArguments(CAstNode node) { if(node.getKind() == CAstNode.VAR) { return node.getChild(0).getValue().equals("arguments"); } else { for(int i=0;i<node.getChildCount();++i) if(usesArguments(node.getChild(i))) return true; return false; } } }
private boolean usesArguments(CAstNode node) { if(node.getKind() == CAstNode.VAR) { return node.getChild(0).getValue().equals("arguments"); } else { for(int i=0;i<node.getChildCount();++i) if(usesArguments(node.getChild(i))) return true; return false; } } }
private boolean hasIncomingEdges(CAstNode n, WalkContext context) { if (context.cfg().hasDelayedEdges(n)) { return true; } else { for (int i = 0; i < n.getChildCount(); i++) { if (hasIncomingEdges(n.getChild(i), context)) { return true; } } return false; } }
private Position getPosition(CAstSourcePositionMap map, CAstNode n) { if (map.getPosition(n) != null) { return map.getPosition(n); } else { for (int i = 0; i < n.getChildCount(); i++) { Position p = getPosition(map, n.getChild(i)); if (p != null) { return p; } } return null; } }
private boolean hasIncomingEdges(CAstNode n, WalkContext context) { if (context.cfg().hasDelayedEdges(n)) { return true; } else { for (int i = 0; i < n.getChildCount(); i++) { if (hasIncomingEdges(n.getChild(i), context)) { return true; } } return false; } }
@Test public void testNativeCAst() throws IOException { CAst Ast = new CAstImpl(); URL junk = IR.class.getClassLoader().getResource("primordial.txt"); SmokeXlator xlator = new SmokeXlator(Ast, junk); CAstNode ast = xlator.translateToCAst().getAST(); System.err.println(ast); assert ast.getChildCount() == 3; } }
private static Set<ChildPos> findNodesAtPos(int kind, Position pos, CAstEntity entity) { Set<ChildPos> res = HashSetFactory.make(); CAstSourcePositionMap spmap = entity.getSourceMap(); CAstNode ast = entity.getAST(); for(int i=0;i<ast.getChildCount();++i) findNodesAtPos(kind, pos, spmap, new ChildPos(ast, i, new RootPos()), res); return res; }
private static Set<ChildPos> findNodesAtPos(int kind, Position pos, CAstEntity entity) { Set<ChildPos> res = HashSetFactory.make(); CAstSourcePositionMap spmap = entity.getSourceMap(); CAstNode ast = entity.getAST(); for(int i=0;i<ast.getChildCount();++i) findNodesAtPos(kind, pos, spmap, new ChildPos(ast, i, new RootPos()), res); return res; }
default <X extends WalkContext<X,Y>, Y> void pushSourcePosition(WalkContext<X, Y> context, CAstNode n, Position p) { if (context.pos().getPosition(n) == null && !(n.getKind()==CAstNode.FUNCTION_EXPR || n.getKind()==CAstNode.FUNCTION_STMT)) { context.pos().setPosition(n, p); for(int i = 0; i < n.getChildCount(); i++) { pushSourcePosition(context, n.getChild(i), p); } } }
@Override protected void leaveThis(CAstNode n, WalkContext c, CAstVisitor<WalkContext> visitor) { if (n.getChildCount() == 0) { super.leaveThis(n, c, visitor); } else { int result = c.currentScope().allocateTempValue(); c.setValue(n, result); c.cfg().addInstruction(new EnclosingObjectReference(c.cfg().getCurrentInstruction(), result, (TypeReference) n.getChild(0).getValue())); } }
@Override protected void leaveThis(CAstNode n, WalkContext c, CAstVisitor<WalkContext> visitor) { if (n.getChildCount() == 0) { super.leaveThis(n, c, visitor); } else { int result = c.currentScope().allocateTempValue(); c.setValue(n, result); c.cfg().addInstruction(new EnclosingObjectReference(c.cfg().getCurrentInstruction(), result, (TypeReference) n.getChild(0).getValue())); } }