Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:27

0001 //===--- APNumericStorage.h - Store APInt/APFloat in ASTContext -*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_AST_APNUMERICSTORAGE_H
0010 #define LLVM_CLANG_AST_APNUMERICSTORAGE_H
0011 
0012 #include "llvm/ADT/APFloat.h"
0013 #include "llvm/ADT/APInt.h"
0014 
0015 namespace clang {
0016 class ASTContext;
0017 
0018 /// Used by IntegerLiteral/FloatingLiteral/EnumConstantDecl to store the
0019 /// numeric without leaking memory.
0020 ///
0021 /// For large floats/integers, APFloat/APInt will allocate memory from the heap
0022 /// to represent these numbers.  Unfortunately, when we use a BumpPtrAllocator
0023 /// to allocate IntegerLiteral/FloatingLiteral nodes the memory associated with
0024 /// the APFloat/APInt values will never get freed. APNumericStorage uses
0025 /// ASTContext's allocator for memory allocation.
0026 class APNumericStorage {
0027   union {
0028     uint64_t VAL;   ///< Used to store the <= 64 bits integer value.
0029     uint64_t *pVal; ///< Used to store the >64 bits integer value.
0030   };
0031   unsigned BitWidth;
0032 
0033   bool hasAllocation() const { return llvm::APInt::getNumWords(BitWidth) > 1; }
0034 
0035   APNumericStorage(const APNumericStorage &) = delete;
0036   void operator=(const APNumericStorage &) = delete;
0037 
0038 protected:
0039   APNumericStorage() : VAL(0), BitWidth(0) {}
0040 
0041   llvm::APInt getIntValue() const {
0042     unsigned NumWords = llvm::APInt::getNumWords(BitWidth);
0043     if (NumWords > 1)
0044       return llvm::APInt(BitWidth, NumWords, pVal);
0045     else
0046       return llvm::APInt(BitWidth, VAL);
0047   }
0048   void setIntValue(const ASTContext &C, const llvm::APInt &Val);
0049 };
0050 
0051 class APIntStorage : private APNumericStorage {
0052 public:
0053   llvm::APInt getValue() const { return getIntValue(); }
0054   void setValue(const ASTContext &C, const llvm::APInt &Val) {
0055     setIntValue(C, Val);
0056   }
0057 };
0058 
0059 class APFloatStorage : private APNumericStorage {
0060 public:
0061   llvm::APFloat getValue(const llvm::fltSemantics &Semantics) const {
0062     return llvm::APFloat(Semantics, getIntValue());
0063   }
0064   void setValue(const ASTContext &C, const llvm::APFloat &Val) {
0065     setIntValue(C, Val.bitcastToAPInt());
0066   }
0067 };
0068 
0069 } // end namespace clang
0070 
0071 #endif // LLVM_CLANG_AST_APNUMERICSTORAGE_H