slang_rs_export_type.h revision 474655a402e70cb329e1bcd4ebbe00bdc5be4206
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#ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_TYPE_H_ // NOLINT 18#define _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_TYPE_H_ 19 20#include <list> 21#include <set> 22#include <string> 23#include <sstream> 24 25#include "clang/AST/Decl.h" 26#include "clang/AST/Type.h" 27 28#include "llvm/ADT/SmallPtrSet.h" 29#include "llvm/ADT/StringMap.h" 30#include "llvm/ADT/StringRef.h" 31 32#include "llvm/Support/ManagedStatic.h" 33 34#include "slang_rs_exportable.h" 35 36#define GET_CANONICAL_TYPE(T) \ 37 (((T) == NULL) ? NULL : (T)->getCanonicalTypeInternal().getTypePtr()) 38#define UNSAFE_CAST_TYPE(TT, T) \ 39 static_cast<TT*>(T->getCanonicalTypeInternal().getTypePtr()) 40#define GET_EXT_VECTOR_ELEMENT_TYPE(T) \ 41 (((T) == NULL) ? NULL : \ 42 GET_CANONICAL_TYPE((T)->getElementType().getTypePtr())) 43#define GET_POINTEE_TYPE(T) \ 44 (((T) == NULL) ? NULL : \ 45 GET_CANONICAL_TYPE((T)->getPointeeType().getTypePtr())) 46#define GET_CONSTANT_ARRAY_ELEMENT_TYPE(T) \ 47 (((T) == NULL) ? NULL : \ 48 GET_CANONICAL_TYPE((T)->getElementType().getTypePtr())) 49#define DUMMY_RS_TYPE_NAME_PREFIX "<" 50#define DUMMY_RS_TYPE_NAME_POSTFIX ">" 51#define DUMMY_TYPE_NAME_FOR_RS_CONSTANT_ARRAY_TYPE \ 52 DUMMY_RS_TYPE_NAME_PREFIX "ConstantArray" DUMMY_RS_TYPE_NAME_POSTFIX 53 54union RSType; 55 56namespace llvm { 57 class Type; 58} // namespace llvm 59 60namespace slang { 61 62 class RSContext; 63 64 // Broad grouping of the data types 65 enum DataTypeCategory { 66 PrimitiveDataType, 67 MatrixDataType, 68 ObjectDataType 69 }; 70 71typedef struct { 72 DataTypeCategory category; 73 const char * rs_type; 74 const char * rs_short_type; 75 uint32_t size_in_bits; 76 const char * c_name; 77 const char * java_name; 78 const char * rs_c_vector_prefix; 79 const char * rs_java_vector_prefix; 80 bool java_promotion; 81} RSReflectionType; 82 83 84typedef struct RSReflectionTypeData_rec { 85 const RSReflectionType *type; 86 uint32_t vecSize; 87 bool isPointer; 88 uint32_t arraySize; 89 90 // Subelements 91 //std::vector<const struct RSReflectionTypeData_rec *> fields; 92 //std::vector< std::string > fieldNames; 93 //std::vector< uint32_t> fieldOffsetBytes; 94} RSReflectionTypeData; 95 96 97class RSExportType : public RSExportable { 98 friend class RSExportElement; 99 public: 100 typedef enum { 101 ExportClassPrimitive, 102 ExportClassPointer, 103 ExportClassVector, 104 ExportClassMatrix, 105 ExportClassConstantArray, 106 ExportClassRecord 107 } ExportClass; 108 109 void convertToRTD(RSReflectionTypeData *rtd) const; 110 111 private: 112 ExportClass mClass; 113 std::string mName; 114 115 // Cache the result after calling convertToLLVMType() at the first time 116 mutable llvm::Type *mLLVMType; 117 // Cache the result after calling convertToSpecType() at the first time 118 mutable union RSType *mSpecType; 119 120 protected: 121 RSExportType(RSContext *Context, 122 ExportClass Class, 123 const llvm::StringRef &Name); 124 125 // Let's make it private since there're some prerequisites to call this 126 // function. 127 // 128 // @T was normalized by calling RSExportType::NormalizeType(). 129 // @TypeName was retrieve from RSExportType::GetTypeName() before calling 130 // this. 131 // 132 static RSExportType *Create(RSContext *Context, 133 const clang::Type *T, 134 const llvm::StringRef &TypeName); 135 136 static llvm::StringRef GetTypeName(const clang::Type *T); 137 138 // This function convert the RSExportType to LLVM type. Actually, it should be 139 // "convert Clang type to LLVM type." However, clang doesn't make this API 140 // (lib/CodeGen/CodeGenTypes.h) public, we need to do by ourselves. 141 // 142 // Once we can get LLVM type, we can use LLVM to get alignment information, 143 // allocation size of a given type and structure layout that LLVM used 144 // (all of these information are target dependent) without dealing with these 145 // by ourselves. 146 virtual llvm::Type *convertToLLVMType() const = 0; 147 // Record type may recursively reference its type definition. We need a 148 // temporary type setup before the type construction gets done. 149 inline void setAbstractLLVMType(llvm::Type *LLVMType) const { 150 mLLVMType = LLVMType; 151 } 152 153 virtual union RSType *convertToSpecType() const = 0; 154 inline void setSpecTypeTemporarily(union RSType *SpecType) const { 155 mSpecType = SpecType; 156 } 157 158 virtual ~RSExportType(); 159 160 public: 161 // This function additionally verifies that the Type T is exportable. 162 // If it is not, this function returns false. Otherwise it returns true. 163 static bool NormalizeType(const clang::Type *&T, 164 llvm::StringRef &TypeName, 165 RSContext *Context, 166 const clang::VarDecl *VD); 167 168 // This function checks whether the specified type can be handled by RS/FS. 169 // If it cannot, this function returns false. Otherwise it returns true. 170 // Filterscript has additional restrictions on supported types. 171 static bool ValidateType(slang::RSContext *Context, clang::ASTContext &C, 172 clang::QualType QT, clang::NamedDecl *ND, 173 clang::SourceLocation Loc, unsigned int TargetAPI, 174 bool IsFilterscript); 175 176 // This function ensures that the VarDecl can be properly handled by RS. 177 // If it cannot, this function returns false. Otherwise it returns true. 178 // Filterscript has additional restrictions on supported types. 179 static bool ValidateVarDecl(slang::RSContext *Context, clang::VarDecl *VD, 180 unsigned int TargetAPI, bool IsFilterscript); 181 182 // @T may not be normalized 183 static RSExportType *Create(RSContext *Context, const clang::Type *T); 184 static RSExportType *CreateFromDecl(RSContext *Context, 185 const clang::VarDecl *VD); 186 187 static const clang::Type *GetTypeOfDecl(const clang::DeclaratorDecl *DD); 188 189 inline ExportClass getClass() const { return mClass; } 190 191 virtual unsigned getSize() const { return 1; } 192 193 inline llvm::Type *getLLVMType() const { 194 if (mLLVMType == NULL) 195 mLLVMType = convertToLLVMType(); 196 return mLLVMType; 197 } 198 199 inline const union RSType *getSpecType() const { 200 if (mSpecType == NULL) 201 mSpecType = convertToSpecType(); 202 return mSpecType; 203 } 204 205 // Return the number of bits necessary to hold the specified RSExportType 206 static size_t GetTypeStoreSize(const RSExportType *ET); 207 208 // The size of allocation of specified RSExportType (alignment considered) 209 static size_t GetTypeAllocSize(const RSExportType *ET); 210 211 inline const std::string &getName() const { return mName; } 212 213 virtual std::string getElementName() const { 214 // Base case is actually an invalid C/Java identifier. 215 return "@@INVALID@@"; 216 } 217 218 virtual bool keep(); 219 virtual bool equals(const RSExportable *E) const; 220}; // RSExportType 221 222// Primitive types 223class RSExportPrimitiveType : public RSExportType { 224 friend class RSExportType; 225 friend class RSExportElement; 226 public: 227 // From graphics/java/android/renderscript/Element.java: Element.DataType 228 // TODO move out of here 229 /* NOTE: The values of the enums are found compiled in the bit code (i.e. as 230 * values, not symbolic. When adding new types, you must add them to the end. 231 * If removing types, you can't re-use the integer value. 232 * 233 * TODO: but if you do this, you won't be able to keep using First* & Last* 234 * for validation. 235 * 236 * IMPORTANT: This enum should correspond one-for-one to the entries found in the 237 * gReflectionsTypes table (except for the two negative numbers). Don't edit one without 238 * the other. 239 */ 240 enum DataType { 241 DataTypeIsStruct = -2, 242 DataTypeUnknown = -1, 243 244 DataTypeFloat16 = 0, 245 DataTypeFloat32 = 1, 246 DataTypeFloat64 = 2, 247 DataTypeSigned8 = 3, 248 DataTypeSigned16 = 4, 249 DataTypeSigned32 = 5, 250 DataTypeSigned64 = 6, 251 DataTypeUnsigned8 = 7, 252 DataTypeUnsigned16 = 8, 253 DataTypeUnsigned32 = 9, 254 DataTypeUnsigned64 = 10, 255 DataTypeBoolean = 11, 256 DataTypeUnsigned565 = 12, 257 DataTypeUnsigned5551 = 13, 258 DataTypeUnsigned4444 = 14, 259 260 DataTypeRSMatrix2x2 = 15, 261 DataTypeRSMatrix3x3 = 16, 262 DataTypeRSMatrix4x4 = 17, 263 264 DataTypeRSElement = 18, 265 DataTypeRSType = 19, 266 DataTypeRSAllocation = 20, 267 DataTypeRSSampler = 21, 268 DataTypeRSScript = 22, 269 DataTypeRSMesh = 23, 270 DataTypeRSPath = 24, 271 DataTypeRSProgramFragment = 25, 272 DataTypeRSProgramVertex = 26, 273 DataTypeRSProgramRaster = 27, 274 DataTypeRSProgramStore = 28, 275 DataTypeRSFont = 29, 276 277 // This should always be last and correspond to the size of the gReflectionTypes table. 278 DataTypeMax 279 }; 280 281 282 private: 283 // NOTE: There's no any instance of RSExportPrimitiveType which mType 284 // is of the value DataTypeRSMatrix*. DataTypeRSMatrix* enumeration here is 285 // only for RSExportPrimitiveType::GetRSObjectType to *recognize* the struct 286 // rs_matrix{2x2, 3x3, 4x4}. These matrix type are represented as 287 // RSExportMatrixType. 288 DataType mType; 289 bool mNormalized; 290 291 typedef llvm::StringMap<DataType> RSSpecificTypeMapTy; 292 static llvm::ManagedStatic<RSSpecificTypeMapTy> RSSpecificTypeMap; 293 294 static llvm::Type *RSObjectLLVMType; 295 296 static const size_t SizeOfDataTypeInBits[]; 297 // @T was normalized by calling RSExportType::NormalizeType() before calling 298 // this. 299 // @TypeName was retrieved from RSExportType::GetTypeName() before calling 300 // this 301 static RSExportPrimitiveType *Create(RSContext *Context, 302 const clang::Type *T, 303 const llvm::StringRef &TypeName, 304 bool Normalized = false); 305 306 protected: 307 RSExportPrimitiveType(RSContext *Context, 308 // for derived class to set their type class 309 ExportClass Class, 310 const llvm::StringRef &Name, 311 DataType DT, 312 bool Normalized) 313 : RSExportType(Context, Class, Name), 314 mType(DT), 315 mNormalized(Normalized) { 316 return; 317 } 318 319 virtual llvm::Type *convertToLLVMType() const; 320 virtual union RSType *convertToSpecType() const; 321 322 static DataType GetDataType(RSContext *Context, const clang::Type *T); 323 324 public: 325 // T is normalized by calling RSExportType::NormalizeType() before 326 // calling this 327 static bool IsPrimitiveType(const clang::Type *T); 328 329 // @T may not be normalized 330 static RSExportPrimitiveType *Create(RSContext *Context, 331 const clang::Type *T); 332 333 static DataType GetRSSpecificType(const llvm::StringRef &TypeName); 334 static DataType GetRSSpecificType(const clang::Type *T); 335 336 static bool IsRSMatrixType(DataType DT); 337 static bool IsRSObjectType(DataType DT); 338 static bool IsRSObjectType(const clang::Type *T) { 339 return IsRSObjectType(GetRSSpecificType(T)); 340 } 341 342 // Determines whether T is [an array of] struct that contains at least one 343 // RS object type within it. 344 static bool IsStructureTypeWithRSObject(const clang::Type *T); 345 346 static size_t GetSizeInBits(const RSExportPrimitiveType *EPT); 347 348 inline DataType getType() const { return mType; } 349 inline bool isRSObjectType() const { 350 return IsRSObjectType(mType); 351 } 352 353 virtual bool equals(const RSExportable *E) const; 354 355 static RSReflectionType *getRSReflectionType(DataType DT); 356 static RSReflectionType *getRSReflectionType( 357 const RSExportPrimitiveType *EPT) { 358 return getRSReflectionType(EPT->getType()); 359 } 360 361 virtual unsigned getSize() const { return (GetSizeInBits(this) >> 3); } 362 363 std::string getElementName() const { 364 return getRSReflectionType(this)->rs_short_type; 365 } 366}; // RSExportPrimitiveType 367 368 369class RSExportPointerType : public RSExportType { 370 friend class RSExportType; 371 friend class RSExportFunc; 372 private: 373 const RSExportType *mPointeeType; 374 375 RSExportPointerType(RSContext *Context, 376 const llvm::StringRef &Name, 377 const RSExportType *PointeeType) 378 : RSExportType(Context, ExportClassPointer, Name), 379 mPointeeType(PointeeType) { 380 return; 381 } 382 383 // @PT was normalized by calling RSExportType::NormalizeType() before calling 384 // this. 385 static RSExportPointerType *Create(RSContext *Context, 386 const clang::PointerType *PT, 387 const llvm::StringRef &TypeName); 388 389 virtual llvm::Type *convertToLLVMType() const; 390 virtual union RSType *convertToSpecType() const; 391 392 public: 393 virtual bool keep(); 394 395 inline const RSExportType *getPointeeType() const { return mPointeeType; } 396 397 virtual bool equals(const RSExportable *E) const; 398}; // RSExportPointerType 399 400 401class RSExportVectorType : public RSExportPrimitiveType { 402 friend class RSExportType; 403 friend class RSExportElement; 404 private: 405 unsigned mNumElement; // number of element 406 407 RSExportVectorType(RSContext *Context, 408 const llvm::StringRef &Name, 409 DataType DT, 410 bool Normalized, 411 unsigned NumElement) 412 : RSExportPrimitiveType(Context, ExportClassVector, Name, 413 DT, Normalized), 414 mNumElement(NumElement) { 415 return; 416 } 417 418 // @EVT was normalized by calling RSExportType::NormalizeType() before 419 // calling this. 420 static RSExportVectorType *Create(RSContext *Context, 421 const clang::ExtVectorType *EVT, 422 const llvm::StringRef &TypeName, 423 bool Normalized = false); 424 425 virtual llvm::Type *convertToLLVMType() const; 426 virtual union RSType *convertToSpecType() const; 427 428 public: 429 static llvm::StringRef GetTypeName(const clang::ExtVectorType *EVT); 430 431 inline unsigned getNumElement() const { return mNumElement; } 432 433 std::string getElementName() const { 434 std::stringstream Name; 435 Name << RSExportPrimitiveType::getRSReflectionType(this)->rs_short_type 436 << "_" << getNumElement(); 437 return Name.str(); 438 } 439 440 virtual bool equals(const RSExportable *E) const; 441}; 442 443// Only *square* *float* matrix is supported by now. 444// 445// struct rs_matrix{2x2,3x3,4x4, ..., NxN} should be defined as the following 446// form *exactly*: 447// typedef struct { 448// float m[{NxN}]; 449// } rs_matrixNxN; 450// 451// where mDim will be N. 452class RSExportMatrixType : public RSExportType { 453 friend class RSExportType; 454 private: 455 unsigned mDim; // dimension 456 457 RSExportMatrixType(RSContext *Context, 458 const llvm::StringRef &Name, 459 unsigned Dim) 460 : RSExportType(Context, ExportClassMatrix, Name), 461 mDim(Dim) { 462 return; 463 } 464 465 virtual llvm::Type *convertToLLVMType() const; 466 virtual union RSType *convertToSpecType() const; 467 468 public: 469 // @RT was normalized by calling RSExportType::NormalizeType() before 470 // calling this. 471 static RSExportMatrixType *Create(RSContext *Context, 472 const clang::RecordType *RT, 473 const llvm::StringRef &TypeName, 474 unsigned Dim); 475 476 inline unsigned getDim() const { return mDim; } 477 478 virtual bool equals(const RSExportable *E) const; 479}; 480 481class RSExportConstantArrayType : public RSExportType { 482 friend class RSExportType; 483 private: 484 const RSExportType *mElementType; // Array element type 485 unsigned mSize; // Array size 486 487 RSExportConstantArrayType(RSContext *Context, 488 const RSExportType *ElementType, 489 unsigned Size) 490 : RSExportType(Context, 491 ExportClassConstantArray, 492 DUMMY_TYPE_NAME_FOR_RS_CONSTANT_ARRAY_TYPE), 493 mElementType(ElementType), 494 mSize(Size) { 495 return; 496 } 497 498 // @CAT was normalized by calling RSExportType::NormalizeType() before 499 // calling this. 500 static RSExportConstantArrayType *Create(RSContext *Context, 501 const clang::ConstantArrayType *CAT); 502 503 virtual llvm::Type *convertToLLVMType() const; 504 virtual union RSType *convertToSpecType() const; 505 506 public: 507 virtual unsigned getSize() const { return mSize; } 508 inline const RSExportType *getElementType() const { return mElementType; } 509 510 std::string getElementName() const { 511 return mElementType->getElementName(); 512 } 513 514 virtual bool keep(); 515 virtual bool equals(const RSExportable *E) const; 516}; 517 518class RSExportRecordType : public RSExportType { 519 friend class RSExportType; 520 public: 521 class Field { 522 private: 523 const RSExportType *mType; 524 // Field name 525 std::string mName; 526 // Link to the struct that contain this field 527 const RSExportRecordType *mParent; 528 // Offset in the container 529 size_t mOffset; 530 531 public: 532 Field(const RSExportType *T, 533 const llvm::StringRef &Name, 534 const RSExportRecordType *Parent, 535 size_t Offset) 536 : mType(T), 537 mName(Name.data(), Name.size()), 538 mParent(Parent), 539 mOffset(Offset) { 540 return; 541 } 542 543 inline const RSExportRecordType *getParent() const { return mParent; } 544 inline const RSExportType *getType() const { return mType; } 545 inline const std::string &getName() const { return mName; } 546 inline size_t getOffsetInParent() const { return mOffset; } 547 }; 548 549 typedef std::list<const Field*>::const_iterator const_field_iterator; 550 551 inline const_field_iterator fields_begin() const { 552 return this->mFields.begin(); 553 } 554 inline const_field_iterator fields_end() const { 555 return this->mFields.end(); 556 } 557 558 private: 559 std::list<const Field*> mFields; 560 bool mIsPacked; 561 // Artificial export struct type is not exported by user (and thus it won't 562 // get reflected) 563 bool mIsArtificial; 564 size_t mAllocSize; 565 566 RSExportRecordType(RSContext *Context, 567 const llvm::StringRef &Name, 568 bool IsPacked, 569 bool IsArtificial, 570 size_t AllocSize) 571 : RSExportType(Context, ExportClassRecord, Name), 572 mIsPacked(IsPacked), 573 mIsArtificial(IsArtificial), 574 mAllocSize(AllocSize) { 575 return; 576 } 577 578 // @RT was normalized by calling RSExportType::NormalizeType() before calling 579 // this. 580 // @TypeName was retrieved from RSExportType::GetTypeName() before calling 581 // this. 582 static RSExportRecordType *Create(RSContext *Context, 583 const clang::RecordType *RT, 584 const llvm::StringRef &TypeName, 585 bool mIsArtificial = false); 586 587 virtual llvm::Type *convertToLLVMType() const; 588 virtual union RSType *convertToSpecType() const; 589 590 public: 591 inline const std::list<const Field*>& getFields() const { return mFields; } 592 inline bool isPacked() const { return mIsPacked; } 593 inline bool isArtificial() const { return mIsArtificial; } 594 inline size_t getAllocSize() const { return mAllocSize; } 595 596 virtual std::string getElementName() const { 597 return "ScriptField_" + getName(); 598 } 599 600 virtual bool keep(); 601 virtual bool equals(const RSExportable *E) const; 602 603 ~RSExportRecordType() { 604 for (std::list<const Field*>::iterator I = mFields.begin(), 605 E = mFields.end(); 606 I != E; 607 I++) 608 if (*I != NULL) 609 delete *I; 610 return; 611 } 612}; // RSExportRecordType 613 614} // namespace slang 615 616#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_TYPE_H_ NOLINT 617