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