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