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