slang_rs_export_type.cpp revision 44f10063c2c08dab103a44cded0c3a288d65d43b
1/* 2 * Copyright 2010-2012, The Android Open Source Project 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 17#include "slang_rs_export_type.h" 18 19#include <list> 20#include <vector> 21 22#include "clang/AST/ASTContext.h" 23#include "clang/AST/Attr.h" 24#include "clang/AST/RecordLayout.h" 25 26#include "llvm/ADT/StringExtras.h" 27#include "llvm/IR/DataLayout.h" 28#include "llvm/IR/DerivedTypes.h" 29#include "llvm/IR/Type.h" 30 31#include "slang_assert.h" 32#include "slang_rs_context.h" 33#include "slang_rs_export_element.h" 34#include "slang_rs_type_spec.h" 35#include "slang_version.h" 36 37#define CHECK_PARENT_EQUALITY(ParentClass, E) \ 38 if (!ParentClass::equals(E)) \ 39 return false; 40 41namespace slang { 42 43namespace { 44 45static 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}, 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}, 57 58 {"BOOLEAN", "BOOLEAN", 8, "bool", "boolean", NULL, NULL, false}, 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 82static const clang::Type *TypeExportableHelper( 83 const clang::Type *T, 84 llvm::SmallPtrSet<const clang::Type*, 8>& SPS, 85 clang::DiagnosticsEngine *DiagEngine, 86 const clang::VarDecl *VD, 87 const clang::RecordDecl *TopLevelRecord); 88 89static void ReportTypeError(clang::DiagnosticsEngine *DiagEngine, 90 const clang::NamedDecl *ND, 91 const clang::RecordDecl *TopLevelRecord, 92 const char *Message, 93 unsigned int TargetAPI = 0) { 94 if (!DiagEngine) { 95 return; 96 } 97 98 const clang::SourceManager &SM = DiagEngine->getSourceManager(); 99 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) { 104 DiagEngine->Report( 105 clang::FullSourceLoc(TopLevelRecord->getLocation(), SM), 106 DiagEngine->getCustomDiagID(clang::DiagnosticsEngine::Error, Message)) 107 << TopLevelRecord->getName() << TargetAPI; 108 } else if (ND) { 109 DiagEngine->Report( 110 clang::FullSourceLoc(ND->getLocation(), SM), 111 DiagEngine->getCustomDiagID(clang::DiagnosticsEngine::Error, Message)) 112 << ND->getName() << TargetAPI; 113 } else { 114 slangAssert(false && "Variables should be validated before exporting"); 115 } 116} 117 118static const clang::Type *ConstantArrayTypeExportableHelper( 119 const clang::ConstantArrayType *CAT, 120 llvm::SmallPtrSet<const clang::Type*, 8>& SPS, 121 clang::DiagnosticsEngine *DiagEngine, 122 const clang::VarDecl *VD, 123 const clang::RecordDecl *TopLevelRecord) { 124 // Check element type 125 const clang::Type *ElementType = GET_CONSTANT_ARRAY_ELEMENT_TYPE(CAT); 126 if (ElementType->isArrayType()) { 127 ReportTypeError(DiagEngine, VD, TopLevelRecord, 128 "multidimensional arrays cannot be exported: '%0'"); 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)) { 137 ReportTypeError(DiagEngine, VD, TopLevelRecord, 138 "vectors of non-primitive types cannot be exported: '%0'"); 139 return NULL; 140 } 141 142 if (numElements == 3 && CAT->getSize() != 1) { 143 ReportTypeError(DiagEngine, VD, TopLevelRecord, 144 "arrays of width 3 vector types cannot be exported: '%0'"); 145 return NULL; 146 } 147 } 148 149 if (TypeExportableHelper(ElementType, SPS, DiagEngine, VD, 150 TopLevelRecord) == NULL) { 151 return NULL; 152 } else { 153 return CAT; 154 } 155} 156 157static const clang::Type *TypeExportableHelper( 158 clang::Type const *T, 159 llvm::SmallPtrSet<clang::Type const *, 8> &SPS, 160 clang::DiagnosticsEngine *DiagEngine, 161 clang::VarDecl const *VD, 162 clang::RecordDecl const *TopLevelRecord) { 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: { 172 const clang::BuiltinType *BT = 173 UNSAFE_CAST_TYPE(const clang::BuiltinType, T); 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) != 187 RSExportPrimitiveType::DataTypeUnknown) { 188 return T; // RS object type, no further checks are needed 189 } 190 191 // Check internal struct 192 if (T->isUnionType()) { 193 ReportTypeError(DiagEngine, VD, T->getAsUnionType()->getDecl(), 194 "unions cannot be exported: '%0'"); 195 return NULL; 196 } else if (!T->isStructureType()) { 197 slangAssert(false && "Unknown type cannot be exported"); 198 return NULL; 199 } 200 201 clang::RecordDecl *RD = T->getAsStructureType()->getDecl(); 202 if (RD != NULL) { 203 RD = RD->getDefinition(); 204 if (RD == NULL) { 205 ReportTypeError(DiagEngine, NULL, T->getAsStructureType()->getDecl(), 206 "struct is not defined in this module"); 207 return NULL; 208 } 209 } 210 211 if (!TopLevelRecord) { 212 TopLevelRecord = RD; 213 } 214 if (RD->getName().empty()) { 215 ReportTypeError(DiagEngine, NULL, RD, 216 "anonymous structures cannot be exported"); 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 236 if (!TypeExportableHelper(FT, SPS, DiagEngine, VD, TopLevelRecord)) { 237 return NULL; 238 } 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()) { 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(); 253 } 254 return NULL; 255 } 256 } 257 258 return T; 259 } 260 case clang::Type::Pointer: { 261 if (TopLevelRecord) { 262 ReportTypeError(DiagEngine, VD, TopLevelRecord, 263 "structures containing pointers cannot be exported: '%0'"); 264 return NULL; 265 } 266 267 const clang::PointerType *PT = 268 UNSAFE_CAST_TYPE(const clang::PointerType, T); 269 const clang::Type *PointeeType = GET_POINTEE_TYPE(PT); 270 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 } 276 // We don't support pointer with array-type pointee or unsupported pointee 277 // type 278 if (PointeeType->isArrayType() || 279 (TypeExportableHelper(PointeeType, SPS, DiagEngine, VD, 280 TopLevelRecord) == NULL)) 281 return NULL; 282 else 283 return T; 284 } 285 case clang::Type::ExtVector: { 286 const clang::ExtVectorType *EVT = 287 UNSAFE_CAST_TYPE(const clang::ExtVectorType, T); 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) || 296 (TypeExportableHelper(ElementType, SPS, DiagEngine, VD, 297 TopLevelRecord) == NULL)) 298 return NULL; 299 else 300 return T; 301 } 302 case clang::Type::ConstantArray: { 303 const clang::ConstantArrayType *CAT = 304 UNSAFE_CAST_TYPE(const clang::ConstantArrayType, T); 305 306 return ConstantArrayTypeExportableHelper(CAT, SPS, DiagEngine, VD, 307 TopLevelRecord); 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 317// If the Type T is not exportable, this function returns NULL. DiagEngine is 318// used to generate proper Clang diagnostic messages when a 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). 322static const clang::Type *TypeExportable(const clang::Type *T, 323 clang::DiagnosticsEngine *DiagEngine, 324 const clang::VarDecl *VD) { 325 llvm::SmallPtrSet<const clang::Type*, 8> SPS = 326 llvm::SmallPtrSet<const clang::Type*, 8>(); 327 328 return TypeExportableHelper(T, SPS, DiagEngine, VD, NULL); 329} 330 331static 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). 338 if (VD->hasLinkage() && 339 (VD->getFormalLinkage() == clang::ExternalLinkage)) { 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 357// Helper function for ValidateType(). We do a recursive descent on the 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// 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. 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. 370// IsFilterscript - whether or not we are compiling for Filterscript 371static bool ValidateTypeHelper( 372 clang::ASTContext &C, 373 const clang::Type *&T, 374 clang::NamedDecl *ND, 375 clang::SourceLocation Loc, 376 llvm::SmallPtrSet<const clang::Type*, 8>& SPS, 377 bool InCompositeType, 378 clang::RecordDecl *UnionDecl, 379 unsigned int TargetAPI, 380 bool IsFilterscript) { 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: { 389 if (RSExportPrimitiveType::IsRSObjectType(T)) { 390 clang::VarDecl *VD = (ND ? llvm::dyn_cast<clang::VarDecl>(ND) : NULL); 391 if (VD && !ValidateRSObjectInVarDecl(VD, InCompositeType, TargetAPI)) { 392 return false; 393 } 394 } 395 396 if (RSExportPrimitiveType::GetRSSpecificType(T) != 397 RSExportPrimitiveType::DataTypeUnknown) { 398 if (!UnionDecl) { 399 return true; 400 } else if (RSExportPrimitiveType::IsRSObjectType(T)) { 401 ReportTypeError(&C.getDiagnostics(), NULL, UnionDecl, 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 444 if (!ValidateTypeHelper(C, FT, ND, Loc, SPS, true, UnionDecl, 445 TargetAPI, IsFilterscript)) { 446 return false; 447 } 448 } 449 450 return true; 451 } 452 453 case clang::Type::Builtin: { 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 } 479 break; 480 } 481 482 case clang::Type::Pointer: { 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 500 const clang::PointerType *PT = 501 UNSAFE_CAST_TYPE(const clang::PointerType, T); 502 const clang::Type *PointeeType = GET_POINTEE_TYPE(PT); 503 504 return ValidateTypeHelper(C, PointeeType, ND, Loc, SPS, InCompositeType, 505 UnionDecl, TargetAPI, IsFilterscript); 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); 512 if (TargetAPI < SLANG_ICS_TARGET_API && 513 InCompositeType && 514 EVT->getNumElements() == 3 && 515 ND && 516 ND->getFormalLinkage() == clang::ExternalLinkage) { 517 ReportTypeError(&C.getDiagnostics(), ND, NULL, 518 "structs containing vectors of dimension 3 cannot " 519 "be exported at this API level: '%0'"); 520 return false; 521 } 522 return ValidateTypeHelper(C, ElementType, ND, Loc, SPS, true, UnionDecl, 523 TargetAPI, IsFilterscript); 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); 530 return ValidateTypeHelper(C, ElementType, ND, Loc, SPS, true, UnionDecl, 531 TargetAPI, IsFilterscript); 532 } 533 534 default: { 535 break; 536 } 537 } 538 539 return true; 540} 541 542} // namespace 543 544/****************************** RSExportType ******************************/ 545bool RSExportType::NormalizeType(const clang::Type *&T, 546 llvm::StringRef &TypeName, 547 clang::DiagnosticsEngine *DiagEngine, 548 const clang::VarDecl *VD) { 549 if ((T = TypeExportable(T, DiagEngine, VD)) == NULL) { 550 return false; 551 } 552 // Get type name 553 TypeName = RSExportType::GetTypeName(T); 554 if (TypeName.empty()) { 555 if (DiagEngine) { 556 if (VD) { 557 DiagEngine->Report( 558 clang::FullSourceLoc(VD->getLocation(), 559 DiagEngine->getSourceManager()), 560 DiagEngine->getCustomDiagID(clang::DiagnosticsEngine::Error, 561 "anonymous types cannot be exported")); 562 } else { 563 DiagEngine->Report( 564 DiagEngine->getCustomDiagID(clang::DiagnosticsEngine::Error, 565 "anonymous types cannot be exported")); 566 } 567 } 568 return false; 569 } 570 571 return true; 572} 573 574bool 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(); 578 llvm::SmallPtrSet<const clang::Type*, 8> SPS = 579 llvm::SmallPtrSet<const clang::Type*, 8>(); 580 581 return ValidateTypeHelper(C, T, ND, Loc, SPS, false, NULL, TargetAPI, 582 IsFilterscript); 583 return true; 584} 585 586bool RSExportType::ValidateVarDecl(clang::VarDecl *VD, unsigned int TargetAPI, 587 bool IsFilterscript) { 588 return ValidateType(VD->getASTContext(), VD->getType(), VD, 589 VD->getLocation(), TargetAPI, IsFilterscript); 590} 591 592const clang::Type 593*RSExportType::GetTypeOfDecl(const clang::DeclaratorDecl *DD) { 594 if (DD) { 595 clang::QualType T; 596 if (DD->getTypeSourceInfo()) 597 T = DD->getTypeSourceInfo()->getType(); 598 else 599 T = DD->getType(); 600 601 if (T.isNull()) 602 return NULL; 603 else 604 return T.getTypePtr(); 605 } 606 return NULL; 607} 608 609llvm::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: { 616 const clang::BuiltinType *BT = 617 UNSAFE_CAST_TYPE(const clang::BuiltinType, T); 618 619 switch (BT->getKind()) { 620#define ENUM_SUPPORT_BUILTIN_TYPE(builtin_type, type, cname) \ 621 case builtin_type: \ 622 return cname; \ 623 break; 624#include "RSClangBuiltinEnums.inc" 625 default: { 626 slangAssert(false && "Unknown data type of the builtin"); 627 break; 628 } 629 } 630 break; 631 } 632 case clang::Type::Record: { 633 clang::RecordDecl *RD; 634 if (T->isStructureType()) { 635 RD = T->getAsStructureType()->getDecl(); 636 } else { 637 break; 638 } 639 640 llvm::StringRef Name = RD->getName(); 641 if (Name.empty()) { 642 if (RD->getTypedefNameForAnonDecl() != NULL) { 643 Name = RD->getTypedefNameForAnonDecl()->getName(); 644 } 645 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"); 653 654 Name = (*RI)->getName(); 655 if (!Name.empty()) 656 break; 657 } 658 } 659 } 660 return Name; 661 } 662 case clang::Type::Pointer: { 663 // "*" plus pointee name 664 const clang::Type *PT = GET_POINTEE_TYPE(T); 665 llvm::StringRef PointeeName; 666 if (NormalizeType(PT, PointeeName, NULL, NULL)) { 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 } 675 case clang::Type::ExtVector: { 676 const clang::ExtVectorType *EVT = 677 UNSAFE_CAST_TYPE(const clang::ExtVectorType, T); 678 return RSExportVectorType::GetTypeName(EVT); 679 break; 680 } 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 } 685 default: { 686 break; 687 } 688 } 689 690 return llvm::StringRef(); 691} 692 693 694RSExportType *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 = 709 RSExportPrimitiveType::GetRSSpecificType(TypeName); 710 switch (dt) { 711 case RSExportPrimitiveType::DataTypeUnknown: { 712 // User-defined types 713 ET = RSExportRecordType::Create(Context, 714 T->getAsStructureType(), 715 TypeName); 716 break; 717 } 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 } 734 case RSExportPrimitiveType::DataTypeRSMatrix4x4: { 735 // 4 x 4 Matrix type 736 ET = RSExportMatrixType::Create(Context, 737 T->getAsStructureType(), 738 TypeName, 739 4); 740 break; 741 } 742 default: { 743 // Others are primitive types 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, 756 UNSAFE_CAST_TYPE(const clang::PointerType, T), TypeName); 757 // FIXME: free the name (allocated in RSExportType::GetTypeName) 758 delete [] TypeName.data(); 759 break; 760 } 761 case clang::Type::ExtVector: { 762 ET = RSExportVectorType::Create(Context, 763 UNSAFE_CAST_TYPE(const clang::ExtVectorType, T), TypeName); 764 break; 765 } 766 case clang::Type::ConstantArray: { 767 ET = RSExportConstantArrayType::Create( 768 Context, 769 UNSAFE_CAST_TYPE(const clang::ConstantArrayType, T)); 770 break; 771 } 772 default: { 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(); 778 break; 779 } 780 } 781 782 return ET; 783} 784 785RSExportType *RSExportType::Create(RSContext *Context, const clang::Type *T) { 786 llvm::StringRef TypeName; 787 if (NormalizeType(T, TypeName, Context->getDiagnostics(), NULL)) { 788 return Create(Context, T, TypeName); 789 } else { 790 return NULL; 791 } 792} 793 794RSExportType *RSExportType::CreateFromDecl(RSContext *Context, 795 const clang::VarDecl *VD) { 796 return RSExportType::Create(Context, GetTypeOfDecl(VD)); 797} 798 799size_t RSExportType::GetTypeStoreSize(const RSExportType *ET) { 800 return ET->getRSContext()->getDataLayout()->getTypeStoreSize( 801 ET->getLLVMType()); 802} 803 804size_t RSExportType::GetTypeAllocSize(const RSExportType *ET) { 805 if (ET->getClass() == RSExportType::ExportClassRecord) 806 return static_cast<const RSExportRecordType*>(ET)->getAllocSize(); 807 else 808 return ET->getRSContext()->getDataLayout()->getTypeAllocSize( 809 ET->getLLVMType()); 810} 811 812RSExportType::RSExportType(RSContext *Context, 813 ExportClass Class, 814 const llvm::StringRef &Name) 815 : RSExportable(Context, RSExportable::EX_TYPE), 816 mClass(Class), 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. 819 mName(Name.data(), Name.size()), 820 mLLVMType(NULL), 821 mSpecType(NULL) { 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); 828 return; 829} 830 831bool RSExportType::keep() { 832 if (!RSExportable::keep()) 833 return false; 834 // Invalidate converted LLVM type. 835 mLLVMType = NULL; 836 return true; 837} 838 839bool RSExportType::equals(const RSExportable *E) const { 840 CHECK_PARENT_EQUALITY(RSExportable, E); 841 return (static_cast<const RSExportType*>(E)->getClass() == getClass()); 842} 843 844RSExportType::~RSExportType() { 845 delete mSpecType; 846} 847 848/************************** RSExportPrimitiveType **************************/ 849llvm::ManagedStatic<RSExportPrimitiveType::RSSpecificTypeMapTy> 850RSExportPrimitiveType::RSSpecificTypeMap; 851 852llvm::Type *RSExportPrimitiveType::RSObjectLLVMType = NULL; 853 854bool RSExportPrimitiveType::IsPrimitiveType(const clang::Type *T) { 855 if ((T != NULL) && (T->getTypeClass() == clang::Type::Builtin)) 856 return true; 857 else 858 return false; 859} 860 861RSExportPrimitiveType::DataType 862RSExportPrimitiveType::GetRSSpecificType(const llvm::StringRef &TypeName) { 863 if (TypeName.empty()) 864 return DataTypeUnknown; 865 866 if (RSSpecificTypeMap->empty()) { 867#define ENUM_RS_MATRIX_TYPE(type, cname, dim) \ 868 RSSpecificTypeMap->GetOrCreateValue(cname, DataType ## type); 869#include "RSMatrixTypeEnums.inc" 870#define ENUM_RS_OBJECT_TYPE(type, cname) \ 871 RSSpecificTypeMap->GetOrCreateValue(cname, DataType ## type); 872#include "RSObjectTypeEnums.inc" 873 } 874 875 RSSpecificTypeMapTy::const_iterator I = RSSpecificTypeMap->find(TypeName); 876 if (I == RSSpecificTypeMap->end()) 877 return DataTypeUnknown; 878 else 879 return I->getValue(); 880} 881 882RSExportPrimitiveType::DataType 883RSExportPrimitiveType::GetRSSpecificType(const clang::Type *T) { 884 T = GET_CANONICAL_TYPE(T); 885 if ((T == NULL) || (T->getTypeClass() != clang::Type::Record)) 886 return DataTypeUnknown; 887 888 return GetRSSpecificType( RSExportType::GetTypeName(T) ); 889} 890 891bool RSExportPrimitiveType::IsRSMatrixType(DataType DT) { 892 return ((DT >= FirstRSMatrixType) && (DT <= LastRSMatrixType)); 893} 894 895bool RSExportPrimitiveType::IsRSObjectType(DataType DT) { 896 return ((DT >= FirstRSObjectType) && (DT <= LastRSObjectType)); 897} 898 899bool 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 } 909 910 const clang::RecordDecl *RD = RT->getDecl(); 911 if (RD) { 912 RD = RD->getDefinition(); 913 } 914 if (!RD) { 915 return false; 916 } 917 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 960const size_t RSExportPrimitiveType::SizeOfDataTypeInBits[] = { 961#define ENUM_RS_DATA_TYPE(type, cname, bits) \ 962 bits, 963#include "RSDataTypeEnums.inc" 964 0 // DataTypeMax 965}; 966 967size_t RSExportPrimitiveType::GetSizeInBits(const RSExportPrimitiveType *EPT) { 968 slangAssert(((EPT->getType() > DataTypeUnknown) && 969 (EPT->getType() < DataTypeMax)) && 970 "RSExportPrimitiveType::GetSizeInBits : unknown data type"); 971 return SizeOfDataTypeInBits[ static_cast<int>(EPT->getType()) ]; 972} 973 974RSExportPrimitiveType::DataType 975RSExportPrimitiveType::GetDataType(RSContext *Context, const clang::Type *T) { 976 if (T == NULL) 977 return DataTypeUnknown; 978 979 switch (T->getTypeClass()) { 980 case clang::Type::Builtin: { 981 const clang::BuiltinType *BT = 982 UNSAFE_CAST_TYPE(const clang::BuiltinType, T); 983 switch (BT->getKind()) { 984#define ENUM_SUPPORT_BUILTIN_TYPE(builtin_type, type, cname) \ 985 case builtin_type: { \ 986 return DataType ## type; \ 987 } 988#include "RSClangBuiltinEnums.inc" 989 // The size of type WChar depend on platform so we abandon the support 990 // to them. 991 default: { 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(); 998 break; 999 } 1000 } 1001 break; 1002 } 1003 case clang::Type::Record: { 1004 // must be RS object type 1005 return RSExportPrimitiveType::GetRSSpecificType(T); 1006 } 1007 default: { 1008 clang::DiagnosticsEngine *DiagEngine = Context->getDiagnostics(); 1009 DiagEngine->Report( 1010 DiagEngine->getCustomDiagID(clang::DiagnosticsEngine::Error, 1011 "primitive type cannot be exported: '%0'")) 1012 << T->getTypeClassName(); 1013 break; 1014 } 1015 } 1016 1017 return DataTypeUnknown; 1018} 1019 1020RSExportPrimitiveType 1021*RSExportPrimitiveType::Create(RSContext *Context, 1022 const clang::Type *T, 1023 const llvm::StringRef &TypeName, 1024 bool Normalized) { 1025 DataType DT = GetDataType(Context, T); 1026 1027 if ((DT == DataTypeUnknown) || TypeName.empty()) 1028 return NULL; 1029 else 1030 return new RSExportPrimitiveType(Context, ExportClassPrimitive, TypeName, 1031 DT, Normalized); 1032} 1033 1034RSExportPrimitiveType *RSExportPrimitiveType::Create(RSContext *Context, 1035 const clang::Type *T) { 1036 llvm::StringRef TypeName; 1037 if (RSExportType::NormalizeType(T, TypeName, Context->getDiagnostics(), NULL) 1038 && IsPrimitiveType(T)) { 1039 return Create(Context, T, TypeName); 1040 } else { 1041 return NULL; 1042 } 1043} 1044 1045llvm::Type *RSExportPrimitiveType::convertToLLVMType() const { 1046 llvm::LLVMContext &C = getRSContext()->getLLVMContext(); 1047 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) { 1058 std::vector<llvm::Type *> Elements; 1059 Elements.push_back(llvm::ArrayType::get(llvm::Type::getInt32Ty(C), 1)); 1060 RSObjectLLVMType = llvm::StructType::get(C, Elements, true); 1061 } 1062 return RSObjectLLVMType; 1063 } 1064 1065 switch (mType) { 1066 case DataTypeFloat32: { 1067 return llvm::Type::getFloatTy(C); 1068 break; 1069 } 1070 case DataTypeFloat64: { 1071 return llvm::Type::getDoubleTy(C); 1072 break; 1073 } 1074 case DataTypeBoolean: { 1075 return llvm::Type::getInt1Ty(C); 1076 break; 1077 } 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 } 1096 case DataTypeSigned64: 1097 case DataTypeUnsigned64: { 1098 return llvm::Type::getInt64Ty(C); 1099 break; 1100 } 1101 default: { 1102 slangAssert(false && "Unknown data type"); 1103 } 1104 } 1105 1106 return NULL; 1107} 1108 1109union 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 1118bool RSExportPrimitiveType::equals(const RSExportable *E) const { 1119 CHECK_PARENT_EQUALITY(RSExportType, E); 1120 return (static_cast<const RSExportPrimitiveType*>(E)->getType() == getType()); 1121} 1122 1123RSReflectionType *RSExportPrimitiveType::getRSReflectionType(DataType DT) { 1124 if (DT > DataTypeUnknown && DT < DataTypeMax) { 1125 return &gReflectionTypes[DT]; 1126 } else { 1127 return NULL; 1128 } 1129} 1130 1131/**************************** RSExportPointerType ****************************/ 1132 1133RSExportPointerType 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* 1144 PointeeET = RSExportPrimitiveType::Create(Context, 1145 Context->getASTContext().IntTy.getTypePtr()); 1146 } 1147 1148 if (PointeeET == NULL) { 1149 // Error diagnostic is emitted for corresponding pointee type 1150 return NULL; 1151 } 1152 1153 return new RSExportPointerType(Context, TypeName, PointeeET); 1154} 1155 1156llvm::Type *RSExportPointerType::convertToLLVMType() const { 1157 llvm::Type *PointeeType = mPointeeType->getLLVMType(); 1158 return llvm::PointerType::getUnqual(PointeeType); 1159} 1160 1161union 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 1173bool RSExportPointerType::keep() { 1174 if (!RSExportType::keep()) 1175 return false; 1176 const_cast<RSExportType*>(mPointeeType)->keep(); 1177 return true; 1178} 1179 1180bool 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 1186/***************************** RSExportVectorType *****************************/ 1187llvm::StringRef 1188RSExportVectorType::GetTypeName(const clang::ExtVectorType *EVT) { 1189 const clang::Type *ElementType = GET_EXT_VECTOR_ELEMENT_TYPE(EVT); 1190 1191 if ((ElementType->getTypeClass() != clang::Type::Builtin)) 1192 return llvm::StringRef(); 1193 1194 const clang::BuiltinType *BT = UNSAFE_CAST_TYPE(const clang::BuiltinType, 1195 ElementType); 1196 if ((EVT->getNumElements() < 1) || 1197 (EVT->getNumElements() > 4)) 1198 return llvm::StringRef(); 1199 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 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; \ 1208 } 1209#include "RSClangBuiltinEnums.inc" 1210 default: { 1211 return llvm::StringRef(); 1212 } 1213 } 1214} 1215 1216RSExportVectorType *RSExportVectorType::Create(RSContext *Context, 1217 const clang::ExtVectorType *EVT, 1218 const llvm::StringRef &TypeName, 1219 bool Normalized) { 1220 slangAssert(EVT != NULL && EVT->getTypeClass() == clang::Type::ExtVector); 1221 1222 const clang::Type *ElementType = GET_EXT_VECTOR_ELEMENT_TYPE(EVT); 1223 RSExportPrimitiveType::DataType DT = 1224 RSExportPrimitiveType::GetDataType(Context, ElementType); 1225 1226 if (DT != RSExportPrimitiveType::DataTypeUnknown) 1227 return new RSExportVectorType(Context, 1228 TypeName, 1229 DT, 1230 Normalized, 1231 EVT->getNumElements()); 1232 else 1233 return NULL; 1234} 1235 1236llvm::Type *RSExportVectorType::convertToLLVMType() const { 1237 llvm::Type *ElementType = RSExportPrimitiveType::convertToLLVMType(); 1238 return llvm::VectorType::get(ElementType, getNumElement()); 1239} 1240 1241union 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 1251bool RSExportVectorType::equals(const RSExportable *E) const { 1252 CHECK_PARENT_EQUALITY(RSExportPrimitiveType, E); 1253 return (static_cast<const RSExportVectorType*>(E)->getNumElement() 1254 == getNumElement()); 1255} 1256 1257/***************************** RSExportMatrixType *****************************/ 1258RSExportMatrixType *RSExportMatrixType::Create(RSContext *Context, 1259 const clang::RecordType *RT, 1260 const llvm::StringRef &TypeName, 1261 unsigned Dim) { 1262 slangAssert((RT != NULL) && (RT->getTypeClass() == clang::Type::Record)); 1263 slangAssert((Dim > 1) && "Invalid dimension of matrix"); 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) { 1270 clang::DiagnosticsEngine *DiagEngine = Context->getDiagnostics(); 1271 const clang::SourceManager *SM = Context->getSourceManager(); 1272 // Find definition, perform further examination 1273 if (RD->field_empty()) { 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'")) 1279 << RD->getName(); 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)) { 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(); 1293 return NULL; 1294 } 1295 const clang::ConstantArrayType *CAT = 1296 static_cast<const clang::ConstantArrayType *>(FT); 1297 const clang::Type *ElementType = GET_CONSTANT_ARRAY_ELEMENT_TYPE(CAT); 1298 if ((ElementType == NULL) || 1299 (ElementType->getTypeClass() != clang::Type::Builtin) || 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(); 1308 return NULL; 1309 } 1310 1311 if (CAT->getSize() != Dim * Dim) { 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()); 1318 return NULL; 1319 } 1320 1321 FIT++; 1322 if (FIT != RD->field_end()) { 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(); 1329 return NULL; 1330 } 1331 } 1332 1333 return new RSExportMatrixType(Context, TypeName, Dim); 1334} 1335 1336llvm::Type *RSExportMatrixType::convertToLLVMType() const { 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); 1345 return llvm::StructType::get(C, X, false); 1346} 1347 1348union 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; 1355 default: slangAssert(false && "Matrix type with unsupported dimension."); 1356 } 1357 return ST.take(); 1358} 1359 1360bool RSExportMatrixType::equals(const RSExportable *E) const { 1361 CHECK_PARENT_EQUALITY(RSExportType, E); 1362 return (static_cast<const RSExportMatrixType*>(E)->getDim() == getDim()); 1363} 1364 1365/************************* RSExportConstantArrayType *************************/ 1366RSExportConstantArrayType 1367*RSExportConstantArrayType::Create(RSContext *Context, 1368 const clang::ConstantArrayType *CAT) { 1369 slangAssert(CAT != NULL && CAT->getTypeClass() == clang::Type::ConstantArray); 1370 1371 slangAssert((CAT->getSize().getActiveBits() < 32) && "array too large"); 1372 1373 unsigned Size = static_cast<unsigned>(CAT->getSize().getZExtValue()); 1374 slangAssert((Size > 0) && "Constant array should have size greater than 0"); 1375 1376 const clang::Type *ElementType = GET_CONSTANT_ARRAY_ELEMENT_TYPE(CAT); 1377 RSExportType *ElementET = RSExportType::Create(Context, ElementType); 1378 1379 if (ElementET == NULL) { 1380 return NULL; 1381 } 1382 1383 return new RSExportConstantArrayType(Context, 1384 ElementET, 1385 Size); 1386} 1387 1388llvm::Type *RSExportConstantArrayType::convertToLLVMType() const { 1389 return llvm::ArrayType::get(mElementType->getLLVMType(), getSize()); 1390} 1391 1392union 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 1406bool RSExportConstantArrayType::keep() { 1407 if (!RSExportType::keep()) 1408 return false; 1409 const_cast<RSExportType*>(mElementType)->keep(); 1410 return true; 1411} 1412 1413bool RSExportConstantArrayType::equals(const RSExportable *E) const { 1414 CHECK_PARENT_EQUALITY(RSExportType, E); 1415 const RSExportConstantArrayType *RHS = 1416 static_cast<const RSExportConstantArrayType*>(E); 1417 return ((getSize() == RHS->getSize()) && 1418 (getElementType()->equals(RHS->getElementType()))); 1419} 1420 1421/**************************** RSExportRecordType ****************************/ 1422RSExportRecordType *RSExportRecordType::Create(RSContext *Context, 1423 const clang::RecordType *RT, 1424 const llvm::StringRef &TypeName, 1425 bool mIsArtificial) { 1426 slangAssert(RT != NULL && RT->getTypeClass() == clang::Type::Record); 1427 1428 const clang::RecordDecl *RD = RT->getDecl(); 1429 slangAssert(RD->isStruct()); 1430 1431 RD = RD->getDefinition(); 1432 if (RD == NULL) { 1433 slangAssert(false && "struct is not defined in this module"); 1434 return NULL; 1435 } 1436 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 = 1440 &Context->getASTContext().getASTRecordLayout(RD); 1441 slangAssert((RL != NULL) && 1442 "Failed to retrieve the struct layout from Clang."); 1443 1444 RSExportRecordType *ERT = 1445 new RSExportRecordType(Context, 1446 TypeName, 1447 RD->hasAttr<clang::PackedAttr>(), 1448 mIsArtificial, 1449 RL->getSize().getQuantity()); 1450 unsigned int Index = 0; 1451 1452 for (clang::RecordDecl::field_iterator FI = RD->field_begin(), 1453 FE = RD->field_end(); 1454 FI != FE; 1455 FI++, Index++) { 1456 clang::DiagnosticsEngine *DiagEngine = Context->getDiagnostics(); 1457 1458 // FIXME: All fields should be primitive type 1459 slangAssert(FI->getKind() == clang::Decl::Field); 1460 clang::FieldDecl *FD = *FI; 1461 1462 if (FD->isBitField()) { 1463 return NULL; 1464 } 1465 1466 // Type 1467 RSExportType *ET = RSExportElement::CreateFromDecl(Context, FD); 1468 1469 if (ET != NULL) { 1470 ERT->mFields.push_back( 1471 new Field(ET, FD->getName(), ERT, 1472 static_cast<size_t>(RL->getFieldOffset(Index) >> 3))); 1473 } else { 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(); 1479 return NULL; 1480 } 1481 } 1482 1483 return ERT; 1484} 1485 1486llvm::Type *RSExportRecordType::convertToLLVMType() const { 1487 // Create an opaque type since struct may reference itself recursively. 1488 1489 // TODO(sliao): LLVM took out the OpaqueType. Any other to migrate to? 1490 std::vector<llvm::Type*> FieldTypes; 1491 1492 for (const_field_iterator FI = fields_begin(), FE = fields_end(); 1493 FI != FE; 1494 FI++) { 1495 const Field *F = *FI; 1496 const RSExportType *FET = F->getType(); 1497 1498 FieldTypes.push_back(FET->getLLVMType()); 1499 } 1500 1501 llvm::StructType *ST = llvm::StructType::get(getRSContext()->getLLVMContext(), 1502 FieldTypes, 1503 mIsPacked); 1504 if (ST != NULL) { 1505 return ST; 1506 } else { 1507 return NULL; 1508 } 1509} 1510 1511union 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( 1516 reinterpret_cast<union RSType*>(operator new(AllocSize))); 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()); 1534 } 1535 1536 // TODO(slang): Check whether all fields were created normally. 1537 1538 return ST.take(); 1539} 1540 1541bool RSExportRecordType::keep() { 1542 if (!RSExportType::keep()) 1543 return false; 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 } 1550 return true; 1551} 1552 1553bool 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} 1572 1573void 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 1621} // namespace slang 1622