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