blob: 0683db0c0753c2536af84e45d916f121f279be3c [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
Matt Wala7682b662015-07-30 15:53:47 -070044// For the data types we support:
45// Category - data type category
46// RsType - element name in RenderScript
47// RsShortType - short element name in RenderScript
48// SizeInBits - size in bits
49// CName - reflected C name
50// JavaName - reflected Java name
51// JavaArrayElementName - reflected name in Java arrays
52// CVecName - prefix for C vector types
53// JavaVecName - prefix for Java vector type
54// JavaPromotion - unsigned type undergoing Java promotion
55//
56// IMPORTANT: The data types in this table should be at the same index as
57// specified by the corresponding DataType enum.
58//
59// TODO: Pull this information out into a separate file.
Stephen Hinesfdd1ba12012-03-08 17:26:32 -080060static RSReflectionType gReflectionTypes[] = {
Matt Wala7682b662015-07-30 15:53:47 -070061#define _ nullptr
62{PrimitiveDataType, "FLOAT_16", "F16", 16, "half", "short", _, "Half", "Half", false},
63{PrimitiveDataType, "FLOAT_32", "F32", 32, "float", "float", "float", "Float", "Float", false},
64{PrimitiveDataType, "FLOAT_64", "F64", 64, "double", "double", "double", "Double", "Double", false},
65{PrimitiveDataType, "SIGNED_8", "I8", 8, "int8_t", "byte", "byte", "Byte", "Byte", false},
66{PrimitiveDataType, "SIGNED_16", "I16", 16, "int16_t", "short", "short", "Short", "Short", false},
67{PrimitiveDataType, "SIGNED_32", "I32", 32, "int32_t", "int", "int", "Int", "Int", false},
68{PrimitiveDataType, "SIGNED_64", "I64", 64, "int64_t", "long", "long", "Long", "Long", false},
69{PrimitiveDataType, "UNSIGNED_8", "U8", 8, "uint8_t", "short", "byte", "UByte", "Short", true},
70{PrimitiveDataType, "UNSIGNED_16", "U16", 16, "uint16_t", "int", "short", "UShort", "Int", true},
71{PrimitiveDataType, "UNSIGNED_32", "U32", 32, "uint32_t", "long", "int", "UInt", "Long", true},
72{PrimitiveDataType, "UNSIGNED_64", "U64", 64, "uint64_t", "long", "long", "ULong", "Long", false},
73{PrimitiveDataType, "BOOLEAN", "BOOLEAN", 8, "bool", "boolean", "byte", _, _, false},
74{PrimitiveDataType, "UNSIGNED_5_6_5", _, 16, _, _, _, _, _, false},
75{PrimitiveDataType, "UNSIGNED_5_5_5_1", _, 16, _, _, _, _, _, false},
76{PrimitiveDataType, "UNSIGNED_4_4_4_4", _, 16, _, _, _, _, _, false},
Stephen Hinesfdd1ba12012-03-08 17:26:32 -080077
Nicolas Collin5f3da4b2015-10-21 10:22:51 +010078{MatrixDataType, "MATRIX_2X2", _, 4*32, "rs_matrix2x2", "Matrix2f", _, _, _, false},
79{MatrixDataType, "MATRIX_3X3", _, 9*32, "rs_matrix3x3", "Matrix3f", _, _, _, false},
80{MatrixDataType, "MATRIX_4X4", _, 16*32, "rs_matrix4x4", "Matrix4f", _, _, _, false},
Stephen Hinesfdd1ba12012-03-08 17:26:32 -080081
Matt Wala7682b662015-07-30 15:53:47 -070082// RS object types are 32 bits in 32-bit RS, but 256 bits in 64-bit RS.
83// This is handled specially by the GetSizeInBits(}, method.
84{ObjectDataType, "RS_ELEMENT", "ELEMENT", 32, "Element", "Element", _, _, _, false},
85{ObjectDataType, "RS_TYPE", "TYPE", 32, "Type", "Type", _, _, _, false},
86{ObjectDataType, "RS_ALLOCATION", "ALLOCATION", 32, "Allocation", "Allocation", _, _, _, false},
87{ObjectDataType, "RS_SAMPLER", "SAMPLER", 32, "Sampler", "Sampler", _, _, _, false},
88{ObjectDataType, "RS_SCRIPT", "SCRIPT", 32, "Script", "Script", _, _, _, false},
89{ObjectDataType, "RS_MESH", "MESH", 32, "Mesh", "Mesh", _, _, _, false},
90{ObjectDataType, "RS_PATH", "PATH", 32, "Path", "Path", _, _, _, false},
91{ObjectDataType, "RS_PROGRAM_FRAGMENT", "PROGRAM_FRAGMENT", 32, "ProgramFragment", "ProgramFragment", _, _, _, false},
92{ObjectDataType, "RS_PROGRAM_VERTEX", "PROGRAM_VERTEX", 32, "ProgramVertex", "ProgramVertex", _, _, _, false},
93{ObjectDataType, "RS_PROGRAM_RASTER", "PROGRAM_RASTER", 32, "ProgramRaster", "ProgramRaster", _, _, _, false},
94{ObjectDataType, "RS_PROGRAM_STORE", "PROGRAM_STORE", 32, "ProgramStore", "ProgramStore", _, _, _, false},
95{ObjectDataType, "RS_FONT", "FONT", 32, "Font", "Font", _, _, _, false},
96#undef _
Stephen Hinesfdd1ba12012-03-08 17:26:32 -080097};
98
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -070099const int kMaxVectorSize = 4;
100
101struct BuiltinInfo {
102 clang::BuiltinType::Kind builtinTypeKind;
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700103 DataType type;
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700104 /* TODO If we return std::string instead of llvm::StringRef, we could build
105 * the name instead of duplicating the entries.
106 */
107 const char *cname[kMaxVectorSize];
108};
109
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700110
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700111BuiltinInfo BuiltinInfoTable[] = {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700112 {clang::BuiltinType::Bool, DataTypeBoolean,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700113 {"bool", "bool2", "bool3", "bool4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700114 {clang::BuiltinType::Char_U, DataTypeUnsigned8,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700115 {"uchar", "uchar2", "uchar3", "uchar4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700116 {clang::BuiltinType::UChar, DataTypeUnsigned8,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700117 {"uchar", "uchar2", "uchar3", "uchar4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700118 {clang::BuiltinType::Char16, DataTypeSigned16,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700119 {"short", "short2", "short3", "short4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700120 {clang::BuiltinType::Char32, DataTypeSigned32,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700121 {"int", "int2", "int3", "int4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700122 {clang::BuiltinType::UShort, DataTypeUnsigned16,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700123 {"ushort", "ushort2", "ushort3", "ushort4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700124 {clang::BuiltinType::UInt, DataTypeUnsigned32,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700125 {"uint", "uint2", "uint3", "uint4"}},
Tim Murraye46265d2014-08-18 14:16:32 -0700126 {clang::BuiltinType::ULong, DataTypeUnsigned64,
127 {"ulong", "ulong2", "ulong3", "ulong4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700128 {clang::BuiltinType::ULongLong, DataTypeUnsigned64,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700129 {"ulong", "ulong2", "ulong3", "ulong4"}},
130
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700131 {clang::BuiltinType::Char_S, DataTypeSigned8,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700132 {"char", "char2", "char3", "char4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700133 {clang::BuiltinType::SChar, DataTypeSigned8,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700134 {"char", "char2", "char3", "char4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700135 {clang::BuiltinType::Short, DataTypeSigned16,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700136 {"short", "short2", "short3", "short4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700137 {clang::BuiltinType::Int, DataTypeSigned32,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700138 {"int", "int2", "int3", "int4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700139 {clang::BuiltinType::Long, DataTypeSigned64,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700140 {"long", "long2", "long3", "long4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700141 {clang::BuiltinType::LongLong, DataTypeSigned64,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700142 {"long", "long2", "long3", "long4"}},
Pirama Arumuga Nainare4dd17d2015-04-07 11:09:14 -0700143 {clang::BuiltinType::Half, DataTypeFloat16,
144 {"half", "half2", "half3", "half4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700145 {clang::BuiltinType::Float, DataTypeFloat32,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700146 {"float", "float2", "float3", "float4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700147 {clang::BuiltinType::Double, DataTypeFloat64,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700148 {"double", "double2", "double3", "double4"}},
149};
150const int BuiltinInfoTableCount = sizeof(BuiltinInfoTable) / sizeof(BuiltinInfoTable[0]);
151
152struct NameAndPrimitiveType {
153 const char *name;
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700154 DataType dataType;
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700155};
156
157static NameAndPrimitiveType MatrixAndObjectDataTypes[] = {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700158 {"rs_matrix2x2", DataTypeRSMatrix2x2},
159 {"rs_matrix3x3", DataTypeRSMatrix3x3},
160 {"rs_matrix4x4", DataTypeRSMatrix4x4},
161 {"rs_element", DataTypeRSElement},
162 {"rs_type", DataTypeRSType},
163 {"rs_allocation", DataTypeRSAllocation},
164 {"rs_sampler", DataTypeRSSampler},
165 {"rs_script", DataTypeRSScript},
166 {"rs_mesh", DataTypeRSMesh},
167 {"rs_path", DataTypeRSPath},
168 {"rs_program_fragment", DataTypeRSProgramFragment},
169 {"rs_program_vertex", DataTypeRSProgramVertex},
170 {"rs_program_raster", DataTypeRSProgramRaster},
171 {"rs_program_store", DataTypeRSProgramStore},
172 {"rs_font", DataTypeRSFont},
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700173};
174
175const int MatrixAndObjectDataTypesCount =
176 sizeof(MatrixAndObjectDataTypes) / sizeof(MatrixAndObjectDataTypes[0]);
177
Stephen Hines24e79f62010-12-13 12:55:57 -0800178static const clang::Type *TypeExportableHelper(
179 const clang::Type *T,
180 llvm::SmallPtrSet<const clang::Type*, 8>& SPS,
Stephen Hines48d893d2013-12-06 18:00:27 -0800181 slang::RSContext *Context,
Stephen Hines24e79f62010-12-13 12:55:57 -0800182 const clang::VarDecl *VD,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700183 const clang::RecordDecl *TopLevelRecord);
Stephen Hines24e79f62010-12-13 12:55:57 -0800184
Tim Murrayee4016d2014-04-10 15:49:08 -0700185template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800186static void ReportTypeError(slang::RSContext *Context,
Stephen Hines11274a72012-09-26 19:14:20 -0700187 const clang::NamedDecl *ND,
Stephen Hines24e79f62010-12-13 12:55:57 -0800188 const clang::RecordDecl *TopLevelRecord,
Tim Murrayee4016d2014-04-10 15:49:08 -0700189 const char (&Message)[N],
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700190 unsigned int TargetAPI = 0) {
Stephen Hines24e79f62010-12-13 12:55:57 -0800191 // Attempt to use the type declaration first (if we have one).
192 // Fall back to the variable definition, if we are looking at something
193 // like an array declaration that can't be exported.
194 if (TopLevelRecord) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800195 Context->ReportError(TopLevelRecord->getLocation(), Message)
196 << TopLevelRecord->getName() << TargetAPI;
Stephen Hines11274a72012-09-26 19:14:20 -0700197 } else if (ND) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800198 Context->ReportError(ND->getLocation(), Message) << ND->getName()
199 << TargetAPI;
Stephen Hines24e79f62010-12-13 12:55:57 -0800200 } else {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800201 slangAssert(false && "Variables should be validated before exporting");
Stephen Hines24e79f62010-12-13 12:55:57 -0800202 }
Stephen Hines24e79f62010-12-13 12:55:57 -0800203}
204
205static const clang::Type *ConstantArrayTypeExportableHelper(
206 const clang::ConstantArrayType *CAT,
207 llvm::SmallPtrSet<const clang::Type*, 8>& SPS,
Stephen Hines48d893d2013-12-06 18:00:27 -0800208 slang::RSContext *Context,
Stephen Hines24e79f62010-12-13 12:55:57 -0800209 const clang::VarDecl *VD,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700210 const clang::RecordDecl *TopLevelRecord) {
Stephen Hines24e79f62010-12-13 12:55:57 -0800211 // Check element type
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700212 const clang::Type *ElementType = GetConstantArrayElementType(CAT);
Stephen Hines24e79f62010-12-13 12:55:57 -0800213 if (ElementType->isArrayType()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800214 ReportTypeError(Context, VD, TopLevelRecord,
Logan Chien9207a2e2011-10-21 15:39:28 +0800215 "multidimensional arrays cannot be exported: '%0'");
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700216 return nullptr;
Stephen Hines24e79f62010-12-13 12:55:57 -0800217 } else if (ElementType->isExtVectorType()) {
218 const clang::ExtVectorType *EVT =
219 static_cast<const clang::ExtVectorType*>(ElementType);
220 unsigned numElements = EVT->getNumElements();
221
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700222 const clang::Type *BaseElementType = GetExtVectorElementType(EVT);
Stephen Hines24e79f62010-12-13 12:55:57 -0800223 if (!RSExportPrimitiveType::IsPrimitiveType(BaseElementType)) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800224 ReportTypeError(Context, VD, TopLevelRecord,
Logan Chien9207a2e2011-10-21 15:39:28 +0800225 "vectors of non-primitive types cannot be exported: '%0'");
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700226 return nullptr;
Stephen Hines24e79f62010-12-13 12:55:57 -0800227 }
228
229 if (numElements == 3 && CAT->getSize() != 1) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800230 ReportTypeError(Context, VD, TopLevelRecord,
Logan Chien9207a2e2011-10-21 15:39:28 +0800231 "arrays of width 3 vector types cannot be exported: '%0'");
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700232 return nullptr;
Stephen Hines24e79f62010-12-13 12:55:57 -0800233 }
234 }
235
Stephen Hines48d893d2013-12-06 18:00:27 -0800236 if (TypeExportableHelper(ElementType, SPS, Context, VD,
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700237 TopLevelRecord) == nullptr) {
238 return nullptr;
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700239 } else {
Stephen Hines24e79f62010-12-13 12:55:57 -0800240 return CAT;
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700241 }
Stephen Hines24e79f62010-12-13 12:55:57 -0800242}
243
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700244BuiltinInfo *FindBuiltinType(clang::BuiltinType::Kind builtinTypeKind) {
245 for (int i = 0; i < BuiltinInfoTableCount; i++) {
246 if (builtinTypeKind == BuiltinInfoTable[i].builtinTypeKind) {
247 return &BuiltinInfoTable[i];
248 }
249 }
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700250 return nullptr;
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700251}
252
Stephen Hines24e79f62010-12-13 12:55:57 -0800253static const clang::Type *TypeExportableHelper(
Stephen Hinese67239d2012-02-24 15:08:36 -0800254 clang::Type const *T,
255 llvm::SmallPtrSet<clang::Type const *, 8> &SPS,
Stephen Hines48d893d2013-12-06 18:00:27 -0800256 slang::RSContext *Context,
Stephen Hinese67239d2012-02-24 15:08:36 -0800257 clang::VarDecl const *VD,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700258 clang::RecordDecl const *TopLevelRecord) {
Stephen Hinese5e64432010-12-02 18:48:20 -0800259 // Normalize first
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700260 if ((T = GetCanonicalType(T)) == nullptr)
261 return nullptr;
Stephen Hinese5e64432010-12-02 18:48:20 -0800262
263 if (SPS.count(T))
264 return T;
265
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700266 const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr();
267
Stephen Hinese5e64432010-12-02 18:48:20 -0800268 switch (T->getTypeClass()) {
269 case clang::Type::Builtin: {
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700270 const clang::BuiltinType *BT = static_cast<const clang::BuiltinType*>(CTI);
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700271 return FindBuiltinType(BT->getKind()) == nullptr ? nullptr : T;
Stephen Hinese5e64432010-12-02 18:48:20 -0800272 }
273 case clang::Type::Record: {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700274 if (RSExportPrimitiveType::GetRSSpecificType(T) != DataTypeUnknown) {
Stephen Hinese5e64432010-12-02 18:48:20 -0800275 return T; // RS object type, no further checks are needed
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700276 }
Stephen Hinese5e64432010-12-02 18:48:20 -0800277
278 // Check internal struct
Stephen Hinescd440ee2010-12-06 14:57:04 -0800279 if (T->isUnionType()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800280 ReportTypeError(Context, VD, T->getAsUnionType()->getDecl(),
Logan Chien9207a2e2011-10-21 15:39:28 +0800281 "unions cannot be exported: '%0'");
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700282 return nullptr;
Stephen Hinescd440ee2010-12-06 14:57:04 -0800283 } else if (!T->isStructureType()) {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800284 slangAssert(false && "Unknown type cannot be exported");
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700285 return nullptr;
Stephen Hinescd440ee2010-12-06 14:57:04 -0800286 }
287
Stephen Hines24e79f62010-12-13 12:55:57 -0800288 clang::RecordDecl *RD = T->getAsStructureType()->getDecl();
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700289 if (RD != nullptr) {
Stephen Hinese5e64432010-12-02 18:48:20 -0800290 RD = RD->getDefinition();
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700291 if (RD == nullptr) {
292 ReportTypeError(Context, nullptr, T->getAsStructureType()->getDecl(),
Logan Chien9207a2e2011-10-21 15:39:28 +0800293 "struct is not defined in this module");
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700294 return nullptr;
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800295 }
296 }
Stephen Hinese5e64432010-12-02 18:48:20 -0800297
298 if (!TopLevelRecord) {
299 TopLevelRecord = RD;
300 }
301 if (RD->getName().empty()) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700302 ReportTypeError(Context, nullptr, RD,
Logan Chien9207a2e2011-10-21 15:39:28 +0800303 "anonymous structures cannot be exported");
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700304 return nullptr;
Stephen Hinese5e64432010-12-02 18:48:20 -0800305 }
306
307 // Fast check
308 if (RD->hasFlexibleArrayMember() || RD->hasObjectMember())
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700309 return nullptr;
Stephen Hinese5e64432010-12-02 18:48:20 -0800310
311 // Insert myself into checking set
312 SPS.insert(T);
313
314 // Check all element
315 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
316 FE = RD->field_end();
317 FI != FE;
318 FI++) {
319 const clang::FieldDecl *FD = *FI;
320 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700321 FT = GetCanonicalType(FT);
Stephen Hinese5e64432010-12-02 18:48:20 -0800322
Stephen Hines48d893d2013-12-06 18:00:27 -0800323 if (!TypeExportableHelper(FT, SPS, Context, VD, TopLevelRecord)) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700324 return nullptr;
Stephen Hinese5e64432010-12-02 18:48:20 -0800325 }
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800326
327 // We don't support bit fields yet
328 //
329 // TODO(zonr/srhines): allow bit fields of size 8, 16, 32
330 if (FD->isBitField()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800331 Context->ReportError(
332 FD->getLocation(),
333 "bit fields are not able to be exported: '%0.%1'")
334 << RD->getName() << FD->getName();
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700335 return nullptr;
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800336 }
Stephen Hinese5e64432010-12-02 18:48:20 -0800337 }
338
339 return T;
340 }
341 case clang::Type::Pointer: {
342 if (TopLevelRecord) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800343 ReportTypeError(Context, VD, TopLevelRecord,
Stephen Hinesab94bcc2015-06-11 17:00:04 -0700344 "structures containing pointers cannot be used as the type of "
345 "an exported global variable or the parameter to an exported "
346 "function: '%0'");
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700347 return nullptr;
Stephen Hinese5e64432010-12-02 18:48:20 -0800348 }
Stephen Hines24e79f62010-12-13 12:55:57 -0800349
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700350 const clang::PointerType *PT = static_cast<const clang::PointerType*>(CTI);
351 const clang::Type *PointeeType = GetPointeeType(PT);
Stephen Hinese5e64432010-12-02 18:48:20 -0800352
Stephen Hinesaa82e742012-04-11 12:38:17 -0700353 if (PointeeType->getTypeClass() == clang::Type::Pointer) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800354 ReportTypeError(Context, VD, TopLevelRecord,
Stephen Hinesaa82e742012-04-11 12:38:17 -0700355 "multiple levels of pointers cannot be exported: '%0'");
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700356 return nullptr;
Stephen Hinesaa82e742012-04-11 12:38:17 -0700357 }
Stephen Hinese5e64432010-12-02 18:48:20 -0800358 // We don't support pointer with array-type pointee or unsupported pointee
359 // type
360 if (PointeeType->isArrayType() ||
Stephen Hines48d893d2013-12-06 18:00:27 -0800361 (TypeExportableHelper(PointeeType, SPS, Context, VD,
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700362 TopLevelRecord) == nullptr))
363 return nullptr;
Stephen Hinese5e64432010-12-02 18:48:20 -0800364 else
365 return T;
366 }
367 case clang::Type::ExtVector: {
368 const clang::ExtVectorType *EVT =
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700369 static_cast<const clang::ExtVectorType*>(CTI);
Stephen Hinese5e64432010-12-02 18:48:20 -0800370 // Only vector with size 2, 3 and 4 are supported.
371 if (EVT->getNumElements() < 2 || EVT->getNumElements() > 4)
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700372 return nullptr;
Stephen Hinese5e64432010-12-02 18:48:20 -0800373
374 // Check base element type
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700375 const clang::Type *ElementType = GetExtVectorElementType(EVT);
Stephen Hinese5e64432010-12-02 18:48:20 -0800376
377 if ((ElementType->getTypeClass() != clang::Type::Builtin) ||
Stephen Hines48d893d2013-12-06 18:00:27 -0800378 (TypeExportableHelper(ElementType, SPS, Context, VD,
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700379 TopLevelRecord) == nullptr))
380 return nullptr;
Stephen Hinese5e64432010-12-02 18:48:20 -0800381 else
382 return T;
383 }
384 case clang::Type::ConstantArray: {
385 const clang::ConstantArrayType *CAT =
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700386 static_cast<const clang::ConstantArrayType*>(CTI);
Stephen Hinese5e64432010-12-02 18:48:20 -0800387
Stephen Hines48d893d2013-12-06 18:00:27 -0800388 return ConstantArrayTypeExportableHelper(CAT, SPS, Context, VD,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700389 TopLevelRecord);
Stephen Hinese5e64432010-12-02 18:48:20 -0800390 }
Stephen Hines48d893d2013-12-06 18:00:27 -0800391 case clang::Type::Enum: {
392 // FIXME: We currently convert enums to integers, rather than reflecting
393 // a more complete (and nicer type-safe Java version).
394 return Context->getASTContext().IntTy.getTypePtr();
395 }
Stephen Hinese5e64432010-12-02 18:48:20 -0800396 default: {
Stephen Hines48d893d2013-12-06 18:00:27 -0800397 slangAssert(false && "Unknown type cannot be validated");
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700398 return nullptr;
Stephen Hinese5e64432010-12-02 18:48:20 -0800399 }
400 }
401}
402
403// Return the type that can be used to create RSExportType, will always return
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700404// the canonical type.
405//
406// If the Type T is not exportable, this function returns nullptr. DiagEngine is
407// used to generate proper Clang diagnostic messages when a non-exportable type
408// is detected. TopLevelRecord is used to capture the highest struct (in the
409// case of a nested hierarchy) for detecting other types that cannot be exported
410// (mostly pointers within a struct).
Stephen Hinese5e64432010-12-02 18:48:20 -0800411static const clang::Type *TypeExportable(const clang::Type *T,
Stephen Hines48d893d2013-12-06 18:00:27 -0800412 slang::RSContext *Context,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700413 const clang::VarDecl *VD) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700414 llvm::SmallPtrSet<const clang::Type*, 8> SPS =
415 llvm::SmallPtrSet<const clang::Type*, 8>();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700416
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700417 return TypeExportableHelper(T, SPS, Context, VD, nullptr);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700418}
419
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800420static bool ValidateRSObjectInVarDecl(slang::RSContext *Context,
421 clang::VarDecl *VD, bool InCompositeType,
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700422 unsigned int TargetAPI) {
423 if (TargetAPI < SLANG_JB_TARGET_API) {
424 // Only if we are already in a composite type (like an array or structure).
425 if (InCompositeType) {
426 // Only if we are actually exported (i.e. non-static).
Stephen Hines44f10062013-06-13 00:33:23 -0700427 if (VD->hasLinkage() &&
428 (VD->getFormalLinkage() == clang::ExternalLinkage)) {
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700429 // Only if we are not a pointer to an object.
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700430 const clang::Type *T = GetCanonicalType(VD->getType().getTypePtr());
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700431 if (T->getTypeClass() != clang::Type::Pointer) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700432 ReportTypeError(Context, VD, nullptr,
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700433 "arrays/structures containing RS object types "
434 "cannot be exported in target API < %1: '%0'",
435 SLANG_JB_TARGET_API);
436 return false;
437 }
438 }
439 }
440 }
441
442 return true;
443}
444
Stephen Hines11274a72012-09-26 19:14:20 -0700445// Helper function for ValidateType(). We do a recursive descent on the
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700446// type hierarchy to ensure that we can properly export/handle the
447// declaration.
448// \return true if the variable declaration is valid,
449// false if it is invalid (along with proper diagnostics).
450//
Stephen Hines11274a72012-09-26 19:14:20 -0700451// C - ASTContext (for diagnostics + builtin types).
452// T - sub-type that we are validating.
453// ND - (optional) top-level named declaration that we are validating.
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700454// SPS - set of types we have already seen/validated.
455// InCompositeType - true if we are within an outer composite type.
456// UnionDecl - set if we are in a sub-type of a union.
457// TargetAPI - target SDK API level.
Stephen Hines11274a72012-09-26 19:14:20 -0700458// IsFilterscript - whether or not we are compiling for Filterscript
Stephen Hinesab94bcc2015-06-11 17:00:04 -0700459// IsExtern - is this type externally visible (i.e. extern global or parameter
460// to an extern function)
Stephen Hines11274a72012-09-26 19:14:20 -0700461static bool ValidateTypeHelper(
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800462 slang::RSContext *Context,
Stephen Hines11274a72012-09-26 19:14:20 -0700463 clang::ASTContext &C,
Stephen Hines78e69cb2011-04-22 15:03:19 -0700464 const clang::Type *&T,
Stephen Hines11274a72012-09-26 19:14:20 -0700465 clang::NamedDecl *ND,
466 clang::SourceLocation Loc,
Stephen Hines78e69cb2011-04-22 15:03:19 -0700467 llvm::SmallPtrSet<const clang::Type*, 8>& SPS,
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700468 bool InCompositeType,
469 clang::RecordDecl *UnionDecl,
Stephen Hines11274a72012-09-26 19:14:20 -0700470 unsigned int TargetAPI,
Stephen Hinesab94bcc2015-06-11 17:00:04 -0700471 bool IsFilterscript,
472 bool IsExtern) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700473 if ((T = GetCanonicalType(T)) == nullptr)
Stephen Hines78e69cb2011-04-22 15:03:19 -0700474 return true;
475
476 if (SPS.count(T))
477 return true;
478
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700479 const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr();
480
Stephen Hines78e69cb2011-04-22 15:03:19 -0700481 switch (T->getTypeClass()) {
482 case clang::Type::Record: {
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700483 if (RSExportPrimitiveType::IsRSObjectType(T)) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700484 clang::VarDecl *VD = (ND ? llvm::dyn_cast<clang::VarDecl>(ND) : nullptr);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800485 if (VD && !ValidateRSObjectInVarDecl(Context, VD, InCompositeType,
486 TargetAPI)) {
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700487 return false;
488 }
489 }
490
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700491 if (RSExportPrimitiveType::GetRSSpecificType(T) != DataTypeUnknown) {
Stephen Hines78e69cb2011-04-22 15:03:19 -0700492 if (!UnionDecl) {
493 return true;
494 } else if (RSExportPrimitiveType::IsRSObjectType(T)) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700495 ReportTypeError(Context, nullptr, UnionDecl,
Stephen Hines78e69cb2011-04-22 15:03:19 -0700496 "unions containing RS object types are not allowed");
497 return false;
498 }
499 }
500
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700501 clang::RecordDecl *RD = nullptr;
Stephen Hines78e69cb2011-04-22 15:03:19 -0700502
503 // Check internal struct
504 if (T->isUnionType()) {
505 RD = T->getAsUnionType()->getDecl();
506 UnionDecl = RD;
507 } else if (T->isStructureType()) {
508 RD = T->getAsStructureType()->getDecl();
509 } else {
510 slangAssert(false && "Unknown type cannot be exported");
511 return false;
512 }
513
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700514 if (RD != nullptr) {
Stephen Hines78e69cb2011-04-22 15:03:19 -0700515 RD = RD->getDefinition();
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700516 if (RD == nullptr) {
Stephen Hines78e69cb2011-04-22 15:03:19 -0700517 // FIXME
518 return true;
519 }
520 }
521
522 // Fast check
523 if (RD->hasFlexibleArrayMember() || RD->hasObjectMember())
524 return false;
525
526 // Insert myself into checking set
527 SPS.insert(T);
528
529 // Check all elements
530 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
531 FE = RD->field_end();
532 FI != FE;
533 FI++) {
534 const clang::FieldDecl *FD = *FI;
535 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700536 FT = GetCanonicalType(FT);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700537
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800538 if (!ValidateTypeHelper(Context, C, FT, ND, Loc, SPS, true, UnionDecl,
Stephen Hinesab94bcc2015-06-11 17:00:04 -0700539 TargetAPI, IsFilterscript, IsExtern)) {
Stephen Hines78e69cb2011-04-22 15:03:19 -0700540 return false;
541 }
542 }
543
544 return true;
545 }
546
547 case clang::Type::Builtin: {
Stephen Hines11274a72012-09-26 19:14:20 -0700548 if (IsFilterscript) {
549 clang::QualType QT = T->getCanonicalTypeInternal();
550 if (QT == C.DoubleTy ||
551 QT == C.LongDoubleTy ||
552 QT == C.LongTy ||
553 QT == C.LongLongTy) {
Stephen Hines11274a72012-09-26 19:14:20 -0700554 if (ND) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800555 Context->ReportError(
556 Loc,
Stephen Hines11274a72012-09-26 19:14:20 -0700557 "Builtin types > 32 bits in size are forbidden in "
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800558 "Filterscript: '%0'")
559 << ND->getName();
Stephen Hines11274a72012-09-26 19:14:20 -0700560 } else {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800561 Context->ReportError(
562 Loc,
Stephen Hines11274a72012-09-26 19:14:20 -0700563 "Builtin types > 32 bits in size are forbidden in "
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800564 "Filterscript");
Stephen Hines11274a72012-09-26 19:14:20 -0700565 }
566 return false;
567 }
568 }
Stephen Hines78e69cb2011-04-22 15:03:19 -0700569 break;
570 }
571
572 case clang::Type::Pointer: {
Stephen Hines11274a72012-09-26 19:14:20 -0700573 if (IsFilterscript) {
574 if (ND) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800575 Context->ReportError(Loc,
576 "Pointers are forbidden in Filterscript: '%0'")
577 << ND->getName();
Stephen Hines11274a72012-09-26 19:14:20 -0700578 return false;
579 } else {
580 // TODO(srhines): Find a better way to handle expressions (i.e. no
581 // NamedDecl) involving pointers in FS that should be allowed.
582 // An example would be calls to library functions like
583 // rsMatrixMultiply() that take rs_matrixNxN * types.
584 }
585 }
586
Stephen Hinesab94bcc2015-06-11 17:00:04 -0700587 // Forbid pointers in structures that are externally visible.
588 if (InCompositeType && IsExtern) {
589 if (ND) {
590 Context->ReportError(Loc,
591 "structures containing pointers cannot be used as the type of "
592 "an exported global variable or the parameter to an exported "
593 "function: '%0'")
594 << ND->getName();
595 } else {
596 Context->ReportError(Loc,
597 "structures containing pointers cannot be used as the type of "
598 "an exported global variable or the parameter to an exported "
599 "function");
600 }
601 return false;
602 }
603
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700604 const clang::PointerType *PT = static_cast<const clang::PointerType*>(CTI);
605 const clang::Type *PointeeType = GetPointeeType(PT);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700606
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800607 return ValidateTypeHelper(Context, C, PointeeType, ND, Loc, SPS,
608 InCompositeType, UnionDecl, TargetAPI,
Stephen Hinesab94bcc2015-06-11 17:00:04 -0700609 IsFilterscript, IsExtern);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700610 }
611
612 case clang::Type::ExtVector: {
613 const clang::ExtVectorType *EVT =
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700614 static_cast<const clang::ExtVectorType*>(CTI);
615 const clang::Type *ElementType = GetExtVectorElementType(EVT);
Stephen Hinesb13b85e2012-08-31 19:27:48 -0700616 if (TargetAPI < SLANG_ICS_TARGET_API &&
617 InCompositeType &&
Stephen Hinesfdae63e2012-11-28 14:42:44 -0800618 EVT->getNumElements() == 3 &&
619 ND &&
Stephen Hines44f10062013-06-13 00:33:23 -0700620 ND->getFormalLinkage() == clang::ExternalLinkage) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700621 ReportTypeError(Context, ND, nullptr,
Stephen Hinesb13b85e2012-08-31 19:27:48 -0700622 "structs containing vectors of dimension 3 cannot "
623 "be exported at this API level: '%0'");
624 return false;
625 }
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800626 return ValidateTypeHelper(Context, C, ElementType, ND, Loc, SPS, true,
Stephen Hinesab94bcc2015-06-11 17:00:04 -0700627 UnionDecl, TargetAPI, IsFilterscript, IsExtern);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700628 }
629
630 case clang::Type::ConstantArray: {
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700631 const clang::ConstantArrayType *CAT = static_cast<const clang::ConstantArrayType*>(CTI);
632 const clang::Type *ElementType = GetConstantArrayElementType(CAT);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800633 return ValidateTypeHelper(Context, C, ElementType, ND, Loc, SPS, true,
Stephen Hinesab94bcc2015-06-11 17:00:04 -0700634 UnionDecl, TargetAPI, IsFilterscript, IsExtern);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700635 }
636
637 default: {
638 break;
639 }
640 }
641
642 return true;
Stephen Hinese5e64432010-12-02 18:48:20 -0800643}
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700644
Stephen Hinese5e64432010-12-02 18:48:20 -0800645} // namespace
646
Jean-Luc Brouilleteca05342014-05-15 12:44:21 -0700647std::string CreateDummyName(const char *type, const std::string &name) {
648 std::stringstream S;
649 S << "<" << type;
650 if (!name.empty()) {
651 S << ":" << name;
652 }
653 S << ">";
654 return S.str();
655}
656
Stephen Hinese5e64432010-12-02 18:48:20 -0800657/****************************** RSExportType ******************************/
658bool RSExportType::NormalizeType(const clang::Type *&T,
659 llvm::StringRef &TypeName,
Stephen Hines48d893d2013-12-06 18:00:27 -0800660 RSContext *Context,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700661 const clang::VarDecl *VD) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700662 if ((T = TypeExportable(T, Context, VD)) == nullptr) {
Stephen Hinese5e64432010-12-02 18:48:20 -0800663 return false;
664 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700665 // Get type name
666 TypeName = RSExportType::GetTypeName(T);
Stephen Hines48d893d2013-12-06 18:00:27 -0800667 if (Context && TypeName.empty()) {
Stephen Hines48d893d2013-12-06 18:00:27 -0800668 if (VD) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800669 Context->ReportError(VD->getLocation(),
670 "anonymous types cannot be exported");
Stephen Hines48d893d2013-12-06 18:00:27 -0800671 } else {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800672 Context->ReportError("anonymous types cannot be exported");
Stephen Hinese5e64432010-12-02 18:48:20 -0800673 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700674 return false;
Stephen Hinese5e64432010-12-02 18:48:20 -0800675 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700676
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700677 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700678}
679
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800680bool RSExportType::ValidateType(slang::RSContext *Context, clang::ASTContext &C,
681 clang::QualType QT, clang::NamedDecl *ND,
682 clang::SourceLocation Loc,
Stephen Hinesab94bcc2015-06-11 17:00:04 -0700683 unsigned int TargetAPI, bool IsFilterscript,
684 bool IsExtern) {
Stephen Hines11274a72012-09-26 19:14:20 -0700685 const clang::Type *T = QT.getTypePtr();
Stephen Hines78e69cb2011-04-22 15:03:19 -0700686 llvm::SmallPtrSet<const clang::Type*, 8> SPS =
687 llvm::SmallPtrSet<const clang::Type*, 8>();
688
Stephen Hinesab94bcc2015-06-11 17:00:04 -0700689 // If this is an externally visible variable declaration, we check if the
690 // type is able to be exported first.
Stephen Hines33ea5732015-06-16 16:00:26 -0700691 if (auto VD = llvm::dyn_cast_or_null<clang::VarDecl>(ND)) {
Stephen Hinesab94bcc2015-06-11 17:00:04 -0700692 if (VD->getFormalLinkage() == clang::ExternalLinkage) {
693 if (!TypeExportable(T, Context, VD)) {
694 return false;
695 }
696 }
697 }
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700698 return ValidateTypeHelper(Context, C, T, ND, Loc, SPS, false, nullptr, TargetAPI,
Stephen Hinesab94bcc2015-06-11 17:00:04 -0700699 IsFilterscript, IsExtern);
Stephen Hines11274a72012-09-26 19:14:20 -0700700}
701
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800702bool RSExportType::ValidateVarDecl(slang::RSContext *Context,
703 clang::VarDecl *VD, unsigned int TargetAPI,
Stephen Hines11274a72012-09-26 19:14:20 -0700704 bool IsFilterscript) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800705 return ValidateType(Context, VD->getASTContext(), VD->getType(), VD,
Stephen Hinesab94bcc2015-06-11 17:00:04 -0700706 VD->getLocation(), TargetAPI, IsFilterscript,
707 (VD->getFormalLinkage() == clang::ExternalLinkage));
Stephen Hines78e69cb2011-04-22 15:03:19 -0700708}
709
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700710const clang::Type
711*RSExportType::GetTypeOfDecl(const clang::DeclaratorDecl *DD) {
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800712 if (DD) {
Stephen Hines8de19222013-08-23 16:30:59 -0700713 clang::QualType T = DD->getType();
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800714
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700715 if (T.isNull())
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700716 return nullptr;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700717 else
718 return T.getTypePtr();
719 }
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700720 return nullptr;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700721}
722
723llvm::StringRef RSExportType::GetTypeName(const clang::Type* T) {
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700724 T = GetCanonicalType(T);
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700725 if (T == nullptr)
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700726 return llvm::StringRef();
727
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700728 const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr();
729
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700730 switch (T->getTypeClass()) {
731 case clang::Type::Builtin: {
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700732 const clang::BuiltinType *BT = static_cast<const clang::BuiltinType*>(CTI);
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700733 BuiltinInfo *info = FindBuiltinType(BT->getKind());
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700734 if (info != nullptr) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700735 return info->cname[0];
Zonr Changa65ec162010-10-17 01:53:05 +0800736 }
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700737 slangAssert(false && "Unknown data type of the builtin");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700738 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700739 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700740 case clang::Type::Record: {
Stephen Hinescd440ee2010-12-06 14:57:04 -0800741 clang::RecordDecl *RD;
742 if (T->isStructureType()) {
743 RD = T->getAsStructureType()->getDecl();
Stephen Hinesdd6206b2010-12-09 19:39:22 -0800744 } else {
Stephen Hinescd440ee2010-12-06 14:57:04 -0800745 break;
746 }
747
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700748 llvm::StringRef Name = RD->getName();
749 if (Name.empty()) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700750 if (RD->getTypedefNameForAnonDecl() != nullptr) {
Shih-wei Liao83f0c622011-06-21 05:34:53 -0700751 Name = RD->getTypedefNameForAnonDecl()->getName();
752 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700753
Shih-wei Liao83f0c622011-06-21 05:34:53 -0700754 if (Name.empty()) {
755 // Try to find a name from redeclaration (i.e. typedef)
756 for (clang::TagDecl::redecl_iterator RI = RD->redecls_begin(),
757 RE = RD->redecls_end();
758 RI != RE;
759 RI++) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700760 slangAssert(*RI != nullptr && "cannot be NULL object");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700761
Shih-wei Liao83f0c622011-06-21 05:34:53 -0700762 Name = (*RI)->getName();
763 if (!Name.empty())
764 break;
765 }
766 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700767 }
768 return Name;
769 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700770 case clang::Type::Pointer: {
771 // "*" plus pointee name
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700772 const clang::PointerType *P = static_cast<const clang::PointerType*>(CTI);
773 const clang::Type *PT = GetPointeeType(P);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700774 llvm::StringRef PointeeName;
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700775 if (NormalizeType(PT, PointeeName, nullptr, nullptr)) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700776 char *Name = new char[ 1 /* * */ + PointeeName.size() + 1 ];
777 Name[0] = '*';
778 memcpy(Name + 1, PointeeName.data(), PointeeName.size());
779 Name[PointeeName.size() + 1] = '\0';
780 return Name;
781 }
782 break;
783 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700784 case clang::Type::ExtVector: {
785 const clang::ExtVectorType *EVT =
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700786 static_cast<const clang::ExtVectorType*>(CTI);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700787 return RSExportVectorType::GetTypeName(EVT);
788 break;
789 }
Zonr Chang2e1dba62010-10-05 22:20:11 +0800790 case clang::Type::ConstantArray : {
791 // Construct name for a constant array is too complicated.
Stephen Hines340b5552014-09-05 02:03:36 -0700792 return "<ConstantArray>";
Zonr Chang2e1dba62010-10-05 22:20:11 +0800793 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700794 default: {
795 break;
796 }
797 }
798
799 return llvm::StringRef();
800}
801
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700802
803RSExportType *RSExportType::Create(RSContext *Context,
804 const clang::Type *T,
805 const llvm::StringRef &TypeName) {
806 // Lookup the context to see whether the type was processed before.
807 // Newly created RSExportType will insert into context
808 // in RSExportType::RSExportType()
809 RSContext::export_type_iterator ETI = Context->findExportType(TypeName);
810
811 if (ETI != Context->export_types_end())
812 return ETI->second;
813
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700814 const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr();
815
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700816 RSExportType *ET = nullptr;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700817 switch (T->getTypeClass()) {
818 case clang::Type::Record: {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700819 DataType dt = RSExportPrimitiveType::GetRSSpecificType(TypeName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700820 switch (dt) {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700821 case DataTypeUnknown: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800822 // User-defined types
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700823 ET = RSExportRecordType::Create(Context,
824 T->getAsStructureType(),
825 TypeName);
826 break;
827 }
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700828 case DataTypeRSMatrix2x2: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800829 // 2 x 2 Matrix type
830 ET = RSExportMatrixType::Create(Context,
831 T->getAsStructureType(),
832 TypeName,
833 2);
834 break;
835 }
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700836 case DataTypeRSMatrix3x3: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800837 // 3 x 3 Matrix type
838 ET = RSExportMatrixType::Create(Context,
839 T->getAsStructureType(),
840 TypeName,
841 3);
842 break;
843 }
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700844 case DataTypeRSMatrix4x4: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800845 // 4 x 4 Matrix type
846 ET = RSExportMatrixType::Create(Context,
847 T->getAsStructureType(),
848 TypeName,
849 4);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700850 break;
851 }
852 default: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800853 // Others are primitive types
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700854 ET = RSExportPrimitiveType::Create(Context, T, TypeName);
855 break;
856 }
857 }
858 break;
859 }
860 case clang::Type::Builtin: {
861 ET = RSExportPrimitiveType::Create(Context, T, TypeName);
862 break;
863 }
864 case clang::Type::Pointer: {
865 ET = RSExportPointerType::Create(Context,
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700866 static_cast<const clang::PointerType*>(CTI),
867 TypeName);
Zonr Chang92b344a2010-10-05 20:39:03 +0800868 // FIXME: free the name (allocated in RSExportType::GetTypeName)
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700869 delete [] TypeName.data();
870 break;
871 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700872 case clang::Type::ExtVector: {
873 ET = RSExportVectorType::Create(Context,
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700874 static_cast<const clang::ExtVectorType*>(CTI),
875 TypeName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700876 break;
877 }
Zonr Chang2e1dba62010-10-05 22:20:11 +0800878 case clang::Type::ConstantArray: {
879 ET = RSExportConstantArrayType::Create(
880 Context,
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700881 static_cast<const clang::ConstantArrayType*>(CTI));
Zonr Chang2e1dba62010-10-05 22:20:11 +0800882 break;
883 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700884 default: {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800885 Context->ReportError("unknown type cannot be exported: '%0'")
886 << T->getTypeClassName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700887 break;
888 }
889 }
890
891 return ET;
892}
893
zonr6315f762010-10-05 15:35:14 +0800894RSExportType *RSExportType::Create(RSContext *Context, const clang::Type *T) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700895 llvm::StringRef TypeName;
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700896 if (NormalizeType(T, TypeName, Context, nullptr)) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700897 return Create(Context, T, TypeName);
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700898 } else {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700899 return nullptr;
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700900 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700901}
902
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700903RSExportType *RSExportType::CreateFromDecl(RSContext *Context,
904 const clang::VarDecl *VD) {
905 return RSExportType::Create(Context, GetTypeOfDecl(VD));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700906}
907
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700908size_t RSExportType::getStoreSize() const {
909 return getRSContext()->getDataLayout()->getTypeStoreSize(getLLVMType());
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700910}
911
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700912size_t RSExportType::getAllocSize() const {
913 return getRSContext()->getDataLayout()->getTypeAllocSize(getLLVMType());
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700914}
Shih-wei Liao1f0d88f2010-06-25 00:15:24 -0700915
Zonr Chang6b6320a2010-10-05 22:42:01 +0800916RSExportType::RSExportType(RSContext *Context,
917 ExportClass Class,
918 const llvm::StringRef &Name)
Zonr Changa41ce1d2010-10-06 02:23:12 +0800919 : RSExportable(Context, RSExportable::EX_TYPE),
Zonr Chang6b6320a2010-10-05 22:42:01 +0800920 mClass(Class),
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800921 // Make a copy on Name since memory stored @Name is either allocated in
922 // ASTContext or allocated in GetTypeName which will be destroyed later.
zonr6315f762010-10-05 15:35:14 +0800923 mName(Name.data(), Name.size()),
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700924 mLLVMType(nullptr) {
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800925 // Don't cache the type whose name start with '<'. Those type failed to
926 // get their name since constructing their name in GetTypeName() requiring
927 // complicated work.
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700928 if (!IsDummyName(Name)) {
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800929 // TODO(zonr): Need to check whether the insertion is successful or not.
930 Context->insertExportType(llvm::StringRef(Name), this);
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700931 }
932
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700933}
934
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800935bool RSExportType::keep() {
936 if (!RSExportable::keep())
937 return false;
Zonr Chang641558f2010-10-12 21:07:06 +0800938 // Invalidate converted LLVM type.
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700939 mLLVMType = nullptr;
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800940 return true;
Zonr Chang641558f2010-10-12 21:07:06 +0800941}
942
943bool RSExportType::equals(const RSExportable *E) const {
944 CHECK_PARENT_EQUALITY(RSExportable, E);
945 return (static_cast<const RSExportType*>(E)->getClass() == getClass());
946}
947
Zonr Changa7a828d2010-10-22 03:44:27 +0800948RSExportType::~RSExportType() {
Zonr Changa7a828d2010-10-22 03:44:27 +0800949}
950
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700951/************************** RSExportPrimitiveType **************************/
Zonr Changb1771ef2010-10-22 18:03:46 +0800952llvm::ManagedStatic<RSExportPrimitiveType::RSSpecificTypeMapTy>
953RSExportPrimitiveType::RSSpecificTypeMap;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700954
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700955bool RSExportPrimitiveType::IsPrimitiveType(const clang::Type *T) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700956 if ((T != nullptr) && (T->getTypeClass() == clang::Type::Builtin))
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700957 return true;
958 else
959 return false;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700960}
961
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700962DataType
Zonr Changb1771ef2010-10-22 18:03:46 +0800963RSExportPrimitiveType::GetRSSpecificType(const llvm::StringRef &TypeName) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700964 if (TypeName.empty())
965 return DataTypeUnknown;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700966
Zonr Changb1771ef2010-10-22 18:03:46 +0800967 if (RSSpecificTypeMap->empty()) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700968 for (int i = 0; i < MatrixAndObjectDataTypesCount; i++) {
Stephen Hines3eb819a2014-11-24 18:16:52 -0800969 (*RSSpecificTypeMap)[MatrixAndObjectDataTypes[i].name] =
970 MatrixAndObjectDataTypes[i].dataType;
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700971 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700972 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700973
Zonr Changb1771ef2010-10-22 18:03:46 +0800974 RSSpecificTypeMapTy::const_iterator I = RSSpecificTypeMap->find(TypeName);
975 if (I == RSSpecificTypeMap->end())
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700976 return DataTypeUnknown;
977 else
978 return I->getValue();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700979}
980
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700981DataType RSExportPrimitiveType::GetRSSpecificType(const clang::Type *T) {
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700982 T = GetCanonicalType(T);
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700983 if ((T == nullptr) || (T->getTypeClass() != clang::Type::Record))
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700984 return DataTypeUnknown;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700985
Zonr Changb1771ef2010-10-22 18:03:46 +0800986 return GetRSSpecificType( RSExportType::GetTypeName(T) );
987}
988
989bool RSExportPrimitiveType::IsRSMatrixType(DataType DT) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700990 if (DT < 0 || DT >= DataTypeMax) {
991 return false;
992 }
993 return gReflectionTypes[DT].category == MatrixDataType;
Zonr Changb1771ef2010-10-22 18:03:46 +0800994}
995
996bool RSExportPrimitiveType::IsRSObjectType(DataType DT) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700997 if (DT < 0 || DT >= DataTypeMax) {
998 return false;
999 }
1000 return gReflectionTypes[DT].category == ObjectDataType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001001}
1002
Stephen Hinesfeaca062011-02-04 14:08:13 -08001003bool RSExportPrimitiveType::IsStructureTypeWithRSObject(const clang::Type *T) {
1004 bool RSObjectTypeSeen = false;
1005 while (T && T->isArrayType()) {
1006 T = T->getArrayElementTypeNoTypeQual();
1007 }
1008
1009 const clang::RecordType *RT = T->getAsStructureType();
1010 if (!RT) {
1011 return false;
1012 }
Stephen Hinesb0fabe52013-01-07 19:06:09 -08001013
Stephen Hinesfeaca062011-02-04 14:08:13 -08001014 const clang::RecordDecl *RD = RT->getDecl();
Stephen Hinesb0fabe52013-01-07 19:06:09 -08001015 if (RD) {
1016 RD = RD->getDefinition();
1017 }
1018 if (!RD) {
1019 return false;
1020 }
1021
Stephen Hinesfeaca062011-02-04 14:08:13 -08001022 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
1023 FE = RD->field_end();
1024 FI != FE;
1025 FI++) {
1026 // We just look through all field declarations to see if we find a
1027 // declaration for an RS object type (or an array of one).
1028 const clang::FieldDecl *FD = *FI;
1029 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
1030 while (FT && FT->isArrayType()) {
1031 FT = FT->getArrayElementTypeNoTypeQual();
1032 }
1033
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001034 DataType DT = GetRSSpecificType(FT);
Stephen Hinesfeaca062011-02-04 14:08:13 -08001035 if (IsRSObjectType(DT)) {
1036 // RS object types definitely need to be zero-initialized
1037 RSObjectTypeSeen = true;
1038 } else {
1039 switch (DT) {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001040 case DataTypeRSMatrix2x2:
1041 case DataTypeRSMatrix3x3:
1042 case DataTypeRSMatrix4x4:
Stephen Hinesfeaca062011-02-04 14:08:13 -08001043 // Matrix types should get zero-initialized as well
1044 RSObjectTypeSeen = true;
1045 break;
1046 default:
1047 // Ignore all other primitive types
1048 break;
1049 }
1050 while (FT && FT->isArrayType()) {
1051 FT = FT->getArrayElementTypeNoTypeQual();
1052 }
1053 if (FT->isStructureType()) {
1054 // Recursively handle structs of structs (even though these can't
1055 // be exported, it is possible for a user to have them internally).
1056 RSObjectTypeSeen |= IsStructureTypeWithRSObject(FT);
1057 }
1058 }
1059 }
1060
1061 return RSObjectTypeSeen;
1062}
1063
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001064size_t RSExportPrimitiveType::GetSizeInBits(const RSExportPrimitiveType *EPT) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001065 int type = EPT->getType();
1066 slangAssert((type > DataTypeUnknown && type < DataTypeMax) &&
Stephen Hines6e6578a2011-02-07 18:05:48 -08001067 "RSExportPrimitiveType::GetSizeInBits : unknown data type");
Stephen Hines9ae18b22014-06-10 23:53:00 -07001068 // All RS object types are 256 bits in 64-bit RS.
1069 if (EPT->isRSObjectType() && EPT->getRSContext()->is64Bit()) {
1070 return 256;
1071 }
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001072 return gReflectionTypes[type].size_in_bits;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001073}
1074
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001075DataType
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001076RSExportPrimitiveType::GetDataType(RSContext *Context, const clang::Type *T) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001077 if (T == nullptr)
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001078 return DataTypeUnknown;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001079
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001080 switch (T->getTypeClass()) {
1081 case clang::Type::Builtin: {
Loganbe274822011-02-16 22:02:54 +08001082 const clang::BuiltinType *BT =
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -07001083 static_cast<const clang::BuiltinType*>(T->getCanonicalTypeInternal().getTypePtr());
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001084 BuiltinInfo *info = FindBuiltinType(BT->getKind());
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001085 if (info != nullptr) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001086 return info->type;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001087 }
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001088 // The size of type WChar depend on platform so we abandon the support
1089 // to them.
1090 Context->ReportError("built-in type cannot be exported: '%0'")
1091 << T->getTypeClassName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001092 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001093 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001094 case clang::Type::Record: {
1095 // must be RS object type
Zonr Changb1771ef2010-10-22 18:03:46 +08001096 return RSExportPrimitiveType::GetRSSpecificType(T);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001097 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001098 default: {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001099 Context->ReportError("primitive type cannot be exported: '%0'")
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001100 << T->getTypeClassName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001101 break;
1102 }
1103 }
1104
1105 return DataTypeUnknown;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001106}
1107
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001108RSExportPrimitiveType
1109*RSExportPrimitiveType::Create(RSContext *Context,
1110 const clang::Type *T,
1111 const llvm::StringRef &TypeName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001112 bool Normalized) {
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001113 DataType DT = GetDataType(Context, T);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001114
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001115 if ((DT == DataTypeUnknown) || TypeName.empty())
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001116 return nullptr;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001117 else
Zonr Chang6b6320a2010-10-05 22:42:01 +08001118 return new RSExportPrimitiveType(Context, ExportClassPrimitive, TypeName,
Stephen Hines2b8fb642012-03-09 00:12:47 -08001119 DT, Normalized);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001120}
1121
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001122RSExportPrimitiveType *RSExportPrimitiveType::Create(RSContext *Context,
Stephen Hines2b8fb642012-03-09 00:12:47 -08001123 const clang::Type *T) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001124 llvm::StringRef TypeName;
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001125 if (RSExportType::NormalizeType(T, TypeName, Context, nullptr)
Stephen Hines78e69cb2011-04-22 15:03:19 -07001126 && IsPrimitiveType(T)) {
Stephen Hines2b8fb642012-03-09 00:12:47 -08001127 return Create(Context, T, TypeName);
Stephen Hinese5e64432010-12-02 18:48:20 -08001128 } else {
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001129 return nullptr;
Stephen Hinese5e64432010-12-02 18:48:20 -08001130 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001131}
1132
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001133llvm::Type *RSExportPrimitiveType::convertToLLVMType() const {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001134 llvm::LLVMContext &C = getRSContext()->getLLVMContext();
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001135
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001136 if (isRSObjectType()) {
1137 // struct {
1138 // int *p;
1139 // } __attribute__((packed, aligned(pointer_size)))
1140 //
1141 // which is
1142 //
1143 // <{ [1 x i32] }> in LLVM
1144 //
Tim Murrayd56a92f2014-08-05 13:27:12 -07001145 std::vector<llvm::Type *> Elements;
1146 if (getRSContext()->is64Bit()) {
1147 // 64-bit path
1148 Elements.push_back(llvm::ArrayType::get(llvm::Type::getInt64Ty(C), 4));
1149 return llvm::StructType::get(C, Elements, true);
1150 } else {
1151 // 32-bit legacy path
1152 Elements.push_back(llvm::ArrayType::get(llvm::Type::getInt32Ty(C), 1));
1153 return llvm::StructType::get(C, Elements, true);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001154 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001155 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001156
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001157 switch (mType) {
Pirama Arumuga Nainare4dd17d2015-04-07 11:09:14 -07001158 case DataTypeFloat16: {
1159 return llvm::Type::getHalfTy(C);
1160 break;
1161 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001162 case DataTypeFloat32: {
1163 return llvm::Type::getFloatTy(C);
1164 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001165 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001166 case DataTypeFloat64: {
1167 return llvm::Type::getDoubleTy(C);
1168 break;
1169 }
Shih-wei Liao91a37832010-10-03 19:11:51 -07001170 case DataTypeBoolean: {
1171 return llvm::Type::getInt1Ty(C);
1172 break;
1173 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001174 case DataTypeSigned8:
1175 case DataTypeUnsigned8: {
1176 return llvm::Type::getInt8Ty(C);
1177 break;
1178 }
1179 case DataTypeSigned16:
1180 case DataTypeUnsigned16:
1181 case DataTypeUnsigned565:
1182 case DataTypeUnsigned5551:
1183 case DataTypeUnsigned4444: {
1184 return llvm::Type::getInt16Ty(C);
1185 break;
1186 }
1187 case DataTypeSigned32:
1188 case DataTypeUnsigned32: {
1189 return llvm::Type::getInt32Ty(C);
1190 break;
1191 }
Stephen Hinesa5d2c232010-10-11 16:10:02 -07001192 case DataTypeSigned64:
1193 case DataTypeUnsigned64: {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001194 return llvm::Type::getInt64Ty(C);
1195 break;
1196 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001197 default: {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001198 slangAssert(false && "Unknown data type");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001199 }
1200 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001201
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001202 return nullptr;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001203}
1204
Zonr Chang641558f2010-10-12 21:07:06 +08001205bool RSExportPrimitiveType::equals(const RSExportable *E) const {
1206 CHECK_PARENT_EQUALITY(RSExportType, E);
1207 return (static_cast<const RSExportPrimitiveType*>(E)->getType() == getType());
1208}
1209
Stephen Hinesfdd1ba12012-03-08 17:26:32 -08001210RSReflectionType *RSExportPrimitiveType::getRSReflectionType(DataType DT) {
1211 if (DT > DataTypeUnknown && DT < DataTypeMax) {
1212 return &gReflectionTypes[DT];
1213 } else {
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001214 return nullptr;
Stephen Hinesfdd1ba12012-03-08 17:26:32 -08001215 }
1216}
1217
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001218/**************************** RSExportPointerType ****************************/
1219
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001220RSExportPointerType
1221*RSExportPointerType::Create(RSContext *Context,
1222 const clang::PointerType *PT,
1223 const llvm::StringRef &TypeName) {
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -07001224 const clang::Type *PointeeType = GetPointeeType(PT);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001225 const RSExportType *PointeeET;
1226
1227 if (PointeeType->getTypeClass() != clang::Type::Pointer) {
1228 PointeeET = RSExportType::Create(Context, PointeeType);
1229 } else {
1230 // Double or higher dimension of pointer, export as int*
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001231 PointeeET = RSExportPrimitiveType::Create(Context,
1232 Context->getASTContext().IntTy.getTypePtr());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001233 }
1234
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001235 if (PointeeET == nullptr) {
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001236 // Error diagnostic is emitted for corresponding pointee type
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001237 return nullptr;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001238 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001239
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001240 return new RSExportPointerType(Context, TypeName, PointeeET);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001241}
1242
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001243llvm::Type *RSExportPointerType::convertToLLVMType() const {
1244 llvm::Type *PointeeType = mPointeeType->getLLVMType();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001245 return llvm::PointerType::getUnqual(PointeeType);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001246}
1247
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001248bool RSExportPointerType::keep() {
1249 if (!RSExportType::keep())
1250 return false;
Zonr Chang641558f2010-10-12 21:07:06 +08001251 const_cast<RSExportType*>(mPointeeType)->keep();
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001252 return true;
Zonr Chang641558f2010-10-12 21:07:06 +08001253}
1254
1255bool RSExportPointerType::equals(const RSExportable *E) const {
1256 CHECK_PARENT_EQUALITY(RSExportType, E);
1257 return (static_cast<const RSExportPointerType*>(E)
1258 ->getPointeeType()->equals(getPointeeType()));
1259}
1260
zonr6315f762010-10-05 15:35:14 +08001261/***************************** RSExportVectorType *****************************/
zonr6315f762010-10-05 15:35:14 +08001262llvm::StringRef
1263RSExportVectorType::GetTypeName(const clang::ExtVectorType *EVT) {
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -07001264 const clang::Type *ElementType = GetExtVectorElementType(EVT);
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001265 llvm::StringRef name;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001266
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001267 if ((ElementType->getTypeClass() != clang::Type::Builtin))
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001268 return name;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001269
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -07001270 const clang::BuiltinType *BT =
1271 static_cast<const clang::BuiltinType*>(
1272 ElementType->getCanonicalTypeInternal().getTypePtr());
1273
Zonr Changa65ec162010-10-17 01:53:05 +08001274 if ((EVT->getNumElements() < 1) ||
1275 (EVT->getNumElements() > 4))
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001276 return name;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001277
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001278 BuiltinInfo *info = FindBuiltinType(BT->getKind());
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001279 if (info != nullptr) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001280 int I = EVT->getNumElements() - 1;
1281 if (I < kMaxVectorSize) {
1282 name = info->cname[I];
1283 } else {
1284 slangAssert(false && "Max vector is 4");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001285 }
1286 }
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001287 return name;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001288}
1289
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001290RSExportVectorType *RSExportVectorType::Create(RSContext *Context,
1291 const clang::ExtVectorType *EVT,
1292 const llvm::StringRef &TypeName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001293 bool Normalized) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001294 slangAssert(EVT != nullptr && EVT->getTypeClass() == clang::Type::ExtVector);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001295
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -07001296 const clang::Type *ElementType = GetExtVectorElementType(EVT);
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001297 DataType DT = RSExportPrimitiveType::GetDataType(Context, ElementType);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001298
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001299 if (DT != DataTypeUnknown)
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001300 return new RSExportVectorType(Context,
1301 TypeName,
1302 DT,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001303 Normalized,
1304 EVT->getNumElements());
1305 else
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001306 return nullptr;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001307}
1308
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001309llvm::Type *RSExportVectorType::convertToLLVMType() const {
1310 llvm::Type *ElementType = RSExportPrimitiveType::convertToLLVMType();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001311 return llvm::VectorType::get(ElementType, getNumElement());
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001312}
1313
Zonr Chang641558f2010-10-12 21:07:06 +08001314bool RSExportVectorType::equals(const RSExportable *E) const {
1315 CHECK_PARENT_EQUALITY(RSExportPrimitiveType, E);
1316 return (static_cast<const RSExportVectorType*>(E)->getNumElement()
1317 == getNumElement());
1318}
1319
Zonr Chang92b344a2010-10-05 20:39:03 +08001320/***************************** RSExportMatrixType *****************************/
1321RSExportMatrixType *RSExportMatrixType::Create(RSContext *Context,
1322 const clang::RecordType *RT,
1323 const llvm::StringRef &TypeName,
1324 unsigned Dim) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001325 slangAssert((RT != nullptr) && (RT->getTypeClass() == clang::Type::Record));
Stephen Hines6e6578a2011-02-07 18:05:48 -08001326 slangAssert((Dim > 1) && "Invalid dimension of matrix");
Zonr Chang92b344a2010-10-05 20:39:03 +08001327
1328 // Check whether the struct rs_matrix is in our expected form (but assume it's
1329 // correct if we're not sure whether it's correct or not)
1330 const clang::RecordDecl* RD = RT->getDecl();
1331 RD = RD->getDefinition();
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001332 if (RD != nullptr) {
Zonr Chang92b344a2010-10-05 20:39:03 +08001333 // Find definition, perform further examination
1334 if (RD->field_empty()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001335 Context->ReportError(
1336 RD->getLocation(),
1337 "invalid matrix struct: must have 1 field for saving values: '%0'")
1338 << RD->getName();
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001339 return nullptr;
Zonr Chang92b344a2010-10-05 20:39:03 +08001340 }
1341
1342 clang::RecordDecl::field_iterator FIT = RD->field_begin();
1343 const clang::FieldDecl *FD = *FIT;
1344 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001345 if ((FT == nullptr) || (FT->getTypeClass() != clang::Type::ConstantArray)) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001346 Context->ReportError(RD->getLocation(),
1347 "invalid matrix struct: first field should"
1348 " be an array with constant size: '%0'")
1349 << RD->getName();
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001350 return nullptr;
Zonr Chang92b344a2010-10-05 20:39:03 +08001351 }
1352 const clang::ConstantArrayType *CAT =
1353 static_cast<const clang::ConstantArrayType *>(FT);
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -07001354 const clang::Type *ElementType = GetConstantArrayElementType(CAT);
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001355 if ((ElementType == nullptr) ||
Zonr Chang92b344a2010-10-05 20:39:03 +08001356 (ElementType->getTypeClass() != clang::Type::Builtin) ||
Logan Chien9207a2e2011-10-21 15:39:28 +08001357 (static_cast<const clang::BuiltinType *>(ElementType)->getKind() !=
1358 clang::BuiltinType::Float)) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001359 Context->ReportError(RD->getLocation(),
1360 "invalid matrix struct: first field "
1361 "should be a float array: '%0'")
1362 << RD->getName();
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001363 return nullptr;
Zonr Chang92b344a2010-10-05 20:39:03 +08001364 }
1365
1366 if (CAT->getSize() != Dim * Dim) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001367 Context->ReportError(RD->getLocation(),
1368 "invalid matrix struct: first field "
1369 "should be an array with size %0: '%1'")
1370 << (Dim * Dim) << (RD->getName());
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001371 return nullptr;
Zonr Chang92b344a2010-10-05 20:39:03 +08001372 }
1373
1374 FIT++;
1375 if (FIT != RD->field_end()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001376 Context->ReportError(RD->getLocation(),
1377 "invalid matrix struct: must have "
1378 "exactly 1 field: '%0'")
1379 << RD->getName();
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001380 return nullptr;
Zonr Chang92b344a2010-10-05 20:39:03 +08001381 }
1382 }
1383
1384 return new RSExportMatrixType(Context, TypeName, Dim);
1385}
1386
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001387llvm::Type *RSExportMatrixType::convertToLLVMType() const {
Zonr Chang92b344a2010-10-05 20:39:03 +08001388 // Construct LLVM type:
1389 // struct {
1390 // float X[mDim * mDim];
1391 // }
1392
1393 llvm::LLVMContext &C = getRSContext()->getLLVMContext();
1394 llvm::ArrayType *X = llvm::ArrayType::get(llvm::Type::getFloatTy(C),
1395 mDim * mDim);
Stephen Hinesa67e4452011-07-19 15:39:26 -07001396 return llvm::StructType::get(C, X, false);
Zonr Chang92b344a2010-10-05 20:39:03 +08001397}
1398
Zonr Chang641558f2010-10-12 21:07:06 +08001399bool RSExportMatrixType::equals(const RSExportable *E) const {
1400 CHECK_PARENT_EQUALITY(RSExportType, E);
1401 return (static_cast<const RSExportMatrixType*>(E)->getDim() == getDim());
1402}
1403
Zonr Chang2e1dba62010-10-05 22:20:11 +08001404/************************* RSExportConstantArrayType *************************/
1405RSExportConstantArrayType
1406*RSExportConstantArrayType::Create(RSContext *Context,
1407 const clang::ConstantArrayType *CAT) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001408 slangAssert(CAT != nullptr && CAT->getTypeClass() == clang::Type::ConstantArray);
Zonr Chang2e1dba62010-10-05 22:20:11 +08001409
Stephen Hines6e6578a2011-02-07 18:05:48 -08001410 slangAssert((CAT->getSize().getActiveBits() < 32) && "array too large");
Zonr Chang2e1dba62010-10-05 22:20:11 +08001411
1412 unsigned Size = static_cast<unsigned>(CAT->getSize().getZExtValue());
Stephen Hines6e6578a2011-02-07 18:05:48 -08001413 slangAssert((Size > 0) && "Constant array should have size greater than 0");
Zonr Chang2e1dba62010-10-05 22:20:11 +08001414
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -07001415 const clang::Type *ElementType = GetConstantArrayElementType(CAT);
Zonr Chang2e1dba62010-10-05 22:20:11 +08001416 RSExportType *ElementET = RSExportType::Create(Context, ElementType);
1417
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001418 if (ElementET == nullptr) {
1419 return nullptr;
Zonr Chang2e1dba62010-10-05 22:20:11 +08001420 }
1421
1422 return new RSExportConstantArrayType(Context,
1423 ElementET,
1424 Size);
1425}
1426
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001427llvm::Type *RSExportConstantArrayType::convertToLLVMType() const {
Zonr Chang2e1dba62010-10-05 22:20:11 +08001428 return llvm::ArrayType::get(mElementType->getLLVMType(), getSize());
1429}
1430
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001431bool RSExportConstantArrayType::keep() {
1432 if (!RSExportType::keep())
1433 return false;
Zonr Chang641558f2010-10-12 21:07:06 +08001434 const_cast<RSExportType*>(mElementType)->keep();
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001435 return true;
Zonr Chang641558f2010-10-12 21:07:06 +08001436}
1437
1438bool RSExportConstantArrayType::equals(const RSExportable *E) const {
1439 CHECK_PARENT_EQUALITY(RSExportType, E);
Stephen Hinesa7b75182010-12-07 15:49:49 -08001440 const RSExportConstantArrayType *RHS =
1441 static_cast<const RSExportConstantArrayType*>(E);
1442 return ((getSize() == RHS->getSize()) &&
1443 (getElementType()->equals(RHS->getElementType())));
Zonr Chang641558f2010-10-12 21:07:06 +08001444}
1445
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001446/**************************** RSExportRecordType ****************************/
1447RSExportRecordType *RSExportRecordType::Create(RSContext *Context,
1448 const clang::RecordType *RT,
1449 const llvm::StringRef &TypeName,
1450 bool mIsArtificial) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001451 slangAssert(RT != nullptr && RT->getTypeClass() == clang::Type::Record);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001452
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001453 const clang::RecordDecl *RD = RT->getDecl();
Stephen Hines6e6578a2011-02-07 18:05:48 -08001454 slangAssert(RD->isStruct());
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001455
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001456 RD = RD->getDefinition();
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001457 if (RD == nullptr) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001458 slangAssert(false && "struct is not defined in this module");
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001459 return nullptr;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001460 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001461
Zonr Chang0da0a7d2010-10-05 21:26:37 +08001462 // Struct layout construct by clang. We rely on this for obtaining the
1463 // alloc size of a struct and offset of every field in that struct.
1464 const clang::ASTRecordLayout *RL =
Stephen Hines9e5b5032010-11-03 13:19:14 -07001465 &Context->getASTContext().getASTRecordLayout(RD);
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001466 slangAssert((RL != nullptr) &&
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001467 "Failed to retrieve the struct layout from Clang.");
Zonr Chang0da0a7d2010-10-05 21:26:37 +08001468
1469 RSExportRecordType *ERT =
1470 new RSExportRecordType(Context,
1471 TypeName,
1472 RD->hasAttr<clang::PackedAttr>(),
1473 mIsArtificial,
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -07001474 RL->getDataSize().getQuantity(),
Shih-wei Liao68318a12011-03-11 05:56:15 -08001475 RL->getSize().getQuantity());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001476 unsigned int Index = 0;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001477
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001478 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
1479 FE = RD->field_end();
1480 FI != FE;
Shih-wei Liao91a37832010-10-03 19:11:51 -07001481 FI++, Index++) {
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001482
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001483 // FIXME: All fields should be primitive type
Shih-wei Liao43730fe2012-08-02 23:06:18 -07001484 slangAssert(FI->getKind() == clang::Decl::Field);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001485 clang::FieldDecl *FD = *FI;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001486
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001487 if (FD->isBitField()) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001488 return nullptr;
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001489 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001490
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001491 // Type
zonr6315f762010-10-05 15:35:14 +08001492 RSExportType *ET = RSExportElement::CreateFromDecl(Context, FD);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001493
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001494 if (ET != nullptr) {
Zonr Chang0da0a7d2010-10-05 21:26:37 +08001495 ERT->mFields.push_back(
1496 new Field(ET, FD->getName(), ERT,
1497 static_cast<size_t>(RL->getFieldOffset(Index) >> 3)));
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001498 } else {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001499 Context->ReportError(RD->getLocation(),
1500 "field type cannot be exported: '%0.%1'")
1501 << RD->getName() << FD->getName();
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001502 return nullptr;
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001503 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001504 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001505
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001506 return ERT;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001507}
1508
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001509llvm::Type *RSExportRecordType::convertToLLVMType() const {
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001510 // Create an opaque type since struct may reference itself recursively.
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001511
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001512 // TODO(sliao): LLVM took out the OpaqueType. Any other to migrate to?
1513 std::vector<llvm::Type*> FieldTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001514
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001515 for (const_field_iterator FI = fields_begin(), FE = fields_end();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001516 FI != FE;
1517 FI++) {
1518 const Field *F = *FI;
1519 const RSExportType *FET = F->getType();
Shih-wei Liao1f0d88f2010-06-25 00:15:24 -07001520
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001521 FieldTypes.push_back(FET->getLLVMType());
1522 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001523
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001524 llvm::StructType *ST = llvm::StructType::get(getRSContext()->getLLVMContext(),
1525 FieldTypes,
1526 mIsPacked);
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001527 if (ST != nullptr) {
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001528 return ST;
1529 } else {
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001530 return nullptr;
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001531 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001532}
Zonr Chang641558f2010-10-12 21:07:06 +08001533
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001534bool RSExportRecordType::keep() {
1535 if (!RSExportType::keep())
1536 return false;
Zonr Chang641558f2010-10-12 21:07:06 +08001537 for (std::list<const Field*>::iterator I = mFields.begin(),
1538 E = mFields.end();
1539 I != E;
1540 I++) {
1541 const_cast<RSExportType*>((*I)->getType())->keep();
1542 }
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001543 return true;
Zonr Chang641558f2010-10-12 21:07:06 +08001544}
1545
1546bool RSExportRecordType::equals(const RSExportable *E) const {
1547 CHECK_PARENT_EQUALITY(RSExportType, E);
1548
1549 const RSExportRecordType *ERT = static_cast<const RSExportRecordType*>(E);
1550
1551 if (ERT->getFields().size() != getFields().size())
1552 return false;
1553
1554 const_field_iterator AI = fields_begin(), BI = ERT->fields_begin();
1555
1556 for (unsigned i = 0, e = getFields().size(); i != e; i++) {
1557 if (!(*AI)->getType()->equals((*BI)->getType()))
1558 return false;
1559 AI++;
1560 BI++;
1561 }
1562
1563 return true;
1564}
Stephen Hinese639eb52010-11-08 19:27:20 -08001565
Jason Sams1b6a0882012-03-12 15:07:58 -07001566void RSExportType::convertToRTD(RSReflectionTypeData *rtd) const {
1567 memset(rtd, 0, sizeof(*rtd));
1568 rtd->vecSize = 1;
1569
1570 switch(getClass()) {
1571 case RSExportType::ExportClassPrimitive: {
1572 const RSExportPrimitiveType *EPT = static_cast<const RSExportPrimitiveType*>(this);
1573 rtd->type = RSExportPrimitiveType::getRSReflectionType(EPT);
1574 return;
1575 }
1576 case RSExportType::ExportClassPointer: {
1577 const RSExportPointerType *EPT = static_cast<const RSExportPointerType*>(this);
1578 const RSExportType *PointeeType = EPT->getPointeeType();
1579 PointeeType->convertToRTD(rtd);
1580 rtd->isPointer = true;
1581 return;
1582 }
1583 case RSExportType::ExportClassVector: {
1584 const RSExportVectorType *EVT = static_cast<const RSExportVectorType*>(this);
1585 rtd->type = EVT->getRSReflectionType(EVT);
1586 rtd->vecSize = EVT->getNumElement();
1587 return;
1588 }
1589 case RSExportType::ExportClassMatrix: {
1590 const RSExportMatrixType *EMT = static_cast<const RSExportMatrixType*>(this);
1591 unsigned Dim = EMT->getDim();
1592 slangAssert((Dim >= 2) && (Dim <= 4));
1593 rtd->type = &gReflectionTypes[15 + Dim-2];
1594 return;
1595 }
1596 case RSExportType::ExportClassConstantArray: {
1597 const RSExportConstantArrayType* CAT =
1598 static_cast<const RSExportConstantArrayType*>(this);
1599 CAT->getElementType()->convertToRTD(rtd);
1600 rtd->arraySize = CAT->getSize();
1601 return;
1602 }
1603 case RSExportType::ExportClassRecord: {
1604 slangAssert(!"RSExportType::ExportClassRecord not implemented");
1605 return;// RS_TYPE_CLASS_NAME_PREFIX + ET->getName() + ".Item";
1606 }
1607 default: {
1608 slangAssert(false && "Unknown class of type");
1609 }
1610 }
1611}
1612
1613
Stephen Hinese639eb52010-11-08 19:27:20 -08001614} // namespace slang