CGCall.h revision b5a03b6217cd6c3eecdc1aa4df03060cf51ac942
1//===----- CGCall.h - Encapsulate calling convention details ----*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These classes wrap the information about a call or function
11// definition used to handle ABI compliancy.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef CLANG_CODEGEN_CGCALL_H
16#define CLANG_CODEGEN_CGCALL_H
17
18#include "llvm/ADT/FoldingSet.h"
19#include "llvm/Value.h"
20#include "clang/AST/Type.h"
21#include "clang/AST/CanonicalType.h"
22
23#include "CGValue.h"
24
25// FIXME: Restructure so we don't have to expose so much stuff.
26#include "ABIInfo.h"
27
28namespace llvm {
29  struct AttributeWithIndex;
30  class Function;
31  class Type;
32  class Value;
33
34  template<typename T, unsigned> class SmallVector;
35}
36
37namespace clang {
38  class ASTContext;
39  class Decl;
40  class FunctionDecl;
41  class ObjCMethodDecl;
42  class VarDecl;
43
44namespace CodeGen {
45  typedef llvm::SmallVector<llvm::AttributeWithIndex, 8> AttributeListType;
46
47  struct CallArg {
48    RValue RV;
49    QualType Ty;
50    bool NeedsCopy;
51    CallArg(RValue rv, QualType ty, bool needscopy)
52    : RV(rv), Ty(ty), NeedsCopy(needscopy)
53    { }
54  };
55
56  /// CallArgList - Type for representing both the value and type of
57  /// arguments in a call.
58  class CallArgList :
59    public llvm::SmallVector<CallArg, 16> {
60  public:
61    void add(RValue rvalue, QualType type, bool needscopy = false) {
62      push_back(CallArg(rvalue, type, needscopy));
63    }
64  };
65
66  /// FunctionArgList - Type for representing both the decl and type
67  /// of parameters to a function. The decl must be either a
68  /// ParmVarDecl or ImplicitParamDecl.
69  class FunctionArgList : public llvm::SmallVector<const VarDecl*, 16> {
70  };
71
72  /// CGFunctionInfo - Class to encapsulate the information about a
73  /// function definition.
74  class CGFunctionInfo : public llvm::FoldingSetNode {
75    struct ArgInfo {
76      CanQualType type;
77      ABIArgInfo info;
78    };
79
80    /// The LLVM::CallingConv to use for this function (as specified by the
81    /// user).
82    unsigned CallingConvention;
83
84    /// The LLVM::CallingConv to actually use for this function, which may
85    /// depend on the ABI.
86    unsigned EffectiveCallingConvention;
87
88    /// Whether this function is noreturn.
89    bool NoReturn;
90
91    unsigned NumArgs;
92    ArgInfo *Args;
93
94    /// How many arguments to pass inreg.
95    bool HasRegParm;
96    unsigned RegParm;
97
98  public:
99    typedef const ArgInfo *const_arg_iterator;
100    typedef ArgInfo *arg_iterator;
101
102    CGFunctionInfo(unsigned CallingConvention, bool NoReturn,
103                   bool HasRegParm, unsigned RegParm, CanQualType ResTy,
104                   const CanQualType *ArgTys, unsigned NumArgTys);
105    ~CGFunctionInfo() { delete[] Args; }
106
107    const_arg_iterator arg_begin() const { return Args + 1; }
108    const_arg_iterator arg_end() const { return Args + 1 + NumArgs; }
109    arg_iterator arg_begin() { return Args + 1; }
110    arg_iterator arg_end() { return Args + 1 + NumArgs; }
111
112    unsigned  arg_size() const { return NumArgs; }
113
114    bool isNoReturn() const { return NoReturn; }
115
116    /// getCallingConvention - Return the user specified calling
117    /// convention.
118    unsigned getCallingConvention() const { return CallingConvention; }
119
120    /// getEffectiveCallingConvention - Return the actual calling convention to
121    /// use, which may depend on the ABI.
122    unsigned getEffectiveCallingConvention() const {
123      return EffectiveCallingConvention;
124    }
125    void setEffectiveCallingConvention(unsigned Value) {
126      EffectiveCallingConvention = Value;
127    }
128
129    bool getHasRegParm() const { return HasRegParm; }
130    unsigned getRegParm() const { return RegParm; }
131
132    CanQualType getReturnType() const { return Args[0].type; }
133
134    ABIArgInfo &getReturnInfo() { return Args[0].info; }
135    const ABIArgInfo &getReturnInfo() const { return Args[0].info; }
136
137    void Profile(llvm::FoldingSetNodeID &ID) {
138      ID.AddInteger(getCallingConvention());
139      ID.AddBoolean(NoReturn);
140      ID.AddBoolean(HasRegParm);
141      ID.AddInteger(RegParm);
142      getReturnType().Profile(ID);
143      for (arg_iterator it = arg_begin(), ie = arg_end(); it != ie; ++it)
144        it->type.Profile(ID);
145    }
146    template<class Iterator>
147    static void Profile(llvm::FoldingSetNodeID &ID,
148                        const FunctionType::ExtInfo &Info,
149                        CanQualType ResTy,
150                        Iterator begin,
151                        Iterator end) {
152      ID.AddInteger(Info.getCC());
153      ID.AddBoolean(Info.getNoReturn());
154      ID.AddBoolean(Info.getHasRegParm());
155      ID.AddInteger(Info.getRegParm());
156      ResTy.Profile(ID);
157      for (; begin != end; ++begin) {
158        CanQualType T = *begin; // force iterator to be over canonical types
159        T.Profile(ID);
160      }
161    }
162  };
163
164  /// ReturnValueSlot - Contains the address where the return value of a
165  /// function can be stored, and whether the address is volatile or not.
166  class ReturnValueSlot {
167    llvm::PointerIntPair<llvm::Value *, 1, bool> Value;
168
169  public:
170    ReturnValueSlot() {}
171    ReturnValueSlot(llvm::Value *Value, bool IsVolatile)
172      : Value(Value, IsVolatile) {}
173
174    bool isNull() const { return !getValue(); }
175
176    bool isVolatile() const { return Value.getInt(); }
177    llvm::Value *getValue() const { return Value.getPointer(); }
178  };
179
180}  // end namespace CodeGen
181}  // end namespace clang
182
183#endif
184