blob: 2c857910b1bf0cfc2c63c6a5ce178156e4a08666 [file] [log] [blame]
Shih-wei Liaod2a5a0e2012-04-25 03:40:50 -07001/*
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
Zonr Changc72c4dd2012-04-12 15:38:53 +080017#ifndef BCC_SOURCE_H
18#define BCC_SOURCE_H
Shih-wei Liaod2a5a0e2012-04-25 03:40:50 -070019
20#include <string>
21
22namespace llvm {
23 class Module;
24}
25
Pirama Arumuga Nainarf229c402016-03-06 23:05:45 -080026namespace bcinfo {
27 class MetadataExtractor;
28}
29
Shih-wei Liaod2a5a0e2012-04-25 03:40:50 -070030namespace bcc {
31
32class BCCContext;
33
34class Source {
35private:
Yang Nia4ded132014-11-17 17:44:08 -080036 const std::string mName; // A unique name
Shih-wei Liaod2a5a0e2012-04-25 03:40:50 -070037 BCCContext &mContext;
38 llvm::Module *mModule;
39
Pirama Arumuga Nainarf229c402016-03-06 23:05:45 -080040 bcinfo::MetadataExtractor *mMetadata;
41
Shih-wei Liaod2a5a0e2012-04-25 03:40:50 -070042 // If true, destructor won't destroy the mModule.
43 bool mNoDelete;
44
Pirama Arumuga Nainarf229c402016-03-06 23:05:45 -080045 // Keep track of whether mModule is destroyed (possibly as a consequence of
46 // getting linked with a different llvm::Module).
47 bool mIsModuleDestroyed;
48
Shih-wei Liaod2a5a0e2012-04-25 03:40:50 -070049private:
Yang Nia4ded132014-11-17 17:44:08 -080050 Source(const char* name, BCCContext &pContext, llvm::Module &pModule,
51 bool pNoDelete = false);
Shih-wei Liaod2a5a0e2012-04-25 03:40:50 -070052
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,
Yang Nia4ded132014-11-17 17:44:08 -080065 const char* name,
Shih-wei Liaod2a5a0e2012-04-25 03:40:50 -070066 llvm::Module &pModule,
David Grosscf5afcb2017-03-20 15:23:26 -070067 uint32_t compilerVersion,
68 uint32_t optimizationLevel,
Shih-wei Liaod2a5a0e2012-04-25 03:40:50 -070069 bool pNoDelete = false);
70
Yang Nia4ded132014-11-17 17:44:08 -080071 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.
Stephen Hines57936132014-11-25 17:54:59 -080075 bool merge(Source &pSource);
Shih-wei Liaod2a5a0e2012-04-25 03:40:50 -070076
David Grosscf5afcb2017-03-20 15:23:26 -070077 unsigned getCompilerVersion() const;
78
79 void getWrapperInformation(unsigned *compilerVersion,
80 unsigned *optimizationLevel) const;
81
Shih-wei Liaod2a5a0e2012-04-25 03:40:50 -070082 inline BCCContext &getContext()
83 { return mContext; }
84 inline const BCCContext &getContext() const
85 { return mContext; }
86
Stephen Hines06731a62013-02-12 19:29:42 -080087 void setModule(llvm::Module *pModule);
88
Shih-wei Liaod2a5a0e2012-04-25 03:40:50 -070089 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
Pirama Arumuga Nainar51ee77b2015-02-19 16:33:27 -080098 void addBuildChecksumMetadata(const char *) const;
99
Dean De Leo09c7a412015-11-25 12:45:45 +0000100 // 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
Pirama Arumuga Nainarf229c402016-03-06 23:05:45 -0800104 // 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
Shih-wei Liaod2a5a0e2012-04-25 03:40:50 -0700112 ~Source();
113};
114
115} // namespace bcc
116
Zonr Changc72c4dd2012-04-12 15:38:53 +0800117#endif // BCC_SOURCE_H