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);
51 class ArrayType : public Type {
53 const Type *ElementType;
54 int NumElements; // >= 0 for sized array, -1 for unbounded/unknown array
56 ArrayType(const ArrayType &); // Do not implement
57 const ArrayType &operator=(const ArrayType &); // Do not implement
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
64 // Private ctor - Only can be created by a static member...
65 ArrayType(const Type *ElType, int NumEl, const string &Name);
68 inline const Type *getElementType() const { return ElementType; }
69 inline int getNumElements() const { return NumElements; }
71 inline bool isSized() const { return NumElements >= 0; }
72 inline bool isUnsized() const { return NumElements == -1; }
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);
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;
95 StructType(const StructType &); // Do not implement
96 const StructType &operator=(const StructType &); // Do not implement
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
103 // Private ctor - Only can be created by a static member...
104 StructType(const vector<const Type*> &Types, const string &Name);
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(),
111 layoutCache->targetInfo = 0;
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);
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.
126 layoutCache->targetInfo = &tmi;
129 if (layoutCache->storageSize < 0) {
130 layoutCache->storageSize = tmi.findOptimalStorageSize(this);
131 assert(layoutCache->storageSize >= 0);
133 return layoutCache->storageSize;
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)
140 if (layoutCache->memberOffsets[i] < 0) {
141 layoutCache->targetInfo = &tmi; // remember which target was used
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);
151 return layoutCache->memberOffsets[i];
156 class PointerType : public Type {
158 const Type *ValueType;
160 PointerType(const PointerType &); // Do not implement
161 const PointerType &operator=(const PointerType &); // Do not implement
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
168 // Private ctor - Only can be created by a static member...
169 PointerType(const Type *ElType);
172 inline const Type *getValueType() const { return ValueType; }
175 static const PointerType *getPointerType(const Type *ElementType);
176 static const PointerType *get(const Type *ElementType) {
177 return getPointerType(ElementType);