Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 1 | /* |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 2 | * Copyright 2010-2012, The Android Open Source Project |
Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 3 | * |
| 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 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 17 | #include "slang_rs_export_type.h" |
| 18 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 19 | #include <list> |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 20 | #include <vector> |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 21 | |
Shih-wei Liao | 43730fe | 2012-08-02 23:06:18 -0700 | [diff] [blame] | 22 | #include "clang/AST/ASTContext.h" |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 23 | #include "clang/AST/Attr.h" |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 24 | #include "clang/AST/RecordLayout.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 25 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 26 | #include "llvm/ADT/StringExtras.h" |
Stephen Hines | 23c4358 | 2013-01-09 20:02:04 -0800 | [diff] [blame] | 27 | #include "llvm/IR/DataLayout.h" |
| 28 | #include "llvm/IR/DerivedTypes.h" |
| 29 | #include "llvm/IR/Type.h" |
Shih-wei Liao | 0a3f20e | 2010-08-10 13:09:49 -0700 | [diff] [blame] | 30 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 31 | #include "slang_assert.h" |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 32 | #include "slang_rs_context.h" |
| 33 | #include "slang_rs_export_element.h" |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 34 | #include "slang_version.h" |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 35 | |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 36 | #define CHECK_PARENT_EQUALITY(ParentClass, E) \ |
| 37 | if (!ParentClass::equals(E)) \ |
| 38 | return false; |
| 39 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 40 | namespace slang { |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 41 | |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 42 | namespace { |
| 43 | |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 44 | /* 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 Hines | fdd1ba1 | 2012-03-08 17:26:32 -0800 | [diff] [blame] | 49 | static RSReflectionType gReflectionTypes[] = { |
Pirama Arumuga Nainar | 7966bcf | 2015-04-07 11:09:14 -0700 | [diff] [blame] | 50 | {PrimitiveDataType, "FLOAT_16", "F16", 16, "half", "short", "Half", "Half", false}, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 51 | {PrimitiveDataType, "FLOAT_32", "F32", 32, "float", "float", "Float", "Float", false}, |
| 52 | {PrimitiveDataType, "FLOAT_64", "F64", 64, "double", "double", "Double", "Double",false}, |
| 53 | {PrimitiveDataType, "SIGNED_8", "I8", 8, "int8_t", "byte", "Byte", "Byte", false}, |
| 54 | {PrimitiveDataType, "SIGNED_16", "I16", 16, "int16_t", "short", "Short", "Short", false}, |
| 55 | {PrimitiveDataType, "SIGNED_32", "I32", 32, "int32_t", "int", "Int", "Int", false}, |
| 56 | {PrimitiveDataType, "SIGNED_64", "I64", 64, "int64_t", "long", "Long", "Long", false}, |
| 57 | {PrimitiveDataType, "UNSIGNED_8", "U8", 8, "uint8_t", "short", "UByte", "Short", true}, |
| 58 | {PrimitiveDataType, "UNSIGNED_16", "U16", 16, "uint16_t", "int", "UShort", "Int", true}, |
| 59 | {PrimitiveDataType, "UNSIGNED_32", "U32", 32, "uint32_t", "long", "UInt", "Long", true}, |
| 60 | {PrimitiveDataType, "UNSIGNED_64", "U64", 64, "uint64_t", "long", "ULong", "Long", false}, |
Stephen Hines | fdd1ba1 | 2012-03-08 17:26:32 -0800 | [diff] [blame] | 61 | |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 62 | {PrimitiveDataType, "BOOLEAN", "BOOLEAN", 8, "bool", "boolean", nullptr, nullptr, false}, |
Stephen Hines | fdd1ba1 | 2012-03-08 17:26:32 -0800 | [diff] [blame] | 63 | |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 64 | {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 Hines | fdd1ba1 | 2012-03-08 17:26:32 -0800 | [diff] [blame] | 67 | |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 68 | {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 Hines | fdd1ba1 | 2012-03-08 17:26:32 -0800 | [diff] [blame] | 71 | |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 72 | // 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 Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 74 | {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 Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 81 | |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 82 | {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 Hines | fdd1ba1 | 2012-03-08 17:26:32 -0800 | [diff] [blame] | 87 | }; |
| 88 | |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 89 | const int kMaxVectorSize = 4; |
| 90 | |
| 91 | struct BuiltinInfo { |
| 92 | clang::BuiltinType::Kind builtinTypeKind; |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 93 | DataType type; |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 94 | /* 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 Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 100 | |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 101 | BuiltinInfo BuiltinInfoTable[] = { |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 102 | {clang::BuiltinType::Bool, DataTypeBoolean, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 103 | {"bool", "bool2", "bool3", "bool4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 104 | {clang::BuiltinType::Char_U, DataTypeUnsigned8, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 105 | {"uchar", "uchar2", "uchar3", "uchar4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 106 | {clang::BuiltinType::UChar, DataTypeUnsigned8, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 107 | {"uchar", "uchar2", "uchar3", "uchar4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 108 | {clang::BuiltinType::Char16, DataTypeSigned16, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 109 | {"short", "short2", "short3", "short4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 110 | {clang::BuiltinType::Char32, DataTypeSigned32, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 111 | {"int", "int2", "int3", "int4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 112 | {clang::BuiltinType::UShort, DataTypeUnsigned16, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 113 | {"ushort", "ushort2", "ushort3", "ushort4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 114 | {clang::BuiltinType::UInt, DataTypeUnsigned32, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 115 | {"uint", "uint2", "uint3", "uint4"}}, |
Tim Murray | e46265d | 2014-08-18 14:16:32 -0700 | [diff] [blame] | 116 | {clang::BuiltinType::ULong, DataTypeUnsigned64, |
| 117 | {"ulong", "ulong2", "ulong3", "ulong4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 118 | {clang::BuiltinType::ULongLong, DataTypeUnsigned64, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 119 | {"ulong", "ulong2", "ulong3", "ulong4"}}, |
| 120 | |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 121 | {clang::BuiltinType::Char_S, DataTypeSigned8, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 122 | {"char", "char2", "char3", "char4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 123 | {clang::BuiltinType::SChar, DataTypeSigned8, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 124 | {"char", "char2", "char3", "char4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 125 | {clang::BuiltinType::Short, DataTypeSigned16, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 126 | {"short", "short2", "short3", "short4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 127 | {clang::BuiltinType::Int, DataTypeSigned32, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 128 | {"int", "int2", "int3", "int4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 129 | {clang::BuiltinType::Long, DataTypeSigned64, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 130 | {"long", "long2", "long3", "long4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 131 | {clang::BuiltinType::LongLong, DataTypeSigned64, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 132 | {"long", "long2", "long3", "long4"}}, |
Pirama Arumuga Nainar | 7966bcf | 2015-04-07 11:09:14 -0700 | [diff] [blame] | 133 | {clang::BuiltinType::Half, DataTypeFloat16, |
| 134 | {"half", "half2", "half3", "half4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 135 | {clang::BuiltinType::Float, DataTypeFloat32, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 136 | {"float", "float2", "float3", "float4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 137 | {clang::BuiltinType::Double, DataTypeFloat64, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 138 | {"double", "double2", "double3", "double4"}}, |
| 139 | }; |
| 140 | const int BuiltinInfoTableCount = sizeof(BuiltinInfoTable) / sizeof(BuiltinInfoTable[0]); |
| 141 | |
| 142 | struct NameAndPrimitiveType { |
| 143 | const char *name; |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 144 | DataType dataType; |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | static NameAndPrimitiveType MatrixAndObjectDataTypes[] = { |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 148 | {"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 Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | const int MatrixAndObjectDataTypesCount = |
| 166 | sizeof(MatrixAndObjectDataTypes) / sizeof(MatrixAndObjectDataTypes[0]); |
| 167 | |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 168 | static const clang::Type *TypeExportableHelper( |
| 169 | const clang::Type *T, |
| 170 | llvm::SmallPtrSet<const clang::Type*, 8>& SPS, |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 171 | slang::RSContext *Context, |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 172 | const clang::VarDecl *VD, |
Stephen Hines | 5bfec8d | 2012-04-04 08:18:57 -0700 | [diff] [blame] | 173 | const clang::RecordDecl *TopLevelRecord); |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 174 | |
Tim Murray | ee4016d | 2014-04-10 15:49:08 -0700 | [diff] [blame] | 175 | template <unsigned N> |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 176 | static void ReportTypeError(slang::RSContext *Context, |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 177 | const clang::NamedDecl *ND, |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 178 | const clang::RecordDecl *TopLevelRecord, |
Tim Murray | ee4016d | 2014-04-10 15:49:08 -0700 | [diff] [blame] | 179 | const char (&Message)[N], |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 180 | unsigned int TargetAPI = 0) { |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 181 | // 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 Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 185 | Context->ReportError(TopLevelRecord->getLocation(), Message) |
| 186 | << TopLevelRecord->getName() << TargetAPI; |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 187 | } else if (ND) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 188 | Context->ReportError(ND->getLocation(), Message) << ND->getName() |
| 189 | << TargetAPI; |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 190 | } else { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 191 | slangAssert(false && "Variables should be validated before exporting"); |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 192 | } |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static const clang::Type *ConstantArrayTypeExportableHelper( |
| 196 | const clang::ConstantArrayType *CAT, |
| 197 | llvm::SmallPtrSet<const clang::Type*, 8>& SPS, |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 198 | slang::RSContext *Context, |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 199 | const clang::VarDecl *VD, |
Stephen Hines | 5bfec8d | 2012-04-04 08:18:57 -0700 | [diff] [blame] | 200 | const clang::RecordDecl *TopLevelRecord) { |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 201 | // Check element type |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 202 | const clang::Type *ElementType = GetConstantArrayElementType(CAT); |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 203 | if (ElementType->isArrayType()) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 204 | ReportTypeError(Context, VD, TopLevelRecord, |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 205 | "multidimensional arrays cannot be exported: '%0'"); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 206 | return nullptr; |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 207 | } else if (ElementType->isExtVectorType()) { |
| 208 | const clang::ExtVectorType *EVT = |
| 209 | static_cast<const clang::ExtVectorType*>(ElementType); |
| 210 | unsigned numElements = EVT->getNumElements(); |
| 211 | |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 212 | const clang::Type *BaseElementType = GetExtVectorElementType(EVT); |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 213 | if (!RSExportPrimitiveType::IsPrimitiveType(BaseElementType)) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 214 | ReportTypeError(Context, VD, TopLevelRecord, |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 215 | "vectors of non-primitive types cannot be exported: '%0'"); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 216 | return nullptr; |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | if (numElements == 3 && CAT->getSize() != 1) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 220 | ReportTypeError(Context, VD, TopLevelRecord, |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 221 | "arrays of width 3 vector types cannot be exported: '%0'"); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 222 | return nullptr; |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 226 | if (TypeExportableHelper(ElementType, SPS, Context, VD, |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 227 | TopLevelRecord) == nullptr) { |
| 228 | return nullptr; |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 229 | } else { |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 230 | return CAT; |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 231 | } |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 234 | BuiltinInfo *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 Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 240 | return nullptr; |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 243 | static const clang::Type *TypeExportableHelper( |
Stephen Hines | e67239d | 2012-02-24 15:08:36 -0800 | [diff] [blame] | 244 | clang::Type const *T, |
| 245 | llvm::SmallPtrSet<clang::Type const *, 8> &SPS, |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 246 | slang::RSContext *Context, |
Stephen Hines | e67239d | 2012-02-24 15:08:36 -0800 | [diff] [blame] | 247 | clang::VarDecl const *VD, |
Stephen Hines | 5bfec8d | 2012-04-04 08:18:57 -0700 | [diff] [blame] | 248 | clang::RecordDecl const *TopLevelRecord) { |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 249 | // Normalize first |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 250 | if ((T = GetCanonicalType(T)) == nullptr) |
| 251 | return nullptr; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 252 | |
| 253 | if (SPS.count(T)) |
| 254 | return T; |
| 255 | |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 256 | const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr(); |
| 257 | |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 258 | switch (T->getTypeClass()) { |
| 259 | case clang::Type::Builtin: { |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 260 | const clang::BuiltinType *BT = static_cast<const clang::BuiltinType*>(CTI); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 261 | return FindBuiltinType(BT->getKind()) == nullptr ? nullptr : T; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 262 | } |
| 263 | case clang::Type::Record: { |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 264 | if (RSExportPrimitiveType::GetRSSpecificType(T) != DataTypeUnknown) { |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 265 | return T; // RS object type, no further checks are needed |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 266 | } |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 267 | |
| 268 | // Check internal struct |
Stephen Hines | cd440ee | 2010-12-06 14:57:04 -0800 | [diff] [blame] | 269 | if (T->isUnionType()) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 270 | ReportTypeError(Context, VD, T->getAsUnionType()->getDecl(), |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 271 | "unions cannot be exported: '%0'"); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 272 | return nullptr; |
Stephen Hines | cd440ee | 2010-12-06 14:57:04 -0800 | [diff] [blame] | 273 | } else if (!T->isStructureType()) { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 274 | slangAssert(false && "Unknown type cannot be exported"); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 275 | return nullptr; |
Stephen Hines | cd440ee | 2010-12-06 14:57:04 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 278 | clang::RecordDecl *RD = T->getAsStructureType()->getDecl(); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 279 | if (RD != nullptr) { |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 280 | RD = RD->getDefinition(); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 281 | if (RD == nullptr) { |
| 282 | ReportTypeError(Context, nullptr, T->getAsStructureType()->getDecl(), |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 283 | "struct is not defined in this module"); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 284 | return nullptr; |
Stephen Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 285 | } |
| 286 | } |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 287 | |
| 288 | if (!TopLevelRecord) { |
| 289 | TopLevelRecord = RD; |
| 290 | } |
| 291 | if (RD->getName().empty()) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 292 | ReportTypeError(Context, nullptr, RD, |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 293 | "anonymous structures cannot be exported"); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 294 | return nullptr; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | // Fast check |
| 298 | if (RD->hasFlexibleArrayMember() || RD->hasObjectMember()) |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 299 | return nullptr; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 300 | |
| 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 Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 311 | FT = GetCanonicalType(FT); |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 312 | |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 313 | if (!TypeExportableHelper(FT, SPS, Context, VD, TopLevelRecord)) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 314 | return nullptr; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 315 | } |
Stephen Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 316 | |
| 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 Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 321 | Context->ReportError( |
| 322 | FD->getLocation(), |
| 323 | "bit fields are not able to be exported: '%0.%1'") |
| 324 | << RD->getName() << FD->getName(); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 325 | return nullptr; |
Stephen Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 326 | } |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | return T; |
| 330 | } |
| 331 | case clang::Type::Pointer: { |
| 332 | if (TopLevelRecord) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 333 | ReportTypeError(Context, VD, TopLevelRecord, |
Stephen Hines | e23d82b | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 334 | "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 Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 337 | return nullptr; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 338 | } |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 339 | |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 340 | const clang::PointerType *PT = static_cast<const clang::PointerType*>(CTI); |
| 341 | const clang::Type *PointeeType = GetPointeeType(PT); |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 342 | |
Stephen Hines | aa82e74 | 2012-04-11 12:38:17 -0700 | [diff] [blame] | 343 | if (PointeeType->getTypeClass() == clang::Type::Pointer) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 344 | ReportTypeError(Context, VD, TopLevelRecord, |
Stephen Hines | aa82e74 | 2012-04-11 12:38:17 -0700 | [diff] [blame] | 345 | "multiple levels of pointers cannot be exported: '%0'"); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 346 | return nullptr; |
Stephen Hines | aa82e74 | 2012-04-11 12:38:17 -0700 | [diff] [blame] | 347 | } |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 348 | // We don't support pointer with array-type pointee or unsupported pointee |
| 349 | // type |
| 350 | if (PointeeType->isArrayType() || |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 351 | (TypeExportableHelper(PointeeType, SPS, Context, VD, |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 352 | TopLevelRecord) == nullptr)) |
| 353 | return nullptr; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 354 | else |
| 355 | return T; |
| 356 | } |
| 357 | case clang::Type::ExtVector: { |
| 358 | const clang::ExtVectorType *EVT = |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 359 | static_cast<const clang::ExtVectorType*>(CTI); |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 360 | // Only vector with size 2, 3 and 4 are supported. |
| 361 | if (EVT->getNumElements() < 2 || EVT->getNumElements() > 4) |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 362 | return nullptr; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 363 | |
| 364 | // Check base element type |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 365 | const clang::Type *ElementType = GetExtVectorElementType(EVT); |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 366 | |
| 367 | if ((ElementType->getTypeClass() != clang::Type::Builtin) || |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 368 | (TypeExportableHelper(ElementType, SPS, Context, VD, |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 369 | TopLevelRecord) == nullptr)) |
| 370 | return nullptr; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 371 | else |
| 372 | return T; |
| 373 | } |
| 374 | case clang::Type::ConstantArray: { |
| 375 | const clang::ConstantArrayType *CAT = |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 376 | static_cast<const clang::ConstantArrayType*>(CTI); |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 377 | |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 378 | return ConstantArrayTypeExportableHelper(CAT, SPS, Context, VD, |
Stephen Hines | 5bfec8d | 2012-04-04 08:18:57 -0700 | [diff] [blame] | 379 | TopLevelRecord); |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 380 | } |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 381 | 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 Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 386 | default: { |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 387 | slangAssert(false && "Unknown type cannot be validated"); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 388 | return nullptr; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | // Return the type that can be used to create RSExportType, will always return |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 394 | // 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 Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 401 | static const clang::Type *TypeExportable(const clang::Type *T, |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 402 | slang::RSContext *Context, |
Stephen Hines | 5bfec8d | 2012-04-04 08:18:57 -0700 | [diff] [blame] | 403 | const clang::VarDecl *VD) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 404 | llvm::SmallPtrSet<const clang::Type*, 8> SPS = |
| 405 | llvm::SmallPtrSet<const clang::Type*, 8>(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 406 | |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 407 | return TypeExportableHelper(T, SPS, Context, VD, nullptr); |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 410 | static bool ValidateRSObjectInVarDecl(slang::RSContext *Context, |
| 411 | clang::VarDecl *VD, bool InCompositeType, |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 412 | 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 Hines | 44f1006 | 2013-06-13 00:33:23 -0700 | [diff] [blame] | 417 | if (VD->hasLinkage() && |
| 418 | (VD->getFormalLinkage() == clang::ExternalLinkage)) { |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 419 | // Only if we are not a pointer to an object. |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 420 | const clang::Type *T = GetCanonicalType(VD->getType().getTypePtr()); |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 421 | if (T->getTypeClass() != clang::Type::Pointer) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 422 | ReportTypeError(Context, VD, nullptr, |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 423 | "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 Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 435 | // Helper function for ValidateType(). We do a recursive descent on the |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 436 | // 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 Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 441 | // 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 Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 444 | // 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 Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 448 | // IsFilterscript - whether or not we are compiling for Filterscript |
Stephen Hines | e23d82b | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 449 | // IsExtern - is this type externally visible (i.e. extern global or parameter |
| 450 | // to an extern function) |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 451 | static bool ValidateTypeHelper( |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 452 | slang::RSContext *Context, |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 453 | clang::ASTContext &C, |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 454 | const clang::Type *&T, |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 455 | clang::NamedDecl *ND, |
| 456 | clang::SourceLocation Loc, |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 457 | llvm::SmallPtrSet<const clang::Type*, 8>& SPS, |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 458 | bool InCompositeType, |
| 459 | clang::RecordDecl *UnionDecl, |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 460 | unsigned int TargetAPI, |
Stephen Hines | e23d82b | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 461 | bool IsFilterscript, |
| 462 | bool IsExtern) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 463 | if ((T = GetCanonicalType(T)) == nullptr) |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 464 | return true; |
| 465 | |
| 466 | if (SPS.count(T)) |
| 467 | return true; |
| 468 | |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 469 | const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr(); |
| 470 | |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 471 | switch (T->getTypeClass()) { |
| 472 | case clang::Type::Record: { |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 473 | if (RSExportPrimitiveType::IsRSObjectType(T)) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 474 | clang::VarDecl *VD = (ND ? llvm::dyn_cast<clang::VarDecl>(ND) : nullptr); |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 475 | if (VD && !ValidateRSObjectInVarDecl(Context, VD, InCompositeType, |
| 476 | TargetAPI)) { |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 477 | return false; |
| 478 | } |
| 479 | } |
| 480 | |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 481 | if (RSExportPrimitiveType::GetRSSpecificType(T) != DataTypeUnknown) { |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 482 | if (!UnionDecl) { |
| 483 | return true; |
| 484 | } else if (RSExportPrimitiveType::IsRSObjectType(T)) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 485 | ReportTypeError(Context, nullptr, UnionDecl, |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 486 | "unions containing RS object types are not allowed"); |
| 487 | return false; |
| 488 | } |
| 489 | } |
| 490 | |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 491 | clang::RecordDecl *RD = nullptr; |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 492 | |
| 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 Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 504 | if (RD != nullptr) { |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 505 | RD = RD->getDefinition(); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 506 | if (RD == nullptr) { |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 507 | // 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 Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 526 | FT = GetCanonicalType(FT); |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 527 | |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 528 | if (!ValidateTypeHelper(Context, C, FT, ND, Loc, SPS, true, UnionDecl, |
Stephen Hines | e23d82b | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 529 | TargetAPI, IsFilterscript, IsExtern)) { |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 530 | return false; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | return true; |
| 535 | } |
| 536 | |
| 537 | case clang::Type::Builtin: { |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 538 | 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 Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 544 | if (ND) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 545 | Context->ReportError( |
| 546 | Loc, |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 547 | "Builtin types > 32 bits in size are forbidden in " |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 548 | "Filterscript: '%0'") |
| 549 | << ND->getName(); |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 550 | } else { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 551 | Context->ReportError( |
| 552 | Loc, |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 553 | "Builtin types > 32 bits in size are forbidden in " |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 554 | "Filterscript"); |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 555 | } |
| 556 | return false; |
| 557 | } |
| 558 | } |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 559 | break; |
| 560 | } |
| 561 | |
| 562 | case clang::Type::Pointer: { |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 563 | if (IsFilterscript) { |
| 564 | if (ND) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 565 | Context->ReportError(Loc, |
| 566 | "Pointers are forbidden in Filterscript: '%0'") |
| 567 | << ND->getName(); |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 568 | 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 Hines | e23d82b | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 577 | // 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 Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 594 | const clang::PointerType *PT = static_cast<const clang::PointerType*>(CTI); |
| 595 | const clang::Type *PointeeType = GetPointeeType(PT); |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 596 | |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 597 | return ValidateTypeHelper(Context, C, PointeeType, ND, Loc, SPS, |
| 598 | InCompositeType, UnionDecl, TargetAPI, |
Stephen Hines | e23d82b | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 599 | IsFilterscript, IsExtern); |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | case clang::Type::ExtVector: { |
| 603 | const clang::ExtVectorType *EVT = |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 604 | static_cast<const clang::ExtVectorType*>(CTI); |
| 605 | const clang::Type *ElementType = GetExtVectorElementType(EVT); |
Stephen Hines | b13b85e | 2012-08-31 19:27:48 -0700 | [diff] [blame] | 606 | if (TargetAPI < SLANG_ICS_TARGET_API && |
| 607 | InCompositeType && |
Stephen Hines | fdae63e | 2012-11-28 14:42:44 -0800 | [diff] [blame] | 608 | EVT->getNumElements() == 3 && |
| 609 | ND && |
Stephen Hines | 44f1006 | 2013-06-13 00:33:23 -0700 | [diff] [blame] | 610 | ND->getFormalLinkage() == clang::ExternalLinkage) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 611 | ReportTypeError(Context, ND, nullptr, |
Stephen Hines | b13b85e | 2012-08-31 19:27:48 -0700 | [diff] [blame] | 612 | "structs containing vectors of dimension 3 cannot " |
| 613 | "be exported at this API level: '%0'"); |
| 614 | return false; |
| 615 | } |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 616 | return ValidateTypeHelper(Context, C, ElementType, ND, Loc, SPS, true, |
Stephen Hines | e23d82b | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 617 | UnionDecl, TargetAPI, IsFilterscript, IsExtern); |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | case clang::Type::ConstantArray: { |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 621 | const clang::ConstantArrayType *CAT = static_cast<const clang::ConstantArrayType*>(CTI); |
| 622 | const clang::Type *ElementType = GetConstantArrayElementType(CAT); |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 623 | return ValidateTypeHelper(Context, C, ElementType, ND, Loc, SPS, true, |
Stephen Hines | e23d82b | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 624 | UnionDecl, TargetAPI, IsFilterscript, IsExtern); |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | default: { |
| 628 | break; |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | return true; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 633 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 634 | |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 635 | } // namespace |
| 636 | |
Jean-Luc Brouillet | eca0534 | 2014-05-15 12:44:21 -0700 | [diff] [blame] | 637 | std::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 Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 647 | /****************************** RSExportType ******************************/ |
| 648 | bool RSExportType::NormalizeType(const clang::Type *&T, |
| 649 | llvm::StringRef &TypeName, |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 650 | RSContext *Context, |
Stephen Hines | 5bfec8d | 2012-04-04 08:18:57 -0700 | [diff] [blame] | 651 | const clang::VarDecl *VD) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 652 | if ((T = TypeExportable(T, Context, VD)) == nullptr) { |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 653 | return false; |
| 654 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 655 | // Get type name |
| 656 | TypeName = RSExportType::GetTypeName(T); |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 657 | if (Context && TypeName.empty()) { |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 658 | if (VD) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 659 | Context->ReportError(VD->getLocation(), |
| 660 | "anonymous types cannot be exported"); |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 661 | } else { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 662 | Context->ReportError("anonymous types cannot be exported"); |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 663 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 664 | return false; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 665 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 666 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 667 | return true; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 670 | bool RSExportType::ValidateType(slang::RSContext *Context, clang::ASTContext &C, |
| 671 | clang::QualType QT, clang::NamedDecl *ND, |
| 672 | clang::SourceLocation Loc, |
Stephen Hines | e23d82b | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 673 | unsigned int TargetAPI, bool IsFilterscript, |
| 674 | bool IsExtern) { |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 675 | const clang::Type *T = QT.getTypePtr(); |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 676 | llvm::SmallPtrSet<const clang::Type*, 8> SPS = |
| 677 | llvm::SmallPtrSet<const clang::Type*, 8>(); |
| 678 | |
Stephen Hines | e23d82b | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 679 | // If this is an externally visible variable declaration, we check if the |
| 680 | // type is able to be exported first. |
Stephen Hines | ddaba0f | 2015-06-16 16:00:26 -0700 | [diff] [blame] | 681 | if (auto VD = llvm::dyn_cast_or_null<clang::VarDecl>(ND)) { |
Stephen Hines | e23d82b | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 682 | if (VD->getFormalLinkage() == clang::ExternalLinkage) { |
| 683 | if (!TypeExportable(T, Context, VD)) { |
| 684 | return false; |
| 685 | } |
| 686 | } |
| 687 | } |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 688 | return ValidateTypeHelper(Context, C, T, ND, Loc, SPS, false, nullptr, TargetAPI, |
Stephen Hines | e23d82b | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 689 | IsFilterscript, IsExtern); |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 692 | bool RSExportType::ValidateVarDecl(slang::RSContext *Context, |
| 693 | clang::VarDecl *VD, unsigned int TargetAPI, |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 694 | bool IsFilterscript) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 695 | return ValidateType(Context, VD->getASTContext(), VD->getType(), VD, |
Stephen Hines | e23d82b | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 696 | VD->getLocation(), TargetAPI, IsFilterscript, |
| 697 | (VD->getFormalLinkage() == clang::ExternalLinkage)); |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 700 | const clang::Type |
| 701 | *RSExportType::GetTypeOfDecl(const clang::DeclaratorDecl *DD) { |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 702 | if (DD) { |
Stephen Hines | 8de1922 | 2013-08-23 16:30:59 -0700 | [diff] [blame] | 703 | clang::QualType T = DD->getType(); |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 704 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 705 | if (T.isNull()) |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 706 | return nullptr; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 707 | else |
| 708 | return T.getTypePtr(); |
| 709 | } |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 710 | return nullptr; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | llvm::StringRef RSExportType::GetTypeName(const clang::Type* T) { |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 714 | T = GetCanonicalType(T); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 715 | if (T == nullptr) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 716 | return llvm::StringRef(); |
| 717 | |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 718 | const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr(); |
| 719 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 720 | switch (T->getTypeClass()) { |
| 721 | case clang::Type::Builtin: { |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 722 | const clang::BuiltinType *BT = static_cast<const clang::BuiltinType*>(CTI); |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 723 | BuiltinInfo *info = FindBuiltinType(BT->getKind()); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 724 | if (info != nullptr) { |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 725 | return info->cname[0]; |
Zonr Chang | a65ec16 | 2010-10-17 01:53:05 +0800 | [diff] [blame] | 726 | } |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 727 | slangAssert(false && "Unknown data type of the builtin"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 728 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 729 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 730 | case clang::Type::Record: { |
Stephen Hines | cd440ee | 2010-12-06 14:57:04 -0800 | [diff] [blame] | 731 | clang::RecordDecl *RD; |
| 732 | if (T->isStructureType()) { |
| 733 | RD = T->getAsStructureType()->getDecl(); |
Stephen Hines | dd6206b | 2010-12-09 19:39:22 -0800 | [diff] [blame] | 734 | } else { |
Stephen Hines | cd440ee | 2010-12-06 14:57:04 -0800 | [diff] [blame] | 735 | break; |
| 736 | } |
| 737 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 738 | llvm::StringRef Name = RD->getName(); |
| 739 | if (Name.empty()) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 740 | if (RD->getTypedefNameForAnonDecl() != nullptr) { |
Shih-wei Liao | 83f0c62 | 2011-06-21 05:34:53 -0700 | [diff] [blame] | 741 | Name = RD->getTypedefNameForAnonDecl()->getName(); |
| 742 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 743 | |
Shih-wei Liao | 83f0c62 | 2011-06-21 05:34:53 -0700 | [diff] [blame] | 744 | 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 Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 750 | slangAssert(*RI != nullptr && "cannot be NULL object"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 751 | |
Shih-wei Liao | 83f0c62 | 2011-06-21 05:34:53 -0700 | [diff] [blame] | 752 | Name = (*RI)->getName(); |
| 753 | if (!Name.empty()) |
| 754 | break; |
| 755 | } |
| 756 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 757 | } |
| 758 | return Name; |
| 759 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 760 | case clang::Type::Pointer: { |
| 761 | // "*" plus pointee name |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 762 | const clang::PointerType *P = static_cast<const clang::PointerType*>(CTI); |
| 763 | const clang::Type *PT = GetPointeeType(P); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 764 | llvm::StringRef PointeeName; |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 765 | if (NormalizeType(PT, PointeeName, nullptr, nullptr)) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 766 | 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 Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 774 | case clang::Type::ExtVector: { |
| 775 | const clang::ExtVectorType *EVT = |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 776 | static_cast<const clang::ExtVectorType*>(CTI); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 777 | return RSExportVectorType::GetTypeName(EVT); |
| 778 | break; |
| 779 | } |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 780 | case clang::Type::ConstantArray : { |
| 781 | // Construct name for a constant array is too complicated. |
Stephen Hines | 340b555 | 2014-09-05 02:03:36 -0700 | [diff] [blame] | 782 | return "<ConstantArray>"; |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 783 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 784 | default: { |
| 785 | break; |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | return llvm::StringRef(); |
| 790 | } |
| 791 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 792 | |
| 793 | RSExportType *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 Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 804 | const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr(); |
| 805 | |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 806 | RSExportType *ET = nullptr; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 807 | switch (T->getTypeClass()) { |
| 808 | case clang::Type::Record: { |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 809 | DataType dt = RSExportPrimitiveType::GetRSSpecificType(TypeName); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 810 | switch (dt) { |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 811 | case DataTypeUnknown: { |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 812 | // User-defined types |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 813 | ET = RSExportRecordType::Create(Context, |
| 814 | T->getAsStructureType(), |
| 815 | TypeName); |
| 816 | break; |
| 817 | } |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 818 | case DataTypeRSMatrix2x2: { |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 819 | // 2 x 2 Matrix type |
| 820 | ET = RSExportMatrixType::Create(Context, |
| 821 | T->getAsStructureType(), |
| 822 | TypeName, |
| 823 | 2); |
| 824 | break; |
| 825 | } |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 826 | case DataTypeRSMatrix3x3: { |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 827 | // 3 x 3 Matrix type |
| 828 | ET = RSExportMatrixType::Create(Context, |
| 829 | T->getAsStructureType(), |
| 830 | TypeName, |
| 831 | 3); |
| 832 | break; |
| 833 | } |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 834 | case DataTypeRSMatrix4x4: { |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 835 | // 4 x 4 Matrix type |
| 836 | ET = RSExportMatrixType::Create(Context, |
| 837 | T->getAsStructureType(), |
| 838 | TypeName, |
| 839 | 4); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 840 | break; |
| 841 | } |
| 842 | default: { |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 843 | // Others are primitive types |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 844 | 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 Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 856 | static_cast<const clang::PointerType*>(CTI), |
| 857 | TypeName); |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 858 | // FIXME: free the name (allocated in RSExportType::GetTypeName) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 859 | delete [] TypeName.data(); |
| 860 | break; |
| 861 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 862 | case clang::Type::ExtVector: { |
| 863 | ET = RSExportVectorType::Create(Context, |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 864 | static_cast<const clang::ExtVectorType*>(CTI), |
| 865 | TypeName); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 866 | break; |
| 867 | } |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 868 | case clang::Type::ConstantArray: { |
| 869 | ET = RSExportConstantArrayType::Create( |
| 870 | Context, |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 871 | static_cast<const clang::ConstantArrayType*>(CTI)); |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 872 | break; |
| 873 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 874 | default: { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 875 | Context->ReportError("unknown type cannot be exported: '%0'") |
| 876 | << T->getTypeClassName(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 877 | break; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | return ET; |
| 882 | } |
| 883 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 884 | RSExportType *RSExportType::Create(RSContext *Context, const clang::Type *T) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 885 | llvm::StringRef TypeName; |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 886 | if (NormalizeType(T, TypeName, Context, nullptr)) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 887 | return Create(Context, T, TypeName); |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 888 | } else { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 889 | return nullptr; |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 890 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 891 | } |
| 892 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 893 | RSExportType *RSExportType::CreateFromDecl(RSContext *Context, |
| 894 | const clang::VarDecl *VD) { |
| 895 | return RSExportType::Create(Context, GetTypeOfDecl(VD)); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 896 | } |
| 897 | |
Jean-Luc Brouillet | c95381a | 2014-05-14 21:24:45 -0700 | [diff] [blame] | 898 | size_t RSExportType::getStoreSize() const { |
| 899 | return getRSContext()->getDataLayout()->getTypeStoreSize(getLLVMType()); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 900 | } |
| 901 | |
Jean-Luc Brouillet | c95381a | 2014-05-14 21:24:45 -0700 | [diff] [blame] | 902 | size_t RSExportType::getAllocSize() const { |
| 903 | return getRSContext()->getDataLayout()->getTypeAllocSize(getLLVMType()); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 904 | } |
Shih-wei Liao | 1f0d88f | 2010-06-25 00:15:24 -0700 | [diff] [blame] | 905 | |
Zonr Chang | 6b6320a | 2010-10-05 22:42:01 +0800 | [diff] [blame] | 906 | RSExportType::RSExportType(RSContext *Context, |
| 907 | ExportClass Class, |
| 908 | const llvm::StringRef &Name) |
Zonr Chang | a41ce1d | 2010-10-06 02:23:12 +0800 | [diff] [blame] | 909 | : RSExportable(Context, RSExportable::EX_TYPE), |
Zonr Chang | 6b6320a | 2010-10-05 22:42:01 +0800 | [diff] [blame] | 910 | mClass(Class), |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 911 | // Make a copy on Name since memory stored @Name is either allocated in |
| 912 | // ASTContext or allocated in GetTypeName which will be destroyed later. |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 913 | mName(Name.data(), Name.size()), |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 914 | mLLVMType(nullptr) { |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 915 | // 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 Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 918 | if (!IsDummyName(Name)) { |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 919 | // TODO(zonr): Need to check whether the insertion is successful or not. |
| 920 | Context->insertExportType(llvm::StringRef(Name), this); |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 921 | } |
| 922 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Zonr Chang | 3cd3dd3 | 2010-10-22 02:11:35 +0800 | [diff] [blame] | 925 | bool RSExportType::keep() { |
| 926 | if (!RSExportable::keep()) |
| 927 | return false; |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 928 | // Invalidate converted LLVM type. |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 929 | mLLVMType = nullptr; |
Zonr Chang | 3cd3dd3 | 2010-10-22 02:11:35 +0800 | [diff] [blame] | 930 | return true; |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | bool RSExportType::equals(const RSExportable *E) const { |
| 934 | CHECK_PARENT_EQUALITY(RSExportable, E); |
| 935 | return (static_cast<const RSExportType*>(E)->getClass() == getClass()); |
| 936 | } |
| 937 | |
Zonr Chang | a7a828d | 2010-10-22 03:44:27 +0800 | [diff] [blame] | 938 | RSExportType::~RSExportType() { |
Zonr Chang | a7a828d | 2010-10-22 03:44:27 +0800 | [diff] [blame] | 939 | } |
| 940 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 941 | /************************** RSExportPrimitiveType **************************/ |
Zonr Chang | b1771ef | 2010-10-22 18:03:46 +0800 | [diff] [blame] | 942 | llvm::ManagedStatic<RSExportPrimitiveType::RSSpecificTypeMapTy> |
| 943 | RSExportPrimitiveType::RSSpecificTypeMap; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 944 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 945 | bool RSExportPrimitiveType::IsPrimitiveType(const clang::Type *T) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 946 | if ((T != nullptr) && (T->getTypeClass() == clang::Type::Builtin)) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 947 | return true; |
| 948 | else |
| 949 | return false; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 950 | } |
| 951 | |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 952 | DataType |
Zonr Chang | b1771ef | 2010-10-22 18:03:46 +0800 | [diff] [blame] | 953 | RSExportPrimitiveType::GetRSSpecificType(const llvm::StringRef &TypeName) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 954 | if (TypeName.empty()) |
| 955 | return DataTypeUnknown; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 956 | |
Zonr Chang | b1771ef | 2010-10-22 18:03:46 +0800 | [diff] [blame] | 957 | if (RSSpecificTypeMap->empty()) { |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 958 | for (int i = 0; i < MatrixAndObjectDataTypesCount; i++) { |
Stephen Hines | 3eb819a | 2014-11-24 18:16:52 -0800 | [diff] [blame] | 959 | (*RSSpecificTypeMap)[MatrixAndObjectDataTypes[i].name] = |
| 960 | MatrixAndObjectDataTypes[i].dataType; |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 961 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 962 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 963 | |
Zonr Chang | b1771ef | 2010-10-22 18:03:46 +0800 | [diff] [blame] | 964 | RSSpecificTypeMapTy::const_iterator I = RSSpecificTypeMap->find(TypeName); |
| 965 | if (I == RSSpecificTypeMap->end()) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 966 | return DataTypeUnknown; |
| 967 | else |
| 968 | return I->getValue(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 971 | DataType RSExportPrimitiveType::GetRSSpecificType(const clang::Type *T) { |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 972 | T = GetCanonicalType(T); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 973 | if ((T == nullptr) || (T->getTypeClass() != clang::Type::Record)) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 974 | return DataTypeUnknown; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 975 | |
Zonr Chang | b1771ef | 2010-10-22 18:03:46 +0800 | [diff] [blame] | 976 | return GetRSSpecificType( RSExportType::GetTypeName(T) ); |
| 977 | } |
| 978 | |
| 979 | bool RSExportPrimitiveType::IsRSMatrixType(DataType DT) { |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 980 | if (DT < 0 || DT >= DataTypeMax) { |
| 981 | return false; |
| 982 | } |
| 983 | return gReflectionTypes[DT].category == MatrixDataType; |
Zonr Chang | b1771ef | 2010-10-22 18:03:46 +0800 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | bool RSExportPrimitiveType::IsRSObjectType(DataType DT) { |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 987 | if (DT < 0 || DT >= DataTypeMax) { |
| 988 | return false; |
| 989 | } |
| 990 | return gReflectionTypes[DT].category == ObjectDataType; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 991 | } |
| 992 | |
Stephen Hines | feaca06 | 2011-02-04 14:08:13 -0800 | [diff] [blame] | 993 | bool 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 Hines | b0fabe5 | 2013-01-07 19:06:09 -0800 | [diff] [blame] | 1003 | |
Stephen Hines | feaca06 | 2011-02-04 14:08:13 -0800 | [diff] [blame] | 1004 | const clang::RecordDecl *RD = RT->getDecl(); |
Stephen Hines | b0fabe5 | 2013-01-07 19:06:09 -0800 | [diff] [blame] | 1005 | if (RD) { |
| 1006 | RD = RD->getDefinition(); |
| 1007 | } |
| 1008 | if (!RD) { |
| 1009 | return false; |
| 1010 | } |
| 1011 | |
Stephen Hines | feaca06 | 2011-02-04 14:08:13 -0800 | [diff] [blame] | 1012 | 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 Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 1024 | DataType DT = GetRSSpecificType(FT); |
Stephen Hines | feaca06 | 2011-02-04 14:08:13 -0800 | [diff] [blame] | 1025 | if (IsRSObjectType(DT)) { |
| 1026 | // RS object types definitely need to be zero-initialized |
| 1027 | RSObjectTypeSeen = true; |
| 1028 | } else { |
| 1029 | switch (DT) { |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 1030 | case DataTypeRSMatrix2x2: |
| 1031 | case DataTypeRSMatrix3x3: |
| 1032 | case DataTypeRSMatrix4x4: |
Stephen Hines | feaca06 | 2011-02-04 14:08:13 -0800 | [diff] [blame] | 1033 | // 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 Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1054 | size_t RSExportPrimitiveType::GetSizeInBits(const RSExportPrimitiveType *EPT) { |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 1055 | int type = EPT->getType(); |
| 1056 | slangAssert((type > DataTypeUnknown && type < DataTypeMax) && |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1057 | "RSExportPrimitiveType::GetSizeInBits : unknown data type"); |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 1058 | // All RS object types are 256 bits in 64-bit RS. |
| 1059 | if (EPT->isRSObjectType() && EPT->getRSContext()->is64Bit()) { |
| 1060 | return 256; |
| 1061 | } |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 1062 | return gReflectionTypes[type].size_in_bits; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 1065 | DataType |
Stephen Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 1066 | RSExportPrimitiveType::GetDataType(RSContext *Context, const clang::Type *T) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1067 | if (T == nullptr) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1068 | return DataTypeUnknown; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1069 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1070 | switch (T->getTypeClass()) { |
| 1071 | case clang::Type::Builtin: { |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 1072 | const clang::BuiltinType *BT = |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 1073 | static_cast<const clang::BuiltinType*>(T->getCanonicalTypeInternal().getTypePtr()); |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 1074 | BuiltinInfo *info = FindBuiltinType(BT->getKind()); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1075 | if (info != nullptr) { |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 1076 | return info->type; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1077 | } |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 1078 | // 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 Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1082 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1083 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1084 | case clang::Type::Record: { |
| 1085 | // must be RS object type |
Zonr Chang | b1771ef | 2010-10-22 18:03:46 +0800 | [diff] [blame] | 1086 | return RSExportPrimitiveType::GetRSSpecificType(T); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1087 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1088 | default: { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 1089 | Context->ReportError("primitive type cannot be exported: '%0'") |
Stephen Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 1090 | << T->getTypeClassName(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1091 | break; |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | return DataTypeUnknown; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1098 | RSExportPrimitiveType |
| 1099 | *RSExportPrimitiveType::Create(RSContext *Context, |
| 1100 | const clang::Type *T, |
| 1101 | const llvm::StringRef &TypeName, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1102 | bool Normalized) { |
Stephen Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 1103 | DataType DT = GetDataType(Context, T); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1104 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1105 | if ((DT == DataTypeUnknown) || TypeName.empty()) |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1106 | return nullptr; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1107 | else |
Zonr Chang | 6b6320a | 2010-10-05 22:42:01 +0800 | [diff] [blame] | 1108 | return new RSExportPrimitiveType(Context, ExportClassPrimitive, TypeName, |
Stephen Hines | 2b8fb64 | 2012-03-09 00:12:47 -0800 | [diff] [blame] | 1109 | DT, Normalized); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1112 | RSExportPrimitiveType *RSExportPrimitiveType::Create(RSContext *Context, |
Stephen Hines | 2b8fb64 | 2012-03-09 00:12:47 -0800 | [diff] [blame] | 1113 | const clang::Type *T) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1114 | llvm::StringRef TypeName; |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1115 | if (RSExportType::NormalizeType(T, TypeName, Context, nullptr) |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 1116 | && IsPrimitiveType(T)) { |
Stephen Hines | 2b8fb64 | 2012-03-09 00:12:47 -0800 | [diff] [blame] | 1117 | return Create(Context, T, TypeName); |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 1118 | } else { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1119 | return nullptr; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 1120 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
Shih-wei Liao | 7c67e57 | 2011-07-19 05:54:53 -0700 | [diff] [blame] | 1123 | llvm::Type *RSExportPrimitiveType::convertToLLVMType() const { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1124 | llvm::LLVMContext &C = getRSContext()->getLLVMContext(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1125 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1126 | 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 Murray | d56a92f | 2014-08-05 13:27:12 -0700 | [diff] [blame] | 1135 | 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 Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1144 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1145 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1146 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1147 | switch (mType) { |
Pirama Arumuga Nainar | 7966bcf | 2015-04-07 11:09:14 -0700 | [diff] [blame] | 1148 | case DataTypeFloat16: { |
| 1149 | return llvm::Type::getHalfTy(C); |
| 1150 | break; |
| 1151 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1152 | case DataTypeFloat32: { |
| 1153 | return llvm::Type::getFloatTy(C); |
| 1154 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1155 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1156 | case DataTypeFloat64: { |
| 1157 | return llvm::Type::getDoubleTy(C); |
| 1158 | break; |
| 1159 | } |
Shih-wei Liao | 91a3783 | 2010-10-03 19:11:51 -0700 | [diff] [blame] | 1160 | case DataTypeBoolean: { |
| 1161 | return llvm::Type::getInt1Ty(C); |
| 1162 | break; |
| 1163 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1164 | 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 Hines | a5d2c23 | 2010-10-11 16:10:02 -0700 | [diff] [blame] | 1182 | case DataTypeSigned64: |
| 1183 | case DataTypeUnsigned64: { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1184 | return llvm::Type::getInt64Ty(C); |
| 1185 | break; |
| 1186 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1187 | default: { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1188 | slangAssert(false && "Unknown data type"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1189 | } |
| 1190 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1191 | |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1192 | return nullptr; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 1195 | bool RSExportPrimitiveType::equals(const RSExportable *E) const { |
| 1196 | CHECK_PARENT_EQUALITY(RSExportType, E); |
| 1197 | return (static_cast<const RSExportPrimitiveType*>(E)->getType() == getType()); |
| 1198 | } |
| 1199 | |
Stephen Hines | fdd1ba1 | 2012-03-08 17:26:32 -0800 | [diff] [blame] | 1200 | RSReflectionType *RSExportPrimitiveType::getRSReflectionType(DataType DT) { |
| 1201 | if (DT > DataTypeUnknown && DT < DataTypeMax) { |
| 1202 | return &gReflectionTypes[DT]; |
| 1203 | } else { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1204 | return nullptr; |
Stephen Hines | fdd1ba1 | 2012-03-08 17:26:32 -0800 | [diff] [blame] | 1205 | } |
| 1206 | } |
| 1207 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1208 | /**************************** RSExportPointerType ****************************/ |
| 1209 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1210 | RSExportPointerType |
| 1211 | *RSExportPointerType::Create(RSContext *Context, |
| 1212 | const clang::PointerType *PT, |
| 1213 | const llvm::StringRef &TypeName) { |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 1214 | const clang::Type *PointeeType = GetPointeeType(PT); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1215 | 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 Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 1221 | PointeeET = RSExportPrimitiveType::Create(Context, |
| 1222 | Context->getASTContext().IntTy.getTypePtr()); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1225 | if (PointeeET == nullptr) { |
Stephen Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 1226 | // Error diagnostic is emitted for corresponding pointee type |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1227 | return nullptr; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1228 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1229 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1230 | return new RSExportPointerType(Context, TypeName, PointeeET); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1231 | } |
| 1232 | |
Shih-wei Liao | 7c67e57 | 2011-07-19 05:54:53 -0700 | [diff] [blame] | 1233 | llvm::Type *RSExportPointerType::convertToLLVMType() const { |
| 1234 | llvm::Type *PointeeType = mPointeeType->getLLVMType(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1235 | return llvm::PointerType::getUnqual(PointeeType); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1236 | } |
| 1237 | |
Zonr Chang | 3cd3dd3 | 2010-10-22 02:11:35 +0800 | [diff] [blame] | 1238 | bool RSExportPointerType::keep() { |
| 1239 | if (!RSExportType::keep()) |
| 1240 | return false; |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 1241 | const_cast<RSExportType*>(mPointeeType)->keep(); |
Zonr Chang | 3cd3dd3 | 2010-10-22 02:11:35 +0800 | [diff] [blame] | 1242 | return true; |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | bool 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 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 1251 | /***************************** RSExportVectorType *****************************/ |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 1252 | llvm::StringRef |
| 1253 | RSExportVectorType::GetTypeName(const clang::ExtVectorType *EVT) { |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 1254 | const clang::Type *ElementType = GetExtVectorElementType(EVT); |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 1255 | llvm::StringRef name; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1256 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1257 | if ((ElementType->getTypeClass() != clang::Type::Builtin)) |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 1258 | return name; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1259 | |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 1260 | const clang::BuiltinType *BT = |
| 1261 | static_cast<const clang::BuiltinType*>( |
| 1262 | ElementType->getCanonicalTypeInternal().getTypePtr()); |
| 1263 | |
Zonr Chang | a65ec16 | 2010-10-17 01:53:05 +0800 | [diff] [blame] | 1264 | if ((EVT->getNumElements() < 1) || |
| 1265 | (EVT->getNumElements() > 4)) |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 1266 | return name; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1267 | |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 1268 | BuiltinInfo *info = FindBuiltinType(BT->getKind()); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1269 | if (info != nullptr) { |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 1270 | 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 Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1275 | } |
| 1276 | } |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 1277 | return name; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1278 | } |
| 1279 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1280 | RSExportVectorType *RSExportVectorType::Create(RSContext *Context, |
| 1281 | const clang::ExtVectorType *EVT, |
| 1282 | const llvm::StringRef &TypeName, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1283 | bool Normalized) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1284 | slangAssert(EVT != nullptr && EVT->getTypeClass() == clang::Type::ExtVector); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1285 | |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 1286 | const clang::Type *ElementType = GetExtVectorElementType(EVT); |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 1287 | DataType DT = RSExportPrimitiveType::GetDataType(Context, ElementType); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1288 | |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 1289 | if (DT != DataTypeUnknown) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1290 | return new RSExportVectorType(Context, |
| 1291 | TypeName, |
| 1292 | DT, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1293 | Normalized, |
| 1294 | EVT->getNumElements()); |
| 1295 | else |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1296 | return nullptr; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1297 | } |
| 1298 | |
Shih-wei Liao | 7c67e57 | 2011-07-19 05:54:53 -0700 | [diff] [blame] | 1299 | llvm::Type *RSExportVectorType::convertToLLVMType() const { |
| 1300 | llvm::Type *ElementType = RSExportPrimitiveType::convertToLLVMType(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1301 | return llvm::VectorType::get(ElementType, getNumElement()); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 1304 | bool 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 Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1310 | /***************************** RSExportMatrixType *****************************/ |
| 1311 | RSExportMatrixType *RSExportMatrixType::Create(RSContext *Context, |
| 1312 | const clang::RecordType *RT, |
| 1313 | const llvm::StringRef &TypeName, |
| 1314 | unsigned Dim) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1315 | slangAssert((RT != nullptr) && (RT->getTypeClass() == clang::Type::Record)); |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1316 | slangAssert((Dim > 1) && "Invalid dimension of matrix"); |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1317 | |
| 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 Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1322 | if (RD != nullptr) { |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1323 | // Find definition, perform further examination |
| 1324 | if (RD->field_empty()) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 1325 | Context->ReportError( |
| 1326 | RD->getLocation(), |
| 1327 | "invalid matrix struct: must have 1 field for saving values: '%0'") |
| 1328 | << RD->getName(); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1329 | return nullptr; |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1330 | } |
| 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 Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1335 | if ((FT == nullptr) || (FT->getTypeClass() != clang::Type::ConstantArray)) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 1336 | Context->ReportError(RD->getLocation(), |
| 1337 | "invalid matrix struct: first field should" |
| 1338 | " be an array with constant size: '%0'") |
| 1339 | << RD->getName(); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1340 | return nullptr; |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1341 | } |
| 1342 | const clang::ConstantArrayType *CAT = |
| 1343 | static_cast<const clang::ConstantArrayType *>(FT); |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 1344 | const clang::Type *ElementType = GetConstantArrayElementType(CAT); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1345 | if ((ElementType == nullptr) || |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1346 | (ElementType->getTypeClass() != clang::Type::Builtin) || |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 1347 | (static_cast<const clang::BuiltinType *>(ElementType)->getKind() != |
| 1348 | clang::BuiltinType::Float)) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 1349 | Context->ReportError(RD->getLocation(), |
| 1350 | "invalid matrix struct: first field " |
| 1351 | "should be a float array: '%0'") |
| 1352 | << RD->getName(); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1353 | return nullptr; |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | if (CAT->getSize() != Dim * Dim) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 1357 | 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 Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1361 | return nullptr; |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | FIT++; |
| 1365 | if (FIT != RD->field_end()) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 1366 | Context->ReportError(RD->getLocation(), |
| 1367 | "invalid matrix struct: must have " |
| 1368 | "exactly 1 field: '%0'") |
| 1369 | << RD->getName(); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1370 | return nullptr; |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | return new RSExportMatrixType(Context, TypeName, Dim); |
| 1375 | } |
| 1376 | |
Shih-wei Liao | 7c67e57 | 2011-07-19 05:54:53 -0700 | [diff] [blame] | 1377 | llvm::Type *RSExportMatrixType::convertToLLVMType() const { |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1378 | // 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 Hines | a67e445 | 2011-07-19 15:39:26 -0700 | [diff] [blame] | 1386 | return llvm::StructType::get(C, X, false); |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1387 | } |
| 1388 | |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 1389 | bool RSExportMatrixType::equals(const RSExportable *E) const { |
| 1390 | CHECK_PARENT_EQUALITY(RSExportType, E); |
| 1391 | return (static_cast<const RSExportMatrixType*>(E)->getDim() == getDim()); |
| 1392 | } |
| 1393 | |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1394 | /************************* RSExportConstantArrayType *************************/ |
| 1395 | RSExportConstantArrayType |
| 1396 | *RSExportConstantArrayType::Create(RSContext *Context, |
| 1397 | const clang::ConstantArrayType *CAT) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1398 | slangAssert(CAT != nullptr && CAT->getTypeClass() == clang::Type::ConstantArray); |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1399 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1400 | slangAssert((CAT->getSize().getActiveBits() < 32) && "array too large"); |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1401 | |
| 1402 | unsigned Size = static_cast<unsigned>(CAT->getSize().getZExtValue()); |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1403 | slangAssert((Size > 0) && "Constant array should have size greater than 0"); |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1404 | |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 1405 | const clang::Type *ElementType = GetConstantArrayElementType(CAT); |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1406 | RSExportType *ElementET = RSExportType::Create(Context, ElementType); |
| 1407 | |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1408 | if (ElementET == nullptr) { |
| 1409 | return nullptr; |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | return new RSExportConstantArrayType(Context, |
| 1413 | ElementET, |
| 1414 | Size); |
| 1415 | } |
| 1416 | |
Shih-wei Liao | 7c67e57 | 2011-07-19 05:54:53 -0700 | [diff] [blame] | 1417 | llvm::Type *RSExportConstantArrayType::convertToLLVMType() const { |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1418 | return llvm::ArrayType::get(mElementType->getLLVMType(), getSize()); |
| 1419 | } |
| 1420 | |
Zonr Chang | 3cd3dd3 | 2010-10-22 02:11:35 +0800 | [diff] [blame] | 1421 | bool RSExportConstantArrayType::keep() { |
| 1422 | if (!RSExportType::keep()) |
| 1423 | return false; |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 1424 | const_cast<RSExportType*>(mElementType)->keep(); |
Zonr Chang | 3cd3dd3 | 2010-10-22 02:11:35 +0800 | [diff] [blame] | 1425 | return true; |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | bool RSExportConstantArrayType::equals(const RSExportable *E) const { |
| 1429 | CHECK_PARENT_EQUALITY(RSExportType, E); |
Stephen Hines | a7b7518 | 2010-12-07 15:49:49 -0800 | [diff] [blame] | 1430 | const RSExportConstantArrayType *RHS = |
| 1431 | static_cast<const RSExportConstantArrayType*>(E); |
| 1432 | return ((getSize() == RHS->getSize()) && |
| 1433 | (getElementType()->equals(RHS->getElementType()))); |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 1434 | } |
| 1435 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1436 | /**************************** RSExportRecordType ****************************/ |
| 1437 | RSExportRecordType *RSExportRecordType::Create(RSContext *Context, |
| 1438 | const clang::RecordType *RT, |
| 1439 | const llvm::StringRef &TypeName, |
| 1440 | bool mIsArtificial) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1441 | slangAssert(RT != nullptr && RT->getTypeClass() == clang::Type::Record); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1442 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1443 | const clang::RecordDecl *RD = RT->getDecl(); |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1444 | slangAssert(RD->isStruct()); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1445 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1446 | RD = RD->getDefinition(); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1447 | if (RD == nullptr) { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1448 | slangAssert(false && "struct is not defined in this module"); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1449 | return nullptr; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1450 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1451 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 1452 | // 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 Hines | 9e5b503 | 2010-11-03 13:19:14 -0700 | [diff] [blame] | 1455 | &Context->getASTContext().getASTRecordLayout(RD); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1456 | slangAssert((RL != nullptr) && |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 1457 | "Failed to retrieve the struct layout from Clang."); |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 1458 | |
| 1459 | RSExportRecordType *ERT = |
| 1460 | new RSExportRecordType(Context, |
| 1461 | TypeName, |
| 1462 | RD->hasAttr<clang::PackedAttr>(), |
| 1463 | mIsArtificial, |
Jean-Luc Brouillet | c95381a | 2014-05-14 21:24:45 -0700 | [diff] [blame] | 1464 | RL->getDataSize().getQuantity(), |
Shih-wei Liao | 68318a1 | 2011-03-11 05:56:15 -0800 | [diff] [blame] | 1465 | RL->getSize().getQuantity()); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1466 | unsigned int Index = 0; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1467 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1468 | for (clang::RecordDecl::field_iterator FI = RD->field_begin(), |
| 1469 | FE = RD->field_end(); |
| 1470 | FI != FE; |
Shih-wei Liao | 91a3783 | 2010-10-03 19:11:51 -0700 | [diff] [blame] | 1471 | FI++, Index++) { |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1472 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1473 | // FIXME: All fields should be primitive type |
Shih-wei Liao | 43730fe | 2012-08-02 23:06:18 -0700 | [diff] [blame] | 1474 | slangAssert(FI->getKind() == clang::Decl::Field); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1475 | clang::FieldDecl *FD = *FI; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1476 | |
Stephen Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 1477 | if (FD->isBitField()) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1478 | return nullptr; |
Stephen Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 1479 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1480 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1481 | // Type |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 1482 | RSExportType *ET = RSExportElement::CreateFromDecl(Context, FD); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1483 | |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1484 | if (ET != nullptr) { |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 1485 | ERT->mFields.push_back( |
| 1486 | new Field(ET, FD->getName(), ERT, |
| 1487 | static_cast<size_t>(RL->getFieldOffset(Index) >> 3))); |
Stephen Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 1488 | } else { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 1489 | Context->ReportError(RD->getLocation(), |
| 1490 | "field type cannot be exported: '%0.%1'") |
| 1491 | << RD->getName() << FD->getName(); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1492 | return nullptr; |
Stephen Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 1493 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1494 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1495 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1496 | return ERT; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1497 | } |
| 1498 | |
Shih-wei Liao | 7c67e57 | 2011-07-19 05:54:53 -0700 | [diff] [blame] | 1499 | llvm::Type *RSExportRecordType::convertToLLVMType() const { |
Zonr Chang | 3cd3dd3 | 2010-10-22 02:11:35 +0800 | [diff] [blame] | 1500 | // Create an opaque type since struct may reference itself recursively. |
Zonr Chang | 3cd3dd3 | 2010-10-22 02:11:35 +0800 | [diff] [blame] | 1501 | |
Shih-wei Liao | 7c67e57 | 2011-07-19 05:54:53 -0700 | [diff] [blame] | 1502 | // TODO(sliao): LLVM took out the OpaqueType. Any other to migrate to? |
| 1503 | std::vector<llvm::Type*> FieldTypes; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1504 | |
Zonr Chang | 3cd3dd3 | 2010-10-22 02:11:35 +0800 | [diff] [blame] | 1505 | for (const_field_iterator FI = fields_begin(), FE = fields_end(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1506 | FI != FE; |
| 1507 | FI++) { |
| 1508 | const Field *F = *FI; |
| 1509 | const RSExportType *FET = F->getType(); |
Shih-wei Liao | 1f0d88f | 2010-06-25 00:15:24 -0700 | [diff] [blame] | 1510 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1511 | FieldTypes.push_back(FET->getLLVMType()); |
| 1512 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1513 | |
Zonr Chang | 3cd3dd3 | 2010-10-22 02:11:35 +0800 | [diff] [blame] | 1514 | llvm::StructType *ST = llvm::StructType::get(getRSContext()->getLLVMContext(), |
| 1515 | FieldTypes, |
| 1516 | mIsPacked); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1517 | if (ST != nullptr) { |
Shih-wei Liao | 7c67e57 | 2011-07-19 05:54:53 -0700 | [diff] [blame] | 1518 | return ST; |
| 1519 | } else { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1520 | return nullptr; |
Shih-wei Liao | 7c67e57 | 2011-07-19 05:54:53 -0700 | [diff] [blame] | 1521 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1522 | } |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 1523 | |
Zonr Chang | 3cd3dd3 | 2010-10-22 02:11:35 +0800 | [diff] [blame] | 1524 | bool RSExportRecordType::keep() { |
| 1525 | if (!RSExportType::keep()) |
| 1526 | return false; |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 1527 | 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 Chang | 3cd3dd3 | 2010-10-22 02:11:35 +0800 | [diff] [blame] | 1533 | return true; |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 1534 | } |
| 1535 | |
| 1536 | bool 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 Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 1555 | |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 1556 | void 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 Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 1604 | } // namespace slang |