xref: /minix3/external/bsd/llvm/dist/clang/lib/CodeGen/CodeGenFunction.h (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===-- CodeGenFunction.h - Per-Function state for LLVM CodeGen -*- C++ -*-===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This is the internal per-function state used for llvm translation.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14*0a6a1f1dSLionel Sambuc #ifndef LLVM_CLANG_LIB_CODEGEN_CODEGENFUNCTION_H
15*0a6a1f1dSLionel Sambuc #define LLVM_CLANG_LIB_CODEGEN_CODEGENFUNCTION_H
16f4a2713aSLionel Sambuc 
17f4a2713aSLionel Sambuc #include "CGBuilder.h"
18f4a2713aSLionel Sambuc #include "CGDebugInfo.h"
19*0a6a1f1dSLionel Sambuc #include "CGLoopInfo.h"
20f4a2713aSLionel Sambuc #include "CGValue.h"
21f4a2713aSLionel Sambuc #include "CodeGenModule.h"
22*0a6a1f1dSLionel Sambuc #include "CodeGenPGO.h"
23*0a6a1f1dSLionel Sambuc #include "EHScopeStack.h"
24f4a2713aSLionel Sambuc #include "clang/AST/CharUnits.h"
25f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h"
26f4a2713aSLionel Sambuc #include "clang/AST/ExprObjC.h"
27f4a2713aSLionel Sambuc #include "clang/AST/Type.h"
28f4a2713aSLionel Sambuc #include "clang/Basic/ABI.h"
29f4a2713aSLionel Sambuc #include "clang/Basic/CapturedStmt.h"
30f4a2713aSLionel Sambuc #include "clang/Basic/TargetInfo.h"
31f4a2713aSLionel Sambuc #include "clang/Frontend/CodeGenOptions.h"
32f4a2713aSLionel Sambuc #include "llvm/ADT/ArrayRef.h"
33f4a2713aSLionel Sambuc #include "llvm/ADT/DenseMap.h"
34f4a2713aSLionel Sambuc #include "llvm/ADT/SmallVector.h"
35*0a6a1f1dSLionel Sambuc #include "llvm/IR/ValueHandle.h"
36f4a2713aSLionel Sambuc #include "llvm/Support/Debug.h"
37f4a2713aSLionel Sambuc 
38f4a2713aSLionel Sambuc namespace llvm {
39f4a2713aSLionel Sambuc class BasicBlock;
40f4a2713aSLionel Sambuc class LLVMContext;
41f4a2713aSLionel Sambuc class MDNode;
42f4a2713aSLionel Sambuc class Module;
43f4a2713aSLionel Sambuc class SwitchInst;
44f4a2713aSLionel Sambuc class Twine;
45f4a2713aSLionel Sambuc class Value;
46f4a2713aSLionel Sambuc class CallSite;
47f4a2713aSLionel Sambuc }
48f4a2713aSLionel Sambuc 
49f4a2713aSLionel Sambuc namespace clang {
50f4a2713aSLionel Sambuc class ASTContext;
51f4a2713aSLionel Sambuc class BlockDecl;
52f4a2713aSLionel Sambuc class CXXDestructorDecl;
53f4a2713aSLionel Sambuc class CXXForRangeStmt;
54f4a2713aSLionel Sambuc class CXXTryStmt;
55f4a2713aSLionel Sambuc class Decl;
56f4a2713aSLionel Sambuc class LabelDecl;
57f4a2713aSLionel Sambuc class EnumConstantDecl;
58f4a2713aSLionel Sambuc class FunctionDecl;
59f4a2713aSLionel Sambuc class FunctionProtoType;
60f4a2713aSLionel Sambuc class LabelStmt;
61f4a2713aSLionel Sambuc class ObjCContainerDecl;
62f4a2713aSLionel Sambuc class ObjCInterfaceDecl;
63f4a2713aSLionel Sambuc class ObjCIvarDecl;
64f4a2713aSLionel Sambuc class ObjCMethodDecl;
65f4a2713aSLionel Sambuc class ObjCImplementationDecl;
66f4a2713aSLionel Sambuc class ObjCPropertyImplDecl;
67f4a2713aSLionel Sambuc class TargetInfo;
68f4a2713aSLionel Sambuc class TargetCodeGenInfo;
69f4a2713aSLionel Sambuc class VarDecl;
70f4a2713aSLionel Sambuc class ObjCForCollectionStmt;
71f4a2713aSLionel Sambuc class ObjCAtTryStmt;
72f4a2713aSLionel Sambuc class ObjCAtThrowStmt;
73f4a2713aSLionel Sambuc class ObjCAtSynchronizedStmt;
74f4a2713aSLionel Sambuc class ObjCAutoreleasePoolStmt;
75f4a2713aSLionel Sambuc 
76f4a2713aSLionel Sambuc namespace CodeGen {
77f4a2713aSLionel Sambuc class CodeGenTypes;
78f4a2713aSLionel Sambuc class CGFunctionInfo;
79f4a2713aSLionel Sambuc class CGRecordLayout;
80f4a2713aSLionel Sambuc class CGBlockInfo;
81f4a2713aSLionel Sambuc class CGCXXABI;
82f4a2713aSLionel Sambuc class BlockFlags;
83f4a2713aSLionel Sambuc class BlockFieldFlags;
84f4a2713aSLionel Sambuc 
85f4a2713aSLionel Sambuc /// The kind of evaluation to perform on values of a particular
86f4a2713aSLionel Sambuc /// type.  Basically, is the code in CGExprScalar, CGExprComplex, or
87f4a2713aSLionel Sambuc /// CGExprAgg?
88f4a2713aSLionel Sambuc ///
89f4a2713aSLionel Sambuc /// TODO: should vectors maybe be split out into their own thing?
90f4a2713aSLionel Sambuc enum TypeEvaluationKind {
91f4a2713aSLionel Sambuc   TEK_Scalar,
92f4a2713aSLionel Sambuc   TEK_Complex,
93f4a2713aSLionel Sambuc   TEK_Aggregate
94f4a2713aSLionel Sambuc };
95f4a2713aSLionel Sambuc 
96f4a2713aSLionel Sambuc /// CodeGenFunction - This class organizes the per-function state that is used
97f4a2713aSLionel Sambuc /// while generating LLVM code.
98f4a2713aSLionel Sambuc class CodeGenFunction : public CodeGenTypeCache {
99f4a2713aSLionel Sambuc   CodeGenFunction(const CodeGenFunction &) LLVM_DELETED_FUNCTION;
100f4a2713aSLionel Sambuc   void operator=(const CodeGenFunction &) LLVM_DELETED_FUNCTION;
101f4a2713aSLionel Sambuc 
102f4a2713aSLionel Sambuc   friend class CGCXXABI;
103f4a2713aSLionel Sambuc public:
104f4a2713aSLionel Sambuc   /// A jump destination is an abstract label, branching to which may
105f4a2713aSLionel Sambuc   /// require a jump out through normal cleanups.
106f4a2713aSLionel Sambuc   struct JumpDest {
JumpDestJumpDest107*0a6a1f1dSLionel Sambuc     JumpDest() : Block(nullptr), ScopeDepth(), Index(0) {}
JumpDestJumpDest108f4a2713aSLionel Sambuc     JumpDest(llvm::BasicBlock *Block,
109f4a2713aSLionel Sambuc              EHScopeStack::stable_iterator Depth,
110f4a2713aSLionel Sambuc              unsigned Index)
111f4a2713aSLionel Sambuc       : Block(Block), ScopeDepth(Depth), Index(Index) {}
112f4a2713aSLionel Sambuc 
isValidJumpDest113*0a6a1f1dSLionel Sambuc     bool isValid() const { return Block != nullptr; }
getBlockJumpDest114f4a2713aSLionel Sambuc     llvm::BasicBlock *getBlock() const { return Block; }
getScopeDepthJumpDest115f4a2713aSLionel Sambuc     EHScopeStack::stable_iterator getScopeDepth() const { return ScopeDepth; }
getDestIndexJumpDest116f4a2713aSLionel Sambuc     unsigned getDestIndex() const { return Index; }
117f4a2713aSLionel Sambuc 
118f4a2713aSLionel Sambuc     // This should be used cautiously.
setScopeDepthJumpDest119f4a2713aSLionel Sambuc     void setScopeDepth(EHScopeStack::stable_iterator depth) {
120f4a2713aSLionel Sambuc       ScopeDepth = depth;
121f4a2713aSLionel Sambuc     }
122f4a2713aSLionel Sambuc 
123f4a2713aSLionel Sambuc   private:
124f4a2713aSLionel Sambuc     llvm::BasicBlock *Block;
125f4a2713aSLionel Sambuc     EHScopeStack::stable_iterator ScopeDepth;
126f4a2713aSLionel Sambuc     unsigned Index;
127f4a2713aSLionel Sambuc   };
128f4a2713aSLionel Sambuc 
129f4a2713aSLionel Sambuc   CodeGenModule &CGM;  // Per-module state.
130f4a2713aSLionel Sambuc   const TargetInfo &Target;
131f4a2713aSLionel Sambuc 
132f4a2713aSLionel Sambuc   typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
133*0a6a1f1dSLionel Sambuc   LoopInfoStack LoopStack;
134f4a2713aSLionel Sambuc   CGBuilderTy Builder;
135f4a2713aSLionel Sambuc 
136*0a6a1f1dSLionel Sambuc   /// \brief CGBuilder insert helper. This function is called after an
137*0a6a1f1dSLionel Sambuc   /// instruction is created using Builder.
138*0a6a1f1dSLionel Sambuc   void InsertHelper(llvm::Instruction *I, const llvm::Twine &Name,
139*0a6a1f1dSLionel Sambuc                     llvm::BasicBlock *BB,
140*0a6a1f1dSLionel Sambuc                     llvm::BasicBlock::iterator InsertPt) const;
141*0a6a1f1dSLionel Sambuc 
142f4a2713aSLionel Sambuc   /// CurFuncDecl - Holds the Decl for the current outermost
143f4a2713aSLionel Sambuc   /// non-closure context.
144f4a2713aSLionel Sambuc   const Decl *CurFuncDecl;
145f4a2713aSLionel Sambuc   /// CurCodeDecl - This is the inner-most code context, which includes blocks.
146f4a2713aSLionel Sambuc   const Decl *CurCodeDecl;
147f4a2713aSLionel Sambuc   const CGFunctionInfo *CurFnInfo;
148f4a2713aSLionel Sambuc   QualType FnRetTy;
149f4a2713aSLionel Sambuc   llvm::Function *CurFn;
150f4a2713aSLionel Sambuc 
151f4a2713aSLionel Sambuc   /// CurGD - The GlobalDecl for the current function being compiled.
152f4a2713aSLionel Sambuc   GlobalDecl CurGD;
153f4a2713aSLionel Sambuc 
154f4a2713aSLionel Sambuc   /// PrologueCleanupDepth - The cleanup depth enclosing all the
155f4a2713aSLionel Sambuc   /// cleanups associated with the parameters.
156f4a2713aSLionel Sambuc   EHScopeStack::stable_iterator PrologueCleanupDepth;
157f4a2713aSLionel Sambuc 
158f4a2713aSLionel Sambuc   /// ReturnBlock - Unified return block.
159f4a2713aSLionel Sambuc   JumpDest ReturnBlock;
160f4a2713aSLionel Sambuc 
161f4a2713aSLionel Sambuc   /// ReturnValue - The temporary alloca to hold the return value. This is null
162f4a2713aSLionel Sambuc   /// iff the function has no return value.
163f4a2713aSLionel Sambuc   llvm::Value *ReturnValue;
164f4a2713aSLionel Sambuc 
165f4a2713aSLionel Sambuc   /// AllocaInsertPoint - This is an instruction in the entry block before which
166f4a2713aSLionel Sambuc   /// we prefer to insert allocas.
167f4a2713aSLionel Sambuc   llvm::AssertingVH<llvm::Instruction> AllocaInsertPt;
168f4a2713aSLionel Sambuc 
169f4a2713aSLionel Sambuc   /// \brief API for captured statement code generation.
170f4a2713aSLionel Sambuc   class CGCapturedStmtInfo {
171f4a2713aSLionel Sambuc   public:
172*0a6a1f1dSLionel Sambuc     explicit CGCapturedStmtInfo(CapturedRegionKind K = CR_Default)
Kind(K)173*0a6a1f1dSLionel Sambuc         : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {}
174f4a2713aSLionel Sambuc     explicit CGCapturedStmtInfo(const CapturedStmt &S,
175f4a2713aSLionel Sambuc                                 CapturedRegionKind K = CR_Default)
Kind(K)176*0a6a1f1dSLionel Sambuc       : Kind(K), ThisValue(nullptr), CXXThisFieldDecl(nullptr) {
177f4a2713aSLionel Sambuc 
178f4a2713aSLionel Sambuc       RecordDecl::field_iterator Field =
179f4a2713aSLionel Sambuc         S.getCapturedRecordDecl()->field_begin();
180f4a2713aSLionel Sambuc       for (CapturedStmt::const_capture_iterator I = S.capture_begin(),
181f4a2713aSLionel Sambuc                                                 E = S.capture_end();
182f4a2713aSLionel Sambuc            I != E; ++I, ++Field) {
183f4a2713aSLionel Sambuc         if (I->capturesThis())
184f4a2713aSLionel Sambuc           CXXThisFieldDecl = *Field;
185*0a6a1f1dSLionel Sambuc         else if (I->capturesVariable())
186f4a2713aSLionel Sambuc           CaptureFields[I->getCapturedVar()] = *Field;
187f4a2713aSLionel Sambuc       }
188f4a2713aSLionel Sambuc     }
189f4a2713aSLionel Sambuc 
190f4a2713aSLionel Sambuc     virtual ~CGCapturedStmtInfo();
191f4a2713aSLionel Sambuc 
getKind()192f4a2713aSLionel Sambuc     CapturedRegionKind getKind() const { return Kind; }
193f4a2713aSLionel Sambuc 
setContextValue(llvm::Value * V)194f4a2713aSLionel Sambuc     void setContextValue(llvm::Value *V) { ThisValue = V; }
195f4a2713aSLionel Sambuc     // \brief Retrieve the value of the context parameter.
getContextValue()196f4a2713aSLionel Sambuc     llvm::Value *getContextValue() const { return ThisValue; }
197f4a2713aSLionel Sambuc 
198f4a2713aSLionel Sambuc     /// \brief Lookup the captured field decl for a variable.
lookup(const VarDecl * VD)199f4a2713aSLionel Sambuc     const FieldDecl *lookup(const VarDecl *VD) const {
200f4a2713aSLionel Sambuc       return CaptureFields.lookup(VD);
201f4a2713aSLionel Sambuc     }
202f4a2713aSLionel Sambuc 
isCXXThisExprCaptured()203*0a6a1f1dSLionel Sambuc     bool isCXXThisExprCaptured() const { return CXXThisFieldDecl != nullptr; }
getThisFieldDecl()204f4a2713aSLionel Sambuc     FieldDecl *getThisFieldDecl() const { return CXXThisFieldDecl; }
205f4a2713aSLionel Sambuc 
classof(const CGCapturedStmtInfo *)206*0a6a1f1dSLionel Sambuc     static bool classof(const CGCapturedStmtInfo *) {
207*0a6a1f1dSLionel Sambuc       return true;
208*0a6a1f1dSLionel Sambuc     }
209*0a6a1f1dSLionel Sambuc 
210f4a2713aSLionel Sambuc     /// \brief Emit the captured statement body.
EmitBody(CodeGenFunction & CGF,Stmt * S)211f4a2713aSLionel Sambuc     virtual void EmitBody(CodeGenFunction &CGF, Stmt *S) {
212*0a6a1f1dSLionel Sambuc       RegionCounter Cnt = CGF.getPGORegionCounter(S);
213*0a6a1f1dSLionel Sambuc       Cnt.beginRegion(CGF.Builder);
214f4a2713aSLionel Sambuc       CGF.EmitStmt(S);
215f4a2713aSLionel Sambuc     }
216f4a2713aSLionel Sambuc 
217f4a2713aSLionel Sambuc     /// \brief Get the name of the capture helper.
getHelperName()218f4a2713aSLionel Sambuc     virtual StringRef getHelperName() const { return "__captured_stmt"; }
219f4a2713aSLionel Sambuc 
220f4a2713aSLionel Sambuc   private:
221f4a2713aSLionel Sambuc     /// \brief The kind of captured statement being generated.
222f4a2713aSLionel Sambuc     CapturedRegionKind Kind;
223f4a2713aSLionel Sambuc 
224f4a2713aSLionel Sambuc     /// \brief Keep the map between VarDecl and FieldDecl.
225f4a2713aSLionel Sambuc     llvm::SmallDenseMap<const VarDecl *, FieldDecl *> CaptureFields;
226f4a2713aSLionel Sambuc 
227f4a2713aSLionel Sambuc     /// \brief The base address of the captured record, passed in as the first
228f4a2713aSLionel Sambuc     /// argument of the parallel region function.
229f4a2713aSLionel Sambuc     llvm::Value *ThisValue;
230f4a2713aSLionel Sambuc 
231f4a2713aSLionel Sambuc     /// \brief Captured 'this' type.
232f4a2713aSLionel Sambuc     FieldDecl *CXXThisFieldDecl;
233f4a2713aSLionel Sambuc   };
234f4a2713aSLionel Sambuc   CGCapturedStmtInfo *CapturedStmtInfo;
235f4a2713aSLionel Sambuc 
236f4a2713aSLionel Sambuc   /// BoundsChecking - Emit run-time bounds checks. Higher values mean
237f4a2713aSLionel Sambuc   /// potentially higher performance penalties.
238f4a2713aSLionel Sambuc   unsigned char BoundsChecking;
239f4a2713aSLionel Sambuc 
240*0a6a1f1dSLionel Sambuc   /// \brief Sanitizers enabled for this function.
241*0a6a1f1dSLionel Sambuc   SanitizerSet SanOpts;
242f4a2713aSLionel Sambuc 
243*0a6a1f1dSLionel Sambuc   /// \brief True if CodeGen currently emits code implementing sanitizer checks.
244*0a6a1f1dSLionel Sambuc   bool IsSanitizerScope;
245*0a6a1f1dSLionel Sambuc 
246*0a6a1f1dSLionel Sambuc   /// \brief RAII object to set/unset CodeGenFunction::IsSanitizerScope.
247*0a6a1f1dSLionel Sambuc   class SanitizerScope {
248*0a6a1f1dSLionel Sambuc     CodeGenFunction *CGF;
249*0a6a1f1dSLionel Sambuc   public:
250*0a6a1f1dSLionel Sambuc     SanitizerScope(CodeGenFunction *CGF);
251*0a6a1f1dSLionel Sambuc     ~SanitizerScope();
252*0a6a1f1dSLionel Sambuc   };
253*0a6a1f1dSLionel Sambuc 
254*0a6a1f1dSLionel Sambuc   /// In C++, whether we are code generating a thunk.  This controls whether we
255*0a6a1f1dSLionel Sambuc   /// should emit cleanups.
256*0a6a1f1dSLionel Sambuc   bool CurFuncIsThunk;
257f4a2713aSLionel Sambuc 
258f4a2713aSLionel Sambuc   /// In ARC, whether we should autorelease the return value.
259f4a2713aSLionel Sambuc   bool AutoreleaseResult;
260f4a2713aSLionel Sambuc 
261*0a6a1f1dSLionel Sambuc   /// Whether we processed a Microsoft-style asm block during CodeGen. These can
262*0a6a1f1dSLionel Sambuc   /// potentially set the return value.
263*0a6a1f1dSLionel Sambuc   bool SawAsmBlock;
264*0a6a1f1dSLionel Sambuc 
265f4a2713aSLionel Sambuc   const CodeGen::CGBlockInfo *BlockInfo;
266f4a2713aSLionel Sambuc   llvm::Value *BlockPointer;
267f4a2713aSLionel Sambuc 
268f4a2713aSLionel Sambuc   llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields;
269f4a2713aSLionel Sambuc   FieldDecl *LambdaThisCaptureField;
270f4a2713aSLionel Sambuc 
271f4a2713aSLionel Sambuc   /// \brief A mapping from NRVO variables to the flags used to indicate
272f4a2713aSLionel Sambuc   /// when the NRVO has been applied to this variable.
273f4a2713aSLionel Sambuc   llvm::DenseMap<const VarDecl *, llvm::Value *> NRVOFlags;
274f4a2713aSLionel Sambuc 
275f4a2713aSLionel Sambuc   EHScopeStack EHStack;
276f4a2713aSLionel Sambuc   llvm::SmallVector<char, 256> LifetimeExtendedCleanupStack;
277f4a2713aSLionel Sambuc 
278f4a2713aSLionel Sambuc   /// Header for data within LifetimeExtendedCleanupStack.
279f4a2713aSLionel Sambuc   struct LifetimeExtendedCleanupHeader {
280f4a2713aSLionel Sambuc     /// The size of the following cleanup object.
281*0a6a1f1dSLionel Sambuc     unsigned Size : 29;
282f4a2713aSLionel Sambuc     /// The kind of cleanup to push: a value from the CleanupKind enumeration.
283f4a2713aSLionel Sambuc     unsigned Kind : 3;
284f4a2713aSLionel Sambuc 
getSizeLifetimeExtendedCleanupHeader285*0a6a1f1dSLionel Sambuc     size_t getSize() const { return size_t(Size); }
getKindLifetimeExtendedCleanupHeader286f4a2713aSLionel Sambuc     CleanupKind getKind() const { return static_cast<CleanupKind>(Kind); }
287f4a2713aSLionel Sambuc   };
288f4a2713aSLionel Sambuc 
289f4a2713aSLionel Sambuc   /// i32s containing the indexes of the cleanup destinations.
290f4a2713aSLionel Sambuc   llvm::AllocaInst *NormalCleanupDest;
291f4a2713aSLionel Sambuc 
292f4a2713aSLionel Sambuc   unsigned NextCleanupDestIndex;
293f4a2713aSLionel Sambuc 
294f4a2713aSLionel Sambuc   /// FirstBlockInfo - The head of a singly-linked-list of block layouts.
295f4a2713aSLionel Sambuc   CGBlockInfo *FirstBlockInfo;
296f4a2713aSLionel Sambuc 
297f4a2713aSLionel Sambuc   /// EHResumeBlock - Unified block containing a call to llvm.eh.resume.
298f4a2713aSLionel Sambuc   llvm::BasicBlock *EHResumeBlock;
299f4a2713aSLionel Sambuc 
300f4a2713aSLionel Sambuc   /// The exception slot.  All landing pads write the current exception pointer
301f4a2713aSLionel Sambuc   /// into this alloca.
302f4a2713aSLionel Sambuc   llvm::Value *ExceptionSlot;
303f4a2713aSLionel Sambuc 
304f4a2713aSLionel Sambuc   /// The selector slot.  Under the MandatoryCleanup model, all landing pads
305f4a2713aSLionel Sambuc   /// write the current selector value into this alloca.
306f4a2713aSLionel Sambuc   llvm::AllocaInst *EHSelectorSlot;
307f4a2713aSLionel Sambuc 
308f4a2713aSLionel Sambuc   /// Emits a landing pad for the current EH stack.
309f4a2713aSLionel Sambuc   llvm::BasicBlock *EmitLandingPad();
310f4a2713aSLionel Sambuc 
311f4a2713aSLionel Sambuc   llvm::BasicBlock *getInvokeDestImpl();
312f4a2713aSLionel Sambuc 
313f4a2713aSLionel Sambuc   template <class T>
saveValueInCond(T value)314f4a2713aSLionel Sambuc   typename DominatingValue<T>::saved_type saveValueInCond(T value) {
315f4a2713aSLionel Sambuc     return DominatingValue<T>::save(*this, value);
316f4a2713aSLionel Sambuc   }
317f4a2713aSLionel Sambuc 
318f4a2713aSLionel Sambuc public:
319f4a2713aSLionel Sambuc   /// ObjCEHValueStack - Stack of Objective-C exception values, used for
320f4a2713aSLionel Sambuc   /// rethrows.
321f4a2713aSLionel Sambuc   SmallVector<llvm::Value*, 8> ObjCEHValueStack;
322f4a2713aSLionel Sambuc 
323f4a2713aSLionel Sambuc   /// A class controlling the emission of a finally block.
324f4a2713aSLionel Sambuc   class FinallyInfo {
325f4a2713aSLionel Sambuc     /// Where the catchall's edge through the cleanup should go.
326f4a2713aSLionel Sambuc     JumpDest RethrowDest;
327f4a2713aSLionel Sambuc 
328f4a2713aSLionel Sambuc     /// A function to call to enter the catch.
329f4a2713aSLionel Sambuc     llvm::Constant *BeginCatchFn;
330f4a2713aSLionel Sambuc 
331f4a2713aSLionel Sambuc     /// An i1 variable indicating whether or not the @finally is
332f4a2713aSLionel Sambuc     /// running for an exception.
333f4a2713aSLionel Sambuc     llvm::AllocaInst *ForEHVar;
334f4a2713aSLionel Sambuc 
335f4a2713aSLionel Sambuc     /// An i8* variable into which the exception pointer to rethrow
336f4a2713aSLionel Sambuc     /// has been saved.
337f4a2713aSLionel Sambuc     llvm::AllocaInst *SavedExnVar;
338f4a2713aSLionel Sambuc 
339f4a2713aSLionel Sambuc   public:
340f4a2713aSLionel Sambuc     void enter(CodeGenFunction &CGF, const Stmt *Finally,
341f4a2713aSLionel Sambuc                llvm::Constant *beginCatchFn, llvm::Constant *endCatchFn,
342f4a2713aSLionel Sambuc                llvm::Constant *rethrowFn);
343f4a2713aSLionel Sambuc     void exit(CodeGenFunction &CGF);
344f4a2713aSLionel Sambuc   };
345f4a2713aSLionel Sambuc 
346f4a2713aSLionel Sambuc   /// pushFullExprCleanup - Push a cleanup to be run at the end of the
347f4a2713aSLionel Sambuc   /// current full-expression.  Safe against the possibility that
348f4a2713aSLionel Sambuc   /// we're currently inside a conditionally-evaluated expression.
349f4a2713aSLionel Sambuc   template <class T, class A0>
pushFullExprCleanup(CleanupKind kind,A0 a0)350f4a2713aSLionel Sambuc   void pushFullExprCleanup(CleanupKind kind, A0 a0) {
351f4a2713aSLionel Sambuc     // If we're not in a conditional branch, or if none of the
352f4a2713aSLionel Sambuc     // arguments requires saving, then use the unconditional cleanup.
353f4a2713aSLionel Sambuc     if (!isInConditionalBranch())
354f4a2713aSLionel Sambuc       return EHStack.pushCleanup<T>(kind, a0);
355f4a2713aSLionel Sambuc 
356f4a2713aSLionel Sambuc     typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
357f4a2713aSLionel Sambuc 
358f4a2713aSLionel Sambuc     typedef EHScopeStack::ConditionalCleanup1<T, A0> CleanupType;
359f4a2713aSLionel Sambuc     EHStack.pushCleanup<CleanupType>(kind, a0_saved);
360f4a2713aSLionel Sambuc     initFullExprCleanup();
361f4a2713aSLionel Sambuc   }
362f4a2713aSLionel Sambuc 
363f4a2713aSLionel Sambuc   /// pushFullExprCleanup - Push a cleanup to be run at the end of the
364f4a2713aSLionel Sambuc   /// current full-expression.  Safe against the possibility that
365f4a2713aSLionel Sambuc   /// we're currently inside a conditionally-evaluated expression.
366f4a2713aSLionel Sambuc   template <class T, class A0, class A1>
pushFullExprCleanup(CleanupKind kind,A0 a0,A1 a1)367f4a2713aSLionel Sambuc   void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1) {
368f4a2713aSLionel Sambuc     // If we're not in a conditional branch, or if none of the
369f4a2713aSLionel Sambuc     // arguments requires saving, then use the unconditional cleanup.
370f4a2713aSLionel Sambuc     if (!isInConditionalBranch())
371f4a2713aSLionel Sambuc       return EHStack.pushCleanup<T>(kind, a0, a1);
372f4a2713aSLionel Sambuc 
373f4a2713aSLionel Sambuc     typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
374f4a2713aSLionel Sambuc     typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1);
375f4a2713aSLionel Sambuc 
376f4a2713aSLionel Sambuc     typedef EHScopeStack::ConditionalCleanup2<T, A0, A1> CleanupType;
377f4a2713aSLionel Sambuc     EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved);
378f4a2713aSLionel Sambuc     initFullExprCleanup();
379f4a2713aSLionel Sambuc   }
380f4a2713aSLionel Sambuc 
381f4a2713aSLionel Sambuc   /// pushFullExprCleanup - Push a cleanup to be run at the end of the
382f4a2713aSLionel Sambuc   /// current full-expression.  Safe against the possibility that
383f4a2713aSLionel Sambuc   /// we're currently inside a conditionally-evaluated expression.
384f4a2713aSLionel Sambuc   template <class T, class A0, class A1, class A2>
pushFullExprCleanup(CleanupKind kind,A0 a0,A1 a1,A2 a2)385f4a2713aSLionel Sambuc   void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1, A2 a2) {
386f4a2713aSLionel Sambuc     // If we're not in a conditional branch, or if none of the
387f4a2713aSLionel Sambuc     // arguments requires saving, then use the unconditional cleanup.
388f4a2713aSLionel Sambuc     if (!isInConditionalBranch()) {
389f4a2713aSLionel Sambuc       return EHStack.pushCleanup<T>(kind, a0, a1, a2);
390f4a2713aSLionel Sambuc     }
391f4a2713aSLionel Sambuc 
392f4a2713aSLionel Sambuc     typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
393f4a2713aSLionel Sambuc     typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1);
394f4a2713aSLionel Sambuc     typename DominatingValue<A2>::saved_type a2_saved = saveValueInCond(a2);
395f4a2713aSLionel Sambuc 
396f4a2713aSLionel Sambuc     typedef EHScopeStack::ConditionalCleanup3<T, A0, A1, A2> CleanupType;
397f4a2713aSLionel Sambuc     EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved, a2_saved);
398f4a2713aSLionel Sambuc     initFullExprCleanup();
399f4a2713aSLionel Sambuc   }
400f4a2713aSLionel Sambuc 
401f4a2713aSLionel Sambuc   /// pushFullExprCleanup - Push a cleanup to be run at the end of the
402f4a2713aSLionel Sambuc   /// current full-expression.  Safe against the possibility that
403f4a2713aSLionel Sambuc   /// we're currently inside a conditionally-evaluated expression.
404f4a2713aSLionel Sambuc   template <class T, class A0, class A1, class A2, class A3>
pushFullExprCleanup(CleanupKind kind,A0 a0,A1 a1,A2 a2,A3 a3)405f4a2713aSLionel Sambuc   void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1, A2 a2, A3 a3) {
406f4a2713aSLionel Sambuc     // If we're not in a conditional branch, or if none of the
407f4a2713aSLionel Sambuc     // arguments requires saving, then use the unconditional cleanup.
408f4a2713aSLionel Sambuc     if (!isInConditionalBranch()) {
409f4a2713aSLionel Sambuc       return EHStack.pushCleanup<T>(kind, a0, a1, a2, a3);
410f4a2713aSLionel Sambuc     }
411f4a2713aSLionel Sambuc 
412f4a2713aSLionel Sambuc     typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
413f4a2713aSLionel Sambuc     typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1);
414f4a2713aSLionel Sambuc     typename DominatingValue<A2>::saved_type a2_saved = saveValueInCond(a2);
415f4a2713aSLionel Sambuc     typename DominatingValue<A3>::saved_type a3_saved = saveValueInCond(a3);
416f4a2713aSLionel Sambuc 
417f4a2713aSLionel Sambuc     typedef EHScopeStack::ConditionalCleanup4<T, A0, A1, A2, A3> CleanupType;
418f4a2713aSLionel Sambuc     EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved,
419f4a2713aSLionel Sambuc                                      a2_saved, a3_saved);
420f4a2713aSLionel Sambuc     initFullExprCleanup();
421f4a2713aSLionel Sambuc   }
422f4a2713aSLionel Sambuc 
423f4a2713aSLionel Sambuc   /// \brief Queue a cleanup to be pushed after finishing the current
424f4a2713aSLionel Sambuc   /// full-expression.
425f4a2713aSLionel Sambuc   template <class T, class A0, class A1, class A2, class A3>
pushCleanupAfterFullExpr(CleanupKind Kind,A0 a0,A1 a1,A2 a2,A3 a3)426f4a2713aSLionel Sambuc   void pushCleanupAfterFullExpr(CleanupKind Kind, A0 a0, A1 a1, A2 a2, A3 a3) {
427f4a2713aSLionel Sambuc     assert(!isInConditionalBranch() && "can't defer conditional cleanup");
428f4a2713aSLionel Sambuc 
429f4a2713aSLionel Sambuc     LifetimeExtendedCleanupHeader Header = { sizeof(T), Kind };
430f4a2713aSLionel Sambuc 
431f4a2713aSLionel Sambuc     size_t OldSize = LifetimeExtendedCleanupStack.size();
432f4a2713aSLionel Sambuc     LifetimeExtendedCleanupStack.resize(
433f4a2713aSLionel Sambuc         LifetimeExtendedCleanupStack.size() + sizeof(Header) + Header.Size);
434f4a2713aSLionel Sambuc 
435f4a2713aSLionel Sambuc     char *Buffer = &LifetimeExtendedCleanupStack[OldSize];
436f4a2713aSLionel Sambuc     new (Buffer) LifetimeExtendedCleanupHeader(Header);
437f4a2713aSLionel Sambuc     new (Buffer + sizeof(Header)) T(a0, a1, a2, a3);
438f4a2713aSLionel Sambuc   }
439f4a2713aSLionel Sambuc 
440f4a2713aSLionel Sambuc   /// Set up the last cleaup that was pushed as a conditional
441f4a2713aSLionel Sambuc   /// full-expression cleanup.
442f4a2713aSLionel Sambuc   void initFullExprCleanup();
443f4a2713aSLionel Sambuc 
444f4a2713aSLionel Sambuc   /// PushDestructorCleanup - Push a cleanup to call the
445f4a2713aSLionel Sambuc   /// complete-object destructor of an object of the given type at the
446f4a2713aSLionel Sambuc   /// given address.  Does nothing if T is not a C++ class type with a
447f4a2713aSLionel Sambuc   /// non-trivial destructor.
448f4a2713aSLionel Sambuc   void PushDestructorCleanup(QualType T, llvm::Value *Addr);
449f4a2713aSLionel Sambuc 
450f4a2713aSLionel Sambuc   /// PushDestructorCleanup - Push a cleanup to call the
451f4a2713aSLionel Sambuc   /// complete-object variant of the given destructor on the object at
452f4a2713aSLionel Sambuc   /// the given address.
453f4a2713aSLionel Sambuc   void PushDestructorCleanup(const CXXDestructorDecl *Dtor,
454f4a2713aSLionel Sambuc                              llvm::Value *Addr);
455f4a2713aSLionel Sambuc 
456f4a2713aSLionel Sambuc   /// PopCleanupBlock - Will pop the cleanup entry on the stack and
457f4a2713aSLionel Sambuc   /// process all branch fixups.
458f4a2713aSLionel Sambuc   void PopCleanupBlock(bool FallThroughIsBranchThrough = false);
459f4a2713aSLionel Sambuc 
460f4a2713aSLionel Sambuc   /// DeactivateCleanupBlock - Deactivates the given cleanup block.
461f4a2713aSLionel Sambuc   /// The block cannot be reactivated.  Pops it if it's the top of the
462f4a2713aSLionel Sambuc   /// stack.
463f4a2713aSLionel Sambuc   ///
464f4a2713aSLionel Sambuc   /// \param DominatingIP - An instruction which is known to
465f4a2713aSLionel Sambuc   ///   dominate the current IP (if set) and which lies along
466f4a2713aSLionel Sambuc   ///   all paths of execution between the current IP and the
467f4a2713aSLionel Sambuc   ///   the point at which the cleanup comes into scope.
468f4a2713aSLionel Sambuc   void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup,
469f4a2713aSLionel Sambuc                               llvm::Instruction *DominatingIP);
470f4a2713aSLionel Sambuc 
471f4a2713aSLionel Sambuc   /// ActivateCleanupBlock - Activates an initially-inactive cleanup.
472f4a2713aSLionel Sambuc   /// Cannot be used to resurrect a deactivated cleanup.
473f4a2713aSLionel Sambuc   ///
474f4a2713aSLionel Sambuc   /// \param DominatingIP - An instruction which is known to
475f4a2713aSLionel Sambuc   ///   dominate the current IP (if set) and which lies along
476f4a2713aSLionel Sambuc   ///   all paths of execution between the current IP and the
477f4a2713aSLionel Sambuc   ///   the point at which the cleanup comes into scope.
478f4a2713aSLionel Sambuc   void ActivateCleanupBlock(EHScopeStack::stable_iterator Cleanup,
479f4a2713aSLionel Sambuc                             llvm::Instruction *DominatingIP);
480f4a2713aSLionel Sambuc 
481f4a2713aSLionel Sambuc   /// \brief Enters a new scope for capturing cleanups, all of which
482f4a2713aSLionel Sambuc   /// will be executed once the scope is exited.
483f4a2713aSLionel Sambuc   class RunCleanupsScope {
484f4a2713aSLionel Sambuc     EHScopeStack::stable_iterator CleanupStackDepth;
485f4a2713aSLionel Sambuc     size_t LifetimeExtendedCleanupStackSize;
486f4a2713aSLionel Sambuc     bool OldDidCallStackSave;
487f4a2713aSLionel Sambuc   protected:
488f4a2713aSLionel Sambuc     bool PerformCleanup;
489f4a2713aSLionel Sambuc   private:
490f4a2713aSLionel Sambuc 
491f4a2713aSLionel Sambuc     RunCleanupsScope(const RunCleanupsScope &) LLVM_DELETED_FUNCTION;
492f4a2713aSLionel Sambuc     void operator=(const RunCleanupsScope &) LLVM_DELETED_FUNCTION;
493f4a2713aSLionel Sambuc 
494f4a2713aSLionel Sambuc   protected:
495f4a2713aSLionel Sambuc     CodeGenFunction& CGF;
496f4a2713aSLionel Sambuc 
497f4a2713aSLionel Sambuc   public:
498f4a2713aSLionel Sambuc     /// \brief Enter a new cleanup scope.
RunCleanupsScope(CodeGenFunction & CGF)499f4a2713aSLionel Sambuc     explicit RunCleanupsScope(CodeGenFunction &CGF)
500f4a2713aSLionel Sambuc       : PerformCleanup(true), CGF(CGF)
501f4a2713aSLionel Sambuc     {
502f4a2713aSLionel Sambuc       CleanupStackDepth = CGF.EHStack.stable_begin();
503f4a2713aSLionel Sambuc       LifetimeExtendedCleanupStackSize =
504f4a2713aSLionel Sambuc           CGF.LifetimeExtendedCleanupStack.size();
505f4a2713aSLionel Sambuc       OldDidCallStackSave = CGF.DidCallStackSave;
506f4a2713aSLionel Sambuc       CGF.DidCallStackSave = false;
507f4a2713aSLionel Sambuc     }
508f4a2713aSLionel Sambuc 
509f4a2713aSLionel Sambuc     /// \brief Exit this cleanup scope, emitting any accumulated
510f4a2713aSLionel Sambuc     /// cleanups.
~RunCleanupsScope()511f4a2713aSLionel Sambuc     ~RunCleanupsScope() {
512f4a2713aSLionel Sambuc       if (PerformCleanup) {
513f4a2713aSLionel Sambuc         CGF.DidCallStackSave = OldDidCallStackSave;
514f4a2713aSLionel Sambuc         CGF.PopCleanupBlocks(CleanupStackDepth,
515f4a2713aSLionel Sambuc                              LifetimeExtendedCleanupStackSize);
516f4a2713aSLionel Sambuc       }
517f4a2713aSLionel Sambuc     }
518f4a2713aSLionel Sambuc 
519f4a2713aSLionel Sambuc     /// \brief Determine whether this scope requires any cleanups.
requiresCleanups()520f4a2713aSLionel Sambuc     bool requiresCleanups() const {
521f4a2713aSLionel Sambuc       return CGF.EHStack.stable_begin() != CleanupStackDepth;
522f4a2713aSLionel Sambuc     }
523f4a2713aSLionel Sambuc 
524f4a2713aSLionel Sambuc     /// \brief Force the emission of cleanups now, instead of waiting
525f4a2713aSLionel Sambuc     /// until this object is destroyed.
ForceCleanup()526f4a2713aSLionel Sambuc     void ForceCleanup() {
527f4a2713aSLionel Sambuc       assert(PerformCleanup && "Already forced cleanup");
528f4a2713aSLionel Sambuc       CGF.DidCallStackSave = OldDidCallStackSave;
529f4a2713aSLionel Sambuc       CGF.PopCleanupBlocks(CleanupStackDepth,
530f4a2713aSLionel Sambuc                            LifetimeExtendedCleanupStackSize);
531f4a2713aSLionel Sambuc       PerformCleanup = false;
532f4a2713aSLionel Sambuc     }
533f4a2713aSLionel Sambuc   };
534f4a2713aSLionel Sambuc 
535*0a6a1f1dSLionel Sambuc   class LexicalScope : public RunCleanupsScope {
536f4a2713aSLionel Sambuc     SourceRange Range;
537f4a2713aSLionel Sambuc     SmallVector<const LabelDecl*, 4> Labels;
538f4a2713aSLionel Sambuc     LexicalScope *ParentScope;
539f4a2713aSLionel Sambuc 
540f4a2713aSLionel Sambuc     LexicalScope(const LexicalScope &) LLVM_DELETED_FUNCTION;
541f4a2713aSLionel Sambuc     void operator=(const LexicalScope &) LLVM_DELETED_FUNCTION;
542f4a2713aSLionel Sambuc 
543f4a2713aSLionel Sambuc   public:
544f4a2713aSLionel Sambuc     /// \brief Enter a new cleanup scope.
LexicalScope(CodeGenFunction & CGF,SourceRange Range)545f4a2713aSLionel Sambuc     explicit LexicalScope(CodeGenFunction &CGF, SourceRange Range)
546f4a2713aSLionel Sambuc       : RunCleanupsScope(CGF), Range(Range), ParentScope(CGF.CurLexicalScope) {
547f4a2713aSLionel Sambuc       CGF.CurLexicalScope = this;
548f4a2713aSLionel Sambuc       if (CGDebugInfo *DI = CGF.getDebugInfo())
549f4a2713aSLionel Sambuc         DI->EmitLexicalBlockStart(CGF.Builder, Range.getBegin());
550f4a2713aSLionel Sambuc     }
551f4a2713aSLionel Sambuc 
addLabel(const LabelDecl * label)552f4a2713aSLionel Sambuc     void addLabel(const LabelDecl *label) {
553f4a2713aSLionel Sambuc       assert(PerformCleanup && "adding label to dead scope?");
554f4a2713aSLionel Sambuc       Labels.push_back(label);
555f4a2713aSLionel Sambuc     }
556f4a2713aSLionel Sambuc 
557f4a2713aSLionel Sambuc     /// \brief Exit this cleanup scope, emitting any accumulated
558f4a2713aSLionel Sambuc     /// cleanups.
~LexicalScope()559f4a2713aSLionel Sambuc     ~LexicalScope() {
560f4a2713aSLionel Sambuc       if (CGDebugInfo *DI = CGF.getDebugInfo())
561f4a2713aSLionel Sambuc         DI->EmitLexicalBlockEnd(CGF.Builder, Range.getEnd());
562f4a2713aSLionel Sambuc 
563f4a2713aSLionel Sambuc       // If we should perform a cleanup, force them now.  Note that
564f4a2713aSLionel Sambuc       // this ends the cleanup scope before rescoping any labels.
565f4a2713aSLionel Sambuc       if (PerformCleanup) ForceCleanup();
566f4a2713aSLionel Sambuc     }
567f4a2713aSLionel Sambuc 
568f4a2713aSLionel Sambuc     /// \brief Force the emission of cleanups now, instead of waiting
569f4a2713aSLionel Sambuc     /// until this object is destroyed.
ForceCleanup()570f4a2713aSLionel Sambuc     void ForceCleanup() {
571f4a2713aSLionel Sambuc       CGF.CurLexicalScope = ParentScope;
572f4a2713aSLionel Sambuc       RunCleanupsScope::ForceCleanup();
573f4a2713aSLionel Sambuc 
574f4a2713aSLionel Sambuc       if (!Labels.empty())
575f4a2713aSLionel Sambuc         rescopeLabels();
576f4a2713aSLionel Sambuc     }
577f4a2713aSLionel Sambuc 
578f4a2713aSLionel Sambuc     void rescopeLabels();
579f4a2713aSLionel Sambuc   };
580f4a2713aSLionel Sambuc 
581*0a6a1f1dSLionel Sambuc   /// \brief The scope used to remap some variables as private in the OpenMP
582*0a6a1f1dSLionel Sambuc   /// loop body (or other captured region emitted without outlining), and to
583*0a6a1f1dSLionel Sambuc   /// restore old vars back on exit.
584*0a6a1f1dSLionel Sambuc   class OMPPrivateScope : public RunCleanupsScope {
585*0a6a1f1dSLionel Sambuc     typedef llvm::DenseMap<const VarDecl *, llvm::Value *> VarDeclMapTy;
586*0a6a1f1dSLionel Sambuc     VarDeclMapTy SavedLocals;
587*0a6a1f1dSLionel Sambuc     VarDeclMapTy SavedPrivates;
588*0a6a1f1dSLionel Sambuc 
589*0a6a1f1dSLionel Sambuc   private:
590*0a6a1f1dSLionel Sambuc     OMPPrivateScope(const OMPPrivateScope &) LLVM_DELETED_FUNCTION;
591*0a6a1f1dSLionel Sambuc     void operator=(const OMPPrivateScope &) LLVM_DELETED_FUNCTION;
592*0a6a1f1dSLionel Sambuc 
593*0a6a1f1dSLionel Sambuc   public:
594*0a6a1f1dSLionel Sambuc     /// \brief Enter a new OpenMP private scope.
OMPPrivateScope(CodeGenFunction & CGF)595*0a6a1f1dSLionel Sambuc     explicit OMPPrivateScope(CodeGenFunction &CGF) : RunCleanupsScope(CGF) {}
596*0a6a1f1dSLionel Sambuc 
597*0a6a1f1dSLionel Sambuc     /// \brief Registers \a LocalVD variable as a private and apply \a
598*0a6a1f1dSLionel Sambuc     /// PrivateGen function for it to generate corresponding private variable.
599*0a6a1f1dSLionel Sambuc     /// \a PrivateGen returns an address of the generated private variable.
600*0a6a1f1dSLionel Sambuc     /// \return true if the variable is registered as private, false if it has
601*0a6a1f1dSLionel Sambuc     /// been privatized already.
602*0a6a1f1dSLionel Sambuc     bool
addPrivate(const VarDecl * LocalVD,const std::function<llvm::Value * ()> & PrivateGen)603*0a6a1f1dSLionel Sambuc     addPrivate(const VarDecl *LocalVD,
604*0a6a1f1dSLionel Sambuc                const std::function<llvm::Value *()> &PrivateGen) {
605*0a6a1f1dSLionel Sambuc       assert(PerformCleanup && "adding private to dead scope");
606*0a6a1f1dSLionel Sambuc       if (SavedLocals.count(LocalVD) > 0) return false;
607*0a6a1f1dSLionel Sambuc       SavedLocals[LocalVD] = CGF.LocalDeclMap.lookup(LocalVD);
608*0a6a1f1dSLionel Sambuc       CGF.LocalDeclMap.erase(LocalVD);
609*0a6a1f1dSLionel Sambuc       SavedPrivates[LocalVD] = PrivateGen();
610*0a6a1f1dSLionel Sambuc       CGF.LocalDeclMap[LocalVD] = SavedLocals[LocalVD];
611*0a6a1f1dSLionel Sambuc       return true;
612*0a6a1f1dSLionel Sambuc     }
613*0a6a1f1dSLionel Sambuc 
614*0a6a1f1dSLionel Sambuc     /// \brief Privatizes local variables previously registered as private.
615*0a6a1f1dSLionel Sambuc     /// Registration is separate from the actual privatization to allow
616*0a6a1f1dSLionel Sambuc     /// initializers use values of the original variables, not the private one.
617*0a6a1f1dSLionel Sambuc     /// This is important, for example, if the private variable is a class
618*0a6a1f1dSLionel Sambuc     /// variable initialized by a constructor that references other private
619*0a6a1f1dSLionel Sambuc     /// variables. But at initialization original variables must be used, not
620*0a6a1f1dSLionel Sambuc     /// private copies.
621*0a6a1f1dSLionel Sambuc     /// \return true if at least one variable was privatized, false otherwise.
Privatize()622*0a6a1f1dSLionel Sambuc     bool Privatize() {
623*0a6a1f1dSLionel Sambuc       for (auto VDPair : SavedPrivates) {
624*0a6a1f1dSLionel Sambuc         CGF.LocalDeclMap[VDPair.first] = VDPair.second;
625*0a6a1f1dSLionel Sambuc       }
626*0a6a1f1dSLionel Sambuc       SavedPrivates.clear();
627*0a6a1f1dSLionel Sambuc       return !SavedLocals.empty();
628*0a6a1f1dSLionel Sambuc     }
629*0a6a1f1dSLionel Sambuc 
ForceCleanup()630*0a6a1f1dSLionel Sambuc     void ForceCleanup() {
631*0a6a1f1dSLionel Sambuc       RunCleanupsScope::ForceCleanup();
632*0a6a1f1dSLionel Sambuc       // Remap vars back to the original values.
633*0a6a1f1dSLionel Sambuc       for (auto I : SavedLocals) {
634*0a6a1f1dSLionel Sambuc         CGF.LocalDeclMap[I.first] = I.second;
635*0a6a1f1dSLionel Sambuc       }
636*0a6a1f1dSLionel Sambuc       SavedLocals.clear();
637*0a6a1f1dSLionel Sambuc     }
638*0a6a1f1dSLionel Sambuc 
639*0a6a1f1dSLionel Sambuc     /// \brief Exit scope - all the mapped variables are restored.
~OMPPrivateScope()640*0a6a1f1dSLionel Sambuc     ~OMPPrivateScope() { ForceCleanup(); }
641*0a6a1f1dSLionel Sambuc   };
642f4a2713aSLionel Sambuc 
643f4a2713aSLionel Sambuc   /// \brief Takes the old cleanup stack size and emits the cleanup blocks
644f4a2713aSLionel Sambuc   /// that have been added.
645f4a2713aSLionel Sambuc   void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize);
646f4a2713aSLionel Sambuc 
647f4a2713aSLionel Sambuc   /// \brief Takes the old cleanup stack size and emits the cleanup blocks
648f4a2713aSLionel Sambuc   /// that have been added, then adds all lifetime-extended cleanups from
649f4a2713aSLionel Sambuc   /// the given position to the stack.
650f4a2713aSLionel Sambuc   void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize,
651f4a2713aSLionel Sambuc                         size_t OldLifetimeExtendedStackSize);
652f4a2713aSLionel Sambuc 
653f4a2713aSLionel Sambuc   void ResolveBranchFixups(llvm::BasicBlock *Target);
654f4a2713aSLionel Sambuc 
655f4a2713aSLionel Sambuc   /// The given basic block lies in the current EH scope, but may be a
656f4a2713aSLionel Sambuc   /// target of a potentially scope-crossing jump; get a stable handle
657f4a2713aSLionel Sambuc   /// to which we can perform this jump later.
getJumpDestInCurrentScope(llvm::BasicBlock * Target)658f4a2713aSLionel Sambuc   JumpDest getJumpDestInCurrentScope(llvm::BasicBlock *Target) {
659f4a2713aSLionel Sambuc     return JumpDest(Target,
660f4a2713aSLionel Sambuc                     EHStack.getInnermostNormalCleanup(),
661f4a2713aSLionel Sambuc                     NextCleanupDestIndex++);
662f4a2713aSLionel Sambuc   }
663f4a2713aSLionel Sambuc 
664f4a2713aSLionel Sambuc   /// The given basic block lies in the current EH scope, but may be a
665f4a2713aSLionel Sambuc   /// target of a potentially scope-crossing jump; get a stable handle
666f4a2713aSLionel Sambuc   /// to which we can perform this jump later.
667f4a2713aSLionel Sambuc   JumpDest getJumpDestInCurrentScope(StringRef Name = StringRef()) {
668f4a2713aSLionel Sambuc     return getJumpDestInCurrentScope(createBasicBlock(Name));
669f4a2713aSLionel Sambuc   }
670f4a2713aSLionel Sambuc 
671f4a2713aSLionel Sambuc   /// EmitBranchThroughCleanup - Emit a branch from the current insert
672f4a2713aSLionel Sambuc   /// block through the normal cleanup handling code (if any) and then
673f4a2713aSLionel Sambuc   /// on to \arg Dest.
674f4a2713aSLionel Sambuc   void EmitBranchThroughCleanup(JumpDest Dest);
675f4a2713aSLionel Sambuc 
676f4a2713aSLionel Sambuc   /// isObviouslyBranchWithoutCleanups - Return true if a branch to the
677f4a2713aSLionel Sambuc   /// specified destination obviously has no cleanups to run.  'false' is always
678f4a2713aSLionel Sambuc   /// a conservatively correct answer for this method.
679f4a2713aSLionel Sambuc   bool isObviouslyBranchWithoutCleanups(JumpDest Dest) const;
680f4a2713aSLionel Sambuc 
681f4a2713aSLionel Sambuc   /// popCatchScope - Pops the catch scope at the top of the EHScope
682f4a2713aSLionel Sambuc   /// stack, emitting any required code (other than the catch handlers
683f4a2713aSLionel Sambuc   /// themselves).
684f4a2713aSLionel Sambuc   void popCatchScope();
685f4a2713aSLionel Sambuc 
686f4a2713aSLionel Sambuc   llvm::BasicBlock *getEHResumeBlock(bool isCleanup);
687f4a2713aSLionel Sambuc   llvm::BasicBlock *getEHDispatchBlock(EHScopeStack::stable_iterator scope);
688f4a2713aSLionel Sambuc 
689f4a2713aSLionel Sambuc   /// An object to manage conditionally-evaluated expressions.
690f4a2713aSLionel Sambuc   class ConditionalEvaluation {
691f4a2713aSLionel Sambuc     llvm::BasicBlock *StartBB;
692f4a2713aSLionel Sambuc 
693f4a2713aSLionel Sambuc   public:
ConditionalEvaluation(CodeGenFunction & CGF)694f4a2713aSLionel Sambuc     ConditionalEvaluation(CodeGenFunction &CGF)
695f4a2713aSLionel Sambuc       : StartBB(CGF.Builder.GetInsertBlock()) {}
696f4a2713aSLionel Sambuc 
begin(CodeGenFunction & CGF)697f4a2713aSLionel Sambuc     void begin(CodeGenFunction &CGF) {
698f4a2713aSLionel Sambuc       assert(CGF.OutermostConditional != this);
699f4a2713aSLionel Sambuc       if (!CGF.OutermostConditional)
700f4a2713aSLionel Sambuc         CGF.OutermostConditional = this;
701f4a2713aSLionel Sambuc     }
702f4a2713aSLionel Sambuc 
end(CodeGenFunction & CGF)703f4a2713aSLionel Sambuc     void end(CodeGenFunction &CGF) {
704*0a6a1f1dSLionel Sambuc       assert(CGF.OutermostConditional != nullptr);
705f4a2713aSLionel Sambuc       if (CGF.OutermostConditional == this)
706*0a6a1f1dSLionel Sambuc         CGF.OutermostConditional = nullptr;
707f4a2713aSLionel Sambuc     }
708f4a2713aSLionel Sambuc 
709f4a2713aSLionel Sambuc     /// Returns a block which will be executed prior to each
710f4a2713aSLionel Sambuc     /// evaluation of the conditional code.
getStartingBlock()711f4a2713aSLionel Sambuc     llvm::BasicBlock *getStartingBlock() const {
712f4a2713aSLionel Sambuc       return StartBB;
713f4a2713aSLionel Sambuc     }
714f4a2713aSLionel Sambuc   };
715f4a2713aSLionel Sambuc 
716f4a2713aSLionel Sambuc   /// isInConditionalBranch - Return true if we're currently emitting
717f4a2713aSLionel Sambuc   /// one branch or the other of a conditional expression.
isInConditionalBranch()718*0a6a1f1dSLionel Sambuc   bool isInConditionalBranch() const { return OutermostConditional != nullptr; }
719f4a2713aSLionel Sambuc 
setBeforeOutermostConditional(llvm::Value * value,llvm::Value * addr)720f4a2713aSLionel Sambuc   void setBeforeOutermostConditional(llvm::Value *value, llvm::Value *addr) {
721f4a2713aSLionel Sambuc     assert(isInConditionalBranch());
722f4a2713aSLionel Sambuc     llvm::BasicBlock *block = OutermostConditional->getStartingBlock();
723f4a2713aSLionel Sambuc     new llvm::StoreInst(value, addr, &block->back());
724f4a2713aSLionel Sambuc   }
725f4a2713aSLionel Sambuc 
726f4a2713aSLionel Sambuc   /// An RAII object to record that we're evaluating a statement
727f4a2713aSLionel Sambuc   /// expression.
728f4a2713aSLionel Sambuc   class StmtExprEvaluation {
729f4a2713aSLionel Sambuc     CodeGenFunction &CGF;
730f4a2713aSLionel Sambuc 
731f4a2713aSLionel Sambuc     /// We have to save the outermost conditional: cleanups in a
732f4a2713aSLionel Sambuc     /// statement expression aren't conditional just because the
733f4a2713aSLionel Sambuc     /// StmtExpr is.
734f4a2713aSLionel Sambuc     ConditionalEvaluation *SavedOutermostConditional;
735f4a2713aSLionel Sambuc 
736f4a2713aSLionel Sambuc   public:
StmtExprEvaluation(CodeGenFunction & CGF)737f4a2713aSLionel Sambuc     StmtExprEvaluation(CodeGenFunction &CGF)
738f4a2713aSLionel Sambuc       : CGF(CGF), SavedOutermostConditional(CGF.OutermostConditional) {
739*0a6a1f1dSLionel Sambuc       CGF.OutermostConditional = nullptr;
740f4a2713aSLionel Sambuc     }
741f4a2713aSLionel Sambuc 
~StmtExprEvaluation()742f4a2713aSLionel Sambuc     ~StmtExprEvaluation() {
743f4a2713aSLionel Sambuc       CGF.OutermostConditional = SavedOutermostConditional;
744f4a2713aSLionel Sambuc       CGF.EnsureInsertPoint();
745f4a2713aSLionel Sambuc     }
746f4a2713aSLionel Sambuc   };
747f4a2713aSLionel Sambuc 
748f4a2713aSLionel Sambuc   /// An object which temporarily prevents a value from being
749f4a2713aSLionel Sambuc   /// destroyed by aggressive peephole optimizations that assume that
750f4a2713aSLionel Sambuc   /// all uses of a value have been realized in the IR.
751f4a2713aSLionel Sambuc   class PeepholeProtection {
752f4a2713aSLionel Sambuc     llvm::Instruction *Inst;
753f4a2713aSLionel Sambuc     friend class CodeGenFunction;
754f4a2713aSLionel Sambuc 
755f4a2713aSLionel Sambuc   public:
PeepholeProtection()756*0a6a1f1dSLionel Sambuc     PeepholeProtection() : Inst(nullptr) {}
757f4a2713aSLionel Sambuc   };
758f4a2713aSLionel Sambuc 
759f4a2713aSLionel Sambuc   /// A non-RAII class containing all the information about a bound
760f4a2713aSLionel Sambuc   /// opaque value.  OpaqueValueMapping, below, is a RAII wrapper for
761f4a2713aSLionel Sambuc   /// this which makes individual mappings very simple; using this
762f4a2713aSLionel Sambuc   /// class directly is useful when you have a variable number of
763f4a2713aSLionel Sambuc   /// opaque values or don't want the RAII functionality for some
764f4a2713aSLionel Sambuc   /// reason.
765f4a2713aSLionel Sambuc   class OpaqueValueMappingData {
766f4a2713aSLionel Sambuc     const OpaqueValueExpr *OpaqueValue;
767f4a2713aSLionel Sambuc     bool BoundLValue;
768f4a2713aSLionel Sambuc     CodeGenFunction::PeepholeProtection Protection;
769f4a2713aSLionel Sambuc 
OpaqueValueMappingData(const OpaqueValueExpr * ov,bool boundLValue)770f4a2713aSLionel Sambuc     OpaqueValueMappingData(const OpaqueValueExpr *ov,
771f4a2713aSLionel Sambuc                            bool boundLValue)
772f4a2713aSLionel Sambuc       : OpaqueValue(ov), BoundLValue(boundLValue) {}
773f4a2713aSLionel Sambuc   public:
OpaqueValueMappingData()774*0a6a1f1dSLionel Sambuc     OpaqueValueMappingData() : OpaqueValue(nullptr) {}
775f4a2713aSLionel Sambuc 
shouldBindAsLValue(const Expr * expr)776f4a2713aSLionel Sambuc     static bool shouldBindAsLValue(const Expr *expr) {
777f4a2713aSLionel Sambuc       // gl-values should be bound as l-values for obvious reasons.
778f4a2713aSLionel Sambuc       // Records should be bound as l-values because IR generation
779f4a2713aSLionel Sambuc       // always keeps them in memory.  Expressions of function type
780f4a2713aSLionel Sambuc       // act exactly like l-values but are formally required to be
781f4a2713aSLionel Sambuc       // r-values in C.
782f4a2713aSLionel Sambuc       return expr->isGLValue() ||
783*0a6a1f1dSLionel Sambuc              expr->getType()->isFunctionType() ||
784*0a6a1f1dSLionel Sambuc              hasAggregateEvaluationKind(expr->getType());
785f4a2713aSLionel Sambuc     }
786f4a2713aSLionel Sambuc 
bind(CodeGenFunction & CGF,const OpaqueValueExpr * ov,const Expr * e)787f4a2713aSLionel Sambuc     static OpaqueValueMappingData bind(CodeGenFunction &CGF,
788f4a2713aSLionel Sambuc                                        const OpaqueValueExpr *ov,
789f4a2713aSLionel Sambuc                                        const Expr *e) {
790f4a2713aSLionel Sambuc       if (shouldBindAsLValue(ov))
791f4a2713aSLionel Sambuc         return bind(CGF, ov, CGF.EmitLValue(e));
792f4a2713aSLionel Sambuc       return bind(CGF, ov, CGF.EmitAnyExpr(e));
793f4a2713aSLionel Sambuc     }
794f4a2713aSLionel Sambuc 
bind(CodeGenFunction & CGF,const OpaqueValueExpr * ov,const LValue & lv)795f4a2713aSLionel Sambuc     static OpaqueValueMappingData bind(CodeGenFunction &CGF,
796f4a2713aSLionel Sambuc                                        const OpaqueValueExpr *ov,
797f4a2713aSLionel Sambuc                                        const LValue &lv) {
798f4a2713aSLionel Sambuc       assert(shouldBindAsLValue(ov));
799f4a2713aSLionel Sambuc       CGF.OpaqueLValues.insert(std::make_pair(ov, lv));
800f4a2713aSLionel Sambuc       return OpaqueValueMappingData(ov, true);
801f4a2713aSLionel Sambuc     }
802f4a2713aSLionel Sambuc 
bind(CodeGenFunction & CGF,const OpaqueValueExpr * ov,const RValue & rv)803f4a2713aSLionel Sambuc     static OpaqueValueMappingData bind(CodeGenFunction &CGF,
804f4a2713aSLionel Sambuc                                        const OpaqueValueExpr *ov,
805f4a2713aSLionel Sambuc                                        const RValue &rv) {
806f4a2713aSLionel Sambuc       assert(!shouldBindAsLValue(ov));
807f4a2713aSLionel Sambuc       CGF.OpaqueRValues.insert(std::make_pair(ov, rv));
808f4a2713aSLionel Sambuc 
809f4a2713aSLionel Sambuc       OpaqueValueMappingData data(ov, false);
810f4a2713aSLionel Sambuc 
811f4a2713aSLionel Sambuc       // Work around an extremely aggressive peephole optimization in
812f4a2713aSLionel Sambuc       // EmitScalarConversion which assumes that all other uses of a
813f4a2713aSLionel Sambuc       // value are extant.
814f4a2713aSLionel Sambuc       data.Protection = CGF.protectFromPeepholes(rv);
815f4a2713aSLionel Sambuc 
816f4a2713aSLionel Sambuc       return data;
817f4a2713aSLionel Sambuc     }
818f4a2713aSLionel Sambuc 
isValid()819*0a6a1f1dSLionel Sambuc     bool isValid() const { return OpaqueValue != nullptr; }
clear()820*0a6a1f1dSLionel Sambuc     void clear() { OpaqueValue = nullptr; }
821f4a2713aSLionel Sambuc 
unbind(CodeGenFunction & CGF)822f4a2713aSLionel Sambuc     void unbind(CodeGenFunction &CGF) {
823f4a2713aSLionel Sambuc       assert(OpaqueValue && "no data to unbind!");
824f4a2713aSLionel Sambuc 
825f4a2713aSLionel Sambuc       if (BoundLValue) {
826f4a2713aSLionel Sambuc         CGF.OpaqueLValues.erase(OpaqueValue);
827f4a2713aSLionel Sambuc       } else {
828f4a2713aSLionel Sambuc         CGF.OpaqueRValues.erase(OpaqueValue);
829f4a2713aSLionel Sambuc         CGF.unprotectFromPeepholes(Protection);
830f4a2713aSLionel Sambuc       }
831f4a2713aSLionel Sambuc     }
832f4a2713aSLionel Sambuc   };
833f4a2713aSLionel Sambuc 
834f4a2713aSLionel Sambuc   /// An RAII object to set (and then clear) a mapping for an OpaqueValueExpr.
835f4a2713aSLionel Sambuc   class OpaqueValueMapping {
836f4a2713aSLionel Sambuc     CodeGenFunction &CGF;
837f4a2713aSLionel Sambuc     OpaqueValueMappingData Data;
838f4a2713aSLionel Sambuc 
839f4a2713aSLionel Sambuc   public:
shouldBindAsLValue(const Expr * expr)840f4a2713aSLionel Sambuc     static bool shouldBindAsLValue(const Expr *expr) {
841f4a2713aSLionel Sambuc       return OpaqueValueMappingData::shouldBindAsLValue(expr);
842f4a2713aSLionel Sambuc     }
843f4a2713aSLionel Sambuc 
844f4a2713aSLionel Sambuc     /// Build the opaque value mapping for the given conditional
845f4a2713aSLionel Sambuc     /// operator if it's the GNU ?: extension.  This is a common
846f4a2713aSLionel Sambuc     /// enough pattern that the convenience operator is really
847f4a2713aSLionel Sambuc     /// helpful.
848f4a2713aSLionel Sambuc     ///
OpaqueValueMapping(CodeGenFunction & CGF,const AbstractConditionalOperator * op)849f4a2713aSLionel Sambuc     OpaqueValueMapping(CodeGenFunction &CGF,
850f4a2713aSLionel Sambuc                        const AbstractConditionalOperator *op) : CGF(CGF) {
851f4a2713aSLionel Sambuc       if (isa<ConditionalOperator>(op))
852f4a2713aSLionel Sambuc         // Leave Data empty.
853f4a2713aSLionel Sambuc         return;
854f4a2713aSLionel Sambuc 
855f4a2713aSLionel Sambuc       const BinaryConditionalOperator *e = cast<BinaryConditionalOperator>(op);
856f4a2713aSLionel Sambuc       Data = OpaqueValueMappingData::bind(CGF, e->getOpaqueValue(),
857f4a2713aSLionel Sambuc                                           e->getCommon());
858f4a2713aSLionel Sambuc     }
859f4a2713aSLionel Sambuc 
OpaqueValueMapping(CodeGenFunction & CGF,const OpaqueValueExpr * opaqueValue,LValue lvalue)860f4a2713aSLionel Sambuc     OpaqueValueMapping(CodeGenFunction &CGF,
861f4a2713aSLionel Sambuc                        const OpaqueValueExpr *opaqueValue,
862f4a2713aSLionel Sambuc                        LValue lvalue)
863f4a2713aSLionel Sambuc       : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, lvalue)) {
864f4a2713aSLionel Sambuc     }
865f4a2713aSLionel Sambuc 
OpaqueValueMapping(CodeGenFunction & CGF,const OpaqueValueExpr * opaqueValue,RValue rvalue)866f4a2713aSLionel Sambuc     OpaqueValueMapping(CodeGenFunction &CGF,
867f4a2713aSLionel Sambuc                        const OpaqueValueExpr *opaqueValue,
868f4a2713aSLionel Sambuc                        RValue rvalue)
869f4a2713aSLionel Sambuc       : CGF(CGF), Data(OpaqueValueMappingData::bind(CGF, opaqueValue, rvalue)) {
870f4a2713aSLionel Sambuc     }
871f4a2713aSLionel Sambuc 
pop()872f4a2713aSLionel Sambuc     void pop() {
873f4a2713aSLionel Sambuc       Data.unbind(CGF);
874f4a2713aSLionel Sambuc       Data.clear();
875f4a2713aSLionel Sambuc     }
876f4a2713aSLionel Sambuc 
~OpaqueValueMapping()877f4a2713aSLionel Sambuc     ~OpaqueValueMapping() {
878f4a2713aSLionel Sambuc       if (Data.isValid()) Data.unbind(CGF);
879f4a2713aSLionel Sambuc     }
880f4a2713aSLionel Sambuc   };
881f4a2713aSLionel Sambuc 
882f4a2713aSLionel Sambuc   /// getByrefValueFieldNumber - Given a declaration, returns the LLVM field
883f4a2713aSLionel Sambuc   /// number that holds the value.
884f4a2713aSLionel Sambuc   unsigned getByRefValueLLVMField(const ValueDecl *VD) const;
885f4a2713aSLionel Sambuc 
886f4a2713aSLionel Sambuc   /// BuildBlockByrefAddress - Computes address location of the
887f4a2713aSLionel Sambuc   /// variable which is declared as __block.
888f4a2713aSLionel Sambuc   llvm::Value *BuildBlockByrefAddress(llvm::Value *BaseAddr,
889f4a2713aSLionel Sambuc                                       const VarDecl *V);
890f4a2713aSLionel Sambuc private:
891f4a2713aSLionel Sambuc   CGDebugInfo *DebugInfo;
892f4a2713aSLionel Sambuc   bool DisableDebugInfo;
893f4a2713aSLionel Sambuc 
894f4a2713aSLionel Sambuc   /// DidCallStackSave - Whether llvm.stacksave has been called. Used to avoid
895f4a2713aSLionel Sambuc   /// calling llvm.stacksave for multiple VLAs in the same scope.
896f4a2713aSLionel Sambuc   bool DidCallStackSave;
897f4a2713aSLionel Sambuc 
898f4a2713aSLionel Sambuc   /// IndirectBranch - The first time an indirect goto is seen we create a block
899f4a2713aSLionel Sambuc   /// with an indirect branch.  Every time we see the address of a label taken,
900f4a2713aSLionel Sambuc   /// we add the label to the indirect goto.  Every subsequent indirect goto is
901f4a2713aSLionel Sambuc   /// codegen'd as a jump to the IndirectBranch's basic block.
902f4a2713aSLionel Sambuc   llvm::IndirectBrInst *IndirectBranch;
903f4a2713aSLionel Sambuc 
904f4a2713aSLionel Sambuc   /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
905f4a2713aSLionel Sambuc   /// decls.
906f4a2713aSLionel Sambuc   typedef llvm::DenseMap<const Decl*, llvm::Value*> DeclMapTy;
907f4a2713aSLionel Sambuc   DeclMapTy LocalDeclMap;
908f4a2713aSLionel Sambuc 
909f4a2713aSLionel Sambuc   /// LabelMap - This keeps track of the LLVM basic block for each C label.
910f4a2713aSLionel Sambuc   llvm::DenseMap<const LabelDecl*, JumpDest> LabelMap;
911f4a2713aSLionel Sambuc 
912f4a2713aSLionel Sambuc   // BreakContinueStack - This keeps track of where break and continue
913f4a2713aSLionel Sambuc   // statements should jump to.
914f4a2713aSLionel Sambuc   struct BreakContinue {
BreakContinueBreakContinue915f4a2713aSLionel Sambuc     BreakContinue(JumpDest Break, JumpDest Continue)
916f4a2713aSLionel Sambuc       : BreakBlock(Break), ContinueBlock(Continue) {}
917f4a2713aSLionel Sambuc 
918f4a2713aSLionel Sambuc     JumpDest BreakBlock;
919f4a2713aSLionel Sambuc     JumpDest ContinueBlock;
920f4a2713aSLionel Sambuc   };
921f4a2713aSLionel Sambuc   SmallVector<BreakContinue, 8> BreakContinueStack;
922f4a2713aSLionel Sambuc 
923*0a6a1f1dSLionel Sambuc   CodeGenPGO PGO;
924*0a6a1f1dSLionel Sambuc 
925*0a6a1f1dSLionel Sambuc public:
926*0a6a1f1dSLionel Sambuc   /// Get a counter for instrumentation of the region associated with the given
927*0a6a1f1dSLionel Sambuc   /// statement.
getPGORegionCounter(const Stmt * S)928*0a6a1f1dSLionel Sambuc   RegionCounter getPGORegionCounter(const Stmt *S) {
929*0a6a1f1dSLionel Sambuc     return RegionCounter(PGO, S);
930*0a6a1f1dSLionel Sambuc   }
931*0a6a1f1dSLionel Sambuc private:
932*0a6a1f1dSLionel Sambuc 
933f4a2713aSLionel Sambuc   /// SwitchInsn - This is nearest current switch instruction. It is null if
934f4a2713aSLionel Sambuc   /// current context is not in a switch.
935f4a2713aSLionel Sambuc   llvm::SwitchInst *SwitchInsn;
936*0a6a1f1dSLionel Sambuc   /// The branch weights of SwitchInsn when doing instrumentation based PGO.
937*0a6a1f1dSLionel Sambuc   SmallVector<uint64_t, 16> *SwitchWeights;
938f4a2713aSLionel Sambuc 
939f4a2713aSLionel Sambuc   /// CaseRangeBlock - This block holds if condition check for last case
940f4a2713aSLionel Sambuc   /// statement range in current switch instruction.
941f4a2713aSLionel Sambuc   llvm::BasicBlock *CaseRangeBlock;
942f4a2713aSLionel Sambuc 
943f4a2713aSLionel Sambuc   /// OpaqueLValues - Keeps track of the current set of opaque value
944f4a2713aSLionel Sambuc   /// expressions.
945f4a2713aSLionel Sambuc   llvm::DenseMap<const OpaqueValueExpr *, LValue> OpaqueLValues;
946f4a2713aSLionel Sambuc   llvm::DenseMap<const OpaqueValueExpr *, RValue> OpaqueRValues;
947f4a2713aSLionel Sambuc 
948f4a2713aSLionel Sambuc   // VLASizeMap - This keeps track of the associated size for each VLA type.
949f4a2713aSLionel Sambuc   // We track this by the size expression rather than the type itself because
950f4a2713aSLionel Sambuc   // in certain situations, like a const qualifier applied to an VLA typedef,
951f4a2713aSLionel Sambuc   // multiple VLA types can share the same size expression.
952f4a2713aSLionel Sambuc   // FIXME: Maybe this could be a stack of maps that is pushed/popped as we
953f4a2713aSLionel Sambuc   // enter/leave scopes.
954f4a2713aSLionel Sambuc   llvm::DenseMap<const Expr*, llvm::Value*> VLASizeMap;
955f4a2713aSLionel Sambuc 
956f4a2713aSLionel Sambuc   /// A block containing a single 'unreachable' instruction.  Created
957f4a2713aSLionel Sambuc   /// lazily by getUnreachableBlock().
958f4a2713aSLionel Sambuc   llvm::BasicBlock *UnreachableBlock;
959f4a2713aSLionel Sambuc 
960f4a2713aSLionel Sambuc   /// Counts of the number return expressions in the function.
961f4a2713aSLionel Sambuc   unsigned NumReturnExprs;
962f4a2713aSLionel Sambuc 
963f4a2713aSLionel Sambuc   /// Count the number of simple (constant) return expressions in the function.
964f4a2713aSLionel Sambuc   unsigned NumSimpleReturnExprs;
965f4a2713aSLionel Sambuc 
966f4a2713aSLionel Sambuc   /// The last regular (non-return) debug location (breakpoint) in the function.
967f4a2713aSLionel Sambuc   SourceLocation LastStopPoint;
968f4a2713aSLionel Sambuc 
969f4a2713aSLionel Sambuc public:
970f4a2713aSLionel Sambuc   /// A scope within which we are constructing the fields of an object which
971f4a2713aSLionel Sambuc   /// might use a CXXDefaultInitExpr. This stashes away a 'this' value to use
972f4a2713aSLionel Sambuc   /// if we need to evaluate a CXXDefaultInitExpr within the evaluation.
973f4a2713aSLionel Sambuc   class FieldConstructionScope {
974f4a2713aSLionel Sambuc   public:
FieldConstructionScope(CodeGenFunction & CGF,llvm::Value * This)975f4a2713aSLionel Sambuc     FieldConstructionScope(CodeGenFunction &CGF, llvm::Value *This)
976f4a2713aSLionel Sambuc         : CGF(CGF), OldCXXDefaultInitExprThis(CGF.CXXDefaultInitExprThis) {
977f4a2713aSLionel Sambuc       CGF.CXXDefaultInitExprThis = This;
978f4a2713aSLionel Sambuc     }
~FieldConstructionScope()979f4a2713aSLionel Sambuc     ~FieldConstructionScope() {
980f4a2713aSLionel Sambuc       CGF.CXXDefaultInitExprThis = OldCXXDefaultInitExprThis;
981f4a2713aSLionel Sambuc     }
982f4a2713aSLionel Sambuc 
983f4a2713aSLionel Sambuc   private:
984f4a2713aSLionel Sambuc     CodeGenFunction &CGF;
985f4a2713aSLionel Sambuc     llvm::Value *OldCXXDefaultInitExprThis;
986f4a2713aSLionel Sambuc   };
987f4a2713aSLionel Sambuc 
988f4a2713aSLionel Sambuc   /// The scope of a CXXDefaultInitExpr. Within this scope, the value of 'this'
989f4a2713aSLionel Sambuc   /// is overridden to be the object under construction.
990f4a2713aSLionel Sambuc   class CXXDefaultInitExprScope {
991f4a2713aSLionel Sambuc   public:
CXXDefaultInitExprScope(CodeGenFunction & CGF)992f4a2713aSLionel Sambuc     CXXDefaultInitExprScope(CodeGenFunction &CGF)
993f4a2713aSLionel Sambuc         : CGF(CGF), OldCXXThisValue(CGF.CXXThisValue) {
994f4a2713aSLionel Sambuc       CGF.CXXThisValue = CGF.CXXDefaultInitExprThis;
995f4a2713aSLionel Sambuc     }
~CXXDefaultInitExprScope()996f4a2713aSLionel Sambuc     ~CXXDefaultInitExprScope() {
997f4a2713aSLionel Sambuc       CGF.CXXThisValue = OldCXXThisValue;
998f4a2713aSLionel Sambuc     }
999f4a2713aSLionel Sambuc 
1000f4a2713aSLionel Sambuc   public:
1001f4a2713aSLionel Sambuc     CodeGenFunction &CGF;
1002f4a2713aSLionel Sambuc     llvm::Value *OldCXXThisValue;
1003f4a2713aSLionel Sambuc   };
1004f4a2713aSLionel Sambuc 
1005f4a2713aSLionel Sambuc private:
1006f4a2713aSLionel Sambuc   /// CXXThisDecl - When generating code for a C++ member function,
1007f4a2713aSLionel Sambuc   /// this will hold the implicit 'this' declaration.
1008f4a2713aSLionel Sambuc   ImplicitParamDecl *CXXABIThisDecl;
1009f4a2713aSLionel Sambuc   llvm::Value *CXXABIThisValue;
1010f4a2713aSLionel Sambuc   llvm::Value *CXXThisValue;
1011f4a2713aSLionel Sambuc 
1012f4a2713aSLionel Sambuc   /// The value of 'this' to use when evaluating CXXDefaultInitExprs within
1013f4a2713aSLionel Sambuc   /// this expression.
1014f4a2713aSLionel Sambuc   llvm::Value *CXXDefaultInitExprThis;
1015f4a2713aSLionel Sambuc 
1016f4a2713aSLionel Sambuc   /// CXXStructorImplicitParamDecl - When generating code for a constructor or
1017f4a2713aSLionel Sambuc   /// destructor, this will hold the implicit argument (e.g. VTT).
1018f4a2713aSLionel Sambuc   ImplicitParamDecl *CXXStructorImplicitParamDecl;
1019f4a2713aSLionel Sambuc   llvm::Value *CXXStructorImplicitParamValue;
1020f4a2713aSLionel Sambuc 
1021f4a2713aSLionel Sambuc   /// OutermostConditional - Points to the outermost active
1022f4a2713aSLionel Sambuc   /// conditional control.  This is used so that we know if a
1023f4a2713aSLionel Sambuc   /// temporary should be destroyed conditionally.
1024f4a2713aSLionel Sambuc   ConditionalEvaluation *OutermostConditional;
1025f4a2713aSLionel Sambuc 
1026f4a2713aSLionel Sambuc   /// The current lexical scope.
1027f4a2713aSLionel Sambuc   LexicalScope *CurLexicalScope;
1028f4a2713aSLionel Sambuc 
1029f4a2713aSLionel Sambuc   /// The current source location that should be used for exception
1030f4a2713aSLionel Sambuc   /// handling code.
1031f4a2713aSLionel Sambuc   SourceLocation CurEHLocation;
1032f4a2713aSLionel Sambuc 
1033f4a2713aSLionel Sambuc   /// ByrefValueInfoMap - For each __block variable, contains a pair of the LLVM
1034f4a2713aSLionel Sambuc   /// type as well as the field number that contains the actual data.
1035f4a2713aSLionel Sambuc   llvm::DenseMap<const ValueDecl *, std::pair<llvm::Type *,
1036f4a2713aSLionel Sambuc                                               unsigned> > ByRefValueInfo;
1037f4a2713aSLionel Sambuc 
1038f4a2713aSLionel Sambuc   llvm::BasicBlock *TerminateLandingPad;
1039f4a2713aSLionel Sambuc   llvm::BasicBlock *TerminateHandler;
1040f4a2713aSLionel Sambuc   llvm::BasicBlock *TrapBB;
1041f4a2713aSLionel Sambuc 
1042f4a2713aSLionel Sambuc   /// Add a kernel metadata node to the named metadata node 'opencl.kernels'.
1043f4a2713aSLionel Sambuc   /// In the kernel metadata node, reference the kernel function and metadata
1044f4a2713aSLionel Sambuc   /// nodes for its optional attribute qualifiers (OpenCL 1.1 6.7.2):
1045f4a2713aSLionel Sambuc   /// - A node for the vec_type_hint(<type>) qualifier contains string
1046f4a2713aSLionel Sambuc   ///   "vec_type_hint", an undefined value of the <type> data type,
1047f4a2713aSLionel Sambuc   ///   and a Boolean that is true if the <type> is integer and signed.
1048f4a2713aSLionel Sambuc   /// - A node for the work_group_size_hint(X,Y,Z) qualifier contains string
1049f4a2713aSLionel Sambuc   ///   "work_group_size_hint", and three 32-bit integers X, Y and Z.
1050f4a2713aSLionel Sambuc   /// - A node for the reqd_work_group_size(X,Y,Z) qualifier contains string
1051f4a2713aSLionel Sambuc   ///   "reqd_work_group_size", and three 32-bit integers X, Y and Z.
1052f4a2713aSLionel Sambuc   void EmitOpenCLKernelMetadata(const FunctionDecl *FD,
1053f4a2713aSLionel Sambuc                                 llvm::Function *Fn);
1054f4a2713aSLionel Sambuc 
1055f4a2713aSLionel Sambuc public:
1056f4a2713aSLionel Sambuc   CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext=false);
1057f4a2713aSLionel Sambuc   ~CodeGenFunction();
1058f4a2713aSLionel Sambuc 
getTypes()1059f4a2713aSLionel Sambuc   CodeGenTypes &getTypes() const { return CGM.getTypes(); }
getContext()1060f4a2713aSLionel Sambuc   ASTContext &getContext() const { return CGM.getContext(); }
getDebugInfo()1061f4a2713aSLionel Sambuc   CGDebugInfo *getDebugInfo() {
1062f4a2713aSLionel Sambuc     if (DisableDebugInfo)
1063*0a6a1f1dSLionel Sambuc       return nullptr;
1064f4a2713aSLionel Sambuc     return DebugInfo;
1065f4a2713aSLionel Sambuc   }
disableDebugInfo()1066f4a2713aSLionel Sambuc   void disableDebugInfo() { DisableDebugInfo = true; }
enableDebugInfo()1067f4a2713aSLionel Sambuc   void enableDebugInfo() { DisableDebugInfo = false; }
1068f4a2713aSLionel Sambuc 
shouldUseFusedARCCalls()1069f4a2713aSLionel Sambuc   bool shouldUseFusedARCCalls() {
1070f4a2713aSLionel Sambuc     return CGM.getCodeGenOpts().OptimizationLevel == 0;
1071f4a2713aSLionel Sambuc   }
1072f4a2713aSLionel Sambuc 
getLangOpts()1073f4a2713aSLionel Sambuc   const LangOptions &getLangOpts() const { return CGM.getLangOpts(); }
1074f4a2713aSLionel Sambuc 
1075f4a2713aSLionel Sambuc   /// Returns a pointer to the function's exception object and selector slot,
1076f4a2713aSLionel Sambuc   /// which is assigned in every landing pad.
1077f4a2713aSLionel Sambuc   llvm::Value *getExceptionSlot();
1078f4a2713aSLionel Sambuc   llvm::Value *getEHSelectorSlot();
1079f4a2713aSLionel Sambuc 
1080f4a2713aSLionel Sambuc   /// Returns the contents of the function's exception object and selector
1081f4a2713aSLionel Sambuc   /// slots.
1082f4a2713aSLionel Sambuc   llvm::Value *getExceptionFromSlot();
1083f4a2713aSLionel Sambuc   llvm::Value *getSelectorFromSlot();
1084f4a2713aSLionel Sambuc 
1085f4a2713aSLionel Sambuc   llvm::Value *getNormalCleanupDestSlot();
1086f4a2713aSLionel Sambuc 
getUnreachableBlock()1087f4a2713aSLionel Sambuc   llvm::BasicBlock *getUnreachableBlock() {
1088f4a2713aSLionel Sambuc     if (!UnreachableBlock) {
1089f4a2713aSLionel Sambuc       UnreachableBlock = createBasicBlock("unreachable");
1090f4a2713aSLionel Sambuc       new llvm::UnreachableInst(getLLVMContext(), UnreachableBlock);
1091f4a2713aSLionel Sambuc     }
1092f4a2713aSLionel Sambuc     return UnreachableBlock;
1093f4a2713aSLionel Sambuc   }
1094f4a2713aSLionel Sambuc 
getInvokeDest()1095f4a2713aSLionel Sambuc   llvm::BasicBlock *getInvokeDest() {
1096*0a6a1f1dSLionel Sambuc     if (!EHStack.requiresLandingPad()) return nullptr;
1097f4a2713aSLionel Sambuc     return getInvokeDestImpl();
1098f4a2713aSLionel Sambuc   }
1099f4a2713aSLionel Sambuc 
getTarget()1100f4a2713aSLionel Sambuc   const TargetInfo &getTarget() const { return Target; }
getLLVMContext()1101f4a2713aSLionel Sambuc   llvm::LLVMContext &getLLVMContext() { return CGM.getLLVMContext(); }
1102f4a2713aSLionel Sambuc 
1103f4a2713aSLionel Sambuc   //===--------------------------------------------------------------------===//
1104f4a2713aSLionel Sambuc   //                                  Cleanups
1105f4a2713aSLionel Sambuc   //===--------------------------------------------------------------------===//
1106f4a2713aSLionel Sambuc 
1107f4a2713aSLionel Sambuc   typedef void Destroyer(CodeGenFunction &CGF, llvm::Value *addr, QualType ty);
1108f4a2713aSLionel Sambuc 
1109f4a2713aSLionel Sambuc   void pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin,
1110f4a2713aSLionel Sambuc                                         llvm::Value *arrayEndPointer,
1111f4a2713aSLionel Sambuc                                         QualType elementType,
1112f4a2713aSLionel Sambuc                                         Destroyer *destroyer);
1113f4a2713aSLionel Sambuc   void pushRegularPartialArrayCleanup(llvm::Value *arrayBegin,
1114f4a2713aSLionel Sambuc                                       llvm::Value *arrayEnd,
1115f4a2713aSLionel Sambuc                                       QualType elementType,
1116f4a2713aSLionel Sambuc                                       Destroyer *destroyer);
1117f4a2713aSLionel Sambuc 
1118f4a2713aSLionel Sambuc   void pushDestroy(QualType::DestructionKind dtorKind,
1119f4a2713aSLionel Sambuc                    llvm::Value *addr, QualType type);
1120f4a2713aSLionel Sambuc   void pushEHDestroy(QualType::DestructionKind dtorKind,
1121f4a2713aSLionel Sambuc                      llvm::Value *addr, QualType type);
1122f4a2713aSLionel Sambuc   void pushDestroy(CleanupKind kind, llvm::Value *addr, QualType type,
1123f4a2713aSLionel Sambuc                    Destroyer *destroyer, bool useEHCleanupForArray);
1124f4a2713aSLionel Sambuc   void pushLifetimeExtendedDestroy(CleanupKind kind, llvm::Value *addr,
1125f4a2713aSLionel Sambuc                                    QualType type, Destroyer *destroyer,
1126f4a2713aSLionel Sambuc                                    bool useEHCleanupForArray);
1127*0a6a1f1dSLionel Sambuc   void pushCallObjectDeleteCleanup(const FunctionDecl *OperatorDelete,
1128*0a6a1f1dSLionel Sambuc                                    llvm::Value *CompletePtr,
1129*0a6a1f1dSLionel Sambuc                                    QualType ElementType);
1130*0a6a1f1dSLionel Sambuc   void pushStackRestore(CleanupKind kind, llvm::Value *SPMem);
1131f4a2713aSLionel Sambuc   void emitDestroy(llvm::Value *addr, QualType type, Destroyer *destroyer,
1132f4a2713aSLionel Sambuc                    bool useEHCleanupForArray);
1133f4a2713aSLionel Sambuc   llvm::Function *generateDestroyHelper(llvm::Constant *addr, QualType type,
1134f4a2713aSLionel Sambuc                                         Destroyer *destroyer,
1135f4a2713aSLionel Sambuc                                         bool useEHCleanupForArray,
1136f4a2713aSLionel Sambuc                                         const VarDecl *VD);
1137f4a2713aSLionel Sambuc   void emitArrayDestroy(llvm::Value *begin, llvm::Value *end,
1138f4a2713aSLionel Sambuc                         QualType type, Destroyer *destroyer,
1139f4a2713aSLionel Sambuc                         bool checkZeroLength, bool useEHCleanup);
1140f4a2713aSLionel Sambuc 
1141f4a2713aSLionel Sambuc   Destroyer *getDestroyer(QualType::DestructionKind destructionKind);
1142f4a2713aSLionel Sambuc 
1143f4a2713aSLionel Sambuc   /// Determines whether an EH cleanup is required to destroy a type
1144f4a2713aSLionel Sambuc   /// with the given destruction kind.
needsEHCleanup(QualType::DestructionKind kind)1145f4a2713aSLionel Sambuc   bool needsEHCleanup(QualType::DestructionKind kind) {
1146f4a2713aSLionel Sambuc     switch (kind) {
1147f4a2713aSLionel Sambuc     case QualType::DK_none:
1148f4a2713aSLionel Sambuc       return false;
1149f4a2713aSLionel Sambuc     case QualType::DK_cxx_destructor:
1150f4a2713aSLionel Sambuc     case QualType::DK_objc_weak_lifetime:
1151f4a2713aSLionel Sambuc       return getLangOpts().Exceptions;
1152f4a2713aSLionel Sambuc     case QualType::DK_objc_strong_lifetime:
1153f4a2713aSLionel Sambuc       return getLangOpts().Exceptions &&
1154f4a2713aSLionel Sambuc              CGM.getCodeGenOpts().ObjCAutoRefCountExceptions;
1155f4a2713aSLionel Sambuc     }
1156f4a2713aSLionel Sambuc     llvm_unreachable("bad destruction kind");
1157f4a2713aSLionel Sambuc   }
1158f4a2713aSLionel Sambuc 
getCleanupKind(QualType::DestructionKind kind)1159f4a2713aSLionel Sambuc   CleanupKind getCleanupKind(QualType::DestructionKind kind) {
1160f4a2713aSLionel Sambuc     return (needsEHCleanup(kind) ? NormalAndEHCleanup : NormalCleanup);
1161f4a2713aSLionel Sambuc   }
1162f4a2713aSLionel Sambuc 
1163f4a2713aSLionel Sambuc   //===--------------------------------------------------------------------===//
1164f4a2713aSLionel Sambuc   //                                  Objective-C
1165f4a2713aSLionel Sambuc   //===--------------------------------------------------------------------===//
1166f4a2713aSLionel Sambuc 
1167f4a2713aSLionel Sambuc   void GenerateObjCMethod(const ObjCMethodDecl *OMD);
1168f4a2713aSLionel Sambuc 
1169*0a6a1f1dSLionel Sambuc   void StartObjCMethod(const ObjCMethodDecl *MD, const ObjCContainerDecl *CD);
1170f4a2713aSLionel Sambuc 
1171f4a2713aSLionel Sambuc   /// GenerateObjCGetter - Synthesize an Objective-C property getter function.
1172f4a2713aSLionel Sambuc   void GenerateObjCGetter(ObjCImplementationDecl *IMP,
1173f4a2713aSLionel Sambuc                           const ObjCPropertyImplDecl *PID);
1174f4a2713aSLionel Sambuc   void generateObjCGetterBody(const ObjCImplementationDecl *classImpl,
1175f4a2713aSLionel Sambuc                               const ObjCPropertyImplDecl *propImpl,
1176f4a2713aSLionel Sambuc                               const ObjCMethodDecl *GetterMothodDecl,
1177f4a2713aSLionel Sambuc                               llvm::Constant *AtomicHelperFn);
1178f4a2713aSLionel Sambuc 
1179f4a2713aSLionel Sambuc   void GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
1180f4a2713aSLionel Sambuc                                   ObjCMethodDecl *MD, bool ctor);
1181f4a2713aSLionel Sambuc 
1182f4a2713aSLionel Sambuc   /// GenerateObjCSetter - Synthesize an Objective-C property setter function
1183f4a2713aSLionel Sambuc   /// for the given property.
1184f4a2713aSLionel Sambuc   void GenerateObjCSetter(ObjCImplementationDecl *IMP,
1185f4a2713aSLionel Sambuc                           const ObjCPropertyImplDecl *PID);
1186f4a2713aSLionel Sambuc   void generateObjCSetterBody(const ObjCImplementationDecl *classImpl,
1187f4a2713aSLionel Sambuc                               const ObjCPropertyImplDecl *propImpl,
1188f4a2713aSLionel Sambuc                               llvm::Constant *AtomicHelperFn);
1189f4a2713aSLionel Sambuc   bool IndirectObjCSetterArg(const CGFunctionInfo &FI);
1190f4a2713aSLionel Sambuc   bool IvarTypeWithAggrGCObjects(QualType Ty);
1191f4a2713aSLionel Sambuc 
1192f4a2713aSLionel Sambuc   //===--------------------------------------------------------------------===//
1193f4a2713aSLionel Sambuc   //                                  Block Bits
1194f4a2713aSLionel Sambuc   //===--------------------------------------------------------------------===//
1195f4a2713aSLionel Sambuc 
1196f4a2713aSLionel Sambuc   llvm::Value *EmitBlockLiteral(const BlockExpr *);
1197f4a2713aSLionel Sambuc   llvm::Value *EmitBlockLiteral(const CGBlockInfo &Info);
1198f4a2713aSLionel Sambuc   static void destroyBlockInfos(CGBlockInfo *info);
1199f4a2713aSLionel Sambuc   llvm::Constant *BuildDescriptorBlockDecl(const BlockExpr *,
1200f4a2713aSLionel Sambuc                                            const CGBlockInfo &Info,
1201f4a2713aSLionel Sambuc                                            llvm::StructType *,
1202f4a2713aSLionel Sambuc                                            llvm::Constant *BlockVarLayout);
1203f4a2713aSLionel Sambuc 
1204f4a2713aSLionel Sambuc   llvm::Function *GenerateBlockFunction(GlobalDecl GD,
1205f4a2713aSLionel Sambuc                                         const CGBlockInfo &Info,
1206f4a2713aSLionel Sambuc                                         const DeclMapTy &ldm,
1207f4a2713aSLionel Sambuc                                         bool IsLambdaConversionToBlock);
1208f4a2713aSLionel Sambuc 
1209f4a2713aSLionel Sambuc   llvm::Constant *GenerateCopyHelperFunction(const CGBlockInfo &blockInfo);
1210f4a2713aSLionel Sambuc   llvm::Constant *GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo);
1211f4a2713aSLionel Sambuc   llvm::Constant *GenerateObjCAtomicSetterCopyHelperFunction(
1212f4a2713aSLionel Sambuc                                              const ObjCPropertyImplDecl *PID);
1213f4a2713aSLionel Sambuc   llvm::Constant *GenerateObjCAtomicGetterCopyHelperFunction(
1214f4a2713aSLionel Sambuc                                              const ObjCPropertyImplDecl *PID);
1215f4a2713aSLionel Sambuc   llvm::Value *EmitBlockCopyAndAutorelease(llvm::Value *Block, QualType Ty);
1216f4a2713aSLionel Sambuc 
1217f4a2713aSLionel Sambuc   void BuildBlockRelease(llvm::Value *DeclPtr, BlockFieldFlags flags);
1218f4a2713aSLionel Sambuc 
1219f4a2713aSLionel Sambuc   class AutoVarEmission;
1220f4a2713aSLionel Sambuc 
1221f4a2713aSLionel Sambuc   void emitByrefStructureInit(const AutoVarEmission &emission);
1222f4a2713aSLionel Sambuc   void enterByrefCleanup(const AutoVarEmission &emission);
1223f4a2713aSLionel Sambuc 
LoadBlockStruct()1224f4a2713aSLionel Sambuc   llvm::Value *LoadBlockStruct() {
1225f4a2713aSLionel Sambuc     assert(BlockPointer && "no block pointer set!");
1226f4a2713aSLionel Sambuc     return BlockPointer;
1227f4a2713aSLionel Sambuc   }
1228f4a2713aSLionel Sambuc 
1229f4a2713aSLionel Sambuc   void AllocateBlockCXXThisPointer(const CXXThisExpr *E);
1230f4a2713aSLionel Sambuc   void AllocateBlockDecl(const DeclRefExpr *E);
1231f4a2713aSLionel Sambuc   llvm::Value *GetAddrOfBlockDecl(const VarDecl *var, bool ByRef);
1232f4a2713aSLionel Sambuc   llvm::Type *BuildByRefType(const VarDecl *var);
1233f4a2713aSLionel Sambuc 
1234f4a2713aSLionel Sambuc   void GenerateCode(GlobalDecl GD, llvm::Function *Fn,
1235f4a2713aSLionel Sambuc                     const CGFunctionInfo &FnInfo);
1236*0a6a1f1dSLionel Sambuc   /// \brief Emit code for the start of a function.
1237*0a6a1f1dSLionel Sambuc   /// \param Loc       The location to be associated with the function.
1238*0a6a1f1dSLionel Sambuc   /// \param StartLoc  The location of the function body.
1239f4a2713aSLionel Sambuc   void StartFunction(GlobalDecl GD,
1240f4a2713aSLionel Sambuc                      QualType RetTy,
1241f4a2713aSLionel Sambuc                      llvm::Function *Fn,
1242f4a2713aSLionel Sambuc                      const CGFunctionInfo &FnInfo,
1243f4a2713aSLionel Sambuc                      const FunctionArgList &Args,
1244*0a6a1f1dSLionel Sambuc                      SourceLocation Loc = SourceLocation(),
1245*0a6a1f1dSLionel Sambuc                      SourceLocation StartLoc = SourceLocation());
1246f4a2713aSLionel Sambuc 
1247f4a2713aSLionel Sambuc   void EmitConstructorBody(FunctionArgList &Args);
1248f4a2713aSLionel Sambuc   void EmitDestructorBody(FunctionArgList &Args);
1249f4a2713aSLionel Sambuc   void emitImplicitAssignmentOperatorBody(FunctionArgList &Args);
1250f4a2713aSLionel Sambuc   void EmitFunctionBody(FunctionArgList &Args, const Stmt *Body);
1251*0a6a1f1dSLionel Sambuc   void EmitBlockWithFallThrough(llvm::BasicBlock *BB, RegionCounter &Cnt);
1252f4a2713aSLionel Sambuc 
1253f4a2713aSLionel Sambuc   void EmitForwardingCallToLambda(const CXXMethodDecl *LambdaCallOperator,
1254f4a2713aSLionel Sambuc                                   CallArgList &CallArgs);
1255f4a2713aSLionel Sambuc   void EmitLambdaToBlockPointerBody(FunctionArgList &Args);
1256f4a2713aSLionel Sambuc   void EmitLambdaBlockInvokeBody();
1257f4a2713aSLionel Sambuc   void EmitLambdaDelegatingInvokeBody(const CXXMethodDecl *MD);
1258f4a2713aSLionel Sambuc   void EmitLambdaStaticInvokeFunction(const CXXMethodDecl *MD);
1259*0a6a1f1dSLionel Sambuc   void EmitAsanPrologueOrEpilogue(bool Prologue);
1260f4a2713aSLionel Sambuc 
1261f4a2713aSLionel Sambuc   /// EmitReturnBlock - Emit the unified return block, trying to avoid its
1262f4a2713aSLionel Sambuc   /// emission when possible.
1263*0a6a1f1dSLionel Sambuc   llvm::DebugLoc EmitReturnBlock();
1264f4a2713aSLionel Sambuc 
1265f4a2713aSLionel Sambuc   /// FinishFunction - Complete IR generation of the current function. It is
1266f4a2713aSLionel Sambuc   /// legal to call this function even if there is no current insertion point.
1267f4a2713aSLionel Sambuc   void FinishFunction(SourceLocation EndLoc=SourceLocation());
1268f4a2713aSLionel Sambuc 
1269f4a2713aSLionel Sambuc   void StartThunk(llvm::Function *Fn, GlobalDecl GD, const CGFunctionInfo &FnInfo);
1270f4a2713aSLionel Sambuc 
1271*0a6a1f1dSLionel Sambuc   void EmitCallAndReturnForThunk(llvm::Value *Callee, const ThunkInfo *Thunk);
1272*0a6a1f1dSLionel Sambuc 
1273*0a6a1f1dSLionel Sambuc   /// Emit a musttail call for a thunk with a potentially adjusted this pointer.
1274*0a6a1f1dSLionel Sambuc   void EmitMustTailThunk(const CXXMethodDecl *MD, llvm::Value *AdjustedThisPtr,
1275*0a6a1f1dSLionel Sambuc                          llvm::Value *Callee);
1276f4a2713aSLionel Sambuc 
1277f4a2713aSLionel Sambuc   /// GenerateThunk - Generate a thunk for the given method.
1278f4a2713aSLionel Sambuc   void GenerateThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo,
1279f4a2713aSLionel Sambuc                      GlobalDecl GD, const ThunkInfo &Thunk);
1280f4a2713aSLionel Sambuc 
1281f4a2713aSLionel Sambuc   void GenerateVarArgsThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo,
1282f4a2713aSLionel Sambuc                             GlobalDecl GD, const ThunkInfo &Thunk);
1283f4a2713aSLionel Sambuc 
1284f4a2713aSLionel Sambuc   void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type,
1285f4a2713aSLionel Sambuc                         FunctionArgList &Args);
1286f4a2713aSLionel Sambuc 
1287f4a2713aSLionel Sambuc   void EmitInitializerForField(FieldDecl *Field, LValue LHS, Expr *Init,
1288f4a2713aSLionel Sambuc                                ArrayRef<VarDecl *> ArrayIndexes);
1289f4a2713aSLionel Sambuc 
1290f4a2713aSLionel Sambuc   /// InitializeVTablePointer - Initialize the vtable pointer of the given
1291f4a2713aSLionel Sambuc   /// subobject.
1292f4a2713aSLionel Sambuc   ///
1293f4a2713aSLionel Sambuc   void InitializeVTablePointer(BaseSubobject Base,
1294f4a2713aSLionel Sambuc                                const CXXRecordDecl *NearestVBase,
1295f4a2713aSLionel Sambuc                                CharUnits OffsetFromNearestVBase,
1296f4a2713aSLionel Sambuc                                const CXXRecordDecl *VTableClass);
1297f4a2713aSLionel Sambuc 
1298f4a2713aSLionel Sambuc   typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
1299f4a2713aSLionel Sambuc   void InitializeVTablePointers(BaseSubobject Base,
1300f4a2713aSLionel Sambuc                                 const CXXRecordDecl *NearestVBase,
1301f4a2713aSLionel Sambuc                                 CharUnits OffsetFromNearestVBase,
1302f4a2713aSLionel Sambuc                                 bool BaseIsNonVirtualPrimaryBase,
1303f4a2713aSLionel Sambuc                                 const CXXRecordDecl *VTableClass,
1304f4a2713aSLionel Sambuc                                 VisitedVirtualBasesSetTy& VBases);
1305f4a2713aSLionel Sambuc 
1306f4a2713aSLionel Sambuc   void InitializeVTablePointers(const CXXRecordDecl *ClassDecl);
1307f4a2713aSLionel Sambuc 
1308f4a2713aSLionel Sambuc   /// GetVTablePtr - Return the Value of the vtable pointer member pointed
1309f4a2713aSLionel Sambuc   /// to by This.
1310f4a2713aSLionel Sambuc   llvm::Value *GetVTablePtr(llvm::Value *This, llvm::Type *Ty);
1311f4a2713aSLionel Sambuc 
1312f4a2713aSLionel Sambuc 
1313f4a2713aSLionel Sambuc   /// CanDevirtualizeMemberFunctionCalls - Checks whether virtual calls on given
1314f4a2713aSLionel Sambuc   /// expr can be devirtualized.
1315f4a2713aSLionel Sambuc   bool CanDevirtualizeMemberFunctionCall(const Expr *Base,
1316f4a2713aSLionel Sambuc                                          const CXXMethodDecl *MD);
1317f4a2713aSLionel Sambuc 
1318f4a2713aSLionel Sambuc   /// EnterDtorCleanups - Enter the cleanups necessary to complete the
1319f4a2713aSLionel Sambuc   /// given phase of destruction for a destructor.  The end result
1320f4a2713aSLionel Sambuc   /// should call destructors on members and base classes in reverse
1321f4a2713aSLionel Sambuc   /// order of their construction.
1322f4a2713aSLionel Sambuc   void EnterDtorCleanups(const CXXDestructorDecl *Dtor, CXXDtorType Type);
1323f4a2713aSLionel Sambuc 
1324f4a2713aSLionel Sambuc   /// ShouldInstrumentFunction - Return true if the current function should be
1325f4a2713aSLionel Sambuc   /// instrumented with __cyg_profile_func_* calls
1326f4a2713aSLionel Sambuc   bool ShouldInstrumentFunction();
1327f4a2713aSLionel Sambuc 
1328f4a2713aSLionel Sambuc   /// EmitFunctionInstrumentation - Emit LLVM code to call the specified
1329f4a2713aSLionel Sambuc   /// instrumentation function with the current function and the call site, if
1330f4a2713aSLionel Sambuc   /// function instrumentation is enabled.
1331f4a2713aSLionel Sambuc   void EmitFunctionInstrumentation(const char *Fn);
1332f4a2713aSLionel Sambuc 
1333f4a2713aSLionel Sambuc   /// EmitMCountInstrumentation - Emit call to .mcount.
1334f4a2713aSLionel Sambuc   void EmitMCountInstrumentation();
1335f4a2713aSLionel Sambuc 
1336f4a2713aSLionel Sambuc   /// EmitFunctionProlog - Emit the target specific LLVM code to load the
1337f4a2713aSLionel Sambuc   /// arguments for the given function. This is also responsible for naming the
1338f4a2713aSLionel Sambuc   /// LLVM function arguments.
1339f4a2713aSLionel Sambuc   void EmitFunctionProlog(const CGFunctionInfo &FI,
1340f4a2713aSLionel Sambuc                           llvm::Function *Fn,
1341f4a2713aSLionel Sambuc                           const FunctionArgList &Args);
1342f4a2713aSLionel Sambuc 
1343f4a2713aSLionel Sambuc   /// EmitFunctionEpilog - Emit the target specific LLVM code to return the
1344f4a2713aSLionel Sambuc   /// given temporary.
1345f4a2713aSLionel Sambuc   void EmitFunctionEpilog(const CGFunctionInfo &FI, bool EmitRetDbgLoc,
1346f4a2713aSLionel Sambuc                           SourceLocation EndLoc);
1347f4a2713aSLionel Sambuc 
1348f4a2713aSLionel Sambuc   /// EmitStartEHSpec - Emit the start of the exception spec.
1349f4a2713aSLionel Sambuc   void EmitStartEHSpec(const Decl *D);
1350f4a2713aSLionel Sambuc 
1351f4a2713aSLionel Sambuc   /// EmitEndEHSpec - Emit the end of the exception spec.
1352f4a2713aSLionel Sambuc   void EmitEndEHSpec(const Decl *D);
1353f4a2713aSLionel Sambuc 
1354f4a2713aSLionel Sambuc   /// getTerminateLandingPad - Return a landing pad that just calls terminate.
1355f4a2713aSLionel Sambuc   llvm::BasicBlock *getTerminateLandingPad();
1356f4a2713aSLionel Sambuc 
1357f4a2713aSLionel Sambuc   /// getTerminateHandler - Return a handler (not a landing pad, just
1358f4a2713aSLionel Sambuc   /// a catch handler) that just calls terminate.  This is used when
1359f4a2713aSLionel Sambuc   /// a terminate scope encloses a try.
1360f4a2713aSLionel Sambuc   llvm::BasicBlock *getTerminateHandler();
1361f4a2713aSLionel Sambuc 
1362f4a2713aSLionel Sambuc   llvm::Type *ConvertTypeForMem(QualType T);
1363f4a2713aSLionel Sambuc   llvm::Type *ConvertType(QualType T);
ConvertType(const TypeDecl * T)1364f4a2713aSLionel Sambuc   llvm::Type *ConvertType(const TypeDecl *T) {
1365f4a2713aSLionel Sambuc     return ConvertType(getContext().getTypeDeclType(T));
1366f4a2713aSLionel Sambuc   }
1367f4a2713aSLionel Sambuc 
1368f4a2713aSLionel Sambuc   /// LoadObjCSelf - Load the value of self. This function is only valid while
1369f4a2713aSLionel Sambuc   /// generating code for an Objective-C method.
1370f4a2713aSLionel Sambuc   llvm::Value *LoadObjCSelf();
1371f4a2713aSLionel Sambuc 
1372f4a2713aSLionel Sambuc   /// TypeOfSelfObject - Return type of object that this self represents.
1373f4a2713aSLionel Sambuc   QualType TypeOfSelfObject();
1374f4a2713aSLionel Sambuc 
1375f4a2713aSLionel Sambuc   /// hasAggregateLLVMType - Return true if the specified AST type will map into
1376f4a2713aSLionel Sambuc   /// an aggregate LLVM type or is void.
1377f4a2713aSLionel Sambuc   static TypeEvaluationKind getEvaluationKind(QualType T);
1378f4a2713aSLionel Sambuc 
hasScalarEvaluationKind(QualType T)1379f4a2713aSLionel Sambuc   static bool hasScalarEvaluationKind(QualType T) {
1380f4a2713aSLionel Sambuc     return getEvaluationKind(T) == TEK_Scalar;
1381f4a2713aSLionel Sambuc   }
1382f4a2713aSLionel Sambuc 
hasAggregateEvaluationKind(QualType T)1383f4a2713aSLionel Sambuc   static bool hasAggregateEvaluationKind(QualType T) {
1384f4a2713aSLionel Sambuc     return getEvaluationKind(T) == TEK_Aggregate;
1385f4a2713aSLionel Sambuc   }
1386f4a2713aSLionel Sambuc 
1387f4a2713aSLionel Sambuc   /// createBasicBlock - Create an LLVM basic block.
1388f4a2713aSLionel Sambuc   llvm::BasicBlock *createBasicBlock(const Twine &name = "",
1389*0a6a1f1dSLionel Sambuc                                      llvm::Function *parent = nullptr,
1390*0a6a1f1dSLionel Sambuc                                      llvm::BasicBlock *before = nullptr) {
1391f4a2713aSLionel Sambuc #ifdef NDEBUG
1392f4a2713aSLionel Sambuc     return llvm::BasicBlock::Create(getLLVMContext(), "", parent, before);
1393f4a2713aSLionel Sambuc #else
1394f4a2713aSLionel Sambuc     return llvm::BasicBlock::Create(getLLVMContext(), name, parent, before);
1395f4a2713aSLionel Sambuc #endif
1396f4a2713aSLionel Sambuc   }
1397f4a2713aSLionel Sambuc 
1398f4a2713aSLionel Sambuc   /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
1399f4a2713aSLionel Sambuc   /// label maps to.
1400f4a2713aSLionel Sambuc   JumpDest getJumpDestForLabel(const LabelDecl *S);
1401f4a2713aSLionel Sambuc 
1402f4a2713aSLionel Sambuc   /// SimplifyForwardingBlocks - If the given basic block is only a branch to
1403f4a2713aSLionel Sambuc   /// another basic block, simplify it. This assumes that no other code could
1404f4a2713aSLionel Sambuc   /// potentially reference the basic block.
1405f4a2713aSLionel Sambuc   void SimplifyForwardingBlocks(llvm::BasicBlock *BB);
1406f4a2713aSLionel Sambuc 
1407f4a2713aSLionel Sambuc   /// EmitBlock - Emit the given block \arg BB and set it as the insert point,
1408f4a2713aSLionel Sambuc   /// adding a fall-through branch from the current insert block if
1409f4a2713aSLionel Sambuc   /// necessary. It is legal to call this function even if there is no current
1410f4a2713aSLionel Sambuc   /// insertion point.
1411f4a2713aSLionel Sambuc   ///
1412f4a2713aSLionel Sambuc   /// IsFinished - If true, indicates that the caller has finished emitting
1413f4a2713aSLionel Sambuc   /// branches to the given block and does not expect to emit code into it. This
1414f4a2713aSLionel Sambuc   /// means the block can be ignored if it is unreachable.
1415f4a2713aSLionel Sambuc   void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false);
1416f4a2713aSLionel Sambuc 
1417f4a2713aSLionel Sambuc   /// EmitBlockAfterUses - Emit the given block somewhere hopefully
1418f4a2713aSLionel Sambuc   /// near its uses, and leave the insertion point in it.
1419f4a2713aSLionel Sambuc   void EmitBlockAfterUses(llvm::BasicBlock *BB);
1420f4a2713aSLionel Sambuc 
1421f4a2713aSLionel Sambuc   /// EmitBranch - Emit a branch to the specified basic block from the current
1422f4a2713aSLionel Sambuc   /// insert block, taking care to avoid creation of branches from dummy
1423f4a2713aSLionel Sambuc   /// blocks. It is legal to call this function even if there is no current
1424f4a2713aSLionel Sambuc   /// insertion point.
1425f4a2713aSLionel Sambuc   ///
1426f4a2713aSLionel Sambuc   /// This function clears the current insertion point. The caller should follow
1427f4a2713aSLionel Sambuc   /// calls to this function with calls to Emit*Block prior to generation new
1428f4a2713aSLionel Sambuc   /// code.
1429f4a2713aSLionel Sambuc   void EmitBranch(llvm::BasicBlock *Block);
1430f4a2713aSLionel Sambuc 
1431f4a2713aSLionel Sambuc   /// HaveInsertPoint - True if an insertion point is defined. If not, this
1432f4a2713aSLionel Sambuc   /// indicates that the current code being emitted is unreachable.
HaveInsertPoint()1433f4a2713aSLionel Sambuc   bool HaveInsertPoint() const {
1434*0a6a1f1dSLionel Sambuc     return Builder.GetInsertBlock() != nullptr;
1435f4a2713aSLionel Sambuc   }
1436f4a2713aSLionel Sambuc 
1437f4a2713aSLionel Sambuc   /// EnsureInsertPoint - Ensure that an insertion point is defined so that
1438f4a2713aSLionel Sambuc   /// emitted IR has a place to go. Note that by definition, if this function
1439f4a2713aSLionel Sambuc   /// creates a block then that block is unreachable; callers may do better to
1440f4a2713aSLionel Sambuc   /// detect when no insertion point is defined and simply skip IR generation.
EnsureInsertPoint()1441f4a2713aSLionel Sambuc   void EnsureInsertPoint() {
1442f4a2713aSLionel Sambuc     if (!HaveInsertPoint())
1443f4a2713aSLionel Sambuc       EmitBlock(createBasicBlock());
1444f4a2713aSLionel Sambuc   }
1445f4a2713aSLionel Sambuc 
1446f4a2713aSLionel Sambuc   /// ErrorUnsupported - Print out an error that codegen doesn't support the
1447f4a2713aSLionel Sambuc   /// specified stmt yet.
1448f4a2713aSLionel Sambuc   void ErrorUnsupported(const Stmt *S, const char *Type);
1449f4a2713aSLionel Sambuc 
1450f4a2713aSLionel Sambuc   //===--------------------------------------------------------------------===//
1451f4a2713aSLionel Sambuc   //                                  Helpers
1452f4a2713aSLionel Sambuc   //===--------------------------------------------------------------------===//
1453f4a2713aSLionel Sambuc 
1454f4a2713aSLionel Sambuc   LValue MakeAddrLValue(llvm::Value *V, QualType T,
1455f4a2713aSLionel Sambuc                         CharUnits Alignment = CharUnits()) {
1456f4a2713aSLionel Sambuc     return LValue::MakeAddr(V, T, Alignment, getContext(),
1457f4a2713aSLionel Sambuc                             CGM.getTBAAInfo(T));
1458f4a2713aSLionel Sambuc   }
1459f4a2713aSLionel Sambuc 
1460*0a6a1f1dSLionel Sambuc   LValue MakeNaturalAlignAddrLValue(llvm::Value *V, QualType T);
1461f4a2713aSLionel Sambuc 
1462f4a2713aSLionel Sambuc   /// CreateTempAlloca - This creates a alloca and inserts it into the entry
1463f4a2713aSLionel Sambuc   /// block. The caller is responsible for setting an appropriate alignment on
1464f4a2713aSLionel Sambuc   /// the alloca.
1465f4a2713aSLionel Sambuc   llvm::AllocaInst *CreateTempAlloca(llvm::Type *Ty,
1466f4a2713aSLionel Sambuc                                      const Twine &Name = "tmp");
1467f4a2713aSLionel Sambuc 
1468f4a2713aSLionel Sambuc   /// InitTempAlloca - Provide an initial value for the given alloca.
1469f4a2713aSLionel Sambuc   void InitTempAlloca(llvm::AllocaInst *Alloca, llvm::Value *Value);
1470f4a2713aSLionel Sambuc 
1471f4a2713aSLionel Sambuc   /// CreateIRTemp - Create a temporary IR object of the given type, with
1472f4a2713aSLionel Sambuc   /// appropriate alignment. This routine should only be used when an temporary
1473f4a2713aSLionel Sambuc   /// value needs to be stored into an alloca (for example, to avoid explicit
1474f4a2713aSLionel Sambuc   /// PHI construction), but the type is the IR type, not the type appropriate
1475f4a2713aSLionel Sambuc   /// for storing in memory.
1476f4a2713aSLionel Sambuc   llvm::AllocaInst *CreateIRTemp(QualType T, const Twine &Name = "tmp");
1477f4a2713aSLionel Sambuc 
1478f4a2713aSLionel Sambuc   /// CreateMemTemp - Create a temporary memory object of the given type, with
1479f4a2713aSLionel Sambuc   /// appropriate alignment.
1480f4a2713aSLionel Sambuc   llvm::AllocaInst *CreateMemTemp(QualType T, const Twine &Name = "tmp");
1481f4a2713aSLionel Sambuc 
1482f4a2713aSLionel Sambuc   /// CreateAggTemp - Create a temporary memory object for the given
1483f4a2713aSLionel Sambuc   /// aggregate type.
1484f4a2713aSLionel Sambuc   AggValueSlot CreateAggTemp(QualType T, const Twine &Name = "tmp") {
1485f4a2713aSLionel Sambuc     CharUnits Alignment = getContext().getTypeAlignInChars(T);
1486f4a2713aSLionel Sambuc     return AggValueSlot::forAddr(CreateMemTemp(T, Name), Alignment,
1487f4a2713aSLionel Sambuc                                  T.getQualifiers(),
1488f4a2713aSLionel Sambuc                                  AggValueSlot::IsNotDestructed,
1489f4a2713aSLionel Sambuc                                  AggValueSlot::DoesNotNeedGCBarriers,
1490f4a2713aSLionel Sambuc                                  AggValueSlot::IsNotAliased);
1491f4a2713aSLionel Sambuc   }
1492f4a2713aSLionel Sambuc 
1493*0a6a1f1dSLionel Sambuc   /// CreateInAllocaTmp - Create a temporary memory object for the given
1494*0a6a1f1dSLionel Sambuc   /// aggregate type.
1495*0a6a1f1dSLionel Sambuc   AggValueSlot CreateInAllocaTmp(QualType T, const Twine &Name = "inalloca");
1496*0a6a1f1dSLionel Sambuc 
1497f4a2713aSLionel Sambuc   /// Emit a cast to void* in the appropriate address space.
1498f4a2713aSLionel Sambuc   llvm::Value *EmitCastToVoidPtr(llvm::Value *value);
1499f4a2713aSLionel Sambuc 
1500f4a2713aSLionel Sambuc   /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
1501f4a2713aSLionel Sambuc   /// expression and compare the result against zero, returning an Int1Ty value.
1502f4a2713aSLionel Sambuc   llvm::Value *EvaluateExprAsBool(const Expr *E);
1503f4a2713aSLionel Sambuc 
1504f4a2713aSLionel Sambuc   /// EmitIgnoredExpr - Emit an expression in a context which ignores the result.
1505f4a2713aSLionel Sambuc   void EmitIgnoredExpr(const Expr *E);
1506f4a2713aSLionel Sambuc 
1507f4a2713aSLionel Sambuc   /// EmitAnyExpr - Emit code to compute the specified expression which can have
1508f4a2713aSLionel Sambuc   /// any type.  The result is returned as an RValue struct.  If this is an
1509f4a2713aSLionel Sambuc   /// aggregate expression, the aggloc/agglocvolatile arguments indicate where
1510f4a2713aSLionel Sambuc   /// the result should be returned.
1511f4a2713aSLionel Sambuc   ///
1512f4a2713aSLionel Sambuc   /// \param ignoreResult True if the resulting value isn't used.
1513f4a2713aSLionel Sambuc   RValue EmitAnyExpr(const Expr *E,
1514f4a2713aSLionel Sambuc                      AggValueSlot aggSlot = AggValueSlot::ignored(),
1515f4a2713aSLionel Sambuc                      bool ignoreResult = false);
1516f4a2713aSLionel Sambuc 
1517f4a2713aSLionel Sambuc   // EmitVAListRef - Emit a "reference" to a va_list; this is either the address
1518f4a2713aSLionel Sambuc   // or the value of the expression, depending on how va_list is defined.
1519f4a2713aSLionel Sambuc   llvm::Value *EmitVAListRef(const Expr *E);
1520f4a2713aSLionel Sambuc 
1521f4a2713aSLionel Sambuc   /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
1522f4a2713aSLionel Sambuc   /// always be accessible even if no aggregate location is provided.
1523f4a2713aSLionel Sambuc   RValue EmitAnyExprToTemp(const Expr *E);
1524f4a2713aSLionel Sambuc 
1525f4a2713aSLionel Sambuc   /// EmitAnyExprToMem - Emits the code necessary to evaluate an
1526f4a2713aSLionel Sambuc   /// arbitrary expression into the given memory location.
1527f4a2713aSLionel Sambuc   void EmitAnyExprToMem(const Expr *E, llvm::Value *Location,
1528f4a2713aSLionel Sambuc                         Qualifiers Quals, bool IsInitializer);
1529f4a2713aSLionel Sambuc 
1530f4a2713aSLionel Sambuc   /// EmitExprAsInit - Emits the code necessary to initialize a
1531f4a2713aSLionel Sambuc   /// location in memory with the given initializer.
1532*0a6a1f1dSLionel Sambuc   void EmitExprAsInit(const Expr *init, const ValueDecl *D, LValue lvalue,
1533*0a6a1f1dSLionel Sambuc                       bool capturedByInit);
1534f4a2713aSLionel Sambuc 
1535f4a2713aSLionel Sambuc   /// hasVolatileMember - returns true if aggregate type has a volatile
1536f4a2713aSLionel Sambuc   /// member.
hasVolatileMember(QualType T)1537f4a2713aSLionel Sambuc   bool hasVolatileMember(QualType T) {
1538f4a2713aSLionel Sambuc     if (const RecordType *RT = T->getAs<RecordType>()) {
1539f4a2713aSLionel Sambuc       const RecordDecl *RD = cast<RecordDecl>(RT->getDecl());
1540f4a2713aSLionel Sambuc       return RD->hasVolatileMember();
1541f4a2713aSLionel Sambuc     }
1542f4a2713aSLionel Sambuc     return false;
1543f4a2713aSLionel Sambuc   }
1544f4a2713aSLionel Sambuc   /// EmitAggregateCopy - Emit an aggregate assignment.
1545f4a2713aSLionel Sambuc   ///
1546f4a2713aSLionel Sambuc   /// The difference to EmitAggregateCopy is that tail padding is not copied.
1547f4a2713aSLionel Sambuc   /// This is required for correctness when assigning non-POD structures in C++.
EmitAggregateAssign(llvm::Value * DestPtr,llvm::Value * SrcPtr,QualType EltTy)1548f4a2713aSLionel Sambuc   void EmitAggregateAssign(llvm::Value *DestPtr, llvm::Value *SrcPtr,
1549f4a2713aSLionel Sambuc                            QualType EltTy) {
1550f4a2713aSLionel Sambuc     bool IsVolatile = hasVolatileMember(EltTy);
1551f4a2713aSLionel Sambuc     EmitAggregateCopy(DestPtr, SrcPtr, EltTy, IsVolatile, CharUnits::Zero(),
1552f4a2713aSLionel Sambuc                       true);
1553f4a2713aSLionel Sambuc   }
1554f4a2713aSLionel Sambuc 
1555f4a2713aSLionel Sambuc   /// EmitAggregateCopy - Emit an aggregate copy.
1556f4a2713aSLionel Sambuc   ///
1557f4a2713aSLionel Sambuc   /// \param isVolatile - True iff either the source or the destination is
1558f4a2713aSLionel Sambuc   /// volatile.
1559f4a2713aSLionel Sambuc   /// \param isAssignment - If false, allow padding to be copied.  This often
1560f4a2713aSLionel Sambuc   /// yields more efficient.
1561f4a2713aSLionel Sambuc   void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
1562f4a2713aSLionel Sambuc                          QualType EltTy, bool isVolatile=false,
1563f4a2713aSLionel Sambuc                          CharUnits Alignment = CharUnits::Zero(),
1564f4a2713aSLionel Sambuc                          bool isAssignment = false);
1565f4a2713aSLionel Sambuc 
1566f4a2713aSLionel Sambuc   /// StartBlock - Start new block named N. If insert block is a dummy block
1567f4a2713aSLionel Sambuc   /// then reuse it.
1568f4a2713aSLionel Sambuc   void StartBlock(const char *N);
1569f4a2713aSLionel Sambuc 
1570f4a2713aSLionel Sambuc   /// GetAddrOfLocalVar - Return the address of a local variable.
GetAddrOfLocalVar(const VarDecl * VD)1571f4a2713aSLionel Sambuc   llvm::Value *GetAddrOfLocalVar(const VarDecl *VD) {
1572f4a2713aSLionel Sambuc     llvm::Value *Res = LocalDeclMap[VD];
1573f4a2713aSLionel Sambuc     assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!");
1574f4a2713aSLionel Sambuc     return Res;
1575f4a2713aSLionel Sambuc   }
1576f4a2713aSLionel Sambuc 
1577f4a2713aSLionel Sambuc   /// getOpaqueLValueMapping - Given an opaque value expression (which
1578f4a2713aSLionel Sambuc   /// must be mapped to an l-value), return its mapping.
getOpaqueLValueMapping(const OpaqueValueExpr * e)1579f4a2713aSLionel Sambuc   const LValue &getOpaqueLValueMapping(const OpaqueValueExpr *e) {
1580f4a2713aSLionel Sambuc     assert(OpaqueValueMapping::shouldBindAsLValue(e));
1581f4a2713aSLionel Sambuc 
1582f4a2713aSLionel Sambuc     llvm::DenseMap<const OpaqueValueExpr*,LValue>::iterator
1583f4a2713aSLionel Sambuc       it = OpaqueLValues.find(e);
1584f4a2713aSLionel Sambuc     assert(it != OpaqueLValues.end() && "no mapping for opaque value!");
1585f4a2713aSLionel Sambuc     return it->second;
1586f4a2713aSLionel Sambuc   }
1587f4a2713aSLionel Sambuc 
1588f4a2713aSLionel Sambuc   /// getOpaqueRValueMapping - Given an opaque value expression (which
1589f4a2713aSLionel Sambuc   /// must be mapped to an r-value), return its mapping.
getOpaqueRValueMapping(const OpaqueValueExpr * e)1590f4a2713aSLionel Sambuc   const RValue &getOpaqueRValueMapping(const OpaqueValueExpr *e) {
1591f4a2713aSLionel Sambuc     assert(!OpaqueValueMapping::shouldBindAsLValue(e));
1592f4a2713aSLionel Sambuc 
1593f4a2713aSLionel Sambuc     llvm::DenseMap<const OpaqueValueExpr*,RValue>::iterator
1594f4a2713aSLionel Sambuc       it = OpaqueRValues.find(e);
1595f4a2713aSLionel Sambuc     assert(it != OpaqueRValues.end() && "no mapping for opaque value!");
1596f4a2713aSLionel Sambuc     return it->second;
1597f4a2713aSLionel Sambuc   }
1598f4a2713aSLionel Sambuc 
1599f4a2713aSLionel Sambuc   /// getAccessedFieldNo - Given an encoded value and a result number, return
1600f4a2713aSLionel Sambuc   /// the input field number being accessed.
1601f4a2713aSLionel Sambuc   static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts);
1602f4a2713aSLionel Sambuc 
1603f4a2713aSLionel Sambuc   llvm::BlockAddress *GetAddrOfLabel(const LabelDecl *L);
1604f4a2713aSLionel Sambuc   llvm::BasicBlock *GetIndirectGotoBlock();
1605f4a2713aSLionel Sambuc 
1606f4a2713aSLionel Sambuc   /// EmitNullInitialization - Generate code to set a value of the given type to
1607f4a2713aSLionel Sambuc   /// null, If the type contains data member pointers, they will be initialized
1608f4a2713aSLionel Sambuc   /// to -1 in accordance with the Itanium C++ ABI.
1609f4a2713aSLionel Sambuc   void EmitNullInitialization(llvm::Value *DestPtr, QualType Ty);
1610f4a2713aSLionel Sambuc 
1611f4a2713aSLionel Sambuc   // EmitVAArg - Generate code to get an argument from the passed in pointer
1612f4a2713aSLionel Sambuc   // and update it accordingly. The return value is a pointer to the argument.
1613f4a2713aSLionel Sambuc   // FIXME: We should be able to get rid of this method and use the va_arg
1614f4a2713aSLionel Sambuc   // instruction in LLVM instead once it works well enough.
1615f4a2713aSLionel Sambuc   llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty);
1616f4a2713aSLionel Sambuc 
1617f4a2713aSLionel Sambuc   /// emitArrayLength - Compute the length of an array, even if it's a
1618f4a2713aSLionel Sambuc   /// VLA, and drill down to the base element type.
1619f4a2713aSLionel Sambuc   llvm::Value *emitArrayLength(const ArrayType *arrayType,
1620f4a2713aSLionel Sambuc                                QualType &baseType,
1621f4a2713aSLionel Sambuc                                llvm::Value *&addr);
1622f4a2713aSLionel Sambuc 
1623f4a2713aSLionel Sambuc   /// EmitVLASize - Capture all the sizes for the VLA expressions in
1624f4a2713aSLionel Sambuc   /// the given variably-modified type and store them in the VLASizeMap.
1625f4a2713aSLionel Sambuc   ///
1626f4a2713aSLionel Sambuc   /// This function can be called with a null (unreachable) insert point.
1627f4a2713aSLionel Sambuc   void EmitVariablyModifiedType(QualType Ty);
1628f4a2713aSLionel Sambuc 
1629f4a2713aSLionel Sambuc   /// getVLASize - Returns an LLVM value that corresponds to the size,
1630f4a2713aSLionel Sambuc   /// in non-variably-sized elements, of a variable length array type,
1631f4a2713aSLionel Sambuc   /// plus that largest non-variably-sized element type.  Assumes that
1632f4a2713aSLionel Sambuc   /// the type has already been emitted with EmitVariablyModifiedType.
1633f4a2713aSLionel Sambuc   std::pair<llvm::Value*,QualType> getVLASize(const VariableArrayType *vla);
1634f4a2713aSLionel Sambuc   std::pair<llvm::Value*,QualType> getVLASize(QualType vla);
1635f4a2713aSLionel Sambuc 
1636f4a2713aSLionel Sambuc   /// LoadCXXThis - Load the value of 'this'. This function is only valid while
1637f4a2713aSLionel Sambuc   /// generating code for an C++ member function.
LoadCXXThis()1638f4a2713aSLionel Sambuc   llvm::Value *LoadCXXThis() {
1639f4a2713aSLionel Sambuc     assert(CXXThisValue && "no 'this' value for this function");
1640f4a2713aSLionel Sambuc     return CXXThisValue;
1641f4a2713aSLionel Sambuc   }
1642f4a2713aSLionel Sambuc 
1643f4a2713aSLionel Sambuc   /// LoadCXXVTT - Load the VTT parameter to base constructors/destructors have
1644f4a2713aSLionel Sambuc   /// virtual bases.
1645f4a2713aSLionel Sambuc   // FIXME: Every place that calls LoadCXXVTT is something
1646f4a2713aSLionel Sambuc   // that needs to be abstracted properly.
LoadCXXVTT()1647f4a2713aSLionel Sambuc   llvm::Value *LoadCXXVTT() {
1648f4a2713aSLionel Sambuc     assert(CXXStructorImplicitParamValue && "no VTT value for this function");
1649f4a2713aSLionel Sambuc     return CXXStructorImplicitParamValue;
1650f4a2713aSLionel Sambuc   }
1651f4a2713aSLionel Sambuc 
1652f4a2713aSLionel Sambuc   /// LoadCXXStructorImplicitParam - Load the implicit parameter
1653f4a2713aSLionel Sambuc   /// for a constructor/destructor.
LoadCXXStructorImplicitParam()1654f4a2713aSLionel Sambuc   llvm::Value *LoadCXXStructorImplicitParam() {
1655f4a2713aSLionel Sambuc     assert(CXXStructorImplicitParamValue &&
1656f4a2713aSLionel Sambuc            "no implicit argument value for this function");
1657f4a2713aSLionel Sambuc     return CXXStructorImplicitParamValue;
1658f4a2713aSLionel Sambuc   }
1659f4a2713aSLionel Sambuc 
1660f4a2713aSLionel Sambuc   /// GetAddressOfBaseOfCompleteClass - Convert the given pointer to a
1661f4a2713aSLionel Sambuc   /// complete class to the given direct base.
1662f4a2713aSLionel Sambuc   llvm::Value *
1663f4a2713aSLionel Sambuc   GetAddressOfDirectBaseInCompleteClass(llvm::Value *Value,
1664f4a2713aSLionel Sambuc                                         const CXXRecordDecl *Derived,
1665f4a2713aSLionel Sambuc                                         const CXXRecordDecl *Base,
1666f4a2713aSLionel Sambuc                                         bool BaseIsVirtual);
1667f4a2713aSLionel Sambuc 
1668f4a2713aSLionel Sambuc   /// GetAddressOfBaseClass - This function will add the necessary delta to the
1669f4a2713aSLionel Sambuc   /// load of 'this' and returns address of the base class.
1670f4a2713aSLionel Sambuc   llvm::Value *GetAddressOfBaseClass(llvm::Value *Value,
1671f4a2713aSLionel Sambuc                                      const CXXRecordDecl *Derived,
1672f4a2713aSLionel Sambuc                                      CastExpr::path_const_iterator PathBegin,
1673f4a2713aSLionel Sambuc                                      CastExpr::path_const_iterator PathEnd,
1674*0a6a1f1dSLionel Sambuc                                      bool NullCheckValue, SourceLocation Loc);
1675f4a2713aSLionel Sambuc 
1676f4a2713aSLionel Sambuc   llvm::Value *GetAddressOfDerivedClass(llvm::Value *Value,
1677f4a2713aSLionel Sambuc                                         const CXXRecordDecl *Derived,
1678f4a2713aSLionel Sambuc                                         CastExpr::path_const_iterator PathBegin,
1679f4a2713aSLionel Sambuc                                         CastExpr::path_const_iterator PathEnd,
1680f4a2713aSLionel Sambuc                                         bool NullCheckValue);
1681f4a2713aSLionel Sambuc 
1682f4a2713aSLionel Sambuc   /// GetVTTParameter - Return the VTT parameter that should be passed to a
1683f4a2713aSLionel Sambuc   /// base constructor/destructor with virtual bases.
1684f4a2713aSLionel Sambuc   /// FIXME: VTTs are Itanium ABI-specific, so the definition should move
1685f4a2713aSLionel Sambuc   /// to ItaniumCXXABI.cpp together with all the references to VTT.
1686f4a2713aSLionel Sambuc   llvm::Value *GetVTTParameter(GlobalDecl GD, bool ForVirtualBase,
1687f4a2713aSLionel Sambuc                                bool Delegating);
1688f4a2713aSLionel Sambuc 
1689f4a2713aSLionel Sambuc   void EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor,
1690f4a2713aSLionel Sambuc                                       CXXCtorType CtorType,
1691f4a2713aSLionel Sambuc                                       const FunctionArgList &Args,
1692f4a2713aSLionel Sambuc                                       SourceLocation Loc);
1693f4a2713aSLionel Sambuc   // It's important not to confuse this and the previous function. Delegating
1694f4a2713aSLionel Sambuc   // constructors are the C++0x feature. The constructor delegate optimization
1695f4a2713aSLionel Sambuc   // is used to reduce duplication in the base and complete consturctors where
1696f4a2713aSLionel Sambuc   // they are substantially the same.
1697f4a2713aSLionel Sambuc   void EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor,
1698f4a2713aSLionel Sambuc                                         const FunctionArgList &Args);
1699f4a2713aSLionel Sambuc   void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type,
1700f4a2713aSLionel Sambuc                               bool ForVirtualBase, bool Delegating,
1701*0a6a1f1dSLionel Sambuc                               llvm::Value *This, const CXXConstructExpr *E);
1702f4a2713aSLionel Sambuc 
1703f4a2713aSLionel Sambuc   void EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D,
1704f4a2713aSLionel Sambuc                               llvm::Value *This, llvm::Value *Src,
1705*0a6a1f1dSLionel Sambuc                               const CXXConstructExpr *E);
1706f4a2713aSLionel Sambuc 
1707f4a2713aSLionel Sambuc   void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
1708f4a2713aSLionel Sambuc                                   const ConstantArrayType *ArrayTy,
1709f4a2713aSLionel Sambuc                                   llvm::Value *ArrayPtr,
1710*0a6a1f1dSLionel Sambuc                                   const CXXConstructExpr *E,
1711f4a2713aSLionel Sambuc                                   bool ZeroInitialization = false);
1712f4a2713aSLionel Sambuc 
1713f4a2713aSLionel Sambuc   void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
1714f4a2713aSLionel Sambuc                                   llvm::Value *NumElements,
1715f4a2713aSLionel Sambuc                                   llvm::Value *ArrayPtr,
1716*0a6a1f1dSLionel Sambuc                                   const CXXConstructExpr *E,
1717f4a2713aSLionel Sambuc                                   bool ZeroInitialization = false);
1718f4a2713aSLionel Sambuc 
1719f4a2713aSLionel Sambuc   static Destroyer destroyCXXObject;
1720f4a2713aSLionel Sambuc 
1721f4a2713aSLionel Sambuc   void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type,
1722f4a2713aSLionel Sambuc                              bool ForVirtualBase, bool Delegating,
1723f4a2713aSLionel Sambuc                              llvm::Value *This);
1724f4a2713aSLionel Sambuc 
1725f4a2713aSLionel Sambuc   void EmitNewArrayInitializer(const CXXNewExpr *E, QualType elementType,
1726*0a6a1f1dSLionel Sambuc                                llvm::Value *NewPtr, llvm::Value *NumElements,
1727*0a6a1f1dSLionel Sambuc                                llvm::Value *AllocSizeWithoutCookie);
1728f4a2713aSLionel Sambuc 
1729f4a2713aSLionel Sambuc   void EmitCXXTemporary(const CXXTemporary *Temporary, QualType TempType,
1730f4a2713aSLionel Sambuc                         llvm::Value *Ptr);
1731f4a2713aSLionel Sambuc 
1732f4a2713aSLionel Sambuc   llvm::Value *EmitCXXNewExpr(const CXXNewExpr *E);
1733f4a2713aSLionel Sambuc   void EmitCXXDeleteExpr(const CXXDeleteExpr *E);
1734f4a2713aSLionel Sambuc 
1735f4a2713aSLionel Sambuc   void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr,
1736f4a2713aSLionel Sambuc                       QualType DeleteTy);
1737f4a2713aSLionel Sambuc 
1738*0a6a1f1dSLionel Sambuc   RValue EmitBuiltinNewDeleteCall(const FunctionProtoType *Type,
1739*0a6a1f1dSLionel Sambuc                                   const Expr *Arg, bool IsDelete);
1740*0a6a1f1dSLionel Sambuc 
1741f4a2713aSLionel Sambuc   llvm::Value* EmitCXXTypeidExpr(const CXXTypeidExpr *E);
1742f4a2713aSLionel Sambuc   llvm::Value *EmitDynamicCast(llvm::Value *V, const CXXDynamicCastExpr *DCE);
1743f4a2713aSLionel Sambuc   llvm::Value* EmitCXXUuidofExpr(const CXXUuidofExpr *E);
1744f4a2713aSLionel Sambuc 
1745f4a2713aSLionel Sambuc   /// \brief Situations in which we might emit a check for the suitability of a
1746f4a2713aSLionel Sambuc   ///        pointer or glvalue.
1747f4a2713aSLionel Sambuc   enum TypeCheckKind {
1748f4a2713aSLionel Sambuc     /// Checking the operand of a load. Must be suitably sized and aligned.
1749f4a2713aSLionel Sambuc     TCK_Load,
1750f4a2713aSLionel Sambuc     /// Checking the destination of a store. Must be suitably sized and aligned.
1751f4a2713aSLionel Sambuc     TCK_Store,
1752f4a2713aSLionel Sambuc     /// Checking the bound value in a reference binding. Must be suitably sized
1753f4a2713aSLionel Sambuc     /// and aligned, but is not required to refer to an object (until the
1754f4a2713aSLionel Sambuc     /// reference is used), per core issue 453.
1755f4a2713aSLionel Sambuc     TCK_ReferenceBinding,
1756f4a2713aSLionel Sambuc     /// Checking the object expression in a non-static data member access. Must
1757f4a2713aSLionel Sambuc     /// be an object within its lifetime.
1758f4a2713aSLionel Sambuc     TCK_MemberAccess,
1759f4a2713aSLionel Sambuc     /// Checking the 'this' pointer for a call to a non-static member function.
1760f4a2713aSLionel Sambuc     /// Must be an object within its lifetime.
1761f4a2713aSLionel Sambuc     TCK_MemberCall,
1762f4a2713aSLionel Sambuc     /// Checking the 'this' pointer for a constructor call.
1763f4a2713aSLionel Sambuc     TCK_ConstructorCall,
1764f4a2713aSLionel Sambuc     /// Checking the operand of a static_cast to a derived pointer type. Must be
1765f4a2713aSLionel Sambuc     /// null or an object within its lifetime.
1766f4a2713aSLionel Sambuc     TCK_DowncastPointer,
1767f4a2713aSLionel Sambuc     /// Checking the operand of a static_cast to a derived reference type. Must
1768f4a2713aSLionel Sambuc     /// be an object within its lifetime.
1769*0a6a1f1dSLionel Sambuc     TCK_DowncastReference,
1770*0a6a1f1dSLionel Sambuc     /// Checking the operand of a cast to a base object. Must be suitably sized
1771*0a6a1f1dSLionel Sambuc     /// and aligned.
1772*0a6a1f1dSLionel Sambuc     TCK_Upcast,
1773*0a6a1f1dSLionel Sambuc     /// Checking the operand of a cast to a virtual base object. Must be an
1774*0a6a1f1dSLionel Sambuc     /// object within its lifetime.
1775*0a6a1f1dSLionel Sambuc     TCK_UpcastToVirtualBase
1776f4a2713aSLionel Sambuc   };
1777f4a2713aSLionel Sambuc 
1778*0a6a1f1dSLionel Sambuc   /// \brief Whether any type-checking sanitizers are enabled. If \c false,
1779*0a6a1f1dSLionel Sambuc   /// calls to EmitTypeCheck can be skipped.
1780*0a6a1f1dSLionel Sambuc   bool sanitizePerformTypeCheck() const;
1781*0a6a1f1dSLionel Sambuc 
1782f4a2713aSLionel Sambuc   /// \brief Emit a check that \p V is the address of storage of the
1783f4a2713aSLionel Sambuc   /// appropriate size and alignment for an object of type \p Type.
1784f4a2713aSLionel Sambuc   void EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc, llvm::Value *V,
1785*0a6a1f1dSLionel Sambuc                      QualType Type, CharUnits Alignment = CharUnits::Zero(),
1786*0a6a1f1dSLionel Sambuc                      bool SkipNullCheck = false);
1787f4a2713aSLionel Sambuc 
1788f4a2713aSLionel Sambuc   /// \brief Emit a check that \p Base points into an array object, which
1789f4a2713aSLionel Sambuc   /// we can access at index \p Index. \p Accessed should be \c false if we
1790f4a2713aSLionel Sambuc   /// this expression is used as an lvalue, for instance in "&Arr[Idx]".
1791f4a2713aSLionel Sambuc   void EmitBoundsCheck(const Expr *E, const Expr *Base, llvm::Value *Index,
1792f4a2713aSLionel Sambuc                        QualType IndexType, bool Accessed);
1793f4a2713aSLionel Sambuc 
1794f4a2713aSLionel Sambuc   llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
1795f4a2713aSLionel Sambuc                                        bool isInc, bool isPre);
1796f4a2713aSLionel Sambuc   ComplexPairTy EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
1797f4a2713aSLionel Sambuc                                          bool isInc, bool isPre);
1798*0a6a1f1dSLionel Sambuc 
1799*0a6a1f1dSLionel Sambuc   void EmitAlignmentAssumption(llvm::Value *PtrValue, unsigned Alignment,
1800*0a6a1f1dSLionel Sambuc                                llvm::Value *OffsetValue = nullptr) {
1801*0a6a1f1dSLionel Sambuc     Builder.CreateAlignmentAssumption(CGM.getDataLayout(), PtrValue, Alignment,
1802*0a6a1f1dSLionel Sambuc                                       OffsetValue);
1803*0a6a1f1dSLionel Sambuc   }
1804*0a6a1f1dSLionel Sambuc 
1805f4a2713aSLionel Sambuc   //===--------------------------------------------------------------------===//
1806f4a2713aSLionel Sambuc   //                            Declaration Emission
1807f4a2713aSLionel Sambuc   //===--------------------------------------------------------------------===//
1808f4a2713aSLionel Sambuc 
1809f4a2713aSLionel Sambuc   /// EmitDecl - Emit a declaration.
1810f4a2713aSLionel Sambuc   ///
1811f4a2713aSLionel Sambuc   /// This function can be called with a null (unreachable) insert point.
1812f4a2713aSLionel Sambuc   void EmitDecl(const Decl &D);
1813f4a2713aSLionel Sambuc 
1814f4a2713aSLionel Sambuc   /// EmitVarDecl - Emit a local variable declaration.
1815f4a2713aSLionel Sambuc   ///
1816f4a2713aSLionel Sambuc   /// This function can be called with a null (unreachable) insert point.
1817f4a2713aSLionel Sambuc   void EmitVarDecl(const VarDecl &D);
1818f4a2713aSLionel Sambuc 
1819*0a6a1f1dSLionel Sambuc   void EmitScalarInit(const Expr *init, const ValueDecl *D, LValue lvalue,
1820*0a6a1f1dSLionel Sambuc                       bool capturedByInit);
1821f4a2713aSLionel Sambuc   void EmitScalarInit(llvm::Value *init, LValue lvalue);
1822f4a2713aSLionel Sambuc 
1823f4a2713aSLionel Sambuc   typedef void SpecialInitFn(CodeGenFunction &Init, const VarDecl &D,
1824f4a2713aSLionel Sambuc                              llvm::Value *Address);
1825f4a2713aSLionel Sambuc 
1826*0a6a1f1dSLionel Sambuc   /// \brief Determine whether the given initializer is trivial in the sense
1827*0a6a1f1dSLionel Sambuc   /// that it requires no code to be generated.
1828*0a6a1f1dSLionel Sambuc   bool isTrivialInitializer(const Expr *Init);
1829*0a6a1f1dSLionel Sambuc 
1830f4a2713aSLionel Sambuc   /// EmitAutoVarDecl - Emit an auto variable declaration.
1831f4a2713aSLionel Sambuc   ///
1832f4a2713aSLionel Sambuc   /// This function can be called with a null (unreachable) insert point.
1833f4a2713aSLionel Sambuc   void EmitAutoVarDecl(const VarDecl &D);
1834f4a2713aSLionel Sambuc 
1835f4a2713aSLionel Sambuc   class AutoVarEmission {
1836f4a2713aSLionel Sambuc     friend class CodeGenFunction;
1837f4a2713aSLionel Sambuc 
1838f4a2713aSLionel Sambuc     const VarDecl *Variable;
1839f4a2713aSLionel Sambuc 
1840f4a2713aSLionel Sambuc     /// The alignment of the variable.
1841f4a2713aSLionel Sambuc     CharUnits Alignment;
1842f4a2713aSLionel Sambuc 
1843f4a2713aSLionel Sambuc     /// The address of the alloca.  Null if the variable was emitted
1844f4a2713aSLionel Sambuc     /// as a global constant.
1845f4a2713aSLionel Sambuc     llvm::Value *Address;
1846f4a2713aSLionel Sambuc 
1847f4a2713aSLionel Sambuc     llvm::Value *NRVOFlag;
1848f4a2713aSLionel Sambuc 
1849f4a2713aSLionel Sambuc     /// True if the variable is a __block variable.
1850f4a2713aSLionel Sambuc     bool IsByRef;
1851f4a2713aSLionel Sambuc 
1852f4a2713aSLionel Sambuc     /// True if the variable is of aggregate type and has a constant
1853f4a2713aSLionel Sambuc     /// initializer.
1854f4a2713aSLionel Sambuc     bool IsConstantAggregate;
1855f4a2713aSLionel Sambuc 
1856f4a2713aSLionel Sambuc     /// Non-null if we should use lifetime annotations.
1857f4a2713aSLionel Sambuc     llvm::Value *SizeForLifetimeMarkers;
1858f4a2713aSLionel Sambuc 
1859f4a2713aSLionel Sambuc     struct Invalid {};
AutoVarEmission(Invalid)1860*0a6a1f1dSLionel Sambuc     AutoVarEmission(Invalid) : Variable(nullptr) {}
1861f4a2713aSLionel Sambuc 
AutoVarEmission(const VarDecl & variable)1862f4a2713aSLionel Sambuc     AutoVarEmission(const VarDecl &variable)
1863*0a6a1f1dSLionel Sambuc       : Variable(&variable), Address(nullptr), NRVOFlag(nullptr),
1864f4a2713aSLionel Sambuc         IsByRef(false), IsConstantAggregate(false),
1865*0a6a1f1dSLionel Sambuc         SizeForLifetimeMarkers(nullptr) {}
1866f4a2713aSLionel Sambuc 
wasEmittedAsGlobal()1867*0a6a1f1dSLionel Sambuc     bool wasEmittedAsGlobal() const { return Address == nullptr; }
1868f4a2713aSLionel Sambuc 
1869f4a2713aSLionel Sambuc   public:
invalid()1870f4a2713aSLionel Sambuc     static AutoVarEmission invalid() { return AutoVarEmission(Invalid()); }
1871f4a2713aSLionel Sambuc 
useLifetimeMarkers()1872*0a6a1f1dSLionel Sambuc     bool useLifetimeMarkers() const {
1873*0a6a1f1dSLionel Sambuc       return SizeForLifetimeMarkers != nullptr;
1874*0a6a1f1dSLionel Sambuc     }
getSizeForLifetimeMarkers()1875f4a2713aSLionel Sambuc     llvm::Value *getSizeForLifetimeMarkers() const {
1876f4a2713aSLionel Sambuc       assert(useLifetimeMarkers());
1877f4a2713aSLionel Sambuc       return SizeForLifetimeMarkers;
1878f4a2713aSLionel Sambuc     }
1879f4a2713aSLionel Sambuc 
1880f4a2713aSLionel Sambuc     /// Returns the raw, allocated address, which is not necessarily
1881f4a2713aSLionel Sambuc     /// the address of the object itself.
getAllocatedAddress()1882f4a2713aSLionel Sambuc     llvm::Value *getAllocatedAddress() const {
1883f4a2713aSLionel Sambuc       return Address;
1884f4a2713aSLionel Sambuc     }
1885f4a2713aSLionel Sambuc 
1886f4a2713aSLionel Sambuc     /// Returns the address of the object within this declaration.
1887f4a2713aSLionel Sambuc     /// Note that this does not chase the forwarding pointer for
1888f4a2713aSLionel Sambuc     /// __block decls.
getObjectAddress(CodeGenFunction & CGF)1889f4a2713aSLionel Sambuc     llvm::Value *getObjectAddress(CodeGenFunction &CGF) const {
1890f4a2713aSLionel Sambuc       if (!IsByRef) return Address;
1891f4a2713aSLionel Sambuc 
1892f4a2713aSLionel Sambuc       return CGF.Builder.CreateStructGEP(Address,
1893f4a2713aSLionel Sambuc                                          CGF.getByRefValueLLVMField(Variable),
1894f4a2713aSLionel Sambuc                                          Variable->getNameAsString());
1895f4a2713aSLionel Sambuc     }
1896f4a2713aSLionel Sambuc   };
1897f4a2713aSLionel Sambuc   AutoVarEmission EmitAutoVarAlloca(const VarDecl &var);
1898f4a2713aSLionel Sambuc   void EmitAutoVarInit(const AutoVarEmission &emission);
1899f4a2713aSLionel Sambuc   void EmitAutoVarCleanups(const AutoVarEmission &emission);
1900f4a2713aSLionel Sambuc   void emitAutoVarTypeCleanup(const AutoVarEmission &emission,
1901f4a2713aSLionel Sambuc                               QualType::DestructionKind dtorKind);
1902f4a2713aSLionel Sambuc 
1903f4a2713aSLionel Sambuc   void EmitStaticVarDecl(const VarDecl &D,
1904f4a2713aSLionel Sambuc                          llvm::GlobalValue::LinkageTypes Linkage);
1905f4a2713aSLionel Sambuc 
1906f4a2713aSLionel Sambuc   /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
1907*0a6a1f1dSLionel Sambuc   void EmitParmDecl(const VarDecl &D, llvm::Value *Arg, bool ArgIsPointer,
1908*0a6a1f1dSLionel Sambuc                     unsigned ArgNo);
1909f4a2713aSLionel Sambuc 
1910f4a2713aSLionel Sambuc   /// protectFromPeepholes - Protect a value that we're intending to
1911f4a2713aSLionel Sambuc   /// store to the side, but which will probably be used later, from
1912f4a2713aSLionel Sambuc   /// aggressive peepholing optimizations that might delete it.
1913f4a2713aSLionel Sambuc   ///
1914f4a2713aSLionel Sambuc   /// Pass the result to unprotectFromPeepholes to declare that
1915f4a2713aSLionel Sambuc   /// protection is no longer required.
1916f4a2713aSLionel Sambuc   ///
1917f4a2713aSLionel Sambuc   /// There's no particular reason why this shouldn't apply to
1918f4a2713aSLionel Sambuc   /// l-values, it's just that no existing peepholes work on pointers.
1919f4a2713aSLionel Sambuc   PeepholeProtection protectFromPeepholes(RValue rvalue);
1920f4a2713aSLionel Sambuc   void unprotectFromPeepholes(PeepholeProtection protection);
1921f4a2713aSLionel Sambuc 
1922f4a2713aSLionel Sambuc   //===--------------------------------------------------------------------===//
1923f4a2713aSLionel Sambuc   //                             Statement Emission
1924f4a2713aSLionel Sambuc   //===--------------------------------------------------------------------===//
1925f4a2713aSLionel Sambuc 
1926f4a2713aSLionel Sambuc   /// EmitStopPoint - Emit a debug stoppoint if we are emitting debug info.
1927f4a2713aSLionel Sambuc   void EmitStopPoint(const Stmt *S);
1928f4a2713aSLionel Sambuc 
1929f4a2713aSLionel Sambuc   /// EmitStmt - Emit the code for the statement \arg S. It is legal to call
1930f4a2713aSLionel Sambuc   /// this function even if there is no current insertion point.
1931f4a2713aSLionel Sambuc   ///
1932f4a2713aSLionel Sambuc   /// This function may clear the current insertion point; callers should use
1933f4a2713aSLionel Sambuc   /// EnsureInsertPoint if they wish to subsequently generate code without first
1934f4a2713aSLionel Sambuc   /// calling EmitBlock, EmitBranch, or EmitStmt.
1935f4a2713aSLionel Sambuc   void EmitStmt(const Stmt *S);
1936f4a2713aSLionel Sambuc 
1937f4a2713aSLionel Sambuc   /// EmitSimpleStmt - Try to emit a "simple" statement which does not
1938f4a2713aSLionel Sambuc   /// necessarily require an insertion point or debug information; typically
1939f4a2713aSLionel Sambuc   /// because the statement amounts to a jump or a container of other
1940f4a2713aSLionel Sambuc   /// statements.
1941f4a2713aSLionel Sambuc   ///
1942f4a2713aSLionel Sambuc   /// \return True if the statement was handled.
1943f4a2713aSLionel Sambuc   bool EmitSimpleStmt(const Stmt *S);
1944f4a2713aSLionel Sambuc 
1945f4a2713aSLionel Sambuc   llvm::Value *EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false,
1946f4a2713aSLionel Sambuc                                 AggValueSlot AVS = AggValueSlot::ignored());
1947f4a2713aSLionel Sambuc   llvm::Value *EmitCompoundStmtWithoutScope(const CompoundStmt &S,
1948f4a2713aSLionel Sambuc                                             bool GetLast = false,
1949f4a2713aSLionel Sambuc                                             AggValueSlot AVS =
1950f4a2713aSLionel Sambuc                                                 AggValueSlot::ignored());
1951f4a2713aSLionel Sambuc 
1952f4a2713aSLionel Sambuc   /// EmitLabel - Emit the block for the given label. It is legal to call this
1953f4a2713aSLionel Sambuc   /// function even if there is no current insertion point.
1954f4a2713aSLionel Sambuc   void EmitLabel(const LabelDecl *D); // helper for EmitLabelStmt.
1955f4a2713aSLionel Sambuc 
1956f4a2713aSLionel Sambuc   void EmitLabelStmt(const LabelStmt &S);
1957f4a2713aSLionel Sambuc   void EmitAttributedStmt(const AttributedStmt &S);
1958f4a2713aSLionel Sambuc   void EmitGotoStmt(const GotoStmt &S);
1959f4a2713aSLionel Sambuc   void EmitIndirectGotoStmt(const IndirectGotoStmt &S);
1960f4a2713aSLionel Sambuc   void EmitIfStmt(const IfStmt &S);
1961*0a6a1f1dSLionel Sambuc 
1962*0a6a1f1dSLionel Sambuc   void EmitCondBrHints(llvm::LLVMContext &Context, llvm::BranchInst *CondBr,
1963*0a6a1f1dSLionel Sambuc                        ArrayRef<const Attr *> Attrs);
1964*0a6a1f1dSLionel Sambuc   void EmitWhileStmt(const WhileStmt &S,
1965*0a6a1f1dSLionel Sambuc                      ArrayRef<const Attr *> Attrs = None);
1966*0a6a1f1dSLionel Sambuc   void EmitDoStmt(const DoStmt &S, ArrayRef<const Attr *> Attrs = None);
1967*0a6a1f1dSLionel Sambuc   void EmitForStmt(const ForStmt &S,
1968*0a6a1f1dSLionel Sambuc                    ArrayRef<const Attr *> Attrs = None);
1969f4a2713aSLionel Sambuc   void EmitReturnStmt(const ReturnStmt &S);
1970f4a2713aSLionel Sambuc   void EmitDeclStmt(const DeclStmt &S);
1971f4a2713aSLionel Sambuc   void EmitBreakStmt(const BreakStmt &S);
1972f4a2713aSLionel Sambuc   void EmitContinueStmt(const ContinueStmt &S);
1973f4a2713aSLionel Sambuc   void EmitSwitchStmt(const SwitchStmt &S);
1974f4a2713aSLionel Sambuc   void EmitDefaultStmt(const DefaultStmt &S);
1975f4a2713aSLionel Sambuc   void EmitCaseStmt(const CaseStmt &S);
1976f4a2713aSLionel Sambuc   void EmitCaseStmtRange(const CaseStmt &S);
1977f4a2713aSLionel Sambuc   void EmitAsmStmt(const AsmStmt &S);
1978f4a2713aSLionel Sambuc 
1979f4a2713aSLionel Sambuc   void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S);
1980f4a2713aSLionel Sambuc   void EmitObjCAtTryStmt(const ObjCAtTryStmt &S);
1981f4a2713aSLionel Sambuc   void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S);
1982f4a2713aSLionel Sambuc   void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S);
1983f4a2713aSLionel Sambuc   void EmitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt &S);
1984f4a2713aSLionel Sambuc 
1985f4a2713aSLionel Sambuc   void EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
1986f4a2713aSLionel Sambuc   void ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
1987f4a2713aSLionel Sambuc 
1988f4a2713aSLionel Sambuc   void EmitCXXTryStmt(const CXXTryStmt &S);
1989f4a2713aSLionel Sambuc   void EmitSEHTryStmt(const SEHTryStmt &S);
1990*0a6a1f1dSLionel Sambuc   void EmitSEHLeaveStmt(const SEHLeaveStmt &S);
1991*0a6a1f1dSLionel Sambuc   void EmitCXXForRangeStmt(const CXXForRangeStmt &S,
1992*0a6a1f1dSLionel Sambuc                            ArrayRef<const Attr *> Attrs = None);
1993f4a2713aSLionel Sambuc 
1994*0a6a1f1dSLionel Sambuc   LValue InitCapturedStruct(const CapturedStmt &S);
1995f4a2713aSLionel Sambuc   llvm::Function *EmitCapturedStmt(const CapturedStmt &S, CapturedRegionKind K);
1996*0a6a1f1dSLionel Sambuc   void GenerateCapturedStmtFunctionProlog(const CapturedStmt &S);
1997*0a6a1f1dSLionel Sambuc   llvm::Function *GenerateCapturedStmtFunctionEpilog(const CapturedStmt &S);
1998*0a6a1f1dSLionel Sambuc   llvm::Function *GenerateCapturedStmtFunction(const CapturedStmt &S);
1999*0a6a1f1dSLionel Sambuc   llvm::Value *GenerateCapturedStmtArgument(const CapturedStmt &S);
2000*0a6a1f1dSLionel Sambuc   void EmitOMPAggregateAssign(LValue OriginalAddr, llvm::Value *PrivateAddr,
2001*0a6a1f1dSLionel Sambuc                               const Expr *AssignExpr, QualType Type,
2002*0a6a1f1dSLionel Sambuc                               const VarDecl *VDInit);
2003*0a6a1f1dSLionel Sambuc   void EmitOMPFirstprivateClause(const OMPExecutableDirective &D,
2004*0a6a1f1dSLionel Sambuc                                  OMPPrivateScope &PrivateScope);
2005*0a6a1f1dSLionel Sambuc   void EmitOMPPrivateClause(const OMPExecutableDirective &D,
2006*0a6a1f1dSLionel Sambuc                             OMPPrivateScope &PrivateScope);
2007*0a6a1f1dSLionel Sambuc 
2008*0a6a1f1dSLionel Sambuc   void EmitOMPParallelDirective(const OMPParallelDirective &S);
2009*0a6a1f1dSLionel Sambuc   void EmitOMPSimdDirective(const OMPSimdDirective &S);
2010*0a6a1f1dSLionel Sambuc   void EmitOMPForDirective(const OMPForDirective &S);
2011*0a6a1f1dSLionel Sambuc   void EmitOMPForSimdDirective(const OMPForSimdDirective &S);
2012*0a6a1f1dSLionel Sambuc   void EmitOMPSectionsDirective(const OMPSectionsDirective &S);
2013*0a6a1f1dSLionel Sambuc   void EmitOMPSectionDirective(const OMPSectionDirective &S);
2014*0a6a1f1dSLionel Sambuc   void EmitOMPSingleDirective(const OMPSingleDirective &S);
2015*0a6a1f1dSLionel Sambuc   void EmitOMPMasterDirective(const OMPMasterDirective &S);
2016*0a6a1f1dSLionel Sambuc   void EmitOMPCriticalDirective(const OMPCriticalDirective &S);
2017*0a6a1f1dSLionel Sambuc   void EmitOMPParallelForDirective(const OMPParallelForDirective &S);
2018*0a6a1f1dSLionel Sambuc   void EmitOMPParallelForSimdDirective(const OMPParallelForSimdDirective &S);
2019*0a6a1f1dSLionel Sambuc   void EmitOMPParallelSectionsDirective(const OMPParallelSectionsDirective &S);
2020*0a6a1f1dSLionel Sambuc   void EmitOMPTaskDirective(const OMPTaskDirective &S);
2021*0a6a1f1dSLionel Sambuc   void EmitOMPTaskyieldDirective(const OMPTaskyieldDirective &S);
2022*0a6a1f1dSLionel Sambuc   void EmitOMPBarrierDirective(const OMPBarrierDirective &S);
2023*0a6a1f1dSLionel Sambuc   void EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S);
2024*0a6a1f1dSLionel Sambuc   void EmitOMPFlushDirective(const OMPFlushDirective &S);
2025*0a6a1f1dSLionel Sambuc   void EmitOMPOrderedDirective(const OMPOrderedDirective &S);
2026*0a6a1f1dSLionel Sambuc   void EmitOMPAtomicDirective(const OMPAtomicDirective &S);
2027*0a6a1f1dSLionel Sambuc   void EmitOMPTargetDirective(const OMPTargetDirective &S);
2028*0a6a1f1dSLionel Sambuc   void EmitOMPTeamsDirective(const OMPTeamsDirective &S);
2029*0a6a1f1dSLionel Sambuc 
2030*0a6a1f1dSLionel Sambuc private:
2031*0a6a1f1dSLionel Sambuc 
2032*0a6a1f1dSLionel Sambuc   /// Helpers for the OpenMP loop directives.
2033*0a6a1f1dSLionel Sambuc   void EmitOMPLoopBody(const OMPLoopDirective &Directive,
2034*0a6a1f1dSLionel Sambuc                        bool SeparateIter = false);
2035*0a6a1f1dSLionel Sambuc   void EmitOMPInnerLoop(const OMPLoopDirective &S, OMPPrivateScope &LoopScope,
2036*0a6a1f1dSLionel Sambuc                         bool SeparateIter = false);
2037*0a6a1f1dSLionel Sambuc   void EmitOMPSimdFinal(const OMPLoopDirective &S);
2038*0a6a1f1dSLionel Sambuc   void EmitOMPWorksharingLoop(const OMPLoopDirective &S);
2039*0a6a1f1dSLionel Sambuc 
2040*0a6a1f1dSLionel Sambuc public:
2041f4a2713aSLionel Sambuc 
2042f4a2713aSLionel Sambuc   //===--------------------------------------------------------------------===//
2043f4a2713aSLionel Sambuc   //                         LValue Expression Emission
2044f4a2713aSLionel Sambuc   //===--------------------------------------------------------------------===//
2045f4a2713aSLionel Sambuc 
2046f4a2713aSLionel Sambuc   /// GetUndefRValue - Get an appropriate 'undef' rvalue for the given type.
2047f4a2713aSLionel Sambuc   RValue GetUndefRValue(QualType Ty);
2048f4a2713aSLionel Sambuc 
2049f4a2713aSLionel Sambuc   /// EmitUnsupportedRValue - Emit a dummy r-value using the type of E
2050f4a2713aSLionel Sambuc   /// and issue an ErrorUnsupported style diagnostic (using the
2051f4a2713aSLionel Sambuc   /// provided Name).
2052f4a2713aSLionel Sambuc   RValue EmitUnsupportedRValue(const Expr *E,
2053f4a2713aSLionel Sambuc                                const char *Name);
2054f4a2713aSLionel Sambuc 
2055f4a2713aSLionel Sambuc   /// EmitUnsupportedLValue - Emit a dummy l-value using the type of E and issue
2056f4a2713aSLionel Sambuc   /// an ErrorUnsupported style diagnostic (using the provided Name).
2057f4a2713aSLionel Sambuc   LValue EmitUnsupportedLValue(const Expr *E,
2058f4a2713aSLionel Sambuc                                const char *Name);
2059f4a2713aSLionel Sambuc 
2060f4a2713aSLionel Sambuc   /// EmitLValue - Emit code to compute a designator that specifies the location
2061f4a2713aSLionel Sambuc   /// of the expression.
2062f4a2713aSLionel Sambuc   ///
2063f4a2713aSLionel Sambuc   /// This can return one of two things: a simple address or a bitfield
2064f4a2713aSLionel Sambuc   /// reference.  In either case, the LLVM Value* in the LValue structure is
2065f4a2713aSLionel Sambuc   /// guaranteed to be an LLVM pointer type.
2066f4a2713aSLionel Sambuc   ///
2067f4a2713aSLionel Sambuc   /// If this returns a bitfield reference, nothing about the pointee type of
2068f4a2713aSLionel Sambuc   /// the LLVM value is known: For example, it may not be a pointer to an
2069f4a2713aSLionel Sambuc   /// integer.
2070f4a2713aSLionel Sambuc   ///
2071f4a2713aSLionel Sambuc   /// If this returns a normal address, and if the lvalue's C type is fixed
2072f4a2713aSLionel Sambuc   /// size, this method guarantees that the returned pointer type will point to
2073f4a2713aSLionel Sambuc   /// an LLVM type of the same size of the lvalue's type.  If the lvalue has a
2074f4a2713aSLionel Sambuc   /// variable length type, this is not possible.
2075f4a2713aSLionel Sambuc   ///
2076f4a2713aSLionel Sambuc   LValue EmitLValue(const Expr *E);
2077f4a2713aSLionel Sambuc 
2078f4a2713aSLionel Sambuc   /// \brief Same as EmitLValue but additionally we generate checking code to
2079f4a2713aSLionel Sambuc   /// guard against undefined behavior.  This is only suitable when we know
2080f4a2713aSLionel Sambuc   /// that the address will be used to access the object.
2081f4a2713aSLionel Sambuc   LValue EmitCheckedLValue(const Expr *E, TypeCheckKind TCK);
2082f4a2713aSLionel Sambuc 
2083f4a2713aSLionel Sambuc   RValue convertTempToRValue(llvm::Value *addr, QualType type,
2084f4a2713aSLionel Sambuc                              SourceLocation Loc);
2085f4a2713aSLionel Sambuc 
2086f4a2713aSLionel Sambuc   void EmitAtomicInit(Expr *E, LValue lvalue);
2087f4a2713aSLionel Sambuc 
2088f4a2713aSLionel Sambuc   RValue EmitAtomicLoad(LValue lvalue, SourceLocation loc,
2089f4a2713aSLionel Sambuc                         AggValueSlot slot = AggValueSlot::ignored());
2090f4a2713aSLionel Sambuc 
2091f4a2713aSLionel Sambuc   void EmitAtomicStore(RValue rvalue, LValue lvalue, bool isInit);
2092f4a2713aSLionel Sambuc 
2093*0a6a1f1dSLionel Sambuc   std::pair<RValue, RValue> EmitAtomicCompareExchange(
2094*0a6a1f1dSLionel Sambuc       LValue Obj, RValue Expected, RValue Desired, SourceLocation Loc,
2095*0a6a1f1dSLionel Sambuc       llvm::AtomicOrdering Success = llvm::SequentiallyConsistent,
2096*0a6a1f1dSLionel Sambuc       llvm::AtomicOrdering Failure = llvm::SequentiallyConsistent,
2097*0a6a1f1dSLionel Sambuc       bool IsWeak = false, AggValueSlot Slot = AggValueSlot::ignored());
2098*0a6a1f1dSLionel Sambuc 
2099f4a2713aSLionel Sambuc   /// EmitToMemory - Change a scalar value from its value
2100f4a2713aSLionel Sambuc   /// representation to its in-memory representation.
2101f4a2713aSLionel Sambuc   llvm::Value *EmitToMemory(llvm::Value *Value, QualType Ty);
2102f4a2713aSLionel Sambuc 
2103f4a2713aSLionel Sambuc   /// EmitFromMemory - Change a scalar value from its memory
2104f4a2713aSLionel Sambuc   /// representation to its value representation.
2105f4a2713aSLionel Sambuc   llvm::Value *EmitFromMemory(llvm::Value *Value, QualType Ty);
2106f4a2713aSLionel Sambuc 
2107f4a2713aSLionel Sambuc   /// EmitLoadOfScalar - Load a scalar value from an address, taking
2108f4a2713aSLionel Sambuc   /// care to appropriately convert from the memory representation to
2109f4a2713aSLionel Sambuc   /// the LLVM value representation.
2110f4a2713aSLionel Sambuc   llvm::Value *EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
2111f4a2713aSLionel Sambuc                                 unsigned Alignment, QualType Ty,
2112f4a2713aSLionel Sambuc                                 SourceLocation Loc,
2113*0a6a1f1dSLionel Sambuc                                 llvm::MDNode *TBAAInfo = nullptr,
2114f4a2713aSLionel Sambuc                                 QualType TBAABaseTy = QualType(),
2115f4a2713aSLionel Sambuc                                 uint64_t TBAAOffset = 0);
2116f4a2713aSLionel Sambuc 
2117f4a2713aSLionel Sambuc   /// EmitLoadOfScalar - Load a scalar value from an address, taking
2118f4a2713aSLionel Sambuc   /// care to appropriately convert from the memory representation to
2119f4a2713aSLionel Sambuc   /// the LLVM value representation.  The l-value must be a simple
2120f4a2713aSLionel Sambuc   /// l-value.
2121f4a2713aSLionel Sambuc   llvm::Value *EmitLoadOfScalar(LValue lvalue, SourceLocation Loc);
2122f4a2713aSLionel Sambuc 
2123f4a2713aSLionel Sambuc   /// EmitStoreOfScalar - Store a scalar value to an address, taking
2124f4a2713aSLionel Sambuc   /// care to appropriately convert from the memory representation to
2125f4a2713aSLionel Sambuc   /// the LLVM value representation.
2126f4a2713aSLionel Sambuc   void EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
2127f4a2713aSLionel Sambuc                          bool Volatile, unsigned Alignment, QualType Ty,
2128*0a6a1f1dSLionel Sambuc                          llvm::MDNode *TBAAInfo = nullptr, bool isInit = false,
2129f4a2713aSLionel Sambuc                          QualType TBAABaseTy = QualType(),
2130f4a2713aSLionel Sambuc                          uint64_t TBAAOffset = 0);
2131f4a2713aSLionel Sambuc 
2132f4a2713aSLionel Sambuc   /// EmitStoreOfScalar - Store a scalar value to an address, taking
2133f4a2713aSLionel Sambuc   /// care to appropriately convert from the memory representation to
2134f4a2713aSLionel Sambuc   /// the LLVM value representation.  The l-value must be a simple
2135f4a2713aSLionel Sambuc   /// l-value.  The isInit flag indicates whether this is an initialization.
2136f4a2713aSLionel Sambuc   /// If so, atomic qualifiers are ignored and the store is always non-atomic.
2137f4a2713aSLionel Sambuc   void EmitStoreOfScalar(llvm::Value *value, LValue lvalue, bool isInit=false);
2138f4a2713aSLionel Sambuc 
2139f4a2713aSLionel Sambuc   /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
2140f4a2713aSLionel Sambuc   /// this method emits the address of the lvalue, then loads the result as an
2141f4a2713aSLionel Sambuc   /// rvalue, returning the rvalue.
2142f4a2713aSLionel Sambuc   RValue EmitLoadOfLValue(LValue V, SourceLocation Loc);
2143f4a2713aSLionel Sambuc   RValue EmitLoadOfExtVectorElementLValue(LValue V);
2144f4a2713aSLionel Sambuc   RValue EmitLoadOfBitfieldLValue(LValue LV);
2145*0a6a1f1dSLionel Sambuc   RValue EmitLoadOfGlobalRegLValue(LValue LV);
2146f4a2713aSLionel Sambuc 
2147f4a2713aSLionel Sambuc   /// EmitStoreThroughLValue - Store the specified rvalue into the specified
2148f4a2713aSLionel Sambuc   /// lvalue, where both are guaranteed to the have the same type, and that type
2149f4a2713aSLionel Sambuc   /// is 'Ty'.
2150f4a2713aSLionel Sambuc   void EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit = false);
2151f4a2713aSLionel Sambuc   void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst);
2152*0a6a1f1dSLionel Sambuc   void EmitStoreThroughGlobalRegLValue(RValue Src, LValue Dst);
2153f4a2713aSLionel Sambuc 
2154f4a2713aSLionel Sambuc   /// EmitStoreThroughBitfieldLValue - Store Src into Dst with same constraints
2155f4a2713aSLionel Sambuc   /// as EmitStoreThroughLValue.
2156f4a2713aSLionel Sambuc   ///
2157f4a2713aSLionel Sambuc   /// \param Result [out] - If non-null, this will be set to a Value* for the
2158f4a2713aSLionel Sambuc   /// bit-field contents after the store, appropriate for use as the result of
2159f4a2713aSLionel Sambuc   /// an assignment to the bit-field.
2160f4a2713aSLionel Sambuc   void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
2161*0a6a1f1dSLionel Sambuc                                       llvm::Value **Result=nullptr);
2162f4a2713aSLionel Sambuc 
2163f4a2713aSLionel Sambuc   /// Emit an l-value for an assignment (simple or compound) of complex type.
2164f4a2713aSLionel Sambuc   LValue EmitComplexAssignmentLValue(const BinaryOperator *E);
2165f4a2713aSLionel Sambuc   LValue EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E);
2166f4a2713aSLionel Sambuc   LValue EmitScalarCompooundAssignWithComplex(const CompoundAssignOperator *E,
2167f4a2713aSLionel Sambuc                                               llvm::Value *&Result);
2168f4a2713aSLionel Sambuc 
2169f4a2713aSLionel Sambuc   // Note: only available for agg return types
2170f4a2713aSLionel Sambuc   LValue EmitBinaryOperatorLValue(const BinaryOperator *E);
2171f4a2713aSLionel Sambuc   LValue EmitCompoundAssignmentLValue(const CompoundAssignOperator *E);
2172f4a2713aSLionel Sambuc   // Note: only available for agg return types
2173f4a2713aSLionel Sambuc   LValue EmitCallExprLValue(const CallExpr *E);
2174f4a2713aSLionel Sambuc   // Note: only available for agg return types
2175f4a2713aSLionel Sambuc   LValue EmitVAArgExprLValue(const VAArgExpr *E);
2176f4a2713aSLionel Sambuc   LValue EmitDeclRefLValue(const DeclRefExpr *E);
2177*0a6a1f1dSLionel Sambuc   LValue EmitReadRegister(const VarDecl *VD);
2178f4a2713aSLionel Sambuc   LValue EmitStringLiteralLValue(const StringLiteral *E);
2179f4a2713aSLionel Sambuc   LValue EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E);
2180f4a2713aSLionel Sambuc   LValue EmitPredefinedLValue(const PredefinedExpr *E);
2181f4a2713aSLionel Sambuc   LValue EmitUnaryOpLValue(const UnaryOperator *E);
2182f4a2713aSLionel Sambuc   LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
2183f4a2713aSLionel Sambuc                                 bool Accessed = false);
2184f4a2713aSLionel Sambuc   LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E);
2185f4a2713aSLionel Sambuc   LValue EmitMemberExpr(const MemberExpr *E);
2186f4a2713aSLionel Sambuc   LValue EmitObjCIsaExpr(const ObjCIsaExpr *E);
2187f4a2713aSLionel Sambuc   LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E);
2188f4a2713aSLionel Sambuc   LValue EmitInitListLValue(const InitListExpr *E);
2189f4a2713aSLionel Sambuc   LValue EmitConditionalOperatorLValue(const AbstractConditionalOperator *E);
2190f4a2713aSLionel Sambuc   LValue EmitCastLValue(const CastExpr *E);
2191f4a2713aSLionel Sambuc   LValue EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
2192f4a2713aSLionel Sambuc   LValue EmitOpaqueValueLValue(const OpaqueValueExpr *e);
2193f4a2713aSLionel Sambuc 
2194*0a6a1f1dSLionel Sambuc   llvm::Value *EmitExtVectorElementLValue(LValue V);
2195*0a6a1f1dSLionel Sambuc 
2196f4a2713aSLionel Sambuc   RValue EmitRValueForField(LValue LV, const FieldDecl *FD, SourceLocation Loc);
2197f4a2713aSLionel Sambuc 
2198f4a2713aSLionel Sambuc   class ConstantEmission {
2199f4a2713aSLionel Sambuc     llvm::PointerIntPair<llvm::Constant*, 1, bool> ValueAndIsReference;
ConstantEmission(llvm::Constant * C,bool isReference)2200f4a2713aSLionel Sambuc     ConstantEmission(llvm::Constant *C, bool isReference)
2201f4a2713aSLionel Sambuc       : ValueAndIsReference(C, isReference) {}
2202f4a2713aSLionel Sambuc   public:
ConstantEmission()2203f4a2713aSLionel Sambuc     ConstantEmission() {}
forReference(llvm::Constant * C)2204f4a2713aSLionel Sambuc     static ConstantEmission forReference(llvm::Constant *C) {
2205f4a2713aSLionel Sambuc       return ConstantEmission(C, true);
2206f4a2713aSLionel Sambuc     }
forValue(llvm::Constant * C)2207f4a2713aSLionel Sambuc     static ConstantEmission forValue(llvm::Constant *C) {
2208f4a2713aSLionel Sambuc       return ConstantEmission(C, false);
2209f4a2713aSLionel Sambuc     }
2210f4a2713aSLionel Sambuc 
2211*0a6a1f1dSLionel Sambuc     LLVM_EXPLICIT operator bool() const {
2212*0a6a1f1dSLionel Sambuc       return ValueAndIsReference.getOpaqueValue() != nullptr;
2213*0a6a1f1dSLionel Sambuc     }
2214f4a2713aSLionel Sambuc 
isReference()2215f4a2713aSLionel Sambuc     bool isReference() const { return ValueAndIsReference.getInt(); }
getReferenceLValue(CodeGenFunction & CGF,Expr * refExpr)2216f4a2713aSLionel Sambuc     LValue getReferenceLValue(CodeGenFunction &CGF, Expr *refExpr) const {
2217f4a2713aSLionel Sambuc       assert(isReference());
2218f4a2713aSLionel Sambuc       return CGF.MakeNaturalAlignAddrLValue(ValueAndIsReference.getPointer(),
2219f4a2713aSLionel Sambuc                                             refExpr->getType());
2220f4a2713aSLionel Sambuc     }
2221f4a2713aSLionel Sambuc 
getValue()2222f4a2713aSLionel Sambuc     llvm::Constant *getValue() const {
2223f4a2713aSLionel Sambuc       assert(!isReference());
2224f4a2713aSLionel Sambuc       return ValueAndIsReference.getPointer();
2225f4a2713aSLionel Sambuc     }
2226f4a2713aSLionel Sambuc   };
2227f4a2713aSLionel Sambuc 
2228f4a2713aSLionel Sambuc   ConstantEmission tryEmitAsConstant(DeclRefExpr *refExpr);
2229f4a2713aSLionel Sambuc 
2230f4a2713aSLionel Sambuc   RValue EmitPseudoObjectRValue(const PseudoObjectExpr *e,
2231f4a2713aSLionel Sambuc                                 AggValueSlot slot = AggValueSlot::ignored());
2232f4a2713aSLionel Sambuc   LValue EmitPseudoObjectLValue(const PseudoObjectExpr *e);
2233f4a2713aSLionel Sambuc 
2234f4a2713aSLionel Sambuc   llvm::Value *EmitIvarOffset(const ObjCInterfaceDecl *Interface,
2235f4a2713aSLionel Sambuc                               const ObjCIvarDecl *Ivar);
2236f4a2713aSLionel Sambuc   LValue EmitLValueForField(LValue Base, const FieldDecl* Field);
2237f4a2713aSLionel Sambuc   LValue EmitLValueForLambdaField(const FieldDecl *Field);
2238f4a2713aSLionel Sambuc 
2239f4a2713aSLionel Sambuc   /// EmitLValueForFieldInitialization - Like EmitLValueForField, except that
2240f4a2713aSLionel Sambuc   /// if the Field is a reference, this will return the address of the reference
2241f4a2713aSLionel Sambuc   /// and not the address of the value stored in the reference.
2242f4a2713aSLionel Sambuc   LValue EmitLValueForFieldInitialization(LValue Base,
2243f4a2713aSLionel Sambuc                                           const FieldDecl* Field);
2244f4a2713aSLionel Sambuc 
2245f4a2713aSLionel Sambuc   LValue EmitLValueForIvar(QualType ObjectTy,
2246f4a2713aSLionel Sambuc                            llvm::Value* Base, const ObjCIvarDecl *Ivar,
2247f4a2713aSLionel Sambuc                            unsigned CVRQualifiers);
2248f4a2713aSLionel Sambuc 
2249f4a2713aSLionel Sambuc   LValue EmitCXXConstructLValue(const CXXConstructExpr *E);
2250f4a2713aSLionel Sambuc   LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E);
2251f4a2713aSLionel Sambuc   LValue EmitLambdaLValue(const LambdaExpr *E);
2252f4a2713aSLionel Sambuc   LValue EmitCXXTypeidLValue(const CXXTypeidExpr *E);
2253f4a2713aSLionel Sambuc   LValue EmitCXXUuidofLValue(const CXXUuidofExpr *E);
2254f4a2713aSLionel Sambuc 
2255f4a2713aSLionel Sambuc   LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E);
2256f4a2713aSLionel Sambuc   LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E);
2257f4a2713aSLionel Sambuc   LValue EmitStmtExprLValue(const StmtExpr *E);
2258f4a2713aSLionel Sambuc   LValue EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E);
2259f4a2713aSLionel Sambuc   LValue EmitObjCSelectorLValue(const ObjCSelectorExpr *E);
2260f4a2713aSLionel Sambuc   void   EmitDeclRefExprDbgValue(const DeclRefExpr *E, llvm::Constant *Init);
2261f4a2713aSLionel Sambuc 
2262f4a2713aSLionel Sambuc   //===--------------------------------------------------------------------===//
2263f4a2713aSLionel Sambuc   //                         Scalar Expression Emission
2264f4a2713aSLionel Sambuc   //===--------------------------------------------------------------------===//
2265f4a2713aSLionel Sambuc 
2266f4a2713aSLionel Sambuc   /// EmitCall - Generate a call of the given function, expecting the given
2267f4a2713aSLionel Sambuc   /// result type, and using the given argument list which specifies both the
2268f4a2713aSLionel Sambuc   /// LLVM arguments and the types they were derived from.
2269f4a2713aSLionel Sambuc   ///
2270f4a2713aSLionel Sambuc   /// \param TargetDecl - If given, the decl of the function in a direct call;
2271f4a2713aSLionel Sambuc   /// used to set attributes on the call (noreturn, etc.).
2272f4a2713aSLionel Sambuc   RValue EmitCall(const CGFunctionInfo &FnInfo,
2273f4a2713aSLionel Sambuc                   llvm::Value *Callee,
2274f4a2713aSLionel Sambuc                   ReturnValueSlot ReturnValue,
2275f4a2713aSLionel Sambuc                   const CallArgList &Args,
2276*0a6a1f1dSLionel Sambuc                   const Decl *TargetDecl = nullptr,
2277*0a6a1f1dSLionel Sambuc                   llvm::Instruction **callOrInvoke = nullptr);
2278f4a2713aSLionel Sambuc 
2279*0a6a1f1dSLionel Sambuc   RValue EmitCall(QualType FnType, llvm::Value *Callee, const CallExpr *E,
2280f4a2713aSLionel Sambuc                   ReturnValueSlot ReturnValue,
2281*0a6a1f1dSLionel Sambuc                   const Decl *TargetDecl = nullptr,
2282*0a6a1f1dSLionel Sambuc                   llvm::Value *Chain = nullptr);
2283f4a2713aSLionel Sambuc   RValue EmitCallExpr(const CallExpr *E,
2284f4a2713aSLionel Sambuc                       ReturnValueSlot ReturnValue = ReturnValueSlot());
2285f4a2713aSLionel Sambuc 
2286f4a2713aSLionel Sambuc   llvm::CallInst *EmitRuntimeCall(llvm::Value *callee,
2287f4a2713aSLionel Sambuc                                   const Twine &name = "");
2288f4a2713aSLionel Sambuc   llvm::CallInst *EmitRuntimeCall(llvm::Value *callee,
2289f4a2713aSLionel Sambuc                                   ArrayRef<llvm::Value*> args,
2290f4a2713aSLionel Sambuc                                   const Twine &name = "");
2291f4a2713aSLionel Sambuc   llvm::CallInst *EmitNounwindRuntimeCall(llvm::Value *callee,
2292f4a2713aSLionel Sambuc                                           const Twine &name = "");
2293f4a2713aSLionel Sambuc   llvm::CallInst *EmitNounwindRuntimeCall(llvm::Value *callee,
2294f4a2713aSLionel Sambuc                                           ArrayRef<llvm::Value*> args,
2295f4a2713aSLionel Sambuc                                           const Twine &name = "");
2296f4a2713aSLionel Sambuc 
2297f4a2713aSLionel Sambuc   llvm::CallSite EmitCallOrInvoke(llvm::Value *Callee,
2298f4a2713aSLionel Sambuc                                   ArrayRef<llvm::Value *> Args,
2299f4a2713aSLionel Sambuc                                   const Twine &Name = "");
2300f4a2713aSLionel Sambuc   llvm::CallSite EmitCallOrInvoke(llvm::Value *Callee,
2301f4a2713aSLionel Sambuc                                   const Twine &Name = "");
2302f4a2713aSLionel Sambuc   llvm::CallSite EmitRuntimeCallOrInvoke(llvm::Value *callee,
2303f4a2713aSLionel Sambuc                                          ArrayRef<llvm::Value*> args,
2304f4a2713aSLionel Sambuc                                          const Twine &name = "");
2305f4a2713aSLionel Sambuc   llvm::CallSite EmitRuntimeCallOrInvoke(llvm::Value *callee,
2306f4a2713aSLionel Sambuc                                          const Twine &name = "");
2307f4a2713aSLionel Sambuc   void EmitNoreturnRuntimeCallOrInvoke(llvm::Value *callee,
2308f4a2713aSLionel Sambuc                                        ArrayRef<llvm::Value*> args);
2309f4a2713aSLionel Sambuc 
2310f4a2713aSLionel Sambuc   llvm::Value *BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
2311f4a2713aSLionel Sambuc                                          NestedNameSpecifier *Qual,
2312f4a2713aSLionel Sambuc                                          llvm::Type *Ty);
2313f4a2713aSLionel Sambuc 
2314f4a2713aSLionel Sambuc   llvm::Value *BuildAppleKextVirtualDestructorCall(const CXXDestructorDecl *DD,
2315f4a2713aSLionel Sambuc                                                    CXXDtorType Type,
2316f4a2713aSLionel Sambuc                                                    const CXXRecordDecl *RD);
2317f4a2713aSLionel Sambuc 
2318*0a6a1f1dSLionel Sambuc   RValue
2319*0a6a1f1dSLionel Sambuc   EmitCXXMemberOrOperatorCall(const CXXMethodDecl *MD, llvm::Value *Callee,
2320*0a6a1f1dSLionel Sambuc                               ReturnValueSlot ReturnValue, llvm::Value *This,
2321f4a2713aSLionel Sambuc                               llvm::Value *ImplicitParam,
2322*0a6a1f1dSLionel Sambuc                               QualType ImplicitParamTy, const CallExpr *E);
2323*0a6a1f1dSLionel Sambuc   RValue EmitCXXStructorCall(const CXXMethodDecl *MD, llvm::Value *Callee,
2324*0a6a1f1dSLionel Sambuc                              ReturnValueSlot ReturnValue, llvm::Value *This,
2325*0a6a1f1dSLionel Sambuc                              llvm::Value *ImplicitParam,
2326*0a6a1f1dSLionel Sambuc                              QualType ImplicitParamTy, const CallExpr *E,
2327*0a6a1f1dSLionel Sambuc                              StructorType Type);
2328f4a2713aSLionel Sambuc   RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E,
2329f4a2713aSLionel Sambuc                                ReturnValueSlot ReturnValue);
2330*0a6a1f1dSLionel Sambuc   RValue EmitCXXMemberOrOperatorMemberCallExpr(const CallExpr *CE,
2331*0a6a1f1dSLionel Sambuc                                                const CXXMethodDecl *MD,
2332*0a6a1f1dSLionel Sambuc                                                ReturnValueSlot ReturnValue,
2333*0a6a1f1dSLionel Sambuc                                                bool HasQualifier,
2334*0a6a1f1dSLionel Sambuc                                                NestedNameSpecifier *Qualifier,
2335*0a6a1f1dSLionel Sambuc                                                bool IsArrow, const Expr *Base);
2336*0a6a1f1dSLionel Sambuc   // Compute the object pointer.
2337f4a2713aSLionel Sambuc   RValue EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
2338f4a2713aSLionel Sambuc                                       ReturnValueSlot ReturnValue);
2339f4a2713aSLionel Sambuc 
2340f4a2713aSLionel Sambuc   RValue EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
2341f4a2713aSLionel Sambuc                                        const CXXMethodDecl *MD,
2342f4a2713aSLionel Sambuc                                        ReturnValueSlot ReturnValue);
2343f4a2713aSLionel Sambuc 
2344f4a2713aSLionel Sambuc   RValue EmitCUDAKernelCallExpr(const CUDAKernelCallExpr *E,
2345f4a2713aSLionel Sambuc                                 ReturnValueSlot ReturnValue);
2346f4a2713aSLionel Sambuc 
2347f4a2713aSLionel Sambuc 
2348f4a2713aSLionel Sambuc   RValue EmitBuiltinExpr(const FunctionDecl *FD,
2349*0a6a1f1dSLionel Sambuc                          unsigned BuiltinID, const CallExpr *E,
2350*0a6a1f1dSLionel Sambuc                          ReturnValueSlot ReturnValue);
2351f4a2713aSLionel Sambuc 
2352f4a2713aSLionel Sambuc   RValue EmitBlockCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue);
2353f4a2713aSLionel Sambuc 
2354f4a2713aSLionel Sambuc   /// EmitTargetBuiltinExpr - Emit the given builtin call. Returns 0 if the call
2355f4a2713aSLionel Sambuc   /// is unhandled by the current target.
2356f4a2713aSLionel Sambuc   llvm::Value *EmitTargetBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2357f4a2713aSLionel Sambuc 
2358f4a2713aSLionel Sambuc   llvm::Value *EmitAArch64CompareBuiltinExpr(llvm::Value *Op, llvm::Type *Ty,
2359f4a2713aSLionel Sambuc                                              const llvm::CmpInst::Predicate Fp,
2360f4a2713aSLionel Sambuc                                              const llvm::CmpInst::Predicate Ip,
2361f4a2713aSLionel Sambuc                                              const llvm::Twine &Name = "");
2362f4a2713aSLionel Sambuc   llvm::Value *EmitARMBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2363*0a6a1f1dSLionel Sambuc 
2364*0a6a1f1dSLionel Sambuc   llvm::Value *EmitCommonNeonBuiltinExpr(unsigned BuiltinID,
2365*0a6a1f1dSLionel Sambuc                                          unsigned LLVMIntrinsic,
2366*0a6a1f1dSLionel Sambuc                                          unsigned AltLLVMIntrinsic,
2367*0a6a1f1dSLionel Sambuc                                          const char *NameHint,
2368*0a6a1f1dSLionel Sambuc                                          unsigned Modifier,
2369*0a6a1f1dSLionel Sambuc                                          const CallExpr *E,
2370*0a6a1f1dSLionel Sambuc                                          SmallVectorImpl<llvm::Value *> &Ops,
2371*0a6a1f1dSLionel Sambuc                                          llvm::Value *Align = nullptr);
2372*0a6a1f1dSLionel Sambuc   llvm::Function *LookupNeonLLVMIntrinsic(unsigned IntrinsicID,
2373*0a6a1f1dSLionel Sambuc                                           unsigned Modifier, llvm::Type *ArgTy,
2374*0a6a1f1dSLionel Sambuc                                           const CallExpr *E);
2375f4a2713aSLionel Sambuc   llvm::Value *EmitNeonCall(llvm::Function *F,
2376f4a2713aSLionel Sambuc                             SmallVectorImpl<llvm::Value*> &O,
2377f4a2713aSLionel Sambuc                             const char *name,
2378f4a2713aSLionel Sambuc                             unsigned shift = 0, bool rightshift = false);
2379f4a2713aSLionel Sambuc   llvm::Value *EmitNeonSplat(llvm::Value *V, llvm::Constant *Idx);
2380f4a2713aSLionel Sambuc   llvm::Value *EmitNeonShiftVector(llvm::Value *V, llvm::Type *Ty,
2381f4a2713aSLionel Sambuc                                    bool negateForRightShift);
2382f4a2713aSLionel Sambuc   llvm::Value *EmitNeonRShiftImm(llvm::Value *Vec, llvm::Value *Amt,
2383f4a2713aSLionel Sambuc                                  llvm::Type *Ty, bool usgn, const char *name);
2384*0a6a1f1dSLionel Sambuc   // Helper functions for EmitAArch64BuiltinExpr.
2385*0a6a1f1dSLionel Sambuc   llvm::Value *vectorWrapScalar8(llvm::Value *Op);
2386*0a6a1f1dSLionel Sambuc   llvm::Value *vectorWrapScalar16(llvm::Value *Op);
2387*0a6a1f1dSLionel Sambuc   llvm::Value *emitVectorWrappedScalar8Intrinsic(
2388*0a6a1f1dSLionel Sambuc       unsigned Int, SmallVectorImpl<llvm::Value *> &Ops, const char *Name);
2389*0a6a1f1dSLionel Sambuc   llvm::Value *emitVectorWrappedScalar16Intrinsic(
2390*0a6a1f1dSLionel Sambuc       unsigned Int, SmallVectorImpl<llvm::Value *> &Ops, const char *Name);
2391*0a6a1f1dSLionel Sambuc   llvm::Value *EmitAArch64BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2392*0a6a1f1dSLionel Sambuc   llvm::Value *EmitNeon64Call(llvm::Function *F,
2393*0a6a1f1dSLionel Sambuc                               llvm::SmallVectorImpl<llvm::Value *> &O,
2394*0a6a1f1dSLionel Sambuc                               const char *name);
2395f4a2713aSLionel Sambuc 
2396f4a2713aSLionel Sambuc   llvm::Value *BuildVector(ArrayRef<llvm::Value*> Ops);
2397f4a2713aSLionel Sambuc   llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2398f4a2713aSLionel Sambuc   llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2399*0a6a1f1dSLionel Sambuc   llvm::Value *EmitR600BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2400f4a2713aSLionel Sambuc 
2401f4a2713aSLionel Sambuc   llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E);
2402f4a2713aSLionel Sambuc   llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E);
2403f4a2713aSLionel Sambuc   llvm::Value *EmitObjCBoxedExpr(const ObjCBoxedExpr *E);
2404f4a2713aSLionel Sambuc   llvm::Value *EmitObjCArrayLiteral(const ObjCArrayLiteral *E);
2405f4a2713aSLionel Sambuc   llvm::Value *EmitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E);
2406f4a2713aSLionel Sambuc   llvm::Value *EmitObjCCollectionLiteral(const Expr *E,
2407f4a2713aSLionel Sambuc                                 const ObjCMethodDecl *MethodWithObjects);
2408f4a2713aSLionel Sambuc   llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E);
2409f4a2713aSLionel Sambuc   RValue EmitObjCMessageExpr(const ObjCMessageExpr *E,
2410f4a2713aSLionel Sambuc                              ReturnValueSlot Return = ReturnValueSlot());
2411f4a2713aSLionel Sambuc 
2412f4a2713aSLionel Sambuc   /// Retrieves the default cleanup kind for an ARC cleanup.
2413f4a2713aSLionel Sambuc   /// Except under -fobjc-arc-eh, ARC cleanups are normal-only.
getARCCleanupKind()2414f4a2713aSLionel Sambuc   CleanupKind getARCCleanupKind() {
2415f4a2713aSLionel Sambuc     return CGM.getCodeGenOpts().ObjCAutoRefCountExceptions
2416f4a2713aSLionel Sambuc              ? NormalAndEHCleanup : NormalCleanup;
2417f4a2713aSLionel Sambuc   }
2418f4a2713aSLionel Sambuc 
2419f4a2713aSLionel Sambuc   // ARC primitives.
2420f4a2713aSLionel Sambuc   void EmitARCInitWeak(llvm::Value *value, llvm::Value *addr);
2421f4a2713aSLionel Sambuc   void EmitARCDestroyWeak(llvm::Value *addr);
2422f4a2713aSLionel Sambuc   llvm::Value *EmitARCLoadWeak(llvm::Value *addr);
2423f4a2713aSLionel Sambuc   llvm::Value *EmitARCLoadWeakRetained(llvm::Value *addr);
2424f4a2713aSLionel Sambuc   llvm::Value *EmitARCStoreWeak(llvm::Value *value, llvm::Value *addr,
2425f4a2713aSLionel Sambuc                                 bool ignored);
2426f4a2713aSLionel Sambuc   void EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src);
2427f4a2713aSLionel Sambuc   void EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src);
2428f4a2713aSLionel Sambuc   llvm::Value *EmitARCRetainAutorelease(QualType type, llvm::Value *value);
2429f4a2713aSLionel Sambuc   llvm::Value *EmitARCRetainAutoreleaseNonBlock(llvm::Value *value);
2430f4a2713aSLionel Sambuc   llvm::Value *EmitARCStoreStrong(LValue lvalue, llvm::Value *value,
2431f4a2713aSLionel Sambuc                                   bool resultIgnored);
2432f4a2713aSLionel Sambuc   llvm::Value *EmitARCStoreStrongCall(llvm::Value *addr, llvm::Value *value,
2433f4a2713aSLionel Sambuc                                       bool resultIgnored);
2434f4a2713aSLionel Sambuc   llvm::Value *EmitARCRetain(QualType type, llvm::Value *value);
2435f4a2713aSLionel Sambuc   llvm::Value *EmitARCRetainNonBlock(llvm::Value *value);
2436f4a2713aSLionel Sambuc   llvm::Value *EmitARCRetainBlock(llvm::Value *value, bool mandatory);
2437f4a2713aSLionel Sambuc   void EmitARCDestroyStrong(llvm::Value *addr, ARCPreciseLifetime_t precise);
2438f4a2713aSLionel Sambuc   void EmitARCRelease(llvm::Value *value, ARCPreciseLifetime_t precise);
2439f4a2713aSLionel Sambuc   llvm::Value *EmitARCAutorelease(llvm::Value *value);
2440f4a2713aSLionel Sambuc   llvm::Value *EmitARCAutoreleaseReturnValue(llvm::Value *value);
2441f4a2713aSLionel Sambuc   llvm::Value *EmitARCRetainAutoreleaseReturnValue(llvm::Value *value);
2442f4a2713aSLionel Sambuc   llvm::Value *EmitARCRetainAutoreleasedReturnValue(llvm::Value *value);
2443f4a2713aSLionel Sambuc 
2444f4a2713aSLionel Sambuc   std::pair<LValue,llvm::Value*>
2445f4a2713aSLionel Sambuc   EmitARCStoreAutoreleasing(const BinaryOperator *e);
2446f4a2713aSLionel Sambuc   std::pair<LValue,llvm::Value*>
2447f4a2713aSLionel Sambuc   EmitARCStoreStrong(const BinaryOperator *e, bool ignored);
2448f4a2713aSLionel Sambuc 
2449f4a2713aSLionel Sambuc   llvm::Value *EmitObjCThrowOperand(const Expr *expr);
2450f4a2713aSLionel Sambuc 
2451f4a2713aSLionel Sambuc   llvm::Value *EmitObjCProduceObject(QualType T, llvm::Value *Ptr);
2452f4a2713aSLionel Sambuc   llvm::Value *EmitObjCConsumeObject(QualType T, llvm::Value *Ptr);
2453f4a2713aSLionel Sambuc   llvm::Value *EmitObjCExtendObjectLifetime(QualType T, llvm::Value *Ptr);
2454f4a2713aSLionel Sambuc 
2455f4a2713aSLionel Sambuc   llvm::Value *EmitARCExtendBlockObject(const Expr *expr);
2456f4a2713aSLionel Sambuc   llvm::Value *EmitARCRetainScalarExpr(const Expr *expr);
2457f4a2713aSLionel Sambuc   llvm::Value *EmitARCRetainAutoreleaseScalarExpr(const Expr *expr);
2458f4a2713aSLionel Sambuc 
2459*0a6a1f1dSLionel Sambuc   void EmitARCIntrinsicUse(ArrayRef<llvm::Value*> values);
2460f4a2713aSLionel Sambuc 
2461f4a2713aSLionel Sambuc   static Destroyer destroyARCStrongImprecise;
2462f4a2713aSLionel Sambuc   static Destroyer destroyARCStrongPrecise;
2463f4a2713aSLionel Sambuc   static Destroyer destroyARCWeak;
2464f4a2713aSLionel Sambuc 
2465f4a2713aSLionel Sambuc   void EmitObjCAutoreleasePoolPop(llvm::Value *Ptr);
2466f4a2713aSLionel Sambuc   llvm::Value *EmitObjCAutoreleasePoolPush();
2467f4a2713aSLionel Sambuc   llvm::Value *EmitObjCMRRAutoreleasePoolPush();
2468f4a2713aSLionel Sambuc   void EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr);
2469f4a2713aSLionel Sambuc   void EmitObjCMRRAutoreleasePoolPop(llvm::Value *Ptr);
2470f4a2713aSLionel Sambuc 
2471f4a2713aSLionel Sambuc   /// \brief Emits a reference binding to the passed in expression.
2472f4a2713aSLionel Sambuc   RValue EmitReferenceBindingToExpr(const Expr *E);
2473f4a2713aSLionel Sambuc 
2474f4a2713aSLionel Sambuc   //===--------------------------------------------------------------------===//
2475f4a2713aSLionel Sambuc   //                           Expression Emission
2476f4a2713aSLionel Sambuc   //===--------------------------------------------------------------------===//
2477f4a2713aSLionel Sambuc 
2478f4a2713aSLionel Sambuc   // Expressions are broken into three classes: scalar, complex, aggregate.
2479f4a2713aSLionel Sambuc 
2480f4a2713aSLionel Sambuc   /// EmitScalarExpr - Emit the computation of the specified expression of LLVM
2481f4a2713aSLionel Sambuc   /// scalar type, returning the result.
2482f4a2713aSLionel Sambuc   llvm::Value *EmitScalarExpr(const Expr *E , bool IgnoreResultAssign = false);
2483f4a2713aSLionel Sambuc 
2484f4a2713aSLionel Sambuc   /// EmitScalarConversion - Emit a conversion from the specified type to the
2485f4a2713aSLionel Sambuc   /// specified destination type, both of which are LLVM scalar types.
2486f4a2713aSLionel Sambuc   llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy,
2487f4a2713aSLionel Sambuc                                     QualType DstTy);
2488f4a2713aSLionel Sambuc 
2489f4a2713aSLionel Sambuc   /// EmitComplexToScalarConversion - Emit a conversion from the specified
2490f4a2713aSLionel Sambuc   /// complex type to the specified destination type, where the destination type
2491f4a2713aSLionel Sambuc   /// is an LLVM scalar type.
2492f4a2713aSLionel Sambuc   llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy,
2493f4a2713aSLionel Sambuc                                              QualType DstTy);
2494f4a2713aSLionel Sambuc 
2495f4a2713aSLionel Sambuc 
2496f4a2713aSLionel Sambuc   /// EmitAggExpr - Emit the computation of the specified expression
2497f4a2713aSLionel Sambuc   /// of aggregate type.  The result is computed into the given slot,
2498f4a2713aSLionel Sambuc   /// which may be null to indicate that the value is not needed.
2499f4a2713aSLionel Sambuc   void EmitAggExpr(const Expr *E, AggValueSlot AS);
2500f4a2713aSLionel Sambuc 
2501f4a2713aSLionel Sambuc   /// EmitAggExprToLValue - Emit the computation of the specified expression of
2502f4a2713aSLionel Sambuc   /// aggregate type into a temporary LValue.
2503f4a2713aSLionel Sambuc   LValue EmitAggExprToLValue(const Expr *E);
2504f4a2713aSLionel Sambuc 
2505f4a2713aSLionel Sambuc   /// EmitGCMemmoveCollectable - Emit special API for structs with object
2506f4a2713aSLionel Sambuc   /// pointers.
2507f4a2713aSLionel Sambuc   void EmitGCMemmoveCollectable(llvm::Value *DestPtr, llvm::Value *SrcPtr,
2508f4a2713aSLionel Sambuc                                 QualType Ty);
2509f4a2713aSLionel Sambuc 
2510f4a2713aSLionel Sambuc   /// EmitExtendGCLifetime - Given a pointer to an Objective-C object,
2511f4a2713aSLionel Sambuc   /// make sure it survives garbage collection until this point.
2512f4a2713aSLionel Sambuc   void EmitExtendGCLifetime(llvm::Value *object);
2513f4a2713aSLionel Sambuc 
2514f4a2713aSLionel Sambuc   /// EmitComplexExpr - Emit the computation of the specified expression of
2515f4a2713aSLionel Sambuc   /// complex type, returning the result.
2516f4a2713aSLionel Sambuc   ComplexPairTy EmitComplexExpr(const Expr *E,
2517f4a2713aSLionel Sambuc                                 bool IgnoreReal = false,
2518f4a2713aSLionel Sambuc                                 bool IgnoreImag = false);
2519f4a2713aSLionel Sambuc 
2520f4a2713aSLionel Sambuc   /// EmitComplexExprIntoLValue - Emit the given expression of complex
2521f4a2713aSLionel Sambuc   /// type and place its result into the specified l-value.
2522f4a2713aSLionel Sambuc   void EmitComplexExprIntoLValue(const Expr *E, LValue dest, bool isInit);
2523f4a2713aSLionel Sambuc 
2524f4a2713aSLionel Sambuc   /// EmitStoreOfComplex - Store a complex number into the specified l-value.
2525f4a2713aSLionel Sambuc   void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit);
2526f4a2713aSLionel Sambuc 
2527f4a2713aSLionel Sambuc   /// EmitLoadOfComplex - Load a complex number from the specified l-value.
2528f4a2713aSLionel Sambuc   ComplexPairTy EmitLoadOfComplex(LValue src, SourceLocation loc);
2529f4a2713aSLionel Sambuc 
2530f4a2713aSLionel Sambuc   /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
2531f4a2713aSLionel Sambuc   /// global variable that has already been created for it.  If the initializer
2532f4a2713aSLionel Sambuc   /// has a different type than GV does, this may free GV and return a different
2533f4a2713aSLionel Sambuc   /// one.  Otherwise it just returns GV.
2534f4a2713aSLionel Sambuc   llvm::GlobalVariable *
2535f4a2713aSLionel Sambuc   AddInitializerToStaticVarDecl(const VarDecl &D,
2536f4a2713aSLionel Sambuc                                 llvm::GlobalVariable *GV);
2537f4a2713aSLionel Sambuc 
2538f4a2713aSLionel Sambuc 
2539f4a2713aSLionel Sambuc   /// EmitCXXGlobalVarDeclInit - Create the initializer for a C++
2540f4a2713aSLionel Sambuc   /// variable with global storage.
2541f4a2713aSLionel Sambuc   void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::Constant *DeclPtr,
2542f4a2713aSLionel Sambuc                                 bool PerformInit);
2543f4a2713aSLionel Sambuc 
2544*0a6a1f1dSLionel Sambuc   llvm::Constant *createAtExitStub(const VarDecl &VD, llvm::Constant *Dtor,
2545*0a6a1f1dSLionel Sambuc                                    llvm::Constant *Addr);
2546*0a6a1f1dSLionel Sambuc 
2547f4a2713aSLionel Sambuc   /// Call atexit() with a function that passes the given argument to
2548f4a2713aSLionel Sambuc   /// the given function.
2549f4a2713aSLionel Sambuc   void registerGlobalDtorWithAtExit(const VarDecl &D, llvm::Constant *fn,
2550f4a2713aSLionel Sambuc                                     llvm::Constant *addr);
2551f4a2713aSLionel Sambuc 
2552f4a2713aSLionel Sambuc   /// Emit code in this function to perform a guarded variable
2553f4a2713aSLionel Sambuc   /// initialization.  Guarded initializations are used when it's not
2554f4a2713aSLionel Sambuc   /// possible to prove that an initialization will be done exactly
2555f4a2713aSLionel Sambuc   /// once, e.g. with a static local variable or a static data member
2556f4a2713aSLionel Sambuc   /// of a class template.
2557f4a2713aSLionel Sambuc   void EmitCXXGuardedInit(const VarDecl &D, llvm::GlobalVariable *DeclPtr,
2558f4a2713aSLionel Sambuc                           bool PerformInit);
2559f4a2713aSLionel Sambuc 
2560f4a2713aSLionel Sambuc   /// GenerateCXXGlobalInitFunc - Generates code for initializing global
2561f4a2713aSLionel Sambuc   /// variables.
2562f4a2713aSLionel Sambuc   void GenerateCXXGlobalInitFunc(llvm::Function *Fn,
2563*0a6a1f1dSLionel Sambuc                                  ArrayRef<llvm::Function *> CXXThreadLocals,
2564*0a6a1f1dSLionel Sambuc                                  llvm::GlobalVariable *Guard = nullptr);
2565f4a2713aSLionel Sambuc 
2566f4a2713aSLionel Sambuc   /// GenerateCXXGlobalDtorsFunc - Generates code for destroying global
2567f4a2713aSLionel Sambuc   /// variables.
2568f4a2713aSLionel Sambuc   void GenerateCXXGlobalDtorsFunc(llvm::Function *Fn,
2569f4a2713aSLionel Sambuc                                   const std::vector<std::pair<llvm::WeakVH,
2570f4a2713aSLionel Sambuc                                   llvm::Constant*> > &DtorsAndObjects);
2571f4a2713aSLionel Sambuc 
2572f4a2713aSLionel Sambuc   void GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
2573f4a2713aSLionel Sambuc                                         const VarDecl *D,
2574f4a2713aSLionel Sambuc                                         llvm::GlobalVariable *Addr,
2575f4a2713aSLionel Sambuc                                         bool PerformInit);
2576f4a2713aSLionel Sambuc 
2577f4a2713aSLionel Sambuc   void EmitCXXConstructExpr(const CXXConstructExpr *E, AggValueSlot Dest);
2578f4a2713aSLionel Sambuc 
2579f4a2713aSLionel Sambuc   void EmitSynthesizedCXXCopyCtor(llvm::Value *Dest, llvm::Value *Src,
2580f4a2713aSLionel Sambuc                                   const Expr *Exp);
2581f4a2713aSLionel Sambuc 
enterFullExpression(const ExprWithCleanups * E)2582f4a2713aSLionel Sambuc   void enterFullExpression(const ExprWithCleanups *E) {
2583f4a2713aSLionel Sambuc     if (E->getNumObjects() == 0) return;
2584f4a2713aSLionel Sambuc     enterNonTrivialFullExpression(E);
2585f4a2713aSLionel Sambuc   }
2586f4a2713aSLionel Sambuc   void enterNonTrivialFullExpression(const ExprWithCleanups *E);
2587f4a2713aSLionel Sambuc 
2588f4a2713aSLionel Sambuc   void EmitCXXThrowExpr(const CXXThrowExpr *E, bool KeepInsertionPoint = true);
2589f4a2713aSLionel Sambuc 
2590f4a2713aSLionel Sambuc   void EmitLambdaExpr(const LambdaExpr *E, AggValueSlot Dest);
2591f4a2713aSLionel Sambuc 
2592*0a6a1f1dSLionel Sambuc   RValue EmitAtomicExpr(AtomicExpr *E, llvm::Value *Dest = nullptr);
2593f4a2713aSLionel Sambuc 
2594f4a2713aSLionel Sambuc   //===--------------------------------------------------------------------===//
2595f4a2713aSLionel Sambuc   //                         Annotations Emission
2596f4a2713aSLionel Sambuc   //===--------------------------------------------------------------------===//
2597f4a2713aSLionel Sambuc 
2598f4a2713aSLionel Sambuc   /// Emit an annotation call (intrinsic or builtin).
2599f4a2713aSLionel Sambuc   llvm::Value *EmitAnnotationCall(llvm::Value *AnnotationFn,
2600f4a2713aSLionel Sambuc                                   llvm::Value *AnnotatedVal,
2601f4a2713aSLionel Sambuc                                   StringRef AnnotationStr,
2602f4a2713aSLionel Sambuc                                   SourceLocation Location);
2603f4a2713aSLionel Sambuc 
2604f4a2713aSLionel Sambuc   /// Emit local annotations for the local variable V, declared by D.
2605f4a2713aSLionel Sambuc   void EmitVarAnnotations(const VarDecl *D, llvm::Value *V);
2606f4a2713aSLionel Sambuc 
2607f4a2713aSLionel Sambuc   /// Emit field annotations for the given field & value. Returns the
2608f4a2713aSLionel Sambuc   /// annotation result.
2609f4a2713aSLionel Sambuc   llvm::Value *EmitFieldAnnotations(const FieldDecl *D, llvm::Value *V);
2610f4a2713aSLionel Sambuc 
2611f4a2713aSLionel Sambuc   //===--------------------------------------------------------------------===//
2612f4a2713aSLionel Sambuc   //                             Internal Helpers
2613f4a2713aSLionel Sambuc   //===--------------------------------------------------------------------===//
2614f4a2713aSLionel Sambuc 
2615f4a2713aSLionel Sambuc   /// ContainsLabel - Return true if the statement contains a label in it.  If
2616f4a2713aSLionel Sambuc   /// this statement is not executed normally, it not containing a label means
2617f4a2713aSLionel Sambuc   /// that we can just remove the code.
2618f4a2713aSLionel Sambuc   static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts = false);
2619f4a2713aSLionel Sambuc 
2620f4a2713aSLionel Sambuc   /// containsBreak - Return true if the statement contains a break out of it.
2621f4a2713aSLionel Sambuc   /// If the statement (recursively) contains a switch or loop with a break
2622f4a2713aSLionel Sambuc   /// inside of it, this is fine.
2623f4a2713aSLionel Sambuc   static bool containsBreak(const Stmt *S);
2624f4a2713aSLionel Sambuc 
2625f4a2713aSLionel Sambuc   /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
2626f4a2713aSLionel Sambuc   /// to a constant, or if it does but contains a label, return false.  If it
2627f4a2713aSLionel Sambuc   /// constant folds return true and set the boolean result in Result.
2628f4a2713aSLionel Sambuc   bool ConstantFoldsToSimpleInteger(const Expr *Cond, bool &Result);
2629f4a2713aSLionel Sambuc 
2630f4a2713aSLionel Sambuc   /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
2631f4a2713aSLionel Sambuc   /// to a constant, or if it does but contains a label, return false.  If it
2632f4a2713aSLionel Sambuc   /// constant folds return true and set the folded value.
2633f4a2713aSLionel Sambuc   bool ConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APSInt &Result);
2634f4a2713aSLionel Sambuc 
2635f4a2713aSLionel Sambuc   /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an
2636f4a2713aSLionel Sambuc   /// if statement) to the specified blocks.  Based on the condition, this might
2637f4a2713aSLionel Sambuc   /// try to simplify the codegen of the conditional based on the branch.
2638*0a6a1f1dSLionel Sambuc   /// TrueCount should be the number of times we expect the condition to
2639*0a6a1f1dSLionel Sambuc   /// evaluate to true based on PGO data.
2640f4a2713aSLionel Sambuc   void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock,
2641*0a6a1f1dSLionel Sambuc                             llvm::BasicBlock *FalseBlock, uint64_t TrueCount);
2642f4a2713aSLionel Sambuc 
2643f4a2713aSLionel Sambuc   /// \brief Emit a description of a type in a format suitable for passing to
2644f4a2713aSLionel Sambuc   /// a runtime sanitizer handler.
2645f4a2713aSLionel Sambuc   llvm::Constant *EmitCheckTypeDescriptor(QualType T);
2646f4a2713aSLionel Sambuc 
2647f4a2713aSLionel Sambuc   /// \brief Convert a value into a format suitable for passing to a runtime
2648f4a2713aSLionel Sambuc   /// sanitizer handler.
2649f4a2713aSLionel Sambuc   llvm::Value *EmitCheckValue(llvm::Value *V);
2650f4a2713aSLionel Sambuc 
2651f4a2713aSLionel Sambuc   /// \brief Emit a description of a source location in a format suitable for
2652f4a2713aSLionel Sambuc   /// passing to a runtime sanitizer handler.
2653f4a2713aSLionel Sambuc   llvm::Constant *EmitCheckSourceLocation(SourceLocation Loc);
2654f4a2713aSLionel Sambuc 
2655f4a2713aSLionel Sambuc   /// \brief Create a basic block that will call a handler function in a
2656f4a2713aSLionel Sambuc   /// sanitizer runtime with the provided arguments, and create a conditional
2657f4a2713aSLionel Sambuc   /// branch to it.
2658*0a6a1f1dSLionel Sambuc   void EmitCheck(ArrayRef<std::pair<llvm::Value *, SanitizerKind>> Checked,
2659*0a6a1f1dSLionel Sambuc                  StringRef CheckName, ArrayRef<llvm::Constant *> StaticArgs,
2660*0a6a1f1dSLionel Sambuc                  ArrayRef<llvm::Value *> DynamicArgs);
2661f4a2713aSLionel Sambuc 
2662f4a2713aSLionel Sambuc   /// \brief Create a basic block that will call the trap intrinsic, and emit a
2663f4a2713aSLionel Sambuc   /// conditional branch to it, for the -ftrapv checks.
2664f4a2713aSLionel Sambuc   void EmitTrapCheck(llvm::Value *Checked);
2665f4a2713aSLionel Sambuc 
2666f4a2713aSLionel Sambuc   /// EmitCallArg - Emit a single call argument.
2667f4a2713aSLionel Sambuc   void EmitCallArg(CallArgList &args, const Expr *E, QualType ArgType);
2668f4a2713aSLionel Sambuc 
2669f4a2713aSLionel Sambuc   /// EmitDelegateCallArg - We are performing a delegate call; that
2670f4a2713aSLionel Sambuc   /// is, the current function is delegating to another one.  Produce
2671f4a2713aSLionel Sambuc   /// a r-value suitable for passing the given parameter.
2672f4a2713aSLionel Sambuc   void EmitDelegateCallArg(CallArgList &args, const VarDecl *param,
2673f4a2713aSLionel Sambuc                            SourceLocation loc);
2674f4a2713aSLionel Sambuc 
2675f4a2713aSLionel Sambuc   /// SetFPAccuracy - Set the minimum required accuracy of the given floating
2676f4a2713aSLionel Sambuc   /// point operation, expressed as the maximum relative error in ulp.
2677f4a2713aSLionel Sambuc   void SetFPAccuracy(llvm::Value *Val, float Accuracy);
2678f4a2713aSLionel Sambuc 
2679f4a2713aSLionel Sambuc private:
2680f4a2713aSLionel Sambuc   llvm::MDNode *getRangeForLoadFromType(QualType Ty);
2681f4a2713aSLionel Sambuc   void EmitReturnOfRValue(RValue RV, QualType Ty);
2682f4a2713aSLionel Sambuc 
2683*0a6a1f1dSLionel Sambuc   void deferPlaceholderReplacement(llvm::Instruction *Old, llvm::Value *New);
2684*0a6a1f1dSLionel Sambuc 
2685*0a6a1f1dSLionel Sambuc   llvm::SmallVector<std::pair<llvm::Instruction *, llvm::Value *>, 4>
2686*0a6a1f1dSLionel Sambuc   DeferredReplacements;
2687*0a6a1f1dSLionel Sambuc 
2688f4a2713aSLionel Sambuc   /// ExpandTypeFromArgs - Reconstruct a structure of type \arg Ty
2689f4a2713aSLionel Sambuc   /// from function arguments into \arg Dst. See ABIArgInfo::Expand.
2690f4a2713aSLionel Sambuc   ///
2691f4a2713aSLionel Sambuc   /// \param AI - The first function argument of the expansion.
2692*0a6a1f1dSLionel Sambuc   void ExpandTypeFromArgs(QualType Ty, LValue Dst,
2693*0a6a1f1dSLionel Sambuc                           SmallVectorImpl<llvm::Argument *>::iterator &AI);
2694f4a2713aSLionel Sambuc 
2695*0a6a1f1dSLionel Sambuc   /// ExpandTypeToArgs - Expand an RValue \arg RV, with the LLVM type for \arg
2696*0a6a1f1dSLionel Sambuc   /// Ty, into individual arguments on the provided vector \arg IRCallArgs,
2697*0a6a1f1dSLionel Sambuc   /// starting at index \arg IRCallArgPos. See ABIArgInfo::Expand.
2698*0a6a1f1dSLionel Sambuc   void ExpandTypeToArgs(QualType Ty, RValue RV, llvm::FunctionType *IRFuncTy,
2699*0a6a1f1dSLionel Sambuc                         SmallVectorImpl<llvm::Value *> &IRCallArgs,
2700*0a6a1f1dSLionel Sambuc                         unsigned &IRCallArgPos);
2701f4a2713aSLionel Sambuc 
2702f4a2713aSLionel Sambuc   llvm::Value* EmitAsmInput(const TargetInfo::ConstraintInfo &Info,
2703f4a2713aSLionel Sambuc                             const Expr *InputExpr, std::string &ConstraintStr);
2704f4a2713aSLionel Sambuc 
2705f4a2713aSLionel Sambuc   llvm::Value* EmitAsmInputLValue(const TargetInfo::ConstraintInfo &Info,
2706f4a2713aSLionel Sambuc                                   LValue InputValue, QualType InputType,
2707f4a2713aSLionel Sambuc                                   std::string &ConstraintStr,
2708f4a2713aSLionel Sambuc                                   SourceLocation Loc);
2709f4a2713aSLionel Sambuc 
2710*0a6a1f1dSLionel Sambuc public:
2711f4a2713aSLionel Sambuc   /// EmitCallArgs - Emit call arguments for a function.
2712f4a2713aSLionel Sambuc   template <typename T>
2713f4a2713aSLionel Sambuc   void EmitCallArgs(CallArgList &Args, const T *CallArgTypeInfo,
2714f4a2713aSLionel Sambuc                     CallExpr::const_arg_iterator ArgBeg,
2715f4a2713aSLionel Sambuc                     CallExpr::const_arg_iterator ArgEnd,
2716*0a6a1f1dSLionel Sambuc                     const FunctionDecl *CalleeDecl = nullptr,
2717*0a6a1f1dSLionel Sambuc                     unsigned ParamsToSkip = 0, bool ForceColumnInfo = false) {
2718*0a6a1f1dSLionel Sambuc     SmallVector<QualType, 16> ArgTypes;
2719f4a2713aSLionel Sambuc     CallExpr::const_arg_iterator Arg = ArgBeg;
2720f4a2713aSLionel Sambuc 
2721*0a6a1f1dSLionel Sambuc     assert((ParamsToSkip == 0 || CallArgTypeInfo) &&
2722*0a6a1f1dSLionel Sambuc            "Can't skip parameters if type info is not provided");
2723f4a2713aSLionel Sambuc     if (CallArgTypeInfo) {
2724*0a6a1f1dSLionel Sambuc       // First, use the argument types that the type info knows about
2725*0a6a1f1dSLionel Sambuc       for (auto I = CallArgTypeInfo->param_type_begin() + ParamsToSkip,
2726*0a6a1f1dSLionel Sambuc                 E = CallArgTypeInfo->param_type_end();
2727*0a6a1f1dSLionel Sambuc            I != E; ++I, ++Arg) {
2728f4a2713aSLionel Sambuc         assert(Arg != ArgEnd && "Running over edge of argument list!");
2729*0a6a1f1dSLionel Sambuc         assert(
2730*0a6a1f1dSLionel Sambuc             ((*I)->isVariablyModifiedType() ||
2731*0a6a1f1dSLionel Sambuc              getContext()
2732*0a6a1f1dSLionel Sambuc                      .getCanonicalType((*I).getNonReferenceType())
2733*0a6a1f1dSLionel Sambuc                      .getTypePtr() ==
2734*0a6a1f1dSLionel Sambuc                  getContext().getCanonicalType(Arg->getType()).getTypePtr()) &&
2735f4a2713aSLionel Sambuc             "type mismatch in call argument!");
2736*0a6a1f1dSLionel Sambuc         ArgTypes.push_back(*I);
2737*0a6a1f1dSLionel Sambuc       }
2738f4a2713aSLionel Sambuc     }
2739f4a2713aSLionel Sambuc 
2740*0a6a1f1dSLionel Sambuc     // Either we've emitted all the call args, or we have a call to variadic
2741*0a6a1f1dSLionel Sambuc     // function.
2742*0a6a1f1dSLionel Sambuc     assert(
2743*0a6a1f1dSLionel Sambuc         (Arg == ArgEnd || !CallArgTypeInfo || CallArgTypeInfo->isVariadic()) &&
2744f4a2713aSLionel Sambuc         "Extra arguments in non-variadic function!");
2745f4a2713aSLionel Sambuc 
2746f4a2713aSLionel Sambuc     // If we still have any arguments, emit them using the type of the argument.
2747*0a6a1f1dSLionel Sambuc     for (; Arg != ArgEnd; ++Arg)
2748*0a6a1f1dSLionel Sambuc       ArgTypes.push_back(getVarArgType(*Arg));
2749f4a2713aSLionel Sambuc 
2750*0a6a1f1dSLionel Sambuc     EmitCallArgs(Args, ArgTypes, ArgBeg, ArgEnd, CalleeDecl, ParamsToSkip,
2751*0a6a1f1dSLionel Sambuc                  ForceColumnInfo);
2752f4a2713aSLionel Sambuc   }
2753*0a6a1f1dSLionel Sambuc 
2754*0a6a1f1dSLionel Sambuc   void EmitCallArgs(CallArgList &Args, ArrayRef<QualType> ArgTypes,
2755*0a6a1f1dSLionel Sambuc                     CallExpr::const_arg_iterator ArgBeg,
2756*0a6a1f1dSLionel Sambuc                     CallExpr::const_arg_iterator ArgEnd,
2757*0a6a1f1dSLionel Sambuc                     const FunctionDecl *CalleeDecl = nullptr,
2758*0a6a1f1dSLionel Sambuc                     unsigned ParamsToSkip = 0, bool ForceColumnInfo = false);
2759*0a6a1f1dSLionel Sambuc 
2760*0a6a1f1dSLionel Sambuc private:
2761*0a6a1f1dSLionel Sambuc   QualType getVarArgType(const Expr *Arg);
2762f4a2713aSLionel Sambuc 
getTargetHooks()2763f4a2713aSLionel Sambuc   const TargetCodeGenInfo &getTargetHooks() const {
2764f4a2713aSLionel Sambuc     return CGM.getTargetCodeGenInfo();
2765f4a2713aSLionel Sambuc   }
2766f4a2713aSLionel Sambuc 
2767f4a2713aSLionel Sambuc   void EmitDeclMetadata();
2768f4a2713aSLionel Sambuc 
2769f4a2713aSLionel Sambuc   CodeGenModule::ByrefHelpers *
2770f4a2713aSLionel Sambuc   buildByrefHelpers(llvm::StructType &byrefType,
2771f4a2713aSLionel Sambuc                     const AutoVarEmission &emission);
2772f4a2713aSLionel Sambuc 
2773f4a2713aSLionel Sambuc   void AddObjCARCExceptionMetadata(llvm::Instruction *Inst);
2774f4a2713aSLionel Sambuc 
2775f4a2713aSLionel Sambuc   /// GetPointeeAlignment - Given an expression with a pointer type, emit the
2776f4a2713aSLionel Sambuc   /// value and compute our best estimate of the alignment of the pointee.
2777f4a2713aSLionel Sambuc   std::pair<llvm::Value*, unsigned> EmitPointerWithAlignment(const Expr *Addr);
2778*0a6a1f1dSLionel Sambuc 
2779*0a6a1f1dSLionel Sambuc   llvm::Value *GetValueForARMHint(unsigned BuiltinID);
2780f4a2713aSLionel Sambuc };
2781f4a2713aSLionel Sambuc 
2782f4a2713aSLionel Sambuc /// Helper class with most of the code for saving a value for a
2783f4a2713aSLionel Sambuc /// conditional expression cleanup.
2784f4a2713aSLionel Sambuc struct DominatingLLVMValue {
2785f4a2713aSLionel Sambuc   typedef llvm::PointerIntPair<llvm::Value*, 1, bool> saved_type;
2786f4a2713aSLionel Sambuc 
2787f4a2713aSLionel Sambuc   /// Answer whether the given value needs extra work to be saved.
needsSavingDominatingLLVMValue2788f4a2713aSLionel Sambuc   static bool needsSaving(llvm::Value *value) {
2789f4a2713aSLionel Sambuc     // If it's not an instruction, we don't need to save.
2790f4a2713aSLionel Sambuc     if (!isa<llvm::Instruction>(value)) return false;
2791f4a2713aSLionel Sambuc 
2792f4a2713aSLionel Sambuc     // If it's an instruction in the entry block, we don't need to save.
2793f4a2713aSLionel Sambuc     llvm::BasicBlock *block = cast<llvm::Instruction>(value)->getParent();
2794f4a2713aSLionel Sambuc     return (block != &block->getParent()->getEntryBlock());
2795f4a2713aSLionel Sambuc   }
2796f4a2713aSLionel Sambuc 
2797f4a2713aSLionel Sambuc   /// Try to save the given value.
saveDominatingLLVMValue2798f4a2713aSLionel Sambuc   static saved_type save(CodeGenFunction &CGF, llvm::Value *value) {
2799f4a2713aSLionel Sambuc     if (!needsSaving(value)) return saved_type(value, false);
2800f4a2713aSLionel Sambuc 
2801f4a2713aSLionel Sambuc     // Otherwise we need an alloca.
2802f4a2713aSLionel Sambuc     llvm::Value *alloca =
2803f4a2713aSLionel Sambuc       CGF.CreateTempAlloca(value->getType(), "cond-cleanup.save");
2804f4a2713aSLionel Sambuc     CGF.Builder.CreateStore(value, alloca);
2805f4a2713aSLionel Sambuc 
2806f4a2713aSLionel Sambuc     return saved_type(alloca, true);
2807f4a2713aSLionel Sambuc   }
2808f4a2713aSLionel Sambuc 
restoreDominatingLLVMValue2809f4a2713aSLionel Sambuc   static llvm::Value *restore(CodeGenFunction &CGF, saved_type value) {
2810f4a2713aSLionel Sambuc     if (!value.getInt()) return value.getPointer();
2811f4a2713aSLionel Sambuc     return CGF.Builder.CreateLoad(value.getPointer());
2812f4a2713aSLionel Sambuc   }
2813f4a2713aSLionel Sambuc };
2814f4a2713aSLionel Sambuc 
2815f4a2713aSLionel Sambuc /// A partial specialization of DominatingValue for llvm::Values that
2816f4a2713aSLionel Sambuc /// might be llvm::Instructions.
2817f4a2713aSLionel Sambuc template <class T> struct DominatingPointer<T,true> : DominatingLLVMValue {
2818f4a2713aSLionel Sambuc   typedef T *type;
2819f4a2713aSLionel Sambuc   static type restore(CodeGenFunction &CGF, saved_type value) {
2820f4a2713aSLionel Sambuc     return static_cast<T*>(DominatingLLVMValue::restore(CGF, value));
2821f4a2713aSLionel Sambuc   }
2822f4a2713aSLionel Sambuc };
2823f4a2713aSLionel Sambuc 
2824f4a2713aSLionel Sambuc /// A specialization of DominatingValue for RValue.
2825f4a2713aSLionel Sambuc template <> struct DominatingValue<RValue> {
2826f4a2713aSLionel Sambuc   typedef RValue type;
2827f4a2713aSLionel Sambuc   class saved_type {
2828f4a2713aSLionel Sambuc     enum Kind { ScalarLiteral, ScalarAddress, AggregateLiteral,
2829f4a2713aSLionel Sambuc                 AggregateAddress, ComplexAddress };
2830f4a2713aSLionel Sambuc 
2831f4a2713aSLionel Sambuc     llvm::Value *Value;
2832f4a2713aSLionel Sambuc     Kind K;
2833f4a2713aSLionel Sambuc     saved_type(llvm::Value *v, Kind k) : Value(v), K(k) {}
2834f4a2713aSLionel Sambuc 
2835f4a2713aSLionel Sambuc   public:
2836f4a2713aSLionel Sambuc     static bool needsSaving(RValue value);
2837f4a2713aSLionel Sambuc     static saved_type save(CodeGenFunction &CGF, RValue value);
2838f4a2713aSLionel Sambuc     RValue restore(CodeGenFunction &CGF);
2839f4a2713aSLionel Sambuc 
2840f4a2713aSLionel Sambuc     // implementations in CGExprCXX.cpp
2841f4a2713aSLionel Sambuc   };
2842f4a2713aSLionel Sambuc 
2843f4a2713aSLionel Sambuc   static bool needsSaving(type value) {
2844f4a2713aSLionel Sambuc     return saved_type::needsSaving(value);
2845f4a2713aSLionel Sambuc   }
2846f4a2713aSLionel Sambuc   static saved_type save(CodeGenFunction &CGF, type value) {
2847f4a2713aSLionel Sambuc     return saved_type::save(CGF, value);
2848f4a2713aSLionel Sambuc   }
2849f4a2713aSLionel Sambuc   static type restore(CodeGenFunction &CGF, saved_type value) {
2850f4a2713aSLionel Sambuc     return value.restore(CGF);
2851f4a2713aSLionel Sambuc   }
2852f4a2713aSLionel Sambuc };
2853f4a2713aSLionel Sambuc 
2854f4a2713aSLionel Sambuc }  // end namespace CodeGen
2855f4a2713aSLionel Sambuc }  // end namespace clang
2856f4a2713aSLionel Sambuc 
2857f4a2713aSLionel Sambuc #endif
2858