blob: 43bbb847f1fb697bed01d51993a498c2975b65d5 [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 llvm::Type *RSObjectLLVMType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700294
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700295 static const size_t SizeOfDataTypeInBits[];
Stephen Hinese5e64432010-12-02 18:48:20 -0800296 // @T was normalized by calling RSExportType::NormalizeType() before calling
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700297 // this.
298 // @TypeName was retrieved from RSExportType::GetTypeName() before calling
299 // this
300 static RSExportPrimitiveType *Create(RSContext *Context,
301 const clang::Type *T,
302 const llvm::StringRef &TypeName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700303 bool Normalized = false);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700304
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700305 protected:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700306 RSExportPrimitiveType(RSContext *Context,
Zonr Chang6b6320a2010-10-05 22:42:01 +0800307 // for derived class to set their type class
308 ExportClass Class,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700309 const llvm::StringRef &Name,
310 DataType DT,
zonr6315f762010-10-05 15:35:14 +0800311 bool Normalized)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800312 : RSExportType(Context, Class, Name),
zonr6315f762010-10-05 15:35:14 +0800313 mType(DT),
zonr6315f762010-10-05 15:35:14 +0800314 mNormalized(Normalized) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700315 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700316
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700317 virtual llvm::Type *convertToLLVMType() const;
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800318
319 static DataType GetDataType(RSContext *Context, const clang::Type *T);
320
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700321 public:
Stephen Hinesdd6206b2010-12-09 19:39:22 -0800322 // T is normalized by calling RSExportType::NormalizeType() before
323 // calling this
324 static bool IsPrimitiveType(const clang::Type *T);
325
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700326 // @T may not be normalized
327 static RSExportPrimitiveType *Create(RSContext *Context,
Stephen Hines2b8fb642012-03-09 00:12:47 -0800328 const clang::Type *T);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700329
Zonr Changb1771ef2010-10-22 18:03:46 +0800330 static DataType GetRSSpecificType(const llvm::StringRef &TypeName);
331 static DataType GetRSSpecificType(const clang::Type *T);
332
333 static bool IsRSMatrixType(DataType DT);
334 static bool IsRSObjectType(DataType DT);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800335 static bool IsRSObjectType(const clang::Type *T) {
336 return IsRSObjectType(GetRSSpecificType(T));
337 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700338
Stephen Hinesfeaca062011-02-04 14:08:13 -0800339 // Determines whether T is [an array of] struct that contains at least one
340 // RS object type within it.
341 static bool IsStructureTypeWithRSObject(const clang::Type *T);
342
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700343 static size_t GetSizeInBits(const RSExportPrimitiveType *EPT);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700344
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700345 inline DataType getType() const { return mType; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700346 inline bool isRSObjectType() const {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700347 return IsRSObjectType(mType);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700348 }
Zonr Chang641558f2010-10-12 21:07:06 +0800349
350 virtual bool equals(const RSExportable *E) const;
Stephen Hinesfdd1ba12012-03-08 17:26:32 -0800351
352 static RSReflectionType *getRSReflectionType(DataType DT);
353 static RSReflectionType *getRSReflectionType(
354 const RSExportPrimitiveType *EPT) {
355 return getRSReflectionType(EPT->getType());
356 }
Stephen Hinesa6b54142012-04-09 18:25:08 -0700357
Stephen Hines1f6c3312012-07-03 17:23:33 -0700358 virtual unsigned getSize() const { return (GetSizeInBits(this) >> 3); }
359
Stephen Hinesa6b54142012-04-09 18:25:08 -0700360 std::string getElementName() const {
361 return getRSReflectionType(this)->rs_short_type;
362 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700363}; // RSExportPrimitiveType
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700364
365
366class RSExportPointerType : public RSExportType {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700367 friend class RSExportType;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700368 friend class RSExportFunc;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700369 private:
370 const RSExportType *mPointeeType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700371
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700372 RSExportPointerType(RSContext *Context,
373 const llvm::StringRef &Name,
zonr6315f762010-10-05 15:35:14 +0800374 const RSExportType *PointeeType)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800375 : RSExportType(Context, ExportClassPointer, Name),
zonr6315f762010-10-05 15:35:14 +0800376 mPointeeType(PointeeType) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700377 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700378
Stephen Hinese5e64432010-12-02 18:48:20 -0800379 // @PT was normalized by calling RSExportType::NormalizeType() before calling
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700380 // this.
381 static RSExportPointerType *Create(RSContext *Context,
382 const clang::PointerType *PT,
383 const llvm::StringRef &TypeName);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700384
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700385 virtual llvm::Type *convertToLLVMType() const;
Stephen Hinesecddee32011-07-20 18:30:09 -0700386
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700387 public:
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800388 virtual bool keep();
Zonr Chang641558f2010-10-12 21:07:06 +0800389
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700390 inline const RSExportType *getPointeeType() const { return mPointeeType; }
Zonr Chang641558f2010-10-12 21:07:06 +0800391
392 virtual bool equals(const RSExportable *E) const;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700393}; // RSExportPointerType
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700394
395
396class RSExportVectorType : public RSExportPrimitiveType {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700397 friend class RSExportType;
398 friend class RSExportElement;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700399 private:
Zonr Chang92b344a2010-10-05 20:39:03 +0800400 unsigned mNumElement; // number of element
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700401
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700402 RSExportVectorType(RSContext *Context,
403 const llvm::StringRef &Name,
404 DataType DT,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700405 bool Normalized,
Zonr Chang92b344a2010-10-05 20:39:03 +0800406 unsigned NumElement)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800407 : RSExportPrimitiveType(Context, ExportClassVector, Name,
Stephen Hines2b8fb642012-03-09 00:12:47 -0800408 DT, Normalized),
zonr6315f762010-10-05 15:35:14 +0800409 mNumElement(NumElement) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700410 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700411
Stephen Hinese5e64432010-12-02 18:48:20 -0800412 // @EVT was normalized by calling RSExportType::NormalizeType() before
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700413 // calling this.
414 static RSExportVectorType *Create(RSContext *Context,
415 const clang::ExtVectorType *EVT,
416 const llvm::StringRef &TypeName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700417 bool Normalized = false);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700418
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700419 virtual llvm::Type *convertToLLVMType() const;
Stephen Hinesecddee32011-07-20 18:30:09 -0700420
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700421 public:
422 static llvm::StringRef GetTypeName(const clang::ExtVectorType *EVT);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700423
Zonr Chang92b344a2010-10-05 20:39:03 +0800424 inline unsigned getNumElement() const { return mNumElement; }
Zonr Chang641558f2010-10-12 21:07:06 +0800425
Stephen Hinesa6b54142012-04-09 18:25:08 -0700426 std::string getElementName() const {
427 std::stringstream Name;
428 Name << RSExportPrimitiveType::getRSReflectionType(this)->rs_short_type
429 << "_" << getNumElement();
430 return Name.str();
431 }
432
Zonr Chang641558f2010-10-12 21:07:06 +0800433 virtual bool equals(const RSExportable *E) const;
Zonr Chang92b344a2010-10-05 20:39:03 +0800434};
435
436// Only *square* *float* matrix is supported by now.
437//
438// struct rs_matrix{2x2,3x3,4x4, ..., NxN} should be defined as the following
439// form *exactly*:
440// typedef struct {
441// float m[{NxN}];
442// } rs_matrixNxN;
443//
444// where mDim will be N.
445class RSExportMatrixType : public RSExportType {
446 friend class RSExportType;
447 private:
Zonr Chang2e1dba62010-10-05 22:20:11 +0800448 unsigned mDim; // dimension
Zonr Chang92b344a2010-10-05 20:39:03 +0800449
450 RSExportMatrixType(RSContext *Context,
451 const llvm::StringRef &Name,
452 unsigned Dim)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800453 : RSExportType(Context, ExportClassMatrix, Name),
Zonr Chang92b344a2010-10-05 20:39:03 +0800454 mDim(Dim) {
Zonr Chang92b344a2010-10-05 20:39:03 +0800455 }
456
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700457 virtual llvm::Type *convertToLLVMType() const;
Stephen Hinesecddee32011-07-20 18:30:09 -0700458
Zonr Chang92b344a2010-10-05 20:39:03 +0800459 public:
Stephen Hinese5e64432010-12-02 18:48:20 -0800460 // @RT was normalized by calling RSExportType::NormalizeType() before
Zonr Chang92b344a2010-10-05 20:39:03 +0800461 // calling this.
462 static RSExportMatrixType *Create(RSContext *Context,
463 const clang::RecordType *RT,
464 const llvm::StringRef &TypeName,
465 unsigned Dim);
466
467 inline unsigned getDim() const { return mDim; }
Zonr Chang641558f2010-10-12 21:07:06 +0800468
469 virtual bool equals(const RSExportable *E) const;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700470};
471
Zonr Chang2e1dba62010-10-05 22:20:11 +0800472class RSExportConstantArrayType : public RSExportType {
473 friend class RSExportType;
474 private:
475 const RSExportType *mElementType; // Array element type
476 unsigned mSize; // Array size
477
478 RSExportConstantArrayType(RSContext *Context,
479 const RSExportType *ElementType,
480 unsigned Size)
481 : RSExportType(Context,
Zonr Chang6b6320a2010-10-05 22:42:01 +0800482 ExportClassConstantArray,
Jean-Luc Brouilleteca05342014-05-15 12:44:21 -0700483 CreateDummyName("ConstantArray", std::string())),
Zonr Chang2e1dba62010-10-05 22:20:11 +0800484 mElementType(ElementType),
485 mSize(Size) {
Zonr Chang2e1dba62010-10-05 22:20:11 +0800486 }
487
Stephen Hinese5e64432010-12-02 18:48:20 -0800488 // @CAT was normalized by calling RSExportType::NormalizeType() before
Zonr Chang2e1dba62010-10-05 22:20:11 +0800489 // calling this.
490 static RSExportConstantArrayType *Create(RSContext *Context,
491 const clang::ConstantArrayType *CAT);
492
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700493 virtual llvm::Type *convertToLLVMType() const;
Stephen Hinesecddee32011-07-20 18:30:09 -0700494
Zonr Chang2e1dba62010-10-05 22:20:11 +0800495 public:
Stephen Hines0d26cef2012-05-01 19:23:01 -0700496 virtual unsigned getSize() const { return mSize; }
Zonr Chang2e1dba62010-10-05 22:20:11 +0800497 inline const RSExportType *getElementType() const { return mElementType; }
Zonr Chang641558f2010-10-12 21:07:06 +0800498
Stephen Hinesa6b54142012-04-09 18:25:08 -0700499 std::string getElementName() const {
500 return mElementType->getElementName();
501 }
502
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800503 virtual bool keep();
Zonr Chang641558f2010-10-12 21:07:06 +0800504 virtual bool equals(const RSExportable *E) const;
Zonr Chang2e1dba62010-10-05 22:20:11 +0800505};
506
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700507class RSExportRecordType : public RSExportType {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700508 friend class RSExportType;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700509 public:
510 class Field {
511 private:
512 const RSExportType *mType;
513 // Field name
514 std::string mName;
515 // Link to the struct that contain this field
516 const RSExportRecordType *mParent;
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800517 // Offset in the container
518 size_t mOffset;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700519
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700520 public:
521 Field(const RSExportType *T,
522 const llvm::StringRef &Name,
523 const RSExportRecordType *Parent,
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800524 size_t Offset)
zonr6315f762010-10-05 15:35:14 +0800525 : mType(T),
526 mName(Name.data(), Name.size()),
527 mParent(Parent),
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800528 mOffset(Offset) {
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700529 }
530
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700531 inline const RSExportRecordType *getParent() const { return mParent; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700532 inline const RSExportType *getType() const { return mType; }
533 inline const std::string &getName() const { return mName; }
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800534 inline size_t getOffsetInParent() const { return mOffset; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700535 };
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700536
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700537 typedef std::list<const Field*>::const_iterator const_field_iterator;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700538
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700539 inline const_field_iterator fields_begin() const {
540 return this->mFields.begin();
541 }
542 inline const_field_iterator fields_end() const {
543 return this->mFields.end();
544 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700545
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700546 private:
547 std::list<const Field*> mFields;
548 bool mIsPacked;
549 // Artificial export struct type is not exported by user (and thus it won't
550 // get reflected)
551 bool mIsArtificial;
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700552 size_t mStoreSize;
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800553 size_t mAllocSize;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700554
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700555 RSExportRecordType(RSContext *Context,
556 const llvm::StringRef &Name,
557 bool IsPacked,
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800558 bool IsArtificial,
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700559 size_t StoreSize,
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800560 size_t AllocSize)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800561 : RSExportType(Context, ExportClassRecord, Name),
zonr6315f762010-10-05 15:35:14 +0800562 mIsPacked(IsPacked),
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800563 mIsArtificial(IsArtificial),
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700564 mStoreSize(StoreSize),
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800565 mAllocSize(AllocSize) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700566 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700567
Stephen Hinese5e64432010-12-02 18:48:20 -0800568 // @RT was normalized by calling RSExportType::NormalizeType() before calling
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700569 // this.
570 // @TypeName was retrieved from RSExportType::GetTypeName() before calling
571 // this.
572 static RSExportRecordType *Create(RSContext *Context,
573 const clang::RecordType *RT,
574 const llvm::StringRef &TypeName,
575 bool mIsArtificial = false);
576
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700577 virtual llvm::Type *convertToLLVMType() const;
Stephen Hinesecddee32011-07-20 18:30:09 -0700578
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700579 public:
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800580 inline const std::list<const Field*>& getFields() const { return mFields; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700581 inline bool isPacked() const { return mIsPacked; }
582 inline bool isArtificial() const { return mIsArtificial; }
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700583 virtual size_t getStoreSize() const { return mStoreSize; }
584 virtual size_t getAllocSize() const { return mAllocSize; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700585
Stephen Hinesa6b54142012-04-09 18:25:08 -0700586 virtual std::string getElementName() const {
587 return "ScriptField_" + getName();
588 }
589
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800590 virtual bool keep();
Zonr Chang641558f2010-10-12 21:07:06 +0800591 virtual bool equals(const RSExportable *E) const;
592
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700593 ~RSExportRecordType() {
594 for (std::list<const Field*>::iterator I = mFields.begin(),
595 E = mFields.end();
596 I != E;
597 I++)
598 if (*I != NULL)
599 delete *I;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700600 }
601}; // RSExportRecordType
602
603} // namespace slang
604
Stephen Hinese639eb52010-11-08 19:27:20 -0800605#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_TYPE_H_ NOLINT