Fix coding style issues to actually attempt to be somewhat uniform
[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 class ArrayType : public Type {
52 private:
53   const Type *ElementType;
54   int NumElements;       // >= 0 for sized array, -1 for unbounded/unknown array
55
56   ArrayType(const ArrayType &);                   // Do not implement
57   const ArrayType &operator=(const ArrayType &);  // Do not implement
58 protected:
59   // This should really be private, but it squelches a bogus warning
60   // from GCC to make them protected:  warning: `class ArrayType' only 
61   // defines private constructors and has no friends
62
63
64   // Private ctor - Only can be created by a static member...
65   ArrayType(const Type *ElType, int NumEl, const string &Name);
66 public:
67
68   inline const Type *getElementType() const { return ElementType; }
69   inline int         getNumElements() const { return NumElements; }
70
71   inline bool isSized()   const { return NumElements >= 0; }
72   inline bool isUnsized() const { return NumElements == -1; }
73
74   static const ArrayType *getArrayType(const Type *ElementType, 
75                                        int NumElements = -1);
76   static const ArrayType *get(const Type *ElementType, int NumElements = -1) {
77     return getArrayType(ElementType, NumElements);
78   }
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   } *layoutCache;
93   
94 private:
95   StructType(const StructType &);                   // Do not implement
96   const StructType &operator=(const StructType &);  // Do not implement
97   
98 protected:
99   // This should really be private, but it squelches a bogus warning
100   // from GCC to make them protected:  warning: `class StructType' only 
101   // defines private constructors and has no friends
102
103   // Private ctor - Only can be created by a static member...
104   StructType(const vector<const Type*> &Types, const string &Name);
105   
106   // Reset cached info so it will be computed when first requested
107   void ResetCachedInfo() const {
108     layoutCache->storageSize = -1;
109     layoutCache->memberOffsets.insert(layoutCache->memberOffsets.begin(),
110                                       ETypes.size(), -1);
111     layoutCache->targetInfo = 0;
112   }
113   
114 public:
115   inline const ElementTypes &getElementTypes() const { return ETypes; }
116   static const StructType *getStructType(const ElementTypes &Params);
117   static const StructType *get(const ElementTypes &Params) {
118     return getStructType(Params);
119   }
120
121
122   unsigned int getStorageSize(const TargetMachine& tmi) const {
123     if (layoutCache->targetInfo && *layoutCache->targetInfo != tmi) {
124       // target machine has changed (hey it could happen). discard cached info.
125       ResetCachedInfo();
126       layoutCache->targetInfo = &tmi;
127     }
128   
129     if (layoutCache->storageSize < 0) {
130       layoutCache->storageSize = tmi.findOptimalStorageSize(this);
131       assert(layoutCache->storageSize >= 0);
132     }
133     return layoutCache->storageSize;
134   }
135   unsigned int getElementOffset(int i, const TargetMachine& tmi) const {
136     // target machine has changed (hey it could happen). discard cached info.
137     if (layoutCache->targetInfo && *layoutCache->targetInfo != tmi)
138       ResetCachedInfo();
139   
140     if (layoutCache->memberOffsets[i] < 0) {
141       layoutCache->targetInfo = &tmi;  // remember which target was used
142       
143       unsigned int *offsetVec = tmi.findOptimalMemberOffsets(this);
144       for (unsigned i=0, N=layoutCache->memberOffsets.size(); i < N; ++i) {
145         layoutCache->memberOffsets[i] = offsetVec[i];
146         assert(layoutCache->memberOffsets[i] >= 0);
147       }
148       delete[] offsetVec; 
149     }
150     
151     return layoutCache->memberOffsets[i];
152   }
153 };
154
155
156 class PointerType : public Type {
157 private:
158   const Type *ValueType;
159
160   PointerType(const PointerType &);                   // Do not implement
161   const PointerType &operator=(const PointerType &);  // Do not implement
162 protected:
163   // This should really be private, but it squelches a bogus warning
164   // from GCC to make them protected:  warning: `class PointerType' only 
165   // defines private constructors and has no friends
166
167
168   // Private ctor - Only can be created by a static member...
169   PointerType(const Type *ElType);
170 public:
171
172   inline const Type *getValueType() const { return ValueType; }
173
174
175   static const PointerType *getPointerType(const Type *ElementType);
176   static const PointerType *get(const Type *ElementType) {
177     return getPointerType(ElementType);
178   }
179 };
180
181 #endif