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