blob: e64621205ebf8f9da13e4666c1d2ed7cd25b9f17 [file] [log] [blame]
zonr6315f762010-10-05 15:35:14 +08001#include "slang_rs_export_element.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07003#include "clang/Basic/SourceLocation.h"
4#include "clang/Basic/IdentifierTable.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -07005
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07006#include "clang/AST/Decl.h"
7#include "clang/AST/Type.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -07008
zonr6315f762010-10-05 15:35:14 +08009#include "slang_rs_context.h"
10#include "slang_rs_export_type.h"
11
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070012using namespace slang;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070013
14bool RSExportElement::Initialized = false;
15RSExportElement::ElementInfoMapTy RSExportElement::ElementInfoMap;
16
17void RSExportElement::Init() {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070018 if (!Initialized) {
19 // Initialize ElementInfoMap
Shih-wei Liao462aefd2010-06-04 15:32:04 -070020#define USE_ELEMENT_DATA_TYPE
21#define USE_ELEMENT_DATA_KIND
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070022#define DEF_ELEMENT(_name, _dk, _dt, _norm, _vsize) \
23 { \
24 ElementInfo *EI = new ElementInfo; \
25 EI->kind = GET_ELEMENT_DATA_KIND(_dk); \
26 EI->type = GET_ELEMENT_DATA_TYPE(_dt); \
27 EI->normalized = _norm; \
28 EI->vsize = _vsize; \
29 \
30 llvm::StringRef Name(_name); \
31 ElementInfoMap.insert( \
32 ElementInfoMapTy::value_type::Create( \
33 Name.begin(), \
34 Name.end(), \
35 ElementInfoMap.getAllocator(), \
zonr6315f762010-10-05 15:35:14 +080036 EI)); \
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070037 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -070038#include "slang_rs_export_element_support.inc"
39
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070040 Initialized = true;
41 }
42 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070043}
44
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070045RSExportType *RSExportElement::Create(RSContext *Context,
46 const clang::Type *T,
47 const ElementInfo *EI) {
48 // Create RSExportType corresponded to the @T first and then verify
Shih-wei Liao462aefd2010-06-04 15:32:04 -070049
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070050 llvm::StringRef TypeName;
51 RSExportType *ET = NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070052
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070053 if (!Initialized)
54 Init();
Shih-wei Liao462aefd2010-06-04 15:32:04 -070055
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070056 assert(EI != NULL && "Element info not found");
Shih-wei Liao462aefd2010-06-04 15:32:04 -070057
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070058 if (!RSExportType::NormalizeType(T, TypeName))
59 return NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070060
zonr6315f762010-10-05 15:35:14 +080061 switch (T->getTypeClass()) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070062 case clang::Type::Builtin:
63 case clang::Type::Pointer: {
64 assert(EI->vsize == 1 && "Element not a primitive class (please check "
65 "your macro)");
66 RSExportPrimitiveType *EPT =
67 RSExportPrimitiveType::Create(Context,
68 T,
69 TypeName,
70 EI->kind,
71 EI->normalized);
72 // Verify
73 assert(EI->type == EPT->getType() && "Element has unexpected type");
74 ET = EPT;
75 break;
Shih-wei Liaof8149d92010-08-22 05:32:02 -070076 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070077 case clang::Type::ConstantArray: {
zonr6315f762010-10-05 15:35:14 +080078 // XXX
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070079 break;
80 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070081 case clang::Type::ExtVector: {
82 assert(EI->vsize > 1 && "Element not a vector class (please check your "
83 "macro)");
84 RSExportVectorType *EVT =
85 RSExportVectorType::Create(Context,
86 static_cast<clang::ExtVectorType*>(
87 T->getCanonicalTypeInternal()
88 .getTypePtr()),
89 TypeName,
90 EI->kind,
91 EI->normalized);
92 // Verify
93 assert(EI->type == EVT->getType() && "Element has unexpected type");
94 assert(EI->vsize == EVT->getNumElement() && "Element has unexpected size "
95 "of vector");
96 ET = EVT;
97 break;
98 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070099 default: {
zonr6315f762010-10-05 15:35:14 +0800100 // TODO(zonr): warn that type is not exportable
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700101 fprintf(stderr, "RSExportElement::Create : type '%s' is not exportable\n",
102 T->getTypeClassName());
103 break;
104 }
105 }
106
107 return ET;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700108}
109
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700110RSExportType *RSExportElement::CreateFromDecl(RSContext *Context,
111 const clang::DeclaratorDecl *DD) {
112 const clang::Type* T = RSExportType::GetTypeOfDecl(DD);
113 const clang::Type* CT = GET_CANONICAL_TYPE(T);
114 const ElementInfo* EI = NULL;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700115
Shih-wei Liao91a37832010-10-03 19:11:51 -0700116 // Note: RS element like rs_pixel_rgb elements are either in the type of
117 // primitive or vector.
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700118 if ((CT->getTypeClass() != clang::Type::Builtin) &&
Zonr Chang92b344a2010-10-05 20:39:03 +0800119 (CT->getTypeClass() != clang::Type::ExtVector)) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700120 return RSExportType::Create(Context, T);
121 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700122
Shih-wei Liao91a37832010-10-03 19:11:51 -0700123 // Following the typedef chain to see whether it's an element name like
124 // rs_pixel_rgb or its alias (via typedef).
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700125 while (T != CT) {
126 if (T->getTypeClass() != clang::Type::Typedef) {
127 break;
Shih-wei Liaof8149d92010-08-22 05:32:02 -0700128 } else {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700129 const clang::TypedefType *TT = static_cast<const clang::TypedefType*>(T);
130 const clang::TypedefDecl *TD = TT->getDecl();
131 EI = GetElementInfo(TD->getName());
132 if (EI != NULL)
133 break;
134
135 T = TD->getUnderlyingType().getTypePtr();
Shih-wei Liaof8149d92010-08-22 05:32:02 -0700136 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700137 }
138
139 if (EI == NULL) {
140 return RSExportType::Create(Context, T);
141 } else {
142 return RSExportElement::Create(Context, T, EI);
143 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700144}
145
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700146const RSExportElement::ElementInfo *
zonr6315f762010-10-05 15:35:14 +0800147RSExportElement::GetElementInfo(const llvm::StringRef &Name) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700148 if (!Initialized)
149 Init();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700150
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700151 ElementInfoMapTy::const_iterator I = ElementInfoMap.find(Name);
152 if (I == ElementInfoMap.end())
153 return NULL;
154 else
155 return I->getValue();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700156}