clang 20.0.0git
ConstantEmitter.h
Go to the documentation of this file.
1//===--- ConstantEmitter.h - IR constant emission ---------------*- 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// A helper class for emitting expressions and values as llvm::Constants
10// and as initializers for global variables.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_LIB_CODEGEN_CONSTANTEMITTER_H
15#define LLVM_CLANG_LIB_CODEGEN_CONSTANTEMITTER_H
16
17#include "CodeGenFunction.h"
18#include "CodeGenModule.h"
19
20namespace clang {
21namespace CodeGen {
22
24public:
27
28private:
29 bool Abstract = false;
30
31 /// Whether non-abstract components of the emitter have been initialized.
32 bool InitializedNonAbstract = false;
33
34 /// Whether the emitter has been finalized.
35 bool Finalized = false;
36
37 /// Whether the constant-emission failed.
38 bool Failed = false;
39
40 /// Whether we're in a constant context.
41 bool InConstantContext = false;
42
43 /// The AST address space where this (non-abstract) initializer is going.
44 /// Used for generating appropriate placeholders.
45 LangAS DestAddressSpace = LangAS::Default;
46
48 PlaceholderAddresses;
49
50public:
52 : CGM(CGM), CGF(CGF) {}
53
54 /// Initialize this emission in the context of the given function.
55 /// Use this if the expression might contain contextual references like
56 /// block addresses or PredefinedExprs.
58 : CGM(CGF.CGM), CGF(&CGF) {}
59
60 ConstantEmitter(const ConstantEmitter &other) = delete;
61 ConstantEmitter &operator=(const ConstantEmitter &other) = delete;
62
64
65 /// Is the current emission context abstract?
66 bool isAbstract() const {
67 return Abstract;
68 }
69
70 bool isInConstantContext() const { return InConstantContext; }
71 void setInConstantContext(bool var) { InConstantContext = var; }
72
73 /// Try to emit the initiaizer of the given declaration as an abstract
74 /// constant. If this succeeds, the emission must be finalized.
75 llvm::Constant *tryEmitForInitializer(const VarDecl &D);
76 llvm::Constant *tryEmitForInitializer(const Expr *E, LangAS destAddrSpace,
77 QualType destType);
78 llvm::Constant *emitForInitializer(const APValue &value, LangAS destAddrSpace,
79 QualType destType);
80
81 void finalize(llvm::GlobalVariable *global);
82
83 // All of the "abstract" emission methods below permit the emission to
84 // be immediately discarded without finalizing anything. Therefore, they
85 // must also promise not to do anything that will, in the future, require
86 // finalization:
87 //
88 // - using the CGF (if present) for anything other than establishing
89 // semantic context; for example, an expression with ignored
90 // side-effects must not be emitted as an abstract expression
91 //
92 // - doing anything that would not be safe to duplicate within an
93 // initializer or to propagate to another context; for example,
94 // side effects, or emitting an initialization that requires a
95 // reference to its current location.
96
97 /// Try to emit the initializer of the given declaration as an abstract
98 /// constant.
99 llvm::Constant *tryEmitAbstractForInitializer(const VarDecl &D);
100
101 /// Emit the result of the given expression as an abstract constant,
102 /// asserting that it succeeded. This is only safe to do when the
103 /// expression is known to be a constant expression with either a fairly
104 /// simple type or a known simple form.
105 llvm::Constant *emitAbstract(const Expr *E, QualType T);
106 llvm::Constant *
107 emitAbstract(SourceLocation loc, const APValue &value, QualType