clang API Documentation

CGCall.h
Go to the documentation of this file.
00001 //===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // These classes wrap the information about a call or function
00011 // definition used to handle ABI compliancy.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef CLANG_CODEGEN_CGCALL_H
00016 #define CLANG_CODEGEN_CGCALL_H
00017 
00018 #include "llvm/ADT/FoldingSet.h"
00019 #include "llvm/Value.h"
00020 #include "clang/AST/Type.h"
00021 #include "clang/AST/CanonicalType.h"
00022 
00023 #include "CGValue.h"
00024 
00025 // FIXME: Restructure so we don't have to expose so much stuff.
00026 #include "ABIInfo.h"
00027 
00028 namespace llvm {
00029   struct AttributeWithIndex;
00030   class Function;
00031   class Type;
00032   class Value;
00033 
00034   template<typename T, unsigned> class SmallVector;
00035 }
00036 
00037 namespace clang {
00038   class ASTContext;
00039   class Decl;
00040   class FunctionDecl;
00041   class ObjCMethodDecl;
00042   class VarDecl;
00043 
00044 namespace CodeGen {
00045   typedef SmallVector<llvm::AttributeWithIndex, 8> AttributeListType;
00046 
00047   struct CallArg {
00048     RValue RV;
00049     QualType Ty;
00050     bool NeedsCopy;
00051     CallArg(RValue rv, QualType ty, bool needscopy)
00052     : RV(rv), Ty(ty), NeedsCopy(needscopy)
00053     { }
00054   };
00055 
00056   /// CallArgList - Type for representing both the value and type of
00057   /// arguments in a call.
00058   class CallArgList :
00059     public SmallVector<CallArg, 16> {
00060   public:
00061     struct Writeback {
00062       /// The original argument.
00063       llvm::Value *Address;
00064 
00065       /// The pointee type of the original argument.
00066       QualType AddressType;
00067 
00068       /// The temporary alloca.
00069       llvm::Value *Temporary;
00070     };
00071 
00072     void add(RValue rvalue, QualType type, bool needscopy = false) {
00073       push_back(CallArg(rvalue, type, needscopy));
00074     }
00075 
00076     void addFrom(const CallArgList &other) {
00077       insert(end(), other.begin(), other.end());
00078       Writebacks.insert(Writebacks.end(),
00079                         other.Writebacks.begin(), other.Writebacks.end());
00080     }
00081 
00082     void addWriteback(llvm::Value *address, QualType addressType,
00083                       llvm::Value *temporary) {
00084       Writeback writeback;
00085       writeback.Address = address;
00086       writeback.AddressType = addressType;
00087       writeback.Temporary = temporary;
00088       Writebacks.push_back(writeback);
00089     }
00090 
00091     bool hasWritebacks() const { return !Writebacks.empty(); }
00092 
00093     typedef SmallVectorImpl<Writeback>::const_iterator writeback_iterator;
00094     writeback_iterator writeback_begin() const { return Writebacks.begin(); }
00095     writeback_iterator writeback_end() const { return Writebacks.end(); }
00096 
00097   private:
00098     SmallVector<Writeback, 1> Writebacks;
00099   };
00100 
00101   /// A class for recording the number of arguments that a function
00102   /// signature requires.
00103   class RequiredArgs {
00104     /// The number of required arguments, or ~0 if the signature does
00105     /// not permit optional arguments.
00106     unsigned NumRequired;
00107   public:
00108     enum All_t { All };
00109 
00110     RequiredArgs(All_t _) : NumRequired(~0U) {}
00111     explicit RequiredArgs(unsigned n) : NumRequired(n) {
00112       assert(n != ~0U);
00113     }
00114 
00115     /// Compute the arguments required by the given formal prototype,
00116     /// given that there may be some additional, non-formal arguments
00117     /// in play.
00118     static RequiredArgs forPrototypePlus(const FunctionProtoType *prototype,
00119                                          unsigned additional) {
00120       if (!prototype->isVariadic()) return All;
00121       return RequiredArgs(prototype->getNumArgs() + additional);
00122     }
00123 
00124     static RequiredArgs forPrototype(const FunctionProtoType *prototype) {
00125       return forPrototypePlus(prototype, 0);
00126     }
00127 
00128     static RequiredArgs forPrototype(CanQual<FunctionProtoType> prototype) {
00129       return forPrototype(prototype.getTypePtr());
00130     }
00131 
00132     static RequiredArgs forPrototypePlus(CanQual<FunctionProtoType> prototype,
00133                                          unsigned additional) {
00134       return forPrototypePlus(prototype.getTypePtr(), additional);
00135     }
00136 
00137     bool allowsOptionalArgs() const { return NumRequired != ~0U; }
00138     bool getNumRequiredArgs() const {
00139       assert(allowsOptionalArgs());
00140       return NumRequired;
00141     }
00142 
00143     unsigned getOpaqueData() const { return NumRequired; }
00144     static RequiredArgs getFromOpaqueData(unsigned value) {
00145       if (value == ~0U) return All;
00146       return RequiredArgs(value);
00147     }
00148   };
00149 
00150   /// FunctionArgList - Type for representing both the decl and type
00151   /// of parameters to a function. The decl must be either a
00152   /// ParmVarDecl or ImplicitParamDecl.
00153   class FunctionArgList : public SmallVector<const VarDecl*, 16> {
00154   };
00155 
00156   /// CGFunctionInfo - Class to encapsulate the information about a
00157   /// function definition.
00158   class CGFunctionInfo : public llvm::FoldingSetNode {
00159     struct ArgInfo {
00160       CanQualType type;
00161       ABIArgInfo info;
00162     };
00163 
00164     /// The LLVM::CallingConv to use for this function (as specified by the
00165     /// user).
00166     unsigned CallingConvention : 8;
00167 
00168     /// The LLVM::CallingConv to actually use for this function, which may
00169     /// depend on the ABI.
00170     unsigned EffectiveCallingConvention : 8;
00171 
00172     /// The clang::CallingConv that this was originally created with.
00173     unsigned ASTCallingConvention : 8;
00174 
00175     /// Whether this function is noreturn.
00176     unsigned NoReturn : 1;
00177 
00178     /// Whether this function is returns-retained.
00179     unsigned ReturnsRetained : 1;
00180 
00181     /// How many arguments to pass inreg.
00182     unsigned HasRegParm : 1;
00183     unsigned RegParm : 4;
00184 
00185     RequiredArgs Required;
00186 
00187     unsigned NumArgs;
00188     ArgInfo *getArgsBuffer() {
00189       return reinterpret_cast<ArgInfo*>(this+1);
00190     }
00191     const ArgInfo *getArgsBuffer() const {
00192       return reinterpret_cast<const ArgInfo*>(this + 1);
00193     }
00194 
00195     CGFunctionInfo() : Required(RequiredArgs::All) {}
00196 
00197   public:
00198     static CGFunctionInfo *create(unsigned llvmCC,
00199                                   const FunctionType::ExtInfo &extInfo,
00200                                   CanQualType resultType,
00201                                   ArrayRef<CanQualType> argTypes,
00202                                   RequiredArgs required);
00203 
00204     typedef const ArgInfo *const_arg_iterator;
00205     typedef ArgInfo *arg_iterator;
00206 
00207     const_arg_iterator arg_begin() const { return getArgsBuffer() + 1; }
00208     const_arg_iterator arg_end() const { return getArgsBuffer() + 1 + NumArgs; }
00209     arg_iterator arg_begin() { return getArgsBuffer() + 1; }
00210     arg_iterator arg_end() { return getArgsBuffer() + 1 + NumArgs; }
00211 
00212     unsigned  arg_size() const { return NumArgs; }
00213 
00214     bool isVariadic() const { return Required.allowsOptionalArgs(); }
00215     RequiredArgs getRequiredArgs() const { return Required; }
00216 
00217     bool isNoReturn() const { return NoReturn; }
00218 
00219     /// In ARC, whether this function retains its return value.  This
00220     /// is not always reliable for call sites.
00221     bool isReturnsRetained() const { return ReturnsRetained; }
00222 
00223     /// getASTCallingConvention() - Return the AST-specified calling
00224     /// convention.
00225     CallingConv getASTCallingConvention() const {
00226       return CallingConv(ASTCallingConvention);
00227     }
00228 
00229     /// getCallingConvention - Return the user specified calling
00230     /// convention, which has been translated into an LLVM CC.
00231     unsigned getCallingConvention() const { return CallingConvention; }
00232 
00233     /// getEffectiveCallingConvention - Return the actual calling convention to
00234     /// use, which may depend on the ABI.
00235     unsigned getEffectiveCallingConvention() const {
00236       return EffectiveCallingConvention;
00237     }
00238     void setEffectiveCallingConvention(unsigned Value) {
00239       EffectiveCallingConvention = Value;
00240     }
00241 
00242     bool getHasRegParm() const { return HasRegParm; }
00243     unsigned getRegParm() const { return RegParm; }
00244 
00245     FunctionType::ExtInfo getExtInfo() const {
00246       return FunctionType::ExtInfo(isNoReturn(),
00247                                    getHasRegParm(), getRegParm(),
00248                                    getASTCallingConvention(),
00249                                    isReturnsRetained());
00250     }
00251 
00252     CanQualType getReturnType() const { return getArgsBuffer()[0].type; }
00253 
00254     ABIArgInfo &getReturnInfo() { return getArgsBuffer()[0].info; }
00255     const ABIArgInfo &getReturnInfo() const { return getArgsBuffer()[0].info; }
00256 
00257     void Profile(llvm::FoldingSetNodeID &ID) {
00258       ID.AddInteger(getASTCallingConvention());
00259       ID.AddBoolean(NoReturn);
00260       ID.AddBoolean(ReturnsRetained);
00261       ID.AddBoolean(HasRegParm);
00262       ID.AddInteger(RegParm);
00263       ID.AddInteger(Required.getOpaqueData());
00264       getReturnType().Profile(ID);
00265       for (arg_iterator it = arg_begin(), ie = arg_end(); it != ie; ++it)
00266         it->type.Profile(ID);
00267     }
00268     static void Profile(llvm::FoldingSetNodeID &ID,
00269                         const FunctionType::ExtInfo &info,
00270                         RequiredArgs required,
00271                         CanQualType resultType,
00272                         ArrayRef<CanQualType> argTypes) {
00273       ID.AddInteger(info.getCC());
00274       ID.AddBoolean(info.getNoReturn());
00275       ID.AddBoolean(info.getProducesResult());
00276       ID.AddBoolean(info.getHasRegParm());
00277       ID.AddInteger(info.getRegParm());
00278       ID.AddInteger(required.getOpaqueData());
00279       resultType.Profile(ID);
00280       for (ArrayRef<CanQualType>::iterator
00281              i = argTypes.begin(), e = argTypes.end(); i != e; ++i) {
00282         i->Profile(ID);
00283       }
00284     }
00285   };
00286   
00287   /// ReturnValueSlot - Contains the address where the return value of a 
00288   /// function can be stored, and whether the address is volatile or not.
00289   class ReturnValueSlot {
00290     llvm::PointerIntPair<llvm::Value *, 1, bool> Value;
00291 
00292   public:
00293     ReturnValueSlot() {}
00294     ReturnValueSlot(llvm::Value *Value, bool IsVolatile)
00295       : Value(Value, IsVolatile) {}
00296 
00297     bool isNull() const { return !getValue(); }
00298     
00299     bool isVolatile() const { return Value.getInt(); }
00300     llvm::Value *getValue() const { return Value.getPointer(); }
00301   };
00302   
00303 }  // end namespace CodeGen
00304 }  // end namespace clang
00305 
00306 #endif