Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 1 | /* |
Stephen Hines | 5bfec8d | 2012-04-04 08:18:57 -0700 | [diff] [blame] | 2 | * Copyright 2010, The Android Open Source Project |
Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 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 | |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 34 | struct DataElementInfo { |
| 35 | const char *name; |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 36 | DataType dataType; |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 37 | bool normalized; |
| 38 | int vsize; |
| 39 | }; |
| 40 | |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 41 | static DataElementInfo DataElementInfoTable[] = { |
Jean-Luc Brouillet | cec9b65 | 2014-05-14 19:33:57 -0700 | [diff] [blame] | 42 | {"rs_pixel_l", DataTypeUnsigned8, true, 1}, |
| 43 | {"rs_pixel_a", DataTypeUnsigned8, true, 1}, |
| 44 | {"rs_pixel_la", DataTypeUnsigned8, true, 2}, |
| 45 | {"rs_pixel_rgb", DataTypeUnsigned8, true, 3}, |
| 46 | {"rs_pixel_rgba", DataTypeUnsigned8, true, 4}, |
| 47 | {"rs_pixel_rgb565", DataTypeUnsigned8, true, 3}, |
| 48 | {"rs_pixel_rgb5551", DataTypeUnsigned8, true, 4}, |
| 49 | {"rs_pixel_rgb4444", DataTypeUnsigned8, true, 4}, |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | const int DataElementInfoTableCount = sizeof(DataElementInfoTable) / sizeof(DataElementInfoTable[0]); |
| 53 | |
| 54 | // TODO Rename RSExportElement to RSExportDataElement |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 55 | void RSExportElement::Init() { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 56 | if (!Initialized) { |
| 57 | // Initialize ElementInfoMap |
Jean-Luc Brouillet | 474655a | 2014-04-28 15:25:51 -0700 | [diff] [blame] | 58 | for (int i = 0; i < DataElementInfoTableCount; i++) { |
| 59 | ElementInfo *EI = new ElementInfo; |
| 60 | EI->type = DataElementInfoTable[i].dataType; |
| 61 | EI->normalized = DataElementInfoTable[i].normalized; |
| 62 | EI->vsize = DataElementInfoTable[i].vsize; |
| 63 | llvm::StringRef Name(DataElementInfoTable[i].name); |
| 64 | ElementInfoMap.insert(ElementInfoMapTy::value_type::Create( |
Stephen Hines | 2eb9a3f | 2014-07-15 16:50:03 -0700 | [diff] [blame] | 65 | Name, ElementInfoMap.getAllocator(), EI)); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 66 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 67 | Initialized = true; |
| 68 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 71 | RSExportType *RSExportElement::Create(RSContext *Context, |
| 72 | const clang::Type *T, |
| 73 | const ElementInfo *EI) { |
| 74 | // Create RSExportType corresponded to the @T first and then verify |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 75 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 76 | llvm::StringRef TypeName; |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 77 | RSExportType *ET = nullptr; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 78 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 79 | if (!Initialized) |
| 80 | Init(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 81 | |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 82 | slangAssert(EI != nullptr && "Element info not found"); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 83 | |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 84 | if (!RSExportType::NormalizeType(T, TypeName, Context, nullptr)) |
| 85 | return nullptr; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 86 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 87 | switch (T->getTypeClass()) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 88 | case clang::Type::Builtin: |
| 89 | case clang::Type::Pointer: { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 90 | slangAssert(EI->vsize == 1 && "Element not a primitive class (please " |
| 91 | "check your macro)"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 92 | RSExportPrimitiveType *EPT = |
| 93 | RSExportPrimitiveType::Create(Context, |
| 94 | T, |
| 95 | TypeName, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 96 | EI->normalized); |
| 97 | // Verify |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 98 | slangAssert(EI->type == EPT->getType() && "Element has unexpected type"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 99 | ET = EPT; |
| 100 | break; |
Shih-wei Liao | f8149d9 | 2010-08-22 05:32:02 -0700 | [diff] [blame] | 101 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 102 | case clang::Type::ExtVector: { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 103 | slangAssert(EI->vsize > 1 && "Element not a vector class (please check " |
| 104 | "your macro)"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 105 | RSExportVectorType *EVT = |
| 106 | RSExportVectorType::Create(Context, |
Logan | be27482 | 2011-02-16 22:02:54 +0800 | [diff] [blame] | 107 | static_cast<const clang::ExtVectorType*>( |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 108 | T->getCanonicalTypeInternal() |
| 109 | .getTypePtr()), |
| 110 | TypeName, |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 111 | EI->normalized); |
| 112 | // Verify |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 113 | slangAssert(EI->type == EVT->getType() && "Element has unexpected type"); |
| 114 | slangAssert(EI->vsize == EVT->getNumElement() && "Element has unexpected " |
| 115 | "size of vector"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 116 | ET = EVT; |
| 117 | break; |
| 118 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 119 | default: { |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 120 | // TODO(zonr): warn that type is not exportable |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 121 | fprintf(stderr, "RSExportElement::Create : type '%s' is not exportable\n", |
| 122 | T->getTypeClassName()); |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | return ET; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 130 | RSExportType *RSExportElement::CreateFromDecl(RSContext *Context, |
| 131 | const clang::DeclaratorDecl *DD) { |
| 132 | const clang::Type* T = RSExportType::GetTypeOfDecl(DD); |
Jean-Luc Brouillet | b095e05 | 2014-05-16 20:10:00 -0700 | [diff] [blame] | 133 | const clang::Type* CT = GetCanonicalType(T); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 134 | const ElementInfo* EI = nullptr; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 135 | |
Shih-wei Liao | 91a3783 | 2010-10-03 19:11:51 -0700 | [diff] [blame] | 136 | // Note: RS element like rs_pixel_rgb elements are either in the type of |
| 137 | // primitive or vector. |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 138 | if ((CT->getTypeClass() != clang::Type::Builtin) && |
Zonr Chang | 92b344a | 2010-10-05 20:39:03 +0800 | [diff] [blame] | 139 | (CT->getTypeClass() != clang::Type::ExtVector)) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 140 | return RSExportType::Create(Context, T); |
| 141 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 142 | |
Shih-wei Liao | 91a3783 | 2010-10-03 19:11:51 -0700 | [diff] [blame] | 143 | // Following the typedef chain to see whether it's an element name like |
| 144 | // rs_pixel_rgb or its alias (via typedef). |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 145 | while (T != CT) { |
| 146 | if (T->getTypeClass() != clang::Type::Typedef) { |
| 147 | break; |
Shih-wei Liao | f8149d9 | 2010-08-22 05:32:02 -0700 | [diff] [blame] | 148 | } else { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 149 | const clang::TypedefType *TT = static_cast<const clang::TypedefType*>(T); |
Shih-wei Liao | 83f0c62 | 2011-06-21 05:34:53 -0700 | [diff] [blame] | 150 | const clang::TypedefNameDecl *TD = TT->getDecl(); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 151 | EI = GetElementInfo(TD->getName()); |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 152 | if (EI != nullptr) |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 153 | break; |
| 154 | |
| 155 | T = TD->getUnderlyingType().getTypePtr(); |
Shih-wei Liao | f8149d9 | 2010-08-22 05:32:02 -0700 | [diff] [blame] | 156 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 159 | if (EI == nullptr) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 160 | return RSExportType::Create(Context, T); |
| 161 | } else { |
| 162 | return RSExportElement::Create(Context, T, EI); |
| 163 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 166 | const RSExportElement::ElementInfo * |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 167 | RSExportElement::GetElementInfo(const llvm::StringRef &Name) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 168 | if (!Initialized) |
| 169 | Init(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 170 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 171 | ElementInfoMapTy::const_iterator I = ElementInfoMap.find(Name); |
| 172 | if (I == ElementInfoMap.end()) |
Chris Wailes | 5abbe0e | 2014-08-12 15:58:29 -0700 | [diff] [blame] | 173 | return nullptr; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 174 | else |
| 175 | return I->getValue(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 176 | } |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 177 | |
| 178 | } // namespace slang |