clang API Documentation
00001 //===-- CGBlocks.h - state for LLVM CodeGen for blocks ----------*- C++ -*-===// 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 // This is the internal state used for llvm translation for block literals. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef CLANG_CODEGEN_CGBLOCKS_H 00015 #define CLANG_CODEGEN_CGBLOCKS_H 00016 00017 #include "CodeGenTypes.h" 00018 #include "clang/AST/Type.h" 00019 #include "llvm/Module.h" 00020 #include "clang/Basic/TargetInfo.h" 00021 #include "clang/AST/CharUnits.h" 00022 #include "clang/AST/Expr.h" 00023 #include "clang/AST/ExprCXX.h" 00024 #include "clang/AST/ExprObjC.h" 00025 00026 #include "CodeGenFunction.h" 00027 #include "CGBuilder.h" 00028 #include "CGCall.h" 00029 #include "CGValue.h" 00030 00031 namespace llvm { 00032 class Module; 00033 class Constant; 00034 class Function; 00035 class GlobalValue; 00036 class TargetData; 00037 class FunctionType; 00038 class PointerType; 00039 class Value; 00040 class LLVMContext; 00041 } 00042 00043 namespace clang { 00044 00045 namespace CodeGen { 00046 00047 class CodeGenModule; 00048 class CGBlockInfo; 00049 00050 enum BlockFlag_t { 00051 BLOCK_HAS_COPY_DISPOSE = (1 << 25), 00052 BLOCK_HAS_CXX_OBJ = (1 << 26), 00053 BLOCK_IS_GLOBAL = (1 << 28), 00054 BLOCK_USE_STRET = (1 << 29), 00055 BLOCK_HAS_SIGNATURE = (1 << 30) 00056 }; 00057 class BlockFlags { 00058 uint32_t flags; 00059 00060 BlockFlags(uint32_t flags) : flags(flags) {} 00061 public: 00062 BlockFlags() : flags(0) {} 00063 BlockFlags(BlockFlag_t flag) : flags(flag) {} 00064 00065 uint32_t getBitMask() const { return flags; } 00066 bool empty() const { return flags == 0; } 00067 00068 friend BlockFlags operator|(BlockFlags l, BlockFlags r) { 00069 return BlockFlags(l.flags | r.flags); 00070 } 00071 friend BlockFlags &operator|=(BlockFlags &l, BlockFlags r) { 00072 l.flags |= r.flags; 00073 return l; 00074 } 00075 friend bool operator&(BlockFlags l, BlockFlags r) { 00076 return (l.flags & r.flags); 00077 } 00078 }; 00079 inline BlockFlags operator|(BlockFlag_t l, BlockFlag_t r) { 00080 return BlockFlags(l) | BlockFlags(r); 00081 } 00082 00083 enum BlockFieldFlag_t { 00084 BLOCK_FIELD_IS_OBJECT = 0x03, /* id, NSObject, __attribute__((NSObject)), 00085 block, ... */ 00086 BLOCK_FIELD_IS_BLOCK = 0x07, /* a block variable */ 00087 00088 BLOCK_FIELD_IS_BYREF = 0x08, /* the on stack structure holding the __block 00089 variable */ 00090 BLOCK_FIELD_IS_WEAK = 0x10, /* declared __weak, only used in byref copy 00091 helpers */ 00092 BLOCK_FIELD_IS_ARC = 0x40, /* field has ARC-specific semantics */ 00093 BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose 00094 support routines */ 00095 BLOCK_BYREF_CURRENT_MAX = 256 00096 }; 00097 00098 class BlockFieldFlags { 00099 uint32_t flags; 00100 00101 BlockFieldFlags(uint32_t flags) : flags(flags) {} 00102 public: 00103 BlockFieldFlags() : flags(0) {} 00104 BlockFieldFlags(BlockFieldFlag_t flag) : flags(flag) {} 00105 00106 uint32_t getBitMask() const { return flags; } 00107 bool empty() const { return flags == 0; } 00108 00109 /// Answers whether the flags indicate that this field is an object 00110 /// or block pointer that requires _Block_object_assign/dispose. 00111 bool isSpecialPointer() const { return flags & BLOCK_FIELD_IS_OBJECT; } 00112 00113 friend BlockFieldFlags operator|(BlockFieldFlags l, BlockFieldFlags r) { 00114 return BlockFieldFlags(l.flags | r.flags); 00115 } 00116 friend BlockFieldFlags &operator|=(BlockFieldFlags &l, BlockFieldFlags r) { 00117 l.flags |= r.flags; 00118 return l; 00119 } 00120 friend bool operator&(BlockFieldFlags l, BlockFieldFlags r) { 00121 return (l.flags & r.flags); 00122 } 00123 }; 00124 inline BlockFieldFlags operator|(BlockFieldFlag_t l, BlockFieldFlag_t r) { 00125 return BlockFieldFlags(l) | BlockFieldFlags(r); 00126 } 00127 00128 /// CGBlockInfo - Information to generate a block literal. 00129 class CGBlockInfo { 00130 public: 00131 /// Name - The name of the block, kindof. 00132 llvm::StringRef Name; 00133 00134 /// The field index of 'this' within the block, if there is one. 00135 unsigned CXXThisIndex; 00136 00137 class Capture { 00138 uintptr_t Data; 00139 EHScopeStack::stable_iterator Cleanup; 00140 00141 public: 00142 bool isIndex() const { return (Data & 1) != 0; } 00143 bool isConstant() const { return !isIndex(); } 00144 unsigned getIndex() const { assert(isIndex()); return Data >> 1; } 00145 llvm::Value *getConstant() const { 00146 assert(isConstant()); 00147 return reinterpret_cast<llvm::Value*>(Data); 00148 } 00149 EHScopeStack::stable_iterator getCleanup() const { 00150 assert(isIndex()); 00151 return Cleanup; 00152 } 00153 void setCleanup(EHScopeStack::stable_iterator cleanup) { 00154 assert(isIndex()); 00155 Cleanup = cleanup; 00156 } 00157 00158 static Capture makeIndex(unsigned index) { 00159 Capture v; 00160 v.Data = (index << 1) | 1; 00161 return v; 00162 } 00163 00164 static Capture makeConstant(llvm::Value *value) { 00165 Capture v; 00166 v.Data = reinterpret_cast<uintptr_t>(value); 00167 return v; 00168 } 00169 }; 00170 00171 /// CanBeGlobal - True if the block can be global, i.e. it has 00172 /// no non-constant captures. 00173 bool CanBeGlobal : 1; 00174 00175 /// True if the block needs a custom copy or dispose function. 00176 bool NeedsCopyDispose : 1; 00177 00178 /// HasCXXObject - True if the block's custom copy/dispose functions 00179 /// need to be run even in GC mode. 00180 bool HasCXXObject : 1; 00181 00182 /// UsesStret : True if the block uses an stret return. Mutable 00183 /// because it gets set later in the block-creation process. 00184 mutable bool UsesStret : 1; 00185 00186 /// The mapping of allocated indexes within the block. 00187 llvm::DenseMap<const VarDecl*, Capture> Captures; 00188 00189 llvm::AllocaInst *Address; 00190 llvm::StructType *StructureType; 00191 const BlockDecl *Block; 00192 const BlockExpr *BlockExpression; 00193 CharUnits BlockSize; 00194 CharUnits BlockAlign; 00195 00196 /// An instruction which dominates the full-expression that the 00197 /// block is inside. 00198 llvm::Instruction *DominatingIP; 00199 00200 /// The next block in the block-info chain. Invalid if this block 00201 /// info is not part of the CGF's block-info chain, which is true 00202 /// if it corresponds to a global block or a block whose expression 00203 /// has been encountered. 00204 CGBlockInfo *NextBlockInfo; 00205 00206 const Capture &getCapture(const VarDecl *var) const { 00207 return const_cast<CGBlockInfo*>(this)->getCapture(var); 00208 } 00209 Capture &getCapture(const VarDecl *var) { 00210 llvm::DenseMap<const VarDecl*, Capture>::iterator 00211 it = Captures.find(var); 00212 assert(it != Captures.end() && "no entry for variable!"); 00213 return it->second; 00214 } 00215 00216 const BlockDecl *getBlockDecl() const { return Block; } 00217 const BlockExpr *getBlockExpr() const { 00218 assert(BlockExpression); 00219 assert(BlockExpression->getBlockDecl() == Block); 00220 return BlockExpression; 00221 } 00222 00223 CGBlockInfo(const BlockDecl *blockDecl, llvm::StringRef Name); 00224 }; 00225 00226 } // end namespace CodeGen 00227 } // end namespace clang 00228 00229 #endif