blob: 080d3b4809d5845bb7283a45fa414463b4554b02 [file] [log] [blame]
Stephen Hinesead5ccb2012-05-03 12:30:38 -07001/*
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 Hines274926b2012-05-03 12:30:46 -070026namespace llvm {
27 class LLVMContext;
28}
29
Stephen Hinesead5ccb2012-05-03 12:30:38 -070030namespace 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 Hines274926b2012-05-03 12:30:46 -070046 // 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 Hinesead5ccb2012-05-03 12:30:38 -070049
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 Hines274926b2012-05-03 12:30:46 -070067 SourceInfo() : module(NULL), shared_context(false) { }
Stephen Hinesead5ccb2012-05-03 12:30:38 -070068
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 Hines274926b2012-05-03 12:30:46 -070085 inline llvm::LLVMContext *getContext() const {
86 return (module) ? &module->getContext() : NULL;
87 }
88
Stephen Hinesead5ccb2012-05-03 12:30:38 -070089 // Share with the given context if it's provided.
Stephen Hines274926b2012-05-03 12:30:46 -070090 int prepareModule(llvm::LLVMContext *context = NULL);
Stephen Hinesead5ccb2012-05-03 12:30:38 -070091
92 template <typename T> void introDependency(T &checker);
93
94 ~SourceInfo();
95 };
96
97
98} // namespace bcc
99
100#endif // BCC_SOURCEINFO_H