blob: 56e256f87a21305d4dd4744f5ec2e35998ddb502 [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),
48 mArraySize(0),
49 mNumInits(0) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070050 // mInit - Evaluate initializer expression
51 const clang::Expr *Initializer = VD->getAnyInitializer();
52 if (Initializer != NULL) {
53 switch (ET->getClass()) {
Shih-wei Liao324c0472010-06-21 13:15:11 -070054 case RSExportType::ExportClassPrimitive:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070055 case RSExportType::ExportClassVector: {
Stephen Hines4c622e02011-11-10 18:57:34 -080056 Initializer->EvaluateAsRValue(mInit, Context->getASTContext());
Shih-wei Liao324c0472010-06-21 13:15:11 -070057 break;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070058 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070059 case RSExportType::ExportClassPointer: {
Stephen Hinesd369cda2012-02-13 12:00:03 -080060 if (Initializer->isNullPointerConstant(Context->getASTContext(),
61 clang::Expr::NPC_ValueDependentIsNotNull)) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070062 mInit.Val = clang::APValue(llvm::APSInt(1));
Stephen Hinesd369cda2012-02-13 12:00:03 -080063 } else {
64 if (!Initializer->EvaluateAsRValue(mInit, Context->getASTContext())) {
65 ReportVarError(Context, Initializer->getExprLoc(),
66 "initializer is not an R-value");
67 }
68 }
Shih-wei Liao324c0472010-06-21 13:15:11 -070069 break;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070070 }
Stephen Hinesd369cda2012-02-13 12:00:03 -080071 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 Liao9ef2f782010-10-01 12:31:37 -070095 case RSExportType::ExportClassRecord: {
Stephen Hinesd369cda2012-02-13 12:00:03 -080096 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 Liao324c0472010-06-21 13:15:11 -0700101 break;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700102 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700103 default: {
Stephen Hines6e6578a2011-02-07 18:05:48 -0800104 slangAssert(false && "Unknown class of type");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700105 }
Shih-wei Liao324c0472010-06-21 13:15:11 -0700106 }
Shih-wei Liao81c1b482010-07-19 15:17:14 -0700107 }
Shih-wei Liao324c0472010-06-21 13:15:11 -0700108
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700109 // mIsConst - Is it a constant?
110 clang::QualType QT = VD->getTypeSourceInfo()->getType();
Shih-wei Liao81c1b482010-07-19 15:17:14 -0700111 if (!QT.isNull()) {
112 mIsConst = QT.isConstQualified();
113 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700114
Shih-wei Liao324c0472010-06-21 13:15:11 -0700115 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700116}
Stephen Hinese639eb52010-11-08 19:27:20 -0800117
118} // namespace slang