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
26namespace llvm {
27  class LLVMContext;
28}
29
30namespace 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;
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;
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:
67    SourceInfo() : module(NULL), shared_context(false) { }
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
85    inline llvm::LLVMContext *getContext() const {
86      return (module) ? &module->getContext() : NULL;
87    }
88
89    // Share with the given context if it's provided.
90    int prepareModule(llvm::LLVMContext *context = NULL);
91
92    template <typename T> void introDependency(T &checker);
93
94    ~SourceInfo();
95  };
96
97
98} // namespace bcc
99
100#endif // BCC_SOURCEINFO_H
101