blob: 570be32fec1651c9f5237ba66b66d93d1457cd9c [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 Liao9ef2f782010-10-01 12:31:37 -070019#include "clang/AST/Type.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070020
Stephen Hinese639eb52010-11-08 19:27:20 -080021#include "llvm/ADT/APSInt.h"
22
zonr6315f762010-10-05 15:35:14 +080023#include "slang_rs_context.h"
24#include "slang_rs_export_type.h"
25
Stephen Hinese639eb52010-11-08 19:27:20 -080026namespace slang {
Shih-wei Liao462aefd2010-06-04 15:32:04 -070027
Stephen Hinesd369cda2012-02-13 12:00:03 -080028namespace {
29
30static 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 Liao9ef2f782010-10-01 12:31:37 -070041RSExportVar::RSExportVar(RSContext *Context,
42 const clang::VarDecl *VD,
zonr6315f762010-10-05 15:35:14 +080043 const RSExportType *ET)
Zonr Changa41ce1d2010-10-06 02:23:12 +080044 : RSExportable(Context, RSExportable::EX_VAR),
zonr6315f762010-10-05 15:35:14 +080045 mName(VD->getName().data(), VD->getName().size()),
46 mET(ET),
Stephen Hinesd369cda2012-02-13 12:00:03 -080047 mIsConst(false),
Stephen Hines1f6c3312012-07-03 17:23:33 -070048 mIsUnsigned(false),
Stephen Hinesd369cda2012-02-13 12:00:03 -080049 mArraySize(0),
50 mNumInits(0) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070051 // mInit - Evaluate initializer expression
52 const clang::Expr *Initializer = VD->getAnyInitializer();
53 if (Initializer != NULL) {
54 switch (ET->getClass()) {
Shih-wei Liao324c0472010-06-21 13:15:11 -070055 case RSExportType::ExportClassPrimitive:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070056 case RSExportType::ExportClassVector: {
Stephen Hines4c622e02011-11-10 18:57:34 -080057 Initializer->EvaluateAsRValue(mInit, Context->getASTContext());
Shih-wei Liao324c0472010-06-21 13:15:11 -070058 break;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070059 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070060 case RSExportType::ExportClassPointer: {
Stephen Hinesd369cda2012-02-13 12:00:03 -080061 if (Initializer->isNullPointerConstant(Context->getASTContext(),
62 clang::Expr::NPC_ValueDependentIsNotNull)) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070063 mInit.Val = clang::APValue(llvm::APSInt(1));
Stephen Hinesd369cda2012-02-13 12:00:03 -080064 } else {
65 if (!Initializer->EvaluateAsRValue(mInit, Context->getASTContext())) {
66 ReportVarError(Context, Initializer->getExprLoc(),
67 "initializer is not an R-value");
68 }
69 }
Shih-wei Liao324c0472010-06-21 13:15:11 -070070 break;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070071 }
Stephen Hinesd369cda2012-02-13 12:00:03 -080072 case RSExportType::ExportClassConstantArray: {
Stephen Hinese67239d2012-02-24 15:08:36 -080073 const clang::InitListExpr *IList =
Stephen Hinesd369cda2012-02-13 12:00:03 -080074 static_cast<const clang::InitListExpr*>(Initializer);
75 if (!IList) {
76 ReportVarError(Context, VD->getLocation(),
77 "Unable to find initializer list");
78 break;
79 }
80 const RSExportConstantArrayType *ECAT =
81 static_cast<const RSExportConstantArrayType*>(ET);
82 mArraySize = ECAT->getSize();
83 mNumInits = IList->getNumInits();
84 for (unsigned int i = 0; i < mNumInits; i++) {
85 clang::Expr::EvalResult tempInit;
86 if (!IList->getInit(i)->EvaluateAsRValue(tempInit,
87 Context->getASTContext())) {
88 ReportVarError(Context, IList->getInit(i)->getExprLoc(),
89 "initializer is not an R-value");
90 }
91 mInitArray.push_back(tempInit);
92 }
93 break;
94 }
95 case RSExportType::ExportClassMatrix:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070096 case RSExportType::ExportClassRecord: {
Stephen Hinesd369cda2012-02-13 12:00:03 -080097 ReportVarError(Context, VD->getLocation(),
98 "Reflection of initializer to variable '%0' (of type "
99 "'%1') is unsupported currently.")
100 << mName
101 << ET->getName();
Shih-wei Liao324c0472010-06-21 13:15:11 -0700102 break;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700103 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700104 default: {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800105 slangAssert(false && "Unknown class of type");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700106 }
Shih-wei Liao324c0472010-06-21 13:15:11 -0700107 }
Shih-wei Liao81c1b482010-07-19 15:17:14 -0700108 }
Shih-wei Liao324c0472010-06-21 13:15:11 -0700109
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700110 clang::QualType QT = VD->getTypeSourceInfo()->getType();
Shih-wei Liao81c1b482010-07-19 15:17:14 -0700111 if (!QT.isNull()) {
112 mIsConst = QT.isConstQualified();
Stephen Hines1f6c3312012-07-03 17:23:33 -0700113 mIsUnsigned = QT->hasUnsignedIntegerRepresentation();
114 if (QT == Context->getASTContext().BoolTy) {
115 mIsUnsigned = false;
116 }
Shih-wei Liao81c1b482010-07-19 15:17:14 -0700117 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700118
Shih-wei Liao324c0472010-06-21 13:15:11 -0700119 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700120}
Stephen Hinese639eb52010-11-08 19:27:20 -0800121
122} // namespace slang