blob: afd2db293a63b7057366cd22008efe2c92878cc1 [file] [log] [blame]
Zonr Changc383a502010-10-12 01:52:08 +08001/*
Stephen Hinesd5a84f62012-04-04 17:44:38 -07002 * Copyright 2010-2012, The Android Open Source Project
Zonr Changc383a502010-10-12 01:52:08 +08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Stephen Hinese639eb52010-11-08 19:27:20 -080017#ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_TYPE_H_ // NOLINT
18#define _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_TYPE_H_
Shih-wei Liao462aefd2010-06-04 15:32:04 -070019
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070020#include <list>
Stephen Hinese639eb52010-11-08 19:27:20 -080021#include <set>
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070022#include <string>
Stephen Hinesa6b54142012-04-09 18:25:08 -070023#include <sstream>
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070024
zonr6315f762010-10-05 15:35:14 +080025#include "clang/AST/Decl.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080026#include "clang/AST/Type.h"
27
28#include "llvm/ADT/SmallPtrSet.h"
29#include "llvm/ADT/StringMap.h"
30#include "llvm/ADT/StringRef.h"
31
32#include "llvm/Support/ManagedStatic.h"
zonr6315f762010-10-05 15:35:14 +080033
Zonr Changa41ce1d2010-10-06 02:23:12 +080034#include "slang_rs_exportable.h"
35
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -070036
37inline const clang::Type* GetCanonicalType(const clang::Type* T) {
38 if (T == NULL) {
39 return NULL;
40 }
41 return T->getCanonicalTypeInternal().getTypePtr();
42}
43
44inline const clang::Type* GetCanonicalType(clang::QualType QT) {
45 return GetCanonicalType(QT.getTypePtr());
46}
47
48inline const clang::Type* GetExtVectorElementType(const clang::ExtVectorType *T) {
49 if (T == NULL) {
50 return NULL;
51 }
52 return GetCanonicalType(T->getElementType());
53}
54
55inline const clang::Type* GetPointeeType(const clang::PointerType *T) {
56 if (T == NULL) {
57 return NULL;
58 }
59 return GetCanonicalType(T->getPointeeType());
60}
61
62inline const clang::Type* GetConstantArrayElementType(const clang::ConstantArrayType *T) {
63 if (T == NULL) {
64 return NULL;
65 }
66 return GetCanonicalType(T->getElementType());
67}
68
Shih-wei Liao462aefd2010-06-04 15:32:04 -070069
70namespace llvm {
zonr6315f762010-10-05 15:35:14 +080071 class Type;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070072} // namespace llvm
Shih-wei Liao462aefd2010-06-04 15:32:04 -070073
74namespace slang {
75
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -070076class RSContext;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070077
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -070078// Broad grouping of the data types
79enum DataTypeCategory {
80 PrimitiveDataType,
81 MatrixDataType,
82 ObjectDataType
83};
84
85// From graphics/java/android/renderscript/Element.java: Element.DataType
86/* NOTE: The values of the enums are found compiled in the bit code (i.e. as
87 * values, not symbolic. When adding new types, you must add them to the end.
88 * If removing types, you can't re-use the integer value.
89 *
90 * TODO: but if you do this, you won't be able to keep using First* & Last*
91 * for validation.
92 *
93 * IMPORTANT: This enum should correspond one-for-one to the entries found in the
94 * gReflectionsTypes table (except for the two negative numbers). Don't edit one without
95 * the other.
96 */
97enum DataType {
98 DataTypeIsStruct = -2,
99 DataTypeUnknown = -1,
100
101 DataTypeFloat16 = 0,
102 DataTypeFloat32 = 1,
103 DataTypeFloat64 = 2,
104 DataTypeSigned8 = 3,
105 DataTypeSigned16 = 4,
106 DataTypeSigned32 = 5,
107 DataTypeSigned64 = 6,
108 DataTypeUnsigned8 = 7,
109 DataTypeUnsigned16 = 8,
110 DataTypeUnsigned32 = 9,
111 DataTypeUnsigned64 = 10,
112 DataTypeBoolean = 11,
113 DataTypeUnsigned565 = 12,
114 DataTypeUnsigned5551 = 13,
115 DataTypeUnsigned4444 = 14,
116
117 DataTypeRSMatrix2x2 = 15,
118 DataTypeRSMatrix3x3 = 16,
119 DataTypeRSMatrix4x4 = 17,
120
121 DataTypeRSElement = 18,
122 DataTypeRSType = 19,
123 DataTypeRSAllocation = 20,
124 DataTypeRSSampler = 21,
125 DataTypeRSScript = 22,
126 DataTypeRSMesh = 23,
127 DataTypeRSPath = 24,
128 DataTypeRSProgramFragment = 25,
129 DataTypeRSProgramVertex = 26,
130 DataTypeRSProgramRaster = 27,
131 DataTypeRSProgramStore = 28,
132 DataTypeRSFont = 29,
133
134 // This should always be last and correspond to the size of the gReflectionTypes table.
135 DataTypeMax
136};
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700137
Stephen Hinesfdd1ba12012-03-08 17:26:32 -0800138typedef struct {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700139 DataTypeCategory category;
Stephen Hinesfdd1ba12012-03-08 17:26:32 -0800140 const char * rs_type;
141 const char * rs_short_type;
142 uint32_t size_in_bits;
143 const char * c_name;
144 const char * java_name;
145 const char * rs_c_vector_prefix;
146 const char * rs_java_vector_prefix;
147 bool java_promotion;
148} RSReflectionType;
149
Jason Sams1b6a0882012-03-12 15:07:58 -0700150
151typedef struct RSReflectionTypeData_rec {
152 const RSReflectionType *type;
153 uint32_t vecSize;
154 bool isPointer;
155 uint32_t arraySize;
156
157 // Subelements
158 //std::vector<const struct RSReflectionTypeData_rec *> fields;
159 //std::vector< std::string > fieldNames;
160 //std::vector< uint32_t> fieldOffsetBytes;
161} RSReflectionTypeData;
162
Jean-Luc Brouilleteca05342014-05-15 12:44:21 -0700163// Make a name for types that are too complicated to create the real names.
164std::string CreateDummyName(const char *type, const std::string &name);
165
166inline bool IsDummyName(const llvm::StringRef &Name) {
167 return Name.startswith("<");
168}
Jason Sams1b6a0882012-03-12 15:07:58 -0700169
Zonr Changa41ce1d2010-10-06 02:23:12 +0800170class RSExportType : public RSExportable {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700171 friend class RSExportElement;
172 public:
173 typedef enum {
174 ExportClassPrimitive,
175 ExportClassPointer,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700176 ExportClassVector,
Zonr Chang92b344a2010-10-05 20:39:03 +0800177 ExportClassMatrix,
Zonr Chang2e1dba62010-10-05 22:20:11 +0800178 ExportClassConstantArray,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700179 ExportClassRecord
180 } ExportClass;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700181
Jason Sams1b6a0882012-03-12 15:07:58 -0700182 void convertToRTD(RSReflectionTypeData *rtd) const;
183
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700184 private:
Zonr Chang6b6320a2010-10-05 22:42:01 +0800185 ExportClass mClass;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700186 std::string mName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700187
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700188 // Cache the result after calling convertToLLVMType() at the first time
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700189 mutable llvm::Type *mLLVMType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700190
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700191 protected:
Zonr Chang6b6320a2010-10-05 22:42:01 +0800192 RSExportType(RSContext *Context,
193 ExportClass Class,
194 const llvm::StringRef &Name);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700195
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700196 // Let's make it private since there're some prerequisites to call this
197 // function.
198 //
Stephen Hinese5e64432010-12-02 18:48:20 -0800199 // @T was normalized by calling RSExportType::NormalizeType().
zonr6315f762010-10-05 15:35:14 +0800200 // @TypeName was retrieve from RSExportType::GetTypeName() before calling
201 // this.
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700202 //
203 static RSExportType *Create(RSContext *Context,
204 const clang::Type *T,
zonr6315f762010-10-05 15:35:14 +0800205 const llvm::StringRef &TypeName);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700206
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700207 static llvm::StringRef GetTypeName(const clang::Type *T);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700208
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700209 // This function convert the RSExportType to LLVM type. Actually, it should be
210 // "convert Clang type to LLVM type." However, clang doesn't make this API
211 // (lib/CodeGen/CodeGenTypes.h) public, we need to do by ourselves.
212 //
213 // Once we can get LLVM type, we can use LLVM to get alignment information,
214 // allocation size of a given type and structure layout that LLVM used
215 // (all of these information are target dependent) without dealing with these
216 // by ourselves.
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700217 virtual llvm::Type *convertToLLVMType() const = 0;
Stephen Hinese639eb52010-11-08 19:27:20 -0800218 // Record type may recursively reference its type definition. We need a
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800219 // temporary type setup before the type construction gets done.
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700220 inline void setAbstractLLVMType(llvm::Type *LLVMType) const {
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800221 mLLVMType = LLVMType;
222 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700223
Zonr Changa7a828d2010-10-22 03:44:27 +0800224 virtual ~RSExportType();
Stephen Hinesecddee32011-07-20 18:30:09 -0700225
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700226 public:
Stephen Hinese5e64432010-12-02 18:48:20 -0800227 // This function additionally verifies that the Type T is exportable.
228 // If it is not, this function returns false. Otherwise it returns true.
229 static bool NormalizeType(const clang::Type *&T,
230 llvm::StringRef &TypeName,
Stephen Hines48d893d2013-12-06 18:00:27 -0800231 RSContext *Context,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700232 const clang::VarDecl *VD);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700233
Stephen Hines11274a72012-09-26 19:14:20 -0700234 // This function checks whether the specified type can be handled by RS/FS.
235 // If it cannot, this function returns false. Otherwise it returns true.
236 // Filterscript has additional restrictions on supported types.
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800237 static bool ValidateType(slang::RSContext *Context, clang::ASTContext &C,
238 clang::QualType QT, clang::NamedDecl *ND,
239 clang::SourceLocation Loc, unsigned int TargetAPI,
240 bool IsFilterscript);
Stephen Hines11274a72012-09-26 19:14:20 -0700241
Stephen Hines78e69cb2011-04-22 15:03:19 -0700242 // This function ensures that the VarDecl can be properly handled by RS.
243 // If it cannot, this function returns false. Otherwise it returns true.
Stephen Hines11274a72012-09-26 19:14:20 -0700244 // Filterscript has additional restrictions on supported types.
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800245 static bool ValidateVarDecl(slang::RSContext *Context, clang::VarDecl *VD,
246 unsigned int TargetAPI, bool IsFilterscript);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700247
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700248 // @T may not be normalized
249 static RSExportType *Create(RSContext *Context, const clang::Type *T);
250 static RSExportType *CreateFromDecl(RSContext *Context,
251 const clang::VarDecl *VD);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700252
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700253 static const clang::Type *GetTypeOfDecl(const clang::DeclaratorDecl *DD);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700254
Zonr Chang6b6320a2010-10-05 22:42:01 +0800255 inline ExportClass getClass() const { return mClass; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700256
Stephen Hines0d26cef2012-05-01 19:23:01 -0700257 virtual unsigned getSize() const { return 1; }
258
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700259 inline llvm::Type *getLLVMType() const {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700260 if (mLLVMType == NULL)
261 mLLVMType = convertToLLVMType();
262 return mLLVMType;
263 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700264
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700265 // Return the maximum number of bytes that may be written when this type is stored.
266 virtual size_t getStoreSize() const;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700267
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700268 // Return the distance in bytes between successive elements of this type; it includes padding.
269 virtual size_t getAllocSize() const;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700270
Zonr Chang641558f2010-10-12 21:07:06 +0800271 inline const std::string &getName() const { return mName; }
272
Stephen Hinesa6b54142012-04-09 18:25:08 -0700273 virtual std::string getElementName() const {
274 // Base case is actually an invalid C/Java identifier.
275 return "@@INVALID@@";
276 }
277
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800278 virtual bool keep();
Zonr Chang641558f2010-10-12 21:07:06 +0800279 virtual bool equals(const RSExportable *E) const;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700280}; // RSExportType
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700281
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700282// Primitive types
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700283class RSExportPrimitiveType : public RSExportType {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700284 friend class RSExportType;
285 friend class RSExportElement;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700286 private:
287 DataType mType;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700288 bool mNormalized;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700289
Zonr Changb1771ef2010-10-22 18:03:46 +0800290 typedef llvm::StringMap<DataType> RSSpecificTypeMapTy;
291 static llvm::ManagedStatic<RSSpecificTypeMapTy> RSSpecificTypeMap;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700292
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700293 static const size_t SizeOfDataTypeInBits[];
Stephen Hinese5e64432010-12-02 18:48:20 -0800294 // @T was normalized by calling RSExportType::NormalizeType() before calling
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700295 // this.
296 // @TypeName was retrieved from RSExportType::GetTypeName() before calling
297 // this
298 static RSExportPrimitiveType *Create(RSContext *Context,
299 const clang::Type *T,
300 const llvm::StringRef &TypeName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700301 bool Normalized = false);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700302
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700303 protected:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700304 RSExportPrimitiveType(RSContext *Context,
Zonr Chang6b6320a2010-10-05 22:42:01 +0800305 // for derived class to set their type class
306 ExportClass Class,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700307 const llvm::StringRef &Name,
308 DataType DT,
zonr6315f762010-10-05 15:35:14 +0800309 bool Normalized)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800310 : RSExportType(Context, Class, Name),
zonr6315f762010-10-05 15:35:14 +0800311 mType(DT),
zonr6315f762010-10-05 15:35:14 +0800312 mNormalized(Normalized) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700313 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700314
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700315 virtual llvm::Type *convertToLLVMType() const;
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800316
317 static DataType GetDataType(RSContext *Context, const clang::Type *T);
318
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700319 public:
Stephen Hinesdd6206b2010-12-09 19:39:22 -0800320 // T is normalized by calling RSExportType::NormalizeType() before
321 // calling this
322 static bool IsPrimitiveType(const clang::Type *T);
323
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700324 // @T may not be normalized
325 static RSExportPrimitiveType *Create(RSContext *Context,
Stephen Hines2b8fb642012-03-09 00:12:47 -0800326 const clang::Type *T);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700327
Zonr Changb1771ef2010-10-22 18:03:46 +0800328 static DataType GetRSSpecificType(const llvm::StringRef &TypeName);
329 static DataType GetRSSpecificType(const clang::Type *T);
330
331 static bool IsRSMatrixType(DataType DT);
332 static bool IsRSObjectType(DataType DT);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800333 static bool IsRSObjectType(const clang::Type *T) {
334 return IsRSObjectType(GetRSSpecificType(T));
335 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700336
Stephen Hinesfeaca062011-02-04 14:08:13 -0800337 // Determines whether T is [an array of] struct that contains at least one
338 // RS object type within it.
339 static bool IsStructureTypeWithRSObject(const clang::Type *T);
340
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700341 static size_t GetSizeInBits(const RSExportPrimitiveType *EPT);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700342
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700343 inline DataType getType() const { return mType; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700344 inline bool isRSObjectType() const {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700345 return IsRSObjectType(mType);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700346 }
Zonr Chang641558f2010-10-12 21:07:06 +0800347
348 virtual bool equals(const RSExportable *E) const;
Stephen Hinesfdd1ba12012-03-08 17:26:32 -0800349
350 static RSReflectionType *getRSReflectionType(DataType DT);
351 static RSReflectionType *getRSReflectionType(
352 const RSExportPrimitiveType *EPT) {
353 return getRSReflectionType(EPT->getType());
354 }
Stephen Hinesa6b54142012-04-09 18:25:08 -0700355
Stephen Hines1f6c3312012-07-03 17:23:33 -0700356 virtual unsigned getSize() const { return (GetSizeInBits(this) >> 3); }
357
Stephen Hinesa6b54142012-04-09 18:25:08 -0700358 std::string getElementName() const {
359 return getRSReflectionType(this)->rs_short_type;
360 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700361}; // RSExportPrimitiveType
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700362
363
364class RSExportPointerType : public RSExportType {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700365 friend class RSExportType;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700366 friend class RSExportFunc;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700367 private:
368 const RSExportType *mPointeeType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700369
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700370 RSExportPointerType(RSContext *Context,
371 const llvm::StringRef &Name,
zonr6315f762010-10-05 15:35:14 +0800372 const RSExportType *PointeeType)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800373 : RSExportType(Context, ExportClassPointer, Name),
zonr6315f762010-10-05 15:35:14 +0800374 mPointeeType(PointeeType) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700375 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700376
Stephen Hinese5e64432010-12-02 18:48:20 -0800377 // @PT was normalized by calling RSExportType::NormalizeType() before calling
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700378 // this.
379 static RSExportPointerType *Create(RSContext *Context,
380 const clang::PointerType *PT,
381 const llvm::StringRef &TypeName);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700382
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700383 virtual llvm::Type *convertToLLVMType() const;
Stephen Hinesecddee32011-07-20 18:30:09 -0700384
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700385 public:
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800386 virtual bool keep();
Zonr Chang641558f2010-10-12 21:07:06 +0800387
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700388 inline const RSExportType *getPointeeType() const { return mPointeeType; }
Zonr Chang641558f2010-10-12 21:07:06 +0800389
390 virtual bool equals(const RSExportable *E) const;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700391}; // RSExportPointerType
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700392
393
394class RSExportVectorType : public RSExportPrimitiveType {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700395 friend class RSExportType;
396 friend class RSExportElement;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700397 private:
Zonr Chang92b344a2010-10-05 20:39:03 +0800398 unsigned mNumElement; // number of element
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700399
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700400 RSExportVectorType(RSContext *Context,
401 const llvm::StringRef &Name,
402 DataType DT,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700403 bool Normalized,
Zonr Chang92b344a2010-10-05 20:39:03 +0800404 unsigned NumElement)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800405 : RSExportPrimitiveType(Context, ExportClassVector, Name,
Stephen Hines2b8fb642012-03-09 00:12:47 -0800406 DT, Normalized),
zonr6315f762010-10-05 15:35:14 +0800407 mNumElement(NumElement) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700408 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700409
Stephen Hinese5e64432010-12-02 18:48:20 -0800410 // @EVT was normalized by calling RSExportType::NormalizeType() before
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700411 // calling this.
412 static RSExportVectorType *Create(RSContext *Context,
413 const clang::ExtVectorType *EVT,
414 const llvm::StringRef &TypeName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700415 bool Normalized = false);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700416
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700417 virtual llvm::Type *convertToLLVMType() const;
Stephen Hinesecddee32011-07-20 18:30:09 -0700418
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700419 public:
420 static llvm::StringRef GetTypeName(const clang::ExtVectorType *EVT);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700421
Zonr Chang92b344a2010-10-05 20:39:03 +0800422 inline unsigned getNumElement() const { return mNumElement; }
Zonr Chang641558f2010-10-12 21:07:06 +0800423
Stephen Hinesa6b54142012-04-09 18:25:08 -0700424 std::string getElementName() const {
425 std::stringstream Name;
426 Name << RSExportPrimitiveType::getRSReflectionType(this)->rs_short_type
427 << "_" << getNumElement();
428 return Name.str();
429 }
430
Zonr Chang641558f2010-10-12 21:07:06 +0800431 virtual bool equals(const RSExportable *E) const;
Zonr Chang92b344a2010-10-05 20:39:03 +0800432};
433
434// Only *square* *float* matrix is supported by now.
435//
436// struct rs_matrix{2x2,3x3,4x4, ..., NxN} should be defined as the following
437// form *exactly*:
438// typedef struct {
439// float m[{NxN}];
440// } rs_matrixNxN;
441//
442// where mDim will be N.
443class RSExportMatrixType : public RSExportType {
444 friend class RSExportType;
445 private:
Zonr Chang2e1dba62010-10-05 22:20:11 +0800446 unsigned mDim; // dimension
Zonr Chang92b344a2010-10-05 20:39:03 +0800447
448 RSExportMatrixType(RSContext *Context,
449 const llvm::StringRef &Name,
450 unsigned Dim)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800451 : RSExportType(Context, ExportClassMatrix, Name),
Zonr Chang92b344a2010-10-05 20:39:03 +0800452 mDim(Dim) {
Zonr Chang92b344a2010-10-05 20:39:03 +0800453 }
454
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700455 virtual llvm::Type *convertToLLVMType() const;
Stephen Hinesecddee32011-07-20 18:30:09 -0700456
Zonr Chang92b344a2010-10-05 20:39:03 +0800457 public:
Stephen Hinese5e64432010-12-02 18:48:20 -0800458 // @RT was normalized by calling RSExportType::NormalizeType() before
Zonr Chang92b344a2010-10-05 20:39:03 +0800459 // calling this.
460 static RSExportMatrixType *Create(RSContext *Context,
461 const clang::RecordType *RT,
462 const llvm::StringRef &TypeName,
463 unsigned Dim);
464
465 inline unsigned getDim() const { return mDim; }
Zonr Chang641558f2010-10-12 21:07:06 +0800466
467 virtual bool equals(const RSExportable *E) const;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700468};
469
Zonr Chang2e1dba62010-10-05 22:20:11 +0800470class RSExportConstantArrayType : public RSExportType {
471 friend class RSExportType;
472 private:
473 const RSExportType *mElementType; // Array element type
474 unsigned mSize; // Array size
475
476 RSExportConstantArrayType(RSContext *Context,
477 const RSExportType *ElementType,
478 unsigned Size)
Stephen Hines340b5552014-09-05 02:03:36 -0700479 : RSExportType(Context, ExportClassConstantArray, "<ConstantArray>"),
Zonr Chang2e1dba62010-10-05 22:20:11 +0800480 mElementType(ElementType),
481 mSize(Size) {
Zonr Chang2e1dba62010-10-05 22:20:11 +0800482 }
483
Stephen Hinese5e64432010-12-02 18:48:20 -0800484 // @CAT was normalized by calling RSExportType::NormalizeType() before
Zonr Chang2e1dba62010-10-05 22:20:11 +0800485 // calling this.
486 static RSExportConstantArrayType *Create(RSContext *Context,
487 const clang::ConstantArrayType *CAT);
488
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700489 virtual llvm::Type *convertToLLVMType() const;
Stephen Hinesecddee32011-07-20 18:30:09 -0700490
Zonr Chang2e1dba62010-10-05 22:20:11 +0800491 public:
Stephen Hines0d26cef2012-05-01 19:23:01 -0700492 virtual unsigned getSize() const { return mSize; }
Zonr Chang2e1dba62010-10-05 22:20:11 +0800493 inline const RSExportType *getElementType() const { return mElementType; }
Zonr Chang641558f2010-10-12 21:07:06 +0800494
Stephen Hinesa6b54142012-04-09 18:25:08 -0700495 std::string getElementName() const {
496 return mElementType->getElementName();
497 }
498
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800499 virtual bool keep();
Zonr Chang641558f2010-10-12 21:07:06 +0800500 virtual bool equals(const RSExportable *E) const;
Zonr Chang2e1dba62010-10-05 22:20:11 +0800501};
502
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700503class RSExportRecordType : public RSExportType {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700504 friend class RSExportType;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700505 public:
506 class Field {
507 private:
508 const RSExportType *mType;
509 // Field name
510 std::string mName;
511 // Link to the struct that contain this field
512 const RSExportRecordType *mParent;
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800513 // Offset in the container
514 size_t mOffset;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700515
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700516 public:
517 Field(const RSExportType *T,
518 const llvm::StringRef &Name,
519 const RSExportRecordType *Parent,
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800520 size_t Offset)
zonr6315f762010-10-05 15:35:14 +0800521 : mType(T),
522 mName(Name.data(), Name.size()),
523 mParent(Parent),
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800524 mOffset(Offset) {
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700525 }
526
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700527 inline const RSExportRecordType *getParent() const { return mParent; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700528 inline const RSExportType *getType() const { return mType; }
529 inline const std::string &getName() const { return mName; }
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800530 inline size_t getOffsetInParent() const { return mOffset; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700531 };
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700532
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700533 typedef std::list<const Field*>::const_iterator const_field_iterator;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700534
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700535 inline const_field_iterator fields_begin() const {
536 return this->mFields.begin();
537 }
538 inline const_field_iterator fields_end() const {
539 return this->mFields.end();
540 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700541
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700542 private:
543 std::list<const Field*> mFields;
544 bool mIsPacked;
545 // Artificial export struct type is not exported by user (and thus it won't
546 // get reflected)
547 bool mIsArtificial;
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700548 size_t mStoreSize;
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800549 size_t mAllocSize;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700550
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700551 RSExportRecordType(RSContext *Context,
552 const llvm::StringRef &Name,
553 bool IsPacked,
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800554 bool IsArtificial,
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700555 size_t StoreSize,
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800556 size_t AllocSize)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800557 : RSExportType(Context, ExportClassRecord, Name),
zonr6315f762010-10-05 15:35:14 +0800558 mIsPacked(IsPacked),
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800559 mIsArtificial(IsArtificial),
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700560 mStoreSize(StoreSize),
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800561 mAllocSize(AllocSize) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700562 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700563
Stephen Hinese5e64432010-12-02 18:48:20 -0800564 // @RT was normalized by calling RSExportType::NormalizeType() before calling
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700565 // this.
566 // @TypeName was retrieved from RSExportType::GetTypeName() before calling
567 // this.
568 static RSExportRecordType *Create(RSContext *Context,
569 const clang::RecordType *RT,
570 const llvm::StringRef &TypeName,
571 bool mIsArtificial = false);
572
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700573 virtual llvm::Type *convertToLLVMType() const;
Stephen Hinesecddee32011-07-20 18:30:09 -0700574
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700575 public:
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800576 inline const std::list<const Field*>& getFields() const { return mFields; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700577 inline bool isPacked() const { return mIsPacked; }
578 inline bool isArtificial() const { return mIsArtificial; }
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700579 virtual size_t getStoreSize() const { return mStoreSize; }
580 virtual size_t getAllocSize() const { return mAllocSize; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700581
Stephen Hinesa6b54142012-04-09 18:25:08 -0700582 virtual std::string getElementName() const {
583 return "ScriptField_" + getName();
584 }
585
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800586 virtual bool keep();
Zonr Chang641558f2010-10-12 21:07:06 +0800587 virtual bool equals(const RSExportable *E) const;
588
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700589 ~RSExportRecordType() {
590 for (std::list<const Field*>::iterator I = mFields.begin(),
591 E = mFields.end();
592 I != E;
593 I++)
594 if (*I != NULL)
595 delete *I;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700596 }
597}; // RSExportRecordType
598
599} // namespace slang
600
Stephen Hinese639eb52010-11-08 19:27:20 -0800601#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_TYPE_H_ NOLINT