slang_rs_export_type.h revision 1f6c331d622ac645ab68a016aa4c577998547373
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 64typedef struct { 65 const char * rs_type; 66 const char * rs_short_type; 67 uint32_t size_in_bits; 68 const char * c_name; 69 const char * java_name; 70 const char * rs_c_vector_prefix; 71 const char * rs_java_vector_prefix; 72 bool java_promotion; 73} RSReflectionType; 74 75 76typedef struct RSReflectionTypeData_rec { 77 const RSReflectionType *type; 78 uint32_t vecSize; 79 bool isPointer; 80 uint32_t arraySize; 81 82 // Subelements 83 //std::vector<const struct RSReflectionTypeData_rec *> fields; 84 //std::vector< std::string > fieldNames; 85 //std::vector< uint32_t> fieldOffsetBytes; 86} RSReflectionTypeData; 87 88 89class RSExportType : public RSExportable { 90 friend class RSExportElement; 91 public: 92 typedef enum { 93 ExportClassPrimitive, 94 ExportClassPointer, 95 ExportClassVector, 96 ExportClassMatrix, 97 ExportClassConstantArray, 98 ExportClassRecord 99 } ExportClass; 100 101 void convertToRTD(RSReflectionTypeData *rtd) const; 102 103 private: 104 ExportClass mClass; 105 std::string mName; 106 107 // Cache the result after calling convertToLLVMType() at the first time 108 mutable llvm::Type *mLLVMType; 109 // Cache the result after calling convertToSpecType() at the first time 110 mutable union RSType *mSpecType; 111 112 protected: 113 RSExportType(RSContext *Context, 114 ExportClass Class, 115 const llvm::StringRef &Name); 116 117 // Let's make it private since there're some prerequisites to call this 118 // function. 119 // 120 // @T was normalized by calling RSExportType::NormalizeType(). 121 // @TypeName was retrieve from RSExportType::GetTypeName() before calling 122 // this. 123 // 124 static RSExportType *Create(RSContext *Context, 125 const clang::Type *T, 126 const llvm::StringRef &TypeName); 127 128 static llvm::StringRef GetTypeName(const clang::Type *T); 129 130 // This function convert the RSExportType to LLVM type. Actually, it should be 131 // "convert Clang type to LLVM type." However, clang doesn't make this API 132 // (lib/CodeGen/CodeGenTypes.h) public, we need to do by ourselves. 133 // 134 // Once we can get LLVM type, we can use LLVM to get alignment information, 135 // allocation size of a given type and structure layout that LLVM used 136 // (all of these information are target dependent) without dealing with these 137 // by ourselves. 138 virtual llvm::Type *convertToLLVMType() const = 0; 139 // Record type may recursively reference its type definition. We need a 140 // temporary type setup before the type construction gets done. 141 inline void setAbstractLLVMType(llvm::Type *LLVMType) const { 142 mLLVMType = LLVMType; 143 } 144 145 virtual union RSType *convertToSpecType() const = 0; 146 inline void setSpecTypeTemporarily(union RSType *SpecType) const { 147 mSpecType = SpecType; 148 } 149 150 virtual ~RSExportType(); 151 152 public: 153 // This function additionally verifies that the Type T is exportable. 154 // If it is not, this function returns false. Otherwise it returns true. 155 static bool NormalizeType(const clang::Type *&T, 156 llvm::StringRef &TypeName, 157 clang::DiagnosticsEngine *Diags, 158 const clang::VarDecl *VD); 159 160 // This function ensures that the VarDecl can be properly handled by RS. 161 // If it cannot, this function returns false. Otherwise it returns true. 162 static bool ValidateVarDecl(clang::VarDecl *VD, unsigned int TargetAPI); 163 164 // @T may not be normalized 165 static RSExportType *Create(RSContext *Context, const clang::Type *T); 166 static RSExportType *CreateFromDecl(RSContext *Context, 167 const clang::VarDecl *VD); 168 169 static const clang::Type *GetTypeOfDecl(const clang::DeclaratorDecl *DD); 170 171 inline ExportClass getClass() const { return mClass; } 172 173 virtual unsigned getSize() const { return 1; } 174 175 inline llvm::Type *getLLVMType() const { 176 if (mLLVMType == NULL) 177 mLLVMType = convertToLLVMType(); 178 return mLLVMType; 179 } 180 181 inline const union RSType *getSpecType() const { 182 if (mSpecType == NULL) 183 mSpecType = convertToSpecType(); 184 return mSpecType; 185 } 186 187 // Return the number of bits necessary to hold the specified RSExportType 188 static size_t GetTypeStoreSize(const RSExportType *ET); 189 190 // The size of allocation of specified RSExportType (alignment considered) 191 static size_t GetTypeAllocSize(const RSExportType *ET); 192 193 inline const std::string &getName() const { return mName; } 194 195 virtual std::string getElementName() const { 196 // Base case is actually an invalid C/Java identifier. 197 return "@@INVALID@@"; 198 } 199 200 virtual bool keep(); 201 virtual bool equals(const RSExportable *E) const; 202}; // RSExportType 203 204// Primitive types 205class RSExportPrimitiveType : public RSExportType { 206 friend class RSExportType; 207 friend class RSExportElement; 208 public: 209 // From graphics/java/android/renderscript/Element.java: Element.DataType 210 typedef enum { 211 DataTypeIsStruct = -2, 212 DataTypeUnknown = -1, 213 214#define ENUM_PRIMITIVE_DATA_TYPE_RANGE(begin_type, end_type) \ 215 FirstPrimitiveType = DataType ## begin_type, \ 216 LastPrimitiveType = DataType ## end_type, 217 218#define ENUM_RS_MATRIX_DATA_TYPE_RANGE(begin_type, end_type) \ 219 FirstRSMatrixType = DataType ## begin_type, \ 220 LastRSMatrixType = DataType ## end_type, 221 222#define ENUM_RS_OBJECT_DATA_TYPE_RANGE(begin_type, end_type) \ 223 FirstRSObjectType = DataType ## begin_type, \ 224 LastRSObjectType = DataType ## end_type, 225 226#define ENUM_RS_DATA_TYPE(type, cname, bits) \ 227 DataType ## type, 228 229#include "RSDataTypeEnums.inc" 230 231 DataTypeMax 232 } DataType; 233 234 private: 235 // NOTE: There's no any instance of RSExportPrimitiveType which mType 236 // is of the value DataTypeRSMatrix*. DataTypeRSMatrix* enumeration here is 237 // only for RSExportPrimitiveType::GetRSObjectType to *recognize* the struct 238 // rs_matrix{2x2, 3x3, 4x4}. These matrix type are represented as 239 // RSExportMatrixType. 240 DataType mType; 241 bool mNormalized; 242 243 typedef llvm::StringMap<DataType> RSSpecificTypeMapTy; 244 static llvm::ManagedStatic<RSSpecificTypeMapTy> RSSpecificTypeMap; 245 246 static llvm::Type *RSObjectLLVMType; 247 248 static const size_t SizeOfDataTypeInBits[]; 249 // @T was normalized by calling RSExportType::NormalizeType() before calling 250 // this. 251 // @TypeName was retrieved from RSExportType::GetTypeName() before calling 252 // this 253 static RSExportPrimitiveType *Create(RSContext *Context, 254 const clang::Type *T, 255 const llvm::StringRef &TypeName, 256 bool Normalized = false); 257 258 protected: 259 RSExportPrimitiveType(RSContext *Context, 260 // for derived class to set their type class 261 ExportClass Class, 262 const llvm::StringRef &Name, 263 DataType DT, 264 bool Normalized) 265 : RSExportType(Context, Class, Name), 266 mType(DT), 267 mNormalized(Normalized) { 268 return; 269 } 270 271 virtual llvm::Type *convertToLLVMType() const; 272 virtual union RSType *convertToSpecType() const; 273 274 static DataType GetDataType(RSContext *Context, const clang::Type *T); 275 276 public: 277 // T is normalized by calling RSExportType::NormalizeType() before 278 // calling this 279 static bool IsPrimitiveType(const clang::Type *T); 280 281 // @T may not be normalized 282 static RSExportPrimitiveType *Create(RSContext *Context, 283 const clang::Type *T); 284 285 static DataType GetRSSpecificType(const llvm::StringRef &TypeName); 286 static DataType GetRSSpecificType(const clang::Type *T); 287 288 static bool IsRSMatrixType(DataType DT); 289 static bool IsRSObjectType(DataType DT); 290 static bool IsRSObjectType(const clang::Type *T) { 291 return IsRSObjectType(GetRSSpecificType(T)); 292 } 293 294 // Determines whether T is [an array of] struct that contains at least one 295 // RS object type within it. 296 static bool IsStructureTypeWithRSObject(const clang::Type *T); 297 298 static size_t GetSizeInBits(const RSExportPrimitiveType *EPT); 299 300 inline DataType getType() const { return mType; } 301 inline bool isRSObjectType() const { 302 return ((mType >= FirstRSObjectType) && (mType <= LastRSObjectType)); 303 } 304 305 virtual bool equals(const RSExportable *E) const; 306 307 static RSReflectionType *getRSReflectionType(DataType DT); 308 static RSReflectionType *getRSReflectionType( 309 const RSExportPrimitiveType *EPT) { 310 return getRSReflectionType(EPT->getType()); 311 } 312 313 virtual unsigned getSize() const { return (GetSizeInBits(this) >> 3); } 314 315 std::string getElementName() const { 316 return getRSReflectionType(this)->rs_short_type; 317 } 318}; // RSExportPrimitiveType 319 320 321class RSExportPointerType : public RSExportType { 322 friend class RSExportType; 323 friend class RSExportFunc; 324 private: 325 const RSExportType *mPointeeType; 326 327 RSExportPointerType(RSContext *Context, 328 const llvm::StringRef &Name, 329 const RSExportType *PointeeType) 330 : RSExportType(Context, ExportClassPointer, Name), 331 mPointeeType(PointeeType) { 332 return; 333 } 334 335 // @PT was normalized by calling RSExportType::NormalizeType() before calling 336 // this. 337 static RSExportPointerType *Create(RSContext *Context, 338 const clang::PointerType *PT, 339 const llvm::StringRef &TypeName); 340 341 virtual llvm::Type *convertToLLVMType() const; 342 virtual union RSType *convertToSpecType() const; 343 344 public: 345 virtual bool keep(); 346 347 inline const RSExportType *getPointeeType() const { return mPointeeType; } 348 349 virtual bool equals(const RSExportable *E) const; 350}; // RSExportPointerType 351 352 353class RSExportVectorType : public RSExportPrimitiveType { 354 friend class RSExportType; 355 friend class RSExportElement; 356 private: 357 unsigned mNumElement; // number of element 358 359 RSExportVectorType(RSContext *Context, 360 const llvm::StringRef &Name, 361 DataType DT, 362 bool Normalized, 363 unsigned NumElement) 364 : RSExportPrimitiveType(Context, ExportClassVector, Name, 365 DT, Normalized), 366 mNumElement(NumElement) { 367 return; 368 } 369 370 // @EVT was normalized by calling RSExportType::NormalizeType() before 371 // calling this. 372 static RSExportVectorType *Create(RSContext *Context, 373 const clang::ExtVectorType *EVT, 374 const llvm::StringRef &TypeName, 375 bool Normalized = false); 376 377 virtual llvm::Type *convertToLLVMType() const; 378 virtual union RSType *convertToSpecType() const; 379 380 public: 381 static llvm::StringRef GetTypeName(const clang::ExtVectorType *EVT); 382 383 inline unsigned getNumElement() const { return mNumElement; } 384 385 std::string getElementName() const { 386 std::stringstream Name; 387 Name << RSExportPrimitiveType::getRSReflectionType(this)->rs_short_type 388 << "_" << getNumElement(); 389 return Name.str(); 390 } 391 392 virtual bool equals(const RSExportable *E) const; 393}; 394 395// Only *square* *float* matrix is supported by now. 396// 397// struct rs_matrix{2x2,3x3,4x4, ..., NxN} should be defined as the following 398// form *exactly*: 399// typedef struct { 400// float m[{NxN}]; 401// } rs_matrixNxN; 402// 403// where mDim will be N. 404class RSExportMatrixType : public RSExportType { 405 friend class RSExportType; 406 private: 407 unsigned mDim; // dimension 408 409 RSExportMatrixType(RSContext *Context, 410 const llvm::StringRef &Name, 411 unsigned Dim) 412 : RSExportType(Context, ExportClassMatrix, Name), 413 mDim(Dim) { 414 return; 415 } 416 417 virtual llvm::Type *convertToLLVMType() const; 418 virtual union RSType *convertToSpecType() const; 419 420 public: 421 // @RT was normalized by calling RSExportType::NormalizeType() before 422 // calling this. 423 static RSExportMatrixType *Create(RSContext *Context, 424 const clang::RecordType *RT, 425 const llvm::StringRef &TypeName, 426 unsigned Dim); 427 428 inline unsigned getDim() const { return mDim; } 429 430 virtual bool equals(const RSExportable *E) const; 431}; 432 433class RSExportConstantArrayType : public RSExportType { 434 friend class RSExportType; 435 private: 436 const RSExportType *mElementType; // Array element type 437 unsigned mSize; // Array size 438 439 RSExportConstantArrayType(RSContext *Context, 440 const RSExportType *ElementType, 441 unsigned Size) 442 : RSExportType(Context, 443 ExportClassConstantArray, 444 DUMMY_TYPE_NAME_FOR_RS_CONSTANT_ARRAY_TYPE), 445 mElementType(ElementType), 446 mSize(Size) { 447 return; 448 } 449 450 // @CAT was normalized by calling RSExportType::NormalizeType() before 451 // calling this. 452 static RSExportConstantArrayType *Create(RSContext *Context, 453 const clang::ConstantArrayType *CAT); 454 455 virtual llvm::Type *convertToLLVMType() const; 456 virtual union RSType *convertToSpecType() const; 457 458 public: 459 virtual unsigned getSize() const { return mSize; } 460 inline const RSExportType *getElementType() const { return mElementType; } 461 462 std::string getElementName() const { 463 return mElementType->getElementName(); 464 } 465 466 virtual bool keep(); 467 virtual bool equals(const RSExportable *E) const; 468}; 469 470class RSExportRecordType : public RSExportType { 471 friend class RSExportType; 472 public: 473 class Field { 474 private: 475 const RSExportType *mType; 476 // Field name 477 std::string mName; 478 // Link to the struct that contain this field 479 const RSExportRecordType *mParent; 480 // Offset in the container 481 size_t mOffset; 482 483 public: 484 Field(const RSExportType *T, 485 const llvm::StringRef &Name, 486 const RSExportRecordType *Parent, 487 size_t Offset) 488 : mType(T), 489 mName(Name.data(), Name.size()), 490 mParent(Parent), 491 mOffset(Offset) { 492 return; 493 } 494 495 inline const RSExportRecordType *getParent() const { return mParent; } 496 inline const RSExportType *getType() const { return mType; } 497 inline const std::string &getName() const { return mName; } 498 inline size_t getOffsetInParent() const { return mOffset; } 499 }; 500 501 typedef std::list<const Field*>::const_iterator const_field_iterator; 502 503 inline const_field_iterator fields_begin() const { 504 return this->mFields.begin(); 505 } 506 inline const_field_iterator fields_end() const { 507 return this->mFields.end(); 508 } 509 510 private: 511 std::list<const Field*> mFields; 512 bool mIsPacked; 513 // Artificial export struct type is not exported by user (and thus it won't 514 // get reflected) 515 bool mIsArtificial; 516 size_t mAllocSize; 517 518 RSExportRecordType(RSContext *Context, 519 const llvm::StringRef &Name, 520 bool IsPacked, 521 bool IsArtificial, 522 size_t AllocSize) 523 : RSExportType(Context, ExportClassRecord, Name), 524 mIsPacked(IsPacked), 525 mIsArtificial(IsArtificial), 526 mAllocSize(AllocSize) { 527 return; 528 } 529 530 // @RT was normalized by calling RSExportType::NormalizeType() before calling 531 // this. 532 // @TypeName was retrieved from RSExportType::GetTypeName() before calling 533 // this. 534 static RSExportRecordType *Create(RSContext *Context, 535 const clang::RecordType *RT, 536 const llvm::StringRef &TypeName, 537 bool mIsArtificial = false); 538 539 virtual llvm::Type *convertToLLVMType() const; 540 virtual union RSType *convertToSpecType() const; 541 542 public: 543 inline const std::list<const Field*>& getFields() const { return mFields; } 544 inline bool isPacked() const { return mIsPacked; } 545 inline bool isArtificial() const { return mIsArtificial; } 546 inline size_t getAllocSize() const { return mAllocSize; } 547 548 virtual std::string getElementName() const { 549 return "ScriptField_" + getName(); 550 } 551 552 virtual bool keep(); 553 virtual bool equals(const RSExportable *E) const; 554 555 ~RSExportRecordType() { 556 for (std::list<const Field*>::iterator I = mFields.begin(), 557 E = mFields.end(); 558 I != E; 559 I++) 560 if (*I != NULL) 561 delete *I; 562 return; 563 } 564}; // RSExportRecordType 565 566} // namespace slang 567 568#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_TYPE_H_ NOLINT 569