blob: 4348ea0a949e1ecc5f9a5c493282c99e404e204e [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
Stephen Hines9ae18b22014-06-10 23:53:00 -070072 // RS object types are 32 bits in 32-bit RS, but 256 bits in 64-bit RS.
73 // This is handled specially by the GetSizeInBits() method.
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -070074 {ObjectDataType, "RS_ELEMENT", "ELEMENT", 32, "Element", "Element", NULL, NULL, false},
75 {ObjectDataType, "RS_TYPE", "TYPE", 32, "Type", "Type", NULL, NULL, false},
76 {ObjectDataType, "RS_ALLOCATION", "ALLOCATION", 32, "Allocation", "Allocation", NULL, NULL, false},
77 {ObjectDataType, "RS_SAMPLER", "SAMPLER", 32, "Sampler", "Sampler", NULL, NULL, false},
78 {ObjectDataType, "RS_SCRIPT", "SCRIPT", 32, "Script", "Script", NULL, NULL, false},
79 {ObjectDataType, "RS_MESH", "MESH", 32, "Mesh", "Mesh", NULL, NULL, false},
80 {ObjectDataType, "RS_PATH", "PATH", 32, "Path", "Path", NULL, NULL, false},
81
82 {ObjectDataType, "RS_PROGRAM_FRAGMENT", "PROGRAM_FRAGMENT", 32, "ProgramFragment", "ProgramFragment", NULL, NULL, false},
83 {ObjectDataType, "RS_PROGRAM_VERTEX", "PROGRAM_VERTEX", 32, "ProgramVertex", "ProgramVertex", NULL, NULL, false},
84 {ObjectDataType, "RS_PROGRAM_RASTER", "PROGRAM_RASTER", 32, "ProgramRaster", "ProgramRaster", NULL, NULL, false},
85 {ObjectDataType, "RS_PROGRAM_STORE", "PROGRAM_STORE", 32, "ProgramStore", "ProgramStore", NULL, NULL, false},
86 {ObjectDataType, "RS_FONT", "FONT", 32, "Font", "Font", NULL, NULL, false}
Stephen Hinesfdd1ba12012-03-08 17:26:32 -080087};
88
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -070089const int kMaxVectorSize = 4;
90
91struct BuiltinInfo {
92 clang::BuiltinType::Kind builtinTypeKind;
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -070093 DataType type;
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -070094 /* TODO If we return std::string instead of llvm::StringRef, we could build
95 * the name instead of duplicating the entries.
96 */
97 const char *cname[kMaxVectorSize];
98};
99
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700100
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700101BuiltinInfo BuiltinInfoTable[] = {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700102 {clang::BuiltinType::Bool, DataTypeBoolean,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700103 {"bool", "bool2", "bool3", "bool4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700104 {clang::BuiltinType::Char_U, DataTypeUnsigned8,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700105 {"uchar", "uchar2", "uchar3", "uchar4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700106 {clang::BuiltinType::UChar, DataTypeUnsigned8,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700107 {"uchar", "uchar2", "uchar3", "uchar4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700108 {clang::BuiltinType::Char16, DataTypeSigned16,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700109 {"short", "short2", "short3", "short4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700110 {clang::BuiltinType::Char32, DataTypeSigned32,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700111 {"int", "int2", "int3", "int4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700112 {clang::BuiltinType::UShort, DataTypeUnsigned16,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700113 {"ushort", "ushort2", "ushort3", "ushort4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700114 {clang::BuiltinType::UInt, DataTypeUnsigned32,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700115 {"uint", "uint2", "uint3", "uint4"}},
Tim Murraye46265d2014-08-18 14:16:32 -0700116 {clang::BuiltinType::ULong, DataTypeUnsigned64,
117 {"ulong", "ulong2", "ulong3", "ulong4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700118 {clang::BuiltinType::ULongLong, DataTypeUnsigned64,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700119 {"ulong", "ulong2", "ulong3", "ulong4"}},
120
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700121 {clang::BuiltinType::Char_S, DataTypeSigned8,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700122 {"char", "char2", "char3", "char4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700123 {clang::BuiltinType::SChar, DataTypeSigned8,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700124 {"char", "char2", "char3", "char4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700125 {clang::BuiltinType::Short, DataTypeSigned16,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700126 {"short", "short2", "short3", "short4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700127 {clang::BuiltinType::Int, DataTypeSigned32,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700128 {"int", "int2", "int3", "int4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700129 {clang::BuiltinType::Long, DataTypeSigned64,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700130 {"long", "long2", "long3", "long4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700131 {clang::BuiltinType::LongLong, DataTypeSigned64,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700132 {"long", "long2", "long3", "long4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700133 {clang::BuiltinType::Float, DataTypeFloat32,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700134 {"float", "float2", "float3", "float4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700135 {clang::BuiltinType::Double, DataTypeFloat64,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700136 {"double", "double2", "double3", "double4"}},
137};
138const int BuiltinInfoTableCount = sizeof(BuiltinInfoTable) / sizeof(BuiltinInfoTable[0]);
139
140struct NameAndPrimitiveType {
141 const char *name;
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700142 DataType dataType;
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700143};
144
145static NameAndPrimitiveType MatrixAndObjectDataTypes[] = {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700146 {"rs_matrix2x2", DataTypeRSMatrix2x2},
147 {"rs_matrix3x3", DataTypeRSMatrix3x3},
148 {"rs_matrix4x4", DataTypeRSMatrix4x4},
149 {"rs_element", DataTypeRSElement},
150 {"rs_type", DataTypeRSType},
151 {"rs_allocation", DataTypeRSAllocation},
152 {"rs_sampler", DataTypeRSSampler},
153 {"rs_script", DataTypeRSScript},
154 {"rs_mesh", DataTypeRSMesh},
155 {"rs_path", DataTypeRSPath},
156 {"rs_program_fragment", DataTypeRSProgramFragment},
157 {"rs_program_vertex", DataTypeRSProgramVertex},
158 {"rs_program_raster", DataTypeRSProgramRaster},
159 {"rs_program_store", DataTypeRSProgramStore},
160 {"rs_font", DataTypeRSFont},
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700161};
162
163const int MatrixAndObjectDataTypesCount =
164 sizeof(MatrixAndObjectDataTypes) / sizeof(MatrixAndObjectDataTypes[0]);
165
Stephen Hines24e79f62010-12-13 12:55:57 -0800166static const clang::Type *TypeExportableHelper(
167 const clang::Type *T,
168 llvm::SmallPtrSet<const clang::Type*, 8>& SPS,
Stephen Hines48d893d2013-12-06 18:00:27 -0800169 slang::RSContext *Context,
Stephen Hines24e79f62010-12-13 12:55:57 -0800170 const clang::VarDecl *VD,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700171 const clang::RecordDecl *TopLevelRecord);
Stephen Hines24e79f62010-12-13 12:55:57 -0800172
Tim Murrayee4016d2014-04-10 15:49:08 -0700173template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800174static void ReportTypeError(slang::RSContext *Context,
Stephen Hines11274a72012-09-26 19:14:20 -0700175 const clang::NamedDecl *ND,
Stephen Hines24e79f62010-12-13 12:55:57 -0800176 const clang::RecordDecl *TopLevelRecord,
Tim Murrayee4016d2014-04-10 15:49:08 -0700177 const char (&Message)[N],
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700178 unsigned int TargetAPI = 0) {
Stephen Hines24e79f62010-12-13 12:55:57 -0800179 // Attempt to use the type declaration first (if we have one).
180 // Fall back to the variable definition, if we are looking at something
181 // like an array declaration that can't be exported.
182 if (TopLevelRecord) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800183 Context->ReportError(TopLevelRecord->getLocation(), Message)
184 << TopLevelRecord->getName() << TargetAPI;
Stephen Hines11274a72012-09-26 19:14:20 -0700185 } else if (ND) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800186 Context->ReportError(ND->getLocation(), Message) << ND->getName()
187 << TargetAPI;
Stephen Hines24e79f62010-12-13 12:55:57 -0800188 } else {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800189 slangAssert(false && "Variables should be validated before exporting");
Stephen Hines24e79f62010-12-13 12:55:57 -0800190 }
Stephen Hines24e79f62010-12-13 12:55:57 -0800191}
192
193static const clang::Type *ConstantArrayTypeExportableHelper(
194 const clang::ConstantArrayType *CAT,
195 llvm::SmallPtrSet<const clang::Type*, 8>& SPS,
Stephen Hines48d893d2013-12-06 18:00:27 -0800196 slang::RSContext *Context,
Stephen Hines24e79f62010-12-13 12:55:57 -0800197 const clang::VarDecl *VD,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700198 const clang::RecordDecl *TopLevelRecord) {
Stephen Hines24e79f62010-12-13 12:55:57 -0800199 // Check element type
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700200 const clang::Type *ElementType = GetConstantArrayElementType(CAT);
Stephen Hines24e79f62010-12-13 12:55:57 -0800201 if (ElementType->isArrayType()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800202 ReportTypeError(Context, VD, TopLevelRecord,
Logan Chien9207a2e2011-10-21 15:39:28 +0800203 "multidimensional arrays cannot be exported: '%0'");
Stephen Hines24e79f62010-12-13 12:55:57 -0800204 return NULL;
205 } else if (ElementType->isExtVectorType()) {
206 const clang::ExtVectorType *EVT =
207 static_cast<const clang::ExtVectorType*>(ElementType);
208 unsigned numElements = EVT->getNumElements();
209
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700210 const clang::Type *BaseElementType = GetExtVectorElementType(EVT);
Stephen Hines24e79f62010-12-13 12:55:57 -0800211 if (!RSExportPrimitiveType::IsPrimitiveType(BaseElementType)) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800212 ReportTypeError(Context, VD, TopLevelRecord,
Logan Chien9207a2e2011-10-21 15:39:28 +0800213 "vectors of non-primitive types cannot be exported: '%0'");
Stephen Hines24e79f62010-12-13 12:55:57 -0800214 return NULL;
215 }
216
217 if (numElements == 3 && CAT->getSize() != 1) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800218 ReportTypeError(Context, VD, TopLevelRecord,
Logan Chien9207a2e2011-10-21 15:39:28 +0800219 "arrays of width 3 vector types cannot be exported: '%0'");
Stephen Hines24e79f62010-12-13 12:55:57 -0800220 return NULL;
221 }
222 }
223
Stephen Hines48d893d2013-12-06 18:00:27 -0800224 if (TypeExportableHelper(ElementType, SPS, Context, VD,
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700225 TopLevelRecord) == NULL) {
Stephen Hines24e79f62010-12-13 12:55:57 -0800226 return NULL;
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700227 } else {
Stephen Hines24e79f62010-12-13 12:55:57 -0800228 return CAT;
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700229 }
Stephen Hines24e79f62010-12-13 12:55:57 -0800230}
231
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700232BuiltinInfo *FindBuiltinType(clang::BuiltinType::Kind builtinTypeKind) {
233 for (int i = 0; i < BuiltinInfoTableCount; i++) {
234 if (builtinTypeKind == BuiltinInfoTable[i].builtinTypeKind) {
235 return &BuiltinInfoTable[i];
236 }
237 }
238 return NULL;
239}
240
Stephen Hines24e79f62010-12-13 12:55:57 -0800241static const clang::Type *TypeExportableHelper(
Stephen Hinese67239d2012-02-24 15:08:36 -0800242 clang::Type const *T,
243 llvm::SmallPtrSet<clang::Type const *, 8> &SPS,
Stephen Hines48d893d2013-12-06 18:00:27 -0800244 slang::RSContext *Context,
Stephen Hinese67239d2012-02-24 15:08:36 -0800245 clang::VarDecl const *VD,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700246 clang::RecordDecl const *TopLevelRecord) {
Stephen Hinese5e64432010-12-02 18:48:20 -0800247 // Normalize first
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700248 if ((T = GetCanonicalType(T)) == NULL)
Stephen Hinese5e64432010-12-02 18:48:20 -0800249 return NULL;
250
251 if (SPS.count(T))
252 return T;
253
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700254 const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr();
255
Stephen Hinese5e64432010-12-02 18:48:20 -0800256 switch (T->getTypeClass()) {
257 case clang::Type::Builtin: {
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700258 const clang::BuiltinType *BT = static_cast<const clang::BuiltinType*>(CTI);
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700259 return FindBuiltinType(BT->getKind()) == NULL ? NULL : T;
Stephen Hinese5e64432010-12-02 18:48:20 -0800260 }
261 case clang::Type::Record: {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700262 if (RSExportPrimitiveType::GetRSSpecificType(T) != DataTypeUnknown) {
Stephen Hinese5e64432010-12-02 18:48:20 -0800263 return T; // RS object type, no further checks are needed
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700264 }
Stephen Hinese5e64432010-12-02 18:48:20 -0800265
266 // Check internal struct
Stephen Hinescd440ee2010-12-06 14:57:04 -0800267 if (T->isUnionType()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800268 ReportTypeError(Context, VD, T->getAsUnionType()->getDecl(),
Logan Chien9207a2e2011-10-21 15:39:28 +0800269 "unions cannot be exported: '%0'");
Stephen Hinescd440ee2010-12-06 14:57:04 -0800270 return NULL;
271 } else if (!T->isStructureType()) {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800272 slangAssert(false && "Unknown type cannot be exported");
Stephen Hinescd440ee2010-12-06 14:57:04 -0800273 return NULL;
274 }
275
Stephen Hines24e79f62010-12-13 12:55:57 -0800276 clang::RecordDecl *RD = T->getAsStructureType()->getDecl();
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800277 if (RD != NULL) {
Stephen Hinese5e64432010-12-02 18:48:20 -0800278 RD = RD->getDefinition();
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800279 if (RD == NULL) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800280 ReportTypeError(Context, NULL, T->getAsStructureType()->getDecl(),
Logan Chien9207a2e2011-10-21 15:39:28 +0800281 "struct is not defined in this module");
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800282 return NULL;
283 }
284 }
Stephen Hinese5e64432010-12-02 18:48:20 -0800285
286 if (!TopLevelRecord) {
287 TopLevelRecord = RD;
288 }
289 if (RD->getName().empty()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800290 ReportTypeError(Context, NULL, RD,
Logan Chien9207a2e2011-10-21 15:39:28 +0800291 "anonymous structures cannot be exported");
Stephen Hinese5e64432010-12-02 18:48:20 -0800292 return NULL;
293 }
294
295 // Fast check
296 if (RD->hasFlexibleArrayMember() || RD->hasObjectMember())
297 return NULL;
298
299 // Insert myself into checking set
300 SPS.insert(T);
301
302 // Check all element
303 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
304 FE = RD->field_end();
305 FI != FE;
306 FI++) {
307 const clang::FieldDecl *FD = *FI;
308 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700309 FT = GetCanonicalType(FT);
Stephen Hinese5e64432010-12-02 18:48:20 -0800310
Stephen Hines48d893d2013-12-06 18:00:27 -0800311 if (!TypeExportableHelper(FT, SPS, Context, VD, TopLevelRecord)) {
Stephen Hinese5e64432010-12-02 18:48:20 -0800312 return NULL;
313 }
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800314
315 // We don't support bit fields yet
316 //
317 // TODO(zonr/srhines): allow bit fields of size 8, 16, 32
318 if (FD->isBitField()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800319 Context->ReportError(
320 FD->getLocation(),
321 "bit fields are not able to be exported: '%0.%1'")
322 << RD->getName() << FD->getName();
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800323 return NULL;
324 }
Stephen Hinese5e64432010-12-02 18:48:20 -0800325 }
326
327 return T;
328 }
329 case clang::Type::Pointer: {
330 if (TopLevelRecord) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800331 ReportTypeError(Context, VD, TopLevelRecord,
Stephen Hinesaa82e742012-04-11 12:38:17 -0700332 "structures containing pointers cannot be exported: '%0'");
Stephen Hinese5e64432010-12-02 18:48:20 -0800333 return NULL;
334 }
Stephen Hines24e79f62010-12-13 12:55:57 -0800335
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700336 const clang::PointerType *PT = static_cast<const clang::PointerType*>(CTI);
337 const clang::Type *PointeeType = GetPointeeType(PT);
Stephen Hinese5e64432010-12-02 18:48:20 -0800338
Stephen Hinesaa82e742012-04-11 12:38:17 -0700339 if (PointeeType->getTypeClass() == clang::Type::Pointer) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800340 ReportTypeError(Context, VD, TopLevelRecord,
Stephen Hinesaa82e742012-04-11 12:38:17 -0700341 "multiple levels of pointers cannot be exported: '%0'");
342 return NULL;
343 }
Stephen Hinese5e64432010-12-02 18:48:20 -0800344 // We don't support pointer with array-type pointee or unsupported pointee
345 // type
346 if (PointeeType->isArrayType() ||
Stephen Hines48d893d2013-12-06 18:00:27 -0800347 (TypeExportableHelper(PointeeType, SPS, Context, VD,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700348 TopLevelRecord) == NULL))
Stephen Hinese5e64432010-12-02 18:48:20 -0800349 return NULL;
350 else
351 return T;
352 }
353 case clang::Type::ExtVector: {
354 const clang::ExtVectorType *EVT =
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700355 static_cast<const clang::ExtVectorType*>(CTI);
Stephen Hinese5e64432010-12-02 18:48:20 -0800356 // Only vector with size 2, 3 and 4 are supported.
357 if (EVT->getNumElements() < 2 || EVT->getNumElements() > 4)
358 return NULL;
359
360 // Check base element type
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700361 const clang::Type *ElementType = GetExtVectorElementType(EVT);
Stephen Hinese5e64432010-12-02 18:48:20 -0800362
363 if ((ElementType->getTypeClass() != clang::Type::Builtin) ||
Stephen Hines48d893d2013-12-06 18:00:27 -0800364 (TypeExportableHelper(ElementType, SPS, Context, VD,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700365 TopLevelRecord) == NULL))
Stephen Hinese5e64432010-12-02 18:48:20 -0800366 return NULL;
367 else
368 return T;
369 }
370 case clang::Type::ConstantArray: {
371 const clang::ConstantArrayType *CAT =
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700372 static_cast<const clang::ConstantArrayType*>(CTI);
Stephen Hinese5e64432010-12-02 18:48:20 -0800373
Stephen Hines48d893d2013-12-06 18:00:27 -0800374 return ConstantArrayTypeExportableHelper(CAT, SPS, Context, VD,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700375 TopLevelRecord);
Stephen Hinese5e64432010-12-02 18:48:20 -0800376 }
Stephen Hines48d893d2013-12-06 18:00:27 -0800377 case clang::Type::Enum: {
378 // FIXME: We currently convert enums to integers, rather than reflecting
379 // a more complete (and nicer type-safe Java version).
380 return Context->getASTContext().IntTy.getTypePtr();
381 }
Stephen Hinese5e64432010-12-02 18:48:20 -0800382 default: {
Stephen Hines48d893d2013-12-06 18:00:27 -0800383 slangAssert(false && "Unknown type cannot be validated");
Stephen Hinese5e64432010-12-02 18:48:20 -0800384 return NULL;
385 }
386 }
387}
388
389// Return the type that can be used to create RSExportType, will always return
390// the canonical type
Logan Chien9207a2e2011-10-21 15:39:28 +0800391// If the Type T is not exportable, this function returns NULL. DiagEngine is
Stephen Hines78e69cb2011-04-22 15:03:19 -0700392// used to generate proper Clang diagnostic messages when a
Stephen Hinese5e64432010-12-02 18:48:20 -0800393// non-exportable type is detected. TopLevelRecord is used to capture the
394// highest struct (in the case of a nested hierarchy) for detecting other
395// types that cannot be exported (mostly pointers within a struct).
396static const clang::Type *TypeExportable(const clang::Type *T,
Stephen Hines48d893d2013-12-06 18:00:27 -0800397 slang::RSContext *Context,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700398 const clang::VarDecl *VD) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700399 llvm::SmallPtrSet<const clang::Type*, 8> SPS =
400 llvm::SmallPtrSet<const clang::Type*, 8>();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700401
Stephen Hines48d893d2013-12-06 18:00:27 -0800402 return TypeExportableHelper(T, SPS, Context, VD, NULL);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700403}
404
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800405static bool ValidateRSObjectInVarDecl(slang::RSContext *Context,
406 clang::VarDecl *VD, bool InCompositeType,
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700407 unsigned int TargetAPI) {
408 if (TargetAPI < SLANG_JB_TARGET_API) {
409 // Only if we are already in a composite type (like an array or structure).
410 if (InCompositeType) {
411 // Only if we are actually exported (i.e. non-static).
Stephen Hines44f10062013-06-13 00:33:23 -0700412 if (VD->hasLinkage() &&
413 (VD->getFormalLinkage() == clang::ExternalLinkage)) {
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700414 // Only if we are not a pointer to an object.
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700415 const clang::Type *T = GetCanonicalType(VD->getType().getTypePtr());
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700416 if (T->getTypeClass() != clang::Type::Pointer) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800417 ReportTypeError(Context, VD, NULL,
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700418 "arrays/structures containing RS object types "
419 "cannot be exported in target API < %1: '%0'",
420 SLANG_JB_TARGET_API);
421 return false;
422 }
423 }
424 }
425 }
426
427 return true;
428}
429
Stephen Hines11274a72012-09-26 19:14:20 -0700430// Helper function for ValidateType(). We do a recursive descent on the
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700431// type hierarchy to ensure that we can properly export/handle the
432// declaration.
433// \return true if the variable declaration is valid,
434// false if it is invalid (along with proper diagnostics).
435//
Stephen Hines11274a72012-09-26 19:14:20 -0700436// C - ASTContext (for diagnostics + builtin types).
437// T - sub-type that we are validating.
438// ND - (optional) top-level named declaration that we are validating.
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700439// SPS - set of types we have already seen/validated.
440// InCompositeType - true if we are within an outer composite type.
441// UnionDecl - set if we are in a sub-type of a union.
442// TargetAPI - target SDK API level.
Stephen Hines11274a72012-09-26 19:14:20 -0700443// IsFilterscript - whether or not we are compiling for Filterscript
444static bool ValidateTypeHelper(
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800445 slang::RSContext *Context,
Stephen Hines11274a72012-09-26 19:14:20 -0700446 clang::ASTContext &C,
Stephen Hines78e69cb2011-04-22 15:03:19 -0700447 const clang::Type *&T,
Stephen Hines11274a72012-09-26 19:14:20 -0700448 clang::NamedDecl *ND,
449 clang::SourceLocation Loc,
Stephen Hines78e69cb2011-04-22 15:03:19 -0700450 llvm::SmallPtrSet<const clang::Type*, 8>& SPS,
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700451 bool InCompositeType,
452 clang::RecordDecl *UnionDecl,
Stephen Hines11274a72012-09-26 19:14:20 -0700453 unsigned int TargetAPI,
454 bool IsFilterscript) {
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700455 if ((T = GetCanonicalType(T)) == NULL)
Stephen Hines78e69cb2011-04-22 15:03:19 -0700456 return true;
457
458 if (SPS.count(T))
459 return true;
460
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700461 const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr();
462
Stephen Hines78e69cb2011-04-22 15:03:19 -0700463 switch (T->getTypeClass()) {
464 case clang::Type::Record: {
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700465 if (RSExportPrimitiveType::IsRSObjectType(T)) {
Stephen Hines11274a72012-09-26 19:14:20 -0700466 clang::VarDecl *VD = (ND ? llvm::dyn_cast<clang::VarDecl>(ND) : NULL);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800467 if (VD && !ValidateRSObjectInVarDecl(Context, VD, InCompositeType,
468 TargetAPI)) {
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700469 return false;
470 }
471 }
472
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700473 if (RSExportPrimitiveType::GetRSSpecificType(T) != DataTypeUnknown) {
Stephen Hines78e69cb2011-04-22 15:03:19 -0700474 if (!UnionDecl) {
475 return true;
476 } else if (RSExportPrimitiveType::IsRSObjectType(T)) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800477 ReportTypeError(Context, NULL, UnionDecl,
Stephen Hines78e69cb2011-04-22 15:03:19 -0700478 "unions containing RS object types are not allowed");
479 return false;
480 }
481 }
482
483 clang::RecordDecl *RD = NULL;
484
485 // Check internal struct
486 if (T->isUnionType()) {
487 RD = T->getAsUnionType()->getDecl();
488 UnionDecl = RD;
489 } else if (T->isStructureType()) {
490 RD = T->getAsStructureType()->getDecl();
491 } else {
492 slangAssert(false && "Unknown type cannot be exported");
493 return false;
494 }
495
496 if (RD != NULL) {
497 RD = RD->getDefinition();
498 if (RD == NULL) {
499 // FIXME
500 return true;
501 }
502 }
503
504 // Fast check
505 if (RD->hasFlexibleArrayMember() || RD->hasObjectMember())
506 return false;
507
508 // Insert myself into checking set
509 SPS.insert(T);
510
511 // Check all elements
512 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
513 FE = RD->field_end();
514 FI != FE;
515 FI++) {
516 const clang::FieldDecl *FD = *FI;
517 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700518 FT = GetCanonicalType(FT);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700519
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800520 if (!ValidateTypeHelper(Context, C, FT, ND, Loc, SPS, true, UnionDecl,
Stephen Hines11274a72012-09-26 19:14:20 -0700521 TargetAPI, IsFilterscript)) {
Stephen Hines78e69cb2011-04-22 15:03:19 -0700522 return false;
523 }
524 }
525
526 return true;
527 }
528
529 case clang::Type::Builtin: {
Stephen Hines11274a72012-09-26 19:14:20 -0700530 if (IsFilterscript) {
531 clang::QualType QT = T->getCanonicalTypeInternal();
532 if (QT == C.DoubleTy ||
533 QT == C.LongDoubleTy ||
534 QT == C.LongTy ||
535 QT == C.LongLongTy) {
Stephen Hines11274a72012-09-26 19:14:20 -0700536 if (ND) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800537 Context->ReportError(
538 Loc,
Stephen Hines11274a72012-09-26 19:14:20 -0700539 "Builtin types > 32 bits in size are forbidden in "
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800540 "Filterscript: '%0'")
541 << ND->getName();
Stephen Hines11274a72012-09-26 19:14:20 -0700542 } else {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800543 Context->ReportError(
544 Loc,
Stephen Hines11274a72012-09-26 19:14:20 -0700545 "Builtin types > 32 bits in size are forbidden in "
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800546 "Filterscript");
Stephen Hines11274a72012-09-26 19:14:20 -0700547 }
548 return false;
549 }
550 }
Stephen Hines78e69cb2011-04-22 15:03:19 -0700551 break;
552 }
553
554 case clang::Type::Pointer: {
Stephen Hines11274a72012-09-26 19:14:20 -0700555 if (IsFilterscript) {
556 if (ND) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800557 Context->ReportError(Loc,
558 "Pointers are forbidden in Filterscript: '%0'")
559 << ND->getName();
Stephen Hines11274a72012-09-26 19:14:20 -0700560 return false;
561 } else {
562 // TODO(srhines): Find a better way to handle expressions (i.e. no
563 // NamedDecl) involving pointers in FS that should be allowed.
564 // An example would be calls to library functions like
565 // rsMatrixMultiply() that take rs_matrixNxN * types.
566 }
567 }
568
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700569 const clang::PointerType *PT = static_cast<const clang::PointerType*>(CTI);
570 const clang::Type *PointeeType = GetPointeeType(PT);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700571
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800572 return ValidateTypeHelper(Context, C, PointeeType, ND, Loc, SPS,
573 InCompositeType, UnionDecl, TargetAPI,
574 IsFilterscript);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700575 }
576
577 case clang::Type::ExtVector: {
578 const clang::ExtVectorType *EVT =
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700579 static_cast<const clang::ExtVectorType*>(CTI);
580 const clang::Type *ElementType = GetExtVectorElementType(EVT);
Stephen Hinesb13b85e2012-08-31 19:27:48 -0700581 if (TargetAPI < SLANG_ICS_TARGET_API &&
582 InCompositeType &&
Stephen Hinesfdae63e2012-11-28 14:42:44 -0800583 EVT->getNumElements() == 3 &&
584 ND &&
Stephen Hines44f10062013-06-13 00:33:23 -0700585 ND->getFormalLinkage() == clang::ExternalLinkage) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800586 ReportTypeError(Context, ND, NULL,
Stephen Hinesb13b85e2012-08-31 19:27:48 -0700587 "structs containing vectors of dimension 3 cannot "
588 "be exported at this API level: '%0'");
589 return false;
590 }
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800591 return ValidateTypeHelper(Context, C, ElementType, ND, Loc, SPS, true,
592 UnionDecl, TargetAPI, IsFilterscript);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700593 }
594
595 case clang::Type::ConstantArray: {
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700596 const clang::ConstantArrayType *CAT = static_cast<const clang::ConstantArrayType*>(CTI);
597 const clang::Type *ElementType = GetConstantArrayElementType(CAT);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800598 return ValidateTypeHelper(Context, C, ElementType, ND, Loc, SPS, true,
599 UnionDecl, TargetAPI, IsFilterscript);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700600 }
601
602 default: {
603 break;
604 }
605 }
606
607 return true;
Stephen Hinese5e64432010-12-02 18:48:20 -0800608}
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700609
Stephen Hinese5e64432010-12-02 18:48:20 -0800610} // namespace
611
Jean-Luc Brouilleteca05342014-05-15 12:44:21 -0700612std::string CreateDummyName(const char *type, const std::string &name) {
613 std::stringstream S;
614 S << "<" << type;
615 if (!name.empty()) {
616 S << ":" << name;
617 }
618 S << ">";
619 return S.str();
620}
621
Stephen Hinese5e64432010-12-02 18:48:20 -0800622/****************************** RSExportType ******************************/
623bool RSExportType::NormalizeType(const clang::Type *&T,
624 llvm::StringRef &TypeName,
Stephen Hines48d893d2013-12-06 18:00:27 -0800625 RSContext *Context,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700626 const clang::VarDecl *VD) {
Stephen Hines48d893d2013-12-06 18:00:27 -0800627 if ((T = TypeExportable(T, Context, VD)) == NULL) {
Stephen Hinese5e64432010-12-02 18:48:20 -0800628 return false;
629 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700630 // Get type name
631 TypeName = RSExportType::GetTypeName(T);
Stephen Hines48d893d2013-12-06 18:00:27 -0800632 if (Context && TypeName.empty()) {
Stephen Hines48d893d2013-12-06 18:00:27 -0800633 if (VD) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800634 Context->ReportError(VD->getLocation(),
635 "anonymous types cannot be exported");
Stephen Hines48d893d2013-12-06 18:00:27 -0800636 } else {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800637 Context->ReportError("anonymous types cannot be exported");
Stephen Hinese5e64432010-12-02 18:48:20 -0800638 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700639 return false;
Stephen Hinese5e64432010-12-02 18:48:20 -0800640 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700641
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700642 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700643}
644
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800645bool RSExportType::ValidateType(slang::RSContext *Context, clang::ASTContext &C,
646 clang::QualType QT, clang::NamedDecl *ND,
647 clang::SourceLocation Loc,
648 unsigned int TargetAPI, bool IsFilterscript) {
Stephen Hines11274a72012-09-26 19:14:20 -0700649 const clang::Type *T = QT.getTypePtr();
Stephen Hines78e69cb2011-04-22 15:03:19 -0700650 llvm::SmallPtrSet<const clang::Type*, 8> SPS =
651 llvm::SmallPtrSet<const clang::Type*, 8>();
652
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800653 return ValidateTypeHelper(Context, C, T, ND, Loc, SPS, false, NULL, TargetAPI,
Stephen Hines11274a72012-09-26 19:14:20 -0700654 IsFilterscript);
655 return true;
656}
657
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800658bool RSExportType::ValidateVarDecl(slang::RSContext *Context,
659 clang::VarDecl *VD, unsigned int TargetAPI,
Stephen Hines11274a72012-09-26 19:14:20 -0700660 bool IsFilterscript) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800661 return ValidateType(Context, VD->getASTContext(), VD->getType(), VD,
Stephen Hines11274a72012-09-26 19:14:20 -0700662 VD->getLocation(), TargetAPI, IsFilterscript);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700663}
664
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700665const clang::Type
666*RSExportType::GetTypeOfDecl(const clang::DeclaratorDecl *DD) {
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800667 if (DD) {
Stephen Hines8de19222013-08-23 16:30:59 -0700668 clang::QualType T = DD->getType();
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800669
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700670 if (T.isNull())
671 return NULL;
672 else
673 return T.getTypePtr();
674 }
675 return NULL;
676}
677
678llvm::StringRef RSExportType::GetTypeName(const clang::Type* T) {
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700679 T = GetCanonicalType(T);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700680 if (T == NULL)
681 return llvm::StringRef();
682
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700683 const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr();
684
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700685 switch (T->getTypeClass()) {
686 case clang::Type::Builtin: {
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700687 const clang::BuiltinType *BT = static_cast<const clang::BuiltinType*>(CTI);
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700688 BuiltinInfo *info = FindBuiltinType(BT->getKind());
689 if (info != NULL) {
690 return info->cname[0];
Zonr Changa65ec162010-10-17 01:53:05 +0800691 }
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700692 slangAssert(false && "Unknown data type of the builtin");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700693 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700694 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700695 case clang::Type::Record: {
Stephen Hinescd440ee2010-12-06 14:57:04 -0800696 clang::RecordDecl *RD;
697 if (T->isStructureType()) {
698 RD = T->getAsStructureType()->getDecl();
Stephen Hinesdd6206b2010-12-09 19:39:22 -0800699 } else {
Stephen Hinescd440ee2010-12-06 14:57:04 -0800700 break;
701 }
702
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700703 llvm::StringRef Name = RD->getName();
704 if (Name.empty()) {
Shih-wei Liao83f0c622011-06-21 05:34:53 -0700705 if (RD->getTypedefNameForAnonDecl() != NULL) {
706 Name = RD->getTypedefNameForAnonDecl()->getName();
707 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700708
Shih-wei Liao83f0c622011-06-21 05:34:53 -0700709 if (Name.empty()) {
710 // Try to find a name from redeclaration (i.e. typedef)
711 for (clang::TagDecl::redecl_iterator RI = RD->redecls_begin(),
712 RE = RD->redecls_end();
713 RI != RE;
714 RI++) {
715 slangAssert(*RI != NULL && "cannot be NULL object");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700716
Shih-wei Liao83f0c622011-06-21 05:34:53 -0700717 Name = (*RI)->getName();
718 if (!Name.empty())
719 break;
720 }
721 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700722 }
723 return Name;
724 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700725 case clang::Type::Pointer: {
726 // "*" plus pointee name
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700727 const clang::PointerType *P = static_cast<const clang::PointerType*>(CTI);
728 const clang::Type *PT = GetPointeeType(P);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700729 llvm::StringRef PointeeName;
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700730 if (NormalizeType(PT, PointeeName, NULL, NULL)) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700731 char *Name = new char[ 1 /* * */ + PointeeName.size() + 1 ];
732 Name[0] = '*';
733 memcpy(Name + 1, PointeeName.data(), PointeeName.size());
734 Name[PointeeName.size() + 1] = '\0';
735 return Name;
736 }
737 break;
738 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700739 case clang::Type::ExtVector: {
740 const clang::ExtVectorType *EVT =
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700741 static_cast<const clang::ExtVectorType*>(CTI);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700742 return RSExportVectorType::GetTypeName(EVT);
743 break;
744 }
Zonr Chang2e1dba62010-10-05 22:20:11 +0800745 case clang::Type::ConstantArray : {
746 // Construct name for a constant array is too complicated.
Stephen Hines340b5552014-09-05 02:03:36 -0700747 return "<ConstantArray>";
Zonr Chang2e1dba62010-10-05 22:20:11 +0800748 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700749 default: {
750 break;
751 }
752 }
753
754 return llvm::StringRef();
755}
756
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700757
758RSExportType *RSExportType::Create(RSContext *Context,
759 const clang::Type *T,
760 const llvm::StringRef &TypeName) {
761 // Lookup the context to see whether the type was processed before.
762 // Newly created RSExportType will insert into context
763 // in RSExportType::RSExportType()
764 RSContext::export_type_iterator ETI = Context->findExportType(TypeName);
765
766 if (ETI != Context->export_types_end())
767 return ETI->second;
768
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700769 const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr();
770
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700771 RSExportType *ET = NULL;
772 switch (T->getTypeClass()) {
773 case clang::Type::Record: {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700774 DataType dt = RSExportPrimitiveType::GetRSSpecificType(TypeName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700775 switch (dt) {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700776 case DataTypeUnknown: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800777 // User-defined types
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700778 ET = RSExportRecordType::Create(Context,
779 T->getAsStructureType(),
780 TypeName);
781 break;
782 }
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700783 case DataTypeRSMatrix2x2: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800784 // 2 x 2 Matrix type
785 ET = RSExportMatrixType::Create(Context,
786 T->getAsStructureType(),
787 TypeName,
788 2);
789 break;
790 }
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700791 case DataTypeRSMatrix3x3: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800792 // 3 x 3 Matrix type
793 ET = RSExportMatrixType::Create(Context,
794 T->getAsStructureType(),
795 TypeName,
796 3);
797 break;
798 }
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700799 case DataTypeRSMatrix4x4: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800800 // 4 x 4 Matrix type
801 ET = RSExportMatrixType::Create(Context,
802 T->getAsStructureType(),
803 TypeName,
804 4);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700805 break;
806 }
807 default: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800808 // Others are primitive types
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700809 ET = RSExportPrimitiveType::Create(Context, T, TypeName);
810 break;
811 }
812 }
813 break;
814 }
815 case clang::Type::Builtin: {
816 ET = RSExportPrimitiveType::Create(Context, T, TypeName);
817 break;
818 }
819 case clang::Type::Pointer: {
820 ET = RSExportPointerType::Create(Context,
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700821 static_cast<const clang::PointerType*>(CTI),
822 TypeName);
Zonr Chang92b344a2010-10-05 20:39:03 +0800823 // FIXME: free the name (allocated in RSExportType::GetTypeName)
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700824 delete [] TypeName.data();
825 break;
826 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700827 case clang::Type::ExtVector: {
828 ET = RSExportVectorType::Create(Context,
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700829 static_cast<const clang::ExtVectorType*>(CTI),
830 TypeName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700831 break;
832 }
Zonr Chang2e1dba62010-10-05 22:20:11 +0800833 case clang::Type::ConstantArray: {
834 ET = RSExportConstantArrayType::Create(
835 Context,
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700836 static_cast<const clang::ConstantArrayType*>(CTI));
Zonr Chang2e1dba62010-10-05 22:20:11 +0800837 break;
838 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700839 default: {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800840 Context->ReportError("unknown type cannot be exported: '%0'")
841 << T->getTypeClassName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700842 break;
843 }
844 }
845
846 return ET;
847}
848
zonr6315f762010-10-05 15:35:14 +0800849RSExportType *RSExportType::Create(RSContext *Context, const clang::Type *T) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700850 llvm::StringRef TypeName;
Stephen Hines48d893d2013-12-06 18:00:27 -0800851 if (NormalizeType(T, TypeName, Context, NULL)) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700852 return Create(Context, T, TypeName);
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700853 } else {
Shih-wei Liaocecd11d2010-09-21 08:07:58 -0700854 return NULL;
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700855 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700856}
857
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700858RSExportType *RSExportType::CreateFromDecl(RSContext *Context,
859 const clang::VarDecl *VD) {
860 return RSExportType::Create(Context, GetTypeOfDecl(VD));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700861}
862
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700863size_t RSExportType::getStoreSize() const {
864 return getRSContext()->getDataLayout()->getTypeStoreSize(getLLVMType());
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700865}
866
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700867size_t RSExportType::getAllocSize() const {
868 return getRSContext()->getDataLayout()->getTypeAllocSize(getLLVMType());
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700869}
Shih-wei Liao1f0d88f2010-06-25 00:15:24 -0700870
Zonr Chang6b6320a2010-10-05 22:42:01 +0800871RSExportType::RSExportType(RSContext *Context,
872 ExportClass Class,
873 const llvm::StringRef &Name)
Zonr Changa41ce1d2010-10-06 02:23:12 +0800874 : RSExportable(Context, RSExportable::EX_TYPE),
Zonr Chang6b6320a2010-10-05 22:42:01 +0800875 mClass(Class),
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800876 // Make a copy on Name since memory stored @Name is either allocated in
877 // ASTContext or allocated in GetTypeName which will be destroyed later.
zonr6315f762010-10-05 15:35:14 +0800878 mName(Name.data(), Name.size()),
Jean-Luc Brouillet23544422014-05-14 15:12:25 -0700879 mLLVMType(NULL) {
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800880 // Don't cache the type whose name start with '<'. Those type failed to
881 // get their name since constructing their name in GetTypeName() requiring
882 // complicated work.
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700883 if (!IsDummyName(Name)) {
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800884 // TODO(zonr): Need to check whether the insertion is successful or not.
885 Context->insertExportType(llvm::StringRef(Name), this);
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700886 }
887
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700888}
889
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800890bool RSExportType::keep() {
891 if (!RSExportable::keep())
892 return false;
Zonr Chang641558f2010-10-12 21:07:06 +0800893 // Invalidate converted LLVM type.
894 mLLVMType = NULL;
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800895 return true;
Zonr Chang641558f2010-10-12 21:07:06 +0800896}
897
898bool RSExportType::equals(const RSExportable *E) const {
899 CHECK_PARENT_EQUALITY(RSExportable, E);
900 return (static_cast<const RSExportType*>(E)->getClass() == getClass());
901}
902
Zonr Changa7a828d2010-10-22 03:44:27 +0800903RSExportType::~RSExportType() {
Zonr Changa7a828d2010-10-22 03:44:27 +0800904}
905
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700906/************************** RSExportPrimitiveType **************************/
Zonr Changb1771ef2010-10-22 18:03:46 +0800907llvm::ManagedStatic<RSExportPrimitiveType::RSSpecificTypeMapTy>
908RSExportPrimitiveType::RSSpecificTypeMap;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700909
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700910bool RSExportPrimitiveType::IsPrimitiveType(const clang::Type *T) {
911 if ((T != NULL) && (T->getTypeClass() == clang::Type::Builtin))
912 return true;
913 else
914 return false;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700915}
916
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700917DataType
Zonr Changb1771ef2010-10-22 18:03:46 +0800918RSExportPrimitiveType::GetRSSpecificType(const llvm::StringRef &TypeName) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700919 if (TypeName.empty())
920 return DataTypeUnknown;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700921
Zonr Changb1771ef2010-10-22 18:03:46 +0800922 if (RSSpecificTypeMap->empty()) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700923 for (int i = 0; i < MatrixAndObjectDataTypesCount; i++) {
924 RSSpecificTypeMap->GetOrCreateValue(MatrixAndObjectDataTypes[i].name,
925 MatrixAndObjectDataTypes[i].dataType);
926 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700927 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700928
Zonr Changb1771ef2010-10-22 18:03:46 +0800929 RSSpecificTypeMapTy::const_iterator I = RSSpecificTypeMap->find(TypeName);
930 if (I == RSSpecificTypeMap->end())
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700931 return DataTypeUnknown;
932 else
933 return I->getValue();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700934}
935
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700936DataType RSExportPrimitiveType::GetRSSpecificType(const clang::Type *T) {
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700937 T = GetCanonicalType(T);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700938 if ((T == NULL) || (T->getTypeClass() != clang::Type::Record))
939 return DataTypeUnknown;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700940
Zonr Changb1771ef2010-10-22 18:03:46 +0800941 return GetRSSpecificType( RSExportType::GetTypeName(T) );
942}
943
944bool RSExportPrimitiveType::IsRSMatrixType(DataType DT) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700945 if (DT < 0 || DT >= DataTypeMax) {
946 return false;
947 }
948 return gReflectionTypes[DT].category == MatrixDataType;
Zonr Changb1771ef2010-10-22 18:03:46 +0800949}
950
951bool RSExportPrimitiveType::IsRSObjectType(DataType DT) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700952 if (DT < 0 || DT >= DataTypeMax) {
953 return false;
954 }
955 return gReflectionTypes[DT].category == ObjectDataType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700956}
957
Stephen Hinesfeaca062011-02-04 14:08:13 -0800958bool RSExportPrimitiveType::IsStructureTypeWithRSObject(const clang::Type *T) {
959 bool RSObjectTypeSeen = false;
960 while (T && T->isArrayType()) {
961 T = T->getArrayElementTypeNoTypeQual();
962 }
963
964 const clang::RecordType *RT = T->getAsStructureType();
965 if (!RT) {
966 return false;
967 }
Stephen Hinesb0fabe52013-01-07 19:06:09 -0800968
Stephen Hinesfeaca062011-02-04 14:08:13 -0800969 const clang::RecordDecl *RD = RT->getDecl();
Stephen Hinesb0fabe52013-01-07 19:06:09 -0800970 if (RD) {
971 RD = RD->getDefinition();
972 }
973 if (!RD) {
974 return false;
975 }
976
Stephen Hinesfeaca062011-02-04 14:08:13 -0800977 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
978 FE = RD->field_end();
979 FI != FE;
980 FI++) {
981 // We just look through all field declarations to see if we find a
982 // declaration for an RS object type (or an array of one).
983 const clang::FieldDecl *FD = *FI;
984 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
985 while (FT && FT->isArrayType()) {
986 FT = FT->getArrayElementTypeNoTypeQual();
987 }
988
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700989 DataType DT = GetRSSpecificType(FT);
Stephen Hinesfeaca062011-02-04 14:08:13 -0800990 if (IsRSObjectType(DT)) {
991 // RS object types definitely need to be zero-initialized
992 RSObjectTypeSeen = true;
993 } else {
994 switch (DT) {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700995 case DataTypeRSMatrix2x2:
996 case DataTypeRSMatrix3x3:
997 case DataTypeRSMatrix4x4:
Stephen Hinesfeaca062011-02-04 14:08:13 -0800998 // Matrix types should get zero-initialized as well
999 RSObjectTypeSeen = true;
1000 break;
1001 default:
1002 // Ignore all other primitive types
1003 break;
1004 }
1005 while (FT && FT->isArrayType()) {
1006 FT = FT->getArrayElementTypeNoTypeQual();
1007 }
1008 if (FT->isStructureType()) {
1009 // Recursively handle structs of structs (even though these can't
1010 // be exported, it is possible for a user to have them internally).
1011 RSObjectTypeSeen |= IsStructureTypeWithRSObject(FT);
1012 }
1013 }
1014 }
1015
1016 return RSObjectTypeSeen;
1017}
1018
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001019size_t RSExportPrimitiveType::GetSizeInBits(const RSExportPrimitiveType *EPT) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001020 int type = EPT->getType();
1021 slangAssert((type > DataTypeUnknown && type < DataTypeMax) &&
Stephen Hines6e6578a2011-02-07 18:05:48 -08001022 "RSExportPrimitiveType::GetSizeInBits : unknown data type");
Stephen Hines9ae18b22014-06-10 23:53:00 -07001023 // All RS object types are 256 bits in 64-bit RS.
1024 if (EPT->isRSObjectType() && EPT->getRSContext()->is64Bit()) {
1025 return 256;
1026 }
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001027 return gReflectionTypes[type].size_in_bits;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001028}
1029
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001030DataType
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001031RSExportPrimitiveType::GetDataType(RSContext *Context, const clang::Type *T) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001032 if (T == NULL)
1033 return DataTypeUnknown;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001034
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001035 switch (T->getTypeClass()) {
1036 case clang::Type::Builtin: {
Loganbe274822011-02-16 22:02:54 +08001037 const clang::BuiltinType *BT =
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -07001038 static_cast<const clang::BuiltinType*>(T->getCanonicalTypeInternal().getTypePtr());
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001039 BuiltinInfo *info = FindBuiltinType(BT->getKind());
1040 if (info != NULL) {
1041 return info->type;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001042 }
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001043 // The size of type WChar depend on platform so we abandon the support
1044 // to them.
1045 Context->ReportError("built-in type cannot be exported: '%0'")
1046 << T->getTypeClassName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001047 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001048 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001049 case clang::Type::Record: {
1050 // must be RS object type
Zonr Changb1771ef2010-10-22 18:03:46 +08001051 return RSExportPrimitiveType::GetRSSpecificType(T);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001052 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001053 default: {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001054 Context->ReportError("primitive type cannot be exported: '%0'")
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001055 << T->getTypeClassName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001056 break;
1057 }
1058 }
1059
1060 return DataTypeUnknown;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001061}
1062
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001063RSExportPrimitiveType
1064*RSExportPrimitiveType::Create(RSContext *Context,
1065 const clang::Type *T,
1066 const llvm::StringRef &TypeName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001067 bool Normalized) {
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001068 DataType DT = GetDataType(Context, T);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001069
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001070 if ((DT == DataTypeUnknown) || TypeName.empty())
1071 return NULL;
1072 else
Zonr Chang6b6320a2010-10-05 22:42:01 +08001073 return new RSExportPrimitiveType(Context, ExportClassPrimitive, TypeName,
Stephen Hines2b8fb642012-03-09 00:12:47 -08001074 DT, Normalized);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001075}
1076
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001077RSExportPrimitiveType *RSExportPrimitiveType::Create(RSContext *Context,
Stephen Hines2b8fb642012-03-09 00:12:47 -08001078 const clang::Type *T) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001079 llvm::StringRef TypeName;
Stephen Hines48d893d2013-12-06 18:00:27 -08001080 if (RSExportType::NormalizeType(T, TypeName, Context, NULL)
Stephen Hines78e69cb2011-04-22 15:03:19 -07001081 && IsPrimitiveType(T)) {
Stephen Hines2b8fb642012-03-09 00:12:47 -08001082 return Create(Context, T, TypeName);
Stephen Hinese5e64432010-12-02 18:48:20 -08001083 } else {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001084 return NULL;
Stephen Hinese5e64432010-12-02 18:48:20 -08001085 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001086}
1087
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001088llvm::Type *RSExportPrimitiveType::convertToLLVMType() const {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001089 llvm::LLVMContext &C = getRSContext()->getLLVMContext();
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001090
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001091 if (isRSObjectType()) {
1092 // struct {
1093 // int *p;
1094 // } __attribute__((packed, aligned(pointer_size)))
1095 //
1096 // which is
1097 //
1098 // <{ [1 x i32] }> in LLVM
1099 //
Tim Murrayd56a92f2014-08-05 13:27:12 -07001100 std::vector<llvm::Type *> Elements;
1101 if (getRSContext()->is64Bit()) {
1102 // 64-bit path
1103 Elements.push_back(llvm::ArrayType::get(llvm::Type::getInt64Ty(C), 4));
1104 return llvm::StructType::get(C, Elements, true);
1105 } else {
1106 // 32-bit legacy path
1107 Elements.push_back(llvm::ArrayType::get(llvm::Type::getInt32Ty(C), 1));
1108 return llvm::StructType::get(C, Elements, true);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001109 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001110 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001111
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001112 switch (mType) {
1113 case DataTypeFloat32: {
1114 return llvm::Type::getFloatTy(C);
1115 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001116 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001117 case DataTypeFloat64: {
1118 return llvm::Type::getDoubleTy(C);
1119 break;
1120 }
Shih-wei Liao91a37832010-10-03 19:11:51 -07001121 case DataTypeBoolean: {
1122 return llvm::Type::getInt1Ty(C);
1123 break;
1124 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001125 case DataTypeSigned8:
1126 case DataTypeUnsigned8: {
1127 return llvm::Type::getInt8Ty(C);
1128 break;
1129 }
1130 case DataTypeSigned16:
1131 case DataTypeUnsigned16:
1132 case DataTypeUnsigned565:
1133 case DataTypeUnsigned5551:
1134 case DataTypeUnsigned4444: {
1135 return llvm::Type::getInt16Ty(C);
1136 break;
1137 }
1138 case DataTypeSigned32:
1139 case DataTypeUnsigned32: {
1140 return llvm::Type::getInt32Ty(C);
1141 break;
1142 }
Stephen Hinesa5d2c232010-10-11 16:10:02 -07001143 case DataTypeSigned64:
1144 case DataTypeUnsigned64: {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001145 return llvm::Type::getInt64Ty(C);
1146 break;
1147 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001148 default: {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001149 slangAssert(false && "Unknown data type");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001150 }
1151 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001152
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001153 return NULL;
1154}
1155
Zonr Chang641558f2010-10-12 21:07:06 +08001156bool RSExportPrimitiveType::equals(const RSExportable *E) const {
1157 CHECK_PARENT_EQUALITY(RSExportType, E);
1158 return (static_cast<const RSExportPrimitiveType*>(E)->getType() == getType());
1159}
1160
Stephen Hinesfdd1ba12012-03-08 17:26:32 -08001161RSReflectionType *RSExportPrimitiveType::getRSReflectionType(DataType DT) {
1162 if (DT > DataTypeUnknown && DT < DataTypeMax) {
1163 return &gReflectionTypes[DT];
1164 } else {
1165 return NULL;
1166 }
1167}
1168
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001169/**************************** RSExportPointerType ****************************/
1170
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001171RSExportPointerType
1172*RSExportPointerType::Create(RSContext *Context,
1173 const clang::PointerType *PT,
1174 const llvm::StringRef &TypeName) {
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -07001175 const clang::Type *PointeeType = GetPointeeType(PT);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001176 const RSExportType *PointeeET;
1177
1178 if (PointeeType->getTypeClass() != clang::Type::Pointer) {
1179 PointeeET = RSExportType::Create(Context, PointeeType);
1180 } else {
1181 // Double or higher dimension of pointer, export as int*
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001182 PointeeET = RSExportPrimitiveType::Create(Context,
1183 Context->getASTContext().IntTy.getTypePtr());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001184 }
1185
1186 if (PointeeET == NULL) {
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001187 // Error diagnostic is emitted for corresponding pointee type
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001188 return NULL;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001189 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001190
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001191 return new RSExportPointerType(Context, TypeName, PointeeET);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001192}
1193
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001194llvm::Type *RSExportPointerType::convertToLLVMType() const {
1195 llvm::Type *PointeeType = mPointeeType->getLLVMType();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001196 return llvm::PointerType::getUnqual(PointeeType);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001197}
1198
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001199bool RSExportPointerType::keep() {
1200 if (!RSExportType::keep())
1201 return false;
Zonr Chang641558f2010-10-12 21:07:06 +08001202 const_cast<RSExportType*>(mPointeeType)->keep();
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001203 return true;
Zonr Chang641558f2010-10-12 21:07:06 +08001204}
1205
1206bool RSExportPointerType::equals(const RSExportable *E) const {
1207 CHECK_PARENT_EQUALITY(RSExportType, E);
1208 return (static_cast<const RSExportPointerType*>(E)
1209 ->getPointeeType()->equals(getPointeeType()));
1210}
1211
zonr6315f762010-10-05 15:35:14 +08001212/***************************** RSExportVectorType *****************************/
zonr6315f762010-10-05 15:35:14 +08001213llvm::StringRef
1214RSExportVectorType::GetTypeName(const clang::ExtVectorType *EVT) {
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -07001215 const clang::Type *ElementType = GetExtVectorElementType(EVT);
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001216 llvm::StringRef name;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001217
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001218 if ((ElementType->getTypeClass() != clang::Type::Builtin))
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001219 return name;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001220
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -07001221 const clang::BuiltinType *BT =
1222 static_cast<const clang::BuiltinType*>(
1223 ElementType->getCanonicalTypeInternal().getTypePtr());
1224
Zonr Changa65ec162010-10-17 01:53:05 +08001225 if ((EVT->getNumElements() < 1) ||
1226 (EVT->getNumElements() > 4))
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001227 return name;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001228
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001229 BuiltinInfo *info = FindBuiltinType(BT->getKind());
1230 if (info != NULL) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001231 int I = EVT->getNumElements() - 1;
1232 if (I < kMaxVectorSize) {
1233 name = info->cname[I];
1234 } else {
1235 slangAssert(false && "Max vector is 4");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001236 }
1237 }
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001238 return name;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001239}
1240
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001241RSExportVectorType *RSExportVectorType::Create(RSContext *Context,
1242 const clang::ExtVectorType *EVT,
1243 const llvm::StringRef &TypeName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001244 bool Normalized) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001245 slangAssert(EVT != NULL && EVT->getTypeClass() == clang::Type::ExtVector);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001246
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -07001247 const clang::Type *ElementType = GetExtVectorElementType(EVT);
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001248 DataType DT = RSExportPrimitiveType::GetDataType(Context, ElementType);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001249
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001250 if (DT != DataTypeUnknown)
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001251 return new RSExportVectorType(Context,
1252 TypeName,
1253 DT,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001254 Normalized,
1255 EVT->getNumElements());
1256 else
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001257 return NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001258}
1259
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001260llvm::Type *RSExportVectorType::convertToLLVMType() const {
1261 llvm::Type *ElementType = RSExportPrimitiveType::convertToLLVMType();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001262 return llvm::VectorType::get(ElementType, getNumElement());
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001263}
1264
Zonr Chang641558f2010-10-12 21:07:06 +08001265bool RSExportVectorType::equals(const RSExportable *E) const {
1266 CHECK_PARENT_EQUALITY(RSExportPrimitiveType, E);
1267 return (static_cast<const RSExportVectorType*>(E)->getNumElement()
1268 == getNumElement());
1269}
1270
Zonr Chang92b344a2010-10-05 20:39:03 +08001271/***************************** RSExportMatrixType *****************************/
1272RSExportMatrixType *RSExportMatrixType::Create(RSContext *Context,
1273 const clang::RecordType *RT,
1274 const llvm::StringRef &TypeName,
1275 unsigned Dim) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001276 slangAssert((RT != NULL) && (RT->getTypeClass() == clang::Type::Record));
1277 slangAssert((Dim > 1) && "Invalid dimension of matrix");
Zonr Chang92b344a2010-10-05 20:39:03 +08001278
1279 // Check whether the struct rs_matrix is in our expected form (but assume it's
1280 // correct if we're not sure whether it's correct or not)
1281 const clang::RecordDecl* RD = RT->getDecl();
1282 RD = RD->getDefinition();
1283 if (RD != NULL) {
1284 // Find definition, perform further examination
1285 if (RD->field_empty()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001286 Context->ReportError(
1287 RD->getLocation(),
1288 "invalid matrix struct: must have 1 field for saving values: '%0'")
1289 << RD->getName();
Zonr Chang92b344a2010-10-05 20:39:03 +08001290 return NULL;
1291 }
1292
1293 clang::RecordDecl::field_iterator FIT = RD->field_begin();
1294 const clang::FieldDecl *FD = *FIT;
1295 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
1296 if ((FT == NULL) || (FT->getTypeClass() != clang::Type::ConstantArray)) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001297 Context->ReportError(RD->getLocation(),
1298 "invalid matrix struct: first field should"
1299 " be an array with constant size: '%0'")
1300 << RD->getName();
Zonr Chang92b344a2010-10-05 20:39:03 +08001301 return NULL;
1302 }
1303 const clang::ConstantArrayType *CAT =
1304 static_cast<const clang::ConstantArrayType *>(FT);
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -07001305 const clang::Type *ElementType = GetConstantArrayElementType(CAT);
Zonr Chang92b344a2010-10-05 20:39:03 +08001306 if ((ElementType == NULL) ||
1307 (ElementType->getTypeClass() != clang::Type::Builtin) ||
Logan Chien9207a2e2011-10-21 15:39:28 +08001308 (static_cast<const clang::BuiltinType *>(ElementType)->getKind() !=
1309 clang::BuiltinType::Float)) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001310 Context->ReportError(RD->getLocation(),
1311 "invalid matrix struct: first field "
1312 "should be a float array: '%0'")
1313 << RD->getName();
Zonr Chang92b344a2010-10-05 20:39:03 +08001314 return NULL;
1315 }
1316
1317 if (CAT->getSize() != Dim * Dim) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001318 Context->ReportError(RD->getLocation(),
1319 "invalid matrix struct: first field "
1320 "should be an array with size %0: '%1'")
1321 << (Dim * Dim) << (RD->getName());
Zonr Chang92b344a2010-10-05 20:39:03 +08001322 return NULL;
1323 }
1324
1325 FIT++;
1326 if (FIT != RD->field_end()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001327 Context->ReportError(RD->getLocation(),
1328 "invalid matrix struct: must have "
1329 "exactly 1 field: '%0'")
1330 << RD->getName();
Zonr Chang92b344a2010-10-05 20:39:03 +08001331 return NULL;
1332 }
1333 }
1334
1335 return new RSExportMatrixType(Context, TypeName, Dim);
1336}
1337
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001338llvm::Type *RSExportMatrixType::convertToLLVMType() const {
Zonr Chang92b344a2010-10-05 20:39:03 +08001339 // Construct LLVM type:
1340 // struct {
1341 // float X[mDim * mDim];
1342 // }
1343
1344 llvm::LLVMContext &C = getRSContext()->getLLVMContext();
1345 llvm::ArrayType *X = llvm::ArrayType::get(llvm::Type::getFloatTy(C),
1346 mDim * mDim);
Stephen Hinesa67e4452011-07-19 15:39:26 -07001347 return llvm::StructType::get(C, X, false);
Zonr Chang92b344a2010-10-05 20:39:03 +08001348}
1349
Zonr Chang641558f2010-10-12 21:07:06 +08001350bool RSExportMatrixType::equals(const RSExportable *E) const {
1351 CHECK_PARENT_EQUALITY(RSExportType, E);
1352 return (static_cast<const RSExportMatrixType*>(E)->getDim() == getDim());
1353}
1354
Zonr Chang2e1dba62010-10-05 22:20:11 +08001355/************************* RSExportConstantArrayType *************************/
1356RSExportConstantArrayType
1357*RSExportConstantArrayType::Create(RSContext *Context,
1358 const clang::ConstantArrayType *CAT) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001359 slangAssert(CAT != NULL && CAT->getTypeClass() == clang::Type::ConstantArray);
Zonr Chang2e1dba62010-10-05 22:20:11 +08001360
Stephen Hines6e6578a2011-02-07 18:05:48 -08001361 slangAssert((CAT->getSize().getActiveBits() < 32) && "array too large");
Zonr Chang2e1dba62010-10-05 22:20:11 +08001362
1363 unsigned Size = static_cast<unsigned>(CAT->getSize().getZExtValue());
Stephen Hines6e6578a2011-02-07 18:05:48 -08001364 slangAssert((Size > 0) && "Constant array should have size greater than 0");
Zonr Chang2e1dba62010-10-05 22:20:11 +08001365
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -07001366 const clang::Type *ElementType = GetConstantArrayElementType(CAT);
Zonr Chang2e1dba62010-10-05 22:20:11 +08001367 RSExportType *ElementET = RSExportType::Create(Context, ElementType);
1368
1369 if (ElementET == NULL) {
Zonr Chang2e1dba62010-10-05 22:20:11 +08001370 return NULL;
1371 }
1372
1373 return new RSExportConstantArrayType(Context,
1374 ElementET,
1375 Size);
1376}
1377
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001378llvm::Type *RSExportConstantArrayType::convertToLLVMType() const {
Zonr Chang2e1dba62010-10-05 22:20:11 +08001379 return llvm::ArrayType::get(mElementType->getLLVMType(), getSize());
1380}
1381
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001382bool RSExportConstantArrayType::keep() {
1383 if (!RSExportType::keep())
1384 return false;
Zonr Chang641558f2010-10-12 21:07:06 +08001385 const_cast<RSExportType*>(mElementType)->keep();
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001386 return true;
Zonr Chang641558f2010-10-12 21:07:06 +08001387}
1388
1389bool RSExportConstantArrayType::equals(const RSExportable *E) const {
1390 CHECK_PARENT_EQUALITY(RSExportType, E);
Stephen Hinesa7b75182010-12-07 15:49:49 -08001391 const RSExportConstantArrayType *RHS =
1392 static_cast<const RSExportConstantArrayType*>(E);
1393 return ((getSize() == RHS->getSize()) &&
1394 (getElementType()->equals(RHS->getElementType())));
Zonr Chang641558f2010-10-12 21:07:06 +08001395}
1396
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001397/**************************** RSExportRecordType ****************************/
1398RSExportRecordType *RSExportRecordType::Create(RSContext *Context,
1399 const clang::RecordType *RT,
1400 const llvm::StringRef &TypeName,
1401 bool mIsArtificial) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001402 slangAssert(RT != NULL && RT->getTypeClass() == clang::Type::Record);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001403
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001404 const clang::RecordDecl *RD = RT->getDecl();
Stephen Hines6e6578a2011-02-07 18:05:48 -08001405 slangAssert(RD->isStruct());
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001406
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001407 RD = RD->getDefinition();
1408 if (RD == NULL) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001409 slangAssert(false && "struct is not defined in this module");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001410 return NULL;
1411 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001412
Zonr Chang0da0a7d2010-10-05 21:26:37 +08001413 // Struct layout construct by clang. We rely on this for obtaining the
1414 // alloc size of a struct and offset of every field in that struct.
1415 const clang::ASTRecordLayout *RL =
Stephen Hines9e5b5032010-11-03 13:19:14 -07001416 &Context->getASTContext().getASTRecordLayout(RD);
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001417 slangAssert((RL != NULL) &&
1418 "Failed to retrieve the struct layout from Clang.");
Zonr Chang0da0a7d2010-10-05 21:26:37 +08001419
1420 RSExportRecordType *ERT =
1421 new RSExportRecordType(Context,
1422 TypeName,
1423 RD->hasAttr<clang::PackedAttr>(),
1424 mIsArtificial,
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -07001425 RL->getDataSize().getQuantity(),
Shih-wei Liao68318a12011-03-11 05:56:15 -08001426 RL->getSize().getQuantity());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001427 unsigned int Index = 0;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001428
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001429 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
1430 FE = RD->field_end();
1431 FI != FE;
Shih-wei Liao91a37832010-10-03 19:11:51 -07001432 FI++, Index++) {
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001433
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001434 // FIXME: All fields should be primitive type
Shih-wei Liao43730fe2012-08-02 23:06:18 -07001435 slangAssert(FI->getKind() == clang::Decl::Field);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001436 clang::FieldDecl *FD = *FI;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001437
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001438 if (FD->isBitField()) {
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001439 return NULL;
1440 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001441
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001442 // Type
zonr6315f762010-10-05 15:35:14 +08001443 RSExportType *ET = RSExportElement::CreateFromDecl(Context, FD);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001444
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001445 if (ET != NULL) {
Zonr Chang0da0a7d2010-10-05 21:26:37 +08001446 ERT->mFields.push_back(
1447 new Field(ET, FD->getName(), ERT,
1448 static_cast<size_t>(RL->getFieldOffset(Index) >> 3)));
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001449 } else {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001450 Context->ReportError(RD->getLocation(),
1451 "field type cannot be exported: '%0.%1'")
1452 << RD->getName() << FD->getName();
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001453 return NULL;
1454 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001455 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001456
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001457 return ERT;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001458}
1459
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001460llvm::Type *RSExportRecordType::convertToLLVMType() const {
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001461 // Create an opaque type since struct may reference itself recursively.
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001462
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001463 // TODO(sliao): LLVM took out the OpaqueType. Any other to migrate to?
1464 std::vector<llvm::Type*> FieldTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001465
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001466 for (const_field_iterator FI = fields_begin(), FE = fields_end();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001467 FI != FE;
1468 FI++) {
1469 const Field *F = *FI;
1470 const RSExportType *FET = F->getType();
Shih-wei Liao1f0d88f2010-06-25 00:15:24 -07001471
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001472 FieldTypes.push_back(FET->getLLVMType());
1473 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001474
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001475 llvm::StructType *ST = llvm::StructType::get(getRSContext()->getLLVMContext(),
1476 FieldTypes,
1477 mIsPacked);
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001478 if (ST != NULL) {
1479 return ST;
1480 } else {
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001481 return NULL;
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001482 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001483}
Zonr Chang641558f2010-10-12 21:07:06 +08001484
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001485bool RSExportRecordType::keep() {
1486 if (!RSExportType::keep())
1487 return false;
Zonr Chang641558f2010-10-12 21:07:06 +08001488 for (std::list<const Field*>::iterator I = mFields.begin(),
1489 E = mFields.end();
1490 I != E;
1491 I++) {
1492 const_cast<RSExportType*>((*I)->getType())->keep();
1493 }
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001494 return true;
Zonr Chang641558f2010-10-12 21:07:06 +08001495}
1496
1497bool RSExportRecordType::equals(const RSExportable *E) const {
1498 CHECK_PARENT_EQUALITY(RSExportType, E);
1499
1500 const RSExportRecordType *ERT = static_cast<const RSExportRecordType*>(E);
1501
1502 if (ERT->getFields().size() != getFields().size())
1503 return false;
1504
1505 const_field_iterator AI = fields_begin(), BI = ERT->fields_begin();
1506
1507 for (unsigned i = 0, e = getFields().size(); i != e; i++) {
1508 if (!(*AI)->getType()->equals((*BI)->getType()))
1509 return false;
1510 AI++;
1511 BI++;
1512 }
1513
1514 return true;
1515}
Stephen Hinese639eb52010-11-08 19:27:20 -08001516
Jason Sams1b6a0882012-03-12 15:07:58 -07001517void RSExportType::convertToRTD(RSReflectionTypeData *rtd) const {
1518 memset(rtd, 0, sizeof(*rtd));
1519 rtd->vecSize = 1;
1520
1521 switch(getClass()) {
1522 case RSExportType::ExportClassPrimitive: {
1523 const RSExportPrimitiveType *EPT = static_cast<const RSExportPrimitiveType*>(this);
1524 rtd->type = RSExportPrimitiveType::getRSReflectionType(EPT);
1525 return;
1526 }
1527 case RSExportType::ExportClassPointer: {
1528 const RSExportPointerType *EPT = static_cast<const RSExportPointerType*>(this);
1529 const RSExportType *PointeeType = EPT->getPointeeType();
1530 PointeeType->convertToRTD(rtd);
1531 rtd->isPointer = true;
1532 return;
1533 }
1534 case RSExportType::ExportClassVector: {
1535 const RSExportVectorType *EVT = static_cast<const RSExportVectorType*>(this);
1536 rtd->type = EVT->getRSReflectionType(EVT);
1537 rtd->vecSize = EVT->getNumElement();
1538 return;
1539 }
1540 case RSExportType::ExportClassMatrix: {
1541 const RSExportMatrixType *EMT = static_cast<const RSExportMatrixType*>(this);
1542 unsigned Dim = EMT->getDim();
1543 slangAssert((Dim >= 2) && (Dim <= 4));
1544 rtd->type = &gReflectionTypes[15 + Dim-2];
1545 return;
1546 }
1547 case RSExportType::ExportClassConstantArray: {
1548 const RSExportConstantArrayType* CAT =
1549 static_cast<const RSExportConstantArrayType*>(this);
1550 CAT->getElementType()->convertToRTD(rtd);
1551 rtd->arraySize = CAT->getSize();
1552 return;
1553 }
1554 case RSExportType::ExportClassRecord: {
1555 slangAssert(!"RSExportType::ExportClassRecord not implemented");
1556 return;// RS_TYPE_CLASS_NAME_PREFIX + ET->getName() + ".Item";
1557 }
1558 default: {
1559 slangAssert(false && "Unknown class of type");
1560 }
1561 }
1562}
1563
1564
Stephen Hinese639eb52010-11-08 19:27:20 -08001565} // namespace slang