Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 1 | #include "slang_rs_context.hpp" |
| 2 | #include "slang_rs_export_type.hpp" |
| 3 | #include "slang_rs_export_element.hpp" |
| 4 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 5 | #include "clang/Basic/SourceLocation.h" |
| 6 | #include "clang/Basic/IdentifierTable.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 7 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 8 | #include "clang/AST/Decl.h" |
| 9 | #include "clang/AST/Type.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 10 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 11 | using namespace slang; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 12 | |
| 13 | bool RSExportElement::Initialized = false; |
| 14 | RSExportElement::ElementInfoMapTy RSExportElement::ElementInfoMap; |
| 15 | |
| 16 | void RSExportElement::Init() { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 17 | if (!Initialized) { |
| 18 | // Initialize ElementInfoMap |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 19 | #define USE_ELEMENT_DATA_TYPE |
| 20 | #define USE_ELEMENT_DATA_KIND |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 21 | #define DEF_ELEMENT(_name, _dk, _dt, _norm, _vsize) \ |
| 22 | { \ |
| 23 | ElementInfo *EI = new ElementInfo; \ |
| 24 | EI->kind = GET_ELEMENT_DATA_KIND(_dk); \ |
| 25 | EI->type = GET_ELEMENT_DATA_TYPE(_dt); \ |
| 26 | EI->normalized = _norm; \ |
| 27 | EI->vsize = _vsize; \ |
| 28 | \ |
| 29 | llvm::StringRef Name(_name); \ |
| 30 | ElementInfoMap.insert( \ |
| 31 | ElementInfoMapTy::value_type::Create( \ |
| 32 | Name.begin(), \ |
| 33 | Name.end(), \ |
| 34 | ElementInfoMap.getAllocator(), \ |
| 35 | EI \ |
| 36 | )); \ |
| 37 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 38 | #include "slang_rs_export_element_support.inc" |
| 39 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 40 | Initialized = true; |
| 41 | } |
| 42 | return; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 45 | RSExportType *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 Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 49 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 50 | llvm::StringRef TypeName; |
| 51 | RSExportType *ET = NULL; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 52 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 53 | if (!Initialized) |
| 54 | Init(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 55 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 56 | assert(EI != NULL && "Element info not found"); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 57 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 58 | if (!RSExportType::NormalizeType(T, TypeName)) |
| 59 | return NULL; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 60 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 61 | switch(T->getTypeClass()) { |
| 62 | 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 Liao | f8149d9 | 2010-08-22 05:32:02 -0700 | [diff] [blame] | 76 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 77 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 78 | case clang::Type::ConstantArray: { |
| 79 | //XXX |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | case clang::Type::ExtVector: { |
| 84 | assert(EI->vsize > 1 && "Element not a vector class (please check your " |
| 85 | "macro)"); |
| 86 | RSExportVectorType *EVT = |
| 87 | RSExportVectorType::Create(Context, |
| 88 | static_cast<clang::ExtVectorType*>( |
| 89 | T->getCanonicalTypeInternal() |
| 90 | .getTypePtr()), |
| 91 | TypeName, |
| 92 | EI->kind, |
| 93 | EI->normalized); |
| 94 | // Verify |
| 95 | assert(EI->type == EVT->getType() && "Element has unexpected type"); |
| 96 | assert(EI->vsize == EVT->getNumElement() && "Element has unexpected size " |
| 97 | "of vector"); |
| 98 | ET = EVT; |
| 99 | break; |
| 100 | } |
| 101 | |
| 102 | case clang::Type::Record: { |
| 103 | // Must be RS object type |
| 104 | |
| 105 | if ( TypeName.equals(llvm::StringRef("rs_matrix2x2")) || |
| 106 | TypeName.equals(llvm::StringRef("rs_matrix3x3")) || |
| 107 | TypeName.equals(llvm::StringRef("rs_matrix4x4")) ) { |
| 108 | |
| 109 | const clang::RecordType *RT = static_cast<const clang::RecordType*> (T); |
| 110 | const clang::RecordDecl *RD = RT->getDecl(); |
| 111 | RD = RD->getDefinition(); |
| 112 | clang::RecordDecl::field_iterator fit = RD->field_begin(); |
| 113 | clang::FieldDecl *FD = *fit; |
| 114 | const clang::Type *FT = RSExportType::GetTypeOfDecl(FD); |
| 115 | RSExportConstantArrayType *ECT = |
| 116 | RSExportConstantArrayType::Create( |
| 117 | Context, |
| 118 | static_cast<const clang::ConstantArrayType*> (FT), |
| 119 | TypeName |
| 120 | ); |
| 121 | ET = ECT; |
| 122 | } else { |
| 123 | RSExportPrimitiveType* EPT = |
| 124 | RSExportPrimitiveType::Create(Context, |
| 125 | T, |
| 126 | TypeName, |
| 127 | EI->kind, |
| 128 | EI->normalized); |
| 129 | // Verify |
| 130 | assert(EI->type == EPT->getType() && "Element has unexpected type"); |
| 131 | ET = EPT; |
| 132 | } |
| 133 | break; |
| 134 | } |
| 135 | |
| 136 | default: { |
| 137 | // TODO: warning: type is not exportable |
| 138 | fprintf(stderr, "RSExportElement::Create : type '%s' is not exportable\n", |
| 139 | T->getTypeClassName()); |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return ET; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 147 | RSExportType *RSExportElement::CreateFromDecl(RSContext *Context, |
| 148 | const clang::DeclaratorDecl *DD) { |
| 149 | const clang::Type* T = RSExportType::GetTypeOfDecl(DD); |
| 150 | const clang::Type* CT = GET_CANONICAL_TYPE(T); |
| 151 | const ElementInfo* EI = NULL; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 152 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 153 | // For rs element that's NOT like those rs_color4f..., just call |
| 154 | // Create(Context, T) without finding EI. |
| 155 | // Note: Those rs_color4f kind of elements are either typed primitive or |
| 156 | // vector |
| 157 | if ((CT->getTypeClass() != clang::Type::Builtin) && |
| 158 | (CT->getTypeClass() != clang::Type::ExtVector) && |
| 159 | (CT->getTypeClass() != clang::Type::Record)) { |
| 160 | return RSExportType::Create(Context, T); |
| 161 | } |
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 | // Iterative query the name of type to see whether it's an element name |
| 164 | // like rs_color4f or its alias (via typedef). |
| 165 | while (T != CT) { |
| 166 | if (T->getTypeClass() != clang::Type::Typedef) { |
| 167 | break; |
Shih-wei Liao | f8149d9 | 2010-08-22 05:32:02 -0700 | [diff] [blame] | 168 | } else { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 169 | const clang::TypedefType *TT = static_cast<const clang::TypedefType*>(T); |
| 170 | const clang::TypedefDecl *TD = TT->getDecl(); |
| 171 | EI = GetElementInfo(TD->getName()); |
| 172 | if (EI != NULL) |
| 173 | break; |
| 174 | |
| 175 | T = TD->getUnderlyingType().getTypePtr(); |
Shih-wei Liao | f8149d9 | 2010-08-22 05:32:02 -0700 | [diff] [blame] | 176 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 177 | } |
| 178 | |
| 179 | if (EI == NULL) { |
| 180 | return RSExportType::Create(Context, T); |
| 181 | } else { |
| 182 | return RSExportElement::Create(Context, T, EI); |
| 183 | } |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 186 | const RSExportElement::ElementInfo * |
| 187 | RSExportElement::GetElementInfo(const llvm::StringRef& Name) { |
| 188 | if (!Initialized) |
| 189 | Init(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 190 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame^] | 191 | ElementInfoMapTy::const_iterator I = ElementInfoMap.find(Name); |
| 192 | if (I == ElementInfoMap.end()) |
| 193 | return NULL; |
| 194 | else |
| 195 | return I->getValue(); |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 196 | } |