blob: 7b1884ba7c874df33dc3d59cebad88f04b9145dd [file] [log] [blame]
Zonr Changc383a502010-10-12 01:52:08 +08001/*
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
Stephen Hinese639eb52010-11-08 19:27:20 -080017#ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_ // NOLINT
18#define _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_
Shih-wei Liao462aefd2010-06-04 15:32:04 -070019
Shih-wei Liao462aefd2010-06-04 15:32:04 -070020#include <cstdio>
Stephen Hinese639eb52010-11-08 19:27:20 -080021#include <list>
22#include <map>
23#include <string>
24
25#include "clang/Lex/Preprocessor.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070026
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070027#include "llvm/ADT/StringSet.h"
28#include "llvm/ADT/StringMap.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070029
Shih-wei Liao462aefd2010-06-04 15:32:04 -070030namespace llvm {
zonr6315f762010-10-05 15:35:14 +080031 class LLVMContext;
32 class TargetData;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070033} // namespace llvm
Shih-wei Liao462aefd2010-06-04 15:32:04 -070034
35namespace clang {
zonr6315f762010-10-05 15:35:14 +080036 class VarDecl;
37 class ASTContext;
38 class TargetInfo;
39 class FunctionDecl;
40 class SourceManager;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070041} // namespace clang
Shih-wei Liao462aefd2010-06-04 15:32:04 -070042
43namespace slang {
Zonr Changa41ce1d2010-10-06 02:23:12 +080044 class RSExportable;
45 class RSExportVar;
46 class RSExportFunc;
47 class RSExportType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070048
49class RSContext {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070050 typedef llvm::StringSet<> NeedExportVarSet;
51 typedef llvm::StringSet<> NeedExportFuncSet;
52 typedef llvm::StringSet<> NeedExportTypeSet;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070053
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070054 public:
Zonr Changa41ce1d2010-10-06 02:23:12 +080055 typedef std::list<RSExportable*> ExportableList;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070056 typedef std::list<RSExportVar*> ExportVarList;
57 typedef std::list<RSExportFunc*> ExportFuncList;
58 typedef llvm::StringMap<RSExportType*> ExportTypeMap;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070059
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070060 private:
Stephen Hines9e5b5032010-11-03 13:19:14 -070061 clang::Preprocessor &mPP;
62 clang::ASTContext &mCtx;
63 const clang::TargetInfo &mTarget;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070064
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070065 llvm::TargetData *mTargetData;
66 llvm::LLVMContext &mLLVMContext;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070067
Zonr Changa41ce1d2010-10-06 02:23:12 +080068 ExportableList mExportables;
69
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070070 NeedExportTypeSet mNeedExportTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070071
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070072 std::string *mLicenseNote;
73 std::string mReflectJavaPackageName;
74 std::string mReflectJavaPathName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070075
Stephen Hines96ab06c2011-01-05 15:29:26 -080076 int version;
77
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070078 bool processExportVar(const clang::VarDecl *VD);
79 bool processExportFunc(const clang::FunctionDecl *FD);
80 bool processExportType(const llvm::StringRef &Name);
Shih-wei Liao537446c2010-06-11 16:05:55 -070081
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070082 ExportVarList mExportVars;
83 ExportFuncList mExportFuncs;
84 ExportTypeMap mExportTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070085
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070086 public:
Stephen Hines9e5b5032010-11-03 13:19:14 -070087 RSContext(clang::Preprocessor &PP,
88 clang::ASTContext &Ctx,
89 const clang::TargetInfo &Target);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070090
Stephen Hines9e5b5032010-11-03 13:19:14 -070091 inline clang::Preprocessor &getPreprocessor() const { return mPP; }
92 inline clang::ASTContext &getASTContext() const { return mCtx; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070093 inline const llvm::TargetData *getTargetData() const { return mTargetData; }
94 inline llvm::LLVMContext &getLLVMContext() const { return mLLVMContext; }
95 inline const clang::SourceManager *getSourceManager() const {
Stephen Hines9e5b5032010-11-03 13:19:14 -070096 return &mPP.getSourceManager();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070097 }
Stephen Hines2ef9bc02010-12-13 18:33:23 -080098 inline clang::Diagnostic *getDiagnostics() const {
99 return &mPP.getDiagnostics();
100 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700101
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700102 inline void setLicenseNote(const std::string &S) {
103 mLicenseNote = new std::string(S);
104 }
105 inline const std::string *getLicenseNote() const { return mLicenseNote; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700106
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700107 inline void addExportType(const std::string &S) {
108 mNeedExportTypes.insert(S);
109 return;
110 }
Victor Hsiehd8a0d182010-07-07 19:22:33 +0800111
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700112 inline void setReflectJavaPackageName(const std::string &S) {
113 mReflectJavaPackageName = S;
114 return;
115 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700116
Stephen Hinesc808a992010-11-29 17:20:42 -0800117 bool processExport();
Zonr Changa41ce1d2010-10-06 02:23:12 +0800118 inline void newExportable(RSExportable *E) {
119 if (E != NULL)
120 mExportables.push_back(E);
121 }
Zonr Chang641558f2010-10-12 21:07:06 +0800122 typedef ExportableList::iterator exportable_iterator;
123 exportable_iterator exportable_begin() {
124 return mExportables.begin();
125 }
126 exportable_iterator exportable_end() {
127 return mExportables.end();
128 }
Shih-wei Liao537446c2010-06-11 16:05:55 -0700129
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700130 typedef ExportVarList::const_iterator const_export_var_iterator;
131 const_export_var_iterator export_vars_begin() const {
132 return mExportVars.begin();
133 }
134 const_export_var_iterator export_vars_end() const {
135 return mExportVars.end();
136 }
137 inline bool hasExportVar() const {
138 return !mExportVars.empty();
139 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700140
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700141 typedef ExportFuncList::const_iterator const_export_func_iterator;
142 const_export_func_iterator export_funcs_begin() const {
143 return mExportFuncs.begin();
144 }
145 const_export_func_iterator export_funcs_end() const {
146 return mExportFuncs.end();
147 }
148 inline bool hasExportFunc() const { return !mExportFuncs.empty(); }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700149
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700150 typedef ExportTypeMap::iterator export_type_iterator;
151 typedef ExportTypeMap::const_iterator const_export_type_iterator;
152 export_type_iterator export_types_begin() { return mExportTypes.begin(); }
153 export_type_iterator export_types_end() { return mExportTypes.end(); }
154 const_export_type_iterator export_types_begin() const {
155 return mExportTypes.begin();
156 }
157 const_export_type_iterator export_types_end() const {
158 return mExportTypes.end();
159 }
160 inline bool hasExportType() const { return !mExportTypes.empty(); }
161 export_type_iterator findExportType(const llvm::StringRef &TypeName) {
162 return mExportTypes.find(TypeName);
163 }
164 const_export_type_iterator findExportType(const llvm::StringRef &TypeName)
165 const {
166 return mExportTypes.find(TypeName);
167 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700168
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700169 // Insert the specified Typename/Type pair into the map. If the key already
170 // exists in the map, return false and ignore the request, otherwise insert it
171 // and return true.
172 bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700173
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700174 bool reflectToJava(const std::string &OutputPathBase,
175 const std::string &OutputPackageName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700176 const std::string &InputFileName,
177 const std::string &OutputBCFileName,
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700178 std::string *RealPackageName);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700179
Stephen Hines96ab06c2011-01-05 15:29:26 -0800180 int getVersion() const { return version; }
181 void setVersion(int v) {
182 version = v;
183 return;
184 }
185
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700186 ~RSContext();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700187};
188
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700189} // namespace slang
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700190
Stephen Hinese639eb52010-11-08 19:27:20 -0800191#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_ NOLINT