TypeOrdering.h revision 263508
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/// \file
11/// \brief Allows QualTypes to be sorted and hence used in maps and sets.
12///
13/// Defines clang::QualTypeOrdering, a total ordering on clang::QualType,
14/// and hence enables QualType values to be sorted and to be used in
15/// std::maps, std::sets, llvm::DenseMaps, and llvm::DenseSets.
16///
17//===----------------------------------------------------------------------===//
18
19#ifndef LLVM_CLANG_TYPE_ORDERING_H
20#define LLVM_CLANG_TYPE_ORDERING_H
21
22#include "clang/AST/CanonicalType.h"
23#include "clang/AST/Type.h"
24#include <functional>
25
26namespace clang {
27
28/// \brief Function object that provides a total ordering on QualType values.
29struct QualTypeOrdering : std::binary_function<QualType, QualType, bool> {
30  bool operator()(QualType T1, QualType T2) const {
31    return std::less<void*>()(T1.getAsOpaquePtr(), T2.getAsOpaquePtr());
32  }
33};
34
35}
36
37namespace llvm {
38  template<class> struct DenseMapInfo;
39
40  template<> struct DenseMapInfo<clang::QualType> {
41    static inline clang::QualType getEmptyKey() { return clang::QualType(); }
42
43    static inline clang::QualType getTombstoneKey() {
44      using clang::QualType;
45      return QualType::getFromOpaquePtr(reinterpret_cast<clang::Type *>(-1));
46    }
47
48    static unsigned getHashValue(clang::QualType Val) {
49      return (unsigned)((uintptr_t)Val.getAsOpaquePtr()) ^
50            ((unsigned)((uintptr_t)Val.getAsOpaquePtr() >> 9));
51    }
52
53    static bool isEqual(clang::QualType LHS, clang::QualType RHS) {
54      return LHS == RHS;
55    }
56  };
57
58  template<> struct DenseMapInfo<clang::CanQualType> {
59    static inline clang::CanQualType getEmptyKey() {
60      return clang::CanQualType();
61    }
62
63    static inline clang::CanQualType getTombstoneKey() {
64      using clang::CanQualType;
65      return CanQualType::getFromOpaquePtr(reinterpret_cast<clang::Type *>(-1));
66    }
67
68    static unsigned getHashValue(clang::CanQualType Val) {
69      return (unsigned)((uintptr_t)Val.getAsOpaquePtr()) ^
70      ((unsigned)((uintptr_t)Val.getAsOpaquePtr() >> 9));
71    }
72
73    static bool isEqual(clang::CanQualType LHS, clang::CanQualType RHS) {
74      return LHS == RHS;
75    }
76  };
77}
78
79#endif
80