Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 1 | //===-- CGBlocks.h - state for LLVM CodeGen for blocks ----------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This is the internal state used for llvm translation for block literals. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef CLANG_CODEGEN_CGBLOCKS_H |
| 15 | #define CLANG_CODEGEN_CGBLOCKS_H |
| 16 | |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 17 | #include "CodeGenTypes.h" |
| 18 | #include "clang/AST/Type.h" |
| 19 | #include "llvm/ADT/DenseMap.h" |
| 20 | #include "llvm/ADT/SmallVector.h" |
| 21 | #include "clang/Basic/TargetInfo.h" |
| 22 | #include "clang/AST/Expr.h" |
| 23 | #include "clang/AST/ExprCXX.h" |
| 24 | #include "clang/AST/ExprObjC.h" |
| 25 | |
| 26 | #include <vector> |
| 27 | #include <map> |
| 28 | |
| 29 | #include "CGBuilder.h" |
| 30 | #include "CGCall.h" |
| 31 | #include "CGValue.h" |
| 32 | |
| 33 | namespace llvm { |
| 34 | class Module; |
| 35 | class Constant; |
| 36 | class Function; |
| 37 | class GlobalValue; |
| 38 | class TargetData; |
| 39 | class FunctionType; |
| 40 | class Value; |
| 41 | } |
| 42 | |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 43 | namespace clang { |
| 44 | |
| 45 | namespace CodeGen { |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 46 | class CodeGenModule; |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 47 | |
| 48 | class BlockBase { |
| 49 | public: |
| 50 | enum { |
| 51 | BLOCK_NEEDS_FREE = (1 << 24), |
| 52 | BLOCK_HAS_COPY_DISPOSE = (1 << 25), |
| 53 | BLOCK_HAS_CXX_OBJ = (1 << 26), |
| 54 | BLOCK_IS_GC = (1 << 27), |
| 55 | BLOCK_IS_GLOBAL = (1 << 28), |
| 56 | BLOCK_HAS_DESCRIPTOR = (1 << 29) |
| 57 | }; |
| 58 | }; |
| 59 | |
| 60 | class BlockModule : public BlockBase { |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 61 | ASTContext &Context; |
| 62 | llvm::Module &TheModule; |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 63 | const llvm::TargetData &TheTargetData; |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 64 | CodeGenTypes &Types; |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 65 | CodeGenModule &CGM; |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 66 | |
| 67 | ASTContext &getContext() const { return Context; } |
| 68 | llvm::Module &getModule() const { return TheModule; } |
| 69 | CodeGenTypes &getTypes() { return Types; } |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 70 | const llvm::TargetData &getTargetData() const { return TheTargetData; } |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 71 | public: |
| 72 | llvm::Constant *getNSConcreteGlobalBlock(); |
| 73 | llvm::Constant *getNSConcreteStackBlock(); |
| 74 | int getGlobalUniqueCount() { return ++Block.GlobalUniqueCount; } |
| 75 | const llvm::Type *getBlockDescriptorType(); |
| 76 | |
| 77 | const llvm::Type *getGenericBlockLiteralType(); |
| 78 | const llvm::Type *getGenericExtendedBlockLiteralType(); |
| 79 | |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 80 | llvm::Constant *GetAddrOfGlobalBlock(const BlockExpr *BE, const char *); |
| 81 | |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 82 | /// NSConcreteGlobalBlock - Cached reference to the class pointer for global |
| 83 | /// blocks. |
| 84 | llvm::Constant *NSConcreteGlobalBlock; |
| 85 | |
| 86 | /// NSConcreteStackBlock - Cached reference to the class poinnter for stack |
| 87 | /// blocks. |
| 88 | llvm::Constant *NSConcreteStackBlock; |
| 89 | |
| 90 | const llvm::Type *BlockDescriptorType; |
| 91 | const llvm::Type *GenericBlockLiteralType; |
| 92 | const llvm::Type *GenericExtendedBlockLiteralType; |
| 93 | struct { |
| 94 | int GlobalUniqueCount; |
| 95 | } Block; |
| 96 | |
Mike Stump | 90a9043 | 2009-03-04 18:47:42 +0000 | [diff] [blame] | 97 | BlockModule(ASTContext &C, llvm::Module &M, const llvm::TargetData &TD, |
| 98 | CodeGenTypes &T, CodeGenModule &CodeGen) |
| 99 | : Context(C), TheModule(M), TheTargetData(TD), Types(T), |
| 100 | CGM(CodeGen), |
| 101 | NSConcreteGlobalBlock(0), NSConcreteStackBlock(0), BlockDescriptorType(0), |
Mike Stump | 2a99814 | 2009-03-04 18:17:45 +0000 | [diff] [blame] | 102 | GenericBlockLiteralType(0) { |
| 103 | Block.GlobalUniqueCount = 0; |
| 104 | } |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | class BlockFunction : public BlockBase { |
| 108 | public: |
| 109 | enum { |
| 110 | BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)), |
| 111 | block, ... */ |
| 112 | BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */ |
| 113 | BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the __block |
| 114 | variable */ |
| 115 | BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy |
| 116 | helpers */ |
| 117 | BLOCK_BYREF_CALLER = 128 /* called from __block (byref) copy/dispose |
| 118 | support routines */ |
| 119 | }; |
Mike Stump | 3947de5 | 2009-03-04 18:57:26 +0000 | [diff] [blame] | 120 | |
Mike Stump | ad75ab4 | 2009-03-04 19:03:44 +0000 | [diff] [blame^] | 121 | /// BlockInfo - Information to generate a block literal. |
| 122 | struct BlockInfo { |
| 123 | /// BlockLiteralTy - The type of the block literal. |
| 124 | const llvm::Type *BlockLiteralTy; |
| 125 | |
| 126 | /// Name - the name of the function this block was created for, if any |
| 127 | const char *Name; |
| 128 | |
| 129 | /// ByCopyDeclRefs - Variables from parent scopes that have been imported |
| 130 | /// into this block. |
| 131 | llvm::SmallVector<const BlockDeclRefExpr *, 8> ByCopyDeclRefs; |
| 132 | |
| 133 | // ByRefDeclRefs - __block variables from parent scopes that have been |
| 134 | // imported into this block. |
| 135 | llvm::SmallVector<const BlockDeclRefExpr *, 8> ByRefDeclRefs; |
| 136 | |
| 137 | BlockInfo(const llvm::Type *blt, const char *n) |
| 138 | : BlockLiteralTy(blt), Name(n) {} |
| 139 | }; |
| 140 | |
Mike Stump | 3947de5 | 2009-03-04 18:57:26 +0000 | [diff] [blame] | 141 | CGBuilderTy &Builder; |
| 142 | |
| 143 | BlockFunction(CGBuilderTy &B) : Builder(B) { } |
| 144 | |
Mike Stump | ad75ab4 | 2009-03-04 19:03:44 +0000 | [diff] [blame^] | 145 | ImplicitParamDecl *BlockStructDecl; |
| 146 | ImplicitParamDecl *getBlockStructDecl() { return BlockStructDecl; } |
| 147 | |
Mike Stump | 3947de5 | 2009-03-04 18:57:26 +0000 | [diff] [blame] | 148 | llvm::Value *BuildCopyHelper(int flag); |
| 149 | llvm::Value *BuildDestroyHelper(int flag); |
| 150 | |
Mike Stump | d883d84 | 2009-03-04 15:35:22 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | } // end namespace CodeGen |
| 154 | } // end namespace clang |
| 155 | |
| 156 | #endif |