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