| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | package com.sun.fortress.nodes_util; |
|---|
| 19 | |
|---|
| 20 | import java.util.Comparator; |
|---|
| 21 | import java.util.List; |
|---|
| 22 | import edu.rice.cs.plt.tuple.Option; |
|---|
| 23 | |
|---|
| 24 | import com.sun.fortress.nodes.*; |
|---|
| 25 | import com.sun.fortress.useful.AnyListComparer; |
|---|
| 26 | import com.sun.fortress.useful.ListComparer; |
|---|
| 27 | import com.sun.fortress.exceptions.InterpreterBug; |
|---|
| 28 | |
|---|
| 29 | public class NodeComparator { |
|---|
| 30 | |
|---|
| 31 | public static int compareOptionalType(Option<Type> a, |
|---|
| 32 | Option<Type> b) { |
|---|
| 33 | if (a.isSome() != b.isSome()) { |
|---|
| 34 | return a.isSome() ? 1 : -1; |
|---|
| 35 | } |
|---|
| 36 | if (a.isSome()) { |
|---|
| 37 | return compare(a.unwrap(), b.unwrap()); |
|---|
| 38 | } |
|---|
| 39 | return 0; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | public static int compareOptionalStaticArg(Option<StaticArg> a, |
|---|
| 43 | Option<StaticArg> b) { |
|---|
| 44 | if (a.isSome() != b.isSome()) { |
|---|
| 45 | return a.isSome() ? 1 : -1; |
|---|
| 46 | } |
|---|
| 47 | if (a.isSome()) { |
|---|
| 48 | return compare(a.unwrap(), b.unwrap()); |
|---|
| 49 | } |
|---|
| 50 | return 0; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | public static int compareOptionalOp(Option<Op> a, |
|---|
| 54 | Option<Op> b) { |
|---|
| 55 | if (a.isSome() != b.isSome()) { |
|---|
| 56 | return a.isSome() ? 1 : -1; |
|---|
| 57 | } |
|---|
| 58 | if (a.isSome()) { |
|---|
| 59 | return a.unwrap().getText().compareTo(b.unwrap().getText()); |
|---|
| 60 | } |
|---|
| 61 | return 0; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | static class ExtentRangeComparer implements Comparator<ExtentRange> { |
|---|
| 65 | public int compare(ExtentRange left, ExtentRange right) { |
|---|
| 66 | return NodeComparator.compare(left, right); |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | final static ExtentRangeComparer extentRangeComparer = new ExtentRangeComparer(); |
|---|
| 70 | public final static AnyListComparer<ExtentRange> extentRangeListComparer = |
|---|
| 71 | new AnyListComparer<ExtentRange>(extentRangeComparer); |
|---|
| 72 | |
|---|
| 73 | static class KeywordTypeComparer implements Comparator<KeywordType> { |
|---|
| 74 | public int compare(KeywordType left, KeywordType right) { |
|---|
| 75 | return NodeComparator.compare(left, right); |
|---|
| 76 | } |
|---|
| 77 | } |
|---|
| 78 | final static KeywordTypeComparer keywordTypeComparer = new KeywordTypeComparer(); |
|---|
| 79 | public final static AnyListComparer<KeywordType> keywordTypeListComparer = |
|---|
| 80 | new AnyListComparer<KeywordType>(keywordTypeComparer); |
|---|
| 81 | |
|---|
| 82 | static class ParamComparer implements Comparator<Param> { |
|---|
| 83 | public int compare(Param left, Param right) { |
|---|
| 84 | return NodeComparator.compare(left, right); |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | final static ParamComparer paramComparer = new ParamComparer(); |
|---|
| 88 | public final static AnyListComparer<Param> paramListComparer = |
|---|
| 89 | new AnyListComparer<Param>(paramComparer); |
|---|
| 90 | |
|---|
| 91 | static class StaticParamComparer implements Comparator<StaticParam> { |
|---|
| 92 | public int compare(StaticParam left, StaticParam right) { |
|---|
| 93 | Class<? extends StaticParam> tclass = left.getClass(); |
|---|
| 94 | Class<? extends StaticParam> oclass = right.getClass(); |
|---|
| 95 | if (oclass != tclass) { |
|---|
| 96 | return tclass.getName().compareTo(oclass.getName()); |
|---|
| 97 | } |
|---|
| 98 | return NodeComparator.compare(left, right); |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | public final static StaticParamComparer staticParamComparer = |
|---|
| 102 | new StaticParamComparer(); |
|---|
| 103 | public static AnyListComparer<StaticParam> staticParamListComparer = |
|---|
| 104 | new AnyListComparer<StaticParam>(staticParamComparer); |
|---|
| 105 | |
|---|
| 106 | static class StaticArgComparer implements Comparator<StaticArg> { |
|---|
| 107 | public int compare(StaticArg left, StaticArg right) { |
|---|
| 108 | return NodeComparator.compare(left, right); |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | final static StaticArgComparer staticArgComparer = new StaticArgComparer(); |
|---|
| 112 | public final static AnyListComparer<StaticArg> staticArgListComparer = |
|---|
| 113 | new AnyListComparer<StaticArg>(staticArgComparer); |
|---|
| 114 | |
|---|
| 115 | static class BaseTypeComparer implements Comparator<BaseType> { |
|---|
| 116 | public int compare(BaseType left, BaseType right) { |
|---|
| 117 | return NodeComparator.compare(left, right); |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | final static BaseTypeComparer traitTypeComparer = new BaseTypeComparer(); |
|---|
| 121 | public final static AnyListComparer<BaseType> traitTypeListComparer = |
|---|
| 122 | new AnyListComparer<BaseType>(traitTypeComparer); |
|---|
| 123 | |
|---|
| 124 | static class TypeComparer implements Comparator<Type> { |
|---|
| 125 | public int compare(Type left, Type right) { |
|---|
| 126 | return NodeComparator.compare(left, right); |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | final static TypeComparer typeComparer = new TypeComparer(); |
|---|
| 130 | public final static AnyListComparer<Type> typeListComparer = |
|---|
| 131 | new AnyListComparer<Type>(typeComparer); |
|---|
| 132 | |
|---|
| 133 | static class IdOrOpComparer implements Comparator<IdOrOp> { |
|---|
| 134 | public int compare(IdOrOp left, IdOrOp right) { |
|---|
| 135 | return NodeComparator.compare(left, right); |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | public final static IdOrOpComparer idOrOpComparer = new IdOrOpComparer(); |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | public static int compare(List<StaticParam> left, List<StaticParam> right) { |
|---|
| 142 | return staticParamListComparer.compare(left, right); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | public static final Comparator<APIName> apiNameComparer = new Comparator<APIName>() { |
|---|
| 146 | |
|---|
| 147 | public int compare(APIName o1, APIName o2) { |
|---|
| 148 | return NodeComparator.compare(o1, o2); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | }; |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | public static int compare(APIName left, APIName right) { |
|---|
| 155 | return ListComparer.stringListComparer.compare(NodeUtil.toStrings(left), |
|---|
| 156 | NodeUtil.toStrings(right)); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | public static int compare(Id left, Id right) { |
|---|
| 160 | return NodeUtil.nameString(left).compareTo(NodeUtil.nameString(right)); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | public static int compare(ExtentRange left, ExtentRange right) { |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | int x = compareOptionalStaticArg(left.getBase(), right.getBase()); |
|---|
| 167 | if (x != 0) return x; |
|---|
| 168 | x = compareOptionalStaticArg(left.getSize(), right.getSize()); |
|---|
| 169 | if (x != 0) return x; |
|---|
| 170 | x = compareOptionalOp(left.getOp(), right.getOp()); |
|---|
| 171 | return 0; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | public static int compare(FnDecl left, FnDecl right) { |
|---|
| 175 | IdOrOpOrAnonymousName fn0 = NodeUtil.getName(left); |
|---|
| 176 | IdOrOpOrAnonymousName fn1 = NodeUtil.getName(right); |
|---|
| 177 | int x = NodeComparator.compare(fn0, fn1); |
|---|
| 178 | if (x != 0) return x; |
|---|
| 179 | x = compare(NodeUtil.getStaticParams(left), NodeUtil.getStaticParams(right)); |
|---|
| 180 | if (x != 0) return x; |
|---|
| 181 | x = paramListComparer.compare(NodeUtil.getParams(left), NodeUtil.getParams(right)); |
|---|
| 182 | return x; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | static class FnDeclComparer implements Comparator<FnDecl> { |
|---|
| 186 | public int compare(FnDecl left, FnDecl right) { |
|---|
| 187 | return NodeComparator.compare(left, right); |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | public final static FnDeclComparer fnAbsDeclOrDeclComparer = new FnDeclComparer(); |
|---|
| 192 | |
|---|
| 193 | public static int compare(IdOrOpOrAnonymousName left, IdOrOpOrAnonymousName right) { |
|---|
| 194 | Class<? extends IdOrOpOrAnonymousName> leftClass = left.getClass(); |
|---|
| 195 | Class<? extends IdOrOpOrAnonymousName> rightClass = right.getClass(); |
|---|
| 196 | |
|---|
| 197 | if (leftClass != rightClass) { |
|---|
| 198 | return leftClass.getName().compareTo(rightClass.getName()); |
|---|
| 199 | } |
|---|
| 200 | else { |
|---|
| 201 | return left.stringName().compareTo(right.stringName()); |
|---|
| 202 | } |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | public static int compare(KeywordType left, KeywordType right) { |
|---|
| 206 | return compare(left.getName(), right.getName(), |
|---|
| 207 | left.getKeywordType(), right.getKeywordType()); |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | public static int compare(Param left, Param right) { |
|---|
| 211 | int x = NodeUtil.nameString(left.getName()) |
|---|
| 212 | .compareTo(NodeUtil.nameString(right.getName())); |
|---|
| 213 | if (x != 0) return x; |
|---|
| 214 | if ( ! NodeUtil.isVarargsParam(left) && ! NodeUtil.isVarargsParam(right)) { |
|---|
| 215 | x = compareOptionalType(left.getIdType(), right.getIdType()); |
|---|
| 216 | } |
|---|
| 217 | if ( NodeUtil.isVarargsParam(left) && NodeUtil.isVarargsParam(right)) { |
|---|
| 218 | x = compareOptionalType(left.getVarargsType(), right.getVarargsType()); |
|---|
| 219 | } |
|---|
| 220 | if (x != 0) return x; |
|---|
| 221 | |
|---|
| 222 | return x; |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | public static int compare(StaticArg left, StaticArg right) { |
|---|
| 226 | Class<? extends StaticArg> leftClass = left.getClass(); |
|---|
| 227 | Class<? extends StaticArg> rightClass = right.getClass(); |
|---|
| 228 | |
|---|
| 229 | if (leftClass != rightClass) { |
|---|
| 230 | return leftClass.getName().compareTo(rightClass.getName()); |
|---|
| 231 | } |
|---|
| 232 | else { |
|---|
| 233 | return subtypeCompareTo(left, right); |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | public static int compare(Type left, Type right) { |
|---|
| 238 | Class<? extends Type> leftClass = left.getClass(); |
|---|
| 239 | Class<? extends Type> rightClass = right.getClass(); |
|---|
| 240 | |
|---|
| 241 | if (leftClass != rightClass) { |
|---|
| 242 | return leftClass.getName().compareTo(rightClass.getName()); |
|---|
| 243 | } |
|---|
| 244 | else { |
|---|
| 245 | return subtypeCompareTo(left, right); |
|---|
| 246 | } |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | public static int compare(Id a, Id b, Type c, Type d) { |
|---|
| 251 | int x = compare(a, b); |
|---|
| 252 | if (x != 0) return x; |
|---|
| 253 | return compare(c, d); |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | public static int compare(Type a, Type b, ExtentRange c, ExtentRange d) { |
|---|
| 257 | int x = compare(a, b); |
|---|
| 258 | if (x != 0) return x; |
|---|
| 259 | |
|---|
| 260 | return compare(c, d); |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | public static int compare(Type a, Type b, Indices c, Indices d) { |
|---|
| 264 | int x = compare(a, b); |
|---|
| 265 | if (x != 0) return x; |
|---|
| 266 | |
|---|
| 267 | return compare(c, d); |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | public static int compare(Type a, Type b, Type c, Type d) { |
|---|
| 271 | int x = compare(a, b); |
|---|
| 272 | if (x != 0) return x; |
|---|
| 273 | return compare(c, d); |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | static int compare(ArrayType left, ArrayType right) { |
|---|
| 278 | return compare(left.getElemType(), right.getElemType(), |
|---|
| 279 | left.getIndices(), right.getIndices()); |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | static int compare(ArrowType left, ArrowType right) { |
|---|
| 283 | int x = compare(left.getRange(), right.getRange()); |
|---|
| 284 | if (x != 0) return x; |
|---|
| 285 | x = compare(left.getDomain(), right.getDomain()); |
|---|
| 286 | if (x != 0) return x; |
|---|
| 287 | return compare(left.getEffect(), right.getEffect()); |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | static int compare(TupleType left, TupleType right) { |
|---|
| 291 | int x = typeListComparer.compare(left.getElements(), right.getElements()); |
|---|
| 292 | if (x != 0) return x; |
|---|
| 293 | x = compareOptionalType(left.getVarargs(), right.getVarargs()); |
|---|
| 294 | if (x != 0) return x; |
|---|
| 295 | return keywordTypeListComparer.compare(left.getKeywords(), right.getKeywords()); |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | static int compare(Effect left, Effect right) { |
|---|
| 299 | if (left.isIoEffect() != right.isIoEffect()) |
|---|
| 300 | return left.isIoEffect() ? 1 : -1; |
|---|
| 301 | if (left.getThrowsClause().isSome() != right.getThrowsClause().isSome()) |
|---|
| 302 | return left.getThrowsClause().isSome() ? 1 : -1; |
|---|
| 303 | if (left.getThrowsClause().isSome()) { |
|---|
| 304 | return typeListComparer.compare(left.getThrowsClause().unwrap(), |
|---|
| 305 | right.getThrowsClause().unwrap()); |
|---|
| 306 | } |
|---|
| 307 | else return 0; |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | static int compare(IntArg left, IntArg right) { |
|---|
| 311 | return subtypeCompareTo(left.getIntVal(), left.getIntVal()); |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | static int subtypeCompareTo(IntExpr left, IntExpr right) { |
|---|
| 315 | if (left instanceof IntBase && right instanceof IntBase) |
|---|
| 316 | return ((IntBase)left).getIntVal().getIntVal().intValue() - |
|---|
| 317 | ((IntBase)right).getIntVal().getIntVal().intValue(); |
|---|
| 318 | |
|---|
| 319 | else return 0; |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | public static int compare(Indices left, Indices right) { |
|---|
| 323 | return extentRangeListComparer |
|---|
| 324 | .compare(left.getExtents(), right.getExtents()); |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | static int compare(VarType left, VarType right) { |
|---|
| 328 | return compare(left.getName(), right.getName()); |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | static int subtypeCompareTo(Indices left, Indices right) { |
|---|
| 332 | throw new InterpreterBug(left, |
|---|
| 333 | "subtypeCompareTo(" + left.getClass() + " " + |
|---|
| 334 | right.getClass() + ") is not implemented!"); |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | static int compare(MatrixType left, MatrixType right) { |
|---|
| 338 | int y = compare(left.getElemType(), right.getElemType()); |
|---|
| 339 | if (y != 0) return y; |
|---|
| 340 | return extentRangeListComparer.compare(left.getDimensions(), |
|---|
| 341 | right.getDimensions()); |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | static int compare(OpArg left, OpArg right) { |
|---|
| 345 | return compare(left.getName().getOriginalName(), right.getName().getOriginalName()); |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | static int compare(TraitType left, TraitType right) { |
|---|
| 349 | int c = compare(left.getName(), right.getName()); |
|---|
| 350 | if (c != 0) return c; |
|---|
| 351 | return staticArgListComparer.compare(left.getArgs(), |
|---|
| 352 | right.getArgs()); |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | static int compare(TaggedDimType left, TaggedDimType right) { |
|---|
| 356 | throw new InterpreterBug(left, |
|---|
| 357 | "subtypeCompareTo(" + left.getClass() + " " + |
|---|
| 358 | right.getClass() + ") is not implemented!"); |
|---|
| 359 | } |
|---|
| 360 | |
|---|
| 361 | static int compare(TaggedUnitType left, TaggedUnitType right) { |
|---|
| 362 | throw new InterpreterBug(left, |
|---|
| 363 | "subtypeCompareTo(" + left.getClass() + " " + |
|---|
| 364 | right.getClass() + ") is not implemented!"); |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | static int compare(StaticParam left, StaticParam right) { |
|---|
| 368 | return NodeUtil.getName(left).compareTo(NodeUtil.getName(right)); |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | static int compare(TypeArg left, TypeArg right) { |
|---|
| 372 | return compare(left.getTypeArg(), right.getTypeArg()); |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | private static int subtypeCompareTo(StaticArg left, StaticArg right) { |
|---|
| 376 | if (left instanceof BoolArg) { |
|---|
| 377 | |
|---|
| 378 | } else if (left instanceof DimArg) { |
|---|
| 379 | |
|---|
| 380 | } else if (left instanceof IntArg) { |
|---|
| 381 | return compare((IntArg) left, (IntArg) right); |
|---|
| 382 | } else if (left instanceof OpArg) { |
|---|
| 383 | return compare((OpArg) left, (OpArg) right); |
|---|
| 384 | } else if (left instanceof TypeArg) { |
|---|
| 385 | return compare((TypeArg) left, (TypeArg) right); |
|---|
| 386 | } else if (left instanceof UnitArg) { |
|---|
| 387 | |
|---|
| 388 | } else { |
|---|
| 389 | |
|---|
| 390 | } |
|---|
| 391 | throw new InterpreterBug(left, |
|---|
| 392 | "subtypeCompareTo(" + left.getClass() + " " + |
|---|
| 393 | right.getClass() + ") is not implemented!"); |
|---|
| 394 | } |
|---|
| 395 | |
|---|
| 396 | private static int subtypeCompareTo(Type left, Type right) { |
|---|
| 397 | |
|---|
| 398 | |
|---|
| 399 | |
|---|
| 400 | if (left instanceof ArrowType) { |
|---|
| 401 | return compare((ArrowType) left, (ArrowType) right); |
|---|
| 402 | } else if (left instanceof TupleType) { |
|---|
| 403 | if ( NodeUtil.isVoidType((TupleType)left) ) |
|---|
| 404 | return 0; |
|---|
| 405 | else return compare((TupleType) left, (TupleType) right); |
|---|
| 406 | } else if (left instanceof TaggedDimType) { |
|---|
| 407 | return compare((TaggedDimType) left, (TaggedDimType) right); |
|---|
| 408 | } else if (left instanceof TaggedUnitType) { |
|---|
| 409 | return compare((TaggedUnitType) left, (TaggedUnitType) right); |
|---|
| 410 | } else if (left instanceof ArrayType) { |
|---|
| 411 | return compare((ArrayType) left, (ArrayType) right); |
|---|
| 412 | } else if (left instanceof VarType) { |
|---|
| 413 | return compare((VarType) left, (VarType) right); |
|---|
| 414 | } else if (left instanceof TraitType) { |
|---|
| 415 | return compare((TraitType) left, (TraitType) right); |
|---|
| 416 | } else if (left instanceof MatrixType) { |
|---|
| 417 | return compare((MatrixType) left, (MatrixType) right); |
|---|
| 418 | } else if (left instanceof AnyType) { |
|---|
| 419 | return 0; |
|---|
| 420 | } else { |
|---|
| 421 | |
|---|
| 422 | } |
|---|
| 423 | throw new InterpreterBug(left, |
|---|
| 424 | "subtypeCompareTo(" + left.getClass() + " " + |
|---|
| 425 | right.getClass() + ") is not implemented!"); |
|---|
| 426 | } |
|---|
| 427 | } |
|---|