1 //===--- CIRGenModule.h - Per-Module state for CIR gen ----------*- C++ -*-===//
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 is the internal per-translation-unit state used for CIR translation.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENMODULE_H
14 #define LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENMODULE_H
16 #include "CIRGenBuilder.h"
17 #include "CIRGenTypeCache.h"
18 #include "CIRGenTypes.h"
20 #include "mlir/IR/Builders.h"
21 #include "mlir/IR/BuiltinOps.h"
22 #include "mlir/IR/MLIRContext.h"
23 #include "clang/Basic/SourceManager.h"
24 #include "llvm/ADT/StringRef.h"
37 /// This class organizes the cross-function state that is used while generating
39 class CIRGenModule
: public CIRGenTypeCache
{
40 CIRGenModule(CIRGenModule
&) = delete;
41 CIRGenModule
&operator=(CIRGenModule
&) = delete;
44 CIRGenModule(mlir::MLIRContext
&mlirContext
, clang::ASTContext
&astContext
,
45 const clang::CodeGenOptions
&cgo
,
46 clang::DiagnosticsEngine
&diags
);
48 ~CIRGenModule() = default;
51 CIRGenBuilderTy builder
;
53 /// Hold Clang AST information.
54 clang::ASTContext
&astContext
;
56 const clang::LangOptions
&langOpts
;
58 /// A "module" matches a c/cpp source file: containing a list of functions.
59 mlir::ModuleOp theModule
;
61 clang::DiagnosticsEngine
&diags
;
63 const clang::TargetInfo
&target
;
68 mlir::ModuleOp
getModule() const { return theModule
; }
69 CIRGenBuilderTy
&getBuilder() { return builder
; }
70 clang::ASTContext
&getASTContext() const { return astContext
; }
71 CIRGenTypes
&getTypes() { return genTypes
; }
72 mlir::MLIRContext
&getMLIRContext() { return *builder
.getContext(); }
74 /// Helpers to convert the presumed location of Clang's SourceLocation to an
76 mlir::Location
getLoc(clang::SourceLocation cLoc
);
77 mlir::Location
getLoc(clang::SourceRange cRange
);
79 void emitTopLevelDecl(clang::Decl
*decl
);
81 /// Emit code for a single global function or variable declaration. Forward
82 /// declarations are emitted lazily.
83 void emitGlobal(clang::GlobalDecl gd
);
85 void emitGlobalDefinition(clang::GlobalDecl gd
,
86 mlir::Operation
*op
= nullptr);
87 void emitGlobalFunctionDefinition(clang::GlobalDecl gd
, mlir::Operation
*op
);
88 void emitGlobalVarDefinition(const clang::VarDecl
*vd
,
89 bool isTentative
= false);
91 /// Helpers to emit "not yet implemented" error diagnostics
92 DiagnosticBuilder
errorNYI(SourceLocation
, llvm::StringRef
);
95 DiagnosticBuilder
errorNYI(SourceLocation loc
, llvm::StringRef feature
,
98 diags
.getCustomDiagID(DiagnosticsEngine::Error
,
99 "ClangIR code gen Not Yet Implemented: %0: %1");
100 return diags
.Report(loc
, diagID
) << feature
<< name
;
103 DiagnosticBuilder
errorNYI(SourceRange
, llvm::StringRef
);
105 template <typename T
>
106 DiagnosticBuilder
errorNYI(SourceRange loc
, llvm::StringRef feature
,
108 return errorNYI(loc
.getBegin(), feature
, name
) << loc
;
111 } // namespace CIRGen
115 #endif // LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENMODULE_H