1 //===--- CIRGenerator.cpp - Emit CIR from ASTs ----------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This builds an AST and converts it to CIR. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "CIRGenModule.h" 14 15 #include "mlir/IR/MLIRContext.h" 16 17 #include "clang/AST/DeclGroup.h" 18 #include "clang/CIR/CIRGenerator.h" 19 #include "clang/CIR/Dialect/IR/CIRDialect.h" 20 21 using namespace cir; 22 using namespace clang; 23 24 void CIRGenerator::anchor() {} 25 26 CIRGenerator::CIRGenerator(clang::DiagnosticsEngine &diags, 27 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs, 28 const CodeGenOptions &cgo) 29 : diags(diags), fs(std::move(vfs)), codeGenOpts{cgo} {} 30 CIRGenerator::~CIRGenerator() = default; 31 32 void CIRGenerator::Initialize(ASTContext &astContext) { 33 using namespace llvm; 34 35 this->astContext = &astContext; 36 37 mlirContext = std::make_unique<mlir::MLIRContext>(); 38 mlirContext->loadDialect<cir::CIRDialect>(); 39 cgm = std::make_unique<clang::CIRGen::CIRGenModule>( 40 *mlirContext.get(), astContext, codeGenOpts, diags); 41 } 42 43 mlir::ModuleOp CIRGenerator::getModule() const { return cgm->getModule(); } 44 45 bool CIRGenerator::HandleTopLevelDecl(DeclGroupRef group) { 46 47 for (Decl *decl : group) 48 cgm->emitTopLevelDecl(decl); 49 50 return true; 51 } 52