CodeGenFunction.h revision 206084
1193326Sed//===-- CodeGenFunction.h - Per-Function state for LLVM CodeGen -*- C++ -*-===//
2193326Sed//
3193326Sed//                     The LLVM Compiler Infrastructure
4193326Sed//
5193326Sed// This file is distributed under the University of Illinois Open Source
6193326Sed// License. See LICENSE.TXT for details.
7193326Sed//
8193326Sed//===----------------------------------------------------------------------===//
9193326Sed//
10193326Sed// This is the internal per-function state used for llvm translation.
11193326Sed//
12193326Sed//===----------------------------------------------------------------------===//
13193326Sed
14193326Sed#ifndef CLANG_CODEGEN_CODEGENFUNCTION_H
15193326Sed#define CLANG_CODEGEN_CODEGENFUNCTION_H
16193326Sed
17193326Sed#include "clang/AST/Type.h"
18193326Sed#include "clang/AST/ExprCXX.h"
19193326Sed#include "clang/AST/ExprObjC.h"
20202379Srdivacky#include "clang/AST/CharUnits.h"
21193326Sed#include "clang/Basic/TargetInfo.h"
22193326Sed#include "llvm/ADT/DenseMap.h"
23193326Sed#include "llvm/ADT/SmallVector.h"
24193326Sed#include "llvm/Support/ValueHandle.h"
25198092Srdivacky#include "CodeGenModule.h"
26193326Sed#include "CGBlocks.h"
27193326Sed#include "CGBuilder.h"
28193326Sed#include "CGCall.h"
29193326Sed#include "CGCXX.h"
30193326Sed#include "CGValue.h"
31193326Sed
32193326Sednamespace llvm {
33193326Sed  class BasicBlock;
34198092Srdivacky  class LLVMContext;
35193326Sed  class Module;
36193326Sed  class SwitchInst;
37198398Srdivacky  class Twine;
38193326Sed  class Value;
39193326Sed}
40193326Sed
41193326Sednamespace clang {
42193326Sed  class ASTContext;
43193326Sed  class CXXDestructorDecl;
44198092Srdivacky  class CXXTryStmt;
45193326Sed  class Decl;
46193326Sed  class EnumConstantDecl;
47193326Sed  class FunctionDecl;
48193326Sed  class FunctionProtoType;
49193326Sed  class LabelStmt;
50193326Sed  class ObjCContainerDecl;
51193326Sed  class ObjCInterfaceDecl;
52193326Sed  class ObjCIvarDecl;
53193326Sed  class ObjCMethodDecl;
54193326Sed  class ObjCImplementationDecl;
55193326Sed  class ObjCPropertyImplDecl;
56193326Sed  class TargetInfo;
57204643Srdivacky  class TargetCodeGenInfo;
58193326Sed  class VarDecl;
59193326Sed  class ObjCForCollectionStmt;
60193326Sed  class ObjCAtTryStmt;
61193326Sed  class ObjCAtThrowStmt;
62193326Sed  class ObjCAtSynchronizedStmt;
63193326Sed
64193326Sednamespace CodeGen {
65193326Sed  class CodeGenTypes;
66193326Sed  class CGDebugInfo;
67193326Sed  class CGFunctionInfo;
68193326Sed  class CGRecordLayout;
69193326Sed
70193326Sed/// CodeGenFunction - This class organizes the per-function state that is used
71193326Sed/// while generating LLVM code.
72193326Sedclass CodeGenFunction : public BlockFunction {
73193326Sed  CodeGenFunction(const CodeGenFunction&); // DO NOT IMPLEMENT
74193326Sed  void operator=(const CodeGenFunction&);  // DO NOT IMPLEMENT
75193326Sedpublic:
76193326Sed  CodeGenModule &CGM;  // Per-module state.
77199482Srdivacky  const TargetInfo &Target;
78193326Sed
79193326Sed  typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
80193326Sed  CGBuilderTy Builder;
81193326Sed
82193326Sed  /// CurFuncDecl - Holds the Decl for the current function or ObjC method.
83193326Sed  /// This excludes BlockDecls.
84193326Sed  const Decl *CurFuncDecl;
85193326Sed  /// CurCodeDecl - This is the inner-most code context, which includes blocks.
86193326Sed  const Decl *CurCodeDecl;
87193326Sed  const CGFunctionInfo *CurFnInfo;
88193326Sed  QualType FnRetTy;
89193326Sed  llvm::Function *CurFn;
90193326Sed
91200583Srdivacky  /// CurGD - The GlobalDecl for the current function being compiled.
92200583Srdivacky  GlobalDecl CurGD;
93200583Srdivacky
94193326Sed  /// ReturnBlock - Unified return block.
95193326Sed  llvm::BasicBlock *ReturnBlock;
96193326Sed  /// ReturnValue - The temporary alloca to hold the return value. This is null
97193326Sed  /// iff the function has no return value.
98200583Srdivacky  llvm::Value *ReturnValue;
99193326Sed
100193326Sed  /// AllocaInsertPoint - This is an instruction in the entry block before which
101193326Sed  /// we prefer to insert allocas.
102193326Sed  llvm::AssertingVH<llvm::Instruction> AllocaInsertPt;
103193326Sed
104193326Sed  const llvm::Type *LLVMIntTy;
105193326Sed  uint32_t LLVMPointerWidth;
106193326Sed
107200583Srdivacky  bool Exceptions;
108200583Srdivacky  bool CatchUndefined;
109193326Sedpublic:
110193326Sed  /// ObjCEHValueStack - Stack of Objective-C exception values, used for
111193326Sed  /// rethrows.
112193326Sed  llvm::SmallVector<llvm::Value*, 8> ObjCEHValueStack;
113193326Sed
114193326Sed  /// PushCleanupBlock - Push a new cleanup entry on the stack and set the
115193326Sed  /// passed in block as the cleanup block.
116198954Srdivacky  void PushCleanupBlock(llvm::BasicBlock *CleanupEntryBlock,
117200583Srdivacky                        llvm::BasicBlock *CleanupExitBlock,
118200583Srdivacky                        llvm::BasicBlock *PreviousInvokeDest,
119200583Srdivacky                        bool EHOnly = false);
120200583Srdivacky  void PushCleanupBlock(llvm::BasicBlock *CleanupEntryBlock) {
121200583Srdivacky    PushCleanupBlock(CleanupEntryBlock, 0, getInvokeDest(), false);
122200583Srdivacky  }
123193326Sed
124193326Sed  /// CleanupBlockInfo - A struct representing a popped cleanup block.
125193326Sed  struct CleanupBlockInfo {
126198954Srdivacky    /// CleanupEntryBlock - the cleanup entry block
127193326Sed    llvm::BasicBlock *CleanupBlock;
128193326Sed
129193326Sed    /// SwitchBlock - the block (if any) containing the switch instruction used
130193326Sed    /// for jumping to the final destination.
131193326Sed    llvm::BasicBlock *SwitchBlock;
132193326Sed
133193326Sed    /// EndBlock - the default destination for the switch instruction.
134193326Sed    llvm::BasicBlock *EndBlock;
135193326Sed
136200583Srdivacky    /// EHOnly - True iff this cleanup should only be performed on the
137200583Srdivacky    /// exceptional edge.
138200583Srdivacky    bool EHOnly;
139200583Srdivacky
140193326Sed    CleanupBlockInfo(llvm::BasicBlock *cb, llvm::BasicBlock *sb,
141200583Srdivacky                     llvm::BasicBlock *eb, bool ehonly = false)
142200583Srdivacky      : CleanupBlock(cb), SwitchBlock(sb), EndBlock(eb), EHOnly(ehonly) {}
143193326Sed  };
144193326Sed
145200583Srdivacky  /// EHCleanupBlock - RAII object that will create a cleanup block for the
146200583Srdivacky  /// exceptional edge and set the insert point to that block.  When destroyed,
147200583Srdivacky  /// it creates the cleanup edge and sets the insert point to the previous
148200583Srdivacky  /// block.
149200583Srdivacky  class EHCleanupBlock {
150200583Srdivacky    CodeGenFunction& CGF;
151200583Srdivacky    llvm::BasicBlock *Cont;
152200583Srdivacky    llvm::BasicBlock *CleanupHandler;
153200583Srdivacky    llvm::BasicBlock *CleanupEntryBB;
154200583Srdivacky    llvm::BasicBlock *PreviousInvokeDest;
155200583Srdivacky  public:
156200583Srdivacky    EHCleanupBlock(CodeGenFunction &cgf)
157200583Srdivacky      : CGF(cgf), Cont(CGF.createBasicBlock("cont")),
158200583Srdivacky        CleanupHandler(CGF.createBasicBlock("ehcleanup")),
159200583Srdivacky        CleanupEntryBB(CGF.createBasicBlock("ehcleanup.rest")),
160200583Srdivacky        PreviousInvokeDest(CGF.getInvokeDest()) {
161200583Srdivacky      CGF.EmitBranch(Cont);
162200583Srdivacky      llvm::BasicBlock *TerminateHandler = CGF.getTerminateHandler();
163200583Srdivacky      CGF.Builder.SetInsertPoint(CleanupEntryBB);
164200583Srdivacky      CGF.setInvokeDest(TerminateHandler);
165200583Srdivacky    }
166200583Srdivacky    ~EHCleanupBlock();
167200583Srdivacky  };
168200583Srdivacky
169193326Sed  /// PopCleanupBlock - Will pop the cleanup entry on the stack, process all
170193326Sed  /// branch fixups and return a block info struct with the switch block and end
171200583Srdivacky  /// block.  This will also reset the invoke handler to the previous value
172200583Srdivacky  /// from when the cleanup block was created.
173193326Sed  CleanupBlockInfo PopCleanupBlock();
174193326Sed
175199990Srdivacky  /// DelayedCleanupBlock - RAII object that will create a cleanup block and set
176199990Srdivacky  /// the insert point to that block. When destructed, it sets the insert point
177199990Srdivacky  /// to the previous block and pushes a new cleanup entry on the stack.
178199990Srdivacky  class DelayedCleanupBlock {
179193326Sed    CodeGenFunction& CGF;
180193326Sed    llvm::BasicBlock *CurBB;
181198954Srdivacky    llvm::BasicBlock *CleanupEntryBB;
182198954Srdivacky    llvm::BasicBlock *CleanupExitBB;
183200583Srdivacky    llvm::BasicBlock *CurInvokeDest;
184200583Srdivacky    bool EHOnly;
185198954Srdivacky
186193326Sed  public:
187200583Srdivacky    DelayedCleanupBlock(CodeGenFunction &cgf, bool ehonly = false)
188198954Srdivacky      : CGF(cgf), CurBB(CGF.Builder.GetInsertBlock()),
189200583Srdivacky        CleanupEntryBB(CGF.createBasicBlock("cleanup")), CleanupExitBB(0),
190200583Srdivacky        CurInvokeDest(CGF.getInvokeDest()),
191200583Srdivacky        EHOnly(ehonly) {
192198954Srdivacky      CGF.Builder.SetInsertPoint(CleanupEntryBB);
193193326Sed    }
194193326Sed
195198954Srdivacky    llvm::BasicBlock *getCleanupExitBlock() {
196198954Srdivacky      if (!CleanupExitBB)
197198954Srdivacky        CleanupExitBB = CGF.createBasicBlock("cleanup.exit");
198198954Srdivacky      return CleanupExitBB;
199198954Srdivacky    }
200198954Srdivacky
201199990Srdivacky    ~DelayedCleanupBlock() {
202200583Srdivacky      CGF.PushCleanupBlock(CleanupEntryBB, CleanupExitBB, CurInvokeDest,
203200583Srdivacky                           EHOnly);
204198092Srdivacky      // FIXME: This is silly, move this into the builder.
205198092Srdivacky      if (CurBB)
206198092Srdivacky        CGF.Builder.SetInsertPoint(CurBB);
207198092Srdivacky      else
208198092Srdivacky        CGF.Builder.ClearInsertionPoint();
209193326Sed    }
210193326Sed  };
211193326Sed
212199990Srdivacky  /// \brief Enters a new scope for capturing cleanups, all of which will be
213199990Srdivacky  /// executed once the scope is exited.
214199990Srdivacky  class CleanupScope {
215199990Srdivacky    CodeGenFunction& CGF;
216199990Srdivacky    size_t CleanupStackDepth;
217199990Srdivacky    bool OldDidCallStackSave;
218199990Srdivacky    bool PerformCleanup;
219199990Srdivacky
220199990Srdivacky    CleanupScope(const CleanupScope &); // DO NOT IMPLEMENT
221199990Srdivacky    CleanupScope &operator=(const CleanupScope &); // DO NOT IMPLEMENT
222199990Srdivacky
223199990Srdivacky  public:
224199990Srdivacky    /// \brief Enter a new cleanup scope.
225199990Srdivacky    explicit CleanupScope(CodeGenFunction &CGF)
226199990Srdivacky      : CGF(CGF), PerformCleanup(true)
227199990Srdivacky    {
228199990Srdivacky      CleanupStackDepth = CGF.CleanupEntries.size();
229199990Srdivacky      OldDidCallStackSave = CGF.DidCallStackSave;
230199990Srdivacky    }
231199990Srdivacky
232199990Srdivacky    /// \brief Exit this cleanup scope, emitting any accumulated
233199990Srdivacky    /// cleanups.
234199990Srdivacky    ~CleanupScope() {
235199990Srdivacky      if (PerformCleanup) {
236199990Srdivacky        CGF.DidCallStackSave = OldDidCallStackSave;
237199990Srdivacky        CGF.EmitCleanupBlocks(CleanupStackDepth);
238199990Srdivacky      }
239199990Srdivacky    }
240199990Srdivacky
241199990Srdivacky    /// \brief Determine whether this scope requires any cleanups.
242199990Srdivacky    bool requiresCleanups() const {
243199990Srdivacky      return CGF.CleanupEntries.size() > CleanupStackDepth;
244199990Srdivacky    }
245199990Srdivacky
246199990Srdivacky    /// \brief Force the emission of cleanups now, instead of waiting
247199990Srdivacky    /// until this object is destroyed.
248199990Srdivacky    void ForceCleanup() {
249199990Srdivacky      assert(PerformCleanup && "Already forced cleanup");
250199990Srdivacky      CGF.DidCallStackSave = OldDidCallStackSave;
251199990Srdivacky      CGF.EmitCleanupBlocks(CleanupStackDepth);
252199990Srdivacky      PerformCleanup = false;
253199990Srdivacky    }
254199990Srdivacky  };
255199990Srdivacky
256206084Srdivacky  /// CXXTemporariesCleanupScope - Enters a new scope for catching live
257206084Srdivacky  /// temporaries, all of which will be popped once the scope is exited.
258206084Srdivacky  class CXXTemporariesCleanupScope {
259206084Srdivacky    CodeGenFunction &CGF;
260206084Srdivacky    size_t NumLiveTemporaries;
261206084Srdivacky
262206084Srdivacky    // DO NOT IMPLEMENT
263206084Srdivacky    CXXTemporariesCleanupScope(const CXXTemporariesCleanupScope &);
264206084Srdivacky    CXXTemporariesCleanupScope &operator=(const CXXTemporariesCleanupScope &);
265206084Srdivacky
266206084Srdivacky  public:
267206084Srdivacky    explicit CXXTemporariesCleanupScope(CodeGenFunction &CGF)
268206084Srdivacky      : CGF(CGF), NumLiveTemporaries(CGF.LiveTemporaries.size()) { }
269206084Srdivacky
270206084Srdivacky    ~CXXTemporariesCleanupScope() {
271206084Srdivacky      while (CGF.LiveTemporaries.size() > NumLiveTemporaries)
272206084Srdivacky        CGF.PopCXXTemporary();
273206084Srdivacky    }
274206084Srdivacky  };
275206084Srdivacky
276206084Srdivacky
277193326Sed  /// EmitCleanupBlocks - Takes the old cleanup stack size and emits the cleanup
278193326Sed  /// blocks that have been added.
279193326Sed  void EmitCleanupBlocks(size_t OldCleanupStackSize);
280193326Sed
281193326Sed  /// EmitBranchThroughCleanup - Emit a branch from the current insert block
282193326Sed  /// through the cleanup handling code (if any) and then on to \arg Dest.
283193326Sed  ///
284193326Sed  /// FIXME: Maybe this should really be in EmitBranch? Don't we always want
285193326Sed  /// this behavior for branches?
286193326Sed  void EmitBranchThroughCleanup(llvm::BasicBlock *Dest);
287193326Sed
288203955Srdivacky  /// BeginConditionalBranch - Should be called before a conditional part of an
289199990Srdivacky  /// expression is emitted. For example, before the RHS of the expression below
290199990Srdivacky  /// is emitted:
291198092Srdivacky  ///
292193576Sed  /// b && f(T());
293193576Sed  ///
294199990Srdivacky  /// This is used to make sure that any temporaries created in the conditional
295193576Sed  /// branch are only destroyed if the branch is taken.
296203955Srdivacky  void BeginConditionalBranch() {
297199990Srdivacky    ++ConditionalBranchLevel;
298199990Srdivacky  }
299198092Srdivacky
300203955Srdivacky  /// EndConditionalBranch - Should be called after a conditional part of an
301199990Srdivacky  /// expression has been emitted.
302203955Srdivacky  void EndConditionalBranch() {
303203955Srdivacky    assert(ConditionalBranchLevel != 0 &&
304203955Srdivacky           "Conditional branch mismatch!");
305203955Srdivacky
306199990Srdivacky    --ConditionalBranchLevel;
307199990Srdivacky  }
308198092Srdivacky
309193326Sedprivate:
310198893Srdivacky  CGDebugInfo *DebugInfo;
311193326Sed
312199990Srdivacky  /// IndirectBranch - The first time an indirect goto is seen we create a block
313199990Srdivacky  /// with an indirect branch.  Every time we see the address of a label taken,
314199990Srdivacky  /// we add the label to the indirect goto.  Every subsequent indirect goto is
315199990Srdivacky  /// codegen'd as a jump to the IndirectBranch's basic block.
316198893Srdivacky  llvm::IndirectBrInst *IndirectBranch;
317193326Sed
318193326Sed  /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
319193326Sed  /// decls.
320193326Sed  llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
321193326Sed
322193326Sed  /// LabelMap - This keeps track of the LLVM basic block for each C label.
323193326Sed  llvm::DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
324193326Sed
325193326Sed  // BreakContinueStack - This keeps track of where break and continue
326193326Sed  // statements should jump to.
327193326Sed  struct BreakContinue {
328193326Sed    BreakContinue(llvm::BasicBlock *bb, llvm::BasicBlock *cb)
329193326Sed      : BreakBlock(bb), ContinueBlock(cb) {}
330193326Sed
331193326Sed    llvm::BasicBlock *BreakBlock;
332193326Sed    llvm::BasicBlock *ContinueBlock;
333193326Sed  };
334193326Sed  llvm::SmallVector<BreakContinue, 8> BreakContinueStack;
335193326Sed
336193326Sed  /// SwitchInsn - This is nearest current switch instruction. It is null if if
337193326Sed  /// current context is not in a switch.
338193326Sed  llvm::SwitchInst *SwitchInsn;
339193326Sed
340193326Sed  /// CaseRangeBlock - This block holds if condition check for last case
341193326Sed  /// statement range in current switch instruction.
342193326Sed  llvm::BasicBlock *CaseRangeBlock;
343193326Sed
344193326Sed  /// InvokeDest - This is the nearest exception target for calls
345193326Sed  /// which can unwind, when exceptions are being used.
346193326Sed  llvm::BasicBlock *InvokeDest;
347193326Sed
348193326Sed  // VLASizeMap - This keeps track of the associated size for each VLA type.
349198092Srdivacky  // We track this by the size expression rather than the type itself because
350198092Srdivacky  // in certain situations, like a const qualifier applied to an VLA typedef,
351198092Srdivacky  // multiple VLA types can share the same size expression.
352193326Sed  // FIXME: Maybe this could be a stack of maps that is pushed/popped as we
353193326Sed  // enter/leave scopes.
354198092Srdivacky  llvm::DenseMap<const Expr*, llvm::Value*> VLASizeMap;
355193326Sed
356193326Sed  /// DidCallStackSave - Whether llvm.stacksave has been called. Used to avoid
357193326Sed  /// calling llvm.stacksave for multiple VLAs in the same scope.
358193326Sed  bool DidCallStackSave;
359193326Sed
360193326Sed  struct CleanupEntry {
361198954Srdivacky    /// CleanupEntryBlock - The block of code that does the actual cleanup.
362198954Srdivacky    llvm::BasicBlock *CleanupEntryBlock;
363193326Sed
364198954Srdivacky    /// CleanupExitBlock - The cleanup exit block.
365198954Srdivacky    llvm::BasicBlock *CleanupExitBlock;
366198954Srdivacky
367193326Sed    /// Blocks - Basic blocks that were emitted in the current cleanup scope.
368193326Sed    std::vector<llvm::BasicBlock *> Blocks;
369193326Sed
370193326Sed    /// BranchFixups - Branch instructions to basic blocks that haven't been
371193326Sed    /// inserted into the current function yet.
372193326Sed    std::vector<llvm::BranchInst *> BranchFixups;
373193326Sed
374200583Srdivacky    /// PreviousInvokeDest - The invoke handler from the start of the cleanup
375200583Srdivacky    /// region.
376200583Srdivacky    llvm::BasicBlock *PreviousInvokeDest;
377200583Srdivacky
378200583Srdivacky    /// EHOnly - Perform this only on the exceptional edge, not the main edge.
379200583Srdivacky    bool EHOnly;
380200583Srdivacky
381198954Srdivacky    explicit CleanupEntry(llvm::BasicBlock *CleanupEntryBlock,
382200583Srdivacky                          llvm::BasicBlock *CleanupExitBlock,
383200583Srdivacky                          llvm::BasicBlock *PreviousInvokeDest,
384200583Srdivacky                          bool ehonly)
385200583Srdivacky      : CleanupEntryBlock(CleanupEntryBlock),
386200583Srdivacky        CleanupExitBlock(CleanupExitBlock),
387200583Srdivacky        PreviousInvokeDest(PreviousInvokeDest),
388200583Srdivacky        EHOnly(ehonly) {}
389193326Sed  };
390193326Sed
391193326Sed  /// CleanupEntries - Stack of cleanup entries.
392193326Sed  llvm::SmallVector<CleanupEntry, 8> CleanupEntries;
393193326Sed
394193326Sed  typedef llvm::DenseMap<llvm::BasicBlock*, size_t> BlockScopeMap;
395193326Sed
396193326Sed  /// BlockScopes - Map of which "cleanup scope" scope basic blocks have.
397193326Sed  BlockScopeMap BlockScopes;
398193326Sed
399199990Srdivacky  /// CXXThisDecl - When generating code for a C++ member function,
400199990Srdivacky  /// this will hold the implicit 'this' declaration.
401193326Sed  ImplicitParamDecl *CXXThisDecl;
402204643Srdivacky  llvm::Value *CXXThisValue;
403198092Srdivacky
404199990Srdivacky  /// CXXVTTDecl - When generating code for a base object constructor or
405199990Srdivacky  /// base object destructor with virtual bases, this will hold the implicit
406199990Srdivacky  /// VTT parameter.
407199990Srdivacky  ImplicitParamDecl *CXXVTTDecl;
408204643Srdivacky  llvm::Value *CXXVTTValue;
409199990Srdivacky
410193401Sed  /// CXXLiveTemporaryInfo - Holds information about a live C++ temporary.
411193401Sed  struct CXXLiveTemporaryInfo {
412193401Sed    /// Temporary - The live temporary.
413193401Sed    const CXXTemporary *Temporary;
414198092Srdivacky
415193401Sed    /// ThisPtr - The pointer to the temporary.
416193401Sed    llvm::Value *ThisPtr;
417198092Srdivacky
418193401Sed    /// DtorBlock - The destructor block.
419193401Sed    llvm::BasicBlock *DtorBlock;
420198092Srdivacky
421199990Srdivacky    /// CondPtr - If this is a conditional temporary, this is the pointer to the
422199990Srdivacky    /// condition variable that states whether the destructor should be called
423199990Srdivacky    /// or not.
424193401Sed    llvm::Value *CondPtr;
425198092Srdivacky
426193401Sed    CXXLiveTemporaryInfo(const CXXTemporary *temporary,
427193401Sed                         llvm::Value *thisptr, llvm::BasicBlock *dtorblock,
428193401Sed                         llvm::Value *condptr)
429198092Srdivacky      : Temporary(temporary), ThisPtr(thisptr), DtorBlock(dtorblock),
430193401Sed      CondPtr(condptr) { }
431193401Sed  };
432198092Srdivacky
433193401Sed  llvm::SmallVector<CXXLiveTemporaryInfo, 4> LiveTemporaries;
434193576Sed
435199990Srdivacky  /// ConditionalBranchLevel - Contains the nesting level of the current
436199990Srdivacky  /// conditional branch. This is used so that we know if a temporary should be
437199990Srdivacky  /// destroyed conditionally.
438199990Srdivacky  unsigned ConditionalBranchLevel;
439198092Srdivacky
440198092Srdivacky
441198092Srdivacky  /// ByrefValueInfoMap - For each __block variable, contains a pair of the LLVM
442198092Srdivacky  /// type as well as the field number that contains the actual data.
443198092Srdivacky  llvm::DenseMap<const ValueDecl *, std::pair<const llvm::Type *,
444198092Srdivacky                                              unsigned> > ByRefValueInfo;
445193401Sed
446198092Srdivacky  /// getByrefValueFieldNumber - Given a declaration, returns the LLVM field
447198092Srdivacky  /// number that holds the value.
448198092Srdivacky  unsigned getByRefValueLLVMField(const ValueDecl *VD) const;
449200583Srdivacky
450200583Srdivacky  llvm::BasicBlock *TerminateHandler;
451200583Srdivacky  llvm::BasicBlock *TrapBB;
452200583Srdivacky
453200583Srdivacky  int UniqueAggrDestructorCount;
454193326Sedpublic:
455193326Sed  CodeGenFunction(CodeGenModule &cgm);
456193326Sed
457193326Sed  ASTContext &getContext() const;
458193326Sed  CGDebugInfo *getDebugInfo() { return DebugInfo; }
459193326Sed
460193326Sed  llvm::BasicBlock *getInvokeDest() { return InvokeDest; }
461193326Sed  void setInvokeDest(llvm::BasicBlock *B) { InvokeDest = B; }
462193326Sed
463198092Srdivacky  llvm::LLVMContext &getLLVMContext() { return VMContext; }
464198092Srdivacky
465193326Sed  //===--------------------------------------------------------------------===//
466193326Sed  //                                  Objective-C
467193326Sed  //===--------------------------------------------------------------------===//
468193326Sed
469193326Sed  void GenerateObjCMethod(const ObjCMethodDecl *OMD);
470193326Sed
471193326Sed  void StartObjCMethod(const ObjCMethodDecl *MD,
472193326Sed                       const ObjCContainerDecl *CD);
473193326Sed
474193326Sed  /// GenerateObjCGetter - Synthesize an Objective-C property getter function.
475193326Sed  void GenerateObjCGetter(ObjCImplementationDecl *IMP,
476193326Sed                          const ObjCPropertyImplDecl *PID);
477193326Sed
478193326Sed  /// GenerateObjCSetter - Synthesize an Objective-C property setter function
479193326Sed  /// for the given property.
480193326Sed  void GenerateObjCSetter(ObjCImplementationDecl *IMP,
481193326Sed                          const ObjCPropertyImplDecl *PID);
482193326Sed
483193326Sed  //===--------------------------------------------------------------------===//
484193326Sed  //                                  Block Bits
485193326Sed  //===--------------------------------------------------------------------===//
486193326Sed
487193326Sed  llvm::Value *BuildBlockLiteralTmp(const BlockExpr *);
488204643Srdivacky  llvm::Constant *BuildDescriptorBlockDecl(const BlockExpr *,
489204643Srdivacky                                           bool BlockHasCopyDispose,
490202379Srdivacky                                           CharUnits Size,
491193326Sed                                           const llvm::StructType *,
492193326Sed                                           std::vector<HelperInfo> *);
493193326Sed
494193326Sed  llvm::Function *GenerateBlockFunction(const BlockExpr *BExpr,
495193326Sed                                        const BlockInfo& Info,
496193326Sed                                        const Decl *OuterFuncDecl,
497193326Sed                                  llvm::DenseMap<const Decl*, llvm::Value*> ldm,
498203955Srdivacky                                        CharUnits &Size, CharUnits &Align,
499193326Sed                      llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
500193326Sed                                        bool &subBlockHasCopyDispose);
501193326Sed
502193326Sed  void BlockForwardSelf();
503193326Sed  llvm::Value *LoadBlockStruct();
504193326Sed
505202379Srdivacky  CharUnits AllocateBlockDecl(const BlockDeclRefExpr *E);
506193326Sed  llvm::Value *GetAddrOfBlockDecl(const BlockDeclRefExpr *E);
507198092Srdivacky  const llvm::Type *BuildByRefType(const ValueDecl *D);
508193326Sed
509198092Srdivacky  void GenerateCode(GlobalDecl GD, llvm::Function *Fn);
510198092Srdivacky  void StartFunction(GlobalDecl GD, QualType RetTy,
511193326Sed                     llvm::Function *Fn,
512193326Sed                     const FunctionArgList &Args,
513193326Sed                     SourceLocation StartLoc);
514193326Sed
515204643Srdivacky  void EmitConstructorBody(FunctionArgList &Args);
516204643Srdivacky  void EmitDestructorBody(FunctionArgList &Args);
517204643Srdivacky  void EmitFunctionBody(FunctionArgList &Args);
518204643Srdivacky
519193326Sed  /// EmitReturnBlock - Emit the unified return block, trying to avoid its
520193326Sed  /// emission when possible.
521193326Sed  void EmitReturnBlock();
522193326Sed
523193326Sed  /// FinishFunction - Complete IR generation of the current function. It is
524193326Sed  /// legal to call this function even if there is no current insertion point.
525193326Sed  void FinishFunction(SourceLocation EndLoc=SourceLocation());
526193326Sed
527206084Srdivacky  /// GenerateThunk - Generate a thunk for the given method.
528206084Srdivacky  void GenerateThunk(llvm::Function *Fn, GlobalDecl GD, const ThunkInfo &Thunk);
529206084Srdivacky
530206084Srdivacky  void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type);
531198893Srdivacky
532206084Srdivacky  /// InitializeVTablePointer - Initialize the vtable pointer of the given
533206084Srdivacky  /// subobject.
534206084Srdivacky  ///
535206084Srdivacky  /// \param BaseIsMorallyVirtual - Whether the base subobject is a virtual base
536206084Srdivacky  /// or a direct or indirect base of a virtual base.
537206084Srdivacky  void InitializeVTablePointer(BaseSubobject Base, bool BaseIsMorallyVirtual,
538206084Srdivacky                               llvm::Constant *VTable,
539206084Srdivacky                               const CXXRecordDecl *VTableClass);
540198092Srdivacky
541206084Srdivacky  typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
542206084Srdivacky  void InitializeVTablePointers(BaseSubobject Base, bool BaseIsMorallyVirtual,
543206084Srdivacky                                bool BaseIsNonVirtualPrimaryBase,
544206084Srdivacky                                llvm::Constant *VTable,
545206084Srdivacky                                const CXXRecordDecl *VTableClass,
546206084Srdivacky                                VisitedVirtualBasesSetTy& VBases);
547198092Srdivacky
548206084Srdivacky  void InitializeVTablePointers(const CXXRecordDecl *ClassDecl);
549200583Srdivacky
550201361Srdivacky
551204643Srdivacky  void SynthesizeCXXCopyConstructor(const FunctionArgList &Args);
552204643Srdivacky  void SynthesizeCXXCopyAssignment(const FunctionArgList &Args);
553198092Srdivacky
554198092Srdivacky  /// EmitDtorEpilogue - Emit all code that comes at the end of class's
555199990Srdivacky  /// destructor. This is to call destructors on members and base classes in
556199990Srdivacky  /// reverse order of their construction.
557198092Srdivacky  void EmitDtorEpilogue(const CXXDestructorDecl *Dtor,
558198092Srdivacky                        CXXDtorType Type);
559198092Srdivacky
560193326Sed  /// EmitFunctionProlog - Emit the target specific LLVM code to load the
561193326Sed  /// arguments for the given function. This is also responsible for naming the
562193326Sed  /// LLVM function arguments.
563193326Sed  void EmitFunctionProlog(const CGFunctionInfo &FI,
564193326Sed                          llvm::Function *Fn,
565193326Sed                          const FunctionArgList &Args);
566193326Sed
567193326Sed  /// EmitFunctionEpilog - Emit the target specific LLVM code to return the
568193326Sed  /// given temporary.
569193326Sed  void EmitFunctionEpilog(const CGFunctionInfo &FI, llvm::Value *ReturnValue);
570193326Sed
571200583Srdivacky  /// EmitStartEHSpec - Emit the start of the exception spec.
572200583Srdivacky  void EmitStartEHSpec(const Decl *D);
573200583Srdivacky
574200583Srdivacky  /// EmitEndEHSpec - Emit the end of the exception spec.
575200583Srdivacky  void EmitEndEHSpec(const Decl *D);
576200583Srdivacky
577200583Srdivacky  /// getTerminateHandler - Return a handler that just calls terminate.
578200583Srdivacky  llvm::BasicBlock *getTerminateHandler();
579200583Srdivacky
580193326Sed  const llvm::Type *ConvertTypeForMem(QualType T);
581193326Sed  const llvm::Type *ConvertType(QualType T);
582203955Srdivacky  const llvm::Type *ConvertType(const TypeDecl *T) {
583203955Srdivacky    return ConvertType(getContext().getTypeDeclType(T));
584203955Srdivacky  }
585193326Sed
586193326Sed  /// LoadObjCSelf - Load the value of self. This function is only valid while
587193326Sed  /// generating code for an Objective-C method.
588193326Sed  llvm::Value *LoadObjCSelf();
589193326Sed
590193326Sed  /// TypeOfSelfObject - Return type of object that this self represents.
591193326Sed  QualType TypeOfSelfObject();
592193326Sed
593193326Sed  /// hasAggregateLLVMType - Return true if the specified AST type will map into
594193326Sed  /// an aggregate LLVM type or is void.
595193326Sed  static bool hasAggregateLLVMType(QualType T);
596193326Sed
597193326Sed  /// createBasicBlock - Create an LLVM basic block.
598193326Sed  llvm::BasicBlock *createBasicBlock(const char *Name="",
599193326Sed                                     llvm::Function *Parent=0,
600193326Sed                                     llvm::BasicBlock *InsertBefore=0) {
601193326Sed#ifdef NDEBUG
602198092Srdivacky    return llvm::BasicBlock::Create(VMContext, "", Parent, InsertBefore);
603193326Sed#else
604198092Srdivacky    return llvm::BasicBlock::Create(VMContext, Name, Parent, InsertBefore);
605193326Sed#endif
606193326Sed  }
607193326Sed
608193326Sed  /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
609193326Sed  /// label maps to.
610193326Sed  llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
611193326Sed
612199990Srdivacky  /// SimplifyForwardingBlocks - If the given basic block is only a branch to
613199990Srdivacky  /// another basic block, simplify it. This assumes that no other code could
614199990Srdivacky  /// potentially reference the basic block.
615193326Sed  void SimplifyForwardingBlocks(llvm::BasicBlock *BB);
616193326Sed
617193326Sed  /// EmitBlock - Emit the given block \arg BB and set it as the insert point,
618193326Sed  /// adding a fall-through branch from the current insert block if
619193326Sed  /// necessary. It is legal to call this function even if there is no current
620193326Sed  /// insertion point.
621193326Sed  ///
622193326Sed  /// IsFinished - If true, indicates that the caller has finished emitting
623193326Sed  /// branches to the given block and does not expect to emit code into it. This
624193326Sed  /// means the block can be ignored if it is unreachable.
625193326Sed  void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false);
626193326Sed
627193326Sed  /// EmitBranch - Emit a branch to the specified basic block from the current
628193326Sed  /// insert block, taking care to avoid creation of branches from dummy
629193326Sed  /// blocks. It is legal to call this function even if there is no current
630193326Sed  /// insertion point.
631193326Sed  ///
632193326Sed  /// This function clears the current insertion point. The caller should follow
633193326Sed  /// calls to this function with calls to Emit*Block prior to generation new
634193326Sed  /// code.
635193326Sed  void EmitBranch(llvm::BasicBlock *Block);
636193326Sed
637193326Sed  /// HaveInsertPoint - True if an insertion point is defined. If not, this
638193326Sed  /// indicates that the current code being emitted is unreachable.
639193326Sed  bool HaveInsertPoint() const {
640193326Sed    return Builder.GetInsertBlock() != 0;
641193326Sed  }
642193326Sed
643193326Sed  /// EnsureInsertPoint - Ensure that an insertion point is defined so that
644193326Sed  /// emitted IR has a place to go. Note that by definition, if this function
645193326Sed  /// creates a block then that block is unreachable; callers may do better to
646193326Sed  /// detect when no insertion point is defined and simply skip IR generation.
647193326Sed  void EnsureInsertPoint() {
648193326Sed    if (!HaveInsertPoint())
649193326Sed      EmitBlock(createBasicBlock());
650193326Sed  }
651193326Sed
652193326Sed  /// ErrorUnsupported - Print out an error that codegen doesn't support the
653193326Sed  /// specified stmt yet.
654193326Sed  void ErrorUnsupported(const Stmt *S, const char *Type,
655193326Sed                        bool OmitOnError=false);
656193326Sed
657193326Sed  //===--------------------------------------------------------------------===//
658193326Sed  //                                  Helpers
659193326Sed  //===--------------------------------------------------------------------===//
660193326Sed
661198092Srdivacky  Qualifiers MakeQualifiers(QualType T) {
662198893Srdivacky    Qualifiers Quals = getContext().getCanonicalType(T).getQualifiers();
663198092Srdivacky    Quals.setObjCGCAttr(getContext().getObjCGCAttrKind(T));
664198092Srdivacky    return Quals;
665198092Srdivacky  }
666198092Srdivacky
667193326Sed  /// CreateTempAlloca - This creates a alloca and inserts it into the entry
668203955Srdivacky  /// block. The caller is responsible for setting an appropriate alignment on
669203955Srdivacky  /// the alloca.
670193326Sed  llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty,
671198398Srdivacky                                     const llvm::Twine &Name = "tmp");
672193326Sed
673204643Srdivacky  /// CreateIRTemp - Create a temporary IR object of the given type, with
674204643Srdivacky  /// appropriate alignment. This routine should only be used when an temporary
675204643Srdivacky  /// value needs to be stored into an alloca (for example, to avoid explicit
676204643Srdivacky  /// PHI construction), but the type is the IR type, not the type appropriate
677204643Srdivacky  /// for storing in memory.
678204643Srdivacky  llvm::Value *CreateIRTemp(QualType T, const llvm::Twine &Name = "tmp");
679204643Srdivacky
680203955Srdivacky  /// CreateMemTemp - Create a temporary memory object of the given type, with
681203955Srdivacky  /// appropriate alignment.
682203955Srdivacky  llvm::Value *CreateMemTemp(QualType T, const llvm::Twine &Name = "tmp");
683203955Srdivacky
684193326Sed  /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
685193326Sed  /// expression and compare the result against zero, returning an Int1Ty value.
686193326Sed  llvm::Value *EvaluateExprAsBool(const Expr *E);
687193326Sed
688193326Sed  /// EmitAnyExpr - Emit code to compute the specified expression which can have
689193326Sed  /// any type.  The result is returned as an RValue struct.  If this is an
690193326Sed  /// aggregate expression, the aggloc/agglocvolatile arguments indicate where
691193326Sed  /// the result should be returned.
692193326Sed  ///
693193326Sed  /// \param IgnoreResult - True if the resulting value isn't used.
694193326Sed  RValue EmitAnyExpr(const Expr *E, llvm::Value *AggLoc = 0,
695198092Srdivacky                     bool IsAggLocVolatile = false, bool IgnoreResult = false,
696198092Srdivacky                     bool IsInitializer = false);
697193326Sed
698193326Sed  // EmitVAListRef - Emit a "reference" to a va_list; this is either the address
699193326Sed  // or the value of the expression, depending on how va_list is defined.
700193326Sed  llvm::Value *EmitVAListRef(const Expr *E);
701193326Sed
702193326Sed  /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
703193326Sed  /// always be accessible even if no aggregate location is provided.
704198092Srdivacky  RValue EmitAnyExprToTemp(const Expr *E, bool IsAggLocVolatile = false,
705198092Srdivacky                           bool IsInitializer = false);
706193326Sed
707193326Sed  /// EmitAggregateCopy - Emit an aggrate copy.
708193326Sed  ///
709193326Sed  /// \param isVolatile - True iff either the source or the destination is
710193326Sed  /// volatile.
711193326Sed  void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
712193326Sed                         QualType EltTy, bool isVolatile=false);
713193326Sed
714193326Sed  void EmitAggregateClear(llvm::Value *DestPtr, QualType Ty);
715193326Sed
716193326Sed  /// StartBlock - Start new block named N. If insert block is a dummy block
717193326Sed  /// then reuse it.
718193326Sed  void StartBlock(const char *N);
719193326Sed
720193326Sed  /// GetAddrOfStaticLocalVar - Return the address of a static local variable.
721193326Sed  llvm::Constant *GetAddrOfStaticLocalVar(const VarDecl *BVD);
722193326Sed
723193326Sed  /// GetAddrOfLocalVar - Return the address of a local variable.
724193326Sed  llvm::Value *GetAddrOfLocalVar(const VarDecl *VD);
725193326Sed
726193326Sed  /// getAccessedFieldNo - Given an encoded value and a result number, return
727193326Sed  /// the input field number being accessed.
728193326Sed  static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts);
729193326Sed
730198893Srdivacky  llvm::BlockAddress *GetAddrOfLabel(const LabelStmt *L);
731198092Srdivacky  llvm::BasicBlock *GetIndirectGotoBlock();
732193326Sed
733193326Sed  /// EmitMemSetToZero - Generate code to memset a value of the given type to 0.
734193326Sed  void EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty);
735193326Sed
736193326Sed  // EmitVAArg - Generate code to get an argument from the passed in pointer
737193326Sed  // and update it accordingly. The return value is a pointer to the argument.
738193326Sed  // FIXME: We should be able to get rid of this method and use the va_arg
739193326Sed  // instruction in LLVM instead once it works well enough.
740193326Sed  llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty);
741193326Sed
742199990Srdivacky  /// EmitVLASize - Generate code for any VLA size expressions that might occur
743199990Srdivacky  /// in a variably modified type. If Ty is a VLA, will return the value that
744199990Srdivacky  /// corresponds to the size in bytes of the VLA type. Will return 0 otherwise.
745198092Srdivacky  ///
746198092Srdivacky  /// This function can be called with a null (unreachable) insert point.
747193326Sed  llvm::Value *EmitVLASize(QualType Ty);
748193326Sed
749193326Sed  // GetVLASize - Returns an LLVM value that corresponds to the size in bytes
750193326Sed  // of a variable length array type.
751193326Sed  llvm::Value *GetVLASize(const VariableArrayType *);
752193326Sed
753193326Sed  /// LoadCXXThis - Load the value of 'this'. This function is only valid while
754193326Sed  /// generating code for an C++ member function.
755204643Srdivacky  llvm::Value *LoadCXXThis() {
756204643Srdivacky    assert(CXXThisValue && "no 'this' value for this function");
757204643Srdivacky    return CXXThisValue;
758204643Srdivacky  }
759198092Srdivacky
760202379Srdivacky  /// LoadCXXVTT - Load the VTT parameter to base constructors/destructors have
761202379Srdivacky  /// virtual bases.
762204643Srdivacky  llvm::Value *LoadCXXVTT() {
763204643Srdivacky    assert(CXXVTTValue && "no VTT value for this function");
764204643Srdivacky    return CXXVTTValue;
765204643Srdivacky  }
766203955Srdivacky
767203955Srdivacky  /// GetAddressOfBaseOfCompleteClass - Convert the given pointer to a
768203955Srdivacky  /// complete class down to one of its virtual bases.
769203955Srdivacky  llvm::Value *GetAddressOfBaseOfCompleteClass(llvm::Value *Value,
770203955Srdivacky                                               bool IsVirtual,
771203955Srdivacky                                               const CXXRecordDecl *Derived,
772203955Srdivacky                                               const CXXRecordDecl *Base);
773202379Srdivacky
774199990Srdivacky  /// GetAddressOfBaseClass - This function will add the necessary delta to the
775199990Srdivacky  /// load of 'this' and returns address of the base class.
776199990Srdivacky  llvm::Value *GetAddressOfBaseClass(llvm::Value *Value,
777199990Srdivacky                                     const CXXRecordDecl *ClassDecl,
778199990Srdivacky                                     const CXXRecordDecl *BaseClassDecl,
779199990Srdivacky                                     bool NullCheckValue);
780199990Srdivacky
781199990Srdivacky  llvm::Value *GetAddressOfDerivedClass(llvm::Value *Value,
782198092Srdivacky                                        const CXXRecordDecl *ClassDecl,
783199990Srdivacky                                        const CXXRecordDecl *DerivedClassDecl,
784198092Srdivacky                                        bool NullCheckValue);
785199990Srdivacky
786203955Srdivacky  llvm::Value *GetVirtualBaseClassOffset(llvm::Value *This,
787203955Srdivacky                                         const CXXRecordDecl *ClassDecl,
788203955Srdivacky                                         const CXXRecordDecl *BaseClassDecl);
789198092Srdivacky
790198092Srdivacky  void EmitClassAggrMemberwiseCopy(llvm::Value *DestValue,
791198092Srdivacky                                   llvm::Value *SrcValue,
792198092Srdivacky                                   const ArrayType *Array,
793198092Srdivacky                                   const CXXRecordDecl *BaseClassDecl,
794198092Srdivacky                                   QualType Ty);
795198092Srdivacky
796198092Srdivacky  void EmitClassAggrCopyAssignment(llvm::Value *DestValue,
797198092Srdivacky                                   llvm::Value *SrcValue,
798198092Srdivacky                                   const ArrayType *Array,
799198092Srdivacky                                   const CXXRecordDecl *BaseClassDecl,
800198092Srdivacky                                   QualType Ty);
801198092Srdivacky
802198092Srdivacky  void EmitClassMemberwiseCopy(llvm::Value *DestValue, llvm::Value *SrcValue,
803198092Srdivacky                               const CXXRecordDecl *ClassDecl,
804198092Srdivacky                               const CXXRecordDecl *BaseClassDecl,
805198092Srdivacky                               QualType Ty);
806198092Srdivacky
807198092Srdivacky  void EmitClassCopyAssignment(llvm::Value *DestValue, llvm::Value *SrcValue,
808198092Srdivacky                               const CXXRecordDecl *ClassDecl,
809198092Srdivacky                               const CXXRecordDecl *BaseClassDecl,
810198092Srdivacky                               QualType Ty);
811198092Srdivacky
812204643Srdivacky  void EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor,
813204643Srdivacky                                      CXXCtorType CtorType,
814204643Srdivacky                                      const FunctionArgList &Args);
815198092Srdivacky  void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type,
816193326Sed                              llvm::Value *This,
817193326Sed                              CallExpr::const_arg_iterator ArgBeg,
818193326Sed                              CallExpr::const_arg_iterator ArgEnd);
819193326Sed
820198092Srdivacky  void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
821198092Srdivacky                                  const ConstantArrayType *ArrayTy,
822199990Srdivacky                                  llvm::Value *ArrayPtr,
823199990Srdivacky                                  CallExpr::const_arg_iterator ArgBeg,
824199990Srdivacky                                  CallExpr::const_arg_iterator ArgEnd);
825199990Srdivacky
826198092Srdivacky  void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
827198092Srdivacky                                  llvm::Value *NumElements,
828199990Srdivacky                                  llvm::Value *ArrayPtr,
829199990Srdivacky                                  CallExpr::const_arg_iterator ArgBeg,
830199990Srdivacky                                  CallExpr::const_arg_iterator ArgEnd);
831198092Srdivacky
832198092Srdivacky  void EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
833198092Srdivacky                                 const ArrayType *Array,
834198092Srdivacky                                 llvm::Value *This);
835198092Srdivacky
836199482Srdivacky  void EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
837199482Srdivacky                                 llvm::Value *NumElements,
838199482Srdivacky                                 llvm::Value *This);
839199482Srdivacky
840202379Srdivacky  llvm::Constant *GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D,
841199482Srdivacky                                                const ArrayType *Array,
842199482Srdivacky                                                llvm::Value *This);
843199482Srdivacky
844193326Sed  void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type,
845193326Sed                             llvm::Value *This);
846198092Srdivacky
847193326Sed  void PushCXXTemporary(const CXXTemporary *Temporary, llvm::Value *Ptr);
848193401Sed  void PopCXXTemporary();
849198092Srdivacky
850193326Sed  llvm::Value *EmitCXXNewExpr(const CXXNewExpr *E);
851198092Srdivacky  void EmitCXXDeleteExpr(const CXXDeleteExpr *E);
852198092Srdivacky
853199482Srdivacky  void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr,
854199482Srdivacky                      QualType DeleteTy);
855199482Srdivacky
856199482Srdivacky  llvm::Value* EmitCXXTypeidExpr(const CXXTypeidExpr *E);
857199482Srdivacky  llvm::Value *EmitDynamicCast(llvm::Value *V, const CXXDynamicCastExpr *DCE);
858199482Srdivacky
859201361Srdivacky  void EmitCheck(llvm::Value *, unsigned Size);
860201361Srdivacky
861202379Srdivacky  llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
862202379Srdivacky                                       bool isInc, bool isPre);
863202379Srdivacky  ComplexPairTy EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
864202379Srdivacky                                         bool isInc, bool isPre);
865193326Sed  //===--------------------------------------------------------------------===//
866193326Sed  //                            Declaration Emission
867193326Sed  //===--------------------------------------------------------------------===//
868193326Sed
869198092Srdivacky  /// EmitDecl - Emit a declaration.
870198092Srdivacky  ///
871198092Srdivacky  /// This function can be called with a null (unreachable) insert point.
872193326Sed  void EmitDecl(const Decl &D);
873198092Srdivacky
874198092Srdivacky  /// EmitBlockVarDecl - Emit a block variable declaration.
875198092Srdivacky  ///
876198092Srdivacky  /// This function can be called with a null (unreachable) insert point.
877193326Sed  void EmitBlockVarDecl(const VarDecl &D);
878198092Srdivacky
879198092Srdivacky  /// EmitLocalBlockVarDecl - Emit a local block variable declaration.
880198092Srdivacky  ///
881198092Srdivacky  /// This function can be called with a null (unreachable) insert point.
882193326Sed  void EmitLocalBlockVarDecl(const VarDecl &D);
883198092Srdivacky
884203955Srdivacky  void EmitStaticBlockVarDecl(const VarDecl &D,
885203955Srdivacky                              llvm::GlobalValue::LinkageTypes Linkage);
886193326Sed
887193326Sed  /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
888193326Sed  void EmitParmDecl(const VarDecl &D, llvm::Value *Arg);
889193326Sed
890193326Sed  //===--------------------------------------------------------------------===//
891193326Sed  //                             Statement Emission
892193326Sed  //===--------------------------------------------------------------------===//
893193326Sed
894193326Sed  /// EmitStopPoint - Emit a debug stoppoint if we are emitting debug info.
895193326Sed  void EmitStopPoint(const Stmt *S);
896193326Sed
897193326Sed  /// EmitStmt - Emit the code for the statement \arg S. It is legal to call
898193326Sed  /// this function even if there is no current insertion point.
899193326Sed  ///
900193326Sed  /// This function may clear the current insertion point; callers should use
901193326Sed  /// EnsureInsertPoint if they wish to subsequently generate code without first
902193326Sed  /// calling EmitBlock, EmitBranch, or EmitStmt.
903193326Sed  void EmitStmt(const Stmt *S);
904193326Sed
905193326Sed  /// EmitSimpleStmt - Try to emit a "simple" statement which does not
906193326Sed  /// necessarily require an insertion point or debug information; typically
907193326Sed  /// because the statement amounts to a jump or a container of other
908193326Sed  /// statements.
909193326Sed  ///
910193326Sed  /// \return True if the statement was handled.
911193326Sed  bool EmitSimpleStmt(const Stmt *S);
912193326Sed
913193326Sed  RValue EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false,
914193326Sed                          llvm::Value *AggLoc = 0, bool isAggVol = false);
915193326Sed
916193326Sed  /// EmitLabel - Emit the block for the given label. It is legal to call this
917193326Sed  /// function even if there is no current insertion point.
918193326Sed  void EmitLabel(const LabelStmt &S); // helper for EmitLabelStmt.
919193326Sed
920193326Sed  void EmitLabelStmt(const LabelStmt &S);
921193326Sed  void EmitGotoStmt(const GotoStmt &S);
922193326Sed  void EmitIndirectGotoStmt(const IndirectGotoStmt &S);
923193326Sed  void EmitIfStmt(const IfStmt &S);
924193326Sed  void EmitWhileStmt(const WhileStmt &S);
925193326Sed  void EmitDoStmt(const DoStmt &S);
926193326Sed  void EmitForStmt(const ForStmt &S);
927193326Sed  void EmitReturnStmt(const ReturnStmt &S);
928193326Sed  void EmitDeclStmt(const DeclStmt &S);
929193326Sed  void EmitBreakStmt(const BreakStmt &S);
930193326Sed  void EmitContinueStmt(const ContinueStmt &S);
931193326Sed  void EmitSwitchStmt(const SwitchStmt &S);
932193326Sed  void EmitDefaultStmt(const DefaultStmt &S);
933193326Sed  void EmitCaseStmt(const CaseStmt &S);
934193326Sed  void EmitCaseStmtRange(const CaseStmt &S);
935193326Sed  void EmitAsmStmt(const AsmStmt &S);
936193326Sed
937193326Sed  void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S);
938193326Sed  void EmitObjCAtTryStmt(const ObjCAtTryStmt &S);
939193326Sed  void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S);
940193326Sed  void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S);
941193326Sed
942204643Srdivacky  struct CXXTryStmtInfo {
943204643Srdivacky    llvm::BasicBlock *SavedLandingPad;
944204643Srdivacky    llvm::BasicBlock *HandlerBlock;
945204643Srdivacky    llvm::BasicBlock *FinallyBlock;
946204643Srdivacky  };
947204643Srdivacky  CXXTryStmtInfo EnterCXXTryStmt(const CXXTryStmt &S);
948204643Srdivacky  void ExitCXXTryStmt(const CXXTryStmt &S, CXXTryStmtInfo Info);
949204643Srdivacky
950198092Srdivacky  void EmitCXXTryStmt(const CXXTryStmt &S);
951198092Srdivacky
952193326Sed  //===--------------------------------------------------------------------===//
953193326Sed  //                         LValue Expression Emission
954193326Sed  //===--------------------------------------------------------------------===//
955193326Sed
956193326Sed  /// GetUndefRValue - Get an appropriate 'undef' rvalue for the given type.
957193326Sed  RValue GetUndefRValue(QualType Ty);
958193326Sed
959193326Sed  /// EmitUnsupportedRValue - Emit a dummy r-value using the type of E
960193326Sed  /// and issue an ErrorUnsupported style diagnostic (using the
961193326Sed  /// provided Name).
962193326Sed  RValue EmitUnsupportedRValue(const Expr *E,
963193326Sed                               const char *Name);
964193326Sed
965193326Sed  /// EmitUnsupportedLValue - Emit a dummy l-value using the type of E and issue
966193326Sed  /// an ErrorUnsupported style diagnostic (using the provided Name).
967193326Sed  LValue EmitUnsupportedLValue(const Expr *E,
968193326Sed                               const char *Name);
969193326Sed
970193326Sed  /// EmitLValue - Emit code to compute a designator that specifies the location
971193326Sed  /// of the expression.
972193326Sed  ///
973193326Sed  /// This can return one of two things: a simple address or a bitfield
974193326Sed  /// reference.  In either case, the LLVM Value* in the LValue structure is
975193326Sed  /// guaranteed to be an LLVM pointer type.
976193326Sed  ///
977193326Sed  /// If this returns a bitfield reference, nothing about the pointee type of
978193326Sed  /// the LLVM value is known: For example, it may not be a pointer to an
979193326Sed  /// integer.
980193326Sed  ///
981193326Sed  /// If this returns a normal address, and if the lvalue's C type is fixed
982193326Sed  /// size, this method guarantees that the returned pointer type will point to
983193326Sed  /// an LLVM type of the same size of the lvalue's type.  If the lvalue has a
984193326Sed  /// variable length type, this is not possible.
985193326Sed  ///
986193326Sed  LValue EmitLValue(const Expr *E);
987193326Sed
988201361Srdivacky  /// EmitCheckedLValue - Same as EmitLValue but additionally we generate
989201361Srdivacky  /// checking code to guard against undefined behavior.  This is only
990201361Srdivacky  /// suitable when we know that the address will be used to access the
991201361Srdivacky  /// object.
992201361Srdivacky  LValue EmitCheckedLValue(const Expr *E);
993201361Srdivacky
994193326Sed  /// EmitLoadOfScalar - Load a scalar value from an address, taking
995193326Sed  /// care to appropriately convert from the memory representation to
996193326Sed  /// the LLVM value representation.
997193326Sed  llvm::Value *EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
998193326Sed                                QualType Ty);
999193326Sed
1000193326Sed  /// EmitStoreOfScalar - Store a scalar value to an address, taking
1001193326Sed  /// care to appropriately convert from the memory representation to
1002193326Sed  /// the LLVM value representation.
1003193326Sed  void EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
1004193326Sed                         bool Volatile, QualType Ty);
1005193326Sed
1006193326Sed  /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
1007193326Sed  /// this method emits the address of the lvalue, then loads the result as an
1008193326Sed  /// rvalue, returning the rvalue.
1009193326Sed  RValue EmitLoadOfLValue(LValue V, QualType LVType);
1010193326Sed  RValue EmitLoadOfExtVectorElementLValue(LValue V, QualType LVType);
1011193326Sed  RValue EmitLoadOfBitfieldLValue(LValue LV, QualType ExprType);
1012193326Sed  RValue EmitLoadOfPropertyRefLValue(LValue LV, QualType ExprType);
1013193326Sed  RValue EmitLoadOfKVCRefLValue(LValue LV, QualType ExprType);
1014193326Sed
1015193326Sed
1016193326Sed  /// EmitStoreThroughLValue - Store the specified rvalue into the specified
1017193326Sed  /// lvalue, where both are guaranteed to the have the same type, and that type
1018193326Sed  /// is 'Ty'.
1019193326Sed  void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
1020193326Sed  void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst,
1021193326Sed                                                QualType Ty);
1022193326Sed  void EmitStoreThroughPropertyRefLValue(RValue Src, LValue Dst, QualType Ty);
1023193326Sed  void EmitStoreThroughKVCRefLValue(RValue Src, LValue Dst, QualType Ty);
1024193326Sed
1025193326Sed  /// EmitStoreThroughLValue - Store Src into Dst with same constraints as
1026193326Sed  /// EmitStoreThroughLValue.
1027193326Sed  ///
1028193326Sed  /// \param Result [out] - If non-null, this will be set to a Value* for the
1029193326Sed  /// bit-field contents after the store, appropriate for use as the result of
1030193326Sed  /// an assignment to the bit-field.
1031193326Sed  void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, QualType Ty,
1032193326Sed                                      llvm::Value **Result=0);
1033193326Sed
1034193326Sed  // Note: only availabe for agg return types
1035193326Sed  LValue EmitBinaryOperatorLValue(const BinaryOperator *E);
1036193326Sed  // Note: only available for agg return types
1037193326Sed  LValue EmitCallExprLValue(const CallExpr *E);
1038193326Sed  // Note: only available for agg return types
1039193326Sed  LValue EmitVAArgExprLValue(const VAArgExpr *E);
1040193326Sed  LValue EmitDeclRefLValue(const DeclRefExpr *E);
1041193326Sed  LValue EmitStringLiteralLValue(const StringLiteral *E);
1042193326Sed  LValue EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E);
1043193326Sed  LValue EmitPredefinedFunctionName(unsigned Type);
1044193326Sed  LValue EmitPredefinedLValue(const PredefinedExpr *E);
1045193326Sed  LValue EmitUnaryOpLValue(const UnaryOperator *E);
1046193326Sed  LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
1047193326Sed  LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E);
1048193326Sed  LValue EmitMemberExpr(const MemberExpr *E);
1049200583Srdivacky  LValue EmitObjCIsaExpr(const ObjCIsaExpr *E);
1050193326Sed  LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E);
1051198092Srdivacky  LValue EmitConditionalOperatorLValue(const ConditionalOperator *E);
1052193326Sed  LValue EmitCastLValue(const CastExpr *E);
1053198398Srdivacky  LValue EmitNullInitializationLValue(const CXXZeroInitValueExpr *E);
1054198398Srdivacky
1055193326Sed  llvm::Value *EmitIvarOffset(const ObjCInterfaceDecl *Interface,
1056193326Sed                              const ObjCIvarDecl *Ivar);
1057199482Srdivacky  LValue EmitLValueForField(llvm::Value* Base, const FieldDecl* Field,
1058203955Srdivacky                            unsigned CVRQualifiers);
1059203955Srdivacky
1060203955Srdivacky  /// EmitLValueForFieldInitialization - Like EmitLValueForField, except that
1061203955Srdivacky  /// if the Field is a reference, this will return the address of the reference
1062203955Srdivacky  /// and not the address of the value stored in the reference.
1063203955Srdivacky  LValue EmitLValueForFieldInitialization(llvm::Value* Base,
1064203955Srdivacky                                          const FieldDecl* Field,
1065203955Srdivacky                                          unsigned CVRQualifiers);
1066203955Srdivacky
1067193326Sed  LValue EmitLValueForIvar(QualType ObjectTy,
1068193326Sed                           llvm::Value* Base, const ObjCIvarDecl *Ivar,
1069193326Sed                           unsigned CVRQualifiers);
1070193326Sed
1071199482Srdivacky  LValue EmitLValueForBitfield(llvm::Value* Base, const FieldDecl* Field,
1072193326Sed                                unsigned CVRQualifiers);
1073193326Sed
1074193326Sed  LValue EmitBlockDeclRefLValue(const BlockDeclRefExpr *E);
1075193326Sed
1076193326Sed  LValue EmitCXXConstructLValue(const CXXConstructExpr *E);
1077193326Sed  LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E);
1078198092Srdivacky  LValue EmitCXXExprWithTemporariesLValue(const CXXExprWithTemporaries *E);
1079199482Srdivacky  LValue EmitCXXTypeidLValue(const CXXTypeidExpr *E);
1080193326Sed
1081193326Sed  LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E);
1082193326Sed  LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E);
1083193326Sed  LValue EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E);
1084198092Srdivacky  LValue EmitObjCKVCRefLValue(const ObjCImplicitSetterGetterRefExpr *E);
1085193326Sed  LValue EmitObjCSuperExprLValue(const ObjCSuperExpr *E);
1086193326Sed  LValue EmitStmtExprLValue(const StmtExpr *E);
1087198398Srdivacky  LValue EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E);
1088198398Srdivacky
1089193326Sed  //===--------------------------------------------------------------------===//
1090193326Sed  //                         Scalar Expression Emission
1091193326Sed  //===--------------------------------------------------------------------===//
1092193326Sed
1093193326Sed  /// EmitCall - Generate a call of the given function, expecting the given
1094193326Sed  /// result type, and using the given argument list which specifies both the
1095193326Sed  /// LLVM arguments and the types they were derived from.
1096193326Sed  ///
1097199990Srdivacky  /// \param TargetDecl - If given, the decl of the function in a direct call;
1098199990Srdivacky  /// used to set attributes on the call (noreturn, etc.).
1099193326Sed  RValue EmitCall(const CGFunctionInfo &FnInfo,
1100193326Sed                  llvm::Value *Callee,
1101201361Srdivacky                  ReturnValueSlot ReturnValue,
1102193326Sed                  const CallArgList &Args,
1103193326Sed                  const Decl *TargetDecl = 0);
1104198092Srdivacky
1105201361Srdivacky  RValue EmitCall(QualType FnType, llvm::Value *Callee,
1106201361Srdivacky                  ReturnValueSlot ReturnValue,
1107193326Sed                  CallExpr::const_arg_iterator ArgBeg,
1108193326Sed                  CallExpr::const_arg_iterator ArgEnd,
1109193326Sed                  const Decl *TargetDecl = 0);
1110201361Srdivacky  RValue EmitCallExpr(const CallExpr *E,
1111201361Srdivacky                      ReturnValueSlot ReturnValue = ReturnValueSlot());
1112198092Srdivacky
1113199482Srdivacky  llvm::Value *BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This,
1114198092Srdivacky                                const llvm::Type *Ty);
1115199482Srdivacky  llvm::Value *BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type,
1116199482Srdivacky                                llvm::Value *&This, const llvm::Type *Ty);
1117199482Srdivacky
1118193326Sed  RValue EmitCXXMemberCall(const CXXMethodDecl *MD,
1119193326Sed                           llvm::Value *Callee,
1120201361Srdivacky                           ReturnValueSlot ReturnValue,
1121193326Sed                           llvm::Value *This,
1122202379Srdivacky                           llvm::Value *VTT,
1123193326Sed                           CallExpr::const_arg_iterator ArgBeg,
1124193326Sed                           CallExpr::const_arg_iterator ArgEnd);
1125201361Srdivacky  RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E,
1126201361Srdivacky                               ReturnValueSlot ReturnValue);
1127201361Srdivacky  RValue EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
1128201361Srdivacky                                      ReturnValueSlot ReturnValue);
1129193326Sed
1130193326Sed  RValue EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
1131201361Srdivacky                                       const CXXMethodDecl *MD,
1132201361Srdivacky                                       ReturnValueSlot ReturnValue);
1133198092Srdivacky
1134193326Sed
1135198092Srdivacky  RValue EmitBuiltinExpr(const FunctionDecl *FD,
1136193326Sed                         unsigned BuiltinID, const CallExpr *E);
1137193326Sed
1138201361Srdivacky  RValue EmitBlockCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue);
1139193326Sed
1140193326Sed  /// EmitTargetBuiltinExpr - Emit the given builtin call. Returns 0 if the call
1141193326Sed  /// is unhandled by the current target.
1142193326Sed  llvm::Value *EmitTargetBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
1143193326Sed
1144204793Srdivacky  llvm::Value *EmitARMBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
1145193326Sed  llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
1146193326Sed  llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
1147193326Sed
1148193326Sed  llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E);
1149193326Sed  llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E);
1150193326Sed  llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E);
1151193326Sed  RValue EmitObjCMessageExpr(const ObjCMessageExpr *E);
1152193326Sed  RValue EmitObjCPropertyGet(const Expr *E);
1153193326Sed  RValue EmitObjCSuperPropertyGet(const Expr *Exp, const Selector &S);
1154193326Sed  void EmitObjCPropertySet(const Expr *E, RValue Src);
1155193326Sed  void EmitObjCSuperPropertySet(const Expr *E, const Selector &S, RValue Src);
1156193326Sed
1157193326Sed
1158193326Sed  /// EmitReferenceBindingToExpr - Emits a reference binding to the passed in
1159193326Sed  /// expression. Will emit a temporary variable if E is not an LValue.
1160203955Srdivacky  RValue EmitReferenceBindingToExpr(const Expr* E, bool IsInitializer = false);
1161198092Srdivacky
1162193326Sed  //===--------------------------------------------------------------------===//
1163193326Sed  //                           Expression Emission
1164193326Sed  //===--------------------------------------------------------------------===//
1165193326Sed
1166193326Sed  // Expressions are broken into three classes: scalar, complex, aggregate.
1167193326Sed
1168193326Sed  /// EmitScalarExpr - Emit the computation of the specified expression of LLVM
1169193326Sed  /// scalar type, returning the result.
1170198092Srdivacky  llvm::Value *EmitScalarExpr(const Expr *E , bool IgnoreResultAssign = false);
1171193326Sed
1172193326Sed  /// EmitScalarConversion - Emit a conversion from the specified type to the
1173193326Sed  /// specified destination type, both of which are LLVM scalar types.
1174193326Sed  llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy,
1175193326Sed                                    QualType DstTy);
1176193326Sed
1177193326Sed  /// EmitComplexToScalarConversion - Emit a conversion from the specified
1178193326Sed  /// complex type to the specified destination type, where the destination type
1179193326Sed  /// is an LLVM scalar type.
1180193326Sed  llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy,
1181193326Sed                                             QualType DstTy);
1182193326Sed
1183193326Sed
1184193326Sed  /// EmitAggExpr - Emit the computation of the specified expression of
1185193326Sed  /// aggregate type.  The result is computed into DestPtr.  Note that if
1186193326Sed  /// DestPtr is null, the value of the aggregate expression is not needed.
1187193326Sed  void EmitAggExpr(const Expr *E, llvm::Value *DestPtr, bool VolatileDest,
1188198092Srdivacky                   bool IgnoreResult = false, bool IsInitializer = false,
1189198092Srdivacky                   bool RequiresGCollection = false);
1190193326Sed
1191203955Srdivacky  /// EmitAggExprToLValue - Emit the computation of the specified expression of
1192203955Srdivacky  /// aggregate type into a temporary LValue.
1193203955Srdivacky  LValue EmitAggExprToLValue(const Expr *E);
1194203955Srdivacky
1195198092Srdivacky  /// EmitGCMemmoveCollectable - Emit special API for structs with object
1196198092Srdivacky  /// pointers.
1197198092Srdivacky  void EmitGCMemmoveCollectable(llvm::Value *DestPtr, llvm::Value *SrcPtr,
1198198092Srdivacky                                QualType Ty);
1199198092Srdivacky
1200193326Sed  /// EmitComplexExpr - Emit the computation of the specified expression of
1201193326Sed  /// complex type, returning the result.
1202193326Sed  ComplexPairTy EmitComplexExpr(const Expr *E, bool IgnoreReal = false,
1203193326Sed                                bool IgnoreImag = false,
1204193326Sed                                bool IgnoreRealAssign = false,
1205193326Sed                                bool IgnoreImagAssign = false);
1206193326Sed
1207193326Sed  /// EmitComplexExprIntoAddr - Emit the computation of the specified expression
1208193326Sed  /// of complex type, storing into the specified Value*.
1209193326Sed  void EmitComplexExprIntoAddr(const Expr *E, llvm::Value *DestAddr,
1210193326Sed                               bool DestIsVolatile);
1211193326Sed
1212193326Sed  /// StoreComplexToAddr - Store a complex number into the specified address.
1213193326Sed  void StoreComplexToAddr(ComplexPairTy V, llvm::Value *DestAddr,
1214193326Sed                          bool DestIsVolatile);
1215193326Sed  /// LoadComplexFromAddr - Load a complex number from the specified address.
1216193326Sed  ComplexPairTy LoadComplexFromAddr(llvm::Value *SrcAddr, bool SrcIsVolatile);
1217193326Sed
1218199990Srdivacky  /// CreateStaticBlockVarDecl - Create a zero-initialized LLVM global for a
1219199990Srdivacky  /// static block var decl.
1220200583Srdivacky  llvm::GlobalVariable *CreateStaticBlockVarDecl(const VarDecl &D,
1221200583Srdivacky                                                 const char *Separator,
1222199990Srdivacky                                       llvm::GlobalValue::LinkageTypes Linkage);
1223200583Srdivacky
1224200583Srdivacky  /// AddInitializerToGlobalBlockVarDecl - Add the initializer for 'D' to the
1225200583Srdivacky  /// global variable that has already been created for it.  If the initializer
1226200583Srdivacky  /// has a different type than GV does, this may free GV and return a different
1227200583Srdivacky  /// one.  Otherwise it just returns GV.
1228200583Srdivacky  llvm::GlobalVariable *
1229200583Srdivacky  AddInitializerToGlobalBlockVarDecl(const VarDecl &D,
1230200583Srdivacky                                     llvm::GlobalVariable *GV);
1231200583Srdivacky
1232193326Sed
1233199990Srdivacky  /// EmitStaticCXXBlockVarDeclInit - Create the initializer for a C++ runtime
1234199990Srdivacky  /// initialized static block var decl.
1235198092Srdivacky  void EmitStaticCXXBlockVarDeclInit(const VarDecl &D,
1236198092Srdivacky                                     llvm::GlobalVariable *GV);
1237193326Sed
1238198092Srdivacky  /// EmitCXXGlobalVarDeclInit - Create the initializer for a C++
1239198092Srdivacky  /// variable with global storage.
1240198092Srdivacky  void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::Constant *DeclPtr);
1241198092Srdivacky
1242198092Srdivacky  /// EmitCXXGlobalDtorRegistration - Emits a call to register the global ptr
1243198092Srdivacky  /// with the C++ runtime so that its destructor will be called at exit.
1244199482Srdivacky  void EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn,
1245198092Srdivacky                                     llvm::Constant *DeclPtr);
1246198092Srdivacky
1247198092Srdivacky  /// GenerateCXXGlobalInitFunc - Generates code for initializing global
1248198092Srdivacky  /// variables.
1249198092Srdivacky  void GenerateCXXGlobalInitFunc(llvm::Function *Fn,
1250202379Srdivacky                                 llvm::Constant **Decls,
1251198092Srdivacky                                 unsigned NumDecls);
1252198092Srdivacky
1253205408Srdivacky  /// GenerateCXXGlobalDtorFunc - Generates code for destroying global
1254205408Srdivacky  /// variables.
1255205408Srdivacky  void GenerateCXXGlobalDtorFunc(llvm::Function *Fn,
1256205408Srdivacky                                 const std::vector<std::pair<llvm::Constant*,
1257205408Srdivacky                                   llvm::Constant*> > &DtorsAndObjects);
1258205408Srdivacky
1259202379Srdivacky  void GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, const VarDecl *D);
1260202379Srdivacky
1261193326Sed  void EmitCXXConstructExpr(llvm::Value *Dest, const CXXConstructExpr *E);
1262198092Srdivacky
1263193326Sed  RValue EmitCXXExprWithTemporaries(const CXXExprWithTemporaries *E,
1264198092Srdivacky                                    llvm::Value *AggLoc = 0,
1265198092Srdivacky                                    bool IsAggLocVolatile = false,
1266198092Srdivacky                                    bool IsInitializer = false);
1267198092Srdivacky
1268198893Srdivacky  void EmitCXXThrowExpr(const CXXThrowExpr *E);
1269199482Srdivacky
1270193326Sed  //===--------------------------------------------------------------------===//
1271193326Sed  //                             Internal Helpers
1272193326Sed  //===--------------------------------------------------------------------===//
1273193326Sed
1274193326Sed  /// ContainsLabel - Return true if the statement contains a label in it.  If
1275193326Sed  /// this statement is not executed normally, it not containing a label means
1276193326Sed  /// that we can just remove the code.
1277193326Sed  static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts = false);
1278193326Sed
1279193326Sed  /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
1280193326Sed  /// to a constant, or if it does but contains a label, return 0.  If it
1281193326Sed  /// constant folds to 'true' and does not contain a label, return 1, if it
1282193326Sed  /// constant folds to 'false' and does not contain a label, return -1.
1283193326Sed  int ConstantFoldsToSimpleInteger(const Expr *Cond);
1284193326Sed
1285193326Sed  /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an
1286193326Sed  /// if statement) to the specified blocks.  Based on the condition, this might
1287193326Sed  /// try to simplify the codegen of the conditional based on the branch.
1288193326Sed  void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock,
1289193326Sed                            llvm::BasicBlock *FalseBlock);
1290200583Srdivacky
1291200583Srdivacky  /// getTrapBB - Create a basic block that will call the trap intrinsic.  We'll
1292200583Srdivacky  /// generate a branch around the created basic block as necessary.
1293200583Srdivacky  llvm::BasicBlock* getTrapBB();
1294206084Srdivacky
1295206084Srdivacky  /// EmitCallArg - Emit a single call argument.
1296206084Srdivacky  RValue EmitCallArg(const Expr *E, QualType ArgType);
1297206084Srdivacky
1298193326Sedprivate:
1299193326Sed
1300193326Sed  void EmitReturnOfRValue(RValue RV, QualType Ty);
1301193326Sed
1302193326Sed  /// ExpandTypeFromArgs - Reconstruct a structure of type \arg Ty
1303193326Sed  /// from function arguments into \arg Dst. See ABIArgInfo::Expand.
1304193326Sed  ///
1305193326Sed  /// \param AI - The first function argument of the expansion.
1306193326Sed  /// \return The argument following the last expanded function
1307193326Sed  /// argument.
1308193326Sed  llvm::Function::arg_iterator
1309193326Sed  ExpandTypeFromArgs(QualType Ty, LValue Dst,
1310193326Sed                     llvm::Function::arg_iterator AI);
1311193326Sed
1312193326Sed  /// ExpandTypeToArgs - Expand an RValue \arg Src, with the LLVM type for \arg
1313193326Sed  /// Ty, into individual arguments on the provided vector \arg Args. See
1314193326Sed  /// ABIArgInfo::Expand.
1315193326Sed  void ExpandTypeToArgs(QualType Ty, RValue Src,
1316193326Sed                        llvm::SmallVector<llvm::Value*, 16> &Args);
1317193326Sed
1318198092Srdivacky  llvm::Value* EmitAsmInput(const AsmStmt &S,
1319193326Sed                            const TargetInfo::ConstraintInfo &Info,
1320193326Sed                            const Expr *InputExpr, std::string &ConstraintStr);
1321193326Sed
1322193326Sed  /// EmitCleanupBlock - emits a single cleanup block.
1323193326Sed  void EmitCleanupBlock();
1324193326Sed
1325193326Sed  /// AddBranchFixup - adds a branch instruction to the list of fixups for the
1326193326Sed  /// current cleanup scope.
1327193326Sed  void AddBranchFixup(llvm::BranchInst *BI);
1328193326Sed
1329193326Sed  /// EmitCallArgs - Emit call arguments for a function.
1330198092Srdivacky  /// The CallArgTypeInfo parameter is used for iterating over the known
1331193326Sed  /// argument types of the function being called.
1332193326Sed  template<typename T>
1333193326Sed  void EmitCallArgs(CallArgList& Args, const T* CallArgTypeInfo,
1334193326Sed                    CallExpr::const_arg_iterator ArgBeg,
1335193326Sed                    CallExpr::const_arg_iterator ArgEnd) {
1336193326Sed      CallExpr::const_arg_iterator Arg = ArgBeg;
1337193326Sed
1338193326Sed    // First, use the argument types that the type info knows about
1339193326Sed    if (CallArgTypeInfo) {
1340193326Sed      for (typename T::arg_type_iterator I = CallArgTypeInfo->arg_type_begin(),
1341193326Sed           E = CallArgTypeInfo->arg_type_end(); I != E; ++I, ++Arg) {
1342199482Srdivacky        assert(Arg != ArgEnd && "Running over edge of argument list!");
1343193326Sed        QualType ArgType = *I;
1344193326Sed
1345193326Sed        assert(getContext().getCanonicalType(ArgType.getNonReferenceType()).
1346198092Srdivacky               getTypePtr() ==
1347198092Srdivacky               getContext().getCanonicalType(Arg->getType()).getTypePtr() &&
1348193326Sed               "type mismatch in call argument!");
1349198092Srdivacky
1350198092Srdivacky        Args.push_back(std::make_pair(EmitCallArg(*Arg, ArgType),
1351193326Sed                                      ArgType));
1352193326Sed      }
1353198092Srdivacky
1354198092Srdivacky      // Either we've emitted all the call args, or we have a call to a
1355193326Sed      // variadic function.
1356198092Srdivacky      assert((Arg == ArgEnd || CallArgTypeInfo->isVariadic()) &&
1357193326Sed             "Extra arguments in non-variadic function!");
1358198092Srdivacky
1359193326Sed    }
1360198092Srdivacky
1361193326Sed    // If we still have any arguments, emit them using the type of the argument.
1362193326Sed    for (; Arg != ArgEnd; ++Arg) {
1363193326Sed      QualType ArgType = Arg->getType();
1364193326Sed      Args.push_back(std::make_pair(EmitCallArg(*Arg, ArgType),
1365193326Sed                                    ArgType));
1366193326Sed    }
1367193326Sed  }
1368204643Srdivacky
1369204643Srdivacky  const TargetCodeGenInfo &getTargetHooks() const {
1370204643Srdivacky    return CGM.getTargetCodeGenInfo();
1371204643Srdivacky  }
1372193326Sed};
1373193326Sed
1374198092Srdivacky
1375193326Sed}  // end namespace CodeGen
1376193326Sed}  // end namespace clang
1377193326Sed
1378193326Sed#endif
1379