blob: 1359451cc7f9b27ff811d2efda5216a6762375de [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
611/****************************** RSExportType ******************************/
612bool RSExportType::NormalizeType(const clang::Type *&T,
613 llvm::StringRef &TypeName,
Stephen Hines48d893d2013-12-06 18:00:27 -0800614 RSContext *Context,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700615 const clang::VarDecl *VD) {
Stephen Hines48d893d2013-12-06 18:00:27 -0800616 if ((T = TypeExportable(T, Context, VD)) == NULL) {
Stephen Hinese5e64432010-12-02 18:48:20 -0800617 return false;
618 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700619 // Get type name
620 TypeName = RSExportType::GetTypeName(T);
Stephen Hines48d893d2013-12-06 18:00:27 -0800621 if (Context && TypeName.empty()) {
Stephen Hines48d893d2013-12-06 18:00:27 -0800622 if (VD) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800623 Context->ReportError(VD->getLocation(),
624 "anonymous types cannot be exported");
Stephen Hines48d893d2013-12-06 18:00:27 -0800625 } else {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800626 Context->ReportError("anonymous types cannot be exported");
Stephen Hinese5e64432010-12-02 18:48:20 -0800627 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700628 return false;
Stephen Hinese5e64432010-12-02 18:48:20 -0800629 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700630
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700631 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700632}
633
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800634bool RSExportType::ValidateType(slang::RSContext *Context, clang::ASTContext &C,
635 clang::QualType QT, clang::NamedDecl *ND,
636 clang::SourceLocation Loc,
637 unsigned int TargetAPI, bool IsFilterscript) {
Stephen Hines11274a72012-09-26 19:14:20 -0700638 const clang::Type *T = QT.getTypePtr();
Stephen Hines78e69cb2011-04-22 15:03:19 -0700639 llvm::SmallPtrSet<const clang::Type*, 8> SPS =
640 llvm::SmallPtrSet<const clang::Type*, 8>();
641
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800642 return ValidateTypeHelper(Context, C, T, ND, Loc, SPS, false, NULL, TargetAPI,
Stephen Hines11274a72012-09-26 19:14:20 -0700643 IsFilterscript);
644 return true;
645}
646
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800647bool RSExportType::ValidateVarDecl(slang::RSContext *Context,
648 clang::VarDecl *VD, unsigned int TargetAPI,
Stephen Hines11274a72012-09-26 19:14:20 -0700649 bool IsFilterscript) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800650 return ValidateType(Context, VD->getASTContext(), VD->getType(), VD,
Stephen Hines11274a72012-09-26 19:14:20 -0700651 VD->getLocation(), TargetAPI, IsFilterscript);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700652}
653
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700654const clang::Type
655*RSExportType::GetTypeOfDecl(const clang::DeclaratorDecl *DD) {
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800656 if (DD) {
Stephen Hines8de19222013-08-23 16:30:59 -0700657 clang::QualType T = DD->getType();
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800658
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700659 if (T.isNull())
660 return NULL;
661 else
662 return T.getTypePtr();
663 }
664 return NULL;
665}
666
667llvm::StringRef RSExportType::GetTypeName(const clang::Type* T) {
668 T = GET_CANONICAL_TYPE(T);
669 if (T == NULL)
670 return llvm::StringRef();
671
672 switch (T->getTypeClass()) {
673 case clang::Type::Builtin: {
Loganbe274822011-02-16 22:02:54 +0800674 const clang::BuiltinType *BT =
675 UNSAFE_CAST_TYPE(const clang::BuiltinType, T);
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700676 BuiltinInfo *info = FindBuiltinType(BT->getKind());
677 if (info != NULL) {
678 return info->cname[0];
Zonr Changa65ec162010-10-17 01:53:05 +0800679 }
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700680 slangAssert(false && "Unknown data type of the builtin");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700681 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700682 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700683 case clang::Type::Record: {
Stephen Hinescd440ee2010-12-06 14:57:04 -0800684 clang::RecordDecl *RD;
685 if (T->isStructureType()) {
686 RD = T->getAsStructureType()->getDecl();
Stephen Hinesdd6206b2010-12-09 19:39:22 -0800687 } else {
Stephen Hinescd440ee2010-12-06 14:57:04 -0800688 break;
689 }
690
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700691 llvm::StringRef Name = RD->getName();
692 if (Name.empty()) {
Shih-wei Liao83f0c622011-06-21 05:34:53 -0700693 if (RD->getTypedefNameForAnonDecl() != NULL) {
694 Name = RD->getTypedefNameForAnonDecl()->getName();
695 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700696
Shih-wei Liao83f0c622011-06-21 05:34:53 -0700697 if (Name.empty()) {
698 // Try to find a name from redeclaration (i.e. typedef)
699 for (clang::TagDecl::redecl_iterator RI = RD->redecls_begin(),
700 RE = RD->redecls_end();
701 RI != RE;
702 RI++) {
703 slangAssert(*RI != NULL && "cannot be NULL object");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700704
Shih-wei Liao83f0c622011-06-21 05:34:53 -0700705 Name = (*RI)->getName();
706 if (!Name.empty())
707 break;
708 }
709 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700710 }
711 return Name;
712 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700713 case clang::Type::Pointer: {
714 // "*" plus pointee name
zonr6315f762010-10-05 15:35:14 +0800715 const clang::Type *PT = GET_POINTEE_TYPE(T);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700716 llvm::StringRef PointeeName;
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700717 if (NormalizeType(PT, PointeeName, NULL, NULL)) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700718 char *Name = new char[ 1 /* * */ + PointeeName.size() + 1 ];
719 Name[0] = '*';
720 memcpy(Name + 1, PointeeName.data(), PointeeName.size());
721 Name[PointeeName.size() + 1] = '\0';
722 return Name;
723 }
724 break;
725 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700726 case clang::Type::ExtVector: {
727 const clang::ExtVectorType *EVT =
Loganbe274822011-02-16 22:02:54 +0800728 UNSAFE_CAST_TYPE(const clang::ExtVectorType, T);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700729 return RSExportVectorType::GetTypeName(EVT);
730 break;
731 }
Zonr Chang2e1dba62010-10-05 22:20:11 +0800732 case clang::Type::ConstantArray : {
733 // Construct name for a constant array is too complicated.
734 return DUMMY_TYPE_NAME_FOR_RS_CONSTANT_ARRAY_TYPE;
735 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700736 default: {
737 break;
738 }
739 }
740
741 return llvm::StringRef();
742}
743
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700744
745RSExportType *RSExportType::Create(RSContext *Context,
746 const clang::Type *T,
747 const llvm::StringRef &TypeName) {
748 // Lookup the context to see whether the type was processed before.
749 // Newly created RSExportType will insert into context
750 // in RSExportType::RSExportType()
751 RSContext::export_type_iterator ETI = Context->findExportType(TypeName);
752
753 if (ETI != Context->export_types_end())
754 return ETI->second;
755
756 RSExportType *ET = NULL;
757 switch (T->getTypeClass()) {
758 case clang::Type::Record: {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700759 DataType dt = RSExportPrimitiveType::GetRSSpecificType(TypeName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700760 switch (dt) {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700761 case DataTypeUnknown: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800762 // User-defined types
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700763 ET = RSExportRecordType::Create(Context,
764 T->getAsStructureType(),
765 TypeName);
766 break;
767 }
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700768 case DataTypeRSMatrix2x2: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800769 // 2 x 2 Matrix type
770 ET = RSExportMatrixType::Create(Context,
771 T->getAsStructureType(),
772 TypeName,
773 2);
774 break;
775 }
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700776 case DataTypeRSMatrix3x3: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800777 // 3 x 3 Matrix type
778 ET = RSExportMatrixType::Create(Context,
779 T->getAsStructureType(),
780 TypeName,
781 3);
782 break;
783 }
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700784 case DataTypeRSMatrix4x4: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800785 // 4 x 4 Matrix type
786 ET = RSExportMatrixType::Create(Context,
787 T->getAsStructureType(),
788 TypeName,
789 4);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700790 break;
791 }
792 default: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800793 // Others are primitive types
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700794 ET = RSExportPrimitiveType::Create(Context, T, TypeName);
795 break;
796 }
797 }
798 break;
799 }
800 case clang::Type::Builtin: {
801 ET = RSExportPrimitiveType::Create(Context, T, TypeName);
802 break;
803 }
804 case clang::Type::Pointer: {
805 ET = RSExportPointerType::Create(Context,
Stephen Hines5baf6322011-04-25 17:21:15 -0700806 UNSAFE_CAST_TYPE(const clang::PointerType, T), TypeName);
Zonr Chang92b344a2010-10-05 20:39:03 +0800807 // FIXME: free the name (allocated in RSExportType::GetTypeName)
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700808 delete [] TypeName.data();
809 break;
810 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700811 case clang::Type::ExtVector: {
812 ET = RSExportVectorType::Create(Context,
Stephen Hines5baf6322011-04-25 17:21:15 -0700813 UNSAFE_CAST_TYPE(const clang::ExtVectorType, T), TypeName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700814 break;
815 }
Zonr Chang2e1dba62010-10-05 22:20:11 +0800816 case clang::Type::ConstantArray: {
817 ET = RSExportConstantArrayType::Create(
818 Context,
Loganbe274822011-02-16 22:02:54 +0800819 UNSAFE_CAST_TYPE(const clang::ConstantArrayType, T));
Zonr Chang2e1dba62010-10-05 22:20:11 +0800820 break;
821 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700822 default: {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800823 Context->ReportError("unknown type cannot be exported: '%0'")
824 << T->getTypeClassName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700825 break;
826 }
827 }
828
829 return ET;
830}
831
zonr6315f762010-10-05 15:35:14 +0800832RSExportType *RSExportType::Create(RSContext *Context, const clang::Type *T) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700833 llvm::StringRef TypeName;
Stephen Hines48d893d2013-12-06 18:00:27 -0800834 if (NormalizeType(T, TypeName, Context, NULL)) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700835 return Create(Context, T, TypeName);
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700836 } else {
Shih-wei Liaocecd11d2010-09-21 08:07:58 -0700837 return NULL;
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700838 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700839}
840
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700841RSExportType *RSExportType::CreateFromDecl(RSContext *Context,
842 const clang::VarDecl *VD) {
843 return RSExportType::Create(Context, GetTypeOfDecl(VD));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700844}
845
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700846size_t RSExportType::getStoreSize() const {
847 return getRSContext()->getDataLayout()->getTypeStoreSize(getLLVMType());
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700848}
849
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700850size_t RSExportType::getAllocSize() const {
851 return getRSContext()->getDataLayout()->getTypeAllocSize(getLLVMType());
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700852}
Shih-wei Liao1f0d88f2010-06-25 00:15:24 -0700853
Zonr Chang6b6320a2010-10-05 22:42:01 +0800854RSExportType::RSExportType(RSContext *Context,
855 ExportClass Class,
856 const llvm::StringRef &Name)
Zonr Changa41ce1d2010-10-06 02:23:12 +0800857 : RSExportable(Context, RSExportable::EX_TYPE),
Zonr Chang6b6320a2010-10-05 22:42:01 +0800858 mClass(Class),
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800859 // Make a copy on Name since memory stored @Name is either allocated in
860 // ASTContext or allocated in GetTypeName which will be destroyed later.
zonr6315f762010-10-05 15:35:14 +0800861 mName(Name.data(), Name.size()),
Jean-Luc Brouillet23544422014-05-14 15:12:25 -0700862 mLLVMType(NULL) {
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800863 // Don't cache the type whose name start with '<'. Those type failed to
864 // get their name since constructing their name in GetTypeName() requiring
865 // complicated work.
866 if (!Name.startswith(DUMMY_RS_TYPE_NAME_PREFIX))
867 // TODO(zonr): Need to check whether the insertion is successful or not.
868 Context->insertExportType(llvm::StringRef(Name), this);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700869 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700870}
871
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800872bool RSExportType::keep() {
873 if (!RSExportable::keep())
874 return false;
Zonr Chang641558f2010-10-12 21:07:06 +0800875 // Invalidate converted LLVM type.
876 mLLVMType = NULL;
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800877 return true;
Zonr Chang641558f2010-10-12 21:07:06 +0800878}
879
880bool RSExportType::equals(const RSExportable *E) const {
881 CHECK_PARENT_EQUALITY(RSExportable, E);
882 return (static_cast<const RSExportType*>(E)->getClass() == getClass());
883}
884
Zonr Changa7a828d2010-10-22 03:44:27 +0800885RSExportType::~RSExportType() {
Zonr Changa7a828d2010-10-22 03:44:27 +0800886}
887
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700888/************************** RSExportPrimitiveType **************************/
Zonr Changb1771ef2010-10-22 18:03:46 +0800889llvm::ManagedStatic<RSExportPrimitiveType::RSSpecificTypeMapTy>
890RSExportPrimitiveType::RSSpecificTypeMap;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700891
zonr6315f762010-10-05 15:35:14 +0800892llvm::Type *RSExportPrimitiveType::RSObjectLLVMType = NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700893
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700894bool RSExportPrimitiveType::IsPrimitiveType(const clang::Type *T) {
895 if ((T != NULL) && (T->getTypeClass() == clang::Type::Builtin))
896 return true;
897 else
898 return false;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700899}
900
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700901DataType
Zonr Changb1771ef2010-10-22 18:03:46 +0800902RSExportPrimitiveType::GetRSSpecificType(const llvm::StringRef &TypeName) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700903 if (TypeName.empty())
904 return DataTypeUnknown;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700905
Zonr Changb1771ef2010-10-22 18:03:46 +0800906 if (RSSpecificTypeMap->empty()) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700907 for (int i = 0; i < MatrixAndObjectDataTypesCount; i++) {
908 RSSpecificTypeMap->GetOrCreateValue(MatrixAndObjectDataTypes[i].name,
909 MatrixAndObjectDataTypes[i].dataType);
910 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700911 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700912
Zonr Changb1771ef2010-10-22 18:03:46 +0800913 RSSpecificTypeMapTy::const_iterator I = RSSpecificTypeMap->find(TypeName);
914 if (I == RSSpecificTypeMap->end())
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700915 return DataTypeUnknown;
916 else
917 return I->getValue();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700918}
919
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700920DataType RSExportPrimitiveType::GetRSSpecificType(const clang::Type *T) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700921 T = GET_CANONICAL_TYPE(T);
922 if ((T == NULL) || (T->getTypeClass() != clang::Type::Record))
923 return DataTypeUnknown;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700924
Zonr Changb1771ef2010-10-22 18:03:46 +0800925 return GetRSSpecificType( RSExportType::GetTypeName(T) );
926}
927
928bool RSExportPrimitiveType::IsRSMatrixType(DataType DT) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700929 if (DT < 0 || DT >= DataTypeMax) {
930 return false;
931 }
932 return gReflectionTypes[DT].category == MatrixDataType;
Zonr Changb1771ef2010-10-22 18:03:46 +0800933}
934
935bool RSExportPrimitiveType::IsRSObjectType(DataType DT) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700936 if (DT < 0 || DT >= DataTypeMax) {
937 return false;
938 }
939 return gReflectionTypes[DT].category == ObjectDataType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700940}
941
Stephen Hinesfeaca062011-02-04 14:08:13 -0800942bool RSExportPrimitiveType::IsStructureTypeWithRSObject(const clang::Type *T) {
943 bool RSObjectTypeSeen = false;
944 while (T && T->isArrayType()) {
945 T = T->getArrayElementTypeNoTypeQual();
946 }
947
948 const clang::RecordType *RT = T->getAsStructureType();
949 if (!RT) {
950 return false;
951 }
Stephen Hinesb0fabe52013-01-07 19:06:09 -0800952
Stephen Hinesfeaca062011-02-04 14:08:13 -0800953 const clang::RecordDecl *RD = RT->getDecl();
Stephen Hinesb0fabe52013-01-07 19:06:09 -0800954 if (RD) {
955 RD = RD->getDefinition();
956 }
957 if (!RD) {
958 return false;
959 }
960
Stephen Hinesfeaca062011-02-04 14:08:13 -0800961 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
962 FE = RD->field_end();
963 FI != FE;
964 FI++) {
965 // We just look through all field declarations to see if we find a
966 // declaration for an RS object type (or an array of one).
967 const clang::FieldDecl *FD = *FI;
968 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
969 while (FT && FT->isArrayType()) {
970 FT = FT->getArrayElementTypeNoTypeQual();
971 }
972
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700973 DataType DT = GetRSSpecificType(FT);
Stephen Hinesfeaca062011-02-04 14:08:13 -0800974 if (IsRSObjectType(DT)) {
975 // RS object types definitely need to be zero-initialized
976 RSObjectTypeSeen = true;
977 } else {
978 switch (DT) {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700979 case DataTypeRSMatrix2x2:
980 case DataTypeRSMatrix3x3:
981 case DataTypeRSMatrix4x4:
Stephen Hinesfeaca062011-02-04 14:08:13 -0800982 // Matrix types should get zero-initialized as well
983 RSObjectTypeSeen = true;
984 break;
985 default:
986 // Ignore all other primitive types
987 break;
988 }
989 while (FT && FT->isArrayType()) {
990 FT = FT->getArrayElementTypeNoTypeQual();
991 }
992 if (FT->isStructureType()) {
993 // Recursively handle structs of structs (even though these can't
994 // be exported, it is possible for a user to have them internally).
995 RSObjectTypeSeen |= IsStructureTypeWithRSObject(FT);
996 }
997 }
998 }
999
1000 return RSObjectTypeSeen;
1001}
1002
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001003size_t RSExportPrimitiveType::GetSizeInBits(const RSExportPrimitiveType *EPT) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001004 int type = EPT->getType();
1005 slangAssert((type > DataTypeUnknown && type < DataTypeMax) &&
Stephen Hines6e6578a2011-02-07 18:05:48 -08001006 "RSExportPrimitiveType::GetSizeInBits : unknown data type");
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001007 return gReflectionTypes[type].size_in_bits;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001008}
1009
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001010DataType
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001011RSExportPrimitiveType::GetDataType(RSContext *Context, const clang::Type *T) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001012 if (T == NULL)
1013 return DataTypeUnknown;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001014
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001015 switch (T->getTypeClass()) {
1016 case clang::Type::Builtin: {
Loganbe274822011-02-16 22:02:54 +08001017 const clang::BuiltinType *BT =
1018 UNSAFE_CAST_TYPE(const clang::BuiltinType, T);
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001019 BuiltinInfo *info = FindBuiltinType(BT->getKind());
1020 if (info != NULL) {
1021 return info->type;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001022 }
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001023 // The size of type WChar depend on platform so we abandon the support
1024 // to them.
1025 Context->ReportError("built-in type cannot be exported: '%0'")
1026 << T->getTypeClassName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001027 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001028 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001029 case clang::Type::Record: {
1030 // must be RS object type
Zonr Changb1771ef2010-10-22 18:03:46 +08001031 return RSExportPrimitiveType::GetRSSpecificType(T);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001032 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001033 default: {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001034 Context->ReportError("primitive type cannot be exported: '%0'")
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001035 << T->getTypeClassName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001036 break;
1037 }
1038 }
1039
1040 return DataTypeUnknown;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001041}
1042
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001043RSExportPrimitiveType
1044*RSExportPrimitiveType::Create(RSContext *Context,
1045 const clang::Type *T,
1046 const llvm::StringRef &TypeName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001047 bool Normalized) {
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001048 DataType DT = GetDataType(Context, T);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001049
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001050 if ((DT == DataTypeUnknown) || TypeName.empty())
1051 return NULL;
1052 else
Zonr Chang6b6320a2010-10-05 22:42:01 +08001053 return new RSExportPrimitiveType(Context, ExportClassPrimitive, TypeName,
Stephen Hines2b8fb642012-03-09 00:12:47 -08001054 DT, Normalized);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001055}
1056
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001057RSExportPrimitiveType *RSExportPrimitiveType::Create(RSContext *Context,
Stephen Hines2b8fb642012-03-09 00:12:47 -08001058 const clang::Type *T) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001059 llvm::StringRef TypeName;
Stephen Hines48d893d2013-12-06 18:00:27 -08001060 if (RSExportType::NormalizeType(T, TypeName, Context, NULL)
Stephen Hines78e69cb2011-04-22 15:03:19 -07001061 && IsPrimitiveType(T)) {
Stephen Hines2b8fb642012-03-09 00:12:47 -08001062 return Create(Context, T, TypeName);
Stephen Hinese5e64432010-12-02 18:48:20 -08001063 } else {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001064 return NULL;
Stephen Hinese5e64432010-12-02 18:48:20 -08001065 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001066}
1067
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001068llvm::Type *RSExportPrimitiveType::convertToLLVMType() const {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001069 llvm::LLVMContext &C = getRSContext()->getLLVMContext();
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001070
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001071 if (isRSObjectType()) {
1072 // struct {
1073 // int *p;
1074 // } __attribute__((packed, aligned(pointer_size)))
1075 //
1076 // which is
1077 //
1078 // <{ [1 x i32] }> in LLVM
1079 //
1080 if (RSObjectLLVMType == NULL) {
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001081 std::vector<llvm::Type *> Elements;
zonr6315f762010-10-05 15:35:14 +08001082 Elements.push_back(llvm::ArrayType::get(llvm::Type::getInt32Ty(C), 1));
Stephen Hinesa67e4452011-07-19 15:39:26 -07001083 RSObjectLLVMType = llvm::StructType::get(C, Elements, true);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001084 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001085 return RSObjectLLVMType;
1086 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001087
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001088 switch (mType) {
1089 case DataTypeFloat32: {
1090 return llvm::Type::getFloatTy(C);
1091 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001092 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001093 case DataTypeFloat64: {
1094 return llvm::Type::getDoubleTy(C);
1095 break;
1096 }
Shih-wei Liao91a37832010-10-03 19:11:51 -07001097 case DataTypeBoolean: {
1098 return llvm::Type::getInt1Ty(C);
1099 break;
1100 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001101 case DataTypeSigned8:
1102 case DataTypeUnsigned8: {
1103 return llvm::Type::getInt8Ty(C);
1104 break;
1105 }
1106 case DataTypeSigned16:
1107 case DataTypeUnsigned16:
1108 case DataTypeUnsigned565:
1109 case DataTypeUnsigned5551:
1110 case DataTypeUnsigned4444: {
1111 return llvm::Type::getInt16Ty(C);
1112 break;
1113 }
1114 case DataTypeSigned32:
1115 case DataTypeUnsigned32: {
1116 return llvm::Type::getInt32Ty(C);
1117 break;
1118 }
Stephen Hinesa5d2c232010-10-11 16:10:02 -07001119 case DataTypeSigned64:
1120 case DataTypeUnsigned64: {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001121 return llvm::Type::getInt64Ty(C);
1122 break;
1123 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001124 default: {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001125 slangAssert(false && "Unknown data type");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001126 }
1127 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001128
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001129 return NULL;
1130}
1131
Zonr Chang641558f2010-10-12 21:07:06 +08001132bool RSExportPrimitiveType::equals(const RSExportable *E) const {
1133 CHECK_PARENT_EQUALITY(RSExportType, E);
1134 return (static_cast<const RSExportPrimitiveType*>(E)->getType() == getType());
1135}
1136
Stephen Hinesfdd1ba12012-03-08 17:26:32 -08001137RSReflectionType *RSExportPrimitiveType::getRSReflectionType(DataType DT) {
1138 if (DT > DataTypeUnknown && DT < DataTypeMax) {
1139 return &gReflectionTypes[DT];
1140 } else {
1141 return NULL;
1142 }
1143}
1144
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001145/**************************** RSExportPointerType ****************************/
1146
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001147RSExportPointerType
1148*RSExportPointerType::Create(RSContext *Context,
1149 const clang::PointerType *PT,
1150 const llvm::StringRef &TypeName) {
1151 const clang::Type *PointeeType = GET_POINTEE_TYPE(PT);
1152 const RSExportType *PointeeET;
1153
1154 if (PointeeType->getTypeClass() != clang::Type::Pointer) {
1155 PointeeET = RSExportType::Create(Context, PointeeType);
1156 } else {
1157 // Double or higher dimension of pointer, export as int*
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001158 PointeeET = RSExportPrimitiveType::Create(Context,
1159 Context->getASTContext().IntTy.getTypePtr());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001160 }
1161
1162 if (PointeeET == NULL) {
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001163 // Error diagnostic is emitted for corresponding pointee type
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001164 return NULL;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001165 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001166
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001167 return new RSExportPointerType(Context, TypeName, PointeeET);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001168}
1169
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001170llvm::Type *RSExportPointerType::convertToLLVMType() const {
1171 llvm::Type *PointeeType = mPointeeType->getLLVMType();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001172 return llvm::PointerType::getUnqual(PointeeType);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001173}
1174
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001175bool RSExportPointerType::keep() {
1176 if (!RSExportType::keep())
1177 return false;
Zonr Chang641558f2010-10-12 21:07:06 +08001178 const_cast<RSExportType*>(mPointeeType)->keep();
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001179 return true;
Zonr Chang641558f2010-10-12 21:07:06 +08001180}
1181
1182bool RSExportPointerType::equals(const RSExportable *E) const {
1183 CHECK_PARENT_EQUALITY(RSExportType, E);
1184 return (static_cast<const RSExportPointerType*>(E)
1185 ->getPointeeType()->equals(getPointeeType()));
1186}
1187
zonr6315f762010-10-05 15:35:14 +08001188/***************************** RSExportVectorType *****************************/
zonr6315f762010-10-05 15:35:14 +08001189llvm::StringRef
1190RSExportVectorType::GetTypeName(const clang::ExtVectorType *EVT) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001191 const clang::Type *ElementType = GET_EXT_VECTOR_ELEMENT_TYPE(EVT);
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001192 llvm::StringRef name;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001193
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001194 if ((ElementType->getTypeClass() != clang::Type::Builtin))
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001195 return name;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001196
Loganbe274822011-02-16 22:02:54 +08001197 const clang::BuiltinType *BT = UNSAFE_CAST_TYPE(const clang::BuiltinType,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001198 ElementType);
Zonr Changa65ec162010-10-17 01:53:05 +08001199 if ((EVT->getNumElements() < 1) ||
1200 (EVT->getNumElements() > 4))
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001201 return name;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001202
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001203 BuiltinInfo *info = FindBuiltinType(BT->getKind());
1204 if (info != NULL) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001205 // Compiler is smart enough to optimize following *big if branches* since
1206 // they all become "constant comparison" after macro expansion
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001207 int I = EVT->getNumElements() - 1;
1208 if (I < kMaxVectorSize) {
1209 name = info->cname[I];
1210 } else {
1211 slangAssert(false && "Max vector is 4");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001212 }
1213 }
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001214 return name;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001215}
1216
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001217RSExportVectorType *RSExportVectorType::Create(RSContext *Context,
1218 const clang::ExtVectorType *EVT,
1219 const llvm::StringRef &TypeName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001220 bool Normalized) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001221 slangAssert(EVT != NULL && EVT->getTypeClass() == clang::Type::ExtVector);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001222
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001223 const clang::Type *ElementType = GET_EXT_VECTOR_ELEMENT_TYPE(EVT);
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001224 DataType DT = RSExportPrimitiveType::GetDataType(Context, ElementType);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001225
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001226 if (DT != DataTypeUnknown)
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001227 return new RSExportVectorType(Context,
1228 TypeName,
1229 DT,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001230 Normalized,
1231 EVT->getNumElements());
1232 else
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001233 return NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001234}
1235
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001236llvm::Type *RSExportVectorType::convertToLLVMType() const {
1237 llvm::Type *ElementType = RSExportPrimitiveType::convertToLLVMType();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001238 return llvm::VectorType::get(ElementType, getNumElement());
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001239}
1240
Zonr Chang641558f2010-10-12 21:07:06 +08001241bool RSExportVectorType::equals(const RSExportable *E) const {
1242 CHECK_PARENT_EQUALITY(RSExportPrimitiveType, E);
1243 return (static_cast<const RSExportVectorType*>(E)->getNumElement()
1244 == getNumElement());
1245}
1246
Zonr Chang92b344a2010-10-05 20:39:03 +08001247/***************************** RSExportMatrixType *****************************/
1248RSExportMatrixType *RSExportMatrixType::Create(RSContext *Context,
1249 const clang::RecordType *RT,
1250 const llvm::StringRef &TypeName,
1251 unsigned Dim) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001252 slangAssert((RT != NULL) && (RT->getTypeClass() == clang::Type::Record));
1253 slangAssert((Dim > 1) && "Invalid dimension of matrix");
Zonr Chang92b344a2010-10-05 20:39:03 +08001254
1255 // Check whether the struct rs_matrix is in our expected form (but assume it's
1256 // correct if we're not sure whether it's correct or not)
1257 const clang::RecordDecl* RD = RT->getDecl();
1258 RD = RD->getDefinition();
1259 if (RD != NULL) {
1260 // Find definition, perform further examination
1261 if (RD->field_empty()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001262 Context->ReportError(
1263 RD->getLocation(),
1264 "invalid matrix struct: must have 1 field for saving values: '%0'")
1265 << RD->getName();
Zonr Chang92b344a2010-10-05 20:39:03 +08001266 return NULL;
1267 }
1268
1269 clang::RecordDecl::field_iterator FIT = RD->field_begin();
1270 const clang::FieldDecl *FD = *FIT;
1271 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
1272 if ((FT == NULL) || (FT->getTypeClass() != clang::Type::ConstantArray)) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001273 Context->ReportError(RD->getLocation(),
1274 "invalid matrix struct: first field should"
1275 " be an array with constant size: '%0'")
1276 << RD->getName();
Zonr Chang92b344a2010-10-05 20:39:03 +08001277 return NULL;
1278 }
1279 const clang::ConstantArrayType *CAT =
1280 static_cast<const clang::ConstantArrayType *>(FT);
Zonr Chang2e1dba62010-10-05 22:20:11 +08001281 const clang::Type *ElementType = GET_CONSTANT_ARRAY_ELEMENT_TYPE(CAT);
Zonr Chang92b344a2010-10-05 20:39:03 +08001282 if ((ElementType == NULL) ||
1283 (ElementType->getTypeClass() != clang::Type::Builtin) ||
Logan Chien9207a2e2011-10-21 15:39:28 +08001284 (static_cast<const clang::BuiltinType *>(ElementType)->getKind() !=
1285 clang::BuiltinType::Float)) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001286 Context->ReportError(RD->getLocation(),
1287 "invalid matrix struct: first field "
1288 "should be a float array: '%0'")
1289 << RD->getName();
Zonr Chang92b344a2010-10-05 20:39:03 +08001290 return NULL;
1291 }
1292
1293 if (CAT->getSize() != Dim * Dim) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001294 Context->ReportError(RD->getLocation(),
1295 "invalid matrix struct: first field "
1296 "should be an array with size %0: '%1'")
1297 << (Dim * Dim) << (RD->getName());
Zonr Chang92b344a2010-10-05 20:39:03 +08001298 return NULL;
1299 }
1300
1301 FIT++;
1302 if (FIT != RD->field_end()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001303 Context->ReportError(RD->getLocation(),
1304 "invalid matrix struct: must have "
1305 "exactly 1 field: '%0'")
1306 << RD->getName();
Zonr Chang92b344a2010-10-05 20:39:03 +08001307 return NULL;
1308 }
1309 }
1310
1311 return new RSExportMatrixType(Context, TypeName, Dim);
1312}
1313
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001314llvm::Type *RSExportMatrixType::convertToLLVMType() const {
Zonr Chang92b344a2010-10-05 20:39:03 +08001315 // Construct LLVM type:
1316 // struct {
1317 // float X[mDim * mDim];
1318 // }
1319
1320 llvm::LLVMContext &C = getRSContext()->getLLVMContext();
1321 llvm::ArrayType *X = llvm::ArrayType::get(llvm::Type::getFloatTy(C),
1322 mDim * mDim);
Stephen Hinesa67e4452011-07-19 15:39:26 -07001323 return llvm::StructType::get(C, X, false);
Zonr Chang92b344a2010-10-05 20:39:03 +08001324}
1325
Zonr Chang641558f2010-10-12 21:07:06 +08001326bool RSExportMatrixType::equals(const RSExportable *E) const {
1327 CHECK_PARENT_EQUALITY(RSExportType, E);
1328 return (static_cast<const RSExportMatrixType*>(E)->getDim() == getDim());
1329}
1330
Zonr Chang2e1dba62010-10-05 22:20:11 +08001331/************************* RSExportConstantArrayType *************************/
1332RSExportConstantArrayType
1333*RSExportConstantArrayType::Create(RSContext *Context,
1334 const clang::ConstantArrayType *CAT) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001335 slangAssert(CAT != NULL && CAT->getTypeClass() == clang::Type::ConstantArray);
Zonr Chang2e1dba62010-10-05 22:20:11 +08001336
Stephen Hines6e6578a2011-02-07 18:05:48 -08001337 slangAssert((CAT->getSize().getActiveBits() < 32) && "array too large");
Zonr Chang2e1dba62010-10-05 22:20:11 +08001338
1339 unsigned Size = static_cast<unsigned>(CAT->getSize().getZExtValue());
Stephen Hines6e6578a2011-02-07 18:05:48 -08001340 slangAssert((Size > 0) && "Constant array should have size greater than 0");
Zonr Chang2e1dba62010-10-05 22:20:11 +08001341
1342 const clang::Type *ElementType = GET_CONSTANT_ARRAY_ELEMENT_TYPE(CAT);
1343 RSExportType *ElementET = RSExportType::Create(Context, ElementType);
1344
1345 if (ElementET == NULL) {
Zonr Chang2e1dba62010-10-05 22:20:11 +08001346 return NULL;
1347 }
1348
1349 return new RSExportConstantArrayType(Context,
1350 ElementET,
1351 Size);
1352}
1353
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001354llvm::Type *RSExportConstantArrayType::convertToLLVMType() const {
Zonr Chang2e1dba62010-10-05 22:20:11 +08001355 return llvm::ArrayType::get(mElementType->getLLVMType(), getSize());
1356}
1357
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001358bool RSExportConstantArrayType::keep() {
1359 if (!RSExportType::keep())
1360 return false;
Zonr Chang641558f2010-10-12 21:07:06 +08001361 const_cast<RSExportType*>(mElementType)->keep();
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001362 return true;
Zonr Chang641558f2010-10-12 21:07:06 +08001363}
1364
1365bool RSExportConstantArrayType::equals(const RSExportable *E) const {
1366 CHECK_PARENT_EQUALITY(RSExportType, E);
Stephen Hinesa7b75182010-12-07 15:49:49 -08001367 const RSExportConstantArrayType *RHS =
1368 static_cast<const RSExportConstantArrayType*>(E);
1369 return ((getSize() == RHS->getSize()) &&
1370 (getElementType()->equals(RHS->getElementType())));
Zonr Chang641558f2010-10-12 21:07:06 +08001371}
1372
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001373/**************************** RSExportRecordType ****************************/
1374RSExportRecordType *RSExportRecordType::Create(RSContext *Context,
1375 const clang::RecordType *RT,
1376 const llvm::StringRef &TypeName,
1377 bool mIsArtificial) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001378 slangAssert(RT != NULL && RT->getTypeClass() == clang::Type::Record);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001379
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001380 const clang::RecordDecl *RD = RT->getDecl();
Stephen Hines6e6578a2011-02-07 18:05:48 -08001381 slangAssert(RD->isStruct());
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001382
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001383 RD = RD->getDefinition();
1384 if (RD == NULL) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001385 slangAssert(false && "struct is not defined in this module");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001386 return NULL;
1387 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001388
Zonr Chang0da0a7d2010-10-05 21:26:37 +08001389 // Struct layout construct by clang. We rely on this for obtaining the
1390 // alloc size of a struct and offset of every field in that struct.
1391 const clang::ASTRecordLayout *RL =
Stephen Hines9e5b5032010-11-03 13:19:14 -07001392 &Context->getASTContext().getASTRecordLayout(RD);
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001393 slangAssert((RL != NULL) &&
1394 "Failed to retrieve the struct layout from Clang.");
Zonr Chang0da0a7d2010-10-05 21:26:37 +08001395
1396 RSExportRecordType *ERT =
1397 new RSExportRecordType(Context,
1398 TypeName,
1399 RD->hasAttr<clang::PackedAttr>(),
1400 mIsArtificial,
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -07001401 RL->getDataSize().getQuantity(),
Shih-wei Liao68318a12011-03-11 05:56:15 -08001402 RL->getSize().getQuantity());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001403 unsigned int Index = 0;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001404
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001405 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
1406 FE = RD->field_end();
1407 FI != FE;
Shih-wei Liao91a37832010-10-03 19:11:51 -07001408 FI++, Index++) {
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001409
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001410 // FIXME: All fields should be primitive type
Shih-wei Liao43730fe2012-08-02 23:06:18 -07001411 slangAssert(FI->getKind() == clang::Decl::Field);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001412 clang::FieldDecl *FD = *FI;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001413
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001414 if (FD->isBitField()) {
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001415 return NULL;
1416 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001417
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001418 // Type
zonr6315f762010-10-05 15:35:14 +08001419 RSExportType *ET = RSExportElement::CreateFromDecl(Context, FD);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001420
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001421 if (ET != NULL) {
Zonr Chang0da0a7d2010-10-05 21:26:37 +08001422 ERT->mFields.push_back(
1423 new Field(ET, FD->getName(), ERT,
1424 static_cast<size_t>(RL->getFieldOffset(Index) >> 3)));
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001425 } else {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001426 Context->ReportError(RD->getLocation(),
1427 "field type cannot be exported: '%0.%1'")
1428 << RD->getName() << FD->getName();
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001429 return NULL;
1430 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001431 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001432
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001433 return ERT;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001434}
1435
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001436llvm::Type *RSExportRecordType::convertToLLVMType() const {
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001437 // Create an opaque type since struct may reference itself recursively.
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001438
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001439 // TODO(sliao): LLVM took out the OpaqueType. Any other to migrate to?
1440 std::vector<llvm::Type*> FieldTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001441
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001442 for (const_field_iterator FI = fields_begin(), FE = fields_end();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001443 FI != FE;
1444 FI++) {
1445 const Field *F = *FI;
1446 const RSExportType *FET = F->getType();
Shih-wei Liao1f0d88f2010-06-25 00:15:24 -07001447
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001448 FieldTypes.push_back(FET->getLLVMType());
1449 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001450
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001451 llvm::StructType *ST = llvm::StructType::get(getRSContext()->getLLVMContext(),
1452 FieldTypes,
1453 mIsPacked);
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001454 if (ST != NULL) {
1455 return ST;
1456 } else {
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001457 return NULL;
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001458 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001459}
Zonr Chang641558f2010-10-12 21:07:06 +08001460
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001461bool RSExportRecordType::keep() {
1462 if (!RSExportType::keep())
1463 return false;
Zonr Chang641558f2010-10-12 21:07:06 +08001464 for (std::list<const Field*>::iterator I = mFields.begin(),
1465 E = mFields.end();
1466 I != E;
1467 I++) {
1468 const_cast<RSExportType*>((*I)->getType())->keep();
1469 }
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001470 return true;
Zonr Chang641558f2010-10-12 21:07:06 +08001471}
1472
1473bool RSExportRecordType::equals(const RSExportable *E) const {
1474 CHECK_PARENT_EQUALITY(RSExportType, E);
1475
1476 const RSExportRecordType *ERT = static_cast<const RSExportRecordType*>(E);
1477
1478 if (ERT->getFields().size() != getFields().size())
1479 return false;
1480
1481 const_field_iterator AI = fields_begin(), BI = ERT->fields_begin();
1482
1483 for (unsigned i = 0, e = getFields().size(); i != e; i++) {
1484 if (!(*AI)->getType()->equals((*BI)->getType()))
1485 return false;
1486 AI++;
1487 BI++;
1488 }
1489
1490 return true;
1491}
Stephen Hinese639eb52010-11-08 19:27:20 -08001492
Jason Sams1b6a0882012-03-12 15:07:58 -07001493void RSExportType::convertToRTD(RSReflectionTypeData *rtd) const {
1494 memset(rtd, 0, sizeof(*rtd));
1495 rtd->vecSize = 1;
1496
1497 switch(getClass()) {
1498 case RSExportType::ExportClassPrimitive: {
1499 const RSExportPrimitiveType *EPT = static_cast<const RSExportPrimitiveType*>(this);
1500 rtd->type = RSExportPrimitiveType::getRSReflectionType(EPT);
1501 return;
1502 }
1503 case RSExportType::ExportClassPointer: {
1504 const RSExportPointerType *EPT = static_cast<const RSExportPointerType*>(this);
1505 const RSExportType *PointeeType = EPT->getPointeeType();
1506 PointeeType->convertToRTD(rtd);
1507 rtd->isPointer = true;
1508 return;
1509 }
1510 case RSExportType::ExportClassVector: {
1511 const RSExportVectorType *EVT = static_cast<const RSExportVectorType*>(this);
1512 rtd->type = EVT->getRSReflectionType(EVT);
1513 rtd->vecSize = EVT->getNumElement();
1514 return;
1515 }
1516 case RSExportType::ExportClassMatrix: {
1517 const RSExportMatrixType *EMT = static_cast<const RSExportMatrixType*>(this);
1518 unsigned Dim = EMT->getDim();
1519 slangAssert((Dim >= 2) && (Dim <= 4));
1520 rtd->type = &gReflectionTypes[15 + Dim-2];
1521 return;
1522 }
1523 case RSExportType::ExportClassConstantArray: {
1524 const RSExportConstantArrayType* CAT =
1525 static_cast<const RSExportConstantArrayType*>(this);
1526 CAT->getElementType()->convertToRTD(rtd);
1527 rtd->arraySize = CAT->getSize();
1528 return;
1529 }
1530 case RSExportType::ExportClassRecord: {
1531 slangAssert(!"RSExportType::ExportClassRecord not implemented");
1532 return;// RS_TYPE_CLASS_NAME_PREFIX + ET->getName() + ".Item";
1533 }
1534 default: {
1535 slangAssert(false && "Unknown class of type");
1536 }
1537 }
1538}
1539
1540
Stephen Hinese639eb52010-11-08 19:27:20 -08001541} // namespace slang