slang_backend.h revision 41ebf534161bb67f6207a070c1f6a895dc853408
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 _SLANG_COMPILER_BACKEND_H
18#define _SLANG_COMPILER_BACKEND_H
19
20#include "llvm/PassManager.h"
21
22#include "llvm/Support/StandardPasses.h"
23#include "llvm/Support/FormattedStream.h"
24
25#include "clang/AST/ASTConsumer.h"
26
27#include "slang.h"
28#include "slang_pragma_recorder.h"
29
30namespace llvm {
31  class formatted_raw_ostream;
32  class LLVMContext;
33  class NamedMDNode;
34  class Module;
35  class PassManager;
36  class FunctionPassManager;
37}
38
39namespace clang {
40  class CodeGenOptions;
41  class CodeGenerator;
42  class DeclGroupRef;
43  class TagDecl;
44  class VarDecl;
45}
46
47namespace slang {
48
49class Backend : public clang::ASTConsumer {
50 private:
51  const clang::CodeGenOptions &mCodeGenOpts;
52  const clang::TargetOptions &mTargetOpts;
53
54  // Output stream
55  llvm::raw_ostream *mpOS;
56  Slang::OutputType mOT;
57
58  // This helps us translate Clang AST using into LLVM IR
59  clang::CodeGenerator *mGen;
60
61  // Passes
62
63  // Passes apply on function scope in a translation unit
64  llvm::FunctionPassManager *mPerFunctionPasses;
65  // Passes apply on module scope
66  llvm::PassManager *mPerModulePasses;
67  // Passes for code emission
68  llvm::FunctionPassManager *mCodeGenPasses;
69
70  llvm::formatted_raw_ostream FormattedOutStream;
71
72  void CreateFunctionPasses();
73  void CreateModulePasses();
74  bool CreateCodeGenPasses();
75
76 protected:
77  llvm::LLVMContext &mLLVMContext;
78  clang::Diagnostic &mDiags;
79
80  llvm::Module *mpModule;
81
82  const PragmaList &mPragmas;
83
84  // Extra handler for subclass to handle translation unit before emission
85  virtual void HandleTranslationUnitEx(clang::ASTContext &Ctx) { return; }
86
87 public:
88  Backend(clang::Diagnostic &Diags,
89          const clang::CodeGenOptions &CodeGenOpts,
90          const clang::TargetOptions &TargetOpts,
91          const PragmaList &Pragmas,
92          llvm::raw_ostream *OS,
93          Slang::OutputType OT);
94
95  // Initialize - This is called to initialize the consumer, providing the
96  // ASTContext.
97  virtual void Initialize(clang::ASTContext &Ctx);
98
99  // HandleTopLevelDecl - Handle the specified top-level declaration.  This is
100  // called by the parser to process every top-level Decl*. Note that D can be
101  // the head of a chain of Decls (e.g. for `int a, b` the chain will have two
102  // elements). Use Decl::getNextDeclarator() to walk the chain.
103  virtual void HandleTopLevelDecl(clang::DeclGroupRef D);
104
105  // HandleTranslationUnit - This method is called when the ASTs for entire
106  // translation unit have been parsed.
107  virtual void HandleTranslationUnit(clang::ASTContext &Ctx);
108
109  // HandleTagDeclDefinition - This callback is invoked each time a TagDecl
110  // (e.g. struct, union, enum, class) is completed.  This allows the client to
111  // hack on the type, which can occur at any point in the file (because these
112  // can be defined in declspecs).
113  virtual void HandleTagDeclDefinition(clang::TagDecl *D);
114
115  // CompleteTentativeDefinition - Callback invoked at the end of a translation
116  // unit to notify the consumer that the given tentative definition should be
117  // completed.
118  virtual void CompleteTentativeDefinition(clang::VarDecl *D);
119
120  virtual ~Backend();
121};
122
123}   // namespace slang
124
125#endif  // _SLANG_COMPILER_BACKEND_H
126