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