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