Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | #ifndef BCC_SOURCEINFO_H |
| 18 | #define BCC_SOURCEINFO_H |
| 19 | |
| 20 | #include "Config.h" |
| 21 | |
| 22 | #include <llvm/Module.h> |
| 23 | |
| 24 | #include <stddef.h> |
| 25 | |
Stephen Hines | 274926b | 2012-05-03 12:30:46 -0700 | [diff] [blame] | 26 | namespace llvm { |
| 27 | class LLVMContext; |
| 28 | } |
| 29 | |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame] | 30 | namespace bcc { |
| 31 | namespace SourceKind { |
| 32 | enum SourceType { |
| 33 | File, |
| 34 | Buffer, |
| 35 | Module, |
| 36 | }; |
| 37 | } |
| 38 | |
| 39 | class SourceInfo { |
| 40 | private: |
| 41 | SourceKind::SourceType type; |
| 42 | |
| 43 | // Note: module should not be a part of union. Since, we are going to |
| 44 | // use module to store the pointer to parsed bitcode. |
| 45 | llvm::Module *module; |
Stephen Hines | 274926b | 2012-05-03 12:30:46 -0700 | [diff] [blame] | 46 | // If true, the LLVM context behind the module is shared with others. |
| 47 | // Therefore, don't try to destroy the context it when destroy the module. |
| 48 | bool shared_context; |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame] | 49 | |
| 50 | union { |
| 51 | struct { |
| 52 | char const *resName; |
| 53 | char const *bitcode; |
| 54 | size_t bitcodeSize; |
| 55 | } buffer; |
| 56 | |
| 57 | struct { |
| 58 | char const *path; |
| 59 | } file; |
| 60 | }; |
| 61 | |
| 62 | unsigned long flags; |
| 63 | |
| 64 | unsigned char sha1[20]; |
| 65 | |
| 66 | private: |
Stephen Hines | 274926b | 2012-05-03 12:30:46 -0700 | [diff] [blame] | 67 | SourceInfo() : module(NULL), shared_context(false) { } |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame] | 68 | |
| 69 | public: |
| 70 | static SourceInfo *createFromBuffer(char const *resName, |
| 71 | char const *bitcode, |
| 72 | size_t bitcodeSize, |
| 73 | unsigned long flags); |
| 74 | |
| 75 | static SourceInfo *createFromFile(char const *path, |
| 76 | unsigned long flags); |
| 77 | |
| 78 | static SourceInfo *createFromModule(llvm::Module *module, |
| 79 | unsigned long flags); |
| 80 | |
| 81 | inline llvm::Module *getModule() const { |
| 82 | return module; |
| 83 | } |
| 84 | |
Stephen Hines | 274926b | 2012-05-03 12:30:46 -0700 | [diff] [blame] | 85 | inline llvm::LLVMContext *getContext() const { |
| 86 | return (module) ? &module->getContext() : NULL; |
| 87 | } |
| 88 | |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame] | 89 | // Share with the given context if it's provided. |
Stephen Hines | 274926b | 2012-05-03 12:30:46 -0700 | [diff] [blame] | 90 | int prepareModule(llvm::LLVMContext *context = NULL); |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame] | 91 | |
| 92 | template <typename T> void introDependency(T &checker); |
| 93 | |
| 94 | ~SourceInfo(); |
| 95 | }; |
| 96 | |
| 97 | |
| 98 | } // namespace bcc |
| 99 | |
| 100 | #endif // BCC_SOURCEINFO_H |