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