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"
21 // Future derived types: SIMD packed format
24 class MethodType : public Type {
26 typedef vector<const Type*> ParamTypes;
28 const Type *ResultType;
31 MethodType(const MethodType &); // Do not implement
32 const MethodType &operator=(const MethodType &); // Do not implement
34 // This should really be private, but it squelches a bogus warning
35 // from GCC to make them protected: warning: `class MethodType' only
36 // defines private constructors and has no friends
38 // Private ctor - Only can be created by a static member...
39 MethodType(const Type *Result, const vector<const Type*> &Params,
43 inline const Type *getReturnType() const { return ResultType; }
44 inline const ParamTypes &getParamTypes() const { return ParamTys; }
46 static const MethodType *getMethodType(const Type *Result,
47 const ParamTypes &Params);
48 static const MethodType *get(const Type *Result, const ParamTypes &Params) {
49 return getMethodType(Result, Params);
55 class ArrayType : public Type {
57 const Type *ElementType;
58 int NumElements; // >= 0 for sized array, -1 for unbounded/unknown array
60 ArrayType(const ArrayType &); // Do not implement
61 const ArrayType &operator=(const ArrayType &); // Do not implement
63 // This should really be private, but it squelches a bogus warning
64 // from GCC to make them protected: warning: `class ArrayType' only
65 // defines private constructors and has no friends
68 // Private ctor - Only can be created by a static member...
69 ArrayType(const Type *ElType, int NumEl, const string &Name);
72 inline const Type *getElementType() const { return ElementType; }
73 inline int getNumElements() const { return NumElements; }
75 inline bool isSized() const { return NumElements >= 0; }
76 inline bool isUnsized() const { return NumElements == -1; }
78 static const ArrayType *getArrayType(const Type *ElementType,
79 int NumElements = -1);
80 static const ArrayType *get(const Type *ElementType, int NumElements = -1) {
81 return getArrayType(ElementType, NumElements);
85 class StructType : public Type {
87 typedef vector<const Type*> ElementTypes;
91 struct StructSizeAndOffsetInfo {
92 int storageSize; // -1 until the value is computd
93 vector<int> memberOffsets; // -1 until values are computed
94 const TargetMachine* targetInfo;
99 StructType(const StructType &); // Do not implement
100 const StructType &operator=(const StructType &); // Do not implement
103 // This should really be private, but it squelches a bogus warning
104 // from GCC to make them protected: warning: `class StructType' only
105 // defines private constructors and has no friends
107 // Private ctor - Only can be created by a static member...
108 StructType(const vector<const Type*> &Types, const string &Name);
110 // Reset cached info so it will be computed when first requested
111 void ResetCachedInfo() const;
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);
120 unsigned int getStorageSize(const TargetMachine& tmi) const;
121 unsigned int getElementOffset(int i, const TargetMachine& tmi) const;
126 StructType::getStorageSize(const TargetMachine& tmi) const
128 if (layoutCache->targetInfo != NULL && ! (* layoutCache->targetInfo == tmi))
129 {// target machine has changed (hey it could happen). discard cached info.
131 layoutCache->targetInfo = &tmi;
134 if (layoutCache->storageSize < 0)
136 layoutCache->storageSize = tmi.findOptimalStorageSize(this);
137 assert(layoutCache->storageSize >= 0);
140 return layoutCache->storageSize;
145 StructType::getElementOffset(int i, const TargetMachine& tmi) const
147 if (layoutCache->targetInfo != NULL && ! (* layoutCache->targetInfo == tmi))
148 {// target machine has changed (hey it could happen). discard cached info.
152 if (layoutCache->memberOffsets[i] < 0)
154 layoutCache->targetInfo = &tmi; // remember which target was used
156 unsigned int* offsetVec = tmi.findOptimalMemberOffsets(this);
157 for (unsigned i=0, N=layoutCache->memberOffsets.size(); i < N; i++)
159 layoutCache->memberOffsets[i] = offsetVec[i];
160 assert(layoutCache->memberOffsets[i] >= 0);
165 return layoutCache->memberOffsets[i];
170 StructType::ResetCachedInfo() const
172 layoutCache->storageSize = -1;
173 layoutCache->memberOffsets.insert(layoutCache->memberOffsets.begin(),
175 layoutCache->targetInfo = NULL;
179 class PointerType : public Type {
181 const Type *ValueType;
183 PointerType(const PointerType &); // Do not implement
184 const PointerType &operator=(const PointerType &); // Do not implement
186 // This should really be private, but it squelches a bogus warning
187 // from GCC to make them protected: warning: `class PointerType' only
188 // defines private constructors and has no friends
191 // Private ctor - Only can be created by a static member...
192 PointerType(const Type *ElType);
195 inline const Type *getValueType() const { return ValueType; }
198 static const PointerType *getPointerType(const Type *ElementType);
199 static const PointerType *get(const Type *ElementType) {
200 return getPointerType(ElementType);