1 29 package org.jruby.evaluator; 30 31 import java.util.Iterator ; 32 import org.jruby.ast.ArgsCatNode; 33 import org.jruby.ast.BeginNode; 34 import org.jruby.ast.BinaryOperatorNode; 35 import org.jruby.ast.BlockPassNode; 36 import org.jruby.ast.BreakNode; 37 import org.jruby.ast.CallNode; 38 import org.jruby.ast.CaseNode; 39 import org.jruby.ast.ClassNode; 40 import org.jruby.ast.ClassVarAsgnNode; 41 import org.jruby.ast.ClassVarDeclNode; 42 import org.jruby.ast.Colon2Node; 43 import org.jruby.ast.ConstDeclNode; 44 import org.jruby.ast.DAsgnNode; 45 import org.jruby.ast.DefinedNode; 46 import org.jruby.ast.DotNode; 47 import org.jruby.ast.EnsureNode; 48 import org.jruby.ast.EvStrNode; 49 import org.jruby.ast.FCallNode; 50 import org.jruby.ast.FlipNode; 51 import org.jruby.ast.ForNode; 52 import org.jruby.ast.GlobalAsgnNode; 53 import org.jruby.ast.IfNode; 54 import org.jruby.ast.InstAsgnNode; 55 import org.jruby.ast.IterNode; 56 import org.jruby.ast.LocalAsgnNode; 57 import org.jruby.ast.Match2Node; 58 import org.jruby.ast.Match3Node; 59 import org.jruby.ast.MatchNode; 60 import org.jruby.ast.MultipleAsgnNode; 61 import org.jruby.ast.NewlineNode; 62 import org.jruby.ast.Node; 63 import org.jruby.ast.NodeTypes; 64 import org.jruby.ast.NotNode; 65 import org.jruby.ast.OpAsgnNode; 66 import org.jruby.ast.OpElementAsgnNode; 67 import org.jruby.ast.OptNNode; 68 import org.jruby.ast.RescueBodyNode; 69 import org.jruby.ast.RescueNode; 70 import org.jruby.ast.ReturnNode; 71 import org.jruby.ast.SClassNode; 72 import org.jruby.ast.SValueNode; 73 import org.jruby.ast.SplatNode; 74 import org.jruby.ast.ToAryNode; 75 import org.jruby.ast.UntilNode; 76 import org.jruby.ast.WhenNode; 77 import org.jruby.ast.WhileNode; 78 79 public class CreateJumpTargetVisitor { 80 public static void setJumpTarget(Object target, Node node) { 81 bigloop: do { 82 if (node == null) return; 83 84 switch (node.nodeId) { 85 case NodeTypes.ANDNODE: 86 case NodeTypes.OPASGNANDNODE: 87 case NodeTypes.OPASGNORNODE: 88 case NodeTypes.ORNODE: 89 setJumpTarget(target, ((BinaryOperatorNode)node).getFirstNode()); 90 node = ((BinaryOperatorNode)node).getSecondNode(); 91 continue bigloop; 92 case NodeTypes.ARGSCATNODE: 93 setJumpTarget(target, ((ArgsCatNode)node).getFirstNode()); 94 node = ((ArgsCatNode)node).getSecondNode(); 95 continue bigloop; 96 case NodeTypes.ARRAYNODE: 97 case NodeTypes.BLOCKNODE: 98 case NodeTypes.DREGEXPNODE: 99 case NodeTypes.DSTRNODE: 100 case NodeTypes.DSYMBOLNODE: 101 case NodeTypes.DXSTRNODE: 102 case NodeTypes.HASHNODE: 103 for (Iterator iter = node.childNodes().iterator(); iter.hasNext();) { 104 setJumpTarget(target, (Node)iter.next()); 105 } 106 return; 107 case NodeTypes.BEGINNODE: 108 node = ((BeginNode)node).getBodyNode(); 109 continue bigloop; 110 case NodeTypes.BLOCKPASSNODE: 111 setJumpTarget(target, ((BlockPassNode)node).getArgsNode()); 112 setJumpTarget(target, ((BlockPassNode)node).getBodyNode()); 113 return; 114 case NodeTypes.BREAKNODE: 115 node = ((BreakNode)node).getValueNode(); 116 continue bigloop; 117 case NodeTypes.CONSTDECLNODE: 118 node = ((ConstDeclNode)node).getValueNode(); 119 continue bigloop; 120 case NodeTypes.CLASSNODE: 121 setJumpTarget(target, ((ClassNode)node).getSuperNode()); 122 node = ((ClassNode)node).getBodyNode(); 123 continue bigloop; 124 case NodeTypes.CLASSVARASGNNODE: 125 node = ((ClassVarAsgnNode)node).getValueNode(); 126 continue bigloop; 127 case NodeTypes.CLASSVARDECLNODE: 128 node = ((ClassVarDeclNode)node).getValueNode(); 129 continue bigloop; 130 case NodeTypes.CALLNODE: 131 setJumpTarget(target, ((CallNode)node).getIterNode()); 132 node = ((CallNode)node).getReceiverNode(); 133 continue bigloop; 134 case NodeTypes.CASENODE: 135 setJumpTarget(target, ((CaseNode)node).getCaseNode()); 136 node = ((CaseNode)node).getFirstWhenNode(); 137 continue bigloop; 138 case NodeTypes.COLON2NODE: 139 node = ((Colon2Node)node).getLeftNode(); 140 continue bigloop; 141 case NodeTypes.DASGNNODE: 142 node = ((DAsgnNode)node).getValueNode(); 143 continue bigloop; 144 case NodeTypes.DEFINEDNODE: 145 node = ((DefinedNode)node).getExpressionNode(); 146 continue bigloop; 147 case NodeTypes.DOTNODE: 148 setJumpTarget(target, ((DotNode)node).getBeginNode()); 149 node = ((DotNode)node).getEndNode(); 150 continue bigloop; 151 case NodeTypes.ENSURENODE: 152 setJumpTarget(target, ((EnsureNode)node).getBodyNode()); 153 node = ((EnsureNode)node).getEnsureNode(); 154 continue bigloop; 155 case NodeTypes.EVSTRNODE: 156 node = ((EvStrNode)node).getBody(); 157 continue bigloop; 158 case NodeTypes.FCALLNODE: 159 setJumpTarget(target, ((FCallNode)node).getIterNode()); 160 return; 161 case NodeTypes.FLIPNODE: 162 setJumpTarget(target, ((FlipNode)node).getBeginNode()); 163 node = ((FlipNode)node).getEndNode(); 164 continue bigloop; 165 case NodeTypes.FORNODE: 166 setJumpTarget(target, ((ForNode)node).getBodyNode()); 167 setJumpTarget(target, ((ForNode)node).getIterNode()); 168 node = ((ForNode)node).getVarNode(); 169 continue bigloop; 170 case NodeTypes.GLOBALASGNNODE: 171 node = ((GlobalAsgnNode)node).getValueNode(); 172 continue bigloop; 173 case NodeTypes.INSTASGNNODE: 174 node = ((InstAsgnNode)node).getValueNode(); 175 continue bigloop; 176 case NodeTypes.IFNODE: 177 setJumpTarget(target, ((IfNode)node).getCondition()); 178 setJumpTarget(target, ((IfNode)node).getThenBody()); 179 node = ((IfNode)node).getElseBody(); 180 continue bigloop; 181 case NodeTypes.ITERNODE: 182 setJumpTarget(target, ((IterNode)node).getBodyNode()); 183 node = ((IterNode)node).getVarNode(); 184 continue bigloop; 185 case NodeTypes.LOCALASGNNODE: 186 node = ((LocalAsgnNode)node).getValueNode(); 187 continue bigloop; 188 case NodeTypes.MULTIPLEASGNNODE: 189 node = ((MultipleAsgnNode)node).getValueNode(); 190 continue bigloop; 191 case NodeTypes.MATCH2NODE: 192 setJumpTarget(target, ((Match2Node)node).getReceiverNode()); 193 node = ((Match2Node)node).getValueNode(); 194 continue bigloop; 195 case NodeTypes.MATCH3NODE: 196 setJumpTarget(target, ((Match3Node)node).getReceiverNode()); 197 node = ((Match3Node)node).getValueNode(); 198 continue bigloop; 199 case NodeTypes.MATCHNODE: 200 node = ((MatchNode)node).getRegexpNode(); 201 continue bigloop; 202 case NodeTypes.NEWLINENODE: 203 node = ((NewlineNode)node).getNextNode(); 204 continue bigloop; 205 case NodeTypes.NOTNODE: 206 node = ((NotNode)node).getConditionNode(); 207 continue bigloop; 208 case NodeTypes.OPELEMENTASGNNODE: 209 setJumpTarget(target, ((OpElementAsgnNode)node).getReceiverNode()); 210 node = ((OpElementAsgnNode)node).getValueNode(); 211 continue bigloop; 212 case NodeTypes.OPASGNNODE: 213 setJumpTarget(target, ((OpAsgnNode)node).getReceiverNode()); 214 node = ((OpAsgnNode)node).getValueNode(); 215 continue bigloop; 216 case NodeTypes.OPTNNODE: 217 node = ((OptNNode)node).getBodyNode(); 218 continue bigloop; 219 case NodeTypes.RESCUEBODYNODE: 220 setJumpTarget(target, ((RescueBodyNode)node).getBodyNode()); 221 setJumpTarget(target, ((RescueBodyNode)node).getExceptionNodes()); 222 node = ((RescueBodyNode)node).getOptRescueNode(); 223 continue bigloop; 224 case NodeTypes.RESCUENODE: 225 setJumpTarget(target, ((RescueNode)node).getBodyNode()); 226 setJumpTarget(target, ((RescueNode)node).getElseNode()); 227 node = ((RescueNode)node).getRescueNode(); 228 continue bigloop; 229 case NodeTypes.RETURNNODE: 230 ((ReturnNode)node).setTarget(target); 231 return; 232 case NodeTypes.SCLASSNODE: 233 setJumpTarget(target, ((SClassNode)node).getReceiverNode()); 234 node = ((SClassNode)node).getBodyNode(); 235 continue bigloop; 236 case NodeTypes.SPLATNODE: 237 node = ((SplatNode)node).getValue(); 238 continue bigloop; 239 case NodeTypes.SVALUENODE: 240 node = ((SValueNode)node).getValue(); 241 continue bigloop; 242 case NodeTypes.TOARYNODE: 243 node = ((ToAryNode)node).getValue(); 244 continue bigloop; 245 case NodeTypes.UNTILNODE: 246 setJumpTarget(target, ((UntilNode)node).getConditionNode()); 247 node = ((UntilNode)node).getBodyNode(); 248 continue bigloop; 249 case NodeTypes.WHENNODE: 250 setJumpTarget(target, ((WhenNode)node).getBodyNode()); 251 setJumpTarget(target, ((WhenNode)node).getExpressionNodes()); 252 node = ((WhenNode)node).getNextCase(); 253 continue bigloop; 254 case NodeTypes.WHILENODE: 255 setJumpTarget(target, ((WhileNode)node).getConditionNode()); 256 node = ((WhileNode)node).getBodyNode(); 257 continue bigloop; 258 default: 259 return; 260 } 261 } while (node != null); 262 } 263 } | Popular Tags |