blob: 0d9e614ac05c5329f2c15d9e43fb137ff9508629 [file] [log] [blame]
Zonr Changc383a502010-10-12 01:52:08 +08001/*
Stephen Hinesd369cda2012-02-13 12:00:03 -08002 * Copyright 2010-2012, The Android Open Source Project
Zonr Changc383a502010-10-12 01:52:08 +08003 *
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_var.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070018
Shih-wei Liaoe37f7d32012-08-03 01:52:45 -070019#include "clang/AST/ASTContext.h"
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070020#include "clang/AST/Type.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070021
Stephen Hinese639eb52010-11-08 19:27:20 -080022#include "llvm/ADT/APSInt.h"
23
zonr6315f762010-10-05 15:35:14 +080024#include "slang_rs_context.h"
25#include "slang_rs_export_type.h"
26
Stephen Hinese639eb52010-11-08 19:27:20 -080027namespace slang {
Shih-wei Liao462aefd2010-06-04 15:32:04 -070028
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070029RSExportVar::RSExportVar(RSContext *Context,
30 const clang::VarDecl *VD,
zonr6315f762010-10-05 15:35:14 +080031 const RSExportType *ET)
Zonr Changa41ce1d2010-10-06 02:23:12 +080032 : RSExportable(Context, RSExportable::EX_VAR),
zonr6315f762010-10-05 15:35:14 +080033 mName(VD->getName().data(), VD->getName().size()),
34 mET(ET),
Stephen Hinesd369cda2012-02-13 12:00:03 -080035 mIsConst(false),
Stephen Hines1f6c3312012-07-03 17:23:33 -070036 mIsUnsigned(false),
Stephen Hinesd369cda2012-02-13 12:00:03 -080037 mArraySize(0),
38 mNumInits(0) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070039 // mInit - Evaluate initializer expression
40 const clang::Expr *Initializer = VD->getAnyInitializer();
Chris Wailes5abbe0e2014-08-12 15:58:29 -070041 if (Initializer != nullptr) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070042 switch (ET->getClass()) {
Shih-wei Liao324c0472010-06-21 13:15:11 -070043 case RSExportType::ExportClassPrimitive:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070044 case RSExportType::ExportClassVector: {
Stephen Hines4c622e02011-11-10 18:57:34 -080045 Initializer->EvaluateAsRValue(mInit, Context->getASTContext());
Shih-wei Liao324c0472010-06-21 13:15:11 -070046 break;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070047 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070048 case RSExportType::ExportClassPointer: {
Stephen Hinesd369cda2012-02-13 12:00:03 -080049 if (Initializer->isNullPointerConstant(Context->getASTContext(),
50 clang::Expr::NPC_ValueDependentIsNotNull)) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070051 mInit.Val = clang::APValue(llvm::APSInt(1));
Stephen Hinesd369cda2012-02-13 12:00:03 -080052 } else {
53 if (!Initializer->EvaluateAsRValue(mInit, Context->getASTContext())) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -080054 Context->ReportError(Initializer->getExprLoc(),
55 "initializer is not an R-value");
Stephen Hinesd369cda2012-02-13 12:00:03 -080056 }
57 }
Shih-wei Liao324c0472010-06-21 13:15:11 -070058 break;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070059 }
Stephen Hinesd369cda2012-02-13 12:00:03 -080060 case RSExportType::ExportClassConstantArray: {
Stephen Hinese67239d2012-02-24 15:08:36 -080061 const clang::InitListExpr *IList =
Stephen Hinesd369cda2012-02-13 12:00:03 -080062 static_cast<const clang::InitListExpr*>(Initializer);
63 if (!IList) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -080064 Context->ReportError(VD->getLocation(),
65 "Unable to find initializer list");
Stephen Hinesd369cda2012-02-13 12:00:03 -080066 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 Brouilletd3f75272014-01-16 18:20:28 -080076 Context->ReportError(IList->getInit(i)->getExprLoc(),
77 "initializer is not an R-value");
Stephen Hinesd369cda2012-02-13 12:00:03 -080078 }
79 mInitArray.push_back(tempInit);
80 }
81 break;
82 }
83 case RSExportType::ExportClassMatrix:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070084 case RSExportType::ExportClassRecord: {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -080085 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 Liao324c0472010-06-21 13:15:11 -070090 break;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070091 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070092 default: {
Stephen Hines6e6578a2011-02-07 18:05:48 -080093 slangAssert(false && "Unknown class of type");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070094 }
Shih-wei Liao324c0472010-06-21 13:15:11 -070095 }
Shih-wei Liao81c1b482010-07-19 15:17:14 -070096 }
Shih-wei Liao324c0472010-06-21 13:15:11 -070097
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070098 clang::QualType QT = VD->getTypeSourceInfo()->getType();
Shih-wei Liao81c1b482010-07-19 15:17:14 -070099 if (!QT.isNull()) {
100 mIsConst = QT.isConstQualified();
Stephen Hines1f6c3312012-07-03 17:23:33 -0700101 mIsUnsigned = QT->hasUnsignedIntegerRepresentation();
102 if (QT == Context->getASTContext().BoolTy) {
103 mIsUnsigned = false;
104 }
Shih-wei Liao81c1b482010-07-19 15:17:14 -0700105 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700106}
Stephen Hinese639eb52010-11-08 19:27:20 -0800107
108} // namespace slang