Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 1 | /* |
Stephen Hines | d369cda | 2012-02-13 12:00:03 -0800 | [diff] [blame^] | 2 | * Copyright 2010-2012, The Android Open Source Project |
Zonr Chang | c383a50 | 2010-10-12 01:52:08 +0800 | [diff] [blame] | 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 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 17 | #include "slang_rs_export_var.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 18 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 19 | #include "clang/AST/Type.h" |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 20 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 21 | #include "llvm/ADT/APSInt.h" |
| 22 | |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 23 | #include "slang_rs_context.h" |
| 24 | #include "slang_rs_export_type.h" |
| 25 | |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 26 | namespace slang { |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 27 | |
Stephen Hines | d369cda | 2012-02-13 12:00:03 -0800 | [diff] [blame^] | 28 | namespace { |
| 29 | |
| 30 | static clang::DiagnosticBuilder ReportVarError(RSContext *Context, |
| 31 | const clang::SourceLocation Loc, |
| 32 | const char *Message) { |
| 33 | clang::DiagnosticsEngine *DiagEngine = Context->getDiagnostics(); |
| 34 | const clang::SourceManager *SM = Context->getSourceManager(); |
| 35 | return DiagEngine->Report(clang::FullSourceLoc(Loc, *SM), |
| 36 | DiagEngine->getCustomDiagID(clang::DiagnosticsEngine::Error, Message)); |
| 37 | } |
| 38 | |
| 39 | } // namespace |
| 40 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 41 | RSExportVar::RSExportVar(RSContext *Context, |
| 42 | const clang::VarDecl *VD, |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 43 | const RSExportType *ET) |
Zonr Chang | a41ce1d | 2010-10-06 02:23:12 +0800 | [diff] [blame] | 44 | : RSExportable(Context, RSExportable::EX_VAR), |
zonr | 6315f76 | 2010-10-05 15:35:14 +0800 | [diff] [blame] | 45 | mName(VD->getName().data(), VD->getName().size()), |
| 46 | mET(ET), |
Stephen Hines | d369cda | 2012-02-13 12:00:03 -0800 | [diff] [blame^] | 47 | mIsConst(false), |
| 48 | mArraySize(0), |
| 49 | mNumInits(0) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 50 | // mInit - Evaluate initializer expression |
| 51 | const clang::Expr *Initializer = VD->getAnyInitializer(); |
| 52 | if (Initializer != NULL) { |
| 53 | switch (ET->getClass()) { |
Shih-wei Liao | 324c047 | 2010-06-21 13:15:11 -0700 | [diff] [blame] | 54 | case RSExportType::ExportClassPrimitive: |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 55 | case RSExportType::ExportClassVector: { |
Stephen Hines | 4c622e0 | 2011-11-10 18:57:34 -0800 | [diff] [blame] | 56 | Initializer->EvaluateAsRValue(mInit, Context->getASTContext()); |
Shih-wei Liao | 324c047 | 2010-06-21 13:15:11 -0700 | [diff] [blame] | 57 | break; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 58 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 59 | case RSExportType::ExportClassPointer: { |
Stephen Hines | d369cda | 2012-02-13 12:00:03 -0800 | [diff] [blame^] | 60 | if (Initializer->isNullPointerConstant(Context->getASTContext(), |
| 61 | clang::Expr::NPC_ValueDependentIsNotNull)) { |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 62 | mInit.Val = clang::APValue(llvm::APSInt(1)); |
Stephen Hines | d369cda | 2012-02-13 12:00:03 -0800 | [diff] [blame^] | 63 | } else { |
| 64 | if (!Initializer->EvaluateAsRValue(mInit, Context->getASTContext())) { |
| 65 | ReportVarError(Context, Initializer->getExprLoc(), |
| 66 | "initializer is not an R-value"); |
| 67 | } |
| 68 | } |
Shih-wei Liao | 324c047 | 2010-06-21 13:15:11 -0700 | [diff] [blame] | 69 | break; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 70 | } |
Stephen Hines | d369cda | 2012-02-13 12:00:03 -0800 | [diff] [blame^] | 71 | case RSExportType::ExportClassConstantArray: { |
| 72 | const clang::InitListExpr *IList = |
| 73 | static_cast<const clang::InitListExpr*>(Initializer); |
| 74 | if (!IList) { |
| 75 | ReportVarError(Context, VD->getLocation(), |
| 76 | "Unable to find initializer list"); |
| 77 | break; |
| 78 | } |
| 79 | const RSExportConstantArrayType *ECAT = |
| 80 | static_cast<const RSExportConstantArrayType*>(ET); |
| 81 | mArraySize = ECAT->getSize(); |
| 82 | mNumInits = IList->getNumInits(); |
| 83 | for (unsigned int i = 0; i < mNumInits; i++) { |
| 84 | clang::Expr::EvalResult tempInit; |
| 85 | if (!IList->getInit(i)->EvaluateAsRValue(tempInit, |
| 86 | Context->getASTContext())) { |
| 87 | ReportVarError(Context, IList->getInit(i)->getExprLoc(), |
| 88 | "initializer is not an R-value"); |
| 89 | } |
| 90 | mInitArray.push_back(tempInit); |
| 91 | } |
| 92 | break; |
| 93 | } |
| 94 | case RSExportType::ExportClassMatrix: |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 95 | case RSExportType::ExportClassRecord: { |
Stephen Hines | d369cda | 2012-02-13 12:00:03 -0800 | [diff] [blame^] | 96 | ReportVarError(Context, VD->getLocation(), |
| 97 | "Reflection of initializer to variable '%0' (of type " |
| 98 | "'%1') is unsupported currently.") |
| 99 | << mName |
| 100 | << ET->getName(); |
Shih-wei Liao | 324c047 | 2010-06-21 13:15:11 -0700 | [diff] [blame] | 101 | break; |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 102 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 103 | default: { |
Stephen Hines | 6e6578a | 2011-02-07 18:05:48 -0800 | [diff] [blame] | 104 | slangAssert(false && "Unknown class of type"); |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 105 | } |
Shih-wei Liao | 324c047 | 2010-06-21 13:15:11 -0700 | [diff] [blame] | 106 | } |
Shih-wei Liao | 81c1b48 | 2010-07-19 15:17:14 -0700 | [diff] [blame] | 107 | } |
Shih-wei Liao | 324c047 | 2010-06-21 13:15:11 -0700 | [diff] [blame] | 108 | |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 109 | // mIsConst - Is it a constant? |
| 110 | clang::QualType QT = VD->getTypeSourceInfo()->getType(); |
Shih-wei Liao | 81c1b48 | 2010-07-19 15:17:14 -0700 | [diff] [blame] | 111 | if (!QT.isNull()) { |
| 112 | mIsConst = QT.isConstQualified(); |
| 113 | } |
Shih-wei Liao | 9ef2f78 | 2010-10-01 12:31:37 -0700 | [diff] [blame] | 114 | |
Shih-wei Liao | 324c047 | 2010-06-21 13:15:11 -0700 | [diff] [blame] | 115 | return; |
Shih-wei Liao | 462aefd | 2010-06-04 15:32:04 -0700 | [diff] [blame] | 116 | } |
Stephen Hines | e639eb5 | 2010-11-08 19:27:20 -0800 | [diff] [blame] | 117 | |
| 118 | } // namespace slang |