slang_rs_export_var.cpp revision 641558f02fe6ce0ee3ae5076eb366c25e2ad5903
1/*
2 * Copyright 2010, The Android Open Source Project
3 *
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
17#include "slang_rs_export_var.h"
18
19#include "llvm/ADT/APSInt.h"
20
21#include "clang/AST/Type.h"
22
23#include "slang_rs_context.h"
24#include "slang_rs_export_type.h"
25
26using namespace slang;
27
28RSExportVar::RSExportVar(RSContext *Context,
29                         const clang::VarDecl *VD,
30                         const RSExportType *ET)
31    : RSExportable(Context, RSExportable::EX_VAR),
32      mName(VD->getName().data(), VD->getName().size()),
33      mET(ET),
34      mIsConst(false) {
35  // mInit - Evaluate initializer expression
36  const clang::Expr *Initializer = VD->getAnyInitializer();
37  if (Initializer != NULL) {
38    switch (ET->getClass()) {
39      case RSExportType::ExportClassPrimitive:
40      case RSExportType::ExportClassVector: {
41        Initializer->Evaluate(mInit, *Context->getASTContext());
42        break;
43      }
44      case RSExportType::ExportClassPointer: {
45        if (Initializer->isNullPointerConstant
46            (*Context->getASTContext(),
47             clang::Expr::NPC_ValueDependentIsNotNull)
48            )
49          mInit.Val = clang::APValue(llvm::APSInt(1));
50        else
51          Initializer->Evaluate(mInit, *Context->getASTContext());
52        break;
53      }
54      case RSExportType::ExportClassRecord: {
55        // No action
56        fprintf(stderr, "RSExportVar::RSExportVar : Reflection of initializer "
57                        "to variable '%s' (of type '%s') is unsupported "
58                        "currently.\n",
59                mName.c_str(),
60                ET->getName().c_str());
61        break;
62      }
63      default: {
64        assert(false && "Unknown class of type");
65      }
66    }
67  }
68
69  // mIsConst - Is it a constant?
70  clang::QualType QT = VD->getTypeSourceInfo()->getType();
71  if (!QT.isNull()) {
72    mIsConst = QT.isConstQualified();
73  }
74
75  return;
76}
77