blob: ab911358fb02083a00c7b3e72b0404b13289d100 [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 {
171 DataTypeUnknown = -1,
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700172
Zonr Changb1771ef2010-10-22 18:03:46 +0800173#define ENUM_PRIMITIVE_DATA_TYPE_RANGE(begin_type, end_type) \
174 FirstPrimitiveType = DataType ## begin_type, \
175 LastPrimitiveType = DataType ## end_type,
176
177#define ENUM_RS_MATRIX_DATA_TYPE_RANGE(begin_type, end_type) \
178 FirstRSMatrixType = DataType ## begin_type, \
179 LastRSMatrixType = DataType ## end_type,
180
181#define ENUM_RS_OBJECT_DATA_TYPE_RANGE(begin_type, end_type) \
182 FirstRSObjectType = DataType ## begin_type, \
183 LastRSObjectType = DataType ## end_type,
184
Zonr Changa65ec162010-10-17 01:53:05 +0800185#define ENUM_RS_DATA_TYPE(type, cname, bits) \
186 DataType ## type,
Zonr Changb1771ef2010-10-22 18:03:46 +0800187
Zonr Changa65ec162010-10-17 01:53:05 +0800188#include "RSDataTypeEnums.inc"
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700189
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700190 DataTypeMax
191 } DataType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700192
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700193 // From graphics/java/android/renderscript/Element.java: Element.DataKind
194 typedef enum {
Zonr Changa65ec162010-10-17 01:53:05 +0800195 DataKindUnknown = -1
196#define ENUM_RS_DATA_KIND(kind) \
197 , DataKind ## kind
198#include "RSDataKindEnums.inc"
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700199 } DataKind;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700200
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700201 private:
Zonr Changa65ec162010-10-17 01:53:05 +0800202 // NOTE: There's no any instance of RSExportPrimitiveType which mType
203 // is of the value DataTypeRSMatrix*. DataTypeRSMatrix* enumeration here is
204 // only for RSExportPrimitiveType::GetRSObjectType to *recognize* the struct
205 // rs_matrix{2x2, 3x3, 4x4}. These matrix type are represented as
206 // RSExportMatrixType.
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700207 DataType mType;
208 DataKind mKind;
209 bool mNormalized;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700210
Zonr Changb1771ef2010-10-22 18:03:46 +0800211 typedef llvm::StringMap<DataType> RSSpecificTypeMapTy;
212 static llvm::ManagedStatic<RSSpecificTypeMapTy> RSSpecificTypeMap;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700213
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700214 static llvm::Type *RSObjectLLVMType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700215
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700216 static const size_t SizeOfDataTypeInBits[];
Stephen Hinese5e64432010-12-02 18:48:20 -0800217 // @T was normalized by calling RSExportType::NormalizeType() before calling
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700218 // this.
219 // @TypeName was retrieved from RSExportType::GetTypeName() before calling
220 // this
221 static RSExportPrimitiveType *Create(RSContext *Context,
222 const clang::Type *T,
223 const llvm::StringRef &TypeName,
224 DataKind DK = DataKindUser,
225 bool Normalized = false);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700226
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700227 protected:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700228 RSExportPrimitiveType(RSContext *Context,
Zonr Chang6b6320a2010-10-05 22:42:01 +0800229 // for derived class to set their type class
230 ExportClass Class,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700231 const llvm::StringRef &Name,
232 DataType DT,
233 DataKind DK,
zonr6315f762010-10-05 15:35:14 +0800234 bool Normalized)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800235 : RSExportType(Context, Class, Name),
zonr6315f762010-10-05 15:35:14 +0800236 mType(DT),
237 mKind(DK),
238 mNormalized(Normalized) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700239 return;
240 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700241
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700242 virtual const llvm::Type *convertToLLVMType() const;
Zonr Changa7a828d2010-10-22 03:44:27 +0800243 virtual union RSType *convertToSpecType() const;
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800244
245 static DataType GetDataType(RSContext *Context, const clang::Type *T);
246
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700247 public:
Stephen Hinesdd6206b2010-12-09 19:39:22 -0800248 // T is normalized by calling RSExportType::NormalizeType() before
249 // calling this
250 static bool IsPrimitiveType(const clang::Type *T);
251
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700252 // @T may not be normalized
253 static RSExportPrimitiveType *Create(RSContext *Context,
254 const clang::Type *T,
255 DataKind DK = DataKindUser);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700256
Zonr Changb1771ef2010-10-22 18:03:46 +0800257 static DataType GetRSSpecificType(const llvm::StringRef &TypeName);
258 static DataType GetRSSpecificType(const clang::Type *T);
259
260 static bool IsRSMatrixType(DataType DT);
261 static bool IsRSObjectType(DataType DT);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700262
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700263 static size_t GetSizeInBits(const RSExportPrimitiveType *EPT);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700264
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700265 inline DataType getType() const { return mType; }
266 inline DataKind getKind() const { return mKind; }
267 inline bool isRSObjectType() const {
268 return ((mType >= DataTypeRSElement) && (mType < DataTypeMax));
269 }
Zonr Chang641558f2010-10-12 21:07:06 +0800270
271 virtual bool equals(const RSExportable *E) const;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700272}; // RSExportPrimitiveType
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700273
274
275class RSExportPointerType : public RSExportType {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700276 friend class RSExportType;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700277 friend class RSExportFunc;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700278 private:
279 const RSExportType *mPointeeType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700280
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700281 RSExportPointerType(RSContext *Context,
282 const llvm::StringRef &Name,
zonr6315f762010-10-05 15:35:14 +0800283 const RSExportType *PointeeType)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800284 : RSExportType(Context, ExportClassPointer, Name),
zonr6315f762010-10-05 15:35:14 +0800285 mPointeeType(PointeeType) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700286 return;
287 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700288
Stephen Hinese5e64432010-12-02 18:48:20 -0800289 // @PT was normalized by calling RSExportType::NormalizeType() before calling
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700290 // this.
291 static RSExportPointerType *Create(RSContext *Context,
292 const clang::PointerType *PT,
293 const llvm::StringRef &TypeName);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700294
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700295 virtual const llvm::Type *convertToLLVMType() const;
Zonr Changa7a828d2010-10-22 03:44:27 +0800296 virtual union RSType *convertToSpecType() const;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700297 public:
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800298 virtual bool keep();
Zonr Chang641558f2010-10-12 21:07:06 +0800299
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700300 inline const RSExportType *getPointeeType() const { return mPointeeType; }
Zonr Chang641558f2010-10-12 21:07:06 +0800301
302 virtual bool equals(const RSExportable *E) const;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700303}; // RSExportPointerType
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700304
305
306class RSExportVectorType : public RSExportPrimitiveType {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700307 friend class RSExportType;
308 friend class RSExportElement;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700309 private:
Zonr Chang92b344a2010-10-05 20:39:03 +0800310 unsigned mNumElement; // number of element
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700311
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700312 RSExportVectorType(RSContext *Context,
313 const llvm::StringRef &Name,
314 DataType DT,
315 DataKind DK,
316 bool Normalized,
Zonr Chang92b344a2010-10-05 20:39:03 +0800317 unsigned NumElement)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800318 : RSExportPrimitiveType(Context, ExportClassVector, Name,
319 DT, DK, Normalized),
zonr6315f762010-10-05 15:35:14 +0800320 mNumElement(NumElement) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700321 return;
322 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700323
Stephen Hinese5e64432010-12-02 18:48:20 -0800324 // @EVT was normalized by calling RSExportType::NormalizeType() before
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700325 // calling this.
326 static RSExportVectorType *Create(RSContext *Context,
327 const clang::ExtVectorType *EVT,
328 const llvm::StringRef &TypeName,
329 DataKind DK = DataKindUser,
330 bool Normalized = false);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700331
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700332 virtual const llvm::Type *convertToLLVMType() const;
Zonr Changa7a828d2010-10-22 03:44:27 +0800333 virtual union RSType *convertToSpecType() const;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700334 public:
335 static llvm::StringRef GetTypeName(const clang::ExtVectorType *EVT);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700336
Zonr Chang92b344a2010-10-05 20:39:03 +0800337 inline unsigned getNumElement() const { return mNumElement; }
Zonr Chang641558f2010-10-12 21:07:06 +0800338
339 virtual bool equals(const RSExportable *E) const;
Zonr Chang92b344a2010-10-05 20:39:03 +0800340};
341
342// Only *square* *float* matrix is supported by now.
343//
344// struct rs_matrix{2x2,3x3,4x4, ..., NxN} should be defined as the following
345// form *exactly*:
346// typedef struct {
347// float m[{NxN}];
348// } rs_matrixNxN;
349//
350// where mDim will be N.
351class RSExportMatrixType : public RSExportType {
352 friend class RSExportType;
353 private:
Zonr Chang2e1dba62010-10-05 22:20:11 +0800354 unsigned mDim; // dimension
Zonr Chang92b344a2010-10-05 20:39:03 +0800355
356 RSExportMatrixType(RSContext *Context,
357 const llvm::StringRef &Name,
358 unsigned Dim)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800359 : RSExportType(Context, ExportClassMatrix, Name),
Zonr Chang92b344a2010-10-05 20:39:03 +0800360 mDim(Dim) {
361 return;
362 }
363
364 virtual const llvm::Type *convertToLLVMType() const;
Zonr Changa7a828d2010-10-22 03:44:27 +0800365 virtual union RSType *convertToSpecType() const;
Zonr Chang92b344a2010-10-05 20:39:03 +0800366 public:
Stephen Hinese5e64432010-12-02 18:48:20 -0800367 // @RT was normalized by calling RSExportType::NormalizeType() before
Zonr Chang92b344a2010-10-05 20:39:03 +0800368 // calling this.
369 static RSExportMatrixType *Create(RSContext *Context,
370 const clang::RecordType *RT,
371 const llvm::StringRef &TypeName,
372 unsigned Dim);
373
374 inline unsigned getDim() const { return mDim; }
Zonr Chang641558f2010-10-12 21:07:06 +0800375
376 virtual bool equals(const RSExportable *E) const;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700377};
378
Zonr Chang2e1dba62010-10-05 22:20:11 +0800379class RSExportConstantArrayType : public RSExportType {
380 friend class RSExportType;
381 private:
382 const RSExportType *mElementType; // Array element type
383 unsigned mSize; // Array size
384
385 RSExportConstantArrayType(RSContext *Context,
386 const RSExportType *ElementType,
387 unsigned Size)
388 : RSExportType(Context,
Zonr Chang6b6320a2010-10-05 22:42:01 +0800389 ExportClassConstantArray,
Zonr Chang2e1dba62010-10-05 22:20:11 +0800390 DUMMY_TYPE_NAME_FOR_RS_CONSTANT_ARRAY_TYPE),
391 mElementType(ElementType),
392 mSize(Size) {
393 return;
394 }
395
Stephen Hinese5e64432010-12-02 18:48:20 -0800396 // @CAT was normalized by calling RSExportType::NormalizeType() before
Zonr Chang2e1dba62010-10-05 22:20:11 +0800397 // calling this.
398 static RSExportConstantArrayType *Create(RSContext *Context,
399 const clang::ConstantArrayType *CAT);
400
401 virtual const llvm::Type *convertToLLVMType() const;
Zonr Changa7a828d2010-10-22 03:44:27 +0800402 virtual union RSType *convertToSpecType() const;
Zonr Chang2e1dba62010-10-05 22:20:11 +0800403 public:
Zonr Chang2e1dba62010-10-05 22:20:11 +0800404 inline unsigned getSize() const { return mSize; }
405 inline const RSExportType *getElementType() const { return mElementType; }
Zonr Chang641558f2010-10-12 21:07:06 +0800406
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800407 virtual bool keep();
Zonr Chang641558f2010-10-12 21:07:06 +0800408 virtual bool equals(const RSExportable *E) const;
Zonr Chang2e1dba62010-10-05 22:20:11 +0800409};
410
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700411class RSExportRecordType : public RSExportType {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700412 friend class RSExportType;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700413 public:
414 class Field {
415 private:
416 const RSExportType *mType;
417 // Field name
418 std::string mName;
419 // Link to the struct that contain this field
420 const RSExportRecordType *mParent;
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800421 // Offset in the container
422 size_t mOffset;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700423
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700424 public:
425 Field(const RSExportType *T,
426 const llvm::StringRef &Name,
427 const RSExportRecordType *Parent,
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800428 size_t Offset)
zonr6315f762010-10-05 15:35:14 +0800429 : mType(T),
430 mName(Name.data(), Name.size()),
431 mParent(Parent),
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800432 mOffset(Offset) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700433 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700434 }
435
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700436 inline const RSExportRecordType *getParent() const { return mParent; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700437 inline const RSExportType *getType() const { return mType; }
438 inline const std::string &getName() const { return mName; }
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800439 inline size_t getOffsetInParent() const { return mOffset; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700440 };
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700441
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700442 typedef std::list<const Field*>::const_iterator const_field_iterator;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700443
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700444 inline const_field_iterator fields_begin() const {
445 return this->mFields.begin();
446 }
447 inline const_field_iterator fields_end() const {
448 return this->mFields.end();
449 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700450
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700451 private:
452 std::list<const Field*> mFields;
453 bool mIsPacked;
454 // Artificial export struct type is not exported by user (and thus it won't
455 // get reflected)
456 bool mIsArtificial;
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800457 size_t mAllocSize;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700458
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700459 RSExportRecordType(RSContext *Context,
460 const llvm::StringRef &Name,
461 bool IsPacked,
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800462 bool IsArtificial,
463 size_t AllocSize)
Zonr Chang6b6320a2010-10-05 22:42:01 +0800464 : RSExportType(Context, ExportClassRecord, Name),
zonr6315f762010-10-05 15:35:14 +0800465 mIsPacked(IsPacked),
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800466 mIsArtificial(IsArtificial),
467 mAllocSize(AllocSize) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700468 return;
469 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700470
Stephen Hinese5e64432010-12-02 18:48:20 -0800471 // @RT was normalized by calling RSExportType::NormalizeType() before calling
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700472 // this.
473 // @TypeName was retrieved from RSExportType::GetTypeName() before calling
474 // this.
475 static RSExportRecordType *Create(RSContext *Context,
476 const clang::RecordType *RT,
477 const llvm::StringRef &TypeName,
478 bool mIsArtificial = false);
479
480 virtual const llvm::Type *convertToLLVMType() const;
Zonr Changa7a828d2010-10-22 03:44:27 +0800481 virtual union RSType *convertToSpecType() const;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700482 public:
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800483 inline const std::list<const Field*>& getFields() const { return mFields; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700484 inline bool isPacked() const { return mIsPacked; }
485 inline bool isArtificial() const { return mIsArtificial; }
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800486 inline size_t getAllocSize() const { return mAllocSize; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700487
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800488 virtual bool keep();
Zonr Chang641558f2010-10-12 21:07:06 +0800489 virtual bool equals(const RSExportable *E) const;
490
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700491 ~RSExportRecordType() {
492 for (std::list<const Field*>::iterator I = mFields.begin(),
493 E = mFields.end();
494 I != E;
495 I++)
496 if (*I != NULL)
497 delete *I;
498 return;
499 }
500}; // RSExportRecordType
501
502} // namespace slang
503
Stephen Hinese639eb52010-11-08 19:27:20 -0800504#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_TYPE_H_ NOLINT