1 //===-- llvm/DerivedTypes.h - Classes for handling data types ----*- C++ -*--=//
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...
7 // The implementations of these classes live in the Type.cpp file.
9 //===----------------------------------------------------------------------===//
11 #ifndef LLVM_DERIVED_TYPES_H
12 #define LLVM_DERIVED_TYPES_H
14 #include "llvm/Type.h"
15 #include "llvm/CodeGen/TargetMachine.h"
18 // Future derived types: SIMD packed format
21 class MethodType : public Type {
23 typedef vector<const Type*> ParamTypes;
25 const Type *ResultType;
28 MethodType(const MethodType &); // Do not implement
29 const MethodType &operator=(const MethodType &); // Do not implement
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
35 // Private ctor - Only can be created by a static member...
36 MethodType(const Type *Result, const vector<const Type*> &Params,
40 inline const Type *getReturnType() const { return ResultType; }
41 inline const ParamTypes &getParamTypes() const { return ParamTys; }
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);
52 class ArrayType : public Type {
54 const Type *ElementType;
55 int NumElements; // >= 0 for sized array, -1 for unbounded/unknown array
57 ArrayType(const ArrayType &); // Do not implement
58 const ArrayType &operator=(const ArrayType &); // Do not implement
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
65 // Private ctor - Only can be created by a static member...
66 ArrayType(const Type *ElType, int NumEl, const string &Name);
69 inline const Type *getElementType() const { return ElementType; }
70 inline int getNumElements() const { return NumElements; }
72 inline bool isSized() const { return NumElements >= 0; }
73 inline bool isUnsized() const { return NumElements == -1; }
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);
82 class StructType : public Type {
84 typedef vector<const Type*> ElementTypes;
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;
96 StructType(const StructType &); // Do not implement
97 const StructType &operator=(const StructType &); // Do not implement
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
104 // Private ctor - Only can be created by a static member...
105 StructType(const vector<const Type*> &Types, const string &Name);
107 // Reset cached info so it will be computed when first requested
108 void ResetCachedInfo() const;
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);
117 unsigned int getStorageSize(const TargetMachine& tmi) const;
118 unsigned int getElementOffset(int i, const TargetMachine& tmi) const;
123 StructType::getStorageSize(const TargetMachine& tmi) const
125 if (layoutCache->targetInfo != NULL && ! (* layoutCache->targetInfo == tmi))
126 {// target machine has changed (hey it could happen). discard cached info.
128 layoutCache->targetInfo = &tmi;
131 if (layoutCache->storageSize < 0)
133 layoutCache->storageSize = tmi.findOptimalStorageSize(this);
134 assert(layoutCache->storageSize >= 0);
137 return layoutCache->storageSize;
142 StructType::getElementOffset(int i, const TargetMachine& tmi) const
144 if (layoutCache->targetInfo != NULL && ! (* layoutCache->targetInfo == tmi))
145 {// target machine has changed (hey it could happen). discard cached info.
149 if (layoutCache->memberOffsets[i] < 0)
151 layoutCache->targetInfo = &tmi; // remember which target was used
153 unsigned int* offsetVec = tmi.findOptimalMemberOffsets(this);
154 for (unsigned i=0, N=layoutCache->memberOffsets.size(); i < N; i++)
156 layoutCache->memberOffsets[i] = offsetVec[i];
157 assert(layoutCache->memberOffsets[i] >= 0);
162 return layoutCache->memberOffsets[i];
167 StructType::ResetCachedInfo() const
169 layoutCache->storageSize = -1;
170 layoutCache->memberOffsets.insert(layoutCache->memberOffsets.begin(),
172 layoutCache->targetInfo = NULL;
176 class PointerType : public Type {
178 const Type *ValueType;
180 PointerType(const PointerType &); // Do not implement
181 const PointerType &operator=(const PointerType &); // Do not implement
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
188 // Private ctor - Only can be created by a static member...
189 PointerType(const Type *ElType);
192 inline const Type *getValueType() const { return ValueType; }
195 static const PointerType *getPointerType(const Type *ElementType);
196 static const PointerType *get(const Type *ElementType) {
197 return getPointerType(ElementType);