CodeGenFunction.h revision 9928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3
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 {
184c4a1a8450a3613ef256a71b9d8305b41f79eef50John McCall    // Anchor the construction vtable.
185c4a1a8450a3613ef256a71b9d8305b41f79eef50John McCall    virtual void anchor();
186da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  public:
187c4a1a8450a3613ef256a71b9d8305b41f79eef50John McCall    // Provide a virtual destructor to suppress a very common warning
188c4a1a8450a3613ef256a71b9d8305b41f79eef50John McCall    // that unfortunately cannot be suppressed without this.  Cleanups
189c4a1a8450a3613ef256a71b9d8305b41f79eef50John McCall    // should not rely on this destructor ever being called.
190c4a1a8450a3613ef256a71b9d8305b41f79eef50John McCall    virtual ~Cleanup() {}
1913e29f96988b0438e42dba3790f61fb3df42d90cbJohn McCall
192da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    /// Emit the cleanup.  For normal cleanups, this is run in the
193da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    /// same EH context as when the cleanup was pushed, i.e. the
194da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    /// immediately-enclosing context of the cleanup scope.  For
195da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    /// EH cleanups, this is run in a terminate context.
196da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    ///
197da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    // \param IsForEHCleanup true if this is for an EH cleanup, false
198da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    ///  if for a normal cleanup.
199da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    virtual void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) = 0;
200da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  };
201da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
202150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// ConditionalCleanupN stores the saved form of its N parameters,
203150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// then restores them and performs the cleanup.
2043ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  template <class T, class A0>
2053ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  class ConditionalCleanup1 : public Cleanup {
206804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    typedef typename DominatingValue<A0>::saved_type A0_saved;
2073ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    A0_saved a0_saved;
2083ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall
2093ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
210804b807ea918184d6de63bd745e1ff75a9bfc679John McCall      A0 a0 = DominatingValue<A0>::restore(CGF, a0_saved);
211c4a1a8450a3613ef256a71b9d8305b41f79eef50John McCall      T(a0).Emit(CGF, IsForEHCleanup);
2123ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    }
2133ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall
2143ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  public:
2153ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    ConditionalCleanup1(A0_saved a0)
2163ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall      : a0_saved(a0) {}
2173ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  };
2183ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall
219150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  template <class T, class A0, class A1>
2203ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  class ConditionalCleanup2 : public Cleanup {
221804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    typedef typename DominatingValue<A0>::saved_type A0_saved;
222804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    typedef typename DominatingValue<A1>::saved_type A1_saved;
2234bbcbda302cba8b1b0d88c20d735d09b483bd005John McCall    A0_saved a0_saved;
2244bbcbda302cba8b1b0d88c20d735d09b483bd005John McCall    A1_saved a1_saved;
225150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
2263ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
227804b807ea918184d6de63bd745e1ff75a9bfc679John McCall      A0 a0 = DominatingValue<A0>::restore(CGF, a0_saved);
228804b807ea918184d6de63bd745e1ff75a9bfc679John McCall      A1 a1 = DominatingValue<A1>::restore(CGF, a1_saved);
229c4a1a8450a3613ef256a71b9d8305b41f79eef50John McCall      T(a0, a1).Emit(CGF, IsForEHCleanup);
230150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    }
231150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
232150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  public:
2333ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    ConditionalCleanup2(A0_saved a0, A1_saved a1)
2343ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall      : a0_saved(a0), a1_saved(a1) {}
235150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  };
236150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
237d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor  template <class T, class A0, class A1, class A2>
238d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor  class ConditionalCleanup3 : public Cleanup {
239d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor    typedef typename DominatingValue<A0>::saved_type A0_saved;
240d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor    typedef typename DominatingValue<A1>::saved_type A1_saved;
241d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor    typedef typename DominatingValue<A2>::saved_type A2_saved;
242d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor    A0_saved a0_saved;
243d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor    A1_saved a1_saved;
244d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor    A2_saved a2_saved;
245d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor
246d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor    void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
247d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor      A0 a0 = DominatingValue<A0>::restore(CGF, a0_saved);
248d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor      A1 a1 = DominatingValue<A1>::restore(CGF, a1_saved);
249d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor      A2 a2 = DominatingValue<A2>::restore(CGF, a2_saved);
250c4a1a8450a3613ef256a71b9d8305b41f79eef50John McCall      T(a0, a1, a2).Emit(CGF, IsForEHCleanup);
251d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor    }
252d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor
253d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor  public:
254d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor    ConditionalCleanup3(A0_saved a0, A1_saved a1, A2_saved a2)
255c4a1a8450a3613ef256a71b9d8305b41f79eef50John McCall      : a0_saved(a0), a1_saved(a1), a2_saved(a2) {}
256d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor  };
257d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor
2589928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  template <class T, class A0, class A1, class A2, class A3>
2599928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  class ConditionalCleanup4 : public Cleanup {
2609928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    typedef typename DominatingValue<A0>::saved_type A0_saved;
2619928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    typedef typename DominatingValue<A1>::saved_type A1_saved;
2629928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    typedef typename DominatingValue<A2>::saved_type A2_saved;
2639928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    typedef typename DominatingValue<A3>::saved_type A3_saved;
2649928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    A0_saved a0_saved;
2659928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    A1_saved a1_saved;
2669928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    A2_saved a2_saved;
2679928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    A3_saved a3_saved;
2689928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall
2699928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
2709928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall      A0 a0 = DominatingValue<A0>::restore(CGF, a0_saved);
2719928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall      A1 a1 = DominatingValue<A1>::restore(CGF, a1_saved);
2729928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall      A2 a2 = DominatingValue<A2>::restore(CGF, a2_saved);
2739928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall      A3 a3 = DominatingValue<A3>::restore(CGF, a3_saved);
2749928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall      T(a0, a1, a2, a3).Emit(CGF, IsForEHCleanup);
2759928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    }
2769928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall
2779928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  public:
2789928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    ConditionalCleanup4(A0_saved a0, A1_saved a1, A2_saved a2, A3_saved a3)
2799928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall      : a0_saved(a0), a1_saved(a1), a2_saved(a2), a3_saved(a3) {}
2809928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  };
2819928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall
282f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallprivate:
283f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // The implementation for this class is in CGException.h and
284f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // CGException.cpp; the definition is here because it's used as a
285f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  // member of CodeGenFunction.
286f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
287f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// The start of the scope-stack buffer, i.e. the allocated pointer
288f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// for the buffer.  All of these pointers are either simultaneously
289f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// null or simultaneously valid.
290f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  char *StartOfBuffer;
291f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
292f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// The end of the buffer.
293f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  char *EndOfBuffer;
294f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
295f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// The first valid entry in the buffer.
296f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  char *StartOfData;
297f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
298f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// The innermost normal cleanup on the stack.
299f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  stable_iterator InnermostNormalCleanup;
300f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
301f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// The innermost EH cleanup on the stack.
302f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  stable_iterator InnermostEHCleanup;
303f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
304f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// The number of catches on the stack.
305f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  unsigned CatchDepth;
306f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
307ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// The current EH destination index.  Reset to FirstCatchIndex
308ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// whenever the last EH cleanup is popped.
309ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  unsigned NextEHDestIndex;
310ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  enum { FirstEHDestIndex = 1 };
311ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
312f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// The current set of branch fixups.  A branch fixup is a jump to
313f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// an as-yet unemitted label, i.e. a label for which we don't yet
314f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// know the EH stack depth.  Whenever we pop a cleanup, we have
315f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// to thread all the current branch fixups through it.
316f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ///
317f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Fixups are recorded as the Use of the respective branch or
318f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// switch statement.  The use points to the final destination.
319f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// When popping out of a cleanup, these uses are threaded through
320f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// the cleanup and adjusted to point to the new cleanup.
321f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ///
322f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Note that branches are allowed to jump into protected scopes
323f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// in certain situations;  e.g. the following code is legal:
324f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ///     struct A { ~A(); }; // trivial ctor, non-trivial dtor
325f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ///     goto foo;
326f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ///     A a;
327f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ///    foo:
328f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ///     bar();
329f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::SmallVector<BranchFixup, 8> BranchFixups;
330f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
331f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  char *allocate(size_t Size);
332f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
3331f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  void *pushCleanup(CleanupKind K, size_t DataSize);
334da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
335f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallpublic:
336f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EHScopeStack() : StartOfBuffer(0), EndOfBuffer(0), StartOfData(0),
337f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                   InnermostNormalCleanup(stable_end()),
338f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                   InnermostEHCleanup(stable_end()),
339ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall                   CatchDepth(0), NextEHDestIndex(FirstEHDestIndex) {}
340f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  ~EHScopeStack() { delete[] StartOfBuffer; }
341f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
342da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  // Variadic templates would make this not terrible.
343da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
344da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  /// Push a lazily-created cleanup on the stack.
3458e3f86193995c47ee0d229e4336c3382410f09f5John McCall  template <class T>
3461f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  void pushCleanup(CleanupKind Kind) {
3471f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    void *Buffer = pushCleanup(Kind, sizeof(T));
3481f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    Cleanup *Obj = new(Buffer) T();
3498e3f86193995c47ee0d229e4336c3382410f09f5John McCall    (void) Obj;
3508e3f86193995c47ee0d229e4336c3382410f09f5John McCall  }
3518e3f86193995c47ee0d229e4336c3382410f09f5John McCall
3528e3f86193995c47ee0d229e4336c3382410f09f5John McCall  /// Push a lazily-created cleanup on the stack.
3538e3f86193995c47ee0d229e4336c3382410f09f5John McCall  template <class T, class A0>
3541f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  void pushCleanup(CleanupKind Kind, A0 a0) {
3551f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    void *Buffer = pushCleanup(Kind, sizeof(T));
3561f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    Cleanup *Obj = new(Buffer) T(a0);
3578e3f86193995c47ee0d229e4336c3382410f09f5John McCall    (void) Obj;
3588e3f86193995c47ee0d229e4336c3382410f09f5John McCall  }
3598e3f86193995c47ee0d229e4336c3382410f09f5John McCall
3608e3f86193995c47ee0d229e4336c3382410f09f5John McCall  /// Push a lazily-created cleanup on the stack.
361da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  template <class T, class A0, class A1>
3621f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  void pushCleanup(CleanupKind Kind, A0 a0, A1 a1) {
3631f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    void *Buffer = pushCleanup(Kind, sizeof(T));
3641f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    Cleanup *Obj = new(Buffer) T(a0, a1);
365da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    (void) Obj;
366da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  }
367da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
368da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  /// Push a lazily-created cleanup on the stack.
369da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  template <class T, class A0, class A1, class A2>
3701f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  void pushCleanup(CleanupKind Kind, A0 a0, A1 a1, A2 a2) {
3711f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    void *Buffer = pushCleanup(Kind, sizeof(T));
3721f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    Cleanup *Obj = new(Buffer) T(a0, a1, a2);
373da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    (void) Obj;
374da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  }
375da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
376da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  /// Push a lazily-created cleanup on the stack.
377da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  template <class T, class A0, class A1, class A2, class A3>
3781f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  void pushCleanup(CleanupKind Kind, A0 a0, A1 a1, A2 a2, A3 a3) {
3791f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    void *Buffer = pushCleanup(Kind, sizeof(T));
3801f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    Cleanup *Obj = new(Buffer) T(a0, a1, a2, a3);
381da65ea86482bc116906edfb9ba1d7124f76cc867John McCall    (void) Obj;
382da65ea86482bc116906edfb9ba1d7124f76cc867John McCall  }
383da65ea86482bc116906edfb9ba1d7124f76cc867John McCall
38477199713ab56f87ffad9a535ff2a0877704eed87John McCall  /// Push a lazily-created cleanup on the stack.
38577199713ab56f87ffad9a535ff2a0877704eed87John McCall  template <class T, class A0, class A1, class A2, class A3, class A4>
3861f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall  void pushCleanup(CleanupKind Kind, A0 a0, A1 a1, A2 a2, A3 a3, A4 a4) {
3871f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    void *Buffer = pushCleanup(Kind, sizeof(T));
3881f0fca54676cfa8616e7f3cd7a26788ab937e3cdJohn McCall    Cleanup *Obj = new(Buffer) T(a0, a1, a2, a3, a4);
38977199713ab56f87ffad9a535ff2a0877704eed87John McCall    (void) Obj;
39077199713ab56f87ffad9a535ff2a0877704eed87John McCall  }
39177199713ab56f87ffad9a535ff2a0877704eed87John McCall
3927d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  // Feel free to add more variants of the following:
3937d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall
3947d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// Push a cleanup with non-constant storage requirements on the
3957d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// stack.  The cleanup type must provide an additional static method:
3967d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  ///   static size_t getExtraSize(size_t);
3977d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// The argument to this method will be the value N, which will also
3987d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// be passed as the first argument to the constructor.
3997d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  ///
4007d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// The data stored in the extra storage must obey the same
4017d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// restrictions as normal cleanup member data.
4027d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  ///
4037d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// The pointer returned from this method is valid until the cleanup
4047d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// stack is modified.
4057d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  template <class T, class A0, class A1, class A2>
4067d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  T *pushCleanupWithExtra(CleanupKind Kind, size_t N, A0 a0, A1 a1, A2 a2) {
4077d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall    void *Buffer = pushCleanup(Kind, sizeof(T) + T::getExtraSize(N));
4087d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall    return new (Buffer) T(N, a0, a1, a2);
4097d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  }
4107d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall
411f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Pops a cleanup scope off the stack.  This should only be called
412f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// by CodeGenFunction::PopCleanupBlock.
413f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  void popCleanup();
414f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
415f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Push a set of catch handlers on the stack.  The catch is
416f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// uninitialized and will need to have the given number of handlers
417f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// set on it.
418f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  class EHCatchScope *pushCatch(unsigned NumHandlers);
419f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
420f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Pops a catch scope off the stack.
421f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  void popCatch();
422f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
423f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Push an exceptions filter on the stack.
424f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  class EHFilterScope *pushFilter(unsigned NumFilters);
425f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
426f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Pops an exceptions filter off the stack.
427f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  void popFilter();
428f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
429f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Push a terminate handler on the stack.
430f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  void pushTerminate();
431f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
432f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Pops a terminate handler off the stack.
433f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  void popTerminate();
434f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
435f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Determines whether the exception-scopes stack is empty.
436f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  bool empty() const { return StartOfData == EndOfBuffer; }
437f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
438f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  bool requiresLandingPad() const {
439f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return (CatchDepth || hasEHCleanups());
440f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
441f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
442f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Determines whether there are any normal cleanups on the stack.
443f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  bool hasNormalCleanups() const {
444f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return InnermostNormalCleanup != stable_end();
445f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
446f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
447f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Returns the innermost normal cleanup on the stack, or
448f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// stable_end() if there are no normal cleanups.
449f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  stable_iterator getInnermostNormalCleanup() const {
450f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return InnermostNormalCleanup;
451f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
452838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall  stable_iterator getInnermostActiveNormalCleanup() const; // CGException.h
453f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
454f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Determines whether there are any EH cleanups on the stack.
455f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  bool hasEHCleanups() const {
456f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return InnermostEHCleanup != stable_end();
457f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
458f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
459f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Returns the innermost EH cleanup on the stack, or stable_end()
460f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// if there are no EH cleanups.
461f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  stable_iterator getInnermostEHCleanup() const {
462f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return InnermostEHCleanup;
463f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
464838d796d020f6a8cd2b2d1e2a0a85c83bbf29543John McCall  stable_iterator getInnermostActiveEHCleanup() const; // CGException.h
465f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
466f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// An unstable reference to a scope-stack depth.  Invalidated by
467f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// pushes but not pops.
468f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  class iterator;
469f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
470f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Returns an iterator pointing to the innermost EH scope.
471f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  iterator begin() const;
472f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
473f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Returns an iterator pointing to the outermost EH scope.
474f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  iterator end() const;
475f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
476f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Create a stable reference to the top of the EH stack.  The
477f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// returned reference is valid until that scope is popped off the
478f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// stack.
479f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  stable_iterator stable_begin() const {
480f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return stable_iterator(EndOfBuffer - StartOfData);
481f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
482f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
483f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Create a stable reference to the bottom of the EH stack.
484f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  static stable_iterator stable_end() {
485f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return stable_iterator(0);
486f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
487f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
488f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Translates an iterator into a stable_iterator.
489f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  stable_iterator stabilize(iterator it) const;
490f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
491f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Finds the nearest cleanup enclosing the given iterator.
492f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Returns stable_iterator::invalid() if there are no such cleanups.
493f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  stable_iterator getEnclosingEHCleanup(iterator it) const;
494f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
495f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Turn a stable reference to a scope depth into a unstable pointer
496f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// to the EH stack.
497f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  iterator find(stable_iterator save) const;
498f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
499f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Removes the cleanup pointed to by the given stable_iterator.
500f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  void removeCleanup(stable_iterator save);
501f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
502f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Add a branch fixup to the current cleanup scope.
503f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  BranchFixup &addBranchFixup() {
504f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    assert(hasNormalCleanups() && "adding fixup in scope without cleanups");
505f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    BranchFixups.push_back(BranchFixup());
506f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return BranchFixups.back();
507f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
508f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
509f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  unsigned getNumBranchFixups() const { return BranchFixups.size(); }
510f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  BranchFixup &getBranchFixup(unsigned I) {
511f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    assert(I < getNumBranchFixups());
512f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return BranchFixups[I];
513f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
514f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
515ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// Pops lazily-removed fixups from the end of the list.  This
516ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// should only be called by procedures which have just popped a
517ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// cleanup or resolved one or more fixups.
518ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  void popNullFixups();
519ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
520ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// Clears the branch-fixups list.  This should only be called by
5218abdbd8118e37e759d1ce3f5814ee0a24b1589e8John McCall  /// ResolveAllBranchFixups.
522ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  void clearFixups() { BranchFixups.clear(); }
523ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
524ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// Gets the next EH destination index.
525ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  unsigned getNextEHDestIndex() { return NextEHDestIndex++; }
526f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall};
527f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
5285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// CodeGenFunction - This class organizes the per-function state that is used
5295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// while generating LLVM code.
5305936e33bf74dd6bf126ceee0f6169a2593d03a69John McCallclass CodeGenFunction : public CodeGenTypeCache {
5318a219ceda2b5afd447e7199b9c53079bead31b89Anders Carlsson  CodeGenFunction(const CodeGenFunction&); // DO NOT IMPLEMENT
5328a219ceda2b5afd447e7199b9c53079bead31b89Anders Carlsson  void operator=(const CodeGenFunction&);  // DO NOT IMPLEMENT
5334c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall
5344c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  friend class CGCXXABI;
535bfc0c1ae4b505b52b36f572a9641f99e76879cabChris Lattnerpublic:
536ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// A jump destination is an abstract label, branching to which may
537ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// require a jump out through normal cleanups.
538f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  struct JumpDest {
539ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    JumpDest() : Block(0), ScopeDepth(), Index(0) {}
540ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    JumpDest(llvm::BasicBlock *Block,
541ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall             EHScopeStack::stable_iterator Depth,
542ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall             unsigned Index)
543ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      : Block(Block), ScopeDepth(Depth), Index(Index) {}
544ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
545ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    bool isValid() const { return Block != 0; }
546ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    llvm::BasicBlock *getBlock() const { return Block; }
547ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    EHScopeStack::stable_iterator getScopeDepth() const { return ScopeDepth; }
548ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    unsigned getDestIndex() const { return Index; }
5499cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
550ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  private:
551f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    llvm::BasicBlock *Block;
552f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    EHScopeStack::stable_iterator ScopeDepth;
553ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    unsigned Index;
554ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  };
555ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
556ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// An unwind destination is an abstract label, branching to which
557ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// may require a jump out through EH cleanups.
558ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  struct UnwindDest {
559ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    UnwindDest() : Block(0), ScopeDepth(), Index(0) {}
560ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    UnwindDest(llvm::BasicBlock *Block,
561ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall               EHScopeStack::stable_iterator Depth,
562ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall               unsigned Index)
563ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall      : Block(Block), ScopeDepth(Depth), Index(Index) {}
564ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
565ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    bool isValid() const { return Block != 0; }
566ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    llvm::BasicBlock *getBlock() const { return Block; }
567ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    EHScopeStack::stable_iterator getScopeDepth() const { return ScopeDepth; }
568ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    unsigned getDestIndex() const { return Index; }
569ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
570ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  private:
571ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    llvm::BasicBlock *Block;
572ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    EHScopeStack::stable_iterator ScopeDepth;
573ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    unsigned Index;
574f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  };
575f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
5765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CodeGenModule &CGM;  // Per-module state.
577444be7366d0a1e172c0290a1ea54c1cb16b5947cDaniel Dunbar  const TargetInfo &Target;
5780dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
57958dee10ed2eee34035f62d1c2d32b3639e9182f8Chris Lattner  typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
58045d196b8387dcefc4df26cda114fa34c6528e928Daniel Dunbar  CGBuilderTy Builder;
5810dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
582b5437d238752dc297e42410e98d38d5250fe0463Chris Lattner  /// CurFuncDecl - Holds the Decl for the current function or ObjC method.
583b5437d238752dc297e42410e98d38d5250fe0463Chris Lattner  /// This excludes BlockDecls.
5844111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner  const Decl *CurFuncDecl;
585b5437d238752dc297e42410e98d38d5250fe0463Chris Lattner  /// CurCodeDecl - This is the inner-most code context, which includes blocks.
586b5437d238752dc297e42410e98d38d5250fe0463Chris Lattner  const Decl *CurCodeDecl;
58788b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  const CGFunctionInfo *CurFnInfo;
588391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  QualType FnRetTy;
5895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::Function *CurFn;
5905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5916a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump  /// CurGD - The GlobalDecl for the current function being compiled.
5926a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump  GlobalDecl CurGD;
5936a1e0eb557d47e85185e09bdf8721f53f4bf9c9cMike Stump
594f85e193739c953358c865005855253af4f68a497John McCall  /// PrologueCleanupDepth - The cleanup depth enclosing all the
595f85e193739c953358c865005855253af4f68a497John McCall  /// cleanups associated with the parameters.
596f85e193739c953358c865005855253af4f68a497John McCall  EHScopeStack::stable_iterator PrologueCleanupDepth;
597f85e193739c953358c865005855253af4f68a497John McCall
5985ca2084cf9b529563209429857f01fdae9dcdfa5Daniel Dunbar  /// ReturnBlock - Unified return block.
599f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  JumpDest ReturnBlock;
600f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
6010dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// ReturnValue - The temporary alloca to hold the return value. This is null
6020dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// iff the function has no return value.
603b17daf9ab790ae71aacad2cc4aa11cd8d86c25d1Eli Friedman  llvm::Value *ReturnValue;
6040dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
605ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// RethrowBlock - Unified rethrow block.
606ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  UnwindDest RethrowBlock;
607ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
6085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// AllocaInsertPoint - This is an instruction in the entry block before which
6095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// we prefer to insert allocas.
610481769b5dc102b0256b35581e787909ad5edfab5Chris Lattner  llvm::AssertingVH<llvm::Instruction> AllocaInsertPt;
6110ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar
6129c276ae0f24d4cee8f7954069d4b8eae45d0447dMike Stump  bool CatchUndefined;
6139cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
614f85e193739c953358c865005855253af4f68a497John McCall  /// In ARC, whether we should autorelease the return value.
615f85e193739c953358c865005855253af4f68a497John McCall  bool AutoreleaseResult;
616f85e193739c953358c865005855253af4f68a497John McCall
617d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  const CodeGen::CGBlockInfo *BlockInfo;
618d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  llvm::Value *BlockPointer;
619d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall
6203d91bbcdab155181556969cad6ec97014405acedDouglas Gregor  /// \brief A mapping from NRVO variables to the flags used to indicate
6213d91bbcdab155181556969cad6ec97014405acedDouglas Gregor  /// when the NRVO has been applied to this variable.
6223d91bbcdab155181556969cad6ec97014405acedDouglas Gregor  llvm::DenseMap<const VarDecl *, llvm::Value *> NRVOFlags;
6239cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
624f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  EHScopeStack EHStack;
6250dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
626ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// i32s containing the indexes of the cleanup destinations.
627ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::AllocaInst *NormalCleanupDest;
628ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::AllocaInst *EHCleanupDest;
629ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
630ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  unsigned NextCleanupDestIndex;
631ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
632f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// The exception slot.  All landing pads write the current
633f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// exception pointer into this alloca.
634f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::Value *ExceptionSlot;
6350dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
63693c332a8ba2c193c435b293966d343dab15f555bJohn McCall  /// The selector slot.  Under the MandatoryCleanup model, all
63793c332a8ba2c193c435b293966d343dab15f555bJohn McCall  /// landing pads write the current selector value into this alloca.
63893c332a8ba2c193c435b293966d343dab15f555bJohn McCall  llvm::AllocaInst *EHSelectorSlot;
63993c332a8ba2c193c435b293966d343dab15f555bJohn McCall
640f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Emits a landing pad for the current EH stack.
641f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *EmitLandingPad();
6420dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
643f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *getInvokeDestImpl();
64499533834ba8f3658559f334e68a518ebb6388ceaMike Stump
6453ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  /// Set up the last cleaup that was pushed as a conditional
6463ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  /// full-expression cleanup.
6473ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  void initFullExprCleanup();
648150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
649150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  template <class T>
650804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  typename DominatingValue<T>::saved_type saveValueInCond(T value) {
651804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    return DominatingValue<T>::save(*this, value);
652150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  }
653150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
654f1549f66a8216a78112286e3978cea2c29d6334cJohn McCallpublic:
655f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// ObjCEHValueStack - Stack of Objective-C exception values, used for
656f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// rethrows.
657f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::SmallVector<llvm::Value*, 8> ObjCEHValueStack;
658bb66f9f2e454135b86462d121629275b6ac38e96Anders Carlsson
659d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  /// A class controlling the emission of a finally block.
660d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  class FinallyInfo {
661d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    /// Where the catchall's edge through the cleanup should go.
662d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    JumpDest RethrowDest;
663d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall
664d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    /// A function to call to enter the catch.
665d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    llvm::Constant *BeginCatchFn;
666d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall
667d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    /// An i1 variable indicating whether or not the @finally is
668d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    /// running for an exception.
669d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    llvm::AllocaInst *ForEHVar;
670d88ea5687968640ada2bc5a10211cbeb68a671ecMike Stump
671d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    /// An i8* variable into which the exception pointer to rethrow
672d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    /// has been saved.
673d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    llvm::AllocaInst *SavedExnVar;
674d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall
675d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  public:
676d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    void enter(CodeGenFunction &CGF, const Stmt *Finally,
677d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall               llvm::Constant *beginCatchFn, llvm::Constant *endCatchFn,
678d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall               llvm::Constant *rethrowFn);
679d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall    void exit(CodeGenFunction &CGF);
680d768e9d29abe1ac1ccc3ed63f2dce835d9bab342John McCall  };
681f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
682150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// pushFullExprCleanup - Push a cleanup to be run at the end of the
683150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// current full-expression.  Safe against the possibility that
684150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// we're currently inside a conditionally-evaluated expression.
6853ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  template <class T, class A0>
6863ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  void pushFullExprCleanup(CleanupKind kind, A0 a0) {
6873ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    // If we're not in a conditional branch, or if none of the
6883ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    // arguments requires saving, then use the unconditional cleanup.
689c4a1a8450a3613ef256a71b9d8305b41f79eef50John McCall    if (!isInConditionalBranch())
690c4a1a8450a3613ef256a71b9d8305b41f79eef50John McCall      return EHStack.pushCleanup<T>(kind, a0);
6913ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall
692804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
6933ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall
6943ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    typedef EHScopeStack::ConditionalCleanup1<T, A0> CleanupType;
6953ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    EHStack.pushCleanup<CleanupType>(kind, a0_saved);
6963ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    initFullExprCleanup();
6973ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  }
6983ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall
6993ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  /// pushFullExprCleanup - Push a cleanup to be run at the end of the
7003ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  /// current full-expression.  Safe against the possibility that
7013ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall  /// we're currently inside a conditionally-evaluated expression.
702150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  template <class T, class A0, class A1>
703150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1) {
704150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    // If we're not in a conditional branch, or if none of the
705150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    // arguments requires saving, then use the unconditional cleanup.
706c4a1a8450a3613ef256a71b9d8305b41f79eef50John McCall    if (!isInConditionalBranch())
707c4a1a8450a3613ef256a71b9d8305b41f79eef50John McCall      return EHStack.pushCleanup<T>(kind, a0, a1);
708150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
709804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
710804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1);
711150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
712150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    typedef EHScopeStack::ConditionalCleanup2<T, A0, A1> CleanupType;
7133ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved);
7143ad32c8d93eb65d1d4943d7df567fc9b4f55d137John McCall    initFullExprCleanup();
715150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  }
716150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
717d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor  /// pushFullExprCleanup - Push a cleanup to be run at the end of the
718d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor  /// current full-expression.  Safe against the possibility that
719d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor  /// we're currently inside a conditionally-evaluated expression.
720d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor  template <class T, class A0, class A1, class A2>
721d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor  void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1, A2 a2) {
722d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor    // If we're not in a conditional branch, or if none of the
723d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor    // arguments requires saving, then use the unconditional cleanup.
724d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor    if (!isInConditionalBranch()) {
725c4a1a8450a3613ef256a71b9d8305b41f79eef50John McCall      return EHStack.pushCleanup<T>(kind, a0, a1, a2);
726d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor    }
727d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor
728d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor    typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
729d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor    typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1);
730d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor    typename DominatingValue<A2>::saved_type a2_saved = saveValueInCond(a2);
731d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor
732d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor    typedef EHScopeStack::ConditionalCleanup3<T, A0, A1, A2> CleanupType;
733d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor    EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved, a2_saved);
734d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor    initFullExprCleanup();
735d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor  }
736d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor
7379928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  /// pushFullExprCleanup - Push a cleanup to be run at the end of the
7389928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  /// current full-expression.  Safe against the possibility that
7399928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  /// we're currently inside a conditionally-evaluated expression.
7409928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  template <class T, class A0, class A1, class A2, class A3>
7419928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  void pushFullExprCleanup(CleanupKind kind, A0 a0, A1 a1, A2 a2, A3 a3) {
7429928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    // If we're not in a conditional branch, or if none of the
7439928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    // arguments requires saving, then use the unconditional cleanup.
7449928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    if (!isInConditionalBranch()) {
7459928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall      return EHStack.pushCleanup<T>(kind, a0, a1, a2, a3);
7469928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    }
7479928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall
7489928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    typename DominatingValue<A0>::saved_type a0_saved = saveValueInCond(a0);
7499928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    typename DominatingValue<A1>::saved_type a1_saved = saveValueInCond(a1);
7509928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    typename DominatingValue<A2>::saved_type a2_saved = saveValueInCond(a2);
7519928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    typename DominatingValue<A3>::saved_type a3_saved = saveValueInCond(a3);
7529928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall
7539928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    typedef EHScopeStack::ConditionalCleanup4<T, A0, A1, A2, A3> CleanupType;
7549928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    EHStack.pushCleanup<CleanupType>(kind, a0_saved, a1_saved,
7559928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall                                     a2_saved, a3_saved);
7569928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    initFullExprCleanup();
7579928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  }
7589928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall
759f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// PushDestructorCleanup - Push a cleanup to call the
760f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// complete-object destructor of an object of the given type at the
761f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// given address.  Does nothing if T is not a C++ class type with a
762f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// non-trivial destructor.
763f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  void PushDestructorCleanup(QualType T, llvm::Value *Addr);
764f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
76581407d404a3fc8d24d0e2a052d85cae183033e2bJohn McCall  /// PushDestructorCleanup - Push a cleanup to call the
76681407d404a3fc8d24d0e2a052d85cae183033e2bJohn McCall  /// complete-object variant of the given destructor on the object at
76781407d404a3fc8d24d0e2a052d85cae183033e2bJohn McCall  /// the given address.
76881407d404a3fc8d24d0e2a052d85cae183033e2bJohn McCall  void PushDestructorCleanup(const CXXDestructorDecl *Dtor,
76981407d404a3fc8d24d0e2a052d85cae183033e2bJohn McCall                             llvm::Value *Addr);
77081407d404a3fc8d24d0e2a052d85cae183033e2bJohn McCall
771f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// PopCleanupBlock - Will pop the cleanup entry on the stack and
772f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// process all branch fixups.
773ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  void PopCleanupBlock(bool FallThroughIsBranchThrough = false);
774f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
7757d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// DeactivateCleanupBlock - Deactivates the given cleanup block.
7767d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// The block cannot be reactivated.  Pops it if it's the top of the
7777d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// stack.
7787d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  void DeactivateCleanupBlock(EHScopeStack::stable_iterator Cleanup);
7797d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall
7807d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// ActivateCleanupBlock - Activates an initially-inactive cleanup.
7817d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  /// Cannot be used to resurrect a deactivated cleanup.
7827d8647f194ae4f2499e5bcd40dcfea34cd21ebc6John McCall  void ActivateCleanupBlock(EHScopeStack::stable_iterator Cleanup);
783cd2d2b7814e0104ed41a8da159a06a8ca77b70d8John McCall
784f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// \brief Enters a new scope for capturing cleanups, all of which
785f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// will be executed once the scope is exited.
786f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  class RunCleanupsScope {
78701234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    CodeGenFunction& CGF;
788f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    EHScopeStack::stable_iterator CleanupStackDepth;
78901234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    bool OldDidCallStackSave;
7905656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    bool PerformCleanup;
79101234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor
792f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    RunCleanupsScope(const RunCleanupsScope &); // DO NOT IMPLEMENT
793f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    RunCleanupsScope &operator=(const RunCleanupsScope &); // DO NOT IMPLEMENT
79401234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor
79501234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor  public:
79601234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    /// \brief Enter a new cleanup scope.
7979cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer    explicit RunCleanupsScope(CodeGenFunction &CGF)
7989cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer      : CGF(CGF), PerformCleanup(true)
7995656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    {
800f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      CleanupStackDepth = CGF.EHStack.stable_begin();
80101234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor      OldDidCallStackSave = CGF.DidCallStackSave;
8024ada2ca7d82dab68d3646f3eb6dcdfee072e8ea4Argyrios Kyrtzidis      CGF.DidCallStackSave = false;
80301234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    }
80401234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor
80501234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    /// \brief Exit this cleanup scope, emitting any accumulated
80601234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    /// cleanups.
807f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    ~RunCleanupsScope() {
8085656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor      if (PerformCleanup) {
8095656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor        CGF.DidCallStackSave = OldDidCallStackSave;
810f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall        CGF.PopCleanupBlocks(CleanupStackDepth);
8115656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor      }
8125656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    }
8135656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor
8145656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    /// \brief Determine whether this scope requires any cleanups.
8155656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    bool requiresCleanups() const {
816f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      return CGF.EHStack.stable_begin() != CleanupStackDepth;
8175656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    }
8185656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor
8195656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    /// \brief Force the emission of cleanups now, instead of waiting
8205656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    /// until this object is destroyed.
8215656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor    void ForceCleanup() {
8225656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor      assert(PerformCleanup && "Already forced cleanup");
82301234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor      CGF.DidCallStackSave = OldDidCallStackSave;
824f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      CGF.PopCleanupBlocks(CleanupStackDepth);
8255656e14d91405417182171a705ed3e3d2d6d7aa3Douglas Gregor      PerformCleanup = false;
82601234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor    }
82701234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor  };
82801234bbc1cb94946df8046ad95e17537082b4f71Douglas Gregor
82944ec82b4a1597802f5bf17721481b8c265bc8dc5Anders Carlsson
830f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// PopCleanupBlocks - Takes the old cleanup stack size and emits
831f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// the cleanup blocks that have been added.
832f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  void PopCleanupBlocks(EHScopeStack::stable_iterator OldCleanupStackSize);
83344ec82b4a1597802f5bf17721481b8c265bc8dc5Anders Carlsson
834ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  void ResolveBranchFixups(llvm::BasicBlock *Target);
835ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
836f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// The given basic block lies in the current EH scope, but may be a
837f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// target of a potentially scope-crossing jump; get a stable handle
838f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// to which we can perform this jump later.
839ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  JumpDest getJumpDestInCurrentScope(llvm::BasicBlock *Target) {
840413e67778d593215d2f2161a4e712c8568f1ddd0John McCall    return JumpDest(Target,
841413e67778d593215d2f2161a4e712c8568f1ddd0John McCall                    EHStack.getInnermostNormalCleanup(),
842413e67778d593215d2f2161a4e712c8568f1ddd0John McCall                    NextCleanupDestIndex++);
843f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
844c71c845fe77ee1f891d60232ec320912d88557eeAnders Carlsson
845f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// The given basic block lies in the current EH scope, but may be a
846f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// target of a potentially scope-crossing jump; get a stable handle
847f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// to which we can perform this jump later.
848ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  JumpDest getJumpDestInCurrentScope(llvm::StringRef Name = llvm::StringRef()) {
849ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall    return getJumpDestInCurrentScope(createBasicBlock(Name));
850f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
851f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
852f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// EmitBranchThroughCleanup - Emit a branch from the current insert
853f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// block through the normal cleanup handling code (if any) and then
854f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// on to \arg Dest.
855f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  void EmitBranchThroughCleanup(JumpDest Dest);
856b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner
857b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner  /// isObviouslyBranchWithoutCleanups - Return true if a branch to the
858b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner  /// specified destination obviously has no cleanups to run.  'false' is always
859b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner  /// a conservatively correct answer for this method.
860b11f9198111796ada02b57f62cdea92134fde9f7Chris Lattner  bool isObviouslyBranchWithoutCleanups(JumpDest Dest) const;
861f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
862f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// EmitBranchThroughEHCleanup - Emit a branch from the current
863f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// insert block through the EH cleanup handling code (if any) and
864f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// then on to \arg Dest.
865ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  void EmitBranchThroughEHCleanup(UnwindDest Dest);
866ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
867ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// getRethrowDest - Returns the unified outermost-scope rethrow
868ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  /// destination.
869ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  UnwindDest getRethrowDest();
8700dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
871150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// An object to manage conditionally-evaluated expressions.
872150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  class ConditionalEvaluation {
873150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    llvm::BasicBlock *StartBB;
8741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
875150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  public:
876150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    ConditionalEvaluation(CodeGenFunction &CGF)
877150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      : StartBB(CGF.Builder.GetInsertBlock()) {}
8789cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
879150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    void begin(CodeGenFunction &CGF) {
880150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      assert(CGF.OutermostConditional != this);
881150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      if (!CGF.OutermostConditional)
882150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall        CGF.OutermostConditional = this;
883150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    }
884150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
885150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    void end(CodeGenFunction &CGF) {
886150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      assert(CGF.OutermostConditional != 0);
887150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      if (CGF.OutermostConditional == this)
888150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall        CGF.OutermostConditional = 0;
889150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    }
890150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
891150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    /// Returns a block which will be executed prior to each
892150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    /// evaluation of the conditional code.
893150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    llvm::BasicBlock *getStartingBlock() const {
894150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      return StartBB;
895150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    }
896150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  };
8971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8983019c444c672938c57f5573840071ecd73425ee7John McCall  /// isInConditionalBranch - Return true if we're currently emitting
8993019c444c672938c57f5573840071ecd73425ee7John McCall  /// one branch or the other of a conditional expression.
900150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  bool isInConditionalBranch() const { return OutermostConditional != 0; }
901150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
902150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// An RAII object to record that we're evaluating a statement
903150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// expression.
904150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  class StmtExprEvaluation {
905150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    CodeGenFunction &CGF;
906150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
907150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    /// We have to save the outermost conditional: cleanups in a
908150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    /// statement expression aren't conditional just because the
909150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    /// StmtExpr is.
910150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    ConditionalEvaluation *SavedOutermostConditional;
911150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
912150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  public:
913150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    StmtExprEvaluation(CodeGenFunction &CGF)
914150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      : CGF(CGF), SavedOutermostConditional(CGF.OutermostConditional) {
915150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      CGF.OutermostConditional = 0;
916150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    }
917150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
918150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    ~StmtExprEvaluation() {
919150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      CGF.OutermostConditional = SavedOutermostConditional;
920150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      CGF.EnsureInsertPoint();
921150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    }
922150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  };
923e996ffd240f20a1048179d7727a6ee3227261921John McCall
92456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// An object which temporarily prevents a value from being
92556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// destroyed by aggressive peephole optimizations that assume that
92656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// all uses of a value have been realized in the IR.
92756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  class PeepholeProtection {
92856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    llvm::Instruction *Inst;
92956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    friend class CodeGenFunction;
93056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
93156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  public:
93256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    PeepholeProtection() : Inst(0) {}
93356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  };
93456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
935e996ffd240f20a1048179d7727a6ee3227261921John McCall  /// An RAII object to set (and then clear) a mapping for an OpaqueValueExpr.
936e996ffd240f20a1048179d7727a6ee3227261921John McCall  class OpaqueValueMapping {
937e996ffd240f20a1048179d7727a6ee3227261921John McCall    CodeGenFunction &CGF;
938e996ffd240f20a1048179d7727a6ee3227261921John McCall    const OpaqueValueExpr *OpaqueValue;
93956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    bool BoundLValue;
94056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    CodeGenFunction::PeepholeProtection Protection;
941e996ffd240f20a1048179d7727a6ee3227261921John McCall
942e996ffd240f20a1048179d7727a6ee3227261921John McCall  public:
94356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    static bool shouldBindAsLValue(const Expr *expr) {
94456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      return expr->isGLValue() || expr->getType()->isRecordType();
94556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
94656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
94756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    /// Build the opaque value mapping for the given conditional
94856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    /// operator if it's the GNU ?: extension.  This is a common
94956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    /// enough pattern that the convenience operator is really
95056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    /// helpful.
95156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    ///
95256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    OpaqueValueMapping(CodeGenFunction &CGF,
95356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall                       const AbstractConditionalOperator *op) : CGF(CGF) {
95456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      if (isa<ConditionalOperator>(op)) {
95556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        OpaqueValue = 0;
95656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        BoundLValue = false;
95756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        return;
95856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      }
95956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
96056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      const BinaryConditionalOperator *e = cast<BinaryConditionalOperator>(op);
96156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      init(e->getOpaqueValue(), e->getCommon());
96256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
96356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
964e996ffd240f20a1048179d7727a6ee3227261921John McCall    OpaqueValueMapping(CodeGenFunction &CGF,
965e996ffd240f20a1048179d7727a6ee3227261921John McCall                       const OpaqueValueExpr *opaqueValue,
96656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall                       LValue lvalue)
96756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      : CGF(CGF), OpaqueValue(opaqueValue), BoundLValue(true) {
968e996ffd240f20a1048179d7727a6ee3227261921John McCall      assert(opaqueValue && "no opaque value expression!");
96956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      assert(shouldBindAsLValue(opaqueValue));
97056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      initLValue(lvalue);
97156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
97256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
97356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    OpaqueValueMapping(CodeGenFunction &CGF,
97456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall                       const OpaqueValueExpr *opaqueValue,
97556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall                       RValue rvalue)
97656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      : CGF(CGF), OpaqueValue(opaqueValue), BoundLValue(false) {
97756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      assert(opaqueValue && "no opaque value expression!");
97856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      assert(!shouldBindAsLValue(opaqueValue));
97956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      initRValue(rvalue);
980e996ffd240f20a1048179d7727a6ee3227261921John McCall    }
981e996ffd240f20a1048179d7727a6ee3227261921John McCall
982e996ffd240f20a1048179d7727a6ee3227261921John McCall    void pop() {
983e996ffd240f20a1048179d7727a6ee3227261921John McCall      assert(OpaqueValue && "mapping already popped!");
98456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      popImpl();
985e996ffd240f20a1048179d7727a6ee3227261921John McCall      OpaqueValue = 0;
986e996ffd240f20a1048179d7727a6ee3227261921John McCall    }
987e996ffd240f20a1048179d7727a6ee3227261921John McCall
988e996ffd240f20a1048179d7727a6ee3227261921John McCall    ~OpaqueValueMapping() {
98956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      if (OpaqueValue) popImpl();
99056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
99156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
99256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  private:
99356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    void popImpl() {
99456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      if (BoundLValue)
99556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        CGF.OpaqueLValues.erase(OpaqueValue);
99656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      else {
99756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        CGF.OpaqueRValues.erase(OpaqueValue);
99856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        CGF.unprotectFromPeepholes(Protection);
99956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      }
100056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
100156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
100256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    void init(const OpaqueValueExpr *ov, const Expr *e) {
100356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      OpaqueValue = ov;
100456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      BoundLValue = shouldBindAsLValue(ov);
100556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      assert(BoundLValue == shouldBindAsLValue(e)
100656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall             && "inconsistent expression value kinds!");
100756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      if (BoundLValue)
100856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        initLValue(CGF.EmitLValue(e));
100956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      else
101056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall        initRValue(CGF.EmitAnyExpr(e));
101156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
101256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
101356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    void initLValue(const LValue &lv) {
101456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      CGF.OpaqueLValues.insert(std::make_pair(OpaqueValue, lv));
101556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    }
101656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
101756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    void initRValue(const RValue &rv) {
101856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      // Work around an extremely aggressive peephole optimization in
101956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      // EmitScalarConversion which assumes that all other uses of a
102056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      // value are extant.
102156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      Protection = CGF.protectFromPeepholes(rv);
102256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      CGF.OpaqueRValues.insert(std::make_pair(OpaqueValue, rv));
1023e996ffd240f20a1048179d7727a6ee3227261921John McCall    }
1024e996ffd240f20a1048179d7727a6ee3227261921John McCall  };
1025e220455a059d926953befe72857b9525273717efFariborz Jahanian
1026e220455a059d926953befe72857b9525273717efFariborz Jahanian  /// getByrefValueFieldNumber - Given a declaration, returns the LLVM field
1027e220455a059d926953befe72857b9525273717efFariborz Jahanian  /// number that holds the value.
1028e220455a059d926953befe72857b9525273717efFariborz Jahanian  unsigned getByRefValueLLVMField(const ValueDecl *VD) const;
102952a80e19ad688091723a52ad53337767bb0ac684Fariborz Jahanian
103052a80e19ad688091723a52ad53337767bb0ac684Fariborz Jahanian  /// BuildBlockByrefAddress - Computes address location of the
103152a80e19ad688091723a52ad53337767bb0ac684Fariborz Jahanian  /// variable which is declared as __block.
103252a80e19ad688091723a52ad53337767bb0ac684Fariborz Jahanian  llvm::Value *BuildBlockByrefAddress(llvm::Value *BaseAddr,
103352a80e19ad688091723a52ad53337767bb0ac684Fariborz Jahanian                                      const VarDecl *V);
10347f02f721d48772e4eee4fccd8af4f800be1bbc1eChris Lattnerprivate:
1035d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  CGDebugInfo *DebugInfo;
1036aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel  bool DisableDebugInfo;
103709429b962811f2c0a7a81a1b0a89f96f5878b20cMike Stump
103893c332a8ba2c193c435b293966d343dab15f555bJohn McCall  /// DidCallStackSave - Whether llvm.stacksave has been called. Used to avoid
103993c332a8ba2c193c435b293966d343dab15f555bJohn McCall  /// calling llvm.stacksave for multiple VLAs in the same scope.
104093c332a8ba2c193c435b293966d343dab15f555bJohn McCall  bool DidCallStackSave;
104193c332a8ba2c193c435b293966d343dab15f555bJohn McCall
1042f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// IndirectBranch - The first time an indirect goto is seen we create a block
1043f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// with an indirect branch.  Every time we see the address of a label taken,
1044f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// we add the label to the indirect goto.  Every subsequent indirect goto is
1045f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// codegen'd as a jump to the IndirectBranch's basic block.
1046d9becd1846e2c72bf6ad283faa1b048f33dd3afeChris Lattner  llvm::IndirectBrInst *IndirectBranch;
10470ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar
10480dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// LocalDeclMap - This keeps track of the LLVM allocas or globals for local C
10490dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// decls.
10506b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  typedef llvm::DenseMap<const Decl*, llvm::Value*> DeclMapTy;
10516b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  DeclMapTy LocalDeclMap;
10525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// LabelMap - This keeps track of the LLVM basic block for each C label.
1054ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  llvm::DenseMap<const LabelDecl*, JumpDest> LabelMap;
10550dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
10560dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  // BreakContinueStack - This keeps track of where break and continue
1057e4b6d342c29d5cb9d311756100df1603810fa892Anders Carlsson  // statements should jump to.
1058da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner  struct BreakContinue {
1059f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    BreakContinue(JumpDest Break, JumpDest Continue)
1060f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      : BreakBlock(Break), ContinueBlock(Continue) {}
10610dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1062f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    JumpDest BreakBlock;
1063f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    JumpDest ContinueBlock;
10640dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  };
1065da13870e99fe33934b2122f06528a5063f78ae4cChris Lattner  llvm::SmallVector<BreakContinue, 8> BreakContinueStack;
106618ccc7776ad6288c5f630dc7967fb99d099c9cd9Daniel Dunbar
10670dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// SwitchInsn - This is nearest current switch instruction. It is null if if
10680dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// current context is not in a switch.
106951b09f2c528c8460b5465c676173324e44176d62Devang Patel  llvm::SwitchInst *SwitchInsn;
107051b09f2c528c8460b5465c676173324e44176d62Devang Patel
10710dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// CaseRangeBlock - This block holds if condition check for last case
107280fd5f96e6805ac43aab99faabd5b4c8b19551b7Devang Patel  /// statement range in current switch instruction.
1073c049e4f406a7f7179eba98659044a32508e53289Devang Patel  llvm::BasicBlock *CaseRangeBlock;
1074c049e4f406a7f7179eba98659044a32508e53289Devang Patel
107556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// OpaqueLValues - Keeps track of the current set of opaque value
1076e996ffd240f20a1048179d7727a6ee3227261921John McCall  /// expressions.
107756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  llvm::DenseMap<const OpaqueValueExpr *, LValue> OpaqueLValues;
107856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  llvm::DenseMap<const OpaqueValueExpr *, RValue> OpaqueRValues;
1079e996ffd240f20a1048179d7727a6ee3227261921John McCall
10800dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  // VLASizeMap - This keeps track of the associated size for each VLA type.
1081bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman  // We track this by the size expression rather than the type itself because
1082bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman  // in certain situations, like a const qualifier applied to an VLA typedef,
1083bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman  // multiple VLA types can share the same size expression.
10840dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  // FIXME: Maybe this could be a stack of maps that is pushed/popped as we
10850dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  // enter/leave scopes.
1086bbed6b964414145b29e7b60b3e538093734ea3f8Eli Friedman  llvm::DenseMap<const Expr*, llvm::Value*> VLASizeMap;
10870dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1088f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// A block containing a single 'unreachable' instruction.  Created
1089f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// lazily by getUnreachableBlock().
1090f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *UnreachableBlock;
10910dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1092f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson  /// CXXThisDecl - When generating code for a C++ member function,
1093f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson  /// this will hold the implicit 'this' declaration.
10942b77ba8bc7a842829ad9193816dc1d7d5e9c5be6Anders Carlsson  ImplicitParamDecl *CXXThisDecl;
10952504941793b549323f9d29c62507cf21d865fadeJohn McCall  llvm::Value *CXXThisValue;
10961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1097f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson  /// CXXVTTDecl - When generating code for a base object constructor or
1098f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson  /// base object destructor with virtual bases, this will hold the implicit
1099f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson  /// VTT parameter.
1100f6c56e2323c3f973253805a2f35629f3253ebed4Anders Carlsson  ImplicitParamDecl *CXXVTTDecl;
11012504941793b549323f9d29c62507cf21d865fadeJohn McCall  llvm::Value *CXXVTTValue;
11029cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1103150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// OutermostConditional - Points to the outermost active
1104150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// conditional control.  This is used so that we know if a
1105150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// temporary should be destroyed conditionally.
1106150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  ConditionalEvaluation *OutermostConditional;
11071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11087dfa4079f4b37507048b19bfc693d3b015d9dd11Anders Carlsson
11097dfa4079f4b37507048b19bfc693d3b015d9dd11Anders Carlsson  /// ByrefValueInfoMap - For each __block variable, contains a pair of the LLVM
11107dfa4079f4b37507048b19bfc693d3b015d9dd11Anders Carlsson  /// type as well as the field number that contains the actual data.
11119cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  llvm::DenseMap<const ValueDecl *, std::pair<const llvm::Type *,
11127dfa4079f4b37507048b19bfc693d3b015d9dd11Anders Carlsson                                              unsigned> > ByRefValueInfo;
11139cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1114f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *TerminateLandingPad;
1115182f383db1782af752ecaf607fdff72a8542088bMike Stump  llvm::BasicBlock *TerminateHandler;
111683252dcfe61aaebcb6bc117e71dc12968729513fChris Lattner  llvm::BasicBlock *TrapBB;
1117940670512d7c3d93389bb38a426abcb7ef44271eEli Friedman
11185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerpublic:
11195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  CodeGenFunction(CodeGenModule &cgm);
11200dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
11211e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall  CodeGenTypes &getTypes() const { return CGM.getTypes(); }
1122f2aac84709c418189e476ad591848dad50291885John McCall  ASTContext &getContext() const { return CGM.getContext(); }
1123aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel  CGDebugInfo *getDebugInfo() {
1124aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel    if (DisableDebugInfo)
1125aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel      return NULL;
1126aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel    return DebugInfo;
1127aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel  }
1128aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel  void disableDebugInfo() { DisableDebugInfo = true; }
1129aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel  void enableDebugInfo() { DisableDebugInfo = false; }
1130aa11289f754d220c9c155b68a4f84cdcfcefef6aDevang Patel
1131f85e193739c953358c865005855253af4f68a497John McCall  bool shouldUseFusedARCCalls() {
1132f85e193739c953358c865005855253af4f68a497John McCall    return CGM.getCodeGenOpts().OptimizationLevel == 0;
1133f85e193739c953358c865005855253af4f68a497John McCall  }
11345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1135d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  const LangOptions &getLangOptions() const { return CGM.getLangOptions(); }
1136d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall
1137f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// Returns a pointer to the function's exception object slot, which
1138f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// is assigned in every landing pad.
1139f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::Value *getExceptionSlot();
114093c332a8ba2c193c435b293966d343dab15f555bJohn McCall  llvm::Value *getEHSelectorSlot();
1141f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1142ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::Value *getNormalCleanupDestSlot();
1143ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall  llvm::Value *getEHCleanupDestSlot();
1144ff8e11579fc904aa4032d90d2be6ce1ac5fc9fe1John McCall
1145f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *getUnreachableBlock() {
1146f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    if (!UnreachableBlock) {
1147f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      UnreachableBlock = createBasicBlock("unreachable");
1148f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall      new llvm::UnreachableInst(getLLVMContext(), UnreachableBlock);
1149f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    }
1150f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return UnreachableBlock;
1151f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
1152f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1153f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *getInvokeDest() {
1154f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    if (!EHStack.requiresLandingPad()) return 0;
1155f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall    return getInvokeDestImpl();
1156f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  }
11579834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar
1158d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  llvm::LLVMContext &getLLVMContext() { return CGM.getLLVMContext(); }
115969243825cb5c91ec7207256aa57ae327cfaf8cb2Owen Anderson
11609834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar  //===--------------------------------------------------------------------===//
1161bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  //                                  Cleanups
1162bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  //===--------------------------------------------------------------------===//
1163bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1164bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  typedef void Destroyer(CodeGenFunction &CGF, llvm::Value *addr, QualType ty);
1165bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
11662673c68aa58e277ebc755b71d81aca618cdedbf9John McCall  void pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin,
11672673c68aa58e277ebc755b71d81aca618cdedbf9John McCall                                        llvm::Value *arrayEndPointer,
11682673c68aa58e277ebc755b71d81aca618cdedbf9John McCall                                        QualType elementType,
11692673c68aa58e277ebc755b71d81aca618cdedbf9John McCall                                        Destroyer &destroyer);
11702673c68aa58e277ebc755b71d81aca618cdedbf9John McCall  void pushRegularPartialArrayCleanup(llvm::Value *arrayBegin,
11712673c68aa58e277ebc755b71d81aca618cdedbf9John McCall                                      llvm::Value *arrayEnd,
11722673c68aa58e277ebc755b71d81aca618cdedbf9John McCall                                      QualType elementType,
11732673c68aa58e277ebc755b71d81aca618cdedbf9John McCall                                      Destroyer &destroyer);
1174bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
11759928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  void pushDestroy(QualType::DestructionKind dtorKind,
11769928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall                   llvm::Value *addr, QualType type);
1177bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  void pushDestroy(CleanupKind kind, llvm::Value *addr, QualType type,
11782673c68aa58e277ebc755b71d81aca618cdedbf9John McCall                   Destroyer &destroyer, bool useEHCleanupForArray);
11792673c68aa58e277ebc755b71d81aca618cdedbf9John McCall  void emitDestroy(llvm::Value *addr, QualType type, Destroyer &destroyer,
11802673c68aa58e277ebc755b71d81aca618cdedbf9John McCall                   bool useEHCleanupForArray);
1181bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  void emitArrayDestroy(llvm::Value *begin, llvm::Value *end,
11822673c68aa58e277ebc755b71d81aca618cdedbf9John McCall                        QualType type, Destroyer &destroyer,
11832673c68aa58e277ebc755b71d81aca618cdedbf9John McCall                        bool useEHCleanup);
1184bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
11859928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  Destroyer &getDestroyer(QualType::DestructionKind destructionKind);
11869928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall
1187bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  /// Determines whether an EH cleanup is required to destroy a type
1188bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  /// with the given destruction kind.
1189bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  bool needsEHCleanup(QualType::DestructionKind kind) {
1190bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    switch (kind) {
1191bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    case QualType::DK_none:
1192bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      return false;
1193bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    case QualType::DK_cxx_destructor:
1194bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    case QualType::DK_objc_weak_lifetime:
1195bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      return getLangOptions().Exceptions;
1196bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    case QualType::DK_objc_strong_lifetime:
1197bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall      return getLangOptions().Exceptions &&
1198bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall             CGM.getCodeGenOpts().ObjCAutoRefCountExceptions;
1199bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    }
1200bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall    llvm_unreachable("bad destruction kind");
1201bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  }
1202bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
12039928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  CleanupKind getCleanupKind(QualType::DestructionKind kind) {
12049928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall    return (needsEHCleanup(kind) ? NormalAndEHCleanup : NormalCleanup);
12059928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall  }
12069928c4805aa8d5fabd488d0d0c5aeb64fd50f0e3John McCall
1207bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  //===--------------------------------------------------------------------===//
12089834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar  //                                  Objective-C
12099834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar  //===--------------------------------------------------------------------===//
12109834ffbe54788239c8361d3cfe5826fd277ddfb2Daniel Dunbar
1211391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  void GenerateObjCMethod(const ObjCMethodDecl *OMD);
1212af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
12130dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  void StartObjCMethod(const ObjCMethodDecl *MD,
12148d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel                       const ObjCContainerDecl *CD,
12158d3f8979e46f9d0b8735566eabe471db0e1e0e53Devang Patel                       SourceLocation StartLoc);
1216af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
12170dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// GenerateObjCGetter - Synthesize an Objective-C property getter function.
1218fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian  void GenerateObjCGetter(ObjCImplementationDecl *IMP,
1219fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian                          const ObjCPropertyImplDecl *PID);
12202846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  void GenerateObjCGetterBody(ObjCIvarDecl *Ivar, bool IsAtomic, bool IsStrong);
12212846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian  void GenerateObjCAtomicSetterBody(ObjCMethodDecl *OMD,
12222846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian                                    ObjCIvarDecl *Ivar);
12232846b97965e980ad2e7c8d444b82cc21d733a699Fariborz Jahanian
1224109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian  void GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP,
1225109dfc6ca6652f60c55ed0f2631aebf323d0200dFariborz Jahanian                                  ObjCMethodDecl *MD, bool ctor);
1226af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
12270dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// GenerateObjCSetter - Synthesize an Objective-C property setter function
12280dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// for the given property.
1229fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian  void GenerateObjCSetter(ObjCImplementationDecl *IMP,
1230fef30b55230064d334a669a065a1c9acdb87cdfeFariborz Jahanian                          const ObjCPropertyImplDecl *PID);
12310b2bd47151ee9205ad6c66d1ffb921918106088aFariborz Jahanian  bool IndirectObjCSetterArg(const CGFunctionInfo &FI);
123215bd58842adaa4f8cca4e58047ed18e033858d9bFariborz Jahanian  bool IvarTypeWithAggrGCObjects(QualType Ty);
1233af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar
12344e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump  //===--------------------------------------------------------------------===//
12354e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump  //                                  Block Bits
12364e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump  //===--------------------------------------------------------------------===//
12374e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump
12386b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  llvm::Value *EmitBlockLiteral(const BlockExpr *);
12392a7eb28397148079cbc8e54e8a3871ef01c4f4bcBlaine Garst  llvm::Constant *BuildDescriptorBlockDecl(const BlockExpr *,
124089ecd41e0a6bfb3b0913dbe41c3c666340b308c7Fariborz Jahanian                                           const CGBlockInfo &Info,
12410892099dbc640720400a1d9decd2733a09d733e5Mike Stump                                           const llvm::StructType *,
1242d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall                                           llvm::Constant *BlockVarLayout);
12434e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump
1244564360be450b319aeafc26698be9811837bfb826Fariborz Jahanian  llvm::Function *GenerateBlockFunction(GlobalDecl GD,
12456b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                                        const CGBlockInfo &Info,
12466cc88f78fd36d3511b89412b193494b3e423cbffMike Stump                                        const Decl *OuterFuncDecl,
12476b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall                                        const DeclMapTy &ldm);
12484e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump
1249d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  llvm::Constant *GenerateCopyHelperFunction(const CGBlockInfo &blockInfo);
1250d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  llvm::Constant *GenerateDestroyHelperFunction(const CGBlockInfo &blockInfo);
1251d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall
1252d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  void BuildBlockRelease(llvm::Value *DeclPtr, BlockFieldFlags flags);
1253d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall
12545af02db5a48476e0748f135369663080eae87c64John McCall  class AutoVarEmission;
12555af02db5a48476e0748f135369663080eae87c64John McCall
12565af02db5a48476e0748f135369663080eae87c64John McCall  void emitByrefStructureInit(const AutoVarEmission &emission);
12575af02db5a48476e0748f135369663080eae87c64John McCall  void enterByrefCleanup(const AutoVarEmission &emission);
12585af02db5a48476e0748f135369663080eae87c64John McCall
12596b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  llvm::Value *LoadBlockStruct() {
12606b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    assert(BlockPointer && "no block pointer set!");
12616b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall    return BlockPointer;
12626b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  }
12634e7a1f7682d94811bd41fca8aefccc38f686db23Mike Stump
1264ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  void AllocateBlockCXXThisPointer(const CXXThisExpr *E);
1265ea1471e0e967548c596a71469702f8846dbaf3c0John McCall  void AllocateBlockDecl(const BlockDeclRefExpr *E);
1266ee5042903d53fa7b0fbc1902d0ea07d57c7775b1John McCall  llvm::Value *GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
1267ee5042903d53fa7b0fbc1902d0ea07d57c7775b1John McCall    return GetAddrOfBlockDecl(E->getDecl(), E->isByRef());
1268ee5042903d53fa7b0fbc1902d0ea07d57c7775b1John McCall  }
12696b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  llvm::Value *GetAddrOfBlockDecl(const VarDecl *var, bool ByRef);
12706b5a61b6dc400027fd793dcadceeb9da944a37eaJohn McCall  const llvm::Type *BuildByRefType(const VarDecl *var);
1271dab514fc30242c7afd6c03956e46136c400fb0d3Mike Stump
1272d26bc76c98006609002d9930f8840490e88ac5b5John McCall  void GenerateCode(GlobalDecl GD, llvm::Function *Fn,
1273d26bc76c98006609002d9930f8840490e88ac5b5John McCall                    const CGFunctionInfo &FnInfo);
12740ff8bafde95f6fa51ccea70738c1b99db870bddcAnders Carlsson  void StartFunction(GlobalDecl GD, QualType RetTy,
12757c086516f3cc9fba2733b1919973206c6ba4b171Daniel Dunbar                     llvm::Function *Fn,
1276d26bc76c98006609002d9930f8840490e88ac5b5John McCall                     const CGFunctionInfo &FnInfo,
12772284ac9ec80299fcdefae9a2787cf85105a0f203Daniel Dunbar                     const FunctionArgList &Args,
12789c6082fe89c61af697f017aa80937581cc2128d8Tilmann Scheller                     SourceLocation StartLoc);
1279a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar
12809fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  void EmitConstructorBody(FunctionArgList &Args);
12819fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  void EmitDestructorBody(FunctionArgList &Args);
12829fc6a7774643a810c8501dae2323e863fefb623eJohn McCall  void EmitFunctionBody(FunctionArgList &Args);
1283a355e07454463b19829ac92ffd115a097faff0e0John McCall
12840dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitReturnBlock - Emit the unified return block, trying to avoid its
12850dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// emission when possible.
12861c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar  void EmitReturnBlock();
12871c1d6074f5a0296dd273362655b1b8f9057289e3Daniel Dunbar
12880dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// FinishFunction - Complete IR generation of the current function. It is
12890dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// legal to call this function even if there is no current insertion point.
1290af05bb9073319d8381b71c4325188853fd4b8ed6Daniel Dunbar  void FinishFunction(SourceLocation EndLoc=SourceLocation());
129117b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
1292519c32880b99396034ecaedc0cd86db2e8485003Anders Carlsson  /// GenerateThunk - Generate a thunk for the given method.
1293d26bc76c98006609002d9930f8840490e88ac5b5John McCall  void GenerateThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo,
1294d26bc76c98006609002d9930f8840490e88ac5b5John McCall                     GlobalDecl GD, const ThunkInfo &Thunk);
12959cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
12967dcdf5ba9324a9577461eae302e88fdd52e310c5Eli Friedman  void GenerateVarArgsThunk(llvm::Function *Fn, const CGFunctionInfo &FnInfo,
12977dcdf5ba9324a9577461eae302e88fdd52e310c5Eli Friedman                            GlobalDecl GD, const ThunkInfo &Thunk);
12987dcdf5ba9324a9577461eae302e88fdd52e310c5Eli Friedman
1299fb8cc253420e93cee33d29df5a2bdae6aaf16e39Douglas Gregor  void EmitCtorPrologue(const CXXConstructorDecl *CD, CXXCtorType Type,
1300fb8cc253420e93cee33d29df5a2bdae6aaf16e39Douglas Gregor                        FunctionArgList &Args);
13011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1302d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson  /// InitializeVTablePointer - Initialize the vtable pointer of the given
1303d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson  /// subobject.
1304d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson  ///
13059cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  void InitializeVTablePointer(BaseSubobject Base,
1306b3b772ea15a4cd54879e244629aa685ead9548bbAnders Carlsson                               const CXXRecordDecl *NearestVBase,
1307d6fb21fa1053e5d616af55b181bb03c50d4b0d24Ken Dyck                               CharUnits OffsetFromNearestVBase,
1308d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson                               llvm::Constant *VTable,
1309d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson                               const CXXRecordDecl *VTableClass);
1310d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson
1311d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson  typedef llvm::SmallPtrSet<const CXXRecordDecl *, 4> VisitedVirtualBasesSetTy;
13129cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  void InitializeVTablePointers(BaseSubobject Base,
1313b3b772ea15a4cd54879e244629aa685ead9548bbAnders Carlsson                                const CXXRecordDecl *NearestVBase,
1314d6fb21fa1053e5d616af55b181bb03c50d4b0d24Ken Dyck                                CharUnits OffsetFromNearestVBase,
1315603d6d12cc4cba1ef219a3d55c698c7c87f87adfAnders Carlsson                                bool BaseIsNonVirtualPrimaryBase,
1316603d6d12cc4cba1ef219a3d55c698c7c87f87adfAnders Carlsson                                llvm::Constant *VTable,
1317603d6d12cc4cba1ef219a3d55c698c7c87f87adfAnders Carlsson                                const CXXRecordDecl *VTableClass,
1318603d6d12cc4cba1ef219a3d55c698c7c87f87adfAnders Carlsson                                VisitedVirtualBasesSetTy& VBases);
131977a259c6dbf582949af8a243b472c0c7faa373a6Eli Friedman
1320603d6d12cc4cba1ef219a3d55c698c7c87f87adfAnders Carlsson  void InitializeVTablePointers(const CXXRecordDecl *ClassDecl);
1321d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson
1322043fb9a1fc0609285f60f0f87e5a18195408f34cDan Gohman  /// GetVTablePtr - Return the Value of the vtable pointer member pointed
1323043fb9a1fc0609285f60f0f87e5a18195408f34cDan Gohman  /// to by This.
1324043fb9a1fc0609285f60f0f87e5a18195408f34cDan Gohman  llvm::Value *GetVTablePtr(llvm::Value *This, const llvm::Type *Ty);
1325d103f9f9b401b419e756f8c1849683cd77586067Anders Carlsson
132650da2cadcc6da86abff6772de65280ace2cabc94John McCall  /// EnterDtorCleanups - Enter the cleanups necessary to complete the
132750da2cadcc6da86abff6772de65280ace2cabc94John McCall  /// given phase of destruction for a destructor.  The end result
132850da2cadcc6da86abff6772de65280ace2cabc94John McCall  /// should call destructors on members and base classes in reverse
132950da2cadcc6da86abff6772de65280ace2cabc94John McCall  /// order of their construction.
133050da2cadcc6da86abff6772de65280ace2cabc94John McCall  void EnterDtorCleanups(const CXXDestructorDecl *Dtor, CXXDtorType Type);
13311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13327255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  /// ShouldInstrumentFunction - Return true if the current function should be
13337255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  /// instrumented with __cyg_profile_func_* calls
13347255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  bool ShouldInstrumentFunction();
13357255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
13367255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  /// EmitFunctionInstrumentation - Emit LLVM code to call the specified
13377255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  /// instrumentation function with the current function and the call site, if
13387255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  /// function instrumentation is enabled.
13397255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner  void EmitFunctionInstrumentation(const char *Fn);
13407255a2d997b15beae82e627052fdb1b2474495c2Chris Lattner
1341be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky  /// EmitMCountInstrumentation - Emit call to .mcount.
1342be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky  void EmitMCountInstrumentation();
1343be4c8705e499b55548467eb7adaa23cbc6edfef9Roman Divacky
13440dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitFunctionProlog - Emit the target specific LLVM code to load the
13450dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// arguments for the given function. This is also responsible for naming the
13460dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// LLVM function arguments.
134788b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  void EmitFunctionProlog(const CGFunctionInfo &FI,
134888b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar                          llvm::Function *Fn,
134917b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar                          const FunctionArgList &Args);
135017b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
13510dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitFunctionEpilog - Emit the target specific LLVM code to return the
13520dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// given temporary.
135335b21b884e5c3447a52a74d7ffaba966b07ac81fChris Lattner  void EmitFunctionEpilog(const CGFunctionInfo &FI);
135417b708d61827cd86278e9580b041dd6cbadf07d3Daniel Dunbar
1355cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  /// EmitStartEHSpec - Emit the start of the exception spec.
1356cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  void EmitStartEHSpec(const Decl *D);
1357cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump
1358cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  /// EmitEndEHSpec - Emit the end of the exception spec.
1359cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump  void EmitEndEHSpec(const Decl *D);
1360cce3d4f9812182ed4e551b7cf0fc86576be8d9c5Mike Stump
1361f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// getTerminateLandingPad - Return a landing pad that just calls terminate.
1362f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::BasicBlock *getTerminateLandingPad();
1363f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1364f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// getTerminateHandler - Return a handler (not a landing pad, just
1365f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// a catch handler) that just calls terminate.  This is used when
1366f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  /// a terminate scope encloses a try.
13679b39c51ae3c547568ac42325f94b4197618f6b18Mike Stump  llvm::BasicBlock *getTerminateHandler();
13689b39c51ae3c547568ac42325f94b4197618f6b18Mike Stump
13699cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ConvertTypeForMem(QualType T);
13709cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ConvertType(QualType T);
13719cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner  llvm::Type *ConvertType(const TypeDecl *T) {
1372bff225ecf77fb891596ecb1b27196310d268365eJohn McCall    return ConvertType(getContext().getTypeDeclType(T));
1373bff225ecf77fb891596ecb1b27196310d268365eJohn McCall  }
1374c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner
13750dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// LoadObjCSelf - Load the value of self. This function is only valid while
13760dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// generating code for an Objective-C method.
1377c8aa5f1f264fb230c38182adab944232bb160c2bChris Lattner  llvm::Value *LoadObjCSelf();
13780dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
13790dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// TypeOfSelfObject - Return type of object that this self represents.
138045012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian  QualType TypeOfSelfObject();
13814111024be81e7c0525e42dadcc126d27e5bf2425Chris Lattner
13825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// hasAggregateLLVMType - Return true if the specified AST type will map into
13835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// an aggregate LLVM type or is void.
13845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  static bool hasAggregateLLVMType(QualType T);
138555e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar
138655e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  /// createBasicBlock - Create an LLVM basic block.
1387d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  llvm::BasicBlock *createBasicBlock(llvm::StringRef name = "",
1388d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall                                     llvm::Function *parent = 0,
1389d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall                                     llvm::BasicBlock *before = 0) {
139029ea6729155c759836be4b5453bee9ef8e1e97eeDaniel Dunbar#ifdef NDEBUG
1391d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall    return llvm::BasicBlock::Create(getLLVMContext(), "", parent, before);
139229ea6729155c759836be4b5453bee9ef8e1e97eeDaniel Dunbar#else
1393d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall    return llvm::BasicBlock::Create(getLLVMContext(), name, parent, before);
139429ea6729155c759836be4b5453bee9ef8e1e97eeDaniel Dunbar#endif
139555e874299f2ad827646a4ca9ea38c402aaeb38c9Daniel Dunbar  }
13960dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
13975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// getBasicBlockForLabel - Return the LLVM basicblock that the specified
13985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// label maps to.
1399ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  JumpDest getJumpDestForLabel(const LabelDecl *S);
14000dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1401f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// SimplifyForwardingBlocks - If the given basic block is only a branch to
1402f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// another basic block, simplify it. This assumes that no other code could
1403f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// potentially reference the basic block.
1404aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar  void SimplifyForwardingBlocks(llvm::BasicBlock *BB);
1405aa5bd87f1fd5f9ca47924248817c89325759b30eDaniel Dunbar
14060dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitBlock - Emit the given block \arg BB and set it as the insert point,
14070dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// adding a fall-through branch from the current insert block if
14080dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// necessary. It is legal to call this function even if there is no current
14090dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// insertion point.
1410a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar  ///
14110dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// IsFinished - If true, indicates that the caller has finished emitting
14120dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// branches to the given block and does not expect to emit code into it. This
14130dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// means the block can be ignored if it is unreachable.
1414a0c21a8faa79e88ac432d116eca58f7a7217195dDaniel Dunbar  void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false);
1415824e3bd76b2e32db2ec6e4d2d113413e518d1c63Daniel Dunbar
14160dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitBranch - Emit a branch to the specified basic block from the current
14170dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// insert block, taking care to avoid creation of branches from dummy
14180dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// blocks. It is legal to call this function even if there is no current
14190dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// insertion point.
14205e08ad3cc62ab94649959ae227a9a411a729bf49Daniel Dunbar  ///
14210dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// This function clears the current insertion point. The caller should follow
14220dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// calls to this function with calls to Emit*Block prior to generation new
14230dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// code.
1424d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar  void EmitBranch(llvm::BasicBlock *Block);
1425d57a871339c7c98d58d93108b806f59bdf4e13e2Daniel Dunbar
14260dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// HaveInsertPoint - True if an insertion point is defined. If not, this
14270dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// indicates that the current code being emitted is unreachable.
14280dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  bool HaveInsertPoint() const {
1429a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar    return Builder.GetInsertBlock() != 0;
1430a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar  }
1431a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar
14320dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EnsureInsertPoint - Ensure that an insertion point is defined so that
14330dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// emitted IR has a place to go. Note that by definition, if this function
14340dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// creates a block then that block is unreachable; callers may do better to
14350dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// detect when no insertion point is defined and simply skip IR generation.
1436a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar  void EnsureInsertPoint() {
1437a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar    if (!HaveInsertPoint())
1438a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar      EmitBlock(createBasicBlock());
1439a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar  }
14400dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1441488e993a135ce700b982bf099c3d6b856301d642Daniel Dunbar  /// ErrorUnsupported - Print out an error that codegen doesn't support the
1442dc5e8268292046114ffe02e48773572a91a310f1Chris Lattner  /// specified stmt yet.
144390df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar  void ErrorUnsupported(const Stmt *S, const char *Type,
144490df4b6661968a84bf64baee489bb2f6d948fcc1Daniel Dunbar                        bool OmitOnError=false);
14455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
14475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //                                  Helpers
14485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
14490dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1450983e3d7dac99aed376939f1a4daadef98e09c3b9Daniel Dunbar  LValue MakeAddrLValue(llvm::Value *V, QualType T, unsigned Alignment = 0) {
14513d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman    return LValue::MakeAddr(V, T, Alignment, getContext(),
14523d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman                            CGM.getTBAAInfo(T));
14535cf8bfec711116b3c4acc70a00717b2e119e7550Daniel Dunbar  }
14545cf8bfec711116b3c4acc70a00717b2e119e7550Daniel Dunbar
14555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// CreateTempAlloca - This creates a alloca and inserts it into the entry
1456195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar  /// block. The caller is responsible for setting an appropriate alignment on
1457195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar  /// the alloca.
14585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::AllocaInst *CreateTempAlloca(const llvm::Type *Ty,
1459259e9ccf882d11491ad149aec5e6d7a061c9f938Daniel Dunbar                                     const llvm::Twine &Name = "tmp");
14600dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1461ac418162692a951ca3796d6830496a85a2d12493John McCall  /// InitTempAlloca - Provide an initial value for the given alloca.
1462ac418162692a951ca3796d6830496a85a2d12493John McCall  void InitTempAlloca(llvm::AllocaInst *Alloca, llvm::Value *Value);
1463ac418162692a951ca3796d6830496a85a2d12493John McCall
14649bd4da2cfa8b956b851200db8f9a37e175edbb15Daniel Dunbar  /// CreateIRTemp - Create a temporary IR object of the given type, with
14659bd4da2cfa8b956b851200db8f9a37e175edbb15Daniel Dunbar  /// appropriate alignment. This routine should only be used when an temporary
14669bd4da2cfa8b956b851200db8f9a37e175edbb15Daniel Dunbar  /// value needs to be stored into an alloca (for example, to avoid explicit
14679bd4da2cfa8b956b851200db8f9a37e175edbb15Daniel Dunbar  /// PHI construction), but the type is the IR type, not the type appropriate
14689bd4da2cfa8b956b851200db8f9a37e175edbb15Daniel Dunbar  /// for storing in memory.
1469121b3facb4e0585d23766f9c1e4fdf9018a4b217Chris Lattner  llvm::AllocaInst *CreateIRTemp(QualType T, const llvm::Twine &Name = "tmp");
14709bd4da2cfa8b956b851200db8f9a37e175edbb15Daniel Dunbar
1471195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar  /// CreateMemTemp - Create a temporary memory object of the given type, with
1472195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar  /// appropriate alignment.
1473121b3facb4e0585d23766f9c1e4fdf9018a4b217Chris Lattner  llvm::AllocaInst *CreateMemTemp(QualType T, const llvm::Twine &Name = "tmp");
1474195337d2e5d4625ae9dc1328c7cdbc7115b0261bDaniel Dunbar
1475558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  /// CreateAggTemp - Create a temporary memory object for the given
1476558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  /// aggregate type.
1477558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  AggValueSlot CreateAggTemp(QualType T, const llvm::Twine &Name = "tmp") {
1478f85e193739c953358c865005855253af4f68a497John McCall    return AggValueSlot::forAddr(CreateMemTemp(T, Name), T.getQualifiers(),
1479f85e193739c953358c865005855253af4f68a497John McCall                                 false);
1480558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  }
1481558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall
1482d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  /// Emit a cast to void* in the appropriate address space.
1483d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall  llvm::Value *EmitCastToVoidPtr(llvm::Value *value);
1484d16c2cf1cafa413709aa487cbbd5dc392f1ba1ffJohn McCall
14855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EvaluateExprAsBool - Perform the usual unary conversions on the specified
14865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// expression and compare the result against zero, returning an Int1Ty value.
14875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::Value *EvaluateExprAsBool(const Expr *E);
14885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14892a41637a995affa1563f4d82a8b026e326a2faa0John McCall  /// EmitIgnoredExpr - Emit an expression in a context which ignores the result.
14902a41637a995affa1563f4d82a8b026e326a2faa0John McCall  void EmitIgnoredExpr(const Expr *E);
14912a41637a995affa1563f4d82a8b026e326a2faa0John McCall
14929b65551d0b387a7597fb39356a4d8ef10046445eChris Lattner  /// EmitAnyExpr - Emit code to compute the specified expression which can have
14939b65551d0b387a7597fb39356a4d8ef10046445eChris Lattner  /// any type.  The result is returned as an RValue struct.  If this is an
14949b65551d0b387a7597fb39356a4d8ef10046445eChris Lattner  /// aggregate expression, the aggloc/agglocvolatile arguments indicate where
14959b65551d0b387a7597fb39356a4d8ef10046445eChris Lattner  /// the result should be returned.
149649d1cd5a09ed3df353371fd7f206674a85e0fb45Mike Stump  ///
149749d1cd5a09ed3df353371fd7f206674a85e0fb45Mike Stump  /// \param IgnoreResult - True if the resulting value isn't used.
1498558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  RValue EmitAnyExpr(const Expr *E,
1499558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall                     AggValueSlot AggSlot = AggValueSlot::ignored(),
1500558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall                     bool IgnoreResult = false);
1501d9363c3a80168283b3da518b4e17f545a6246857Devang Patel
15020dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  // EmitVAListRef - Emit a "reference" to a va_list; this is either the address
15030dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  // or the value of the expression, depending on how va_list is defined.
15044fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman  llvm::Value *EmitVAListRef(const Expr *E);
15054fd0aa5803357d8c72eeac2cae15e12649ea08feEli Friedman
15060dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
15070dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// always be accessible even if no aggregate location is provided.
1508558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  RValue EmitAnyExprToTemp(const Expr *E);
150946f45b9bec4a265ad8400a538e5ec3a5683617f1Daniel Dunbar
151060d3365b46eb826fba44483583c0051ac5c41fe3John McCall  /// EmitAnyExprToMem - Emits the code necessary to evaluate an
15113d3ec1c099ec8bfac3aa1fb0126fe515b7c7fa05John McCall  /// arbitrary expression into the given memory location.
15123d3ec1c099ec8bfac3aa1fb0126fe515b7c7fa05John McCall  void EmitAnyExprToMem(const Expr *E, llvm::Value *Location,
1513f85e193739c953358c865005855253af4f68a497John McCall                        Qualifiers Quals, bool IsInitializer);
15143d3ec1c099ec8bfac3aa1fb0126fe515b7c7fa05John McCall
151560d3365b46eb826fba44483583c0051ac5c41fe3John McCall  /// EmitExprAsInit - Emits the code necessary to initialize a
151660d3365b46eb826fba44483583c0051ac5c41fe3John McCall  /// location in memory with the given initializer.
1517f85e193739c953358c865005855253af4f68a497John McCall  void EmitExprAsInit(const Expr *init, const ValueDecl *D,
1518a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall                      LValue lvalue, bool capturedByInit);
151960d3365b46eb826fba44483583c0051ac5c41fe3John McCall
152027fe2e6c97a3782a0639d87b460741e8ba5d076dMike Stump  /// EmitAggregateCopy - Emit an aggrate copy.
152127fe2e6c97a3782a0639d87b460741e8ba5d076dMike Stump  ///
152227fe2e6c97a3782a0639d87b460741e8ba5d076dMike Stump  /// \param isVolatile - True iff either the source or the destination is
152327fe2e6c97a3782a0639d87b460741e8ba5d076dMike Stump  /// volatile.
15247482d12c345c6391f8956850545e2d4aa7701ce6Daniel Dunbar  void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
152527fe2e6c97a3782a0639d87b460741e8ba5d076dMike Stump                         QualType EltTy, bool isVolatile=false);
15267482d12c345c6391f8956850545e2d4aa7701ce6Daniel Dunbar
152751b09f2c528c8460b5465c676173324e44176d62Devang Patel  /// StartBlock - Start new block named N. If insert block is a dummy block
152851b09f2c528c8460b5465c676173324e44176d62Devang Patel  /// then reuse it.
152951b09f2c528c8460b5465c676173324e44176d62Devang Patel  void StartBlock(const char *N);
153051b09f2c528c8460b5465c676173324e44176d62Devang Patel
1531813733577d33ec56479667b49e1bff42dc6bba90Lauro Ramos Venancio  /// GetAddrOfStaticLocalVar - Return the address of a static local variable.
15324c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  llvm::Constant *GetAddrOfStaticLocalVar(const VarDecl *BVD) {
15334c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    return cast<llvm::Constant>(GetAddrOfLocalVar(BVD));
15344c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  }
15354f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman
1536dde0a94120915fa925d1ffcdb997c7b44dc9fa21Anders Carlsson  /// GetAddrOfLocalVar - Return the address of a local variable.
15374c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  llvm::Value *GetAddrOfLocalVar(const VarDecl *VD) {
15384c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    llvm::Value *Res = LocalDeclMap[VD];
15394c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    assert(Res && "Invalid argument to GetAddrOfLocalVar(), no decl!");
15404c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall    return Res;
15414c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall  }
15420dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
154356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// getOpaqueLValueMapping - Given an opaque value expression (which
154456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// must be mapped to an l-value), return its mapping.
154556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  const LValue &getOpaqueLValueMapping(const OpaqueValueExpr *e) {
154656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    assert(OpaqueValueMapping::shouldBindAsLValue(e));
154756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
154856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    llvm::DenseMap<const OpaqueValueExpr*,LValue>::iterator
154956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      it = OpaqueLValues.find(e);
155056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    assert(it != OpaqueLValues.end() && "no mapping for opaque value!");
155156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    return it->second;
155256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  }
155356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
155456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// getOpaqueRValueMapping - Given an opaque value expression (which
155556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// must be mapped to an r-value), return its mapping.
155656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  const RValue &getOpaqueRValueMapping(const OpaqueValueExpr *e) {
155756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    assert(!OpaqueValueMapping::shouldBindAsLValue(e));
155856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
155956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    llvm::DenseMap<const OpaqueValueExpr*,RValue>::iterator
156056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall      it = OpaqueRValues.find(e);
156156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall    assert(it != OpaqueRValues.end() && "no mapping for opaque value!");
1562e996ffd240f20a1048179d7727a6ee3227261921John McCall    return it->second;
1563e996ffd240f20a1048179d7727a6ee3227261921John McCall  }
1564e996ffd240f20a1048179d7727a6ee3227261921John McCall
15654f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman  /// getAccessedFieldNo - Given an encoded value and a result number, return
15664f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman  /// the input field number being accessed.
15674f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman  static unsigned getAccessedFieldNo(unsigned Idx, const llvm::Constant *Elts);
15684f8d123e3e2c260de3377208106ddba87cee28b4Dan Gohman
1569ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  llvm::BlockAddress *GetAddrOfLabel(const LabelDecl *L);
15703d00fdc82fd550ae4bfbb2e700a1fc85bbd6d6fdChris Lattner  llvm::BasicBlock *GetIndirectGotoBlock();
15710ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar
15721884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  /// EmitNullInitialization - Generate code to set a value of the given type to
15731884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  /// null, If the type contains data member pointers, they will be initialized
15741884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  /// to -1 in accordance with the Itanium C++ ABI.
15751884eb0b5c55edda4893ddec45e7dbad79758782Anders Carlsson  void EmitNullInitialization(llvm::Value *DestPtr, QualType Ty);
1576ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson
1577ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson  // EmitVAArg - Generate code to get an argument from the passed in pointer
1578ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson  // and update it accordingly. The return value is a pointer to the argument.
1579ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson  // FIXME: We should be able to get rid of this method and use the va_arg
15800dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  // instruction in LLVM instead once it works well enough.
1581ddf7cac45d85b73127adbbd91a2b28fc7291c57eAnders Carlsson  llvm::Value *EmitVAArg(llvm::Value *VAListAddr, QualType Ty);
1582f666b7780d04186521adcaedb0e15dfa4d5e6933Anders Carlsson
1583bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  /// emitArrayLength - Compute the length of an array, even if it's a
1584bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  /// VLA, and drill down to the base element type.
1585bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  llvm::Value *emitArrayLength(const ArrayType *arrayType,
1586bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                               QualType &baseType,
1587bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                               llvm::Value *&addr);
1588bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
1589bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  /// EmitVLASize - Capture all the sizes for the VLA expressions in
1590bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  /// the given variably-modified type and store them in the VLASizeMap.
1591d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  ///
1592d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  /// This function can be called with a null (unreachable) insert point.
1593bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  void EmitVariablyModifiedType(QualType Ty);
1594bc8d40d85f3fa1e34569834916f18fecaa635152John McCall
1595bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  /// getVLASize - Returns an LLVM value that corresponds to the size,
1596bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  /// in non-variably-sized elements, of a variable length array type,
1597bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  /// plus that largest non-variably-sized element type.  Assumes that
1598bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  /// the type has already been emitted with EmitVariablyModifiedType.
1599bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  std::pair<llvm::Value*,QualType> getVLASize(const VariableArrayType *vla);
1600bc8d40d85f3fa1e34569834916f18fecaa635152John McCall  std::pair<llvm::Value*,QualType> getVLASize(QualType vla);
1601dcc90d87e6430c643b4311ae5b0089535bca41f7Anders Carlsson
16025f4307b7ba164b03c853c8d3eb4674d33f8967a6Anders Carlsson  /// LoadCXXThis - Load the value of 'this'. This function is only valid while
16035f4307b7ba164b03c853c8d3eb4674d33f8967a6Anders Carlsson  /// generating code for an C++ member function.
16042504941793b549323f9d29c62507cf21d865fadeJohn McCall  llvm::Value *LoadCXXThis() {
16052504941793b549323f9d29c62507cf21d865fadeJohn McCall    assert(CXXThisValue && "no 'this' value for this function");
16062504941793b549323f9d29c62507cf21d865fadeJohn McCall    return CXXThisValue;
16072504941793b549323f9d29c62507cf21d865fadeJohn McCall  }
16081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1609c997d4278d329e18891aac9698fb991b2d4622ebAnders Carlsson  /// LoadCXXVTT - Load the VTT parameter to base constructors/destructors have
1610c997d4278d329e18891aac9698fb991b2d4622ebAnders Carlsson  /// virtual bases.
16112504941793b549323f9d29c62507cf21d865fadeJohn McCall  llvm::Value *LoadCXXVTT() {
16122504941793b549323f9d29c62507cf21d865fadeJohn McCall    assert(CXXVTTValue && "no VTT value for this function");
16132504941793b549323f9d29c62507cf21d865fadeJohn McCall    return CXXVTTValue;
16142504941793b549323f9d29c62507cf21d865fadeJohn McCall  }
1615bff225ecf77fb891596ecb1b27196310d268365eJohn McCall
1616bff225ecf77fb891596ecb1b27196310d268365eJohn McCall  /// GetAddressOfBaseOfCompleteClass - Convert the given pointer to a
16178561a8666c70f924c8f0209c41b9b77bbbf90607Anders Carlsson  /// complete class to the given direct base.
16188561a8666c70f924c8f0209c41b9b77bbbf90607Anders Carlsson  llvm::Value *
16198561a8666c70f924c8f0209c41b9b77bbbf90607Anders Carlsson  GetAddressOfDirectBaseInCompleteClass(llvm::Value *Value,
16208561a8666c70f924c8f0209c41b9b77bbbf90607Anders Carlsson                                        const CXXRecordDecl *Derived,
16218561a8666c70f924c8f0209c41b9b77bbbf90607Anders Carlsson                                        const CXXRecordDecl *Base,
16228561a8666c70f924c8f0209c41b9b77bbbf90607Anders Carlsson                                        bool BaseIsVirtual);
1623a88ad5618fcb07c5374c79d6781a33234984b039Anders Carlsson
1624f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// GetAddressOfBaseClass - This function will add the necessary delta to the
1625f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// load of 'this' and returns address of the base class.
16269cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  llvm::Value *GetAddressOfBaseClass(llvm::Value *Value,
16278561a8666c70f924c8f0209c41b9b77bbbf90607Anders Carlsson                                     const CXXRecordDecl *Derived,
1628f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                     CastExpr::path_const_iterator PathBegin,
1629f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                     CastExpr::path_const_iterator PathEnd,
163034a2d384c745ebc39cae45dc1c0c4a6a7012e09bAnders Carlsson                                     bool NullCheckValue);
163134a2d384c745ebc39cae45dc1c0c4a6a7012e09bAnders Carlsson
1632a3697c9c155bda93fd2802f37084b620f4738822Anders Carlsson  llvm::Value *GetAddressOfDerivedClass(llvm::Value *Value,
16338561a8666c70f924c8f0209c41b9b77bbbf90607Anders Carlsson                                        const CXXRecordDecl *Derived,
1634f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                        CastExpr::path_const_iterator PathBegin,
1635f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                        CastExpr::path_const_iterator PathEnd,
16365a0f49ebc83e7fe0da07b9964c44b0a7fae270cbAnders Carlsson                                        bool NullCheckValue);
1637a3697c9c155bda93fd2802f37084b620f4738822Anders Carlsson
1638bb7e17b52ffaa4097b4c4d7935746d23539ffe2aAnders Carlsson  llvm::Value *GetVirtualBaseClassOffset(llvm::Value *This,
1639bb7e17b52ffaa4097b4c4d7935746d23539ffe2aAnders Carlsson                                         const CXXRecordDecl *ClassDecl,
1640bb7e17b52ffaa4097b4c4d7935746d23539ffe2aAnders Carlsson                                         const CXXRecordDecl *BaseClassDecl);
16419cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1642c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall  void EmitDelegateCXXConstructorCall(const CXXConstructorDecl *Ctor,
1643c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall                                      CXXCtorType CtorType,
1644c0bf462cf35fe050bddbd8bff967298e4a67e79dJohn McCall                                      const FunctionArgList &Args);
1645059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt  // It's important not to confuse this and the previous function. Delegating
1646059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt  // constructors are the C++0x feature. The constructor delegate optimization
1647059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt  // is used to reduce duplication in the base and complete consturctors where
1648059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt  // they are substantially the same.
1649059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt  void EmitDelegatingCXXConstructorCall(const CXXConstructorDecl *Ctor,
1650059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt                                        const FunctionArgList &Args);
1651155ed4a23366f4514befb1c9f5f89d16f8b8b6dbAnders Carlsson  void EmitCXXConstructorCall(const CXXConstructorDecl *D, CXXCtorType Type,
1652155ed4a23366f4514befb1c9f5f89d16f8b8b6dbAnders Carlsson                              bool ForVirtualBase, llvm::Value *This,
1653b14095aa98c6fedd3625920c4ce834bcaf24d9f7Anders Carlsson                              CallExpr::const_arg_iterator ArgBeg,
1654b14095aa98c6fedd3625920c4ce834bcaf24d9f7Anders Carlsson                              CallExpr::const_arg_iterator ArgEnd);
165534999876e215b22febc240b1a6dc054215d12f9cFariborz Jahanian
165634999876e215b22febc240b1a6dc054215d12f9cFariborz Jahanian  void EmitSynthesizedCXXCopyCtorCall(const CXXConstructorDecl *D,
165734999876e215b22febc240b1a6dc054215d12f9cFariborz Jahanian                              llvm::Value *This, llvm::Value *Src,
165834999876e215b22febc240b1a6dc054215d12f9cFariborz Jahanian                              CallExpr::const_arg_iterator ArgBeg,
165934999876e215b22febc240b1a6dc054215d12f9cFariborz Jahanian                              CallExpr::const_arg_iterator ArgEnd);
16601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1661288dcaf329c49b01dacd5c1dd9f35609555feecdFariborz Jahanian  void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
1662569c1f4a6c703eaa8b963bed7d5c2fd5d4569e93Anders Carlsson                                  const ConstantArrayType *ArrayTy,
16635d4d946ec2d88696fd8422aeb64dc29688e6a2c1Anders Carlsson                                  llvm::Value *ArrayPtr,
16645d4d946ec2d88696fd8422aeb64dc29688e6a2c1Anders Carlsson                                  CallExpr::const_arg_iterator ArgBeg,
166559174c0633fb5cde41735cfbff5744bdf837e8d9Douglas Gregor                                  CallExpr::const_arg_iterator ArgEnd,
166659174c0633fb5cde41735cfbff5744bdf837e8d9Douglas Gregor                                  bool ZeroInitialization = false);
16679cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1668569c1f4a6c703eaa8b963bed7d5c2fd5d4569e93Anders Carlsson  void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
1669569c1f4a6c703eaa8b963bed7d5c2fd5d4569e93Anders Carlsson                                  llvm::Value *NumElements,
16705d4d946ec2d88696fd8422aeb64dc29688e6a2c1Anders Carlsson                                  llvm::Value *ArrayPtr,
16715d4d946ec2d88696fd8422aeb64dc29688e6a2c1Anders Carlsson                                  CallExpr::const_arg_iterator ArgBeg,
167259174c0633fb5cde41735cfbff5744bdf837e8d9Douglas Gregor                                  CallExpr::const_arg_iterator ArgEnd,
167359174c0633fb5cde41735cfbff5744bdf837e8d9Douglas Gregor                                  bool ZeroInitialization = false);
1674b14095aa98c6fedd3625920c4ce834bcaf24d9f7Anders Carlsson
1675f800f6c09ed4a71bcb593d6962e0fda2c2845a70Fariborz Jahanian  void EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
1676f800f6c09ed4a71bcb593d6962e0fda2c2845a70Fariborz Jahanian                                 const ArrayType *Array,
1677f800f6c09ed4a71bcb593d6962e0fda2c2845a70Fariborz Jahanian                                 llvm::Value *This);
16781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1679bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  static Destroyer destroyCXXObject;
1680bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
168172c2153b466263a21870b2b1c817900d4981493eFariborz Jahanian  void EmitCXXAggrDestructorCall(const CXXDestructorDecl *D,
168272c2153b466263a21870b2b1c817900d4981493eFariborz Jahanian                                 llvm::Value *NumElements,
168372c2153b466263a21870b2b1c817900d4981493eFariborz Jahanian                                 llvm::Value *This);
168472c2153b466263a21870b2b1c817900d4981493eFariborz Jahanian
168502e370a02f05b4a734fe5e8c88efc4ed9dac60faAnders Carlsson  llvm::Function *GenerateCXXAggrDestructorHelper(const CXXDestructorDecl *D,
168602e370a02f05b4a734fe5e8c88efc4ed9dac60faAnders Carlsson                                                  const ArrayType *Array,
168702e370a02f05b4a734fe5e8c88efc4ed9dac60faAnders Carlsson                                                  llvm::Value *This);
168888f4280f6b60edcbdea6a38a3da8b17d66473ad6Fariborz Jahanian
16897267c1693abe7875b0c57268be05005ae013c6c9Anders Carlsson  void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type,
16908e6404ca28d6bbb76e97ea2a53a74816c2a74665Anders Carlsson                             bool ForVirtualBase, llvm::Value *This);
16919cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
1692ef66872797e46ced3ffdb428e582bc7f00474f99Fariborz Jahanian  void EmitNewArrayInitializer(const CXXNewExpr *E, llvm::Value *NewPtr,
1693ef66872797e46ced3ffdb428e582bc7f00474f99Fariborz Jahanian                               llvm::Value *NumElements);
16941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1695f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  void EmitCXXTemporary(const CXXTemporary *Temporary, llvm::Value *Ptr);
16961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1697a00703dccbdc6cc4a293db38477dea7db5538c7eAnders Carlsson  llvm::Value *EmitCXXNewExpr(const CXXNewExpr *E);
169860e282cc1e508be327b0481cecedc206873cb86aAnders Carlsson  void EmitCXXDeleteExpr(const CXXDeleteExpr *E);
16991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17004bf8152799ec41796f989ea1b94f6f1bac349117Eli Friedman  void EmitDeleteCall(const FunctionDecl *DeleteFD, llvm::Value *Ptr,
17014bf8152799ec41796f989ea1b94f6f1bac349117Eli Friedman                      QualType DeleteTy);
17024bf8152799ec41796f989ea1b94f6f1bac349117Eli Friedman
1703c2e84ae9a6d25cea3e583c768e576b4c981ec028Mike Stump  llvm::Value* EmitCXXTypeidExpr(const CXXTypeidExpr *E);
1704c849c052d6b4b70f2651c1969531861a5f230053Mike Stump  llvm::Value *EmitDynamicCast(llvm::Value *V, const CXXDynamicCastExpr *DCE);
1705c2e84ae9a6d25cea3e583c768e576b4c981ec028Mike Stump
1706b14e62d5aee7522b98c410ee65bd750c2cfe6f01Mike Stump  void EmitCheck(llvm::Value *, unsigned Size);
1707b14e62d5aee7522b98c410ee65bd750c2cfe6f01Mike Stump
1708dd36d328730d8c02915d0884541b45ec6ca0049dChris Lattner  llvm::Value *EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
1709dd36d328730d8c02915d0884541b45ec6ca0049dChris Lattner                                       bool isInc, bool isPre);
1710dd36d328730d8c02915d0884541b45ec6ca0049dChris Lattner  ComplexPairTy EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
1711dd36d328730d8c02915d0884541b45ec6ca0049dChris Lattner                                         bool isInc, bool isPre);
17125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
17135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //                            Declaration Emission
17145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
17150dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1716d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  /// EmitDecl - Emit a declaration.
1717d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  ///
1718d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  /// This function can be called with a null (unreachable) insert point.
17195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitDecl(const Decl &D);
1720d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar
1721b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall  /// EmitVarDecl - Emit a local variable declaration.
1722d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  ///
1723d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  /// This function can be called with a null (unreachable) insert point.
1724b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall  void EmitVarDecl(const VarDecl &D);
1725d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar
1726f85e193739c953358c865005855253af4f68a497John McCall  void EmitScalarInit(const Expr *init, const ValueDecl *D,
1727a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall                      LValue lvalue, bool capturedByInit);
17287acddacc921cd0b3f813443a8641eeddb82dfbd4John McCall  void EmitScalarInit(llvm::Value *init, LValue lvalue);
1729f85e193739c953358c865005855253af4f68a497John McCall
1730f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  typedef void SpecialInitFn(CodeGenFunction &Init, const VarDecl &D,
1731f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                             llvm::Value *Address);
1732f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
1733b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall  /// EmitAutoVarDecl - Emit an auto variable declaration.
1734d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  ///
1735d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar  /// This function can be called with a null (unreachable) insert point.
173634695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  void EmitAutoVarDecl(const VarDecl &D);
173734695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
173834695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  class AutoVarEmission {
173934695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    friend class CodeGenFunction;
174034695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
174157b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    const VarDecl *Variable;
174234695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
174334695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// The alignment of the variable.
174434695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    CharUnits Alignment;
174534695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
174634695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// The address of the alloca.  Null if the variable was emitted
174734695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// as a global constant.
174834695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    llvm::Value *Address;
174934695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
175034695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    llvm::Value *NRVOFlag;
175134695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
175234695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// True if the variable is a __block variable.
175334695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    bool IsByRef;
175434695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
175534695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// True if the variable is of aggregate type and has a constant
175634695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// initializer.
175734695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    bool IsConstantAggregate;
175834695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
175957b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    struct Invalid {};
176057b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    AutoVarEmission(Invalid) : Variable(0) {}
176157b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall
176234695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    AutoVarEmission(const VarDecl &variable)
176357b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall      : Variable(&variable), Address(0), NRVOFlag(0),
176434695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall        IsByRef(false), IsConstantAggregate(false) {}
176534695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
176634695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    bool wasEmittedAsGlobal() const { return Address == 0; }
176734695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
176834695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  public:
176957b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall    static AutoVarEmission invalid() { return AutoVarEmission(Invalid()); }
177057b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall
177134695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// Returns the address of the object within this declaration.
177234695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// Note that this does not chase the forwarding pointer for
177334695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    /// __block decls.
177434695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    llvm::Value *getObjectAddress(CodeGenFunction &CGF) const {
177534695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall      if (!IsByRef) return Address;
177634695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall
177734695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall      return CGF.Builder.CreateStructGEP(Address,
177857b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall                                         CGF.getByRefValueLLVMField(Variable),
177957b3b6a60856eaec30fd876a8a3face8f7e3ad7bJohn McCall                                         Variable->getNameAsString());
178034695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall    }
178134695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  };
178234695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  AutoVarEmission EmitAutoVarAlloca(const VarDecl &var);
178334695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  void EmitAutoVarInit(const AutoVarEmission &emission);
178434695856c0e67b3765b46304cc71b5d2cd5b71c7John McCall  void EmitAutoVarCleanups(const AutoVarEmission &emission);
1785bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  void emitAutoVarTypeCleanup(const AutoVarEmission &emission,
1786bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall                              QualType::DestructionKind dtorKind);
1787d286f05f1234bac289173f0eed88d7ecbaea0099Daniel Dunbar
1788b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall  void EmitStaticVarDecl(const VarDecl &D,
1789b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall                         llvm::GlobalValue::LinkageTypes Linkage);
1790b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar
1791b7ec246872b412f0e7bb9e93eacfd78cfa6adfb3Daniel Dunbar  /// EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
1792093ac461b37a573dcf226fa55faed96f318169b9Devang Patel  void EmitParmDecl(const VarDecl &D, llvm::Value *Arg, unsigned ArgNo);
17930dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
179456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// protectFromPeepholes - Protect a value that we're intending to
179556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// store to the side, but which will probably be used later, from
179656ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// aggressive peepholing optimizations that might delete it.
179756ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  ///
179856ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// Pass the result to unprotectFromPeepholes to declare that
179956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// protection is no longer required.
180056ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  ///
180156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// There's no particular reason why this shouldn't apply to
180256ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  /// l-values, it's just that no existing peepholes work on pointers.
180356ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  PeepholeProtection protectFromPeepholes(RValue rvalue);
180456ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  void unprotectFromPeepholes(PeepholeProtection protection);
180556ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
18065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
18075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //                             Statement Emission
18085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
18095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
18100dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitStopPoint - Emit a debug stoppoint if we are emitting debug info.
18110912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  void EmitStopPoint(const Stmt *S);
18120912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar
18130dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitStmt - Emit the code for the statement \arg S. It is legal to call
18140dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// this function even if there is no current insertion point.
18150dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  ///
18160dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// This function may clear the current insertion point; callers should use
18170dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EnsureInsertPoint if they wish to subsequently generate code without first
18180dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// calling EmitBlock, EmitBranch, or EmitStmt.
18195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitStmt(const Stmt *S);
1820a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar
18210912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  /// EmitSimpleStmt - Try to emit a "simple" statement which does not
18220dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// necessarily require an insertion point or debug information; typically
18230dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// because the statement amounts to a jump or a container of other
18240dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// statements.
18250912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  ///
18260912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  /// \return True if the statement was handled.
18270912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  bool EmitSimpleStmt(const Stmt *S);
18280912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar
18299b65551d0b387a7597fb39356a4d8ef10046445eChris Lattner  RValue EmitCompoundStmt(const CompoundStmt &S, bool GetLast = false,
1830558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall                          AggValueSlot AVS = AggValueSlot::ignored());
1831a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar
18320dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitLabel - Emit the block for the given label. It is legal to call this
18330dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// function even if there is no current insertion point.
1834ad8dcf4a9df0e24051dc31bf9e6f3cd138a34298Chris Lattner  void EmitLabel(const LabelDecl *D); // helper for EmitLabelStmt.
1835a448fb2da03ece39978784793eea68760e8205a1Daniel Dunbar
18365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitLabelStmt(const LabelStmt &S);
18375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitGotoStmt(const GotoStmt &S);
18380ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  void EmitIndirectGotoStmt(const IndirectGotoStmt &S);
18395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitIfStmt(const IfStmt &S);
18405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitWhileStmt(const WhileStmt &S);
18415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitDoStmt(const DoStmt &S);
18425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitForStmt(const ForStmt &S);
18435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitReturnStmt(const ReturnStmt &S);
18445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void EmitDeclStmt(const DeclStmt &S);
18450912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  void EmitBreakStmt(const BreakStmt &S);
18460912425f79418a215c2fbd2d8fc9511244a4aa46Daniel Dunbar  void EmitContinueStmt(const ContinueStmt &S);
184751b09f2c528c8460b5465c676173324e44176d62Devang Patel  void EmitSwitchStmt(const SwitchStmt &S);
184851b09f2c528c8460b5465c676173324e44176d62Devang Patel  void EmitDefaultStmt(const DefaultStmt &S);
184951b09f2c528c8460b5465c676173324e44176d62Devang Patel  void EmitCaseStmt(const CaseStmt &S);
1850c049e4f406a7f7179eba98659044a32508e53289Devang Patel  void EmitCaseStmtRange(const CaseStmt &S);
1851fb1aeb804c08d5288a923fb278161783e6abdc66Anders Carlsson  void EmitAsmStmt(const AsmStmt &S);
18520dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
18533d8400d9a61aa4b63ff35e5cede405b32a41425eAnders Carlsson  void EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S);
185464d5d6c5903157c521af496479d06dc26032d718Anders Carlsson  void EmitObjCAtTryStmt(const ObjCAtTryStmt &S);
185564d5d6c5903157c521af496479d06dc26032d718Anders Carlsson  void EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S);
185610cac6f7115b59a466bb8d2d51cdddeb38aadc37Chris Lattner  void EmitObjCAtSynchronizedStmt(const ObjCAtSynchronizedStmt &S);
1857f85e193739c953358c865005855253af4f68a497John McCall  void EmitObjCAutoreleasePoolStmt(const ObjCAutoreleasePoolStmt &S);
18580dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
185993c332a8ba2c193c435b293966d343dab15f555bJohn McCall  llvm::Constant *getUnwindResumeFn();
186086a3a03667bdb0dcab7e6a2877dfd234b07a6d43Douglas Gregor  llvm::Constant *getUnwindResumeOrRethrowFn();
186159a7000a79118e4c140885ccbb2ac6a686a73092John McCall  void EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
186259a7000a79118e4c140885ccbb2ac6a686a73092John McCall  void ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock = false);
18639fc6a7774643a810c8501dae2323e863fefb623eJohn McCall
18646815e941998659a55c20c147861b0f437928c3d8Anders Carlsson  void EmitCXXTryStmt(const CXXTryStmt &S);
1865ad762fcdc16b9e4705b12b09d92b8c026212b906Richard Smith  void EmitCXXForRangeStmt(const CXXForRangeStmt &S);
18669cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
18675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
18685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //                         LValue Expression Emission
18695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
18705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
187113e81737a433b23f8c662d10d1d57356122a8caaDaniel Dunbar  /// GetUndefRValue - Get an appropriate 'undef' rvalue for the given type.
187213e81737a433b23f8c662d10d1d57356122a8caaDaniel Dunbar  RValue GetUndefRValue(QualType Ty);
187313e81737a433b23f8c662d10d1d57356122a8caaDaniel Dunbar
1874ce1d38b8163650e473d7084e0686ed5a7956057bDaniel Dunbar  /// EmitUnsupportedRValue - Emit a dummy r-value using the type of E
1875ce1d38b8163650e473d7084e0686ed5a7956057bDaniel Dunbar  /// and issue an ErrorUnsupported style diagnostic (using the
1876ce1d38b8163650e473d7084e0686ed5a7956057bDaniel Dunbar  /// provided Name).
1877ce1d38b8163650e473d7084e0686ed5a7956057bDaniel Dunbar  RValue EmitUnsupportedRValue(const Expr *E,
1878ce1d38b8163650e473d7084e0686ed5a7956057bDaniel Dunbar                               const char *Name);
1879ce1d38b8163650e473d7084e0686ed5a7956057bDaniel Dunbar
18800dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitUnsupportedLValue - Emit a dummy l-value using the type of E and issue
18810dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// an ErrorUnsupported style diagnostic (using the provided Name).
18826ba82a4a4b295e54cf192c3e98b28f39990c6194Daniel Dunbar  LValue EmitUnsupportedLValue(const Expr *E,
18836ba82a4a4b295e54cf192c3e98b28f39990c6194Daniel Dunbar                               const char *Name);
18846ba82a4a4b295e54cf192c3e98b28f39990c6194Daniel Dunbar
18855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EmitLValue - Emit code to compute a designator that specifies the location
18865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// of the expression.
18875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
18885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// This can return one of two things: a simple address or a bitfield
18895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// reference.  In either case, the LLVM Value* in the LValue structure is
18905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// guaranteed to be an LLVM pointer type.
18915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
18925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// If this returns a bitfield reference, nothing about the pointee type of
18935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// the LLVM value is known: For example, it may not be a pointer to an
18945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// integer.
18955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
18965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// If this returns a normal address, and if the lvalue's C type is fixed
18975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// size, this method guarantees that the returned pointer type will point to
18985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// an LLVM type of the same size of the lvalue's type.  If the lvalue has a
18995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// variable length type, this is not possible.
19005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ///
19015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LValue EmitLValue(const Expr *E);
19020dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
1903b14e62d5aee7522b98c410ee65bd750c2cfe6f01Mike Stump  /// EmitCheckedLValue - Same as EmitLValue but additionally we generate
1904b14e62d5aee7522b98c410ee65bd750c2cfe6f01Mike Stump  /// checking code to guard against undefined behavior.  This is only
1905b14e62d5aee7522b98c410ee65bd750c2cfe6f01Mike Stump  /// suitable when we know that the address will be used to access the
1906b14e62d5aee7522b98c410ee65bd750c2cfe6f01Mike Stump  /// object.
1907b14e62d5aee7522b98c410ee65bd750c2cfe6f01Mike Stump  LValue EmitCheckedLValue(const Expr *E);
1908b14e62d5aee7522b98c410ee65bd750c2cfe6f01Mike Stump
190926815d97c5743481e317f17a8d53a6819d061862John McCall  /// EmitToMemory - Change a scalar value from its value
191026815d97c5743481e317f17a8d53a6819d061862John McCall  /// representation to its in-memory representation.
191126815d97c5743481e317f17a8d53a6819d061862John McCall  llvm::Value *EmitToMemory(llvm::Value *Value, QualType Ty);
191226815d97c5743481e317f17a8d53a6819d061862John McCall
191326815d97c5743481e317f17a8d53a6819d061862John McCall  /// EmitFromMemory - Change a scalar value from its memory
191426815d97c5743481e317f17a8d53a6819d061862John McCall  /// representation to its value representation.
191526815d97c5743481e317f17a8d53a6819d061862John McCall  llvm::Value *EmitFromMemory(llvm::Value *Value, QualType Ty);
191626815d97c5743481e317f17a8d53a6819d061862John McCall
19179d9cc874bcc168474757a8f90476684b4a93ed76Daniel Dunbar  /// EmitLoadOfScalar - Load a scalar value from an address, taking
19189d9cc874bcc168474757a8f90476684b4a93ed76Daniel Dunbar  /// care to appropriately convert from the memory representation to
19199d9cc874bcc168474757a8f90476684b4a93ed76Daniel Dunbar  /// the LLVM value representation.
192009429b962811f2c0a7a81a1b0a89f96f5878b20cMike Stump  llvm::Value *EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
19213d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman                                unsigned Alignment, QualType Ty,
19223d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman                                llvm::MDNode *TBAAInfo = 0);
1923545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall
1924545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  /// EmitLoadOfScalar - Load a scalar value from an address, taking
1925545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  /// care to appropriately convert from the memory representation to
1926545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  /// the LLVM value representation.  The l-value must be a simple
1927545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  /// l-value.
1928a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall  llvm::Value *EmitLoadOfScalar(LValue lvalue);
19299d9cc874bcc168474757a8f90476684b4a93ed76Daniel Dunbar
19309d9cc874bcc168474757a8f90476684b4a93ed76Daniel Dunbar  /// EmitStoreOfScalar - Store a scalar value to an address, taking
19319d9cc874bcc168474757a8f90476684b4a93ed76Daniel Dunbar  /// care to appropriately convert from the memory representation to
19329d9cc874bcc168474757a8f90476684b4a93ed76Daniel Dunbar  /// the LLVM value representation.
193309429b962811f2c0a7a81a1b0a89f96f5878b20cMike Stump  void EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
19343d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman                         bool Volatile, unsigned Alignment, QualType Ty,
19353d5aff5d3036b0ff09d114857cd2276134b3d8c9Dan Gohman                         llvm::MDNode *TBAAInfo = 0);
1936545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall
1937545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  /// EmitStoreOfScalar - Store a scalar value to an address, taking
1938545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  /// care to appropriately convert from the memory representation to
1939545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  /// the LLVM value representation.  The l-value must be a simple
1940545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  /// l-value.
1941a07398ed98ea2b55ad7a505a3aab18aed93b149fJohn McCall  void EmitStoreOfScalar(llvm::Value *value, LValue lvalue);
19429d9cc874bcc168474757a8f90476684b4a93ed76Daniel Dunbar
19435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EmitLoadOfLValue - Given an expression that represents a value lvalue,
19445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// this method emits the address of the lvalue, then loads the result as an
19455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// rvalue, returning the rvalue.
1946545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  RValue EmitLoadOfLValue(LValue V);
1947545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  RValue EmitLoadOfExtVectorElementLValue(LValue V);
1948545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  RValue EmitLoadOfBitfieldLValue(LValue LV);
1949119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall  RValue EmitLoadOfPropertyRefLValue(LValue LV,
1950119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall                                 ReturnValueSlot Return = ReturnValueSlot());
19510dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
19525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// EmitStoreThroughLValue - Store the specified rvalue into the specified
19535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// lvalue, where both are guaranteed to the have the same type, and that type
19545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  /// is 'Ty'.
1955545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  void EmitStoreThroughLValue(RValue Src, LValue Dst);
1956545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  void EmitStoreThroughExtVectorComponentLValue(RValue Src, LValue Dst);
1957119a1c6c4029d30cae7b31a2826aa0ff70d01668John McCall  void EmitStoreThroughPropertyRefLValue(RValue Src, LValue Dst);
1958ed3849b456d64d124bddc7ac044f3ce590bd9d69Daniel Dunbar
19590dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitStoreThroughLValue - Store Src into Dst with same constraints as
19600dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitStoreThroughLValue.
1961ed3849b456d64d124bddc7ac044f3ce590bd9d69Daniel Dunbar  ///
19620dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// \param Result [out] - If non-null, this will be set to a Value* for the
19630dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// bit-field contents after the store, appropriate for use as the result of
19640dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// an assignment to the bit-field.
1965545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  void EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
1966ed3849b456d64d124bddc7ac044f3ce590bd9d69Daniel Dunbar                                      llvm::Value **Result=0);
19670dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
196883ce9d4a552987d34cbd500e983db8d770232379John McCall  /// Emit an l-value for an assignment (simple or compound) of complex type.
196983ce9d4a552987d34cbd500e983db8d770232379John McCall  LValue EmitComplexAssignmentLValue(const BinaryOperator *E);
19702a41637a995affa1563f4d82a8b026e326a2faa0John McCall  LValue EmitComplexCompoundAssignmentLValue(const CompoundAssignOperator *E);
197183ce9d4a552987d34cbd500e983db8d770232379John McCall
1972fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner  // Note: only available for agg return types
197380e62c29fe21cd06c93eba6e72b7e32dca06fcf9Daniel Dunbar  LValue EmitBinaryOperatorLValue(const BinaryOperator *E);
19742a41637a995affa1563f4d82a8b026e326a2faa0John McCall  LValue EmitCompoundAssignmentLValue(const CompoundAssignOperator *E);
19755b5c9ef865607e179413462dcd71bcebb5b7daaeDaniel Dunbar  // Note: only available for agg return types
197622c940ee588a420cd2e6c8f68be2ac3cc80061ffChristopher Lamb  LValue EmitCallExprLValue(const CallExpr *E);
19775b5c9ef865607e179413462dcd71bcebb5b7daaeDaniel Dunbar  // Note: only available for agg return types
19785b5c9ef865607e179413462dcd71bcebb5b7daaeDaniel Dunbar  LValue EmitVAArgExprLValue(const VAArgExpr *E);
19795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LValue EmitDeclRefLValue(const DeclRefExpr *E);
19805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LValue EmitStringLiteralLValue(const StringLiteral *E);
1981eaf2bb89eb2aad3b80673de30febe52df43c10ecChris Lattner  LValue EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E);
1982d9f6910f4ef37c0e8eeee2a01287d9572c3176efChris Lattner  LValue EmitPredefinedLValue(const PredefinedExpr *E);
19835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LValue EmitUnaryOpLValue(const UnaryOperator *E);
19845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E);
1985213541a68a3e137d11d2cefb612c6cdb410d7e8eNate Begeman  LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E);
1986b84a06e68ffd71da22e3c75b6e4bbdba37816413Devang Patel  LValue EmitMemberExpr(const MemberExpr *E);
1987820bca41c3899374775d2a1dfc2ef2e22aaf1c7bFariborz Jahanian  LValue EmitObjCIsaExpr(const ObjCIsaExpr *E);
198806e863f2902b8ba55b056308875c19f7ba3dab25Eli Friedman  LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E);
198956ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall  LValue EmitConditionalOperatorLValue(const AbstractConditionalOperator *E);
199075dfedaf702822c9e51a4f3c5d3ecf2d2ad99272Chris Lattner  LValue EmitCastLValue(const CastExpr *E);
1991ed8abf18329df67b0abcbb3a10458bd8c1d2a595Douglas Gregor  LValue EmitNullInitializationLValue(const CXXScalarValueInitExpr *E);
199203e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor  LValue EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
1993e996ffd240f20a1048179d7727a6ee3227261921John McCall  LValue EmitOpaqueValueLValue(const OpaqueValueExpr *e);
19949cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
19952a03192a02dbf4fdff438d1e658356bde871aba4Daniel Dunbar  llvm::Value *EmitIvarOffset(const ObjCInterfaceDecl *Interface,
199629e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar                              const ObjCIvarDecl *Ivar);
1997a9976d3b192690db20f59dc44099ac4ca939bdb7John McCall  LValue EmitLValueForAnonRecordField(llvm::Value* Base,
199800eb3f9c5b33e3d99aee1f8b75dd9c9678fdd66bFrancois Pichet                                      const IndirectFieldDecl* Field,
1999a9976d3b192690db20f59dc44099ac4ca939bdb7John McCall                                      unsigned CVRQualifiers);
20000ed303c918915cbe2c611cddbfabd00404664831Anders Carlsson  LValue EmitLValueForField(llvm::Value* Base, const FieldDecl* Field,
2001e6d2a534851a649485cb087e9dfcaf8a65886858Anders Carlsson                            unsigned CVRQualifiers);
20029cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
200306a2970e9480c6d02b367b2f970baff29b9f9721Anders Carlsson  /// EmitLValueForFieldInitialization - Like EmitLValueForField, except that
200406a2970e9480c6d02b367b2f970baff29b9f9721Anders Carlsson  /// if the Field is a reference, this will return the address of the reference
200506a2970e9480c6d02b367b2f970baff29b9f9721Anders Carlsson  /// and not the address of the value stored in the reference.
20069cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  LValue EmitLValueForFieldInitialization(llvm::Value* Base,
200706a2970e9480c6d02b367b2f970baff29b9f9721Anders Carlsson                                          const FieldDecl* Field,
200806a2970e9480c6d02b367b2f970baff29b9f9721Anders Carlsson                                          unsigned CVRQualifiers);
20099cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
201045012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian  LValue EmitLValueForIvar(QualType ObjectTy,
201145012a7ef5abf1042c893f3f2fa5c23cb5485ea9Fariborz Jahanian                           llvm::Value* Base, const ObjCIvarDecl *Ivar,
201229e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar                           unsigned CVRQualifiers);
201329e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar
20140ed303c918915cbe2c611cddbfabd00404664831Anders Carlsson  LValue EmitLValueForBitfield(llvm::Value* Base, const FieldDecl* Field,
2015598d3f61b6ca854e9d3c2f3359e24468502a61aaFariborz Jahanian                                unsigned CVRQualifiers);
2016fd64bb635dc221baa19f81d5d2a084f7eb269f7fFariborz Jahanian
2017a99038c0757a836c6faeeddaa5dfd249b32f6e9eMike Stump  LValue EmitBlockDeclRefLValue(const BlockDeclRefExpr *E);
2018a99038c0757a836c6faeeddaa5dfd249b32f6e9eMike Stump
2019b58d017f2b9eeed33f2ab3ede968b89cf5296bf2Anders Carlsson  LValue EmitCXXConstructLValue(const CXXConstructExpr *E);
2020e61c9e80a108b5db7e5f0e702f0e455d737c6390Anders Carlsson  LValue EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E);
20214765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  LValue EmitExprWithCleanupsLValue(const ExprWithCleanups *E);
2022c2e84ae9a6d25cea3e583c768e576b4c981ec028Mike Stump  LValue EmitCXXTypeidLValue(const CXXTypeidExpr *E);
20239cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
20240a04d77bde7e3a661c2b41b60630d125d09ed6efDaniel Dunbar  LValue EmitObjCMessageExprLValue(const ObjCMessageExpr *E);
2025391d77a26382dddf25da73e29fc1fa5aaaea4c6fChris Lattner  LValue EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E);
202685c59edda02df48fae8dc85049743319bc6e7e89Daniel Dunbar  LValue EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E);
2027654599454c8e6cc83b1b9b3af43c49c2f66a26cbChris Lattner  LValue EmitStmtExprLValue(const StmtExpr *E);
20288bfd31f9dad09cd52225d868bbd92a9bebe87775Fariborz Jahanian  LValue EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E);
202903b2960c14aede6ac82bdef32247094ebb72fa69Fariborz Jahanian  LValue EmitObjCSelectorLValue(const ObjCSelectorExpr *E);
2030189d6ef40eff11b83b2cda941d5ed89a5cef09b2John McCall  void   EmitDeclRefExprDbgValue(const DeclRefExpr *E, llvm::Constant *Init);
203156ca35d396d8692c384c785f9aeebcf22563fe1eJohn McCall
20325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
2033883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner  //                         Scalar Expression Emission
20345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  //===--------------------------------------------------------------------===//
20355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
20360dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitCall - Generate a call of the given function, expecting the given
20370dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// result type, and using the given argument list which specifies both the
20380dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// LLVM arguments and the types they were derived from.
2039c0ef9f59937c3971c48b6fed37cf5bd8985c024bDaniel Dunbar  ///
2040f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// \param TargetDecl - If given, the decl of the function in a direct call;
2041f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump  /// used to set attributes on the call (noreturn, etc.).
204288b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar  RValue EmitCall(const CGFunctionInfo &FnInfo,
204388b5396b0897f28d22ae3debf4a0d97b33b6c362Daniel Dunbar                  llvm::Value *Callee,
2044f3c47c9525153aea2de0ec4bd615b9cf2d81c103Anders Carlsson                  ReturnValueSlot ReturnValue,
2045c0ef9f59937c3971c48b6fed37cf5bd8985c024bDaniel Dunbar                  const CallArgList &Args,
2046dd5c98f709837e5dd3da08d44d1ce407975df2cfDavid Chisnall                  const Decl *TargetDecl = 0,
20474b02afcb45cd1a384de7d45f440a8be091dd500bDavid Chisnall                  llvm::Instruction **callOrInvoke = 0);
20481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
204931777a2540879051a3c643b90e02c3fd3d315243Anders Carlsson  RValue EmitCall(QualType FnType, llvm::Value *Callee,
2050d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson                  ReturnValueSlot ReturnValue,
2051986477149db76e6d9f8d3c9859c9880c11b680aeAnders Carlsson                  CallExpr::const_arg_iterator ArgBeg,
2052986477149db76e6d9f8d3c9859c9880c11b680aeAnders Carlsson                  CallExpr::const_arg_iterator ArgEnd,
2053986477149db76e6d9f8d3c9859c9880c11b680aeAnders Carlsson                  const Decl *TargetDecl = 0);
20549cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  RValue EmitCallExpr(const CallExpr *E,
2055d2490a91341b57df7a7e54f8a707e7ecde2eeb4eAnders Carlsson                      ReturnValueSlot ReturnValue = ReturnValueSlot());
20561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2057f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall  llvm::CallSite EmitCallOrInvoke(llvm::Value *Callee,
2058f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                                  llvm::Value * const *ArgBegin,
2059f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                                  llvm::Value * const *ArgEnd,
2060f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall                                  const llvm::Twine &Name = "");
2061f1549f66a8216a78112286e3978cea2c29d6334cJohn McCall
2062566abee1e9828a7700b51e4d17ea08234fde3bb4Anders Carlsson  llvm::Value *BuildVirtualCall(const CXXMethodDecl *MD, llvm::Value *This,
2063f0070dbae9535836ad41711081465dec2259786bMike Stump                                const llvm::Type *Ty);
20649cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  llvm::Value *BuildVirtualCall(const CXXDestructorDecl *DD, CXXDtorType Type,
206583eedd9fa800c273647c7a90465ea9c110f9d93cAnders Carlsson                                llvm::Value *This, const llvm::Type *Ty);
20662726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian  llvm::Value *BuildAppleKextVirtualCall(const CXXMethodDecl *MD,
20672726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian                                         NestedNameSpecifier *Qual,
20682726267f094a0c1f5ac5b501ec5a9898c58876bfFariborz Jahanian                                         const llvm::Type *Ty);
2069ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian
2070ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian  llvm::Value *BuildAppleKextVirtualDestructorCall(const CXXDestructorDecl *DD,
2071ccd5259d33cbbdd6f5fbf7ccab4cb4a2702309eaFariborz Jahanian                                                   CXXDtorType Type,
2072771c678c04f5f685b4f188ec6c2fd88ad0f7457fFariborz Jahanian                                                   const CXXRecordDecl *RD);
2073566abee1e9828a7700b51e4d17ea08234fde3bb4Anders Carlsson
2074b9de2c55b2a33776e2bee8ee57df7599b374c8a5Anders Carlsson  RValue EmitCXXMemberCall(const CXXMethodDecl *MD,
2075b9de2c55b2a33776e2bee8ee57df7599b374c8a5Anders Carlsson                           llvm::Value *Callee,
2076a1736c0c750d4514a5d8fda36670addf1e4de54aAnders Carlsson                           ReturnValueSlot ReturnValue,
2077b9de2c55b2a33776e2bee8ee57df7599b374c8a5Anders Carlsson                           llvm::Value *This,
2078c997d4278d329e18891aac9698fb991b2d4622ebAnders Carlsson                           llvm::Value *VTT,
2079b9de2c55b2a33776e2bee8ee57df7599b374c8a5Anders Carlsson                           CallExpr::const_arg_iterator ArgBeg,
2080b9de2c55b2a33776e2bee8ee57df7599b374c8a5Anders Carlsson                           CallExpr::const_arg_iterator ArgEnd);
2081a1736c0c750d4514a5d8fda36670addf1e4de54aAnders Carlsson  RValue EmitCXXMemberCallExpr(const CXXMemberCallExpr *E,
2082a1736c0c750d4514a5d8fda36670addf1e4de54aAnders Carlsson                               ReturnValueSlot ReturnValue);
2083a1736c0c750d4514a5d8fda36670addf1e4de54aAnders Carlsson  RValue EmitCXXMemberPointerCallExpr(const CXXMemberCallExpr *E,
2084a1736c0c750d4514a5d8fda36670addf1e4de54aAnders Carlsson                                      ReturnValueSlot ReturnValue);
20855549976193e34417d4474a5f4a514268ef6666c7Ted Kremenek
2086a2447e0d1e28669cd637204a871f15b1215277fdAnders Carlsson  llvm::Value *EmitCXXOperatorMemberCallee(const CXXOperatorCallExpr *E,
2087a2447e0d1e28669cd637204a871f15b1215277fdAnders Carlsson                                           const CXXMethodDecl *MD,
2088a2447e0d1e28669cd637204a871f15b1215277fdAnders Carlsson                                           llvm::Value *This);
20890f294632f36459174199b77699e339715244b5abAnders Carlsson  RValue EmitCXXOperatorMemberCallExpr(const CXXOperatorCallExpr *E,
2090a1736c0c750d4514a5d8fda36670addf1e4de54aAnders Carlsson                                       const CXXMethodDecl *MD,
2091a1736c0c750d4514a5d8fda36670addf1e4de54aAnders Carlsson                                       ReturnValueSlot ReturnValue);
20921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20939cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
20941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  RValue EmitBuiltinExpr(const FunctionDecl *FD,
2095ef2abfee3ea16ec74942dc09e9e425f46aeb2582Daniel Dunbar                         unsigned BuiltinID, const CallExpr *E);
20965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
2097a1736c0c750d4514a5d8fda36670addf1e4de54aAnders Carlsson  RValue EmitBlockCallExpr(const CallExpr *E, ReturnValueSlot ReturnValue);
209809429b962811f2c0a7a81a1b0a89f96f5878b20cMike Stump
20990dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitTargetBuiltinExpr - Emit the given builtin call. Returns 0 if the call
21000dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// is unhandled by the current target.
2101f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar  llvm::Value *EmitTargetBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2102f02e9ddf5efc75917af712b3c7f909581205f0a5Daniel Dunbar
21032752c0137d95aa2f4ee1cdff4b564bac842e041bChris Lattner  llvm::Value *EmitARMBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
21049cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  llvm::Value *EmitNeonCall(llvm::Function *F,
210530d91718a676177f0d0d0210ce4fdb4f616df6e5Nate Begeman                            llvm::SmallVectorImpl<llvm::Value*> &O,
2106db3d4d036037f379f12643e067b229862d61e932Bob Wilson                            const char *name,
210761eecf5aea018ea65c9ab0bccacd2996b15c632dNate Begeman                            unsigned shift = 0, bool rightshift = false);
2108cf55652cf668c1402eee0b12edd2e5a1bc34d7a1Bob Wilson  llvm::Value *EmitNeonSplat(llvm::Value *V, llvm::Constant *Idx);
2109464ccb68f22a7e1c0a2844551c16f721540c91c3Nate Begeman  llvm::Value *EmitNeonShiftVector(llvm::Value *V, const llvm::Type *Ty,
211061eecf5aea018ea65c9ab0bccacd2996b15c632dNate Begeman                                   bool negateForRightShift);
21119cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
2112aa51e513850688b7963efc62abf1eface7037602Bill Wendling  llvm::Value *BuildVector(const llvm::SmallVectorImpl<llvm::Value*> &Ops);
2113564f1de67d7ba43646b8740db86d6269e3dfbe0bAnders Carlsson  llvm::Value *EmitX86BuiltinExpr(unsigned BuiltinID, const CallExpr *E);
2114564f1de67d7ba43646b8740db86d6269e3dfbe0bAnders Carlsson  llvm::Value *EmitPPCBuiltinExpr(unsigned BuiltinID, const CallExpr *E);
21150dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
2116ed7c618f849e2541b1d0288c43154937652c5b15Daniel Dunbar  llvm::Value *EmitObjCProtocolExpr(const ObjCProtocolExpr *E);
21177f02f721d48772e4eee4fccd8af4f800be1bbc1eChris Lattner  llvm::Value *EmitObjCStringLiteral(const ObjCStringLiteral *E);
21188fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner  llvm::Value *EmitObjCSelectorExpr(const ObjCSelectorExpr *E);
2119ef072fd2f3347cfd857d6eb787b245b950771430John McCall  RValue EmitObjCMessageExpr(const ObjCMessageExpr *E,
2120ef072fd2f3347cfd857d6eb787b245b950771430John McCall                             ReturnValueSlot Return = ReturnValueSlot());
21218fdf32822be2238aa7db62d40e75b168b637ab7dChris Lattner
2122f85e193739c953358c865005855253af4f68a497John McCall  /// Retrieves the default cleanup kind for an ARC cleanup.
2123f85e193739c953358c865005855253af4f68a497John McCall  /// Except under -fobjc-arc-eh, ARC cleanups are normal-only.
2124f85e193739c953358c865005855253af4f68a497John McCall  CleanupKind getARCCleanupKind() {
2125f85e193739c953358c865005855253af4f68a497John McCall    return CGM.getCodeGenOpts().ObjCAutoRefCountExceptions
2126f85e193739c953358c865005855253af4f68a497John McCall             ? NormalAndEHCleanup : NormalCleanup;
2127f85e193739c953358c865005855253af4f68a497John McCall  }
2128f85e193739c953358c865005855253af4f68a497John McCall
2129f85e193739c953358c865005855253af4f68a497John McCall  // ARC primitives.
2130f85e193739c953358c865005855253af4f68a497John McCall  void EmitARCInitWeak(llvm::Value *value, llvm::Value *addr);
2131f85e193739c953358c865005855253af4f68a497John McCall  void EmitARCDestroyWeak(llvm::Value *addr);
2132f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCLoadWeak(llvm::Value *addr);
2133f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCLoadWeakRetained(llvm::Value *addr);
2134f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCStoreWeak(llvm::Value *value, llvm::Value *addr,
2135f85e193739c953358c865005855253af4f68a497John McCall                                bool ignored);
2136f85e193739c953358c865005855253af4f68a497John McCall  void EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src);
2137f85e193739c953358c865005855253af4f68a497John McCall  void EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src);
2138f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetainAutorelease(QualType type, llvm::Value *value);
2139f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetainAutoreleaseNonBlock(llvm::Value *value);
2140545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall  llvm::Value *EmitARCStoreStrong(LValue lvalue, llvm::Value *value,
2141545d996ec5a3113f046944f11b27cc2d6cb055b4John McCall                                  bool ignored);
2142f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCStoreStrongCall(llvm::Value *addr, llvm::Value *value,
2143f85e193739c953358c865005855253af4f68a497John McCall                                      bool ignored);
2144f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetain(QualType type, llvm::Value *value);
2145f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetainNonBlock(llvm::Value *value);
2146f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetainBlock(llvm::Value *value);
2147f85e193739c953358c865005855253af4f68a497John McCall  void EmitARCRelease(llvm::Value *value, bool precise);
2148f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCAutorelease(llvm::Value *value);
2149f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCAutoreleaseReturnValue(llvm::Value *value);
2150f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetainAutoreleaseReturnValue(llvm::Value *value);
2151f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetainAutoreleasedReturnValue(llvm::Value *value);
2152f85e193739c953358c865005855253af4f68a497John McCall
2153f85e193739c953358c865005855253af4f68a497John McCall  std::pair<LValue,llvm::Value*>
2154f85e193739c953358c865005855253af4f68a497John McCall  EmitARCStoreAutoreleasing(const BinaryOperator *e);
2155f85e193739c953358c865005855253af4f68a497John McCall  std::pair<LValue,llvm::Value*>
2156f85e193739c953358c865005855253af4f68a497John McCall  EmitARCStoreStrong(const BinaryOperator *e, bool ignored);
2157f85e193739c953358c865005855253af4f68a497John McCall
2158f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitObjCProduceObject(QualType T, llvm::Value *Ptr);
2159f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitObjCConsumeObject(QualType T, llvm::Value *Ptr);
2160f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitObjCExtendObjectLifetime(QualType T, llvm::Value *Ptr);
2161f85e193739c953358c865005855253af4f68a497John McCall
2162f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetainScalarExpr(const Expr *expr);
2163f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitARCRetainAutoreleaseScalarExpr(const Expr *expr);
2164f85e193739c953358c865005855253af4f68a497John McCall
2165bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  static Destroyer destroyARCStrongImprecise;
2166bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  static Destroyer destroyARCStrongPrecise;
2167bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall  static Destroyer destroyARCWeak;
2168bdc4d80956c83a486e58d3df6bb524a1f66ff574John McCall
2169f85e193739c953358c865005855253af4f68a497John McCall  void EmitObjCAutoreleasePoolPop(llvm::Value *Ptr);
2170f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitObjCAutoreleasePoolPush();
2171f85e193739c953358c865005855253af4f68a497John McCall  llvm::Value *EmitObjCMRRAutoreleasePoolPush();
2172f85e193739c953358c865005855253af4f68a497John McCall  void EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr);
2173f85e193739c953358c865005855253af4f68a497John McCall  void EmitObjCMRRAutoreleasePoolPop(llvm::Value *Ptr);
2174f85e193739c953358c865005855253af4f68a497John McCall
21754029ca7ae9d3ffd7738fe2ed8782bebc30f36fd6Anders Carlsson  /// EmitReferenceBindingToExpr - Emits a reference binding to the passed in
21764029ca7ae9d3ffd7738fe2ed8782bebc30f36fd6Anders Carlsson  /// expression. Will emit a temporary variable if E is not an LValue.
21779cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer  RValue EmitReferenceBindingToExpr(const Expr* E,
217832f36baa6c8d491c374af622b4e3ac28d597453cAnders Carlsson                                    const NamedDecl *InitializedDecl);
21793aba09376c5f49c4c8d176109ea4835bc2c528eeAnders Carlsson
2180883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner  //===--------------------------------------------------------------------===//
2181bfc0c1ae4b505b52b36f572a9641f99e76879cabChris Lattner  //                           Expression Emission
2182883f6a7cc7dccb1d675e27121a82614d63492a8dChris Lattner  //===--------------------------------------------------------------------===//
2183bfc0c1ae4b505b52b36f572a9641f99e76879cabChris Lattner
2184bfc0c1ae4b505b52b36f572a9641f99e76879cabChris Lattner  // Expressions are broken into three classes: scalar, complex, aggregate.
21850dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
21860dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// EmitScalarExpr - Emit the computation of the specified expression of LLVM
21870dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// scalar type, returning the result.
218814c5cbf59cffee52275230922283a247de407712Anders Carlsson  llvm::Value *EmitScalarExpr(const Expr *E , bool IgnoreResultAssign = false);
21890dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
21903707b255f8993fb362904c9cff87b0e9bc6ca317Chris Lattner  /// EmitScalarConversion - Emit a conversion from the specified type to the
21913707b255f8993fb362904c9cff87b0e9bc6ca317Chris Lattner  /// specified destination type, both of which are LLVM scalar types.
21923707b255f8993fb362904c9cff87b0e9bc6ca317Chris Lattner  llvm::Value *EmitScalarConversion(llvm::Value *Src, QualType SrcTy,
21933707b255f8993fb362904c9cff87b0e9bc6ca317Chris Lattner                                    QualType DstTy);
21940dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
21954f1a7b380809b4ca19ad2daff61bc11cd1e979d8Chris Lattner  /// EmitComplexToScalarConversion - Emit a conversion from the specified
21960dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// complex type to the specified destination type, where the destination type
21970dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// is an LLVM scalar type.
21984f1a7b380809b4ca19ad2daff61bc11cd1e979d8Chris Lattner  llvm::Value *EmitComplexToScalarConversion(ComplexPairTy Src, QualType SrcTy,
21994f1a7b380809b4ca19ad2daff61bc11cd1e979d8Chris Lattner                                             QualType DstTy);
22000dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
22010dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
2202558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  /// EmitAggExpr - Emit the computation of the specified expression
2203558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  /// of aggregate type.  The result is computed into the given slot,
2204558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  /// which may be null to indicate that the value is not needed.
2205474e2fe4957e6e72cee36ed189eaf21878ad0e91Fariborz Jahanian  void EmitAggExpr(const Expr *E, AggValueSlot AS, bool IgnoreResult = false);
22060dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
220718aba0dd518e486d8b50523e7dafb4b5657135d2Daniel Dunbar  /// EmitAggExprToLValue - Emit the computation of the specified expression of
220818aba0dd518e486d8b50523e7dafb4b5657135d2Daniel Dunbar  /// aggregate type into a temporary LValue.
220918aba0dd518e486d8b50523e7dafb4b5657135d2Daniel Dunbar  LValue EmitAggExprToLValue(const Expr *E);
221018aba0dd518e486d8b50523e7dafb4b5657135d2Daniel Dunbar
2211082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  /// EmitGCMemmoveCollectable - Emit special API for structs with object
2212082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  /// pointers.
2213082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian  void EmitGCMemmoveCollectable(llvm::Value *DestPtr, llvm::Value *SrcPtr,
221408c321380fff07d476a19daab6d29522c046cd49Fariborz Jahanian                                QualType Ty);
2215082b02e8403d3ee9d2ded969fbe0e5d472f04cd8Fariborz Jahanian
22160c24c808e4dce43e88abf2d5f98546b901bd4315John McCall  /// EmitExtendGCLifetime - Given a pointer to an Objective-C object,
22170c24c808e4dce43e88abf2d5f98546b901bd4315John McCall  /// make sure it survives garbage collection until this point.
22180c24c808e4dce43e88abf2d5f98546b901bd4315John McCall  void EmitExtendGCLifetime(llvm::Value *object);
22190c24c808e4dce43e88abf2d5f98546b901bd4315John McCall
2220b6ef18a2b06d6760459e1756a61f79ff496cee19Chris Lattner  /// EmitComplexExpr - Emit the computation of the specified expression of
222123b1cdb0b5e089468bb8475b8ec9287af67b4b59Chris Lattner  /// complex type, returning the result.
2222b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall  ComplexPairTy EmitComplexExpr(const Expr *E,
2223b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall                                bool IgnoreReal = false,
2224b418d74c11498b7e1044103131e2e3be4d63512eJohn McCall                                bool IgnoreImag = false);
22250dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
222623b1cdb0b5e089468bb8475b8ec9287af67b4b59Chris Lattner  /// EmitComplexExprIntoAddr - Emit the computation of the specified expression
222723b1cdb0b5e089468bb8475b8ec9287af67b4b59Chris Lattner  /// of complex type, storing into the specified Value*.
2228190dbe203c2e892ac40f1786035c6c9be3f44a4cChris Lattner  void EmitComplexExprIntoAddr(const Expr *E, llvm::Value *DestAddr,
2229190dbe203c2e892ac40f1786035c6c9be3f44a4cChris Lattner                               bool DestIsVolatile);
22307f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar
22317f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar  /// StoreComplexToAddr - Store a complex number into the specified address.
22327f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar  void StoreComplexToAddr(ComplexPairTy V, llvm::Value *DestAddr,
22337f8ea5c5b3a6a4332a841eefdd86b0726722ea7bDaniel Dunbar                          bool DestIsVolatile);
22349b65551d0b387a7597fb39356a4d8ef10046445eChris Lattner  /// LoadComplexFromAddr - Load a complex number from the specified address.
22359b65551d0b387a7597fb39356a4d8ef10046445eChris Lattner  ComplexPairTy LoadComplexFromAddr(llvm::Value *SrcAddr, bool SrcIsVolatile);
22362621fd1d6d3c5eadcae246859f62738645df7540Chris Lattner
2237b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall  /// CreateStaticVarDecl - Create a zero-initialized LLVM global for
2238b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall  /// a static local variable.
2239b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall  llvm::GlobalVariable *CreateStaticVarDecl(const VarDecl &D,
2240b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall                                            const char *Separator,
2241f71d232ce5354bd2f0c94ded30fb936c2a3d968bMike Stump                                       llvm::GlobalValue::LinkageTypes Linkage);
22429cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
2243b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall  /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
2244761acc19d966a67f2175783d09412d6ac9463400Chris Lattner  /// global variable that has already been created for it.  If the initializer
2245761acc19d966a67f2175783d09412d6ac9463400Chris Lattner  /// has a different type than GV does, this may free GV and return a different
2246761acc19d966a67f2175783d09412d6ac9463400Chris Lattner  /// one.  Otherwise it just returns GV.
2247761acc19d966a67f2175783d09412d6ac9463400Chris Lattner  llvm::GlobalVariable *
2248b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall  AddInitializerToStaticVarDecl(const VarDecl &D,
2249b6bbcc9995186799a60ce17d0c1acff31601653aJohn McCall                                llvm::GlobalVariable *GV);
22509cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
22510096acf421c4609ce7f43e8b05f8c5ca866d4611Daniel Dunbar
22523b2e16b3d25f6b311dba2871e2a566c96238c3d2Anders Carlsson  /// EmitCXXGlobalVarDeclInit - Create the initializer for a C++
22533b2e16b3d25f6b311dba2871e2a566c96238c3d2Anders Carlsson  /// variable with global storage.
22543b2e16b3d25f6b311dba2871e2a566c96238c3d2Anders Carlsson  void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::Constant *DeclPtr);
22553b2e16b3d25f6b311dba2871e2a566c96238c3d2Anders Carlsson
22563b2e16b3d25f6b311dba2871e2a566c96238c3d2Anders Carlsson  /// EmitCXXGlobalDtorRegistration - Emits a call to register the global ptr
22573b2e16b3d25f6b311dba2871e2a566c96238c3d2Anders Carlsson  /// with the C++ runtime so that its destructor will be called at exit.
225888f4280f6b60edcbdea6a38a3da8b17d66473ad6Fariborz Jahanian  void EmitCXXGlobalDtorRegistration(llvm::Constant *DtorFn,
22593b2e16b3d25f6b311dba2871e2a566c96238c3d2Anders Carlsson                                     llvm::Constant *DeclPtr);
22601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22613030eb82593097502469a8b3fc26112c79c75605John McCall  /// Emit code in this function to perform a guarded variable
22623030eb82593097502469a8b3fc26112c79c75605John McCall  /// initialization.  Guarded initializations are used when it's not
22633030eb82593097502469a8b3fc26112c79c75605John McCall  /// possible to prove that an initialization will be done exactly
22643030eb82593097502469a8b3fc26112c79c75605John McCall  /// once, e.g. with a static local variable or a static data member
22653030eb82593097502469a8b3fc26112c79c75605John McCall  /// of a class template.
22663030eb82593097502469a8b3fc26112c79c75605John McCall  void EmitCXXGuardedInit(const VarDecl &D, llvm::GlobalVariable *DeclPtr);
22675cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall
2268efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar  /// GenerateCXXGlobalInitFunc - Generates code for initializing global
2269efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar  /// variables.
2270efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar  void GenerateCXXGlobalInitFunc(llvm::Function *Fn,
2271efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar                                 llvm::Constant **Decls,
2272efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar                                 unsigned NumDecls);
2273efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar
2274efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar  /// GenerateCXXGlobalDtorFunc - Generates code for destroying global
2275efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar  /// variables.
2276efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar  void GenerateCXXGlobalDtorFunc(llvm::Function *Fn,
2277810112e28dc839715d17b0a786f23aaa19600ac0Chris Lattner                                 const std::vector<std::pair<llvm::WeakVH,
2278efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar                                   llvm::Constant*> > &DtorsAndObjects);
2279efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar
2280d26bc76c98006609002d9930f8840490e88ac5b5John McCall  void GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn,
2281d26bc76c98006609002d9930f8840490e88ac5b5John McCall                                        const VarDecl *D,
22823030eb82593097502469a8b3fc26112c79c75605John McCall                                        llvm::GlobalVariable *Addr);
2283efb0fa9e11f75af51744a6159530ef7cc8efa24aDaniel Dunbar
2284558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  void EmitCXXConstructExpr(const CXXConstructExpr *E, AggValueSlot Dest);
228534999876e215b22febc240b1a6dc054215d12f9cFariborz Jahanian
228634999876e215b22febc240b1a6dc054215d12f9cFariborz Jahanian  void EmitSynthesizedCXXCopyCtor(llvm::Value *Dest, llvm::Value *Src,
2287830937bc1100fba7682f7c32c40512085870f50cFariborz Jahanian                                  const Expr *Exp);
22881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22894765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall  RValue EmitExprWithCleanups(const ExprWithCleanups *E,
22904765fa05b5652fcc4356371c2f481d0ea9a1b007John McCall                              AggValueSlot Slot =AggValueSlot::ignored());
22911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2292756b5c4f9d52642d87d1948bee58f97a4f795b24Anders Carlsson  void EmitCXXThrowExpr(const CXXThrowExpr *E);
22931eb2e59338c4b9c0429fc39ca98662adc9e7a3f2Douglas Gregor
22940ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  //===--------------------------------------------------------------------===//
22950ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  //                             Internal Helpers
22960ffb125996336fc7602b162c0a9e392f1a93060fDaniel Dunbar  //===--------------------------------------------------------------------===//
22970dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
22980946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  /// ContainsLabel - Return true if the statement contains a label in it.  If
22990946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  /// this statement is not executed normally, it not containing a label means
23000946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  /// that we can just remove the code.
23010946ccd1e58c1f1da31ddbca67c5b6301ac8b255Chris Lattner  static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts = false);
23020dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
2303ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  /// containsBreak - Return true if the statement contains a break out of it.
2304ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  /// If the statement (recursively) contains a switch or loop with a break
2305ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  /// inside of it, this is fine.
2306ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  static bool containsBreak(const Stmt *S);
2307ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
23084bc0455d826e9634b7a68c0d06146edd633c9453Daniel Dunbar  /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
2309c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner  /// to a constant, or if it does but contains a label, return false.  If it
2310c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner  /// constant folds return true and set the boolean result in Result.
2311c2c90011a688c04a4e980282f08c267e081c4b00Chris Lattner  bool ConstantFoldsToSimpleInteger(const Expr *Cond, bool &Result);
23120dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
2313ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  /// ConstantFoldsToSimpleInteger - If the specified expression does not fold
2314ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  /// to a constant, or if it does but contains a label, return false.  If it
2315ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  /// constant folds return true and set the folded value.
2316ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner  bool ConstantFoldsToSimpleInteger(const Expr *Cond, llvm::APInt &Result);
2317ef425a69006afaa87751ee41ccf8ff405d9ede70Chris Lattner
231831a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  /// EmitBranchOnBoolExpr - Emit a branch on a boolean condition (e.g. for an
231931a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  /// if statement) to the specified blocks.  Based on the condition, this might
232031a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattner  /// try to simplify the codegen of the conditional based on the branch.
23219bc47e29dce8f095be7a6d07dbb02a5a7a112949Chris Lattner  void EmitBranchOnBoolExpr(const Expr *Cond, llvm::BasicBlock *TrueBlock,
23224bc0455d826e9634b7a68c0d06146edd633c9453Daniel Dunbar                            llvm::BasicBlock *FalseBlock);
2323be07f60131bc6f8d6696f4644df1ef667a1730d5Mike Stump
232415037caa1542bb810ad54c653aeb80f61df7b00cMike Stump  /// getTrapBB - Create a basic block that will call the trap intrinsic.  We'll
232515037caa1542bb810ad54c653aeb80f61df7b00cMike Stump  /// generate a branch around the created basic block as necessary.
23266c552c1d5f47fbba00e6268d96a26ad026f2da2aChris Lattner  llvm::BasicBlock *getTrapBB();
23279cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer
232821c9ad9d29d08a287292c670e7c52bc522c7f8bbAnders Carlsson  /// EmitCallArg - Emit a single call argument.
2329413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  void EmitCallArg(CallArgList &args, const Expr *E, QualType ArgType);
233021c9ad9d29d08a287292c670e7c52bc522c7f8bbAnders Carlsson
23312736071ea3a46f90e65c93418961611d96c10ab9John McCall  /// EmitDelegateCallArg - We are performing a delegate call; that
23322736071ea3a46f90e65c93418961611d96c10ab9John McCall  /// is, the current function is delegating to another one.  Produce
23332736071ea3a46f90e65c93418961611d96c10ab9John McCall  /// a r-value suitable for passing the given parameter.
2334413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall  void EmitDelegateCallArg(CallArgList &args, const VarDecl *param);
23352736071ea3a46f90e65c93418961611d96c10ab9John McCall
233631a0984b5cb4af99d2407c0f25bf5af68df681c6Chris Lattnerprivate:
233729e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar  void EmitReturnOfRValue(RValue RV, QualType Ty);
233829e0bccf2bcce22b877f8b2ed173f564c116b97eDaniel Dunbar
23395627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  /// ExpandTypeFromArgs - Reconstruct a structure of type \arg Ty
23405627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  /// from function arguments into \arg Dst. See ABIArgInfo::Expand.
23415627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  ///
23425627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  /// \param AI - The first function argument of the expansion.
23435627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  /// \return The argument following the last expanded function
23445627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  /// argument.
23450dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  llvm::Function::arg_iterator
23465627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar  ExpandTypeFromArgs(QualType Ty, LValue Dst,
23475627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar                     llvm::Function::arg_iterator AI);
23485627377df5439a1d3d46a4e4cef4ae44f84a322bDaniel Dunbar
23490dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// ExpandTypeToArgs - Expand an RValue \arg Src, with the LLVM type for \arg
23500dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// Ty, into individual arguments on the provided vector \arg Args. See
23510dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  /// ABIArgInfo::Expand.
23520dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump  void ExpandTypeToArgs(QualType Ty, RValue Src,
2353811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner                        llvm::SmallVector<llvm::Value*, 16> &Args,
2354811bf3669f4d82c57fe3cd3c49050fdbc95d0affChris Lattner                        llvm::FunctionType *IRFuncTy);
2355c8c7b187adcd989e01a9fba1ebead6c2abd770c9Anders Carlsson
23561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  llvm::Value* EmitAsmInput(const AsmStmt &S,
2357b84e8a670ad1d00c0a34295c42ac700cdde04815Daniel Dunbar                            const TargetInfo::ConstraintInfo &Info,
2358c8c7b187adcd989e01a9fba1ebead6c2abd770c9Anders Carlsson                            const Expr *InputExpr, std::string &ConstraintStr);
23590dd9e88aa972d80025c66fb3d7df0266b44cad1aMike Stump
23606d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman  llvm::Value* EmitAsmInputLValue(const AsmStmt &S,
23616d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                  const TargetInfo::ConstraintInfo &Info,
23626d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                  LValue InputValue, QualType InputType,
23636d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman                                  std::string &ConstraintStr);
23646d7cfd7ef82e42ff30ee1dafd2883fd94e9f8294Eli Friedman
23650139bb96494b4c4ba0824617d5d2495dc7e44c76Anders Carlsson  /// EmitCallArgs - Emit call arguments for a function.
23661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  /// The CallArgTypeInfo parameter is used for iterating over the known
2367af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson  /// argument types of the function being called.
2368af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson  template<typename T>
2369af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson  void EmitCallArgs(CallArgList& Args, const T* CallArgTypeInfo,
23700139bb96494b4c4ba0824617d5d2495dc7e44c76Anders Carlsson                    CallExpr::const_arg_iterator ArgBeg,
2371af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson                    CallExpr::const_arg_iterator ArgEnd) {
2372af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson      CallExpr::const_arg_iterator Arg = ArgBeg;
2373af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson
2374af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson    // First, use the argument types that the type info knows about
2375af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson    if (CallArgTypeInfo) {
2376af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson      for (typename T::arg_type_iterator I = CallArgTypeInfo->arg_type_begin(),
2377af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson           E = CallArgTypeInfo->arg_type_end(); I != E; ++I, ++Arg) {
237844b0a3e858af7b19f489619e52cd58970611cd15Eli Friedman        assert(Arg != ArgEnd && "Running over edge of argument list!");
2379af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson        QualType ArgType = *I;
2380745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian#ifndef NDEBUG
2381745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian        QualType ActualArgType = Arg->getType();
2382745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian        if (ArgType->isPointerType() && ActualArgType->isPointerType()) {
23839cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer          QualType ActualBaseType =
2384745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian            ActualArgType->getAs<PointerType>()->getPointeeType();
23859cac4942b920d4c5514e71949e3062ed626bfbdfMichael J. Spencer          QualType ArgBaseType =
2386745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian            ArgType->getAs<PointerType>()->getPointeeType();
2387745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian          if (ArgBaseType->isVariableArrayType()) {
2388745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian            if (const VariableArrayType *VAT =
2389745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian                getContext().getAsVariableArrayType(ActualBaseType)) {
2390745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian              if (!VAT->getSizeExpr())
2391745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian                ActualArgType = ArgType;
2392745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian            }
2393745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian          }
2394745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian        }
2395af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson        assert(getContext().getCanonicalType(ArgType.getNonReferenceType()).
23961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump               getTypePtr() ==
2397745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian               getContext().getCanonicalType(ActualArgType).getTypePtr() &&
2398af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson               "type mismatch in call argument!");
2399745da3a5bb4ea35f93f50301e7fbbb7d78d3b6bbFariborz Jahanian#endif
2400413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall        EmitCallArg(Args, *Arg, ArgType);
2401af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson      }
24021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
24031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // Either we've emitted all the call args, or we have a call to a
2404af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson      // variadic function.
24051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert((Arg == ArgEnd || CallArgTypeInfo->isVariadic()) &&
2406af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson             "Extra arguments in non-variadic function!");
24071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2408af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson    }
24091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2410af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson    // If we still have any arguments, emit them using the type of the argument.
2411413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall    for (; Arg != ArgEnd; ++Arg)
2412413ebdb1af6fb0d81845b61254daf02ba0449afdJohn McCall      EmitCallArg(Args, *Arg, Arg->getType());
2413af23f69aa6247e6bd20a66b2e16babb91f192535Anders Carlsson  }
2414492c4f998d848673d3d6c9e6416115df4036a71dJohn McCall
2415492c4f998d848673d3d6c9e6416115df4036a71dJohn McCall  const TargetCodeGenInfo &getTargetHooks() const {
2416492c4f998d848673d3d6c9e6416115df4036a71dJohn McCall    return CGM.getTargetCodeGenInfo();
2417492c4f998d848673d3d6c9e6416115df4036a71dJohn McCall  }
2418744016dde06fcffd50931e94a98c850f8b12cd87John McCall
2419744016dde06fcffd50931e94a98c850f8b12cd87John McCall  void EmitDeclMetadata();
2420f0c11f7e6848f023ced6a5b51399ba787c7d4d0bJohn McCall
2421f0c11f7e6848f023ced6a5b51399ba787c7d4d0bJohn McCall  CodeGenModule::ByrefHelpers *
2422f0c11f7e6848f023ced6a5b51399ba787c7d4d0bJohn McCall  buildByrefHelpers(const llvm::StructType &byrefType,
2423f0c11f7e6848f023ced6a5b51399ba787c7d4d0bJohn McCall                    const AutoVarEmission &emission);
24245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
24251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2426150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall/// Helper class with most of the code for saving a value for a
2427150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall/// conditional expression cleanup.
2428804b807ea918184d6de63bd745e1ff75a9bfc679John McCallstruct DominatingLLVMValue {
2429150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  typedef llvm::PointerIntPair<llvm::Value*, 1, bool> saved_type;
2430150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
2431150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// Answer whether the given value needs extra work to be saved.
2432150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  static bool needsSaving(llvm::Value *value) {
2433150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    // If it's not an instruction, we don't need to save.
2434150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    if (!isa<llvm::Instruction>(value)) return false;
2435150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
2436150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    // If it's an instruction in the entry block, we don't need to save.
2437150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    llvm::BasicBlock *block = cast<llvm::Instruction>(value)->getParent();
2438150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    return (block != &block->getParent()->getEntryBlock());
2439150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  }
2440150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
2441150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  /// Try to save the given value.
2442150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  static saved_type save(CodeGenFunction &CGF, llvm::Value *value) {
2443150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    if (!needsSaving(value)) return saved_type(value, false);
2444150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
2445150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    // Otherwise we need an alloca.
2446150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    llvm::Value *alloca =
2447150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall      CGF.CreateTempAlloca(value->getType(), "cond-cleanup.save");
2448150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    CGF.Builder.CreateStore(value, alloca);
2449150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
2450150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    return saved_type(alloca, true);
2451150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  }
2452150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
2453150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  static llvm::Value *restore(CodeGenFunction &CGF, saved_type value) {
2454150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    if (!value.getInt()) return value.getPointer();
2455150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall    return CGF.Builder.CreateLoad(value.getPointer());
2456150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  }
2457150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall};
2458150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
2459804b807ea918184d6de63bd745e1ff75a9bfc679John McCall/// A partial specialization of DominatingValue for llvm::Values that
2460804b807ea918184d6de63bd745e1ff75a9bfc679John McCall/// might be llvm::Instructions.
2461804b807ea918184d6de63bd745e1ff75a9bfc679John McCalltemplate <class T> struct DominatingPointer<T,true> : DominatingLLVMValue {
2462804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  typedef T *type;
2463804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  static type restore(CodeGenFunction &CGF, saved_type value) {
2464804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    return static_cast<T*>(DominatingLLVMValue::restore(CGF, value));
2465804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  }
2466804b807ea918184d6de63bd745e1ff75a9bfc679John McCall};
2467804b807ea918184d6de63bd745e1ff75a9bfc679John McCall
2468804b807ea918184d6de63bd745e1ff75a9bfc679John McCall/// A specialization of DominatingValue for RValue.
2469804b807ea918184d6de63bd745e1ff75a9bfc679John McCalltemplate <> struct DominatingValue<RValue> {
2470804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  typedef RValue type;
2471804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  class saved_type {
2472804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    enum Kind { ScalarLiteral, ScalarAddress, AggregateLiteral,
2473804b807ea918184d6de63bd745e1ff75a9bfc679John McCall                AggregateAddress, ComplexAddress };
2474804b807ea918184d6de63bd745e1ff75a9bfc679John McCall
2475804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    llvm::Value *Value;
2476804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    Kind K;
2477804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    saved_type(llvm::Value *v, Kind k) : Value(v), K(k) {}
2478804b807ea918184d6de63bd745e1ff75a9bfc679John McCall
2479804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  public:
2480804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    static bool needsSaving(RValue value);
2481804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    static saved_type save(CodeGenFunction &CGF, RValue value);
2482804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    RValue restore(CodeGenFunction &CGF);
2483804b807ea918184d6de63bd745e1ff75a9bfc679John McCall
2484804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    // implementations in CGExprCXX.cpp
2485804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  };
2486804b807ea918184d6de63bd745e1ff75a9bfc679John McCall
2487804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  static bool needsSaving(type value) {
2488804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    return saved_type::needsSaving(value);
2489804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  }
2490804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  static saved_type save(CodeGenFunction &CGF, type value) {
2491804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    return saved_type::save(CGF, value);
2492804b807ea918184d6de63bd745e1ff75a9bfc679John McCall  }
2493150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  static type restore(CodeGenFunction &CGF, saved_type value) {
2494804b807ea918184d6de63bd745e1ff75a9bfc679John McCall    return value.restore(CGF);
2495150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall  }
2496150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall};
2497150b462afc7a713edd19bcbbbb22381fe060d4f5John McCall
24985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace CodeGen
24995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}  // end namespace clang
25005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
25015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#endif
2502