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