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