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