blob: d0b60947ea5b8ea9755495f0a3fc709412ea01cc [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[] = {
Pirama Arumuga Nainar7966bcf2015-04-07 11:09:14 -070050 {PrimitiveDataType, "FLOAT_16", "F16", 16, "half", "short", "Half", "Half", false},
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -070051 {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
Chris Wailes5abbe0e2014-08-12 15:58:29 -070062 {PrimitiveDataType, "BOOLEAN", "BOOLEAN", 8, "bool", "boolean", nullptr, nullptr, false},
Stephen Hinesfdd1ba12012-03-08 17:26:32 -080063
Chris Wailes5abbe0e2014-08-12 15:58:29 -070064 {PrimitiveDataType, "UNSIGNED_5_6_5", nullptr, 16, nullptr, nullptr, nullptr, nullptr, false},
65 {PrimitiveDataType, "UNSIGNED_5_5_5_1", nullptr, 16, nullptr, nullptr, nullptr, nullptr, false},
66 {PrimitiveDataType, "UNSIGNED_4_4_4_4", nullptr, 16, nullptr, nullptr, nullptr, nullptr, false},
Stephen Hinesfdd1ba12012-03-08 17:26:32 -080067
Chris Wailes5abbe0e2014-08-12 15:58:29 -070068 {MatrixDataType, "MATRIX_2X2", nullptr, 4*32, "rsMatrix_2x2", "Matrix2f", nullptr, nullptr, false},
69 {MatrixDataType, "MATRIX_3X3", nullptr, 9*32, "rsMatrix_3x3", "Matrix3f", nullptr, nullptr, false},
70 {MatrixDataType, "MATRIX_4X4", nullptr, 16*32, "rsMatrix_4x4", "Matrix4f", nullptr, nullptr, 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.
Chris Wailes5abbe0e2014-08-12 15:58:29 -070074 {ObjectDataType, "RS_ELEMENT", "ELEMENT", 32, "Element", "Element", nullptr, nullptr, false},
75 {ObjectDataType, "RS_TYPE", "TYPE", 32, "Type", "Type", nullptr, nullptr, false},
76 {ObjectDataType, "RS_ALLOCATION", "ALLOCATION", 32, "Allocation", "Allocation", nullptr, nullptr, false},
77 {ObjectDataType, "RS_SAMPLER", "SAMPLER", 32, "Sampler", "Sampler", nullptr, nullptr, false},
78 {ObjectDataType, "RS_SCRIPT", "SCRIPT", 32, "Script", "Script", nullptr, nullptr, false},
79 {ObjectDataType, "RS_MESH", "MESH", 32, "Mesh", "Mesh", nullptr, nullptr, false},
80 {ObjectDataType, "RS_PATH", "PATH", 32, "Path", "Path", nullptr, nullptr, false},
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -070081
Chris Wailes5abbe0e2014-08-12 15:58:29 -070082 {ObjectDataType, "RS_PROGRAM_FRAGMENT", "PROGRAM_FRAGMENT", 32, "ProgramFragment", "ProgramFragment", nullptr, nullptr, false},
83 {ObjectDataType, "RS_PROGRAM_VERTEX", "PROGRAM_VERTEX", 32, "ProgramVertex", "ProgramVertex", nullptr, nullptr, false},
84 {ObjectDataType, "RS_PROGRAM_RASTER", "PROGRAM_RASTER", 32, "ProgramRaster", "ProgramRaster", nullptr, nullptr, false},
85 {ObjectDataType, "RS_PROGRAM_STORE", "PROGRAM_STORE", 32, "ProgramStore", "ProgramStore", nullptr, nullptr, false},
86 {ObjectDataType, "RS_FONT", "FONT", 32, "Font", "Font", nullptr, nullptr, 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"}},
Pirama Arumuga Nainar7966bcf2015-04-07 11:09:14 -0700133 {clang::BuiltinType::Half, DataTypeFloat16,
134 {"half", "half2", "half3", "half4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700135 {clang::BuiltinType::Float, DataTypeFloat32,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700136 {"float", "float2", "float3", "float4"}},
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700137 {clang::BuiltinType::Double, DataTypeFloat64,
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700138 {"double", "double2", "double3", "double4"}},
139};
140const int BuiltinInfoTableCount = sizeof(BuiltinInfoTable) / sizeof(BuiltinInfoTable[0]);
141
142struct NameAndPrimitiveType {
143 const char *name;
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700144 DataType dataType;
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700145};
146
147static NameAndPrimitiveType MatrixAndObjectDataTypes[] = {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700148 {"rs_matrix2x2", DataTypeRSMatrix2x2},
149 {"rs_matrix3x3", DataTypeRSMatrix3x3},
150 {"rs_matrix4x4", DataTypeRSMatrix4x4},
151 {"rs_element", DataTypeRSElement},
152 {"rs_type", DataTypeRSType},
153 {"rs_allocation", DataTypeRSAllocation},
154 {"rs_sampler", DataTypeRSSampler},
155 {"rs_script", DataTypeRSScript},
156 {"rs_mesh", DataTypeRSMesh},
157 {"rs_path", DataTypeRSPath},
158 {"rs_program_fragment", DataTypeRSProgramFragment},
159 {"rs_program_vertex", DataTypeRSProgramVertex},
160 {"rs_program_raster", DataTypeRSProgramRaster},
161 {"rs_program_store", DataTypeRSProgramStore},
162 {"rs_font", DataTypeRSFont},
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700163};
164
165const int MatrixAndObjectDataTypesCount =
166 sizeof(MatrixAndObjectDataTypes) / sizeof(MatrixAndObjectDataTypes[0]);
167
Stephen Hines24e79f62010-12-13 12:55:57 -0800168static const clang::Type *TypeExportableHelper(
169 const clang::Type *T,
170 llvm::SmallPtrSet<const clang::Type*, 8>& SPS,
Stephen Hines48d893d2013-12-06 18:00:27 -0800171 slang::RSContext *Context,
Stephen Hines24e79f62010-12-13 12:55:57 -0800172 const clang::VarDecl *VD,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700173 const clang::RecordDecl *TopLevelRecord);
Stephen Hines24e79f62010-12-13 12:55:57 -0800174
Tim Murrayee4016d2014-04-10 15:49:08 -0700175template <unsigned N>
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800176static void ReportTypeError(slang::RSContext *Context,
Stephen Hines11274a72012-09-26 19:14:20 -0700177 const clang::NamedDecl *ND,
Stephen Hines24e79f62010-12-13 12:55:57 -0800178 const clang::RecordDecl *TopLevelRecord,
Tim Murrayee4016d2014-04-10 15:49:08 -0700179 const char (&Message)[N],
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700180 unsigned int TargetAPI = 0) {
Stephen Hines24e79f62010-12-13 12:55:57 -0800181 // Attempt to use the type declaration first (if we have one).
182 // Fall back to the variable definition, if we are looking at something
183 // like an array declaration that can't be exported.
184 if (TopLevelRecord) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800185 Context->ReportError(TopLevelRecord->getLocation(), Message)
186 << TopLevelRecord->getName() << TargetAPI;
Stephen Hines11274a72012-09-26 19:14:20 -0700187 } else if (ND) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800188 Context->ReportError(ND->getLocation(), Message) << ND->getName()
189 << TargetAPI;
Stephen Hines24e79f62010-12-13 12:55:57 -0800190 } else {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800191 slangAssert(false && "Variables should be validated before exporting");
Stephen Hines24e79f62010-12-13 12:55:57 -0800192 }
Stephen Hines24e79f62010-12-13 12:55:57 -0800193}
194
195static const clang::Type *ConstantArrayTypeExportableHelper(
196 const clang::ConstantArrayType *CAT,
197 llvm::SmallPtrSet<const clang::Type*, 8>& SPS,
Stephen Hines48d893d2013-12-06 18:00:27 -0800198 slang::RSContext *Context,
Stephen Hines24e79f62010-12-13 12:55:57 -0800199 const clang::VarDecl *VD,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700200 const clang::RecordDecl *TopLevelRecord) {
Stephen Hines24e79f62010-12-13 12:55:57 -0800201 // Check element type
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700202 const clang::Type *ElementType = GetConstantArrayElementType(CAT);
Stephen Hines24e79f62010-12-13 12:55:57 -0800203 if (ElementType->isArrayType()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800204 ReportTypeError(Context, VD, TopLevelRecord,
Logan Chien9207a2e2011-10-21 15:39:28 +0800205 "multidimensional arrays cannot be exported: '%0'");
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700206 return nullptr;
Stephen Hines24e79f62010-12-13 12:55:57 -0800207 } else if (ElementType->isExtVectorType()) {
208 const clang::ExtVectorType *EVT =
209 static_cast<const clang::ExtVectorType*>(ElementType);
210 unsigned numElements = EVT->getNumElements();
211
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700212 const clang::Type *BaseElementType = GetExtVectorElementType(EVT);
Stephen Hines24e79f62010-12-13 12:55:57 -0800213 if (!RSExportPrimitiveType::IsPrimitiveType(BaseElementType)) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800214 ReportTypeError(Context, VD, TopLevelRecord,
Logan Chien9207a2e2011-10-21 15:39:28 +0800215 "vectors of non-primitive types cannot be exported: '%0'");
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700216 return nullptr;
Stephen Hines24e79f62010-12-13 12:55:57 -0800217 }
218
219 if (numElements == 3 && CAT->getSize() != 1) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800220 ReportTypeError(Context, VD, TopLevelRecord,
Logan Chien9207a2e2011-10-21 15:39:28 +0800221 "arrays of width 3 vector types cannot be exported: '%0'");
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700222 return nullptr;
Stephen Hines24e79f62010-12-13 12:55:57 -0800223 }
224 }
225
Stephen Hines48d893d2013-12-06 18:00:27 -0800226 if (TypeExportableHelper(ElementType, SPS, Context, VD,
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700227 TopLevelRecord) == nullptr) {
228 return nullptr;
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700229 } else {
Stephen Hines24e79f62010-12-13 12:55:57 -0800230 return CAT;
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700231 }
Stephen Hines24e79f62010-12-13 12:55:57 -0800232}
233
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700234BuiltinInfo *FindBuiltinType(clang::BuiltinType::Kind builtinTypeKind) {
235 for (int i = 0; i < BuiltinInfoTableCount; i++) {
236 if (builtinTypeKind == BuiltinInfoTable[i].builtinTypeKind) {
237 return &BuiltinInfoTable[i];
238 }
239 }
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700240 return nullptr;
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700241}
242
Stephen Hines24e79f62010-12-13 12:55:57 -0800243static const clang::Type *TypeExportableHelper(
Stephen Hinese67239d2012-02-24 15:08:36 -0800244 clang::Type const *T,
245 llvm::SmallPtrSet<clang::Type const *, 8> &SPS,
Stephen Hines48d893d2013-12-06 18:00:27 -0800246 slang::RSContext *Context,
Stephen Hinese67239d2012-02-24 15:08:36 -0800247 clang::VarDecl const *VD,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700248 clang::RecordDecl const *TopLevelRecord) {
Stephen Hinese5e64432010-12-02 18:48:20 -0800249 // Normalize first
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700250 if ((T = GetCanonicalType(T)) == nullptr)
251 return nullptr;
Stephen Hinese5e64432010-12-02 18:48:20 -0800252
253 if (SPS.count(T))
254 return T;
255
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700256 const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr();
257
Stephen Hinese5e64432010-12-02 18:48:20 -0800258 switch (T->getTypeClass()) {
259 case clang::Type::Builtin: {
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700260 const clang::BuiltinType *BT = static_cast<const clang::BuiltinType*>(CTI);
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700261 return FindBuiltinType(BT->getKind()) == nullptr ? nullptr : T;
Stephen Hinese5e64432010-12-02 18:48:20 -0800262 }
263 case clang::Type::Record: {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700264 if (RSExportPrimitiveType::GetRSSpecificType(T) != DataTypeUnknown) {
Stephen Hinese5e64432010-12-02 18:48:20 -0800265 return T; // RS object type, no further checks are needed
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700266 }
Stephen Hinese5e64432010-12-02 18:48:20 -0800267
268 // Check internal struct
Stephen Hinescd440ee2010-12-06 14:57:04 -0800269 if (T->isUnionType()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800270 ReportTypeError(Context, VD, T->getAsUnionType()->getDecl(),
Logan Chien9207a2e2011-10-21 15:39:28 +0800271 "unions cannot be exported: '%0'");
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700272 return nullptr;
Stephen Hinescd440ee2010-12-06 14:57:04 -0800273 } else if (!T->isStructureType()) {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800274 slangAssert(false && "Unknown type cannot be exported");
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700275 return nullptr;
Stephen Hinescd440ee2010-12-06 14:57:04 -0800276 }
277
Stephen Hines24e79f62010-12-13 12:55:57 -0800278 clang::RecordDecl *RD = T->getAsStructureType()->getDecl();
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700279 if (RD != nullptr) {
Stephen Hinese5e64432010-12-02 18:48:20 -0800280 RD = RD->getDefinition();
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700281 if (RD == nullptr) {
282 ReportTypeError(Context, nullptr, T->getAsStructureType()->getDecl(),
Logan Chien9207a2e2011-10-21 15:39:28 +0800283 "struct is not defined in this module");
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700284 return nullptr;
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800285 }
286 }
Stephen Hinese5e64432010-12-02 18:48:20 -0800287
288 if (!TopLevelRecord) {
289 TopLevelRecord = RD;
290 }
291 if (RD->getName().empty()) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700292 ReportTypeError(Context, nullptr, RD,
Logan Chien9207a2e2011-10-21 15:39:28 +0800293 "anonymous structures cannot be exported");
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700294 return nullptr;
Stephen Hinese5e64432010-12-02 18:48:20 -0800295 }
296
297 // Fast check
298 if (RD->hasFlexibleArrayMember() || RD->hasObjectMember())
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700299 return nullptr;
Stephen Hinese5e64432010-12-02 18:48:20 -0800300
301 // Insert myself into checking set
302 SPS.insert(T);
303
304 // Check all element
305 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
306 FE = RD->field_end();
307 FI != FE;
308 FI++) {
309 const clang::FieldDecl *FD = *FI;
310 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700311 FT = GetCanonicalType(FT);
Stephen Hinese5e64432010-12-02 18:48:20 -0800312
Stephen Hines48d893d2013-12-06 18:00:27 -0800313 if (!TypeExportableHelper(FT, SPS, Context, VD, TopLevelRecord)) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700314 return nullptr;
Stephen Hinese5e64432010-12-02 18:48:20 -0800315 }
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800316
317 // We don't support bit fields yet
318 //
319 // TODO(zonr/srhines): allow bit fields of size 8, 16, 32
320 if (FD->isBitField()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800321 Context->ReportError(
322 FD->getLocation(),
323 "bit fields are not able to be exported: '%0.%1'")
324 << RD->getName() << FD->getName();
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700325 return nullptr;
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800326 }
Stephen Hinese5e64432010-12-02 18:48:20 -0800327 }
328
329 return T;
330 }
331 case clang::Type::Pointer: {
332 if (TopLevelRecord) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800333 ReportTypeError(Context, VD, TopLevelRecord,
Stephen Hinese23d82b2015-06-11 17:00:04 -0700334 "structures containing pointers cannot be used as the type of "
335 "an exported global variable or the parameter to an exported "
336 "function: '%0'");
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700337 return nullptr;
Stephen Hinese5e64432010-12-02 18:48:20 -0800338 }
Stephen Hines24e79f62010-12-13 12:55:57 -0800339
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700340 const clang::PointerType *PT = static_cast<const clang::PointerType*>(CTI);
341 const clang::Type *PointeeType = GetPointeeType(PT);
Stephen Hinese5e64432010-12-02 18:48:20 -0800342
Stephen Hinesaa82e742012-04-11 12:38:17 -0700343 if (PointeeType->getTypeClass() == clang::Type::Pointer) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800344 ReportTypeError(Context, VD, TopLevelRecord,
Stephen Hinesaa82e742012-04-11 12:38:17 -0700345 "multiple levels of pointers cannot be exported: '%0'");
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700346 return nullptr;
Stephen Hinesaa82e742012-04-11 12:38:17 -0700347 }
Stephen Hinese5e64432010-12-02 18:48:20 -0800348 // We don't support pointer with array-type pointee or unsupported pointee
349 // type
350 if (PointeeType->isArrayType() ||
Stephen Hines48d893d2013-12-06 18:00:27 -0800351 (TypeExportableHelper(PointeeType, SPS, Context, VD,
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700352 TopLevelRecord) == nullptr))
353 return nullptr;
Stephen Hinese5e64432010-12-02 18:48:20 -0800354 else
355 return T;
356 }
357 case clang::Type::ExtVector: {
358 const clang::ExtVectorType *EVT =
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700359 static_cast<const clang::ExtVectorType*>(CTI);
Stephen Hinese5e64432010-12-02 18:48:20 -0800360 // Only vector with size 2, 3 and 4 are supported.
361 if (EVT->getNumElements() < 2 || EVT->getNumElements() > 4)
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700362 return nullptr;
Stephen Hinese5e64432010-12-02 18:48:20 -0800363
364 // Check base element type
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700365 const clang::Type *ElementType = GetExtVectorElementType(EVT);
Stephen Hinese5e64432010-12-02 18:48:20 -0800366
367 if ((ElementType->getTypeClass() != clang::Type::Builtin) ||
Stephen Hines48d893d2013-12-06 18:00:27 -0800368 (TypeExportableHelper(ElementType, SPS, Context, VD,
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700369 TopLevelRecord) == nullptr))
370 return nullptr;
Stephen Hinese5e64432010-12-02 18:48:20 -0800371 else
372 return T;
373 }
374 case clang::Type::ConstantArray: {
375 const clang::ConstantArrayType *CAT =
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700376 static_cast<const clang::ConstantArrayType*>(CTI);
Stephen Hinese5e64432010-12-02 18:48:20 -0800377
Stephen Hines48d893d2013-12-06 18:00:27 -0800378 return ConstantArrayTypeExportableHelper(CAT, SPS, Context, VD,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700379 TopLevelRecord);
Stephen Hinese5e64432010-12-02 18:48:20 -0800380 }
Stephen Hines48d893d2013-12-06 18:00:27 -0800381 case clang::Type::Enum: {
382 // FIXME: We currently convert enums to integers, rather than reflecting
383 // a more complete (and nicer type-safe Java version).
384 return Context->getASTContext().IntTy.getTypePtr();
385 }
Stephen Hinese5e64432010-12-02 18:48:20 -0800386 default: {
Stephen Hines48d893d2013-12-06 18:00:27 -0800387 slangAssert(false && "Unknown type cannot be validated");
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700388 return nullptr;
Stephen Hinese5e64432010-12-02 18:48:20 -0800389 }
390 }
391}
392
393// Return the type that can be used to create RSExportType, will always return
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700394// the canonical type.
395//
396// If the Type T is not exportable, this function returns nullptr. DiagEngine is
397// used to generate proper Clang diagnostic messages when a non-exportable type
398// is detected. TopLevelRecord is used to capture the highest struct (in the
399// case of a nested hierarchy) for detecting other types that cannot be exported
400// (mostly pointers within a struct).
Stephen Hinese5e64432010-12-02 18:48:20 -0800401static const clang::Type *TypeExportable(const clang::Type *T,
Stephen Hines48d893d2013-12-06 18:00:27 -0800402 slang::RSContext *Context,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700403 const clang::VarDecl *VD) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700404 llvm::SmallPtrSet<const clang::Type*, 8> SPS =
405 llvm::SmallPtrSet<const clang::Type*, 8>();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700406
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700407 return TypeExportableHelper(T, SPS, Context, VD, nullptr);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700408}
409
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800410static bool ValidateRSObjectInVarDecl(slang::RSContext *Context,
411 clang::VarDecl *VD, bool InCompositeType,
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700412 unsigned int TargetAPI) {
413 if (TargetAPI < SLANG_JB_TARGET_API) {
414 // Only if we are already in a composite type (like an array or structure).
415 if (InCompositeType) {
416 // Only if we are actually exported (i.e. non-static).
Stephen Hines44f10062013-06-13 00:33:23 -0700417 if (VD->hasLinkage() &&
418 (VD->getFormalLinkage() == clang::ExternalLinkage)) {
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700419 // Only if we are not a pointer to an object.
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700420 const clang::Type *T = GetCanonicalType(VD->getType().getTypePtr());
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700421 if (T->getTypeClass() != clang::Type::Pointer) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700422 ReportTypeError(Context, VD, nullptr,
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700423 "arrays/structures containing RS object types "
424 "cannot be exported in target API < %1: '%0'",
425 SLANG_JB_TARGET_API);
426 return false;
427 }
428 }
429 }
430 }
431
432 return true;
433}
434
Stephen Hines11274a72012-09-26 19:14:20 -0700435// Helper function for ValidateType(). We do a recursive descent on the
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700436// type hierarchy to ensure that we can properly export/handle the
437// declaration.
438// \return true if the variable declaration is valid,
439// false if it is invalid (along with proper diagnostics).
440//
Stephen Hines11274a72012-09-26 19:14:20 -0700441// C - ASTContext (for diagnostics + builtin types).
442// T - sub-type that we are validating.
443// ND - (optional) top-level named declaration that we are validating.
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700444// SPS - set of types we have already seen/validated.
445// InCompositeType - true if we are within an outer composite type.
446// UnionDecl - set if we are in a sub-type of a union.
447// TargetAPI - target SDK API level.
Stephen Hines11274a72012-09-26 19:14:20 -0700448// IsFilterscript - whether or not we are compiling for Filterscript
Stephen Hinese23d82b2015-06-11 17:00:04 -0700449// IsExtern - is this type externally visible (i.e. extern global or parameter
450// to an extern function)
Stephen Hines11274a72012-09-26 19:14:20 -0700451static bool ValidateTypeHelper(
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800452 slang::RSContext *Context,
Stephen Hines11274a72012-09-26 19:14:20 -0700453 clang::ASTContext &C,
Stephen Hines78e69cb2011-04-22 15:03:19 -0700454 const clang::Type *&T,
Stephen Hines11274a72012-09-26 19:14:20 -0700455 clang::NamedDecl *ND,
456 clang::SourceLocation Loc,
Stephen Hines78e69cb2011-04-22 15:03:19 -0700457 llvm::SmallPtrSet<const clang::Type*, 8>& SPS,
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700458 bool InCompositeType,
459 clang::RecordDecl *UnionDecl,
Stephen Hines11274a72012-09-26 19:14:20 -0700460 unsigned int TargetAPI,
Stephen Hinese23d82b2015-06-11 17:00:04 -0700461 bool IsFilterscript,
462 bool IsExtern) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700463 if ((T = GetCanonicalType(T)) == nullptr)
Stephen Hines78e69cb2011-04-22 15:03:19 -0700464 return true;
465
466 if (SPS.count(T))
467 return true;
468
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700469 const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr();
470
Stephen Hines78e69cb2011-04-22 15:03:19 -0700471 switch (T->getTypeClass()) {
472 case clang::Type::Record: {
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700473 if (RSExportPrimitiveType::IsRSObjectType(T)) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700474 clang::VarDecl *VD = (ND ? llvm::dyn_cast<clang::VarDecl>(ND) : nullptr);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800475 if (VD && !ValidateRSObjectInVarDecl(Context, VD, InCompositeType,
476 TargetAPI)) {
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700477 return false;
478 }
479 }
480
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700481 if (RSExportPrimitiveType::GetRSSpecificType(T) != DataTypeUnknown) {
Stephen Hines78e69cb2011-04-22 15:03:19 -0700482 if (!UnionDecl) {
483 return true;
484 } else if (RSExportPrimitiveType::IsRSObjectType(T)) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700485 ReportTypeError(Context, nullptr, UnionDecl,
Stephen Hines78e69cb2011-04-22 15:03:19 -0700486 "unions containing RS object types are not allowed");
487 return false;
488 }
489 }
490
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700491 clang::RecordDecl *RD = nullptr;
Stephen Hines78e69cb2011-04-22 15:03:19 -0700492
493 // Check internal struct
494 if (T->isUnionType()) {
495 RD = T->getAsUnionType()->getDecl();
496 UnionDecl = RD;
497 } else if (T->isStructureType()) {
498 RD = T->getAsStructureType()->getDecl();
499 } else {
500 slangAssert(false && "Unknown type cannot be exported");
501 return false;
502 }
503
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700504 if (RD != nullptr) {
Stephen Hines78e69cb2011-04-22 15:03:19 -0700505 RD = RD->getDefinition();
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700506 if (RD == nullptr) {
Stephen Hines78e69cb2011-04-22 15:03:19 -0700507 // FIXME
508 return true;
509 }
510 }
511
512 // Fast check
513 if (RD->hasFlexibleArrayMember() || RD->hasObjectMember())
514 return false;
515
516 // Insert myself into checking set
517 SPS.insert(T);
518
519 // Check all elements
520 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
521 FE = RD->field_end();
522 FI != FE;
523 FI++) {
524 const clang::FieldDecl *FD = *FI;
525 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700526 FT = GetCanonicalType(FT);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700527
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800528 if (!ValidateTypeHelper(Context, C, FT, ND, Loc, SPS, true, UnionDecl,
Stephen Hinese23d82b2015-06-11 17:00:04 -0700529 TargetAPI, IsFilterscript, IsExtern)) {
Stephen Hines78e69cb2011-04-22 15:03:19 -0700530 return false;
531 }
532 }
533
534 return true;
535 }
536
537 case clang::Type::Builtin: {
Stephen Hines11274a72012-09-26 19:14:20 -0700538 if (IsFilterscript) {
539 clang::QualType QT = T->getCanonicalTypeInternal();
540 if (QT == C.DoubleTy ||
541 QT == C.LongDoubleTy ||
542 QT == C.LongTy ||
543 QT == C.LongLongTy) {
Stephen Hines11274a72012-09-26 19:14:20 -0700544 if (ND) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800545 Context->ReportError(
546 Loc,
Stephen Hines11274a72012-09-26 19:14:20 -0700547 "Builtin types > 32 bits in size are forbidden in "
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800548 "Filterscript: '%0'")
549 << ND->getName();
Stephen Hines11274a72012-09-26 19:14:20 -0700550 } else {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800551 Context->ReportError(
552 Loc,
Stephen Hines11274a72012-09-26 19:14:20 -0700553 "Builtin types > 32 bits in size are forbidden in "
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800554 "Filterscript");
Stephen Hines11274a72012-09-26 19:14:20 -0700555 }
556 return false;
557 }
558 }
Stephen Hines78e69cb2011-04-22 15:03:19 -0700559 break;
560 }
561
562 case clang::Type::Pointer: {
Stephen Hines11274a72012-09-26 19:14:20 -0700563 if (IsFilterscript) {
564 if (ND) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800565 Context->ReportError(Loc,
566 "Pointers are forbidden in Filterscript: '%0'")
567 << ND->getName();
Stephen Hines11274a72012-09-26 19:14:20 -0700568 return false;
569 } else {
570 // TODO(srhines): Find a better way to handle expressions (i.e. no
571 // NamedDecl) involving pointers in FS that should be allowed.
572 // An example would be calls to library functions like
573 // rsMatrixMultiply() that take rs_matrixNxN * types.
574 }
575 }
576
Stephen Hinese23d82b2015-06-11 17:00:04 -0700577 // Forbid pointers in structures that are externally visible.
578 if (InCompositeType && IsExtern) {
579 if (ND) {
580 Context->ReportError(Loc,
581 "structures containing pointers cannot be used as the type of "
582 "an exported global variable or the parameter to an exported "
583 "function: '%0'")
584 << ND->getName();
585 } else {
586 Context->ReportError(Loc,
587 "structures containing pointers cannot be used as the type of "
588 "an exported global variable or the parameter to an exported "
589 "function");
590 }
591 return false;
592 }
593
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700594 const clang::PointerType *PT = static_cast<const clang::PointerType*>(CTI);
595 const clang::Type *PointeeType = GetPointeeType(PT);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700596
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800597 return ValidateTypeHelper(Context, C, PointeeType, ND, Loc, SPS,
598 InCompositeType, UnionDecl, TargetAPI,
Stephen Hinese23d82b2015-06-11 17:00:04 -0700599 IsFilterscript, IsExtern);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700600 }
601
602 case clang::Type::ExtVector: {
603 const clang::ExtVectorType *EVT =
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700604 static_cast<const clang::ExtVectorType*>(CTI);
605 const clang::Type *ElementType = GetExtVectorElementType(EVT);
Stephen Hinesb13b85e2012-08-31 19:27:48 -0700606 if (TargetAPI < SLANG_ICS_TARGET_API &&
607 InCompositeType &&
Stephen Hinesfdae63e2012-11-28 14:42:44 -0800608 EVT->getNumElements() == 3 &&
609 ND &&
Stephen Hines44f10062013-06-13 00:33:23 -0700610 ND->getFormalLinkage() == clang::ExternalLinkage) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700611 ReportTypeError(Context, ND, nullptr,
Stephen Hinesb13b85e2012-08-31 19:27:48 -0700612 "structs containing vectors of dimension 3 cannot "
613 "be exported at this API level: '%0'");
614 return false;
615 }
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800616 return ValidateTypeHelper(Context, C, ElementType, ND, Loc, SPS, true,
Stephen Hinese23d82b2015-06-11 17:00:04 -0700617 UnionDecl, TargetAPI, IsFilterscript, IsExtern);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700618 }
619
620 case clang::Type::ConstantArray: {
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700621 const clang::ConstantArrayType *CAT = static_cast<const clang::ConstantArrayType*>(CTI);
622 const clang::Type *ElementType = GetConstantArrayElementType(CAT);
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800623 return ValidateTypeHelper(Context, C, ElementType, ND, Loc, SPS, true,
Stephen Hinese23d82b2015-06-11 17:00:04 -0700624 UnionDecl, TargetAPI, IsFilterscript, IsExtern);
Stephen Hines78e69cb2011-04-22 15:03:19 -0700625 }
626
627 default: {
628 break;
629 }
630 }
631
632 return true;
Stephen Hinese5e64432010-12-02 18:48:20 -0800633}
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700634
Stephen Hinese5e64432010-12-02 18:48:20 -0800635} // namespace
636
Jean-Luc Brouilleteca05342014-05-15 12:44:21 -0700637std::string CreateDummyName(const char *type, const std::string &name) {
638 std::stringstream S;
639 S << "<" << type;
640 if (!name.empty()) {
641 S << ":" << name;
642 }
643 S << ">";
644 return S.str();
645}
646
Stephen Hinese5e64432010-12-02 18:48:20 -0800647/****************************** RSExportType ******************************/
648bool RSExportType::NormalizeType(const clang::Type *&T,
649 llvm::StringRef &TypeName,
Stephen Hines48d893d2013-12-06 18:00:27 -0800650 RSContext *Context,
Stephen Hines5bfec8d2012-04-04 08:18:57 -0700651 const clang::VarDecl *VD) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700652 if ((T = TypeExportable(T, Context, VD)) == nullptr) {
Stephen Hinese5e64432010-12-02 18:48:20 -0800653 return false;
654 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700655 // Get type name
656 TypeName = RSExportType::GetTypeName(T);
Stephen Hines48d893d2013-12-06 18:00:27 -0800657 if (Context && TypeName.empty()) {
Stephen Hines48d893d2013-12-06 18:00:27 -0800658 if (VD) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800659 Context->ReportError(VD->getLocation(),
660 "anonymous types cannot be exported");
Stephen Hines48d893d2013-12-06 18:00:27 -0800661 } else {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800662 Context->ReportError("anonymous types cannot be exported");
Stephen Hinese5e64432010-12-02 18:48:20 -0800663 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700664 return false;
Stephen Hinese5e64432010-12-02 18:48:20 -0800665 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700666
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700667 return true;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700668}
669
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800670bool RSExportType::ValidateType(slang::RSContext *Context, clang::ASTContext &C,
671 clang::QualType QT, clang::NamedDecl *ND,
672 clang::SourceLocation Loc,
Stephen Hinese23d82b2015-06-11 17:00:04 -0700673 unsigned int TargetAPI, bool IsFilterscript,
674 bool IsExtern) {
Stephen Hines11274a72012-09-26 19:14:20 -0700675 const clang::Type *T = QT.getTypePtr();
Stephen Hines78e69cb2011-04-22 15:03:19 -0700676 llvm::SmallPtrSet<const clang::Type*, 8> SPS =
677 llvm::SmallPtrSet<const clang::Type*, 8>();
678
Stephen Hinese23d82b2015-06-11 17:00:04 -0700679 // If this is an externally visible variable declaration, we check if the
680 // type is able to be exported first.
Stephen Hinesddaba0f2015-06-16 16:00:26 -0700681 if (auto VD = llvm::dyn_cast_or_null<clang::VarDecl>(ND)) {
Stephen Hinese23d82b2015-06-11 17:00:04 -0700682 if (VD->getFormalLinkage() == clang::ExternalLinkage) {
683 if (!TypeExportable(T, Context, VD)) {
684 return false;
685 }
686 }
687 }
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700688 return ValidateTypeHelper(Context, C, T, ND, Loc, SPS, false, nullptr, TargetAPI,
Stephen Hinese23d82b2015-06-11 17:00:04 -0700689 IsFilterscript, IsExtern);
Stephen Hines11274a72012-09-26 19:14:20 -0700690}
691
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800692bool RSExportType::ValidateVarDecl(slang::RSContext *Context,
693 clang::VarDecl *VD, unsigned int TargetAPI,
Stephen Hines11274a72012-09-26 19:14:20 -0700694 bool IsFilterscript) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800695 return ValidateType(Context, VD->getASTContext(), VD->getType(), VD,
Stephen Hinese23d82b2015-06-11 17:00:04 -0700696 VD->getLocation(), TargetAPI, IsFilterscript,
697 (VD->getFormalLinkage() == clang::ExternalLinkage));
Stephen Hines78e69cb2011-04-22 15:03:19 -0700698}
699
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700700const clang::Type
701*RSExportType::GetTypeOfDecl(const clang::DeclaratorDecl *DD) {
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800702 if (DD) {
Stephen Hines8de19222013-08-23 16:30:59 -0700703 clang::QualType T = DD->getType();
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800704
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700705 if (T.isNull())
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700706 return nullptr;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700707 else
708 return T.getTypePtr();
709 }
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700710 return nullptr;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700711}
712
713llvm::StringRef RSExportType::GetTypeName(const clang::Type* T) {
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700714 T = GetCanonicalType(T);
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700715 if (T == nullptr)
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700716 return llvm::StringRef();
717
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700718 const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr();
719
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700720 switch (T->getTypeClass()) {
721 case clang::Type::Builtin: {
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700722 const clang::BuiltinType *BT = static_cast<const clang::BuiltinType*>(CTI);
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700723 BuiltinInfo *info = FindBuiltinType(BT->getKind());
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700724 if (info != nullptr) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700725 return info->cname[0];
Zonr Changa65ec162010-10-17 01:53:05 +0800726 }
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700727 slangAssert(false && "Unknown data type of the builtin");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700728 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700729 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700730 case clang::Type::Record: {
Stephen Hinescd440ee2010-12-06 14:57:04 -0800731 clang::RecordDecl *RD;
732 if (T->isStructureType()) {
733 RD = T->getAsStructureType()->getDecl();
Stephen Hinesdd6206b2010-12-09 19:39:22 -0800734 } else {
Stephen Hinescd440ee2010-12-06 14:57:04 -0800735 break;
736 }
737
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700738 llvm::StringRef Name = RD->getName();
739 if (Name.empty()) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700740 if (RD->getTypedefNameForAnonDecl() != nullptr) {
Shih-wei Liao83f0c622011-06-21 05:34:53 -0700741 Name = RD->getTypedefNameForAnonDecl()->getName();
742 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700743
Shih-wei Liao83f0c622011-06-21 05:34:53 -0700744 if (Name.empty()) {
745 // Try to find a name from redeclaration (i.e. typedef)
746 for (clang::TagDecl::redecl_iterator RI = RD->redecls_begin(),
747 RE = RD->redecls_end();
748 RI != RE;
749 RI++) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700750 slangAssert(*RI != nullptr && "cannot be NULL object");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700751
Shih-wei Liao83f0c622011-06-21 05:34:53 -0700752 Name = (*RI)->getName();
753 if (!Name.empty())
754 break;
755 }
756 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700757 }
758 return Name;
759 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700760 case clang::Type::Pointer: {
761 // "*" plus pointee name
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700762 const clang::PointerType *P = static_cast<const clang::PointerType*>(CTI);
763 const clang::Type *PT = GetPointeeType(P);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700764 llvm::StringRef PointeeName;
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700765 if (NormalizeType(PT, PointeeName, nullptr, nullptr)) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700766 char *Name = new char[ 1 /* * */ + PointeeName.size() + 1 ];
767 Name[0] = '*';
768 memcpy(Name + 1, PointeeName.data(), PointeeName.size());
769 Name[PointeeName.size() + 1] = '\0';
770 return Name;
771 }
772 break;
773 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700774 case clang::Type::ExtVector: {
775 const clang::ExtVectorType *EVT =
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700776 static_cast<const clang::ExtVectorType*>(CTI);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700777 return RSExportVectorType::GetTypeName(EVT);
778 break;
779 }
Zonr Chang2e1dba62010-10-05 22:20:11 +0800780 case clang::Type::ConstantArray : {
781 // Construct name for a constant array is too complicated.
Stephen Hines340b5552014-09-05 02:03:36 -0700782 return "<ConstantArray>";
Zonr Chang2e1dba62010-10-05 22:20:11 +0800783 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700784 default: {
785 break;
786 }
787 }
788
789 return llvm::StringRef();
790}
791
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700792
793RSExportType *RSExportType::Create(RSContext *Context,
794 const clang::Type *T,
795 const llvm::StringRef &TypeName) {
796 // Lookup the context to see whether the type was processed before.
797 // Newly created RSExportType will insert into context
798 // in RSExportType::RSExportType()
799 RSContext::export_type_iterator ETI = Context->findExportType(TypeName);
800
801 if (ETI != Context->export_types_end())
802 return ETI->second;
803
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700804 const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr();
805
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700806 RSExportType *ET = nullptr;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700807 switch (T->getTypeClass()) {
808 case clang::Type::Record: {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700809 DataType dt = RSExportPrimitiveType::GetRSSpecificType(TypeName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700810 switch (dt) {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700811 case DataTypeUnknown: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800812 // User-defined types
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700813 ET = RSExportRecordType::Create(Context,
814 T->getAsStructureType(),
815 TypeName);
816 break;
817 }
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700818 case DataTypeRSMatrix2x2: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800819 // 2 x 2 Matrix type
820 ET = RSExportMatrixType::Create(Context,
821 T->getAsStructureType(),
822 TypeName,
823 2);
824 break;
825 }
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700826 case DataTypeRSMatrix3x3: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800827 // 3 x 3 Matrix type
828 ET = RSExportMatrixType::Create(Context,
829 T->getAsStructureType(),
830 TypeName,
831 3);
832 break;
833 }
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700834 case DataTypeRSMatrix4x4: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800835 // 4 x 4 Matrix type
836 ET = RSExportMatrixType::Create(Context,
837 T->getAsStructureType(),
838 TypeName,
839 4);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700840 break;
841 }
842 default: {
Zonr Chang92b344a2010-10-05 20:39:03 +0800843 // Others are primitive types
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700844 ET = RSExportPrimitiveType::Create(Context, T, TypeName);
845 break;
846 }
847 }
848 break;
849 }
850 case clang::Type::Builtin: {
851 ET = RSExportPrimitiveType::Create(Context, T, TypeName);
852 break;
853 }
854 case clang::Type::Pointer: {
855 ET = RSExportPointerType::Create(Context,
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700856 static_cast<const clang::PointerType*>(CTI),
857 TypeName);
Zonr Chang92b344a2010-10-05 20:39:03 +0800858 // FIXME: free the name (allocated in RSExportType::GetTypeName)
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700859 delete [] TypeName.data();
860 break;
861 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700862 case clang::Type::ExtVector: {
863 ET = RSExportVectorType::Create(Context,
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700864 static_cast<const clang::ExtVectorType*>(CTI),
865 TypeName);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700866 break;
867 }
Zonr Chang2e1dba62010-10-05 22:20:11 +0800868 case clang::Type::ConstantArray: {
869 ET = RSExportConstantArrayType::Create(
870 Context,
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700871 static_cast<const clang::ConstantArrayType*>(CTI));
Zonr Chang2e1dba62010-10-05 22:20:11 +0800872 break;
873 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700874 default: {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800875 Context->ReportError("unknown type cannot be exported: '%0'")
876 << T->getTypeClassName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700877 break;
878 }
879 }
880
881 return ET;
882}
883
zonr6315f762010-10-05 15:35:14 +0800884RSExportType *RSExportType::Create(RSContext *Context, const clang::Type *T) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700885 llvm::StringRef TypeName;
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700886 if (NormalizeType(T, TypeName, Context, nullptr)) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700887 return Create(Context, T, TypeName);
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700888 } else {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700889 return nullptr;
Stephen Hinesd5a84f62012-04-04 17:44:38 -0700890 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700891}
892
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700893RSExportType *RSExportType::CreateFromDecl(RSContext *Context,
894 const clang::VarDecl *VD) {
895 return RSExportType::Create(Context, GetTypeOfDecl(VD));
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700896}
897
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700898size_t RSExportType::getStoreSize() const {
899 return getRSContext()->getDataLayout()->getTypeStoreSize(getLLVMType());
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700900}
901
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -0700902size_t RSExportType::getAllocSize() const {
903 return getRSContext()->getDataLayout()->getTypeAllocSize(getLLVMType());
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700904}
Shih-wei Liao1f0d88f2010-06-25 00:15:24 -0700905
Zonr Chang6b6320a2010-10-05 22:42:01 +0800906RSExportType::RSExportType(RSContext *Context,
907 ExportClass Class,
908 const llvm::StringRef &Name)
Zonr Changa41ce1d2010-10-06 02:23:12 +0800909 : RSExportable(Context, RSExportable::EX_TYPE),
Zonr Chang6b6320a2010-10-05 22:42:01 +0800910 mClass(Class),
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800911 // Make a copy on Name since memory stored @Name is either allocated in
912 // ASTContext or allocated in GetTypeName which will be destroyed later.
zonr6315f762010-10-05 15:35:14 +0800913 mName(Name.data(), Name.size()),
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700914 mLLVMType(nullptr) {
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800915 // Don't cache the type whose name start with '<'. Those type failed to
916 // get their name since constructing their name in GetTypeName() requiring
917 // complicated work.
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700918 if (!IsDummyName(Name)) {
Zonr Chang0da0a7d2010-10-05 21:26:37 +0800919 // TODO(zonr): Need to check whether the insertion is successful or not.
920 Context->insertExportType(llvm::StringRef(Name), this);
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700921 }
922
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700923}
924
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800925bool RSExportType::keep() {
926 if (!RSExportable::keep())
927 return false;
Zonr Chang641558f2010-10-12 21:07:06 +0800928 // Invalidate converted LLVM type.
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700929 mLLVMType = nullptr;
Zonr Chang3cd3dd32010-10-22 02:11:35 +0800930 return true;
Zonr Chang641558f2010-10-12 21:07:06 +0800931}
932
933bool RSExportType::equals(const RSExportable *E) const {
934 CHECK_PARENT_EQUALITY(RSExportable, E);
935 return (static_cast<const RSExportType*>(E)->getClass() == getClass());
936}
937
Zonr Changa7a828d2010-10-22 03:44:27 +0800938RSExportType::~RSExportType() {
Zonr Changa7a828d2010-10-22 03:44:27 +0800939}
940
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700941/************************** RSExportPrimitiveType **************************/
Zonr Changb1771ef2010-10-22 18:03:46 +0800942llvm::ManagedStatic<RSExportPrimitiveType::RSSpecificTypeMapTy>
943RSExportPrimitiveType::RSSpecificTypeMap;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700944
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700945bool RSExportPrimitiveType::IsPrimitiveType(const clang::Type *T) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700946 if ((T != nullptr) && (T->getTypeClass() == clang::Type::Builtin))
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700947 return true;
948 else
949 return false;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700950}
951
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700952DataType
Zonr Changb1771ef2010-10-22 18:03:46 +0800953RSExportPrimitiveType::GetRSSpecificType(const llvm::StringRef &TypeName) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700954 if (TypeName.empty())
955 return DataTypeUnknown;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700956
Zonr Changb1771ef2010-10-22 18:03:46 +0800957 if (RSSpecificTypeMap->empty()) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700958 for (int i = 0; i < MatrixAndObjectDataTypesCount; i++) {
Stephen Hines3eb819a2014-11-24 18:16:52 -0800959 (*RSSpecificTypeMap)[MatrixAndObjectDataTypes[i].name] =
960 MatrixAndObjectDataTypes[i].dataType;
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700961 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700962 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700963
Zonr Changb1771ef2010-10-22 18:03:46 +0800964 RSSpecificTypeMapTy::const_iterator I = RSSpecificTypeMap->find(TypeName);
965 if (I == RSSpecificTypeMap->end())
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700966 return DataTypeUnknown;
967 else
968 return I->getValue();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700969}
970
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -0700971DataType RSExportPrimitiveType::GetRSSpecificType(const clang::Type *T) {
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -0700972 T = GetCanonicalType(T);
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700973 if ((T == nullptr) || (T->getTypeClass() != clang::Type::Record))
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700974 return DataTypeUnknown;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700975
Zonr Changb1771ef2010-10-22 18:03:46 +0800976 return GetRSSpecificType( RSExportType::GetTypeName(T) );
977}
978
979bool RSExportPrimitiveType::IsRSMatrixType(DataType DT) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700980 if (DT < 0 || DT >= DataTypeMax) {
981 return false;
982 }
983 return gReflectionTypes[DT].category == MatrixDataType;
Zonr Changb1771ef2010-10-22 18:03:46 +0800984}
985
986bool RSExportPrimitiveType::IsRSObjectType(DataType DT) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -0700987 if (DT < 0 || DT >= DataTypeMax) {
988 return false;
989 }
990 return gReflectionTypes[DT].category == ObjectDataType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700991}
992
Stephen Hinesfeaca062011-02-04 14:08:13 -0800993bool RSExportPrimitiveType::IsStructureTypeWithRSObject(const clang::Type *T) {
994 bool RSObjectTypeSeen = false;
995 while (T && T->isArrayType()) {
996 T = T->getArrayElementTypeNoTypeQual();
997 }
998
999 const clang::RecordType *RT = T->getAsStructureType();
1000 if (!RT) {
1001 return false;
1002 }
Stephen Hinesb0fabe52013-01-07 19:06:09 -08001003
Stephen Hinesfeaca062011-02-04 14:08:13 -08001004 const clang::RecordDecl *RD = RT->getDecl();
Stephen Hinesb0fabe52013-01-07 19:06:09 -08001005 if (RD) {
1006 RD = RD->getDefinition();
1007 }
1008 if (!RD) {
1009 return false;
1010 }
1011
Stephen Hinesfeaca062011-02-04 14:08:13 -08001012 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
1013 FE = RD->field_end();
1014 FI != FE;
1015 FI++) {
1016 // We just look through all field declarations to see if we find a
1017 // declaration for an RS object type (or an array of one).
1018 const clang::FieldDecl *FD = *FI;
1019 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
1020 while (FT && FT->isArrayType()) {
1021 FT = FT->getArrayElementTypeNoTypeQual();
1022 }
1023
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001024 DataType DT = GetRSSpecificType(FT);
Stephen Hinesfeaca062011-02-04 14:08:13 -08001025 if (IsRSObjectType(DT)) {
1026 // RS object types definitely need to be zero-initialized
1027 RSObjectTypeSeen = true;
1028 } else {
1029 switch (DT) {
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001030 case DataTypeRSMatrix2x2:
1031 case DataTypeRSMatrix3x3:
1032 case DataTypeRSMatrix4x4:
Stephen Hinesfeaca062011-02-04 14:08:13 -08001033 // Matrix types should get zero-initialized as well
1034 RSObjectTypeSeen = true;
1035 break;
1036 default:
1037 // Ignore all other primitive types
1038 break;
1039 }
1040 while (FT && FT->isArrayType()) {
1041 FT = FT->getArrayElementTypeNoTypeQual();
1042 }
1043 if (FT->isStructureType()) {
1044 // Recursively handle structs of structs (even though these can't
1045 // be exported, it is possible for a user to have them internally).
1046 RSObjectTypeSeen |= IsStructureTypeWithRSObject(FT);
1047 }
1048 }
1049 }
1050
1051 return RSObjectTypeSeen;
1052}
1053
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001054size_t RSExportPrimitiveType::GetSizeInBits(const RSExportPrimitiveType *EPT) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001055 int type = EPT->getType();
1056 slangAssert((type > DataTypeUnknown && type < DataTypeMax) &&
Stephen Hines6e6578a2011-02-07 18:05:48 -08001057 "RSExportPrimitiveType::GetSizeInBits : unknown data type");
Stephen Hines9ae18b22014-06-10 23:53:00 -07001058 // All RS object types are 256 bits in 64-bit RS.
1059 if (EPT->isRSObjectType() && EPT->getRSContext()->is64Bit()) {
1060 return 256;
1061 }
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001062 return gReflectionTypes[type].size_in_bits;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001063}
1064
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001065DataType
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001066RSExportPrimitiveType::GetDataType(RSContext *Context, const clang::Type *T) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001067 if (T == nullptr)
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001068 return DataTypeUnknown;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001069
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001070 switch (T->getTypeClass()) {
1071 case clang::Type::Builtin: {
Loganbe274822011-02-16 22:02:54 +08001072 const clang::BuiltinType *BT =
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -07001073 static_cast<const clang::BuiltinType*>(T->getCanonicalTypeInternal().getTypePtr());
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001074 BuiltinInfo *info = FindBuiltinType(BT->getKind());
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001075 if (info != nullptr) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001076 return info->type;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001077 }
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001078 // The size of type WChar depend on platform so we abandon the support
1079 // to them.
1080 Context->ReportError("built-in type cannot be exported: '%0'")
1081 << T->getTypeClassName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001082 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001083 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001084 case clang::Type::Record: {
1085 // must be RS object type
Zonr Changb1771ef2010-10-22 18:03:46 +08001086 return RSExportPrimitiveType::GetRSSpecificType(T);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001087 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001088 default: {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001089 Context->ReportError("primitive type cannot be exported: '%0'")
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001090 << T->getTypeClassName();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001091 break;
1092 }
1093 }
1094
1095 return DataTypeUnknown;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001096}
1097
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001098RSExportPrimitiveType
1099*RSExportPrimitiveType::Create(RSContext *Context,
1100 const clang::Type *T,
1101 const llvm::StringRef &TypeName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001102 bool Normalized) {
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001103 DataType DT = GetDataType(Context, T);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001104
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001105 if ((DT == DataTypeUnknown) || TypeName.empty())
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001106 return nullptr;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001107 else
Zonr Chang6b6320a2010-10-05 22:42:01 +08001108 return new RSExportPrimitiveType(Context, ExportClassPrimitive, TypeName,
Stephen Hines2b8fb642012-03-09 00:12:47 -08001109 DT, Normalized);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001110}
1111
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001112RSExportPrimitiveType *RSExportPrimitiveType::Create(RSContext *Context,
Stephen Hines2b8fb642012-03-09 00:12:47 -08001113 const clang::Type *T) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001114 llvm::StringRef TypeName;
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001115 if (RSExportType::NormalizeType(T, TypeName, Context, nullptr)
Stephen Hines78e69cb2011-04-22 15:03:19 -07001116 && IsPrimitiveType(T)) {
Stephen Hines2b8fb642012-03-09 00:12:47 -08001117 return Create(Context, T, TypeName);
Stephen Hinese5e64432010-12-02 18:48:20 -08001118 } else {
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001119 return nullptr;
Stephen Hinese5e64432010-12-02 18:48:20 -08001120 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001121}
1122
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001123llvm::Type *RSExportPrimitiveType::convertToLLVMType() const {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001124 llvm::LLVMContext &C = getRSContext()->getLLVMContext();
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001125
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001126 if (isRSObjectType()) {
1127 // struct {
1128 // int *p;
1129 // } __attribute__((packed, aligned(pointer_size)))
1130 //
1131 // which is
1132 //
1133 // <{ [1 x i32] }> in LLVM
1134 //
Tim Murrayd56a92f2014-08-05 13:27:12 -07001135 std::vector<llvm::Type *> Elements;
1136 if (getRSContext()->is64Bit()) {
1137 // 64-bit path
1138 Elements.push_back(llvm::ArrayType::get(llvm::Type::getInt64Ty(C), 4));
1139 return llvm::StructType::get(C, Elements, true);
1140 } else {
1141 // 32-bit legacy path
1142 Elements.push_back(llvm::ArrayType::get(llvm::Type::getInt32Ty(C), 1));
1143 return llvm::StructType::get(C, Elements, true);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001144 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001145 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001146
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001147 switch (mType) {
Pirama Arumuga Nainar7966bcf2015-04-07 11:09:14 -07001148 case DataTypeFloat16: {
1149 return llvm::Type::getHalfTy(C);
1150 break;
1151 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001152 case DataTypeFloat32: {
1153 return llvm::Type::getFloatTy(C);
1154 break;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001155 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001156 case DataTypeFloat64: {
1157 return llvm::Type::getDoubleTy(C);
1158 break;
1159 }
Shih-wei Liao91a37832010-10-03 19:11:51 -07001160 case DataTypeBoolean: {
1161 return llvm::Type::getInt1Ty(C);
1162 break;
1163 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001164 case DataTypeSigned8:
1165 case DataTypeUnsigned8: {
1166 return llvm::Type::getInt8Ty(C);
1167 break;
1168 }
1169 case DataTypeSigned16:
1170 case DataTypeUnsigned16:
1171 case DataTypeUnsigned565:
1172 case DataTypeUnsigned5551:
1173 case DataTypeUnsigned4444: {
1174 return llvm::Type::getInt16Ty(C);
1175 break;
1176 }
1177 case DataTypeSigned32:
1178 case DataTypeUnsigned32: {
1179 return llvm::Type::getInt32Ty(C);
1180 break;
1181 }
Stephen Hinesa5d2c232010-10-11 16:10:02 -07001182 case DataTypeSigned64:
1183 case DataTypeUnsigned64: {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001184 return llvm::Type::getInt64Ty(C);
1185 break;
1186 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001187 default: {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001188 slangAssert(false && "Unknown data type");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001189 }
1190 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001191
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001192 return nullptr;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001193}
1194
Zonr Chang641558f2010-10-12 21:07:06 +08001195bool RSExportPrimitiveType::equals(const RSExportable *E) const {
1196 CHECK_PARENT_EQUALITY(RSExportType, E);
1197 return (static_cast<const RSExportPrimitiveType*>(E)->getType() == getType());
1198}
1199
Stephen Hinesfdd1ba12012-03-08 17:26:32 -08001200RSReflectionType *RSExportPrimitiveType::getRSReflectionType(DataType DT) {
1201 if (DT > DataTypeUnknown && DT < DataTypeMax) {
1202 return &gReflectionTypes[DT];
1203 } else {
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001204 return nullptr;
Stephen Hinesfdd1ba12012-03-08 17:26:32 -08001205 }
1206}
1207
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001208/**************************** RSExportPointerType ****************************/
1209
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001210RSExportPointerType
1211*RSExportPointerType::Create(RSContext *Context,
1212 const clang::PointerType *PT,
1213 const llvm::StringRef &TypeName) {
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -07001214 const clang::Type *PointeeType = GetPointeeType(PT);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001215 const RSExportType *PointeeET;
1216
1217 if (PointeeType->getTypeClass() != clang::Type::Pointer) {
1218 PointeeET = RSExportType::Create(Context, PointeeType);
1219 } else {
1220 // Double or higher dimension of pointer, export as int*
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001221 PointeeET = RSExportPrimitiveType::Create(Context,
1222 Context->getASTContext().IntTy.getTypePtr());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001223 }
1224
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001225 if (PointeeET == nullptr) {
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001226 // Error diagnostic is emitted for corresponding pointee type
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001227 return nullptr;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001228 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001229
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001230 return new RSExportPointerType(Context, TypeName, PointeeET);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001231}
1232
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001233llvm::Type *RSExportPointerType::convertToLLVMType() const {
1234 llvm::Type *PointeeType = mPointeeType->getLLVMType();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001235 return llvm::PointerType::getUnqual(PointeeType);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001236}
1237
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001238bool RSExportPointerType::keep() {
1239 if (!RSExportType::keep())
1240 return false;
Zonr Chang641558f2010-10-12 21:07:06 +08001241 const_cast<RSExportType*>(mPointeeType)->keep();
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001242 return true;
Zonr Chang641558f2010-10-12 21:07:06 +08001243}
1244
1245bool RSExportPointerType::equals(const RSExportable *E) const {
1246 CHECK_PARENT_EQUALITY(RSExportType, E);
1247 return (static_cast<const RSExportPointerType*>(E)
1248 ->getPointeeType()->equals(getPointeeType()));
1249}
1250
zonr6315f762010-10-05 15:35:14 +08001251/***************************** RSExportVectorType *****************************/
zonr6315f762010-10-05 15:35:14 +08001252llvm::StringRef
1253RSExportVectorType::GetTypeName(const clang::ExtVectorType *EVT) {
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -07001254 const clang::Type *ElementType = GetExtVectorElementType(EVT);
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001255 llvm::StringRef name;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001256
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001257 if ((ElementType->getTypeClass() != clang::Type::Builtin))
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001258 return name;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001259
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -07001260 const clang::BuiltinType *BT =
1261 static_cast<const clang::BuiltinType*>(
1262 ElementType->getCanonicalTypeInternal().getTypePtr());
1263
Zonr Changa65ec162010-10-17 01:53:05 +08001264 if ((EVT->getNumElements() < 1) ||
1265 (EVT->getNumElements() > 4))
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001266 return name;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001267
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001268 BuiltinInfo *info = FindBuiltinType(BT->getKind());
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001269 if (info != nullptr) {
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001270 int I = EVT->getNumElements() - 1;
1271 if (I < kMaxVectorSize) {
1272 name = info->cname[I];
1273 } else {
1274 slangAssert(false && "Max vector is 4");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001275 }
1276 }
Jean-Luc Brouillet474655a2014-04-28 15:25:51 -07001277 return name;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001278}
1279
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001280RSExportVectorType *RSExportVectorType::Create(RSContext *Context,
1281 const clang::ExtVectorType *EVT,
1282 const llvm::StringRef &TypeName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001283 bool Normalized) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001284 slangAssert(EVT != nullptr && EVT->getTypeClass() == clang::Type::ExtVector);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001285
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -07001286 const clang::Type *ElementType = GetExtVectorElementType(EVT);
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001287 DataType DT = RSExportPrimitiveType::GetDataType(Context, ElementType);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001288
Jean-Luc Brouilletcec9b652014-05-14 19:33:57 -07001289 if (DT != DataTypeUnknown)
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001290 return new RSExportVectorType(Context,
1291 TypeName,
1292 DT,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001293 Normalized,
1294 EVT->getNumElements());
1295 else
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001296 return nullptr;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001297}
1298
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001299llvm::Type *RSExportVectorType::convertToLLVMType() const {
1300 llvm::Type *ElementType = RSExportPrimitiveType::convertToLLVMType();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001301 return llvm::VectorType::get(ElementType, getNumElement());
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001302}
1303
Zonr Chang641558f2010-10-12 21:07:06 +08001304bool RSExportVectorType::equals(const RSExportable *E) const {
1305 CHECK_PARENT_EQUALITY(RSExportPrimitiveType, E);
1306 return (static_cast<const RSExportVectorType*>(E)->getNumElement()
1307 == getNumElement());
1308}
1309
Zonr Chang92b344a2010-10-05 20:39:03 +08001310/***************************** RSExportMatrixType *****************************/
1311RSExportMatrixType *RSExportMatrixType::Create(RSContext *Context,
1312 const clang::RecordType *RT,
1313 const llvm::StringRef &TypeName,
1314 unsigned Dim) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001315 slangAssert((RT != nullptr) && (RT->getTypeClass() == clang::Type::Record));
Stephen Hines6e6578a2011-02-07 18:05:48 -08001316 slangAssert((Dim > 1) && "Invalid dimension of matrix");
Zonr Chang92b344a2010-10-05 20:39:03 +08001317
1318 // Check whether the struct rs_matrix is in our expected form (but assume it's
1319 // correct if we're not sure whether it's correct or not)
1320 const clang::RecordDecl* RD = RT->getDecl();
1321 RD = RD->getDefinition();
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001322 if (RD != nullptr) {
Zonr Chang92b344a2010-10-05 20:39:03 +08001323 // Find definition, perform further examination
1324 if (RD->field_empty()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001325 Context->ReportError(
1326 RD->getLocation(),
1327 "invalid matrix struct: must have 1 field for saving values: '%0'")
1328 << RD->getName();
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001329 return nullptr;
Zonr Chang92b344a2010-10-05 20:39:03 +08001330 }
1331
1332 clang::RecordDecl::field_iterator FIT = RD->field_begin();
1333 const clang::FieldDecl *FD = *FIT;
1334 const clang::Type *FT = RSExportType::GetTypeOfDecl(FD);
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001335 if ((FT == nullptr) || (FT->getTypeClass() != clang::Type::ConstantArray)) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001336 Context->ReportError(RD->getLocation(),
1337 "invalid matrix struct: first field should"
1338 " be an array with constant size: '%0'")
1339 << RD->getName();
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001340 return nullptr;
Zonr Chang92b344a2010-10-05 20:39:03 +08001341 }
1342 const clang::ConstantArrayType *CAT =
1343 static_cast<const clang::ConstantArrayType *>(FT);
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -07001344 const clang::Type *ElementType = GetConstantArrayElementType(CAT);
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001345 if ((ElementType == nullptr) ||
Zonr Chang92b344a2010-10-05 20:39:03 +08001346 (ElementType->getTypeClass() != clang::Type::Builtin) ||
Logan Chien9207a2e2011-10-21 15:39:28 +08001347 (static_cast<const clang::BuiltinType *>(ElementType)->getKind() !=
1348 clang::BuiltinType::Float)) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001349 Context->ReportError(RD->getLocation(),
1350 "invalid matrix struct: first field "
1351 "should be a float array: '%0'")
1352 << RD->getName();
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001353 return nullptr;
Zonr Chang92b344a2010-10-05 20:39:03 +08001354 }
1355
1356 if (CAT->getSize() != Dim * Dim) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001357 Context->ReportError(RD->getLocation(),
1358 "invalid matrix struct: first field "
1359 "should be an array with size %0: '%1'")
1360 << (Dim * Dim) << (RD->getName());
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001361 return nullptr;
Zonr Chang92b344a2010-10-05 20:39:03 +08001362 }
1363
1364 FIT++;
1365 if (FIT != RD->field_end()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001366 Context->ReportError(RD->getLocation(),
1367 "invalid matrix struct: must have "
1368 "exactly 1 field: '%0'")
1369 << RD->getName();
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001370 return nullptr;
Zonr Chang92b344a2010-10-05 20:39:03 +08001371 }
1372 }
1373
1374 return new RSExportMatrixType(Context, TypeName, Dim);
1375}
1376
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001377llvm::Type *RSExportMatrixType::convertToLLVMType() const {
Zonr Chang92b344a2010-10-05 20:39:03 +08001378 // Construct LLVM type:
1379 // struct {
1380 // float X[mDim * mDim];
1381 // }
1382
1383 llvm::LLVMContext &C = getRSContext()->getLLVMContext();
1384 llvm::ArrayType *X = llvm::ArrayType::get(llvm::Type::getFloatTy(C),
1385 mDim * mDim);
Stephen Hinesa67e4452011-07-19 15:39:26 -07001386 return llvm::StructType::get(C, X, false);
Zonr Chang92b344a2010-10-05 20:39:03 +08001387}
1388
Zonr Chang641558f2010-10-12 21:07:06 +08001389bool RSExportMatrixType::equals(const RSExportable *E) const {
1390 CHECK_PARENT_EQUALITY(RSExportType, E);
1391 return (static_cast<const RSExportMatrixType*>(E)->getDim() == getDim());
1392}
1393
Zonr Chang2e1dba62010-10-05 22:20:11 +08001394/************************* RSExportConstantArrayType *************************/
1395RSExportConstantArrayType
1396*RSExportConstantArrayType::Create(RSContext *Context,
1397 const clang::ConstantArrayType *CAT) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001398 slangAssert(CAT != nullptr && CAT->getTypeClass() == clang::Type::ConstantArray);
Zonr Chang2e1dba62010-10-05 22:20:11 +08001399
Stephen Hines6e6578a2011-02-07 18:05:48 -08001400 slangAssert((CAT->getSize().getActiveBits() < 32) && "array too large");
Zonr Chang2e1dba62010-10-05 22:20:11 +08001401
1402 unsigned Size = static_cast<unsigned>(CAT->getSize().getZExtValue());
Stephen Hines6e6578a2011-02-07 18:05:48 -08001403 slangAssert((Size > 0) && "Constant array should have size greater than 0");
Zonr Chang2e1dba62010-10-05 22:20:11 +08001404
Jean-Luc Brouilletb095e052014-05-16 20:10:00 -07001405 const clang::Type *ElementType = GetConstantArrayElementType(CAT);
Zonr Chang2e1dba62010-10-05 22:20:11 +08001406 RSExportType *ElementET = RSExportType::Create(Context, ElementType);
1407
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001408 if (ElementET == nullptr) {
1409 return nullptr;
Zonr Chang2e1dba62010-10-05 22:20:11 +08001410 }
1411
1412 return new RSExportConstantArrayType(Context,
1413 ElementET,
1414 Size);
1415}
1416
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001417llvm::Type *RSExportConstantArrayType::convertToLLVMType() const {
Zonr Chang2e1dba62010-10-05 22:20:11 +08001418 return llvm::ArrayType::get(mElementType->getLLVMType(), getSize());
1419}
1420
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001421bool RSExportConstantArrayType::keep() {
1422 if (!RSExportType::keep())
1423 return false;
Zonr Chang641558f2010-10-12 21:07:06 +08001424 const_cast<RSExportType*>(mElementType)->keep();
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001425 return true;
Zonr Chang641558f2010-10-12 21:07:06 +08001426}
1427
1428bool RSExportConstantArrayType::equals(const RSExportable *E) const {
1429 CHECK_PARENT_EQUALITY(RSExportType, E);
Stephen Hinesa7b75182010-12-07 15:49:49 -08001430 const RSExportConstantArrayType *RHS =
1431 static_cast<const RSExportConstantArrayType*>(E);
1432 return ((getSize() == RHS->getSize()) &&
1433 (getElementType()->equals(RHS->getElementType())));
Zonr Chang641558f2010-10-12 21:07:06 +08001434}
1435
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001436/**************************** RSExportRecordType ****************************/
1437RSExportRecordType *RSExportRecordType::Create(RSContext *Context,
1438 const clang::RecordType *RT,
1439 const llvm::StringRef &TypeName,
1440 bool mIsArtificial) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001441 slangAssert(RT != nullptr && RT->getTypeClass() == clang::Type::Record);
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001442
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001443 const clang::RecordDecl *RD = RT->getDecl();
Stephen Hines6e6578a2011-02-07 18:05:48 -08001444 slangAssert(RD->isStruct());
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001445
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001446 RD = RD->getDefinition();
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001447 if (RD == nullptr) {
Stephen Hines6e6578a2011-02-07 18:05:48 -08001448 slangAssert(false && "struct is not defined in this module");
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001449 return nullptr;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001450 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001451
Zonr Chang0da0a7d2010-10-05 21:26:37 +08001452 // Struct layout construct by clang. We rely on this for obtaining the
1453 // alloc size of a struct and offset of every field in that struct.
1454 const clang::ASTRecordLayout *RL =
Stephen Hines9e5b5032010-11-03 13:19:14 -07001455 &Context->getASTContext().getASTRecordLayout(RD);
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001456 slangAssert((RL != nullptr) &&
Stephen Hinesf2174cf2011-02-09 23:21:37 -08001457 "Failed to retrieve the struct layout from Clang.");
Zonr Chang0da0a7d2010-10-05 21:26:37 +08001458
1459 RSExportRecordType *ERT =
1460 new RSExportRecordType(Context,
1461 TypeName,
1462 RD->hasAttr<clang::PackedAttr>(),
1463 mIsArtificial,
Jean-Luc Brouilletc95381a2014-05-14 21:24:45 -07001464 RL->getDataSize().getQuantity(),
Shih-wei Liao68318a12011-03-11 05:56:15 -08001465 RL->getSize().getQuantity());
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001466 unsigned int Index = 0;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001467
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001468 for (clang::RecordDecl::field_iterator FI = RD->field_begin(),
1469 FE = RD->field_end();
1470 FI != FE;
Shih-wei Liao91a37832010-10-03 19:11:51 -07001471 FI++, Index++) {
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001472
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001473 // FIXME: All fields should be primitive type
Shih-wei Liao43730fe2012-08-02 23:06:18 -07001474 slangAssert(FI->getKind() == clang::Decl::Field);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001475 clang::FieldDecl *FD = *FI;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001476
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001477 if (FD->isBitField()) {
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001478 return nullptr;
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001479 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001480
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001481 // Type
zonr6315f762010-10-05 15:35:14 +08001482 RSExportType *ET = RSExportElement::CreateFromDecl(Context, FD);
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001483
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001484 if (ET != nullptr) {
Zonr Chang0da0a7d2010-10-05 21:26:37 +08001485 ERT->mFields.push_back(
1486 new Field(ET, FD->getName(), ERT,
1487 static_cast<size_t>(RL->getFieldOffset(Index) >> 3)));
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001488 } else {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -08001489 Context->ReportError(RD->getLocation(),
1490 "field type cannot be exported: '%0.%1'")
1491 << RD->getName() << FD->getName();
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001492 return nullptr;
Stephen Hines2ef9bc02010-12-13 18:33:23 -08001493 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001494 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001495
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001496 return ERT;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001497}
1498
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001499llvm::Type *RSExportRecordType::convertToLLVMType() const {
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001500 // Create an opaque type since struct may reference itself recursively.
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001501
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001502 // TODO(sliao): LLVM took out the OpaqueType. Any other to migrate to?
1503 std::vector<llvm::Type*> FieldTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001504
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001505 for (const_field_iterator FI = fields_begin(), FE = fields_end();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001506 FI != FE;
1507 FI++) {
1508 const Field *F = *FI;
1509 const RSExportType *FET = F->getType();
Shih-wei Liao1f0d88f2010-06-25 00:15:24 -07001510
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07001511 FieldTypes.push_back(FET->getLLVMType());
1512 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001513
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001514 llvm::StructType *ST = llvm::StructType::get(getRSContext()->getLLVMContext(),
1515 FieldTypes,
1516 mIsPacked);
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001517 if (ST != nullptr) {
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001518 return ST;
1519 } else {
Chris Wailes5abbe0e2014-08-12 15:58:29 -07001520 return nullptr;
Shih-wei Liao7c67e572011-07-19 05:54:53 -07001521 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -07001522}
Zonr Chang641558f2010-10-12 21:07:06 +08001523
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001524bool RSExportRecordType::keep() {
1525 if (!RSExportType::keep())
1526 return false;
Zonr Chang641558f2010-10-12 21:07:06 +08001527 for (std::list<const Field*>::iterator I = mFields.begin(),
1528 E = mFields.end();
1529 I != E;
1530 I++) {
1531 const_cast<RSExportType*>((*I)->getType())->keep();
1532 }
Zonr Chang3cd3dd32010-10-22 02:11:35 +08001533 return true;
Zonr Chang641558f2010-10-12 21:07:06 +08001534}
1535
1536bool RSExportRecordType::equals(const RSExportable *E) const {
1537 CHECK_PARENT_EQUALITY(RSExportType, E);
1538
1539 const RSExportRecordType *ERT = static_cast<const RSExportRecordType*>(E);
1540
1541 if (ERT->getFields().size() != getFields().size())
1542 return false;
1543
1544 const_field_iterator AI = fields_begin(), BI = ERT->fields_begin();
1545
1546 for (unsigned i = 0, e = getFields().size(); i != e; i++) {
1547 if (!(*AI)->getType()->equals((*BI)->getType()))
1548 return false;
1549 AI++;
1550 BI++;
1551 }
1552
1553 return true;
1554}
Stephen Hinese639eb52010-11-08 19:27:20 -08001555
Jason Sams1b6a0882012-03-12 15:07:58 -07001556void RSExportType::convertToRTD(RSReflectionTypeData *rtd) const {
1557 memset(rtd, 0, sizeof(*rtd));
1558 rtd->vecSize = 1;
1559
1560 switch(getClass()) {
1561 case RSExportType::ExportClassPrimitive: {
1562 const RSExportPrimitiveType *EPT = static_cast<const RSExportPrimitiveType*>(this);
1563 rtd->type = RSExportPrimitiveType::getRSReflectionType(EPT);
1564 return;
1565 }
1566 case RSExportType::ExportClassPointer: {
1567 const RSExportPointerType *EPT = static_cast<const RSExportPointerType*>(this);
1568 const RSExportType *PointeeType = EPT->getPointeeType();
1569 PointeeType->convertToRTD(rtd);
1570 rtd->isPointer = true;
1571 return;
1572 }
1573 case RSExportType::ExportClassVector: {
1574 const RSExportVectorType *EVT = static_cast<const RSExportVectorType*>(this);
1575 rtd->type = EVT->getRSReflectionType(EVT);
1576 rtd->vecSize = EVT->getNumElement();
1577 return;
1578 }
1579 case RSExportType::ExportClassMatrix: {
1580 const RSExportMatrixType *EMT = static_cast<const RSExportMatrixType*>(this);
1581 unsigned Dim = EMT->getDim();
1582 slangAssert((Dim >= 2) && (Dim <= 4));
1583 rtd->type = &gReflectionTypes[15 + Dim-2];
1584 return;
1585 }
1586 case RSExportType::ExportClassConstantArray: {
1587 const RSExportConstantArrayType* CAT =
1588 static_cast<const RSExportConstantArrayType*>(this);
1589 CAT->getElementType()->convertToRTD(rtd);
1590 rtd->arraySize = CAT->getSize();
1591 return;
1592 }
1593 case RSExportType::ExportClassRecord: {
1594 slangAssert(!"RSExportType::ExportClassRecord not implemented");
1595 return;// RS_TYPE_CLASS_NAME_PREFIX + ET->getName() + ".Item";
1596 }
1597 default: {
1598 slangAssert(false && "Unknown class of type");
1599 }
1600 }
1601}
1602
1603
Stephen Hinese639eb52010-11-08 19:27:20 -08001604} // namespace slang