TypeOrdering.h revision f5586f6b311c98e1022a8fe0609053849b70d323
1//===-------------- TypeOrdering.h - Total ordering for types -------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file provides a function objects and specializations that
11//  allow QualType values to be sorted, used in std::maps, std::sets,
12//  llvm::DenseMaps, and llvm::DenseSets.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_CLANG_TYPE_ORDERING_H
17#define LLVM_CLANG_TYPE_ORDERING_H
18
19#include "clang/AST/Type.h"
20#include "clang/AST/CanonicalType.h"
21#include <functional>
22
23namespace clang {
24
25/// QualTypeOrdering - Function object that provides a total ordering
26/// on QualType values.
27struct QualTypeOrdering : std::binary_function<QualType, QualType, bool> {
28  bool operator()(QualType T1, QualType T2) const {
29    return std::less<void*>()(T1.getAsOpaquePtr(), T2.getAsOpaquePtr());
30  }
31};
32
33}
34
35namespace llvm {
36  template<class> struct DenseMapInfo;
37
38  template<> struct DenseMapInfo<clang::QualType> {
39    static inline clang::QualType getEmptyKey() { return clang::QualType(); }
40
41    static inline clang::QualType getTombstoneKey() {
42      using clang::QualType;
43      return QualType::getFromOpaquePtr(reinterpret_cast<clang::Type *>(-1));
44    }
45
46    static unsigned getHashValue(clang::QualType Val) {
47      return (unsigned)((uintptr_t)Val.getAsOpaquePtr()) ^
48            ((unsigned)((uintptr_t)Val.getAsOpaquePtr() >> 9));
49    }
50
51    static bool isEqual(clang::QualType LHS, clang::QualType RHS) {
52      return LHS == RHS;
53    }
54  };
55
56  template<> struct DenseMapInfo<clang::CanQualType> {
57    static inline clang::CanQualType getEmptyKey() {
58      return clang::CanQualType();
59    }
60
61    static inline clang::CanQualType getTombstoneKey() {
62      using clang::CanQualType;
63      return CanQualType::getFromOpaquePtr(reinterpret_cast<clang::Type *>(-1));
64    }
65
66    static unsigned getHashValue(clang::CanQualType Val) {
67      return (unsigned)((uintptr_t)Val.getAsOpaquePtr()) ^
68      ((unsigned)((uintptr_t)Val.getAsOpaquePtr() >> 9));
69    }
70
71    static bool isEqual(clang::CanQualType LHS, clang::CanQualType RHS) {
72      return LHS == RHS;
73    }
74  };
75}
76
77#endif
78