clang API Documentation

TypeOrdering.h
Go to the documentation of this file.
00001 //===-------------- TypeOrdering.h - Total ordering for types -------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 ///
00010 /// \file
00011 /// \brief Allows QualTypes to be sorted and hence used in maps and sets.
00012 ///
00013 /// Defines clang::QualTypeOrdering, a total ordering on clang::QualType,
00014 /// and hence enables QualType values to be sorted and to be used in
00015 /// std::maps, std::sets, llvm::DenseMaps, and llvm::DenseSets.
00016 ///
00017 //===----------------------------------------------------------------------===//
00018 
00019 #ifndef LLVM_CLANG_AST_TYPEORDERING_H
00020 #define LLVM_CLANG_AST_TYPEORDERING_H
00021 
00022 #include "clang/AST/CanonicalType.h"
00023 #include "clang/AST/Type.h"
00024 #include <functional>
00025 
00026 namespace clang {
00027 
00028 /// \brief Function object that provides a total ordering on QualType values.
00029 struct QualTypeOrdering : std::binary_function<QualType, QualType, bool> {
00030   bool operator()(QualType T1, QualType T2) const {
00031     return std::less<void*>()(T1.getAsOpaquePtr(), T2.getAsOpaquePtr());
00032   }
00033 };
00034 
00035 }
00036 
00037 namespace llvm {
00038   template<class> struct DenseMapInfo;
00039 
00040   template<> struct DenseMapInfo<clang::QualType> {
00041     static inline clang::QualType getEmptyKey() { return clang::QualType(); }
00042 
00043     static inline clang::QualType getTombstoneKey() {
00044       using clang::QualType;
00045       return QualType::getFromOpaquePtr(reinterpret_cast<clang::Type *>(-1));
00046     }
00047 
00048     static unsigned getHashValue(clang::QualType Val) {
00049       return (unsigned)((uintptr_t)Val.getAsOpaquePtr()) ^
00050             ((unsigned)((uintptr_t)Val.getAsOpaquePtr() >> 9));
00051     }
00052 
00053     static bool isEqual(clang::QualType LHS, clang::QualType RHS) {
00054       return LHS == RHS;
00055     }
00056   };
00057 
00058   template<> struct DenseMapInfo<clang::CanQualType> {
00059     static inline clang::CanQualType getEmptyKey() { 
00060       return clang::CanQualType(); 
00061     }
00062     
00063     static inline clang::CanQualType getTombstoneKey() {
00064       using clang::CanQualType;
00065       return CanQualType::getFromOpaquePtr(reinterpret_cast<clang::Type *>(-1));
00066     }
00067     
00068     static unsigned getHashValue(clang::CanQualType Val) {
00069       return (unsigned)((uintptr_t)Val.getAsOpaquePtr()) ^
00070       ((unsigned)((uintptr_t)Val.getAsOpaquePtr() >> 9));
00071     }
00072     
00073     static bool isEqual(clang::CanQualType LHS, clang::CanQualType RHS) {
00074       return LHS == RHS;
00075     }
00076   };
00077 }
00078 
00079 #endif