blob: c1b9c3c5ae56971933a8a4c61345df072c4fad57 [file] [log] [blame]
Zonr Changc383a502010-10-12 01:52:08 +08001/*
Stephen Hines5bfec8d2012-04-04 08:18:57 -07002 * Copyright 2010, The Android Open Source Project
Zonr Changc383a502010-10-12 01:52:08 +08003 *
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
Stephen Hines6e6578a2011-02-07 18:05:48 -080025#include "slang_assert.h"
zonr6315f762010-10-05 15:35:14 +080026#include "slang_rs_context.h"
27#include "slang_rs_export_type.h"
28
Stephen Hinese639eb52010-11-08 19:27:20 -080029namespace slang {
Shih-wei Liao462aefd2010-06-04 15:32:04 -070030
31bool RSExportElement::Initialized = false;
32RSExportElement::ElementInfoMapTy RSExportElement::ElementInfoMap;
33
34void RSExportElement::Init() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070035 if (!Initialized) {
36 // Initialize ElementInfoMap
Stephen Hines2b8fb642012-03-09 00:12:47 -080037#define ENUM_RS_DATA_ELEMENT(_name, _dt, _norm, _vsize) \
Zonr Changa65ec162010-10-17 01:53:05 +080038 { \
39 ElementInfo *EI = new ElementInfo; \
Zonr Changa65ec162010-10-17 01:53:05 +080040 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
Stephen Hines6e6578a2011-02-07 18:05:48 -080070 slangAssert(EI != NULL && "Element info not found");
Shih-wei Liao462aefd2010-06-04 15:32:04 -070071
Stephen Hines78e69cb2011-04-22 15:03:19 -070072 if (!RSExportType::NormalizeType(T, TypeName, Context->getDiagnostics(),
Stephen Hines5bfec8d2012-04-04 08:18:57 -070073 NULL))
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070074 return NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070075
zonr6315f762010-10-05 15:35:14 +080076 switch (T->getTypeClass()) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070077 case clang::Type::Builtin:
78 case clang::Type::Pointer: {
Stephen Hines6e6578a2011-02-07 18:05:48 -080079 slangAssert(EI->vsize == 1 && "Element not a primitive class (please "
80 "check your macro)");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070081 RSExportPrimitiveType *EPT =
82 RSExportPrimitiveType::Create(Context,
83 T,
84 TypeName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070085 EI->normalized);
86 // Verify
Stephen Hines6e6578a2011-02-07 18:05:48 -080087 slangAssert(EI->type == EPT->getType() && "Element has unexpected type");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070088 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: {
Stephen Hines6e6578a2011-02-07 18:05:48 -080092 slangAssert(EI->vsize > 1 && "Element not a vector class (please check "
93 "your macro)");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070094 RSExportVectorType *EVT =
95 RSExportVectorType::Create(Context,
Loganbe274822011-02-16 22:02:54 +080096 static_cast<const clang::ExtVectorType*>(
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070097 T->getCanonicalTypeInternal()
98 .getTypePtr()),
99 TypeName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700100 EI->normalized);
101 // Verify
Stephen Hines6e6578a2011-02-07 18:05:48 -0800102 slangAssert(EI->type == EVT->getType() && "Element has unexpected type");
103 slangAssert(EI->vsize == EVT->getNumElement() && "Element has unexpected "
104 "size of vector");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700105 ET = EVT;
106 break;
107 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700108 default: {
zonr6315f762010-10-05 15:35:14 +0800109 // TODO(zonr): warn that type is not exportable
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700110 fprintf(stderr, "RSExportElement::Create : type '%s' is not exportable\n",
111 T->getTypeClassName());
112 break;
113 }
114 }
115
116 return ET;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700117}
118
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700119RSExportType *RSExportElement::CreateFromDecl(RSContext *Context,
120 const clang::DeclaratorDecl *DD) {
121 const clang::Type* T = RSExportType::GetTypeOfDecl(DD);
122 const clang::Type* CT = GET_CANONICAL_TYPE(T);
123 const ElementInfo* EI = NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700124
Shih-wei Liao91a37832010-10-03 19:11:51 -0700125 // Note: RS element like rs_pixel_rgb elements are either in the type of
126 // primitive or vector.
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700127 if ((CT->getTypeClass() != clang::Type::Builtin) &&
Zonr Chang92b344a2010-10-05 20:39:03 +0800128 (CT->getTypeClass() != clang::Type::ExtVector)) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700129 return RSExportType::Create(Context, T);
130 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700131
Shih-wei Liao91a37832010-10-03 19:11:51 -0700132 // Following the typedef chain to see whether it's an element name like
133 // rs_pixel_rgb or its alias (via typedef).
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700134 while (T != CT) {
135 if (T->getTypeClass() != clang::Type::Typedef) {
136 break;
Shih-wei Liaof8149d92010-08-22 05:32:02 -0700137 } else {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700138 const clang::TypedefType *TT = static_cast<const clang::TypedefType*>(T);
Shih-wei Liao83f0c622011-06-21 05:34:53 -0700139 const clang::TypedefNameDecl *TD = TT->getDecl();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700140 EI = GetElementInfo(TD->getName());
141 if (EI != NULL)
142 break;
143
144 T = TD->getUnderlyingType().getTypePtr();
Shih-wei Liaof8149d92010-08-22 05:32:02 -0700145 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700146 }
147
148 if (EI == NULL) {
149 return RSExportType::Create(Context, T);
150 } else {
151 return RSExportElement::Create(Context, T, EI);
152 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700153}
154
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700155const RSExportElement::ElementInfo *
zonr6315f762010-10-05 15:35:14 +0800156RSExportElement::GetElementInfo(const llvm::StringRef &Name) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700157 if (!Initialized)
158 Init();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700159
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700160 ElementInfoMapTy::const_iterator I = ElementInfoMap.find(Name);
161 if (I == ElementInfoMap.end())
162 return NULL;
163 else
164 return I->getValue();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700165}
Stephen Hinese639eb52010-11-08 19:27:20 -0800166
167} // namespace slang