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 | 78e69cb | 2011-04-22 15:03:19 -0700 | [diff] [blame] | 73 | if (!RSExportType::NormalizeType(T, TypeName, Context->getDiagnostics(), |
| 74 | NULL)) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 75 | return NULL; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 76 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 77 | switch (T->getTypeClass()) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 78 | case clang::Type::Builtin: |
| 79 | case clang::Type::Pointer: { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 80 | slangAssert(EI->vsize == 1 && "Element not a primitive class (please " |
| 81 | "check your macro)"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 82 | RSExportPrimitiveType *EPT = |
| 83 | RSExportPrimitiveType::Create(Context, |
| 84 | T, |
| 85 | TypeName, |
| 86 | EI->kind, |
| 87 | EI->normalized); |
| 88 | // Verify |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 89 | slangAssert(EI->type == EPT->getType() && "Element has unexpected type"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 90 | ET = EPT; |
| 91 | break; |
Shih-wei Liao | f8149d9 | 2010-08-22 05:32:02 -0700 | [diff] [blame] | 92 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 93 | case clang::Type::ExtVector: { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 94 | slangAssert(EI->vsize > 1 && "Element not a vector class (please check " |
| 95 | "your macro)"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 96 | RSExportVectorType *EVT = |
| 97 | RSExportVectorType::Create(Context, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 98 | static_cast<const clang::ExtVectorType*>( |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 99 | T->getCanonicalTypeInternal() |
| 100 | .getTypePtr()), |
| 101 | TypeName, |
| 102 | EI->kind, |
| 103 | EI->normalized); |
| 104 | // Verify |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 105 | slangAssert(EI->type == EVT->getType() && "Element has unexpected type"); |
| 106 | slangAssert(EI->vsize == EVT->getNumElement() && "Element has unexpected " |
| 107 | "size of vector"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 108 | ET = EVT; |
| 109 | break; |
| 110 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 111 | default: { |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 112 | // TODO(zonr): warn that type is not exportable |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 113 | fprintf(stderr, "RSExportElement::Create : type '%s' is not exportable\n", |
| 114 | T->getTypeClassName()); |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return ET; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 122 | RSExportType *RSExportElement::CreateFromDecl(RSContext *Context, |
| 123 | const clang::DeclaratorDecl *DD) { |
| 124 | const clang::Type* T = RSExportType::GetTypeOfDecl(DD); |
| 125 | const clang::Type* CT = GET_CANONICAL_TYPE(T); |
| 126 | const ElementInfo* EI = NULL; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 127 | |
Shih-wei Liao | 91a3783 | 2010-10-03 19:11:51 -0700 | [diff] [blame] | 128 | // Note: RS element like rs_pixel_rgb elements are either in the type of |
| 129 | // primitive or vector. |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 130 | if ((CT->getTypeClass() != clang::Type::Builtin) && |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 131 | (CT->getTypeClass() != clang::Type::ExtVector)) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 132 | return RSExportType::Create(Context, T); |
| 133 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 134 | |
Shih-wei Liao | 91a3783 | 2010-10-03 19:11:51 -0700 | [diff] [blame] | 135 | // Following the typedef chain to see whether it's an element name like |
| 136 | // rs_pixel_rgb or its alias (via typedef). |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 137 | while (T != CT) { |
| 138 | if (T->getTypeClass() != clang::Type::Typedef) { |
| 139 | break; |
Shih-wei Liao | f8149d9 | 2010-08-22 05:32:02 -0700 | [diff] [blame] | 140 | } else { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 141 | const clang::TypedefType *TT = static_cast<const clang::TypedefType*>(T); |
Shih-wei Liao | 83f0c62 | 2011-06-21 05:34:53 -0700 | [diff] [blame^] | 142 | const clang::TypedefNameDecl *TD = TT->getDecl(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 143 | EI = GetElementInfo(TD->getName()); |
| 144 | if (EI != NULL) |
| 145 | break; |
| 146 | |
| 147 | T = TD->getUnderlyingType().getTypePtr(); |
Shih-wei Liao | f8149d9 | 2010-08-22 05:32:02 -0700 | [diff] [blame] | 148 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | if (EI == NULL) { |
| 152 | return RSExportType::Create(Context, T); |
| 153 | } else { |
| 154 | return RSExportElement::Create(Context, T, EI); |
| 155 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 158 | const RSExportElement::ElementInfo * |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 159 | RSExportElement::GetElementInfo(const llvm::StringRef &Name) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 160 | if (!Initialized) |
| 161 | Init(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 162 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 163 | ElementInfoMapTy::const_iterator I = ElementInfoMap.find(Name); |
| 164 | if (I == ElementInfoMap.end()) |
| 165 | return NULL; |
| 166 | else |
| 167 | return I->getValue(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 168 | } |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 169 | |
| 170 | } // namespace slang |