CodeGenFunction.h revision fbfb52d4cf10b4fd1ac19d1445fe2ffcb9c2bbf1
1//===-- CodeGenFunction.h - Per-Function state for LLVM CodeGen -*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This is the internal per-function state used for llvm translation.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef CLANG_CODEGEN_CODEGENFUNCTION_H
15#define CLANG_CODEGEN_CODEGENFUNCTION_H
16
17#include "clang/AST/Type.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/AST/ExprObjC.h"
20#include "clang/Basic/TargetInfo.h"
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/Support/ValueHandle.h"
24#include <map>
25#include "CodeGenModule.h"
26#include "CGBlocks.h"
27#include "CGBuilder.h"
28#include "CGCall.h"
29#include "CGCXX.h"
30#include "CGValue.h"
31
32namespace llvm {
33  class BasicBlock;
34  class LLVMContext;
35  class Module;
36  class SwitchInst;
37  class Twine;
38  class Value;
39}
40
41namespace clang {
42  class ASTContext;
43  class CXXDestructorDecl;
44  class CXXTryStmt;
45  class Decl;
46  class EnumConstantDecl;
47  class FunctionDecl;
48  class FunctionProtoType;
49  class LabelStmt;
50  class ObjCContainerDecl;
51  class ObjCInterfaceDecl;
52  class ObjCIvarDecl;
53  class ObjCMethodDecl;
54  class ObjCImplementationDecl;
55  class ObjCPropertyImplDecl;
56  class TargetInfo;
57  class VarDecl;
58  class ObjCForCollectionStmt;
59  class ObjCAtTryStmt;
60  class ObjCAtThrowStmt;
61  class ObjCAtSynchronizedStmt;
62
63namespace CodeGen {
64  class CodeGenModule;
65  class CodeGenTypes;
66  class CGDebugInfo;
67  class CGFunctionInfo;
68  class CGRecordLayout;
69
70/// CodeGenFunction - This class organizes the per-function state that is used
71/// while generating LLVM code.
72class CodeGenFunction : public BlockFunction {
73  CodeGenFunction(const CodeGenFunction&); // DO NOT IMPLEMENT
74  void operator=(const CodeGenFunction&);  // DO NOT IMPLEMENT
75public:
76  CodeGenModule &CGM;  // Per-module state.
77  TargetInfo &Target;
78
79  typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
80  CGBuilderTy Builder;
81
82  /// CurFuncDecl - Holds the Decl for the current function or ObjC method.
83  /// This excludes BlockDecls.
84  const Decl *CurFuncDecl;
85  /// CurCodeDecl - This is the inner-most code context, which includes blocks.
86  const Decl *CurCodeDecl;
87  const CGFunctionInfo *CurFnInfo;
88  QualType FnRetTy;
89  llvm::Function *CurFn;
90
91  /// ReturnBlock - Unified return block.
92  llvm::BasicBlock *ReturnBlock;
93  /// ReturnValue - The temporary alloca to hold the return value. This is null
94  /// iff the function has no return value.
95  llvm::Instruction *ReturnValue;
96
97  /// AllocaInsertPoint - This is an instruction in the entry block before which
98  /// we prefer to insert allocas.
99  llvm::AssertingVH<llvm::Instruction> AllocaInsertPt;
100
101  const llvm::Type *LLVMIntTy;
102  uint32_t LLVMPointerWidth;
103
104public:
105  /// ObjCEHValueStack - Stack of Objective-C exception values, used for
106  /// rethrows.
107  llvm::SmallVector<llvm::Value*, 8> ObjCEHValueStack;
108
109  /// PushCleanupBlock - Push a new cleanup entry on the stack and set the
110  /// passed in block as the cleanup block.
111  void PushCleanupBlock(llvm::BasicBlock *CleanupEntryBlock,
112                        llvm::BasicBlock *CleanupExitBlock = 0);
113
114  /// CleanupBlockInfo - A struct representing a popped cleanup block.
115  struct CleanupBlockInfo {
116    /// CleanupEntryBlock - the cleanup entry block
117    llvm::BasicBlock *CleanupBlock;
118
119    /// SwitchBlock - the block (if any) containing the switch instruction used
120    /// for jumping to the final destination.
121    llvm::BasicBlock *SwitchBlock;
122
123    /// EndBlock - the default destination for the switch instruction.
124    llvm::BasicBlock *EndBlock;
125
126    CleanupBlockInfo(llvm::BasicBlock *cb, llvm::BasicBlock *sb,
127                     llvm::BasicBlock *eb)
128      : CleanupBlock(cb), SwitchBlock(sb), EndBlock(eb) {}
129  };
130
131  /// PopCleanupBlock - Will pop the cleanup entry on the stack, process all
132  /// branch fixups and return a block info struct with the switch block and end
133  /// block.
134  CleanupBlockInfo PopCleanupBlock();
135
136  /// CleanupScope - RAII object that will create a cleanup block and set the
137  /// insert point to that block. When destructed, it sets the insert point to
138  /// the previous block and pushes a new cleanup entry on the stack.
139  class CleanupScope {
140    CodeGenFunction& CGF;
141    llvm::BasicBlock *CurBB;
142    llvm::BasicBlock *CleanupEntryBB;
143    llvm::BasicBlock *CleanupExitBB;
144
145  public:
146    CleanupScope(CodeGenFunction &cgf)
147      : CGF(cgf), CurBB(CGF.Builder.GetInsertBlock()),
148      CleanupEntryBB(CGF.createBasicBlock("cleanup")), CleanupExitBB(0) {
149      CGF.Builder.SetInsertPoint(CleanupEntryBB);
150    }
151
152    llvm::BasicBlock *getCleanupExitBlock() {
153      if (!CleanupExitBB)
154        CleanupExitBB = CGF.createBasicBlock("cleanup.exit");
155      return CleanupExitBB;
156    }
157
158    ~CleanupScope() {
159      CGF.PushCleanupBlock(CleanupEntryBB, CleanupExitBB);
160      // FIXME: This is silly, move this into the builder.
161      if (CurBB)
162        CGF.Builder.SetInsertPoint(CurBB);
163      else
164        CGF.Builder.ClearInsertionPoint();
165    }
166  };
167
168  /// EmitCleanupBlocks - Takes the old cleanup stack size and emits the cleanup
169  /// blocks that have been added.
170  void EmitCleanupBlocks(size_t OldCleanupStackSize);
171
172  /// EmitBranchThroughCleanup - Emit a branch from the current insert block
173  /// through the cleanup handling code (if any) and then on to \arg Dest.
174  ///
175  /// FIXME: Maybe this should really be in EmitBranch? Don't we always want
176  /// this behavior for branches?
177  void EmitBranchThroughCleanup(llvm::BasicBlock *Dest);
178
179  /// PushConditionalTempDestruction - Should be called before a conditional
180  /// part of an expression is emitted. For example, before the RHS of the
181  /// expression below is emitted:
182  ///
183  /// b && f(T());
184  ///
185  /// This is used to make sure that any temporaryes created in the conditional
186  /// branch are only destroyed if the branch is taken.
187  void PushConditionalTempDestruction();
188
189  /// PopConditionalTempDestruction - Should be called after a conditional
190  /// part of an expression has been emitted.
191  void PopConditionalTempDestruction();
192
193private:
194  CGDebugInfo *DebugInfo;
195
196  /// IndirectBranch - The first time an indirect goto is seen we create a
197  /// block with an indirect branch.  Every time we see the address of a label
198  /// taken, we add the label to the indirect goto.  Every subsequent indirect
199  /// goto is codegen'd as a jump to the IndirectBranch's basic block.
200  llvm::IndirectBrInst *IndirectBranch;
201
202  /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
203  /// decls.
204  llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
205
206  /// LabelMap - This keeps track of the LLVM basic block for each C label.
207  llvm::DenseMap<const LabelStmt*, llvm::BasicBlock*> LabelMap;
208
209  // BreakContinueStack - This keeps track of where break and continue
210  // statements should jump to.
211  struct BreakContinue {
212    BreakContinue(llvm::BasicBlock *bb, llvm::BasicBlock *cb)
213      : BreakBlock(bb), ContinueBlock(cb) {}
214
215    llvm::BasicBlock *BreakBlock;
216    llvm::BasicBlock *ContinueBlock;
217  };
218  llvm::SmallVector<BreakContinue, 8> BreakContinueStack;
219
220  /// SwitchInsn - This is nearest current switch instruction. It is null if if
221  /// current context is not in a switch.
222  llvm::SwitchInst *SwitchInsn;
223
224  /// CaseRangeBlock - This block holds if condition check for last case
225  /// statement range in current switch instruction.
226  llvm::BasicBlock *CaseRangeBlock;
227
228  /// InvokeDest - This is the nearest exception target for calls
229  /// which can unwind, when exceptions are being used.
230  llvm::BasicBlock *InvokeDest;
231
232  // VLASizeMap - This keeps track of the associated size for each VLA type.
233  // We track this by the size expression rather than the type itself because
234  // in certain situations, like a const qualifier applied to an VLA typedef,
235  // multiple VLA types can share the same size expression.
236  // FIXME: Maybe this could be a stack of maps that is pushed/popped as we
237  // enter/leave scopes.
238  llvm::DenseMap<const Expr*, llvm::Value*> VLASizeMap;
239
240  /// DidCallStackSave - Whether llvm.stacksave has been called. Used to avoid
241  /// calling llvm.stacksave for multiple VLAs in the same scope.
242  bool DidCallStackSave;
243
244  struct CleanupEntry {
245    /// CleanupEntryBlock - The block of code that does the actual cleanup.
246    llvm::BasicBlock *CleanupEntryBlock;
247
248    /// CleanupExitBlock - The cleanup exit block.
249    llvm::BasicBlock *CleanupExitBlock;
250
251    /// Blocks - Basic blocks that were emitted in the current cleanup scope.
252    std::vector<llvm::BasicBlock *> Blocks;
253
254    /// BranchFixups - Branch instructions to basic blocks that haven't been
255    /// inserted into the current function yet.
256    std::vector<llvm::BranchInst *> BranchFixups;
257
258    explicit CleanupEntry(llvm::BasicBlock *CleanupEntryBlock,
259                          llvm::BasicBlock *CleanupExitBlock)
260      : CleanupEntryBlock(CleanupEntryBlock),
261      CleanupExitBlock(CleanupExitBlock) {}
262  };
263
264  /// CleanupEntries - Stack of cleanup entries.
265  llvm::SmallVector<CleanupEntry, 8> CleanupEntries;
266
267  typedef llvm::DenseMap<llvm::BasicBlock*, size_t> BlockScopeMap;
268
269  /// BlockScopes - Map of which "cleanup scope" scope basic blocks have.
270  BlockScopeMap BlockScopes;
271
272  /// CXXThisDecl - When parsing an C++ function, this will hold the implicit
273  /// 'this' declaration.
274  ImplicitParamDecl *CXXThisDecl;
275
276  /// CXXLiveTemporaryInfo - Holds information about a live C++ temporary.
277  struct CXXLiveTemporaryInfo {
278    /// Temporary - The live temporary.
279    const CXXTemporary *Temporary;
280
281    /// ThisPtr - The pointer to the temporary.
282    llvm::Value *ThisPtr;
283
284    /// DtorBlock - The destructor block.
285    llvm::BasicBlock *DtorBlock;
286
287    /// CondPtr - If this is a conditional temporary, this is the pointer to
288    /// the condition variable that states whether the destructor should be
289    /// called or not.
290    llvm::Value *CondPtr;
291
292    CXXLiveTemporaryInfo(const CXXTemporary *temporary,
293                         llvm::Value *thisptr, llvm::BasicBlock *dtorblock,
294                         llvm::Value *condptr)
295      : Temporary(temporary), ThisPtr(thisptr), DtorBlock(dtorblock),
296      CondPtr(condptr) { }
297  };
298
299  llvm::SmallVector<CXXLiveTemporaryInfo, 4> LiveTemporaries;
300
301  /// ConditionalTempDestructionStack - Contains the number of live temporaries
302  /// when PushConditionalTempDestruction was called. This is used so that
303  /// we know how many temporaries were created by a certain expression.
304  llvm::SmallVector<size_t, 4> ConditionalTempDestructionStack;
305
306
307  /// ByrefValueInfoMap - For each __block variable, contains a pair of the LLVM
308  /// type as well as the field number that contains the actual data.
309  llvm::DenseMap<const ValueDecl *, std::pair<const llvm::Type *,
310                                              unsigned> > ByRefValueInfo;
311
312  /// getByrefValueFieldNumber - Given a declaration, returns the LLVM field
313  /// number that holds the value.
314  unsigned getByRefValueLLVMField(const ValueDecl *VD) const;
315
316public:
317  CodeGenFunction(CodeGenModule &cgm);
318
319  ASTContext &getContext() const;
320  CGDebugInfo *getDebugInfo() { return DebugInfo; }
321
322  llvm::BasicBlock *getInvokeDest() { return InvokeDest; }
323  void setInvokeDest(llvm::BasicBlock *B) { InvokeDest = B; }
324
325  llvm::LLVMContext &getLLVMContext() { return VMContext; }
326
327  //===--------------------------------------------------------------------===//
328  //                                  Objective-C
329  //===--------------------------------------------------------------------===//
330
331  void GenerateObjCMethod(const ObjCMethodDecl *OMD);
332
333  void StartObjCMethod(const ObjCMethodDecl *MD,
334                       const ObjCContainerDecl *CD);
335
336  /// GenerateObjCGetter - Synthesize an Objective-C property getter function.
337  void GenerateObjCGetter(ObjCImplementationDecl *IMP,
338                          const ObjCPropertyImplDecl *PID);
339
340  /// GenerateObjCSetter - Synthesize an Objective-C property setter function
341  /// for the given property.
342  void GenerateObjCSetter(ObjCImplementationDecl *IMP,
343                          const ObjCPropertyImplDecl *PID);
344
345  //===--------------------------------------------------------------------===//
346  //                                  Block Bits
347  //===--------------------------------------------------------------------===//
348
349  llvm::Value *BuildBlockLiteralTmp(const BlockExpr *);
350  llvm::Constant *BuildDescriptorBlockDecl(bool BlockHasCopyDispose,
351                                           uint64_t Size,
352                                           const llvm::StructType *,
353                                           std::vector<HelperInfo> *);
354
355  llvm::Function *GenerateBlockFunction(const BlockExpr *BExpr,
356                                        const BlockInfo& Info,
357                                        const Decl *OuterFuncDecl,
358                                  llvm::DenseMap<const Decl*, llvm::Value*> ldm,
359                                        uint64_t &Size, uint64_t &Align,
360                      llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
361                                        bool &subBlockHasCopyDispose);
362
363  void BlockForwardSelf();
364  llvm::Value *LoadBlockStruct();
365
366  uint64_t AllocateBlockDecl(const BlockDeclRefExpr *E);
367  llvm::Value *GetAddrOfBlockDecl(const BlockDeclRefExpr *E);
368  const llvm::Type *BuildByRefType(const ValueDecl *D);
369
370  void GenerateCode(GlobalDecl GD, llvm::Function *Fn);
371  void StartFunction(GlobalDecl GD, QualType RetTy,
372                     llvm::Function *Fn,
373                     const FunctionArgList &Args,
374                     SourceLocation StartLoc);
375
376  /// EmitReturnBlock - Emit the unified return block, trying to avoid its
377  /// emission when possible.
378  void EmitReturnBlock();
379
380  /// FinishFunction - Complete IR generation of the current function. It is
381  /// legal to call this function even if there is no current insertion point.
382  void FinishFunction(SourceLocation EndLoc=SourceLocation());
383
384  /// GenerateVtable - Generate the vtable for the given type.
385  llvm::Value *GenerateVtable(const CXXRecordDecl *RD);
386
387  /// GenerateVTT - Generate the VTT for the given type.
388  llvm::Value *GenerateVTT(const CXXRecordDecl *RD);
389
390  /// DynamicTypeAdjust - Do the non-virtual and virtual adjustments on an
391  /// object pointer to alter the dynamic type of the pointer.  Used by
392  /// GenerateCovariantThunk for building thunks.
393  llvm::Value *DynamicTypeAdjust(llvm::Value *V, int64_t nv, int64_t v);
394
395  /// GenerateThunk - Generate a thunk for the given method
396  llvm::Constant *GenerateThunk(llvm::Function *Fn, const CXXMethodDecl *MD,
397                                bool Extern, int64_t nv, int64_t v);
398  llvm::Constant *GenerateCovariantThunk(llvm::Function *Fn,
399                                         const CXXMethodDecl *MD, bool Extern,
400                                         int64_t nv_t, int64_t v_t,
401                                         int64_t nv_r, int64_t v_r);
402
403  void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type);
404
405  void SynthesizeCXXCopyConstructor(const CXXConstructorDecl *Ctor,
406                                    CXXCtorType Type,
407                                    llvm::Function *Fn,
408                                    const FunctionArgList &Args);
409
410  void SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
411                                   llvm::Function *Fn,
412                                   const FunctionArgList &Args);
413
414  void SynthesizeDefaultConstructor(const CXXConstructorDecl *Ctor,
415                                    CXXCtorType Type,
416                                    llvm::Function *Fn,
417                                    const FunctionArgList &Args);
418
419  void SynthesizeDefaultDestructor(const CXXDestructorDecl *Dtor,
420                                   CXXDtorType Type,
421                                   llvm::Function *Fn,
422                                   const FunctionArgList &Args);
423
424  /// EmitDtorEpilogue - Emit all code that comes at the end of class's
425  /// destructor. This is to call destructors on members and base classes
426  /// in reverse order of their construction.
427  void EmitDtorEpilogue(const CXXDestructorDecl *Dtor,
428                        CXXDtorType Type);
429
430  /// EmitFunctionProlog - Emit the target specific LLVM code to load the
431  /// arguments for the given function. This is also responsible for naming the
432  /// LLVM function arguments.
433  void EmitFunctionProlog(const CGFunctionInfo &FI,
434                          llvm::Function *Fn,
435                          const FunctionArgList &Args);
436
437  /// EmitFunctionEpilog - Emit the target specific LLVM code to return the
438  /// given temporary.
439  void EmitFunctionEpilog(const CGFunctionInfo &FI, llvm::Value *ReturnValue);
440
441  const llvm::Type *ConvertTypeForMem(QualType T);
442  const llvm::Type *ConvertType(QualType T);
443
444  /// LoadObjCSelf - Load the value of self. This function is only valid while
445  /// generating code for an Objective-C method.
446  llvm::Value *LoadObjCSelf();
447
448  /// TypeOfSelfObject - Return type of object that this self represents.
449  QualType TypeOfSelfObject();
450
451  /// hasAggregateLLVMType - Return true if the specified AST type will map into
452  /// an aggregate LLVM type or is void.
453  static bool hasAggregateLLVMType(QualType T);
454
455  /// createBasicBlock - Create an LLVM basic block.
456  llvm::BasicBlock *createBasicBlock(const char *Name="",
457                                     llvm::Function *Parent=0,
458                                     llvm::BasicBlock *InsertBefore=0) {
459#ifdef NDEBUG
460    return llvm::BasicBlock::Create(VMContext, "", Parent, InsertBefore);
461#else
462    return llvm::BasicBlock::Create(VMContext, Name, Parent, InsertBefore);
463#endif
464  }
465
466  /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
467  /// label maps to.
468  llvm::BasicBlock *getBasicBlockForLabel(const LabelStmt *S);
469
470  /// SimplifyForwardingBlocks - If the given basic block is only a
471  /// branch to another basic block, simplify it. This assumes that no
472  /// other code could potentially reference the basic block.
473  void SimplifyForwardingBlocks(llvm::BasicBlock *BB);
474
475  /// EmitBlock - Emit the given block \arg BB and set it as the insert point,
476  /// adding a fall-through branch from the current insert block if
477  /// necessary. It is legal to call this function even if there is no current
478  /// insertion point.
479  ///
480  /// IsFinished - If true, indicates that the caller has finished emitting
481  /// branches to the given block and does not expect to emit code into it. This
482  /// means the block can be ignored if it is unreachable.
483  void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false);
484
485  /// EmitBranch - Emit a branch to the specified basic block from the current
486  /// insert block, taking care to avoid creation of branches from dummy
487  /// blocks. It is legal to call this function even if there is no current
488  /// insertion point.
489  ///
490  /// This function clears the current insertion point. The caller should follow
491  /// calls to this function with calls to Emit*Block prior to generation new
492  /// code.
493  void EmitBranch(llvm::BasicBlock *Block);
494
495  /// HaveInsertPoint - True if an insertion point is defined. If not, this
496  /// indicates that the current code being emitted is unreachable.
497  bool HaveInsertPoint() const {
498    return Builder.GetInsertBlock() != 0;
499  }
500
501  /// EnsureInsertPoint - Ensure that an insertion point is defined so that
502  /// emitted IR has a place to go. Note that by definition, if this function
503  /// creates a block then that block is unreachable; callers may do better to
504  /// detect when no insertion point is defined and simply skip IR generation.
505  void EnsureInsertPoint() {
506    if (!HaveInsertPoint())
507      EmitBlock(createBasicBlock());
508  }
509
510  /// ErrorUnsupported - Print out an error that codegen doesn't support the
511  /// specified stmt yet.
512  void ErrorUnsupported(const Stmt *S, const char *Type,
513                        bool OmitOnError=false);
514
515  //===--------------------------------------------------------------------===//
516  //                                  Helpers
517  //===--------------------------------------------------------------------===//
518
519  Qualifiers MakeQualifiers(QualType T) {
520    Qualifiers Quals = getContext().getCanonicalType(T).getQualifiers();
521    Quals.setObjCGCAttr(getContext().getObjCGCAttrKind(T));
522    return Quals;
523  }
524
525  /// CreateTempAlloca - This creates a alloca and inserts it into the entry
526  /// block.
527  llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty,
528                                     const llvm::Twine &Name = "tmp");
529
530  /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
531  /// expression and compare the result against zero, returning an Int1Ty value.
532  llvm::Value *EvaluateExprAsBool(const Expr *E);
533
534  /// EmitAnyExpr - Emit code to compute the specified expression which can have
535  /// any type.  The result is returned as an RValue struct.  If this is an
536  /// aggregate expression, the aggloc/agglocvolatile arguments indicate where
537  /// the result should be returned.
538  ///
539  /// \param IgnoreResult - True if the resulting value isn't used.
540  RValue EmitAnyExpr(const Expr *E, llvm::Value *AggLoc = 0,
541                     bool IsAggLocVolatile = false, bool IgnoreResult = false,
542                     bool IsInitializer = false);
543
544  // EmitVAListRef - Emit a "reference" to a va_list; this is either the address
545  // or the value of the expression, depending on how va_list is defined.
546  llvm::Value *EmitVAListRef(const Expr *E);
547
548  /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
549  /// always be accessible even if no aggregate location is provided.
550  RValue EmitAnyExprToTemp(const Expr *E, bool IsAggLocVolatile = false,
551                           bool IsInitializer = false);
552
553  /// EmitAggregateCopy - Emit an aggrate copy.
554  ///
555  /// \param isVolatile - True iff either the source or the destination is
556  /// volatile.
557  void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
558                         QualType EltTy, bool isVolatile=false);
559
560  void EmitAggregateClear(llvm::Value *DestPtr, QualType Ty);
561
562  /// StartBlock - Start new block named N. If insert block is a dummy block
563  /// then reuse it.
564  void StartBlock(const char *N);
565
566  /// GetAddrOfStaticLocalVar - Return the address of a static local variable.
567  llvm::Constant *GetAddrOfStaticLocalVar(const VarDecl *BVD);
568
569  /// GetAddrOfLocalVar - Return the address of a local variable.
570  llvm::Value *GetAddrOfLocalVar(const VarDecl *VD);
571
572  /// getAccessedFieldNo - Given an encoded value and a result number, return
573  /// the input field number being accessed.
574  static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts);
575
576  llvm::BlockAddress *GetAddrOfLabel(const LabelStmt *L);
577  llvm::BasicBlock *GetIndirectGotoBlock();
578
579  /// EmitMemSetToZero - Generate code to memset a value of the given type to 0.
580  void EmitMemSetToZero(llvm::Value *DestPtr, QualType Ty);
581
582  // EmitVAArg - Generate code to get an argument from the passed in pointer
583  // and update it accordingly. The return value is a pointer to the argument.
584  // FIXME: We should be able to get rid of this method and use the va_arg
585  // instruction in LLVM instead once it works well enough.
586  llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty);
587
588  // EmitVLASize - Generate code for any VLA size expressions that might occur
589  // in a variably modified type. If Ty is a VLA, will return the value that
590  // corresponds to the size in bytes of the VLA type. Will return 0 otherwise.
591  ///
592  /// This function can be called with a null (unreachable) insert point.
593  llvm::Value *EmitVLASize(QualType Ty);
594
595  // GetVLASize - Returns an LLVM value that corresponds to the size in bytes
596  // of a variable length array type.
597  llvm::Value *GetVLASize(const VariableArrayType *);
598
599  /// LoadCXXThis - Load the value of 'this'. This function is only valid while
600  /// generating code for an C++ member function.
601  llvm::Value *LoadCXXThis();
602
603  /// GetAddressCXXOfBaseClass - This function will add the necessary delta
604  /// to the load of 'this' and returns address of the base class.
605  // FIXME. This currently only does a derived to non-virtual base conversion.
606  // Other kinds of conversions will come later.
607  llvm::Value *GetAddressCXXOfBaseClass(llvm::Value *BaseValue,
608                                        const CXXRecordDecl *ClassDecl,
609                                        const CXXRecordDecl *BaseClassDecl,
610                                        bool NullCheckValue);
611
612  llvm::Value *
613  GetVirtualCXXBaseClassOffset(llvm::Value *This,
614                               const CXXRecordDecl *ClassDecl,
615                               const CXXRecordDecl *BaseClassDecl);
616
617  void EmitClassAggrMemberwiseCopy(llvm::Value *DestValue,
618                                   llvm::Value *SrcValue,
619                                   const ArrayType *Array,
620                                   const CXXRecordDecl *BaseClassDecl,
621                                   QualType Ty);
622
623  void EmitClassAggrCopyAssignment(llvm::Value *DestValue,
624                                   llvm::Value *SrcValue,
625                                   const ArrayType *Array,
626                                   const CXXRecordDecl *BaseClassDecl,
627                                   QualType Ty);
628
629  void EmitClassMemberwiseCopy(llvm::Value *DestValue, llvm::Value *SrcValue,
630                               const CXXRecordDecl *ClassDecl,
631                               const CXXRecordDecl *BaseClassDecl,
632                               QualType Ty);
633
634  void EmitClassCopyAssignment(llvm::Value *DestValue, llvm::Value *SrcValue,
635                               const CXXRecordDecl *ClassDecl,
636                               const CXXRecordDecl *BaseClassDecl,
637                               QualType Ty);
638
639  void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type,
640                              llvm::Value *This,
641                              CallExpr::const_arg_iterator ArgBeg,
642                              CallExpr::const_arg_iterator ArgEnd);
643
644  void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
645                                  const ConstantArrayType *ArrayTy,
646                                  llvm::Value *ArrayPtr);
647  void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
648                                  llvm::Value *NumElements,
649                                  llvm::Value *ArrayPtr);
650
651  void EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
652                                 const ArrayType *Array,
653                                 llvm::Value *This);
654
655  void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type,
656                             llvm::Value *This);
657
658  void PushCXXTemporary(const CXXTemporary *Temporary, llvm::Value *Ptr);
659  void PopCXXTemporary();
660
661  llvm::Value *EmitCXXNewExpr(const CXXNewExpr *E);
662  void EmitCXXDeleteExpr(const CXXDeleteExpr *E);
663
664  //===--------------------------------------------------------------------===//
665  //                            Declaration Emission
666  //===--------------------------------------------------------------------===//
667
668  /// EmitDecl - Emit a declaration.
669  ///
670  /// This function can be called with a null (unreachable) insert point.
671  void EmitDecl(const Decl &D);
672
673  /// EmitBlockVarDecl - Emit a block variable declaration.
674  ///
675  /// This function can be called with a null (unreachable) insert point.
676  void EmitBlockVarDecl(const VarDecl &D);
677
678  /// EmitLocalBlockVarDecl - Emit a local block variable declaration.
679  ///
680  /// This function can be called with a null (unreachable) insert point.
681  void EmitLocalBlockVarDecl(const VarDecl &D);
682
683  void EmitStaticBlockVarDecl(const VarDecl &D);
684
685  /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
686  void EmitParmDecl(const VarDecl &D, llvm::Value *Arg);
687
688  //===--------------------------------------------------------------------===//
689  //                             Statement Emission
690  //===--------------------------------------------------------------------===//
691
692  /// EmitStopPoint - Emit a debug stoppoint if we are emitting debug info.
693  void EmitStopPoint(const Stmt *S);
694
695  /// EmitStmt - Emit the code for the statement \arg S. It is legal to call
696  /// this function even if there is no current insertion point.
697  ///
698  /// This function may clear the current insertion point; callers should use
699  /// EnsureInsertPoint if they wish to subsequently generate code without first
700  /// calling EmitBlock, EmitBranch, or EmitStmt.
701  void EmitStmt(const Stmt *S);
702
703  /// EmitSimpleStmt - Try to emit a "simple" statement which does not
704  /// necessarily require an insertion point or debug information; typically
705  /// because the statement amounts to a jump or a container of other
706  /// statements.
707  ///
708  /// \return True if the statement was handled.
709  bool EmitSimpleStmt(const Stmt *S);
710
711  RValue EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false,
712                          llvm::Value *AggLoc = 0, bool isAggVol = false);
713
714  /// EmitLabel - Emit the block for the given label. It is legal to call this
715  /// function even if there is no current insertion point.
716  void EmitLabel(const LabelStmt &S); // helper for EmitLabelStmt.
717
718  void EmitLabelStmt(const LabelStmt &S);
719  void EmitGotoStmt(const GotoStmt &S);
720  void EmitIndirectGotoStmt(const IndirectGotoStmt &S);
721  void EmitIfStmt(const IfStmt &S);
722  void EmitWhileStmt(const WhileStmt &S);
723  void EmitDoStmt(const DoStmt &S);
724  void EmitForStmt(const ForStmt &S);
725  void EmitReturnStmt(const ReturnStmt &S);
726  void EmitDeclStmt(const DeclStmt &S);
727  void EmitBreakStmt(const BreakStmt &S);
728  void EmitContinueStmt(const ContinueStmt &S);
729  void EmitSwitchStmt(const SwitchStmt &S);
730  void EmitDefaultStmt(const DefaultStmt &S);
731  void EmitCaseStmt(const CaseStmt &S);
732  void EmitCaseStmtRange(const CaseStmt &S);
733  void EmitAsmStmt(const AsmStmt &S);
734
735  void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S);
736  void EmitObjCAtTryStmt(const ObjCAtTryStmt &S);
737  void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S);
738  void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S);
739
740  void EmitCXXTryStmt(const CXXTryStmt &S);
741
742  //===--------------------------------------------------------------------===//
743  //                         LValue Expression Emission
744  //===--------------------------------------------------------------------===//
745
746  /// GetUndefRValue - Get an appropriate 'undef' rvalue for the given type.
747  RValue GetUndefRValue(QualType Ty);
748
749  /// EmitUnsupportedRValue - Emit a dummy r-value using the type of E
750  /// and issue an ErrorUnsupported style diagnostic (using the
751  /// provided Name).
752  RValue EmitUnsupportedRValue(const Expr *E,
753                               const char *Name);
754
755  /// EmitUnsupportedLValue - Emit a dummy l-value using the type of E and issue
756  /// an ErrorUnsupported style diagnostic (using the provided Name).
757  LValue EmitUnsupportedLValue(const Expr *E,
758                               const char *Name);
759
760  /// EmitLValue - Emit code to compute a designator that specifies the location
761  /// of the expression.
762  ///
763  /// This can return one of two things: a simple address or a bitfield
764  /// reference.  In either case, the LLVM Value* in the LValue structure is
765  /// guaranteed to be an LLVM pointer type.
766  ///
767  /// If this returns a bitfield reference, nothing about the pointee type of
768  /// the LLVM value is known: For example, it may not be a pointer to an
769  /// integer.
770  ///
771  /// If this returns a normal address, and if the lvalue's C type is fixed
772  /// size, this method guarantees that the returned pointer type will point to
773  /// an LLVM type of the same size of the lvalue's type.  If the lvalue has a
774  /// variable length type, this is not possible.
775  ///
776  LValue EmitLValue(const Expr *E);
777
778  /// EmitLoadOfScalar - Load a scalar value from an address, taking
779  /// care to appropriately convert from the memory representation to
780  /// the LLVM value representation.
781  llvm::Value *EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
782                                QualType Ty);
783
784  /// EmitStoreOfScalar - Store a scalar value to an address, taking
785  /// care to appropriately convert from the memory representation to
786  /// the LLVM value representation.
787  void EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
788                         bool Volatile, QualType Ty);
789
790  /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
791  /// this method emits the address of the lvalue, then loads the result as an
792  /// rvalue, returning the rvalue.
793  RValue EmitLoadOfLValue(LValue V, QualType LVType);
794  RValue EmitLoadOfExtVectorElementLValue(LValue V, QualType LVType);
795  RValue EmitLoadOfBitfieldLValue(LValue LV, QualType ExprType);
796  RValue EmitLoadOfPropertyRefLValue(LValue LV, QualType ExprType);
797  RValue EmitLoadOfKVCRefLValue(LValue LV, QualType ExprType);
798
799
800  /// EmitStoreThroughLValue - Store the specified rvalue into the specified
801  /// lvalue, where both are guaranteed to the have the same type, and that type
802  /// is 'Ty'.
803  void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
804  void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst,
805                                                QualType Ty);
806  void EmitStoreThroughPropertyRefLValue(RValue Src, LValue Dst, QualType Ty);
807  void EmitStoreThroughKVCRefLValue(RValue Src, LValue Dst, QualType Ty);
808
809  /// EmitStoreThroughLValue - Store Src into Dst with same constraints as
810  /// EmitStoreThroughLValue.
811  ///
812  /// \param Result [out] - If non-null, this will be set to a Value* for the
813  /// bit-field contents after the store, appropriate for use as the result of
814  /// an assignment to the bit-field.
815  void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, QualType Ty,
816                                      llvm::Value **Result=0);
817
818  // Note: only availabe for agg return types
819  LValue EmitBinaryOperatorLValue(const BinaryOperator *E);
820  // Note: only available for agg return types
821  LValue EmitCallExprLValue(const CallExpr *E);
822  // Note: only available for agg return types
823  LValue EmitVAArgExprLValue(const VAArgExpr *E);
824  LValue EmitDeclRefLValue(const DeclRefExpr *E);
825  LValue EmitStringLiteralLValue(const StringLiteral *E);
826  LValue EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E);
827  LValue EmitPredefinedFunctionName(unsigned Type);
828  LValue EmitPredefinedLValue(const PredefinedExpr *E);
829  LValue EmitUnaryOpLValue(const UnaryOperator *E);
830  LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
831  LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E);
832  LValue EmitMemberExpr(const MemberExpr *E);
833  LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E);
834  LValue EmitConditionalOperatorLValue(const ConditionalOperator *E);
835  LValue EmitCastLValue(const CastExpr *E);
836  LValue EmitNullInitializationLValue(const CXXZeroInitValueExpr *E);
837
838  LValue EmitPointerToDataMemberLValue(const FieldDecl *Field);
839
840  llvm::Value *EmitIvarOffset(const ObjCInterfaceDecl *Interface,
841                              const ObjCIvarDecl *Ivar);
842  LValue EmitLValueForField(llvm::Value* Base, FieldDecl* Field,
843                            bool isUnion, unsigned CVRQualifiers);
844  LValue EmitLValueForIvar(QualType ObjectTy,
845                           llvm::Value* Base, const ObjCIvarDecl *Ivar,
846                           unsigned CVRQualifiers);
847
848  LValue EmitLValueForBitfield(llvm::Value* Base, FieldDecl* Field,
849                                unsigned CVRQualifiers);
850
851  LValue EmitBlockDeclRefLValue(const BlockDeclRefExpr *E);
852
853  LValue EmitCXXConditionDeclLValue(const CXXConditionDeclExpr *E);
854  LValue EmitCXXConstructLValue(const CXXConstructExpr *E);
855  LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E);
856  LValue EmitCXXExprWithTemporariesLValue(const CXXExprWithTemporaries *E);
857
858  LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E);
859  LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E);
860  LValue EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E);
861  LValue EmitObjCKVCRefLValue(const ObjCImplicitSetterGetterRefExpr *E);
862  LValue EmitObjCSuperExprLValue(const ObjCSuperExpr *E);
863  LValue EmitStmtExprLValue(const StmtExpr *E);
864  LValue EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E);
865
866  //===--------------------------------------------------------------------===//
867  //                         Scalar Expression Emission
868  //===--------------------------------------------------------------------===//
869
870  /// EmitCall - Generate a call of the given function, expecting the given
871  /// result type, and using the given argument list which specifies both the
872  /// LLVM arguments and the types they were derived from.
873  ///
874  /// \param TargetDecl - If given, the decl of the function in a
875  /// direct call; used to set attributes on the call (noreturn,
876  /// etc.).
877  RValue EmitCall(const CGFunctionInfo &FnInfo,
878                  llvm::Value *Callee,
879                  const CallArgList &Args,
880                  const Decl *TargetDecl = 0);
881
882  RValue EmitCall(llvm::Value *Callee, QualType FnType,
883                  CallExpr::const_arg_iterator ArgBeg,
884                  CallExpr::const_arg_iterator ArgEnd,
885                  const Decl *TargetDecl = 0);
886  RValue EmitCallExpr(const CallExpr *E);
887
888  llvm::Value *BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *&This,
889                                const llvm::Type *Ty);
890  RValue EmitCXXMemberCall(const CXXMethodDecl *MD,
891                           llvm::Value *Callee,
892                           llvm::Value *This,
893                           CallExpr::const_arg_iterator ArgBeg,
894                           CallExpr::const_arg_iterator ArgEnd);
895  RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E);
896  RValue EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E);
897
898  RValue EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
899                                       const CXXMethodDecl *MD);
900
901
902  RValue EmitBuiltinExpr(const FunctionDecl *FD,
903                         unsigned BuiltinID, const CallExpr *E);
904
905  RValue EmitBlockCallExpr(const CallExpr *E);
906
907  /// EmitTargetBuiltinExpr - Emit the given builtin call. Returns 0 if the call
908  /// is unhandled by the current target.
909  llvm::Value *EmitTargetBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
910
911  llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
912  llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
913
914  llvm::Value *EmitShuffleVector(llvm::Value* V1, llvm::Value *V2, ...);
915  llvm::Value *EmitVector(llvm::Value * const *Vals, unsigned NumVals,
916                          bool isSplat = false);
917
918  llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E);
919  llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E);
920  llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E);
921  RValue EmitObjCMessageExpr(const ObjCMessageExpr *E);
922  RValue EmitObjCPropertyGet(const Expr *E);
923  RValue EmitObjCSuperPropertyGet(const Expr *Exp, const Selector &S);
924  void EmitObjCPropertySet(const Expr *E, RValue Src);
925  void EmitObjCSuperPropertySet(const Expr *E, const Selector &S, RValue Src);
926
927
928  /// EmitReferenceBindingToExpr - Emits a reference binding to the passed in
929  /// expression. Will emit a temporary variable if E is not an LValue.
930  RValue EmitReferenceBindingToExpr(const Expr* E, QualType DestType,
931                                    bool IsInitializer = false);
932
933  //===--------------------------------------------------------------------===//
934  //                           Expression Emission
935  //===--------------------------------------------------------------------===//
936
937  // Expressions are broken into three classes: scalar, complex, aggregate.
938
939  /// EmitScalarExpr - Emit the computation of the specified expression of LLVM
940  /// scalar type, returning the result.
941  llvm::Value *EmitScalarExpr(const Expr *E , bool IgnoreResultAssign = false);
942
943  /// EmitScalarConversion - Emit a conversion from the specified type to the
944  /// specified destination type, both of which are LLVM scalar types.
945  llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy,
946                                    QualType DstTy);
947
948  /// EmitComplexToScalarConversion - Emit a conversion from the specified
949  /// complex type to the specified destination type, where the destination type
950  /// is an LLVM scalar type.
951  llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy,
952                                             QualType DstTy);
953
954
955  /// EmitAggExpr - Emit the computation of the specified expression of
956  /// aggregate type.  The result is computed into DestPtr.  Note that if
957  /// DestPtr is null, the value of the aggregate expression is not needed.
958  void EmitAggExpr(const Expr *E, llvm::Value *DestPtr, bool VolatileDest,
959                   bool IgnoreResult = false, bool IsInitializer = false,
960                   bool RequiresGCollection = false);
961
962  /// EmitGCMemmoveCollectable - Emit special API for structs with object
963  /// pointers.
964  void EmitGCMemmoveCollectable(llvm::Value *DestPtr, llvm::Value *SrcPtr,
965                                QualType Ty);
966
967  /// EmitComplexExpr - Emit the computation of the specified expression of
968  /// complex type, returning the result.
969  ComplexPairTy EmitComplexExpr(const Expr *E, bool IgnoreReal = false,
970                                bool IgnoreImag = false,
971                                bool IgnoreRealAssign = false,
972                                bool IgnoreImagAssign = false);
973
974  /// EmitComplexExprIntoAddr - Emit the computation of the specified expression
975  /// of complex type, storing into the specified Value*.
976  void EmitComplexExprIntoAddr(const Expr *E, llvm::Value *DestAddr,
977                               bool DestIsVolatile);
978
979  /// StoreComplexToAddr - Store a complex number into the specified address.
980  void StoreComplexToAddr(ComplexPairTy V, llvm::Value *DestAddr,
981                          bool DestIsVolatile);
982  /// LoadComplexFromAddr - Load a complex number from the specified address.
983  ComplexPairTy LoadComplexFromAddr(llvm::Value *SrcAddr, bool SrcIsVolatile);
984
985  /// CreateStaticBlockVarDecl - Create a zero-initialized LLVM global
986  /// for a static block var decl.
987  llvm::GlobalVariable * CreateStaticBlockVarDecl(const VarDecl &D,
988                                                  const char *Separator,
989                                                  llvm::GlobalValue::LinkageTypes
990                                                  Linkage);
991
992  /// EmitStaticCXXBlockVarDeclInit - Create the initializer for a C++
993  /// runtime initialized static block var decl.
994  void EmitStaticCXXBlockVarDeclInit(const VarDecl &D,
995                                     llvm::GlobalVariable *GV);
996
997  /// EmitCXXGlobalVarDeclInit - Create the initializer for a C++
998  /// variable with global storage.
999  void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::Constant *DeclPtr);
1000
1001  /// EmitCXXGlobalDtorRegistration - Emits a call to register the global ptr
1002  /// with the C++ runtime so that its destructor will be called at exit.
1003  void EmitCXXGlobalDtorRegistration(const CXXDestructorDecl *Dtor,
1004                                     llvm::Constant *DeclPtr);
1005
1006  /// GenerateCXXGlobalInitFunc - Generates code for initializing global
1007  /// variables.
1008  void GenerateCXXGlobalInitFunc(llvm::Function *Fn,
1009                                 const VarDecl **Decls,
1010                                 unsigned NumDecls);
1011
1012  void EmitCXXConstructExpr(llvm::Value *Dest, const CXXConstructExpr *E);
1013
1014  RValue EmitCXXExprWithTemporaries(const CXXExprWithTemporaries *E,
1015                                    llvm::Value *AggLoc = 0,
1016                                    bool IsAggLocVolatile = false,
1017                                    bool IsInitializer = false);
1018
1019  void EmitCXXThrowExpr(const CXXThrowExpr *E);
1020
1021  //===--------------------------------------------------------------------===//
1022  //                             Internal Helpers
1023  //===--------------------------------------------------------------------===//
1024
1025  /// ContainsLabel - Return true if the statement contains a label in it.  If
1026  /// this statement is not executed normally, it not containing a label means
1027  /// that we can just remove the code.
1028  static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts = false);
1029
1030  /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
1031  /// to a constant, or if it does but contains a label, return 0.  If it
1032  /// constant folds to 'true' and does not contain a label, return 1, if it
1033  /// constant folds to 'false' and does not contain a label, return -1.
1034  int ConstantFoldsToSimpleInteger(const Expr *Cond);
1035
1036  /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an
1037  /// if statement) to the specified blocks.  Based on the condition, this might
1038  /// try to simplify the codegen of the conditional based on the branch.
1039  void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock,
1040                            llvm::BasicBlock *FalseBlock);
1041private:
1042
1043  void EmitReturnOfRValue(RValue RV, QualType Ty);
1044
1045  /// ExpandTypeFromArgs - Reconstruct a structure of type \arg Ty
1046  /// from function arguments into \arg Dst. See ABIArgInfo::Expand.
1047  ///
1048  /// \param AI - The first function argument of the expansion.
1049  /// \return The argument following the last expanded function
1050  /// argument.
1051  llvm::Function::arg_iterator
1052  ExpandTypeFromArgs(QualType Ty, LValue Dst,
1053                     llvm::Function::arg_iterator AI);
1054
1055  /// ExpandTypeToArgs - Expand an RValue \arg Src, with the LLVM type for \arg
1056  /// Ty, into individual arguments on the provided vector \arg Args. See
1057  /// ABIArgInfo::Expand.
1058  void ExpandTypeToArgs(QualType Ty, RValue Src,
1059                        llvm::SmallVector<llvm::Value*, 16> &Args);
1060
1061  llvm::Value* EmitAsmInput(const AsmStmt &S,
1062                            const TargetInfo::ConstraintInfo &Info,
1063                            const Expr *InputExpr, std::string &ConstraintStr);
1064
1065  /// EmitCleanupBlock - emits a single cleanup block.
1066  void EmitCleanupBlock();
1067
1068  /// AddBranchFixup - adds a branch instruction to the list of fixups for the
1069  /// current cleanup scope.
1070  void AddBranchFixup(llvm::BranchInst *BI);
1071
1072  /// EmitCallArg - Emit a single call argument.
1073  RValue EmitCallArg(const Expr *E, QualType ArgType);
1074
1075  /// EmitCallArgs - Emit call arguments for a function.
1076  /// The CallArgTypeInfo parameter is used for iterating over the known
1077  /// argument types of the function being called.
1078  template<typename T>
1079  void EmitCallArgs(CallArgList& Args, const T* CallArgTypeInfo,
1080                    CallExpr::const_arg_iterator ArgBeg,
1081                    CallExpr::const_arg_iterator ArgEnd) {
1082      CallExpr::const_arg_iterator Arg = ArgBeg;
1083
1084    // First, use the argument types that the type info knows about
1085    if (CallArgTypeInfo) {
1086      for (typename T::arg_type_iterator I = CallArgTypeInfo->arg_type_begin(),
1087           E = CallArgTypeInfo->arg_type_end(); I != E; ++I, ++Arg) {
1088        QualType ArgType = *I;
1089
1090        assert(getContext().getCanonicalType(ArgType.getNonReferenceType()).
1091               getTypePtr() ==
1092               getContext().getCanonicalType(Arg->getType()).getTypePtr() &&
1093               "type mismatch in call argument!");
1094
1095        Args.push_back(std::make_pair(EmitCallArg(*Arg, ArgType),
1096                                      ArgType));
1097      }
1098
1099      // Either we've emitted all the call args, or we have a call to a
1100      // variadic function.
1101      assert((Arg == ArgEnd || CallArgTypeInfo->isVariadic()) &&
1102             "Extra arguments in non-variadic function!");
1103
1104    }
1105
1106    // If we still have any arguments, emit them using the type of the argument.
1107    for (; Arg != ArgEnd; ++Arg) {
1108      QualType ArgType = Arg->getType();
1109      Args.push_back(std::make_pair(EmitCallArg(*Arg, ArgType),
1110                                    ArgType));
1111    }
1112  }
1113};
1114
1115
1116}  // end namespace CodeGen
1117}  // end namespace clang
1118
1119#endif
1120