blob: 199757a4739a7333afcfe2acd9722968d29d107f [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
Stephen Hines3fd0a942011-01-18 12:27:39 -080030#include "slang_pragma_recorder.h"
31
Shih-wei Liao462aefd2010-06-04 15:32:04 -070032namespace llvm {
zonr6315f762010-10-05 15:35:14 +080033 class LLVMContext;
34 class TargetData;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070035} // namespace llvm
Shih-wei Liao462aefd2010-06-04 15:32:04 -070036
37namespace clang {
zonr6315f762010-10-05 15:35:14 +080038 class VarDecl;
39 class ASTContext;
40 class TargetInfo;
41 class FunctionDecl;
42 class SourceManager;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070043} // namespace clang
Shih-wei Liao462aefd2010-06-04 15:32:04 -070044
45namespace slang {
Zonr Changa41ce1d2010-10-06 02:23:12 +080046 class RSExportable;
47 class RSExportVar;
48 class RSExportFunc;
49 class RSExportType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070050
51class RSContext {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070052 typedef llvm::StringSet<> NeedExportVarSet;
53 typedef llvm::StringSet<> NeedExportFuncSet;
54 typedef llvm::StringSet<> NeedExportTypeSet;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070055
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070056 public:
Zonr Changa41ce1d2010-10-06 02:23:12 +080057 typedef std::list<RSExportable*> ExportableList;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070058 typedef std::list<RSExportVar*> ExportVarList;
59 typedef std::list<RSExportFunc*> ExportFuncList;
60 typedef llvm::StringMap<RSExportType*> ExportTypeMap;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070061
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070062 private:
Stephen Hines9e5b5032010-11-03 13:19:14 -070063 clang::Preprocessor &mPP;
64 clang::ASTContext &mCtx;
65 const clang::TargetInfo &mTarget;
Stephen Hines3fd0a942011-01-18 12:27:39 -080066 PragmaList *mPragmas;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070067
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070068 llvm::TargetData *mTargetData;
69 llvm::LLVMContext &mLLVMContext;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070070
Zonr Changa41ce1d2010-10-06 02:23:12 +080071 ExportableList mExportables;
72
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070073 NeedExportTypeSet mNeedExportTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070074
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070075 std::string *mLicenseNote;
76 std::string mReflectJavaPackageName;
77 std::string mReflectJavaPathName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070078
Stephen Hines96ab06c2011-01-05 15:29:26 -080079 int version;
80
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070081 bool processExportVar(const clang::VarDecl *VD);
82 bool processExportFunc(const clang::FunctionDecl *FD);
83 bool processExportType(const llvm::StringRef &Name);
Shih-wei Liao537446c2010-06-11 16:05:55 -070084
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070085 ExportVarList mExportVars;
86 ExportFuncList mExportFuncs;
87 ExportTypeMap mExportTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070088
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070089 public:
Stephen Hines9e5b5032010-11-03 13:19:14 -070090 RSContext(clang::Preprocessor &PP,
91 clang::ASTContext &Ctx,
Stephen Hines3fd0a942011-01-18 12:27:39 -080092 const clang::TargetInfo &Target,
93 PragmaList *Pragmas);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070094
Stephen Hines9e5b5032010-11-03 13:19:14 -070095 inline clang::Preprocessor &getPreprocessor() const { return mPP; }
96 inline clang::ASTContext &getASTContext() const { return mCtx; }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070097 inline const llvm::TargetData *getTargetData() const { return mTargetData; }
98 inline llvm::LLVMContext &getLLVMContext() const { return mLLVMContext; }
99 inline const clang::SourceManager *getSourceManager() const {
Stephen Hines9e5b5032010-11-03 13:19:14 -0700100 return &mPP.getSourceManager();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700101 }
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800102 inline clang::Diagnostic *getDiagnostics() const {
103 return &mPP.getDiagnostics();
104 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700105
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700106 inline void setLicenseNote(const std::string &S) {
107 mLicenseNote = new std::string(S);
108 }
109 inline const std::string *getLicenseNote() const { return mLicenseNote; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700110
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700111 inline void addExportType(const std::string &S) {
112 mNeedExportTypes.insert(S);
113 return;
114 }
Victor Hsiehd8a0d182010-07-07 19:22:33 +0800115
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700116 inline void setReflectJavaPackageName(const std::string &S) {
117 mReflectJavaPackageName = S;
118 return;
119 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700120
Stephen Hinesc808a992010-11-29 17:20:42 -0800121 bool processExport();
Zonr Changa41ce1d2010-10-06 02:23:12 +0800122 inline void newExportable(RSExportable *E) {
123 if (E != NULL)
124 mExportables.push_back(E);
125 }
Zonr Chang641558f2010-10-12 21:07:06 +0800126 typedef ExportableList::iterator exportable_iterator;
127 exportable_iterator exportable_begin() {
128 return mExportables.begin();
129 }
130 exportable_iterator exportable_end() {
131 return mExportables.end();
132 }
Shih-wei Liao537446c2010-06-11 16:05:55 -0700133
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700134 typedef ExportVarList::const_iterator const_export_var_iterator;
135 const_export_var_iterator export_vars_begin() const {
136 return mExportVars.begin();
137 }
138 const_export_var_iterator export_vars_end() const {
139 return mExportVars.end();
140 }
141 inline bool hasExportVar() const {
142 return !mExportVars.empty();
143 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700144
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700145 typedef ExportFuncList::const_iterator const_export_func_iterator;
146 const_export_func_iterator export_funcs_begin() const {
147 return mExportFuncs.begin();
148 }
149 const_export_func_iterator export_funcs_end() const {
150 return mExportFuncs.end();
151 }
152 inline bool hasExportFunc() const { return !mExportFuncs.empty(); }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700153
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700154 typedef ExportTypeMap::iterator export_type_iterator;
155 typedef ExportTypeMap::const_iterator const_export_type_iterator;
156 export_type_iterator export_types_begin() { return mExportTypes.begin(); }
157 export_type_iterator export_types_end() { return mExportTypes.end(); }
158 const_export_type_iterator export_types_begin() const {
159 return mExportTypes.begin();
160 }
161 const_export_type_iterator export_types_end() const {
162 return mExportTypes.end();
163 }
164 inline bool hasExportType() const { return !mExportTypes.empty(); }
165 export_type_iterator findExportType(const llvm::StringRef &TypeName) {
166 return mExportTypes.find(TypeName);
167 }
168 const_export_type_iterator findExportType(const llvm::StringRef &TypeName)
169 const {
170 return mExportTypes.find(TypeName);
171 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700172
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700173 // Insert the specified Typename/Type pair into the map. If the key already
174 // exists in the map, return false and ignore the request, otherwise insert it
175 // and return true.
176 bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700177
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700178 bool reflectToJava(const std::string &OutputPathBase,
179 const std::string &OutputPackageName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700180 const std::string &InputFileName,
181 const std::string &OutputBCFileName,
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700182 std::string *RealPackageName);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700183
Stephen Hines96ab06c2011-01-05 15:29:26 -0800184 int getVersion() const { return version; }
185 void setVersion(int v) {
186 version = v;
187 return;
188 }
189
Stephen Hines3fd0a942011-01-18 12:27:39 -0800190 void addPragma(const std::string &T, const std::string &V) {
191 mPragmas->push_back(make_pair(T, V));
192 }
193
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700194 ~RSContext();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700195};
196
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700197} // namespace slang
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700198
Stephen Hinese639eb52010-11-08 19:27:20 -0800199#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_ NOLINT