blob: 78675be39cbcc04114621864fa0932620f1801f6 [file] [log] [blame]
Zonr Changc383a502010-10-12 01:52:08 +08001/*
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
zonr6315f762010-10-05 15:35:14 +080017#include "slang_rs_export_element.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070018
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070019#include "clang/AST/Decl.h"
20#include "clang/AST/Type.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070021
Stephen Hinese639eb52010-11-08 19:27:20 -080022#include "clang/Basic/SourceLocation.h"
23#include "clang/Basic/IdentifierTable.h"
24
zonr6315f762010-10-05 15:35:14 +080025#include "slang_rs_context.h"
26#include "slang_rs_export_type.h"
27
Stephen Hinese639eb52010-11-08 19:27:20 -080028namespace slang {
Shih-wei Liao462aefd2010-06-04 15:32:04 -070029
30bool RSExportElement::Initialized = false;
31RSExportElement::ElementInfoMapTy RSExportElement::ElementInfoMap;
32
33void RSExportElement::Init() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070034 if (!Initialized) {
35 // Initialize ElementInfoMap
Zonr Changa65ec162010-10-17 01:53:05 +080036#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 Liao9ef2f782010-10-01 12:31:37 -070051 }
Zonr Changa65ec162010-10-17 01:53:05 +080052#include "RSDataElementEnums.inc"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070053
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070054 Initialized = true;
55 }
56 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070057}
58
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070059RSExportType *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 Liao462aefd2010-06-04 15:32:04 -070063
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070064 llvm::StringRef TypeName;
65 RSExportType *ET = NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070066
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070067 if (!Initialized)
68 Init();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070069
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070070 assert(EI != NULL && "Element info not found");
Shih-wei Liao462aefd2010-06-04 15:32:04 -070071
Stephen Hinesdd6206b2010-12-09 19:39:22 -080072 if (!RSExportType::NormalizeType(T, TypeName, NULL, NULL, NULL))
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070073 return NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070074
zonr6315f762010-10-05 15:35:14 +080075 switch (T->getTypeClass()) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070076 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 Liaof8149d92010-08-22 05:32:02 -070090 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070091 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 Liao9ef2f782010-10-01 12:31:37 -0700109 default: {
zonr6315f762010-10-05 15:35:14 +0800110 // TODO(zonr): warn that type is not exportable
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700111 fprintf(stderr, "RSExportElement::Create : type '%s' is not exportable\n",
112 T->getTypeClassName());
113 break;
114 }
115 }
116
117 return ET;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700118}
119
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700120RSExportType *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 Liao462aefd2010-06-04 15:32:04 -0700125
Shih-wei Liao91a37832010-10-03 19:11:51 -0700126 // Note: RS element like rs_pixel_rgb elements are either in the type of
127 // primitive or vector.
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700128 if ((CT->getTypeClass() != clang::Type::Builtin) &&
Zonr Chang92b344a2010-10-05 20:39:03 +0800129 (CT->getTypeClass() != clang::Type::ExtVector)) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700130 return RSExportType::Create(Context, T);
131 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700132
Shih-wei Liao91a37832010-10-03 19:11:51 -0700133 // Following the typedef chain to see whether it's an element name like
134 // rs_pixel_rgb or its alias (via typedef).
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700135 while (T != CT) {
136 if (T->getTypeClass() != clang::Type::Typedef) {
137 break;
Shih-wei Liaof8149d92010-08-22 05:32:02 -0700138 } else {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700139 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 Liaof8149d92010-08-22 05:32:02 -0700146 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700147 }
148
149 if (EI == NULL) {
150 return RSExportType::Create(Context, T);
151 } else {
152 return RSExportElement::Create(Context, T, EI);
153 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700154}
155
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700156const RSExportElement::ElementInfo *
zonr6315f762010-10-05 15:35:14 +0800157RSExportElement::GetElementInfo(const llvm::StringRef &Name) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700158 if (!Initialized)
159 Init();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700160
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700161 ElementInfoMapTy::const_iterator I = ElementInfoMap.find(Name);
162 if (I == ElementInfoMap.end())
163 return NULL;
164 else
165 return I->getValue();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700166}
Stephen Hinese639eb52010-11-08 19:27:20 -0800167
168} // namespace slang