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 
27 namespace slang {
28 
RSExportVar(RSContext * Context,const clang::VarDecl * VD,const RSExportType * ET)29 RSExportVar::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       mIsUnsigned(false),
37       mArraySize(0),
38       mNumInits(0) {
39   // mInit - Evaluate initializer expression
40   const clang::Expr *Initializer = VD->getAnyInitializer();
41   if (Initializer != nullptr) {
42     switch (ET->getClass()) {
43       case RSExportType::ExportClassPrimitive:
44       case RSExportType::ExportClassVector: {
45         Initializer->EvaluateAsRValue(mInit, Context->getASTContext());
46         break;
47       }
48       case RSExportType::ExportClassPointer: {
49         if (Initializer->isNullPointerConstant(Context->getASTContext(),
50                 clang::Expr::NPC_ValueDependentIsNotNull)) {
51           mInit.Val = clang::APValue(llvm::APSInt(1));
52         } else {
53           if (!Initializer->EvaluateAsRValue(mInit, Context->getASTContext())) {
54             Context->ReportError(Initializer->getExprLoc(),
55                                  "initializer is not an R-value");
56           }
57         }
58         break;
59       }
60       case RSExportType::ExportClassConstantArray: {
61         const clang::InitListExpr *IList =
62             static_cast<const clang::InitListExpr*>(Initializer);
63         if (!IList) {
64           Context->ReportError(VD->getLocation(),
65                                "Unable to find initializer list");
66           break;
67         }
68         const RSExportConstantArrayType *ECAT =
69             static_cast<const RSExportConstantArrayType*>(ET);
70         mArraySize = ECAT->getNumElement();
71         mNumInits = IList->getNumInits();
72         for (unsigned int i = 0; i < mNumInits; i++) {
73           clang::Expr::EvalResult tempInit;
74           if (!IList->getInit(i)->EvaluateAsRValue(tempInit,
75                                                    Context->getASTContext())) {
76             Context->ReportError(IList->getInit(i)->getExprLoc(),
77                                  "initializer is not an R-value");
78           }
79           mInitArray.push_back(tempInit);
80         }
81         break;
82       }
83       case RSExportType::ExportClassMatrix:
84       case RSExportType::ExportClassRecord: {
85         Context->ReportError(
86             VD->getLocation(),
87             "Reflection of initializer to variable '%0' (of type "
88             "'%1') is unsupported currently.")
89             << mName << ET->getName();
90         break;
91       }
92       default: {
93         slangAssert(false && "Unknown class of type");
94       }
95     }
96   }
97 
98   clang::QualType QT = VD->getTypeSourceInfo()->getType();
99   if (!QT.isNull()) {
100     mIsConst = QT.isConstQualified();
101     mIsUnsigned = QT->hasUnsignedIntegerRepresentation();
102     if (QT == Context->getASTContext().BoolTy) {
103       mIsUnsigned = false;
104     }
105   }
106 }
107 
108 }  // namespace slang
109