zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 1 | #ifndef _SLANG_COMPILER_RS_EXPORT_TYPE_H |
| 2 | #define _SLANG_COMPILER_RS_EXPORT_TYPE_H |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 3 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 4 | #include <set> |
| 5 | #include <list> |
| 6 | #include <string> |
| 7 | |
Zonr Chang | a41ce1d | 2010-10-06 02:23:12 +0800 | [diff] [blame] | 8 | #include "llvm/Support/ManagedStatic.h" |
| 9 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 10 | #include "llvm/ADT/StringRef.h" |
| 11 | #include "llvm/ADT/StringMap.h" |
| 12 | #include "llvm/ADT/SmallPtrSet.h" |
| 13 | |
| 14 | #include "clang/AST/Type.h" |
| 15 | #include "clang/AST/Decl.h" |
| 16 | |
Zonr Chang | a41ce1d | 2010-10-06 02:23:12 +0800 | [diff] [blame] | 17 | #include "slang_rs_exportable.h" |
| 18 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 19 | #define GET_CANONICAL_TYPE(T) \ |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 20 | (((T) == NULL) ? NULL : (T)->getCanonicalTypeInternal().getTypePtr()) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 21 | #define UNSAFE_CAST_TYPE(TT, T) \ |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 22 | static_cast<TT*>(T->getCanonicalTypeInternal().getTypePtr()) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 23 | #define GET_EXT_VECTOR_ELEMENT_TYPE(T) \ |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 24 | (((T) == NULL) ? NULL : \ |
| 25 | GET_CANONICAL_TYPE((T)->getElementType().getTypePtr())) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 26 | #define GET_POINTEE_TYPE(T) \ |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 27 | (((T) == NULL) ? NULL : \ |
| 28 | GET_CANONICAL_TYPE((T)->getPointeeType().getTypePtr())) |
| 29 | #define GET_CONSTANT_ARRAY_ELEMENT_TYPE(T) \ |
| 30 | (((T) == NULL) ? NULL : \ |
| 31 | GET_CANONICAL_TYPE((T)->getElementType().getTypePtr())) |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 32 | #define DUMMY_RS_TYPE_NAME_PREFIX "<" |
| 33 | #define DUMMY_RS_TYPE_NAME_POSTFIX ">" |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 34 | #define DUMMY_TYPE_NAME_FOR_RS_CONSTANT_ARRAY_TYPE \ |
| 35 | DUMMY_RS_TYPE_NAME_PREFIX"ConstantArray"DUMMY_RS_TYPE_NAME_POSTFIX |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 36 | |
| 37 | namespace llvm { |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 38 | class Type; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 39 | } // namespace llvm |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 40 | |
| 41 | namespace slang { |
| 42 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 43 | class RSContext; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 44 | |
Zonr Chang | a41ce1d | 2010-10-06 02:23:12 +0800 | [diff] [blame] | 45 | class RSExportType : public RSExportable { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 46 | friend class RSExportElement; |
| 47 | public: |
| 48 | typedef enum { |
| 49 | ExportClassPrimitive, |
| 50 | ExportClassPointer, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 51 | ExportClassVector, |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 52 | ExportClassMatrix, |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 53 | ExportClassConstantArray, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 54 | ExportClassRecord |
| 55 | } ExportClass; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 56 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 57 | private: |
| 58 | RSContext *mContext; |
Zonr Chang | 6b6320a | 2010-10-05 22:42:01 +0800 | [diff] [blame] | 59 | ExportClass mClass; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 60 | std::string mName; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 61 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 62 | // Cache the result after calling convertToLLVMType() at the first time |
| 63 | mutable const llvm::Type *mLLVMType; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 64 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 65 | protected: |
Zonr Chang | 6b6320a | 2010-10-05 22:42:01 +0800 | [diff] [blame] | 66 | RSExportType(RSContext *Context, |
| 67 | ExportClass Class, |
| 68 | const llvm::StringRef &Name); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 69 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 70 | // Let's make it private since there're some prerequisites to call this |
| 71 | // function. |
| 72 | // |
| 73 | // @T was normalized by calling RSExportType::TypeExportable(). |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 74 | // @TypeName was retrieve from RSExportType::GetTypeName() before calling |
| 75 | // this. |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 76 | // |
| 77 | static RSExportType *Create(RSContext *Context, |
| 78 | const clang::Type *T, |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 79 | const llvm::StringRef &TypeName); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 80 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 81 | static llvm::StringRef GetTypeName(const clang::Type *T); |
| 82 | // Return the type that can be used to create RSExportType, will always return |
| 83 | // the canonical type |
| 84 | static const clang::Type |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 85 | *TypeExportable(const clang::Type *T, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 86 | // Contain the checked type for recursion |
| 87 | llvm::SmallPtrSet<const clang::Type*, 8> &SPS); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 88 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 89 | // This function convert the RSExportType to LLVM type. Actually, it should be |
| 90 | // "convert Clang type to LLVM type." However, clang doesn't make this API |
| 91 | // (lib/CodeGen/CodeGenTypes.h) public, we need to do by ourselves. |
| 92 | // |
| 93 | // Once we can get LLVM type, we can use LLVM to get alignment information, |
| 94 | // allocation size of a given type and structure layout that LLVM used |
| 95 | // (all of these information are target dependent) without dealing with these |
| 96 | // by ourselves. |
| 97 | virtual const llvm::Type *convertToLLVMType() const = 0; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 98 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 99 | virtual ~RSExportType() {} |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 100 | public: |
| 101 | static bool NormalizeType(const clang::Type *&T, llvm::StringRef &TypeName); |
| 102 | // @T may not be normalized |
| 103 | static RSExportType *Create(RSContext *Context, const clang::Type *T); |
| 104 | static RSExportType *CreateFromDecl(RSContext *Context, |
| 105 | const clang::VarDecl *VD); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 106 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 107 | static const clang::Type *GetTypeOfDecl(const clang::DeclaratorDecl *DD); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 108 | |
Zonr Chang | 6b6320a | 2010-10-05 22:42:01 +0800 | [diff] [blame] | 109 | inline ExportClass getClass() const { return mClass; } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 110 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 111 | inline const llvm::Type *getLLVMType() const { |
| 112 | if (mLLVMType == NULL) |
| 113 | mLLVMType = convertToLLVMType(); |
| 114 | return mLLVMType; |
| 115 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 116 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 117 | // Return the number of bits necessary to hold the specified RSExportType |
| 118 | static size_t GetTypeStoreSize(const RSExportType *ET); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 119 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 120 | // The size of allocation of specified RSExportType (alignment considered) |
| 121 | static size_t GetTypeAllocSize(const RSExportType *ET); |
| 122 | static unsigned char GetTypeAlignment(const RSExportType *ET); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 123 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 124 | const std::string &getName() const { return mName; } |
| 125 | inline RSContext *getRSContext() const { return mContext; } |
| 126 | }; // RSExportType |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 127 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 128 | // Primitive types |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 129 | class RSExportPrimitiveType : public RSExportType { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 130 | friend class RSExportType; |
| 131 | friend class RSExportElement; |
| 132 | public: |
| 133 | // From graphics/java/android/renderscript/Element.java: Element.DataType |
| 134 | typedef enum { |
| 135 | DataTypeUnknown = -1, |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 136 | |
Shih-wei Liao | 91a3783 | 2010-10-03 19:11:51 -0700 | [diff] [blame] | 137 | DataTypeFloat16 = 0, |
| 138 | DataTypeFloat32, |
| 139 | DataTypeFloat64, |
| 140 | DataTypeSigned8, |
| 141 | DataTypeSigned16, |
| 142 | DataTypeSigned32, |
| 143 | DataTypeSigned64, |
| 144 | DataTypeUnsigned8, |
| 145 | DataTypeUnsigned16, |
| 146 | DataTypeUnsigned32, |
| 147 | DataTypeUnSigned64, |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 148 | |
Shih-wei Liao | 91a3783 | 2010-10-03 19:11:51 -0700 | [diff] [blame] | 149 | DataTypeBoolean, |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 150 | |
Shih-wei Liao | 91a3783 | 2010-10-03 19:11:51 -0700 | [diff] [blame] | 151 | DataTypeUnsigned565, |
| 152 | DataTypeUnsigned5551, |
| 153 | DataTypeUnsigned4444, |
Shih-wei Liao | dd35e49 | 2010-06-21 14:42:40 -0700 | [diff] [blame] | 154 | |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 155 | // Actually, there's no any instance of RSExportPrimitiveType which mType |
| 156 | // is of the value DataTypeRSMatrix*. DataTypeRSMatrix* enumeration here is |
| 157 | // only for RSExportPrimitiveType::GetRSObjectType to *recognize* the struct |
| 158 | // rs_matrix{2x2, 3x3, 4x4}. These matrix type are represented as |
| 159 | // RSExportMatrixType. |
Shih-wei Liao | 91a3783 | 2010-10-03 19:11:51 -0700 | [diff] [blame] | 160 | DataTypeRSMatrix2x2, |
| 161 | DataTypeRSMatrix3x3, |
| 162 | DataTypeRSMatrix4x4, |
| 163 | |
| 164 | DataTypeRSElement, |
| 165 | DataTypeRSType, |
| 166 | DataTypeRSAllocation, |
| 167 | DataTypeRSSampler, |
| 168 | DataTypeRSScript, |
| 169 | DataTypeRSMesh, |
| 170 | DataTypeRSProgramFragment, |
| 171 | DataTypeRSProgramVertex, |
| 172 | DataTypeRSProgramRaster, |
| 173 | DataTypeRSProgramStore, |
| 174 | DataTypeRSFont, |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 175 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 176 | DataTypeMax |
| 177 | } DataType; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 178 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 179 | // From graphics/java/android/renderscript/Element.java: Element.DataKind |
| 180 | typedef enum { |
Shih-wei Liao | 91a3783 | 2010-10-03 19:11:51 -0700 | [diff] [blame] | 181 | DataKindUser, |
| 182 | DataKindPixelL, |
| 183 | DataKindPixelA, |
| 184 | DataKindPixelLA, |
| 185 | DataKindPixelRGB, |
| 186 | DataKindPixelRGBA |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 187 | } DataKind; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 188 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 189 | private: |
| 190 | DataType mType; |
| 191 | DataKind mKind; |
| 192 | bool mNormalized; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 193 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 194 | typedef llvm::StringMap<DataType> RSObjectTypeMapTy; |
Zonr Chang | a41ce1d | 2010-10-06 02:23:12 +0800 | [diff] [blame] | 195 | static llvm::ManagedStatic<RSObjectTypeMapTy> RSObjectTypeMap; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 196 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 197 | static llvm::Type *RSObjectLLVMType; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 198 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 199 | static const size_t SizeOfDataTypeInBits[]; |
| 200 | // @T was normalized by calling RSExportType::TypeExportable() before calling |
| 201 | // this. |
| 202 | // @TypeName was retrieved from RSExportType::GetTypeName() before calling |
| 203 | // this |
| 204 | static RSExportPrimitiveType *Create(RSContext *Context, |
| 205 | const clang::Type *T, |
| 206 | const llvm::StringRef &TypeName, |
| 207 | DataKind DK = DataKindUser, |
| 208 | bool Normalized = false); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 209 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 210 | protected: |
| 211 | // T is normalized by calling RSExportType::TypeExportable() before |
| 212 | // calling this |
| 213 | static bool IsPrimitiveType(const clang::Type *T); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 214 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 215 | static DataType GetDataType(const clang::Type *T); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 216 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 217 | RSExportPrimitiveType(RSContext *Context, |
Zonr Chang | 6b6320a | 2010-10-05 22:42:01 +0800 | [diff] [blame] | 218 | // for derived class to set their type class |
| 219 | ExportClass Class, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 220 | const llvm::StringRef &Name, |
| 221 | DataType DT, |
| 222 | DataKind DK, |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 223 | bool Normalized) |
Zonr Chang | 6b6320a | 2010-10-05 22:42:01 +0800 | [diff] [blame] | 224 | : RSExportType(Context, Class, Name), |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 225 | mType(DT), |
| 226 | mKind(DK), |
| 227 | mNormalized(Normalized) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 228 | return; |
| 229 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 230 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 231 | virtual const llvm::Type *convertToLLVMType() const; |
| 232 | public: |
| 233 | // @T may not be normalized |
| 234 | static RSExportPrimitiveType *Create(RSContext *Context, |
| 235 | const clang::Type *T, |
| 236 | DataKind DK = DataKindUser); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 237 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 238 | static DataType GetRSObjectType(const llvm::StringRef &TypeName); |
| 239 | static DataType GetRSObjectType(const clang::Type *T); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 240 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 241 | static size_t GetSizeInBits(const RSExportPrimitiveType *EPT); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 242 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 243 | inline DataType getType() const { return mType; } |
| 244 | inline DataKind getKind() const { return mKind; } |
| 245 | inline bool isRSObjectType() const { |
| 246 | return ((mType >= DataTypeRSElement) && (mType < DataTypeMax)); |
| 247 | } |
| 248 | }; // RSExportPrimitiveType |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 249 | |
| 250 | |
| 251 | class RSExportPointerType : public RSExportType { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 252 | friend class RSExportType; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 253 | friend class RSExportFunc; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 254 | private: |
| 255 | const RSExportType *mPointeeType; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 256 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 257 | RSExportPointerType(RSContext *Context, |
| 258 | const llvm::StringRef &Name, |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 259 | const RSExportType *PointeeType) |
Zonr Chang | 6b6320a | 2010-10-05 22:42:01 +0800 | [diff] [blame] | 260 | : RSExportType(Context, ExportClassPointer, Name), |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 261 | mPointeeType(PointeeType) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 262 | return; |
| 263 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 264 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 265 | // @PT was normalized by calling RSExportType::TypeExportable() before calling |
| 266 | // this. |
| 267 | static RSExportPointerType *Create(RSContext *Context, |
| 268 | const clang::PointerType *PT, |
| 269 | const llvm::StringRef &TypeName); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 270 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 271 | virtual const llvm::Type *convertToLLVMType() const; |
| 272 | public: |
| 273 | static const clang::Type *IntegerType; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 274 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 275 | inline const RSExportType *getPointeeType() const { return mPointeeType; } |
| 276 | }; // RSExportPointerType |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 277 | |
| 278 | |
| 279 | class RSExportVectorType : public RSExportPrimitiveType { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 280 | friend class RSExportType; |
| 281 | friend class RSExportElement; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 282 | private: |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 283 | unsigned mNumElement; // number of element |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 284 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 285 | RSExportVectorType(RSContext *Context, |
| 286 | const llvm::StringRef &Name, |
| 287 | DataType DT, |
| 288 | DataKind DK, |
| 289 | bool Normalized, |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 290 | unsigned NumElement) |
Zonr Chang | 6b6320a | 2010-10-05 22:42:01 +0800 | [diff] [blame] | 291 | : RSExportPrimitiveType(Context, ExportClassVector, Name, |
| 292 | DT, DK, Normalized), |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 293 | mNumElement(NumElement) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 294 | return; |
| 295 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 296 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 297 | // @EVT was normalized by calling RSExportType::TypeExportable() before |
| 298 | // calling this. |
| 299 | static RSExportVectorType *Create(RSContext *Context, |
| 300 | const clang::ExtVectorType *EVT, |
| 301 | const llvm::StringRef &TypeName, |
| 302 | DataKind DK = DataKindUser, |
| 303 | bool Normalized = false); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 304 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 305 | static const char *VectorTypeNameStore[][3]; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 306 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 307 | virtual const llvm::Type *convertToLLVMType() const; |
| 308 | public: |
| 309 | static llvm::StringRef GetTypeName(const clang::ExtVectorType *EVT); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 310 | |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 311 | inline unsigned getNumElement() const { return mNumElement; } |
| 312 | }; |
| 313 | |
| 314 | // Only *square* *float* matrix is supported by now. |
| 315 | // |
| 316 | // struct rs_matrix{2x2,3x3,4x4, ..., NxN} should be defined as the following |
| 317 | // form *exactly*: |
| 318 | // typedef struct { |
| 319 | // float m[{NxN}]; |
| 320 | // } rs_matrixNxN; |
| 321 | // |
| 322 | // where mDim will be N. |
| 323 | class RSExportMatrixType : public RSExportType { |
| 324 | friend class RSExportType; |
| 325 | private: |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 326 | unsigned mDim; // dimension |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 327 | |
| 328 | RSExportMatrixType(RSContext *Context, |
| 329 | const llvm::StringRef &Name, |
| 330 | unsigned Dim) |
Zonr Chang | 6b6320a | 2010-10-05 22:42:01 +0800 | [diff] [blame] | 331 | : RSExportType(Context, ExportClassMatrix, Name), |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 332 | mDim(Dim) { |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | virtual const llvm::Type *convertToLLVMType() const; |
| 337 | public: |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 338 | // @RT was normalized by calling RSExportType::TypeExportable() before |
| 339 | // calling this. |
| 340 | static RSExportMatrixType *Create(RSContext *Context, |
| 341 | const clang::RecordType *RT, |
| 342 | const llvm::StringRef &TypeName, |
| 343 | unsigned Dim); |
| 344 | |
| 345 | inline unsigned getDim() const { return mDim; } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 346 | }; |
| 347 | |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 348 | class RSExportConstantArrayType : public RSExportType { |
| 349 | friend class RSExportType; |
| 350 | private: |
| 351 | const RSExportType *mElementType; // Array element type |
| 352 | unsigned mSize; // Array size |
| 353 | |
| 354 | RSExportConstantArrayType(RSContext *Context, |
| 355 | const RSExportType *ElementType, |
| 356 | unsigned Size) |
| 357 | : RSExportType(Context, |
Zonr Chang | 6b6320a | 2010-10-05 22:42:01 +0800 | [diff] [blame] | 358 | ExportClassConstantArray, |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 359 | DUMMY_TYPE_NAME_FOR_RS_CONSTANT_ARRAY_TYPE), |
| 360 | mElementType(ElementType), |
| 361 | mSize(Size) { |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | // @CAT was normalized by calling RSExportType::TypeExportable() before |
| 366 | // calling this. |
| 367 | static RSExportConstantArrayType *Create(RSContext *Context, |
| 368 | const clang::ConstantArrayType *CAT); |
| 369 | |
| 370 | virtual const llvm::Type *convertToLLVMType() const; |
| 371 | public: |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 372 | inline unsigned getSize() const { return mSize; } |
| 373 | inline const RSExportType *getElementType() const { return mElementType; } |
| 374 | }; |
| 375 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 376 | class RSExportRecordType : public RSExportType { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 377 | friend class RSExportType; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 378 | public: |
| 379 | class Field { |
| 380 | private: |
| 381 | const RSExportType *mType; |
| 382 | // Field name |
| 383 | std::string mName; |
| 384 | // Link to the struct that contain this field |
| 385 | const RSExportRecordType *mParent; |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 386 | // Offset in the container |
| 387 | size_t mOffset; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 388 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 389 | public: |
| 390 | Field(const RSExportType *T, |
| 391 | const llvm::StringRef &Name, |
| 392 | const RSExportRecordType *Parent, |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 393 | size_t Offset) |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 394 | : mType(T), |
| 395 | mName(Name.data(), Name.size()), |
| 396 | mParent(Parent), |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 397 | mOffset(Offset) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 398 | return; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 401 | inline const RSExportRecordType *getParent() const { return mParent; } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 402 | inline const RSExportType *getType() const { return mType; } |
| 403 | inline const std::string &getName() const { return mName; } |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 404 | inline size_t getOffsetInParent() const { return mOffset; } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 405 | }; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 406 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 407 | typedef std::list<const Field*>::const_iterator const_field_iterator; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 408 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 409 | inline const_field_iterator fields_begin() const { |
| 410 | return this->mFields.begin(); |
| 411 | } |
| 412 | inline const_field_iterator fields_end() const { |
| 413 | return this->mFields.end(); |
| 414 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 415 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 416 | private: |
| 417 | std::list<const Field*> mFields; |
| 418 | bool mIsPacked; |
| 419 | // Artificial export struct type is not exported by user (and thus it won't |
| 420 | // get reflected) |
| 421 | bool mIsArtificial; |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 422 | size_t mAllocSize; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 423 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 424 | RSExportRecordType(RSContext *Context, |
| 425 | const llvm::StringRef &Name, |
| 426 | bool IsPacked, |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 427 | bool IsArtificial, |
| 428 | size_t AllocSize) |
Zonr Chang | 6b6320a | 2010-10-05 22:42:01 +0800 | [diff] [blame] | 429 | : RSExportType(Context, ExportClassRecord, Name), |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 430 | mIsPacked(IsPacked), |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 431 | mIsArtificial(IsArtificial), |
| 432 | mAllocSize(AllocSize) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 433 | return; |
| 434 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 435 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 436 | // @RT was normalized by calling RSExportType::TypeExportable() before calling |
| 437 | // this. |
| 438 | // @TypeName was retrieved from RSExportType::GetTypeName() before calling |
| 439 | // this. |
| 440 | static RSExportRecordType *Create(RSContext *Context, |
| 441 | const clang::RecordType *RT, |
| 442 | const llvm::StringRef &TypeName, |
| 443 | bool mIsArtificial = false); |
| 444 | |
| 445 | virtual const llvm::Type *convertToLLVMType() const; |
| 446 | public: |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 447 | inline const std::list<const Field*>& getFields() const { return mFields; } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 448 | inline bool isPacked() const { return mIsPacked; } |
| 449 | inline bool isArtificial() const { return mIsArtificial; } |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 450 | inline size_t getAllocSize() const { return mAllocSize; } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 451 | |
| 452 | ~RSExportRecordType() { |
| 453 | for (std::list<const Field*>::iterator I = mFields.begin(), |
| 454 | E = mFields.end(); |
| 455 | I != E; |
| 456 | I++) |
| 457 | if (*I != NULL) |
| 458 | delete *I; |
| 459 | return; |
| 460 | } |
| 461 | }; // RSExportRecordType |
| 462 | |
| 463 | } // namespace slang |
| 464 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 465 | #endif // _SLANG_COMPILER_RS_EXPORT_TYPE_H |