blob: 2b2a2ebf210bd2134c94a42543227faac169ea38 [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) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -070038 if (T == nullptr) {
39 return nullptr;
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -070040 }
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) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -070049 if (T == nullptr) {
50 return nullptr;
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -070051 }
52 return GetCanonicalType(T->getElementType());
53}
54
55inline const clang::Type* GetPointeeType(const clang::PointerType *T) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -070056 if (T == nullptr) {
57 return nullptr;
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -070058 }
59 return GetCanonicalType(T->getPointeeType());
60}
61
62inline const clang::Type* GetConstantArrayElementType(const clang::ConstantArrayType *T) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -070063 if (T == nullptr) {
64 return nullptr;
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -070065 }
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 {
Matt Wala7682b662015-07-30 15:53:47 -0700139 // The data type category
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700140 DataTypeCategory category;
Matt Wala7682b662015-07-30 15:53:47 -0700141 // The element name in RenderScript
Stephen Hinesfdd1ba12012-03-08 17:26:32 -0800142 const char * rs_type;
Matt Wala7682b662015-07-30 15:53:47 -0700143 // The short element name in RenderScript
Stephen Hinesfdd1ba12012-03-08 17:26:32 -0800144 const char * rs_short_type;
Matt Wala7682b662015-07-30 15:53:47 -0700145 // The size of the type in bits
Stephen Hinesfdd1ba12012-03-08 17:26:32 -0800146 uint32_t size_in_bits;
Matt Wala7682b662015-07-30 15:53:47 -0700147 // The reflected name in C code
Stephen Hinesfdd1ba12012-03-08 17:26:32 -0800148 const char * c_name;
Matt Wala7682b662015-07-30 15:53:47 -0700149 // The reflected name in Java code
Stephen Hinesfdd1ba12012-03-08 17:26:32 -0800150 const char * java_name;
Matt Wala7682b662015-07-30 15:53:47 -0700151 // The array type that is compatible with Allocations of our type,
152 // for use with copyTo(), copyFrom()
153 const char * java_array_element_name;
154 // The prefix for C vector types
Stephen Hinesfdd1ba12012-03-08 17:26:32 -0800155 const char * rs_c_vector_prefix;
Matt Wala7682b662015-07-30 15:53:47 -0700156 // The prefix for Java vector types
Stephen Hinesfdd1ba12012-03-08 17:26:32 -0800157 const char * rs_java_vector_prefix;
Matt Wala7682b662015-07-30 15:53:47 -0700158 // Indicates an unsigned type undergoing Java promotion
Stephen Hinesfdd1ba12012-03-08 17:26:32 -0800159 bool java_promotion;
160} RSReflectionType;
161
Jason Sams1b6a0882012-03-12 15:07:58 -0700162
163typedef struct RSReflectionTypeData_rec {
164 const RSReflectionType *type;
165 uint32_t vecSize;
166 bool isPointer;
167 uint32_t arraySize;
168
169 // Subelements
170 //std::vector<const struct RSReflectionTypeData_rec *> fields;
171 //std::vector< std::string > fieldNames;
172 //std::vector< uint32_t> fieldOffsetBytes;
173} RSReflectionTypeData;
174
Jean-Luc Brouilleteca05342014-05-15 12:44:21 -0700175// Make a name for types that are too complicated to create the real names.
176std::string CreateDummyName(const char *type, const std::string &name);
177
178inline bool IsDummyName(const llvm::StringRef &Name) {
179 return Name.startswith("<");
180}
Jason Sams1b6a0882012-03-12 15:07:58 -0700181
Zonr Changa41ce1d2010-10-06 02:23:12 +0800182class RSExportType : public RSExportable {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700183 friend class RSExportElement;
184 public:
185 typedef enum {
186 ExportClassPrimitive,
187 ExportClassPointer,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700188 ExportClassVector,
Zonr Chang92b344a2010-10-05 20:39:03 +0800189 ExportClassMatrix,
Zonr Chang2e1dba62010-10-05 22:20:11 +0800190 ExportClassConstantArray,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700191 ExportClassRecord
192 } ExportClass;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700193
Jason Sams1b6a0882012-03-12 15:07:58 -0700194 void convertToRTD(RSReflectionTypeData *rtd) const;
195
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700196 private:
Zonr Chang6b6320a2010-10-05 22:42:01 +0800197 ExportClass mClass;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700198 std::string mName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700199
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700200 // Cache the result after calling convertToLLVMType() at the first time
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700201 mutable llvm::Type *mLLVMType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700202
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700203 protected:
Zonr Chang6b6320a2010-10-05 22:42:01 +0800204 RSExportType(RSContext *Context,
205 ExportClass Class,
206 const llvm::StringRef &Name);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700207
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700208 // Let's make it private since there're some prerequisites to call this
209 // function.
210 //
Stephen Hinese5e64432010-12-02 18:48:20 -0800211 // @T was normalized by calling RSExportType::NormalizeType().
zonr6315f762010-10-05 15:35:14 +0800212 // @TypeName was retrieve from RSExportType::GetTypeName() before calling
213 // this.
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700214 //
215 static RSExportType *Create(RSContext *Context,
216 const clang::Type *T,
zonr6315f762010-10-05 15:35:14 +0800217 const llvm::StringRef &TypeName);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700218
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700219 static llvm::StringRef GetTypeName(const clang::Type *T);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700220
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700221 // This function convert the RSExportType to LLVM type. Actually, it should be
222 // "convert Clang type to LLVM type." However, clang doesn't make this API
223 // (lib/CodeGen/CodeGenTypes.h) public, we need to do by ourselves.
224 //
225 // Once we can get LLVM type, we can use LLVM to get alignment information,
226 // allocation size of a given type and structure layout that LLVM used
227 // (all of these information are target dependent) without dealing with these
228 // by ourselves.
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700229 virtual llvm::Type *convertToLLVMType() const = 0;
Stephen Hinese639eb52010-11-08 19:27:20 -0800230 // Record type may recursively reference its type definition. We need a
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800231 // temporary type setup before the type construction gets done.
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700232 inline void setAbstractLLVMType(llvm::Type *LLVMType) const {
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800233 mLLVMType = LLVMType;
234 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700235
Zonr Changa7a828d2010-10-22 03:44:27 +0800236 virtual ~RSExportType();
Stephen Hinesecddee32011-07-20 18:30:09 -0700237
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700238 public:
Stephen Hinese5e64432010-12-02 18:48:20 -0800239 // This function additionally verifies that the Type T is exportable.
240 // If it is not, this function returns false. Otherwise it returns true.
241 static bool NormalizeType(const clang::Type *&T,
242 llvm::StringRef &TypeName,
Stephen Hines48d893d2013-12-06 18:00:27 -0800243 RSContext *Context,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700244 const clang::VarDecl *VD);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700245
Stephen Hines11274a72012-09-26 19:14:20 -0700246 // This function checks whether the specified type can be handled by RS/FS.
247 // If it cannot, this function returns false. Otherwise it returns true.
248 // Filterscript has additional restrictions on supported types.
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800249 static bool ValidateType(slang::RSContext *Context, clang::ASTContext &C,
250 clang::QualType QT, clang::NamedDecl *ND,
251 clang::SourceLocation Loc, unsigned int TargetAPI,
Stephen Hinesab94bcc2015-06-11 17:00:04 -0700252 bool IsFilterscript, bool IsExtern);
Stephen Hines11274a72012-09-26 19:14:20 -0700253
Stephen Hines78e69cb2011-04-22 15:03:19 -0700254 // This function ensures that the VarDecl can be properly handled by RS.
255 // If it cannot, this function returns false. Otherwise it returns true.
Stephen Hines11274a72012-09-26 19:14:20 -0700256 // Filterscript has additional restrictions on supported types.
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800257 static bool ValidateVarDecl(slang::RSContext *Context, clang::VarDecl *VD,
258 unsigned int TargetAPI, bool IsFilterscript);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700259
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700260 // @T may not be normalized
261 static RSExportType *Create(RSContext *Context, const clang::Type *T);
262 static RSExportType *CreateFromDecl(RSContext *Context,
263 const clang::VarDecl *VD);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700264
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700265 static const clang::Type *GetTypeOfDecl(const clang::DeclaratorDecl *DD);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700266
Zonr Chang6b6320a2010-10-05 22:42:01 +0800267 inline ExportClass getClass() const { return mClass; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700268
Stephen Hines0d26cef2012-05-01 19:23:01 -0700269 virtual unsigned getSize() const { return 1; }
270
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700271 inline llvm::Type *getLLVMType() const {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700272 if (mLLVMType == nullptr)
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700273 mLLVMType = convertToLLVMType();
274 return mLLVMType;
275 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700276
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700277 // Return the maximum number of bytes that may be written when this type is stored.
278 virtual size_t getStoreSize() const;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700279
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700280 // Return the distance in bytes between successive elements of this type; it includes padding.
281 virtual size_t getAllocSize() const;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700282
Zonr Chang641558f2010-10-12 21:07:06 +0800283 inline const std::string &getName() const { return mName; }
284
Stephen Hinesa6b54142012-04-09 18:25:08 -0700285 virtual std::string getElementName() const {
286 // Base case is actually an invalid C/Java identifier.
287 return "@@INVALID@@";
288 }
289
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800290 virtual bool keep();
Zonr Chang641558f2010-10-12 21:07:06 +0800291 virtual bool equals(const RSExportable *E) const;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700292}; // RSExportType
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700293
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700294// Primitive types
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700295class RSExportPrimitiveType : public RSExportType {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700296 friend class RSExportType;
297 friend class RSExportElement;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700298 private:
299 DataType mType;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700300 bool mNormalized;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700301
Zonr Changb1771ef2010-10-22 18:03:46 +0800302 typedef llvm::StringMap<DataType> RSSpecificTypeMapTy;
303 static llvm::ManagedStatic<RSSpecificTypeMapTy> RSSpecificTypeMap;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700304
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700305 static const size_t SizeOfDataTypeInBits[];
Stephen Hinese5e64432010-12-02 18:48:20 -0800306 // @T was normalized by calling RSExportType::NormalizeType() before calling
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700307 // this.
308 // @TypeName was retrieved from RSExportType::GetTypeName() before calling
309 // this
310 static RSExportPrimitiveType *Create(RSContext *Context,
311 const clang::Type *T,
312 const llvm::StringRef &TypeName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700313 bool Normalized = false);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700314
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700315 protected:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700316 RSExportPrimitiveType(RSContext *Context,
Zonr Chang6b6320a2010-10-05 22:42:01 +0800317 // for derived class to set their type class
318 ExportClass Class,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700319 const llvm::StringRef &Name,
320 DataType DT,
zonr6315f762010-10-05 15:35:14 +0800321 bool Normalized)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800322 : RSExportType(Context, Class, Name),
zonr6315f762010-10-05 15:35:14 +0800323 mType(DT),
zonr6315f762010-10-05 15:35:14 +0800324 mNormalized(Normalized) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700325 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700326
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700327 virtual llvm::Type *convertToLLVMType() const;
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800328
329 static DataType GetDataType(RSContext *Context, const clang::Type *T);
330
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700331 public:
Stephen Hinesdd6206b2010-12-09 19:39:22 -0800332 // T is normalized by calling RSExportType::NormalizeType() before
333 // calling this
334 static bool IsPrimitiveType(const clang::Type *T);
335
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700336 // @T may not be normalized
337 static RSExportPrimitiveType *Create(RSContext *Context,
Stephen Hines2b8fb642012-03-09 00:12:47 -0800338 const clang::Type *T);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700339
Zonr Changb1771ef2010-10-22 18:03:46 +0800340 static DataType GetRSSpecificType(const llvm::StringRef &TypeName);
341 static DataType GetRSSpecificType(const clang::Type *T);
342
343 static bool IsRSMatrixType(DataType DT);
344 static bool IsRSObjectType(DataType DT);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800345 static bool IsRSObjectType(const clang::Type *T) {
346 return IsRSObjectType(GetRSSpecificType(T));
347 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700348
Stephen Hinesfeaca062011-02-04 14:08:13 -0800349 // Determines whether T is [an array of] struct that contains at least one
350 // RS object type within it.
351 static bool IsStructureTypeWithRSObject(const clang::Type *T);
352
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700353 static size_t GetSizeInBits(const RSExportPrimitiveType *EPT);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700354
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700355 inline DataType getType() const { return mType; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700356 inline bool isRSObjectType() const {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700357 return IsRSObjectType(mType);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700358 }
Zonr Chang641558f2010-10-12 21:07:06 +0800359
360 virtual bool equals(const RSExportable *E) const;
Stephen Hinesfdd1ba12012-03-08 17:26:32 -0800361
362 static RSReflectionType *getRSReflectionType(DataType DT);
363 static RSReflectionType *getRSReflectionType(
364 const RSExportPrimitiveType *EPT) {
365 return getRSReflectionType(EPT->getType());
366 }
Stephen Hinesa6b54142012-04-09 18:25:08 -0700367
Stephen Hines1f6c3312012-07-03 17:23:33 -0700368 virtual unsigned getSize() const { return (GetSizeInBits(this) >> 3); }
369
Stephen Hinesa6b54142012-04-09 18:25:08 -0700370 std::string getElementName() const {
371 return getRSReflectionType(this)->rs_short_type;
372 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700373}; // RSExportPrimitiveType
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700374
375
376class RSExportPointerType : public RSExportType {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700377 friend class RSExportType;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700378 friend class RSExportFunc;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700379 private:
380 const RSExportType *mPointeeType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700381
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700382 RSExportPointerType(RSContext *Context,
383 const llvm::StringRef &Name,
zonr6315f762010-10-05 15:35:14 +0800384 const RSExportType *PointeeType)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800385 : RSExportType(Context, ExportClassPointer, Name),
zonr6315f762010-10-05 15:35:14 +0800386 mPointeeType(PointeeType) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700387 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700388
Stephen Hinese5e64432010-12-02 18:48:20 -0800389 // @PT was normalized by calling RSExportType::NormalizeType() before calling
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700390 // this.
391 static RSExportPointerType *Create(RSContext *Context,
392 const clang::PointerType *PT,
393 const llvm::StringRef &TypeName);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700394
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700395 virtual llvm::Type *convertToLLVMType() const;
Stephen Hinesecddee32011-07-20 18:30:09 -0700396
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700397 public:
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800398 virtual bool keep();
Zonr Chang641558f2010-10-12 21:07:06 +0800399
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700400 inline const RSExportType *getPointeeType() const { return mPointeeType; }
Zonr Chang641558f2010-10-12 21:07:06 +0800401
402 virtual bool equals(const RSExportable *E) const;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700403}; // RSExportPointerType
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700404
405
406class RSExportVectorType : public RSExportPrimitiveType {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700407 friend class RSExportType;
408 friend class RSExportElement;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700409 private:
Zonr Chang92b344a2010-10-05 20:39:03 +0800410 unsigned mNumElement; // number of element
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700411
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700412 RSExportVectorType(RSContext *Context,
413 const llvm::StringRef &Name,
414 DataType DT,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700415 bool Normalized,
Zonr Chang92b344a2010-10-05 20:39:03 +0800416 unsigned NumElement)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800417 : RSExportPrimitiveType(Context, ExportClassVector, Name,
Stephen Hines2b8fb642012-03-09 00:12:47 -0800418 DT, Normalized),
zonr6315f762010-10-05 15:35:14 +0800419 mNumElement(NumElement) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700420 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700421
Stephen Hinese5e64432010-12-02 18:48:20 -0800422 // @EVT was normalized by calling RSExportType::NormalizeType() before
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700423 // calling this.
424 static RSExportVectorType *Create(RSContext *Context,
425 const clang::ExtVectorType *EVT,
426 const llvm::StringRef &TypeName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700427 bool Normalized = false);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700428
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700429 virtual llvm::Type *convertToLLVMType() const;
Stephen Hinesecddee32011-07-20 18:30:09 -0700430
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700431 public:
432 static llvm::StringRef GetTypeName(const clang::ExtVectorType *EVT);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700433
Zonr Chang92b344a2010-10-05 20:39:03 +0800434 inline unsigned getNumElement() const { return mNumElement; }
Zonr Chang641558f2010-10-12 21:07:06 +0800435
Stephen Hinesa6b54142012-04-09 18:25:08 -0700436 std::string getElementName() const {
437 std::stringstream Name;
438 Name << RSExportPrimitiveType::getRSReflectionType(this)->rs_short_type
439 << "_" << getNumElement();
440 return Name.str();
441 }
442
Zonr Chang641558f2010-10-12 21:07:06 +0800443 virtual bool equals(const RSExportable *E) const;
Zonr Chang92b344a2010-10-05 20:39:03 +0800444};
445
446// Only *square* *float* matrix is supported by now.
447//
448// struct rs_matrix{2x2,3x3,4x4, ..., NxN} should be defined as the following
449// form *exactly*:
450// typedef struct {
451// float m[{NxN}];
452// } rs_matrixNxN;
453//
454// where mDim will be N.
455class RSExportMatrixType : public RSExportType {
456 friend class RSExportType;
457 private:
Zonr Chang2e1dba62010-10-05 22:20:11 +0800458 unsigned mDim; // dimension
Zonr Chang92b344a2010-10-05 20:39:03 +0800459
460 RSExportMatrixType(RSContext *Context,
461 const llvm::StringRef &Name,
462 unsigned Dim)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800463 : RSExportType(Context, ExportClassMatrix, Name),
Zonr Chang92b344a2010-10-05 20:39:03 +0800464 mDim(Dim) {
Zonr Chang92b344a2010-10-05 20:39:03 +0800465 }
466
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700467 virtual llvm::Type *convertToLLVMType() const;
Stephen Hinesecddee32011-07-20 18:30:09 -0700468
Zonr Chang92b344a2010-10-05 20:39:03 +0800469 public:
Stephen Hinese5e64432010-12-02 18:48:20 -0800470 // @RT was normalized by calling RSExportType::NormalizeType() before
Zonr Chang92b344a2010-10-05 20:39:03 +0800471 // calling this.
472 static RSExportMatrixType *Create(RSContext *Context,
473 const clang::RecordType *RT,
474 const llvm::StringRef &TypeName,
475 unsigned Dim);
476
477 inline unsigned getDim() const { return mDim; }
Zonr Chang641558f2010-10-12 21:07:06 +0800478
479 virtual bool equals(const RSExportable *E) const;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700480};
481
Zonr Chang2e1dba62010-10-05 22:20:11 +0800482class RSExportConstantArrayType : public RSExportType {
483 friend class RSExportType;
484 private:
485 const RSExportType *mElementType; // Array element type
486 unsigned mSize; // Array size
487
488 RSExportConstantArrayType(RSContext *Context,
489 const RSExportType *ElementType,
490 unsigned Size)
Stephen Hines340b5552014-09-05 02:03:36 -0700491 : RSExportType(Context, ExportClassConstantArray, "<ConstantArray>"),
Zonr Chang2e1dba62010-10-05 22:20:11 +0800492 mElementType(ElementType),
493 mSize(Size) {
Zonr Chang2e1dba62010-10-05 22:20:11 +0800494 }
495
Stephen Hinese5e64432010-12-02 18:48:20 -0800496 // @CAT was normalized by calling RSExportType::NormalizeType() before
Zonr Chang2e1dba62010-10-05 22:20:11 +0800497 // calling this.
498 static RSExportConstantArrayType *Create(RSContext *Context,
499 const clang::ConstantArrayType *CAT);
500
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700501 virtual llvm::Type *convertToLLVMType() const;
Stephen Hinesecddee32011-07-20 18:30:09 -0700502
Zonr Chang2e1dba62010-10-05 22:20:11 +0800503 public:
Stephen Hines0d26cef2012-05-01 19:23:01 -0700504 virtual unsigned getSize() const { return mSize; }
Zonr Chang2e1dba62010-10-05 22:20:11 +0800505 inline const RSExportType *getElementType() const { return mElementType; }
Zonr Chang641558f2010-10-12 21:07:06 +0800506
Stephen Hinesa6b54142012-04-09 18:25:08 -0700507 std::string getElementName() const {
508 return mElementType->getElementName();
509 }
510
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800511 virtual bool keep();
Zonr Chang641558f2010-10-12 21:07:06 +0800512 virtual bool equals(const RSExportable *E) const;
Zonr Chang2e1dba62010-10-05 22:20:11 +0800513};
514
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700515class RSExportRecordType : public RSExportType {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700516 friend class RSExportType;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700517 public:
518 class Field {
519 private:
520 const RSExportType *mType;
521 // Field name
522 std::string mName;
523 // Link to the struct that contain this field
524 const RSExportRecordType *mParent;
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800525 // Offset in the container
526 size_t mOffset;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700527
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700528 public:
529 Field(const RSExportType *T,
530 const llvm::StringRef &Name,
531 const RSExportRecordType *Parent,
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800532 size_t Offset)
zonr6315f762010-10-05 15:35:14 +0800533 : mType(T),
534 mName(Name.data(), Name.size()),
535 mParent(Parent),
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800536 mOffset(Offset) {
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700537 }
538
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700539 inline const RSExportRecordType *getParent() const { return mParent; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700540 inline const RSExportType *getType() const { return mType; }
541 inline const std::string &getName() const { return mName; }
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800542 inline size_t getOffsetInParent() const { return mOffset; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700543 };
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700544
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700545 typedef std::list<const Field*>::const_iterator const_field_iterator;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700546
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700547 inline const_field_iterator fields_begin() const {
548 return this->mFields.begin();
549 }
550 inline const_field_iterator fields_end() const {
551 return this->mFields.end();
552 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700553
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700554 private:
555 std::list<const Field*> mFields;
556 bool mIsPacked;
557 // Artificial export struct type is not exported by user (and thus it won't
558 // get reflected)
559 bool mIsArtificial;
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700560 size_t mStoreSize;
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800561 size_t mAllocSize;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700562
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700563 RSExportRecordType(RSContext *Context,
564 const llvm::StringRef &Name,
565 bool IsPacked,
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800566 bool IsArtificial,
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700567 size_t StoreSize,
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800568 size_t AllocSize)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800569 : RSExportType(Context, ExportClassRecord, Name),
zonr6315f762010-10-05 15:35:14 +0800570 mIsPacked(IsPacked),
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800571 mIsArtificial(IsArtificial),
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700572 mStoreSize(StoreSize),
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800573 mAllocSize(AllocSize) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700574 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700575
Stephen Hinese5e64432010-12-02 18:48:20 -0800576 // @RT was normalized by calling RSExportType::NormalizeType() before calling
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700577 // this.
578 // @TypeName was retrieved from RSExportType::GetTypeName() before calling
579 // this.
580 static RSExportRecordType *Create(RSContext *Context,
581 const clang::RecordType *RT,
582 const llvm::StringRef &TypeName,
583 bool mIsArtificial = false);
584
Shih-wei Liao7c67e572011-07-19 05:54:53 -0700585 virtual llvm::Type *convertToLLVMType() const;
Stephen Hinesecddee32011-07-20 18:30:09 -0700586
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700587 public:
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800588 inline const std::list<const Field*>& getFields() const { return mFields; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700589 inline bool isPacked() const { return mIsPacked; }
590 inline bool isArtificial() const { return mIsArtificial; }
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700591 virtual size_t getStoreSize() const { return mStoreSize; }
592 virtual size_t getAllocSize() const { return mAllocSize; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700593
Stephen Hinesa6b54142012-04-09 18:25:08 -0700594 virtual std::string getElementName() const {
595 return "ScriptField_" + getName();
596 }
597
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800598 virtual bool keep();
Zonr Chang641558f2010-10-12 21:07:06 +0800599 virtual bool equals(const RSExportable *E) const;
600
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700601 ~RSExportRecordType() {
602 for (std::list<const Field*>::iterator I = mFields.begin(),
603 E = mFields.end();
604 I != E;
605 I++)
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700606 if (*I != nullptr)
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700607 delete *I;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700608 }
609}; // RSExportRecordType
610
611} // namespace slang
612
Stephen Hinese639eb52010-11-08 19:27:20 -0800613#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_TYPE_H_ NOLINT