blob: 91b1aa18ca15b5a96db46a7e905f6ad636cc9f08 [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"
Loganbe274822011-02-16 22:02:54 +080026#include "clang/AST/Mangle.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070027
Loganbe274822011-02-16 22:02:54 +080028#include "llvm/ADT/OwningPtr.h"
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070029#include "llvm/ADT/StringSet.h"
30#include "llvm/ADT/StringMap.h"
Shih-wei Liao462aefd2010-06-04 15:32:04 -070031
Stephen Hines3fd0a942011-01-18 12:27:39 -080032#include "slang_pragma_recorder.h"
33
Shih-wei Liao462aefd2010-06-04 15:32:04 -070034namespace llvm {
zonr6315f762010-10-05 15:35:14 +080035 class LLVMContext;
36 class TargetData;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070037} // namespace llvm
Shih-wei Liao462aefd2010-06-04 15:32:04 -070038
39namespace clang {
zonr6315f762010-10-05 15:35:14 +080040 class VarDecl;
41 class ASTContext;
42 class TargetInfo;
43 class FunctionDecl;
44 class SourceManager;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070045} // namespace clang
Shih-wei Liao462aefd2010-06-04 15:32:04 -070046
47namespace slang {
Zonr Changa41ce1d2010-10-06 02:23:12 +080048 class RSExportable;
49 class RSExportVar;
50 class RSExportFunc;
51 class RSExportType;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070052
53class RSContext {
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070054 typedef llvm::StringSet<> NeedExportVarSet;
55 typedef llvm::StringSet<> NeedExportFuncSet;
56 typedef llvm::StringSet<> NeedExportTypeSet;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070057
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070058 public:
Zonr Changa41ce1d2010-10-06 02:23:12 +080059 typedef std::list<RSExportable*> ExportableList;
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070060 typedef std::list<RSExportVar*> ExportVarList;
61 typedef std::list<RSExportFunc*> ExportFuncList;
62 typedef llvm::StringMap<RSExportType*> ExportTypeMap;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070063
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070064 private:
Stephen Hines9e5b5032010-11-03 13:19:14 -070065 clang::Preprocessor &mPP;
66 clang::ASTContext &mCtx;
67 const clang::TargetInfo &mTarget;
Stephen Hines3fd0a942011-01-18 12:27:39 -080068 PragmaList *mPragmas;
Stephen Hines4cc67fc2011-01-31 16:48:57 -080069 std::vector<std::string> *mGeneratedFileNames;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070070
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070071 llvm::TargetData *mTargetData;
72 llvm::LLVMContext &mLLVMContext;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070073
Zonr Changa41ce1d2010-10-06 02:23:12 +080074 ExportableList mExportables;
75
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070076 NeedExportTypeSet mNeedExportTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070077
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070078 std::string *mLicenseNote;
79 std::string mReflectJavaPackageName;
80 std::string mReflectJavaPathName;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070081
Stephen Hines96ab06c2011-01-05 15:29:26 -080082 int version;
Loganbe274822011-02-16 22:02:54 +080083 llvm::OwningPtr<clang::MangleContext> mMangleCtx;
Stephen Hines96ab06c2011-01-05 15:29:26 -080084
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070085 bool processExportVar(const clang::VarDecl *VD);
86 bool processExportFunc(const clang::FunctionDecl *FD);
87 bool processExportType(const llvm::StringRef &Name);
Shih-wei Liao537446c2010-06-11 16:05:55 -070088
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070089 ExportVarList mExportVars;
90 ExportFuncList mExportFuncs;
91 ExportTypeMap mExportTypes;
Shih-wei Liao462aefd2010-06-04 15:32:04 -070092
Shih-wei Liao9ef2f782010-10-01 12:31:37 -070093 public:
Stephen Hines9e5b5032010-11-03 13:19:14 -070094 RSContext(clang::Preprocessor &PP,
95 clang::ASTContext &Ctx,
Stephen Hines3fd0a942011-01-18 12:27:39 -080096 const clang::TargetInfo &Target,
Stephen Hines4cc67fc2011-01-31 16:48:57 -080097 PragmaList *Pragmas,
98 std::vector<std::string> *GeneratedFileNames);
Shih-wei Liao462aefd2010-06-04 15:32:04 -070099
Stephen Hines9e5b5032010-11-03 13:19:14 -0700100 inline clang::Preprocessor &getPreprocessor() const { return mPP; }
101 inline clang::ASTContext &getASTContext() const { return mCtx; }
Loganbe274822011-02-16 22:02:54 +0800102 inline clang::MangleContext &getMangleContext() const {
103 return *mMangleCtx;
Stephen Hinesf2174cf2011-02-09 23:21:37 -0800104 }
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700105 inline const llvm::TargetData *getTargetData() const { return mTargetData; }
106 inline llvm::LLVMContext &getLLVMContext() const { return mLLVMContext; }
107 inline const clang::SourceManager *getSourceManager() const {
Stephen Hines9e5b5032010-11-03 13:19:14 -0700108 return &mPP.getSourceManager();
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700109 }
Stephen Hines2ef9bc02010-12-13 18:33:23 -0800110 inline clang::Diagnostic *getDiagnostics() const {
111 return &mPP.getDiagnostics();
112 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700113
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700114 inline void setLicenseNote(const std::string &S) {
115 mLicenseNote = new std::string(S);
116 }
117 inline const std::string *getLicenseNote() const { return mLicenseNote; }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700118
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700119 inline void addExportType(const std::string &S) {
120 mNeedExportTypes.insert(S);
121 return;
122 }
Victor Hsiehd8a0d182010-07-07 19:22:33 +0800123
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700124 inline void setReflectJavaPackageName(const std::string &S) {
125 mReflectJavaPackageName = S;
126 return;
127 }
Stephen Hines4cc67fc2011-01-31 16:48:57 -0800128 inline const std::string &getReflectJavaPackageName() {
129 return mReflectJavaPackageName;
130 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700131
Stephen Hinesc808a992010-11-29 17:20:42 -0800132 bool processExport();
Zonr Changa41ce1d2010-10-06 02:23:12 +0800133 inline void newExportable(RSExportable *E) {
134 if (E != NULL)
135 mExportables.push_back(E);
136 }
Zonr Chang641558f2010-10-12 21:07:06 +0800137 typedef ExportableList::iterator exportable_iterator;
138 exportable_iterator exportable_begin() {
139 return mExportables.begin();
140 }
141 exportable_iterator exportable_end() {
142 return mExportables.end();
143 }
Shih-wei Liao537446c2010-06-11 16:05:55 -0700144
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700145 typedef ExportVarList::const_iterator const_export_var_iterator;
146 const_export_var_iterator export_vars_begin() const {
147 return mExportVars.begin();
148 }
149 const_export_var_iterator export_vars_end() const {
150 return mExportVars.end();
151 }
152 inline bool hasExportVar() const {
153 return !mExportVars.empty();
154 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700155
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700156 typedef ExportFuncList::const_iterator const_export_func_iterator;
157 const_export_func_iterator export_funcs_begin() const {
158 return mExportFuncs.begin();
159 }
160 const_export_func_iterator export_funcs_end() const {
161 return mExportFuncs.end();
162 }
163 inline bool hasExportFunc() const { return !mExportFuncs.empty(); }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700164
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700165 typedef ExportTypeMap::iterator export_type_iterator;
166 typedef ExportTypeMap::const_iterator const_export_type_iterator;
167 export_type_iterator export_types_begin() { return mExportTypes.begin(); }
168 export_type_iterator export_types_end() { return mExportTypes.end(); }
169 const_export_type_iterator export_types_begin() const {
170 return mExportTypes.begin();
171 }
172 const_export_type_iterator export_types_end() const {
173 return mExportTypes.end();
174 }
175 inline bool hasExportType() const { return !mExportTypes.empty(); }
176 export_type_iterator findExportType(const llvm::StringRef &TypeName) {
177 return mExportTypes.find(TypeName);
178 }
179 const_export_type_iterator findExportType(const llvm::StringRef &TypeName)
180 const {
181 return mExportTypes.find(TypeName);
182 }
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700183
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700184 // Insert the specified Typename/Type pair into the map. If the key already
185 // exists in the map, return false and ignore the request, otherwise insert it
186 // and return true.
187 bool insertExportType(const llvm::StringRef &TypeName, RSExportType *Type);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700188
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700189 bool reflectToJava(const std::string &OutputPathBase,
190 const std::string &OutputPackageName,
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700191 const std::string &InputFileName,
192 const std::string &OutputBCFileName,
Shih-wei Liaob81c6a42010-10-10 14:15:00 -0700193 std::string *RealPackageName);
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700194
Stephen Hines96ab06c2011-01-05 15:29:26 -0800195 int getVersion() const { return version; }
196 void setVersion(int v) {
197 version = v;
198 return;
199 }
200
Stephen Hines3fd0a942011-01-18 12:27:39 -0800201 void addPragma(const std::string &T, const std::string &V) {
202 mPragmas->push_back(make_pair(T, V));
203 }
204
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700205 ~RSContext();
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700206};
207
Shih-wei Liao9ef2f782010-10-01 12:31:37 -0700208} // namespace slang
Shih-wei Liao462aefd2010-06-04 15:32:04 -0700209
Stephen Hinese639eb52010-11-08 19:27:20 -0800210#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CONTEXT_H_ NOLINT