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