slang_rs_export_var.cpp revision 9ef2f785e0cc490af678dfd685995dec787321ff
1#include "slang_rs_context.hpp"
2#include "slang_rs_export_var.hpp"
3#include "slang_rs_export_type.hpp"
4
5#include "llvm/ADT/APSInt.h"
6
7#include "clang/AST/Type.h"
8
9using namespace slang;
10
11RSExportVar::RSExportVar(RSContext *Context,
12                         const clang::VarDecl *VD,
13                         const RSExportType *ET) :
14    mContext(Context),
15    mName(VD->getName().data(), VD->getName().size()),
16    mET(ET),
17    mIsConst(false) {
18  // mInit - Evaluate initializer expression
19  const clang::Expr *Initializer = VD->getAnyInitializer();
20  if (Initializer != NULL) {
21    switch (ET->getClass()) {
22      case RSExportType::ExportClassPrimitive:
23      case RSExportType::ExportClassVector: {
24        Initializer->Evaluate(mInit, *Context->getASTContext());
25        break;
26      }
27
28      case RSExportType::ExportClassPointer: {
29        if (Initializer->isNullPointerConstant
30            (*Context->getASTContext(),
31             clang::Expr::NPC_ValueDependentIsNotNull)
32            )
33          mInit.Val = clang::APValue(llvm::APSInt(1));
34        else
35          Initializer->Evaluate(mInit, *Context->getASTContext());
36        break;
37      }
38
39      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());
46        break;
47      }
48
49      default: {
50        assert(false && "Unknown class of type");
51      }
52    }
53  }
54
55  // mIsConst - Is it a constant?
56  clang::QualType QT = VD->getTypeSourceInfo()->getType();
57  if (!QT.isNull()) {
58    mIsConst = QT.isConstQualified();
59  }
60
61  return;
62}
63