CodeGenFunction.h revision 7acddacc921cd0b3f813443a8641eeddb82dfbd4
1bd012ff1fa088181646a784f385b28867372d434Daniel Dunbar//===-- CodeGenFunction.h - Per-Function state for LLVM CodeGen -*- C++ -*-===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
100dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump// This is the internal per-function state used for llvm translation.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14ef52a2fb2ace36c92f88c6e125bd7defa17dafa5Chris Lattner#ifndef CLANG_CODEGEN_CODEGENFUNCTION_H
15ef52a2fb2ace36c92f88c6e125bd7defa17dafa5Chris Lattner#define CLANG_CODEGEN_CODEGENFUNCTION_H
165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
17391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner#include "clang/AST/Type.h"
18e3a09e6ad09f8d1387ecaa008aaf85527909da0aArgyrios Kyrtzidis#include "clang/AST/ExprCXX.h"
195549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek#include "clang/AST/ExprObjC.h"
20199c3d6cd16aebbb9c7f0d42af9d922c9628bf70Ken Dyck#include "clang/AST/CharUnits.h"
21f85e193739c953358c865005855253af4f68a497John McCall#include "clang/Frontend/CodeGenOptions.h"
2214110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne#include "clang/Basic/ABI.h"
23481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner#include "clang/Basic/TargetInfo.h"
24481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner#include "llvm/ADT/DenseMap.h"
25481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner#include "llvm/ADT/SmallVector.h"
26481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner#include "llvm/Support/ValueHandle.h"
2769243825cb5c91ec7207256aa57ae327cfaf8cb2Owen Anderson#include "CodeGenModule.h"
2845d196b8387dcefc4df26cda114fa34c6528e928Daniel Dunbar#include "CGBuilder.h"
298f2926b73ed635afecd020da787af6a837601a2bDaniel Dunbar#include "CGValue.h"
308f2926b73ed635afecd020da787af6a837601a2bDaniel Dunbar
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace llvm {
32c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar  class BasicBlock;
33f21efe9088a0b3eda1209d1706529f6cb2073092Benjamin Kramer  class LLVMContext;
34dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall  class MDNode;
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  class Module;
36898d508d4c9e9d45914952473e39196b20830a9fDaniel Dunbar  class SwitchInst;
37259e9ccf882d11491ad149aec5e6d7a061c9f938Daniel Dunbar  class Twine;
38ed3849b456d64d124bddc7ac044f3ce590bd9d69Daniel Dunbar  class Value;
39f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  class CallSite;
405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace clang {
438d308384a220c7dc81755c47cdcbdd87dac25d5bDevang Patel  class APValue;
445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  class ASTContext;
457267c1693abe7875b0c57268be05005ae013c6c9Anders Carlsson  class CXXDestructorDecl;
46ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  class CXXForRangeStmt;
476815e941998659a55c20c147861b0f437928c3d8Anders Carlsson  class CXXTryStmt;
485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  class Decl;
49ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  class LabelDecl;
50c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar  class EnumConstantDecl;
515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  class FunctionDecl;
5272564e73277e29f6db3305d1f27ba408abb7ed88Douglas Gregor  class FunctionProtoType;
53c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar  class LabelStmt;
54679a502d462ef819e6175b58e255ca3f3391e7cfFariborz Jahanian  class ObjCContainerDecl;
5529e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar  class ObjCInterfaceDecl;
5629e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar  class ObjCIvarDecl;
57391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  class ObjCMethodDecl;
58fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian  class ObjCImplementationDecl;
59af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  class ObjCPropertyImplDecl;
605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  class TargetInfo;
61492c4f998d848673d3d6c9e6416115df4036a71dJohn McCall  class TargetCodeGenInfo;
62c4a1dea2dc56bd1357ec91b829a0b9e68229a13eDaniel Dunbar  class VarDecl;
6316f0049415ec596504891259e2a83e19871c0d52Chris Lattner  class ObjCForCollectionStmt;
6416f0049415ec596504891259e2a83e19871c0d52Chris Lattner  class ObjCAtTryStmt;
6516f0049415ec596504891259e2a83e19871c0d52Chris Lattner  class ObjCAtThrowStmt;
6616f0049415ec596504891259e2a83e19871c0d52Chris Lattner  class ObjCAtSynchronizedStmt;
67f85e193739c953358c865005855253af4f68a497John McCall  class ObjCAutoreleasePoolStmt;
685549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek
695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencernamespace CodeGen {
70b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel  class CodeGenTypes;
71e896d98548b02223c7740d807a0aa6e20fba7079Anders Carlsson  class CGDebugInfo;
72bb36d331f439f49859efcfb4435c61762fbba6f9Daniel Dunbar  class CGFunctionInfo;
730dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  class CGRecordLayout;
74ee5042903d53fa7b0fbc1902d0ea07d57c7775b1John McCall  class CGBlockInfo;
754c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  class CGCXXABI;
76d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  class BlockFlags;
77d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  class BlockFieldFlags;
780dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
79f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// A branch fixup.  These are required when emitting a goto to a
80f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// label which hasn't been emitted yet.  The goto is optimistically
81f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// emitted as a branch to the basic block for the label, and (if it
82f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// occurs in a scope with non-trivial cleanups) a fixup is added to
83f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// the innermost cleanup.  When a (normal) cleanup is popped, any
84f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// unresolved fixups in that scope are threaded through the cleanup.
85f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallstruct BranchFixup {
86ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// The block containing the terminator which needs to be modified
87ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// into a switch if this fixup is resolved into the current scope.
88ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// If null, LatestBranch points directly to the destination.
89ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::BasicBlock *OptimisticBranchBlock;
90f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
91ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// The ultimate destination of the branch.
92f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ///
93f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// This can be set to null to indicate that this fixup was
94f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// successfully resolved.
95f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *Destination;
96f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
97ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// The destination index value.
98ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  unsigned DestinationIndex;
99ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
100ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// The initial branch of the fixup.
101ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::BranchInst *InitialBranch;
102f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall};
103f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
104804b807ea918184d6de63bd745e1ff75a9bfc679John McCalltemplate <class T> struct InvariantValue {
105150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  typedef T type;
106150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  typedef T saved_type;
107150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  static bool needsSaving(type value) { return false; }
108150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  static saved_type save(CodeGenFunction &CGF, type value) { return value; }
109150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  static type restore(CodeGenFunction &CGF, saved_type value) { return value; }
110150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall};
111804b807ea918184d6de63bd745e1ff75a9bfc679John McCall
112804b807ea918184d6de63bd745e1ff75a9bfc679John McCall/// A metaprogramming class for ensuring that a value will dominate an
113804b807ea918184d6de63bd745e1ff75a9bfc679John McCall/// arbitrary position in a function.
114804b807ea918184d6de63bd745e1ff75a9bfc679John McCalltemplate <class T> struct DominatingValue : InvariantValue<T> {};
115804b807ea918184d6de63bd745e1ff75a9bfc679John McCall
116804b807ea918184d6de63bd745e1ff75a9bfc679John McCalltemplate <class T, bool mightBeInstruction =
117804b807ea918184d6de63bd745e1ff75a9bfc679John McCall            llvm::is_base_of<llvm::Value, T>::value &&
118804b807ea918184d6de63bd745e1ff75a9bfc679John McCall            !llvm::is_base_of<llvm::Constant, T>::value &&
119804b807ea918184d6de63bd745e1ff75a9bfc679John McCall            !llvm::is_base_of<llvm::BasicBlock, T>::value>
120804b807ea918184d6de63bd745e1ff75a9bfc679John McCallstruct DominatingPointer;
121804b807ea918184d6de63bd745e1ff75a9bfc679John McCalltemplate <class T> struct DominatingPointer<T,false> : InvariantValue<T*> {};
122804b807ea918184d6de63bd745e1ff75a9bfc679John McCall// template <class T> struct DominatingPointer<T,true> at end of file
123804b807ea918184d6de63bd745e1ff75a9bfc679John McCall
124804b807ea918184d6de63bd745e1ff75a9bfc679John McCalltemplate <class T> struct DominatingValue<T*> : DominatingPointer<T> {};
125150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
126cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCallenum CleanupKind {
127cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall  EHCleanup = 0x1,
128cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall  NormalCleanup = 0x2,
129cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall  NormalAndEHCleanup = EHCleanup | NormalCleanup,
130cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall
131cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall  InactiveCleanup = 0x4,
132cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall  InactiveEHCleanup = EHCleanup | InactiveCleanup,
133cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall  InactiveNormalCleanup = NormalCleanup | InactiveCleanup,
134cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall  InactiveNormalAndEHCleanup = NormalAndEHCleanup | InactiveCleanup
135cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall};
136da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
137f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// A stack of scopes which respond to exceptions, including cleanups
138f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall/// and catch blocks.
139f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallclass EHScopeStack {
140f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallpublic:
141f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// A saved depth on the scope stack.  This is necessary because
142f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// pushing scopes onto the stack invalidates iterators.
143f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  class stable_iterator {
144f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    friend class EHScopeStack;
145f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
146f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    /// Offset from StartOfData to EndOfBuffer.
147f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    ptrdiff_t Size;
148f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
149f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    stable_iterator(ptrdiff_t Size) : Size(Size) {}
150f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
151f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  public:
152f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    static stable_iterator invalid() { return stable_iterator(-1); }
153f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    stable_iterator() : Size(-1) {}
154f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
155f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    bool isValid() const { return Size >= 0; }
156f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
157838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall    /// Returns true if this scope encloses I.
158838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall    /// Returns false if I is invalid.
159838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall    /// This scope must be valid.
160ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    bool encloses(stable_iterator I) const { return Size <= I.Size; }
161838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall
162838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall    /// Returns true if this scope strictly encloses I: that is,
163838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall    /// if it encloses I and is not I.
164838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall    /// Returns false is I is invalid.
165838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall    /// This scope must be valid.
166ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    bool strictlyEncloses(stable_iterator I) const { return Size < I.Size; }
1671bda662cb37d9fe24be4b3cf8ea0f4e550e1f889John McCall
168f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    friend bool operator==(stable_iterator A, stable_iterator B) {
169f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      return A.Size == B.Size;
170f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    }
171f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    friend bool operator!=(stable_iterator A, stable_iterator B) {
172f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      return A.Size != B.Size;
173f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    }
174f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  };
175f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1761f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  /// Information for lazily generating a cleanup.  Subclasses must be
1771f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  /// POD-like: cleanups will not be destructed, and they will be
1781f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  /// allocated on the cleanup stack and freely copied and moved
1791f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  /// around.
180da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  ///
1811f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  /// Cleanup implementations should generally be declared in an
182da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  /// anonymous namespace.
1831f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  class Cleanup {
184da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  public:
1853e29f96988b0438e42dba3790f61fb3df42d90cbJohn McCall    // Anchor the construction vtable.  We use the destructor because
1863e29f96988b0438e42dba3790f61fb3df42d90cbJohn McCall    // gcc gives an obnoxious warning if there are virtual methods
1873e29f96988b0438e42dba3790f61fb3df42d90cbJohn McCall    // with an accessible non-virtual destructor.  Unfortunately,
1883e29f96988b0438e42dba3790f61fb3df42d90cbJohn McCall    // declaring this destructor makes it non-trivial, but there
1893e29f96988b0438e42dba3790f61fb3df42d90cbJohn McCall    // doesn't seem to be any other way around this warning.
1903e29f96988b0438e42dba3790f61fb3df42d90cbJohn McCall    //
1913e29f96988b0438e42dba3790f61fb3df42d90cbJohn McCall    // This destructor will never be called.
1921f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    virtual ~Cleanup();
1933e29f96988b0438e42dba3790f61fb3df42d90cbJohn McCall
194da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    /// Emit the cleanup.  For normal cleanups, this is run in the
195da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    /// same EH context as when the cleanup was pushed, i.e. the
196da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    /// immediately-enclosing context of the cleanup scope.  For
197da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    /// EH cleanups, this is run in a terminate context.
198da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    ///
199da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    // \param IsForEHCleanup true if this is for an EH cleanup, false
200da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    ///  if for a normal cleanup.
201da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    virtual void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) = 0;
202da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  };
203da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
2043ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  /// UnconditionalCleanupN stores its N parameters and just passes
2053ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  /// them to the real cleanup function.
2063ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  template <class T, class A0>
2073ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  class UnconditionalCleanup1 : public Cleanup {
2083ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    A0 a0;
209150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  public:
2103ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    UnconditionalCleanup1(A0 a0) : a0(a0) {}
2113ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
2123ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall      T::Emit(CGF, IsForEHCleanup, a0);
2133ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    }
214150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  };
215150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
216150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  template <class T, class A0, class A1>
217150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  class UnconditionalCleanup2 : public Cleanup {
218150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    A0 a0; A1 a1;
2194bbcbda302cba8b1b0d88c20d735d09b483bd005John McCall  public:
220150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    UnconditionalCleanup2(A0 a0, A1 a1) : a0(a0), a1(a1) {}
221150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
222150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      T::Emit(CGF, IsForEHCleanup, a0, a1);
223150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    }
224150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  };
225150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
226150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// ConditionalCleanupN stores the saved form of its N parameters,
227150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// then restores them and performs the cleanup.
2283ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  template <class T, class A0>
2293ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  class ConditionalCleanup1 : public Cleanup {
230804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    typedef typename DominatingValue<A0>::saved_type A0_saved;
2313ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    A0_saved a0_saved;
2323ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall
2333ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
234804b807ea918184d6de63bd745e1ff75a9bfc679John McCall      A0 a0 = DominatingValue<A0>::restore(CGF, a0_saved);
2353ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall      T::Emit(CGF, IsForEHCleanup, a0);
2363ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    }
2373ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall
2383ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  public:
2393ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    ConditionalCleanup1(A0_saved a0)
2403ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall      : a0_saved(a0) {}
2413ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  };
2423ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall
243150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  template <class T, class A0, class A1>
2443ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  class ConditionalCleanup2 : public Cleanup {
245804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    typedef typename DominatingValue<A0>::saved_type A0_saved;
246804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    typedef typename DominatingValue<A1>::saved_type A1_saved;
2474bbcbda302cba8b1b0d88c20d735d09b483bd005John McCall    A0_saved a0_saved;
2484bbcbda302cba8b1b0d88c20d735d09b483bd005John McCall    A1_saved a1_saved;
249150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
2503ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
251804b807ea918184d6de63bd745e1ff75a9bfc679John McCall      A0 a0 = DominatingValue<A0>::restore(CGF, a0_saved);
252804b807ea918184d6de63bd745e1ff75a9bfc679John McCall      A1 a1 = DominatingValue<A1>::restore(CGF, a1_saved);
253150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      T::Emit(CGF, IsForEHCleanup, a0, a1);
254150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    }
255150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
256150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  public:
2573ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    ConditionalCleanup2(A0_saved a0, A1_saved a1)
2583ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall      : a0_saved(a0), a1_saved(a1) {}
259150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  };
260150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
261f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallprivate:
262f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // The implementation for this class is in CGException.h and
263f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // CGException.cpp; the definition is here because it's used as a
264f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // member of CodeGenFunction.
265f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
266f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// The start of the scope-stack buffer, i.e. the allocated pointer
267f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// for the buffer.  All of these pointers are either simultaneously
268f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// null or simultaneously valid.
269f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  char *StartOfBuffer;
270f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
271f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// The end of the buffer.
272f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  char *EndOfBuffer;
273f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
274f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// The first valid entry in the buffer.
275f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  char *StartOfData;
276f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
277f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// The innermost normal cleanup on the stack.
278f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  stable_iterator InnermostNormalCleanup;
279f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
280f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// The innermost EH cleanup on the stack.
281f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  stable_iterator InnermostEHCleanup;
282f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
283f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// The number of catches on the stack.
284f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  unsigned CatchDepth;
285f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
286ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// The current EH destination index.  Reset to FirstCatchIndex
287ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// whenever the last EH cleanup is popped.
288ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  unsigned NextEHDestIndex;
289ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  enum { FirstEHDestIndex = 1 };
290ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
291f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// The current set of branch fixups.  A branch fixup is a jump to
292f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// an as-yet unemitted label, i.e. a label for which we don't yet
293f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// know the EH stack depth.  Whenever we pop a cleanup, we have
294f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// to thread all the current branch fixups through it.
295f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ///
296f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Fixups are recorded as the Use of the respective branch or
297f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// switch statement.  The use points to the final destination.
298f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// When popping out of a cleanup, these uses are threaded through
299f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// the cleanup and adjusted to point to the new cleanup.
300f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ///
301f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Note that branches are allowed to jump into protected scopes
302f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// in certain situations;  e.g. the following code is legal:
303f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ///     struct A { ~A(); }; // trivial ctor, non-trivial dtor
304f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ///     goto foo;
305f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ///     A a;
306f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ///    foo:
307f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ///     bar();
308f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::SmallVector<BranchFixup, 8> BranchFixups;
309f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
310f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  char *allocate(size_t Size);
311f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
3121f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  void *pushCleanup(CleanupKind K, size_t DataSize);
313da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
314f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallpublic:
315f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EHScopeStack() : StartOfBuffer(0), EndOfBuffer(0), StartOfData(0),
316f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                   InnermostNormalCleanup(stable_end()),
317f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                   InnermostEHCleanup(stable_end()),
318ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall                   CatchDepth(0), NextEHDestIndex(FirstEHDestIndex) {}
319f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ~EHScopeStack() { delete[] StartOfBuffer; }
320f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
321da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  // Variadic templates would make this not terrible.
322da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
323da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  /// Push a lazily-created cleanup on the stack.
3248e3f86193995c47ee0d229e4336c3382410f09f5John McCall  template <class T>
3251f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  void pushCleanup(CleanupKind Kind) {
3261f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    void *Buffer = pushCleanup(Kind, sizeof(T));
3271f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    Cleanup *Obj = new(Buffer) T();
3288e3f86193995c47ee0d229e4336c3382410f09f5John McCall    (void) Obj;
3298e3f86193995c47ee0d229e4336c3382410f09f5John McCall  }
3308e3f86193995c47ee0d229e4336c3382410f09f5John McCall
3318e3f86193995c47ee0d229e4336c3382410f09f5John McCall  /// Push a lazily-created cleanup on the stack.
3328e3f86193995c47ee0d229e4336c3382410f09f5John McCall  template <class T, class A0>
3331f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  void pushCleanup(CleanupKind Kind, A0 a0) {
3341f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    void *Buffer = pushCleanup(Kind, sizeof(T));
3351f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    Cleanup *Obj = new(Buffer) T(a0);
3368e3f86193995c47ee0d229e4336c3382410f09f5John McCall    (void) Obj;
3378e3f86193995c47ee0d229e4336c3382410f09f5John McCall  }
3388e3f86193995c47ee0d229e4336c3382410f09f5John McCall
3398e3f86193995c47ee0d229e4336c3382410f09f5John McCall  /// Push a lazily-created cleanup on the stack.
340da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  template <class T, class A0, class A1>
3411f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  void pushCleanup(CleanupKind Kind, A0 a0, A1 a1) {
3421f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    void *Buffer = pushCleanup(Kind, sizeof(T));
3431f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    Cleanup *Obj = new(Buffer) T(a0, a1);
344da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    (void) Obj;
345da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  }
346da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
347da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  /// Push a lazily-created cleanup on the stack.
348da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  template <class T, class A0, class A1, class A2>
3491f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  void pushCleanup(CleanupKind Kind, A0 a0, A1 a1, A2 a2) {
3501f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    void *Buffer = pushCleanup(Kind, sizeof(T));
3511f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    Cleanup *Obj = new(Buffer) T(a0, a1, a2);
352da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    (void) Obj;
353da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  }
354da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
355da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  /// Push a lazily-created cleanup on the stack.
356da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  template <class T, class A0, class A1, class A2, class A3>
3571f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  void pushCleanup(CleanupKind Kind, A0 a0, A1 a1, A2 a2, A3 a3) {
3581f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    void *Buffer = pushCleanup(Kind, sizeof(T));
3591f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    Cleanup *Obj = new(Buffer) T(a0, a1, a2, a3);
360da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    (void) Obj;
361da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  }
362da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
36377199713ab56f87ffad9a535ff2a0877704eed87John McCall  /// Push a lazily-created cleanup on the stack.
36477199713ab56f87ffad9a535ff2a0877704eed87John McCall  template <class T, class A0, class A1, class A2, class A3, class A4>
3651f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  void pushCleanup(CleanupKind Kind, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) {
3661f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    void *Buffer = pushCleanup(Kind, sizeof(T));
3671f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    Cleanup *Obj = new(Buffer) T(a0, a1, a2, a3, a4);
36877199713ab56f87ffad9a535ff2a0877704eed87John McCall    (void) Obj;
36977199713ab56f87ffad9a535ff2a0877704eed87John McCall  }
37077199713ab56f87ffad9a535ff2a0877704eed87John McCall
3717d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  // Feel free to add more variants of the following:
3727d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall
3737d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// Push a cleanup with non-constant storage requirements on the
3747d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// stack.  The cleanup type must provide an additional static method:
3757d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  ///   static size_t getExtraSize(size_t);
3767d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// The argument to this method will be the value N, which will also
3777d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// be passed as the first argument to the constructor.
3787d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  ///
3797d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// The data stored in the extra storage must obey the same
3807d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// restrictions as normal cleanup member data.
3817d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  ///
3827d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// The pointer returned from this method is valid until the cleanup
3837d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// stack is modified.
3847d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  template <class T, class A0, class A1, class A2>
3857d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  T *pushCleanupWithExtra(CleanupKind Kind, size_t N, A0 a0, A1 a1, A2 a2) {
3867d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall    void *Buffer = pushCleanup(Kind, sizeof(T) + T::getExtraSize(N));
3877d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall    return new (Buffer) T(N, a0, a1, a2);
3887d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  }
3897d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall
390f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Pops a cleanup scope off the stack.  This should only be called
391f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// by CodeGenFunction::PopCleanupBlock.
392f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  void popCleanup();
393f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
394f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Push a set of catch handlers on the stack.  The catch is
395f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// uninitialized and will need to have the given number of handlers
396f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// set on it.
397f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  class EHCatchScope *pushCatch(unsigned NumHandlers);
398f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
399f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Pops a catch scope off the stack.
400f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  void popCatch();
401f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
402f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Push an exceptions filter on the stack.
403f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  class EHFilterScope *pushFilter(unsigned NumFilters);
404f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
405f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Pops an exceptions filter off the stack.
406f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  void popFilter();
407f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
408f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Push a terminate handler on the stack.
409f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  void pushTerminate();
410f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
411f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Pops a terminate handler off the stack.
412f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  void popTerminate();
413f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
414f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Determines whether the exception-scopes stack is empty.
415f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  bool empty() const { return StartOfData == EndOfBuffer; }
416f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
417f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  bool requiresLandingPad() const {
418f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return (CatchDepth || hasEHCleanups());
419f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
420f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
421f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Determines whether there are any normal cleanups on the stack.
422f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  bool hasNormalCleanups() const {
423f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return InnermostNormalCleanup != stable_end();
424f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
425f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
426f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Returns the innermost normal cleanup on the stack, or
427f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// stable_end() if there are no normal cleanups.
428f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  stable_iterator getInnermostNormalCleanup() const {
429f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return InnermostNormalCleanup;
430f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
431838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall  stable_iterator getInnermostActiveNormalCleanup() const; // CGException.h
432f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
433f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Determines whether there are any EH cleanups on the stack.
434f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  bool hasEHCleanups() const {
435f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return InnermostEHCleanup != stable_end();
436f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
437f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
438f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Returns the innermost EH cleanup on the stack, or stable_end()
439f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// if there are no EH cleanups.
440f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  stable_iterator getInnermostEHCleanup() const {
441f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return InnermostEHCleanup;
442f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
443838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall  stable_iterator getInnermostActiveEHCleanup() const; // CGException.h
444f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
445f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// An unstable reference to a scope-stack depth.  Invalidated by
446f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// pushes but not pops.
447f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  class iterator;
448f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
449f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Returns an iterator pointing to the innermost EH scope.
450f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  iterator begin() const;
451f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
452f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Returns an iterator pointing to the outermost EH scope.
453f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  iterator end() const;
454f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
455f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Create a stable reference to the top of the EH stack.  The
456f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// returned reference is valid until that scope is popped off the
457f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// stack.
458f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  stable_iterator stable_begin() const {
459f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return stable_iterator(EndOfBuffer - StartOfData);
460f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
461f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
462f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Create a stable reference to the bottom of the EH stack.
463f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  static stable_iterator stable_end() {
464f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return stable_iterator(0);
465f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
466f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
467f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Translates an iterator into a stable_iterator.
468f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  stable_iterator stabilize(iterator it) const;
469f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
470f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Finds the nearest cleanup enclosing the given iterator.
471f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Returns stable_iterator::invalid() if there are no such cleanups.
472f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  stable_iterator getEnclosingEHCleanup(iterator it) const;
473f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
474f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Turn a stable reference to a scope depth into a unstable pointer
475f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// to the EH stack.
476f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  iterator find(stable_iterator save) const;
477f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
478f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Removes the cleanup pointed to by the given stable_iterator.
479f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  void removeCleanup(stable_iterator save);
480f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
481f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Add a branch fixup to the current cleanup scope.
482f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  BranchFixup &addBranchFixup() {
483f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    assert(hasNormalCleanups() && "adding fixup in scope without cleanups");
484f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    BranchFixups.push_back(BranchFixup());
485f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return BranchFixups.back();
486f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
487f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
488f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  unsigned getNumBranchFixups() const { return BranchFixups.size(); }
489f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  BranchFixup &getBranchFixup(unsigned I) {
490f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    assert(I < getNumBranchFixups());
491f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return BranchFixups[I];
492f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
493f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
494ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// Pops lazily-removed fixups from the end of the list.  This
495ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// should only be called by procedures which have just popped a
496ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// cleanup or resolved one or more fixups.
497ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  void popNullFixups();
498ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
499ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// Clears the branch-fixups list.  This should only be called by
5008abdbd8118e37e759d1ce3f5814ee0a24b1589e8John McCall  /// ResolveAllBranchFixups.
501ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  void clearFixups() { BranchFixups.clear(); }
502ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
503ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// Gets the next EH destination index.
504ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  unsigned getNextEHDestIndex() { return NextEHDestIndex++; }
505f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall};
506f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
5075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// CodeGenFunction - This class organizes the per-function state that is used
5085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// while generating LLVM code.
5095936e33bf74dd6bf126ceee0f6169a2593d03a69John McCallclass CodeGenFunction : public CodeGenTypeCache {
5108a219ceda2b5afd447e7199b9c53079bead31b89Anders Carlsson  CodeGenFunction(const CodeGenFunction&); // DO NOT IMPLEMENT
5118a219ceda2b5afd447e7199b9c53079bead31b89Anders Carlsson  void operator=(const CodeGenFunction&);  // DO NOT IMPLEMENT
5124c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
5134c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  friend class CGCXXABI;
514bfc0c1ae4b505b52b36f572a9641f99e76879cabChris Lattnerpublic:
515ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// A jump destination is an abstract label, branching to which may
516ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// require a jump out through normal cleanups.
517f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  struct JumpDest {
518ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    JumpDest() : Block(0), ScopeDepth(), Index(0) {}
519ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    JumpDest(llvm::BasicBlock *Block,
520ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall             EHScopeStack::stable_iterator Depth,
521ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall             unsigned Index)
522ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      : Block(Block), ScopeDepth(Depth), Index(Index) {}
523ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
524ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    bool isValid() const { return Block != 0; }
525ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    llvm::BasicBlock *getBlock() const { return Block; }
526ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    EHScopeStack::stable_iterator getScopeDepth() const { return ScopeDepth; }
527ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    unsigned getDestIndex() const { return Index; }
5289cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
529ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  private:
530f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    llvm::BasicBlock *Block;
531f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    EHScopeStack::stable_iterator ScopeDepth;
532ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    unsigned Index;
533ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  };
534ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
535ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// An unwind destination is an abstract label, branching to which
536ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// may require a jump out through EH cleanups.
537ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  struct UnwindDest {
538ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    UnwindDest() : Block(0), ScopeDepth(), Index(0) {}
539ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    UnwindDest(llvm::BasicBlock *Block,
540ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall               EHScopeStack::stable_iterator Depth,
541ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall               unsigned Index)
542ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      : Block(Block), ScopeDepth(Depth), Index(Index) {}
543ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
544ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    bool isValid() const { return Block != 0; }
545ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    llvm::BasicBlock *getBlock() const { return Block; }
546ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    EHScopeStack::stable_iterator getScopeDepth() const { return ScopeDepth; }
547ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    unsigned getDestIndex() const { return Index; }
548ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
549ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  private:
550ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    llvm::BasicBlock *Block;
551ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    EHScopeStack::stable_iterator ScopeDepth;
552ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    unsigned Index;
553f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  };
554f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
5555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CodeGenModule &CGM;  // Per-module state.
556444be7366d0a1e172c0290a1ea54c1cb16b5947cDaniel Dunbar  const TargetInfo &Target;
5570dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
55858dee10ed2eee34035f62d1c2d32b3639e9182f8Chris Lattner  typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
55945d196b8387dcefc4df26cda114fa34c6528e928Daniel Dunbar  CGBuilderTy Builder;
5600dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
561b5437d238752dc297e42410e98d38d5250fe0463Chris Lattner  /// CurFuncDecl - Holds the Decl for the current function or ObjC method.
562b5437d238752dc297e42410e98d38d5250fe0463Chris Lattner  /// This excludes BlockDecls.
5634111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner  const Decl *CurFuncDecl;
564b5437d238752dc297e42410e98d38d5250fe0463Chris Lattner  /// CurCodeDecl - This is the inner-most code context, which includes blocks.
565b5437d238752dc297e42410e98d38d5250fe0463Chris Lattner  const Decl *CurCodeDecl;
56688b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  const CGFunctionInfo *CurFnInfo;
567391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  QualType FnRetTy;
5685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::Function *CurFn;
5695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5706a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump  /// CurGD - The GlobalDecl for the current function being compiled.
5716a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump  GlobalDecl CurGD;
5726a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump
573f85e193739c953358c865005855253af4f68a497John McCall  /// PrologueCleanupDepth - The cleanup depth enclosing all the
574f85e193739c953358c865005855253af4f68a497John McCall  /// cleanups associated with the parameters.
575f85e193739c953358c865005855253af4f68a497John McCall  EHScopeStack::stable_iterator PrologueCleanupDepth;
576f85e193739c953358c865005855253af4f68a497John McCall
5775ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar  /// ReturnBlock - Unified return block.
578f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest ReturnBlock;
579f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
5800dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// ReturnValue - The temporary alloca to hold the return value. This is null
5810dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// iff the function has no return value.
582b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  llvm::Value *ReturnValue;
5830dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
584ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// RethrowBlock - Unified rethrow block.
585ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  UnwindDest RethrowBlock;
586ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
5875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// AllocaInsertPoint - This is an instruction in the entry block before which
5885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// we prefer to insert allocas.
589481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner  llvm::AssertingVH<llvm::Instruction> AllocaInsertPt;
5900ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar
5919c276ae0f24d4cee8f7954069d4b8eae45d0447dMike Stump  bool CatchUndefined;
5929cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
593f85e193739c953358c865005855253af4f68a497John McCall  /// In ARC, whether we should autorelease the return value.
594f85e193739c953358c865005855253af4f68a497John McCall  bool AutoreleaseResult;
595f85e193739c953358c865005855253af4f68a497John McCall
596d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  const CodeGen::CGBlockInfo *BlockInfo;
597d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  llvm::Value *BlockPointer;
598d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall
5993d91bbcdab155181556969cad6ec97014405acedDouglas Gregor  /// \brief A mapping from NRVO variables to the flags used to indicate
6003d91bbcdab155181556969cad6ec97014405acedDouglas Gregor  /// when the NRVO has been applied to this variable.
6013d91bbcdab155181556969cad6ec97014405acedDouglas Gregor  llvm::DenseMap<const VarDecl *, llvm::Value *> NRVOFlags;
6029cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
603f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EHScopeStack EHStack;
6040dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
605ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// i32s containing the indexes of the cleanup destinations.
606ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::AllocaInst *NormalCleanupDest;
607ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::AllocaInst *EHCleanupDest;
608ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
609ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  unsigned NextCleanupDestIndex;
610ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
611f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// The exception slot.  All landing pads write the current
612f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// exception pointer into this alloca.
613f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::Value *ExceptionSlot;
6140dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
61593c332a8ba2c193c435b293966d343dab15f555bJohn McCall  /// The selector slot.  Under the MandatoryCleanup model, all
61693c332a8ba2c193c435b293966d343dab15f555bJohn McCall  /// landing pads write the current selector value into this alloca.
61793c332a8ba2c193c435b293966d343dab15f555bJohn McCall  llvm::AllocaInst *EHSelectorSlot;
61893c332a8ba2c193c435b293966d343dab15f555bJohn McCall
619f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Emits a landing pad for the current EH stack.
620f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *EmitLandingPad();
6210dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
622f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *getInvokeDestImpl();
62399533834ba8f3658559f334e68a518ebb6388ceaMike Stump
6243ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  /// Set up the last cleaup that was pushed as a conditional
6253ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  /// full-expression cleanup.
6263ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  void initFullExprCleanup();
627150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
628150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  template <class T>
629804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  typename DominatingValue<T>::saved_type saveValueInCond(T value) {
630804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    return DominatingValue<T>::save(*this, value);
631150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  }
632150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
633f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallpublic:
634f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// ObjCEHValueStack - Stack of Objective-C exception values, used for
635f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// rethrows.
636f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::SmallVector<llvm::Value*, 8> ObjCEHValueStack;
637bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
638f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // A struct holding information about a finally block's IR
639f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // generation.  For now, doesn't actually hold anything.
640f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  struct FinallyInfo {
641d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump  };
642d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump
643f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  FinallyInfo EnterFinallyBlock(const Stmt *Stmt,
644f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                                llvm::Constant *BeginCatchFn,
645f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                                llvm::Constant *EndCatchFn,
646f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                                llvm::Constant *RethrowFn);
647f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  void ExitFinallyBlock(FinallyInfo &FinallyInfo);
648f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
649150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// pushFullExprCleanup - Push a cleanup to be run at the end of the
650150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// current full-expression.  Safe against the possibility that
651150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// we're currently inside a conditionally-evaluated expression.
6523ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  template <class T, class A0>
6533ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  void pushFullExprCleanup(CleanupKind kind, A0 a0) {
6543ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    // If we're not in a conditional branch, or if none of the
6553ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    // arguments requires saving, then use the unconditional cleanup.
6563ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    if (!isInConditionalBranch()) {
6573ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall      typedef EHScopeStack::UnconditionalCleanup1<T, A0> CleanupType;
6583ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall      return EHStack.pushCleanup<CleanupType>(kind, a0);
6593ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    }
6603ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall
661804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
6623ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall
6633ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    typedef EHScopeStack::ConditionalCleanup1<T, A0> CleanupType;
6643ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    EHStack.pushCleanup<CleanupType>(kind, a0_saved);
6653ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    initFullExprCleanup();
6663ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  }
6673ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall
6683ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  /// pushFullExprCleanup - Push a cleanup to be run at the end of the
6693ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  /// current full-expression.  Safe against the possibility that
6703ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  /// we're currently inside a conditionally-evaluated expression.
671150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  template <class T, class A0, class A1>
672150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1) {
673150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    // If we're not in a conditional branch, or if none of the
674150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    // arguments requires saving, then use the unconditional cleanup.
6753ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    if (!isInConditionalBranch()) {
676150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      typedef EHScopeStack::UnconditionalCleanup2<T, A0, A1> CleanupType;
677150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      return EHStack.pushCleanup<CleanupType>(kind, a0, a1);
678150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    }
679150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
680804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
681804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1);
682150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
683150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    typedef EHScopeStack::ConditionalCleanup2<T, A0, A1> CleanupType;
6843ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved);
6853ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    initFullExprCleanup();
686150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  }
687150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
688f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// PushDestructorCleanup - Push a cleanup to call the
689f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// complete-object destructor of an object of the given type at the
690f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// given address.  Does nothing if T is not a C++ class type with a
691f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// non-trivial destructor.
692f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  void PushDestructorCleanup(QualType T, llvm::Value *Addr);
693f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
69481407d404a3fc8d24d0e2a052d85cae183033e2bJohn McCall  /// PushDestructorCleanup - Push a cleanup to call the
69581407d404a3fc8d24d0e2a052d85cae183033e2bJohn McCall  /// complete-object variant of the given destructor on the object at
69681407d404a3fc8d24d0e2a052d85cae183033e2bJohn McCall  /// the given address.
69781407d404a3fc8d24d0e2a052d85cae183033e2bJohn McCall  void PushDestructorCleanup(const CXXDestructorDecl *Dtor,
69881407d404a3fc8d24d0e2a052d85cae183033e2bJohn McCall                             llvm::Value *Addr);
69981407d404a3fc8d24d0e2a052d85cae183033e2bJohn McCall
700f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// PopCleanupBlock - Will pop the cleanup entry on the stack and
701f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// process all branch fixups.
702ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  void PopCleanupBlock(bool FallThroughIsBranchThrough = false);
703f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
7047d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// DeactivateCleanupBlock - Deactivates the given cleanup block.
7057d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// The block cannot be reactivated.  Pops it if it's the top of the
7067d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// stack.
7077d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup);
7087d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall
7097d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// ActivateCleanupBlock - Activates an initially-inactive cleanup.
7107d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// Cannot be used to resurrect a deactivated cleanup.
7117d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  void ActivateCleanupBlock(EHScopeStack::stable_iterator Cleanup);
712cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall
713f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// \brief Enters a new scope for capturing cleanups, all of which
714f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// will be executed once the scope is exited.
715f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  class RunCleanupsScope {
71601234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    CodeGenFunction& CGF;
717f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    EHScopeStack::stable_iterator CleanupStackDepth;
71801234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    bool OldDidCallStackSave;
7195656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    bool PerformCleanup;
72001234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor
721f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    RunCleanupsScope(const RunCleanupsScope &); // DO NOT IMPLEMENT
722f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    RunCleanupsScope &operator=(const RunCleanupsScope &); // DO NOT IMPLEMENT
72301234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor
72401234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor  public:
72501234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    /// \brief Enter a new cleanup scope.
7269cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer    explicit RunCleanupsScope(CodeGenFunction &CGF)
7279cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer      : CGF(CGF), PerformCleanup(true)
7285656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    {
729f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      CleanupStackDepth = CGF.EHStack.stable_begin();
73001234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor      OldDidCallStackSave = CGF.DidCallStackSave;
7314ada2ca7d82dab68d3646f3eb6dcdfee072e8ea4Argyrios Kyrtzidis      CGF.DidCallStackSave = false;
73201234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    }
73301234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor
73401234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    /// \brief Exit this cleanup scope, emitting any accumulated
73501234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    /// cleanups.
736f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    ~RunCleanupsScope() {
7375656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor      if (PerformCleanup) {
7385656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor        CGF.DidCallStackSave = OldDidCallStackSave;
739f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall        CGF.PopCleanupBlocks(CleanupStackDepth);
7405656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor      }
7415656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    }
7425656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor
7435656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    /// \brief Determine whether this scope requires any cleanups.
7445656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    bool requiresCleanups() const {
745f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      return CGF.EHStack.stable_begin() != CleanupStackDepth;
7465656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    }
7475656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor
7485656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    /// \brief Force the emission of cleanups now, instead of waiting
7495656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    /// until this object is destroyed.
7505656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    void ForceCleanup() {
7515656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor      assert(PerformCleanup && "Already forced cleanup");
75201234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor      CGF.DidCallStackSave = OldDidCallStackSave;
753f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      CGF.PopCleanupBlocks(CleanupStackDepth);
7545656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor      PerformCleanup = false;
75501234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    }
75601234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor  };
75701234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor
75844ec82b4a1597802f5bf17721481b8c265bc8dc5Anders Carlsson
759f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// PopCleanupBlocks - Takes the old cleanup stack size and emits
760f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// the cleanup blocks that have been added.
761f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize);
76244ec82b4a1597802f5bf17721481b8c265bc8dc5Anders Carlsson
763ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  void ResolveBranchFixups(llvm::BasicBlock *Target);
764ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
765f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// The given basic block lies in the current EH scope, but may be a
766f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// target of a potentially scope-crossing jump; get a stable handle
767f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// to which we can perform this jump later.
768ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  JumpDest getJumpDestInCurrentScope(llvm::BasicBlock *Target) {
769413e67778d593215d2f2161a4e712c8568f1ddd0John McCall    return JumpDest(Target,
770413e67778d593215d2f2161a4e712c8568f1ddd0John McCall                    EHStack.getInnermostNormalCleanup(),
771413e67778d593215d2f2161a4e712c8568f1ddd0John McCall                    NextCleanupDestIndex++);
772f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
773c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson
774f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// The given basic block lies in the current EH scope, but may be a
775f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// target of a potentially scope-crossing jump; get a stable handle
776f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// to which we can perform this jump later.
777ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  JumpDest getJumpDestInCurrentScope(llvm::StringRef Name = llvm::StringRef()) {
778ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    return getJumpDestInCurrentScope(createBasicBlock(Name));
779f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
780f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
781f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// EmitBranchThroughCleanup - Emit a branch from the current insert
782f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// block through the normal cleanup handling code (if any) and then
783f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// on to \arg Dest.
784f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  void EmitBranchThroughCleanup(JumpDest Dest);
785b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner
786b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner  /// isObviouslyBranchWithoutCleanups - Return true if a branch to the
787b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner  /// specified destination obviously has no cleanups to run.  'false' is always
788b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner  /// a conservatively correct answer for this method.
789b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner  bool isObviouslyBranchWithoutCleanups(JumpDest Dest) const;
790f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
791f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// EmitBranchThroughEHCleanup - Emit a branch from the current
792f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// insert block through the EH cleanup handling code (if any) and
793f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// then on to \arg Dest.
794ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  void EmitBranchThroughEHCleanup(UnwindDest Dest);
795ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
796ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// getRethrowDest - Returns the unified outermost-scope rethrow
797ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// destination.
798ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  UnwindDest getRethrowDest();
7990dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
800150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// An object to manage conditionally-evaluated expressions.
801150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  class ConditionalEvaluation {
802150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    llvm::BasicBlock *StartBB;
8031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
804150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  public:
805150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    ConditionalEvaluation(CodeGenFunction &CGF)
806150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      : StartBB(CGF.Builder.GetInsertBlock()) {}
8079cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
808150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    void begin(CodeGenFunction &CGF) {
809150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      assert(CGF.OutermostConditional != this);
810150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      if (!CGF.OutermostConditional)
811150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall        CGF.OutermostConditional = this;
812150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    }
813150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
814150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    void end(CodeGenFunction &CGF) {
815150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      assert(CGF.OutermostConditional != 0);
816150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      if (CGF.OutermostConditional == this)
817150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall        CGF.OutermostConditional = 0;
818150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    }
819150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
820150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    /// Returns a block which will be executed prior to each
821150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    /// evaluation of the conditional code.
822150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    llvm::BasicBlock *getStartingBlock() const {
823150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      return StartBB;
824150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    }
825150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  };
8261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8273019c444c672938c57f5573840071ecd73425ee7John McCall  /// isInConditionalBranch - Return true if we're currently emitting
8283019c444c672938c57f5573840071ecd73425ee7John McCall  /// one branch or the other of a conditional expression.
829150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  bool isInConditionalBranch() const { return OutermostConditional != 0; }
830150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
831150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// An RAII object to record that we're evaluating a statement
832150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// expression.
833150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  class StmtExprEvaluation {
834150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    CodeGenFunction &CGF;
835150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
836150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    /// We have to save the outermost conditional: cleanups in a
837150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    /// statement expression aren't conditional just because the
838150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    /// StmtExpr is.
839150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    ConditionalEvaluation *SavedOutermostConditional;
840150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
841150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  public:
842150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    StmtExprEvaluation(CodeGenFunction &CGF)
843150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      : CGF(CGF), SavedOutermostConditional(CGF.OutermostConditional) {
844150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      CGF.OutermostConditional = 0;
845150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    }
846150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
847150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    ~StmtExprEvaluation() {
848150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      CGF.OutermostConditional = SavedOutermostConditional;
849150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      CGF.EnsureInsertPoint();
850150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    }
851150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  };
852e996ffd240f20a1048179d7727a6ee3227261921John McCall
85356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// An object which temporarily prevents a value from being
85456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// destroyed by aggressive peephole optimizations that assume that
85556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// all uses of a value have been realized in the IR.
85656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  class PeepholeProtection {
85756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    llvm::Instruction *Inst;
85856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    friend class CodeGenFunction;
85956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
86056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  public:
86156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    PeepholeProtection() : Inst(0) {}
86256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  };
86356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
864e996ffd240f20a1048179d7727a6ee3227261921John McCall  /// An RAII object to set (and then clear) a mapping for an OpaqueValueExpr.
865e996ffd240f20a1048179d7727a6ee3227261921John McCall  class OpaqueValueMapping {
866e996ffd240f20a1048179d7727a6ee3227261921John McCall    CodeGenFunction &CGF;
867e996ffd240f20a1048179d7727a6ee3227261921John McCall    const OpaqueValueExpr *OpaqueValue;
86856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    bool BoundLValue;
86956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    CodeGenFunction::PeepholeProtection Protection;
870e996ffd240f20a1048179d7727a6ee3227261921John McCall
871e996ffd240f20a1048179d7727a6ee3227261921John McCall  public:
87256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    static bool shouldBindAsLValue(const Expr *expr) {
87356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      return expr->isGLValue() || expr->getType()->isRecordType();
87456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
87556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
87656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    /// Build the opaque value mapping for the given conditional
87756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    /// operator if it's the GNU ?: extension.  This is a common
87856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    /// enough pattern that the convenience operator is really
87956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    /// helpful.
88056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    ///
88156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    OpaqueValueMapping(CodeGenFunction &CGF,
88256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall                       const AbstractConditionalOperator *op) : CGF(CGF) {
88356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      if (isa<ConditionalOperator>(op)) {
88456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        OpaqueValue = 0;
88556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        BoundLValue = false;
88656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        return;
88756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      }
88856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
88956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      const BinaryConditionalOperator *e = cast<BinaryConditionalOperator>(op);
89056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      init(e->getOpaqueValue(), e->getCommon());
89156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
89256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
893e996ffd240f20a1048179d7727a6ee3227261921John McCall    OpaqueValueMapping(CodeGenFunction &CGF,
894e996ffd240f20a1048179d7727a6ee3227261921John McCall                       const OpaqueValueExpr *opaqueValue,
89556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall                       LValue lvalue)
89656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      : CGF(CGF), OpaqueValue(opaqueValue), BoundLValue(true) {
897e996ffd240f20a1048179d7727a6ee3227261921John McCall      assert(opaqueValue && "no opaque value expression!");
89856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      assert(shouldBindAsLValue(opaqueValue));
89956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      initLValue(lvalue);
90056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
90156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
90256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    OpaqueValueMapping(CodeGenFunction &CGF,
90356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall                       const OpaqueValueExpr *opaqueValue,
90456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall                       RValue rvalue)
90556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      : CGF(CGF), OpaqueValue(opaqueValue), BoundLValue(false) {
90656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      assert(opaqueValue && "no opaque value expression!");
90756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      assert(!shouldBindAsLValue(opaqueValue));
90856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      initRValue(rvalue);
909e996ffd240f20a1048179d7727a6ee3227261921John McCall    }
910e996ffd240f20a1048179d7727a6ee3227261921John McCall
911e996ffd240f20a1048179d7727a6ee3227261921John McCall    void pop() {
912e996ffd240f20a1048179d7727a6ee3227261921John McCall      assert(OpaqueValue && "mapping already popped!");
91356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      popImpl();
914e996ffd240f20a1048179d7727a6ee3227261921John McCall      OpaqueValue = 0;
915e996ffd240f20a1048179d7727a6ee3227261921John McCall    }
916e996ffd240f20a1048179d7727a6ee3227261921John McCall
917e996ffd240f20a1048179d7727a6ee3227261921John McCall    ~OpaqueValueMapping() {
91856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      if (OpaqueValue) popImpl();
91956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
92056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
92156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  private:
92256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    void popImpl() {
92356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      if (BoundLValue)
92456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        CGF.OpaqueLValues.erase(OpaqueValue);
92556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      else {
92656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        CGF.OpaqueRValues.erase(OpaqueValue);
92756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        CGF.unprotectFromPeepholes(Protection);
92856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      }
92956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
93056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
93156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    void init(const OpaqueValueExpr *ov, const Expr *e) {
93256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      OpaqueValue = ov;
93356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      BoundLValue = shouldBindAsLValue(ov);
93456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      assert(BoundLValue == shouldBindAsLValue(e)
93556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall             && "inconsistent expression value kinds!");
93656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      if (BoundLValue)
93756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        initLValue(CGF.EmitLValue(e));
93856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      else
93956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        initRValue(CGF.EmitAnyExpr(e));
94056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
94156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
94256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    void initLValue(const LValue &lv) {
94356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      CGF.OpaqueLValues.insert(std::make_pair(OpaqueValue, lv));
94456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
94556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
94656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    void initRValue(const RValue &rv) {
94756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      // Work around an extremely aggressive peephole optimization in
94856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      // EmitScalarConversion which assumes that all other uses of a
94956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      // value are extant.
95056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      Protection = CGF.protectFromPeepholes(rv);
95156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      CGF.OpaqueRValues.insert(std::make_pair(OpaqueValue, rv));
952e996ffd240f20a1048179d7727a6ee3227261921John McCall    }
953e996ffd240f20a1048179d7727a6ee3227261921John McCall  };
954e220455a059d926953befe72857b9525273717efFariborz Jahanian
955e220455a059d926953befe72857b9525273717efFariborz Jahanian  /// getByrefValueFieldNumber - Given a declaration, returns the LLVM field
956e220455a059d926953befe72857b9525273717efFariborz Jahanian  /// number that holds the value.
957e220455a059d926953befe72857b9525273717efFariborz Jahanian  unsigned getByRefValueLLVMField(const ValueDecl *VD) const;
95852a80e19ad688091723a52ad53337767bb0ac684Fariborz Jahanian
95952a80e19ad688091723a52ad53337767bb0ac684Fariborz Jahanian  /// BuildBlockByrefAddress - Computes address location of the
96052a80e19ad688091723a52ad53337767bb0ac684Fariborz Jahanian  /// variable which is declared as __block.
96152a80e19ad688091723a52ad53337767bb0ac684Fariborz Jahanian  llvm::Value *BuildBlockByrefAddress(llvm::Value *BaseAddr,
96252a80e19ad688091723a52ad53337767bb0ac684Fariborz Jahanian                                      const VarDecl *V);
9637f02f721d48772e4eee4fccd8af4f800be1bbc1eChris Lattnerprivate:
964d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  CGDebugInfo *DebugInfo;
965aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel  bool DisableDebugInfo;
96609429b962811f2c0a7a81a1b0a89f96f5878b20cMike Stump
96793c332a8ba2c193c435b293966d343dab15f555bJohn McCall  /// DidCallStackSave - Whether llvm.stacksave has been called. Used to avoid
96893c332a8ba2c193c435b293966d343dab15f555bJohn McCall  /// calling llvm.stacksave for multiple VLAs in the same scope.
96993c332a8ba2c193c435b293966d343dab15f555bJohn McCall  bool DidCallStackSave;
97093c332a8ba2c193c435b293966d343dab15f555bJohn McCall
971f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// IndirectBranch - The first time an indirect goto is seen we create a block
972f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// with an indirect branch.  Every time we see the address of a label taken,
973f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// we add the label to the indirect goto.  Every subsequent indirect goto is
974f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// codegen'd as a jump to the IndirectBranch's basic block.
975d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  llvm::IndirectBrInst *IndirectBranch;
9760ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar
9770dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
9780dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// decls.
9796b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  typedef llvm::DenseMap<const Decl*, llvm::Value*> DeclMapTy;
9806b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  DeclMapTy LocalDeclMap;
9815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// LabelMap - This keeps track of the LLVM basic block for each C label.
983ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  llvm::DenseMap<const LabelDecl*, JumpDest> LabelMap;
9840dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
9850dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  // BreakContinueStack - This keeps track of where break and continue
986e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson  // statements should jump to.
987da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner  struct BreakContinue {
988f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    BreakContinue(JumpDest Break, JumpDest Continue)
989f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      : BreakBlock(Break), ContinueBlock(Continue) {}
9900dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
991f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    JumpDest BreakBlock;
992f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    JumpDest ContinueBlock;
9930dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  };
994da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner  llvm::SmallVector<BreakContinue, 8> BreakContinueStack;
99518ccc7776ad6288c5f630dc7967fb99d099c9cd9Daniel Dunbar
9960dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// SwitchInsn - This is nearest current switch instruction. It is null if if
9970dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// current context is not in a switch.
99851b09f2c528c8460b5465c676173324e44176d62Devang Patel  llvm::SwitchInst *SwitchInsn;
99951b09f2c528c8460b5465c676173324e44176d62Devang Patel
10000dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// CaseRangeBlock - This block holds if condition check for last case
100180fd5f96e6805ac43aab99faabd5b4c8b19551b7Devang Patel  /// statement range in current switch instruction.
1002c049e4f406a7f7179eba98659044a32508e53289Devang Patel  llvm::BasicBlock *CaseRangeBlock;
1003c049e4f406a7f7179eba98659044a32508e53289Devang Patel
100456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// OpaqueLValues - Keeps track of the current set of opaque value
1005e996ffd240f20a1048179d7727a6ee3227261921John McCall  /// expressions.
100656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  llvm::DenseMap<const OpaqueValueExpr *, LValue> OpaqueLValues;
100756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  llvm::DenseMap<const OpaqueValueExpr *, RValue> OpaqueRValues;
1008e996ffd240f20a1048179d7727a6ee3227261921John McCall
10090dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  // VLASizeMap - This keeps track of the associated size for each VLA type.
1010bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman  // We track this by the size expression rather than the type itself because
1011bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman  // in certain situations, like a const qualifier applied to an VLA typedef,
1012bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman  // multiple VLA types can share the same size expression.
10130dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  // FIXME: Maybe this could be a stack of maps that is pushed/popped as we
10140dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  // enter/leave scopes.
1015bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman  llvm::DenseMap<const Expr*, llvm::Value*> VLASizeMap;
10160dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1017f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// A block containing a single 'unreachable' instruction.  Created
1018f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// lazily by getUnreachableBlock().
1019f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *UnreachableBlock;
10200dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1021f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson  /// CXXThisDecl - When generating code for a C++ member function,
1022f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson  /// this will hold the implicit 'this' declaration.
10232b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  ImplicitParamDecl *CXXThisDecl;
10242504941793b549323f9d29c62507cf21d865fadeJohn McCall  llvm::Value *CXXThisValue;
10251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1026f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson  /// CXXVTTDecl - When generating code for a base object constructor or
1027f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson  /// base object destructor with virtual bases, this will hold the implicit
1028f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson  /// VTT parameter.
1029f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson  ImplicitParamDecl *CXXVTTDecl;
10302504941793b549323f9d29c62507cf21d865fadeJohn McCall  llvm::Value *CXXVTTValue;
10319cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1032150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// OutermostConditional - Points to the outermost active
1033150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// conditional control.  This is used so that we know if a
1034150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// temporary should be destroyed conditionally.
1035150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  ConditionalEvaluation *OutermostConditional;
10361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10377dfa4079f4b37507048b19bfc693d3b015d9dd11Anders Carlsson
10387dfa4079f4b37507048b19bfc693d3b015d9dd11Anders Carlsson  /// ByrefValueInfoMap - For each __block variable, contains a pair of the LLVM
10397dfa4079f4b37507048b19bfc693d3b015d9dd11Anders Carlsson  /// type as well as the field number that contains the actual data.
10409cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  llvm::DenseMap<const ValueDecl *, std::pair<const llvm::Type *,
10417dfa4079f4b37507048b19bfc693d3b015d9dd11Anders Carlsson                                              unsigned> > ByRefValueInfo;
10429cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1043f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *TerminateLandingPad;
1044182f383db1782af752ecaf607fdff72a8542088bMike Stump  llvm::BasicBlock *TerminateHandler;
104583252dcfe61aaebcb6bc117e71dc12968729513fChris Lattner  llvm::BasicBlock *TrapBB;
1046940670512d7c3d93389bb38a426abcb7ef44271eEli Friedman
10475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
10485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CodeGenFunction(CodeGenModule &cgm);
10490dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
10501e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CodeGenTypes &getTypes() const { return CGM.getTypes(); }
1051f2aac84709c418189e476ad591848dad50291885John McCall  ASTContext &getContext() const { return CGM.getContext(); }
1052aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel  CGDebugInfo *getDebugInfo() {
1053aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel    if (DisableDebugInfo)
1054aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel      return NULL;
1055aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel    return DebugInfo;
1056aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel  }
1057aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel  void disableDebugInfo() { DisableDebugInfo = true; }
1058aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel  void enableDebugInfo() { DisableDebugInfo = false; }
1059aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel
1060f85e193739c953358c865005855253af4f68a497John McCall  bool shouldUseFusedARCCalls() {
1061f85e193739c953358c865005855253af4f68a497John McCall    return CGM.getCodeGenOpts().OptimizationLevel == 0;
1062f85e193739c953358c865005855253af4f68a497John McCall  }
10635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1064d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  const LangOptions &getLangOptions() const { return CGM.getLangOptions(); }
1065d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall
1066f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Returns a pointer to the function's exception object slot, which
1067f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// is assigned in every landing pad.
1068f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::Value *getExceptionSlot();
106993c332a8ba2c193c435b293966d343dab15f555bJohn McCall  llvm::Value *getEHSelectorSlot();
1070f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1071ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::Value *getNormalCleanupDestSlot();
1072ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::Value *getEHCleanupDestSlot();
1073ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1074f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *getUnreachableBlock() {
1075f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    if (!UnreachableBlock) {
1076f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      UnreachableBlock = createBasicBlock("unreachable");
1077f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      new llvm::UnreachableInst(getLLVMContext(), UnreachableBlock);
1078f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    }
1079f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return UnreachableBlock;
1080f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
1081f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1082f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *getInvokeDest() {
1083f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    if (!EHStack.requiresLandingPad()) return 0;
1084f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return getInvokeDestImpl();
1085f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
10869834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar
1087d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  llvm::LLVMContext &getLLVMContext() { return CGM.getLLVMContext(); }
108869243825cb5c91ec7207256aa57ae327cfaf8cb2Owen Anderson
10899834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar  //===--------------------------------------------------------------------===//
10909834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar  //                                  Objective-C
10919834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar  //===--------------------------------------------------------------------===//
10929834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar
1093391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  void GenerateObjCMethod(const ObjCMethodDecl *OMD);
1094af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
10950dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  void StartObjCMethod(const ObjCMethodDecl *MD,
10968d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel                       const ObjCContainerDecl *CD,
10978d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel                       SourceLocation StartLoc);
1098af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
10990dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// GenerateObjCGetter - Synthesize an Objective-C property getter function.
1100fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian  void GenerateObjCGetter(ObjCImplementationDecl *IMP,
1101fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian                          const ObjCPropertyImplDecl *PID);
11022846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  void GenerateObjCGetterBody(ObjCIvarDecl *Ivar, bool IsAtomic, bool IsStrong);
11032846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  void GenerateObjCAtomicSetterBody(ObjCMethodDecl *OMD,
11042846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian                                    ObjCIvarDecl *Ivar);
11052846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian
1106109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  void GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
1107109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian                                  ObjCMethodDecl *MD, bool ctor);
1108af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
11090dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// GenerateObjCSetter - Synthesize an Objective-C property setter function
11100dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// for the given property.
1111fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian  void GenerateObjCSetter(ObjCImplementationDecl *IMP,
1112fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian                          const ObjCPropertyImplDecl *PID);
11130b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian  bool IndirectObjCSetterArg(const CGFunctionInfo &FI);
111415bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian  bool IvarTypeWithAggrGCObjects(QualType Ty);
1115af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
11164e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump  //===--------------------------------------------------------------------===//
11174e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump  //                                  Block Bits
11184e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump  //===--------------------------------------------------------------------===//
11194e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump
11206b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  llvm::Value *EmitBlockLiteral(const BlockExpr *);
11212a7eb28397148079cbc8e54e8a3871ef01c4f4bcBlaine Garst  llvm::Constant *BuildDescriptorBlockDecl(const BlockExpr *,
112289ecd41e0a6bfb3b0913dbe41c3c666340b308c7Fariborz Jahanian                                           const CGBlockInfo &Info,
11230892099dbc640720400a1d9decd2733a09d733e5Mike Stump                                           const llvm::StructType *,
1124d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall                                           llvm::Constant *BlockVarLayout);
11254e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump
1126564360be450b319aeafc26698be9811837bfb826Fariborz Jahanian  llvm::Function *GenerateBlockFunction(GlobalDecl GD,
11276b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                                        const CGBlockInfo &Info,
11286cc88f78fd36d3511b89412b193494b3e423cbffMike Stump                                        const Decl *OuterFuncDecl,
11296b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                                        const DeclMapTy &ldm);
11304e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump
1131d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  llvm::Constant *GenerateCopyHelperFunction(const CGBlockInfo &blockInfo);
1132d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  llvm::Constant *GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo);
1133d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall
1134d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  void BuildBlockRelease(llvm::Value *DeclPtr, BlockFieldFlags flags);
1135d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall
11365af02db5a48476e0748f135369663080eae87c64John McCall  class AutoVarEmission;
11375af02db5a48476e0748f135369663080eae87c64John McCall
11385af02db5a48476e0748f135369663080eae87c64John McCall  void emitByrefStructureInit(const AutoVarEmission &emission);
11395af02db5a48476e0748f135369663080eae87c64John McCall  void enterByrefCleanup(const AutoVarEmission &emission);
11405af02db5a48476e0748f135369663080eae87c64John McCall
11416b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  llvm::Value *LoadBlockStruct() {
11426b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    assert(BlockPointer && "no block pointer set!");
11436b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    return BlockPointer;
11446b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  }
11454e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump
1146ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  void AllocateBlockCXXThisPointer(const CXXThisExpr *E);
1147ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  void AllocateBlockDecl(const BlockDeclRefExpr *E);
1148ee5042903d53fa7b0fbc1902d0ea07d57c7775b1John McCall  llvm::Value *GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
1149ee5042903d53fa7b0fbc1902d0ea07d57c7775b1John McCall    return GetAddrOfBlockDecl(E->getDecl(), E->isByRef());
1150ee5042903d53fa7b0fbc1902d0ea07d57c7775b1John McCall  }
11516b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  llvm::Value *GetAddrOfBlockDecl(const VarDecl *var, bool ByRef);
11526b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  const llvm::Type *BuildByRefType(const VarDecl *var);
1153dab514fc30242c7afd6c03956e46136c400fb0d3Mike Stump
1154d26bc76c98006609002d9930f8840490e88ac5b5John McCall  void GenerateCode(GlobalDecl GD, llvm::Function *Fn,
1155d26bc76c98006609002d9930f8840490e88ac5b5John McCall                    const CGFunctionInfo &FnInfo);
11560ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson  void StartFunction(GlobalDecl GD, QualType RetTy,
11577c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                     llvm::Function *Fn,
1158d26bc76c98006609002d9930f8840490e88ac5b5John McCall                     const CGFunctionInfo &FnInfo,
11592284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                     const FunctionArgList &Args,
11609c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller                     SourceLocation StartLoc);
1161a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar
11629fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  void EmitConstructorBody(FunctionArgList &Args);
11639fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  void EmitDestructorBody(FunctionArgList &Args);
11649fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  void EmitFunctionBody(FunctionArgList &Args);
1165a355e07454463b19829ac92ffd115a097faff0e0John McCall
11660dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitReturnBlock - Emit the unified return block, trying to avoid its
11670dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// emission when possible.
11681c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  void EmitReturnBlock();
11691c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
11700dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// FinishFunction - Complete IR generation of the current function. It is
11710dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// legal to call this function even if there is no current insertion point.
1172af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  void FinishFunction(SourceLocation EndLoc=SourceLocation());
117317b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
1174519c32880b99396034ecaedc0cd86db2e8485003Anders Carlsson  /// GenerateThunk - Generate a thunk for the given method.
1175d26bc76c98006609002d9930f8840490e88ac5b5John McCall  void GenerateThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo,
1176d26bc76c98006609002d9930f8840490e88ac5b5John McCall                     GlobalDecl GD, const ThunkInfo &Thunk);
11779cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
11787dcdf5ba9324a9577461eae302e88fdd52e310c5Eli Friedman  void GenerateVarArgsThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo,
11797dcdf5ba9324a9577461eae302e88fdd52e310c5Eli Friedman                            GlobalDecl GD, const ThunkInfo &Thunk);
11807dcdf5ba9324a9577461eae302e88fdd52e310c5Eli Friedman
1181fb8cc253420e93cee33d29df5a2bdae6aaf16e39Douglas Gregor  void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type,
1182fb8cc253420e93cee33d29df5a2bdae6aaf16e39Douglas Gregor                        FunctionArgList &Args);
11831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1184d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson  /// InitializeVTablePointer - Initialize the vtable pointer of the given
1185d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson  /// subobject.
1186d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson  ///
11879cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  void InitializeVTablePointer(BaseSubobject Base,
1188b3b772ea15a4cd54879e244629aa685ead9548bbAnders Carlsson                               const CXXRecordDecl *NearestVBase,
1189d6fb21fa1053e5d616af55b181bb03c50d4b0d24Ken Dyck                               CharUnits OffsetFromNearestVBase,
1190d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson                               llvm::Constant *VTable,
1191d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson                               const CXXRecordDecl *VTableClass);
1192d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson
1193d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson  typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
11949cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  void InitializeVTablePointers(BaseSubobject Base,
1195b3b772ea15a4cd54879e244629aa685ead9548bbAnders Carlsson                                const CXXRecordDecl *NearestVBase,
1196d6fb21fa1053e5d616af55b181bb03c50d4b0d24Ken Dyck                                CharUnits OffsetFromNearestVBase,
1197603d6d12cc4cba1ef219a3d55c698c7c87f87adfAnders Carlsson                                bool BaseIsNonVirtualPrimaryBase,
1198603d6d12cc4cba1ef219a3d55c698c7c87f87adfAnders Carlsson                                llvm::Constant *VTable,
1199603d6d12cc4cba1ef219a3d55c698c7c87f87adfAnders Carlsson                                const CXXRecordDecl *VTableClass,
1200603d6d12cc4cba1ef219a3d55c698c7c87f87adfAnders Carlsson                                VisitedVirtualBasesSetTy& VBases);
120177a259c6dbf582949af8a243b472c0c7faa373a6Eli Friedman
1202603d6d12cc4cba1ef219a3d55c698c7c87f87adfAnders Carlsson  void InitializeVTablePointers(const CXXRecordDecl *ClassDecl);
1203d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson
1204043fb9a1fc0609285f60f0f87e5a18195408f34cDan Gohman  /// GetVTablePtr - Return the Value of the vtable pointer member pointed
1205043fb9a1fc0609285f60f0f87e5a18195408f34cDan Gohman  /// to by This.
1206043fb9a1fc0609285f60f0f87e5a18195408f34cDan Gohman  llvm::Value *GetVTablePtr(llvm::Value *This, const llvm::Type *Ty);
1207d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson
120850da2cadcc6da86abff6772de65280ace2cabc94John McCall  /// EnterDtorCleanups - Enter the cleanups necessary to complete the
120950da2cadcc6da86abff6772de65280ace2cabc94John McCall  /// given phase of destruction for a destructor.  The end result
121050da2cadcc6da86abff6772de65280ace2cabc94John McCall  /// should call destructors on members and base classes in reverse
121150da2cadcc6da86abff6772de65280ace2cabc94John McCall  /// order of their construction.
121250da2cadcc6da86abff6772de65280ace2cabc94John McCall  void EnterDtorCleanups(const CXXDestructorDecl *Dtor, CXXDtorType Type);
12131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12147255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  /// ShouldInstrumentFunction - Return true if the current function should be
12157255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  /// instrumented with __cyg_profile_func_* calls
12167255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  bool ShouldInstrumentFunction();
12177255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
12187255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  /// EmitFunctionInstrumentation - Emit LLVM code to call the specified
12197255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  /// instrumentation function with the current function and the call site, if
12207255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  /// function instrumentation is enabled.
12217255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  void EmitFunctionInstrumentation(const char *Fn);
12227255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
1223be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky  /// EmitMCountInstrumentation - Emit call to .mcount.
1224be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky  void EmitMCountInstrumentation();
1225be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky
12260dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitFunctionProlog - Emit the target specific LLVM code to load the
12270dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// arguments for the given function. This is also responsible for naming the
12280dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// LLVM function arguments.
122988b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  void EmitFunctionProlog(const CGFunctionInfo &FI,
123088b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar                          llvm::Function *Fn,
123117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar                          const FunctionArgList &Args);
123217b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
12330dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitFunctionEpilog - Emit the target specific LLVM code to return the
12340dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// given temporary.
123535b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  void EmitFunctionEpilog(const CGFunctionInfo &FI);
123617b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
1237cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  /// EmitStartEHSpec - Emit the start of the exception spec.
1238cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  void EmitStartEHSpec(const Decl *D);
1239cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump
1240cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  /// EmitEndEHSpec - Emit the end of the exception spec.
1241cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  void EmitEndEHSpec(const Decl *D);
1242cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump
1243f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// getTerminateLandingPad - Return a landing pad that just calls terminate.
1244f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *getTerminateLandingPad();
1245f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1246f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// getTerminateHandler - Return a handler (not a landing pad, just
1247f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// a catch handler) that just calls terminate.  This is used when
1248f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// a terminate scope encloses a try.
12499b39c51ae3c547568ac42325f94b4197618f6b18Mike Stump  llvm::BasicBlock *getTerminateHandler();
12509b39c51ae3c547568ac42325f94b4197618f6b18Mike Stump
12518b1a343b6b360d63d5dc8a6beb841ce4414c1e00Daniel Dunbar  const llvm::Type *ConvertTypeForMem(QualType T);
12525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  const llvm::Type *ConvertType(QualType T);
1253bff225ecf77fb891596ecb1b27196310d268365eJohn McCall  const llvm::Type *ConvertType(const TypeDecl *T) {
1254bff225ecf77fb891596ecb1b27196310d268365eJohn McCall    return ConvertType(getContext().getTypeDeclType(T));
1255bff225ecf77fb891596ecb1b27196310d268365eJohn McCall  }
1256c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner
12570dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// LoadObjCSelf - Load the value of self. This function is only valid while
12580dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// generating code for an Objective-C method.
1259c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner  llvm::Value *LoadObjCSelf();
12600dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
12610dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// TypeOfSelfObject - Return type of object that this self represents.
126245012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian  QualType TypeOfSelfObject();
12634111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner
12645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// hasAggregateLLVMType - Return true if the specified AST type will map into
12655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// an aggregate LLVM type or is void.
12665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  static bool hasAggregateLLVMType(QualType T);
126755e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar
126855e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  /// createBasicBlock - Create an LLVM basic block.
1269d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  llvm::BasicBlock *createBasicBlock(llvm::StringRef name = "",
1270d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall                                     llvm::Function *parent = 0,
1271d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall                                     llvm::BasicBlock *before = 0) {
127229ea6729155c759836be4b5453bee9ef8e1e97eeDaniel Dunbar#ifdef NDEBUG
1273d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall    return llvm::BasicBlock::Create(getLLVMContext(), "", parent, before);
127429ea6729155c759836be4b5453bee9ef8e1e97eeDaniel Dunbar#else
1275d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall    return llvm::BasicBlock::Create(getLLVMContext(), name, parent, before);
127629ea6729155c759836be4b5453bee9ef8e1e97eeDaniel Dunbar#endif
127755e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  }
12780dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
12795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
12805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// label maps to.
1281ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  JumpDest getJumpDestForLabel(const LabelDecl *S);
12820dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1283f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// SimplifyForwardingBlocks - If the given basic block is only a branch to
1284f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// another basic block, simplify it. This assumes that no other code could
1285f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// potentially reference the basic block.
1286aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  void SimplifyForwardingBlocks(llvm::BasicBlock *BB);
1287aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar
12880dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitBlock - Emit the given block \arg BB and set it as the insert point,
12890dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// adding a fall-through branch from the current insert block if
12900dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// necessary. It is legal to call this function even if there is no current
12910dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// insertion point.
1292a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar  ///
12930dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// IsFinished - If true, indicates that the caller has finished emitting
12940dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// branches to the given block and does not expect to emit code into it. This
12950dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// means the block can be ignored if it is unreachable.
1296a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar  void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false);
1297824e3bd76b2e32db2ec6e4d2d113413e518d1c63Daniel Dunbar
12980dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitBranch - Emit a branch to the specified basic block from the current
12990dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// insert block, taking care to avoid creation of branches from dummy
13000dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// blocks. It is legal to call this function even if there is no current
13010dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// insertion point.
13025e08ad3cc62ab94649959ae227a9a411a729bf49Daniel Dunbar  ///
13030dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// This function clears the current insertion point. The caller should follow
13040dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// calls to this function with calls to Emit*Block prior to generation new
13050dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// code.
1306d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  void EmitBranch(llvm::BasicBlock *Block);
1307d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar
13080dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// HaveInsertPoint - True if an insertion point is defined. If not, this
13090dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// indicates that the current code being emitted is unreachable.
13100dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  bool HaveInsertPoint() const {
1311a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar    return Builder.GetInsertBlock() != 0;
1312a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar  }
1313a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar
13140dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EnsureInsertPoint - Ensure that an insertion point is defined so that
13150dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// emitted IR has a place to go. Note that by definition, if this function
13160dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// creates a block then that block is unreachable; callers may do better to
13170dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// detect when no insertion point is defined and simply skip IR generation.
1318a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar  void EnsureInsertPoint() {
1319a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar    if (!HaveInsertPoint())
1320a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar      EmitBlock(createBasicBlock());
1321a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar  }
13220dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1323488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar  /// ErrorUnsupported - Print out an error that codegen doesn't support the
1324dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner  /// specified stmt yet.
132590df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  void ErrorUnsupported(const Stmt *S, const char *Type,
132690df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar                        bool OmitOnError=false);
13275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
13295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //                                  Helpers
13305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
13310dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1332983e3d7dac99aed376939f1a4daadef98e09c3b9Daniel Dunbar  LValue MakeAddrLValue(llvm::Value *V, QualType T, unsigned Alignment = 0) {
13333d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman    return LValue::MakeAddr(V, T, Alignment, getContext(),
13343d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman                            CGM.getTBAAInfo(T));
13355cf8bfec711116b3c4acc70a00717b2e119e7550Daniel Dunbar  }
13365cf8bfec711116b3c4acc70a00717b2e119e7550Daniel Dunbar
13375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// CreateTempAlloca - This creates a alloca and inserts it into the entry
1338195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar  /// block. The caller is responsible for setting an appropriate alignment on
1339195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar  /// the alloca.
13405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty,
1341259e9ccf882d11491ad149aec5e6d7a061c9f938Daniel Dunbar                                     const llvm::Twine &Name = "tmp");
13420dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1343ac418162692a951ca3796d6830496a85a2d12493John McCall  /// InitTempAlloca - Provide an initial value for the given alloca.
1344ac418162692a951ca3796d6830496a85a2d12493John McCall  void InitTempAlloca(llvm::AllocaInst *Alloca, llvm::Value *Value);
1345ac418162692a951ca3796d6830496a85a2d12493John McCall
13469bd4da2cfa8b956b851200db8f9a37e175edbb15Daniel Dunbar  /// CreateIRTemp - Create a temporary IR object of the given type, with
13479bd4da2cfa8b956b851200db8f9a37e175edbb15Daniel Dunbar  /// appropriate alignment. This routine should only be used when an temporary
13489bd4da2cfa8b956b851200db8f9a37e175edbb15Daniel Dunbar  /// value needs to be stored into an alloca (for example, to avoid explicit
13499bd4da2cfa8b956b851200db8f9a37e175edbb15Daniel Dunbar  /// PHI construction), but the type is the IR type, not the type appropriate
13509bd4da2cfa8b956b851200db8f9a37e175edbb15Daniel Dunbar  /// for storing in memory.
1351121b3facb4e0585d23766f9c1e4fdf9018a4b217Chris Lattner  llvm::AllocaInst *CreateIRTemp(QualType T, const llvm::Twine &Name = "tmp");
13529bd4da2cfa8b956b851200db8f9a37e175edbb15Daniel Dunbar
1353195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar  /// CreateMemTemp - Create a temporary memory object of the given type, with
1354195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar  /// appropriate alignment.
1355121b3facb4e0585d23766f9c1e4fdf9018a4b217Chris Lattner  llvm::AllocaInst *CreateMemTemp(QualType T, const llvm::Twine &Name = "tmp");
1356195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar
1357558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  /// CreateAggTemp - Create a temporary memory object for the given
1358558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  /// aggregate type.
1359558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  AggValueSlot CreateAggTemp(QualType T, const llvm::Twine &Name = "tmp") {
1360f85e193739c953358c865005855253af4f68a497John McCall    return AggValueSlot::forAddr(CreateMemTemp(T, Name), T.getQualifiers(),
1361f85e193739c953358c865005855253af4f68a497John McCall                                 false);
1362558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  }
1363558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall
1364d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  /// Emit a cast to void* in the appropriate address space.
1365d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  llvm::Value *EmitCastToVoidPtr(llvm::Value *value);
1366d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall
13675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
13685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// expression and compare the result against zero, returning an Int1Ty value.
13695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::Value *EvaluateExprAsBool(const Expr *E);
13705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
13712a41637a995affa1563f4d82a8b026e326a2faa0John McCall  /// EmitIgnoredExpr - Emit an expression in a context which ignores the result.
13722a41637a995affa1563f4d82a8b026e326a2faa0John McCall  void EmitIgnoredExpr(const Expr *E);
13732a41637a995affa1563f4d82a8b026e326a2faa0John McCall
13749b65551d0b387a7597fb39356a4d8ef10046445eChris Lattner  /// EmitAnyExpr - Emit code to compute the specified expression which can have
13759b65551d0b387a7597fb39356a4d8ef10046445eChris Lattner  /// any type.  The result is returned as an RValue struct.  If this is an
13769b65551d0b387a7597fb39356a4d8ef10046445eChris Lattner  /// aggregate expression, the aggloc/agglocvolatile arguments indicate where
13779b65551d0b387a7597fb39356a4d8ef10046445eChris Lattner  /// the result should be returned.
137849d1cd5a09ed3df353371fd7f206674a85e0fb45Mike Stump  ///
137949d1cd5a09ed3df353371fd7f206674a85e0fb45Mike Stump  /// \param IgnoreResult - True if the resulting value isn't used.
1380558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  RValue EmitAnyExpr(const Expr *E,
1381558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall                     AggValueSlot AggSlot = AggValueSlot::ignored(),
1382558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall                     bool IgnoreResult = false);
1383d9363c3a80168283b3da518b4e17f545a6246857Devang Patel
13840dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  // EmitVAListRef - Emit a "reference" to a va_list; this is either the address
13850dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  // or the value of the expression, depending on how va_list is defined.
13864fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  llvm::Value *EmitVAListRef(const Expr *E);
13874fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman
13880dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
13890dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// always be accessible even if no aggregate location is provided.
1390558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  RValue EmitAnyExprToTemp(const Expr *E);
139146f45b9bec4a265ad8400a538e5ec3a5683617f1Daniel Dunbar
139260d3365b46eb826fba44483583c0051ac5c41fe3John McCall  /// EmitAnyExprToMem - Emits the code necessary to evaluate an
13933d3ec1c099ec8bfac3aa1fb0126fe515b7c7fa05John McCall  /// arbitrary expression into the given memory location.
13943d3ec1c099ec8bfac3aa1fb0126fe515b7c7fa05John McCall  void EmitAnyExprToMem(const Expr *E, llvm::Value *Location,
1395f85e193739c953358c865005855253af4f68a497John McCall                        Qualifiers Quals, bool IsInitializer);
13963d3ec1c099ec8bfac3aa1fb0126fe515b7c7fa05John McCall
139760d3365b46eb826fba44483583c0051ac5c41fe3John McCall  /// EmitExprAsInit - Emits the code necessary to initialize a
139860d3365b46eb826fba44483583c0051ac5c41fe3John McCall  /// location in memory with the given initializer.
1399f85e193739c953358c865005855253af4f68a497John McCall  void EmitExprAsInit(const Expr *init, const ValueDecl *D,
1400a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall                      LValue lvalue, bool capturedByInit);
140160d3365b46eb826fba44483583c0051ac5c41fe3John McCall
140227fe2e6c97a3782a0639d87b460741e8ba5d076dMike Stump  /// EmitAggregateCopy - Emit an aggrate copy.
140327fe2e6c97a3782a0639d87b460741e8ba5d076dMike Stump  ///
140427fe2e6c97a3782a0639d87b460741e8ba5d076dMike Stump  /// \param isVolatile - True iff either the source or the destination is
140527fe2e6c97a3782a0639d87b460741e8ba5d076dMike Stump  /// volatile.
14067482d12c345c6391f8956850545e2d4aa7701ce6Daniel Dunbar  void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
140727fe2e6c97a3782a0639d87b460741e8ba5d076dMike Stump                         QualType EltTy, bool isVolatile=false);
14087482d12c345c6391f8956850545e2d4aa7701ce6Daniel Dunbar
140951b09f2c528c8460b5465c676173324e44176d62Devang Patel  /// StartBlock - Start new block named N. If insert block is a dummy block
141051b09f2c528c8460b5465c676173324e44176d62Devang Patel  /// then reuse it.
141151b09f2c528c8460b5465c676173324e44176d62Devang Patel  void StartBlock(const char *N);
141251b09f2c528c8460b5465c676173324e44176d62Devang Patel
1413813733577d33ec56479667b49e1bff42dc6bba90Lauro Ramos Venancio  /// GetAddrOfStaticLocalVar - Return the address of a static local variable.
14144c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  llvm::Constant *GetAddrOfStaticLocalVar(const VarDecl *BVD) {
14154c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    return cast<llvm::Constant>(GetAddrOfLocalVar(BVD));
14164c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  }
14174f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman
1418dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson  /// GetAddrOfLocalVar - Return the address of a local variable.
14194c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  llvm::Value *GetAddrOfLocalVar(const VarDecl *VD) {
14204c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    llvm::Value *Res = LocalDeclMap[VD];
14214c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!");
14224c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    return Res;
14234c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  }
14240dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
142556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// getOpaqueLValueMapping - Given an opaque value expression (which
142656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// must be mapped to an l-value), return its mapping.
142756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  const LValue &getOpaqueLValueMapping(const OpaqueValueExpr *e) {
142856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    assert(OpaqueValueMapping::shouldBindAsLValue(e));
142956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
143056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    llvm::DenseMap<const OpaqueValueExpr*,LValue>::iterator
143156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      it = OpaqueLValues.find(e);
143256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    assert(it != OpaqueLValues.end() && "no mapping for opaque value!");
143356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    return it->second;
143456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  }
143556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
143656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// getOpaqueRValueMapping - Given an opaque value expression (which
143756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// must be mapped to an r-value), return its mapping.
143856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  const RValue &getOpaqueRValueMapping(const OpaqueValueExpr *e) {
143956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    assert(!OpaqueValueMapping::shouldBindAsLValue(e));
144056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
144156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    llvm::DenseMap<const OpaqueValueExpr*,RValue>::iterator
144256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      it = OpaqueRValues.find(e);
144356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    assert(it != OpaqueRValues.end() && "no mapping for opaque value!");
1444e996ffd240f20a1048179d7727a6ee3227261921John McCall    return it->second;
1445e996ffd240f20a1048179d7727a6ee3227261921John McCall  }
1446e996ffd240f20a1048179d7727a6ee3227261921John McCall
14474f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman  /// getAccessedFieldNo - Given an encoded value and a result number, return
14484f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman  /// the input field number being accessed.
14494f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman  static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts);
14504f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman
1451ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  llvm::BlockAddress *GetAddrOfLabel(const LabelDecl *L);
14523d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  llvm::BasicBlock *GetIndirectGotoBlock();
14530ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar
14541884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  /// EmitNullInitialization - Generate code to set a value of the given type to
14551884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  /// null, If the type contains data member pointers, they will be initialized
14561884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  /// to -1 in accordance with the Itanium C++ ABI.
14571884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  void EmitNullInitialization(llvm::Value *DestPtr, QualType Ty);
1458ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson
1459ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson  // EmitVAArg - Generate code to get an argument from the passed in pointer
1460ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson  // and update it accordingly. The return value is a pointer to the argument.
1461ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson  // FIXME: We should be able to get rid of this method and use the va_arg
14620dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  // instruction in LLVM instead once it works well enough.
1463ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson  llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty);
1464f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson
1465f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// EmitVLASize - Generate code for any VLA size expressions that might occur
1466f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// in a variably modified type. If Ty is a VLA, will return the value that
1467f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// corresponds to the size in bytes of the VLA type. Will return 0 otherwise.
1468d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  ///
1469d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  /// This function can be called with a null (unreachable) insert point.
147060d35413662ebdcd1d31e34a8a7c665eb6977f1eAnders Carlsson  llvm::Value *EmitVLASize(QualType Ty);
14710dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1472dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson  // GetVLASize - Returns an LLVM value that corresponds to the size in bytes
1473dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson  // of a variable length array type.
1474dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson  llvm::Value *GetVLASize(const VariableArrayType *);
1475dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson
14765f4307b7ba164b03c853c8d3eb4674d33f8967a6Anders Carlsson  /// LoadCXXThis - Load the value of 'this'. This function is only valid while
14775f4307b7ba164b03c853c8d3eb4674d33f8967a6Anders Carlsson  /// generating code for an C++ member function.
14782504941793b549323f9d29c62507cf21d865fadeJohn McCall  llvm::Value *LoadCXXThis() {
14792504941793b549323f9d29c62507cf21d865fadeJohn McCall    assert(CXXThisValue && "no 'this' value for this function");
14802504941793b549323f9d29c62507cf21d865fadeJohn McCall    return CXXThisValue;
14812504941793b549323f9d29c62507cf21d865fadeJohn McCall  }
14821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1483c997d4278d329e18891aac9698fb991b2d4622ebAnders Carlsson  /// LoadCXXVTT - Load the VTT parameter to base constructors/destructors have
1484c997d4278d329e18891aac9698fb991b2d4622ebAnders Carlsson  /// virtual bases.
14852504941793b549323f9d29c62507cf21d865fadeJohn McCall  llvm::Value *LoadCXXVTT() {
14862504941793b549323f9d29c62507cf21d865fadeJohn McCall    assert(CXXVTTValue && "no VTT value for this function");
14872504941793b549323f9d29c62507cf21d865fadeJohn McCall    return CXXVTTValue;
14882504941793b549323f9d29c62507cf21d865fadeJohn McCall  }
1489bff225ecf77fb891596ecb1b27196310d268365eJohn McCall
1490bff225ecf77fb891596ecb1b27196310d268365eJohn McCall  /// GetAddressOfBaseOfCompleteClass - Convert the given pointer to a
14918561a8666c70f924c8f0209c41b9b77bbbf90607Anders Carlsson  /// complete class to the given direct base.
14928561a8666c70f924c8f0209c41b9b77bbbf90607Anders Carlsson  llvm::Value *
14938561a8666c70f924c8f0209c41b9b77bbbf90607Anders Carlsson  GetAddressOfDirectBaseInCompleteClass(llvm::Value *Value,
14948561a8666c70f924c8f0209c41b9b77bbbf90607Anders Carlsson                                        const CXXRecordDecl *Derived,
14958561a8666c70f924c8f0209c41b9b77bbbf90607Anders Carlsson                                        const CXXRecordDecl *Base,
14968561a8666c70f924c8f0209c41b9b77bbbf90607Anders Carlsson                                        bool BaseIsVirtual);
1497a88ad5618fcb07c5374c79d6781a33234984b039Anders Carlsson
1498f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// GetAddressOfBaseClass - This function will add the necessary delta to the
1499f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// load of 'this' and returns address of the base class.
15009cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  llvm::Value *GetAddressOfBaseClass(llvm::Value *Value,
15018561a8666c70f924c8f0209c41b9b77bbbf90607Anders Carlsson                                     const CXXRecordDecl *Derived,
1502f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                     CastExpr::path_const_iterator PathBegin,
1503f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                     CastExpr::path_const_iterator PathEnd,
150434a2d384c745ebc39cae45dc1c0c4a6a7012e09bAnders Carlsson                                     bool NullCheckValue);
150534a2d384c745ebc39cae45dc1c0c4a6a7012e09bAnders Carlsson
1506a3697c9c155bda93fd2802f37084b620f4738822Anders Carlsson  llvm::Value *GetAddressOfDerivedClass(llvm::Value *Value,
15078561a8666c70f924c8f0209c41b9b77bbbf90607Anders Carlsson                                        const CXXRecordDecl *Derived,
1508f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                        CastExpr::path_const_iterator PathBegin,
1509f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                        CastExpr::path_const_iterator PathEnd,
15105a0f49ebc83e7fe0da07b9964c44b0a7fae270cbAnders Carlsson                                        bool NullCheckValue);
1511a3697c9c155bda93fd2802f37084b620f4738822Anders Carlsson
1512bb7e17b52ffaa4097b4c4d7935746d23539ffe2aAnders Carlsson  llvm::Value *GetVirtualBaseClassOffset(llvm::Value *This,
1513bb7e17b52ffaa4097b4c4d7935746d23539ffe2aAnders Carlsson                                         const CXXRecordDecl *ClassDecl,
1514bb7e17b52ffaa4097b4c4d7935746d23539ffe2aAnders Carlsson                                         const CXXRecordDecl *BaseClassDecl);
15159cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1516c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall  void EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor,
1517c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall                                      CXXCtorType CtorType,
1518c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall                                      const FunctionArgList &Args);
1519059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt  // It's important not to confuse this and the previous function. Delegating
1520059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt  // constructors are the C++0x feature. The constructor delegate optimization
1521059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt  // is used to reduce duplication in the base and complete consturctors where
1522059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt  // they are substantially the same.
1523059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt  void EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor,
1524059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt                                        const FunctionArgList &Args);
1525155ed4a23366f4514befb1c9f5f89d16f8b8b6dbAnders Carlsson  void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type,
1526155ed4a23366f4514befb1c9f5f89d16f8b8b6dbAnders Carlsson                              bool ForVirtualBase, llvm::Value *This,
1527b14095aa98c6fedd3625920c4ce834bcaf24d9f7Anders Carlsson                              CallExpr::const_arg_iterator ArgBeg,
1528b14095aa98c6fedd3625920c4ce834bcaf24d9f7Anders Carlsson                              CallExpr::const_arg_iterator ArgEnd);
152934999876e215b22febc240b1a6dc054215d12f9cFariborz Jahanian
153034999876e215b22febc240b1a6dc054215d12f9cFariborz Jahanian  void EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D,
153134999876e215b22febc240b1a6dc054215d12f9cFariborz Jahanian                              llvm::Value *This, llvm::Value *Src,
153234999876e215b22febc240b1a6dc054215d12f9cFariborz Jahanian                              CallExpr::const_arg_iterator ArgBeg,
153334999876e215b22febc240b1a6dc054215d12f9cFariborz Jahanian                              CallExpr::const_arg_iterator ArgEnd);
15341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1535288dcaf329c49b01dacd5c1dd9f35609555feecdFariborz Jahanian  void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
1536569c1f4a6c703eaa8b963bed7d5c2fd5d4569e93Anders Carlsson                                  const ConstantArrayType *ArrayTy,
15375d4d946ec2d88696fd8422aeb64dc29688e6a2c1Anders Carlsson                                  llvm::Value *ArrayPtr,
15385d4d946ec2d88696fd8422aeb64dc29688e6a2c1Anders Carlsson                                  CallExpr::const_arg_iterator ArgBeg,
153959174c0633fb5cde41735cfbff5744bdf837e8d9Douglas Gregor                                  CallExpr::const_arg_iterator ArgEnd,
154059174c0633fb5cde41735cfbff5744bdf837e8d9Douglas Gregor                                  bool ZeroInitialization = false);
15419cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1542569c1f4a6c703eaa8b963bed7d5c2fd5d4569e93Anders Carlsson  void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
1543569c1f4a6c703eaa8b963bed7d5c2fd5d4569e93Anders Carlsson                                  llvm::Value *NumElements,
15445d4d946ec2d88696fd8422aeb64dc29688e6a2c1Anders Carlsson                                  llvm::Value *ArrayPtr,
15455d4d946ec2d88696fd8422aeb64dc29688e6a2c1Anders Carlsson                                  CallExpr::const_arg_iterator ArgBeg,
154659174c0633fb5cde41735cfbff5744bdf837e8d9Douglas Gregor                                  CallExpr::const_arg_iterator ArgEnd,
154759174c0633fb5cde41735cfbff5744bdf837e8d9Douglas Gregor                                  bool ZeroInitialization = false);
1548b14095aa98c6fedd3625920c4ce834bcaf24d9f7Anders Carlsson
1549f800f6c09ed4a71bcb593d6962e0fda2c2845a70Fariborz Jahanian  void EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
1550f800f6c09ed4a71bcb593d6962e0fda2c2845a70Fariborz Jahanian                                 const ArrayType *Array,
1551f800f6c09ed4a71bcb593d6962e0fda2c2845a70Fariborz Jahanian                                 llvm::Value *This);
15521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
155372c2153b466263a21870b2b1c817900d4981493eFariborz Jahanian  void EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
155472c2153b466263a21870b2b1c817900d4981493eFariborz Jahanian                                 llvm::Value *NumElements,
155572c2153b466263a21870b2b1c817900d4981493eFariborz Jahanian                                 llvm::Value *This);
155672c2153b466263a21870b2b1c817900d4981493eFariborz Jahanian
155702e370a02f05b4a734fe5e8c88efc4ed9dac60faAnders Carlsson  llvm::Function *GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D,
155802e370a02f05b4a734fe5e8c88efc4ed9dac60faAnders Carlsson                                                  const ArrayType *Array,
155902e370a02f05b4a734fe5e8c88efc4ed9dac60faAnders Carlsson                                                  llvm::Value *This);
156088f4280f6b60edcbdea6a38a3da8b17d66473ad6Fariborz Jahanian
15617267c1693abe7875b0c57268be05005ae013c6c9Anders Carlsson  void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type,
15628e6404ca28d6bbb76e97ea2a53a74816c2a74665Anders Carlsson                             bool ForVirtualBase, llvm::Value *This);
15639cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1564ef66872797e46ced3ffdb428e582bc7f00474f99Fariborz Jahanian  void EmitNewArrayInitializer(const CXXNewExpr *E, llvm::Value *NewPtr,
1565ef66872797e46ced3ffdb428e582bc7f00474f99Fariborz Jahanian                               llvm::Value *NumElements);
15661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1567f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  void EmitCXXTemporary(const CXXTemporary *Temporary, llvm::Value *Ptr);
15681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1569a00703dccbdc6cc4a293db38477dea7db5538c7eAnders Carlsson  llvm::Value *EmitCXXNewExpr(const CXXNewExpr *E);
157060e282cc1e508be327b0481cecedc206873cb86aAnders Carlsson  void EmitCXXDeleteExpr(const CXXDeleteExpr *E);
15711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15724bf8152799ec41796f989ea1b94f6f1bac349117Eli Friedman  void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr,
15734bf8152799ec41796f989ea1b94f6f1bac349117Eli Friedman                      QualType DeleteTy);
15744bf8152799ec41796f989ea1b94f6f1bac349117Eli Friedman
1575c2e84ae9a6d25cea3e583c768e576b4c981ec028Mike Stump  llvm::Value* EmitCXXTypeidExpr(const CXXTypeidExpr *E);
1576c849c052d6b4b70f2651c1969531861a5f230053Mike Stump  llvm::Value *EmitDynamicCast(llvm::Value *V, const CXXDynamicCastExpr *DCE);
1577c2e84ae9a6d25cea3e583c768e576b4c981ec028Mike Stump
1578b14e62d5aee7522b98c410ee65bd750c2cfe6f01Mike Stump  void EmitCheck(llvm::Value *, unsigned Size);
1579b14e62d5aee7522b98c410ee65bd750c2cfe6f01Mike Stump
1580dd36d328730d8c02915d0884541b45ec6ca0049dChris Lattner  llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
1581dd36d328730d8c02915d0884541b45ec6ca0049dChris Lattner                                       bool isInc, bool isPre);
1582dd36d328730d8c02915d0884541b45ec6ca0049dChris Lattner  ComplexPairTy EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
1583dd36d328730d8c02915d0884541b45ec6ca0049dChris Lattner                                         bool isInc, bool isPre);
15845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
15855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //                            Declaration Emission
15865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
15870dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1588d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  /// EmitDecl - Emit a declaration.
1589d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  ///
1590d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  /// This function can be called with a null (unreachable) insert point.
15915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitDecl(const Decl &D);
1592d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar
1593b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall  /// EmitVarDecl - Emit a local variable declaration.
1594d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  ///
1595d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  /// This function can be called with a null (unreachable) insert point.
1596b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall  void EmitVarDecl(const VarDecl &D);
1597d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar
1598f85e193739c953358c865005855253af4f68a497John McCall  void EmitScalarInit(const Expr *init, const ValueDecl *D,
1599a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall                      LValue lvalue, bool capturedByInit);
16007acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall  void EmitScalarInit(llvm::Value *init, LValue lvalue);
1601f85e193739c953358c865005855253af4f68a497John McCall
1602f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  typedef void SpecialInitFn(CodeGenFunction &Init, const VarDecl &D,
1603f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                             llvm::Value *Address);
1604f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1605b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall  /// EmitAutoVarDecl - Emit an auto variable declaration.
1606d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  ///
1607d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  /// This function can be called with a null (unreachable) insert point.
160834695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  void EmitAutoVarDecl(const VarDecl &D);
160934695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
161034695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  class AutoVarEmission {
161134695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    friend class CodeGenFunction;
161234695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
161357b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    const VarDecl *Variable;
161434695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
161534695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// The alignment of the variable.
161634695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    CharUnits Alignment;
161734695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
161834695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// The address of the alloca.  Null if the variable was emitted
161934695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// as a global constant.
162034695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    llvm::Value *Address;
162134695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
162234695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    llvm::Value *NRVOFlag;
162334695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
162434695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// True if the variable is a __block variable.
162534695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    bool IsByRef;
162634695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
162734695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// True if the variable is of aggregate type and has a constant
162834695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// initializer.
162934695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    bool IsConstantAggregate;
163034695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
163157b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    struct Invalid {};
163257b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    AutoVarEmission(Invalid) : Variable(0) {}
163357b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall
163434695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    AutoVarEmission(const VarDecl &variable)
163557b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall      : Variable(&variable), Address(0), NRVOFlag(0),
163634695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall        IsByRef(false), IsConstantAggregate(false) {}
163734695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
163834695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    bool wasEmittedAsGlobal() const { return Address == 0; }
163934695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
164034695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  public:
164157b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    static AutoVarEmission invalid() { return AutoVarEmission(Invalid()); }
164257b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall
164334695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// Returns the address of the object within this declaration.
164434695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// Note that this does not chase the forwarding pointer for
164534695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// __block decls.
164634695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    llvm::Value *getObjectAddress(CodeGenFunction &CGF) const {
164734695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall      if (!IsByRef) return Address;
164834695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
164934695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall      return CGF.Builder.CreateStructGEP(Address,
165057b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall                                         CGF.getByRefValueLLVMField(Variable),
165157b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall                                         Variable->getNameAsString());
165234695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    }
165334695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  };
165434695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  AutoVarEmission EmitAutoVarAlloca(const VarDecl &var);
165534695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  void EmitAutoVarInit(const AutoVarEmission &emission);
165634695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  void EmitAutoVarCleanups(const AutoVarEmission &emission);
1657d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar
1658b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall  void EmitStaticVarDecl(const VarDecl &D,
1659b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall                         llvm::GlobalValue::LinkageTypes Linkage);
1660b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar
1661b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar  /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
1662093ac461b37a573dcf226fa55faed96f318169b9Devang Patel  void EmitParmDecl(const VarDecl &D, llvm::Value *Arg, unsigned ArgNo);
16630dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
166456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// protectFromPeepholes - Protect a value that we're intending to
166556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// store to the side, but which will probably be used later, from
166656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// aggressive peepholing optimizations that might delete it.
166756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  ///
166856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// Pass the result to unprotectFromPeepholes to declare that
166956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// protection is no longer required.
167056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  ///
167156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// There's no particular reason why this shouldn't apply to
167256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// l-values, it's just that no existing peepholes work on pointers.
167356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  PeepholeProtection protectFromPeepholes(RValue rvalue);
167456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  void unprotectFromPeepholes(PeepholeProtection protection);
167556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
16765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
16775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //                             Statement Emission
16785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
16795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
16800dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitStopPoint - Emit a debug stoppoint if we are emitting debug info.
16810912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  void EmitStopPoint(const Stmt *S);
16820912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar
16830dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitStmt - Emit the code for the statement \arg S. It is legal to call
16840dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// this function even if there is no current insertion point.
16850dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  ///
16860dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// This function may clear the current insertion point; callers should use
16870dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EnsureInsertPoint if they wish to subsequently generate code without first
16880dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// calling EmitBlock, EmitBranch, or EmitStmt.
16895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitStmt(const Stmt *S);
1690a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar
16910912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  /// EmitSimpleStmt - Try to emit a "simple" statement which does not
16920dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// necessarily require an insertion point or debug information; typically
16930dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// because the statement amounts to a jump or a container of other
16940dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// statements.
16950912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  ///
16960912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  /// \return True if the statement was handled.
16970912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  bool EmitSimpleStmt(const Stmt *S);
16980912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar
16999b65551d0b387a7597fb39356a4d8ef10046445eChris Lattner  RValue EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false,
1700558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall                          AggValueSlot AVS = AggValueSlot::ignored());
1701a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar
17020dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitLabel - Emit the block for the given label. It is legal to call this
17030dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// function even if there is no current insertion point.
1704ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  void EmitLabel(const LabelDecl *D); // helper for EmitLabelStmt.
1705a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar
17065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitLabelStmt(const LabelStmt &S);
17075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitGotoStmt(const GotoStmt &S);
17080ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  void EmitIndirectGotoStmt(const IndirectGotoStmt &S);
17095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitIfStmt(const IfStmt &S);
17105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitWhileStmt(const WhileStmt &S);
17115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitDoStmt(const DoStmt &S);
17125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitForStmt(const ForStmt &S);
17135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitReturnStmt(const ReturnStmt &S);
17145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitDeclStmt(const DeclStmt &S);
17150912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  void EmitBreakStmt(const BreakStmt &S);
17160912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  void EmitContinueStmt(const ContinueStmt &S);
171751b09f2c528c8460b5465c676173324e44176d62Devang Patel  void EmitSwitchStmt(const SwitchStmt &S);
171851b09f2c528c8460b5465c676173324e44176d62Devang Patel  void EmitDefaultStmt(const DefaultStmt &S);
171951b09f2c528c8460b5465c676173324e44176d62Devang Patel  void EmitCaseStmt(const CaseStmt &S);
1720c049e4f406a7f7179eba98659044a32508e53289Devang Patel  void EmitCaseStmtRange(const CaseStmt &S);
1721fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  void EmitAsmStmt(const AsmStmt &S);
17220dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
17233d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S);
172464d5d6c5903157c521af496479d06dc26032d718Anders Carlsson  void EmitObjCAtTryStmt(const ObjCAtTryStmt &S);
172564d5d6c5903157c521af496479d06dc26032d718Anders Carlsson  void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S);
172610cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattner  void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S);
1727f85e193739c953358c865005855253af4f68a497John McCall  void EmitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt &S);
17280dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
172993c332a8ba2c193c435b293966d343dab15f555bJohn McCall  llvm::Constant *getUnwindResumeFn();
173086a3a03667bdb0dcab7e6a2877dfd234b07a6d43Douglas Gregor  llvm::Constant *getUnwindResumeOrRethrowFn();
173159a7000a79118e4c140885ccbb2ac6a686a73092John McCall  void EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
173259a7000a79118e4c140885ccbb2ac6a686a73092John McCall  void ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
17339fc6a7774643a810c8501dae2323e863fefb623eJohn McCall
17346815e941998659a55c20c147861b0f437928c3d8Anders Carlsson  void EmitCXXTryStmt(const CXXTryStmt &S);
1735ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  void EmitCXXForRangeStmt(const CXXForRangeStmt &S);
17369cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
17375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
17385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //                         LValue Expression Emission
17395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
17405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
174113e81737a433b23f8c662d10d1d57356122a8caaDaniel Dunbar  /// GetUndefRValue - Get an appropriate 'undef' rvalue for the given type.
174213e81737a433b23f8c662d10d1d57356122a8caaDaniel Dunbar  RValue GetUndefRValue(QualType Ty);
174313e81737a433b23f8c662d10d1d57356122a8caaDaniel Dunbar
1744ce1d38b8163650e473d7084e0686ed5a7956057bDaniel Dunbar  /// EmitUnsupportedRValue - Emit a dummy r-value using the type of E
1745ce1d38b8163650e473d7084e0686ed5a7956057bDaniel Dunbar  /// and issue an ErrorUnsupported style diagnostic (using the
1746ce1d38b8163650e473d7084e0686ed5a7956057bDaniel Dunbar  /// provided Name).
1747ce1d38b8163650e473d7084e0686ed5a7956057bDaniel Dunbar  RValue EmitUnsupportedRValue(const Expr *E,
1748ce1d38b8163650e473d7084e0686ed5a7956057bDaniel Dunbar                               const char *Name);
1749ce1d38b8163650e473d7084e0686ed5a7956057bDaniel Dunbar
17500dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitUnsupportedLValue - Emit a dummy l-value using the type of E and issue
17510dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// an ErrorUnsupported style diagnostic (using the provided Name).
17526ba82a4a4b295e54cf192c3e98b28f39990c6194Daniel Dunbar  LValue EmitUnsupportedLValue(const Expr *E,
17536ba82a4a4b295e54cf192c3e98b28f39990c6194Daniel Dunbar                               const char *Name);
17546ba82a4a4b295e54cf192c3e98b28f39990c6194Daniel Dunbar
17555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EmitLValue - Emit code to compute a designator that specifies the location
17565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// of the expression.
17575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
17585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// This can return one of two things: a simple address or a bitfield
17595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// reference.  In either case, the LLVM Value* in the LValue structure is
17605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// guaranteed to be an LLVM pointer type.
17615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
17625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// If this returns a bitfield reference, nothing about the pointee type of
17635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the LLVM value is known: For example, it may not be a pointer to an
17645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// integer.
17655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
17665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// If this returns a normal address, and if the lvalue's C type is fixed
17675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// size, this method guarantees that the returned pointer type will point to
17685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// an LLVM type of the same size of the lvalue's type.  If the lvalue has a
17695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// variable length type, this is not possible.
17705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
17715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LValue EmitLValue(const Expr *E);
17720dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1773b14e62d5aee7522b98c410ee65bd750c2cfe6f01Mike Stump  /// EmitCheckedLValue - Same as EmitLValue but additionally we generate
1774b14e62d5aee7522b98c410ee65bd750c2cfe6f01Mike Stump  /// checking code to guard against undefined behavior.  This is only
1775b14e62d5aee7522b98c410ee65bd750c2cfe6f01Mike Stump  /// suitable when we know that the address will be used to access the
1776b14e62d5aee7522b98c410ee65bd750c2cfe6f01Mike Stump  /// object.
1777b14e62d5aee7522b98c410ee65bd750c2cfe6f01Mike Stump  LValue EmitCheckedLValue(const Expr *E);
1778b14e62d5aee7522b98c410ee65bd750c2cfe6f01Mike Stump
177926815d97c5743481e317f17a8d53a6819d061862John McCall  /// EmitToMemory - Change a scalar value from its value
178026815d97c5743481e317f17a8d53a6819d061862John McCall  /// representation to its in-memory representation.
178126815d97c5743481e317f17a8d53a6819d061862John McCall  llvm::Value *EmitToMemory(llvm::Value *Value, QualType Ty);
178226815d97c5743481e317f17a8d53a6819d061862John McCall
178326815d97c5743481e317f17a8d53a6819d061862John McCall  /// EmitFromMemory - Change a scalar value from its memory
178426815d97c5743481e317f17a8d53a6819d061862John McCall  /// representation to its value representation.
178526815d97c5743481e317f17a8d53a6819d061862John McCall  llvm::Value *EmitFromMemory(llvm::Value *Value, QualType Ty);
178626815d97c5743481e317f17a8d53a6819d061862John McCall
17879d9cc874bcc168474757a8f90476684b4a93ed76Daniel Dunbar  /// EmitLoadOfScalar - Load a scalar value from an address, taking
17889d9cc874bcc168474757a8f90476684b4a93ed76Daniel Dunbar  /// care to appropriately convert from the memory representation to
17899d9cc874bcc168474757a8f90476684b4a93ed76Daniel Dunbar  /// the LLVM value representation.
179009429b962811f2c0a7a81a1b0a89f96f5878b20cMike Stump  llvm::Value *EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
17913d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman                                unsigned Alignment, QualType Ty,
17923d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman                                llvm::MDNode *TBAAInfo = 0);
1793a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall  llvm::Value *EmitLoadOfScalar(LValue lvalue);
17949d9cc874bcc168474757a8f90476684b4a93ed76Daniel Dunbar
17959d9cc874bcc168474757a8f90476684b4a93ed76Daniel Dunbar  /// EmitStoreOfScalar - Store a scalar value to an address, taking
17969d9cc874bcc168474757a8f90476684b4a93ed76Daniel Dunbar  /// care to appropriately convert from the memory representation to
17979d9cc874bcc168474757a8f90476684b4a93ed76Daniel Dunbar  /// the LLVM value representation.
179809429b962811f2c0a7a81a1b0a89f96f5878b20cMike Stump  void EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
17993d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman                         bool Volatile, unsigned Alignment, QualType Ty,
18003d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman                         llvm::MDNode *TBAAInfo = 0);
1801a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall  void EmitStoreOfScalar(llvm::Value *value, LValue lvalue);
18029d9cc874bcc168474757a8f90476684b4a93ed76Daniel Dunbar
18035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
18045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// this method emits the address of the lvalue, then loads the result as an
18055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// rvalue, returning the rvalue.
18065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  RValue EmitLoadOfLValue(LValue V, QualType LVType);
1807213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  RValue EmitLoadOfExtVectorElementLValue(LValue V, QualType LVType);
18083b8c22d93da1432b2d4cea779d14912645c93866Lauro Ramos Venancio  RValue EmitLoadOfBitfieldLValue(LValue LV, QualType ExprType);
1809119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall  RValue EmitLoadOfPropertyRefLValue(LValue LV,
1810119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall                                 ReturnValueSlot Return = ReturnValueSlot());
18110dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
18125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EmitStoreThroughLValue - Store the specified rvalue into the specified
18135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// lvalue, where both are guaranteed to the have the same type, and that type
18145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// is 'Ty'.
18155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitStoreThroughLValue(RValue Src, LValue Dst, QualType Ty);
1816213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst,
1817213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman                                                QualType Ty);
1818119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall  void EmitStoreThroughPropertyRefLValue(RValue Src, LValue Dst);
1819ed3849b456d64d124bddc7ac044f3ce590bd9d69Daniel Dunbar
18200dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitStoreThroughLValue - Store Src into Dst with same constraints as
18210dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitStoreThroughLValue.
1822ed3849b456d64d124bddc7ac044f3ce590bd9d69Daniel Dunbar  ///
18230dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// \param Result [out] - If non-null, this will be set to a Value* for the
18240dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// bit-field contents after the store, appropriate for use as the result of
18250dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// an assignment to the bit-field.
1826ed3849b456d64d124bddc7ac044f3ce590bd9d69Daniel Dunbar  void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst, QualType Ty,
1827ed3849b456d64d124bddc7ac044f3ce590bd9d69Daniel Dunbar                                      llvm::Value **Result=0);
18280dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
182983ce9d4a552987d34cbd500e983db8d770232379John McCall  /// Emit an l-value for an assignment (simple or compound) of complex type.
183083ce9d4a552987d34cbd500e983db8d770232379John McCall  LValue EmitComplexAssignmentLValue(const BinaryOperator *E);
18312a41637a995affa1563f4d82a8b026e326a2faa0John McCall  LValue EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E);
183283ce9d4a552987d34cbd500e983db8d770232379John McCall
1833fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner  // Note: only available for agg return types
183480e62c29fe21cd06c93eba6e72b7e32dca06fcf9Daniel Dunbar  LValue EmitBinaryOperatorLValue(const BinaryOperator *E);
18352a41637a995affa1563f4d82a8b026e326a2faa0John McCall  LValue EmitCompoundAssignmentLValue(const CompoundAssignOperator *E);
18365b5c9ef865607e179413462dcd71bcebb5b7daaeDaniel Dunbar  // Note: only available for agg return types
183722c940ee588a420cd2e6c8f68be2ac3cc80061ffChristopher Lamb  LValue EmitCallExprLValue(const CallExpr *E);
18385b5c9ef865607e179413462dcd71bcebb5b7daaeDaniel Dunbar  // Note: only available for agg return types
18395b5c9ef865607e179413462dcd71bcebb5b7daaeDaniel Dunbar  LValue EmitVAArgExprLValue(const VAArgExpr *E);
18405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LValue EmitDeclRefLValue(const DeclRefExpr *E);
18415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LValue EmitStringLiteralLValue(const StringLiteral *E);
1842eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  LValue EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E);
1843d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris Lattner  LValue EmitPredefinedLValue(const PredefinedExpr *E);
18445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LValue EmitUnaryOpLValue(const UnaryOperator *E);
18455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
1846213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E);
1847b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel  LValue EmitMemberExpr(const MemberExpr *E);
1848820bca41c3899374775d2a1dfc2ef2e22aaf1c7bFariborz Jahanian  LValue EmitObjCIsaExpr(const ObjCIsaExpr *E);
184906e863f2902b8ba55b056308875c19f7ba3dab25Eli Friedman  LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E);
185056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  LValue EmitConditionalOperatorLValue(const AbstractConditionalOperator *E);
185175dfedaf702822c9e51a4f3c5d3ecf2d2ad99272Chris Lattner  LValue EmitCastLValue(const CastExpr *E);
1852ed8abf18329df67b0abcbb3a10458bd8c1d2a595Douglas Gregor  LValue EmitNullInitializationLValue(const CXXScalarValueInitExpr *E);
1853e996ffd240f20a1048179d7727a6ee3227261921John McCall  LValue EmitOpaqueValueLValue(const OpaqueValueExpr *e);
18549cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
18552a03192a02dbf4fdff438d1e658356bde871aba4Daniel Dunbar  llvm::Value *EmitIvarOffset(const ObjCInterfaceDecl *Interface,
185629e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar                              const ObjCIvarDecl *Ivar);
1857a9976d3b192690db20f59dc44099ac4ca939bdb7John McCall  LValue EmitLValueForAnonRecordField(llvm::Value* Base,
185800eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                      const IndirectFieldDecl* Field,
1859a9976d3b192690db20f59dc44099ac4ca939bdb7John McCall                                      unsigned CVRQualifiers);
18600ed303c918915cbe2c611cddbfabd00404664831Anders Carlsson  LValue EmitLValueForField(llvm::Value* Base, const FieldDecl* Field,
1861e6d2a534851a649485cb087e9dfcaf8a65886858Anders Carlsson                            unsigned CVRQualifiers);
18629cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
186306a2970e9480c6d02b367b2f970baff29b9f9721Anders Carlsson  /// EmitLValueForFieldInitialization - Like EmitLValueForField, except that
186406a2970e9480c6d02b367b2f970baff29b9f9721Anders Carlsson  /// if the Field is a reference, this will return the address of the reference
186506a2970e9480c6d02b367b2f970baff29b9f9721Anders Carlsson  /// and not the address of the value stored in the reference.
18669cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  LValue EmitLValueForFieldInitialization(llvm::Value* Base,
186706a2970e9480c6d02b367b2f970baff29b9f9721Anders Carlsson                                          const FieldDecl* Field,
186806a2970e9480c6d02b367b2f970baff29b9f9721Anders Carlsson                                          unsigned CVRQualifiers);
18699cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
187045012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian  LValue EmitLValueForIvar(QualType ObjectTy,
187145012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian                           llvm::Value* Base, const ObjCIvarDecl *Ivar,
187229e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar                           unsigned CVRQualifiers);
187329e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar
18740ed303c918915cbe2c611cddbfabd00404664831Anders Carlsson  LValue EmitLValueForBitfield(llvm::Value* Base, const FieldDecl* Field,
1875598d3f61b6ca854e9d3c2f3359e24468502a61aaFariborz Jahanian                                unsigned CVRQualifiers);
1876fd64bb635dc221baa19f81d5d2a084f7eb269f7fFariborz Jahanian
1877a99038c0757a836c6faeeddaa5dfd249b32f6e9eMike Stump  LValue EmitBlockDeclRefLValue(const BlockDeclRefExpr *E);
1878a99038c0757a836c6faeeddaa5dfd249b32f6e9eMike Stump
1879b58d017f2b9eeed33f2ab3ede968b89cf5296bf2Anders Carlsson  LValue EmitCXXConstructLValue(const CXXConstructExpr *E);
1880e61c9e80a108b5db7e5f0e702f0e455d737c6390Anders Carlsson  LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E);
18814765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  LValue EmitExprWithCleanupsLValue(const ExprWithCleanups *E);
1882c2e84ae9a6d25cea3e583c768e576b4c981ec028Mike Stump  LValue EmitCXXTypeidLValue(const CXXTypeidExpr *E);
18839cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
18840a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar  LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E);
1885391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E);
188685c59edda02df48fae8dc85049743319bc6e7e89Daniel Dunbar  LValue EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E);
1887654599454c8e6cc83b1b9b3af43c49c2f66a26cbChris Lattner  LValue EmitStmtExprLValue(const StmtExpr *E);
18888bfd31f9dad09cd52225d868bbd92a9bebe87775Fariborz Jahanian  LValue EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E);
188903b2960c14aede6ac82bdef32247094ebb72fa69Fariborz Jahanian  LValue EmitObjCSelectorLValue(const ObjCSelectorExpr *E);
1890189d6ef40eff11b83b2cda941d5ed89a5cef09b2John McCall  void   EmitDeclRefExprDbgValue(const DeclRefExpr *E, llvm::Constant *Init);
189156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
18925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
1893883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner  //                         Scalar Expression Emission
18945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
18955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
18960dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitCall - Generate a call of the given function, expecting the given
18970dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// result type, and using the given argument list which specifies both the
18980dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// LLVM arguments and the types they were derived from.
1899c0ef9f59937c3971c48b6fed37cf5bd8985c024bDaniel Dunbar  ///
1900f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// \param TargetDecl - If given, the decl of the function in a direct call;
1901f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// used to set attributes on the call (noreturn, etc.).
190288b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  RValue EmitCall(const CGFunctionInfo &FnInfo,
190388b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar                  llvm::Value *Callee,
1904f3c47c9525153aea2de0ec4bd615b9cf2d81c103Anders Carlsson                  ReturnValueSlot ReturnValue,
1905c0ef9f59937c3971c48b6fed37cf5bd8985c024bDaniel Dunbar                  const CallArgList &Args,
1906dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall                  const Decl *TargetDecl = 0,
19074b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall                  llvm::Instruction **callOrInvoke = 0);
19081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
190931777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson  RValue EmitCall(QualType FnType, llvm::Value *Callee,
1910d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson                  ReturnValueSlot ReturnValue,
1911986477149db76e6d9f8d3c9859c9880c11b680aeAnders Carlsson                  CallExpr::const_arg_iterator ArgBeg,
1912986477149db76e6d9f8d3c9859c9880c11b680aeAnders Carlsson                  CallExpr::const_arg_iterator ArgEnd,
1913986477149db76e6d9f8d3c9859c9880c11b680aeAnders Carlsson                  const Decl *TargetDecl = 0);
19149cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  RValue EmitCallExpr(const CallExpr *E,
1915d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson                      ReturnValueSlot ReturnValue = ReturnValueSlot());
19161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1917f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::CallSite EmitCallOrInvoke(llvm::Value *Callee,
1918f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                                  llvm::Value * const *ArgBegin,
1919f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                                  llvm::Value * const *ArgEnd,
1920f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                                  const llvm::Twine &Name = "");
1921f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1922566abee1e9828a7700b51e4d17ea08234fde3bb4Anders Carlsson  llvm::Value *BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This,
1923f0070dbae9535836ad41711081465dec2259786bMike Stump                                const llvm::Type *Ty);
19249cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  llvm::Value *BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type,
192583eedd9fa800c273647c7a90465ea9c110f9d93cAnders Carlsson                                llvm::Value *This, const llvm::Type *Ty);
19262726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian  llvm::Value *BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
19272726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian                                         NestedNameSpecifier *Qual,
19282726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian                                         const llvm::Type *Ty);
1929ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian
1930ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian  llvm::Value *BuildAppleKextVirtualDestructorCall(const CXXDestructorDecl *DD,
1931ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian                                                   CXXDtorType Type,
1932771c678c04f5f685b4f188ec6c2fd88ad0f7457fFariborz Jahanian                                                   const CXXRecordDecl *RD);
1933566abee1e9828a7700b51e4d17ea08234fde3bb4Anders Carlsson
1934b9de2c55b2a33776e2bee8ee57df7599b374c8a5Anders Carlsson  RValue EmitCXXMemberCall(const CXXMethodDecl *MD,
1935b9de2c55b2a33776e2bee8ee57df7599b374c8a5Anders Carlsson                           llvm::Value *Callee,
1936a1736c0c750d4514a5d8fda36670addf1e4de54aAnders Carlsson                           ReturnValueSlot ReturnValue,
1937b9de2c55b2a33776e2bee8ee57df7599b374c8a5Anders Carlsson                           llvm::Value *This,
1938c997d4278d329e18891aac9698fb991b2d4622ebAnders Carlsson                           llvm::Value *VTT,
1939b9de2c55b2a33776e2bee8ee57df7599b374c8a5Anders Carlsson                           CallExpr::const_arg_iterator ArgBeg,
1940b9de2c55b2a33776e2bee8ee57df7599b374c8a5Anders Carlsson                           CallExpr::const_arg_iterator ArgEnd);
1941a1736c0c750d4514a5d8fda36670addf1e4de54aAnders Carlsson  RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E,
1942a1736c0c750d4514a5d8fda36670addf1e4de54aAnders Carlsson                               ReturnValueSlot ReturnValue);
1943a1736c0c750d4514a5d8fda36670addf1e4de54aAnders Carlsson  RValue EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
1944a1736c0c750d4514a5d8fda36670addf1e4de54aAnders Carlsson                                      ReturnValueSlot ReturnValue);
19455549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek
1946a2447e0d1e28669cd637204a871f15b1215277fdAnders Carlsson  llvm::Value *EmitCXXOperatorMemberCallee(const CXXOperatorCallExpr *E,
1947a2447e0d1e28669cd637204a871f15b1215277fdAnders Carlsson                                           const CXXMethodDecl *MD,
1948a2447e0d1e28669cd637204a871f15b1215277fdAnders Carlsson                                           llvm::Value *This);
19490f294632f36459174199b77699e339715244b5abAnders Carlsson  RValue EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
1950a1736c0c750d4514a5d8fda36670addf1e4de54aAnders Carlsson                                       const CXXMethodDecl *MD,
1951a1736c0c750d4514a5d8fda36670addf1e4de54aAnders Carlsson                                       ReturnValueSlot ReturnValue);
19521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19539cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
19541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  RValue EmitBuiltinExpr(const FunctionDecl *FD,
1955ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar                         unsigned BuiltinID, const CallExpr *E);
19565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1957a1736c0c750d4514a5d8fda36670addf1e4de54aAnders Carlsson  RValue EmitBlockCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue);
195809429b962811f2c0a7a81a1b0a89f96f5878b20cMike Stump
19590dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitTargetBuiltinExpr - Emit the given builtin call. Returns 0 if the call
19600dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// is unhandled by the current target.
1961f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar  llvm::Value *EmitTargetBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
1962f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar
19632752c0137d95aa2f4ee1cdff4b564bac842e041bChris Lattner  llvm::Value *EmitARMBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
19649cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  llvm::Value *EmitNeonCall(llvm::Function *F,
196530d91718a676177f0d0d0210ce4fdb4f616df6e5Nate Begeman                            llvm::SmallVectorImpl<llvm::Value*> &O,
1966db3d4d036037f379f12643e067b229862d61e932Bob Wilson                            const char *name,
196761eecf5aea018ea65c9ab0bccacd2996b15c632dNate Begeman                            unsigned shift = 0, bool rightshift = false);
1968cf55652cf668c1402eee0b12edd2e5a1bc34d7a1Bob Wilson  llvm::Value *EmitNeonSplat(llvm::Value *V, llvm::Constant *Idx);
1969464ccb68f22a7e1c0a2844551c16f721540c91c3Nate Begeman  llvm::Value *EmitNeonShiftVector(llvm::Value *V, const llvm::Type *Ty,
197061eecf5aea018ea65c9ab0bccacd2996b15c632dNate Begeman                                   bool negateForRightShift);
19719cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1972aa51e513850688b7963efc62abf1eface7037602Bill Wendling  llvm::Value *BuildVector(const llvm::SmallVectorImpl<llvm::Value*> &Ops);
1973564f1de67d7ba43646b8740db86d6269e3dfbe0bAnders Carlsson  llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
1974564f1de67d7ba43646b8740db86d6269e3dfbe0bAnders Carlsson  llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
19750dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1976ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar  llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E);
19777f02f721d48772e4eee4fccd8af4f800be1bbc1eChris Lattner  llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E);
19788fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E);
1979ef072fd2f3347cfd857d6eb787b245b950771430John McCall  RValue EmitObjCMessageExpr(const ObjCMessageExpr *E,
1980ef072fd2f3347cfd857d6eb787b245b950771430John McCall                             ReturnValueSlot Return = ReturnValueSlot());
19818fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner
1982f85e193739c953358c865005855253af4f68a497John McCall  /// Retrieves the default cleanup kind for an ARC cleanup.
1983f85e193739c953358c865005855253af4f68a497John McCall  /// Except under -fobjc-arc-eh, ARC cleanups are normal-only.
1984f85e193739c953358c865005855253af4f68a497John McCall  CleanupKind getARCCleanupKind() {
1985f85e193739c953358c865005855253af4f68a497John McCall    return CGM.getCodeGenOpts().ObjCAutoRefCountExceptions
1986f85e193739c953358c865005855253af4f68a497John McCall             ? NormalAndEHCleanup : NormalCleanup;
1987f85e193739c953358c865005855253af4f68a497John McCall  }
1988f85e193739c953358c865005855253af4f68a497John McCall
1989f85e193739c953358c865005855253af4f68a497John McCall  // ARC primitives.
1990f85e193739c953358c865005855253af4f68a497John McCall  void EmitARCInitWeak(llvm::Value *value, llvm::Value *addr);
1991f85e193739c953358c865005855253af4f68a497John McCall  void EmitARCDestroyWeak(llvm::Value *addr);
1992f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCLoadWeak(llvm::Value *addr);
1993f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCLoadWeakRetained(llvm::Value *addr);
1994f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCStoreWeak(llvm::Value *value, llvm::Value *addr,
1995f85e193739c953358c865005855253af4f68a497John McCall                                bool ignored);
1996f85e193739c953358c865005855253af4f68a497John McCall  void EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src);
1997f85e193739c953358c865005855253af4f68a497John McCall  void EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src);
1998f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetainAutorelease(QualType type, llvm::Value *value);
1999f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetainAutoreleaseNonBlock(llvm::Value *value);
2000f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCStoreStrong(LValue addr, QualType type,
2001f85e193739c953358c865005855253af4f68a497John McCall                                  llvm::Value *value, bool ignored);
2002f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCStoreStrongCall(llvm::Value *addr, llvm::Value *value,
2003f85e193739c953358c865005855253af4f68a497John McCall                                      bool ignored);
2004f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetain(QualType type, llvm::Value *value);
2005f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetainNonBlock(llvm::Value *value);
2006f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetainBlock(llvm::Value *value);
2007f85e193739c953358c865005855253af4f68a497John McCall  void EmitARCRelease(llvm::Value *value, bool precise);
2008f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCAutorelease(llvm::Value *value);
2009f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCAutoreleaseReturnValue(llvm::Value *value);
2010f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetainAutoreleaseReturnValue(llvm::Value *value);
2011f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetainAutoreleasedReturnValue(llvm::Value *value);
2012f85e193739c953358c865005855253af4f68a497John McCall
2013f85e193739c953358c865005855253af4f68a497John McCall  std::pair<LValue,llvm::Value*>
2014f85e193739c953358c865005855253af4f68a497John McCall  EmitARCStoreAutoreleasing(const BinaryOperator *e);
2015f85e193739c953358c865005855253af4f68a497John McCall  std::pair<LValue,llvm::Value*>
2016f85e193739c953358c865005855253af4f68a497John McCall  EmitARCStoreStrong(const BinaryOperator *e, bool ignored);
2017f85e193739c953358c865005855253af4f68a497John McCall
2018f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitObjCProduceObject(QualType T, llvm::Value *Ptr);
2019f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitObjCConsumeObject(QualType T, llvm::Value *Ptr);
2020f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitObjCExtendObjectLifetime(QualType T, llvm::Value *Ptr);
2021f85e193739c953358c865005855253af4f68a497John McCall
2022f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetainScalarExpr(const Expr *expr);
2023f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetainAutoreleaseScalarExpr(const Expr *expr);
2024f85e193739c953358c865005855253af4f68a497John McCall
2025f85e193739c953358c865005855253af4f68a497John McCall  void PushARCReleaseCleanup(CleanupKind kind, QualType type,
2026f85e193739c953358c865005855253af4f68a497John McCall                             llvm::Value *addr, bool precise);
2027f85e193739c953358c865005855253af4f68a497John McCall  void PushARCWeakReleaseCleanup(CleanupKind kind, QualType type,
2028f85e193739c953358c865005855253af4f68a497John McCall                                 llvm::Value *addr);
2029f85e193739c953358c865005855253af4f68a497John McCall  void PushARCFieldReleaseCleanup(CleanupKind cleanupKind,
2030f85e193739c953358c865005855253af4f68a497John McCall                                  const FieldDecl *Field);
2031f85e193739c953358c865005855253af4f68a497John McCall  void PushARCFieldWeakReleaseCleanup(CleanupKind cleanupKind,
2032f85e193739c953358c865005855253af4f68a497John McCall                                      const FieldDecl *Field);
2033f85e193739c953358c865005855253af4f68a497John McCall
2034f85e193739c953358c865005855253af4f68a497John McCall  void EmitObjCAutoreleasePoolPop(llvm::Value *Ptr);
2035f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitObjCAutoreleasePoolPush();
2036f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitObjCMRRAutoreleasePoolPush();
2037f85e193739c953358c865005855253af4f68a497John McCall  void EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr);
2038f85e193739c953358c865005855253af4f68a497John McCall  void EmitObjCMRRAutoreleasePoolPop(llvm::Value *Ptr);
2039f85e193739c953358c865005855253af4f68a497John McCall
20404029ca7ae9d3ffd7738fe2ed8782bebc30f36fd6Anders Carlsson  /// EmitReferenceBindingToExpr - Emits a reference binding to the passed in
20414029ca7ae9d3ffd7738fe2ed8782bebc30f36fd6Anders Carlsson  /// expression. Will emit a temporary variable if E is not an LValue.
20429cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  RValue EmitReferenceBindingToExpr(const Expr* E,
204332f36baa6c8d491c374af622b4e3ac28d597453cAnders Carlsson                                    const NamedDecl *InitializedDecl);
20443aba09376c5f49c4c8d176109ea4835bc2c528eeAnders Carlsson
2045883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner  //===--------------------------------------------------------------------===//
2046bfc0c1ae4b505b52b36f572a9641f99e76879cabChris Lattner  //                           Expression Emission
2047883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner  //===--------------------------------------------------------------------===//
2048bfc0c1ae4b505b52b36f572a9641f99e76879cabChris Lattner
2049bfc0c1ae4b505b52b36f572a9641f99e76879cabChris Lattner  // Expressions are broken into three classes: scalar, complex, aggregate.
20500dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
20510dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitScalarExpr - Emit the computation of the specified expression of LLVM
20520dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// scalar type, returning the result.
205314c5cbf59cffee52275230922283a247de407712Anders Carlsson  llvm::Value *EmitScalarExpr(const Expr *E , bool IgnoreResultAssign = false);
20540dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
20553707b255f8993fb362904c9cff87b0e9bc6ca317Chris Lattner  /// EmitScalarConversion - Emit a conversion from the specified type to the
20563707b255f8993fb362904c9cff87b0e9bc6ca317Chris Lattner  /// specified destination type, both of which are LLVM scalar types.
20573707b255f8993fb362904c9cff87b0e9bc6ca317Chris Lattner  llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy,
20583707b255f8993fb362904c9cff87b0e9bc6ca317Chris Lattner                                    QualType DstTy);
20590dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
20604f1a7b380809b4ca19ad2daff61bc11cd1e979d8Chris Lattner  /// EmitComplexToScalarConversion - Emit a conversion from the specified
20610dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// complex type to the specified destination type, where the destination type
20620dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// is an LLVM scalar type.
20634f1a7b380809b4ca19ad2daff61bc11cd1e979d8Chris Lattner  llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy,
20644f1a7b380809b4ca19ad2daff61bc11cd1e979d8Chris Lattner                                             QualType DstTy);
20650dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
20660dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
2067558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  /// EmitAggExpr - Emit the computation of the specified expression
2068558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  /// of aggregate type.  The result is computed into the given slot,
2069558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  /// which may be null to indicate that the value is not needed.
2070474e2fe4957e6e72cee36ed189eaf21878ad0e91Fariborz Jahanian  void EmitAggExpr(const Expr *E, AggValueSlot AS, bool IgnoreResult = false);
20710dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
207218aba0dd518e486d8b50523e7dafb4b5657135d2Daniel Dunbar  /// EmitAggExprToLValue - Emit the computation of the specified expression of
207318aba0dd518e486d8b50523e7dafb4b5657135d2Daniel Dunbar  /// aggregate type into a temporary LValue.
207418aba0dd518e486d8b50523e7dafb4b5657135d2Daniel Dunbar  LValue EmitAggExprToLValue(const Expr *E);
207518aba0dd518e486d8b50523e7dafb4b5657135d2Daniel Dunbar
2076082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  /// EmitGCMemmoveCollectable - Emit special API for structs with object
2077082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  /// pointers.
2078082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  void EmitGCMemmoveCollectable(llvm::Value *DestPtr, llvm::Value *SrcPtr,
207908c321380fff07d476a19daab6d29522c046cd49Fariborz Jahanian                                QualType Ty);
2080082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian
2081b6ef18a2b06d6760459e1756a61f79ff496cee19Chris Lattner  /// EmitComplexExpr - Emit the computation of the specified expression of
208223b1cdb0b5e089468bb8475b8ec9287af67b4b59Chris Lattner  /// complex type, returning the result.
2083b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall  ComplexPairTy EmitComplexExpr(const Expr *E,
2084b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall                                bool IgnoreReal = false,
2085b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall                                bool IgnoreImag = false);
20860dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
208723b1cdb0b5e089468bb8475b8ec9287af67b4b59Chris Lattner  /// EmitComplexExprIntoAddr - Emit the computation of the specified expression
208823b1cdb0b5e089468bb8475b8ec9287af67b4b59Chris Lattner  /// of complex type, storing into the specified Value*.
2089190dbe203c2e892ac40f1786035c6c9be3f44a4cChris Lattner  void EmitComplexExprIntoAddr(const Expr *E, llvm::Value *DestAddr,
2090190dbe203c2e892ac40f1786035c6c9be3f44a4cChris Lattner                               bool DestIsVolatile);
20917f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar
20927f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar  /// StoreComplexToAddr - Store a complex number into the specified address.
20937f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar  void StoreComplexToAddr(ComplexPairTy V, llvm::Value *DestAddr,
20947f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar                          bool DestIsVolatile);
20959b65551d0b387a7597fb39356a4d8ef10046445eChris Lattner  /// LoadComplexFromAddr - Load a complex number from the specified address.
20969b65551d0b387a7597fb39356a4d8ef10046445eChris Lattner  ComplexPairTy LoadComplexFromAddr(llvm::Value *SrcAddr, bool SrcIsVolatile);
20972621fd1d6d3c5eadcae246859f62738645df7540Chris Lattner
2098b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall  /// CreateStaticVarDecl - Create a zero-initialized LLVM global for
2099b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall  /// a static local variable.
2100b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall  llvm::GlobalVariable *CreateStaticVarDecl(const VarDecl &D,
2101b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall                                            const char *Separator,
2102f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump                                       llvm::GlobalValue::LinkageTypes Linkage);
21039cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
2104b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall  /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
2105761acc19d966a67f2175783d09412d6ac9463400Chris Lattner  /// global variable that has already been created for it.  If the initializer
2106761acc19d966a67f2175783d09412d6ac9463400Chris Lattner  /// has a different type than GV does, this may free GV and return a different
2107761acc19d966a67f2175783d09412d6ac9463400Chris Lattner  /// one.  Otherwise it just returns GV.
2108761acc19d966a67f2175783d09412d6ac9463400Chris Lattner  llvm::GlobalVariable *
2109b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall  AddInitializerToStaticVarDecl(const VarDecl &D,
2110b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall                                llvm::GlobalVariable *GV);
21119cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
21120096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar
21133b2e16b3d25f6b311dba2871e2a566c96238c3d2Anders Carlsson  /// EmitCXXGlobalVarDeclInit - Create the initializer for a C++
21143b2e16b3d25f6b311dba2871e2a566c96238c3d2Anders Carlsson  /// variable with global storage.
21153b2e16b3d25f6b311dba2871e2a566c96238c3d2Anders Carlsson  void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::Constant *DeclPtr);
21163b2e16b3d25f6b311dba2871e2a566c96238c3d2Anders Carlsson
21173b2e16b3d25f6b311dba2871e2a566c96238c3d2Anders Carlsson  /// EmitCXXGlobalDtorRegistration - Emits a call to register the global ptr
21183b2e16b3d25f6b311dba2871e2a566c96238c3d2Anders Carlsson  /// with the C++ runtime so that its destructor will be called at exit.
211988f4280f6b60edcbdea6a38a3da8b17d66473ad6Fariborz Jahanian  void EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn,
21203b2e16b3d25f6b311dba2871e2a566c96238c3d2Anders Carlsson                                     llvm::Constant *DeclPtr);
21211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21223030eb82593097502469a8b3fc26112c79c75605John McCall  /// Emit code in this function to perform a guarded variable
21233030eb82593097502469a8b3fc26112c79c75605John McCall  /// initialization.  Guarded initializations are used when it's not
21243030eb82593097502469a8b3fc26112c79c75605John McCall  /// possible to prove that an initialization will be done exactly
21253030eb82593097502469a8b3fc26112c79c75605John McCall  /// once, e.g. with a static local variable or a static data member
21263030eb82593097502469a8b3fc26112c79c75605John McCall  /// of a class template.
21273030eb82593097502469a8b3fc26112c79c75605John McCall  void EmitCXXGuardedInit(const VarDecl &D, llvm::GlobalVariable *DeclPtr);
21285cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
2129efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar  /// GenerateCXXGlobalInitFunc - Generates code for initializing global
2130efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar  /// variables.
2131efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar  void GenerateCXXGlobalInitFunc(llvm::Function *Fn,
2132efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar                                 llvm::Constant **Decls,
2133efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar                                 unsigned NumDecls);
2134efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar
2135efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar  /// GenerateCXXGlobalDtorFunc - Generates code for destroying global
2136efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar  /// variables.
2137efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar  void GenerateCXXGlobalDtorFunc(llvm::Function *Fn,
2138810112e28dc839715d17b0a786f23aaa19600ac0Chris Lattner                                 const std::vector<std::pair<llvm::WeakVH,
2139efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar                                   llvm::Constant*> > &DtorsAndObjects);
2140efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar
2141d26bc76c98006609002d9930f8840490e88ac5b5John McCall  void GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
2142d26bc76c98006609002d9930f8840490e88ac5b5John McCall                                        const VarDecl *D,
21433030eb82593097502469a8b3fc26112c79c75605John McCall                                        llvm::GlobalVariable *Addr);
2144efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar
2145558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  void EmitCXXConstructExpr(const CXXConstructExpr *E, AggValueSlot Dest);
214634999876e215b22febc240b1a6dc054215d12f9cFariborz Jahanian
214734999876e215b22febc240b1a6dc054215d12f9cFariborz Jahanian  void EmitSynthesizedCXXCopyCtor(llvm::Value *Dest, llvm::Value *Src,
2148830937bc1100fba7682f7c32c40512085870f50cFariborz Jahanian                                  const Expr *Exp);
21491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21504765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  RValue EmitExprWithCleanups(const ExprWithCleanups *E,
21514765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall                              AggValueSlot Slot =AggValueSlot::ignored());
21521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2153756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson  void EmitCXXThrowExpr(const CXXThrowExpr *E);
21541eb2e59338c4b9c0429fc39ca98662adc9e7a3f2Douglas Gregor
21550ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  //===--------------------------------------------------------------------===//
21560ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  //                             Internal Helpers
21570ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  //===--------------------------------------------------------------------===//
21580dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
21590946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  /// ContainsLabel - Return true if the statement contains a label in it.  If
21600946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  /// this statement is not executed normally, it not containing a label means
21610946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  /// that we can just remove the code.
21620946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts = false);
21630dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
2164ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  /// containsBreak - Return true if the statement contains a break out of it.
2165ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  /// If the statement (recursively) contains a switch or loop with a break
2166ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  /// inside of it, this is fine.
2167ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  static bool containsBreak(const Stmt *S);
2168ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
21694bc0455d826e9634b7a68c0d06146edd633c9453Daniel Dunbar  /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
2170c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner  /// to a constant, or if it does but contains a label, return false.  If it
2171c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner  /// constant folds return true and set the boolean result in Result.
2172c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner  bool ConstantFoldsToSimpleInteger(const Expr *Cond, bool &Result);
21730dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
2174ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
2175ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  /// to a constant, or if it does but contains a label, return false.  If it
2176ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  /// constant folds return true and set the folded value.
2177ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  bool ConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APInt &Result);
2178ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
217931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an
218031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  /// if statement) to the specified blocks.  Based on the condition, this might
218131a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  /// try to simplify the codegen of the conditional based on the branch.
21829bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner  void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock,
21834bc0455d826e9634b7a68c0d06146edd633c9453Daniel Dunbar                            llvm::BasicBlock *FalseBlock);
2184be07f60131bc6f8d6696f4644df1ef667a1730d5Mike Stump
218515037caa1542bb810ad54c653aeb80f61df7b00cMike Stump  /// getTrapBB - Create a basic block that will call the trap intrinsic.  We'll
218615037caa1542bb810ad54c653aeb80f61df7b00cMike Stump  /// generate a branch around the created basic block as necessary.
21876c552c1d5f47fbba00e6268d96a26ad026f2da2aChris Lattner  llvm::BasicBlock *getTrapBB();
21889cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
218921c9ad9d29d08a287292c670e7c52bc522c7f8bbAnders Carlsson  /// EmitCallArg - Emit a single call argument.
2190413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  void EmitCallArg(CallArgList &args, const Expr *E, QualType ArgType);
219121c9ad9d29d08a287292c670e7c52bc522c7f8bbAnders Carlsson
21922736071ea3a46f90e65c93418961611d96c10ab9John McCall  /// EmitDelegateCallArg - We are performing a delegate call; that
21932736071ea3a46f90e65c93418961611d96c10ab9John McCall  /// is, the current function is delegating to another one.  Produce
21942736071ea3a46f90e65c93418961611d96c10ab9John McCall  /// a r-value suitable for passing the given parameter.
2195413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  void EmitDelegateCallArg(CallArgList &args, const VarDecl *param);
21962736071ea3a46f90e65c93418961611d96c10ab9John McCall
219731a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnerprivate:
219829e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar  void EmitReturnOfRValue(RValue RV, QualType Ty);
219929e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar
22005627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  /// ExpandTypeFromArgs - Reconstruct a structure of type \arg Ty
22015627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  /// from function arguments into \arg Dst. See ABIArgInfo::Expand.
22025627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  ///
22035627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  /// \param AI - The first function argument of the expansion.
22045627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  /// \return The argument following the last expanded function
22055627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  /// argument.
22060dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  llvm::Function::arg_iterator
22075627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  ExpandTypeFromArgs(QualType Ty, LValue Dst,
22085627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar                     llvm::Function::arg_iterator AI);
22095627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
22100dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// ExpandTypeToArgs - Expand an RValue \arg Src, with the LLVM type for \arg
22110dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// Ty, into individual arguments on the provided vector \arg Args. See
22120dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// ABIArgInfo::Expand.
22130dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  void ExpandTypeToArgs(QualType Ty, RValue Src,
22145627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar                        llvm::SmallVector<llvm::Value*, 16> &Args);
2215c8c7b187adcd989e01a9fba1ebead6c2abd770c9Anders Carlsson
22161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Value* EmitAsmInput(const AsmStmt &S,
2217b84e8a670ad1d00c0a34295c42ac700cdde04815Daniel Dunbar                            const TargetInfo::ConstraintInfo &Info,
2218c8c7b187adcd989e01a9fba1ebead6c2abd770c9Anders Carlsson                            const Expr *InputExpr, std::string &ConstraintStr);
22190dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
22206d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman  llvm::Value* EmitAsmInputLValue(const AsmStmt &S,
22216d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                  const TargetInfo::ConstraintInfo &Info,
22226d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                  LValue InputValue, QualType InputType,
22236d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                  std::string &ConstraintStr);
22246d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman
22250139bb96494b4c4ba0824617d5d2495dc7e44c76Anders Carlsson  /// EmitCallArgs - Emit call arguments for a function.
22261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// The CallArgTypeInfo parameter is used for iterating over the known
2227af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson  /// argument types of the function being called.
2228af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson  template<typename T>
2229af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson  void EmitCallArgs(CallArgList& Args, const T* CallArgTypeInfo,
22300139bb96494b4c4ba0824617d5d2495dc7e44c76Anders Carlsson                    CallExpr::const_arg_iterator ArgBeg,
2231af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson                    CallExpr::const_arg_iterator ArgEnd) {
2232af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson      CallExpr::const_arg_iterator Arg = ArgBeg;
2233af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson
2234af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson    // First, use the argument types that the type info knows about
2235af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson    if (CallArgTypeInfo) {
2236af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson      for (typename T::arg_type_iterator I = CallArgTypeInfo->arg_type_begin(),
2237af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson           E = CallArgTypeInfo->arg_type_end(); I != E; ++I, ++Arg) {
223844b0a3e858af7b19f489619e52cd58970611cd15Eli Friedman        assert(Arg != ArgEnd && "Running over edge of argument list!");
2239af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson        QualType ArgType = *I;
2240745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian#ifndef NDEBUG
2241745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian        QualType ActualArgType = Arg->getType();
2242745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian        if (ArgType->isPointerType() && ActualArgType->isPointerType()) {
22439cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer          QualType ActualBaseType =
2244745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian            ActualArgType->getAs<PointerType>()->getPointeeType();
22459cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer          QualType ArgBaseType =
2246745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian            ArgType->getAs<PointerType>()->getPointeeType();
2247745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian          if (ArgBaseType->isVariableArrayType()) {
2248745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian            if (const VariableArrayType *VAT =
2249745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian                getContext().getAsVariableArrayType(ActualBaseType)) {
2250745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian              if (!VAT->getSizeExpr())
2251745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian                ActualArgType = ArgType;
2252745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian            }
2253745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian          }
2254745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian        }
2255af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson        assert(getContext().getCanonicalType(ArgType.getNonReferenceType()).
22561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump               getTypePtr() ==
2257745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian               getContext().getCanonicalType(ActualArgType).getTypePtr() &&
2258af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson               "type mismatch in call argument!");
2259745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian#endif
2260413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall        EmitCallArg(Args, *Arg, ArgType);
2261af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson      }
22621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // Either we've emitted all the call args, or we have a call to a
2264af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson      // variadic function.
22651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert((Arg == ArgEnd || CallArgTypeInfo->isVariadic()) &&
2266af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson             "Extra arguments in non-variadic function!");
22671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2268af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson    }
22691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2270af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson    // If we still have any arguments, emit them using the type of the argument.
2271413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall    for (; Arg != ArgEnd; ++Arg)
2272413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall      EmitCallArg(Args, *Arg, Arg->getType());
2273af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson  }
2274492c4f998d848673d3d6c9e6416115df4036a71dJohn McCall
2275492c4f998d848673d3d6c9e6416115df4036a71dJohn McCall  const TargetCodeGenInfo &getTargetHooks() const {
2276492c4f998d848673d3d6c9e6416115df4036a71dJohn McCall    return CGM.getTargetCodeGenInfo();
2277492c4f998d848673d3d6c9e6416115df4036a71dJohn McCall  }
2278744016dde06fcffd50931e94a98c850f8b12cd87John McCall
2279744016dde06fcffd50931e94a98c850f8b12cd87John McCall  void EmitDeclMetadata();
2280f0c11f7e6848f023ced6a5b51399ba787c7d4d0bJohn McCall
2281f0c11f7e6848f023ced6a5b51399ba787c7d4d0bJohn McCall  CodeGenModule::ByrefHelpers *
2282f0c11f7e6848f023ced6a5b51399ba787c7d4d0bJohn McCall  buildByrefHelpers(const llvm::StructType &byrefType,
2283f0c11f7e6848f023ced6a5b51399ba787c7d4d0bJohn McCall                    const AutoVarEmission &emission);
22845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
22851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2286150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall/// Helper class with most of the code for saving a value for a
2287150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall/// conditional expression cleanup.
2288804b807ea918184d6de63bd745e1ff75a9bfc679John McCallstruct DominatingLLVMValue {
2289150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  typedef llvm::PointerIntPair<llvm::Value*, 1, bool> saved_type;
2290150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
2291150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// Answer whether the given value needs extra work to be saved.
2292150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  static bool needsSaving(llvm::Value *value) {
2293150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    // If it's not an instruction, we don't need to save.
2294150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    if (!isa<llvm::Instruction>(value)) return false;
2295150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
2296150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    // If it's an instruction in the entry block, we don't need to save.
2297150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    llvm::BasicBlock *block = cast<llvm::Instruction>(value)->getParent();
2298150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    return (block != &block->getParent()->getEntryBlock());
2299150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  }
2300150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
2301150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// Try to save the given value.
2302150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  static saved_type save(CodeGenFunction &CGF, llvm::Value *value) {
2303150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    if (!needsSaving(value)) return saved_type(value, false);
2304150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
2305150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    // Otherwise we need an alloca.
2306150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    llvm::Value *alloca =
2307150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      CGF.CreateTempAlloca(value->getType(), "cond-cleanup.save");
2308150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    CGF.Builder.CreateStore(value, alloca);
2309150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
2310150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    return saved_type(alloca, true);
2311150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  }
2312150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
2313150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  static llvm::Value *restore(CodeGenFunction &CGF, saved_type value) {
2314150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    if (!value.getInt()) return value.getPointer();
2315150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    return CGF.Builder.CreateLoad(value.getPointer());
2316150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  }
2317150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall};
2318150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
2319804b807ea918184d6de63bd745e1ff75a9bfc679John McCall/// A partial specialization of DominatingValue for llvm::Values that
2320804b807ea918184d6de63bd745e1ff75a9bfc679John McCall/// might be llvm::Instructions.
2321804b807ea918184d6de63bd745e1ff75a9bfc679John McCalltemplate <class T> struct DominatingPointer<T,true> : DominatingLLVMValue {
2322804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  typedef T *type;
2323804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  static type restore(CodeGenFunction &CGF, saved_type value) {
2324804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    return static_cast<T*>(DominatingLLVMValue::restore(CGF, value));
2325804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  }
2326804b807ea918184d6de63bd745e1ff75a9bfc679John McCall};
2327804b807ea918184d6de63bd745e1ff75a9bfc679John McCall
2328804b807ea918184d6de63bd745e1ff75a9bfc679John McCall/// A specialization of DominatingValue for RValue.
2329804b807ea918184d6de63bd745e1ff75a9bfc679John McCalltemplate <> struct DominatingValue<RValue> {
2330804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  typedef RValue type;
2331804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  class saved_type {
2332804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    enum Kind { ScalarLiteral, ScalarAddress, AggregateLiteral,
2333804b807ea918184d6de63bd745e1ff75a9bfc679John McCall                AggregateAddress, ComplexAddress };
2334804b807ea918184d6de63bd745e1ff75a9bfc679John McCall
2335804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    llvm::Value *Value;
2336804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    Kind K;
2337804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    saved_type(llvm::Value *v, Kind k) : Value(v), K(k) {}
2338804b807ea918184d6de63bd745e1ff75a9bfc679John McCall
2339804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  public:
2340804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    static bool needsSaving(RValue value);
2341804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    static saved_type save(CodeGenFunction &CGF, RValue value);
2342804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    RValue restore(CodeGenFunction &CGF);
2343804b807ea918184d6de63bd745e1ff75a9bfc679John McCall
2344804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    // implementations in CGExprCXX.cpp
2345804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  };
2346804b807ea918184d6de63bd745e1ff75a9bfc679John McCall
2347804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  static bool needsSaving(type value) {
2348804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    return saved_type::needsSaving(value);
2349804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  }
2350804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  static saved_type save(CodeGenFunction &CGF, type value) {
2351804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    return saved_type::save(CGF, value);
2352804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  }
2353150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  static type restore(CodeGenFunction &CGF, saved_type value) {
2354804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    return value.restore(CGF);
2355150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  }
2356150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall};
2357150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
23585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace CodeGen
23595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace clang
23605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
23615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
2362