类型相等与类型区分

以下算法(伪代码)确定两种类型是否相等而不是 不同 类型。 为简洁起见,省略了辅助集 s 的循环检查:

  1. proc typeEqualsOrDistinct(a, b: PType): bool =
  2. if a.kind == b.kind:
  3. case a.kind
  4. of int, intXX, float, floatXX, char, string, cstring, pointer,
  5. bool, nil, void:
  6. # leaf type: kinds identical; nothing more to check
  7. result = true
  8. of ref, ptr, var, set, seq, openarray:
  9. result = typeEqualsOrDistinct(a.baseType, b.baseType)
  10. of range:
  11. result = typeEqualsOrDistinct(a.baseType, b.baseType) and
  12. (a.rangeA == b.rangeA) and (a.rangeB == b.rangeB)
  13. of array:
  14. result = typeEqualsOrDistinct(a.baseType, b.baseType) and
  15. typeEqualsOrDistinct(a.indexType, b.indexType)
  16. of tuple:
  17. if a.tupleLen == b.tupleLen:
  18. for i in 0..a.tupleLen-1:
  19. if not typeEqualsOrDistinct(a[i], b[i]): return false
  20. result = true
  21. of distinct:
  22. result = typeEqualsOrDistinct(a.baseType, b.baseType)
  23. of object, enum:
  24. result = a == b
  25. of proc:
  26. result = typeEqualsOrDistinct(a.parameterTuple, b.parameterTuple) and
  27. typeEqualsOrDistinct(a.resultType, b.resultType) and
  28. a.callingConvention == b.callingConvention
  29. elif a.kind == distinct:
  30. result = typeEqualsOrDistinct(a.baseType, b)
  31. elif b.kind == distinct:
  32. result = typeEqualsOrDistinct(a, b.baseType)