slang_rs_export_type.h revision b1771ef128b10c4d4575634828006bfba20b1d9c
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 _SLANG_COMPILER_RS_EXPORT_TYPE_H 18#define _SLANG_COMPILER_RS_EXPORT_TYPE_H 19 20#include <set> 21#include <list> 22#include <string> 23 24#include "llvm/Support/ManagedStatic.h" 25 26#include "llvm/ADT/StringRef.h" 27#include "llvm/ADT/StringMap.h" 28#include "llvm/ADT/SmallPtrSet.h" 29 30#include "clang/AST/Type.h" 31#include "clang/AST/Decl.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::TypeExportable(). 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 // Return the type that can be used to create RSExportType, will always return 102 // the canonical type 103 static const clang::Type 104 *TypeExportable(const clang::Type *T, 105 // Contain the checked type for recursion 106 llvm::SmallPtrSet<const clang::Type*, 8> &SPS); 107 108 // This function convert the RSExportType to LLVM type. Actually, it should be 109 // "convert Clang type to LLVM type." However, clang doesn't make this API 110 // (lib/CodeGen/CodeGenTypes.h) public, we need to do by ourselves. 111 // 112 // Once we can get LLVM type, we can use LLVM to get alignment information, 113 // allocation size of a given type and structure layout that LLVM used 114 // (all of these information are target dependent) without dealing with these 115 // by ourselves. 116 virtual const llvm::Type *convertToLLVMType() const = 0; 117 // Record type may recursively referece its type definition. We need a 118 // temporary type setup before the type construction gets done. 119 inline void setAbstractLLVMType(const llvm::Type *LLVMType) const { 120 mLLVMType = LLVMType; 121 } 122 123 virtual union RSType *convertToSpecType() const = 0; 124 inline void setSpecTypeTemporarily(union RSType *SpecType) const { 125 mSpecType = SpecType; 126 } 127 128 virtual ~RSExportType(); 129 public: 130 static bool NormalizeType(const clang::Type *&T, llvm::StringRef &TypeName); 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::TypeExportable() 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 // T is normalized by calling RSExportType::TypeExportable() before 229 // calling this 230 static bool IsPrimitiveType(const clang::Type *T); 231 232 static DataType GetDataType(const clang::Type *T); 233 234 RSExportPrimitiveType(RSContext *Context, 235 // for derived class to set their type class 236 ExportClass Class, 237 const llvm::StringRef &Name, 238 DataType DT, 239 DataKind DK, 240 bool Normalized) 241 : RSExportType(Context, Class, Name), 242 mType(DT), 243 mKind(DK), 244 mNormalized(Normalized) { 245 return; 246 } 247 248 virtual const llvm::Type *convertToLLVMType() const; 249 virtual union RSType *convertToSpecType() const; 250 public: 251 // @T may not be normalized 252 static RSExportPrimitiveType *Create(RSContext *Context, 253 const clang::Type *T, 254 DataKind DK = DataKindUser); 255 256 static DataType GetRSSpecificType(const llvm::StringRef &TypeName); 257 static DataType GetRSSpecificType(const clang::Type *T); 258 259 static bool IsRSMatrixType(DataType DT); 260 static bool IsRSObjectType(DataType DT); 261 262 static size_t GetSizeInBits(const RSExportPrimitiveType *EPT); 263 264 inline DataType getType() const { return mType; } 265 inline DataKind getKind() const { return mKind; } 266 inline bool isRSObjectType() const { 267 return ((mType >= DataTypeRSElement) && (mType < DataTypeMax)); 268 } 269 270 virtual bool equals(const RSExportable *E) const; 271}; // RSExportPrimitiveType 272 273 274class RSExportPointerType : public RSExportType { 275 friend class RSExportType; 276 friend class RSExportFunc; 277 private: 278 const RSExportType *mPointeeType; 279 280 RSExportPointerType(RSContext *Context, 281 const llvm::StringRef &Name, 282 const RSExportType *PointeeType) 283 : RSExportType(Context, ExportClassPointer, Name), 284 mPointeeType(PointeeType) { 285 return; 286 } 287 288 // @PT was normalized by calling RSExportType::TypeExportable() before calling 289 // this. 290 static RSExportPointerType *Create(RSContext *Context, 291 const clang::PointerType *PT, 292 const llvm::StringRef &TypeName); 293 294 virtual const llvm::Type *convertToLLVMType() const; 295 virtual union RSType *convertToSpecType() const; 296 public: 297 static const clang::Type *IntegerType; 298 299 virtual bool keep(); 300 301 inline const RSExportType *getPointeeType() const { return mPointeeType; } 302 303 virtual bool equals(const RSExportable *E) const; 304}; // RSExportPointerType 305 306 307class RSExportVectorType : public RSExportPrimitiveType { 308 friend class RSExportType; 309 friend class RSExportElement; 310 private: 311 unsigned mNumElement; // number of element 312 313 RSExportVectorType(RSContext *Context, 314 const llvm::StringRef &Name, 315 DataType DT, 316 DataKind DK, 317 bool Normalized, 318 unsigned NumElement) 319 : RSExportPrimitiveType(Context, ExportClassVector, Name, 320 DT, DK, Normalized), 321 mNumElement(NumElement) { 322 return; 323 } 324 325 // @EVT was normalized by calling RSExportType::TypeExportable() before 326 // calling this. 327 static RSExportVectorType *Create(RSContext *Context, 328 const clang::ExtVectorType *EVT, 329 const llvm::StringRef &TypeName, 330 DataKind DK = DataKindUser, 331 bool Normalized = false); 332 333 virtual const llvm::Type *convertToLLVMType() const; 334 virtual union RSType *convertToSpecType() const; 335 public: 336 static llvm::StringRef GetTypeName(const clang::ExtVectorType *EVT); 337 338 inline unsigned getNumElement() const { return mNumElement; } 339 340 virtual bool equals(const RSExportable *E) const; 341}; 342 343// Only *square* *float* matrix is supported by now. 344// 345// struct rs_matrix{2x2,3x3,4x4, ..., NxN} should be defined as the following 346// form *exactly*: 347// typedef struct { 348// float m[{NxN}]; 349// } rs_matrixNxN; 350// 351// where mDim will be N. 352class RSExportMatrixType : public RSExportType { 353 friend class RSExportType; 354 private: 355 unsigned mDim; // dimension 356 357 RSExportMatrixType(RSContext *Context, 358 const llvm::StringRef &Name, 359 unsigned Dim) 360 : RSExportType(Context, ExportClassMatrix, Name), 361 mDim(Dim) { 362 return; 363 } 364 365 virtual const llvm::Type *convertToLLVMType() const; 366 virtual union RSType *convertToSpecType() const; 367 public: 368 // @RT was normalized by calling RSExportType::TypeExportable() before 369 // calling this. 370 static RSExportMatrixType *Create(RSContext *Context, 371 const clang::RecordType *RT, 372 const llvm::StringRef &TypeName, 373 unsigned Dim); 374 375 inline unsigned getDim() const { return mDim; } 376 377 virtual bool equals(const RSExportable *E) const; 378}; 379 380class RSExportConstantArrayType : public RSExportType { 381 friend class RSExportType; 382 private: 383 const RSExportType *mElementType; // Array element type 384 unsigned mSize; // Array size 385 386 RSExportConstantArrayType(RSContext *Context, 387 const RSExportType *ElementType, 388 unsigned Size) 389 : RSExportType(Context, 390 ExportClassConstantArray, 391 DUMMY_TYPE_NAME_FOR_RS_CONSTANT_ARRAY_TYPE), 392 mElementType(ElementType), 393 mSize(Size) { 394 return; 395 } 396 397 // @CAT was normalized by calling RSExportType::TypeExportable() before 398 // calling this. 399 static RSExportConstantArrayType *Create(RSContext *Context, 400 const clang::ConstantArrayType *CAT); 401 402 virtual const llvm::Type *convertToLLVMType() const; 403 virtual union RSType *convertToSpecType() const; 404 public: 405 inline unsigned getSize() const { return mSize; } 406 inline const RSExportType *getElementType() const { return mElementType; } 407 408 virtual bool keep(); 409 virtual bool equals(const RSExportable *E) const; 410}; 411 412class RSExportRecordType : public RSExportType { 413 friend class RSExportType; 414 public: 415 class Field { 416 private: 417 const RSExportType *mType; 418 // Field name 419 std::string mName; 420 // Link to the struct that contain this field 421 const RSExportRecordType *mParent; 422 // Offset in the container 423 size_t mOffset; 424 425 public: 426 Field(const RSExportType *T, 427 const llvm::StringRef &Name, 428 const RSExportRecordType *Parent, 429 size_t Offset) 430 : mType(T), 431 mName(Name.data(), Name.size()), 432 mParent(Parent), 433 mOffset(Offset) { 434 return; 435 } 436 437 inline const RSExportRecordType *getParent() const { return mParent; } 438 inline const RSExportType *getType() const { return mType; } 439 inline const std::string &getName() const { return mName; } 440 inline size_t getOffsetInParent() const { return mOffset; } 441 }; 442 443 typedef std::list<const Field*>::const_iterator const_field_iterator; 444 445 inline const_field_iterator fields_begin() const { 446 return this->mFields.begin(); 447 } 448 inline const_field_iterator fields_end() const { 449 return this->mFields.end(); 450 } 451 452 private: 453 std::list<const Field*> mFields; 454 bool mIsPacked; 455 // Artificial export struct type is not exported by user (and thus it won't 456 // get reflected) 457 bool mIsArtificial; 458 size_t mAllocSize; 459 460 RSExportRecordType(RSContext *Context, 461 const llvm::StringRef &Name, 462 bool IsPacked, 463 bool IsArtificial, 464 size_t AllocSize) 465 : RSExportType(Context, ExportClassRecord, Name), 466 mIsPacked(IsPacked), 467 mIsArtificial(IsArtificial), 468 mAllocSize(AllocSize) { 469 return; 470 } 471 472 // @RT was normalized by calling RSExportType::TypeExportable() before calling 473 // this. 474 // @TypeName was retrieved from RSExportType::GetTypeName() before calling 475 // this. 476 static RSExportRecordType *Create(RSContext *Context, 477 const clang::RecordType *RT, 478 const llvm::StringRef &TypeName, 479 bool mIsArtificial = false); 480 481 virtual const llvm::Type *convertToLLVMType() const; 482 virtual union RSType *convertToSpecType() const; 483 public: 484 inline const std::list<const Field*>& getFields() const { return mFields; } 485 inline bool isPacked() const { return mIsPacked; } 486 inline bool isArtificial() const { return mIsArtificial; } 487 inline size_t getAllocSize() const { return mAllocSize; } 488 489 virtual bool keep(); 490 virtual bool equals(const RSExportable *E) const; 491 492 ~RSExportRecordType() { 493 for (std::list<const Field*>::iterator I = mFields.begin(), 494 E = mFields.end(); 495 I != E; 496 I++) 497 if (*I != NULL) 498 delete *I; 499 return; 500 } 501}; // RSExportRecordType 502 503} // namespace slang 504 505#endif // _SLANG_COMPILER_RS_EXPORT_TYPE_H 506