blob: abbaf3aa03ff6646075bf9e3bfe9351b46bf211e [file] [log] [blame]
Zonr Changc383a502010-10-12 01:52:08 +08001/*
2 * Copyright 2010, The Android Open Source Project
3 *
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>
23
zonr6315f762010-10-05 15:35:14 +080024#include "clang/AST/Decl.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080025#include "clang/AST/Type.h"
26
27#include "llvm/ADT/SmallPtrSet.h"
28#include "llvm/ADT/StringMap.h"
29#include "llvm/ADT/StringRef.h"
30
31#include "llvm/Support/ManagedStatic.h"
zonr6315f762010-10-05 15:35:14 +080032
Zonr Changa41ce1d2010-10-06 02:23:12 +080033#include "slang_rs_exportable.h"
34
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070035#define GET_CANONICAL_TYPE(T) \
Zonr Chang2e1dba62010-10-05 22:20:11 +080036 (((T) == NULL) ? NULL : (T)->getCanonicalTypeInternal().getTypePtr())
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070037#define UNSAFE_CAST_TYPE(TT, T) \
Zonr Chang2e1dba62010-10-05 22:20:11 +080038 static_cast<TT*>(T->getCanonicalTypeInternal().getTypePtr())
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070039#define GET_EXT_VECTOR_ELEMENT_TYPE(T) \
Zonr Chang2e1dba62010-10-05 22:20:11 +080040 (((T) == NULL) ? NULL : \
41 GET_CANONICAL_TYPE((T)->getElementType().getTypePtr()))
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070042#define GET_POINTEE_TYPE(T) \
Zonr Chang2e1dba62010-10-05 22:20:11 +080043 (((T) == NULL) ? NULL : \
44 GET_CANONICAL_TYPE((T)->getPointeeType().getTypePtr()))
45#define GET_CONSTANT_ARRAY_ELEMENT_TYPE(T) \
46 (((T) == NULL) ? NULL : \
47 GET_CANONICAL_TYPE((T)->getElementType().getTypePtr()))
Zonr Chang0da0a7d2010-10-05 21:26:37 +080048#define DUMMY_RS_TYPE_NAME_PREFIX "<"
49#define DUMMY_RS_TYPE_NAME_POSTFIX ">"
Zonr Chang2e1dba62010-10-05 22:20:11 +080050#define DUMMY_TYPE_NAME_FOR_RS_CONSTANT_ARRAY_TYPE \
51 DUMMY_RS_TYPE_NAME_PREFIX"ConstantArray"DUMMY_RS_TYPE_NAME_POSTFIX
Shih-wei Liao462aefd2010-06-04 15:32:04 -070052
Zonr Changa7a828d2010-10-22 03:44:27 +080053union RSType;
54
Shih-wei Liao462aefd2010-06-04 15:32:04 -070055namespace llvm {
zonr6315f762010-10-05 15:35:14 +080056 class Type;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070057} // namespace llvm
Shih-wei Liao462aefd2010-06-04 15:32:04 -070058
59namespace slang {
60
zonr6315f762010-10-05 15:35:14 +080061 class RSContext;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070062
Zonr Changa41ce1d2010-10-06 02:23:12 +080063class RSExportType : public RSExportable {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070064 friend class RSExportElement;
65 public:
66 typedef enum {
67 ExportClassPrimitive,
68 ExportClassPointer,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070069 ExportClassVector,
Zonr Chang92b344a2010-10-05 20:39:03 +080070 ExportClassMatrix,
Zonr Chang2e1dba62010-10-05 22:20:11 +080071 ExportClassConstantArray,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070072 ExportClassRecord
73 } ExportClass;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070074
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070075 private:
Zonr Chang6b6320a2010-10-05 22:42:01 +080076 ExportClass mClass;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070077 std::string mName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070078
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070079 // Cache the result after calling convertToLLVMType() at the first time
80 mutable const llvm::Type *mLLVMType;
Zonr Changa7a828d2010-10-22 03:44:27 +080081 // Cache the result after calling convertToSpecType() at the first time
82 mutable union RSType *mSpecType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070083
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070084 protected:
Zonr Chang6b6320a2010-10-05 22:42:01 +080085 RSExportType(RSContext *Context,
86 ExportClass Class,
87 const llvm::StringRef &Name);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070088
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070089 // Let's make it private since there're some prerequisites to call this
90 // function.
91 //
Stephen Hinese5e64432010-12-02 18:48:20 -080092 // @T was normalized by calling RSExportType::NormalizeType().
zonr6315f762010-10-05 15:35:14 +080093 // @TypeName was retrieve from RSExportType::GetTypeName() before calling
94 // this.
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070095 //
96 static RSExportType *Create(RSContext *Context,
97 const clang::Type *T,
zonr6315f762010-10-05 15:35:14 +080098 const llvm::StringRef &TypeName);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070099
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700100 static llvm::StringRef GetTypeName(const clang::Type *T);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700101
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700102 // This function convert the RSExportType to LLVM type. Actually, it should be
103 // "convert Clang type to LLVM type." However, clang doesn't make this API
104 // (lib/CodeGen/CodeGenTypes.h) public, we need to do by ourselves.
105 //
106 // Once we can get LLVM type, we can use LLVM to get alignment information,
107 // allocation size of a given type and structure layout that LLVM used
108 // (all of these information are target dependent) without dealing with these
109 // by ourselves.
110 virtual const llvm::Type *convertToLLVMType() const = 0;
Stephen Hinese639eb52010-11-08 19:27:20 -0800111 // Record type may recursively reference its type definition. We need a
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800112 // temporary type setup before the type construction gets done.
113 inline void setAbstractLLVMType(const llvm::Type *LLVMType) const {
114 mLLVMType = LLVMType;
115 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700116
Zonr Changa7a828d2010-10-22 03:44:27 +0800117 virtual union RSType *convertToSpecType() const = 0;
118 inline void setSpecTypeTemporarily(union RSType *SpecType) const {
119 mSpecType = SpecType;
120 }
121
122 virtual ~RSExportType();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700123 public:
Stephen Hinese5e64432010-12-02 18:48:20 -0800124 // This function additionally verifies that the Type T is exportable.
125 // If it is not, this function returns false. Otherwise it returns true.
126 static bool NormalizeType(const clang::Type *&T,
127 llvm::StringRef &TypeName,
128 clang::Diagnostic *Diags,
Stephen Hinesdd6206b2010-12-09 19:39:22 -0800129 clang::SourceManager *SM,
130 const clang::VarDecl *VD);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700131 // @T may not be normalized
132 static RSExportType *Create(RSContext *Context, const clang::Type *T);
133 static RSExportType *CreateFromDecl(RSContext *Context,
134 const clang::VarDecl *VD);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700135
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700136 static const clang::Type *GetTypeOfDecl(const clang::DeclaratorDecl *DD);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700137
Zonr Chang6b6320a2010-10-05 22:42:01 +0800138 inline ExportClass getClass() const { return mClass; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700139
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700140 inline const llvm::Type *getLLVMType() const {
141 if (mLLVMType == NULL)
142 mLLVMType = convertToLLVMType();
143 return mLLVMType;
144 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700145
Zonr Changa7a828d2010-10-22 03:44:27 +0800146 inline const union RSType *getSpecType() const {
147 if (mSpecType == NULL)
148 mSpecType = convertToSpecType();
149 return mSpecType;
150 }
151
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700152 // Return the number of bits necessary to hold the specified RSExportType
153 static size_t GetTypeStoreSize(const RSExportType *ET);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700154
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700155 // The size of allocation of specified RSExportType (alignment considered)
156 static size_t GetTypeAllocSize(const RSExportType *ET);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700157
Zonr Chang641558f2010-10-12 21:07:06 +0800158 inline const std::string &getName() const { return mName; }
159
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800160 virtual bool keep();
Zonr Chang641558f2010-10-12 21:07:06 +0800161 virtual bool equals(const RSExportable *E) const;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700162}; // RSExportType
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700163
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700164// Primitive types
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700165class RSExportPrimitiveType : public RSExportType {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700166 friend class RSExportType;
167 friend class RSExportElement;
168 public:
169 // From graphics/java/android/renderscript/Element.java: Element.DataType
170 typedef enum {
Stephen Hinesfeaca062011-02-04 14:08:13 -0800171 DataTypeIsStruct = -2,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700172 DataTypeUnknown = -1,
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700173
Zonr Changb1771ef2010-10-22 18:03:46 +0800174#define ENUM_PRIMITIVE_DATA_TYPE_RANGE(begin_type, end_type) \
175 FirstPrimitiveType = DataType ## begin_type, \
176 LastPrimitiveType = DataType ## end_type,
177
178#define ENUM_RS_MATRIX_DATA_TYPE_RANGE(begin_type, end_type) \
179 FirstRSMatrixType = DataType ## begin_type, \
180 LastRSMatrixType = DataType ## end_type,
181
182#define ENUM_RS_OBJECT_DATA_TYPE_RANGE(begin_type, end_type) \
183 FirstRSObjectType = DataType ## begin_type, \
184 LastRSObjectType = DataType ## end_type,
185
Zonr Changa65ec162010-10-17 01:53:05 +0800186#define ENUM_RS_DATA_TYPE(type, cname, bits) \
187 DataType ## type,
Zonr Changb1771ef2010-10-22 18:03:46 +0800188
Zonr Changa65ec162010-10-17 01:53:05 +0800189#include "RSDataTypeEnums.inc"
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700190
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700191 DataTypeMax
192 } DataType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700193
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700194 // From graphics/java/android/renderscript/Element.java: Element.DataKind
195 typedef enum {
Zonr Changa65ec162010-10-17 01:53:05 +0800196 DataKindUnknown = -1
197#define ENUM_RS_DATA_KIND(kind) \
198 , DataKind ## kind
199#include "RSDataKindEnums.inc"
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700200 } DataKind;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700201
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700202 private:
Zonr Changa65ec162010-10-17 01:53:05 +0800203 // NOTE: There's no any instance of RSExportPrimitiveType which mType
204 // is of the value DataTypeRSMatrix*. DataTypeRSMatrix* enumeration here is
205 // only for RSExportPrimitiveType::GetRSObjectType to *recognize* the struct
206 // rs_matrix{2x2, 3x3, 4x4}. These matrix type are represented as
207 // RSExportMatrixType.
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700208 DataType mType;
209 DataKind mKind;
210 bool mNormalized;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700211
Zonr Changb1771ef2010-10-22 18:03:46 +0800212 typedef llvm::StringMap<DataType> RSSpecificTypeMapTy;
213 static llvm::ManagedStatic<RSSpecificTypeMapTy> RSSpecificTypeMap;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700214
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700215 static llvm::Type *RSObjectLLVMType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700216
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700217 static const size_t SizeOfDataTypeInBits[];
Stephen Hinese5e64432010-12-02 18:48:20 -0800218 // @T was normalized by calling RSExportType::NormalizeType() before calling
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700219 // this.
220 // @TypeName was retrieved from RSExportType::GetTypeName() before calling
221 // this
222 static RSExportPrimitiveType *Create(RSContext *Context,
223 const clang::Type *T,
224 const llvm::StringRef &TypeName,
225 DataKind DK = DataKindUser,
226 bool Normalized = false);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700227
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700228 protected:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700229 RSExportPrimitiveType(RSContext *Context,
Zonr Chang6b6320a2010-10-05 22:42:01 +0800230 // for derived class to set their type class
231 ExportClass Class,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700232 const llvm::StringRef &Name,
233 DataType DT,
234 DataKind DK,
zonr6315f762010-10-05 15:35:14 +0800235 bool Normalized)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800236 : RSExportType(Context, Class, Name),
zonr6315f762010-10-05 15:35:14 +0800237 mType(DT),
238 mKind(DK),
239 mNormalized(Normalized) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700240 return;
241 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700242
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700243 virtual const llvm::Type *convertToLLVMType() const;
Zonr Changa7a828d2010-10-22 03:44:27 +0800244 virtual union RSType *convertToSpecType() const;
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800245
246 static DataType GetDataType(RSContext *Context, const clang::Type *T);
247
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700248 public:
Stephen Hinesdd6206b2010-12-09 19:39:22 -0800249 // T is normalized by calling RSExportType::NormalizeType() before
250 // calling this
251 static bool IsPrimitiveType(const clang::Type *T);
252
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700253 // @T may not be normalized
254 static RSExportPrimitiveType *Create(RSContext *Context,
255 const clang::Type *T,
256 DataKind DK = DataKindUser);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700257
Zonr Changb1771ef2010-10-22 18:03:46 +0800258 static DataType GetRSSpecificType(const llvm::StringRef &TypeName);
259 static DataType GetRSSpecificType(const clang::Type *T);
260
261 static bool IsRSMatrixType(DataType DT);
262 static bool IsRSObjectType(DataType DT);
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800263 static bool IsRSObjectType(const clang::Type *T) {
264 return IsRSObjectType(GetRSSpecificType(T));
265 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700266
Stephen Hinesfeaca062011-02-04 14:08:13 -0800267 // Determines whether T is [an array of] struct that contains at least one
268 // RS object type within it.
269 static bool IsStructureTypeWithRSObject(const clang::Type *T);
270
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700271 static size_t GetSizeInBits(const RSExportPrimitiveType *EPT);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700272
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700273 inline DataType getType() const { return mType; }
274 inline DataKind getKind() const { return mKind; }
275 inline bool isRSObjectType() const {
Stephen Hinesb3a12fe2011-01-26 20:16:38 -0800276 return ((mType >= FirstRSObjectType) && (mType <= LastRSObjectType));
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700277 }
Zonr Chang641558f2010-10-12 21:07:06 +0800278
279 virtual bool equals(const RSExportable *E) const;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700280}; // RSExportPrimitiveType
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700281
282
283class RSExportPointerType : public RSExportType {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700284 friend class RSExportType;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700285 friend class RSExportFunc;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700286 private:
287 const RSExportType *mPointeeType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700288
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700289 RSExportPointerType(RSContext *Context,
290 const llvm::StringRef &Name,
zonr6315f762010-10-05 15:35:14 +0800291 const RSExportType *PointeeType)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800292 : RSExportType(Context, ExportClassPointer, Name),
zonr6315f762010-10-05 15:35:14 +0800293 mPointeeType(PointeeType) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700294 return;
295 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700296
Stephen Hinese5e64432010-12-02 18:48:20 -0800297 // @PT was normalized by calling RSExportType::NormalizeType() before calling
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700298 // this.
299 static RSExportPointerType *Create(RSContext *Context,
300 const clang::PointerType *PT,
301 const llvm::StringRef &TypeName);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700302
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700303 virtual const llvm::Type *convertToLLVMType() const;
Zonr Changa7a828d2010-10-22 03:44:27 +0800304 virtual union RSType *convertToSpecType() const;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700305 public:
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800306 virtual bool keep();
Zonr Chang641558f2010-10-12 21:07:06 +0800307
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700308 inline const RSExportType *getPointeeType() const { return mPointeeType; }
Zonr Chang641558f2010-10-12 21:07:06 +0800309
310 virtual bool equals(const RSExportable *E) const;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700311}; // RSExportPointerType
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700312
313
314class RSExportVectorType : public RSExportPrimitiveType {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700315 friend class RSExportType;
316 friend class RSExportElement;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700317 private:
Zonr Chang92b344a2010-10-05 20:39:03 +0800318 unsigned mNumElement; // number of element
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700319
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700320 RSExportVectorType(RSContext *Context,
321 const llvm::StringRef &Name,
322 DataType DT,
323 DataKind DK,
324 bool Normalized,
Zonr Chang92b344a2010-10-05 20:39:03 +0800325 unsigned NumElement)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800326 : RSExportPrimitiveType(Context, ExportClassVector, Name,
327 DT, DK, Normalized),
zonr6315f762010-10-05 15:35:14 +0800328 mNumElement(NumElement) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700329 return;
330 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700331
Stephen Hinese5e64432010-12-02 18:48:20 -0800332 // @EVT was normalized by calling RSExportType::NormalizeType() before
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700333 // calling this.
334 static RSExportVectorType *Create(RSContext *Context,
335 const clang::ExtVectorType *EVT,
336 const llvm::StringRef &TypeName,
337 DataKind DK = DataKindUser,
338 bool Normalized = false);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700339
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700340 virtual const llvm::Type *convertToLLVMType() const;
Zonr Changa7a828d2010-10-22 03:44:27 +0800341 virtual union RSType *convertToSpecType() const;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700342 public:
343 static llvm::StringRef GetTypeName(const clang::ExtVectorType *EVT);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700344
Zonr Chang92b344a2010-10-05 20:39:03 +0800345 inline unsigned getNumElement() const { return mNumElement; }
Zonr Chang641558f2010-10-12 21:07:06 +0800346
347 virtual bool equals(const RSExportable *E) const;
Zonr Chang92b344a2010-10-05 20:39:03 +0800348};
349
350// Only *square* *float* matrix is supported by now.
351//
352// struct rs_matrix{2x2,3x3,4x4, ..., NxN} should be defined as the following
353// form *exactly*:
354// typedef struct {
355// float m[{NxN}];
356// } rs_matrixNxN;
357//
358// where mDim will be N.
359class RSExportMatrixType : public RSExportType {
360 friend class RSExportType;
361 private:
Zonr Chang2e1dba62010-10-05 22:20:11 +0800362 unsigned mDim; // dimension
Zonr Chang92b344a2010-10-05 20:39:03 +0800363
364 RSExportMatrixType(RSContext *Context,
365 const llvm::StringRef &Name,
366 unsigned Dim)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800367 : RSExportType(Context, ExportClassMatrix, Name),
Zonr Chang92b344a2010-10-05 20:39:03 +0800368 mDim(Dim) {
369 return;
370 }
371
372 virtual const llvm::Type *convertToLLVMType() const;
Zonr Changa7a828d2010-10-22 03:44:27 +0800373 virtual union RSType *convertToSpecType() const;
Zonr Chang92b344a2010-10-05 20:39:03 +0800374 public:
Stephen Hinese5e64432010-12-02 18:48:20 -0800375 // @RT was normalized by calling RSExportType::NormalizeType() before
Zonr Chang92b344a2010-10-05 20:39:03 +0800376 // calling this.
377 static RSExportMatrixType *Create(RSContext *Context,
378 const clang::RecordType *RT,
379 const llvm::StringRef &TypeName,
380 unsigned Dim);
381
382 inline unsigned getDim() const { return mDim; }
Zonr Chang641558f2010-10-12 21:07:06 +0800383
384 virtual bool equals(const RSExportable *E) const;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700385};
386
Zonr Chang2e1dba62010-10-05 22:20:11 +0800387class RSExportConstantArrayType : public RSExportType {
388 friend class RSExportType;
389 private:
390 const RSExportType *mElementType; // Array element type
391 unsigned mSize; // Array size
392
393 RSExportConstantArrayType(RSContext *Context,
394 const RSExportType *ElementType,
395 unsigned Size)
396 : RSExportType(Context,
Zonr Chang6b6320a2010-10-05 22:42:01 +0800397 ExportClassConstantArray,
Zonr Chang2e1dba62010-10-05 22:20:11 +0800398 DUMMY_TYPE_NAME_FOR_RS_CONSTANT_ARRAY_TYPE),
399 mElementType(ElementType),
400 mSize(Size) {
401 return;
402 }
403
Stephen Hinese5e64432010-12-02 18:48:20 -0800404 // @CAT was normalized by calling RSExportType::NormalizeType() before
Zonr Chang2e1dba62010-10-05 22:20:11 +0800405 // calling this.
406 static RSExportConstantArrayType *Create(RSContext *Context,
407 const clang::ConstantArrayType *CAT);
408
409 virtual const llvm::Type *convertToLLVMType() const;
Zonr Changa7a828d2010-10-22 03:44:27 +0800410 virtual union RSType *convertToSpecType() const;
Zonr Chang2e1dba62010-10-05 22:20:11 +0800411 public:
Zonr Chang2e1dba62010-10-05 22:20:11 +0800412 inline unsigned getSize() const { return mSize; }
413 inline const RSExportType *getElementType() const { return mElementType; }
Zonr Chang641558f2010-10-12 21:07:06 +0800414
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800415 virtual bool keep();
Zonr Chang641558f2010-10-12 21:07:06 +0800416 virtual bool equals(const RSExportable *E) const;
Zonr Chang2e1dba62010-10-05 22:20:11 +0800417};
418
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700419class RSExportRecordType : public RSExportType {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700420 friend class RSExportType;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700421 public:
422 class Field {
423 private:
424 const RSExportType *mType;
425 // Field name
426 std::string mName;
427 // Link to the struct that contain this field
428 const RSExportRecordType *mParent;
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800429 // Offset in the container
430 size_t mOffset;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700431
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700432 public:
433 Field(const RSExportType *T,
434 const llvm::StringRef &Name,
435 const RSExportRecordType *Parent,
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800436 size_t Offset)
zonr6315f762010-10-05 15:35:14 +0800437 : mType(T),
438 mName(Name.data(), Name.size()),
439 mParent(Parent),
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800440 mOffset(Offset) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700441 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700442 }
443
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700444 inline const RSExportRecordType *getParent() const { return mParent; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700445 inline const RSExportType *getType() const { return mType; }
446 inline const std::string &getName() const { return mName; }
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800447 inline size_t getOffsetInParent() const { return mOffset; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700448 };
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700449
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700450 typedef std::list<const Field*>::const_iterator const_field_iterator;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700451
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700452 inline const_field_iterator fields_begin() const {
453 return this->mFields.begin();
454 }
455 inline const_field_iterator fields_end() const {
456 return this->mFields.end();
457 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700458
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700459 private:
460 std::list<const Field*> mFields;
461 bool mIsPacked;
462 // Artificial export struct type is not exported by user (and thus it won't
463 // get reflected)
464 bool mIsArtificial;
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800465 size_t mAllocSize;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700466
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700467 RSExportRecordType(RSContext *Context,
468 const llvm::StringRef &Name,
469 bool IsPacked,
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800470 bool IsArtificial,
471 size_t AllocSize)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800472 : RSExportType(Context, ExportClassRecord, Name),
zonr6315f762010-10-05 15:35:14 +0800473 mIsPacked(IsPacked),
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800474 mIsArtificial(IsArtificial),
475 mAllocSize(AllocSize) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700476 return;
477 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700478
Stephen Hinese5e64432010-12-02 18:48:20 -0800479 // @RT was normalized by calling RSExportType::NormalizeType() before calling
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700480 // this.
481 // @TypeName was retrieved from RSExportType::GetTypeName() before calling
482 // this.
483 static RSExportRecordType *Create(RSContext *Context,
484 const clang::RecordType *RT,
485 const llvm::StringRef &TypeName,
486 bool mIsArtificial = false);
487
488 virtual const llvm::Type *convertToLLVMType() const;
Zonr Changa7a828d2010-10-22 03:44:27 +0800489 virtual union RSType *convertToSpecType() const;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700490 public:
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800491 inline const std::list<const Field*>& getFields() const { return mFields; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700492 inline bool isPacked() const { return mIsPacked; }
493 inline bool isArtificial() const { return mIsArtificial; }
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800494 inline size_t getAllocSize() const { return mAllocSize; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700495
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800496 virtual bool keep();
Zonr Chang641558f2010-10-12 21:07:06 +0800497 virtual bool equals(const RSExportable *E) const;
498
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700499 ~RSExportRecordType() {
500 for (std::list<const Field*>::iterator I = mFields.begin(),
501 E = mFields.end();
502 I != E;
503 I++)
504 if (*I != NULL)
505 delete *I;
506 return;
507 }
508}; // RSExportRecordType
509
510} // namespace slang
511
Stephen Hinese639eb52010-11-08 19:27:20 -0800512#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_TYPE_H_ NOLINT