blob: 9026f1cd603c2b755fe40edd1093e8db66970ec9 [file] [log] [blame]
Logan474cbd22011-01-31 01:47:44 +08001/*
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
Logan474cbd22011-01-31 01:47:44 +080022#include <llvm/Module.h>
23
24#include <stddef.h>
25
Zonr Changdf3fee42012-01-10 15:58:36 +080026namespace llvm {
27 class LLVMContext;
28}
Logan474cbd22011-01-31 01:47:44 +080029
Zonr Changdf3fee42012-01-10 15:58:36 +080030namespace bcc {
Logan474cbd22011-01-31 01:47:44 +080031 namespace SourceKind {
32 enum SourceType {
33 File,
34 Buffer,
35 Module,
36 };
37 }
38
39 class SourceInfo {
40 private:
41 SourceKind::SourceType type;
42
Logan474cbd22011-01-31 01:47:44 +080043 // 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 Changdf3fee42012-01-10 15:58:36 +080045 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;
Logan474cbd22011-01-31 01:47:44 +080049
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 Changdf3fee42012-01-10 15:58:36 +080069 SourceInfo() : module(NULL), shared_context(false) { }
Logan474cbd22011-01-31 01:47:44 +080070
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 Changdf3fee42012-01-10 15:58:36 +080083 inline llvm::Module *getModule() const {
84 return module;
Logan474cbd22011-01-31 01:47:44 +080085 }
86
Zonr Changdf3fee42012-01-10 15:58:36 +080087 inline llvm::LLVMContext *getContext() const {
88 return (module) ? &module->getContext() : NULL;
Logan474cbd22011-01-31 01:47:44 +080089 }
90
Zonr Changdf3fee42012-01-10 15:58:36 +080091 // Share with the given context if it's provided.
92 int prepareModule(llvm::LLVMContext *context = NULL);
Logan474cbd22011-01-31 01:47:44 +080093
94#if USE_CACHE
95 template <typename T> void introDependency(T &checker);
96#endif
Zonr Changdf3fee42012-01-10 15:58:36 +080097
98 ~SourceInfo();
Logan474cbd22011-01-31 01:47:44 +080099 };
100
101
102} // namespace bcc
103
104#endif // BCC_SOURCEINFO_H