1/*
2 * Copyright 2010-2012, 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/ASTContext.h"
20#include "clang/AST/Type.h"
21
22#include "llvm/ADT/APSInt.h"
23
24#include "slang_rs_context.h"
25#include "slang_rs_export_type.h"
26
27namespace slang {
28
29namespace {
30
31static clang::DiagnosticBuilder ReportVarError(RSContext *Context,
32                           const clang::SourceLocation Loc,
33                           const char *Message) {
34  clang::DiagnosticsEngine *DiagEngine = Context->getDiagnostics();
35  const clang::SourceManager *SM = Context->getSourceManager();
36  return DiagEngine->Report(clang::FullSourceLoc(Loc, *SM),
37      DiagEngine->getCustomDiagID(clang::DiagnosticsEngine::Error, Message));
38}
39
40}  // namespace
41
42RSExportVar::RSExportVar(RSContext *Context,
43                         const clang::VarDecl *VD,
44                         const RSExportType *ET)
45    : RSExportable(Context, RSExportable::EX_VAR),
46      mName(VD->getName().data(), VD->getName().size()),
47      mET(ET),
48      mIsConst(false),
49      mIsUnsigned(false),
50      mArraySize(0),
51      mNumInits(0) {
52  // mInit - Evaluate initializer expression
53  const clang::Expr *Initializer = VD->getAnyInitializer();
54  if (Initializer != NULL) {
55    switch (ET->getClass()) {
56      case RSExportType::ExportClassPrimitive:
57      case RSExportType::ExportClassVector: {
58        Initializer->EvaluateAsRValue(mInit, Context->getASTContext());
59        break;
60      }
61      case RSExportType::ExportClassPointer: {
62        if (Initializer->isNullPointerConstant(Context->getASTContext(),
63                clang::Expr::NPC_ValueDependentIsNotNull)) {
64          mInit.Val = clang::APValue(llvm::APSInt(1));
65        } else {
66          if (!Initializer->EvaluateAsRValue(mInit, Context->getASTContext())) {
67            ReportVarError(Context, Initializer->getExprLoc(),
68                           "initializer is not an R-value");
69          }
70        }
71        break;
72      }
73      case RSExportType::ExportClassConstantArray: {
74        const clang::InitListExpr *IList =
75            static_cast<const clang::InitListExpr*>(Initializer);
76        if (!IList) {
77          ReportVarError(Context, VD->getLocation(),
78                         "Unable to find initializer list");
79          break;
80        }
81        const RSExportConstantArrayType *ECAT =
82            static_cast<const RSExportConstantArrayType*>(ET);
83        mArraySize = ECAT->getSize();
84        mNumInits = IList->getNumInits();
85        for (unsigned int i = 0; i < mNumInits; i++) {
86          clang::Expr::EvalResult tempInit;
87          if (!IList->getInit(i)->EvaluateAsRValue(tempInit,
88                                                   Context->getASTContext())) {
89            ReportVarError(Context, IList->getInit(i)->getExprLoc(),
90                           "initializer is not an R-value");
91          }
92          mInitArray.push_back(tempInit);
93        }
94        break;
95      }
96      case RSExportType::ExportClassMatrix:
97      case RSExportType::ExportClassRecord: {
98        ReportVarError(Context, VD->getLocation(),
99                       "Reflection of initializer to variable '%0' (of type "
100                       "'%1') is unsupported currently.")
101            << mName
102            << ET->getName();
103        break;
104      }
105      default: {
106        slangAssert(false && "Unknown class of type");
107      }
108    }
109  }
110
111  clang::QualType QT = VD->getTypeSourceInfo()->getType();
112  if (!QT.isNull()) {
113    mIsConst = QT.isConstQualified();
114    mIsUnsigned = QT->hasUnsignedIntegerRepresentation();
115    if (QT == Context->getASTContext().BoolTy) {
116      mIsUnsigned = false;
117    }
118  }
119
120  return;
121}
122
123}  // namespace slang
124