zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame^] | 1 | #include "slang_rs_export_element.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 2 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 3 | #include "clang/Basic/SourceLocation.h" |
| 4 | #include "clang/Basic/IdentifierTable.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 5 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 6 | #include "clang/AST/Decl.h" |
| 7 | #include "clang/AST/Type.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 8 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame^] | 9 | #include "slang_rs_context.h" |
| 10 | #include "slang_rs_export_type.h" |
| 11 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 12 | using namespace slang; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 13 | |
| 14 | bool RSExportElement::Initialized = false; |
| 15 | RSExportElement::ElementInfoMapTy RSExportElement::ElementInfoMap; |
| 16 | |
| 17 | void RSExportElement::Init() { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 18 | if (!Initialized) { |
| 19 | // Initialize ElementInfoMap |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 20 | #define USE_ELEMENT_DATA_TYPE |
| 21 | #define USE_ELEMENT_DATA_KIND |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 22 | #define DEF_ELEMENT(_name, _dk, _dt, _norm, _vsize) \ |
| 23 | { \ |
| 24 | ElementInfo *EI = new ElementInfo; \ |
| 25 | EI->kind = GET_ELEMENT_DATA_KIND(_dk); \ |
| 26 | EI->type = GET_ELEMENT_DATA_TYPE(_dt); \ |
| 27 | EI->normalized = _norm; \ |
| 28 | EI->vsize = _vsize; \ |
| 29 | \ |
| 30 | llvm::StringRef Name(_name); \ |
| 31 | ElementInfoMap.insert( \ |
| 32 | ElementInfoMapTy::value_type::Create( \ |
| 33 | Name.begin(), \ |
| 34 | Name.end(), \ |
| 35 | ElementInfoMap.getAllocator(), \ |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame^] | 36 | EI)); \ |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 37 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 38 | #include "slang_rs_export_element_support.inc" |
| 39 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 40 | Initialized = true; |
| 41 | } |
| 42 | return; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 45 | RSExportType *RSExportElement::Create(RSContext *Context, |
| 46 | const clang::Type *T, |
| 47 | const ElementInfo *EI) { |
| 48 | // Create RSExportType corresponded to the @T first and then verify |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 49 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 50 | llvm::StringRef TypeName; |
| 51 | RSExportType *ET = NULL; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 52 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 53 | if (!Initialized) |
| 54 | Init(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 55 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 56 | assert(EI != NULL && "Element info not found"); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 57 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 58 | if (!RSExportType::NormalizeType(T, TypeName)) |
| 59 | return NULL; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 60 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame^] | 61 | switch (T->getTypeClass()) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 62 | case clang::Type::Builtin: |
| 63 | case clang::Type::Pointer: { |
| 64 | assert(EI->vsize == 1 && "Element not a primitive class (please check " |
| 65 | "your macro)"); |
| 66 | RSExportPrimitiveType *EPT = |
| 67 | RSExportPrimitiveType::Create(Context, |
| 68 | T, |
| 69 | TypeName, |
| 70 | EI->kind, |
| 71 | EI->normalized); |
| 72 | // Verify |
| 73 | assert(EI->type == EPT->getType() && "Element has unexpected type"); |
| 74 | ET = EPT; |
| 75 | break; |
Shih-wei Liao | f8149d9 | 2010-08-22 05:32:02 -0700 | [diff] [blame] | 76 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 77 | case clang::Type::ConstantArray: { |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame^] | 78 | // XXX |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 79 | break; |
| 80 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 81 | case clang::Type::ExtVector: { |
| 82 | assert(EI->vsize > 1 && "Element not a vector class (please check your " |
| 83 | "macro)"); |
| 84 | RSExportVectorType *EVT = |
| 85 | RSExportVectorType::Create(Context, |
| 86 | static_cast<clang::ExtVectorType*>( |
| 87 | T->getCanonicalTypeInternal() |
| 88 | .getTypePtr()), |
| 89 | TypeName, |
| 90 | EI->kind, |
| 91 | EI->normalized); |
| 92 | // Verify |
| 93 | assert(EI->type == EVT->getType() && "Element has unexpected type"); |
| 94 | assert(EI->vsize == EVT->getNumElement() && "Element has unexpected size " |
| 95 | "of vector"); |
| 96 | ET = EVT; |
| 97 | break; |
| 98 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 99 | case clang::Type::Record: { |
| 100 | // Must be RS object type |
| 101 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame^] | 102 | if (TypeName.equals(llvm::StringRef("rs_matrix2x2")) || |
| 103 | TypeName.equals(llvm::StringRef("rs_matrix3x3")) || |
| 104 | TypeName.equals(llvm::StringRef("rs_matrix4x4"))) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 105 | const clang::RecordType *RT = static_cast<const clang::RecordType*> (T); |
| 106 | const clang::RecordDecl *RD = RT->getDecl(); |
| 107 | RD = RD->getDefinition(); |
| 108 | clang::RecordDecl::field_iterator fit = RD->field_begin(); |
| 109 | clang::FieldDecl *FD = *fit; |
| 110 | const clang::Type *FT = RSExportType::GetTypeOfDecl(FD); |
| 111 | RSExportConstantArrayType *ECT = |
| 112 | RSExportConstantArrayType::Create( |
| 113 | Context, |
| 114 | static_cast<const clang::ConstantArrayType*> (FT), |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame^] | 115 | TypeName); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 116 | ET = ECT; |
| 117 | } else { |
| 118 | RSExportPrimitiveType* EPT = |
| 119 | RSExportPrimitiveType::Create(Context, |
| 120 | T, |
| 121 | TypeName, |
| 122 | EI->kind, |
| 123 | EI->normalized); |
| 124 | // Verify |
| 125 | assert(EI->type == EPT->getType() && "Element has unexpected type"); |
| 126 | ET = EPT; |
| 127 | } |
| 128 | break; |
| 129 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 130 | default: { |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame^] | 131 | // TODO(zonr): warn that type is not exportable |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 132 | fprintf(stderr, "RSExportElement::Create : type '%s' is not exportable\n", |
| 133 | T->getTypeClassName()); |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return ET; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 141 | RSExportType *RSExportElement::CreateFromDecl(RSContext *Context, |
| 142 | const clang::DeclaratorDecl *DD) { |
| 143 | const clang::Type* T = RSExportType::GetTypeOfDecl(DD); |
| 144 | const clang::Type* CT = GET_CANONICAL_TYPE(T); |
| 145 | const ElementInfo* EI = NULL; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 146 | |
Shih-wei Liao | 91a3783 | 2010-10-03 19:11:51 -0700 | [diff] [blame] | 147 | // Note: RS element like rs_pixel_rgb elements are either in the type of |
| 148 | // primitive or vector. |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 149 | if ((CT->getTypeClass() != clang::Type::Builtin) && |
| 150 | (CT->getTypeClass() != clang::Type::ExtVector) && |
| 151 | (CT->getTypeClass() != clang::Type::Record)) { |
| 152 | return RSExportType::Create(Context, T); |
| 153 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 154 | |
Shih-wei Liao | 91a3783 | 2010-10-03 19:11:51 -0700 | [diff] [blame] | 155 | // Following the typedef chain to see whether it's an element name like |
| 156 | // rs_pixel_rgb or its alias (via typedef). |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 157 | while (T != CT) { |
| 158 | if (T->getTypeClass() != clang::Type::Typedef) { |
| 159 | break; |
Shih-wei Liao | f8149d9 | 2010-08-22 05:32:02 -0700 | [diff] [blame] | 160 | } else { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 161 | const clang::TypedefType *TT = static_cast<const clang::TypedefType*>(T); |
| 162 | const clang::TypedefDecl *TD = TT->getDecl(); |
| 163 | EI = GetElementInfo(TD->getName()); |
| 164 | if (EI != NULL) |
| 165 | break; |
| 166 | |
| 167 | T = TD->getUnderlyingType().getTypePtr(); |
Shih-wei Liao | f8149d9 | 2010-08-22 05:32:02 -0700 | [diff] [blame] | 168 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | if (EI == NULL) { |
| 172 | return RSExportType::Create(Context, T); |
| 173 | } else { |
| 174 | return RSExportElement::Create(Context, T, EI); |
| 175 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 178 | const RSExportElement::ElementInfo * |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame^] | 179 | RSExportElement::GetElementInfo(const llvm::StringRef &Name) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 180 | if (!Initialized) |
| 181 | Init(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 182 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 183 | ElementInfoMapTy::const_iterator I = ElementInfoMap.find(Name); |
| 184 | if (I == ElementInfoMap.end()) |
| 185 | return NULL; |
| 186 | else |
| 187 | return I->getValue(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 188 | } |