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