Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===- ConstantInitFuture.h - "Future" constant initializers ----*- 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 // This class defines the ConstantInitFuture class.  This is split out
0010 // from ConstantInitBuilder.h in order to allow APIs to work with it
0011 // without having to include that entire header.  This is particularly
0012 // important because it is often useful to be able to default-construct
0013 // a future in, say, a default argument.
0014 //
0015 //===----------------------------------------------------------------------===//
0016 
0017 #ifndef LLVM_CLANG_CODEGEN_CONSTANTINITFUTURE_H
0018 #define LLVM_CLANG_CODEGEN_CONSTANTINITFUTURE_H
0019 
0020 #include "llvm/ADT/PointerUnion.h"
0021 #include "llvm/IR/Constant.h"
0022 
0023 // Forward-declare ConstantInitBuilderBase and give it a
0024 // PointerLikeTypeTraits specialization so that we can safely use it
0025 // in a PointerUnion below.
0026 namespace clang {
0027 namespace CodeGen {
0028 class ConstantInitBuilderBase;
0029 }
0030 }
0031 namespace llvm {
0032 template <>
0033 struct PointerLikeTypeTraits< ::clang::CodeGen::ConstantInitBuilderBase*> {
0034   using T = ::clang::CodeGen::ConstantInitBuilderBase*;
0035 
0036   static inline void *getAsVoidPointer(T p) { return p; }
0037   static inline T getFromVoidPointer(void *p) {return static_cast<T>(p);}
0038   static constexpr int NumLowBitsAvailable = 2;
0039 };
0040 }
0041 
0042 namespace clang {
0043 namespace CodeGen {
0044 
0045 /// A "future" for a completed constant initializer, which can be passed
0046 /// around independently of any sub-builders (but not the original parent).
0047 class ConstantInitFuture {
0048   using PairTy = llvm::PointerUnion<ConstantInitBuilderBase*, llvm::Constant*>;
0049 
0050   PairTy Data;
0051 
0052   friend class ConstantInitBuilderBase;
0053   explicit ConstantInitFuture(ConstantInitBuilderBase *builder);
0054 
0055 public:
0056   ConstantInitFuture() {}
0057 
0058   /// A future can be explicitly created from a fixed initializer.
0059   explicit ConstantInitFuture(llvm::Constant *initializer) : Data(initializer) {
0060     assert(initializer && "creating null future");
0061   }
0062 
0063   /// Is this future non-null?
0064   explicit operator bool() const { return bool(Data); }
0065 
0066   /// Return the type of the initializer.
0067   llvm::Type *getType() const;
0068 
0069   /// Abandon this initializer.
0070   void abandon();
0071 
0072   /// Install the initializer into a global variable.  This cannot
0073   /// be called multiple times.
0074   void installInGlobal(llvm::GlobalVariable *global);
0075 
0076   void *getOpaqueValue() const { return Data.getOpaqueValue(); }
0077   static ConstantInitFuture getFromOpaqueValue(void *value) {
0078     ConstantInitFuture result;
0079     result.Data = PairTy::getFromOpaqueValue(value);
0080     return result;
0081   }
0082   static constexpr int NumLowBitsAvailable =
0083       llvm::PointerLikeTypeTraits<PairTy>::NumLowBitsAvailable;
0084 };
0085 
0086 }  // end namespace CodeGen
0087 }  // end namespace clang
0088 
0089 namespace llvm {
0090 
0091 template <>
0092 struct PointerLikeTypeTraits< ::clang::CodeGen::ConstantInitFuture> {
0093   using T = ::clang::CodeGen::ConstantInitFuture;
0094 
0095   static inline void *getAsVoidPointer(T future) {
0096     return future.getOpaqueValue();
0097   }
0098   static inline T getFromVoidPointer(void *p) {
0099     return T::getFromOpaqueValue(p);
0100   }
0101   static constexpr int NumLowBitsAvailable = T::NumLowBitsAvailable;
0102 };
0103 
0104 } // end namespace llvm
0105 
0106 #endif