slang_rs_export_type.h revision d3f7527b105d21f1c69d3473eb88a762f2c3ab5a
1c383a500aa59423264811be3874461bf8adbfea0Zonr Chang/*
2d5a84f6d49d64738e4bb7c9dea7242e48acad959Stephen Hines * Copyright 2010-2012, The Android Open Source Project
3c383a500aa59423264811be3874461bf8adbfea0Zonr Chang *
4c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * Licensed under the Apache License, Version 2.0 (the "License");
5c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * you may not use this file except in compliance with the License.
6c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * You may obtain a copy of the License at
7c383a500aa59423264811be3874461bf8adbfea0Zonr Chang *
8c383a500aa59423264811be3874461bf8adbfea0Zonr Chang *     http://www.apache.org/licenses/LICENSE-2.0
9c383a500aa59423264811be3874461bf8adbfea0Zonr Chang *
10c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * Unless required by applicable law or agreed to in writing, software
11c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * distributed under the License is distributed on an "AS IS" BASIS,
12c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * See the License for the specific language governing permissions and
14c383a500aa59423264811be3874461bf8adbfea0Zonr Chang * limitations under the License.
15c383a500aa59423264811be3874461bf8adbfea0Zonr Chang */
16c383a500aa59423264811be3874461bf8adbfea0Zonr Chang
17e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_TYPE_H_  // NOLINT
18e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#define _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_TYPE_H_
19462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
209ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao#include <list>
21e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include <set>
229ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao#include <string>
23a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines#include <sstream>
249ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
25e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "clang/AST/Decl.h"
26e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "clang/AST/Type.h"
27a41ce1d98094da84643995d40d71c529905123fcZonr Chang
286315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr#include "llvm/ADT/SmallPtrSet.h"
29e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "llvm/ADT/StringMap.h"
30e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "llvm/ADT/StringRef.h"
316315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr
32e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#include "llvm/Support/ManagedStatic.h"
336315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr
34a41ce1d98094da84643995d40d71c529905123fcZonr Chang#include "slang_rs_exportable.h"
35a41ce1d98094da84643995d40d71c529905123fcZonr Chang
369ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao#define GET_CANONICAL_TYPE(T) \
372e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang    (((T) == NULL) ? NULL : (T)->getCanonicalTypeInternal().getTypePtr())
389ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao#define UNSAFE_CAST_TYPE(TT, T) \
392e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang    static_cast<TT*>(T->getCanonicalTypeInternal().getTypePtr())
409ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao#define GET_EXT_VECTOR_ELEMENT_TYPE(T) \
412e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang    (((T) == NULL) ? NULL : \
422e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang                     GET_CANONICAL_TYPE((T)->getElementType().getTypePtr()))
439ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao#define GET_POINTEE_TYPE(T) \
442e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang    (((T) == NULL) ? NULL : \
452e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang                     GET_CANONICAL_TYPE((T)->getPointeeType().getTypePtr()))
462e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang#define GET_CONSTANT_ARRAY_ELEMENT_TYPE(T)  \
472e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang    (((T) == NULL) ? NULL : \
482e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang                     GET_CANONICAL_TYPE((T)->getElementType().getTypePtr()))
490da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang#define DUMMY_RS_TYPE_NAME_PREFIX   "<"
500da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang#define DUMMY_RS_TYPE_NAME_POSTFIX  ">"
512e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang#define DUMMY_TYPE_NAME_FOR_RS_CONSTANT_ARRAY_TYPE  \
522e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang    DUMMY_RS_TYPE_NAME_PREFIX"ConstantArray"DUMMY_RS_TYPE_NAME_POSTFIX
53462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
54a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr Changunion RSType;
55a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr Chang
56462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liaonamespace llvm {
576315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr  class Type;
589ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao}   // namespace llvm
59462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
60462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liaonamespace slang {
61462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
626315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr  class RSContext;
63462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
64fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hinestypedef struct {
65fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hines    const char * rs_type;
66fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hines    const char * rs_short_type;
67fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hines    uint32_t size_in_bits;
68fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hines    const char * c_name;
69fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hines    const char * java_name;
70fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hines    const char * rs_c_vector_prefix;
71fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hines    const char * rs_java_vector_prefix;
72fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hines    bool java_promotion;
73fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hines} RSReflectionType;
74fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hines
751b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams
761b6a0883cd6984e11e59b0c847fb334df1f41afcJason Samstypedef struct RSReflectionTypeData_rec {
771b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams    const RSReflectionType *type;
781b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams    uint32_t vecSize;
791b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams    bool isPointer;
801b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams    uint32_t arraySize;
811b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams
821b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams    // Subelements
831b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams    //std::vector<const struct RSReflectionTypeData_rec *> fields;
841b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams    //std::vector< std::string > fieldNames;
851b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams    //std::vector< uint32_t> fieldOffsetBytes;
861b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams} RSReflectionTypeData;
871b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams
881b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams
89a41ce1d98094da84643995d40d71c529905123fcZonr Changclass RSExportType : public RSExportable {
909ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  friend class RSExportElement;
919ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao public:
929ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  typedef enum {
939ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    ExportClassPrimitive,
949ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    ExportClassPointer,
959ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    ExportClassVector,
9692b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang    ExportClassMatrix,
972e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang    ExportClassConstantArray,
989ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    ExportClassRecord
999ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  } ExportClass;
100462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
1011b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams  void convertToRTD(RSReflectionTypeData *rtd) const;
1021b6a0883cd6984e11e59b0c847fb334df1f41afcJason Sams
1039ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao private:
1046b6320ad5faee29e0f75fe937e40156746ef9e80Zonr Chang  ExportClass mClass;
1059ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  std::string mName;
1069ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1079ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // Cache the result after calling convertToLLVMType() at the first time
1087c67e578c760408dba0c2f64da6e074dd8b56fd9Shih-wei Liao  mutable llvm::Type *mLLVMType;
109a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr Chang  // Cache the result after calling convertToSpecType() at the first time
110a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr Chang  mutable union RSType *mSpecType;
1119ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1129ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao protected:
1136b6320ad5faee29e0f75fe937e40156746ef9e80Zonr Chang  RSExportType(RSContext *Context,
1146b6320ad5faee29e0f75fe937e40156746ef9e80Zonr Chang               ExportClass Class,
1156b6320ad5faee29e0f75fe937e40156746ef9e80Zonr Chang               const llvm::StringRef &Name);
1169ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1179ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // Let's make it private since there're some prerequisites to call this
1189ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // function.
1199ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  //
120e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  // @T was normalized by calling RSExportType::NormalizeType().
1216315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr  // @TypeName was retrieve from RSExportType::GetTypeName() before calling
1226315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr  //           this.
1239ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  //
1249ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  static RSExportType *Create(RSContext *Context,
1259ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                              const clang::Type *T,
1266315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr                              const llvm::StringRef &TypeName);
1279ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1289ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  static llvm::StringRef GetTypeName(const clang::Type *T);
1299ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1309ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // This function convert the RSExportType to LLVM type. Actually, it should be
1319ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // "convert Clang type to LLVM type." However, clang doesn't make this API
1329ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // (lib/CodeGen/CodeGenTypes.h) public, we need to do by ourselves.
1339ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  //
1349ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // Once we can get LLVM type, we can use LLVM to get alignment information,
1359ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // allocation size of a given type and structure layout that LLVM used
1369ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // (all of these information are target dependent) without dealing with these
1379ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // by ourselves.
1387c67e578c760408dba0c2f64da6e074dd8b56fd9Shih-wei Liao  virtual llvm::Type *convertToLLVMType() const = 0;
139e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines  // Record type may recursively reference its type definition. We need a
1403cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang  // temporary type setup before the type construction gets done.
1417c67e578c760408dba0c2f64da6e074dd8b56fd9Shih-wei Liao  inline void setAbstractLLVMType(llvm::Type *LLVMType) const {
1423cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang    mLLVMType = LLVMType;
1433cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang  }
1449ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
145a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr Chang  virtual union RSType *convertToSpecType() const = 0;
146a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr Chang  inline void setSpecTypeTemporarily(union RSType *SpecType) const {
147a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr Chang    mSpecType = SpecType;
148a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr Chang  }
149a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr Chang
150a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr Chang  virtual ~RSExportType();
151ecddee364d731c09c77b4c3fa647fa4f50e26756Stephen Hines
1529ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao public:
153e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  // This function additionally verifies that the Type T is exportable.
154e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  // If it is not, this function returns false. Otherwise it returns true.
155e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  static bool NormalizeType(const clang::Type *&T,
156e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines                            llvm::StringRef &TypeName,
15748d893dc7794b3cfb74f35955ca763ee4170f9adStephen Hines                            RSContext *Context,
1585bfec8dd08b3bde9ba3b331e2115210b0e910eaeStephen Hines                            const clang::VarDecl *VD);
15978e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
16011274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines  // This function checks whether the specified type can be handled by RS/FS.
16111274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines  // If it cannot, this function returns false. Otherwise it returns true.
16211274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines  // Filterscript has additional restrictions on supported types.
163d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet  static bool ValidateType(slang::RSContext *Context, clang::ASTContext &C,
164d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                           clang::QualType QT, clang::NamedDecl *ND,
165d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                           clang::SourceLocation Loc, unsigned int TargetAPI,
166d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                           bool IsFilterscript);
16711274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines
16878e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines  // This function ensures that the VarDecl can be properly handled by RS.
16978e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines  // If it cannot, this function returns false. Otherwise it returns true.
17011274a7324b478ec13e1d10a1b81350b34a65ab1Stephen Hines  // Filterscript has additional restrictions on supported types.
171d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet  static bool ValidateVarDecl(slang::RSContext *Context, clang::VarDecl *VD,
172d3f7527b105d21f1c69d3473eb88a762f2c3ab5aJean-Luc Brouillet                              unsigned int TargetAPI, bool IsFilterscript);
17378e69cb06b9b0683b2ac9dcafde87b867690ef2fStephen Hines
1749ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // @T may not be normalized
1759ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  static RSExportType *Create(RSContext *Context, const clang::Type *T);
1769ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  static RSExportType *CreateFromDecl(RSContext *Context,
1779ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                      const clang::VarDecl *VD);
1789ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1799ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  static const clang::Type *GetTypeOfDecl(const clang::DeclaratorDecl *DD);
1809ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1816b6320ad5faee29e0f75fe937e40156746ef9e80Zonr Chang  inline ExportClass getClass() const { return mClass; }
1829ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
1830d26cef64debfaa6862a27587c1fd0d30baa3b1dStephen Hines  virtual unsigned getSize() const { return 1; }
1840d26cef64debfaa6862a27587c1fd0d30baa3b1dStephen Hines
1857c67e578c760408dba0c2f64da6e074dd8b56fd9Shih-wei Liao  inline llvm::Type *getLLVMType() const {
1869ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    if (mLLVMType == NULL)
1879ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      mLLVMType = convertToLLVMType();
1889ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    return mLLVMType;
1899ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
190462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
191a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr Chang  inline const union RSType *getSpecType() const {
192a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr Chang    if (mSpecType == NULL)
193a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr Chang      mSpecType = convertToSpecType();
194a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr Chang    return mSpecType;
195a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr Chang  }
196a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr Chang
1979ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // Return the number of bits necessary to hold the specified RSExportType
1989ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  static size_t GetTypeStoreSize(const RSExportType *ET);
199462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
2009ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // The size of allocation of specified RSExportType (alignment considered)
2019ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  static size_t GetTypeAllocSize(const RSExportType *ET);
202462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
203641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang  inline const std::string &getName() const { return mName; }
204641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang
205a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines  virtual std::string getElementName() const {
206a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines    // Base case is actually an invalid C/Java identifier.
207a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines    return "@@INVALID@@";
208a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines  }
209a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines
2103cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang  virtual bool keep();
211641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang  virtual bool equals(const RSExportable *E) const;
2129ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao};  // RSExportType
213462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
2149ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao// Primitive types
215462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liaoclass RSExportPrimitiveType : public RSExportType {
2169ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  friend class RSExportType;
2179ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  friend class RSExportElement;
2189ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao public:
2199ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // From graphics/java/android/renderscript/Element.java: Element.DataType
2209ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  typedef enum {
221feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines    DataTypeIsStruct = -2,
2229ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    DataTypeUnknown = -1,
2239ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
224b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang#define ENUM_PRIMITIVE_DATA_TYPE_RANGE(begin_type, end_type)  \
225b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang    FirstPrimitiveType = DataType ## begin_type,  \
226b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang    LastPrimitiveType = DataType ## end_type,
227b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang
228b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang#define ENUM_RS_MATRIX_DATA_TYPE_RANGE(begin_type, end_type)  \
229b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang    FirstRSMatrixType = DataType ## begin_type,  \
230b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang    LastRSMatrixType = DataType ## end_type,
231b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang
232b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang#define ENUM_RS_OBJECT_DATA_TYPE_RANGE(begin_type, end_type)  \
233b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang    FirstRSObjectType = DataType ## begin_type,  \
234b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang    LastRSObjectType = DataType ## end_type,
235b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang
236a65ec168e41e3ee9c6e8ac04cde694bbbfc2590aZonr Chang#define ENUM_RS_DATA_TYPE(type, cname, bits)  \
237a65ec168e41e3ee9c6e8ac04cde694bbbfc2590aZonr Chang    DataType ## type,
238b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang
239a65ec168e41e3ee9c6e8ac04cde694bbbfc2590aZonr Chang#include "RSDataTypeEnums.inc"
2409ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
2419ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    DataTypeMax
2429ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  } DataType;
2439ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
2449ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao private:
245a65ec168e41e3ee9c6e8ac04cde694bbbfc2590aZonr Chang  // NOTE: There's no any instance of RSExportPrimitiveType which mType
246a65ec168e41e3ee9c6e8ac04cde694bbbfc2590aZonr Chang  // is of the value DataTypeRSMatrix*. DataTypeRSMatrix* enumeration here is
247a65ec168e41e3ee9c6e8ac04cde694bbbfc2590aZonr Chang  // only for RSExportPrimitiveType::GetRSObjectType to *recognize* the struct
248a65ec168e41e3ee9c6e8ac04cde694bbbfc2590aZonr Chang  // rs_matrix{2x2, 3x3, 4x4}. These matrix type are represented as
249a65ec168e41e3ee9c6e8ac04cde694bbbfc2590aZonr Chang  // RSExportMatrixType.
2509ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  DataType mType;
2519ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  bool mNormalized;
2529ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
253b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang  typedef llvm::StringMap<DataType> RSSpecificTypeMapTy;
254b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang  static llvm::ManagedStatic<RSSpecificTypeMapTy> RSSpecificTypeMap;
2559ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
2569ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  static llvm::Type *RSObjectLLVMType;
2579ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
2589ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  static const size_t SizeOfDataTypeInBits[];
259e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  // @T was normalized by calling RSExportType::NormalizeType() before calling
2609ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // this.
2619ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // @TypeName was retrieved from RSExportType::GetTypeName() before calling
2629ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // this
2639ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  static RSExportPrimitiveType *Create(RSContext *Context,
2649ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                       const clang::Type *T,
2659ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                       const llvm::StringRef &TypeName,
2669ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                       bool Normalized = false);
2679ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
2689ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao protected:
2699ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  RSExportPrimitiveType(RSContext *Context,
2706b6320ad5faee29e0f75fe937e40156746ef9e80Zonr Chang                        // for derived class to set their type class
2716b6320ad5faee29e0f75fe937e40156746ef9e80Zonr Chang                        ExportClass Class,
2729ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                        const llvm::StringRef &Name,
2739ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                        DataType DT,
2746315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr                        bool Normalized)
2756b6320ad5faee29e0f75fe937e40156746ef9e80Zonr Chang      : RSExportType(Context, Class, Name),
2766315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr        mType(DT),
2776315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr        mNormalized(Normalized) {
2789ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    return;
2799ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
2809ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
2817c67e578c760408dba0c2f64da6e074dd8b56fd9Shih-wei Liao  virtual llvm::Type *convertToLLVMType() const;
282a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr Chang  virtual union RSType *convertToSpecType() const;
2832ef9bc0cfbca2152d972c0975005f8c897c2a42cStephen Hines
2842ef9bc0cfbca2152d972c0975005f8c897c2a42cStephen Hines  static DataType GetDataType(RSContext *Context, const clang::Type *T);
2852ef9bc0cfbca2152d972c0975005f8c897c2a42cStephen Hines
2869ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao public:
287dd6206bb61bf8df2ed6b643abe8a29c48a315685Stephen Hines  // T is normalized by calling RSExportType::NormalizeType() before
288dd6206bb61bf8df2ed6b643abe8a29c48a315685Stephen Hines  // calling this
289dd6206bb61bf8df2ed6b643abe8a29c48a315685Stephen Hines  static bool IsPrimitiveType(const clang::Type *T);
290dd6206bb61bf8df2ed6b643abe8a29c48a315685Stephen Hines
2919ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // @T may not be normalized
2929ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  static RSExportPrimitiveType *Create(RSContext *Context,
2932b8fb64be3047df940a219872b331eb11de2758dStephen Hines                                       const clang::Type *T);
294462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
295b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang  static DataType GetRSSpecificType(const llvm::StringRef &TypeName);
296b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang  static DataType GetRSSpecificType(const clang::Type *T);
297b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang
298b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang  static bool IsRSMatrixType(DataType DT);
299b1771ef128b10c4d4575634828006bfba20b1d9cZonr Chang  static bool IsRSObjectType(DataType DT);
300f2174cfd6a556b51aadf2b8765e50df080e8f18eStephen Hines  static bool IsRSObjectType(const clang::Type *T) {
301f2174cfd6a556b51aadf2b8765e50df080e8f18eStephen Hines    return IsRSObjectType(GetRSSpecificType(T));
302f2174cfd6a556b51aadf2b8765e50df080e8f18eStephen Hines  }
303462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
304feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines  // Determines whether T is [an array of] struct that contains at least one
305feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines  // RS object type within it.
306feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines  static bool IsStructureTypeWithRSObject(const clang::Type *T);
307feaca06fcb0772e9e972a0d61b17259fc5124d50Stephen Hines
3089ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  static size_t GetSizeInBits(const RSExportPrimitiveType *EPT);
309462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
3109ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  inline DataType getType() const { return mType; }
3119ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  inline bool isRSObjectType() const {
312b3a12fe7c18a06f99201dc491a932a90ab7d975cStephen Hines    return ((mType >= FirstRSObjectType) && (mType <= LastRSObjectType));
3139ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
314641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang
315641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang  virtual bool equals(const RSExportable *E) const;
316fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hines
317fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hines  static RSReflectionType *getRSReflectionType(DataType DT);
318fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hines  static RSReflectionType *getRSReflectionType(
319fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hines      const RSExportPrimitiveType *EPT) {
320fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hines    return getRSReflectionType(EPT->getType());
321fdd1ba13a69501a1b91fdc9be31413215d467497Stephen Hines  }
322a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines
3231f6c331d622ac645ab68a016aa4c577998547373Stephen Hines  virtual unsigned getSize() const { return (GetSizeInBits(this) >> 3); }
3241f6c331d622ac645ab68a016aa4c577998547373Stephen Hines
325a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines  std::string getElementName() const {
326a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines    return getRSReflectionType(this)->rs_short_type;
327a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines  }
3289ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao};  // RSExportPrimitiveType
329462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
330462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
331462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liaoclass RSExportPointerType : public RSExportType {
3329ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  friend class RSExportType;
3339ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  friend class RSExportFunc;
3349ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao private:
3359ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  const RSExportType *mPointeeType;
3369ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
3379ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  RSExportPointerType(RSContext *Context,
3389ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                      const llvm::StringRef &Name,
3396315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr                      const RSExportType *PointeeType)
3406b6320ad5faee29e0f75fe937e40156746ef9e80Zonr Chang      : RSExportType(Context, ExportClassPointer, Name),
3416315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr        mPointeeType(PointeeType) {
3429ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    return;
3439ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
3449ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
345e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  // @PT was normalized by calling RSExportType::NormalizeType() before calling
3469ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // this.
3479ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  static RSExportPointerType *Create(RSContext *Context,
3489ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                     const clang::PointerType *PT,
3499ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                     const llvm::StringRef &TypeName);
3509ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
3517c67e578c760408dba0c2f64da6e074dd8b56fd9Shih-wei Liao  virtual llvm::Type *convertToLLVMType() const;
352a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr Chang  virtual union RSType *convertToSpecType() const;
353ecddee364d731c09c77b4c3fa647fa4f50e26756Stephen Hines
3549ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao public:
3553cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang  virtual bool keep();
356641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang
3579ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  inline const RSExportType *getPointeeType() const { return mPointeeType; }
358641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang
359641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang  virtual bool equals(const RSExportable *E) const;
3609ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao};  // RSExportPointerType
361462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
362462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
363462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liaoclass RSExportVectorType : public RSExportPrimitiveType {
3649ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  friend class RSExportType;
3659ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  friend class RSExportElement;
3669ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao private:
36792b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang  unsigned mNumElement;   // number of element
3689ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
3699ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  RSExportVectorType(RSContext *Context,
3709ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                     const llvm::StringRef &Name,
3719ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                     DataType DT,
3729ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                     bool Normalized,
37392b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang                     unsigned NumElement)
3746b6320ad5faee29e0f75fe937e40156746ef9e80Zonr Chang      : RSExportPrimitiveType(Context, ExportClassVector, Name,
3752b8fb64be3047df940a219872b331eb11de2758dStephen Hines                              DT, Normalized),
3766315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr        mNumElement(NumElement) {
3779ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    return;
3789ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
3799ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
380e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  // @EVT was normalized by calling RSExportType::NormalizeType() before
3819ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // calling this.
3829ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  static RSExportVectorType *Create(RSContext *Context,
3839ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                    const clang::ExtVectorType *EVT,
3849ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                    const llvm::StringRef &TypeName,
3859ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                    bool Normalized = false);
3869ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
3877c67e578c760408dba0c2f64da6e074dd8b56fd9Shih-wei Liao  virtual llvm::Type *convertToLLVMType() const;
388a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr Chang  virtual union RSType *convertToSpecType() const;
389ecddee364d731c09c77b4c3fa647fa4f50e26756Stephen Hines
3909ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao public:
3919ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  static llvm::StringRef GetTypeName(const clang::ExtVectorType *EVT);
392462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
39392b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang  inline unsigned getNumElement() const { return mNumElement; }
394641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang
395a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines  std::string getElementName() const {
396a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines    std::stringstream Name;
397a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines    Name << RSExportPrimitiveType::getRSReflectionType(this)->rs_short_type
398a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines         << "_" << getNumElement();
399a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines    return Name.str();
400a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines  }
401a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines
402641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang  virtual bool equals(const RSExportable *E) const;
40392b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang};
40492b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang
40592b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang// Only *square* *float* matrix is supported by now.
40692b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang//
40792b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang// struct rs_matrix{2x2,3x3,4x4, ..., NxN} should be defined as the following
40892b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang// form *exactly*:
40992b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang//  typedef struct {
41092b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang//    float m[{NxN}];
41192b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang//  } rs_matrixNxN;
41292b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang//
41392b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang//  where mDim will be N.
41492b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Changclass RSExportMatrixType : public RSExportType {
41592b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang  friend class RSExportType;
41692b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang private:
4172e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang  unsigned mDim;  // dimension
41892b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang
41992b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang  RSExportMatrixType(RSContext *Context,
42092b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang                     const llvm::StringRef &Name,
42192b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang                     unsigned Dim)
4226b6320ad5faee29e0f75fe937e40156746ef9e80Zonr Chang    : RSExportType(Context, ExportClassMatrix, Name),
42392b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang      mDim(Dim) {
42492b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang    return;
42592b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang  }
42692b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang
4277c67e578c760408dba0c2f64da6e074dd8b56fd9Shih-wei Liao  virtual llvm::Type *convertToLLVMType() const;
428a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr Chang  virtual union RSType *convertToSpecType() const;
429ecddee364d731c09c77b4c3fa647fa4f50e26756Stephen Hines
43092b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang public:
431e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  // @RT was normalized by calling RSExportType::NormalizeType() before
43292b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang  // calling this.
43392b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang  static RSExportMatrixType *Create(RSContext *Context,
43492b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang                                    const clang::RecordType *RT,
43592b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang                                    const llvm::StringRef &TypeName,
43692b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang                                    unsigned Dim);
43792b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang
43892b344a51c6c4934e96882bd401e4b13d6d03db8Zonr Chang  inline unsigned getDim() const { return mDim; }
439641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang
440641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang  virtual bool equals(const RSExportable *E) const;
441462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao};
442462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
4432e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Changclass RSExportConstantArrayType : public RSExportType {
4442e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang  friend class RSExportType;
4452e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang private:
4462e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang  const RSExportType *mElementType;  // Array element type
4472e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang  unsigned mSize;  // Array size
4482e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang
4492e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang  RSExportConstantArrayType(RSContext *Context,
4502e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang                            const RSExportType *ElementType,
4512e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang                            unsigned Size)
4522e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang    : RSExportType(Context,
4536b6320ad5faee29e0f75fe937e40156746ef9e80Zonr Chang                   ExportClassConstantArray,
4542e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang                   DUMMY_TYPE_NAME_FOR_RS_CONSTANT_ARRAY_TYPE),
4552e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang      mElementType(ElementType),
4562e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang      mSize(Size) {
4572e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang    return;
4582e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang  }
4592e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang
460e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  // @CAT was normalized by calling RSExportType::NormalizeType() before
4612e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang  // calling this.
4622e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang  static RSExportConstantArrayType *Create(RSContext *Context,
4632e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang                                           const clang::ConstantArrayType *CAT);
4642e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang
4657c67e578c760408dba0c2f64da6e074dd8b56fd9Shih-wei Liao  virtual llvm::Type *convertToLLVMType() const;
466a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr Chang  virtual union RSType *convertToSpecType() const;
467ecddee364d731c09c77b4c3fa647fa4f50e26756Stephen Hines
4682e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang public:
4690d26cef64debfaa6862a27587c1fd0d30baa3b1dStephen Hines  virtual unsigned getSize() const { return mSize; }
4702e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang  inline const RSExportType *getElementType() const { return mElementType; }
471641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang
472a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines  std::string getElementName() const {
473a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines    return mElementType->getElementName();
474a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines  }
475a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines
4763cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang  virtual bool keep();
477641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang  virtual bool equals(const RSExportable *E) const;
4782e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang};
4792e1dba6c779a0ae55c76d36a3c03553e16725ab7Zonr Chang
480462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liaoclass RSExportRecordType : public RSExportType {
4819ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  friend class RSExportType;
4829ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao public:
4839ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  class Field {
4849ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao   private:
4859ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    const RSExportType *mType;
4869ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    // Field name
4879ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    std::string mName;
4889ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    // Link to the struct that contain this field
4899ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    const RSExportRecordType *mParent;
4900da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang    // Offset in the container
4910da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang    size_t mOffset;
4929ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
4939ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao   public:
4949ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    Field(const RSExportType *T,
4959ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao          const llvm::StringRef &Name,
4969ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao          const RSExportRecordType *Parent,
4970da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang          size_t Offset)
4986315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr        : mType(T),
4996315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr          mName(Name.data(), Name.size()),
5006315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr          mParent(Parent),
5010da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang          mOffset(Offset) {
5029ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      return;
503462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao    }
504462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
5059ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    inline const RSExportRecordType *getParent() const { return mParent; }
5069ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    inline const RSExportType *getType() const { return mType; }
5079ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    inline const std::string &getName() const { return mName; }
5080da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang    inline size_t getOffsetInParent() const { return mOffset; }
5099ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  };
510462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
5119ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  typedef std::list<const Field*>::const_iterator const_field_iterator;
512462aefd62cc646d2ff753c1d003ef3cd7bbea26Shih-wei Liao
5139ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  inline const_field_iterator fields_begin() const {
5149ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    return this->mFields.begin();
5159ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
5169ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  inline const_field_iterator fields_end() const {
5179ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    return this->mFields.end();
5189ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
5199ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
5209ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao private:
5219ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  std::list<const Field*> mFields;
5229ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  bool mIsPacked;
5239ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // Artificial export struct type is not exported by user (and thus it won't
5249ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // get reflected)
5259ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  bool mIsArtificial;
5260da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang  size_t mAllocSize;
5279ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
5289ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  RSExportRecordType(RSContext *Context,
5299ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                     const llvm::StringRef &Name,
5309ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                     bool IsPacked,
5310da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang                     bool IsArtificial,
5320da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang                     size_t AllocSize)
5336b6320ad5faee29e0f75fe937e40156746ef9e80Zonr Chang      : RSExportType(Context, ExportClassRecord, Name),
5346315f76e3cc6ff2d012d1183a0b030d4ff0dc808zonr        mIsPacked(IsPacked),
5350da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang        mIsArtificial(IsArtificial),
5360da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang        mAllocSize(AllocSize) {
5379ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    return;
5389ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
5399ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
540e5e64432476a44b59c61ded233b1149109c7a7c3Stephen Hines  // @RT was normalized by calling RSExportType::NormalizeType() before calling
5419ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // this.
5429ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // @TypeName was retrieved from RSExportType::GetTypeName() before calling
5439ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  // this.
5449ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  static RSExportRecordType *Create(RSContext *Context,
5459ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                    const clang::RecordType *RT,
5469ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                    const llvm::StringRef &TypeName,
5479ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao                                    bool mIsArtificial = false);
5489ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
5497c67e578c760408dba0c2f64da6e074dd8b56fd9Shih-wei Liao  virtual llvm::Type *convertToLLVMType() const;
550a7a828d1ff95c5a8f2327f56a137a2bcb3a9a8faZonr Chang  virtual union RSType *convertToSpecType() const;
551ecddee364d731c09c77b4c3fa647fa4f50e26756Stephen Hines
5529ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao public:
5530da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang  inline const std::list<const Field*>& getFields() const { return mFields; }
5549ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  inline bool isPacked() const { return mIsPacked; }
5559ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  inline bool isArtificial() const { return mIsArtificial; }
5560da0a7dc51c25943fe31d0bfccbdfee326a3199cZonr Chang  inline size_t getAllocSize() const { return mAllocSize; }
5579ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
558a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines  virtual std::string getElementName() const {
559a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines    return "ScriptField_" + getName();
560a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines  }
561a6b54146b93eec68f6daa4b1877639cdc34801dcStephen Hines
5623cd3dd327445fcfa49f0e96cb2de2055bce541e9Zonr Chang  virtual bool keep();
563641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang  virtual bool equals(const RSExportable *E) const;
564641558f02fe6ce0ee3ae5076eb366c25e2ad5903Zonr Chang
5659ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  ~RSExportRecordType() {
5669ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    for (std::list<const Field*>::iterator I = mFields.begin(),
5679ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao             E = mFields.end();
5689ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao         I != E;
5699ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao         I++)
5709ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao      if (*I != NULL)
5719ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao        delete *I;
5729ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao    return;
5739ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao  }
5749ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao};  // RSExportRecordType
5759ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
5769ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao}   // namespace slang
5779ef2f785e0cc490af678dfd685995dec787321ffShih-wei Liao
578e639eb5caa2c386b4a60659a4929e8a6141a2cbeStephen Hines#endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_EXPORT_TYPE_H_  NOLINT
579