405a632d359381a283257a3fffc89612295b14f1
[oota-llvm.git] / include / llvm / DerivedTypes.h
1 //===-- llvm/DerivedTypes.h - Classes for handling data types ----*- C++ -*--=//
2 //
3 // This file contains the declarations of classes that represent "derived 
4 // types".  These are things like "arrays of x" or "structure of x, y, z" or
5 // "method returning x taking (y,z) as parameters", etc...
6 //
7 // The implementations of these classes live in the Type.cpp file.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef LLVM_DERIVED_TYPES_H
12 #define LLVM_DERIVED_TYPES_H
13
14 #include "llvm/Type.h"
15 #include "llvm/CodeGen/TargetMachine.h"
16 #include <vector>
17
18 // Future derived types: SIMD packed format
19
20
21 class MethodType : public Type {
22 public:
23   typedef vector<const Type*> ParamTypes;
24 private:
25   const Type *ResultType;
26   ParamTypes ParamTys;
27
28   MethodType(const MethodType &);                   // Do not implement
29   const MethodType &operator=(const MethodType &);  // Do not implement
30 protected:
31   // This should really be private, but it squelches a bogus warning
32   // from GCC to make them protected:  warning: `class MethodType' only 
33   // defines private constructors and has no friends
34
35   // Private ctor - Only can be created by a static member...
36   MethodType(const Type *Result, const vector<const Type*> &Params, 
37              const string &Name);
38 public:
39
40   inline const Type *getReturnType() const { return ResultType; }
41   inline const ParamTypes &getParamTypes() const { return ParamTys; }
42
43   static const MethodType *getMethodType(const Type *Result, 
44                                          const ParamTypes &Params);
45   static const MethodType *get(const Type *Result, const ParamTypes &Params) {
46     return getMethodType(Result, Params);
47   }
48 };
49
50
51
52 class ArrayType : public Type {
53 private:
54   const Type *ElementType;
55   int NumElements;       // >= 0 for sized array, -1 for unbounded/unknown array
56
57   ArrayType(const ArrayType &);                   // Do not implement
58   const ArrayType &operator=(const ArrayType &);  // Do not implement
59 protected:
60   // This should really be private, but it squelches a bogus warning
61   // from GCC to make them protected:  warning: `class ArrayType' only 
62   // defines private constructors and has no friends
63
64
65   // Private ctor - Only can be created by a static member...
66   ArrayType(const Type *ElType, int NumEl, const string &Name);
67 public:
68
69   inline const Type *getElementType() const { return ElementType; }
70   inline int         getNumElements() const { return NumElements; }
71
72   inline bool isSized()   const { return NumElements >= 0; }
73   inline bool isUnsized() const { return NumElements == -1; }
74
75   static const ArrayType *getArrayType(const Type *ElementType, 
76                                        int NumElements = -1);
77   static const ArrayType *get(const Type *ElementType, int NumElements = -1) {
78     return getArrayType(ElementType, NumElements);
79   }
80 };
81
82 class StructType : public Type {
83 public:
84   typedef vector<const Type*> ElementTypes;
85
86 private:
87   ElementTypes ETypes;
88   struct StructSizeAndOffsetInfo {
89     int storageSize;                    // -1 until the value is computd
90     vector<int> memberOffsets;          // -1 until values are computed 
91     const TargetMachine* targetInfo;
92   }
93   *layoutCache;
94   
95 private:
96   StructType(const StructType &);                   // Do not implement
97   const StructType &operator=(const StructType &);  // Do not implement
98   
99 protected:
100   // This should really be private, but it squelches a bogus warning
101   // from GCC to make them protected:  warning: `class StructType' only 
102   // defines private constructors and has no friends
103
104   // Private ctor - Only can be created by a static member...
105   StructType(const vector<const Type*> &Types, const string &Name);
106   
107   // Reset cached info so it will be computed when first requested
108   void ResetCachedInfo() const;
109   
110 public:
111
112   inline const ElementTypes &getElementTypes() const { return ETypes; }
113   static const StructType *getStructType(const ElementTypes &Params);
114   static const StructType *get(const ElementTypes &Params) {
115     return getStructType(Params);
116   }
117   unsigned int             getStorageSize(const TargetMachine& tmi) const;
118   unsigned int             getElementOffset(int i, const TargetMachine& tmi) const;
119 };
120
121
122 inline unsigned int
123 StructType::getStorageSize(const TargetMachine& tmi) const
124 {
125   if (layoutCache->targetInfo != NULL && ! (* layoutCache->targetInfo == tmi))
126     {// target machine has changed (hey it could happen). discard cached info.
127       ResetCachedInfo();
128       layoutCache->targetInfo = &tmi;
129     }
130   
131   if (layoutCache->storageSize < 0)
132     {
133       layoutCache->storageSize = tmi.findOptimalStorageSize(this);
134       assert(layoutCache->storageSize >= 0);
135     }
136   
137   return layoutCache->storageSize;
138 }
139
140
141 inline unsigned int
142 StructType::getElementOffset(int i, const TargetMachine& tmi) const
143 {
144   if (layoutCache->targetInfo != NULL && ! (* layoutCache->targetInfo == tmi))
145     {// target machine has changed (hey it could happen). discard cached info.
146       ResetCachedInfo();
147     }
148   
149   if (layoutCache->memberOffsets[i] < 0)
150     {
151       layoutCache->targetInfo = &tmi;   // remember which target was used
152       
153       unsigned int* offsetVec = tmi.findOptimalMemberOffsets(this);
154       for (unsigned i=0, N=layoutCache->memberOffsets.size(); i < N; i++)
155         {
156           layoutCache->memberOffsets[i] = offsetVec[i];
157           assert(layoutCache->memberOffsets[i] >= 0);
158         }
159       delete[] offsetVec; 
160     }
161   
162   return layoutCache->memberOffsets[i];
163 }
164
165
166 inline void
167 StructType::ResetCachedInfo() const
168 {
169   layoutCache->storageSize = -1;
170   layoutCache->memberOffsets.insert(layoutCache->memberOffsets.begin(),
171                                     ETypes.size(), -1);
172   layoutCache->targetInfo = NULL;
173 }
174
175
176 class PointerType : public Type {
177 private:
178   const Type *ValueType;
179
180   PointerType(const PointerType &);                   // Do not implement
181   const PointerType &operator=(const PointerType &);  // Do not implement
182 protected:
183   // This should really be private, but it squelches a bogus warning
184   // from GCC to make them protected:  warning: `class PointerType' only 
185   // defines private constructors and has no friends
186
187
188   // Private ctor - Only can be created by a static member...
189   PointerType(const Type *ElType);
190 public:
191
192   inline const Type *getValueType() const { return ValueType; }
193
194
195   static const PointerType *getPointerType(const Type *ElementType);
196   static const PointerType *get(const Type *ElementType) {
197     return getPointerType(ElementType);
198   }
199 };
200
201 #endif