Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [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 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 22 | #include <llvm/Module.h> |
| 23 | |
| 24 | #include <stddef.h> |
| 25 | |
Zonr Chang | df3fee4 | 2012-01-10 15:58:36 +0800 | [diff] [blame^] | 26 | namespace llvm { |
| 27 | class LLVMContext; |
| 28 | } |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 29 | |
Zonr Chang | df3fee4 | 2012-01-10 15:58:36 +0800 | [diff] [blame^] | 30 | namespace bcc { |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 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 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 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. |
Zonr Chang | df3fee4 | 2012-01-10 15:58:36 +0800 | [diff] [blame^] | 45 | llvm::Module *module; |
| 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; |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [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 | #if USE_CACHE |
| 65 | unsigned char sha1[20]; |
| 66 | #endif |
| 67 | |
| 68 | private: |
Zonr Chang | df3fee4 | 2012-01-10 15:58:36 +0800 | [diff] [blame^] | 69 | SourceInfo() : module(NULL), shared_context(false) { } |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 70 | |
| 71 | public: |
| 72 | static SourceInfo *createFromBuffer(char const *resName, |
| 73 | char const *bitcode, |
| 74 | size_t bitcodeSize, |
| 75 | unsigned long flags); |
| 76 | |
| 77 | static SourceInfo *createFromFile(char const *path, |
| 78 | unsigned long flags); |
| 79 | |
| 80 | static SourceInfo *createFromModule(llvm::Module *module, |
| 81 | unsigned long flags); |
| 82 | |
Zonr Chang | df3fee4 | 2012-01-10 15:58:36 +0800 | [diff] [blame^] | 83 | inline llvm::Module *getModule() const { |
| 84 | return module; |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 85 | } |
| 86 | |
Zonr Chang | df3fee4 | 2012-01-10 15:58:36 +0800 | [diff] [blame^] | 87 | inline llvm::LLVMContext *getContext() const { |
| 88 | return (module) ? &module->getContext() : NULL; |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 89 | } |
| 90 | |
Zonr Chang | df3fee4 | 2012-01-10 15:58:36 +0800 | [diff] [blame^] | 91 | // Share with the given context if it's provided. |
| 92 | int prepareModule(llvm::LLVMContext *context = NULL); |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 93 | |
| 94 | #if USE_CACHE |
| 95 | template <typename T> void introDependency(T &checker); |
| 96 | #endif |
Zonr Chang | df3fee4 | 2012-01-10 15:58:36 +0800 | [diff] [blame^] | 97 | |
| 98 | ~SourceInfo(); |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | |
| 102 | } // namespace bcc |
| 103 | |
| 104 | #endif // BCC_SOURCEINFO_H |