blob: 2d83652d6e425dc4d9b47107bf556eb616600de1 [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
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
Zonr Changa65ec162010-10-17 01:53:05 +080037#define ENUM_RS_DATA_ELEMENT(_name, _dk, _dt, _norm, _vsize) \
38 { \
39 ElementInfo *EI = new ElementInfo; \
40 EI->kind = RSExportPrimitiveType::DataKind ## _dk; \
41 EI->type = RSExportPrimitiveType::DataType ## _dt; \
42 EI->normalized = _norm; \
43 EI->vsize = _vsize; \
44 \
45 llvm::StringRef Name(_name); \
46 ElementInfoMap.insert( \
47 ElementInfoMapTy::value_type::Create( \
48 Name.begin(), \
49 Name.end(), \
50 ElementInfoMap.getAllocator(), \
51 EI)); \
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070052 }
Zonr Changa65ec162010-10-17 01:53:05 +080053#include "RSDataElementEnums.inc"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070054
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070055 Initialized = true;
56 }
57 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070058}
59
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070060RSExportType *RSExportElement::Create(RSContext *Context,
61 const clang::Type *T,
62 const ElementInfo *EI) {
63 // Create RSExportType corresponded to the @T first and then verify
Shih-wei Liao462aefd2010-06-04 15:32:04 -070064
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070065 llvm::StringRef TypeName;
66 RSExportType *ET = NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070067
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070068 if (!Initialized)
69 Init();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070070
Stephen Hines6e6578a2011-02-07 18:05:48 -080071 slangAssert(EI != NULL && "Element info not found");
Shih-wei Liao462aefd2010-06-04 15:32:04 -070072
Stephen Hines78e69cb2011-04-22 15:03:19 -070073 if (!RSExportType::NormalizeType(T, TypeName, Context->getDiagnostics(),
74 NULL))
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070075 return NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070076
zonr6315f762010-10-05 15:35:14 +080077 switch (T->getTypeClass()) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070078 case clang::Type::Builtin:
79 case clang::Type::Pointer: {
Stephen Hines6e6578a2011-02-07 18:05:48 -080080 slangAssert(EI->vsize == 1 && "Element not a primitive class (please "
81 "check your macro)");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070082 RSExportPrimitiveType *EPT =
83 RSExportPrimitiveType::Create(Context,
84 T,
85 TypeName,
86 EI->kind,
87 EI->normalized);
88 // Verify
Stephen Hines6e6578a2011-02-07 18:05:48 -080089 slangAssert(EI->type == EPT->getType() && "Element has unexpected type");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070090 ET = EPT;
91 break;
Shih-wei Liaof8149d92010-08-22 05:32:02 -070092 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070093 case clang::Type::ExtVector: {
Stephen Hines6e6578a2011-02-07 18:05:48 -080094 slangAssert(EI->vsize > 1 && "Element not a vector class (please check "
95 "your macro)");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070096 RSExportVectorType *EVT =
97 RSExportVectorType::Create(Context,
Loganbe274822011-02-16 22:02:54 +080098 static_cast<const clang::ExtVectorType*>(
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070099 T->getCanonicalTypeInternal()
100 .getTypePtr()),
101 TypeName,
102 EI->kind,
103 EI->normalized);
104 // Verify
Stephen Hines6e6578a2011-02-07 18:05:48 -0800105 slangAssert(EI->type == EVT->getType() && "Element has unexpected type");
106 slangAssert(EI->vsize == EVT->getNumElement() && "Element has unexpected "
107 "size of vector");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700108 ET = EVT;
109 break;
110 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700111 default: {
zonr6315f762010-10-05 15:35:14 +0800112 // TODO(zonr): warn that type is not exportable
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700113 fprintf(stderr, "RSExportElement::Create : type '%s' is not exportable\n",
114 T->getTypeClassName());
115 break;
116 }
117 }
118
119 return ET;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700120}
121
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700122RSExportType *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 Liao462aefd2010-06-04 15:32:04 -0700127
Shih-wei Liao91a37832010-10-03 19:11:51 -0700128 // Note: RS element like rs_pixel_rgb elements are either in the type of
129 // primitive or vector.
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700130 if ((CT->getTypeClass() != clang::Type::Builtin) &&
Zonr Chang92b344a2010-10-05 20:39:03 +0800131 (CT->getTypeClass() != clang::Type::ExtVector)) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700132 return RSExportType::Create(Context, T);
133 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700134
Shih-wei Liao91a37832010-10-03 19:11:51 -0700135 // Following the typedef chain to see whether it's an element name like
136 // rs_pixel_rgb or its alias (via typedef).
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700137 while (T != CT) {
138 if (T->getTypeClass() != clang::Type::Typedef) {
139 break;
Shih-wei Liaof8149d92010-08-22 05:32:02 -0700140 } else {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700141 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 Liaof8149d92010-08-22 05:32:02 -0700148 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700149 }
150
151 if (EI == NULL) {
152 return RSExportType::Create(Context, T);
153 } else {
154 return RSExportElement::Create(Context, T, EI);
155 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700156}
157
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700158const RSExportElement::ElementInfo *
zonr6315f762010-10-05 15:35:14 +0800159RSExportElement::GetElementInfo(const llvm::StringRef &Name) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700160 if (!Initialized)
161 Init();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700162
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700163 ElementInfoMapTy::const_iterator I = ElementInfoMap.find(Name);
164 if (I == ElementInfoMap.end())
165 return NULL;
166 else
167 return I->getValue();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700168}
Stephen Hinese639eb52010-11-08 19:27:20 -0800169
170} // namespace slang