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 | |
Matt Wala | 7682b66 | 2015-07-30 15:53:47 -0700 | [diff] [blame] | 44 | // For the data types we support: |
| 45 | // Category - data type category |
| 46 | // RsType - element name in RenderScript |
| 47 | // RsShortType - short element name in RenderScript |
| 48 | // SizeInBits - size in bits |
| 49 | // CName - reflected C name |
| 50 | // JavaName - reflected Java name |
| 51 | // JavaArrayElementName - reflected name in Java arrays |
| 52 | // CVecName - prefix for C vector types |
| 53 | // JavaVecName - prefix for Java vector type |
| 54 | // JavaPromotion - unsigned type undergoing Java promotion |
| 55 | // |
| 56 | // IMPORTANT: The data types in this table should be at the same index as |
| 57 | // specified by the corresponding DataType enum. |
| 58 | // |
| 59 | // TODO: Pull this information out into a separate file. |
Stephen Hines | fdd1ba1 | 2012-03-08 17:26:32 -0800 | [diff] [blame] | 60 | static RSReflectionType gReflectionTypes[] = { |
Matt Wala | 7682b66 | 2015-07-30 15:53:47 -0700 | [diff] [blame] | 61 | #define _ nullptr |
| 62 | {PrimitiveDataType, "FLOAT_16", "F16", 16, "half", "short", _, "Half", "Half", false}, |
| 63 | {PrimitiveDataType, "FLOAT_32", "F32", 32, "float", "float", "float", "Float", "Float", false}, |
| 64 | {PrimitiveDataType, "FLOAT_64", "F64", 64, "double", "double", "double", "Double", "Double", false}, |
| 65 | {PrimitiveDataType, "SIGNED_8", "I8", 8, "int8_t", "byte", "byte", "Byte", "Byte", false}, |
| 66 | {PrimitiveDataType, "SIGNED_16", "I16", 16, "int16_t", "short", "short", "Short", "Short", false}, |
| 67 | {PrimitiveDataType, "SIGNED_32", "I32", 32, "int32_t", "int", "int", "Int", "Int", false}, |
| 68 | {PrimitiveDataType, "SIGNED_64", "I64", 64, "int64_t", "long", "long", "Long", "Long", false}, |
| 69 | {PrimitiveDataType, "UNSIGNED_8", "U8", 8, "uint8_t", "short", "byte", "UByte", "Short", true}, |
| 70 | {PrimitiveDataType, "UNSIGNED_16", "U16", 16, "uint16_t", "int", "short", "UShort", "Int", true}, |
| 71 | {PrimitiveDataType, "UNSIGNED_32", "U32", 32, "uint32_t", "long", "int", "UInt", "Long", true}, |
| 72 | {PrimitiveDataType, "UNSIGNED_64", "U64", 64, "uint64_t", "long", "long", "ULong", "Long", false}, |
| 73 | {PrimitiveDataType, "BOOLEAN", "BOOLEAN", 8, "bool", "boolean", "byte", _, _, false}, |
| 74 | {PrimitiveDataType, "UNSIGNED_5_6_5", _, 16, _, _, _, _, _, false}, |
| 75 | {PrimitiveDataType, "UNSIGNED_5_5_5_1", _, 16, _, _, _, _, _, false}, |
| 76 | {PrimitiveDataType, "UNSIGNED_4_4_4_4", _, 16, _, _, _, _, _, false}, |
Stephen Hines | fdd1ba1 | 2012-03-08 17:26:32 -0800 | [diff] [blame] | 77 | |
Nicolas Collin | 5f3da4b | 2015-10-21 10:22:51 +0100 | [diff] [blame] | 78 | {MatrixDataType, "MATRIX_2X2", _, 4*32, "rs_matrix2x2", "Matrix2f", _, _, _, false}, |
| 79 | {MatrixDataType, "MATRIX_3X3", _, 9*32, "rs_matrix3x3", "Matrix3f", _, _, _, false}, |
| 80 | {MatrixDataType, "MATRIX_4X4", _, 16*32, "rs_matrix4x4", "Matrix4f", _, _, _, false}, |
Stephen Hines | fdd1ba1 | 2012-03-08 17:26:32 -0800 | [diff] [blame] | 81 | |
Matt Wala | 7682b66 | 2015-07-30 15:53:47 -0700 | [diff] [blame] | 82 | // RS object types are 32 bits in 32-bit RS, but 256 bits in 64-bit RS. |
| 83 | // This is handled specially by the GetSizeInBits(}, method. |
| 84 | {ObjectDataType, "RS_ELEMENT", "ELEMENT", 32, "Element", "Element", _, _, _, false}, |
| 85 | {ObjectDataType, "RS_TYPE", "TYPE", 32, "Type", "Type", _, _, _, false}, |
| 86 | {ObjectDataType, "RS_ALLOCATION", "ALLOCATION", 32, "Allocation", "Allocation", _, _, _, false}, |
| 87 | {ObjectDataType, "RS_SAMPLER", "SAMPLER", 32, "Sampler", "Sampler", _, _, _, false}, |
| 88 | {ObjectDataType, "RS_SCRIPT", "SCRIPT", 32, "Script", "Script", _, _, _, false}, |
| 89 | {ObjectDataType, "RS_MESH", "MESH", 32, "Mesh", "Mesh", _, _, _, false}, |
| 90 | {ObjectDataType, "RS_PATH", "PATH", 32, "Path", "Path", _, _, _, false}, |
| 91 | {ObjectDataType, "RS_PROGRAM_FRAGMENT", "PROGRAM_FRAGMENT", 32, "ProgramFragment", "ProgramFragment", _, _, _, false}, |
| 92 | {ObjectDataType, "RS_PROGRAM_VERTEX", "PROGRAM_VERTEX", 32, "ProgramVertex", "ProgramVertex", _, _, _, false}, |
| 93 | {ObjectDataType, "RS_PROGRAM_RASTER", "PROGRAM_RASTER", 32, "ProgramRaster", "ProgramRaster", _, _, _, false}, |
| 94 | {ObjectDataType, "RS_PROGRAM_STORE", "PROGRAM_STORE", 32, "ProgramStore", "ProgramStore", _, _, _, false}, |
| 95 | {ObjectDataType, "RS_FONT", "FONT", 32, "Font", "Font", _, _, _, false}, |
| 96 | #undef _ |
Stephen Hines | fdd1ba1 | 2012-03-08 17:26:32 -0800 | [diff] [blame] | 97 | }; |
| 98 | |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 99 | const int kMaxVectorSize = 4; |
| 100 | |
| 101 | struct BuiltinInfo { |
| 102 | clang::BuiltinType::Kind builtinTypeKind; |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 103 | DataType type; |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 104 | /* TODO If we return std::string instead of llvm::StringRef, we could build |
| 105 | * the name instead of duplicating the entries. |
| 106 | */ |
| 107 | const char *cname[kMaxVectorSize]; |
| 108 | }; |
| 109 | |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 110 | |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 111 | BuiltinInfo BuiltinInfoTable[] = { |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 112 | {clang::BuiltinType::Bool, DataTypeBoolean, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 113 | {"bool", "bool2", "bool3", "bool4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 114 | {clang::BuiltinType::Char_U, DataTypeUnsigned8, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 115 | {"uchar", "uchar2", "uchar3", "uchar4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 116 | {clang::BuiltinType::UChar, DataTypeUnsigned8, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 117 | {"uchar", "uchar2", "uchar3", "uchar4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 118 | {clang::BuiltinType::Char16, DataTypeSigned16, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 119 | {"short", "short2", "short3", "short4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 120 | {clang::BuiltinType::Char32, DataTypeSigned32, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 121 | {"int", "int2", "int3", "int4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 122 | {clang::BuiltinType::UShort, DataTypeUnsigned16, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 123 | {"ushort", "ushort2", "ushort3", "ushort4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 124 | {clang::BuiltinType::UInt, DataTypeUnsigned32, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 125 | {"uint", "uint2", "uint3", "uint4"}}, |
Tim Murray | e46265d | 2014-08-18 14:16:32 -0700 | [diff] [blame] | 126 | {clang::BuiltinType::ULong, DataTypeUnsigned64, |
| 127 | {"ulong", "ulong2", "ulong3", "ulong4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 128 | {clang::BuiltinType::ULongLong, DataTypeUnsigned64, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 129 | {"ulong", "ulong2", "ulong3", "ulong4"}}, |
| 130 | |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 131 | {clang::BuiltinType::Char_S, DataTypeSigned8, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 132 | {"char", "char2", "char3", "char4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 133 | {clang::BuiltinType::SChar, DataTypeSigned8, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 134 | {"char", "char2", "char3", "char4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 135 | {clang::BuiltinType::Short, DataTypeSigned16, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 136 | {"short", "short2", "short3", "short4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 137 | {clang::BuiltinType::Int, DataTypeSigned32, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 138 | {"int", "int2", "int3", "int4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 139 | {clang::BuiltinType::Long, DataTypeSigned64, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 140 | {"long", "long2", "long3", "long4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 141 | {clang::BuiltinType::LongLong, DataTypeSigned64, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 142 | {"long", "long2", "long3", "long4"}}, |
Pirama Arumuga Nainar | e4dd17d | 2015-04-07 11:09:14 -0700 | [diff] [blame] | 143 | {clang::BuiltinType::Half, DataTypeFloat16, |
| 144 | {"half", "half2", "half3", "half4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 145 | {clang::BuiltinType::Float, DataTypeFloat32, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 146 | {"float", "float2", "float3", "float4"}}, |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 147 | {clang::BuiltinType::Double, DataTypeFloat64, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 148 | {"double", "double2", "double3", "double4"}}, |
| 149 | }; |
| 150 | const int BuiltinInfoTableCount = sizeof(BuiltinInfoTable) / sizeof(BuiltinInfoTable[0]); |
| 151 | |
| 152 | struct NameAndPrimitiveType { |
| 153 | const char *name; |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 154 | DataType dataType; |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | static NameAndPrimitiveType MatrixAndObjectDataTypes[] = { |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 158 | {"rs_matrix2x2", DataTypeRSMatrix2x2}, |
| 159 | {"rs_matrix3x3", DataTypeRSMatrix3x3}, |
| 160 | {"rs_matrix4x4", DataTypeRSMatrix4x4}, |
| 161 | {"rs_element", DataTypeRSElement}, |
| 162 | {"rs_type", DataTypeRSType}, |
| 163 | {"rs_allocation", DataTypeRSAllocation}, |
| 164 | {"rs_sampler", DataTypeRSSampler}, |
| 165 | {"rs_script", DataTypeRSScript}, |
| 166 | {"rs_mesh", DataTypeRSMesh}, |
| 167 | {"rs_path", DataTypeRSPath}, |
| 168 | {"rs_program_fragment", DataTypeRSProgramFragment}, |
| 169 | {"rs_program_vertex", DataTypeRSProgramVertex}, |
| 170 | {"rs_program_raster", DataTypeRSProgramRaster}, |
| 171 | {"rs_program_store", DataTypeRSProgramStore}, |
| 172 | {"rs_font", DataTypeRSFont}, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | const int MatrixAndObjectDataTypesCount = |
| 176 | sizeof(MatrixAndObjectDataTypes) / sizeof(MatrixAndObjectDataTypes[0]); |
| 177 | |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 178 | static const clang::Type *TypeExportableHelper( |
| 179 | const clang::Type *T, |
| 180 | llvm::SmallPtrSet<const clang::Type*, 8>& SPS, |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 181 | slang::RSContext *Context, |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 182 | const clang::VarDecl *VD, |
Stephen Hines | 5bfec8d | 2012-04-04 08:18:57 -0700 | [diff] [blame] | 183 | const clang::RecordDecl *TopLevelRecord); |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 184 | |
Tim Murray | ee4016d | 2014-04-10 15:49:08 -0700 | [diff] [blame] | 185 | template <unsigned N> |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 186 | static void ReportTypeError(slang::RSContext *Context, |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 187 | const clang::NamedDecl *ND, |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 188 | const clang::RecordDecl *TopLevelRecord, |
Tim Murray | ee4016d | 2014-04-10 15:49:08 -0700 | [diff] [blame] | 189 | const char (&Message)[N], |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 190 | unsigned int TargetAPI = 0) { |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 191 | // Attempt to use the type declaration first (if we have one). |
| 192 | // Fall back to the variable definition, if we are looking at something |
| 193 | // like an array declaration that can't be exported. |
| 194 | if (TopLevelRecord) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 195 | Context->ReportError(TopLevelRecord->getLocation(), Message) |
| 196 | << TopLevelRecord->getName() << TargetAPI; |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 197 | } else if (ND) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 198 | Context->ReportError(ND->getLocation(), Message) << ND->getName() |
| 199 | << TargetAPI; |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 200 | } else { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 201 | slangAssert(false && "Variables should be validated before exporting"); |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 202 | } |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | static const clang::Type *ConstantArrayTypeExportableHelper( |
| 206 | const clang::ConstantArrayType *CAT, |
| 207 | llvm::SmallPtrSet<const clang::Type*, 8>& SPS, |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 208 | slang::RSContext *Context, |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 209 | const clang::VarDecl *VD, |
Stephen Hines | 5bfec8d | 2012-04-04 08:18:57 -0700 | [diff] [blame] | 210 | const clang::RecordDecl *TopLevelRecord) { |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 211 | // Check element type |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 212 | const clang::Type *ElementType = GetConstantArrayElementType(CAT); |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 213 | if (ElementType->isArrayType()) { |
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 | "multidimensional arrays 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 | } else if (ElementType->isExtVectorType()) { |
| 218 | const clang::ExtVectorType *EVT = |
| 219 | static_cast<const clang::ExtVectorType*>(ElementType); |
| 220 | unsigned numElements = EVT->getNumElements(); |
| 221 | |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 222 | const clang::Type *BaseElementType = GetExtVectorElementType(EVT); |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 223 | if (!RSExportPrimitiveType::IsPrimitiveType(BaseElementType)) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 224 | ReportTypeError(Context, VD, TopLevelRecord, |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 225 | "vectors of non-primitive types cannot be exported: '%0'"); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 226 | return nullptr; |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | if (numElements == 3 && CAT->getSize() != 1) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 230 | ReportTypeError(Context, VD, TopLevelRecord, |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 231 | "arrays of width 3 vector types cannot be exported: '%0'"); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 232 | return nullptr; |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 236 | if (TypeExportableHelper(ElementType, SPS, Context, VD, |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 237 | TopLevelRecord) == nullptr) { |
| 238 | return nullptr; |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 239 | } else { |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 240 | return CAT; |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 241 | } |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 242 | } |
| 243 | |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 244 | BuiltinInfo *FindBuiltinType(clang::BuiltinType::Kind builtinTypeKind) { |
| 245 | for (int i = 0; i < BuiltinInfoTableCount; i++) { |
| 246 | if (builtinTypeKind == BuiltinInfoTable[i].builtinTypeKind) { |
| 247 | return &BuiltinInfoTable[i]; |
| 248 | } |
| 249 | } |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 250 | return nullptr; |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 253 | static const clang::Type *TypeExportableHelper( |
Stephen Hines | e67239d | 2012-02-24 15:08:36 -0800 | [diff] [blame] | 254 | clang::Type const *T, |
| 255 | llvm::SmallPtrSet<clang::Type const *, 8> &SPS, |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 256 | slang::RSContext *Context, |
Stephen Hines | e67239d | 2012-02-24 15:08:36 -0800 | [diff] [blame] | 257 | clang::VarDecl const *VD, |
Stephen Hines | 5bfec8d | 2012-04-04 08:18:57 -0700 | [diff] [blame] | 258 | clang::RecordDecl const *TopLevelRecord) { |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 259 | // Normalize first |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 260 | if ((T = GetCanonicalType(T)) == nullptr) |
| 261 | return nullptr; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 262 | |
| 263 | if (SPS.count(T)) |
| 264 | return T; |
| 265 | |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 266 | const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr(); |
| 267 | |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 268 | switch (T->getTypeClass()) { |
| 269 | case clang::Type::Builtin: { |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 270 | const clang::BuiltinType *BT = static_cast<const clang::BuiltinType*>(CTI); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 271 | return FindBuiltinType(BT->getKind()) == nullptr ? nullptr : T; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 272 | } |
| 273 | case clang::Type::Record: { |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 274 | if (RSExportPrimitiveType::GetRSSpecificType(T) != DataTypeUnknown) { |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 275 | return T; // RS object type, no further checks are needed |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 276 | } |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 277 | |
| 278 | // Check internal struct |
Stephen Hines | cd440ee | 2010-12-06 14:57:04 -0800 | [diff] [blame] | 279 | if (T->isUnionType()) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 280 | ReportTypeError(Context, VD, T->getAsUnionType()->getDecl(), |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 281 | "unions cannot be exported: '%0'"); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 282 | return nullptr; |
Stephen Hines | cd440ee | 2010-12-06 14:57:04 -0800 | [diff] [blame] | 283 | } else if (!T->isStructureType()) { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 284 | slangAssert(false && "Unknown type cannot be exported"); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 285 | return nullptr; |
Stephen Hines | cd440ee | 2010-12-06 14:57:04 -0800 | [diff] [blame] | 286 | } |
| 287 | |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 288 | clang::RecordDecl *RD = T->getAsStructureType()->getDecl(); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 289 | if (RD != nullptr) { |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 290 | RD = RD->getDefinition(); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 291 | if (RD == nullptr) { |
| 292 | ReportTypeError(Context, nullptr, T->getAsStructureType()->getDecl(), |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 293 | "struct is not defined in this module"); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 294 | return nullptr; |
Stephen Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 295 | } |
| 296 | } |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 297 | |
| 298 | if (!TopLevelRecord) { |
| 299 | TopLevelRecord = RD; |
| 300 | } |
| 301 | if (RD->getName().empty()) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 302 | ReportTypeError(Context, nullptr, RD, |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 303 | "anonymous structures cannot be exported"); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 304 | return nullptr; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | // Fast check |
| 308 | if (RD->hasFlexibleArrayMember() || RD->hasObjectMember()) |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 309 | return nullptr; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 310 | |
| 311 | // Insert myself into checking set |
| 312 | SPS.insert(T); |
| 313 | |
| 314 | // Check all element |
| 315 | for (clang::RecordDecl::field_iterator FI = RD->field_begin(), |
| 316 | FE = RD->field_end(); |
| 317 | FI != FE; |
| 318 | FI++) { |
| 319 | const clang::FieldDecl *FD = *FI; |
| 320 | const clang::Type *FT = RSExportType::GetTypeOfDecl(FD); |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 321 | FT = GetCanonicalType(FT); |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 322 | |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 323 | if (!TypeExportableHelper(FT, SPS, Context, VD, TopLevelRecord)) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 324 | return nullptr; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 325 | } |
Stephen Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 326 | |
| 327 | // We don't support bit fields yet |
| 328 | // |
| 329 | // TODO(zonr/srhines): allow bit fields of size 8, 16, 32 |
| 330 | if (FD->isBitField()) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 331 | Context->ReportError( |
| 332 | FD->getLocation(), |
| 333 | "bit fields are not able to be exported: '%0.%1'") |
| 334 | << RD->getName() << FD->getName(); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 335 | return nullptr; |
Stephen Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 336 | } |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | return T; |
| 340 | } |
| 341 | case clang::Type::Pointer: { |
| 342 | if (TopLevelRecord) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 343 | ReportTypeError(Context, VD, TopLevelRecord, |
Stephen Hines | ab94bcc | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 344 | "structures containing pointers cannot be used as the type of " |
| 345 | "an exported global variable or the parameter to an exported " |
| 346 | "function: '%0'"); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 347 | return nullptr; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 348 | } |
Stephen Hines | 24e79f6 | 2010-12-13 12:55:57 -0800 | [diff] [blame] | 349 | |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 350 | const clang::PointerType *PT = static_cast<const clang::PointerType*>(CTI); |
| 351 | const clang::Type *PointeeType = GetPointeeType(PT); |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 352 | |
Stephen Hines | aa82e74 | 2012-04-11 12:38:17 -0700 | [diff] [blame] | 353 | if (PointeeType->getTypeClass() == clang::Type::Pointer) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 354 | ReportTypeError(Context, VD, TopLevelRecord, |
Stephen Hines | aa82e74 | 2012-04-11 12:38:17 -0700 | [diff] [blame] | 355 | "multiple levels of pointers cannot be exported: '%0'"); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 356 | return nullptr; |
Stephen Hines | aa82e74 | 2012-04-11 12:38:17 -0700 | [diff] [blame] | 357 | } |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 358 | // We don't support pointer with array-type pointee or unsupported pointee |
| 359 | // type |
| 360 | if (PointeeType->isArrayType() || |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 361 | (TypeExportableHelper(PointeeType, SPS, Context, VD, |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 362 | TopLevelRecord) == nullptr)) |
| 363 | return nullptr; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 364 | else |
| 365 | return T; |
| 366 | } |
| 367 | case clang::Type::ExtVector: { |
| 368 | const clang::ExtVectorType *EVT = |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 369 | static_cast<const clang::ExtVectorType*>(CTI); |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 370 | // Only vector with size 2, 3 and 4 are supported. |
| 371 | if (EVT->getNumElements() < 2 || EVT->getNumElements() > 4) |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 372 | return nullptr; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 373 | |
| 374 | // Check base element type |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 375 | const clang::Type *ElementType = GetExtVectorElementType(EVT); |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 376 | |
| 377 | if ((ElementType->getTypeClass() != clang::Type::Builtin) || |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 378 | (TypeExportableHelper(ElementType, SPS, Context, VD, |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 379 | TopLevelRecord) == nullptr)) |
| 380 | return nullptr; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 381 | else |
| 382 | return T; |
| 383 | } |
| 384 | case clang::Type::ConstantArray: { |
| 385 | const clang::ConstantArrayType *CAT = |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 386 | static_cast<const clang::ConstantArrayType*>(CTI); |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 387 | |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 388 | return ConstantArrayTypeExportableHelper(CAT, SPS, Context, VD, |
Stephen Hines | 5bfec8d | 2012-04-04 08:18:57 -0700 | [diff] [blame] | 389 | TopLevelRecord); |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 390 | } |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 391 | case clang::Type::Enum: { |
| 392 | // FIXME: We currently convert enums to integers, rather than reflecting |
| 393 | // a more complete (and nicer type-safe Java version). |
| 394 | return Context->getASTContext().IntTy.getTypePtr(); |
| 395 | } |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 396 | default: { |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 397 | slangAssert(false && "Unknown type cannot be validated"); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 398 | return nullptr; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | // 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] | 404 | // the canonical type. |
| 405 | // |
| 406 | // If the Type T is not exportable, this function returns nullptr. DiagEngine is |
| 407 | // used to generate proper Clang diagnostic messages when a non-exportable type |
| 408 | // is detected. TopLevelRecord is used to capture the highest struct (in the |
| 409 | // case of a nested hierarchy) for detecting other types that cannot be exported |
| 410 | // (mostly pointers within a struct). |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 411 | static const clang::Type *TypeExportable(const clang::Type *T, |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 412 | slang::RSContext *Context, |
Stephen Hines | 5bfec8d | 2012-04-04 08:18:57 -0700 | [diff] [blame] | 413 | const clang::VarDecl *VD) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 414 | llvm::SmallPtrSet<const clang::Type*, 8> SPS = |
| 415 | llvm::SmallPtrSet<const clang::Type*, 8>(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 416 | |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 417 | return TypeExportableHelper(T, SPS, Context, VD, nullptr); |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 420 | static bool ValidateRSObjectInVarDecl(slang::RSContext *Context, |
| 421 | clang::VarDecl *VD, bool InCompositeType, |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 422 | unsigned int TargetAPI) { |
| 423 | if (TargetAPI < SLANG_JB_TARGET_API) { |
| 424 | // Only if we are already in a composite type (like an array or structure). |
| 425 | if (InCompositeType) { |
| 426 | // Only if we are actually exported (i.e. non-static). |
Stephen Hines | 44f1006 | 2013-06-13 00:33:23 -0700 | [diff] [blame] | 427 | if (VD->hasLinkage() && |
| 428 | (VD->getFormalLinkage() == clang::ExternalLinkage)) { |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 429 | // Only if we are not a pointer to an object. |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 430 | const clang::Type *T = GetCanonicalType(VD->getType().getTypePtr()); |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 431 | if (T->getTypeClass() != clang::Type::Pointer) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 432 | ReportTypeError(Context, VD, nullptr, |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 433 | "arrays/structures containing RS object types " |
| 434 | "cannot be exported in target API < %1: '%0'", |
| 435 | SLANG_JB_TARGET_API); |
| 436 | return false; |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | return true; |
| 443 | } |
| 444 | |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 445 | // Helper function for ValidateType(). We do a recursive descent on the |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 446 | // type hierarchy to ensure that we can properly export/handle the |
| 447 | // declaration. |
| 448 | // \return true if the variable declaration is valid, |
| 449 | // false if it is invalid (along with proper diagnostics). |
| 450 | // |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 451 | // C - ASTContext (for diagnostics + builtin types). |
| 452 | // T - sub-type that we are validating. |
| 453 | // ND - (optional) top-level named declaration that we are validating. |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 454 | // SPS - set of types we have already seen/validated. |
| 455 | // InCompositeType - true if we are within an outer composite type. |
| 456 | // UnionDecl - set if we are in a sub-type of a union. |
| 457 | // TargetAPI - target SDK API level. |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 458 | // IsFilterscript - whether or not we are compiling for Filterscript |
Stephen Hines | ab94bcc | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 459 | // IsExtern - is this type externally visible (i.e. extern global or parameter |
| 460 | // to an extern function) |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 461 | static bool ValidateTypeHelper( |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 462 | slang::RSContext *Context, |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 463 | clang::ASTContext &C, |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 464 | const clang::Type *&T, |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 465 | clang::NamedDecl *ND, |
| 466 | clang::SourceLocation Loc, |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 467 | llvm::SmallPtrSet<const clang::Type*, 8>& SPS, |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 468 | bool InCompositeType, |
| 469 | clang::RecordDecl *UnionDecl, |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 470 | unsigned int TargetAPI, |
Stephen Hines | ab94bcc | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 471 | bool IsFilterscript, |
| 472 | bool IsExtern) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 473 | if ((T = GetCanonicalType(T)) == nullptr) |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 474 | return true; |
| 475 | |
| 476 | if (SPS.count(T)) |
| 477 | return true; |
| 478 | |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 479 | const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr(); |
| 480 | |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 481 | switch (T->getTypeClass()) { |
| 482 | case clang::Type::Record: { |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 483 | if (RSExportPrimitiveType::IsRSObjectType(T)) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 484 | clang::VarDecl *VD = (ND ? llvm::dyn_cast<clang::VarDecl>(ND) : nullptr); |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 485 | if (VD && !ValidateRSObjectInVarDecl(Context, VD, InCompositeType, |
| 486 | TargetAPI)) { |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 487 | return false; |
| 488 | } |
| 489 | } |
| 490 | |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 491 | if (RSExportPrimitiveType::GetRSSpecificType(T) != DataTypeUnknown) { |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 492 | if (!UnionDecl) { |
| 493 | return true; |
| 494 | } else if (RSExportPrimitiveType::IsRSObjectType(T)) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 495 | ReportTypeError(Context, nullptr, UnionDecl, |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 496 | "unions containing RS object types are not allowed"); |
| 497 | return false; |
| 498 | } |
| 499 | } |
| 500 | |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 501 | clang::RecordDecl *RD = nullptr; |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 502 | |
| 503 | // Check internal struct |
| 504 | if (T->isUnionType()) { |
| 505 | RD = T->getAsUnionType()->getDecl(); |
| 506 | UnionDecl = RD; |
| 507 | } else if (T->isStructureType()) { |
| 508 | RD = T->getAsStructureType()->getDecl(); |
| 509 | } else { |
| 510 | slangAssert(false && "Unknown type cannot be exported"); |
| 511 | return false; |
| 512 | } |
| 513 | |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 514 | if (RD != nullptr) { |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 515 | RD = RD->getDefinition(); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 516 | if (RD == nullptr) { |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 517 | // FIXME |
| 518 | return true; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | // Fast check |
| 523 | if (RD->hasFlexibleArrayMember() || RD->hasObjectMember()) |
| 524 | return false; |
| 525 | |
| 526 | // Insert myself into checking set |
| 527 | SPS.insert(T); |
| 528 | |
| 529 | // Check all elements |
| 530 | for (clang::RecordDecl::field_iterator FI = RD->field_begin(), |
| 531 | FE = RD->field_end(); |
| 532 | FI != FE; |
| 533 | FI++) { |
| 534 | const clang::FieldDecl *FD = *FI; |
| 535 | const clang::Type *FT = RSExportType::GetTypeOfDecl(FD); |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 536 | FT = GetCanonicalType(FT); |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 537 | |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 538 | if (!ValidateTypeHelper(Context, C, FT, ND, Loc, SPS, true, UnionDecl, |
Stephen Hines | ab94bcc | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 539 | TargetAPI, IsFilterscript, IsExtern)) { |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 540 | return false; |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | return true; |
| 545 | } |
| 546 | |
| 547 | case clang::Type::Builtin: { |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 548 | if (IsFilterscript) { |
| 549 | clang::QualType QT = T->getCanonicalTypeInternal(); |
| 550 | if (QT == C.DoubleTy || |
| 551 | QT == C.LongDoubleTy || |
| 552 | QT == C.LongTy || |
| 553 | QT == C.LongLongTy) { |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 554 | if (ND) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 555 | Context->ReportError( |
| 556 | Loc, |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 557 | "Builtin types > 32 bits in size are forbidden in " |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 558 | "Filterscript: '%0'") |
| 559 | << ND->getName(); |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 560 | } else { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 561 | Context->ReportError( |
| 562 | Loc, |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 563 | "Builtin types > 32 bits in size are forbidden in " |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 564 | "Filterscript"); |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 565 | } |
| 566 | return false; |
| 567 | } |
| 568 | } |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 569 | break; |
| 570 | } |
| 571 | |
| 572 | case clang::Type::Pointer: { |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 573 | if (IsFilterscript) { |
| 574 | if (ND) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 575 | Context->ReportError(Loc, |
| 576 | "Pointers are forbidden in Filterscript: '%0'") |
| 577 | << ND->getName(); |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 578 | return false; |
| 579 | } else { |
| 580 | // TODO(srhines): Find a better way to handle expressions (i.e. no |
| 581 | // NamedDecl) involving pointers in FS that should be allowed. |
| 582 | // An example would be calls to library functions like |
| 583 | // rsMatrixMultiply() that take rs_matrixNxN * types. |
| 584 | } |
| 585 | } |
| 586 | |
Stephen Hines | ab94bcc | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 587 | // Forbid pointers in structures that are externally visible. |
| 588 | if (InCompositeType && IsExtern) { |
| 589 | if (ND) { |
| 590 | Context->ReportError(Loc, |
| 591 | "structures containing pointers cannot be used as the type of " |
| 592 | "an exported global variable or the parameter to an exported " |
| 593 | "function: '%0'") |
| 594 | << ND->getName(); |
| 595 | } else { |
| 596 | Context->ReportError(Loc, |
| 597 | "structures containing pointers cannot be used as the type of " |
| 598 | "an exported global variable or the parameter to an exported " |
| 599 | "function"); |
| 600 | } |
| 601 | return false; |
| 602 | } |
| 603 | |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 604 | const clang::PointerType *PT = static_cast<const clang::PointerType*>(CTI); |
| 605 | const clang::Type *PointeeType = GetPointeeType(PT); |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 606 | |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 607 | return ValidateTypeHelper(Context, C, PointeeType, ND, Loc, SPS, |
| 608 | InCompositeType, UnionDecl, TargetAPI, |
Stephen Hines | ab94bcc | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 609 | IsFilterscript, IsExtern); |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | case clang::Type::ExtVector: { |
| 613 | const clang::ExtVectorType *EVT = |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 614 | static_cast<const clang::ExtVectorType*>(CTI); |
| 615 | const clang::Type *ElementType = GetExtVectorElementType(EVT); |
Stephen Hines | b13b85e | 2012-08-31 19:27:48 -0700 | [diff] [blame] | 616 | if (TargetAPI < SLANG_ICS_TARGET_API && |
| 617 | InCompositeType && |
Stephen Hines | fdae63e | 2012-11-28 14:42:44 -0800 | [diff] [blame] | 618 | EVT->getNumElements() == 3 && |
| 619 | ND && |
Stephen Hines | 44f1006 | 2013-06-13 00:33:23 -0700 | [diff] [blame] | 620 | ND->getFormalLinkage() == clang::ExternalLinkage) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 621 | ReportTypeError(Context, ND, nullptr, |
Stephen Hines | b13b85e | 2012-08-31 19:27:48 -0700 | [diff] [blame] | 622 | "structs containing vectors of dimension 3 cannot " |
| 623 | "be exported at this API level: '%0'"); |
| 624 | return false; |
| 625 | } |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 626 | return ValidateTypeHelper(Context, C, ElementType, ND, Loc, SPS, true, |
Stephen Hines | ab94bcc | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 627 | UnionDecl, TargetAPI, IsFilterscript, IsExtern); |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | case clang::Type::ConstantArray: { |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 631 | const clang::ConstantArrayType *CAT = static_cast<const clang::ConstantArrayType*>(CTI); |
| 632 | const clang::Type *ElementType = GetConstantArrayElementType(CAT); |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 633 | return ValidateTypeHelper(Context, C, ElementType, ND, Loc, SPS, true, |
Stephen Hines | ab94bcc | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 634 | UnionDecl, TargetAPI, IsFilterscript, IsExtern); |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | default: { |
| 638 | break; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | return true; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 643 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 644 | |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 645 | } // namespace |
| 646 | |
Jean-Luc Brouillet | eca0534 | 2014-05-15 12:44:21 -0700 | [diff] [blame] | 647 | std::string CreateDummyName(const char *type, const std::string &name) { |
| 648 | std::stringstream S; |
| 649 | S << "<" << type; |
| 650 | if (!name.empty()) { |
| 651 | S << ":" << name; |
| 652 | } |
| 653 | S << ">"; |
| 654 | return S.str(); |
| 655 | } |
| 656 | |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 657 | /****************************** RSExportType ******************************/ |
| 658 | bool RSExportType::NormalizeType(const clang::Type *&T, |
| 659 | llvm::StringRef &TypeName, |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 660 | RSContext *Context, |
Stephen Hines | 5bfec8d | 2012-04-04 08:18:57 -0700 | [diff] [blame] | 661 | const clang::VarDecl *VD) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 662 | if ((T = TypeExportable(T, Context, VD)) == nullptr) { |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 663 | return false; |
| 664 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 665 | // Get type name |
| 666 | TypeName = RSExportType::GetTypeName(T); |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 667 | if (Context && TypeName.empty()) { |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 668 | if (VD) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 669 | Context->ReportError(VD->getLocation(), |
| 670 | "anonymous types cannot be exported"); |
Stephen Hines | 48d893d | 2013-12-06 18:00:27 -0800 | [diff] [blame] | 671 | } else { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 672 | Context->ReportError("anonymous types cannot be exported"); |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 673 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 674 | return false; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 675 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 676 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 677 | return true; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 678 | } |
| 679 | |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 680 | bool RSExportType::ValidateType(slang::RSContext *Context, clang::ASTContext &C, |
| 681 | clang::QualType QT, clang::NamedDecl *ND, |
| 682 | clang::SourceLocation Loc, |
Stephen Hines | ab94bcc | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 683 | unsigned int TargetAPI, bool IsFilterscript, |
| 684 | bool IsExtern) { |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 685 | const clang::Type *T = QT.getTypePtr(); |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 686 | llvm::SmallPtrSet<const clang::Type*, 8> SPS = |
| 687 | llvm::SmallPtrSet<const clang::Type*, 8>(); |
| 688 | |
Stephen Hines | ab94bcc | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 689 | // If this is an externally visible variable declaration, we check if the |
| 690 | // type is able to be exported first. |
Stephen Hines | 33ea573 | 2015-06-16 16:00:26 -0700 | [diff] [blame] | 691 | if (auto VD = llvm::dyn_cast_or_null<clang::VarDecl>(ND)) { |
Stephen Hines | ab94bcc | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 692 | if (VD->getFormalLinkage() == clang::ExternalLinkage) { |
| 693 | if (!TypeExportable(T, Context, VD)) { |
| 694 | return false; |
| 695 | } |
| 696 | } |
| 697 | } |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 698 | return ValidateTypeHelper(Context, C, T, ND, Loc, SPS, false, nullptr, TargetAPI, |
Stephen Hines | ab94bcc | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 699 | IsFilterscript, IsExtern); |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 700 | } |
| 701 | |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 702 | bool RSExportType::ValidateVarDecl(slang::RSContext *Context, |
| 703 | clang::VarDecl *VD, unsigned int TargetAPI, |
Stephen Hines | 11274a7 | 2012-09-26 19:14:20 -0700 | [diff] [blame] | 704 | bool IsFilterscript) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 705 | return ValidateType(Context, VD->getASTContext(), VD->getType(), VD, |
Stephen Hines | ab94bcc | 2015-06-11 17:00:04 -0700 | [diff] [blame] | 706 | VD->getLocation(), TargetAPI, IsFilterscript, |
| 707 | (VD->getFormalLinkage() == clang::ExternalLinkage)); |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 710 | const clang::Type |
| 711 | *RSExportType::GetTypeOfDecl(const clang::DeclaratorDecl *DD) { |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 712 | if (DD) { |
Stephen Hines | 8de1922 | 2013-08-23 16:30:59 -0700 | [diff] [blame] | 713 | clang::QualType T = DD->getType(); |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 714 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 715 | if (T.isNull()) |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 716 | return nullptr; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 717 | else |
| 718 | return T.getTypePtr(); |
| 719 | } |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 720 | return nullptr; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | llvm::StringRef RSExportType::GetTypeName(const clang::Type* T) { |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 724 | T = GetCanonicalType(T); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 725 | if (T == nullptr) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 726 | return llvm::StringRef(); |
| 727 | |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 728 | const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr(); |
| 729 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 730 | switch (T->getTypeClass()) { |
| 731 | case clang::Type::Builtin: { |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 732 | const clang::BuiltinType *BT = static_cast<const clang::BuiltinType*>(CTI); |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 733 | BuiltinInfo *info = FindBuiltinType(BT->getKind()); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 734 | if (info != nullptr) { |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 735 | return info->cname[0]; |
Zonr Chang | a65ec16 | 2010-10-17 01:53:05 +0800 | [diff] [blame] | 736 | } |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 737 | slangAssert(false && "Unknown data type of the builtin"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 738 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 739 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 740 | case clang::Type::Record: { |
Stephen Hines | cd440ee | 2010-12-06 14:57:04 -0800 | [diff] [blame] | 741 | clang::RecordDecl *RD; |
| 742 | if (T->isStructureType()) { |
| 743 | RD = T->getAsStructureType()->getDecl(); |
Stephen Hines | dd6206b | 2010-12-09 19:39:22 -0800 | [diff] [blame] | 744 | } else { |
Stephen Hines | cd440ee | 2010-12-06 14:57:04 -0800 | [diff] [blame] | 745 | break; |
| 746 | } |
| 747 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 748 | llvm::StringRef Name = RD->getName(); |
| 749 | if (Name.empty()) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 750 | if (RD->getTypedefNameForAnonDecl() != nullptr) { |
Shih-wei Liao | 83f0c62 | 2011-06-21 05:34:53 -0700 | [diff] [blame] | 751 | Name = RD->getTypedefNameForAnonDecl()->getName(); |
| 752 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 753 | |
Shih-wei Liao | 83f0c62 | 2011-06-21 05:34:53 -0700 | [diff] [blame] | 754 | if (Name.empty()) { |
| 755 | // Try to find a name from redeclaration (i.e. typedef) |
| 756 | for (clang::TagDecl::redecl_iterator RI = RD->redecls_begin(), |
| 757 | RE = RD->redecls_end(); |
| 758 | RI != RE; |
| 759 | RI++) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 760 | slangAssert(*RI != nullptr && "cannot be NULL object"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 761 | |
Shih-wei Liao | 83f0c62 | 2011-06-21 05:34:53 -0700 | [diff] [blame] | 762 | Name = (*RI)->getName(); |
| 763 | if (!Name.empty()) |
| 764 | break; |
| 765 | } |
| 766 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 767 | } |
| 768 | return Name; |
| 769 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 770 | case clang::Type::Pointer: { |
| 771 | // "*" plus pointee name |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 772 | const clang::PointerType *P = static_cast<const clang::PointerType*>(CTI); |
| 773 | const clang::Type *PT = GetPointeeType(P); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 774 | llvm::StringRef PointeeName; |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 775 | if (NormalizeType(PT, PointeeName, nullptr, nullptr)) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 776 | char *Name = new char[ 1 /* * */ + PointeeName.size() + 1 ]; |
| 777 | Name[0] = '*'; |
| 778 | memcpy(Name + 1, PointeeName.data(), PointeeName.size()); |
| 779 | Name[PointeeName.size() + 1] = '\0'; |
| 780 | return Name; |
| 781 | } |
| 782 | break; |
| 783 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 784 | case clang::Type::ExtVector: { |
| 785 | const clang::ExtVectorType *EVT = |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 786 | static_cast<const clang::ExtVectorType*>(CTI); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 787 | return RSExportVectorType::GetTypeName(EVT); |
| 788 | break; |
| 789 | } |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 790 | case clang::Type::ConstantArray : { |
| 791 | // Construct name for a constant array is too complicated. |
Stephen Hines | 340b555 | 2014-09-05 02:03:36 -0700 | [diff] [blame] | 792 | return "<ConstantArray>"; |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 793 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 794 | default: { |
| 795 | break; |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | return llvm::StringRef(); |
| 800 | } |
| 801 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 802 | |
| 803 | RSExportType *RSExportType::Create(RSContext *Context, |
| 804 | const clang::Type *T, |
| 805 | const llvm::StringRef &TypeName) { |
| 806 | // Lookup the context to see whether the type was processed before. |
| 807 | // Newly created RSExportType will insert into context |
| 808 | // in RSExportType::RSExportType() |
| 809 | RSContext::export_type_iterator ETI = Context->findExportType(TypeName); |
| 810 | |
| 811 | if (ETI != Context->export_types_end()) |
| 812 | return ETI->second; |
| 813 | |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 814 | const clang::Type *CTI = T->getCanonicalTypeInternal().getTypePtr(); |
| 815 | |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 816 | RSExportType *ET = nullptr; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 817 | switch (T->getTypeClass()) { |
| 818 | case clang::Type::Record: { |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 819 | DataType dt = RSExportPrimitiveType::GetRSSpecificType(TypeName); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 820 | switch (dt) { |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 821 | case DataTypeUnknown: { |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 822 | // User-defined types |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 823 | ET = RSExportRecordType::Create(Context, |
| 824 | T->getAsStructureType(), |
| 825 | TypeName); |
| 826 | break; |
| 827 | } |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 828 | case DataTypeRSMatrix2x2: { |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 829 | // 2 x 2 Matrix type |
| 830 | ET = RSExportMatrixType::Create(Context, |
| 831 | T->getAsStructureType(), |
| 832 | TypeName, |
| 833 | 2); |
| 834 | break; |
| 835 | } |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 836 | case DataTypeRSMatrix3x3: { |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 837 | // 3 x 3 Matrix type |
| 838 | ET = RSExportMatrixType::Create(Context, |
| 839 | T->getAsStructureType(), |
| 840 | TypeName, |
| 841 | 3); |
| 842 | break; |
| 843 | } |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 844 | case DataTypeRSMatrix4x4: { |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 845 | // 4 x 4 Matrix type |
| 846 | ET = RSExportMatrixType::Create(Context, |
| 847 | T->getAsStructureType(), |
| 848 | TypeName, |
| 849 | 4); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 850 | break; |
| 851 | } |
| 852 | default: { |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 853 | // Others are primitive types |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 854 | ET = RSExportPrimitiveType::Create(Context, T, TypeName); |
| 855 | break; |
| 856 | } |
| 857 | } |
| 858 | break; |
| 859 | } |
| 860 | case clang::Type::Builtin: { |
| 861 | ET = RSExportPrimitiveType::Create(Context, T, TypeName); |
| 862 | break; |
| 863 | } |
| 864 | case clang::Type::Pointer: { |
| 865 | ET = RSExportPointerType::Create(Context, |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 866 | static_cast<const clang::PointerType*>(CTI), |
| 867 | TypeName); |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 868 | // FIXME: free the name (allocated in RSExportType::GetTypeName) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 869 | delete [] TypeName.data(); |
| 870 | break; |
| 871 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 872 | case clang::Type::ExtVector: { |
| 873 | ET = RSExportVectorType::Create(Context, |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 874 | static_cast<const clang::ExtVectorType*>(CTI), |
| 875 | TypeName); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 876 | break; |
| 877 | } |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 878 | case clang::Type::ConstantArray: { |
| 879 | ET = RSExportConstantArrayType::Create( |
| 880 | Context, |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 881 | static_cast<const clang::ConstantArrayType*>(CTI)); |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 882 | break; |
| 883 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 884 | default: { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 885 | Context->ReportError("unknown type cannot be exported: '%0'") |
| 886 | << T->getTypeClassName(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 887 | break; |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | return ET; |
| 892 | } |
| 893 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 894 | RSExportType *RSExportType::Create(RSContext *Context, const clang::Type *T) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 895 | llvm::StringRef TypeName; |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 896 | if (NormalizeType(T, TypeName, Context, nullptr)) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 897 | return Create(Context, T, TypeName); |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 898 | } else { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 899 | return nullptr; |
Stephen Hines | d5a84f6 | 2012-04-04 17:44:38 -0700 | [diff] [blame] | 900 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 901 | } |
| 902 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 903 | RSExportType *RSExportType::CreateFromDecl(RSContext *Context, |
| 904 | const clang::VarDecl *VD) { |
| 905 | return RSExportType::Create(Context, GetTypeOfDecl(VD)); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Jean-Luc Brouillet | c95381a | 2014-05-14 21:24:45 -0700 | [diff] [blame] | 908 | size_t RSExportType::getStoreSize() const { |
| 909 | return getRSContext()->getDataLayout()->getTypeStoreSize(getLLVMType()); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 910 | } |
| 911 | |
Jean-Luc Brouillet | c95381a | 2014-05-14 21:24:45 -0700 | [diff] [blame] | 912 | size_t RSExportType::getAllocSize() const { |
| 913 | return getRSContext()->getDataLayout()->getTypeAllocSize(getLLVMType()); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 914 | } |
Shih-wei Liao | 1f0d88f | 2010-06-25 00:15:24 -0700 | [diff] [blame] | 915 | |
Zonr Chang | 6b6320a | 2010-10-05 22:42:01 +0800 | [diff] [blame] | 916 | RSExportType::RSExportType(RSContext *Context, |
| 917 | ExportClass Class, |
| 918 | const llvm::StringRef &Name) |
Zonr Chang | a41ce1d | 2010-10-06 02:23:12 +0800 | [diff] [blame] | 919 | : RSExportable(Context, RSExportable::EX_TYPE), |
Zonr Chang | 6b6320a | 2010-10-05 22:42:01 +0800 | [diff] [blame] | 920 | mClass(Class), |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 921 | // Make a copy on Name since memory stored @Name is either allocated in |
| 922 | // ASTContext or allocated in GetTypeName which will be destroyed later. |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 923 | mName(Name.data(), Name.size()), |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 924 | mLLVMType(nullptr) { |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 925 | // Don't cache the type whose name start with '<'. Those type failed to |
| 926 | // get their name since constructing their name in GetTypeName() requiring |
| 927 | // complicated work. |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 928 | if (!IsDummyName(Name)) { |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 929 | // TODO(zonr): Need to check whether the insertion is successful or not. |
| 930 | Context->insertExportType(llvm::StringRef(Name), this); |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 931 | } |
| 932 | |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 933 | } |
| 934 | |
Zonr Chang | 3cd3dd3 | 2010-10-22 02:11:35 +0800 | [diff] [blame] | 935 | bool RSExportType::keep() { |
| 936 | if (!RSExportable::keep()) |
| 937 | return false; |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 938 | // Invalidate converted LLVM type. |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 939 | mLLVMType = nullptr; |
Zonr Chang | 3cd3dd3 | 2010-10-22 02:11:35 +0800 | [diff] [blame] | 940 | return true; |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | bool RSExportType::equals(const RSExportable *E) const { |
| 944 | CHECK_PARENT_EQUALITY(RSExportable, E); |
| 945 | return (static_cast<const RSExportType*>(E)->getClass() == getClass()); |
| 946 | } |
| 947 | |
Zonr Chang | a7a828d | 2010-10-22 03:44:27 +0800 | [diff] [blame] | 948 | RSExportType::~RSExportType() { |
Zonr Chang | a7a828d | 2010-10-22 03:44:27 +0800 | [diff] [blame] | 949 | } |
| 950 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 951 | /************************** RSExportPrimitiveType **************************/ |
Zonr Chang | b1771ef | 2010-10-22 18:03:46 +0800 | [diff] [blame] | 952 | llvm::ManagedStatic<RSExportPrimitiveType::RSSpecificTypeMapTy> |
| 953 | RSExportPrimitiveType::RSSpecificTypeMap; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 954 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 955 | bool RSExportPrimitiveType::IsPrimitiveType(const clang::Type *T) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 956 | if ((T != nullptr) && (T->getTypeClass() == clang::Type::Builtin)) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 957 | return true; |
| 958 | else |
| 959 | return false; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 960 | } |
| 961 | |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 962 | DataType |
Zonr Chang | b1771ef | 2010-10-22 18:03:46 +0800 | [diff] [blame] | 963 | RSExportPrimitiveType::GetRSSpecificType(const llvm::StringRef &TypeName) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 964 | if (TypeName.empty()) |
| 965 | return DataTypeUnknown; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 966 | |
Zonr Chang | b1771ef | 2010-10-22 18:03:46 +0800 | [diff] [blame] | 967 | if (RSSpecificTypeMap->empty()) { |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 968 | for (int i = 0; i < MatrixAndObjectDataTypesCount; i++) { |
Stephen Hines | 3eb819a | 2014-11-24 18:16:52 -0800 | [diff] [blame] | 969 | (*RSSpecificTypeMap)[MatrixAndObjectDataTypes[i].name] = |
| 970 | MatrixAndObjectDataTypes[i].dataType; |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 971 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 972 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 973 | |
Zonr Chang | b1771ef | 2010-10-22 18:03:46 +0800 | [diff] [blame] | 974 | RSSpecificTypeMapTy::const_iterator I = RSSpecificTypeMap->find(TypeName); |
| 975 | if (I == RSSpecificTypeMap->end()) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 976 | return DataTypeUnknown; |
| 977 | else |
| 978 | return I->getValue(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 979 | } |
| 980 | |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 981 | DataType RSExportPrimitiveType::GetRSSpecificType(const clang::Type *T) { |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 982 | T = GetCanonicalType(T); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 983 | if ((T == nullptr) || (T->getTypeClass() != clang::Type::Record)) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 984 | return DataTypeUnknown; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 985 | |
Zonr Chang | b1771ef | 2010-10-22 18:03:46 +0800 | [diff] [blame] | 986 | return GetRSSpecificType( RSExportType::GetTypeName(T) ); |
| 987 | } |
| 988 | |
| 989 | bool RSExportPrimitiveType::IsRSMatrixType(DataType DT) { |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 990 | if (DT < 0 || DT >= DataTypeMax) { |
| 991 | return false; |
| 992 | } |
| 993 | return gReflectionTypes[DT].category == MatrixDataType; |
Zonr Chang | b1771ef | 2010-10-22 18:03:46 +0800 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | bool RSExportPrimitiveType::IsRSObjectType(DataType DT) { |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 997 | if (DT < 0 || DT >= DataTypeMax) { |
| 998 | return false; |
| 999 | } |
| 1000 | return gReflectionTypes[DT].category == ObjectDataType; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1001 | } |
| 1002 | |
Stephen Hines | feaca06 | 2011-02-04 14:08:13 -0800 | [diff] [blame] | 1003 | bool RSExportPrimitiveType::IsStructureTypeWithRSObject(const clang::Type *T) { |
| 1004 | bool RSObjectTypeSeen = false; |
| 1005 | while (T && T->isArrayType()) { |
| 1006 | T = T->getArrayElementTypeNoTypeQual(); |
| 1007 | } |
| 1008 | |
| 1009 | const clang::RecordType *RT = T->getAsStructureType(); |
| 1010 | if (!RT) { |
| 1011 | return false; |
| 1012 | } |
Stephen Hines | b0fabe5 | 2013-01-07 19:06:09 -0800 | [diff] [blame] | 1013 | |
Stephen Hines | feaca06 | 2011-02-04 14:08:13 -0800 | [diff] [blame] | 1014 | const clang::RecordDecl *RD = RT->getDecl(); |
Stephen Hines | b0fabe5 | 2013-01-07 19:06:09 -0800 | [diff] [blame] | 1015 | if (RD) { |
| 1016 | RD = RD->getDefinition(); |
| 1017 | } |
| 1018 | if (!RD) { |
| 1019 | return false; |
| 1020 | } |
| 1021 | |
Stephen Hines | feaca06 | 2011-02-04 14:08:13 -0800 | [diff] [blame] | 1022 | for (clang::RecordDecl::field_iterator FI = RD->field_begin(), |
| 1023 | FE = RD->field_end(); |
| 1024 | FI != FE; |
| 1025 | FI++) { |
| 1026 | // We just look through all field declarations to see if we find a |
| 1027 | // declaration for an RS object type (or an array of one). |
| 1028 | const clang::FieldDecl *FD = *FI; |
| 1029 | const clang::Type *FT = RSExportType::GetTypeOfDecl(FD); |
| 1030 | while (FT && FT->isArrayType()) { |
| 1031 | FT = FT->getArrayElementTypeNoTypeQual(); |
| 1032 | } |
| 1033 | |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 1034 | DataType DT = GetRSSpecificType(FT); |
Stephen Hines | feaca06 | 2011-02-04 14:08:13 -0800 | [diff] [blame] | 1035 | if (IsRSObjectType(DT)) { |
| 1036 | // RS object types definitely need to be zero-initialized |
| 1037 | RSObjectTypeSeen = true; |
| 1038 | } else { |
| 1039 | switch (DT) { |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 1040 | case DataTypeRSMatrix2x2: |
| 1041 | case DataTypeRSMatrix3x3: |
| 1042 | case DataTypeRSMatrix4x4: |
Stephen Hines | feaca06 | 2011-02-04 14:08:13 -0800 | [diff] [blame] | 1043 | // Matrix types should get zero-initialized as well |
| 1044 | RSObjectTypeSeen = true; |
| 1045 | break; |
| 1046 | default: |
| 1047 | // Ignore all other primitive types |
| 1048 | break; |
| 1049 | } |
| 1050 | while (FT && FT->isArrayType()) { |
| 1051 | FT = FT->getArrayElementTypeNoTypeQual(); |
| 1052 | } |
| 1053 | if (FT->isStructureType()) { |
| 1054 | // Recursively handle structs of structs (even though these can't |
| 1055 | // be exported, it is possible for a user to have them internally). |
| 1056 | RSObjectTypeSeen |= IsStructureTypeWithRSObject(FT); |
| 1057 | } |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | return RSObjectTypeSeen; |
| 1062 | } |
| 1063 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1064 | size_t RSExportPrimitiveType::GetSizeInBits(const RSExportPrimitiveType *EPT) { |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 1065 | int type = EPT->getType(); |
| 1066 | slangAssert((type > DataTypeUnknown && type < DataTypeMax) && |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1067 | "RSExportPrimitiveType::GetSizeInBits : unknown data type"); |
Stephen Hines | 9ae18b2 | 2014-06-10 23:53:00 -0700 | [diff] [blame] | 1068 | // All RS object types are 256 bits in 64-bit RS. |
| 1069 | if (EPT->isRSObjectType() && EPT->getRSContext()->is64Bit()) { |
| 1070 | return 256; |
| 1071 | } |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 1072 | return gReflectionTypes[type].size_in_bits; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 1075 | DataType |
Stephen Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 1076 | RSExportPrimitiveType::GetDataType(RSContext *Context, const clang::Type *T) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1077 | if (T == nullptr) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1078 | return DataTypeUnknown; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1079 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1080 | switch (T->getTypeClass()) { |
| 1081 | case clang::Type::Builtin: { |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 1082 | const clang::BuiltinType *BT = |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 1083 | static_cast<const clang::BuiltinType*>(T->getCanonicalTypeInternal().getTypePtr()); |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 1084 | BuiltinInfo *info = FindBuiltinType(BT->getKind()); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1085 | if (info != nullptr) { |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 1086 | return info->type; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1087 | } |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 1088 | // The size of type WChar depend on platform so we abandon the support |
| 1089 | // to them. |
| 1090 | Context->ReportError("built-in type cannot be exported: '%0'") |
| 1091 | << T->getTypeClassName(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1092 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1093 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1094 | case clang::Type::Record: { |
| 1095 | // must be RS object type |
Zonr Chang | b1771ef | 2010-10-22 18:03:46 +0800 | [diff] [blame] | 1096 | return RSExportPrimitiveType::GetRSSpecificType(T); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1097 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1098 | default: { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 1099 | Context->ReportError("primitive type cannot be exported: '%0'") |
Stephen Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 1100 | << T->getTypeClassName(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1101 | break; |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | return DataTypeUnknown; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1106 | } |
| 1107 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1108 | RSExportPrimitiveType |
| 1109 | *RSExportPrimitiveType::Create(RSContext *Context, |
| 1110 | const clang::Type *T, |
| 1111 | const llvm::StringRef &TypeName, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1112 | bool Normalized) { |
Stephen Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 1113 | DataType DT = GetDataType(Context, T); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1114 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1115 | if ((DT == DataTypeUnknown) || TypeName.empty()) |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1116 | return nullptr; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1117 | else |
Zonr Chang | 6b6320a | 2010-10-05 22:42:01 +0800 | [diff] [blame] | 1118 | return new RSExportPrimitiveType(Context, ExportClassPrimitive, TypeName, |
Stephen Hines | 2b8fb64 | 2012-03-09 00:12:47 -0800 | [diff] [blame] | 1119 | DT, Normalized); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1120 | } |
| 1121 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1122 | RSExportPrimitiveType *RSExportPrimitiveType::Create(RSContext *Context, |
Stephen Hines | 2b8fb64 | 2012-03-09 00:12:47 -0800 | [diff] [blame] | 1123 | const clang::Type *T) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1124 | llvm::StringRef TypeName; |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1125 | if (RSExportType::NormalizeType(T, TypeName, Context, nullptr) |
Stephen Hines | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 1126 | && IsPrimitiveType(T)) { |
Stephen Hines | 2b8fb64 | 2012-03-09 00:12:47 -0800 | [diff] [blame] | 1127 | return Create(Context, T, TypeName); |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 1128 | } else { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1129 | return nullptr; |
Stephen Hines | e5e6443 | 2010-12-02 18:48:20 -0800 | [diff] [blame] | 1130 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
Shih-wei Liao | 7c67e57 | 2011-07-19 05:54:53 -0700 | [diff] [blame] | 1133 | llvm::Type *RSExportPrimitiveType::convertToLLVMType() const { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1134 | llvm::LLVMContext &C = getRSContext()->getLLVMContext(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1135 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1136 | if (isRSObjectType()) { |
| 1137 | // struct { |
| 1138 | // int *p; |
| 1139 | // } __attribute__((packed, aligned(pointer_size))) |
| 1140 | // |
| 1141 | // which is |
| 1142 | // |
| 1143 | // <{ [1 x i32] }> in LLVM |
| 1144 | // |
Tim Murray | d56a92f | 2014-08-05 13:27:12 -0700 | [diff] [blame] | 1145 | std::vector<llvm::Type *> Elements; |
| 1146 | if (getRSContext()->is64Bit()) { |
| 1147 | // 64-bit path |
| 1148 | Elements.push_back(llvm::ArrayType::get(llvm::Type::getInt64Ty(C), 4)); |
| 1149 | return llvm::StructType::get(C, Elements, true); |
| 1150 | } else { |
| 1151 | // 32-bit legacy path |
| 1152 | Elements.push_back(llvm::ArrayType::get(llvm::Type::getInt32Ty(C), 1)); |
| 1153 | return llvm::StructType::get(C, Elements, true); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1154 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1155 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1156 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1157 | switch (mType) { |
Pirama Arumuga Nainar | e4dd17d | 2015-04-07 11:09:14 -0700 | [diff] [blame] | 1158 | case DataTypeFloat16: { |
| 1159 | return llvm::Type::getHalfTy(C); |
| 1160 | break; |
| 1161 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1162 | case DataTypeFloat32: { |
| 1163 | return llvm::Type::getFloatTy(C); |
| 1164 | break; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1165 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1166 | case DataTypeFloat64: { |
| 1167 | return llvm::Type::getDoubleTy(C); |
| 1168 | break; |
| 1169 | } |
Shih-wei Liao | 91a3783 | 2010-10-03 19:11:51 -0700 | [diff] [blame] | 1170 | case DataTypeBoolean: { |
| 1171 | return llvm::Type::getInt1Ty(C); |
| 1172 | break; |
| 1173 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1174 | case DataTypeSigned8: |
| 1175 | case DataTypeUnsigned8: { |
| 1176 | return llvm::Type::getInt8Ty(C); |
| 1177 | break; |
| 1178 | } |
| 1179 | case DataTypeSigned16: |
| 1180 | case DataTypeUnsigned16: |
| 1181 | case DataTypeUnsigned565: |
| 1182 | case DataTypeUnsigned5551: |
| 1183 | case DataTypeUnsigned4444: { |
| 1184 | return llvm::Type::getInt16Ty(C); |
| 1185 | break; |
| 1186 | } |
| 1187 | case DataTypeSigned32: |
| 1188 | case DataTypeUnsigned32: { |
| 1189 | return llvm::Type::getInt32Ty(C); |
| 1190 | break; |
| 1191 | } |
Stephen Hines | a5d2c23 | 2010-10-11 16:10:02 -0700 | [diff] [blame] | 1192 | case DataTypeSigned64: |
| 1193 | case DataTypeUnsigned64: { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1194 | return llvm::Type::getInt64Ty(C); |
| 1195 | break; |
| 1196 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1197 | default: { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1198 | slangAssert(false && "Unknown data type"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1199 | } |
| 1200 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1201 | |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1202 | return nullptr; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 1205 | bool RSExportPrimitiveType::equals(const RSExportable *E) const { |
| 1206 | CHECK_PARENT_EQUALITY(RSExportType, E); |
| 1207 | return (static_cast<const RSExportPrimitiveType*>(E)->getType() == getType()); |
| 1208 | } |
| 1209 | |
Stephen Hines | fdd1ba1 | 2012-03-08 17:26:32 -0800 | [diff] [blame] | 1210 | RSReflectionType *RSExportPrimitiveType::getRSReflectionType(DataType DT) { |
| 1211 | if (DT > DataTypeUnknown && DT < DataTypeMax) { |
| 1212 | return &gReflectionTypes[DT]; |
| 1213 | } else { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1214 | return nullptr; |
Stephen Hines | fdd1ba1 | 2012-03-08 17:26:32 -0800 | [diff] [blame] | 1215 | } |
| 1216 | } |
| 1217 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1218 | /**************************** RSExportPointerType ****************************/ |
| 1219 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1220 | RSExportPointerType |
| 1221 | *RSExportPointerType::Create(RSContext *Context, |
| 1222 | const clang::PointerType *PT, |
| 1223 | const llvm::StringRef &TypeName) { |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 1224 | const clang::Type *PointeeType = GetPointeeType(PT); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1225 | const RSExportType *PointeeET; |
| 1226 | |
| 1227 | if (PointeeType->getTypeClass() != clang::Type::Pointer) { |
| 1228 | PointeeET = RSExportType::Create(Context, PointeeType); |
| 1229 | } else { |
| 1230 | // Double or higher dimension of pointer, export as int* |
Stephen Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 1231 | PointeeET = RSExportPrimitiveType::Create(Context, |
| 1232 | Context->getASTContext().IntTy.getTypePtr()); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1233 | } |
| 1234 | |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1235 | if (PointeeET == nullptr) { |
Stephen Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 1236 | // Error diagnostic is emitted for corresponding pointee type |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1237 | return nullptr; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1238 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1239 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1240 | return new RSExportPointerType(Context, TypeName, PointeeET); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
Shih-wei Liao | 7c67e57 | 2011-07-19 05:54:53 -0700 | [diff] [blame] | 1243 | llvm::Type *RSExportPointerType::convertToLLVMType() const { |
| 1244 | llvm::Type *PointeeType = mPointeeType->getLLVMType(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1245 | return llvm::PointerType::getUnqual(PointeeType); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1246 | } |
| 1247 | |
Zonr Chang | 3cd3dd3 | 2010-10-22 02:11:35 +0800 | [diff] [blame] | 1248 | bool RSExportPointerType::keep() { |
| 1249 | if (!RSExportType::keep()) |
| 1250 | return false; |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 1251 | const_cast<RSExportType*>(mPointeeType)->keep(); |
Zonr Chang | 3cd3dd3 | 2010-10-22 02:11:35 +0800 | [diff] [blame] | 1252 | return true; |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | bool RSExportPointerType::equals(const RSExportable *E) const { |
| 1256 | CHECK_PARENT_EQUALITY(RSExportType, E); |
| 1257 | return (static_cast<const RSExportPointerType*>(E) |
| 1258 | ->getPointeeType()->equals(getPointeeType())); |
| 1259 | } |
| 1260 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 1261 | /***************************** RSExportVectorType *****************************/ |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 1262 | llvm::StringRef |
| 1263 | RSExportVectorType::GetTypeName(const clang::ExtVectorType *EVT) { |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 1264 | const clang::Type *ElementType = GetExtVectorElementType(EVT); |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 1265 | llvm::StringRef name; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1266 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1267 | if ((ElementType->getTypeClass() != clang::Type::Builtin)) |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 1268 | return name; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1269 | |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 1270 | const clang::BuiltinType *BT = |
| 1271 | static_cast<const clang::BuiltinType*>( |
| 1272 | ElementType->getCanonicalTypeInternal().getTypePtr()); |
| 1273 | |
Zonr Chang | a65ec16 | 2010-10-17 01:53:05 +0800 | [diff] [blame] | 1274 | if ((EVT->getNumElements() < 1) || |
| 1275 | (EVT->getNumElements() > 4)) |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 1276 | return name; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1277 | |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 1278 | BuiltinInfo *info = FindBuiltinType(BT->getKind()); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1279 | if (info != nullptr) { |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 1280 | int I = EVT->getNumElements() - 1; |
| 1281 | if (I < kMaxVectorSize) { |
| 1282 | name = info->cname[I]; |
| 1283 | } else { |
| 1284 | slangAssert(false && "Max vector is 4"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1285 | } |
| 1286 | } |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 1287 | return name; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1290 | RSExportVectorType *RSExportVectorType::Create(RSContext *Context, |
| 1291 | const clang::ExtVectorType *EVT, |
| 1292 | const llvm::StringRef &TypeName, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1293 | bool Normalized) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1294 | slangAssert(EVT != nullptr && EVT->getTypeClass() == clang::Type::ExtVector); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1295 | |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 1296 | const clang::Type *ElementType = GetExtVectorElementType(EVT); |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 1297 | DataType DT = RSExportPrimitiveType::GetDataType(Context, ElementType); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1298 | |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 1299 | if (DT != DataTypeUnknown) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1300 | return new RSExportVectorType(Context, |
| 1301 | TypeName, |
| 1302 | DT, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1303 | Normalized, |
| 1304 | EVT->getNumElements()); |
| 1305 | else |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1306 | return nullptr; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
Shih-wei Liao | 7c67e57 | 2011-07-19 05:54:53 -0700 | [diff] [blame] | 1309 | llvm::Type *RSExportVectorType::convertToLLVMType() const { |
| 1310 | llvm::Type *ElementType = RSExportPrimitiveType::convertToLLVMType(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1311 | return llvm::VectorType::get(ElementType, getNumElement()); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 1314 | bool RSExportVectorType::equals(const RSExportable *E) const { |
| 1315 | CHECK_PARENT_EQUALITY(RSExportPrimitiveType, E); |
| 1316 | return (static_cast<const RSExportVectorType*>(E)->getNumElement() |
| 1317 | == getNumElement()); |
| 1318 | } |
| 1319 | |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1320 | /***************************** RSExportMatrixType *****************************/ |
| 1321 | RSExportMatrixType *RSExportMatrixType::Create(RSContext *Context, |
| 1322 | const clang::RecordType *RT, |
| 1323 | const llvm::StringRef &TypeName, |
| 1324 | unsigned Dim) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1325 | slangAssert((RT != nullptr) && (RT->getTypeClass() == clang::Type::Record)); |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1326 | slangAssert((Dim > 1) && "Invalid dimension of matrix"); |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1327 | |
| 1328 | // Check whether the struct rs_matrix is in our expected form (but assume it's |
| 1329 | // correct if we're not sure whether it's correct or not) |
| 1330 | const clang::RecordDecl* RD = RT->getDecl(); |
| 1331 | RD = RD->getDefinition(); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1332 | if (RD != nullptr) { |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1333 | // Find definition, perform further examination |
| 1334 | if (RD->field_empty()) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 1335 | Context->ReportError( |
| 1336 | RD->getLocation(), |
| 1337 | "invalid matrix struct: must have 1 field for saving values: '%0'") |
| 1338 | << RD->getName(); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1339 | return nullptr; |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | clang::RecordDecl::field_iterator FIT = RD->field_begin(); |
| 1343 | const clang::FieldDecl *FD = *FIT; |
| 1344 | const clang::Type *FT = RSExportType::GetTypeOfDecl(FD); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1345 | if ((FT == nullptr) || (FT->getTypeClass() != clang::Type::ConstantArray)) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 1346 | Context->ReportError(RD->getLocation(), |
| 1347 | "invalid matrix struct: first field should" |
| 1348 | " be an array with constant size: '%0'") |
| 1349 | << RD->getName(); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1350 | return nullptr; |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1351 | } |
| 1352 | const clang::ConstantArrayType *CAT = |
| 1353 | static_cast<const clang::ConstantArrayType *>(FT); |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 1354 | const clang::Type *ElementType = GetConstantArrayElementType(CAT); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1355 | if ((ElementType == nullptr) || |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1356 | (ElementType->getTypeClass() != clang::Type::Builtin) || |
Logan Chien | 9207a2e | 2011-10-21 15:39:28 +0800 | [diff] [blame] | 1357 | (static_cast<const clang::BuiltinType *>(ElementType)->getKind() != |
| 1358 | clang::BuiltinType::Float)) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 1359 | Context->ReportError(RD->getLocation(), |
| 1360 | "invalid matrix struct: first field " |
| 1361 | "should be a float array: '%0'") |
| 1362 | << RD->getName(); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1363 | return nullptr; |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | if (CAT->getSize() != Dim * Dim) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 1367 | Context->ReportError(RD->getLocation(), |
| 1368 | "invalid matrix struct: first field " |
| 1369 | "should be an array with size %0: '%1'") |
| 1370 | << (Dim * Dim) << (RD->getName()); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1371 | return nullptr; |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | FIT++; |
| 1375 | if (FIT != RD->field_end()) { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 1376 | Context->ReportError(RD->getLocation(), |
| 1377 | "invalid matrix struct: must have " |
| 1378 | "exactly 1 field: '%0'") |
| 1379 | << RD->getName(); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1380 | return nullptr; |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | return new RSExportMatrixType(Context, TypeName, Dim); |
| 1385 | } |
| 1386 | |
Shih-wei Liao | 7c67e57 | 2011-07-19 05:54:53 -0700 | [diff] [blame] | 1387 | llvm::Type *RSExportMatrixType::convertToLLVMType() const { |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1388 | // Construct LLVM type: |
| 1389 | // struct { |
| 1390 | // float X[mDim * mDim]; |
| 1391 | // } |
| 1392 | |
| 1393 | llvm::LLVMContext &C = getRSContext()->getLLVMContext(); |
| 1394 | llvm::ArrayType *X = llvm::ArrayType::get(llvm::Type::getFloatTy(C), |
| 1395 | mDim * mDim); |
Stephen Hines | a67e445 | 2011-07-19 15:39:26 -0700 | [diff] [blame] | 1396 | return llvm::StructType::get(C, X, false); |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 1397 | } |
| 1398 | |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 1399 | bool RSExportMatrixType::equals(const RSExportable *E) const { |
| 1400 | CHECK_PARENT_EQUALITY(RSExportType, E); |
| 1401 | return (static_cast<const RSExportMatrixType*>(E)->getDim() == getDim()); |
| 1402 | } |
| 1403 | |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1404 | /************************* RSExportConstantArrayType *************************/ |
| 1405 | RSExportConstantArrayType |
| 1406 | *RSExportConstantArrayType::Create(RSContext *Context, |
| 1407 | const clang::ConstantArrayType *CAT) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1408 | slangAssert(CAT != nullptr && CAT->getTypeClass() == clang::Type::ConstantArray); |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1409 | |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1410 | slangAssert((CAT->getSize().getActiveBits() < 32) && "array too large"); |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1411 | |
| 1412 | unsigned Size = static_cast<unsigned>(CAT->getSize().getZExtValue()); |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1413 | slangAssert((Size > 0) && "Constant array should have size greater than 0"); |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1414 | |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 1415 | const clang::Type *ElementType = GetConstantArrayElementType(CAT); |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1416 | RSExportType *ElementET = RSExportType::Create(Context, ElementType); |
| 1417 | |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1418 | if (ElementET == nullptr) { |
| 1419 | return nullptr; |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | return new RSExportConstantArrayType(Context, |
| 1423 | ElementET, |
| 1424 | Size); |
| 1425 | } |
| 1426 | |
Shih-wei Liao | 7c67e57 | 2011-07-19 05:54:53 -0700 | [diff] [blame] | 1427 | llvm::Type *RSExportConstantArrayType::convertToLLVMType() const { |
Zonr Chang | 2e1dba6 | 2010-10-05 22:20:11 +0800 | [diff] [blame] | 1428 | return llvm::ArrayType::get(mElementType->getLLVMType(), getSize()); |
| 1429 | } |
| 1430 | |
Zonr Chang | 3cd3dd3 | 2010-10-22 02:11:35 +0800 | [diff] [blame] | 1431 | bool RSExportConstantArrayType::keep() { |
| 1432 | if (!RSExportType::keep()) |
| 1433 | return false; |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 1434 | const_cast<RSExportType*>(mElementType)->keep(); |
Zonr Chang | 3cd3dd3 | 2010-10-22 02:11:35 +0800 | [diff] [blame] | 1435 | return true; |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | bool RSExportConstantArrayType::equals(const RSExportable *E) const { |
| 1439 | CHECK_PARENT_EQUALITY(RSExportType, E); |
Stephen Hines | a7b7518 | 2010-12-07 15:49:49 -0800 | [diff] [blame] | 1440 | const RSExportConstantArrayType *RHS = |
| 1441 | static_cast<const RSExportConstantArrayType*>(E); |
| 1442 | return ((getSize() == RHS->getSize()) && |
| 1443 | (getElementType()->equals(RHS->getElementType()))); |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 1444 | } |
| 1445 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1446 | /**************************** RSExportRecordType ****************************/ |
| 1447 | RSExportRecordType *RSExportRecordType::Create(RSContext *Context, |
| 1448 | const clang::RecordType *RT, |
| 1449 | const llvm::StringRef &TypeName, |
| 1450 | bool mIsArtificial) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1451 | slangAssert(RT != nullptr && RT->getTypeClass() == clang::Type::Record); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1452 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1453 | const clang::RecordDecl *RD = RT->getDecl(); |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1454 | slangAssert(RD->isStruct()); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1455 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1456 | RD = RD->getDefinition(); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1457 | if (RD == nullptr) { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 1458 | slangAssert(false && "struct is not defined in this module"); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1459 | return nullptr; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1460 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1461 | |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 1462 | // Struct layout construct by clang. We rely on this for obtaining the |
| 1463 | // alloc size of a struct and offset of every field in that struct. |
| 1464 | const clang::ASTRecordLayout *RL = |
Stephen Hines | 9e5b503 | 2010-11-03 13:19:14 -0700 | [diff] [blame] | 1465 | &Context->getASTContext().getASTRecordLayout(RD); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1466 | slangAssert((RL != nullptr) && |
Stephen Hines | f2174cf | 2011-02-09 23:21:37 -0800 | [diff] [blame] | 1467 | "Failed to retrieve the struct layout from Clang."); |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 1468 | |
| 1469 | RSExportRecordType *ERT = |
| 1470 | new RSExportRecordType(Context, |
| 1471 | TypeName, |
| 1472 | RD->hasAttr<clang::PackedAttr>(), |
| 1473 | mIsArtificial, |
Jean-Luc Brouillet | c95381a | 2014-05-14 21:24:45 -0700 | [diff] [blame] | 1474 | RL->getDataSize().getQuantity(), |
Shih-wei Liao | 68318a1 | 2011-03-11 05:56:15 -0800 | [diff] [blame] | 1475 | RL->getSize().getQuantity()); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1476 | unsigned int Index = 0; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1477 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1478 | for (clang::RecordDecl::field_iterator FI = RD->field_begin(), |
| 1479 | FE = RD->field_end(); |
| 1480 | FI != FE; |
Shih-wei Liao | 91a3783 | 2010-10-03 19:11:51 -0700 | [diff] [blame] | 1481 | FI++, Index++) { |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1482 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1483 | // FIXME: All fields should be primitive type |
Shih-wei Liao | 43730fe | 2012-08-02 23:06:18 -0700 | [diff] [blame] | 1484 | slangAssert(FI->getKind() == clang::Decl::Field); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1485 | clang::FieldDecl *FD = *FI; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1486 | |
Stephen Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 1487 | if (FD->isBitField()) { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1488 | return nullptr; |
Stephen Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 1489 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1490 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1491 | // Type |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 1492 | RSExportType *ET = RSExportElement::CreateFromDecl(Context, FD); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1493 | |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1494 | if (ET != nullptr) { |
Zonr Chang | 0da0a7d | 2010-10-05 21:26:37 +0800 | [diff] [blame] | 1495 | ERT->mFields.push_back( |
| 1496 | new Field(ET, FD->getName(), ERT, |
| 1497 | static_cast<size_t>(RL->getFieldOffset(Index) >> 3))); |
Stephen Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 1498 | } else { |
Jean-Luc Brouillet | d3f7527 | 2014-01-16 18:20:28 -0800 | [diff] [blame] | 1499 | Context->ReportError(RD->getLocation(), |
| 1500 | "field type cannot be exported: '%0.%1'") |
| 1501 | << RD->getName() << FD->getName(); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1502 | return nullptr; |
Stephen Hines | 2ef9bc0 | 2010-12-13 18:33:23 -0800 | [diff] [blame] | 1503 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1504 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1505 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1506 | return ERT; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1507 | } |
| 1508 | |
Shih-wei Liao | 7c67e57 | 2011-07-19 05:54:53 -0700 | [diff] [blame] | 1509 | llvm::Type *RSExportRecordType::convertToLLVMType() const { |
Zonr Chang | 3cd3dd3 | 2010-10-22 02:11:35 +0800 | [diff] [blame] | 1510 | // Create an opaque type since struct may reference itself recursively. |
Zonr Chang | 3cd3dd3 | 2010-10-22 02:11:35 +0800 | [diff] [blame] | 1511 | |
Shih-wei Liao | 7c67e57 | 2011-07-19 05:54:53 -0700 | [diff] [blame] | 1512 | // TODO(sliao): LLVM took out the OpaqueType. Any other to migrate to? |
| 1513 | std::vector<llvm::Type*> FieldTypes; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1514 | |
Zonr Chang | 3cd3dd3 | 2010-10-22 02:11:35 +0800 | [diff] [blame] | 1515 | for (const_field_iterator FI = fields_begin(), FE = fields_end(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1516 | FI != FE; |
| 1517 | FI++) { |
| 1518 | const Field *F = *FI; |
| 1519 | const RSExportType *FET = F->getType(); |
Shih-wei Liao | 1f0d88f | 2010-06-25 00:15:24 -0700 | [diff] [blame] | 1520 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 1521 | FieldTypes.push_back(FET->getLLVMType()); |
| 1522 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1523 | |
Zonr Chang | 3cd3dd3 | 2010-10-22 02:11:35 +0800 | [diff] [blame] | 1524 | llvm::StructType *ST = llvm::StructType::get(getRSContext()->getLLVMContext(), |
| 1525 | FieldTypes, |
| 1526 | mIsPacked); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1527 | if (ST != nullptr) { |
Shih-wei Liao | 7c67e57 | 2011-07-19 05:54:53 -0700 | [diff] [blame] | 1528 | return ST; |
| 1529 | } else { |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 1530 | return nullptr; |
Shih-wei Liao | 7c67e57 | 2011-07-19 05:54:53 -0700 | [diff] [blame] | 1531 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1532 | } |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 1533 | |
Zonr Chang | 3cd3dd3 | 2010-10-22 02:11:35 +0800 | [diff] [blame] | 1534 | bool RSExportRecordType::keep() { |
| 1535 | if (!RSExportType::keep()) |
| 1536 | return false; |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 1537 | for (std::list<const Field*>::iterator I = mFields.begin(), |
| 1538 | E = mFields.end(); |
| 1539 | I != E; |
| 1540 | I++) { |
| 1541 | const_cast<RSExportType*>((*I)->getType())->keep(); |
| 1542 | } |
Zonr Chang | 3cd3dd3 | 2010-10-22 02:11:35 +0800 | [diff] [blame] | 1543 | return true; |
Zonr Chang | 641558f | 2010-10-12 21:07:06 +0800 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | bool RSExportRecordType::equals(const RSExportable *E) const { |
| 1547 | CHECK_PARENT_EQUALITY(RSExportType, E); |
| 1548 | |
| 1549 | const RSExportRecordType *ERT = static_cast<const RSExportRecordType*>(E); |
| 1550 | |
| 1551 | if (ERT->getFields().size() != getFields().size()) |
| 1552 | return false; |
| 1553 | |
| 1554 | const_field_iterator AI = fields_begin(), BI = ERT->fields_begin(); |
| 1555 | |
| 1556 | for (unsigned i = 0, e = getFields().size(); i != e; i++) { |
| 1557 | if (!(*AI)->getType()->equals((*BI)->getType())) |
| 1558 | return false; |
| 1559 | AI++; |
| 1560 | BI++; |
| 1561 | } |
| 1562 | |
| 1563 | return true; |
| 1564 | } |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 1565 | |
Jason Sams | 1b6a088 | 2012-03-12 15:07:58 -0700 | [diff] [blame] | 1566 | void RSExportType::convertToRTD(RSReflectionTypeData *rtd) const { |
| 1567 | memset(rtd, 0, sizeof(*rtd)); |
| 1568 | rtd->vecSize = 1; |
| 1569 | |
| 1570 | switch(getClass()) { |
| 1571 | case RSExportType::ExportClassPrimitive: { |
| 1572 | const RSExportPrimitiveType *EPT = static_cast<const RSExportPrimitiveType*>(this); |
| 1573 | rtd->type = RSExportPrimitiveType::getRSReflectionType(EPT); |
| 1574 | return; |
| 1575 | } |
| 1576 | case RSExportType::ExportClassPointer: { |
| 1577 | const RSExportPointerType *EPT = static_cast<const RSExportPointerType*>(this); |
| 1578 | const RSExportType *PointeeType = EPT->getPointeeType(); |
| 1579 | PointeeType->convertToRTD(rtd); |
| 1580 | rtd->isPointer = true; |
| 1581 | return; |
| 1582 | } |
| 1583 | case RSExportType::ExportClassVector: { |
| 1584 | const RSExportVectorType *EVT = static_cast<const RSExportVectorType*>(this); |
| 1585 | rtd->type = EVT->getRSReflectionType(EVT); |
| 1586 | rtd->vecSize = EVT->getNumElement(); |
| 1587 | return; |
| 1588 | } |
| 1589 | case RSExportType::ExportClassMatrix: { |
| 1590 | const RSExportMatrixType *EMT = static_cast<const RSExportMatrixType*>(this); |
| 1591 | unsigned Dim = EMT->getDim(); |
| 1592 | slangAssert((Dim >= 2) && (Dim <= 4)); |
| 1593 | rtd->type = &gReflectionTypes[15 + Dim-2]; |
| 1594 | return; |
| 1595 | } |
| 1596 | case RSExportType::ExportClassConstantArray: { |
| 1597 | const RSExportConstantArrayType* CAT = |
| 1598 | static_cast<const RSExportConstantArrayType*>(this); |
| 1599 | CAT->getElementType()->convertToRTD(rtd); |
| 1600 | rtd->arraySize = CAT->getSize(); |
| 1601 | return; |
| 1602 | } |
| 1603 | case RSExportType::ExportClassRecord: { |
| 1604 | slangAssert(!"RSExportType::ExportClassRecord not implemented"); |
| 1605 | return;// RS_TYPE_CLASS_NAME_PREFIX + ET->getName() + ".Item"; |
| 1606 | } |
| 1607 | default: { |
| 1608 | slangAssert(false && "Unknown class of type"); |
| 1609 | } |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 1614 | } // namespace slang |