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