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/Basic/SourceLocation.h" |
| 20 | #include "clang/Basic/IdentifierTable.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 21 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 22 | #include "clang/AST/Decl.h" |
| 23 | #include "clang/AST/Type.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 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 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 28 | using 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 |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 36 | #define USE_ELEMENT_DATA_TYPE |
| 37 | #define USE_ELEMENT_DATA_KIND |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 38 | #define DEF_ELEMENT(_name, _dk, _dt, _norm, _vsize) \ |
| 39 | { \ |
| 40 | ElementInfo *EI = new ElementInfo; \ |
| 41 | EI->kind = GET_ELEMENT_DATA_KIND(_dk); \ |
| 42 | EI->type = GET_ELEMENT_DATA_TYPE(_dt); \ |
| 43 | EI->normalized = _norm; \ |
| 44 | EI->vsize = _vsize; \ |
| 45 | \ |
| 46 | llvm::StringRef Name(_name); \ |
| 47 | ElementInfoMap.insert( \ |
| 48 | ElementInfoMapTy::value_type::Create( \ |
| 49 | Name.begin(), \ |
| 50 | Name.end(), \ |
| 51 | ElementInfoMap.getAllocator(), \ |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 52 | EI)); \ |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 53 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 54 | #include "slang_rs_export_element_support.inc" |
| 55 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 56 | Initialized = true; |
| 57 | } |
| 58 | return; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 61 | RSExportType *RSExportElement::Create(RSContext *Context, |
| 62 | const clang::Type *T, |
| 63 | const ElementInfo *EI) { |
| 64 | // Create RSExportType corresponded to the @T first and then verify |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 65 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 66 | llvm::StringRef TypeName; |
| 67 | RSExportType *ET = NULL; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 68 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 69 | if (!Initialized) |
| 70 | Init(); |
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 | assert(EI != NULL && "Element info not found"); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 73 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 74 | if (!RSExportType::NormalizeType(T, TypeName)) |
| 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: { |
| 80 | assert(EI->vsize == 1 && "Element not a primitive class (please check " |
| 81 | "your macro)"); |
| 82 | RSExportPrimitiveType *EPT = |
| 83 | RSExportPrimitiveType::Create(Context, |
| 84 | T, |
| 85 | TypeName, |
| 86 | EI->kind, |
| 87 | EI->normalized); |
| 88 | // Verify |
| 89 | assert(EI->type == EPT->getType() && "Element has unexpected type"); |
| 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: { |
| 94 | assert(EI->vsize > 1 && "Element not a vector class (please check your " |
| 95 | "macro)"); |
| 96 | RSExportVectorType *EVT = |
| 97 | RSExportVectorType::Create(Context, |
| 98 | static_cast<clang::ExtVectorType*>( |
| 99 | T->getCanonicalTypeInternal() |
| 100 | .getTypePtr()), |
| 101 | TypeName, |
| 102 | EI->kind, |
| 103 | EI->normalized); |
| 104 | // Verify |
| 105 | assert(EI->type == EVT->getType() && "Element has unexpected type"); |
| 106 | assert(EI->vsize == EVT->getNumElement() && "Element has unexpected size " |
| 107 | "of vector"); |
| 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); |
| 142 | const clang::TypedefDecl *TD = TT->getDecl(); |
| 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 | } |