blob: c410255fcfaa5e5e13ef284e033c222aec2328e7 [file] [log] [blame]
zonr6315f762010-10-05 15:35:14 +08001#include "slang_rs_export_var.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -07002
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07003#include "llvm/ADT/APSInt.h"
Shih-wei Liao324c0472010-06-21 13:15:11 -07004
Shih-wei Liao9ef2f782010-10-01 12:31:37 -07005#include "clang/AST/Type.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -07006
zonr6315f762010-10-05 15:35:14 +08007#include "slang_rs_context.h"
8#include "slang_rs_export_type.h"
9
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070010using namespace slang;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070011
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070012RSExportVar::RSExportVar(RSContext *Context,
13 const clang::VarDecl *VD,
zonr6315f762010-10-05 15:35:14 +080014 const RSExportType *ET)
Zonr Changa41ce1d2010-10-06 02:23:12 +080015 : RSExportable(Context, RSExportable::EX_VAR),
16 mContext(Context),
zonr6315f762010-10-05 15:35:14 +080017 mName(VD->getName().data(), VD->getName().size()),
18 mET(ET),
19 mIsConst(false) {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070020 // mInit - Evaluate initializer expression
21 const clang::Expr *Initializer = VD->getAnyInitializer();
22 if (Initializer != NULL) {
23 switch (ET->getClass()) {
Shih-wei Liao324c0472010-06-21 13:15:11 -070024 case RSExportType::ExportClassPrimitive:
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070025 case RSExportType::ExportClassVector: {
Shih-wei Liaof52a6202010-09-10 17:40:53 -070026 Initializer->Evaluate(mInit, *Context->getASTContext());
Shih-wei Liao324c0472010-06-21 13:15:11 -070027 break;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070028 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070029 case RSExportType::ExportClassPointer: {
30 if (Initializer->isNullPointerConstant
31 (*Context->getASTContext(),
32 clang::Expr::NPC_ValueDependentIsNotNull)
33 )
34 mInit.Val = clang::APValue(llvm::APSInt(1));
Shih-wei Liao324c0472010-06-21 13:15:11 -070035 else
Shih-wei Liaof52a6202010-09-10 17:40:53 -070036 Initializer->Evaluate(mInit, *Context->getASTContext());
Shih-wei Liao324c0472010-06-21 13:15:11 -070037 break;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070038 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070039 case RSExportType::ExportClassRecord: {
40 // No action
41 fprintf(stderr, "RSExportVar::RSExportVar : Reflection of initializer "
42 "to variable '%s' (of type '%s') is unsupported "
43 "currently.\n",
44 mName.c_str(),
45 ET->getName().c_str());
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 default: {
Shih-wei Liao324c0472010-06-21 13:15:11 -070049 assert(false && "Unknown class of type");
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070050 }
Shih-wei Liao324c0472010-06-21 13:15:11 -070051 }
Shih-wei Liao81c1b482010-07-19 15:17:14 -070052 }
Shih-wei Liao324c0472010-06-21 13:15:11 -070053
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070054 // mIsConst - Is it a constant?
55 clang::QualType QT = VD->getTypeSourceInfo()->getType();
Shih-wei Liao81c1b482010-07-19 15:17:14 -070056 if (!QT.isNull()) {
57 mIsConst = QT.isConstQualified();
58 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070059
Shih-wei Liao324c0472010-06-21 13:15:11 -070060 return;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070061}