1 /* 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.graalvm.compiler.graph.iterators; 24 25 import org.graalvm.compiler.graph.Node; 26 27 public abstract class NodePredicates { 28 29 private static final TautologyPredicate TAUTOLOGY = new TautologyPredicate(); 30 private static final ContradictionPredicate CONTRADICTION = new ContradictionPredicate(); 31 private static final IsNullPredicate IS_NULL = new IsNullPredicate(); 32 33 public static NodePredicate alwaysTrue() { 34 return TAUTOLOGY; 35 } 36 37 public static NodePredicate alwaysFalse() { 38 return CONTRADICTION; 39 } 40 41 public static NodePredicate isNull() { 42 return IS_NULL; 43 } 44 45 public static NegativeTypePredicate isNotA(Class<? extends Node> clazz) { 46 return new NegativeTypePredicate(clazz); 47 } 48 49 public static PositiveTypePredicate isA(Class<? extends Node> clazz) { 50 return new PositiveTypePredicate(clazz); 51 } 52 53 static final class TautologyPredicate implements NodePredicate { 54 55 @Override 56 public boolean apply(Node n) { 57 return true; 58 } 59 60 @Override 61 public NodePredicate and(NodePredicate np) { 62 return np; 63 } 64 } 65 66 static final class ContradictionPredicate implements NodePredicate { 67 68 @Override 69 public boolean apply(Node n) { 70 return false; 71 } 72 73 @Override 74 public NodePredicate and(NodePredicate np) { 75 return this; 76 } 77 } 78 79 static final class AndPredicate implements NodePredicate { 80 81 private final NodePredicate a; 82 private final NodePredicate b; 83 84 AndPredicate(NodePredicate a, NodePredicate b) { 85 this.a = a; 86 this.b = b; 87 } 88 89 @Override 90 public boolean apply(Node n) { 91 return a.apply(n) && b.apply(n); 92 } 93 } 94 95 static final class NotPredicate implements NodePredicate { 96 97 private final NodePredicate a; 98 99 NotPredicate(NodePredicate n) { 100 this.a = n; 101 } 102 103 @Override 104 public boolean apply(Node n) { 105 return !a.apply(n); 106 } 107 108 public NodePredicate negate() { 109 return a; 110 } 111 } 112 113 static final class IsNullPredicate implements NodePredicate { 114 115 @Override 116 public boolean apply(Node n) { 117 return n == null; 118 } 119 } 120 121 public static final class PositiveTypePredicate implements NodePredicate { 122 123 private final Class<?> type; 124 private PositiveTypePredicate or; 125 126 PositiveTypePredicate(Class<?> type) { 127 this.type = type; 128 } 129 130 public PositiveTypePredicate(NegativeTypePredicate a) { 131 type = a.type; 132 if (a.nor != null) { 133 or = new PositiveTypePredicate(a.nor); 134 } 135 } 136 137 @Override 138 public boolean apply(Node n) { 139 return type.isInstance(n) || (or != null && or.apply(n)); 140 } 141 142 public PositiveTypePredicate or(Class<? extends Node> clazz) { 143 if (or == null) { 144 or = new PositiveTypePredicate(clazz); 145 } else { 146 or.or(clazz); 147 } 148 return this; 149 } 150 151 public NodePredicate negate() { 152 return new NegativeTypePredicate(this); 153 } 154 } 155 156 public static final class NegativeTypePredicate implements NodePredicate { 157 158 private final Class<?> type; 159 private NegativeTypePredicate nor; 160 161 NegativeTypePredicate(Class<?> type) { 162 this.type = type; 163 } 164 165 public NegativeTypePredicate(PositiveTypePredicate a) { 166 type = a.type; 167 if (a.or != null) { 168 nor = new NegativeTypePredicate(a.or); 169 } 170 } 171 172 @Override 173 public boolean apply(Node n) { 174 return !type.isInstance(n) && (nor == null || nor.apply(n)); 175 } 176 177 public NegativeTypePredicate nor(Class<? extends Node> clazz) { 178 if (nor == null) { 179 nor = new NegativeTypePredicate(clazz); 180 } else { 181 nor.nor(clazz); 182 } 183 return this; 184 } 185 186 public NodePredicate negate() { 187 return new PositiveTypePredicate(this); 188 } 189 } 190 }