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