1 //===--- CIRGenModule.h - Per-Module state for CIR gen ----------*- C++ -*-===// 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 is the internal per-translation-unit state used for CIR translation. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENMODULE_H 14 #define LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENMODULE_H 15 16 #include "CIRGenBuilder.h" 17 #include "CIRGenTypeCache.h" 18 #include "CIRGenTypes.h" 19 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" 25 26 namespace clang { 27 class ASTContext; 28 class CodeGenOptions; 29 class Decl; 30 class GlobalDecl; 31 class LangOptions; 32 class TargetInfo; 33 class VarDecl; 34 35 namespace CIRGen { 36 37 /// This class organizes the cross-function state that is used while generating 38 /// CIR code. 39 class CIRGenModule : public CIRGenTypeCache { 40 CIRGenModule(CIRGenModule &) = delete; 41 CIRGenModule &operator=(CIRGenModule &) = delete; 42 43 public: 44 CIRGenModule(mlir::MLIRContext &mlirContext, clang::ASTContext &astContext, 45 const clang::CodeGenOptions &cgo, 46 clang::DiagnosticsEngine &diags); 47 48 ~CIRGenModule() = default; 49 50 private: 51 CIRGenBuilderTy builder; 52 53 /// Hold Clang AST information. 54 clang::ASTContext &astContext; 55 56 const clang::LangOptions &langOpts; 57 58 /// A "module" matches a c/cpp source file: containing a list of functions. 59 mlir::ModuleOp theModule; 60 61 clang::DiagnosticsEngine &diags; 62 63 const clang::TargetInfo ⌖ 64 65 CIRGenTypes genTypes; 66 67 public: 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(); } 73 74 /// Helpers to convert the presumed location of Clang's SourceLocation to an 75 /// MLIR Location. 76 mlir::Location getLoc(clang::SourceLocation cLoc); 77 mlir::Location getLoc(clang::SourceRange cRange); 78 79 void emitTopLevelDecl(clang::Decl *decl); 80 81 /// Emit code for a single global function or variable declaration. Forward 82 /// declarations are emitted lazily. 83 void emitGlobal(clang::GlobalDecl gd); 84 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); 90 91 /// Helpers to emit "not yet implemented" error diagnostics 92 DiagnosticBuilder errorNYI(SourceLocation, llvm::StringRef); 93 94 template <typename T> 95 DiagnosticBuilder errorNYI(SourceLocation loc, llvm::StringRef feature, 96 const T &name) { 97 unsigned diagID = 98 diags.getCustomDiagID(DiagnosticsEngine::Error, 99 "ClangIR code gen Not Yet Implemented: %0: %1"); 100 return diags.Report(loc, diagID) << feature << name; 101 } 102 103 DiagnosticBuilder errorNYI(SourceRange, llvm::StringRef); 104 105 template <typename T> 106 DiagnosticBuilder errorNYI(SourceRange loc, llvm::StringRef feature, 107 const T &name) { 108 return errorNYI(loc.getBegin(), feature, name) << loc; 109 } 110 }; 111 } // namespace CIRGen 112 113 } // namespace clang 114 115 #endif // LLVM_CLANG_LIB_CIR_CODEGEN_CIRGENMODULE_H 116