slang_backend.h revision 3fd0a94a5cf1656569b1aea07043cc63939dcb46
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#include "llvm/Support/StandardPasses.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 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 84 // This handler will be invoked before Clang translates @Ctx to LLVM IR. This 85 // give you an opportunity to modified the IR in AST level (scope information, 86 // unoptimized IR, etc.). After the return from this method, slang will start 87 // translate @Ctx into LLVM IR. One should not operate on @Ctx afterwards 88 // since the changes applied on that never reflects to the LLVM module used 89 // in the final codegen. 90 virtual void HandleTranslationUnitPre(clang::ASTContext &Ctx) { return; } 91 92 // This handler will be invoked when Clang have converted AST tree to LLVM IR. 93 // The @M contains the resulting LLVM IR tree. After the return from this 94 // method, slang will start doing optimization and code generation for @M. 95 virtual void HandleTranslationUnitPost(llvm::Module *M) { return; } 96 97 public: 98 Backend(clang::Diagnostic *Diags, 99 const clang::CodeGenOptions &CodeGenOpts, 100 const clang::TargetOptions &TargetOpts, 101 PragmaList *Pragmas, 102 llvm::raw_ostream *OS, 103 Slang::OutputType OT); 104 105 // Initialize - This is called to initialize the consumer, providing the 106 // ASTContext. 107 virtual void Initialize(clang::ASTContext &Ctx); 108 109 // HandleTopLevelDecl - Handle the specified top-level declaration. This is 110 // called by the parser to process every top-level Decl*. Note that D can be 111 // the head of a chain of Decls (e.g. for `int a, b` the chain will have two 112 // elements). Use Decl::getNextDeclarator() to walk the chain. 113 virtual void HandleTopLevelDecl(clang::DeclGroupRef D); 114 115 // HandleTranslationUnit - This method is called when the ASTs for entire 116 // translation unit have been parsed. 117 virtual void HandleTranslationUnit(clang::ASTContext &Ctx); 118 119 // HandleTagDeclDefinition - This callback is invoked each time a TagDecl 120 // (e.g. struct, union, enum, class) is completed. This allows the client to 121 // hack on the type, which can occur at any point in the file (because these 122 // can be defined in declspecs). 123 virtual void HandleTagDeclDefinition(clang::TagDecl *D); 124 125 // CompleteTentativeDefinition - Callback invoked at the end of a translation 126 // unit to notify the consumer that the given tentative definition should be 127 // completed. 128 virtual void CompleteTentativeDefinition(clang::VarDecl *D); 129 130 virtual ~Backend(); 131}; 132 133} // namespace slang 134 135#endif // _FRAMEWORKS_COMPILE_SLANG_SLANG_BACKEND_H_ NOLINT 136