blob: 395f07aba4b4f54f797e4e2e1efa2da95d44150d [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
zonr6315f762010-10-05 15:35:14 +080017#include "slang_rs_export_type.h"
18
Stephen Hinese639eb52010-11-08 19:27:20 -080019#include <list>
zonr6315f762010-10-05 15:35:14 +080020#include <vector>
Shih-wei Liao462aefd2010-06-04 15:32:04 -070021
Shih-wei Liao43730fe2012-08-02 23:06:18 -070022#include "clang/AST/ASTContext.h"
Stephen Hines23c43582013-01-09 20:02:04 -080023#include "clang/AST/Attr.h"
Stephen Hinese639eb52010-11-08 19:27:20 -080024#include "clang/AST/RecordLayout.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070025
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070026#include "llvm/ADT/StringExtras.h"
Stephen Hines23c43582013-01-09 20:02:04 -080027#include "llvm/IR/DataLayout.h"
28#include "llvm/IR/DerivedTypes.h"
29#include "llvm/IR/Type.h"
Shih-wei Liao0a3f20e2010-08-10 13:09:49 -070030
Stephen Hines6e6578a2011-02-07 18:05:48 -080031#include "slang_assert.h"
zonr6315f762010-10-05 15:35:14 +080032#include "slang_rs_context.h"
33#include "slang_rs_export_element.h"
Stephen Hinesd5a84f62012-04-04 17:44:38 -070034#include "slang_version.h"
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070035
Zonr Chang641558f2010-10-12 21:07:06 +080036#define CHECK_PARENT_EQUALITY(ParentClass, E) \
37 if (!ParentClass::equals(E)) \
38 return false;
39
Stephen Hinese639eb52010-11-08 19:27:20 -080040namespace slang {
Shih-wei Liao462aefd2010-06-04 15:32:04 -070041
Stephen Hinese5e64432010-12-02 18:48:20 -080042namespace {
43
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -070044/* For the data types we support, their category, names, and size (in bits).
45 *
46 * IMPORTANT: The data types in this table should be at the same index
47 * as specified by the corresponding DataType enum.
48 */
Stephen Hinesfdd1ba12012-03-08 17:26:32 -080049static RSReflectionType gReflectionTypes[] = {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -070050 {PrimitiveDataType, "FLOAT_16", "F16", 16, "half", "half", "Half", "Half", false},
51 {PrimitiveDataType, "FLOAT_32", "F32", 32, "float", "float", "Float", "Float", false},
52 {PrimitiveDataType, "FLOAT_64", "F64", 64, "double", "double", "Double", "Double",false},
53 {PrimitiveDataType, "SIGNED_8", "I8", 8, "int8_t", "byte", "Byte", "Byte", false},
54 {PrimitiveDataType, "SIGNED_16", "I16", 16, "int16_t", "short", "Short", "Short", false},
55 {PrimitiveDataType, "SIGNED_32", "I32", 32, "int32_t", "int", "Int", "Int", false},
56 {PrimitiveDataType, "SIGNED_64", "I64", 64, "int64_t", "long", "Long", "Long", false},
57 {PrimitiveDataType, "UNSIGNED_8", "U8", 8, "uint8_t", "short", "UByte", "Short", true},
58 {PrimitiveDataType, "UNSIGNED_16", "U16", 16, "uint16_t", "int", "UShort", "Int", true},
59 {PrimitiveDataType, "UNSIGNED_32", "U32", 32, "uint32_t", "long", "UInt", "Long", true},
60 {PrimitiveDataType, "UNSIGNED_64", "U64", 64, "uint64_t", "long", "ULong", "Long", false},
Stephen Hinesfdd1ba12012-03-08 17:26:32 -080061
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -070062 {PrimitiveDataType, "BOOLEAN", "BOOLEAN", 8, "bool", "boolean", NULL, NULL, false},
Stephen Hinesfdd1ba12012-03-08 17:26:32 -080063
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -070064 {PrimitiveDataType, "UNSIGNED_5_6_5", NULL, 16, NULL, NULL, NULL, NULL, false},
65 {PrimitiveDataType, "UNSIGNED_5_5_5_1", NULL, 16, NULL, NULL, NULL, NULL, false},
66 {PrimitiveDataType, "UNSIGNED_4_4_4_4", NULL, 16, NULL, NULL, NULL, NULL, false},
Stephen Hinesfdd1ba12012-03-08 17:26:32 -080067
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -070068 {MatrixDataType, "MATRIX_2X2", NULL, 4*32, "rsMatrix_2x2", "Matrix2f", NULL, NULL, false},
69 {MatrixDataType, "MATRIX_3X3", NULL, 9*32, "rsMatrix_3x3", "Matrix3f", NULL, NULL, false},
70 {MatrixDataType, "MATRIX_4X4", NULL, 16*32, "rsMatrix_4x4", "Matrix4f", NULL, NULL, false},
Stephen Hinesfdd1ba12012-03-08 17:26:32 -080071
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -070072 // TODO: For 64 bit, what will be the size of the objects??
73 {ObjectDataType, "RS_ELEMENT", "ELEMENT", 32, "Element", "Element", NULL, NULL, false},
74 {ObjectDataType, "RS_TYPE", "TYPE", 32, "Type", "Type", NULL, NULL, false},
75 {ObjectDataType, "RS_ALLOCATION", "ALLOCATION", 32, "Allocation", "Allocation", NULL, NULL, false},
76 {ObjectDataType, "RS_SAMPLER", "SAMPLER", 32, "Sampler", "Sampler", NULL, NULL, false},
77 {ObjectDataType, "RS_SCRIPT", "SCRIPT", 32, "Script", "Script", NULL, NULL, false},
78 {ObjectDataType, "RS_MESH", "MESH", 32, "Mesh", "Mesh", NULL, NULL, false},
79 {ObjectDataType, "RS_PATH", "PATH", 32, "Path", "Path", NULL, NULL, false},
80
81 {ObjectDataType, "RS_PROGRAM_FRAGMENT", "PROGRAM_FRAGMENT", 32, "ProgramFragment", "ProgramFragment", NULL, NULL, false},
82 {ObjectDataType, "RS_PROGRAM_VERTEX", "PROGRAM_VERTEX", 32, "ProgramVertex", "ProgramVertex", NULL, NULL, false},
83 {ObjectDataType, "RS_PROGRAM_RASTER", "PROGRAM_RASTER", 32, "ProgramRaster", "ProgramRaster", NULL, NULL, false},
84 {ObjectDataType, "RS_PROGRAM_STORE", "PROGRAM_STORE", 32, "ProgramStore", "ProgramStore", NULL, NULL, false},
85 {ObjectDataType, "RS_FONT", "FONT", 32, "Font", "Font", NULL, NULL, false}
Stephen Hinesfdd1ba12012-03-08 17:26:32 -080086};
87
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -070088const int kMaxVectorSize = 4;
89
90struct BuiltinInfo {
91 clang::BuiltinType::Kind builtinTypeKind;
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -070092 DataType type;
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -070093 /* TODO If we return std::string instead of llvm::StringRef, we could build
94 * the name instead of duplicating the entries.
95 */
96 const char *cname[kMaxVectorSize];
97};
98
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -070099
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700100BuiltinInfo BuiltinInfoTable[] = {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700101 {clang::BuiltinType::Bool, DataTypeBoolean,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700102 {"bool", "bool2", "bool3", "bool4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700103 {clang::BuiltinType::Char_U, DataTypeUnsigned8,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700104 {"uchar", "uchar2", "uchar3", "uchar4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700105 {clang::BuiltinType::UChar, DataTypeUnsigned8,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700106 {"uchar", "uchar2", "uchar3", "uchar4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700107 {clang::BuiltinType::Char16, DataTypeSigned16,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700108 {"short", "short2", "short3", "short4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700109 {clang::BuiltinType::Char32, DataTypeSigned32,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700110 {"int", "int2", "int3", "int4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700111 {clang::BuiltinType::UShort, DataTypeUnsigned16,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700112 {"ushort", "ushort2", "ushort3", "ushort4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700113 {clang::BuiltinType::UInt, DataTypeUnsigned32,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700114 {"uint", "uint2", "uint3", "uint4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700115 {clang::BuiltinType::ULong, DataTypeUnsigned32,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700116 {"uint", "uint2", "uint3", "uint4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700117 {clang::BuiltinType::ULongLong, DataTypeUnsigned64,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700118 {"ulong", "ulong2", "ulong3", "ulong4"}},
119
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700120 {clang::BuiltinType::Char_S, DataTypeSigned8,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700121 {"char", "char2", "char3", "char4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700122 {clang::BuiltinType::SChar, DataTypeSigned8,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700123 {"char", "char2", "char3", "char4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700124 {clang::BuiltinType::Short, DataTypeSigned16,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700125 {"short", "short2", "short3", "short4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700126 {clang::BuiltinType::Int, DataTypeSigned32,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700127 {"int", "int2", "int3", "int4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700128 {clang::BuiltinType::Long, DataTypeSigned64,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700129 {"long", "long2", "long3", "long4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700130 {clang::BuiltinType::LongLong, DataTypeSigned64,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700131 {"long", "long2", "long3", "long4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700132 {clang::BuiltinType::Float, DataTypeFloat32,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700133 {"float", "float2", "float3", "float4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700134 {clang::BuiltinType::Double, DataTypeFloat64,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700135 {"double", "double2", "double3", "double4"}},
136};
137const int BuiltinInfoTableCount = sizeof(BuiltinInfoTable) / sizeof(BuiltinInfoTable[0]);
138
139struct NameAndPrimitiveType {
140 const char *name;
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700141 DataType dataType;
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700142};
143
144static NameAndPrimitiveType MatrixAndObjectDataTypes[] = {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700145 {"rs_matrix2x2", DataTypeRSMatrix2x2},
146 {"rs_matrix3x3", DataTypeRSMatrix3x3},
147 {"rs_matrix4x4", DataTypeRSMatrix4x4},
148 {"rs_element", DataTypeRSElement},
149 {"rs_type", DataTypeRSType},
150 {"rs_allocation", DataTypeRSAllocation},
151 {"rs_sampler", DataTypeRSSampler},
152 {"rs_script", DataTypeRSScript},
153 {"rs_mesh", DataTypeRSMesh},
154 {"rs_path", DataTypeRSPath},
155 {"rs_program_fragment", DataTypeRSProgramFragment},
156 {"rs_program_vertex", DataTypeRSProgramVertex},
157 {"rs_program_raster", DataTypeRSProgramRaster},
158 {"rs_program_store", DataTypeRSProgramStore},
159 {"rs_font", DataTypeRSFont},
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700160};
161
162const int MatrixAndObjectDataTypesCount =
163 sizeof(MatrixAndObjectDataTypes) / sizeof(MatrixAndObjectDataTypes[0]);
164
Stephen Hines24e79f62010-12-13 12:55:57 -0800165static const clang::Type *TypeExportableHelper(
166 const clang::Type *T,
167 llvm::SmallPtrSet<const clang::Type*, 8>& SPS,
Stephen Hines48d893d2013-12-06 18:00:27 -0800168 slang::RSContext *Context,
Stephen Hines24e79f62010-12-13 12:55:57 -0800169 const clang::VarDecl *VD,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700170 const clang::RecordDecl *TopLevelRecord);
Stephen Hines24e79f62010-12-13 12:55:57 -0800171
Tim Murrayee4016d2014-04-10 15:49:08 -0700172template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800173static void ReportTypeError(slang::RSContext *Context,
Stephen Hines11274a72012-09-26 19:14:20 -0700174 const clang::NamedDecl *ND,
Stephen Hines24e79f62010-12-13 12:55:57 -0800175 const clang::RecordDecl *TopLevelRecord,
Tim Murrayee4016d2014-04-10 15:49:08 -0700176 const char (&Message)[N],
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700177 unsigned int TargetAPI = 0) {
Stephen Hines24e79f62010-12-13 12:55:57 -0800178 // Attempt to use the type declaration first (if we have one).
179 // Fall back to the variable definition, if we are looking at something
180 // like an array declaration that can't be exported.
181 if (TopLevelRecord) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800182 Context->ReportError(TopLevelRecord->getLocation(), Message)
183 << TopLevelRecord->getName() << TargetAPI;
Stephen Hines11274a72012-09-26 19:14:20 -0700184 } else if (ND) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800185 Context->ReportError(ND->getLocation(), Message) << ND->getName()
186 << TargetAPI;
Stephen Hines24e79f62010-12-13 12:55:57 -0800187 } else {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800188 slangAssert(false && "Variables should be validated before exporting");
Stephen Hines24e79f62010-12-13 12:55:57 -0800189 }
Stephen Hines24e79f62010-12-13 12:55:57 -0800190}
191
192static const clang::Type *ConstantArrayTypeExportableHelper(
193 const clang::ConstantArrayType *CAT,
194 llvm::SmallPtrSet<const clang::Type*, 8>& SPS,
Stephen Hines48d893d2013-12-06 18:00:27 -0800195 slang::RSContext *Context,
Stephen Hines24e79f62010-12-13 12:55:57 -0800196 const clang::VarDecl *VD,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700197 const clang::RecordDecl *TopLevelRecord) {
Stephen Hines24e79f62010-12-13 12:55:57 -0800198 // Check element type
199 const clang::Type *ElementType = GET_CONSTANT_ARRAY_ELEMENT_TYPE(CAT);
200 if (ElementType->isArrayType()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800201 ReportTypeError(Context, VD, TopLevelRecord,
Logan Chien9207a2e2011-10-21 15:39:28 +0800202 "multidimensional arrays cannot be exported: '%0'");
Stephen Hines24e79f62010-12-13 12:55:57 -0800203 return NULL;
204 } else if (ElementType->isExtVectorType()) {
205 const clang::ExtVectorType *EVT =
206 static_cast<const clang::ExtVectorType*>(ElementType);
207 unsigned numElements = EVT->getNumElements();
208
209 const clang::Type *BaseElementType = GET_EXT_VECTOR_ELEMENT_TYPE(EVT);
210 if (!RSExportPrimitiveType::IsPrimitiveType(BaseElementType)) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800211 ReportTypeError(Context, VD, TopLevelRecord,
Logan Chien9207a2e2011-10-21 15:39:28 +0800212 "vectors of non-primitive types cannot be exported: '%0'");
Stephen Hines24e79f62010-12-13 12:55:57 -0800213 return NULL;
214 }
215
216 if (numElements == 3 && CAT->getSize() != 1) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800217 ReportTypeError(Context, VD, TopLevelRecord,
Logan Chien9207a2e2011-10-21 15:39:28 +0800218 "arrays of width 3 vector types cannot be exported: '%0'");
Stephen Hines24e79f62010-12-13 12:55:57 -0800219 return NULL;
220 }
221 }
222
Stephen Hines48d893d2013-12-06 18:00:27 -0800223 if (TypeExportableHelper(ElementType, SPS, Context, VD,
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700224 TopLevelRecord) == NULL) {
Stephen Hines24e79f62010-12-13 12:55:57 -0800225 return NULL;
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700226 } else {
Stephen Hines24e79f62010-12-13 12:55:57 -0800227 return CAT;
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700228 }
Stephen Hines24e79f62010-12-13 12:55:57 -0800229}
230
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700231BuiltinInfo *FindBuiltinType(clang::BuiltinType::Kind builtinTypeKind) {
232 for (int i = 0; i < BuiltinInfoTableCount; i++) {
233 if (builtinTypeKind == BuiltinInfoTable[i].builtinTypeKind) {
234 return &BuiltinInfoTable[i];
235 }
236 }
237 return NULL;
238}
239
Stephen Hines24e79f62010-12-13 12:55:57 -0800240static const clang::Type *TypeExportableHelper(
Stephen Hinese67239d2012-02-24 15:08:36 -0800241 clang::Type const *T,
242 llvm::SmallPtrSet<clang::Type const *, 8> &SPS,
Stephen Hines48d893d2013-12-06 18:00:27 -0800243 slang::RSContext *Context,
Stephen Hinese67239d2012-02-24 15:08:36 -0800244 clang::VarDecl const *VD,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700245 clang::RecordDecl const *TopLevelRecord) {
Stephen Hinese5e64432010-12-02 18:48:20 -0800246 // Normalize first
247 if ((T = GET_CANONICAL_TYPE(T)) == NULL)
248 return NULL;
249
250 if (SPS.count(T))
251 return T;
252
253 switch (T->getTypeClass()) {
254 case clang::Type::Builtin: {
Loganbe274822011-02-16 22:02:54 +0800255 const clang::BuiltinType *BT =
256 UNSAFE_CAST_TYPE(const clang::BuiltinType, T);
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700257 return FindBuiltinType(BT->getKind()) == NULL ? NULL : T;
Stephen Hinese5e64432010-12-02 18:48:20 -0800258 }
259 case clang::Type::Record: {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700260 if (RSExportPrimitiveType::GetRSSpecificType(T) != DataTypeUnknown) {
Stephen Hinese5e64432010-12-02 18:48:20 -0800261 return T; // RS object type, no further checks are needed
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700262 }
Stephen Hinese5e64432010-12-02 18:48:20 -0800263
264 // Check internal struct
Stephen Hinescd440ee2010-12-06 14:57:04 -0800265 if (T->isUnionType()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800266 ReportTypeError(Context, VD, T->getAsUnionType()->getDecl(),
Logan Chien9207a2e2011-10-21 15:39:28 +0800267 "unions cannot be exported: '%0'");
Stephen Hinescd440ee2010-12-06 14:57:04 -0800268 return NULL;
269 } else if (!T->isStructureType()) {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800270 slangAssert(false && "Unknown type cannot be exported");
Stephen Hinescd440ee2010-12-06 14:57:04 -0800271 return NULL;
272 }
273
Stephen Hines24e79f62010-12-13 12:55:57 -0800274 clang::RecordDecl *RD = T->getAsStructureType()->getDecl();
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800275 if (RD != NULL) {
Stephen Hinese5e64432010-12-02 18:48:20 -0800276 RD = RD->getDefinition();
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800277 if (RD == NULL) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800278 ReportTypeError(Context, NULL, T->getAsStructureType()->getDecl(),
Logan Chien9207a2e2011-10-21 15:39:28 +0800279 "struct is not defined in this module");
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800280 return NULL;
281 }
282 }
Stephen Hinese5e64432010-12-02 18:48:20 -0800283
284 if (!TopLevelRecord) {
285 TopLevelRecord = RD;
286 }
287 if (RD->getName().empty()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800288 ReportTypeError(Context, NULL, RD,
Logan Chien9207a2e2011-10-21 15:39:28 +0800289 "anonymous structures cannot be exported");
Stephen Hinese5e64432010-12-02 18:48:20 -0800290 return NULL;
291 }
292
293 // Fast check
294 if (RD->hasFlexibleArrayMember() || RD->hasObjectMember())
295 return NULL;
296
297 // Insert myself into checking set
298 SPS.insert(T);
299
300 // Check all element
301 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
302 FE = RD->field_end();
303 FI != FE;
304 FI++) {
305 const clang::FieldDecl *FD = *FI;
306 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
307 FT = GET_CANONICAL_TYPE(FT);
308
Stephen Hines48d893d2013-12-06 18:00:27 -0800309 if (!TypeExportableHelper(FT, SPS, Context, VD, TopLevelRecord)) {
Stephen Hinese5e64432010-12-02 18:48:20 -0800310 return NULL;
311 }
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800312
313 // We don't support bit fields yet
314 //
315 // TODO(zonr/srhines): allow bit fields of size 8, 16, 32
316 if (FD->isBitField()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800317 Context->ReportError(
318 FD->getLocation(),
319 "bit fields are not able to be exported: '%0.%1'")
320 << RD->getName() << FD->getName();
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800321 return NULL;
322 }
Stephen Hinese5e64432010-12-02 18:48:20 -0800323 }
324
325 return T;
326 }
327 case clang::Type::Pointer: {
328 if (TopLevelRecord) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800329 ReportTypeError(Context, VD, TopLevelRecord,
Stephen Hinesaa82e742012-04-11 12:38:17 -0700330 "structures containing pointers cannot be exported: '%0'");
Stephen Hinese5e64432010-12-02 18:48:20 -0800331 return NULL;
332 }
Stephen Hines24e79f62010-12-13 12:55:57 -0800333
Loganbe274822011-02-16 22:02:54 +0800334 const clang::PointerType *PT =
335 UNSAFE_CAST_TYPE(const clang::PointerType, T);
Stephen Hinese5e64432010-12-02 18:48:20 -0800336 const clang::Type *PointeeType = GET_POINTEE_TYPE(PT);
337
Stephen Hinesaa82e742012-04-11 12:38:17 -0700338 if (PointeeType->getTypeClass() == clang::Type::Pointer) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800339 ReportTypeError(Context, VD, TopLevelRecord,
Stephen Hinesaa82e742012-04-11 12:38:17 -0700340 "multiple levels of pointers cannot be exported: '%0'");
341 return NULL;
342 }
Stephen Hinese5e64432010-12-02 18:48:20 -0800343 // We don't support pointer with array-type pointee or unsupported pointee
344 // type
345 if (PointeeType->isArrayType() ||
Stephen Hines48d893d2013-12-06 18:00:27 -0800346 (TypeExportableHelper(PointeeType, SPS, Context, VD,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700347 TopLevelRecord) == NULL))
Stephen Hinese5e64432010-12-02 18:48:20 -0800348 return NULL;
349 else
350 return T;
351 }
352 case clang::Type::ExtVector: {
353 const clang::ExtVectorType *EVT =
Loganbe274822011-02-16 22:02:54 +0800354 UNSAFE_CAST_TYPE(const clang::ExtVectorType, T);
Stephen Hinese5e64432010-12-02 18:48:20 -0800355 // Only vector with size 2, 3 and 4 are supported.
356 if (EVT->getNumElements() < 2 || EVT->getNumElements() > 4)
357 return NULL;
358
359 // Check base element type
360 const clang::Type *ElementType = GET_EXT_VECTOR_ELEMENT_TYPE(EVT);
361
362 if ((ElementType->getTypeClass() != clang::Type::Builtin) ||
Stephen Hines48d893d2013-12-06 18:00:27 -0800363 (TypeExportableHelper(ElementType, SPS, Context, VD,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700364 TopLevelRecord) == NULL))
Stephen Hinese5e64432010-12-02 18:48:20 -0800365 return NULL;
366 else
367 return T;
368 }
369 case clang::Type::ConstantArray: {
370 const clang::ConstantArrayType *CAT =
Loganbe274822011-02-16 22:02:54 +0800371 UNSAFE_CAST_TYPE(const clang::ConstantArrayType, T);
Stephen Hinese5e64432010-12-02 18:48:20 -0800372
Stephen Hines48d893d2013-12-06 18:00:27 -0800373 return ConstantArrayTypeExportableHelper(CAT, SPS, Context, VD,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700374 TopLevelRecord);
Stephen Hinese5e64432010-12-02 18:48:20 -0800375 }
Stephen Hines48d893d2013-12-06 18:00:27 -0800376 case clang::Type::Enum: {
377 // FIXME: We currently convert enums to integers, rather than reflecting
378 // a more complete (and nicer type-safe Java version).
379 return Context->getASTContext().IntTy.getTypePtr();
380 }
Stephen Hinese5e64432010-12-02 18:48:20 -0800381 default: {
Stephen Hines48d893d2013-12-06 18:00:27 -0800382 slangAssert(false && "Unknown type cannot be validated");
Stephen Hinese5e64432010-12-02 18:48:20 -0800383 return NULL;
384 }
385 }
386}
387
388// Return the type that can be used to create RSExportType, will always return
389// the canonical type
Logan Chien9207a2e2011-10-21 15:39:28 +0800390// If the Type T is not exportable, this function returns NULL. DiagEngine is
Stephen Hines78e69cb2011-04-22 15:03:19 -0700391// used to generate proper Clang diagnostic messages when a
Stephen Hinese5e64432010-12-02 18:48:20 -0800392// non-exportable type is detected. TopLevelRecord is used to capture the
393// highest struct (in the case of a nested hierarchy) for detecting other
394// types that cannot be exported (mostly pointers within a struct).
395static const clang::Type *TypeExportable(const clang::Type *T,
Stephen Hines48d893d2013-12-06 18:00:27 -0800396 slang::RSContext *Context,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700397 const clang::VarDecl *VD) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700398 llvm::SmallPtrSet<const clang::Type*, 8> SPS =
399 llvm::SmallPtrSet<const clang::Type*, 8>();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700400
Stephen Hines48d893d2013-12-06 18:00:27 -0800401 return TypeExportableHelper(T, SPS, Context, VD, NULL);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700402}
403
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800404static bool ValidateRSObjectInVarDecl(slang::RSContext *Context,
405 clang::VarDecl *VD, bool InCompositeType,
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700406 unsigned int TargetAPI) {
407 if (TargetAPI < SLANG_JB_TARGET_API) {
408 // Only if we are already in a composite type (like an array or structure).
409 if (InCompositeType) {
410 // Only if we are actually exported (i.e. non-static).
Stephen Hines44f10062013-06-13 00:33:23 -0700411 if (VD->hasLinkage() &&
412 (VD->getFormalLinkage() == clang::ExternalLinkage)) {
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700413 // Only if we are not a pointer to an object.
414 const clang::Type *T = GET_CANONICAL_TYPE(VD->getType().getTypePtr());
415 if (T->getTypeClass() != clang::Type::Pointer) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800416 ReportTypeError(Context, VD, NULL,
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700417 "arrays/structures containing RS object types "
418 "cannot be exported in target API < %1: '%0'",
419 SLANG_JB_TARGET_API);
420 return false;
421 }
422 }
423 }
424 }
425
426 return true;
427}
428
Stephen Hines11274a72012-09-26 19:14:20 -0700429// Helper function for ValidateType(). We do a recursive descent on the
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700430// type hierarchy to ensure that we can properly export/handle the
431// declaration.
432// \return true if the variable declaration is valid,
433// false if it is invalid (along with proper diagnostics).
434//
Stephen Hines11274a72012-09-26 19:14:20 -0700435// C - ASTContext (for diagnostics + builtin types).
436// T - sub-type that we are validating.
437// ND - (optional) top-level named declaration that we are validating.
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700438// SPS - set of types we have already seen/validated.
439// InCompositeType - true if we are within an outer composite type.
440// UnionDecl - set if we are in a sub-type of a union.
441// TargetAPI - target SDK API level.
Stephen Hines11274a72012-09-26 19:14:20 -0700442// IsFilterscript - whether or not we are compiling for Filterscript
443static bool ValidateTypeHelper(
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800444 slang::RSContext *Context,
Stephen Hines11274a72012-09-26 19:14:20 -0700445 clang::ASTContext &C,
Stephen Hines78e69cb2011-04-22 15:03:19 -0700446 const clang::Type *&T,
Stephen Hines11274a72012-09-26 19:14:20 -0700447 clang::NamedDecl *ND,
448 clang::SourceLocation Loc,
Stephen Hines78e69cb2011-04-22 15:03:19 -0700449 llvm::SmallPtrSet<const clang::Type*, 8>& SPS,
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700450 bool InCompositeType,
451 clang::RecordDecl *UnionDecl,
Stephen Hines11274a72012-09-26 19:14:20 -0700452 unsigned int TargetAPI,
453 bool IsFilterscript) {
Stephen Hines78e69cb2011-04-22 15:03:19 -0700454 if ((T = GET_CANONICAL_TYPE(T)) == NULL)
455 return true;
456
457 if (SPS.count(T))
458 return true;
459
460 switch (T->getTypeClass()) {
461 case clang::Type::Record: {
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700462 if (RSExportPrimitiveType::IsRSObjectType(T)) {
Stephen Hines11274a72012-09-26 19:14:20 -0700463 clang::VarDecl *VD = (ND ? llvm::dyn_cast<clang::VarDecl>(ND) : NULL);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800464 if (VD && !ValidateRSObjectInVarDecl(Context, VD, InCompositeType,
465 TargetAPI)) {
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700466 return false;
467 }
468 }
469
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700470 if (RSExportPrimitiveType::GetRSSpecificType(T) != DataTypeUnknown) {
Stephen Hines78e69cb2011-04-22 15:03:19 -0700471 if (!UnionDecl) {
472 return true;
473 } else if (RSExportPrimitiveType::IsRSObjectType(T)) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800474 ReportTypeError(Context, NULL, UnionDecl,
Stephen Hines78e69cb2011-04-22 15:03:19 -0700475 "unions containing RS object types are not allowed");
476 return false;
477 }
478 }
479
480 clang::RecordDecl *RD = NULL;
481
482 // Check internal struct
483 if (T->isUnionType()) {
484 RD = T->getAsUnionType()->getDecl();
485 UnionDecl = RD;
486 } else if (T->isStructureType()) {
487 RD = T->getAsStructureType()->getDecl();
488 } else {
489 slangAssert(false && "Unknown type cannot be exported");
490 return false;
491 }
492
493 if (RD != NULL) {
494 RD = RD->getDefinition();
495 if (RD == NULL) {
496 // FIXME
497 return true;
498 }
499 }
500
501 // Fast check
502 if (RD->hasFlexibleArrayMember() || RD->hasObjectMember())
503 return false;
504
505 // Insert myself into checking set
506 SPS.insert(T);
507
508 // Check all elements
509 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
510 FE = RD->field_end();
511 FI != FE;
512 FI++) {
513 const clang::FieldDecl *FD = *FI;
514 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
515 FT = GET_CANONICAL_TYPE(FT);
516
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800517 if (!ValidateTypeHelper(Context, C, FT, ND, Loc, SPS, true, UnionDecl,
Stephen Hines11274a72012-09-26 19:14:20 -0700518 TargetAPI, IsFilterscript)) {
Stephen Hines78e69cb2011-04-22 15:03:19 -0700519 return false;
520 }
521 }
522
523 return true;
524 }
525
526 case clang::Type::Builtin: {
Stephen Hines11274a72012-09-26 19:14:20 -0700527 if (IsFilterscript) {
528 clang::QualType QT = T->getCanonicalTypeInternal();
529 if (QT == C.DoubleTy ||
530 QT == C.LongDoubleTy ||
531 QT == C.LongTy ||
532 QT == C.LongLongTy) {
Stephen Hines11274a72012-09-26 19:14:20 -0700533 if (ND) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800534 Context->ReportError(
535 Loc,
Stephen Hines11274a72012-09-26 19:14:20 -0700536 "Builtin types > 32 bits in size are forbidden in "
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800537 "Filterscript: '%0'")
538 << ND->getName();
Stephen Hines11274a72012-09-26 19:14:20 -0700539 } else {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800540 Context->ReportError(
541 Loc,
Stephen Hines11274a72012-09-26 19:14:20 -0700542 "Builtin types > 32 bits in size are forbidden in "
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800543 "Filterscript");
Stephen Hines11274a72012-09-26 19:14:20 -0700544 }
545 return false;
546 }
547 }
Stephen Hines78e69cb2011-04-22 15:03:19 -0700548 break;
549 }
550
551 case clang::Type::Pointer: {
Stephen Hines11274a72012-09-26 19:14:20 -0700552 if (IsFilterscript) {
553 if (ND) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800554 Context->ReportError(Loc,
555 "Pointers are forbidden in Filterscript: '%0'")
556 << ND->getName();
Stephen Hines11274a72012-09-26 19:14:20 -0700557 return false;
558 } else {
559 // TODO(srhines): Find a better way to handle expressions (i.e. no
560 // NamedDecl) involving pointers in FS that should be allowed.
561 // An example would be calls to library functions like
562 // rsMatrixMultiply() that take rs_matrixNxN * types.
563 }
564 }
565
Stephen Hines78e69cb2011-04-22 15:03:19 -0700566 const clang::PointerType *PT =
567 UNSAFE_CAST_TYPE(const clang::PointerType, T);
568 const clang::Type *PointeeType = GET_POINTEE_TYPE(PT);
569
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800570 return ValidateTypeHelper(Context, C, PointeeType, ND, Loc, SPS,
571 InCompositeType, UnionDecl, TargetAPI,
572 IsFilterscript);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700573 }
574
575 case clang::Type::ExtVector: {
576 const clang::ExtVectorType *EVT =
577 UNSAFE_CAST_TYPE(const clang::ExtVectorType, T);
578 const clang::Type *ElementType = GET_EXT_VECTOR_ELEMENT_TYPE(EVT);
Stephen Hinesb13b85e2012-08-31 19:27:48 -0700579 if (TargetAPI < SLANG_ICS_TARGET_API &&
580 InCompositeType &&
Stephen Hinesfdae63e2012-11-28 14:42:44 -0800581 EVT->getNumElements() == 3 &&
582 ND &&
Stephen Hines44f10062013-06-13 00:33:23 -0700583 ND->getFormalLinkage() == clang::ExternalLinkage) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800584 ReportTypeError(Context, ND, NULL,
Stephen Hinesb13b85e2012-08-31 19:27:48 -0700585 "structs containing vectors of dimension 3 cannot "
586 "be exported at this API level: '%0'");
587 return false;
588 }
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800589 return ValidateTypeHelper(Context, C, ElementType, ND, Loc, SPS, true,
590 UnionDecl, TargetAPI, IsFilterscript);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700591 }
592
593 case clang::Type::ConstantArray: {
594 const clang::ConstantArrayType *CAT =
595 UNSAFE_CAST_TYPE(const clang::ConstantArrayType, T);
596 const clang::Type *ElementType = GET_CONSTANT_ARRAY_ELEMENT_TYPE(CAT);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800597 return ValidateTypeHelper(Context, C, ElementType, ND, Loc, SPS, true,
598 UnionDecl, TargetAPI, IsFilterscript);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700599 }
600
601 default: {
602 break;
603 }
604 }
605
606 return true;
Stephen Hinese5e64432010-12-02 18:48:20 -0800607}
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700608
Stephen Hinese5e64432010-12-02 18:48:20 -0800609} // namespace
610
Jean-Luc Brouilleteca05342014-05-15 12:44:21 -0700611std::string CreateDummyName(const char *type, const std::string &name) {
612 std::stringstream S;
613 S << "<" << type;
614 if (!name.empty()) {
615 S << ":" << name;
616 }
617 S << ">";
618 return S.str();
619}
620
Stephen Hinese5e64432010-12-02 18:48:20 -0800621/****************************** RSExportType ******************************/
622bool RSExportType::NormalizeType(const clang::Type *&T,
623 llvm::StringRef &TypeName,
Stephen Hines48d893d2013-12-06 18:00:27 -0800624 RSContext *Context,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700625 const clang::VarDecl *VD) {
Stephen Hines48d893d2013-12-06 18:00:27 -0800626 if ((T = TypeExportable(T, Context, VD)) == NULL) {
Stephen Hinese5e64432010-12-02 18:48:20 -0800627 return false;
628 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700629 // Get type name
630 TypeName = RSExportType::GetTypeName(T);
Stephen Hines48d893d2013-12-06 18:00:27 -0800631 if (Context && TypeName.empty()) {
Stephen Hines48d893d2013-12-06 18:00:27 -0800632 if (VD) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800633 Context->ReportError(VD->getLocation(),
634 "anonymous types cannot be exported");
Stephen Hines48d893d2013-12-06 18:00:27 -0800635 } else {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800636 Context->ReportError("anonymous types cannot be exported");
Stephen Hinese5e64432010-12-02 18:48:20 -0800637 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700638 return false;
Stephen Hinese5e64432010-12-02 18:48:20 -0800639 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700640
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700641 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700642}
643
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800644bool RSExportType::ValidateType(slang::RSContext *Context, clang::ASTContext &C,
645 clang::QualType QT, clang::NamedDecl *ND,
646 clang::SourceLocation Loc,
647 unsigned int TargetAPI, bool IsFilterscript) {
Stephen Hines11274a72012-09-26 19:14:20 -0700648 const clang::Type *T = QT.getTypePtr();
Stephen Hines78e69cb2011-04-22 15:03:19 -0700649 llvm::SmallPtrSet<const clang::Type*, 8> SPS =
650 llvm::SmallPtrSet<const clang::Type*, 8>();
651
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800652 return ValidateTypeHelper(Context, C, T, ND, Loc, SPS, false, NULL, TargetAPI,
Stephen Hines11274a72012-09-26 19:14:20 -0700653 IsFilterscript);
654 return true;
655}
656
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800657bool RSExportType::ValidateVarDecl(slang::RSContext *Context,
658 clang::VarDecl *VD, unsigned int TargetAPI,
Stephen Hines11274a72012-09-26 19:14:20 -0700659 bool IsFilterscript) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800660 return ValidateType(Context, VD->getASTContext(), VD->getType(), VD,
Stephen Hines11274a72012-09-26 19:14:20 -0700661 VD->getLocation(), TargetAPI, IsFilterscript);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700662}
663
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700664const clang::Type
665*RSExportType::GetTypeOfDecl(const clang::DeclaratorDecl *DD) {
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800666 if (DD) {
Stephen Hines8de19222013-08-23 16:30:59 -0700667 clang::QualType T = DD->getType();
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800668
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700669 if (T.isNull())
670 return NULL;
671 else
672 return T.getTypePtr();
673 }
674 return NULL;
675}
676
677llvm::StringRef RSExportType::GetTypeName(const clang::Type* T) {
678 T = GET_CANONICAL_TYPE(T);
679 if (T == NULL)
680 return llvm::StringRef();
681
682 switch (T->getTypeClass()) {
683 case clang::Type::Builtin: {
Loganbe274822011-02-16 22:02:54 +0800684 const clang::BuiltinType *BT =
685 UNSAFE_CAST_TYPE(const clang::BuiltinType, T);
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700686 BuiltinInfo *info = FindBuiltinType(BT->getKind());
687 if (info != NULL) {
688 return info->cname[0];
Zonr Changa65ec162010-10-17 01:53:05 +0800689 }
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700690 slangAssert(false && "Unknown data type of the builtin");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700691 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700692 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700693 case clang::Type::Record: {
Stephen Hinescd440ee2010-12-06 14:57:04 -0800694 clang::RecordDecl *RD;
695 if (T->isStructureType()) {
696 RD = T->getAsStructureType()->getDecl();
Stephen Hinesdd6206b2010-12-09 19:39:22 -0800697 } else {
Stephen Hinescd440ee2010-12-06 14:57:04 -0800698 break;
699 }
700
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700701 llvm::StringRef Name = RD->getName();
702 if (Name.empty()) {
Shih-wei Liao83f0c622011-06-21 05:34:53 -0700703 if (RD->getTypedefNameForAnonDecl() != NULL) {
704 Name = RD->getTypedefNameForAnonDecl()->getName();
705 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700706
Shih-wei Liao83f0c622011-06-21 05:34:53 -0700707 if (Name.empty()) {
708 // Try to find a name from redeclaration (i.e. typedef)
709 for (clang::TagDecl::redecl_iterator RI = RD->redecls_begin(),
710 RE = RD->redecls_end();
711 RI != RE;
712 RI++) {
713 slangAssert(*RI != NULL && "cannot be NULL object");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700714
Shih-wei Liao83f0c622011-06-21 05:34:53 -0700715 Name = (*RI)->getName();
716 if (!Name.empty())
717 break;
718 }
719 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700720 }
721 return Name;
722 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700723 case clang::Type::Pointer: {
724 // "*" plus pointee name
zonr6315f762010-10-05 15:35:14 +0800725 const clang::Type *PT = GET_POINTEE_TYPE(T);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700726 llvm::StringRef PointeeName;
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700727 if (NormalizeType(PT, PointeeName, NULL, NULL)) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700728 char *Name = new char[ 1 /* * */ + PointeeName.size() + 1 ];
729 Name[0] = '*';
730 memcpy(Name + 1, PointeeName.data(), PointeeName.size());
731 Name[PointeeName.size() + 1] = '\0';
732 return Name;
733 }
734 break;
735 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700736 case clang::Type::ExtVector: {
737 const clang::ExtVectorType *EVT =
Loganbe274822011-02-16 22:02:54 +0800738 UNSAFE_CAST_TYPE(const clang::ExtVectorType, T);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700739 return RSExportVectorType::GetTypeName(EVT);
740 break;
741 }
Zonr Chang2e1dba62010-10-05 22:20:11 +0800742 case clang::Type::ConstantArray : {
743 // Construct name for a constant array is too complicated.
Jean-Luc Brouilleteca05342014-05-15 12:44:21 -0700744 return CreateDummyName("ConstantArray", std::string());
Zonr Chang2e1dba62010-10-05 22:20:11 +0800745 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700746 default: {
747 break;
748 }
749 }
750
751 return llvm::StringRef();
752}
753
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700754
755RSExportType *RSExportType::Create(RSContext *Context,
756 const clang::Type *T,
757 const llvm::StringRef &TypeName) {
758 // Lookup the context to see whether the type was processed before.
759 // Newly created RSExportType will insert into context
760 // in RSExportType::RSExportType()
761 RSContext::export_type_iterator ETI = Context->findExportType(TypeName);
762
763 if (ETI != Context->export_types_end())
764 return ETI->second;
765
766 RSExportType *ET = NULL;
767 switch (T->getTypeClass()) {
768 case clang::Type::Record: {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700769 DataType dt = RSExportPrimitiveType::GetRSSpecificType(TypeName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700770 switch (dt) {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700771 case DataTypeUnknown: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800772 // User-defined types
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700773 ET = RSExportRecordType::Create(Context,
774 T->getAsStructureType(),
775 TypeName);
776 break;
777 }
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700778 case DataTypeRSMatrix2x2: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800779 // 2 x 2 Matrix type
780 ET = RSExportMatrixType::Create(Context,
781 T->getAsStructureType(),
782 TypeName,
783 2);
784 break;
785 }
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700786 case DataTypeRSMatrix3x3: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800787 // 3 x 3 Matrix type
788 ET = RSExportMatrixType::Create(Context,
789 T->getAsStructureType(),
790 TypeName,
791 3);
792 break;
793 }
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700794 case DataTypeRSMatrix4x4: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800795 // 4 x 4 Matrix type
796 ET = RSExportMatrixType::Create(Context,
797 T->getAsStructureType(),
798 TypeName,
799 4);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700800 break;
801 }
802 default: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800803 // Others are primitive types
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700804 ET = RSExportPrimitiveType::Create(Context, T, TypeName);
805 break;
806 }
807 }
808 break;
809 }
810 case clang::Type::Builtin: {
811 ET = RSExportPrimitiveType::Create(Context, T, TypeName);
812 break;
813 }
814 case clang::Type::Pointer: {
815 ET = RSExportPointerType::Create(Context,
Stephen Hines5baf6322011-04-25 17:21:15 -0700816 UNSAFE_CAST_TYPE(const clang::PointerType, T), TypeName);
Zonr Chang92b344a2010-10-05 20:39:03 +0800817 // FIXME: free the name (allocated in RSExportType::GetTypeName)
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700818 delete [] TypeName.data();
819 break;
820 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700821 case clang::Type::ExtVector: {
822 ET = RSExportVectorType::Create(Context,
Stephen Hines5baf6322011-04-25 17:21:15 -0700823 UNSAFE_CAST_TYPE(const clang::ExtVectorType, T), TypeName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700824 break;
825 }
Zonr Chang2e1dba62010-10-05 22:20:11 +0800826 case clang::Type::ConstantArray: {
827 ET = RSExportConstantArrayType::Create(
828 Context,
Loganbe274822011-02-16 22:02:54 +0800829 UNSAFE_CAST_TYPE(const clang::ConstantArrayType, T));
Zonr Chang2e1dba62010-10-05 22:20:11 +0800830 break;
831 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700832 default: {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800833 Context->ReportError("unknown type cannot be exported: '%0'")
834 << T->getTypeClassName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700835 break;
836 }
837 }
838
839 return ET;
840}
841
zonr6315f762010-10-05 15:35:14 +0800842RSExportType *RSExportType::Create(RSContext *Context, const clang::Type *T) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700843 llvm::StringRef TypeName;
Stephen Hines48d893d2013-12-06 18:00:27 -0800844 if (NormalizeType(T, TypeName, Context, NULL)) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700845 return Create(Context, T, TypeName);
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700846 } else {
Shih-wei Liaocecd11d2010-09-21 08:07:58 -0700847 return NULL;
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700848 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700849}
850
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700851RSExportType *RSExportType::CreateFromDecl(RSContext *Context,
852 const clang::VarDecl *VD) {
853 return RSExportType::Create(Context, GetTypeOfDecl(VD));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700854}
855
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700856size_t RSExportType::getStoreSize() const {
857 return getRSContext()->getDataLayout()->getTypeStoreSize(getLLVMType());
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700858}
859
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700860size_t RSExportType::getAllocSize() const {
861 return getRSContext()->getDataLayout()->getTypeAllocSize(getLLVMType());
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700862}
Shih-wei Liao1f0d88f2010-06-25 00:15:24 -0700863
Zonr Chang6b6320a2010-10-05 22:42:01 +0800864RSExportType::RSExportType(RSContext *Context,
865 ExportClass Class,
866 const llvm::StringRef &Name)
Zonr Changa41ce1d2010-10-06 02:23:12 +0800867 : RSExportable(Context, RSExportable::EX_TYPE),
Zonr Chang6b6320a2010-10-05 22:42:01 +0800868 mClass(Class),
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800869 // Make a copy on Name since memory stored @Name is either allocated in
870 // ASTContext or allocated in GetTypeName which will be destroyed later.
zonr6315f762010-10-05 15:35:14 +0800871 mName(Name.data(), Name.size()),
Jean-Luc Brouillet23544422014-05-14 15:12:25 -0700872 mLLVMType(NULL) {
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800873 // Don't cache the type whose name start with '<'. Those type failed to
874 // get their name since constructing their name in GetTypeName() requiring
875 // complicated work.
Jean-Luc Brouilleteca05342014-05-15 12:44:21 -0700876 if (!IsDummyName(Name))
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800877 // TODO(zonr): Need to check whether the insertion is successful or not.
878 Context->insertExportType(llvm::StringRef(Name), this);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700879 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700880}
881
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800882bool RSExportType::keep() {
883 if (!RSExportable::keep())
884 return false;
Zonr Chang641558f2010-10-12 21:07:06 +0800885 // Invalidate converted LLVM type.
886 mLLVMType = NULL;
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800887 return true;
Zonr Chang641558f2010-10-12 21:07:06 +0800888}
889
890bool RSExportType::equals(const RSExportable *E) const {
891 CHECK_PARENT_EQUALITY(RSExportable, E);
892 return (static_cast<const RSExportType*>(E)->getClass() == getClass());
893}
894
Zonr Changa7a828d2010-10-22 03:44:27 +0800895RSExportType::~RSExportType() {
Zonr Changa7a828d2010-10-22 03:44:27 +0800896}
897
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700898/************************** RSExportPrimitiveType **************************/
Zonr Changb1771ef2010-10-22 18:03:46 +0800899llvm::ManagedStatic<RSExportPrimitiveType::RSSpecificTypeMapTy>
900RSExportPrimitiveType::RSSpecificTypeMap;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700901
zonr6315f762010-10-05 15:35:14 +0800902llvm::Type *RSExportPrimitiveType::RSObjectLLVMType = NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700903
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700904bool RSExportPrimitiveType::IsPrimitiveType(const clang::Type *T) {
905 if ((T != NULL) && (T->getTypeClass() == clang::Type::Builtin))
906 return true;
907 else
908 return false;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700909}
910
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700911DataType
Zonr Changb1771ef2010-10-22 18:03:46 +0800912RSExportPrimitiveType::GetRSSpecificType(const llvm::StringRef &TypeName) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700913 if (TypeName.empty())
914 return DataTypeUnknown;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700915
Zonr Changb1771ef2010-10-22 18:03:46 +0800916 if (RSSpecificTypeMap->empty()) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700917 for (int i = 0; i < MatrixAndObjectDataTypesCount; i++) {
918 RSSpecificTypeMap->GetOrCreateValue(MatrixAndObjectDataTypes[i].name,
919 MatrixAndObjectDataTypes[i].dataType);
920 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700921 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700922
Zonr Changb1771ef2010-10-22 18:03:46 +0800923 RSSpecificTypeMapTy::const_iterator I = RSSpecificTypeMap->find(TypeName);
924 if (I == RSSpecificTypeMap->end())
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700925 return DataTypeUnknown;
926 else
927 return I->getValue();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700928}
929
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700930DataType RSExportPrimitiveType::GetRSSpecificType(const clang::Type *T) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700931 T = GET_CANONICAL_TYPE(T);
932 if ((T == NULL) || (T->getTypeClass() != clang::Type::Record))
933 return DataTypeUnknown;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700934
Zonr Changb1771ef2010-10-22 18:03:46 +0800935 return GetRSSpecificType( RSExportType::GetTypeName(T) );
936}
937
938bool RSExportPrimitiveType::IsRSMatrixType(DataType DT) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700939 if (DT < 0 || DT >= DataTypeMax) {
940 return false;
941 }
942 return gReflectionTypes[DT].category == MatrixDataType;
Zonr Changb1771ef2010-10-22 18:03:46 +0800943}
944
945bool RSExportPrimitiveType::IsRSObjectType(DataType DT) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700946 if (DT < 0 || DT >= DataTypeMax) {
947 return false;
948 }
949 return gReflectionTypes[DT].category == ObjectDataType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700950}
951
Stephen Hinesfeaca062011-02-04 14:08:13 -0800952bool RSExportPrimitiveType::IsStructureTypeWithRSObject(const clang::Type *T) {
953 bool RSObjectTypeSeen = false;
954 while (T && T->isArrayType()) {
955 T = T->getArrayElementTypeNoTypeQual();
956 }
957
958 const clang::RecordType *RT = T->getAsStructureType();
959 if (!RT) {
960 return false;
961 }
Stephen Hinesb0fabe52013-01-07 19:06:09 -0800962
Stephen Hinesfeaca062011-02-04 14:08:13 -0800963 const clang::RecordDecl *RD = RT->getDecl();
Stephen Hinesb0fabe52013-01-07 19:06:09 -0800964 if (RD) {
965 RD = RD->getDefinition();
966 }
967 if (!RD) {
968 return false;
969 }
970
Stephen Hinesfeaca062011-02-04 14:08:13 -0800971 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
972 FE = RD->field_end();
973 FI != FE;
974 FI++) {
975 // We just look through all field declarations to see if we find a
976 // declaration for an RS object type (or an array of one).
977 const clang::FieldDecl *FD = *FI;
978 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
979 while (FT && FT->isArrayType()) {
980 FT = FT->getArrayElementTypeNoTypeQual();
981 }
982
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700983 DataType DT = GetRSSpecificType(FT);
Stephen Hinesfeaca062011-02-04 14:08:13 -0800984 if (IsRSObjectType(DT)) {
985 // RS object types definitely need to be zero-initialized
986 RSObjectTypeSeen = true;
987 } else {
988 switch (DT) {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700989 case DataTypeRSMatrix2x2:
990 case DataTypeRSMatrix3x3:
991 case DataTypeRSMatrix4x4:
Stephen Hinesfeaca062011-02-04 14:08:13 -0800992 // Matrix types should get zero-initialized as well
993 RSObjectTypeSeen = true;
994 break;
995 default:
996 // Ignore all other primitive types
997 break;
998 }
999 while (FT && FT->isArrayType()) {
1000 FT = FT->getArrayElementTypeNoTypeQual();
1001 }
1002 if (FT->isStructureType()) {
1003 // Recursively handle structs of structs (even though these can't
1004 // be exported, it is possible for a user to have them internally).
1005 RSObjectTypeSeen |= IsStructureTypeWithRSObject(FT);
1006 }
1007 }
1008 }
1009
1010 return RSObjectTypeSeen;
1011}
1012
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001013size_t RSExportPrimitiveType::GetSizeInBits(const RSExportPrimitiveType *EPT) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001014 int type = EPT->getType();
1015 slangAssert((type > DataTypeUnknown && type < DataTypeMax) &&
Stephen Hines6e6578a2011-02-07 18:05:48 -08001016 "RSExportPrimitiveType::GetSizeInBits : unknown data type");
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001017 return gReflectionTypes[type].size_in_bits;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001018}
1019
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001020DataType
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001021RSExportPrimitiveType::GetDataType(RSContext *Context, const clang::Type *T) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001022 if (T == NULL)
1023 return DataTypeUnknown;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001024
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001025 switch (T->getTypeClass()) {
1026 case clang::Type::Builtin: {
Loganbe274822011-02-16 22:02:54 +08001027 const clang::BuiltinType *BT =
1028 UNSAFE_CAST_TYPE(const clang::BuiltinType, T);
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001029 BuiltinInfo *info = FindBuiltinType(BT->getKind());
1030 if (info != NULL) {
1031 return info->type;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001032 }
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001033 // The size of type WChar depend on platform so we abandon the support
1034 // to them.
1035 Context->ReportError("built-in type cannot be exported: '%0'")
1036 << T->getTypeClassName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001037 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001038 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001039 case clang::Type::Record: {
1040 // must be RS object type
Zonr Changb1771ef2010-10-22 18:03:46 +08001041 return RSExportPrimitiveType::GetRSSpecificType(T);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001042 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001043 default: {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001044 Context->ReportError("primitive type cannot be exported: '%0'")
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001045 << T->getTypeClassName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001046 break;
1047 }
1048 }
1049
1050 return DataTypeUnknown;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001051}
1052
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001053RSExportPrimitiveType
1054*RSExportPrimitiveType::Create(RSContext *Context,
1055 const clang::Type *T,
1056 const llvm::StringRef &TypeName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001057 bool Normalized) {
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001058 DataType DT = GetDataType(Context, T);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001059
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001060 if ((DT == DataTypeUnknown) || TypeName.empty())
1061 return NULL;
1062 else
Zonr Chang6b6320a2010-10-05 22:42:01 +08001063 return new RSExportPrimitiveType(Context, ExportClassPrimitive, TypeName,
Stephen Hines2b8fb642012-03-09 00:12:47 -08001064 DT, Normalized);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001065}
1066
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001067RSExportPrimitiveType *RSExportPrimitiveType::Create(RSContext *Context,
Stephen Hines2b8fb642012-03-09 00:12:47 -08001068 const clang::Type *T) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001069 llvm::StringRef TypeName;
Stephen Hines48d893d2013-12-06 18:00:27 -08001070 if (RSExportType::NormalizeType(T, TypeName, Context, NULL)
Stephen Hines78e69cb2011-04-22 15:03:19 -07001071 && IsPrimitiveType(T)) {
Stephen Hines2b8fb642012-03-09 00:12:47 -08001072 return Create(Context, T, TypeName);
Stephen Hinese5e64432010-12-02 18:48:20 -08001073 } else {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001074 return NULL;
Stephen Hinese5e64432010-12-02 18:48:20 -08001075 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001076}
1077
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001078llvm::Type *RSExportPrimitiveType::convertToLLVMType() const {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001079 llvm::LLVMContext &C = getRSContext()->getLLVMContext();
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001080
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001081 if (isRSObjectType()) {
1082 // struct {
1083 // int *p;
1084 // } __attribute__((packed, aligned(pointer_size)))
1085 //
1086 // which is
1087 //
1088 // <{ [1 x i32] }> in LLVM
1089 //
1090 if (RSObjectLLVMType == NULL) {
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001091 std::vector<llvm::Type *> Elements;
zonr6315f762010-10-05 15:35:14 +08001092 Elements.push_back(llvm::ArrayType::get(llvm::Type::getInt32Ty(C), 1));
Stephen Hinesa67e4452011-07-19 15:39:26 -07001093 RSObjectLLVMType = llvm::StructType::get(C, Elements, true);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001094 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001095 return RSObjectLLVMType;
1096 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001097
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001098 switch (mType) {
1099 case DataTypeFloat32: {
1100 return llvm::Type::getFloatTy(C);
1101 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001102 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001103 case DataTypeFloat64: {
1104 return llvm::Type::getDoubleTy(C);
1105 break;
1106 }
Shih-wei Liao91a37832010-10-03 19:11:51 -07001107 case DataTypeBoolean: {
1108 return llvm::Type::getInt1Ty(C);
1109 break;
1110 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001111 case DataTypeSigned8:
1112 case DataTypeUnsigned8: {
1113 return llvm::Type::getInt8Ty(C);
1114 break;
1115 }
1116 case DataTypeSigned16:
1117 case DataTypeUnsigned16:
1118 case DataTypeUnsigned565:
1119 case DataTypeUnsigned5551:
1120 case DataTypeUnsigned4444: {
1121 return llvm::Type::getInt16Ty(C);
1122 break;
1123 }
1124 case DataTypeSigned32:
1125 case DataTypeUnsigned32: {
1126 return llvm::Type::getInt32Ty(C);
1127 break;
1128 }
Stephen Hinesa5d2c232010-10-11 16:10:02 -07001129 case DataTypeSigned64:
1130 case DataTypeUnsigned64: {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001131 return llvm::Type::getInt64Ty(C);
1132 break;
1133 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001134 default: {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001135 slangAssert(false && "Unknown data type");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001136 }
1137 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001138
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001139 return NULL;
1140}
1141
Zonr Chang641558f2010-10-12 21:07:06 +08001142bool RSExportPrimitiveType::equals(const RSExportable *E) const {
1143 CHECK_PARENT_EQUALITY(RSExportType, E);
1144 return (static_cast<const RSExportPrimitiveType*>(E)->getType() == getType());
1145}
1146
Stephen Hinesfdd1ba12012-03-08 17:26:32 -08001147RSReflectionType *RSExportPrimitiveType::getRSReflectionType(DataType DT) {
1148 if (DT > DataTypeUnknown && DT < DataTypeMax) {
1149 return &gReflectionTypes[DT];
1150 } else {
1151 return NULL;
1152 }
1153}
1154
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001155/**************************** RSExportPointerType ****************************/
1156
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001157RSExportPointerType
1158*RSExportPointerType::Create(RSContext *Context,
1159 const clang::PointerType *PT,
1160 const llvm::StringRef &TypeName) {
1161 const clang::Type *PointeeType = GET_POINTEE_TYPE(PT);
1162 const RSExportType *PointeeET;
1163
1164 if (PointeeType->getTypeClass() != clang::Type::Pointer) {
1165 PointeeET = RSExportType::Create(Context, PointeeType);
1166 } else {
1167 // Double or higher dimension of pointer, export as int*
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001168 PointeeET = RSExportPrimitiveType::Create(Context,
1169 Context->getASTContext().IntTy.getTypePtr());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001170 }
1171
1172 if (PointeeET == NULL) {
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001173 // Error diagnostic is emitted for corresponding pointee type
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001174 return NULL;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001175 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001176
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001177 return new RSExportPointerType(Context, TypeName, PointeeET);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001178}
1179
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001180llvm::Type *RSExportPointerType::convertToLLVMType() const {
1181 llvm::Type *PointeeType = mPointeeType->getLLVMType();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001182 return llvm::PointerType::getUnqual(PointeeType);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001183}
1184
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001185bool RSExportPointerType::keep() {
1186 if (!RSExportType::keep())
1187 return false;
Zonr Chang641558f2010-10-12 21:07:06 +08001188 const_cast<RSExportType*>(mPointeeType)->keep();
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001189 return true;
Zonr Chang641558f2010-10-12 21:07:06 +08001190}
1191
1192bool RSExportPointerType::equals(const RSExportable *E) const {
1193 CHECK_PARENT_EQUALITY(RSExportType, E);
1194 return (static_cast<const RSExportPointerType*>(E)
1195 ->getPointeeType()->equals(getPointeeType()));
1196}
1197
zonr6315f762010-10-05 15:35:14 +08001198/***************************** RSExportVectorType *****************************/
zonr6315f762010-10-05 15:35:14 +08001199llvm::StringRef
1200RSExportVectorType::GetTypeName(const clang::ExtVectorType *EVT) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001201 const clang::Type *ElementType = GET_EXT_VECTOR_ELEMENT_TYPE(EVT);
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001202 llvm::StringRef name;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001203
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001204 if ((ElementType->getTypeClass() != clang::Type::Builtin))
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001205 return name;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001206
Loganbe274822011-02-16 22:02:54 +08001207 const clang::BuiltinType *BT = UNSAFE_CAST_TYPE(const clang::BuiltinType,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001208 ElementType);
Zonr Changa65ec162010-10-17 01:53:05 +08001209 if ((EVT->getNumElements() < 1) ||
1210 (EVT->getNumElements() > 4))
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001211 return name;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001212
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001213 BuiltinInfo *info = FindBuiltinType(BT->getKind());
1214 if (info != NULL) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001215 // Compiler is smart enough to optimize following *big if branches* since
1216 // they all become "constant comparison" after macro expansion
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001217 int I = EVT->getNumElements() - 1;
1218 if (I < kMaxVectorSize) {
1219 name = info->cname[I];
1220 } else {
1221 slangAssert(false && "Max vector is 4");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001222 }
1223 }
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001224 return name;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001225}
1226
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001227RSExportVectorType *RSExportVectorType::Create(RSContext *Context,
1228 const clang::ExtVectorType *EVT,
1229 const llvm::StringRef &TypeName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001230 bool Normalized) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001231 slangAssert(EVT != NULL && EVT->getTypeClass() == clang::Type::ExtVector);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001232
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001233 const clang::Type *ElementType = GET_EXT_VECTOR_ELEMENT_TYPE(EVT);
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001234 DataType DT = RSExportPrimitiveType::GetDataType(Context, ElementType);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001235
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001236 if (DT != DataTypeUnknown)
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001237 return new RSExportVectorType(Context,
1238 TypeName,
1239 DT,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001240 Normalized,
1241 EVT->getNumElements());
1242 else
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001243 return NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001244}
1245
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001246llvm::Type *RSExportVectorType::convertToLLVMType() const {
1247 llvm::Type *ElementType = RSExportPrimitiveType::convertToLLVMType();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001248 return llvm::VectorType::get(ElementType, getNumElement());
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001249}
1250
Zonr Chang641558f2010-10-12 21:07:06 +08001251bool RSExportVectorType::equals(const RSExportable *E) const {
1252 CHECK_PARENT_EQUALITY(RSExportPrimitiveType, E);
1253 return (static_cast<const RSExportVectorType*>(E)->getNumElement()
1254 == getNumElement());
1255}
1256
Zonr Chang92b344a2010-10-05 20:39:03 +08001257/***************************** RSExportMatrixType *****************************/
1258RSExportMatrixType *RSExportMatrixType::Create(RSContext *Context,
1259 const clang::RecordType *RT,
1260 const llvm::StringRef &TypeName,
1261 unsigned Dim) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001262 slangAssert((RT != NULL) && (RT->getTypeClass() == clang::Type::Record));
1263 slangAssert((Dim > 1) && "Invalid dimension of matrix");
Zonr Chang92b344a2010-10-05 20:39:03 +08001264
1265 // Check whether the struct rs_matrix is in our expected form (but assume it's
1266 // correct if we're not sure whether it's correct or not)
1267 const clang::RecordDecl* RD = RT->getDecl();
1268 RD = RD->getDefinition();
1269 if (RD != NULL) {
1270 // Find definition, perform further examination
1271 if (RD->field_empty()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001272 Context->ReportError(
1273 RD->getLocation(),
1274 "invalid matrix struct: must have 1 field for saving values: '%0'")
1275 << RD->getName();
Zonr Chang92b344a2010-10-05 20:39:03 +08001276 return NULL;
1277 }
1278
1279 clang::RecordDecl::field_iterator FIT = RD->field_begin();
1280 const clang::FieldDecl *FD = *FIT;
1281 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
1282 if ((FT == NULL) || (FT->getTypeClass() != clang::Type::ConstantArray)) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001283 Context->ReportError(RD->getLocation(),
1284 "invalid matrix struct: first field should"
1285 " be an array with constant size: '%0'")
1286 << RD->getName();
Zonr Chang92b344a2010-10-05 20:39:03 +08001287 return NULL;
1288 }
1289 const clang::ConstantArrayType *CAT =
1290 static_cast<const clang::ConstantArrayType *>(FT);
Zonr Chang2e1dba62010-10-05 22:20:11 +08001291 const clang::Type *ElementType = GET_CONSTANT_ARRAY_ELEMENT_TYPE(CAT);
Zonr Chang92b344a2010-10-05 20:39:03 +08001292 if ((ElementType == NULL) ||
1293 (ElementType->getTypeClass() != clang::Type::Builtin) ||
Logan Chien9207a2e2011-10-21 15:39:28 +08001294 (static_cast<const clang::BuiltinType *>(ElementType)->getKind() !=
1295 clang::BuiltinType::Float)) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001296 Context->ReportError(RD->getLocation(),
1297 "invalid matrix struct: first field "
1298 "should be a float array: '%0'")
1299 << RD->getName();
Zonr Chang92b344a2010-10-05 20:39:03 +08001300 return NULL;
1301 }
1302
1303 if (CAT->getSize() != Dim * Dim) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001304 Context->ReportError(RD->getLocation(),
1305 "invalid matrix struct: first field "
1306 "should be an array with size %0: '%1'")
1307 << (Dim * Dim) << (RD->getName());
Zonr Chang92b344a2010-10-05 20:39:03 +08001308 return NULL;
1309 }
1310
1311 FIT++;
1312 if (FIT != RD->field_end()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001313 Context->ReportError(RD->getLocation(),
1314 "invalid matrix struct: must have "
1315 "exactly 1 field: '%0'")
1316 << RD->getName();
Zonr Chang92b344a2010-10-05 20:39:03 +08001317 return NULL;
1318 }
1319 }
1320
1321 return new RSExportMatrixType(Context, TypeName, Dim);
1322}
1323
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001324llvm::Type *RSExportMatrixType::convertToLLVMType() const {
Zonr Chang92b344a2010-10-05 20:39:03 +08001325 // Construct LLVM type:
1326 // struct {
1327 // float X[mDim * mDim];
1328 // }
1329
1330 llvm::LLVMContext &C = getRSContext()->getLLVMContext();
1331 llvm::ArrayType *X = llvm::ArrayType::get(llvm::Type::getFloatTy(C),
1332 mDim * mDim);
Stephen Hinesa67e4452011-07-19 15:39:26 -07001333 return llvm::StructType::get(C, X, false);
Zonr Chang92b344a2010-10-05 20:39:03 +08001334}
1335
Zonr Chang641558f2010-10-12 21:07:06 +08001336bool RSExportMatrixType::equals(const RSExportable *E) const {
1337 CHECK_PARENT_EQUALITY(RSExportType, E);
1338 return (static_cast<const RSExportMatrixType*>(E)->getDim() == getDim());
1339}
1340
Zonr Chang2e1dba62010-10-05 22:20:11 +08001341/************************* RSExportConstantArrayType *************************/
1342RSExportConstantArrayType
1343*RSExportConstantArrayType::Create(RSContext *Context,
1344 const clang::ConstantArrayType *CAT) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001345 slangAssert(CAT != NULL && CAT->getTypeClass() == clang::Type::ConstantArray);
Zonr Chang2e1dba62010-10-05 22:20:11 +08001346
Stephen Hines6e6578a2011-02-07 18:05:48 -08001347 slangAssert((CAT->getSize().getActiveBits() < 32) && "array too large");
Zonr Chang2e1dba62010-10-05 22:20:11 +08001348
1349 unsigned Size = static_cast<unsigned>(CAT->getSize().getZExtValue());
Stephen Hines6e6578a2011-02-07 18:05:48 -08001350 slangAssert((Size > 0) && "Constant array should have size greater than 0");
Zonr Chang2e1dba62010-10-05 22:20:11 +08001351
1352 const clang::Type *ElementType = GET_CONSTANT_ARRAY_ELEMENT_TYPE(CAT);
1353 RSExportType *ElementET = RSExportType::Create(Context, ElementType);
1354
1355 if (ElementET == NULL) {
Zonr Chang2e1dba62010-10-05 22:20:11 +08001356 return NULL;
1357 }
1358
1359 return new RSExportConstantArrayType(Context,
1360 ElementET,
1361 Size);
1362}
1363
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001364llvm::Type *RSExportConstantArrayType::convertToLLVMType() const {
Zonr Chang2e1dba62010-10-05 22:20:11 +08001365 return llvm::ArrayType::get(mElementType->getLLVMType(), getSize());
1366}
1367
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001368bool RSExportConstantArrayType::keep() {
1369 if (!RSExportType::keep())
1370 return false;
Zonr Chang641558f2010-10-12 21:07:06 +08001371 const_cast<RSExportType*>(mElementType)->keep();
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001372 return true;
Zonr Chang641558f2010-10-12 21:07:06 +08001373}
1374
1375bool RSExportConstantArrayType::equals(const RSExportable *E) const {
1376 CHECK_PARENT_EQUALITY(RSExportType, E);
Stephen Hinesa7b75182010-12-07 15:49:49 -08001377 const RSExportConstantArrayType *RHS =
1378 static_cast<const RSExportConstantArrayType*>(E);
1379 return ((getSize() == RHS->getSize()) &&
1380 (getElementType()->equals(RHS->getElementType())));
Zonr Chang641558f2010-10-12 21:07:06 +08001381}
1382
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001383/**************************** RSExportRecordType ****************************/
1384RSExportRecordType *RSExportRecordType::Create(RSContext *Context,
1385 const clang::RecordType *RT,
1386 const llvm::StringRef &TypeName,
1387 bool mIsArtificial) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001388 slangAssert(RT != NULL && RT->getTypeClass() == clang::Type::Record);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001389
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001390 const clang::RecordDecl *RD = RT->getDecl();
Stephen Hines6e6578a2011-02-07 18:05:48 -08001391 slangAssert(RD->isStruct());
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001392
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001393 RD = RD->getDefinition();
1394 if (RD == NULL) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001395 slangAssert(false && "struct is not defined in this module");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001396 return NULL;
1397 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001398
Zonr Chang0da0a7d2010-10-05 21:26:37 +08001399 // Struct layout construct by clang. We rely on this for obtaining the
1400 // alloc size of a struct and offset of every field in that struct.
1401 const clang::ASTRecordLayout *RL =
Stephen Hines9e5b5032010-11-03 13:19:14 -07001402 &Context->getASTContext().getASTRecordLayout(RD);
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001403 slangAssert((RL != NULL) &&
1404 "Failed to retrieve the struct layout from Clang.");
Zonr Chang0da0a7d2010-10-05 21:26:37 +08001405
1406 RSExportRecordType *ERT =
1407 new RSExportRecordType(Context,
1408 TypeName,
1409 RD->hasAttr<clang::PackedAttr>(),
1410 mIsArtificial,
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -07001411 RL->getDataSize().getQuantity(),
Shih-wei Liao68318a12011-03-11 05:56:15 -08001412 RL->getSize().getQuantity());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001413 unsigned int Index = 0;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001414
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001415 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
1416 FE = RD->field_end();
1417 FI != FE;
Shih-wei Liao91a37832010-10-03 19:11:51 -07001418 FI++, Index++) {
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001419
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001420 // FIXME: All fields should be primitive type
Shih-wei Liao43730fe2012-08-02 23:06:18 -07001421 slangAssert(FI->getKind() == clang::Decl::Field);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001422 clang::FieldDecl *FD = *FI;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001423
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001424 if (FD->isBitField()) {
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001425 return NULL;
1426 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001427
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001428 // Type
zonr6315f762010-10-05 15:35:14 +08001429 RSExportType *ET = RSExportElement::CreateFromDecl(Context, FD);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001430
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001431 if (ET != NULL) {
Zonr Chang0da0a7d2010-10-05 21:26:37 +08001432 ERT->mFields.push_back(
1433 new Field(ET, FD->getName(), ERT,
1434 static_cast<size_t>(RL->getFieldOffset(Index) >> 3)));
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001435 } else {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001436 Context->ReportError(RD->getLocation(),
1437 "field type cannot be exported: '%0.%1'")
1438 << RD->getName() << FD->getName();
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001439 return NULL;
1440 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001441 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001442
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001443 return ERT;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001444}
1445
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001446llvm::Type *RSExportRecordType::convertToLLVMType() const {
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001447 // Create an opaque type since struct may reference itself recursively.
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001448
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001449 // TODO(sliao): LLVM took out the OpaqueType. Any other to migrate to?
1450 std::vector<llvm::Type*> FieldTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001451
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001452 for (const_field_iterator FI = fields_begin(), FE = fields_end();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001453 FI != FE;
1454 FI++) {
1455 const Field *F = *FI;
1456 const RSExportType *FET = F->getType();
Shih-wei Liao1f0d88f2010-06-25 00:15:24 -07001457
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001458 FieldTypes.push_back(FET->getLLVMType());
1459 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001460
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001461 llvm::StructType *ST = llvm::StructType::get(getRSContext()->getLLVMContext(),
1462 FieldTypes,
1463 mIsPacked);
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001464 if (ST != NULL) {
1465 return ST;
1466 } else {
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001467 return NULL;
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001468 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001469}
Zonr Chang641558f2010-10-12 21:07:06 +08001470
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001471bool RSExportRecordType::keep() {
1472 if (!RSExportType::keep())
1473 return false;
Zonr Chang641558f2010-10-12 21:07:06 +08001474 for (std::list<const Field*>::iterator I = mFields.begin(),
1475 E = mFields.end();
1476 I != E;
1477 I++) {
1478 const_cast<RSExportType*>((*I)->getType())->keep();
1479 }
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001480 return true;
Zonr Chang641558f2010-10-12 21:07:06 +08001481}
1482
1483bool RSExportRecordType::equals(const RSExportable *E) const {
1484 CHECK_PARENT_EQUALITY(RSExportType, E);
1485
1486 const RSExportRecordType *ERT = static_cast<const RSExportRecordType*>(E);
1487
1488 if (ERT->getFields().size() != getFields().size())
1489 return false;
1490
1491 const_field_iterator AI = fields_begin(), BI = ERT->fields_begin();
1492
1493 for (unsigned i = 0, e = getFields().size(); i != e; i++) {
1494 if (!(*AI)->getType()->equals((*BI)->getType()))
1495 return false;
1496 AI++;
1497 BI++;
1498 }
1499
1500 return true;
1501}
Stephen Hinese639eb52010-11-08 19:27:20 -08001502
Jason Sams1b6a0882012-03-12 15:07:58 -07001503void RSExportType::convertToRTD(RSReflectionTypeData *rtd) const {
1504 memset(rtd, 0, sizeof(*rtd));
1505 rtd->vecSize = 1;
1506
1507 switch(getClass()) {
1508 case RSExportType::ExportClassPrimitive: {
1509 const RSExportPrimitiveType *EPT = static_cast<const RSExportPrimitiveType*>(this);
1510 rtd->type = RSExportPrimitiveType::getRSReflectionType(EPT);
1511 return;
1512 }
1513 case RSExportType::ExportClassPointer: {
1514 const RSExportPointerType *EPT = static_cast<const RSExportPointerType*>(this);
1515 const RSExportType *PointeeType = EPT->getPointeeType();
1516 PointeeType->convertToRTD(rtd);
1517 rtd->isPointer = true;
1518 return;
1519 }
1520 case RSExportType::ExportClassVector: {
1521 const RSExportVectorType *EVT = static_cast<const RSExportVectorType*>(this);
1522 rtd->type = EVT->getRSReflectionType(EVT);
1523 rtd->vecSize = EVT->getNumElement();
1524 return;
1525 }
1526 case RSExportType::ExportClassMatrix: {
1527 const RSExportMatrixType *EMT = static_cast<const RSExportMatrixType*>(this);
1528 unsigned Dim = EMT->getDim();
1529 slangAssert((Dim >= 2) && (Dim <= 4));
1530 rtd->type = &gReflectionTypes[15 + Dim-2];
1531 return;
1532 }
1533 case RSExportType::ExportClassConstantArray: {
1534 const RSExportConstantArrayType* CAT =
1535 static_cast<const RSExportConstantArrayType*>(this);
1536 CAT->getElementType()->convertToRTD(rtd);
1537 rtd->arraySize = CAT->getSize();
1538 return;
1539 }
1540 case RSExportType::ExportClassRecord: {
1541 slangAssert(!"RSExportType::ExportClassRecord not implemented");
1542 return;// RS_TYPE_CLASS_NAME_PREFIX + ET->getName() + ".Item";
1543 }
1544 default: {
1545 slangAssert(false && "Unknown class of type");
1546 }
1547 }
1548}
1549
1550
Stephen Hinese639eb52010-11-08 19:27:20 -08001551} // namespace slang