blob: 42864ceb633a233da6e2bdad67f770d3af8917af [file] [log] [blame]
Daniel Dunbarbd012ff2008-07-29 23:18:29 +00001//===-- CodeGenFunction.h - Per-Function state for LLVM CodeGen -*- C++ -*-===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Mike Stump0dd9e882009-02-08 23:14:22 +000010// This is the internal per-function state used for llvm translation.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattneref52a2f2008-02-29 17:10:38 +000014#ifndef CLANG_CODEGEN_CODEGENFUNCTION_H
15#define CLANG_CODEGEN_CODEGENFUNCTION_H
Reid Spencer5f016e22007-07-11 17:01:13 +000016
Chris Lattner391d77a2008-03-30 23:03:07 +000017#include "clang/AST/Type.h"
Argyrios Kyrtzidise3a09e62008-09-10 02:36:38 +000018#include "clang/AST/ExprCXX.h"
Ted Kremenek55499762008-06-17 02:43:46 +000019#include "clang/AST/ExprObjC.h"
Ken Dyck199c3d62010-01-11 17:06:35 +000020#include "clang/AST/CharUnits.h"
John McCallf85e1932011-06-15 23:02:42 +000021#include "clang/Frontend/CodeGenOptions.h"
Peter Collingbourne14110472011-01-13 18:57:25 +000022#include "clang/Basic/ABI.h"
Chris Lattner481769b2009-03-31 22:17:44 +000023#include "clang/Basic/TargetInfo.h"
Jay Foad4c7d9f12011-07-15 08:37:34 +000024#include "llvm/ADT/ArrayRef.h"
Chris Lattner481769b2009-03-31 22:17:44 +000025#include "llvm/ADT/DenseMap.h"
26#include "llvm/ADT/SmallVector.h"
27#include "llvm/Support/ValueHandle.h"
Owen Anderson69243822009-07-13 04:10:07 +000028#include "CodeGenModule.h"
Daniel Dunbar45d196b2008-11-01 01:53:16 +000029#include "CGBuilder.h"
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000030#include "CGValue.h"
31
Reid Spencer5f016e22007-07-11 17:01:13 +000032namespace llvm {
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000033 class BasicBlock;
Benjamin Kramerf21efe92009-08-11 17:46:57 +000034 class LLVMContext;
David Chisnalldd5c98f2010-05-01 11:15:56 +000035 class MDNode;
Reid Spencer5f016e22007-07-11 17:01:13 +000036 class Module;
Daniel Dunbar898d5082008-09-30 01:06:03 +000037 class SwitchInst;
Daniel Dunbar259e9cc2009-10-19 01:21:05 +000038 class Twine;
Daniel Dunbared3849b2008-11-19 09:36:46 +000039 class Value;
John McCallf1549f62010-07-06 01:34:17 +000040 class CallSite;
Reid Spencer5f016e22007-07-11 17:01:13 +000041}
42
43namespace clang {
Devang Patel8d308382010-08-10 07:24:25 +000044 class APValue;
Reid Spencer5f016e22007-07-11 17:01:13 +000045 class ASTContext;
Anders Carlsson7267c162009-05-29 21:03:38 +000046 class CXXDestructorDecl;
Richard Smithad762fc2011-04-14 22:09:26 +000047 class CXXForRangeStmt;
Anders Carlsson6815e942009-09-27 18:58:34 +000048 class CXXTryStmt;
Reid Spencer5f016e22007-07-11 17:01:13 +000049 class Decl;
Chris Lattnerad8dcf42011-02-17 07:39:24 +000050 class LabelDecl;
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000051 class EnumConstantDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +000052 class FunctionDecl;
Douglas Gregor72564e72009-02-26 23:50:07 +000053 class FunctionProtoType;
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000054 class LabelStmt;
Fariborz Jahanian679a5022009-01-10 21:06:09 +000055 class ObjCContainerDecl;
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +000056 class ObjCInterfaceDecl;
57 class ObjCIvarDecl;
Chris Lattner391d77a2008-03-30 23:03:07 +000058 class ObjCMethodDecl;
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000059 class ObjCImplementationDecl;
Daniel Dunbaraf05bb92008-08-26 08:29:31 +000060 class ObjCPropertyImplDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +000061 class TargetInfo;
John McCall492c4f92010-03-03 04:15:11 +000062 class TargetCodeGenInfo;
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000063 class VarDecl;
Chris Lattner16f00492009-04-26 01:32:48 +000064 class ObjCForCollectionStmt;
65 class ObjCAtTryStmt;
66 class ObjCAtThrowStmt;
67 class ObjCAtSynchronizedStmt;
John McCallf85e1932011-06-15 23:02:42 +000068 class ObjCAutoreleasePoolStmt;
Devang Patelb84a06e2007-10-23 02:10:49 +000069
Reid Spencer5f016e22007-07-11 17:01:13 +000070namespace CodeGen {
Devang Patelb84a06e2007-10-23 02:10:49 +000071 class CodeGenTypes;
Anders Carlssone896d982009-02-13 08:11:52 +000072 class CGDebugInfo;
Daniel Dunbarbb36d332009-02-02 21:43:58 +000073 class CGFunctionInfo;
Mike Stump0dd9e882009-02-08 23:14:22 +000074 class CGRecordLayout;
John McCallee504292010-05-21 04:11:14 +000075 class CGBlockInfo;
John McCall4c40d982010-08-31 07:33:07 +000076 class CGCXXABI;
John McCalld16c2cf2011-02-08 08:22:06 +000077 class BlockFlags;
78 class BlockFieldFlags;
Mike Stump0dd9e882009-02-08 23:14:22 +000079
John McCallf1549f62010-07-06 01:34:17 +000080/// A branch fixup. These are required when emitting a goto to a
81/// label which hasn't been emitted yet. The goto is optimistically
82/// emitted as a branch to the basic block for the label, and (if it
83/// occurs in a scope with non-trivial cleanups) a fixup is added to
84/// the innermost cleanup. When a (normal) cleanup is popped, any
85/// unresolved fixups in that scope are threaded through the cleanup.
86struct BranchFixup {
John McCallff8e1152010-07-23 21:56:41 +000087 /// The block containing the terminator which needs to be modified
88 /// into a switch if this fixup is resolved into the current scope.
89 /// If null, LatestBranch points directly to the destination.
90 llvm::BasicBlock *OptimisticBranchBlock;
John McCallf1549f62010-07-06 01:34:17 +000091
John McCallff8e1152010-07-23 21:56:41 +000092 /// The ultimate destination of the branch.
John McCallf1549f62010-07-06 01:34:17 +000093 ///
94 /// This can be set to null to indicate that this fixup was
95 /// successfully resolved.
96 llvm::BasicBlock *Destination;
97
John McCallff8e1152010-07-23 21:56:41 +000098 /// The destination index value.
99 unsigned DestinationIndex;
100
101 /// The initial branch of the fixup.
102 llvm::BranchInst *InitialBranch;
John McCallf1549f62010-07-06 01:34:17 +0000103};
104
John McCall804b8072011-01-28 10:53:53 +0000105template <class T> struct InvariantValue {
John McCall150b4622011-01-26 04:00:11 +0000106 typedef T type;
107 typedef T saved_type;
108 static bool needsSaving(type value) { return false; }
109 static saved_type save(CodeGenFunction &CGF, type value) { return value; }
110 static type restore(CodeGenFunction &CGF, saved_type value) { return value; }
111};
John McCall804b8072011-01-28 10:53:53 +0000112
113/// A metaprogramming class for ensuring that a value will dominate an
114/// arbitrary position in a function.
115template <class T> struct DominatingValue : InvariantValue<T> {};
116
117template <class T, bool mightBeInstruction =
118 llvm::is_base_of<llvm::Value, T>::value &&
119 !llvm::is_base_of<llvm::Constant, T>::value &&
120 !llvm::is_base_of<llvm::BasicBlock, T>::value>
121struct DominatingPointer;
122template <class T> struct DominatingPointer<T,false> : InvariantValue<T*> {};
123// template <class T> struct DominatingPointer<T,true> at end of file
124
125template <class T> struct DominatingValue<T*> : DominatingPointer<T> {};
John McCall150b4622011-01-26 04:00:11 +0000126
John McCallcd2d2b72010-08-13 21:20:51 +0000127enum CleanupKind {
128 EHCleanup = 0x1,
129 NormalCleanup = 0x2,
130 NormalAndEHCleanup = EHCleanup | NormalCleanup,
131
132 InactiveCleanup = 0x4,
133 InactiveEHCleanup = EHCleanup | InactiveCleanup,
134 InactiveNormalCleanup = NormalCleanup | InactiveCleanup,
135 InactiveNormalAndEHCleanup = NormalAndEHCleanup | InactiveCleanup
136};
John McCallda65ea82010-07-13 20:32:21 +0000137
John McCallf1549f62010-07-06 01:34:17 +0000138/// A stack of scopes which respond to exceptions, including cleanups
139/// and catch blocks.
140class EHScopeStack {
141public:
142 /// A saved depth on the scope stack. This is necessary because
143 /// pushing scopes onto the stack invalidates iterators.
144 class stable_iterator {
145 friend class EHScopeStack;
146
147 /// Offset from StartOfData to EndOfBuffer.
148 ptrdiff_t Size;
149
150 stable_iterator(ptrdiff_t Size) : Size(Size) {}
151
152 public:
153 static stable_iterator invalid() { return stable_iterator(-1); }
154 stable_iterator() : Size(-1) {}
155
156 bool isValid() const { return Size >= 0; }
157
John McCall838d7962010-08-14 07:46:19 +0000158 /// Returns true if this scope encloses I.
159 /// Returns false if I is invalid.
160 /// This scope must be valid.
John McCallff8e1152010-07-23 21:56:41 +0000161 bool encloses(stable_iterator I) const { return Size <= I.Size; }
John McCall838d7962010-08-14 07:46:19 +0000162
163 /// Returns true if this scope strictly encloses I: that is,
164 /// if it encloses I and is not I.
165 /// Returns false is I is invalid.
166 /// This scope must be valid.
John McCallff8e1152010-07-23 21:56:41 +0000167 bool strictlyEncloses(stable_iterator I) const { return Size < I.Size; }
John McCall1bda6622010-07-21 00:40:03 +0000168
John McCallf1549f62010-07-06 01:34:17 +0000169 friend bool operator==(stable_iterator A, stable_iterator B) {
170 return A.Size == B.Size;
171 }
172 friend bool operator!=(stable_iterator A, stable_iterator B) {
173 return A.Size != B.Size;
174 }
175 };
176
John McCall1f0fca52010-07-21 07:22:38 +0000177 /// Information for lazily generating a cleanup. Subclasses must be
178 /// POD-like: cleanups will not be destructed, and they will be
179 /// allocated on the cleanup stack and freely copied and moved
180 /// around.
John McCallda65ea82010-07-13 20:32:21 +0000181 ///
John McCall1f0fca52010-07-21 07:22:38 +0000182 /// Cleanup implementations should generally be declared in an
John McCallda65ea82010-07-13 20:32:21 +0000183 /// anonymous namespace.
John McCall1f0fca52010-07-21 07:22:38 +0000184 class Cleanup {
John McCallc4a1a842011-07-12 00:15:30 +0000185 // Anchor the construction vtable.
186 virtual void anchor();
John McCallda65ea82010-07-13 20:32:21 +0000187 public:
John McCallad346f42011-07-12 20:27:29 +0000188 /// Generation flags.
189 class Flags {
190 enum {
191 F_IsForEH = 0x1,
192 F_IsNormalCleanupKind = 0x2,
193 F_IsEHCleanupKind = 0x4
194 };
195 unsigned flags;
196
197 public:
198 Flags() : flags(0) {}
199
200 /// isForEH - true if the current emission is for an EH cleanup.
201 bool isForEHCleanup() const { return flags & F_IsForEH; }
202 bool isForNormalCleanup() const { return !isForEHCleanup(); }
203 void setIsForEHCleanup() { flags |= F_IsForEH; }
204
205 bool isNormalCleanupKind() const { return flags & F_IsNormalCleanupKind; }
206 void setIsNormalCleanupKind() { flags |= F_IsNormalCleanupKind; }
207
208 /// isEHCleanupKind - true if the cleanup was pushed as an EH
209 /// cleanup.
210 bool isEHCleanupKind() const { return flags & F_IsEHCleanupKind; }
211 void setIsEHCleanupKind() { flags |= F_IsEHCleanupKind; }
212 };
213
John McCallc4a1a842011-07-12 00:15:30 +0000214 // Provide a virtual destructor to suppress a very common warning
215 // that unfortunately cannot be suppressed without this. Cleanups
216 // should not rely on this destructor ever being called.
217 virtual ~Cleanup() {}
John McCall3e29f962010-07-13 23:19:49 +0000218
John McCallda65ea82010-07-13 20:32:21 +0000219 /// Emit the cleanup. For normal cleanups, this is run in the
220 /// same EH context as when the cleanup was pushed, i.e. the
221 /// immediately-enclosing context of the cleanup scope. For
222 /// EH cleanups, this is run in a terminate context.
223 ///
224 // \param IsForEHCleanup true if this is for an EH cleanup, false
225 /// if for a normal cleanup.
John McCallad346f42011-07-12 20:27:29 +0000226 virtual void Emit(CodeGenFunction &CGF, Flags flags) = 0;
John McCallda65ea82010-07-13 20:32:21 +0000227 };
228
John McCall150b4622011-01-26 04:00:11 +0000229 /// ConditionalCleanupN stores the saved form of its N parameters,
230 /// then restores them and performs the cleanup.
John McCall3ad32c82011-01-28 08:37:24 +0000231 template <class T, class A0>
232 class ConditionalCleanup1 : public Cleanup {
John McCall804b8072011-01-28 10:53:53 +0000233 typedef typename DominatingValue<A0>::saved_type A0_saved;
John McCall3ad32c82011-01-28 08:37:24 +0000234 A0_saved a0_saved;
235
John McCallad346f42011-07-12 20:27:29 +0000236 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall804b8072011-01-28 10:53:53 +0000237 A0 a0 = DominatingValue<A0>::restore(CGF, a0_saved);
John McCallad346f42011-07-12 20:27:29 +0000238 T(a0).Emit(CGF, flags);
John McCall3ad32c82011-01-28 08:37:24 +0000239 }
240
241 public:
242 ConditionalCleanup1(A0_saved a0)
243 : a0_saved(a0) {}
244 };
245
John McCall150b4622011-01-26 04:00:11 +0000246 template <class T, class A0, class A1>
John McCall3ad32c82011-01-28 08:37:24 +0000247 class ConditionalCleanup2 : public Cleanup {
John McCall804b8072011-01-28 10:53:53 +0000248 typedef typename DominatingValue<A0>::saved_type A0_saved;
249 typedef typename DominatingValue<A1>::saved_type A1_saved;
John McCall4bbcbda2011-01-26 19:15:39 +0000250 A0_saved a0_saved;
251 A1_saved a1_saved;
John McCall150b4622011-01-26 04:00:11 +0000252
John McCallad346f42011-07-12 20:27:29 +0000253 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall804b8072011-01-28 10:53:53 +0000254 A0 a0 = DominatingValue<A0>::restore(CGF, a0_saved);
255 A1 a1 = DominatingValue<A1>::restore(CGF, a1_saved);
John McCallad346f42011-07-12 20:27:29 +0000256 T(a0, a1).Emit(CGF, flags);
John McCall150b4622011-01-26 04:00:11 +0000257 }
258
259 public:
John McCall3ad32c82011-01-28 08:37:24 +0000260 ConditionalCleanup2(A0_saved a0, A1_saved a1)
261 : a0_saved(a0), a1_saved(a1) {}
John McCall150b4622011-01-26 04:00:11 +0000262 };
263
Douglas Gregord7b23162011-06-22 16:12:01 +0000264 template <class T, class A0, class A1, class A2>
265 class ConditionalCleanup3 : public Cleanup {
266 typedef typename DominatingValue<A0>::saved_type A0_saved;
267 typedef typename DominatingValue<A1>::saved_type A1_saved;
268 typedef typename DominatingValue<A2>::saved_type A2_saved;
269 A0_saved a0_saved;
270 A1_saved a1_saved;
271 A2_saved a2_saved;
272
John McCallad346f42011-07-12 20:27:29 +0000273 void Emit(CodeGenFunction &CGF, Flags flags) {
Douglas Gregord7b23162011-06-22 16:12:01 +0000274 A0 a0 = DominatingValue<A0>::restore(CGF, a0_saved);
275 A1 a1 = DominatingValue<A1>::restore(CGF, a1_saved);
276 A2 a2 = DominatingValue<A2>::restore(CGF, a2_saved);
John McCallad346f42011-07-12 20:27:29 +0000277 T(a0, a1, a2).Emit(CGF, flags);
Douglas Gregord7b23162011-06-22 16:12:01 +0000278 }
279
280 public:
281 ConditionalCleanup3(A0_saved a0, A1_saved a1, A2_saved a2)
John McCallc4a1a842011-07-12 00:15:30 +0000282 : a0_saved(a0), a1_saved(a1), a2_saved(a2) {}
Douglas Gregord7b23162011-06-22 16:12:01 +0000283 };
284
John McCall9928c482011-07-12 16:41:08 +0000285 template <class T, class A0, class A1, class A2, class A3>
286 class ConditionalCleanup4 : public Cleanup {
287 typedef typename DominatingValue<A0>::saved_type A0_saved;
288 typedef typename DominatingValue<A1>::saved_type A1_saved;
289 typedef typename DominatingValue<A2>::saved_type A2_saved;
290 typedef typename DominatingValue<A3>::saved_type A3_saved;
291 A0_saved a0_saved;
292 A1_saved a1_saved;
293 A2_saved a2_saved;
294 A3_saved a3_saved;
295
John McCallad346f42011-07-12 20:27:29 +0000296 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall9928c482011-07-12 16:41:08 +0000297 A0 a0 = DominatingValue<A0>::restore(CGF, a0_saved);
298 A1 a1 = DominatingValue<A1>::restore(CGF, a1_saved);
299 A2 a2 = DominatingValue<A2>::restore(CGF, a2_saved);
300 A3 a3 = DominatingValue<A3>::restore(CGF, a3_saved);
John McCallad346f42011-07-12 20:27:29 +0000301 T(a0, a1, a2, a3).Emit(CGF, flags);
John McCall9928c482011-07-12 16:41:08 +0000302 }
303
304 public:
305 ConditionalCleanup4(A0_saved a0, A1_saved a1, A2_saved a2, A3_saved a3)
306 : a0_saved(a0), a1_saved(a1), a2_saved(a2), a3_saved(a3) {}
307 };
308
John McCallf1549f62010-07-06 01:34:17 +0000309private:
310 // The implementation for this class is in CGException.h and
311 // CGException.cpp; the definition is here because it's used as a
312 // member of CodeGenFunction.
313
314 /// The start of the scope-stack buffer, i.e. the allocated pointer
315 /// for the buffer. All of these pointers are either simultaneously
316 /// null or simultaneously valid.
317 char *StartOfBuffer;
318
319 /// The end of the buffer.
320 char *EndOfBuffer;
321
322 /// The first valid entry in the buffer.
323 char *StartOfData;
324
325 /// The innermost normal cleanup on the stack.
326 stable_iterator InnermostNormalCleanup;
327
328 /// The innermost EH cleanup on the stack.
329 stable_iterator InnermostEHCleanup;
330
331 /// The number of catches on the stack.
332 unsigned CatchDepth;
333
John McCallff8e1152010-07-23 21:56:41 +0000334 /// The current EH destination index. Reset to FirstCatchIndex
335 /// whenever the last EH cleanup is popped.
336 unsigned NextEHDestIndex;
337 enum { FirstEHDestIndex = 1 };
338
John McCallf1549f62010-07-06 01:34:17 +0000339 /// The current set of branch fixups. A branch fixup is a jump to
340 /// an as-yet unemitted label, i.e. a label for which we don't yet
341 /// know the EH stack depth. Whenever we pop a cleanup, we have
342 /// to thread all the current branch fixups through it.
343 ///
344 /// Fixups are recorded as the Use of the respective branch or
345 /// switch statement. The use points to the final destination.
346 /// When popping out of a cleanup, these uses are threaded through
347 /// the cleanup and adjusted to point to the new cleanup.
348 ///
349 /// Note that branches are allowed to jump into protected scopes
350 /// in certain situations; e.g. the following code is legal:
351 /// struct A { ~A(); }; // trivial ctor, non-trivial dtor
352 /// goto foo;
353 /// A a;
354 /// foo:
355 /// bar();
Chris Lattner686775d2011-07-20 06:58:45 +0000356 SmallVector<BranchFixup, 8> BranchFixups;
John McCallf1549f62010-07-06 01:34:17 +0000357
358 char *allocate(size_t Size);
359
John McCall1f0fca52010-07-21 07:22:38 +0000360 void *pushCleanup(CleanupKind K, size_t DataSize);
John McCallda65ea82010-07-13 20:32:21 +0000361
John McCallf1549f62010-07-06 01:34:17 +0000362public:
363 EHScopeStack() : StartOfBuffer(0), EndOfBuffer(0), StartOfData(0),
364 InnermostNormalCleanup(stable_end()),
365 InnermostEHCleanup(stable_end()),
John McCallff8e1152010-07-23 21:56:41 +0000366 CatchDepth(0), NextEHDestIndex(FirstEHDestIndex) {}
John McCallf1549f62010-07-06 01:34:17 +0000367 ~EHScopeStack() { delete[] StartOfBuffer; }
368
John McCallda65ea82010-07-13 20:32:21 +0000369 // Variadic templates would make this not terrible.
370
371 /// Push a lazily-created cleanup on the stack.
John McCall8e3f8612010-07-13 22:12:14 +0000372 template <class T>
John McCall1f0fca52010-07-21 07:22:38 +0000373 void pushCleanup(CleanupKind Kind) {
374 void *Buffer = pushCleanup(Kind, sizeof(T));
375 Cleanup *Obj = new(Buffer) T();
John McCall8e3f8612010-07-13 22:12:14 +0000376 (void) Obj;
377 }
378
379 /// Push a lazily-created cleanup on the stack.
380 template <class T, class A0>
John McCall1f0fca52010-07-21 07:22:38 +0000381 void pushCleanup(CleanupKind Kind, A0 a0) {
382 void *Buffer = pushCleanup(Kind, sizeof(T));
383 Cleanup *Obj = new(Buffer) T(a0);
John McCall8e3f8612010-07-13 22:12:14 +0000384 (void) Obj;
385 }
386
387 /// Push a lazily-created cleanup on the stack.
John McCallda65ea82010-07-13 20:32:21 +0000388 template <class T, class A0, class A1>
John McCall1f0fca52010-07-21 07:22:38 +0000389 void pushCleanup(CleanupKind Kind, A0 a0, A1 a1) {
390 void *Buffer = pushCleanup(Kind, sizeof(T));
391 Cleanup *Obj = new(Buffer) T(a0, a1);
John McCallda65ea82010-07-13 20:32:21 +0000392 (void) Obj;
393 }
394
395 /// Push a lazily-created cleanup on the stack.
396 template <class T, class A0, class A1, class A2>
John McCall1f0fca52010-07-21 07:22:38 +0000397 void pushCleanup(CleanupKind Kind, A0 a0, A1 a1, A2 a2) {
398 void *Buffer = pushCleanup(Kind, sizeof(T));
399 Cleanup *Obj = new(Buffer) T(a0, a1, a2);
John McCallda65ea82010-07-13 20:32:21 +0000400 (void) Obj;
401 }
402
403 /// Push a lazily-created cleanup on the stack.
404 template <class T, class A0, class A1, class A2, class A3>
John McCall1f0fca52010-07-21 07:22:38 +0000405 void pushCleanup(CleanupKind Kind, A0 a0, A1 a1, A2 a2, A3 a3) {
406 void *Buffer = pushCleanup(Kind, sizeof(T));
407 Cleanup *Obj = new(Buffer) T(a0, a1, a2, a3);
John McCallda65ea82010-07-13 20:32:21 +0000408 (void) Obj;
409 }
410
John McCall77199712010-07-21 05:47:49 +0000411 /// Push a lazily-created cleanup on the stack.
412 template <class T, class A0, class A1, class A2, class A3, class A4>
John McCall1f0fca52010-07-21 07:22:38 +0000413 void pushCleanup(CleanupKind Kind, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) {
414 void *Buffer = pushCleanup(Kind, sizeof(T));
415 Cleanup *Obj = new(Buffer) T(a0, a1, a2, a3, a4);
John McCall77199712010-07-21 05:47:49 +0000416 (void) Obj;
417 }
418
John McCall7d8647f2010-09-14 07:57:04 +0000419 // Feel free to add more variants of the following:
420
421 /// Push a cleanup with non-constant storage requirements on the
422 /// stack. The cleanup type must provide an additional static method:
423 /// static size_t getExtraSize(size_t);
424 /// The argument to this method will be the value N, which will also
425 /// be passed as the first argument to the constructor.
426 ///
427 /// The data stored in the extra storage must obey the same
428 /// restrictions as normal cleanup member data.
429 ///
430 /// The pointer returned from this method is valid until the cleanup
431 /// stack is modified.
432 template <class T, class A0, class A1, class A2>
433 T *pushCleanupWithExtra(CleanupKind Kind, size_t N, A0 a0, A1 a1, A2 a2) {
434 void *Buffer = pushCleanup(Kind, sizeof(T) + T::getExtraSize(N));
435 return new (Buffer) T(N, a0, a1, a2);
436 }
437
John McCallf1549f62010-07-06 01:34:17 +0000438 /// Pops a cleanup scope off the stack. This should only be called
439 /// by CodeGenFunction::PopCleanupBlock.
440 void popCleanup();
441
442 /// Push a set of catch handlers on the stack. The catch is
443 /// uninitialized and will need to have the given number of handlers
444 /// set on it.
445 class EHCatchScope *pushCatch(unsigned NumHandlers);
446
447 /// Pops a catch scope off the stack.
448 void popCatch();
449
450 /// Push an exceptions filter on the stack.
451 class EHFilterScope *pushFilter(unsigned NumFilters);
452
453 /// Pops an exceptions filter off the stack.
454 void popFilter();
455
456 /// Push a terminate handler on the stack.
457 void pushTerminate();
458
459 /// Pops a terminate handler off the stack.
460 void popTerminate();
461
462 /// Determines whether the exception-scopes stack is empty.
463 bool empty() const { return StartOfData == EndOfBuffer; }
464
465 bool requiresLandingPad() const {
466 return (CatchDepth || hasEHCleanups());
467 }
468
469 /// Determines whether there are any normal cleanups on the stack.
470 bool hasNormalCleanups() const {
471 return InnermostNormalCleanup != stable_end();
472 }
473
474 /// Returns the innermost normal cleanup on the stack, or
475 /// stable_end() if there are no normal cleanups.
476 stable_iterator getInnermostNormalCleanup() const {
477 return InnermostNormalCleanup;
478 }
John McCall838d7962010-08-14 07:46:19 +0000479 stable_iterator getInnermostActiveNormalCleanup() const; // CGException.h
John McCallf1549f62010-07-06 01:34:17 +0000480
481 /// Determines whether there are any EH cleanups on the stack.
482 bool hasEHCleanups() const {
483 return InnermostEHCleanup != stable_end();
484 }
485
486 /// Returns the innermost EH cleanup on the stack, or stable_end()
487 /// if there are no EH cleanups.
488 stable_iterator getInnermostEHCleanup() const {
489 return InnermostEHCleanup;
490 }
John McCall838d7962010-08-14 07:46:19 +0000491 stable_iterator getInnermostActiveEHCleanup() const; // CGException.h
John McCallf1549f62010-07-06 01:34:17 +0000492
493 /// An unstable reference to a scope-stack depth. Invalidated by
494 /// pushes but not pops.
495 class iterator;
496
497 /// Returns an iterator pointing to the innermost EH scope.
498 iterator begin() const;
499
500 /// Returns an iterator pointing to the outermost EH scope.
501 iterator end() const;
502
503 /// Create a stable reference to the top of the EH stack. The
504 /// returned reference is valid until that scope is popped off the
505 /// stack.
506 stable_iterator stable_begin() const {
507 return stable_iterator(EndOfBuffer - StartOfData);
508 }
509
510 /// Create a stable reference to the bottom of the EH stack.
511 static stable_iterator stable_end() {
512 return stable_iterator(0);
513 }
514
515 /// Translates an iterator into a stable_iterator.
516 stable_iterator stabilize(iterator it) const;
517
518 /// Finds the nearest cleanup enclosing the given iterator.
519 /// Returns stable_iterator::invalid() if there are no such cleanups.
520 stable_iterator getEnclosingEHCleanup(iterator it) const;
521
522 /// Turn a stable reference to a scope depth into a unstable pointer
523 /// to the EH stack.
524 iterator find(stable_iterator save) const;
525
526 /// Removes the cleanup pointed to by the given stable_iterator.
527 void removeCleanup(stable_iterator save);
528
529 /// Add a branch fixup to the current cleanup scope.
530 BranchFixup &addBranchFixup() {
531 assert(hasNormalCleanups() && "adding fixup in scope without cleanups");
532 BranchFixups.push_back(BranchFixup());
533 return BranchFixups.back();
534 }
535
536 unsigned getNumBranchFixups() const { return BranchFixups.size(); }
537 BranchFixup &getBranchFixup(unsigned I) {
538 assert(I < getNumBranchFixups());
539 return BranchFixups[I];
540 }
541
John McCallff8e1152010-07-23 21:56:41 +0000542 /// Pops lazily-removed fixups from the end of the list. This
543 /// should only be called by procedures which have just popped a
544 /// cleanup or resolved one or more fixups.
545 void popNullFixups();
546
547 /// Clears the branch-fixups list. This should only be called by
John McCall8abdbd82010-09-18 02:24:39 +0000548 /// ResolveAllBranchFixups.
John McCallff8e1152010-07-23 21:56:41 +0000549 void clearFixups() { BranchFixups.clear(); }
550
551 /// Gets the next EH destination index.
552 unsigned getNextEHDestIndex() { return NextEHDestIndex++; }
John McCallf1549f62010-07-06 01:34:17 +0000553};
554
Reid Spencer5f016e22007-07-11 17:01:13 +0000555/// CodeGenFunction - This class organizes the per-function state that is used
556/// while generating LLVM code.
John McCall5936e332011-02-15 09:22:45 +0000557class CodeGenFunction : public CodeGenTypeCache {
Anders Carlsson8a219ce2009-02-24 04:21:31 +0000558 CodeGenFunction(const CodeGenFunction&); // DO NOT IMPLEMENT
559 void operator=(const CodeGenFunction&); // DO NOT IMPLEMENT
John McCall4c40d982010-08-31 07:33:07 +0000560
561 friend class CGCXXABI;
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000562public:
John McCallff8e1152010-07-23 21:56:41 +0000563 /// A jump destination is an abstract label, branching to which may
564 /// require a jump out through normal cleanups.
John McCallf1549f62010-07-06 01:34:17 +0000565 struct JumpDest {
John McCallff8e1152010-07-23 21:56:41 +0000566 JumpDest() : Block(0), ScopeDepth(), Index(0) {}
567 JumpDest(llvm::BasicBlock *Block,
568 EHScopeStack::stable_iterator Depth,
569 unsigned Index)
570 : Block(Block), ScopeDepth(Depth), Index(Index) {}
571
572 bool isValid() const { return Block != 0; }
573 llvm::BasicBlock *getBlock() const { return Block; }
574 EHScopeStack::stable_iterator getScopeDepth() const { return ScopeDepth; }
575 unsigned getDestIndex() const { return Index; }
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000576
John McCallff8e1152010-07-23 21:56:41 +0000577 private:
John McCallf1549f62010-07-06 01:34:17 +0000578 llvm::BasicBlock *Block;
579 EHScopeStack::stable_iterator ScopeDepth;
John McCallff8e1152010-07-23 21:56:41 +0000580 unsigned Index;
581 };
582
583 /// An unwind destination is an abstract label, branching to which
584 /// may require a jump out through EH cleanups.
585 struct UnwindDest {
586 UnwindDest() : Block(0), ScopeDepth(), Index(0) {}
587 UnwindDest(llvm::BasicBlock *Block,
588 EHScopeStack::stable_iterator Depth,
589 unsigned Index)
590 : Block(Block), ScopeDepth(Depth), Index(Index) {}
591
592 bool isValid() const { return Block != 0; }
593 llvm::BasicBlock *getBlock() const { return Block; }
594 EHScopeStack::stable_iterator getScopeDepth() const { return ScopeDepth; }
595 unsigned getDestIndex() const { return Index; }
596
597 private:
598 llvm::BasicBlock *Block;
599 EHScopeStack::stable_iterator ScopeDepth;
600 unsigned Index;
John McCallf1549f62010-07-06 01:34:17 +0000601 };
602
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 CodeGenModule &CGM; // Per-module state.
Daniel Dunbar444be732009-11-13 05:51:54 +0000604 const TargetInfo &Target;
Mike Stump0dd9e882009-02-08 23:14:22 +0000605
Chris Lattner58dee102007-08-21 16:57:55 +0000606 typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000607 CGBuilderTy Builder;
Mike Stump0dd9e882009-02-08 23:14:22 +0000608
Chris Lattnerb5437d22009-04-23 05:30:27 +0000609 /// CurFuncDecl - Holds the Decl for the current function or ObjC method.
610 /// This excludes BlockDecls.
Chris Lattner41110242008-06-17 18:05:57 +0000611 const Decl *CurFuncDecl;
Chris Lattnerb5437d22009-04-23 05:30:27 +0000612 /// CurCodeDecl - This is the inner-most code context, which includes blocks.
613 const Decl *CurCodeDecl;
Daniel Dunbar88b53962009-02-02 22:03:45 +0000614 const CGFunctionInfo *CurFnInfo;
Chris Lattner391d77a2008-03-30 23:03:07 +0000615 QualType FnRetTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000616 llvm::Function *CurFn;
617
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000618 /// CurGD - The GlobalDecl for the current function being compiled.
619 GlobalDecl CurGD;
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000620
John McCallf85e1932011-06-15 23:02:42 +0000621 /// PrologueCleanupDepth - The cleanup depth enclosing all the
622 /// cleanups associated with the parameters.
623 EHScopeStack::stable_iterator PrologueCleanupDepth;
624
Daniel Dunbar5ca20842008-09-09 21:00:17 +0000625 /// ReturnBlock - Unified return block.
John McCallf1549f62010-07-06 01:34:17 +0000626 JumpDest ReturnBlock;
627
Mike Stump0dd9e882009-02-08 23:14:22 +0000628 /// ReturnValue - The temporary alloca to hold the return value. This is null
629 /// iff the function has no return value.
Eli Friedmanb17daf92009-12-04 02:43:40 +0000630 llvm::Value *ReturnValue;
Mike Stump0dd9e882009-02-08 23:14:22 +0000631
John McCallff8e1152010-07-23 21:56:41 +0000632 /// RethrowBlock - Unified rethrow block.
633 UnwindDest RethrowBlock;
634
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 /// AllocaInsertPoint - This is an instruction in the entry block before which
636 /// we prefer to insert allocas.
Chris Lattner481769b2009-03-31 22:17:44 +0000637 llvm::AssertingVH<llvm::Instruction> AllocaInsertPt;
Daniel Dunbar0ffb1252008-08-04 16:51:22 +0000638
Mike Stump9c276ae2009-12-12 01:27:46 +0000639 bool CatchUndefined;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000640
John McCallf85e1932011-06-15 23:02:42 +0000641 /// In ARC, whether we should autorelease the return value.
642 bool AutoreleaseResult;
643
John McCalld16c2cf2011-02-08 08:22:06 +0000644 const CodeGen::CGBlockInfo *BlockInfo;
645 llvm::Value *BlockPointer;
646
Douglas Gregor3d91bbc2010-05-17 15:52:46 +0000647 /// \brief A mapping from NRVO variables to the flags used to indicate
648 /// when the NRVO has been applied to this variable.
649 llvm::DenseMap<const VarDecl *, llvm::Value *> NRVOFlags;
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000650
John McCallf1549f62010-07-06 01:34:17 +0000651 EHScopeStack EHStack;
652
John McCallff8e1152010-07-23 21:56:41 +0000653 /// i32s containing the indexes of the cleanup destinations.
654 llvm::AllocaInst *NormalCleanupDest;
655 llvm::AllocaInst *EHCleanupDest;
656
657 unsigned NextCleanupDestIndex;
658
John McCallf1549f62010-07-06 01:34:17 +0000659 /// The exception slot. All landing pads write the current
660 /// exception pointer into this alloca.
661 llvm::Value *ExceptionSlot;
662
John McCall93c332a2011-05-28 21:13:02 +0000663 /// The selector slot. Under the MandatoryCleanup model, all
664 /// landing pads write the current selector value into this alloca.
665 llvm::AllocaInst *EHSelectorSlot;
666
John McCallf1549f62010-07-06 01:34:17 +0000667 /// Emits a landing pad for the current EH stack.
668 llvm::BasicBlock *EmitLandingPad();
669
670 llvm::BasicBlock *getInvokeDestImpl();
671
John McCall3ad32c82011-01-28 08:37:24 +0000672 /// Set up the last cleaup that was pushed as a conditional
673 /// full-expression cleanup.
674 void initFullExprCleanup();
John McCall150b4622011-01-26 04:00:11 +0000675
676 template <class T>
John McCall804b8072011-01-28 10:53:53 +0000677 typename DominatingValue<T>::saved_type saveValueInCond(T value) {
678 return DominatingValue<T>::save(*this, value);
John McCall150b4622011-01-26 04:00:11 +0000679 }
680
Daniel Dunbar18ccc772008-09-28 01:03:14 +0000681public:
Anders Carlssonfa1f7562009-02-10 06:07:49 +0000682 /// ObjCEHValueStack - Stack of Objective-C exception values, used for
683 /// rethrows.
Chris Lattner686775d2011-07-20 06:58:45 +0000684 SmallVector<llvm::Value*, 8> ObjCEHValueStack;
Mike Stump0dd9e882009-02-08 23:14:22 +0000685
John McCalld768e9d2011-06-22 02:32:12 +0000686 /// A class controlling the emission of a finally block.
687 class FinallyInfo {
688 /// Where the catchall's edge through the cleanup should go.
689 JumpDest RethrowDest;
Anders Carlssonbb66f9f2009-02-08 07:46:24 +0000690
John McCalld768e9d2011-06-22 02:32:12 +0000691 /// A function to call to enter the catch.
692 llvm::Constant *BeginCatchFn;
693
694 /// An i1 variable indicating whether or not the @finally is
695 /// running for an exception.
696 llvm::AllocaInst *ForEHVar;
697
698 /// An i8* variable into which the exception pointer to rethrow
699 /// has been saved.
700 llvm::AllocaInst *SavedExnVar;
701
702 public:
703 void enter(CodeGenFunction &CGF, const Stmt *Finally,
704 llvm::Constant *beginCatchFn, llvm::Constant *endCatchFn,
705 llvm::Constant *rethrowFn);
706 void exit(CodeGenFunction &CGF);
707 };
Mike Stumpd88ea562009-12-09 03:35:49 +0000708
John McCall150b4622011-01-26 04:00:11 +0000709 /// pushFullExprCleanup - Push a cleanup to be run at the end of the
710 /// current full-expression. Safe against the possibility that
711 /// we're currently inside a conditionally-evaluated expression.
John McCall3ad32c82011-01-28 08:37:24 +0000712 template <class T, class A0>
713 void pushFullExprCleanup(CleanupKind kind, A0 a0) {
714 // If we're not in a conditional branch, or if none of the
715 // arguments requires saving, then use the unconditional cleanup.
John McCallc4a1a842011-07-12 00:15:30 +0000716 if (!isInConditionalBranch())
717 return EHStack.pushCleanup<T>(kind, a0);
John McCall3ad32c82011-01-28 08:37:24 +0000718
John McCall804b8072011-01-28 10:53:53 +0000719 typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
John McCall3ad32c82011-01-28 08:37:24 +0000720
721 typedef EHScopeStack::ConditionalCleanup1<T, A0> CleanupType;
722 EHStack.pushCleanup<CleanupType>(kind, a0_saved);
723 initFullExprCleanup();
724 }
725
726 /// pushFullExprCleanup - Push a cleanup to be run at the end of the
727 /// current full-expression. Safe against the possibility that
728 /// we're currently inside a conditionally-evaluated expression.
John McCall150b4622011-01-26 04:00:11 +0000729 template <class T, class A0, class A1>
730 void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1) {
731 // If we're not in a conditional branch, or if none of the
732 // arguments requires saving, then use the unconditional cleanup.
John McCallc4a1a842011-07-12 00:15:30 +0000733 if (!isInConditionalBranch())
734 return EHStack.pushCleanup<T>(kind, a0, a1);
John McCall150b4622011-01-26 04:00:11 +0000735
John McCall804b8072011-01-28 10:53:53 +0000736 typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
737 typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1);
John McCall150b4622011-01-26 04:00:11 +0000738
739 typedef EHScopeStack::ConditionalCleanup2<T, A0, A1> CleanupType;
John McCall3ad32c82011-01-28 08:37:24 +0000740 EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved);
741 initFullExprCleanup();
John McCall150b4622011-01-26 04:00:11 +0000742 }
743
Douglas Gregord7b23162011-06-22 16:12:01 +0000744 /// pushFullExprCleanup - Push a cleanup to be run at the end of the
745 /// current full-expression. Safe against the possibility that
746 /// we're currently inside a conditionally-evaluated expression.
747 template <class T, class A0, class A1, class A2>
748 void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1, A2 a2) {
749 // If we're not in a conditional branch, or if none of the
750 // arguments requires saving, then use the unconditional cleanup.
751 if (!isInConditionalBranch()) {
John McCallc4a1a842011-07-12 00:15:30 +0000752 return EHStack.pushCleanup<T>(kind, a0, a1, a2);
Douglas Gregord7b23162011-06-22 16:12:01 +0000753 }
754
755 typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
756 typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1);
757 typename DominatingValue<A2>::saved_type a2_saved = saveValueInCond(a2);
758
759 typedef EHScopeStack::ConditionalCleanup3<T, A0, A1, A2> CleanupType;
760 EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved, a2_saved);
761 initFullExprCleanup();
762 }
763
John McCall9928c482011-07-12 16:41:08 +0000764 /// pushFullExprCleanup - Push a cleanup to be run at the end of the
765 /// current full-expression. Safe against the possibility that
766 /// we're currently inside a conditionally-evaluated expression.
767 template <class T, class A0, class A1, class A2, class A3>
768 void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1, A2 a2, A3 a3) {
769 // If we're not in a conditional branch, or if none of the
770 // arguments requires saving, then use the unconditional cleanup.
771 if (!isInConditionalBranch()) {
772 return EHStack.pushCleanup<T>(kind, a0, a1, a2, a3);
773 }
774
775 typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
776 typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1);
777 typename DominatingValue<A2>::saved_type a2_saved = saveValueInCond(a2);
778 typename DominatingValue<A3>::saved_type a3_saved = saveValueInCond(a3);
779
780 typedef EHScopeStack::ConditionalCleanup4<T, A0, A1, A2, A3> CleanupType;
781 EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved,
782 a2_saved, a3_saved);
783 initFullExprCleanup();
784 }
785
John McCallf1549f62010-07-06 01:34:17 +0000786 /// PushDestructorCleanup - Push a cleanup to call the
787 /// complete-object destructor of an object of the given type at the
788 /// given address. Does nothing if T is not a C++ class type with a
789 /// non-trivial destructor.
790 void PushDestructorCleanup(QualType T, llvm::Value *Addr);
791
John McCall81407d42010-07-21 06:29:51 +0000792 /// PushDestructorCleanup - Push a cleanup to call the
793 /// complete-object variant of the given destructor on the object at
794 /// the given address.
795 void PushDestructorCleanup(const CXXDestructorDecl *Dtor,
796 llvm::Value *Addr);
797
John McCallf1549f62010-07-06 01:34:17 +0000798 /// PopCleanupBlock - Will pop the cleanup entry on the stack and
799 /// process all branch fixups.
John McCallff8e1152010-07-23 21:56:41 +0000800 void PopCleanupBlock(bool FallThroughIsBranchThrough = false);
John McCallf1549f62010-07-06 01:34:17 +0000801
John McCall7d8647f2010-09-14 07:57:04 +0000802 /// DeactivateCleanupBlock - Deactivates the given cleanup block.
803 /// The block cannot be reactivated. Pops it if it's the top of the
804 /// stack.
805 void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup);
806
807 /// ActivateCleanupBlock - Activates an initially-inactive cleanup.
808 /// Cannot be used to resurrect a deactivated cleanup.
809 void ActivateCleanupBlock(EHScopeStack::stable_iterator Cleanup);
John McCallcd2d2b72010-08-13 21:20:51 +0000810
John McCallf1549f62010-07-06 01:34:17 +0000811 /// \brief Enters a new scope for capturing cleanups, all of which
812 /// will be executed once the scope is exited.
813 class RunCleanupsScope {
Douglas Gregor01234bb2009-11-24 16:43:22 +0000814 CodeGenFunction& CGF;
John McCallf1549f62010-07-06 01:34:17 +0000815 EHScopeStack::stable_iterator CleanupStackDepth;
Douglas Gregor01234bb2009-11-24 16:43:22 +0000816 bool OldDidCallStackSave;
Douglas Gregor5656e142009-11-24 21:15:44 +0000817 bool PerformCleanup;
Douglas Gregor01234bb2009-11-24 16:43:22 +0000818
John McCallf1549f62010-07-06 01:34:17 +0000819 RunCleanupsScope(const RunCleanupsScope &); // DO NOT IMPLEMENT
820 RunCleanupsScope &operator=(const RunCleanupsScope &); // DO NOT IMPLEMENT
Douglas Gregor01234bb2009-11-24 16:43:22 +0000821
822 public:
823 /// \brief Enter a new cleanup scope.
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000824 explicit RunCleanupsScope(CodeGenFunction &CGF)
825 : CGF(CGF), PerformCleanup(true)
Douglas Gregor5656e142009-11-24 21:15:44 +0000826 {
John McCallf1549f62010-07-06 01:34:17 +0000827 CleanupStackDepth = CGF.EHStack.stable_begin();
Douglas Gregor01234bb2009-11-24 16:43:22 +0000828 OldDidCallStackSave = CGF.DidCallStackSave;
Argyrios Kyrtzidis4ada2ca2010-09-14 00:42:34 +0000829 CGF.DidCallStackSave = false;
Douglas Gregor01234bb2009-11-24 16:43:22 +0000830 }
831
832 /// \brief Exit this cleanup scope, emitting any accumulated
833 /// cleanups.
John McCallf1549f62010-07-06 01:34:17 +0000834 ~RunCleanupsScope() {
Douglas Gregor5656e142009-11-24 21:15:44 +0000835 if (PerformCleanup) {
836 CGF.DidCallStackSave = OldDidCallStackSave;
John McCallf1549f62010-07-06 01:34:17 +0000837 CGF.PopCleanupBlocks(CleanupStackDepth);
Douglas Gregor5656e142009-11-24 21:15:44 +0000838 }
839 }
840
841 /// \brief Determine whether this scope requires any cleanups.
842 bool requiresCleanups() const {
John McCallf1549f62010-07-06 01:34:17 +0000843 return CGF.EHStack.stable_begin() != CleanupStackDepth;
Douglas Gregor5656e142009-11-24 21:15:44 +0000844 }
845
846 /// \brief Force the emission of cleanups now, instead of waiting
847 /// until this object is destroyed.
848 void ForceCleanup() {
849 assert(PerformCleanup && "Already forced cleanup");
Douglas Gregor01234bb2009-11-24 16:43:22 +0000850 CGF.DidCallStackSave = OldDidCallStackSave;
John McCallf1549f62010-07-06 01:34:17 +0000851 CGF.PopCleanupBlocks(CleanupStackDepth);
Douglas Gregor5656e142009-11-24 21:15:44 +0000852 PerformCleanup = false;
Douglas Gregor01234bb2009-11-24 16:43:22 +0000853 }
854 };
855
Anders Carlsson44ec82b2010-03-30 03:14:41 +0000856
John McCallf1549f62010-07-06 01:34:17 +0000857 /// PopCleanupBlocks - Takes the old cleanup stack size and emits
858 /// the cleanup blocks that have been added.
859 void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize);
Anders Carlsson44ec82b2010-03-30 03:14:41 +0000860
John McCallff8e1152010-07-23 21:56:41 +0000861 void ResolveBranchFixups(llvm::BasicBlock *Target);
862
John McCallf1549f62010-07-06 01:34:17 +0000863 /// The given basic block lies in the current EH scope, but may be a
864 /// target of a potentially scope-crossing jump; get a stable handle
865 /// to which we can perform this jump later.
John McCallff8e1152010-07-23 21:56:41 +0000866 JumpDest getJumpDestInCurrentScope(llvm::BasicBlock *Target) {
John McCall413e6772010-07-28 01:07:35 +0000867 return JumpDest(Target,
868 EHStack.getInnermostNormalCleanup(),
869 NextCleanupDestIndex++);
John McCallf1549f62010-07-06 01:34:17 +0000870 }
Anders Carlssonc71c8452009-02-07 23:50:39 +0000871
John McCallf1549f62010-07-06 01:34:17 +0000872 /// The given basic block lies in the current EH scope, but may be a
873 /// target of a potentially scope-crossing jump; get a stable handle
874 /// to which we can perform this jump later.
Chris Lattner686775d2011-07-20 06:58:45 +0000875 JumpDest getJumpDestInCurrentScope(StringRef Name = StringRef()) {
John McCallff8e1152010-07-23 21:56:41 +0000876 return getJumpDestInCurrentScope(createBasicBlock(Name));
John McCallf1549f62010-07-06 01:34:17 +0000877 }
878
879 /// EmitBranchThroughCleanup - Emit a branch from the current insert
880 /// block through the normal cleanup handling code (if any) and then
881 /// on to \arg Dest.
882 void EmitBranchThroughCleanup(JumpDest Dest);
Chris Lattnerb11f9192011-04-17 00:54:30 +0000883
884 /// isObviouslyBranchWithoutCleanups - Return true if a branch to the
885 /// specified destination obviously has no cleanups to run. 'false' is always
886 /// a conservatively correct answer for this method.
887 bool isObviouslyBranchWithoutCleanups(JumpDest Dest) const;
John McCallf1549f62010-07-06 01:34:17 +0000888
889 /// EmitBranchThroughEHCleanup - Emit a branch from the current
890 /// insert block through the EH cleanup handling code (if any) and
891 /// then on to \arg Dest.
John McCallff8e1152010-07-23 21:56:41 +0000892 void EmitBranchThroughEHCleanup(UnwindDest Dest);
893
894 /// getRethrowDest - Returns the unified outermost-scope rethrow
895 /// destination.
896 UnwindDest getRethrowDest();
Mike Stump0dd9e882009-02-08 23:14:22 +0000897
John McCall150b4622011-01-26 04:00:11 +0000898 /// An object to manage conditionally-evaluated expressions.
899 class ConditionalEvaluation {
900 llvm::BasicBlock *StartBB;
Mike Stump1eb44332009-09-09 15:08:12 +0000901
John McCall150b4622011-01-26 04:00:11 +0000902 public:
903 ConditionalEvaluation(CodeGenFunction &CGF)
904 : StartBB(CGF.Builder.GetInsertBlock()) {}
Michael J. Spencer9cac4942010-10-19 06:39:39 +0000905
John McCall150b4622011-01-26 04:00:11 +0000906 void begin(CodeGenFunction &CGF) {
907 assert(CGF.OutermostConditional != this);
908 if (!CGF.OutermostConditional)
909 CGF.OutermostConditional = this;
910 }
911
912 void end(CodeGenFunction &CGF) {
913 assert(CGF.OutermostConditional != 0);
914 if (CGF.OutermostConditional == this)
915 CGF.OutermostConditional = 0;
916 }
917
918 /// Returns a block which will be executed prior to each
919 /// evaluation of the conditional code.
920 llvm::BasicBlock *getStartingBlock() const {
921 return StartBB;
922 }
923 };
Mike Stump1eb44332009-09-09 15:08:12 +0000924
John McCall3019c442010-09-17 00:50:28 +0000925 /// isInConditionalBranch - Return true if we're currently emitting
926 /// one branch or the other of a conditional expression.
John McCall150b4622011-01-26 04:00:11 +0000927 bool isInConditionalBranch() const { return OutermostConditional != 0; }
928
929 /// An RAII object to record that we're evaluating a statement
930 /// expression.
931 class StmtExprEvaluation {
932 CodeGenFunction &CGF;
933
934 /// We have to save the outermost conditional: cleanups in a
935 /// statement expression aren't conditional just because the
936 /// StmtExpr is.
937 ConditionalEvaluation *SavedOutermostConditional;
938
939 public:
940 StmtExprEvaluation(CodeGenFunction &CGF)
941 : CGF(CGF), SavedOutermostConditional(CGF.OutermostConditional) {
942 CGF.OutermostConditional = 0;
943 }
944
945 ~StmtExprEvaluation() {
946 CGF.OutermostConditional = SavedOutermostConditional;
947 CGF.EnsureInsertPoint();
948 }
949 };
John McCalle996ffd2011-02-16 08:02:54 +0000950
John McCall56ca35d2011-02-17 10:25:35 +0000951 /// An object which temporarily prevents a value from being
952 /// destroyed by aggressive peephole optimizations that assume that
953 /// all uses of a value have been realized in the IR.
954 class PeepholeProtection {
955 llvm::Instruction *Inst;
956 friend class CodeGenFunction;
957
958 public:
959 PeepholeProtection() : Inst(0) {}
960 };
961
John McCalle996ffd2011-02-16 08:02:54 +0000962 /// An RAII object to set (and then clear) a mapping for an OpaqueValueExpr.
963 class OpaqueValueMapping {
964 CodeGenFunction &CGF;
965 const OpaqueValueExpr *OpaqueValue;
John McCall56ca35d2011-02-17 10:25:35 +0000966 bool BoundLValue;
967 CodeGenFunction::PeepholeProtection Protection;
John McCalle996ffd2011-02-16 08:02:54 +0000968
969 public:
John McCall56ca35d2011-02-17 10:25:35 +0000970 static bool shouldBindAsLValue(const Expr *expr) {
971 return expr->isGLValue() || expr->getType()->isRecordType();
972 }
973
974 /// Build the opaque value mapping for the given conditional
975 /// operator if it's the GNU ?: extension. This is a common
976 /// enough pattern that the convenience operator is really
977 /// helpful.
978 ///
979 OpaqueValueMapping(CodeGenFunction &CGF,
980 const AbstractConditionalOperator *op) : CGF(CGF) {
981 if (isa<ConditionalOperator>(op)) {
982 OpaqueValue = 0;
983 BoundLValue = false;
984 return;
985 }
986
987 const BinaryConditionalOperator *e = cast<BinaryConditionalOperator>(op);
988 init(e->getOpaqueValue(), e->getCommon());
989 }
990
John McCalle996ffd2011-02-16 08:02:54 +0000991 OpaqueValueMapping(CodeGenFunction &CGF,
992 const OpaqueValueExpr *opaqueValue,
John McCall56ca35d2011-02-17 10:25:35 +0000993 LValue lvalue)
994 : CGF(CGF), OpaqueValue(opaqueValue), BoundLValue(true) {
John McCalle996ffd2011-02-16 08:02:54 +0000995 assert(opaqueValue && "no opaque value expression!");
John McCall56ca35d2011-02-17 10:25:35 +0000996 assert(shouldBindAsLValue(opaqueValue));
997 initLValue(lvalue);
998 }
999
1000 OpaqueValueMapping(CodeGenFunction &CGF,
1001 const OpaqueValueExpr *opaqueValue,
1002 RValue rvalue)
1003 : CGF(CGF), OpaqueValue(opaqueValue), BoundLValue(false) {
1004 assert(opaqueValue && "no opaque value expression!");
1005 assert(!shouldBindAsLValue(opaqueValue));
1006 initRValue(rvalue);
John McCalle996ffd2011-02-16 08:02:54 +00001007 }
1008
1009 void pop() {
1010 assert(OpaqueValue && "mapping already popped!");
John McCall56ca35d2011-02-17 10:25:35 +00001011 popImpl();
John McCalle996ffd2011-02-16 08:02:54 +00001012 OpaqueValue = 0;
1013 }
1014
1015 ~OpaqueValueMapping() {
John McCall56ca35d2011-02-17 10:25:35 +00001016 if (OpaqueValue) popImpl();
1017 }
1018
1019 private:
1020 void popImpl() {
1021 if (BoundLValue)
1022 CGF.OpaqueLValues.erase(OpaqueValue);
1023 else {
1024 CGF.OpaqueRValues.erase(OpaqueValue);
1025 CGF.unprotectFromPeepholes(Protection);
1026 }
1027 }
1028
1029 void init(const OpaqueValueExpr *ov, const Expr *e) {
1030 OpaqueValue = ov;
1031 BoundLValue = shouldBindAsLValue(ov);
1032 assert(BoundLValue == shouldBindAsLValue(e)
1033 && "inconsistent expression value kinds!");
1034 if (BoundLValue)
1035 initLValue(CGF.EmitLValue(e));
1036 else
1037 initRValue(CGF.EmitAnyExpr(e));
1038 }
1039
1040 void initLValue(const LValue &lv) {
1041 CGF.OpaqueLValues.insert(std::make_pair(OpaqueValue, lv));
1042 }
1043
1044 void initRValue(const RValue &rv) {
1045 // Work around an extremely aggressive peephole optimization in
1046 // EmitScalarConversion which assumes that all other uses of a
1047 // value are extant.
1048 Protection = CGF.protectFromPeepholes(rv);
1049 CGF.OpaqueRValues.insert(std::make_pair(OpaqueValue, rv));
John McCalle996ffd2011-02-16 08:02:54 +00001050 }
1051 };
Fariborz Jahaniane2204552010-11-16 19:29:39 +00001052
1053 /// getByrefValueFieldNumber - Given a declaration, returns the LLVM field
1054 /// number that holds the value.
1055 unsigned getByRefValueLLVMField(const ValueDecl *VD) const;
Fariborz Jahanian52a80e12011-01-26 23:08:27 +00001056
1057 /// BuildBlockByrefAddress - Computes address location of the
1058 /// variable which is declared as __block.
1059 llvm::Value *BuildBlockByrefAddress(llvm::Value *BaseAddr,
1060 const VarDecl *V);
Chris Lattner7f02f722007-08-24 05:35:26 +00001061private:
Chris Lattnerd9becd12009-10-28 23:59:40 +00001062 CGDebugInfo *DebugInfo;
Devang Patelaa112892011-03-07 18:45:56 +00001063 bool DisableDebugInfo;
Mike Stump09429b92009-02-17 17:00:02 +00001064
John McCall93c332a2011-05-28 21:13:02 +00001065 /// DidCallStackSave - Whether llvm.stacksave has been called. Used to avoid
1066 /// calling llvm.stacksave for multiple VLAs in the same scope.
1067 bool DidCallStackSave;
1068
Mike Stumpf71d2322009-11-30 20:08:49 +00001069 /// IndirectBranch - The first time an indirect goto is seen we create a block
1070 /// with an indirect branch. Every time we see the address of a label taken,
1071 /// we add the label to the indirect goto. Every subsequent indirect goto is
1072 /// codegen'd as a jump to the IndirectBranch's basic block.
Chris Lattnerd9becd12009-10-28 23:59:40 +00001073 llvm::IndirectBrInst *IndirectBranch;
Daniel Dunbar0ffb1252008-08-04 16:51:22 +00001074
Mike Stump0dd9e882009-02-08 23:14:22 +00001075 /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
1076 /// decls.
John McCall6b5a61b2011-02-07 10:33:21 +00001077 typedef llvm::DenseMap<const Decl*, llvm::Value*> DeclMapTy;
1078 DeclMapTy LocalDeclMap;
Reid Spencer5f016e22007-07-11 17:01:13 +00001079
1080 /// LabelMap - This keeps track of the LLVM basic block for each C label.
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001081 llvm::DenseMap<const LabelDecl*, JumpDest> LabelMap;
Mike Stump0dd9e882009-02-08 23:14:22 +00001082
Mike Stump0dd9e882009-02-08 23:14:22 +00001083 // BreakContinueStack - This keeps track of where break and continue
Anders Carlssone4b6d342009-02-10 05:52:02 +00001084 // statements should jump to.
Chris Lattnerda138702007-07-16 21:28:45 +00001085 struct BreakContinue {
John McCallf1549f62010-07-06 01:34:17 +00001086 BreakContinue(JumpDest Break, JumpDest Continue)
1087 : BreakBlock(Break), ContinueBlock(Continue) {}
Mike Stump0dd9e882009-02-08 23:14:22 +00001088
John McCallf1549f62010-07-06 01:34:17 +00001089 JumpDest BreakBlock;
1090 JumpDest ContinueBlock;
Mike Stump0dd9e882009-02-08 23:14:22 +00001091 };
Chris Lattner686775d2011-07-20 06:58:45 +00001092 SmallVector<BreakContinue, 8> BreakContinueStack;
Daniel Dunbar18ccc772008-09-28 01:03:14 +00001093
Mike Stump0dd9e882009-02-08 23:14:22 +00001094 /// SwitchInsn - This is nearest current switch instruction. It is null if if
1095 /// current context is not in a switch.
Devang Patel51b09f22007-10-04 23:45:31 +00001096 llvm::SwitchInst *SwitchInsn;
1097
Mike Stump0dd9e882009-02-08 23:14:22 +00001098 /// CaseRangeBlock - This block holds if condition check for last case
Devang Patel80fd5f92007-10-09 17:08:50 +00001099 /// statement range in current switch instruction.
Devang Patelc049e4f2007-10-08 20:57:48 +00001100 llvm::BasicBlock *CaseRangeBlock;
1101
John McCall56ca35d2011-02-17 10:25:35 +00001102 /// OpaqueLValues - Keeps track of the current set of opaque value
John McCalle996ffd2011-02-16 08:02:54 +00001103 /// expressions.
John McCall56ca35d2011-02-17 10:25:35 +00001104 llvm::DenseMap<const OpaqueValueExpr *, LValue> OpaqueLValues;
1105 llvm::DenseMap<const OpaqueValueExpr *, RValue> OpaqueRValues;
John McCalle996ffd2011-02-16 08:02:54 +00001106
Mike Stump0dd9e882009-02-08 23:14:22 +00001107 // VLASizeMap - This keeps track of the associated size for each VLA type.
Eli Friedmanbbed6b92009-08-15 02:50:32 +00001108 // We track this by the size expression rather than the type itself because
1109 // in certain situations, like a const qualifier applied to an VLA typedef,
1110 // multiple VLA types can share the same size expression.
Mike Stump0dd9e882009-02-08 23:14:22 +00001111 // FIXME: Maybe this could be a stack of maps that is pushed/popped as we
1112 // enter/leave scopes.
Eli Friedmanbbed6b92009-08-15 02:50:32 +00001113 llvm::DenseMap<const Expr*, llvm::Value*> VLASizeMap;
Mike Stump0dd9e882009-02-08 23:14:22 +00001114
John McCallf1549f62010-07-06 01:34:17 +00001115 /// A block containing a single 'unreachable' instruction. Created
1116 /// lazily by getUnreachableBlock().
1117 llvm::BasicBlock *UnreachableBlock;
Mike Stump0dd9e882009-02-08 23:14:22 +00001118
Anders Carlssonf6c56e22009-11-25 03:15:49 +00001119 /// CXXThisDecl - When generating code for a C++ member function,
1120 /// this will hold the implicit 'this' declaration.
Anders Carlsson2b77ba82009-04-04 20:47:02 +00001121 ImplicitParamDecl *CXXThisDecl;
John McCall25049412010-02-16 22:04:33 +00001122 llvm::Value *CXXThisValue;
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Anders Carlssonf6c56e22009-11-25 03:15:49 +00001124 /// CXXVTTDecl - When generating code for a base object constructor or
1125 /// base object destructor with virtual bases, this will hold the implicit
1126 /// VTT parameter.
1127 ImplicitParamDecl *CXXVTTDecl;
John McCall25049412010-02-16 22:04:33 +00001128 llvm::Value *CXXVTTValue;
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001129
John McCall150b4622011-01-26 04:00:11 +00001130 /// OutermostConditional - Points to the outermost active
1131 /// conditional control. This is used so that we know if a
1132 /// temporary should be destroyed conditionally.
1133 ConditionalEvaluation *OutermostConditional;
Mike Stump1eb44332009-09-09 15:08:12 +00001134
Anders Carlsson7dfa4072009-09-12 02:14:24 +00001135
1136 /// ByrefValueInfoMap - For each __block variable, contains a pair of the LLVM
1137 /// type as well as the field number that contains the actual data.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001138 llvm::DenseMap<const ValueDecl *, std::pair<llvm::Type *,
Anders Carlsson7dfa4072009-09-12 02:14:24 +00001139 unsigned> > ByRefValueInfo;
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001140
John McCallf1549f62010-07-06 01:34:17 +00001141 llvm::BasicBlock *TerminateLandingPad;
Mike Stump182f3832009-12-10 00:02:42 +00001142 llvm::BasicBlock *TerminateHandler;
Chris Lattner83252dc2010-07-20 21:07:09 +00001143 llvm::BasicBlock *TrapBB;
Eli Friedman94067052009-12-10 02:21:21 +00001144
Reid Spencer5f016e22007-07-11 17:01:13 +00001145public:
1146 CodeGenFunction(CodeGenModule &cgm);
Mike Stump0dd9e882009-02-08 23:14:22 +00001147
John McCall1e7fe752010-09-02 09:58:18 +00001148 CodeGenTypes &getTypes() const { return CGM.getTypes(); }
John McCallf2aac842011-05-15 02:34:36 +00001149 ASTContext &getContext() const { return CGM.getContext(); }
Devang Patelaa112892011-03-07 18:45:56 +00001150 CGDebugInfo *getDebugInfo() {
1151 if (DisableDebugInfo)
1152 return NULL;
1153 return DebugInfo;
1154 }
1155 void disableDebugInfo() { DisableDebugInfo = true; }
1156 void enableDebugInfo() { DisableDebugInfo = false; }
1157
John McCallf85e1932011-06-15 23:02:42 +00001158 bool shouldUseFusedARCCalls() {
1159 return CGM.getCodeGenOpts().OptimizationLevel == 0;
1160 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001161
John McCalld16c2cf2011-02-08 08:22:06 +00001162 const LangOptions &getLangOptions() const { return CGM.getLangOptions(); }
1163
John McCallf1549f62010-07-06 01:34:17 +00001164 /// Returns a pointer to the function's exception object slot, which
1165 /// is assigned in every landing pad.
1166 llvm::Value *getExceptionSlot();
John McCall93c332a2011-05-28 21:13:02 +00001167 llvm::Value *getEHSelectorSlot();
John McCallf1549f62010-07-06 01:34:17 +00001168
John McCallff8e1152010-07-23 21:56:41 +00001169 llvm::Value *getNormalCleanupDestSlot();
1170 llvm::Value *getEHCleanupDestSlot();
1171
John McCallf1549f62010-07-06 01:34:17 +00001172 llvm::BasicBlock *getUnreachableBlock() {
1173 if (!UnreachableBlock) {
1174 UnreachableBlock = createBasicBlock("unreachable");
1175 new llvm::UnreachableInst(getLLVMContext(), UnreachableBlock);
1176 }
1177 return UnreachableBlock;
1178 }
1179
1180 llvm::BasicBlock *getInvokeDest() {
1181 if (!EHStack.requiresLandingPad()) return 0;
1182 return getInvokeDestImpl();
1183 }
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001184
John McCalld16c2cf2011-02-08 08:22:06 +00001185 llvm::LLVMContext &getLLVMContext() { return CGM.getLLVMContext(); }
Owen Anderson69243822009-07-13 04:10:07 +00001186
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001187 //===--------------------------------------------------------------------===//
John McCallbdc4d802011-07-09 01:37:26 +00001188 // Cleanups
1189 //===--------------------------------------------------------------------===//
1190
1191 typedef void Destroyer(CodeGenFunction &CGF, llvm::Value *addr, QualType ty);
1192
John McCall2673c682011-07-11 08:38:19 +00001193 void pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin,
1194 llvm::Value *arrayEndPointer,
1195 QualType elementType,
1196 Destroyer &destroyer);
1197 void pushRegularPartialArrayCleanup(llvm::Value *arrayBegin,
1198 llvm::Value *arrayEnd,
1199 QualType elementType,
1200 Destroyer &destroyer);
John McCallbdc4d802011-07-09 01:37:26 +00001201
John McCall9928c482011-07-12 16:41:08 +00001202 void pushDestroy(QualType::DestructionKind dtorKind,
1203 llvm::Value *addr, QualType type);
John McCallbdc4d802011-07-09 01:37:26 +00001204 void pushDestroy(CleanupKind kind, llvm::Value *addr, QualType type,
John McCall2673c682011-07-11 08:38:19 +00001205 Destroyer &destroyer, bool useEHCleanupForArray);
1206 void emitDestroy(llvm::Value *addr, QualType type, Destroyer &destroyer,
1207 bool useEHCleanupForArray);
John McCalla91f6662011-07-13 03:01:35 +00001208 llvm::Function *generateDestroyHelper(llvm::Constant *addr,
1209 QualType type,
1210 Destroyer &destroyer,
1211 bool useEHCleanupForArray);
John McCallbdc4d802011-07-09 01:37:26 +00001212 void emitArrayDestroy(llvm::Value *begin, llvm::Value *end,
John McCall2673c682011-07-11 08:38:19 +00001213 QualType type, Destroyer &destroyer,
John McCallfbf780a2011-07-13 08:09:46 +00001214 bool checkZeroLength, bool useEHCleanup);
John McCallbdc4d802011-07-09 01:37:26 +00001215
John McCall9928c482011-07-12 16:41:08 +00001216 Destroyer &getDestroyer(QualType::DestructionKind destructionKind);
1217
John McCallbdc4d802011-07-09 01:37:26 +00001218 /// Determines whether an EH cleanup is required to destroy a type
1219 /// with the given destruction kind.
1220 bool needsEHCleanup(QualType::DestructionKind kind) {
1221 switch (kind) {
1222 case QualType::DK_none:
1223 return false;
1224 case QualType::DK_cxx_destructor:
1225 case QualType::DK_objc_weak_lifetime:
1226 return getLangOptions().Exceptions;
1227 case QualType::DK_objc_strong_lifetime:
1228 return getLangOptions().Exceptions &&
1229 CGM.getCodeGenOpts().ObjCAutoRefCountExceptions;
1230 }
1231 llvm_unreachable("bad destruction kind");
1232 }
1233
John McCall9928c482011-07-12 16:41:08 +00001234 CleanupKind getCleanupKind(QualType::DestructionKind kind) {
1235 return (needsEHCleanup(kind) ? NormalAndEHCleanup : NormalCleanup);
1236 }
1237
John McCallbdc4d802011-07-09 01:37:26 +00001238 //===--------------------------------------------------------------------===//
Daniel Dunbar9834ffb2009-02-23 17:26:39 +00001239 // Objective-C
1240 //===--------------------------------------------------------------------===//
1241
Chris Lattner391d77a2008-03-30 23:03:07 +00001242 void GenerateObjCMethod(const ObjCMethodDecl *OMD);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001243
Mike Stump0dd9e882009-02-08 23:14:22 +00001244 void StartObjCMethod(const ObjCMethodDecl *MD,
Devang Patel8d3f8972011-05-19 23:37:41 +00001245 const ObjCContainerDecl *CD,
1246 SourceLocation StartLoc);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001247
Mike Stump0dd9e882009-02-08 23:14:22 +00001248 /// GenerateObjCGetter - Synthesize an Objective-C property getter function.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +00001249 void GenerateObjCGetter(ObjCImplementationDecl *IMP,
1250 const ObjCPropertyImplDecl *PID);
Fariborz Jahanian2846b972011-02-18 19:15:13 +00001251 void GenerateObjCGetterBody(ObjCIvarDecl *Ivar, bool IsAtomic, bool IsStrong);
1252 void GenerateObjCAtomicSetterBody(ObjCMethodDecl *OMD,
1253 ObjCIvarDecl *Ivar);
1254
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00001255 void GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
1256 ObjCMethodDecl *MD, bool ctor);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001257
Mike Stump0dd9e882009-02-08 23:14:22 +00001258 /// GenerateObjCSetter - Synthesize an Objective-C property setter function
1259 /// for the given property.
Fariborz Jahanianfef30b52008-12-09 20:23:04 +00001260 void GenerateObjCSetter(ObjCImplementationDecl *IMP,
1261 const ObjCPropertyImplDecl *PID);
Fariborz Jahanian0b2bd472010-04-13 00:38:05 +00001262 bool IndirectObjCSetterArg(const CGFunctionInfo &FI);
Fariborz Jahanian15bd5882010-04-13 18:32:24 +00001263 bool IvarTypeWithAggrGCObjects(QualType Ty);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001264
Mike Stump4e7a1f72009-02-21 20:00:35 +00001265 //===--------------------------------------------------------------------===//
1266 // Block Bits
1267 //===--------------------------------------------------------------------===//
1268
John McCall6b5a61b2011-02-07 10:33:21 +00001269 llvm::Value *EmitBlockLiteral(const BlockExpr *);
Blaine Garst2a7eb282010-02-23 21:51:17 +00001270 llvm::Constant *BuildDescriptorBlockDecl(const BlockExpr *,
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001271 const CGBlockInfo &Info,
Chris Lattner2acc6e32011-07-18 04:24:23 +00001272 llvm::StructType *,
John McCalld16c2cf2011-02-08 08:22:06 +00001273 llvm::Constant *BlockVarLayout);
Mike Stump4e7a1f72009-02-21 20:00:35 +00001274
Fariborz Jahanian564360b2010-06-24 00:08:06 +00001275 llvm::Function *GenerateBlockFunction(GlobalDecl GD,
John McCall6b5a61b2011-02-07 10:33:21 +00001276 const CGBlockInfo &Info,
Mike Stump6cc88f72009-03-20 21:53:12 +00001277 const Decl *OuterFuncDecl,
John McCall6b5a61b2011-02-07 10:33:21 +00001278 const DeclMapTy &ldm);
Mike Stump4e7a1f72009-02-21 20:00:35 +00001279
John McCalld16c2cf2011-02-08 08:22:06 +00001280 llvm::Constant *GenerateCopyHelperFunction(const CGBlockInfo &blockInfo);
1281 llvm::Constant *GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo);
1282
John McCalld16c2cf2011-02-08 08:22:06 +00001283 void BuildBlockRelease(llvm::Value *DeclPtr, BlockFieldFlags flags);
1284
John McCall5af02db2011-03-31 01:59:53 +00001285 class AutoVarEmission;
1286
1287 void emitByrefStructureInit(const AutoVarEmission &emission);
1288 void enterByrefCleanup(const AutoVarEmission &emission);
1289
John McCall6b5a61b2011-02-07 10:33:21 +00001290 llvm::Value *LoadBlockStruct() {
1291 assert(BlockPointer && "no block pointer set!");
1292 return BlockPointer;
1293 }
Mike Stump4e7a1f72009-02-21 20:00:35 +00001294
John McCallea1471e2010-05-20 01:18:31 +00001295 void AllocateBlockCXXThisPointer(const CXXThisExpr *E);
1296 void AllocateBlockDecl(const BlockDeclRefExpr *E);
John McCallee504292010-05-21 04:11:14 +00001297 llvm::Value *GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
1298 return GetAddrOfBlockDecl(E->getDecl(), E->isByRef());
1299 }
John McCall6b5a61b2011-02-07 10:33:21 +00001300 llvm::Value *GetAddrOfBlockDecl(const VarDecl *var, bool ByRef);
Chris Lattner2acc6e32011-07-18 04:24:23 +00001301 llvm::Type *BuildByRefType(const VarDecl *var);
Mike Stumpdab514f2009-03-04 03:23:46 +00001302
John McCalld26bc762011-03-09 04:27:21 +00001303 void GenerateCode(GlobalDecl GD, llvm::Function *Fn,
1304 const CGFunctionInfo &FnInfo);
Anders Carlsson0ff8baf2009-09-11 00:07:24 +00001305 void StartFunction(GlobalDecl GD, QualType RetTy,
Daniel Dunbar7c086512008-09-09 23:14:03 +00001306 llvm::Function *Fn,
John McCalld26bc762011-03-09 04:27:21 +00001307 const CGFunctionInfo &FnInfo,
Daniel Dunbar2284ac92008-10-18 18:22:23 +00001308 const FunctionArgList &Args,
Tilmann Scheller9c6082f2011-03-02 21:36:49 +00001309 SourceLocation StartLoc);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001310
John McCall9fc6a772010-02-19 09:25:03 +00001311 void EmitConstructorBody(FunctionArgList &Args);
1312 void EmitDestructorBody(FunctionArgList &Args);
1313 void EmitFunctionBody(FunctionArgList &Args);
John McCalla355e072010-02-18 03:17:58 +00001314
Mike Stump0dd9e882009-02-08 23:14:22 +00001315 /// EmitReturnBlock - Emit the unified return block, trying to avoid its
1316 /// emission when possible.
Daniel Dunbar1c1d6072009-01-26 23:27:52 +00001317 void EmitReturnBlock();
1318
Mike Stump0dd9e882009-02-08 23:14:22 +00001319 /// FinishFunction - Complete IR generation of the current function. It is
1320 /// legal to call this function even if there is no current insertion point.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001321 void FinishFunction(SourceLocation EndLoc=SourceLocation());
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001322
Anders Carlsson519c3282010-03-24 00:39:18 +00001323 /// GenerateThunk - Generate a thunk for the given method.
John McCalld26bc762011-03-09 04:27:21 +00001324 void GenerateThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo,
1325 GlobalDecl GD, const ThunkInfo &Thunk);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001326
Eli Friedman7dcdf5b2011-05-06 17:27:27 +00001327 void GenerateVarArgsThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo,
1328 GlobalDecl GD, const ThunkInfo &Thunk);
1329
Douglas Gregorfb8cc252010-05-05 05:51:00 +00001330 void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type,
1331 FunctionArgList &Args);
Mike Stump1eb44332009-09-09 15:08:12 +00001332
Anders Carlssond103f9f2010-03-28 19:40:00 +00001333 /// InitializeVTablePointer - Initialize the vtable pointer of the given
1334 /// subobject.
1335 ///
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001336 void InitializeVTablePointer(BaseSubobject Base,
Anders Carlssonb3b772e2010-04-20 05:22:15 +00001337 const CXXRecordDecl *NearestVBase,
Ken Dyckd6fb21f2011-03-23 01:04:18 +00001338 CharUnits OffsetFromNearestVBase,
Anders Carlssond103f9f2010-03-28 19:40:00 +00001339 llvm::Constant *VTable,
1340 const CXXRecordDecl *VTableClass);
1341
1342 typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001343 void InitializeVTablePointers(BaseSubobject Base,
Anders Carlssonb3b772e2010-04-20 05:22:15 +00001344 const CXXRecordDecl *NearestVBase,
Ken Dyckd6fb21f2011-03-23 01:04:18 +00001345 CharUnits OffsetFromNearestVBase,
Anders Carlsson603d6d12010-03-28 21:07:49 +00001346 bool BaseIsNonVirtualPrimaryBase,
1347 llvm::Constant *VTable,
1348 const CXXRecordDecl *VTableClass,
1349 VisitedVirtualBasesSetTy& VBases);
Eli Friedman77a259c2009-12-08 06:46:18 +00001350
Anders Carlsson603d6d12010-03-28 21:07:49 +00001351 void InitializeVTablePointers(const CXXRecordDecl *ClassDecl);
Anders Carlssond103f9f2010-03-28 19:40:00 +00001352
Dan Gohman043fb9a2010-10-26 18:44:08 +00001353 /// GetVTablePtr - Return the Value of the vtable pointer member pointed
1354 /// to by This.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001355 llvm::Value *GetVTablePtr(llvm::Value *This, llvm::Type *Ty);
Anders Carlssond103f9f2010-03-28 19:40:00 +00001356
John McCall50da2ca2010-07-21 05:30:47 +00001357 /// EnterDtorCleanups - Enter the cleanups necessary to complete the
1358 /// given phase of destruction for a destructor. The end result
1359 /// should call destructors on members and base classes in reverse
1360 /// order of their construction.
1361 void EnterDtorCleanups(const CXXDestructorDecl *Dtor, CXXDtorType Type);
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Chris Lattner7255a2d2010-06-22 00:03:40 +00001363 /// ShouldInstrumentFunction - Return true if the current function should be
1364 /// instrumented with __cyg_profile_func_* calls
1365 bool ShouldInstrumentFunction();
1366
1367 /// EmitFunctionInstrumentation - Emit LLVM code to call the specified
1368 /// instrumentation function with the current function and the call site, if
1369 /// function instrumentation is enabled.
1370 void EmitFunctionInstrumentation(const char *Fn);
1371
Roman Divackybe4c8702011-02-10 16:52:03 +00001372 /// EmitMCountInstrumentation - Emit call to .mcount.
1373 void EmitMCountInstrumentation();
1374
Mike Stump0dd9e882009-02-08 23:14:22 +00001375 /// EmitFunctionProlog - Emit the target specific LLVM code to load the
1376 /// arguments for the given function. This is also responsible for naming the
1377 /// LLVM function arguments.
Daniel Dunbar88b53962009-02-02 22:03:45 +00001378 void EmitFunctionProlog(const CGFunctionInfo &FI,
1379 llvm::Function *Fn,
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001380 const FunctionArgList &Args);
1381
Mike Stump0dd9e882009-02-08 23:14:22 +00001382 /// EmitFunctionEpilog - Emit the target specific LLVM code to return the
1383 /// given temporary.
Chris Lattner35b21b82010-06-27 01:06:27 +00001384 void EmitFunctionEpilog(const CGFunctionInfo &FI);
Daniel Dunbar17b708d2008-09-09 23:27:19 +00001385
Mike Stumpcce3d4f2009-12-07 23:38:24 +00001386 /// EmitStartEHSpec - Emit the start of the exception spec.
1387 void EmitStartEHSpec(const Decl *D);
1388
1389 /// EmitEndEHSpec - Emit the end of the exception spec.
1390 void EmitEndEHSpec(const Decl *D);
1391
John McCallf1549f62010-07-06 01:34:17 +00001392 /// getTerminateLandingPad - Return a landing pad that just calls terminate.
1393 llvm::BasicBlock *getTerminateLandingPad();
1394
1395 /// getTerminateHandler - Return a handler (not a landing pad, just
1396 /// a catch handler) that just calls terminate. This is used when
1397 /// a terminate scope encloses a try.
Mike Stump9b39c512009-12-09 22:59:31 +00001398 llvm::BasicBlock *getTerminateHandler();
1399
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001400 llvm::Type *ConvertTypeForMem(QualType T);
1401 llvm::Type *ConvertType(QualType T);
1402 llvm::Type *ConvertType(const TypeDecl *T) {
John McCallbff225e2010-02-16 04:15:37 +00001403 return ConvertType(getContext().getTypeDeclType(T));
1404 }
Chris Lattnerc8aa5f12008-04-04 04:07:35 +00001405
Mike Stump0dd9e882009-02-08 23:14:22 +00001406 /// LoadObjCSelf - Load the value of self. This function is only valid while
1407 /// generating code for an Objective-C method.
Chris Lattnerc8aa5f12008-04-04 04:07:35 +00001408 llvm::Value *LoadObjCSelf();
Mike Stump0dd9e882009-02-08 23:14:22 +00001409
1410 /// TypeOfSelfObject - Return type of object that this self represents.
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001411 QualType TypeOfSelfObject();
Chris Lattner41110242008-06-17 18:05:57 +00001412
Reid Spencer5f016e22007-07-11 17:01:13 +00001413 /// hasAggregateLLVMType - Return true if the specified AST type will map into
1414 /// an aggregate LLVM type or is void.
1415 static bool hasAggregateLLVMType(QualType T);
Daniel Dunbar55e87422008-11-11 02:29:29 +00001416
1417 /// createBasicBlock - Create an LLVM basic block.
Chris Lattner686775d2011-07-20 06:58:45 +00001418 llvm::BasicBlock *createBasicBlock(StringRef name = "",
John McCalld16c2cf2011-02-08 08:22:06 +00001419 llvm::Function *parent = 0,
1420 llvm::BasicBlock *before = 0) {
Daniel Dunbar29ea6722008-11-12 00:01:12 +00001421#ifdef NDEBUG
John McCalld16c2cf2011-02-08 08:22:06 +00001422 return llvm::BasicBlock::Create(getLLVMContext(), "", parent, before);
Daniel Dunbar29ea6722008-11-12 00:01:12 +00001423#else
John McCalld16c2cf2011-02-08 08:22:06 +00001424 return llvm::BasicBlock::Create(getLLVMContext(), name, parent, before);
Daniel Dunbar29ea6722008-11-12 00:01:12 +00001425#endif
Daniel Dunbar55e87422008-11-11 02:29:29 +00001426 }
Mike Stump0dd9e882009-02-08 23:14:22 +00001427
Reid Spencer5f016e22007-07-11 17:01:13 +00001428 /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
1429 /// label maps to.
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001430 JumpDest getJumpDestForLabel(const LabelDecl *S);
Mike Stump0dd9e882009-02-08 23:14:22 +00001431
Mike Stumpf71d2322009-11-30 20:08:49 +00001432 /// SimplifyForwardingBlocks - If the given basic block is only a branch to
1433 /// another basic block, simplify it. This assumes that no other code could
1434 /// potentially reference the basic block.
Daniel Dunbaraa5bd872009-04-01 04:37:47 +00001435 void SimplifyForwardingBlocks(llvm::BasicBlock *BB);
1436
Mike Stump0dd9e882009-02-08 23:14:22 +00001437 /// EmitBlock - Emit the given block \arg BB and set it as the insert point,
1438 /// adding a fall-through branch from the current insert block if
1439 /// necessary. It is legal to call this function even if there is no current
1440 /// insertion point.
Daniel Dunbara0c21a82008-11-13 01:24:05 +00001441 ///
Mike Stump0dd9e882009-02-08 23:14:22 +00001442 /// IsFinished - If true, indicates that the caller has finished emitting
1443 /// branches to the given block and does not expect to emit code into it. This
1444 /// means the block can be ignored if it is unreachable.
Daniel Dunbara0c21a82008-11-13 01:24:05 +00001445 void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false);
Daniel Dunbar824e3bd2008-11-11 04:34:23 +00001446
Mike Stump0dd9e882009-02-08 23:14:22 +00001447 /// EmitBranch - Emit a branch to the specified basic block from the current
1448 /// insert block, taking care to avoid creation of branches from dummy
1449 /// blocks. It is legal to call this function even if there is no current
1450 /// insertion point.
Daniel Dunbar5e08ad32008-11-11 22:06:59 +00001451 ///
Mike Stump0dd9e882009-02-08 23:14:22 +00001452 /// This function clears the current insertion point. The caller should follow
1453 /// calls to this function with calls to Emit*Block prior to generation new
1454 /// code.
Daniel Dunbard57a8712008-11-11 09:41:28 +00001455 void EmitBranch(llvm::BasicBlock *Block);
1456
Mike Stump0dd9e882009-02-08 23:14:22 +00001457 /// HaveInsertPoint - True if an insertion point is defined. If not, this
1458 /// indicates that the current code being emitted is unreachable.
1459 bool HaveInsertPoint() const {
Daniel Dunbara448fb22008-11-11 23:11:34 +00001460 return Builder.GetInsertBlock() != 0;
1461 }
1462
Mike Stump0dd9e882009-02-08 23:14:22 +00001463 /// EnsureInsertPoint - Ensure that an insertion point is defined so that
1464 /// emitted IR has a place to go. Note that by definition, if this function
1465 /// creates a block then that block is unreachable; callers may do better to
1466 /// detect when no insertion point is defined and simply skip IR generation.
Daniel Dunbara448fb22008-11-11 23:11:34 +00001467 void EnsureInsertPoint() {
1468 if (!HaveInsertPoint())
1469 EmitBlock(createBasicBlock());
1470 }
Mike Stump0dd9e882009-02-08 23:14:22 +00001471
Daniel Dunbar488e9932008-08-16 00:56:44 +00001472 /// ErrorUnsupported - Print out an error that codegen doesn't support the
Chris Lattnerdc5e8262007-12-02 01:43:38 +00001473 /// specified stmt yet.
Daniel Dunbar90df4b62008-09-04 03:43:08 +00001474 void ErrorUnsupported(const Stmt *S, const char *Type,
1475 bool OmitOnError=false);
Reid Spencer5f016e22007-07-11 17:01:13 +00001476
1477 //===--------------------------------------------------------------------===//
1478 // Helpers
1479 //===--------------------------------------------------------------------===//
Mike Stump0dd9e882009-02-08 23:14:22 +00001480
Daniel Dunbar983e3d72010-08-21 04:20:22 +00001481 LValue MakeAddrLValue(llvm::Value *V, QualType T, unsigned Alignment = 0) {
Dan Gohman3d5aff52010-10-14 23:06:10 +00001482 return LValue::MakeAddr(V, T, Alignment, getContext(),
1483 CGM.getTBAAInfo(T));
Daniel Dunbar5cf8bfe2010-08-21 02:53:44 +00001484 }
1485
Reid Spencer5f016e22007-07-11 17:01:13 +00001486 /// CreateTempAlloca - This creates a alloca and inserts it into the entry
Daniel Dunbar195337d2010-02-09 02:48:28 +00001487 /// block. The caller is responsible for setting an appropriate alignment on
1488 /// the alloca.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001489 llvm::AllocaInst *CreateTempAlloca(llvm::Type *Ty,
Chris Lattner8cc488f2011-07-20 07:06:53 +00001490 const Twine &Name = "tmp");
Mike Stump0dd9e882009-02-08 23:14:22 +00001491
John McCallac418162010-04-22 01:10:34 +00001492 /// InitTempAlloca - Provide an initial value for the given alloca.
1493 void InitTempAlloca(llvm::AllocaInst *Alloca, llvm::Value *Value);
1494
Daniel Dunbar9bd4da22010-02-16 19:44:13 +00001495 /// CreateIRTemp - Create a temporary IR object of the given type, with
1496 /// appropriate alignment. This routine should only be used when an temporary
1497 /// value needs to be stored into an alloca (for example, to avoid explicit
1498 /// PHI construction), but the type is the IR type, not the type appropriate
1499 /// for storing in memory.
Chris Lattner8cc488f2011-07-20 07:06:53 +00001500 llvm::AllocaInst *CreateIRTemp(QualType T, const Twine &Name = "tmp");
Daniel Dunbar9bd4da22010-02-16 19:44:13 +00001501
Daniel Dunbar195337d2010-02-09 02:48:28 +00001502 /// CreateMemTemp - Create a temporary memory object of the given type, with
1503 /// appropriate alignment.
Chris Lattner8cc488f2011-07-20 07:06:53 +00001504 llvm::AllocaInst *CreateMemTemp(QualType T, const Twine &Name = "tmp");
Daniel Dunbar195337d2010-02-09 02:48:28 +00001505
John McCall558d2ab2010-09-15 10:14:12 +00001506 /// CreateAggTemp - Create a temporary memory object for the given
1507 /// aggregate type.
Chris Lattner8cc488f2011-07-20 07:06:53 +00001508 AggValueSlot CreateAggTemp(QualType T, const Twine &Name = "tmp") {
John McCallf85e1932011-06-15 23:02:42 +00001509 return AggValueSlot::forAddr(CreateMemTemp(T, Name), T.getQualifiers(),
1510 false);
John McCall558d2ab2010-09-15 10:14:12 +00001511 }
1512
John McCalld16c2cf2011-02-08 08:22:06 +00001513 /// Emit a cast to void* in the appropriate address space.
1514 llvm::Value *EmitCastToVoidPtr(llvm::Value *value);
1515
Reid Spencer5f016e22007-07-11 17:01:13 +00001516 /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
1517 /// expression and compare the result against zero, returning an Int1Ty value.
1518 llvm::Value *EvaluateExprAsBool(const Expr *E);
1519
John McCall2a416372010-12-05 02:00:02 +00001520 /// EmitIgnoredExpr - Emit an expression in a context which ignores the result.
1521 void EmitIgnoredExpr(const Expr *E);
1522
Chris Lattner9b655512007-08-31 22:49:20 +00001523 /// EmitAnyExpr - Emit code to compute the specified expression which can have
1524 /// any type. The result is returned as an RValue struct. If this is an
1525 /// aggregate expression, the aggloc/agglocvolatile arguments indicate where
1526 /// the result should be returned.
Mike Stump49d1cd52009-05-26 22:03:21 +00001527 ///
1528 /// \param IgnoreResult - True if the resulting value isn't used.
John McCall558d2ab2010-09-15 10:14:12 +00001529 RValue EmitAnyExpr(const Expr *E,
1530 AggValueSlot AggSlot = AggValueSlot::ignored(),
1531 bool IgnoreResult = false);
Devang Pateld9363c32007-09-28 21:49:18 +00001532
Mike Stump0dd9e882009-02-08 23:14:22 +00001533 // EmitVAListRef - Emit a "reference" to a va_list; this is either the address
1534 // or the value of the expression, depending on how va_list is defined.
Eli Friedman4fd0aa52009-01-20 17:46:04 +00001535 llvm::Value *EmitVAListRef(const Expr *E);
1536
Mike Stump0dd9e882009-02-08 23:14:22 +00001537 /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
1538 /// always be accessible even if no aggregate location is provided.
John McCall558d2ab2010-09-15 10:14:12 +00001539 RValue EmitAnyExprToTemp(const Expr *E);
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001540
John McCall60d33652011-03-08 09:11:50 +00001541 /// EmitAnyExprToMem - Emits the code necessary to evaluate an
John McCall3d3ec1c2010-04-21 10:05:39 +00001542 /// arbitrary expression into the given memory location.
1543 void EmitAnyExprToMem(const Expr *E, llvm::Value *Location,
John McCallf85e1932011-06-15 23:02:42 +00001544 Qualifiers Quals, bool IsInitializer);
John McCall3d3ec1c2010-04-21 10:05:39 +00001545
John McCall60d33652011-03-08 09:11:50 +00001546 /// EmitExprAsInit - Emits the code necessary to initialize a
1547 /// location in memory with the given initializer.
John McCallf85e1932011-06-15 23:02:42 +00001548 void EmitExprAsInit(const Expr *init, const ValueDecl *D,
John McCalla07398e2011-06-16 04:16:24 +00001549 LValue lvalue, bool capturedByInit);
John McCall60d33652011-03-08 09:11:50 +00001550
Mike Stump27fe2e62009-05-23 22:29:41 +00001551 /// EmitAggregateCopy - Emit an aggrate copy.
1552 ///
1553 /// \param isVolatile - True iff either the source or the destination is
1554 /// volatile.
Daniel Dunbar7482d122008-09-09 20:49:46 +00001555 void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
Mike Stump27fe2e62009-05-23 22:29:41 +00001556 QualType EltTy, bool isVolatile=false);
Daniel Dunbar7482d122008-09-09 20:49:46 +00001557
Devang Patel51b09f22007-10-04 23:45:31 +00001558 /// StartBlock - Start new block named N. If insert block is a dummy block
1559 /// then reuse it.
1560 void StartBlock(const char *N);
1561
Lauro Ramos Venancio81373352008-02-26 21:41:45 +00001562 /// GetAddrOfStaticLocalVar - Return the address of a static local variable.
John McCall4c40d982010-08-31 07:33:07 +00001563 llvm::Constant *GetAddrOfStaticLocalVar(const VarDecl *BVD) {
1564 return cast<llvm::Constant>(GetAddrOfLocalVar(BVD));
1565 }
Dan Gohman4f8d1232008-05-22 00:50:06 +00001566
Anders Carlssondde0a942008-09-11 09:15:33 +00001567 /// GetAddrOfLocalVar - Return the address of a local variable.
John McCall4c40d982010-08-31 07:33:07 +00001568 llvm::Value *GetAddrOfLocalVar(const VarDecl *VD) {
1569 llvm::Value *Res = LocalDeclMap[VD];
1570 assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!");
1571 return Res;
1572 }
Mike Stump0dd9e882009-02-08 23:14:22 +00001573
John McCall56ca35d2011-02-17 10:25:35 +00001574 /// getOpaqueLValueMapping - Given an opaque value expression (which
1575 /// must be mapped to an l-value), return its mapping.
1576 const LValue &getOpaqueLValueMapping(const OpaqueValueExpr *e) {
1577 assert(OpaqueValueMapping::shouldBindAsLValue(e));
1578
1579 llvm::DenseMap<const OpaqueValueExpr*,LValue>::iterator
1580 it = OpaqueLValues.find(e);
1581 assert(it != OpaqueLValues.end() && "no mapping for opaque value!");
1582 return it->second;
1583 }
1584
1585 /// getOpaqueRValueMapping - Given an opaque value expression (which
1586 /// must be mapped to an r-value), return its mapping.
1587 const RValue &getOpaqueRValueMapping(const OpaqueValueExpr *e) {
1588 assert(!OpaqueValueMapping::shouldBindAsLValue(e));
1589
1590 llvm::DenseMap<const OpaqueValueExpr*,RValue>::iterator
1591 it = OpaqueRValues.find(e);
1592 assert(it != OpaqueRValues.end() && "no mapping for opaque value!");
John McCalle996ffd2011-02-16 08:02:54 +00001593 return it->second;
1594 }
1595
Dan Gohman4f8d1232008-05-22 00:50:06 +00001596 /// getAccessedFieldNo - Given an encoded value and a result number, return
1597 /// the input field number being accessed.
1598 static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts);
1599
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001600 llvm::BlockAddress *GetAddrOfLabel(const LabelDecl *L);
Chris Lattner3d00fdc2009-10-13 06:55:33 +00001601 llvm::BasicBlock *GetIndirectGotoBlock();
Daniel Dunbar0ffb1252008-08-04 16:51:22 +00001602
Anders Carlsson1884eb02010-05-22 17:35:42 +00001603 /// EmitNullInitialization - Generate code to set a value of the given type to
1604 /// null, If the type contains data member pointers, they will be initialized
1605 /// to -1 in accordance with the Itanium C++ ABI.
1606 void EmitNullInitialization(llvm::Value *DestPtr, QualType Ty);
Anders Carlssonddf7cac2008-11-04 05:30:00 +00001607
1608 // EmitVAArg - Generate code to get an argument from the passed in pointer
1609 // and update it accordingly. The return value is a pointer to the argument.
1610 // FIXME: We should be able to get rid of this method and use the va_arg
Mike Stump0dd9e882009-02-08 23:14:22 +00001611 // instruction in LLVM instead once it works well enough.
Anders Carlssonddf7cac2008-11-04 05:30:00 +00001612 llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty);
Anders Carlssonf666b772008-12-20 20:27:15 +00001613
John McCallbdc4d802011-07-09 01:37:26 +00001614 /// emitArrayLength - Compute the length of an array, even if it's a
1615 /// VLA, and drill down to the base element type.
1616 llvm::Value *emitArrayLength(const ArrayType *arrayType,
1617 QualType &baseType,
1618 llvm::Value *&addr);
1619
John McCallbc8d40d2011-06-24 21:55:10 +00001620 /// EmitVLASize - Capture all the sizes for the VLA expressions in
1621 /// the given variably-modified type and store them in the VLASizeMap.
Daniel Dunbard286f052009-07-19 06:58:07 +00001622 ///
1623 /// This function can be called with a null (unreachable) insert point.
John McCallbc8d40d2011-06-24 21:55:10 +00001624 void EmitVariablyModifiedType(QualType Ty);
Mike Stump0dd9e882009-02-08 23:14:22 +00001625
John McCallbc8d40d2011-06-24 21:55:10 +00001626 /// getVLASize - Returns an LLVM value that corresponds to the size,
1627 /// in non-variably-sized elements, of a variable length array type,
1628 /// plus that largest non-variably-sized element type. Assumes that
1629 /// the type has already been emitted with EmitVariablyModifiedType.
1630 std::pair<llvm::Value*,QualType> getVLASize(const VariableArrayType *vla);
1631 std::pair<llvm::Value*,QualType> getVLASize(QualType vla);
Anders Carlssondcc90d82008-12-12 07:19:02 +00001632
Anders Carlsson5f4307b2009-04-14 16:58:56 +00001633 /// LoadCXXThis - Load the value of 'this'. This function is only valid while
1634 /// generating code for an C++ member function.
John McCall25049412010-02-16 22:04:33 +00001635 llvm::Value *LoadCXXThis() {
1636 assert(CXXThisValue && "no 'this' value for this function");
1637 return CXXThisValue;
1638 }
Mike Stump1eb44332009-09-09 15:08:12 +00001639
Anders Carlssonc997d422010-01-02 01:01:18 +00001640 /// LoadCXXVTT - Load the VTT parameter to base constructors/destructors have
1641 /// virtual bases.
John McCall25049412010-02-16 22:04:33 +00001642 llvm::Value *LoadCXXVTT() {
1643 assert(CXXVTTValue && "no VTT value for this function");
1644 return CXXVTTValue;
1645 }
John McCallbff225e2010-02-16 04:15:37 +00001646
1647 /// GetAddressOfBaseOfCompleteClass - Convert the given pointer to a
Anders Carlsson8561a862010-04-24 23:01:49 +00001648 /// complete class to the given direct base.
1649 llvm::Value *
1650 GetAddressOfDirectBaseInCompleteClass(llvm::Value *Value,
1651 const CXXRecordDecl *Derived,
1652 const CXXRecordDecl *Base,
1653 bool BaseIsVirtual);
Anders Carlssona88ad562010-04-24 21:51:08 +00001654
Mike Stumpf71d2322009-11-30 20:08:49 +00001655 /// GetAddressOfBaseClass - This function will add the necessary delta to the
1656 /// load of 'this' and returns address of the base class.
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001657 llvm::Value *GetAddressOfBaseClass(llvm::Value *Value,
Anders Carlsson8561a862010-04-24 23:01:49 +00001658 const CXXRecordDecl *Derived,
John McCallf871d0c2010-08-07 06:22:56 +00001659 CastExpr::path_const_iterator PathBegin,
1660 CastExpr::path_const_iterator PathEnd,
Anders Carlsson34a2d382010-04-24 21:06:20 +00001661 bool NullCheckValue);
1662
Anders Carlssona3697c92009-11-23 17:57:54 +00001663 llvm::Value *GetAddressOfDerivedClass(llvm::Value *Value,
Anders Carlsson8561a862010-04-24 23:01:49 +00001664 const CXXRecordDecl *Derived,
John McCallf871d0c2010-08-07 06:22:56 +00001665 CastExpr::path_const_iterator PathBegin,
1666 CastExpr::path_const_iterator PathEnd,
Anders Carlssona3697c92009-11-23 17:57:54 +00001667 bool NullCheckValue);
1668
Anders Carlssonbb7e17b2010-01-31 01:36:53 +00001669 llvm::Value *GetVirtualBaseClassOffset(llvm::Value *This,
1670 const CXXRecordDecl *ClassDecl,
1671 const CXXRecordDecl *BaseClassDecl);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001672
John McCallc0bf4622010-02-23 00:48:20 +00001673 void EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor,
1674 CXXCtorType CtorType,
1675 const FunctionArgList &Args);
Sean Hunt059ce0d2011-05-01 07:04:31 +00001676 // It's important not to confuse this and the previous function. Delegating
1677 // constructors are the C++0x feature. The constructor delegate optimization
1678 // is used to reduce duplication in the base and complete consturctors where
1679 // they are substantially the same.
1680 void EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor,
1681 const FunctionArgList &Args);
Anders Carlsson155ed4a2010-05-02 23:20:53 +00001682 void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type,
1683 bool ForVirtualBase, llvm::Value *This,
Anders Carlssonb14095a2009-04-17 00:06:03 +00001684 CallExpr::const_arg_iterator ArgBeg,
1685 CallExpr::const_arg_iterator ArgEnd);
Fariborz Jahanian34999872010-11-13 21:53:34 +00001686
1687 void EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D,
1688 llvm::Value *This, llvm::Value *Src,
1689 CallExpr::const_arg_iterator ArgBeg,
1690 CallExpr::const_arg_iterator ArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001691
Fariborz Jahanian288dcaf2009-08-19 20:55:16 +00001692 void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
Anders Carlsson569c1f42009-09-23 02:45:36 +00001693 const ConstantArrayType *ArrayTy,
Anders Carlsson5d4d9462009-11-24 18:43:52 +00001694 llvm::Value *ArrayPtr,
1695 CallExpr::const_arg_iterator ArgBeg,
Douglas Gregor59174c02010-07-21 01:10:17 +00001696 CallExpr::const_arg_iterator ArgEnd,
1697 bool ZeroInitialization = false);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001698
Anders Carlsson569c1f42009-09-23 02:45:36 +00001699 void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
1700 llvm::Value *NumElements,
Anders Carlsson5d4d9462009-11-24 18:43:52 +00001701 llvm::Value *ArrayPtr,
1702 CallExpr::const_arg_iterator ArgBeg,
Douglas Gregor59174c02010-07-21 01:10:17 +00001703 CallExpr::const_arg_iterator ArgEnd,
1704 bool ZeroInitialization = false);
Anders Carlssonb14095a2009-04-17 00:06:03 +00001705
John McCallbdc4d802011-07-09 01:37:26 +00001706 static Destroyer destroyCXXObject;
1707
Anders Carlsson7267c162009-05-29 21:03:38 +00001708 void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type,
Anders Carlsson8e6404c2010-05-02 23:29:11 +00001709 bool ForVirtualBase, llvm::Value *This);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001710
Fariborz Jahanianef668722010-06-25 18:26:07 +00001711 void EmitNewArrayInitializer(const CXXNewExpr *E, llvm::Value *NewPtr,
1712 llvm::Value *NumElements);
Mike Stump1eb44332009-09-09 15:08:12 +00001713
John McCallf1549f62010-07-06 01:34:17 +00001714 void EmitCXXTemporary(const CXXTemporary *Temporary, llvm::Value *Ptr);
Mike Stump1eb44332009-09-09 15:08:12 +00001715
Anders Carlssona00703d2009-05-31 01:40:14 +00001716 llvm::Value *EmitCXXNewExpr(const CXXNewExpr *E);
Anders Carlsson60e282c2009-08-16 21:13:42 +00001717 void EmitCXXDeleteExpr(const CXXDeleteExpr *E);
Mike Stump1eb44332009-09-09 15:08:12 +00001718
Eli Friedman4bf81522009-11-18 00:57:03 +00001719 void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr,
1720 QualType DeleteTy);
1721
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001722 llvm::Value* EmitCXXTypeidExpr(const CXXTypeidExpr *E);
Mike Stumpc849c052009-11-16 06:50:58 +00001723 llvm::Value *EmitDynamicCast(llvm::Value *V, const CXXDynamicCastExpr *DCE);
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001724
Mike Stumpb14e62d2009-12-16 02:57:00 +00001725 void EmitCheck(llvm::Value *, unsigned Size);
1726
Chris Lattnerdd36d322010-01-09 21:40:03 +00001727 llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
1728 bool isInc, bool isPre);
1729 ComplexPairTy EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
1730 bool isInc, bool isPre);
Reid Spencer5f016e22007-07-11 17:01:13 +00001731 //===--------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00001732 // Declaration Emission
1733 //===--------------------------------------------------------------------===//
Mike Stump0dd9e882009-02-08 23:14:22 +00001734
Daniel Dunbard286f052009-07-19 06:58:07 +00001735 /// EmitDecl - Emit a declaration.
1736 ///
1737 /// This function can be called with a null (unreachable) insert point.
Reid Spencer5f016e22007-07-11 17:01:13 +00001738 void EmitDecl(const Decl &D);
Daniel Dunbard286f052009-07-19 06:58:07 +00001739
John McCallb6bbcc92010-10-15 04:57:14 +00001740 /// EmitVarDecl - Emit a local variable declaration.
Daniel Dunbard286f052009-07-19 06:58:07 +00001741 ///
1742 /// This function can be called with a null (unreachable) insert point.
John McCallb6bbcc92010-10-15 04:57:14 +00001743 void EmitVarDecl(const VarDecl &D);
Daniel Dunbard286f052009-07-19 06:58:07 +00001744
John McCallf85e1932011-06-15 23:02:42 +00001745 void EmitScalarInit(const Expr *init, const ValueDecl *D,
John McCalla07398e2011-06-16 04:16:24 +00001746 LValue lvalue, bool capturedByInit);
John McCall7acddac2011-06-17 06:42:21 +00001747 void EmitScalarInit(llvm::Value *init, LValue lvalue);
John McCallf85e1932011-06-15 23:02:42 +00001748
John McCallf1549f62010-07-06 01:34:17 +00001749 typedef void SpecialInitFn(CodeGenFunction &Init, const VarDecl &D,
1750 llvm::Value *Address);
1751
John McCallb6bbcc92010-10-15 04:57:14 +00001752 /// EmitAutoVarDecl - Emit an auto variable declaration.
Daniel Dunbard286f052009-07-19 06:58:07 +00001753 ///
1754 /// This function can be called with a null (unreachable) insert point.
John McCall34695852011-02-22 06:44:22 +00001755 void EmitAutoVarDecl(const VarDecl &D);
1756
1757 class AutoVarEmission {
1758 friend class CodeGenFunction;
1759
John McCall57b3b6a2011-02-22 07:16:58 +00001760 const VarDecl *Variable;
John McCall34695852011-02-22 06:44:22 +00001761
1762 /// The alignment of the variable.
1763 CharUnits Alignment;
1764
1765 /// The address of the alloca. Null if the variable was emitted
1766 /// as a global constant.
1767 llvm::Value *Address;
1768
1769 llvm::Value *NRVOFlag;
1770
1771 /// True if the variable is a __block variable.
1772 bool IsByRef;
1773
1774 /// True if the variable is of aggregate type and has a constant
1775 /// initializer.
1776 bool IsConstantAggregate;
1777
John McCall57b3b6a2011-02-22 07:16:58 +00001778 struct Invalid {};
1779 AutoVarEmission(Invalid) : Variable(0) {}
1780
John McCall34695852011-02-22 06:44:22 +00001781 AutoVarEmission(const VarDecl &variable)
John McCall57b3b6a2011-02-22 07:16:58 +00001782 : Variable(&variable), Address(0), NRVOFlag(0),
John McCall34695852011-02-22 06:44:22 +00001783 IsByRef(false), IsConstantAggregate(false) {}
1784
1785 bool wasEmittedAsGlobal() const { return Address == 0; }
1786
1787 public:
John McCall57b3b6a2011-02-22 07:16:58 +00001788 static AutoVarEmission invalid() { return AutoVarEmission(Invalid()); }
1789
John McCall34695852011-02-22 06:44:22 +00001790 /// Returns the address of the object within this declaration.
1791 /// Note that this does not chase the forwarding pointer for
1792 /// __block decls.
1793 llvm::Value *getObjectAddress(CodeGenFunction &CGF) const {
1794 if (!IsByRef) return Address;
1795
1796 return CGF.Builder.CreateStructGEP(Address,
John McCall57b3b6a2011-02-22 07:16:58 +00001797 CGF.getByRefValueLLVMField(Variable),
1798 Variable->getNameAsString());
John McCall34695852011-02-22 06:44:22 +00001799 }
1800 };
1801 AutoVarEmission EmitAutoVarAlloca(const VarDecl &var);
1802 void EmitAutoVarInit(const AutoVarEmission &emission);
1803 void EmitAutoVarCleanups(const AutoVarEmission &emission);
John McCallbdc4d802011-07-09 01:37:26 +00001804 void emitAutoVarTypeCleanup(const AutoVarEmission &emission,
1805 QualType::DestructionKind dtorKind);
Daniel Dunbard286f052009-07-19 06:58:07 +00001806
John McCallb6bbcc92010-10-15 04:57:14 +00001807 void EmitStaticVarDecl(const VarDecl &D,
1808 llvm::GlobalValue::LinkageTypes Linkage);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00001809
1810 /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
Devang Patel093ac462011-03-03 20:13:15 +00001811 void EmitParmDecl(const VarDecl &D, llvm::Value *Arg, unsigned ArgNo);
Mike Stump0dd9e882009-02-08 23:14:22 +00001812
John McCall56ca35d2011-02-17 10:25:35 +00001813 /// protectFromPeepholes - Protect a value that we're intending to
1814 /// store to the side, but which will probably be used later, from
1815 /// aggressive peepholing optimizations that might delete it.
1816 ///
1817 /// Pass the result to unprotectFromPeepholes to declare that
1818 /// protection is no longer required.
1819 ///
1820 /// There's no particular reason why this shouldn't apply to
1821 /// l-values, it's just that no existing peepholes work on pointers.
1822 PeepholeProtection protectFromPeepholes(RValue rvalue);
1823 void unprotectFromPeepholes(PeepholeProtection protection);
1824
Reid Spencer5f016e22007-07-11 17:01:13 +00001825 //===--------------------------------------------------------------------===//
1826 // Statement Emission
1827 //===--------------------------------------------------------------------===//
1828
Mike Stump0dd9e882009-02-08 23:14:22 +00001829 /// EmitStopPoint - Emit a debug stoppoint if we are emitting debug info.
Daniel Dunbar09124252008-11-12 08:21:33 +00001830 void EmitStopPoint(const Stmt *S);
1831
Mike Stump0dd9e882009-02-08 23:14:22 +00001832 /// EmitStmt - Emit the code for the statement \arg S. It is legal to call
1833 /// this function even if there is no current insertion point.
1834 ///
1835 /// This function may clear the current insertion point; callers should use
1836 /// EnsureInsertPoint if they wish to subsequently generate code without first
1837 /// calling EmitBlock, EmitBranch, or EmitStmt.
Reid Spencer5f016e22007-07-11 17:01:13 +00001838 void EmitStmt(const Stmt *S);
Daniel Dunbara448fb22008-11-11 23:11:34 +00001839
Daniel Dunbar09124252008-11-12 08:21:33 +00001840 /// EmitSimpleStmt - Try to emit a "simple" statement which does not
Mike Stump0dd9e882009-02-08 23:14:22 +00001841 /// necessarily require an insertion point or debug information; typically
1842 /// because the statement amounts to a jump or a container of other
1843 /// statements.
Daniel Dunbar09124252008-11-12 08:21:33 +00001844 ///
1845 /// \return True if the statement was handled.
1846 bool EmitSimpleStmt(const Stmt *S);
1847
Chris Lattner9b655512007-08-31 22:49:20 +00001848 RValue EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false,
John McCall558d2ab2010-09-15 10:14:12 +00001849 AggValueSlot AVS = AggValueSlot::ignored());
Daniel Dunbara448fb22008-11-11 23:11:34 +00001850
Mike Stump0dd9e882009-02-08 23:14:22 +00001851 /// EmitLabel - Emit the block for the given label. It is legal to call this
1852 /// function even if there is no current insertion point.
Chris Lattnerad8dcf42011-02-17 07:39:24 +00001853 void EmitLabel(const LabelDecl *D); // helper for EmitLabelStmt.
Daniel Dunbara448fb22008-11-11 23:11:34 +00001854
Reid Spencer5f016e22007-07-11 17:01:13 +00001855 void EmitLabelStmt(const LabelStmt &S);
1856 void EmitGotoStmt(const GotoStmt &S);
Daniel Dunbar0ffb1252008-08-04 16:51:22 +00001857 void EmitIndirectGotoStmt(const IndirectGotoStmt &S);
Reid Spencer5f016e22007-07-11 17:01:13 +00001858 void EmitIfStmt(const IfStmt &S);
1859 void EmitWhileStmt(const WhileStmt &S);
1860 void EmitDoStmt(const DoStmt &S);
1861 void EmitForStmt(const ForStmt &S);
1862 void EmitReturnStmt(const ReturnStmt &S);
1863 void EmitDeclStmt(const DeclStmt &S);
Daniel Dunbar09124252008-11-12 08:21:33 +00001864 void EmitBreakStmt(const BreakStmt &S);
1865 void EmitContinueStmt(const ContinueStmt &S);
Devang Patel51b09f22007-10-04 23:45:31 +00001866 void EmitSwitchStmt(const SwitchStmt &S);
1867 void EmitDefaultStmt(const DefaultStmt &S);
1868 void EmitCaseStmt(const CaseStmt &S);
Devang Patelc049e4f2007-10-08 20:57:48 +00001869 void EmitCaseStmtRange(const CaseStmt &S);
Anders Carlssonfb1aeb82008-02-05 16:35:33 +00001870 void EmitAsmStmt(const AsmStmt &S);
Mike Stump0dd9e882009-02-08 23:14:22 +00001871
Anders Carlsson3d8400d2008-08-30 19:51:14 +00001872 void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001873 void EmitObjCAtTryStmt(const ObjCAtTryStmt &S);
1874 void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S);
Chris Lattner10cac6f2008-11-15 21:26:17 +00001875 void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S);
John McCallf85e1932011-06-15 23:02:42 +00001876 void EmitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt &S);
Mike Stump0dd9e882009-02-08 23:14:22 +00001877
John McCall93c332a2011-05-28 21:13:02 +00001878 llvm::Constant *getUnwindResumeFn();
Douglas Gregor86a3a032010-05-16 01:24:12 +00001879 llvm::Constant *getUnwindResumeOrRethrowFn();
John McCall59a70002010-07-07 06:56:46 +00001880 void EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
1881 void ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
John McCall9fc6a772010-02-19 09:25:03 +00001882
Anders Carlsson6815e942009-09-27 18:58:34 +00001883 void EmitCXXTryStmt(const CXXTryStmt &S);
Richard Smithad762fc2011-04-14 22:09:26 +00001884 void EmitCXXForRangeStmt(const CXXForRangeStmt &S);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00001885
Reid Spencer5f016e22007-07-11 17:01:13 +00001886 //===--------------------------------------------------------------------===//
1887 // LValue Expression Emission
1888 //===--------------------------------------------------------------------===//
1889
Daniel Dunbar13e81732009-02-05 07:09:07 +00001890 /// GetUndefRValue - Get an appropriate 'undef' rvalue for the given type.
1891 RValue GetUndefRValue(QualType Ty);
1892
Daniel Dunbarce1d38b2009-01-09 16:50:52 +00001893 /// EmitUnsupportedRValue - Emit a dummy r-value using the type of E
1894 /// and issue an ErrorUnsupported style diagnostic (using the
1895 /// provided Name).
1896 RValue EmitUnsupportedRValue(const Expr *E,
1897 const char *Name);
1898
Mike Stump0dd9e882009-02-08 23:14:22 +00001899 /// EmitUnsupportedLValue - Emit a dummy l-value using the type of E and issue
1900 /// an ErrorUnsupported style diagnostic (using the provided Name).
Daniel Dunbar6ba82a42008-08-25 20:45:57 +00001901 LValue EmitUnsupportedLValue(const Expr *E,
1902 const char *Name);
1903
Reid Spencer5f016e22007-07-11 17:01:13 +00001904 /// EmitLValue - Emit code to compute a designator that specifies the location
1905 /// of the expression.
1906 ///
1907 /// This can return one of two things: a simple address or a bitfield
1908 /// reference. In either case, the LLVM Value* in the LValue structure is
1909 /// guaranteed to be an LLVM pointer type.
1910 ///
1911 /// If this returns a bitfield reference, nothing about the pointee type of
1912 /// the LLVM value is known: For example, it may not be a pointer to an
1913 /// integer.
1914 ///
1915 /// If this returns a normal address, and if the lvalue's C type is fixed
1916 /// size, this method guarantees that the returned pointer type will point to
1917 /// an LLVM type of the same size of the lvalue's type. If the lvalue has a
1918 /// variable length type, this is not possible.
1919 ///
1920 LValue EmitLValue(const Expr *E);
Mike Stump0dd9e882009-02-08 23:14:22 +00001921
Mike Stumpb14e62d2009-12-16 02:57:00 +00001922 /// EmitCheckedLValue - Same as EmitLValue but additionally we generate
1923 /// checking code to guard against undefined behavior. This is only
1924 /// suitable when we know that the address will be used to access the
1925 /// object.
1926 LValue EmitCheckedLValue(const Expr *E);
1927
John McCall26815d92010-10-27 20:58:56 +00001928 /// EmitToMemory - Change a scalar value from its value
1929 /// representation to its in-memory representation.
1930 llvm::Value *EmitToMemory(llvm::Value *Value, QualType Ty);
1931
1932 /// EmitFromMemory - Change a scalar value from its memory
1933 /// representation to its value representation.
1934 llvm::Value *EmitFromMemory(llvm::Value *Value, QualType Ty);
1935
Daniel Dunbar9d9cc872009-02-10 00:57:50 +00001936 /// EmitLoadOfScalar - Load a scalar value from an address, taking
1937 /// care to appropriately convert from the memory representation to
1938 /// the LLVM value representation.
Mike Stump09429b92009-02-17 17:00:02 +00001939 llvm::Value *EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
Dan Gohman3d5aff52010-10-14 23:06:10 +00001940 unsigned Alignment, QualType Ty,
1941 llvm::MDNode *TBAAInfo = 0);
John McCall545d9962011-06-25 02:11:03 +00001942
1943 /// EmitLoadOfScalar - Load a scalar value from an address, taking
1944 /// care to appropriately convert from the memory representation to
1945 /// the LLVM value representation. The l-value must be a simple
1946 /// l-value.
John McCalla07398e2011-06-16 04:16:24 +00001947 llvm::Value *EmitLoadOfScalar(LValue lvalue);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +00001948
1949 /// EmitStoreOfScalar - Store a scalar value to an address, taking
1950 /// care to appropriately convert from the memory representation to
1951 /// the LLVM value representation.
Mike Stump09429b92009-02-17 17:00:02 +00001952 void EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
Dan Gohman3d5aff52010-10-14 23:06:10 +00001953 bool Volatile, unsigned Alignment, QualType Ty,
1954 llvm::MDNode *TBAAInfo = 0);
John McCall545d9962011-06-25 02:11:03 +00001955
1956 /// EmitStoreOfScalar - Store a scalar value to an address, taking
1957 /// care to appropriately convert from the memory representation to
1958 /// the LLVM value representation. The l-value must be a simple
1959 /// l-value.
John McCalla07398e2011-06-16 04:16:24 +00001960 void EmitStoreOfScalar(llvm::Value *value, LValue lvalue);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +00001961
Reid Spencer5f016e22007-07-11 17:01:13 +00001962 /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
1963 /// this method emits the address of the lvalue, then loads the result as an
1964 /// rvalue, returning the rvalue.
John McCall545d9962011-06-25 02:11:03 +00001965 RValue EmitLoadOfLValue(LValue V);
1966 RValue EmitLoadOfExtVectorElementLValue(LValue V);
1967 RValue EmitLoadOfBitfieldLValue(LValue LV);
John McCall119a1c62010-12-04 02:32:38 +00001968 RValue EmitLoadOfPropertyRefLValue(LValue LV,
1969 ReturnValueSlot Return = ReturnValueSlot());
Mike Stump0dd9e882009-02-08 23:14:22 +00001970
Reid Spencer5f016e22007-07-11 17:01:13 +00001971 /// EmitStoreThroughLValue - Store the specified rvalue into the specified
1972 /// lvalue, where both are guaranteed to the have the same type, and that type
1973 /// is 'Ty'.
John McCall545d9962011-06-25 02:11:03 +00001974 void EmitStoreThroughLValue(RValue Src, LValue Dst);
1975 void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst);
John McCall119a1c62010-12-04 02:32:38 +00001976 void EmitStoreThroughPropertyRefLValue(RValue Src, LValue Dst);
Daniel Dunbared3849b2008-11-19 09:36:46 +00001977
Mike Stump0dd9e882009-02-08 23:14:22 +00001978 /// EmitStoreThroughLValue - Store Src into Dst with same constraints as
1979 /// EmitStoreThroughLValue.
Daniel Dunbared3849b2008-11-19 09:36:46 +00001980 ///
Mike Stump0dd9e882009-02-08 23:14:22 +00001981 /// \param Result [out] - If non-null, this will be set to a Value* for the
1982 /// bit-field contents after the store, appropriate for use as the result of
1983 /// an assignment to the bit-field.
John McCall545d9962011-06-25 02:11:03 +00001984 void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Daniel Dunbared3849b2008-11-19 09:36:46 +00001985 llvm::Value **Result=0);
Mike Stump0dd9e882009-02-08 23:14:22 +00001986
John McCall83ce9d42010-11-16 23:07:28 +00001987 /// Emit an l-value for an assignment (simple or compound) of complex type.
1988 LValue EmitComplexAssignmentLValue(const BinaryOperator *E);
John McCall2a416372010-12-05 02:00:02 +00001989 LValue EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E);
John McCall83ce9d42010-11-16 23:07:28 +00001990
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00001991 // Note: only available for agg return types
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001992 LValue EmitBinaryOperatorLValue(const BinaryOperator *E);
John McCall2a416372010-12-05 02:00:02 +00001993 LValue EmitCompoundAssignmentLValue(const CompoundAssignOperator *E);
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +00001994 // Note: only available for agg return types
Christopher Lamb22c940e2007-12-29 05:02:41 +00001995 LValue EmitCallExprLValue(const CallExpr *E);
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +00001996 // Note: only available for agg return types
1997 LValue EmitVAArgExprLValue(const VAArgExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +00001998 LValue EmitDeclRefLValue(const DeclRefExpr *E);
1999 LValue EmitStringLiteralLValue(const StringLiteral *E);
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002000 LValue EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E);
Chris Lattnerd9f69102008-08-10 01:53:14 +00002001 LValue EmitPredefinedLValue(const PredefinedExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +00002002 LValue EmitUnaryOpLValue(const UnaryOperator *E);
2003 LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
Nate Begeman213541a2008-04-18 23:10:10 +00002004 LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E);
Devang Patelb84a06e2007-10-23 02:10:49 +00002005 LValue EmitMemberExpr(const MemberExpr *E);
Fariborz Jahanian820bca42009-12-09 23:35:29 +00002006 LValue EmitObjCIsaExpr(const ObjCIsaExpr *E);
Eli Friedman06e863f2008-05-13 23:18:27 +00002007 LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E);
John McCall56ca35d2011-02-17 10:25:35 +00002008 LValue EmitConditionalOperatorLValue(const AbstractConditionalOperator *E);
Chris Lattner75dfeda2009-03-18 18:28:57 +00002009 LValue EmitCastLValue(const CastExpr *E);
Douglas Gregored8abf12010-07-08 06:14:04 +00002010 LValue EmitNullInitializationLValue(const CXXScalarValueInitExpr *E);
Douglas Gregor03e80032011-06-21 17:03:29 +00002011 LValue EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
John McCalle996ffd2011-02-16 08:02:54 +00002012 LValue EmitOpaqueValueLValue(const OpaqueValueExpr *e);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002013
Daniel Dunbar2a031922009-04-22 05:08:15 +00002014 llvm::Value *EmitIvarOffset(const ObjCInterfaceDecl *Interface,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00002015 const ObjCIvarDecl *Ivar);
John McCalla9976d32010-05-21 01:18:57 +00002016 LValue EmitLValueForAnonRecordField(llvm::Value* Base,
Francois Pichet00eb3f92010-12-04 09:14:42 +00002017 const IndirectFieldDecl* Field,
John McCalla9976d32010-05-21 01:18:57 +00002018 unsigned CVRQualifiers);
Anders Carlsson0ed303c2009-11-17 03:57:07 +00002019 LValue EmitLValueForField(llvm::Value* Base, const FieldDecl* Field,
Anders Carlssone6d2a532010-01-29 05:05:36 +00002020 unsigned CVRQualifiers);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002021
Anders Carlsson06a29702010-01-29 05:24:29 +00002022 /// EmitLValueForFieldInitialization - Like EmitLValueForField, except that
2023 /// if the Field is a reference, this will return the address of the reference
2024 /// and not the address of the value stored in the reference.
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002025 LValue EmitLValueForFieldInitialization(llvm::Value* Base,
Anders Carlsson06a29702010-01-29 05:24:29 +00002026 const FieldDecl* Field,
2027 unsigned CVRQualifiers);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002028
Fariborz Jahanian45012a72009-02-03 00:09:52 +00002029 LValue EmitLValueForIvar(QualType ObjectTy,
2030 llvm::Value* Base, const ObjCIvarDecl *Ivar,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00002031 unsigned CVRQualifiers);
2032
Anders Carlsson0ed303c2009-11-17 03:57:07 +00002033 LValue EmitLValueForBitfield(llvm::Value* Base, const FieldDecl* Field,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002034 unsigned CVRQualifiers);
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00002035
Mike Stumpa99038c2009-02-28 09:07:16 +00002036 LValue EmitBlockDeclRefLValue(const BlockDeclRefExpr *E);
2037
Anders Carlssonb58d0172009-05-30 23:23:33 +00002038 LValue EmitCXXConstructLValue(const CXXConstructExpr *E);
Anders Carlssone61c9e82009-05-30 23:30:54 +00002039 LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E);
John McCall4765fa02010-12-06 08:20:24 +00002040 LValue EmitExprWithCleanupsLValue(const ExprWithCleanups *E);
Mike Stumpc2e84ae2009-11-15 08:09:41 +00002041 LValue EmitCXXTypeidLValue(const CXXTypeidExpr *E);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002042
Daniel Dunbar0a04d772008-08-23 10:51:21 +00002043 LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E);
Chris Lattner391d77a2008-03-30 23:03:07 +00002044 LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E);
Daniel Dunbar85c59ed2008-08-29 08:11:39 +00002045 LValue EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E);
Chris Lattner65459942009-04-25 19:35:26 +00002046 LValue EmitStmtExprLValue(const StmtExpr *E);
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00002047 LValue EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E);
Fariborz Jahanian03b29602010-06-17 19:56:20 +00002048 LValue EmitObjCSelectorLValue(const ObjCSelectorExpr *E);
John McCall189d6ef2010-10-09 01:34:31 +00002049 void EmitDeclRefExprDbgValue(const DeclRefExpr *E, llvm::Constant *Init);
John McCall56ca35d2011-02-17 10:25:35 +00002050
Reid Spencer5f016e22007-07-11 17:01:13 +00002051 //===--------------------------------------------------------------------===//
Chris Lattner883f6a72007-08-11 00:04:45 +00002052 // Scalar Expression Emission
Reid Spencer5f016e22007-07-11 17:01:13 +00002053 //===--------------------------------------------------------------------===//
2054
Mike Stump0dd9e882009-02-08 23:14:22 +00002055 /// EmitCall - Generate a call of the given function, expecting the given
2056 /// result type, and using the given argument list which specifies both the
2057 /// LLVM arguments and the types they were derived from.
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00002058 ///
Mike Stumpf71d2322009-11-30 20:08:49 +00002059 /// \param TargetDecl - If given, the decl of the function in a direct call;
2060 /// used to set attributes on the call (noreturn, etc.).
Daniel Dunbar88b53962009-02-02 22:03:45 +00002061 RValue EmitCall(const CGFunctionInfo &FnInfo,
2062 llvm::Value *Callee,
Anders Carlssonf3c47c92009-12-24 19:25:24 +00002063 ReturnValueSlot ReturnValue,
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00002064 const CallArgList &Args,
David Chisnalldd5c98f2010-05-01 11:15:56 +00002065 const Decl *TargetDecl = 0,
David Chisnall4b02afc2010-05-02 13:41:58 +00002066 llvm::Instruction **callOrInvoke = 0);
Mike Stump1eb44332009-09-09 15:08:12 +00002067
Anders Carlsson31777a22009-12-24 19:08:58 +00002068 RValue EmitCall(QualType FnType, llvm::Value *Callee,
Anders Carlssond2490a92009-12-24 20:40:36 +00002069 ReturnValueSlot ReturnValue,
Anders Carlsson98647712009-05-27 01:22:39 +00002070 CallExpr::const_arg_iterator ArgBeg,
2071 CallExpr::const_arg_iterator ArgEnd,
2072 const Decl *TargetDecl = 0);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002073 RValue EmitCallExpr(const CallExpr *E,
Anders Carlssond2490a92009-12-24 20:40:36 +00002074 ReturnValueSlot ReturnValue = ReturnValueSlot());
Mike Stump1eb44332009-09-09 15:08:12 +00002075
John McCallf1549f62010-07-06 01:34:17 +00002076 llvm::CallSite EmitCallOrInvoke(llvm::Value *Callee,
Chris Lattner2d3ba4f2011-07-23 17:14:25 +00002077 ArrayRef<llvm::Value *> Args,
Chris Lattner8cc488f2011-07-20 07:06:53 +00002078 const Twine &Name = "");
Jay Foad4c7d9f12011-07-15 08:37:34 +00002079 llvm::CallSite EmitCallOrInvoke(llvm::Value *Callee,
Chris Lattner8cc488f2011-07-20 07:06:53 +00002080 const Twine &Name = "");
John McCallf1549f62010-07-06 01:34:17 +00002081
Anders Carlsson566abee2009-11-13 04:45:41 +00002082 llvm::Value *BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This,
Chris Lattner2acc6e32011-07-18 04:24:23 +00002083 llvm::Type *Ty);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002084 llvm::Value *BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type,
Chris Lattner2acc6e32011-07-18 04:24:23 +00002085 llvm::Value *This, llvm::Type *Ty);
Fariborz Jahanian27262672011-01-20 17:19:02 +00002086 llvm::Value *BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
2087 NestedNameSpecifier *Qual,
Chris Lattner2acc6e32011-07-18 04:24:23 +00002088 llvm::Type *Ty);
Fariborz Jahanianccd52592011-02-01 23:22:34 +00002089
2090 llvm::Value *BuildAppleKextVirtualDestructorCall(const CXXDestructorDecl *DD,
2091 CXXDtorType Type,
Fariborz Jahanian771c6782011-02-03 19:27:17 +00002092 const CXXRecordDecl *RD);
Anders Carlsson566abee2009-11-13 04:45:41 +00002093
Anders Carlssonb9de2c52009-05-11 23:37:08 +00002094 RValue EmitCXXMemberCall(const CXXMethodDecl *MD,
2095 llvm::Value *Callee,
Anders Carlssona1736c02009-12-24 21:13:40 +00002096 ReturnValueSlot ReturnValue,
Anders Carlssonb9de2c52009-05-11 23:37:08 +00002097 llvm::Value *This,
Anders Carlssonc997d422010-01-02 01:01:18 +00002098 llvm::Value *VTT,
Anders Carlssonb9de2c52009-05-11 23:37:08 +00002099 CallExpr::const_arg_iterator ArgBeg,
2100 CallExpr::const_arg_iterator ArgEnd);
Anders Carlssona1736c02009-12-24 21:13:40 +00002101 RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E,
2102 ReturnValueSlot ReturnValue);
2103 RValue EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
2104 ReturnValueSlot ReturnValue);
Ted Kremenek55499762008-06-17 02:43:46 +00002105
Anders Carlssona2447e02011-05-08 20:32:23 +00002106 llvm::Value *EmitCXXOperatorMemberCallee(const CXXOperatorCallExpr *E,
2107 const CXXMethodDecl *MD,
2108 llvm::Value *This);
Anders Carlsson0f294632009-05-27 04:18:27 +00002109 RValue EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
Anders Carlssona1736c02009-12-24 21:13:40 +00002110 const CXXMethodDecl *MD,
2111 ReturnValueSlot ReturnValue);
Mike Stump1eb44332009-09-09 15:08:12 +00002112
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002113
Mike Stump1eb44332009-09-09 15:08:12 +00002114 RValue EmitBuiltinExpr(const FunctionDecl *FD,
Daniel Dunbaref2abfe2009-02-16 22:43:43 +00002115 unsigned BuiltinID, const CallExpr *E);
Reid Spencer5f016e22007-07-11 17:01:13 +00002116
Anders Carlssona1736c02009-12-24 21:13:40 +00002117 RValue EmitBlockCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue);
Mike Stump09429b92009-02-17 17:00:02 +00002118
Mike Stump0dd9e882009-02-08 23:14:22 +00002119 /// EmitTargetBuiltinExpr - Emit the given builtin call. Returns 0 if the call
2120 /// is unhandled by the current target.
Daniel Dunbarf02e9dd2008-10-10 00:24:54 +00002121 llvm::Value *EmitTargetBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2122
Chris Lattner2752c012010-03-03 19:03:45 +00002123 llvm::Value *EmitARMBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002124 llvm::Value *EmitNeonCall(llvm::Function *F,
Chris Lattner686775d2011-07-20 06:58:45 +00002125 SmallVectorImpl<llvm::Value*> &O,
Bob Wilsondb3d4d02010-12-08 22:37:56 +00002126 const char *name,
Nate Begeman61eecf52010-06-14 05:21:25 +00002127 unsigned shift = 0, bool rightshift = false);
Bob Wilsoncf556522010-12-07 22:40:02 +00002128 llvm::Value *EmitNeonSplat(llvm::Value *V, llvm::Constant *Idx);
Chris Lattner2acc6e32011-07-18 04:24:23 +00002129 llvm::Value *EmitNeonShiftVector(llvm::Value *V, llvm::Type *Ty,
Nate Begeman61eecf52010-06-14 05:21:25 +00002130 bool negateForRightShift);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002131
Chris Lattner686775d2011-07-20 06:58:45 +00002132 llvm::Value *BuildVector(const SmallVectorImpl<llvm::Value*> &Ops);
Anders Carlsson564f1de2007-12-09 23:17:02 +00002133 llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2134 llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
Mike Stump0dd9e882009-02-08 23:14:22 +00002135
Daniel Dunbared7c6182008-08-20 00:28:19 +00002136 llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E);
Chris Lattner7f02f722007-08-24 05:35:26 +00002137 llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E);
Chris Lattner8fdf3282008-06-24 17:04:18 +00002138 llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E);
John McCallef072fd2010-05-22 01:48:05 +00002139 RValue EmitObjCMessageExpr(const ObjCMessageExpr *E,
2140 ReturnValueSlot Return = ReturnValueSlot());
Chris Lattner8fdf3282008-06-24 17:04:18 +00002141
John McCallf85e1932011-06-15 23:02:42 +00002142 /// Retrieves the default cleanup kind for an ARC cleanup.
2143 /// Except under -fobjc-arc-eh, ARC cleanups are normal-only.
2144 CleanupKind getARCCleanupKind() {
2145 return CGM.getCodeGenOpts().ObjCAutoRefCountExceptions
2146 ? NormalAndEHCleanup : NormalCleanup;
2147 }
2148
2149 // ARC primitives.
2150 void EmitARCInitWeak(llvm::Value *value, llvm::Value *addr);
2151 void EmitARCDestroyWeak(llvm::Value *addr);
2152 llvm::Value *EmitARCLoadWeak(llvm::Value *addr);
2153 llvm::Value *EmitARCLoadWeakRetained(llvm::Value *addr);
2154 llvm::Value *EmitARCStoreWeak(llvm::Value *value, llvm::Value *addr,
2155 bool ignored);
2156 void EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src);
2157 void EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src);
2158 llvm::Value *EmitARCRetainAutorelease(QualType type, llvm::Value *value);
2159 llvm::Value *EmitARCRetainAutoreleaseNonBlock(llvm::Value *value);
John McCall545d9962011-06-25 02:11:03 +00002160 llvm::Value *EmitARCStoreStrong(LValue lvalue, llvm::Value *value,
2161 bool ignored);
John McCallf85e1932011-06-15 23:02:42 +00002162 llvm::Value *EmitARCStoreStrongCall(llvm::Value *addr, llvm::Value *value,
2163 bool ignored);
2164 llvm::Value *EmitARCRetain(QualType type, llvm::Value *value);
2165 llvm::Value *EmitARCRetainNonBlock(llvm::Value *value);
2166 llvm::Value *EmitARCRetainBlock(llvm::Value *value);
2167 void EmitARCRelease(llvm::Value *value, bool precise);
2168 llvm::Value *EmitARCAutorelease(llvm::Value *value);
2169 llvm::Value *EmitARCAutoreleaseReturnValue(llvm::Value *value);
2170 llvm::Value *EmitARCRetainAutoreleaseReturnValue(llvm::Value *value);
2171 llvm::Value *EmitARCRetainAutoreleasedReturnValue(llvm::Value *value);
2172
2173 std::pair<LValue,llvm::Value*>
2174 EmitARCStoreAutoreleasing(const BinaryOperator *e);
2175 std::pair<LValue,llvm::Value*>
2176 EmitARCStoreStrong(const BinaryOperator *e, bool ignored);
2177
2178 llvm::Value *EmitObjCProduceObject(QualType T, llvm::Value *Ptr);
2179 llvm::Value *EmitObjCConsumeObject(QualType T, llvm::Value *Ptr);
2180 llvm::Value *EmitObjCExtendObjectLifetime(QualType T, llvm::Value *Ptr);
2181
2182 llvm::Value *EmitARCRetainScalarExpr(const Expr *expr);
2183 llvm::Value *EmitARCRetainAutoreleaseScalarExpr(const Expr *expr);
2184
John McCallbdc4d802011-07-09 01:37:26 +00002185 static Destroyer destroyARCStrongImprecise;
2186 static Destroyer destroyARCStrongPrecise;
2187 static Destroyer destroyARCWeak;
2188
John McCallf85e1932011-06-15 23:02:42 +00002189 void EmitObjCAutoreleasePoolPop(llvm::Value *Ptr);
2190 llvm::Value *EmitObjCAutoreleasePoolPush();
2191 llvm::Value *EmitObjCMRRAutoreleasePoolPush();
2192 void EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr);
2193 void EmitObjCMRRAutoreleasePoolPop(llvm::Value *Ptr);
2194
Anders Carlsson4029ca72009-05-20 00:24:07 +00002195 /// EmitReferenceBindingToExpr - Emits a reference binding to the passed in
2196 /// expression. Will emit a temporary variable if E is not an LValue.
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002197 RValue EmitReferenceBindingToExpr(const Expr* E,
Anders Carlsson32f36ba2010-06-26 16:35:32 +00002198 const NamedDecl *InitializedDecl);
Anders Carlsson3aba0932010-01-31 18:34:51 +00002199
Chris Lattner883f6a72007-08-11 00:04:45 +00002200 //===--------------------------------------------------------------------===//
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +00002201 // Expression Emission
Chris Lattner883f6a72007-08-11 00:04:45 +00002202 //===--------------------------------------------------------------------===//
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +00002203
2204 // Expressions are broken into three classes: scalar, complex, aggregate.
Mike Stump0dd9e882009-02-08 23:14:22 +00002205
2206 /// EmitScalarExpr - Emit the computation of the specified expression of LLVM
2207 /// scalar type, returning the result.
Anders Carlsson14c5cbf2009-08-16 07:36:22 +00002208 llvm::Value *EmitScalarExpr(const Expr *E , bool IgnoreResultAssign = false);
Mike Stump0dd9e882009-02-08 23:14:22 +00002209
Chris Lattner3707b252007-08-26 06:48:56 +00002210 /// EmitScalarConversion - Emit a conversion from the specified type to the
2211 /// specified destination type, both of which are LLVM scalar types.
2212 llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy,
2213 QualType DstTy);
Mike Stump0dd9e882009-02-08 23:14:22 +00002214
Chris Lattner4f1a7b32007-08-26 16:34:22 +00002215 /// EmitComplexToScalarConversion - Emit a conversion from the specified
Mike Stump0dd9e882009-02-08 23:14:22 +00002216 /// complex type to the specified destination type, where the destination type
2217 /// is an LLVM scalar type.
Chris Lattner4f1a7b32007-08-26 16:34:22 +00002218 llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy,
2219 QualType DstTy);
Mike Stump0dd9e882009-02-08 23:14:22 +00002220
2221
John McCall558d2ab2010-09-15 10:14:12 +00002222 /// EmitAggExpr - Emit the computation of the specified expression
2223 /// of aggregate type. The result is computed into the given slot,
2224 /// which may be null to indicate that the value is not needed.
Fariborz Jahanian474e2fe2010-09-16 00:20:07 +00002225 void EmitAggExpr(const Expr *E, AggValueSlot AS, bool IgnoreResult = false);
Mike Stump0dd9e882009-02-08 23:14:22 +00002226
Daniel Dunbar18aba0d2010-02-05 19:38:31 +00002227 /// EmitAggExprToLValue - Emit the computation of the specified expression of
2228 /// aggregate type into a temporary LValue.
2229 LValue EmitAggExprToLValue(const Expr *E);
2230
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002231 /// EmitGCMemmoveCollectable - Emit special API for structs with object
2232 /// pointers.
2233 void EmitGCMemmoveCollectable(llvm::Value *DestPtr, llvm::Value *SrcPtr,
Fariborz Jahanian08c32132009-08-31 19:33:16 +00002234 QualType Ty);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002235
John McCall0c24c802011-06-24 23:21:27 +00002236 /// EmitExtendGCLifetime - Given a pointer to an Objective-C object,
2237 /// make sure it survives garbage collection until this point.
2238 void EmitExtendGCLifetime(llvm::Value *object);
2239
Chris Lattnerb6ef18a2007-08-21 05:54:00 +00002240 /// EmitComplexExpr - Emit the computation of the specified expression of
Chris Lattner23b1cdb2007-08-23 23:43:33 +00002241 /// complex type, returning the result.
John McCallb418d742010-11-16 10:08:07 +00002242 ComplexPairTy EmitComplexExpr(const Expr *E,
2243 bool IgnoreReal = false,
2244 bool IgnoreImag = false);
Mike Stump0dd9e882009-02-08 23:14:22 +00002245
Chris Lattner23b1cdb2007-08-23 23:43:33 +00002246 /// EmitComplexExprIntoAddr - Emit the computation of the specified expression
2247 /// of complex type, storing into the specified Value*.
Chris Lattner190dbe22007-08-26 16:22:13 +00002248 void EmitComplexExprIntoAddr(const Expr *E, llvm::Value *DestAddr,
2249 bool DestIsVolatile);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00002250
2251 /// StoreComplexToAddr - Store a complex number into the specified address.
2252 void StoreComplexToAddr(ComplexPairTy V, llvm::Value *DestAddr,
2253 bool DestIsVolatile);
Chris Lattner9b655512007-08-31 22:49:20 +00002254 /// LoadComplexFromAddr - Load a complex number from the specified address.
2255 ComplexPairTy LoadComplexFromAddr(llvm::Value *SrcAddr, bool SrcIsVolatile);
Chris Lattner2621fd12008-05-08 05:58:21 +00002256
John McCallb6bbcc92010-10-15 04:57:14 +00002257 /// CreateStaticVarDecl - Create a zero-initialized LLVM global for
2258 /// a static local variable.
2259 llvm::GlobalVariable *CreateStaticVarDecl(const VarDecl &D,
2260 const char *Separator,
Mike Stumpf71d2322009-11-30 20:08:49 +00002261 llvm::GlobalValue::LinkageTypes Linkage);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002262
John McCallb6bbcc92010-10-15 04:57:14 +00002263 /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
Chris Lattner761acc12009-12-05 08:22:11 +00002264 /// global variable that has already been created for it. If the initializer
2265 /// has a different type than GV does, this may free GV and return a different
2266 /// one. Otherwise it just returns GV.
2267 llvm::GlobalVariable *
John McCallb6bbcc92010-10-15 04:57:14 +00002268 AddInitializerToStaticVarDecl(const VarDecl &D,
2269 llvm::GlobalVariable *GV);
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002270
Daniel Dunbar0096acf2009-02-25 19:24:29 +00002271
Anders Carlsson3b2e16b2009-08-08 21:45:14 +00002272 /// EmitCXXGlobalVarDeclInit - Create the initializer for a C++
2273 /// variable with global storage.
2274 void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::Constant *DeclPtr);
2275
2276 /// EmitCXXGlobalDtorRegistration - Emits a call to register the global ptr
2277 /// with the C++ runtime so that its destructor will be called at exit.
Fariborz Jahanian88f42802009-11-10 19:24:06 +00002278 void EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn,
Anders Carlsson3b2e16b2009-08-08 21:45:14 +00002279 llvm::Constant *DeclPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00002280
John McCall3030eb82010-11-06 09:44:32 +00002281 /// Emit code in this function to perform a guarded variable
2282 /// initialization. Guarded initializations are used when it's not
2283 /// possible to prove that an initialization will be done exactly
2284 /// once, e.g. with a static local variable or a static data member
2285 /// of a class template.
2286 void EmitCXXGuardedInit(const VarDecl &D, llvm::GlobalVariable *DeclPtr);
John McCall5cd91b52010-09-08 01:44:27 +00002287
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00002288 /// GenerateCXXGlobalInitFunc - Generates code for initializing global
2289 /// variables.
2290 void GenerateCXXGlobalInitFunc(llvm::Function *Fn,
2291 llvm::Constant **Decls,
2292 unsigned NumDecls);
2293
2294 /// GenerateCXXGlobalDtorFunc - Generates code for destroying global
2295 /// variables.
2296 void GenerateCXXGlobalDtorFunc(llvm::Function *Fn,
Chris Lattner810112e2010-06-19 05:52:45 +00002297 const std::vector<std::pair<llvm::WeakVH,
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00002298 llvm::Constant*> > &DtorsAndObjects);
2299
John McCalld26bc762011-03-09 04:27:21 +00002300 void GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
2301 const VarDecl *D,
John McCall3030eb82010-11-06 09:44:32 +00002302 llvm::GlobalVariable *Addr);
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00002303
John McCall558d2ab2010-09-15 10:14:12 +00002304 void EmitCXXConstructExpr(const CXXConstructExpr *E, AggValueSlot Dest);
Fariborz Jahanian34999872010-11-13 21:53:34 +00002305
2306 void EmitSynthesizedCXXCopyCtor(llvm::Value *Dest, llvm::Value *Src,
Fariborz Jahanian830937b2010-12-02 17:02:11 +00002307 const Expr *Exp);
Mike Stump1eb44332009-09-09 15:08:12 +00002308
John McCall4765fa02010-12-06 08:20:24 +00002309 RValue EmitExprWithCleanups(const ExprWithCleanups *E,
2310 AggValueSlot Slot =AggValueSlot::ignored());
Mike Stump1eb44332009-09-09 15:08:12 +00002311
Anders Carlsson756b5c42009-10-30 01:42:31 +00002312 void EmitCXXThrowExpr(const CXXThrowExpr *E);
Douglas Gregor1eb2e592010-05-16 00:44:00 +00002313
Daniel Dunbar0ffb1252008-08-04 16:51:22 +00002314 //===--------------------------------------------------------------------===//
2315 // Internal Helpers
2316 //===--------------------------------------------------------------------===//
Mike Stump0dd9e882009-02-08 23:14:22 +00002317
Chris Lattner0946ccd2008-11-11 07:41:27 +00002318 /// ContainsLabel - Return true if the statement contains a label in it. If
2319 /// this statement is not executed normally, it not containing a label means
2320 /// that we can just remove the code.
2321 static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts = false);
Mike Stump0dd9e882009-02-08 23:14:22 +00002322
Chris Lattneref425a62011-02-28 00:18:40 +00002323 /// containsBreak - Return true if the statement contains a break out of it.
2324 /// If the statement (recursively) contains a switch or loop with a break
2325 /// inside of it, this is fine.
2326 static bool containsBreak(const Stmt *S);
2327
Daniel Dunbar4bc04552008-11-12 10:12:14 +00002328 /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
Chris Lattnerc2c90012011-02-27 23:02:32 +00002329 /// to a constant, or if it does but contains a label, return false. If it
2330 /// constant folds return true and set the boolean result in Result.
2331 bool ConstantFoldsToSimpleInteger(const Expr *Cond, bool &Result);
Mike Stump0dd9e882009-02-08 23:14:22 +00002332
Chris Lattneref425a62011-02-28 00:18:40 +00002333 /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
2334 /// to a constant, or if it does but contains a label, return false. If it
2335 /// constant folds return true and set the folded value.
2336 bool ConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APInt &Result);
2337
Chris Lattner31a09842008-11-12 08:04:58 +00002338 /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an
2339 /// if statement) to the specified blocks. Based on the condition, this might
2340 /// try to simplify the codegen of the conditional based on the branch.
Chris Lattner9bc47e22008-11-12 07:46:33 +00002341 void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock,
Daniel Dunbar4bc04552008-11-12 10:12:14 +00002342 llvm::BasicBlock *FalseBlock);
Mike Stumpbe07f602009-12-14 21:58:14 +00002343
Mike Stump15037ca2009-12-15 00:35:12 +00002344 /// getTrapBB - Create a basic block that will call the trap intrinsic. We'll
2345 /// generate a branch around the created basic block as necessary.
Chris Lattner6c552c12010-07-20 20:19:24 +00002346 llvm::BasicBlock *getTrapBB();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002347
Anders Carlsson21c9ad92010-03-30 03:27:09 +00002348 /// EmitCallArg - Emit a single call argument.
John McCall413ebdb2011-03-11 20:59:21 +00002349 void EmitCallArg(CallArgList &args, const Expr *E, QualType ArgType);
Anders Carlsson21c9ad92010-03-30 03:27:09 +00002350
John McCall27360712010-05-26 22:34:26 +00002351 /// EmitDelegateCallArg - We are performing a delegate call; that
2352 /// is, the current function is delegating to another one. Produce
2353 /// a r-value suitable for passing the given parameter.
John McCall413ebdb2011-03-11 20:59:21 +00002354 void EmitDelegateCallArg(CallArgList &args, const VarDecl *param);
John McCall27360712010-05-26 22:34:26 +00002355
Chris Lattner31a09842008-11-12 08:04:58 +00002356private:
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00002357 void EmitReturnOfRValue(RValue RV, QualType Ty);
2358
Daniel Dunbar56273772008-09-17 00:51:38 +00002359 /// ExpandTypeFromArgs - Reconstruct a structure of type \arg Ty
2360 /// from function arguments into \arg Dst. See ABIArgInfo::Expand.
2361 ///
2362 /// \param AI - The first function argument of the expansion.
2363 /// \return The argument following the last expanded function
2364 /// argument.
Mike Stump0dd9e882009-02-08 23:14:22 +00002365 llvm::Function::arg_iterator
Daniel Dunbar56273772008-09-17 00:51:38 +00002366 ExpandTypeFromArgs(QualType Ty, LValue Dst,
2367 llvm::Function::arg_iterator AI);
2368
Mike Stump0dd9e882009-02-08 23:14:22 +00002369 /// ExpandTypeToArgs - Expand an RValue \arg Src, with the LLVM type for \arg
2370 /// Ty, into individual arguments on the provided vector \arg Args. See
2371 /// ABIArgInfo::Expand.
2372 void ExpandTypeToArgs(QualType Ty, RValue Src,
Chris Lattner686775d2011-07-20 06:58:45 +00002373 SmallVector<llvm::Value*, 16> &Args,
Chris Lattner811bf362011-07-12 06:29:11 +00002374 llvm::FunctionType *IRFuncTy);
Anders Carlssonc8c7b182009-01-11 19:40:10 +00002375
Mike Stump1eb44332009-09-09 15:08:12 +00002376 llvm::Value* EmitAsmInput(const AsmStmt &S,
Daniel Dunbarb84e8a62009-05-04 06:56:16 +00002377 const TargetInfo::ConstraintInfo &Info,
Anders Carlssonc8c7b182009-01-11 19:40:10 +00002378 const Expr *InputExpr, std::string &ConstraintStr);
Mike Stump0dd9e882009-02-08 23:14:22 +00002379
Eli Friedman6d7cfd72010-07-16 00:55:21 +00002380 llvm::Value* EmitAsmInputLValue(const AsmStmt &S,
2381 const TargetInfo::ConstraintInfo &Info,
2382 LValue InputValue, QualType InputType,
2383 std::string &ConstraintStr);
2384
Anders Carlsson0139bb92009-04-08 20:47:54 +00002385 /// EmitCallArgs - Emit call arguments for a function.
Mike Stump1eb44332009-09-09 15:08:12 +00002386 /// The CallArgTypeInfo parameter is used for iterating over the known
Anders Carlssonaf23f692009-04-18 20:20:22 +00002387 /// argument types of the function being called.
2388 template<typename T>
2389 void EmitCallArgs(CallArgList& Args, const T* CallArgTypeInfo,
Anders Carlsson0139bb92009-04-08 20:47:54 +00002390 CallExpr::const_arg_iterator ArgBeg,
Anders Carlssonaf23f692009-04-18 20:20:22 +00002391 CallExpr::const_arg_iterator ArgEnd) {
2392 CallExpr::const_arg_iterator Arg = ArgBeg;
Anders Carlsson0139bb92009-04-08 20:47:54 +00002393
Anders Carlssonaf23f692009-04-18 20:20:22 +00002394 // First, use the argument types that the type info knows about
2395 if (CallArgTypeInfo) {
2396 for (typename T::arg_type_iterator I = CallArgTypeInfo->arg_type_begin(),
2397 E = CallArgTypeInfo->arg_type_end(); I != E; ++I, ++Arg) {
Eli Friedman44b0a3e2009-11-18 03:42:04 +00002398 assert(Arg != ArgEnd && "Running over edge of argument list!");
Anders Carlssonaf23f692009-04-18 20:20:22 +00002399 QualType ArgType = *I;
Fariborz Jahanian745da3a2010-09-24 17:30:16 +00002400#ifndef NDEBUG
2401 QualType ActualArgType = Arg->getType();
2402 if (ArgType->isPointerType() && ActualArgType->isPointerType()) {
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002403 QualType ActualBaseType =
Fariborz Jahanian745da3a2010-09-24 17:30:16 +00002404 ActualArgType->getAs<PointerType>()->getPointeeType();
Michael J. Spencer9cac4942010-10-19 06:39:39 +00002405 QualType ArgBaseType =
Fariborz Jahanian745da3a2010-09-24 17:30:16 +00002406 ArgType->getAs<PointerType>()->getPointeeType();
2407 if (ArgBaseType->isVariableArrayType()) {
2408 if (const VariableArrayType *VAT =
2409 getContext().getAsVariableArrayType(ActualBaseType)) {
2410 if (!VAT->getSizeExpr())
2411 ActualArgType = ArgType;
2412 }
2413 }
2414 }
Anders Carlssonaf23f692009-04-18 20:20:22 +00002415 assert(getContext().getCanonicalType(ArgType.getNonReferenceType()).
Mike Stump1eb44332009-09-09 15:08:12 +00002416 getTypePtr() ==
Fariborz Jahanian745da3a2010-09-24 17:30:16 +00002417 getContext().getCanonicalType(ActualArgType).getTypePtr() &&
Anders Carlssonaf23f692009-04-18 20:20:22 +00002418 "type mismatch in call argument!");
Fariborz Jahanian745da3a2010-09-24 17:30:16 +00002419#endif
John McCall413ebdb2011-03-11 20:59:21 +00002420 EmitCallArg(Args, *Arg, ArgType);
Anders Carlssonaf23f692009-04-18 20:20:22 +00002421 }
Mike Stump1eb44332009-09-09 15:08:12 +00002422
2423 // Either we've emitted all the call args, or we have a call to a
Anders Carlssonaf23f692009-04-18 20:20:22 +00002424 // variadic function.
Mike Stump1eb44332009-09-09 15:08:12 +00002425 assert((Arg == ArgEnd || CallArgTypeInfo->isVariadic()) &&
Anders Carlssonaf23f692009-04-18 20:20:22 +00002426 "Extra arguments in non-variadic function!");
Mike Stump1eb44332009-09-09 15:08:12 +00002427
Anders Carlssonaf23f692009-04-18 20:20:22 +00002428 }
Mike Stump1eb44332009-09-09 15:08:12 +00002429
Anders Carlssonaf23f692009-04-18 20:20:22 +00002430 // If we still have any arguments, emit them using the type of the argument.
John McCall413ebdb2011-03-11 20:59:21 +00002431 for (; Arg != ArgEnd; ++Arg)
2432 EmitCallArg(Args, *Arg, Arg->getType());
Anders Carlssonaf23f692009-04-18 20:20:22 +00002433 }
John McCall492c4f92010-03-03 04:15:11 +00002434
2435 const TargetCodeGenInfo &getTargetHooks() const {
2436 return CGM.getTargetCodeGenInfo();
2437 }
John McCall744016d2010-07-06 23:57:41 +00002438
2439 void EmitDeclMetadata();
John McCallf0c11f72011-03-31 08:03:29 +00002440
2441 CodeGenModule::ByrefHelpers *
Chris Lattner2acc6e32011-07-18 04:24:23 +00002442 buildByrefHelpers(llvm::StructType &byrefType,
John McCallf0c11f72011-03-31 08:03:29 +00002443 const AutoVarEmission &emission);
Reid Spencer5f016e22007-07-11 17:01:13 +00002444};
Mike Stump1eb44332009-09-09 15:08:12 +00002445
John McCall150b4622011-01-26 04:00:11 +00002446/// Helper class with most of the code for saving a value for a
2447/// conditional expression cleanup.
John McCall804b8072011-01-28 10:53:53 +00002448struct DominatingLLVMValue {
John McCall150b4622011-01-26 04:00:11 +00002449 typedef llvm::PointerIntPair<llvm::Value*, 1, bool> saved_type;
2450
2451 /// Answer whether the given value needs extra work to be saved.
2452 static bool needsSaving(llvm::Value *value) {
2453 // If it's not an instruction, we don't need to save.
2454 if (!isa<llvm::Instruction>(value)) return false;
2455
2456 // If it's an instruction in the entry block, we don't need to save.
2457 llvm::BasicBlock *block = cast<llvm::Instruction>(value)->getParent();
2458 return (block != &block->getParent()->getEntryBlock());
2459 }
2460
2461 /// Try to save the given value.
2462 static saved_type save(CodeGenFunction &CGF, llvm::Value *value) {
2463 if (!needsSaving(value)) return saved_type(value, false);
2464
2465 // Otherwise we need an alloca.
2466 llvm::Value *alloca =
2467 CGF.CreateTempAlloca(value->getType(), "cond-cleanup.save");
2468 CGF.Builder.CreateStore(value, alloca);
2469
2470 return saved_type(alloca, true);
2471 }
2472
2473 static llvm::Value *restore(CodeGenFunction &CGF, saved_type value) {
2474 if (!value.getInt()) return value.getPointer();
2475 return CGF.Builder.CreateLoad(value.getPointer());
2476 }
2477};
2478
John McCall804b8072011-01-28 10:53:53 +00002479/// A partial specialization of DominatingValue for llvm::Values that
2480/// might be llvm::Instructions.
2481template <class T> struct DominatingPointer<T,true> : DominatingLLVMValue {
2482 typedef T *type;
John McCall150b4622011-01-26 04:00:11 +00002483 static type restore(CodeGenFunction &CGF, saved_type value) {
John McCall804b8072011-01-28 10:53:53 +00002484 return static_cast<T*>(DominatingLLVMValue::restore(CGF, value));
2485 }
2486};
2487
2488/// A specialization of DominatingValue for RValue.
2489template <> struct DominatingValue<RValue> {
2490 typedef RValue type;
2491 class saved_type {
2492 enum Kind { ScalarLiteral, ScalarAddress, AggregateLiteral,
2493 AggregateAddress, ComplexAddress };
2494
2495 llvm::Value *Value;
2496 Kind K;
2497 saved_type(llvm::Value *v, Kind k) : Value(v), K(k) {}
2498
2499 public:
2500 static bool needsSaving(RValue value);
2501 static saved_type save(CodeGenFunction &CGF, RValue value);
2502 RValue restore(CodeGenFunction &CGF);
2503
2504 // implementations in CGExprCXX.cpp
2505 };
2506
2507 static bool needsSaving(type value) {
2508 return saved_type::needsSaving(value);
2509 }
2510 static saved_type save(CodeGenFunction &CGF, type value) {
2511 return saved_type::save(CGF, value);
2512 }
2513 static type restore(CodeGenFunction &CGF, saved_type value) {
2514 return value.restore(CGF);
John McCall150b4622011-01-26 04:00:11 +00002515 }
2516};
2517
Reid Spencer5f016e22007-07-11 17:01:13 +00002518} // end namespace CodeGen
2519} // end namespace clang
2520
2521#endif