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