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