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