1 //===- Types.cpp - Helper for the selection of C++ data types. ------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "Types.h" 10 11 // For LLVM_ATTRIBUTE_UNUSED 12 #include "llvm/Support/Compiler.h" 13 14 #include <cassert> 15 16 using namespace llvm; 17 18 const char *llvm::getMinimalTypeForRange(uint64_t Range, unsigned MaxSize LLVM_ATTRIBUTE_UNUSED) { 19 // TODO: The original callers only used 32 and 64 so these are the only 20 // values permitted. Rather than widen the supported values we should 21 // allow 64 for the callers that currently use 32 and remove the 22 // argument altogether. 23 assert((MaxSize == 32 || MaxSize == 64) && "Unexpected size"); 24 assert(MaxSize <= 64 && "Unexpected size"); 25 assert(((MaxSize > 32) ? Range <= 0xFFFFFFFFFFFFFFFFULL 26 : Range <= 0xFFFFFFFFULL) && 27 "Enum too large"); 28 29 if (Range > 0xFFFFFFFFULL) 30 return "uint64_t"; 31 if (Range > 0xFFFF) 32 return "uint32_t"; 33 if (Range > 0xFF) 34 return "uint16_t"; 35 return "uint8_t"; 36 } 37 38 const char *llvm::getMinimalTypeForEnumBitfield(uint64_t Size) { 39 uint64_t MaxIndex = Size; 40 if (MaxIndex > 0) 41 MaxIndex--; 42 assert(MaxIndex <= 64 && "Too many bits"); 43 return getMinimalTypeForRange(1ULL << MaxIndex); 44 } 45