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