1 //===--- CIRGenerator.cpp - Emit CIR from ASTs ----------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This builds an AST and converts it to CIR.
11 //===----------------------------------------------------------------------===//
13 #include "CIRGenModule.h"
15 #include "mlir/IR/MLIRContext.h"
17 #include "clang/AST/DeclGroup.h"
18 #include "clang/CIR/CIRGenerator.h"
19 #include "clang/CIR/Dialect/IR/CIRDialect.h"
22 using namespace clang
;
24 void CIRGenerator::anchor() {}
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;
32 void CIRGenerator::Initialize(ASTContext
&astContext
) {
35 this->astContext
= &astContext
;
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
);
43 mlir::ModuleOp
CIRGenerator::getModule() const { return cgm
->getModule(); }
45 bool CIRGenerator::HandleTopLevelDecl(DeclGroupRef group
) {
47 for (Decl
*decl
: group
)
48 cgm
->emitTopLevelDecl(decl
);