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