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#ifndef BCC_SOURCE_H
18#define BCC_SOURCE_H
19
20#include <string>
21
22namespace llvm {
23  class Module;
24}
25
26namespace bcinfo {
27  class MetadataExtractor;
28}
29
30namespace bcc {
31
32class BCCContext;
33
34class Source {
35private:
36  const std::string mName; // A unique name
37  BCCContext &mContext;
38  llvm::Module *mModule;
39
40  bcinfo::MetadataExtractor *mMetadata;
41
42  // If true, destructor won't destroy the mModule.
43  bool mNoDelete;
44
45  // Keep track of whether mModule is destroyed (possibly as a consequence of
46  // getting linked with a different llvm::Module).
47  bool mIsModuleDestroyed;
48
49private:
50  Source(const char* name, BCCContext &pContext, llvm::Module &pModule,
51         bool pNoDelete = false);
52
53public:
54  static Source *CreateFromBuffer(BCCContext &pContext,
55                                  const char *pName,
56                                  const char *pBitcode,
57                                  size_t pBitcodeSize);
58
59  static Source *CreateFromFile(BCCContext &pContext,
60                                const std::string &pPath);
61
62  // Create a Source object from an existing module. If pNoDelete
63  // is true, destructor won't call delete on the given module.
64  static Source *CreateFromModule(BCCContext &pContext,
65                                  const char* name,
66                                  llvm::Module &pModule,
67                                  uint32_t compilerVersion,
68                                  uint32_t optimizationLevel,
69                                  bool pNoDelete = false);
70
71  const std::string& getName() const { return mName; }
72
73  // Merge the current source with pSource. pSource
74  // will be destroyed after successfully merged. Return false on error.
75  bool merge(Source &pSource);
76
77  unsigned getCompilerVersion() const;
78
79  void getWrapperInformation(unsigned *compilerVersion,
80                             unsigned *optimizationLevel) const;
81
82  inline BCCContext &getContext()
83  { return mContext; }
84  inline const BCCContext &getContext() const
85  { return mContext; }
86
87  void setModule(llvm::Module *pModule);
88
89  inline llvm::Module &getModule()
90  { return *mModule;  }
91  inline const llvm::Module &getModule() const
92  { return *mModule;  }
93
94  // Get the "identifier" of the bitcode. This will return the value of pName
95  // when it's created using CreateFromBuffer and pPath if CreateFromFile().
96  const std::string &getIdentifier() const;
97
98  void addBuildChecksumMetadata(const char *) const;
99
100  // Get whether debugging has been enabled for this module by checking
101  // for presence of debug info in the module.
102  bool getDebugInfoEnabled() const;
103
104  // Extract metadata from mModule using MetadataExtractor.
105  bool extractMetadata();
106  bcinfo::MetadataExtractor* getMetadata() const { return mMetadata; }
107
108  // Mark mModule was destroyed in the process of linking with a different
109  // llvm::Module
110  void markModuleDestroyed() { mIsModuleDestroyed = true; }
111
112  ~Source();
113};
114
115} // namespace bcc
116
117#endif // BCC_SOURCE_H
118