blob: 4720605c544be6042fa32bf82b106b3265beac2b [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
Stephen Hinesd369cda2012-02-13 12:00:03 -080029namespace {
30
31static clang::DiagnosticBuilder ReportVarError(RSContext *Context,
32 const clang::SourceLocation Loc,
33 const char *Message) {
34 clang::DiagnosticsEngine *DiagEngine = Context->getDiagnostics();
35 const clang::SourceManager *SM = Context->getSourceManager();
36 return DiagEngine->Report(clang::FullSourceLoc(Loc, *SM),
37 DiagEngine->getCustomDiagID(clang::DiagnosticsEngine::Error, Message));
38}
39
40} // namespace
41
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070042RSExportVar::RSExportVar(RSContext *Context,
43 const clang::VarDecl *VD,
zonr6315f762010-10-05 15:35:14 +080044 const RSExportType *ET)
Zonr Changa41ce1d2010-10-06 02:23:12 +080045 : RSExportable(Context, RSExportable::EX_VAR),
zonr6315f762010-10-05 15:35:14 +080046 mName(VD->getName().data(), VD->getName().size()),
47 mET(ET),
Stephen Hinesd369cda2012-02-13 12:00:03 -080048 mIsConst(false),
Stephen Hines1f6c3312012-07-03 17:23:33 -070049 mIsUnsigned(false),
Stephen Hinesd369cda2012-02-13 12:00:03 -080050 mArraySize(0),
51 mNumInits(0) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070052 // mInit - Evaluate initializer expression
53 const clang::Expr *Initializer = VD->getAnyInitializer();
54 if (Initializer != NULL) {
55 switch (ET->getClass()) {
Shih-wei Liao324c0472010-06-21 13:15:11 -070056 case RSExportType::ExportClassPrimitive:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070057 case RSExportType::ExportClassVector: {
Stephen Hines4c622e02011-11-10 18:57:34 -080058 Initializer->EvaluateAsRValue(mInit, Context->getASTContext());
Shih-wei Liao324c0472010-06-21 13:15:11 -070059 break;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070060 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070061 case RSExportType::ExportClassPointer: {
Stephen Hinesd369cda2012-02-13 12:00:03 -080062 if (Initializer->isNullPointerConstant(Context->getASTContext(),
63 clang::Expr::NPC_ValueDependentIsNotNull)) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070064 mInit.Val = clang::APValue(llvm::APSInt(1));
Stephen Hinesd369cda2012-02-13 12:00:03 -080065 } else {
66 if (!Initializer->EvaluateAsRValue(mInit, Context->getASTContext())) {
67 ReportVarError(Context, Initializer->getExprLoc(),
68 "initializer is not an R-value");
69 }
70 }
Shih-wei Liao324c0472010-06-21 13:15:11 -070071 break;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070072 }
Stephen Hinesd369cda2012-02-13 12:00:03 -080073 case RSExportType::ExportClassConstantArray: {
Stephen Hinese67239d2012-02-24 15:08:36 -080074 const clang::InitListExpr *IList =
Stephen Hinesd369cda2012-02-13 12:00:03 -080075 static_cast<const clang::InitListExpr*>(Initializer);
76 if (!IList) {
77 ReportVarError(Context, VD->getLocation(),
78 "Unable to find initializer list");
79 break;
80 }
81 const RSExportConstantArrayType *ECAT =
82 static_cast<const RSExportConstantArrayType*>(ET);
83 mArraySize = ECAT->getSize();
84 mNumInits = IList->getNumInits();
85 for (unsigned int i = 0; i < mNumInits; i++) {
86 clang::Expr::EvalResult tempInit;
87 if (!IList->getInit(i)->EvaluateAsRValue(tempInit,
88 Context->getASTContext())) {
89 ReportVarError(Context, IList->getInit(i)->getExprLoc(),
90 "initializer is not an R-value");
91 }
92 mInitArray.push_back(tempInit);
93 }
94 break;
95 }
96 case RSExportType::ExportClassMatrix:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070097 case RSExportType::ExportClassRecord: {
Stephen Hinesd369cda2012-02-13 12:00:03 -080098 ReportVarError(Context, VD->getLocation(),
99 "Reflection of initializer to variable '%0' (of type "
100 "'%1') is unsupported currently.")
101 << mName
102 << ET->getName();
Shih-wei Liao324c0472010-06-21 13:15:11 -0700103 break;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700104 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700105 default: {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800106 slangAssert(false && "Unknown class of type");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700107 }
Shih-wei Liao324c0472010-06-21 13:15:11 -0700108 }
Shih-wei Liao81c1b482010-07-19 15:17:14 -0700109 }
Shih-wei Liao324c0472010-06-21 13:15:11 -0700110
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700111 clang::QualType QT = VD->getTypeSourceInfo()->getType();
Shih-wei Liao81c1b482010-07-19 15:17:14 -0700112 if (!QT.isNull()) {
113 mIsConst = QT.isConstQualified();
Stephen Hines1f6c3312012-07-03 17:23:33 -0700114 mIsUnsigned = QT->hasUnsignedIntegerRepresentation();
115 if (QT == Context->getASTContext().BoolTy) {
116 mIsUnsigned = false;
117 }
Shih-wei Liao81c1b482010-07-19 15:17:14 -0700118 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700119
Shih-wei Liao324c0472010-06-21 13:15:11 -0700120 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700121}
Stephen Hinese639eb52010-11-08 19:27:20 -0800122
123} // namespace slang