slang_rs_export_type.h revision e5e64432476a44b59c61ded233b1149109c7a7c3
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  // @T may not be normalized
131  static RSExportType *Create(RSContext *Context, const clang::Type *T);
132  static RSExportType *CreateFromDecl(RSContext *Context,
133                                      const clang::VarDecl *VD);
134
135  static const clang::Type *GetTypeOfDecl(const clang::DeclaratorDecl *DD);
136
137  inline ExportClass getClass() const { return mClass; }
138
139  inline const llvm::Type *getLLVMType() const {
140    if (mLLVMType == NULL)
141      mLLVMType = convertToLLVMType();
142    return mLLVMType;
143  }
144
145  inline const union RSType *getSpecType() const {
146    if (mSpecType == NULL)
147      mSpecType = convertToSpecType();
148    return mSpecType;
149  }
150
151  // Return the number of bits necessary to hold the specified RSExportType
152  static size_t GetTypeStoreSize(const RSExportType *ET);
153
154  // The size of allocation of specified RSExportType (alignment considered)
155  static size_t GetTypeAllocSize(const RSExportType *ET);
156
157  inline const std::string &getName() const { return mName; }
158
159  virtual bool keep();
160  virtual bool equals(const RSExportable *E) const;
161};  // RSExportType
162
163// Primitive types
164class RSExportPrimitiveType : public RSExportType {
165  friend class RSExportType;
166  friend class RSExportElement;
167 public:
168  // From graphics/java/android/renderscript/Element.java: Element.DataType
169  typedef enum {
170    DataTypeUnknown = -1,
171
172#define ENUM_PRIMITIVE_DATA_TYPE_RANGE(begin_type, end_type)  \
173    FirstPrimitiveType = DataType ## begin_type,  \
174    LastPrimitiveType = DataType ## end_type,
175
176#define ENUM_RS_MATRIX_DATA_TYPE_RANGE(begin_type, end_type)  \
177    FirstRSMatrixType = DataType ## begin_type,  \
178    LastRSMatrixType = DataType ## end_type,
179
180#define ENUM_RS_OBJECT_DATA_TYPE_RANGE(begin_type, end_type)  \
181    FirstRSObjectType = DataType ## begin_type,  \
182    LastRSObjectType = DataType ## end_type,
183
184#define ENUM_RS_DATA_TYPE(type, cname, bits)  \
185    DataType ## type,
186
187#include "RSDataTypeEnums.inc"
188
189    DataTypeMax
190  } DataType;
191
192  // From graphics/java/android/renderscript/Element.java: Element.DataKind
193  typedef enum {
194    DataKindUnknown = -1
195#define ENUM_RS_DATA_KIND(kind) \
196    , DataKind ## kind
197#include "RSDataKindEnums.inc"
198  } DataKind;
199
200 private:
201  // NOTE: There's no any instance of RSExportPrimitiveType which mType
202  // is of the value DataTypeRSMatrix*. DataTypeRSMatrix* enumeration here is
203  // only for RSExportPrimitiveType::GetRSObjectType to *recognize* the struct
204  // rs_matrix{2x2, 3x3, 4x4}. These matrix type are represented as
205  // RSExportMatrixType.
206  DataType mType;
207  DataKind mKind;
208  bool mNormalized;
209
210  typedef llvm::StringMap<DataType> RSSpecificTypeMapTy;
211  static llvm::ManagedStatic<RSSpecificTypeMapTy> RSSpecificTypeMap;
212
213  static llvm::Type *RSObjectLLVMType;
214
215  static const size_t SizeOfDataTypeInBits[];
216  // @T was normalized by calling RSExportType::NormalizeType() before calling
217  // this.
218  // @TypeName was retrieved from RSExportType::GetTypeName() before calling
219  // this
220  static RSExportPrimitiveType *Create(RSContext *Context,
221                                       const clang::Type *T,
222                                       const llvm::StringRef &TypeName,
223                                       DataKind DK = DataKindUser,
224                                       bool Normalized = false);
225
226 protected:
227  // T is normalized by calling RSExportType::NormalizeType() before
228  // calling this
229  static bool IsPrimitiveType(const clang::Type *T);
230
231  static DataType GetDataType(const clang::Type *T);
232
233  RSExportPrimitiveType(RSContext *Context,
234                        // for derived class to set their type class
235                        ExportClass Class,
236                        const llvm::StringRef &Name,
237                        DataType DT,
238                        DataKind DK,
239                        bool Normalized)
240      : RSExportType(Context, Class, Name),
241        mType(DT),
242        mKind(DK),
243        mNormalized(Normalized) {
244    return;
245  }
246
247  virtual const llvm::Type *convertToLLVMType() const;
248  virtual union RSType *convertToSpecType() const;
249 public:
250  // @T may not be normalized
251  static RSExportPrimitiveType *Create(RSContext *Context,
252                                       const clang::Type *T,
253                                       DataKind DK = DataKindUser);
254
255  static DataType GetRSSpecificType(const llvm::StringRef &TypeName);
256  static DataType GetRSSpecificType(const clang::Type *T);
257
258  static bool IsRSMatrixType(DataType DT);
259  static bool IsRSObjectType(DataType DT);
260
261  static size_t GetSizeInBits(const RSExportPrimitiveType *EPT);
262
263  inline DataType getType() const { return mType; }
264  inline DataKind getKind() const { return mKind; }
265  inline bool isRSObjectType() const {
266    return ((mType >= DataTypeRSElement) && (mType < DataTypeMax));
267  }
268
269  virtual bool equals(const RSExportable *E) const;
270};  // RSExportPrimitiveType
271
272
273class RSExportPointerType : public RSExportType {
274  friend class RSExportType;
275  friend class RSExportFunc;
276 private:
277  const RSExportType *mPointeeType;
278
279  RSExportPointerType(RSContext *Context,
280                      const llvm::StringRef &Name,
281                      const RSExportType *PointeeType)
282      : RSExportType(Context, ExportClassPointer, Name),
283        mPointeeType(PointeeType) {
284    return;
285  }
286
287  // @PT was normalized by calling RSExportType::NormalizeType() before calling
288  // this.
289  static RSExportPointerType *Create(RSContext *Context,
290                                     const clang::PointerType *PT,
291                                     const llvm::StringRef &TypeName);
292
293  virtual const llvm::Type *convertToLLVMType() const;
294  virtual union RSType *convertToSpecType() const;
295 public:
296  static const clang::Type *IntegerType;
297
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