clang 20.0.0git
Compiler.h
Go to the documentation of this file.
1//===--- Compiler.h - Code generator for expressions -----*- 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// Defines the constexpr bytecode compiler.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_INTERP_BYTECODEEXPRGEN_H
14#define LLVM_CLANG_AST_INTERP_BYTECODEEXPRGEN_H
15
16#include "ByteCodeEmitter.h"
17#include "EvalEmitter.h"
18#include "Pointer.h"
19#include "PrimType.h"
20#include "Record.h"
21#include "clang/AST/Decl.h"
22#include "clang/AST/Expr.h"
25
26namespace clang {
27class QualType;
28
29namespace interp {
30
31template <class Emitter> class LocalScope;
32template <class Emitter> class DestructorScope;
33template <class Emitter> class VariableScope;
34template <class Emitter> class DeclScope;
35template <class Emitter> class InitLinkScope;
36template <class Emitter> class InitStackScope;
37template <class Emitter> class OptionScope;
38template <class Emitter> class ArrayIndexScope;
39template <class Emitter> class SourceLocScope;
40template <class Emitter> class LoopScope;
41template <class Emitter> class LabelScope;
42template <class Emitter> class SwitchScope;
43template <class Emitter> class StmtExprScope;
44
45template <class Emitter> class Compiler;
46struct InitLink {
47public:
48 enum {
49 K_This = 0,
51 K_Temp = 2,
52 K_Decl = 3,
53 K_Elem = 5,
54 };
55
56 static InitLink This() { return InitLink{K_This}; }
57 static InitLink Field(unsigned Offset) {
58 InitLink IL{K_Field};
59 IL.Offset = Offset;
60 return IL;
61 }
62 static InitLink Temp(unsigned Offset) {
63 InitLink IL{K_Temp};
64 IL.Offset = Offset;
65 return IL;
66 }
67 static InitLink Decl(const ValueDecl *D) {
68 InitLink IL{K_Decl};
69 IL.D = D;
70 return IL;
71 }
72 static InitLink Elem(unsigned Index) {
73 InitLink IL{K_Elem};
74 IL.Offset = Index;
75 return IL;
76 }
77
78 InitLink(uint8_t Kind) : Kind(Kind) {}
79 template <class Emitter>
80 bool emit(Compiler<Emitter> *Ctx, const Expr *E) const;
81
82 uint32_t Kind;
83 union {
84 unsigned Offset;
85 const ValueDecl *D;
86 };
87};
88
89/// State encapsulating if a the variable creation has been successful,
90/// unsuccessful, or no variable has been created at all.
92 std::optional<bool> S = std::nullopt;
93 VarCreationState() = default;
94 VarCreationState(bool b) : S(b) {}
96
97 operator bool() const { return S && *S; }
98 bool notCreated() const { return !S; }
99};
100
101/// Compilation context for expressions.
102template <class Emitter>
103class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
104 public Emitter {
105protected:
106 // Aliases for types defined in the emitter.
107 using LabelTy = typename Emitter::LabelTy;
108 using AddrTy = typename Emitter::AddrTy;
109 using OptLabelTy = std::optional<LabelTy>;
110 using CaseMap = llvm::DenseMap<const SwitchCase *, LabelTy>;
111
112 /// Current compilation context.
114 /// Program to link to.
116
117public:
118 /// Initializes the compiler and the backend emitter.
119 template <typename... Tys>
120 Compiler(Context &Ctx, Program &P, Tys &&...Args)
121 : Emitter(Ctx, P, Args...), Ctx(Ctx), P(P) {}
122
123 // Expressions.
124 bool VisitCastExpr(const CastExpr *E);
128 bool VisitParenExpr(const ParenExpr *E);
134 bool VisitCallExpr(const CallExpr *E);
135 bool VisitBuiltinCallExpr(const CallExpr *E);
139 bool VisitGNUNullExpr(const GNUNullExpr *E);
140 bool VisitCXXThisExpr(const CXXThisExpr *E);
143 bool VisitDeclRefExpr(const DeclRefExpr *E);
147 bool VisitInitListExpr(const InitListExpr *E);
149 bool VisitConstantExpr(const ConstantExpr *E);
151 bool VisitMemberExpr(const MemberExpr *E);
170 bool VisitLambdaExpr(const LambdaExpr *E);
172 bool VisitCXXThrowExpr(const CXXThrowExpr *E);
177 bool VisitOffsetOfExpr(const OffsetOfExpr *E);
181 bool VisitChooseExpr(const ChooseExpr *E);
182 bool VisitEmbedExpr(const EmbedExpr *E);
187 bool VisitRequiresExpr(const RequiresExpr *E);
192 bool VisitRecoveryExpr(const RecoveryExpr *E);
199 bool VisitStmtExpr(const StmtExpr *E);
200 bool VisitCXXNewExpr(const CXXNewExpr *E);
202 bool VisitBlockExpr(const BlockExpr *E);
203
204 // Statements.
205 bool visitCompoundStmt(const CompoundStmt *S);
206 bool visitDeclStmt(const DeclStmt *DS);
207 bool visitReturnStmt(const ReturnStmt *RS);
208 bool visitIfStmt(const IfStmt *IS);
209 bool visitWhileStmt(const WhileStmt *S);
210 bool visitDoStmt(const DoStmt *S);
211 bool visitForStmt(const ForStmt *S);
213 bool visitBreakStmt(const BreakStmt *S);
214 bool visitContinueStmt(const ContinueStmt *S);
215 bool visitSwitchStmt(const SwitchStmt *S);
216 bool visitCaseStmt(const CaseStmt *S);
217 bool visitDefaultStmt(const DefaultStmt *S);
218 bool visitAttributedStmt(const AttributedStmt *S);
219 bool visitCXXTryStmt(const CXXTryStmt *S);
220
221protected:
222 bool visitStmt(const Stmt *S);
223 bool visitExpr(const Expr *E) override;
224 bool visitFunc(const FunctionDecl *F) override;
225
226 bool visitDeclAndReturn(const VarDecl *VD, bool ConstantContext) override;
227
228protected:
229 /// Emits scope cleanup instructions.
230 void emitCleanup();
231
232 /// Returns a record type from a record or pointer type.
234
235 /// Returns a record from a record or pointer type.
237 Record *getRecord(const RecordDecl *RD);
238
239 /// Returns a function for the given FunctionDecl.
240 /// If the function does not exist yet, it is compiled.
241 const Function *getFunction(const FunctionDecl *FD);
242
243 std::optional<PrimType> classify(const Expr *E) const {
244 return Ctx.classify(E);
245 }
246 std::optional<PrimType> classify(QualType Ty) const {
247 return Ctx.classify(Ty);
248 }
249
250 /// Classifies a known primitive type.
252 if (auto T = classify(Ty)) {
253 return *T;
254 }
255 llvm_unreachable("not a primitive type");
256 }
257 /// Classifies a known primitive expression.
258 PrimType classifyPrim(const Expr *E) const {
259 if (auto T = classify(E))
260 return *T;
261 llvm_unreachable("not a primitive type");
262 }
263
264 /// Evaluates an expression and places the result on the stack. If the
265 /// expression is of composite type, a local variable will be created
266 /// and a pointer to said variable will be placed on the stack.
267 bool visit(const Expr *E);
268 /// Compiles an initializer. This is like visit() but it will never
269 /// create a variable and instead rely on a variable already having
270 /// been created. visitInitializer() then relies on a pointer to this
271 /// variable being on top of the stack.
272 bool visitInitializer(const Expr *E);
273 /// Evaluates an expression for side effects and discards the result.
274 bool discard(const Expr *E);
275 /// Just pass evaluation on to \p E. This leaves all the parsing flags
276 /// intact.
277 bool delegate(const Expr *E);
278 /// Creates and initializes a variable from the given decl.
279 VarCreationState visitVarDecl(const VarDecl *VD, bool Toplevel = false);
281 /// Visit an APValue.
282 bool visitAPValue(const APValue &Val, PrimType ValType, const Expr *E);
283 bool visitAPValueInitializer(const APValue &Val, const Expr *E);
284 /// Visit the given decl as if we have a reference to it.
285 bool visitDeclRef(const ValueDecl *D, const Expr *E);
286
287 /// Visits an expression and converts it to a boolean.
288 bool visitBool(const Expr *E);
289
290 bool visitInitList(ArrayRef<const Expr *> Inits, const Expr *ArrayFiller,
291 const Expr *E);
292 bool visitArrayElemInit(unsigned ElemIndex, const Expr *Init);
293
294 /// Creates a local primitive value.
295 unsigned allocateLocalPrimitive(DeclTy &&Decl, PrimType Ty, bool IsConst,
296 bool IsExtended = false);
297
298 /// Allocates a space storing a local given its type.
299 std::optional<unsigned>
300 allocateLocal(DeclTy &&Decl, const ValueDecl *ExtendingDecl = nullptr);
301 unsigned allocateTemporary(const Expr *E);
302
303private:
304 friend class VariableScope<Emitter>;
305 friend class LocalScope<Emitter>;
306 friend class DestructorScope<Emitter>;
307 friend class DeclScope<Emitter>;
308 friend class InitLinkScope<Emitter>;
309 friend class InitStackScope<Emitter>;
310 friend class OptionScope<Emitter>;
311 friend class ArrayIndexScope<Emitter>;
312 friend class SourceLocScope<Emitter>;
313 friend struct InitLink;
314 friend class LoopScope<Emitter>;
315 friend class LabelScope<Emitter>;
316 friend class SwitchScope<Emitter>;
317 friend class StmtExprScope<Emitter>;
318
319 /// Emits a zero initializer.
320 bool visitZeroInitializer(PrimType T, QualType QT, const Expr *E);
321 bool visitZeroRecordInitializer(const Record *R, const Expr *E);
322
323 /// Emits an APSInt constant.
324 bool emitConst(const llvm::APSInt &Value, PrimType Ty, const Expr *E);
325 bool emitConst(const llvm::APSInt &Value, const Expr *E);
326 bool emitConst(const llvm::APInt &Value, const Expr *E) {
327 return emitConst(static_cast<llvm::APSInt>(Value), E);
328 }
329
330 /// Emits an integer constant.
331 template <typename T> bool emitConst(T Value, PrimType Ty, const Expr *E);
332 template <typename T> bool emitConst(T Value, const Expr *E);
333
334 llvm::RoundingMode getRoundingMode(const Expr *E) const {
335 FPOptions FPO = E->getFPFeaturesInEffect(Ctx.getLangOpts());
336
337 if (FPO.getRoundingMode() == llvm::RoundingMode::Dynamic)
338 return llvm::RoundingMode::NearestTiesToEven;
339
340 return FPO.getRoundingMode();
341 }
342
343 bool emitPrimCast(PrimType FromT, PrimType ToT, QualType ToQT, const Expr *E);
344 PrimType classifyComplexElementType(QualType T) const {
345 assert(T->isAnyComplexType());
346
347 QualType ElemType = T->getAs<ComplexType>()->getElementType();
348
349 return *this->classify(ElemType);
350 }
351
352 bool emitComplexReal(const Expr *SubExpr);
353 bool emitComplexBoolCast(const Expr *E);
354 bool emitComplexComparison(const Expr *LHS, const Expr *RHS,
355 const BinaryOperator *E);
356
357 bool emitRecordDestruction(const Record *R);
358 bool emitDestruction(const Descriptor *Desc);
359 unsigned collectBaseOffset(const QualType BaseType,
360 const QualType DerivedType);
361 bool emitLambdaStaticInvokerBody(const CXXMethodDecl *MD);
362 bool compileConstructor(const CXXConstructorDecl *Ctor);
363 bool compileDestructor(const CXXDestructorDecl *Dtor);
364
365 bool checkLiteralType(const Expr *E);
366
367protected:
368 /// Variable to storage mapping.
369 llvm::DenseMap<const ValueDecl *, Scope::Local> Locals;
370
371 /// OpaqueValueExpr to location mapping.
372 llvm::DenseMap<const OpaqueValueExpr *, unsigned> OpaqueExprs;
373
374 /// Current scope.
376
377 /// Current argument index. Needed to emit ArrayInitIndexExpr.
378 std::optional<uint64_t> ArrayIndex;
379
380 /// DefaultInit- or DefaultArgExpr, needed for SourceLocExpr.
381 const Expr *SourceLocDefaultExpr = nullptr;
382
383 /// Flag indicating if return value is to be discarded.
384 bool DiscardResult = false;
385
386 bool InStmtExpr = false;
387
388 /// Flag inidicating if we're initializing an already created
389 /// variable. This is set in visitInitializer().
390 bool Initializing = false;
391 const ValueDecl *InitializingDecl = nullptr;
392
394 bool InitStackActive = false;
395
396 /// Type of the expression returned by the function.
397 std::optional<PrimType> ReturnType;
398
399 /// Switch case mapping.
401
402 /// Point to break to.
404 /// Point to continue to.
406 /// Default case label.
408};
409
410extern template class Compiler<ByteCodeEmitter>;
411extern template class Compiler<EvalEmitter>;
412
413/// Scope chain managing the variable lifetimes.
414template <class Emitter> class VariableScope {
415public:
417 : Ctx(Ctx), Parent(Ctx->VarScope), ValDecl(VD) {
418 Ctx->VarScope = this;
419 }
420
421 virtual ~VariableScope() { Ctx->VarScope = this->Parent; }
422
423 void add(const Scope::Local &Local, bool IsExtended) {
424 if (IsExtended)
425 this->addExtended(Local);
426 else
427 this->addLocal(Local);
428 }
429
430 virtual void addLocal(const Scope::Local &Local) {
431 if (this->Parent)
432 this->Parent->addLocal(Local);
433 }
434
435 virtual void addExtended(const Scope::Local &Local) {
436 if (this->Parent)
437 this->Parent->addExtended(Local);
438 }
439
440 void addExtended(const Scope::Local &Local, const ValueDecl *ExtendingDecl) {
441 // Walk up the chain of scopes until we find the one for ExtendingDecl.
442 // If there is no such scope, attach it to the parent one.
443 VariableScope *P = this;
444 while (P) {
445 if (P->ValDecl == ExtendingDecl) {
446 P->addLocal(Local);
447 return;
448 }
449 P = P->Parent;
450 if (!P)
451 break;
452 }
453
454 // Use the parent scope.
455 if (this->Parent)
456 this->Parent->addLocal(Local);
457 else
458 this->addLocal(Local);
459 }
460
461 virtual void emitDestruction() {}
462 virtual bool emitDestructors(const Expr *E = nullptr) { return true; }
463 virtual bool destroyLocals(const Expr *E = nullptr) { return true; }
464 VariableScope *getParent() const { return Parent; }
465
466protected:
467 /// Compiler instance.
469 /// Link to the parent scope.
471 const ValueDecl *ValDecl = nullptr;
472};
473
474/// Generic scope for local variables.
475template <class Emitter> class LocalScope : public VariableScope<Emitter> {
476public:
479 : VariableScope<Emitter>(Ctx, VD) {}
480
481 /// Emit a Destroy op for this scope.
482 ~LocalScope() override {
483 if (!Idx)
484 return;
485 this->Ctx->emitDestroy(*Idx, SourceInfo{});
486 removeStoredOpaqueValues();
487 }
488
489 /// Overriden to support explicit destruction.
490 void emitDestruction() override {
491 if (!Idx)
492 return;
493
494 this->emitDestructors();
495 this->Ctx->emitDestroy(*Idx, SourceInfo{});
496 }
497
498 /// Explicit destruction of local variables.
499 bool destroyLocals(const Expr *E = nullptr) override {
500 if (!Idx)
501 return true;
502
503 bool Success = this->emitDestructors(E);
504 this->Ctx->emitDestroy(*Idx, E);
505 this->Idx = std::nullopt;
506 return Success;
507 }
508
509 void addLocal(const Scope::Local &Local) override {
510 if (!Idx) {
511 Idx = this->Ctx->Descriptors.size();
512 this->Ctx->Descriptors.emplace_back();
513 this->Ctx->emitInitScope(*Idx, {});
514 }
515
516 this->Ctx->Descriptors[*Idx].emplace_back(Local);
517 }
518
519 bool emitDestructors(const Expr *E = nullptr) override {
520 if (!Idx)
521 return true;
522 // Emit destructor calls for local variables of record
523 // type with a destructor.
524 for (Scope::Local &Local : this->Ctx->Descriptors[*Idx]) {
525 if (!Local.Desc->isPrimitive() && !Local.Desc->isPrimitiveArray()) {
526 if (!this->Ctx->emitGetPtrLocal(Local.Offset, E))
527 return false;
528
529 if (!this->Ctx->emitDestruction(Local.Desc))
530 return false;
531
532 if (!this->Ctx->emitPopPtr(E))
533 return false;
534 removeIfStoredOpaqueValue(Local);
535 }
536 }
537 return true;
538 }
539
541 if (!Idx)
542 return;
543
544 for (const Scope::Local &Local : this->Ctx->Descriptors[*Idx]) {
545 removeIfStoredOpaqueValue(Local);
546 }
547 }
548
550 if (const auto *OVE =
551 llvm::dyn_cast_if_present<OpaqueValueExpr>(Local.Desc->asExpr())) {
552 if (auto It = this->Ctx->OpaqueExprs.find(OVE);
553 It != this->Ctx->OpaqueExprs.end())
554 this->Ctx->OpaqueExprs.erase(It);
555 };
556 }
557
558 /// Index of the scope in the chain.
559 std::optional<unsigned> Idx;
560};
561
562/// Scope for storage declared in a compound statement.
563template <class Emitter> class BlockScope final : public LocalScope<Emitter> {
564public:
566
567 void addExtended(const Scope::Local &Local) override {
568 // If we to this point, just add the variable as a normal local
569 // variable. It will be destroyed at the end of the block just
570 // like all others.
571 this->addLocal(Local);
572 }
573};
574
575template <class Emitter> class ArrayIndexScope final {
576public:
577 ArrayIndexScope(Compiler<Emitter> *Ctx, uint64_t Index) : Ctx(Ctx) {
578 OldArrayIndex = Ctx->ArrayIndex;
579 Ctx->ArrayIndex = Index;
580 }
581
582 ~ArrayIndexScope() { Ctx->ArrayIndex = OldArrayIndex; }
583
584private:
586 std::optional<uint64_t> OldArrayIndex;
587};
588
589template <class Emitter> class SourceLocScope final {
590public:
591 SourceLocScope(Compiler<Emitter> *Ctx, const Expr *DefaultExpr) : Ctx(Ctx) {
592 assert(DefaultExpr);
593 // We only switch if the current SourceLocDefaultExpr is null.
594 if (!Ctx->SourceLocDefaultExpr) {
595 Enabled = true;
596 Ctx->SourceLocDefaultExpr = DefaultExpr;
597 }
598 }
599
601 if (Enabled)
602 Ctx->SourceLocDefaultExpr = nullptr;
603 }
604
605private:
607 bool Enabled = false;
608};
609
610template <class Emitter> class InitLinkScope final {
611public:
613 Ctx->InitStack.push_back(std::move(Link));
614 }
615
616 ~InitLinkScope() { this->Ctx->InitStack.pop_back(); }
617
618private:
620};
621
622template <class Emitter> class InitStackScope final {
623public:
625 : Ctx(Ctx), OldValue(Ctx->InitStackActive) {
626 Ctx->InitStackActive = Active;
627 }
628
629 ~InitStackScope() { this->Ctx->InitStackActive = OldValue; }
630
631private:
633 bool OldValue;
634};
635
636} // namespace interp
637} // namespace clang
638
639#endif
NodeId Parent
Definition: ASTDiff.cpp:191
StringRef P
const Decl * D
Expr * E
llvm::MachO::Record Record
Definition: MachO.h:31
__device__ __2f16 b
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
AbstractConditionalOperator - An abstract base class for ConditionalOperator and BinaryConditionalOpe...
Definition: Expr.h:4175
AddrLabelExpr - The GNU address of label extension, representing &&label.
Definition: Expr.h:4372
Represents the index of the current element of an array being initialized by an ArrayInitLoopExpr.
Definition: Expr.h:5756
Represents a loop initializing the elements of an array.
Definition: Expr.h:5703
ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
Definition: Expr.h:2674
An Embarcadero array type trait, as used in the implementation of __array_rank and __array_extent.
Definition: ExprCXX.h:2853
Represents an attribute applied to a statement.
Definition: Stmt.h:2090
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3860
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:6365
BreakStmt - This represents a break.
Definition: Stmt.h:2990
Represents binding an expression to a temporary.
Definition: ExprCXX.h:1491
A boolean literal, per ([C++ lex.bool] Boolean literals).
Definition: ExprCXX.h:720
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1546
A default argument (C++ [dcl.fct.default]).
Definition: ExprCXX.h:1268
A use of a default initializer in a constructor or in aggregate initialization.
Definition: ExprCXX.h:1375
Represents a delete expression for memory deallocation and destructor calls, e.g.
Definition: ExprCXX.h:2498
CXXForRangeStmt - This represents C++0x [stmt.ranged]'s ranged for statement, represented as 'for (ra...
Definition: StmtCXX.h:135
Represents a call to an inherited base class constructor from an inheriting constructor.
Definition: ExprCXX.h:1737
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)".
Definition: ExprCXX.h:2241
Represents a C++11 noexcept expression (C++ [expr.unary.noexcept]).
Definition: ExprCXX.h:4126
The null pointer literal (C++11 [lex.nullptr])
Definition: ExprCXX.h:765
Represents a list-initialization with parenthesis.
Definition: ExprCXX.h:4954
A C++ reinterpret_cast expression (C++ [expr.reinterpret.cast]).
Definition: ExprCXX.h:523
A rewritten comparison expression that was originally written using operator syntax.
Definition: ExprCXX.h:283
An expression "T()" which creates an rvalue of a non-class type T.
Definition: ExprCXX.h:2182
Implicit construction of a std::initializer_list<T> object from an array temporary within list-initia...
Definition: ExprCXX.h:797
Represents the this expression in C++.
Definition: ExprCXX.h:1152
A C++ throw-expression (C++ [except.throw]).
Definition: ExprCXX.h:1206
CXXTryStmt - A C++ try block, including all handlers.
Definition: StmtCXX.h:69
A Microsoft C++ __uuidof expression, which gets the _GUID that corresponds to the supplied type or ex...
Definition: ExprCXX.h:1066
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2830
CaseStmt - Represent a case statement.
Definition: Stmt.h:1811
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
Definition: Expr.h:3498
ChooseExpr - GNU builtin-in function __builtin_choose_expr.
Definition: Expr.h:4592
Complex values, per C99 6.2.5p11.
Definition: Type.h:3134
CompoundAssignOperator - For compound assignments (e.g.
Definition: Expr.h:4122
CompoundLiteralExpr - [C99 6.5.2.5].
Definition: Expr.h:3428
CompoundStmt - This represents a group of statements like { stmt stmt }.
Definition: Stmt.h:1611
Represents the specialization of a concept - evaluates to a prvalue of type bool.
Definition: ExprConcepts.h:42
ConstStmtVisitor - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:195
ConstantExpr - An expression that occurs in a constant context and optionally the result of evaluatin...
Definition: Expr.h:1077
ContinueStmt - This represents a continue.
Definition: Stmt.h:2960
ConvertVectorExpr - Clang builtin function __builtin_convertvector This AST node provides support for...
Definition: Expr.h:4533
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1265
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
Definition: Stmt.h:1502
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
DoStmt - This represents a 'do/while' stmt.
Definition: Stmt.h:2735
Represents a reference to #emded data.
Definition: Expr.h:4867
Represents an expression – generally a full-expression – that introduces cleanups to be run at the en...
Definition: ExprCXX.h:3474
This represents one expression.
Definition: Expr.h:110
An expression trait intrinsic.
Definition: ExprCXX.h:2924
ExtVectorElementExpr - This represents access to specific elements of a vector, and may occur on the ...
Definition: Expr.h:6305
RoundingMode getRoundingMode() const
Definition: LangOptions.h:881
ForStmt - This represents a 'for (init;cond;inc)' stmt.
Definition: Stmt.h:2791
Represents a function declaration or definition.
Definition: Decl.h:1932
GNUNullExpr - Implements the GNU __null extension, which is a name for a null pointer constant that h...
Definition: Expr.h:4667
Represents a C11 generic selection.
Definition: Expr.h:5917
IfStmt - This represents an if/then/else.
Definition: Stmt.h:2148
ImaginaryLiteral - We support imaginary integer and floating point literals, like "1....
Definition: Expr.h:1717
Represents an implicitly-generated value initialization of an object of a given type.
Definition: Expr.h:5792
Describes an C or C++ initializer list.
Definition: Expr.h:5039
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
Definition: ExprCXX.h:1954
Represents a prvalue temporary that is written into memory so that a reference can bind to it.
Definition: ExprCXX.h:4728
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition: Expr.h:3187
ObjCBoolLiteralExpr - Objective-C Boolean Literal.
Definition: ExprObjC.h:87
ObjCBoxedExpr - used for generalized expression boxing.
Definition: ExprObjC.h:127
ObjCEncodeExpr, used for @encode in Objective-C.
Definition: ExprObjC.h:410
ObjCStringLiteral, used for Objective-C string literals i.e.
Definition: ExprObjC.h:51
OffsetOfExpr - [C99 7.17] - This represents an expression of the form offsetof(record-type,...
Definition: Expr.h:2475
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class.
Definition: Expr.h:1173
ParenExpr - This represents a parenthesized expression, e.g.
Definition: Expr.h:2135
[C99 6.4.2.2] - A predefined identifier such as func.
Definition: Expr.h:1991
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Definition: Expr.h:6497
A (possibly-)qualified type.
Definition: Type.h:941
Represents a struct/union/class.
Definition: Decl.h:4145
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:5965
Frontend produces RecoveryExprs on semantic errors that prevent creating other well-formed expression...
Definition: Expr.h:7101
C++2a [expr.prim.req]: A requires-expression provides a concise way to express requirements on templa...
Definition: ExprConcepts.h:510
ReturnStmt - This represents a return, optionally of an expression: return; return 4;.
Definition: Stmt.h:3029
ShuffleVectorExpr - clang-specific builtin-in function __builtin_shufflevector.
Definition: Expr.h:4465
Represents an expression that computes the length of a parameter pack.
Definition: ExprCXX.h:4258
Represents a function call to one of __builtin_LINE(), __builtin_COLUMN(), __builtin_FUNCTION(),...
Definition: Expr.h:4761
StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
Definition: Expr.h:4417
Stmt - This represents one statement.
Definition: Stmt.h:84
StringLiteral - This represents a string literal expression, e.g.
Definition: Expr.h:1778
Represents a reference to a non-type template parameter that has been substituted with a template arg...
Definition: ExprCXX.h:4484
SwitchStmt - This represents a 'switch' stmt.
Definition: Stmt.h:2398
A type trait used in the implementation of various C++11 and Library TR1 trait templates.
Definition: ExprCXX.h:2768
bool isAnyComplexType() const
Definition: Type.h:8111
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:8540
UnaryExprOrTypeTraitExpr - expression with either a type or (unevaluated) expression operand.
Definition: Expr.h:2578
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition: Expr.h:2188
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:667
Represents a variable declaration or definition.
Definition: Decl.h:879
WhileStmt - This represents a 'while' stmt.
Definition: Stmt.h:2594
ArrayIndexScope(Compiler< Emitter > *Ctx, uint64_t Index)
Definition: Compiler.h:577
Scope for storage declared in a compound statement.
Definition: Compiler.h:563
BlockScope(Compiler< Emitter > *Ctx)
Definition: Compiler.h:565
void addExtended(const Scope::Local &Local) override
Definition: Compiler.h:567
Compilation context for expressions.
Definition: Compiler.h:104
std::optional< PrimType > classify(const Expr *E) const
Definition: Compiler.h:243
llvm::SmallVector< InitLink > InitStack
Definition: Compiler.h:393
OptLabelTy BreakLabel
Point to break to.
Definition: Compiler.h:403
bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E)
Definition: Compiler.cpp:1785
bool VisitCXXDeleteExpr(const CXXDeleteExpr *E)
Definition: Compiler.cpp:2902
bool VisitOffsetOfExpr(const OffsetOfExpr *E)
Definition: Compiler.cpp:2653
bool visitContinueStmt(const ContinueStmt *S)
Definition: Compiler.cpp:4580
bool VisitCharacterLiteral(const CharacterLiteral *E)
Definition: Compiler.cpp:2000
PrimType classifyPrim(const Expr *E) const
Classifies a known primitive expression.
Definition: Compiler.h:258
bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E)
Definition: Compiler.cpp:1586
bool VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E)
Definition: Compiler.cpp:2975
bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E)
Definition: Compiler.cpp:2326
bool visitBool(const Expr *E)
Visits an expression and converts it to a boolean.
Definition: Compiler.cpp:3291
bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E)
Definition: Compiler.cpp:4175
bool visitDeclAndReturn(const VarDecl *VD, bool ConstantContext) override
Toplevel visitDeclAndReturn().
Definition: Compiler.cpp:3674
PrimType classifyPrim(QualType Ty) const
Classifies a known primitive type.
Definition: Compiler.h:251
bool visitExpr(const Expr *E) override
Definition: Compiler.cpp:3607
bool VisitTypeTraitExpr(const TypeTraitExpr *E)
Definition: Compiler.cpp:2391
bool VisitLambdaExpr(const LambdaExpr *E)
Definition: Compiler.cpp:2407
bool VisitMemberExpr(const MemberExpr *E)
Definition: Compiler.cpp:1733
llvm::DenseMap< const OpaqueValueExpr *, unsigned > OpaqueExprs
OpaqueValueExpr to location mapping.
Definition: Compiler.h:372
bool VisitBinaryOperator(const BinaryOperator *E)
Definition: Compiler.cpp:693
bool visitAttributedStmt(const AttributedStmt *S)
Definition: Compiler.cpp:4671
bool VisitPackIndexingExpr(const PackIndexingExpr *E)
Definition: Compiler.cpp:3014
bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E)
Definition: Compiler.cpp:1289
bool VisitCallExpr(const CallExpr *E)
Definition: Compiler.cpp:3989
std::optional< uint64_t > ArrayIndex
Current argument index. Needed to emit ArrayInitIndexExpr.
Definition: Compiler.h:378
bool VisitPseudoObjectExpr(const PseudoObjectExpr *E)
Definition: Compiler.cpp:2990
bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E)
Definition: Compiler.cpp:2462
const Function * getFunction(const FunctionDecl *FD)
Returns a function for the given FunctionDecl.
Definition: Compiler.cpp:3603
void emitCleanup()
Emits scope cleanup instructions.
Definition: Compiler.cpp:5425
bool VisitCastExpr(const CastExpr *E)
Definition: Compiler.cpp:178
bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E)
Definition: Compiler.cpp:1965
bool VisitComplexUnaryOperator(const UnaryOperator *E)
Definition: Compiler.cpp:5182
std::optional< PrimType > classify(QualType Ty) const
Definition: Compiler.h:246
llvm::DenseMap< const SwitchCase *, LabelTy > CaseMap
Definition: Compiler.h:110
bool visitDeclStmt(const DeclStmt *DS)
Definition: Compiler.cpp:4315
bool VisitBlockExpr(const BlockExpr *E)
Definition: Compiler.cpp:2913
bool visitAPValue(const APValue &Val, PrimType ValType, const Expr *E)
Visit an APValue.
Definition: Compiler.cpp:3844
bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E)
Definition: Compiler.cpp:2688
bool VisitLogicalBinOp(const BinaryOperator *E)
Definition: Compiler.cpp:921
bool visitCompoundStmt(const CompoundStmt *S)
Definition: Compiler.cpp:4306
std::optional< PrimType > ReturnType
Type of the expression returned by the function.
Definition: Compiler.h:397
Context & Ctx
Current compilation context.
Definition: Compiler.h:113
bool visitDeclRef(const ValueDecl *D, const Expr *E)
Visit the given decl as if we have a reference to it.
Definition: Compiler.cpp:5289
bool visitBreakStmt(const BreakStmt *S)
Definition: Compiler.cpp:4571
bool visitForStmt(const ForStmt *S)
Definition: Compiler.cpp:4468
bool VisitDeclRefExpr(const DeclRefExpr *E)
Definition: Compiler.cpp:5420
bool VisitOpaqueValueExpr(const OpaqueValueExpr *E)
Definition: Compiler.cpp:1824
bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E)
Definition: Compiler.cpp:1794
OptLabelTy DefaultLabel
Default case label.
Definition: Compiler.h:407
bool VisitStmtExpr(const StmtExpr *E)
Definition: Compiler.cpp:3212
bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E)
Definition: Compiler.cpp:4194
VariableScope< Emitter > * VarScope
Current scope.
Definition: Compiler.h:375
bool VisitCXXNewExpr(const CXXNewExpr *E)
Definition: Compiler.cpp:2802
const ValueDecl * InitializingDecl
Definition: Compiler.h:391
bool VisitCompoundAssignOperator(const CompoundAssignOperator *E)
Definition: Compiler.cpp:2118
bool visitArrayElemInit(unsigned ElemIndex, const Expr *Init)
Pointer to the array(not the element!) must be on the stack when calling this.
Definition: Compiler.cpp:1559
bool delegate(const Expr *E)
Just pass evaluation on to E.
Definition: Compiler.cpp:3240
bool discard(const Expr *E)
Evaluates an expression for side effects and discards the result.
Definition: Compiler.cpp:3234
bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E)
Definition: Compiler.cpp:4182
CaseMap CaseLabels
Switch case mapping.
Definition: Compiler.h:400
Record * getRecord(QualType Ty)
Returns a record from a record or pointer type.
Definition: Compiler.cpp:3591
bool visit(const Expr *E)
Evaluates an expression and places the result on the stack.
Definition: Compiler.cpp:3250
const RecordType * getRecordTy(QualType Ty)
Returns a record type from a record or pointer type.
Definition: Compiler.cpp:3585
bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E)
Definition: Compiler.cpp:3179
std::optional< unsigned > allocateLocal(DeclTy &&Decl, const ValueDecl *ExtendingDecl=nullptr)
Allocates a space storing a local given its type.
Definition: Compiler.cpp:3523
bool visitInitList(ArrayRef< const Expr * > Inits, const Expr *ArrayFiller, const Expr *E)
Definition: Compiler.cpp:1319
bool VisitSizeOfPackExpr(const SizeOfPackExpr *E)
Definition: Compiler.cpp:2747
bool VisitPredefinedExpr(const PredefinedExpr *E)
Definition: Compiler.cpp:2446
bool VisitSourceLocExpr(const SourceLocExpr *E)
Definition: Compiler.cpp:2597
Compiler(Context &Ctx, Program &P, Tys &&...Args)
Initializes the compiler and the backend emitter.
Definition: Compiler.h:120
bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E)
Definition: Compiler.cpp:3108
bool VisitObjCStringLiteral(const ObjCStringLiteral *E)
Definition: Compiler.cpp:1960
bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E)
Definition: Compiler.cpp:2400
bool visitInitializer(const Expr *E)
Compiles an initializer.
Definition: Compiler.cpp:3277
const Expr * SourceLocDefaultExpr
DefaultInit- or DefaultArgExpr, needed for SourceLocExpr.
Definition: Compiler.h:381
bool VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *E)
Definition: Compiler.cpp:2320
bool VisitPointerArithBinOp(const BinaryOperator *E)
Perform addition/subtraction of a pointer and an integer or subtraction of two pointers.
Definition: Compiler.cpp:873
bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E)
Definition: Compiler.cpp:2763
bool visitDefaultStmt(const DefaultStmt *S)
Definition: Compiler.cpp:4665
typename Emitter::LabelTy LabelTy
Definition: Compiler.h:107
VarCreationState visitDecl(const VarDecl *VD)
Definition: Compiler.cpp:3646
bool visitStmt(const Stmt *S)
Definition: Compiler.cpp:4256
bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E)
Definition: Compiler.cpp:2924
bool visitAPValueInitializer(const APValue &Val, const Expr *E)
Definition: Compiler.cpp:3871
bool VisitCXXConstructExpr(const CXXConstructExpr *E)
Definition: Compiler.cpp:2483
bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E)
Definition: Compiler.cpp:4202
bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E)
Definition: Compiler.cpp:3170
bool VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E)
Definition: Compiler.cpp:2771
bool VisitRecoveryExpr(const RecoveryExpr *E)
Definition: Compiler.cpp:3019
bool VisitRequiresExpr(const RequiresExpr *E)
Definition: Compiler.cpp:2967
bool Initializing
Flag inidicating if we're initializing an already created variable.
Definition: Compiler.h:390
bool visitReturnStmt(const ReturnStmt *RS)
Definition: Compiler.cpp:4332
bool VisitCXXThrowExpr(const CXXThrowExpr *E)
Definition: Compiler.cpp:2454
bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E)
Definition: Compiler.cpp:1592
bool VisitChooseExpr(const ChooseExpr *E)
Definition: Compiler.cpp:2758
bool visitFunc(const FunctionDecl *F) override
Definition: Compiler.cpp:4937
bool visitCXXForRangeStmt(const CXXForRangeStmt *S)
Definition: Compiler.cpp:4515
bool visitCaseStmt(const CaseStmt *S)
Definition: Compiler.cpp:4659
bool VisitComplexBinOp(const BinaryOperator *E)
Definition: Compiler.cpp:982
llvm::DenseMap< const ValueDecl *, Scope::Local > Locals
Variable to storage mapping.
Definition: Compiler.h:369
bool VisitAbstractConditionalOperator(const AbstractConditionalOperator *E)
Definition: Compiler.cpp:1860
OptLabelTy ContinueLabel
Point to continue to.
Definition: Compiler.h:405
bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
Definition: Compiler.cpp:1205
typename Emitter::AddrTy AddrTy
Definition: Compiler.h:108
bool VisitCXXRewrittenBinaryOperator(const CXXRewrittenBinaryOperator *E)
Definition: Compiler.cpp:2984
bool VisitUnaryOperator(const UnaryOperator *E)
Definition: Compiler.cpp:4963
bool VisitFloatCompoundAssignOperator(const CompoundAssignOperator *E)
Definition: Compiler.cpp:2007
bool VisitGenericSelectionExpr(const GenericSelectionExpr *E)
Definition: Compiler.cpp:2752
bool visitDoStmt(const DoStmt *S)
Definition: Compiler.cpp:4440
bool VisitIntegerLiteral(const IntegerLiteral *E)
Definition: Compiler.cpp:650
bool VisitInitListExpr(const InitListExpr *E)
Definition: Compiler.cpp:1581
bool VisitStringLiteral(const StringLiteral *E)
Definition: Compiler.cpp:1903
bool VisitParenExpr(const ParenExpr *E)
Definition: Compiler.cpp:688
bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E)
Definition: Compiler.cpp:2474
bool VisitShuffleVectorExpr(const ShuffleVectorExpr *E)
Definition: Compiler.cpp:3066
bool VisitPointerCompoundAssignOperator(const CompoundAssignOperator *E)
Definition: Compiler.cpp:2081
std::optional< LabelTy > OptLabelTy
Definition: Compiler.h:109
bool DiscardResult
Flag indicating if return value is to be discarded.
Definition: Compiler.h:384
bool VisitEmbedExpr(const EmbedExpr *E)
Definition: Compiler.cpp:1614
bool VisitConvertVectorExpr(const ConvertVectorExpr *E)
Definition: Compiler.cpp:3034
bool VisitCXXThisExpr(const CXXThisExpr *E)
Definition: Compiler.cpp:4222
bool VisitConstantExpr(const ConstantExpr *E)
Definition: Compiler.cpp:1598
unsigned allocateTemporary(const Expr *E)
Definition: Compiler.cpp:3564
bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E)
Definition: Compiler.cpp:1643
bool visitSwitchStmt(const SwitchStmt *S)
Definition: Compiler.cpp:4589
bool VisitCXXUuidofExpr(const CXXUuidofExpr *E)
Definition: Compiler.cpp:2930
unsigned allocateLocalPrimitive(DeclTy &&Decl, PrimType Ty, bool IsConst, bool IsExtended=false)
Creates a local primitive value.
Definition: Compiler.cpp:3498
bool VisitExprWithCleanups(const ExprWithCleanups *E)
Definition: Compiler.cpp:2239
bool visitWhileStmt(const WhileStmt *S)
Definition: Compiler.cpp:4409
bool visitIfStmt(const IfStmt *IS)
Definition: Compiler.cpp:4366
bool VisitAddrLabelExpr(const AddrLabelExpr *E)
Definition: Compiler.cpp:3024
bool VisitFloatingLiteral(const FloatingLiteral *E)
Definition: Compiler.cpp:658
Program & P
Program to link to.
Definition: Compiler.h:115
bool VisitBuiltinCallExpr(const CallExpr *E)
Definition: Compiler.cpp:3934
bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E)
Definition: Compiler.cpp:2247
bool VisitGNUNullExpr(const GNUNullExpr *E)
Definition: Compiler.cpp:4211
bool VisitImaginaryLiteral(const ImaginaryLiteral *E)
Definition: Compiler.cpp:666
bool VisitSYCLUniqueStableNameExpr(const SYCLUniqueStableNameExpr *E)
Definition: Compiler.cpp:1976
VarCreationState visitVarDecl(const VarDecl *VD, bool Toplevel=false)
Creates and initializes a variable from the given decl.
Definition: Compiler.cpp:3734
bool visitCXXTryStmt(const CXXTryStmt *S)
Definition: Compiler.cpp:4702
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:40
const LangOptions & getLangOpts() const
Returns the language options.
Definition: Context.cpp:128
std::optional< PrimType > classify(QualType T) const
Classifies a type.
Definition: Context.cpp:130
Scope used to handle temporaries in toplevel variable declarations.
Definition: Compiler.cpp:28
Bytecode function.
Definition: Function.h:81
InitLinkScope(Compiler< Emitter > *Ctx, InitLink &&Link)
Definition: Compiler.h:612
InitStackScope(Compiler< Emitter > *Ctx, bool Active)
Definition: Compiler.h:624
Scope managing label targets.
Definition: Compiler.cpp:99
Generic scope for local variables.
Definition: Compiler.h:475
LocalScope(Compiler< Emitter > *Ctx)
Definition: Compiler.h:477
~LocalScope() override
Emit a Destroy op for this scope.
Definition: Compiler.h:482
bool destroyLocals(const Expr *E=nullptr) override
Explicit destruction of local variables.
Definition: Compiler.h:499
LocalScope(Compiler< Emitter > *Ctx, const ValueDecl *VD)
Definition: Compiler.h:478
bool emitDestructors(const Expr *E=nullptr) override
Definition: Compiler.h:519
void emitDestruction() override
Overriden to support explicit destruction.
Definition: Compiler.h:490
void removeIfStoredOpaqueValue(const Scope::Local &Local)
Definition: Compiler.h:549
void addLocal(const Scope::Local &Local) override
Definition: Compiler.h:509
std::optional< unsigned > Idx
Index of the scope in the chain.
Definition: Compiler.h:559
Sets the context for break/continue statements.
Definition: Compiler.cpp:110
Scope used to handle initialization methods.
Definition: Compiler.cpp:52
The program contains and links the bytecode for all functions.
Definition: Program.h:39
Structure/Class descriptor.
Definition: Record.h:25
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:77
SourceLocScope(Compiler< Emitter > *Ctx, const Expr *DefaultExpr)
Definition: Compiler.h:591
Scope chain managing the variable lifetimes.
Definition: Compiler.h:414
virtual void addExtended(const Scope::Local &Local)
Definition: Compiler.h:435
void addExtended(const Scope::Local &Local, const ValueDecl *ExtendingDecl)
Definition: Compiler.h:440
void add(const Scope::Local &Local, bool IsExtended)
Definition: Compiler.h:423
Compiler< Emitter > * Ctx
Compiler instance.
Definition: Compiler.h:468
virtual bool emitDestructors(const Expr *E=nullptr)
Definition: Compiler.h:462
virtual bool destroyLocals(const Expr *E=nullptr)
Definition: Compiler.h:463
VariableScope * Parent
Link to the parent scope.
Definition: Compiler.h:470
virtual void addLocal(const Scope::Local &Local)
Definition: Compiler.h:430
VariableScope * getParent() const
Definition: Compiler.h:464
VariableScope(Compiler< Emitter > *Ctx, const ValueDecl *VD)
Definition: Compiler.h:416
virtual void emitDestruction()
Definition: Compiler.h:461
Defines the clang::TargetInfo interface.
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:33
llvm::PointerUnion< const Decl *, const Expr * > DeclTy
Definition: Descriptor.h:28
The JSON file list parser is used to communicate input to InstallAPI.
@ Link
'link' clause, allowed on 'declare' construct.
const FunctionProtoType * T
@ Success
Template argument deduction was successful.
#define bool
Definition: stdbool.h:24
Information about a local's storage.
Definition: Function.h:39
State encapsulating if a the variable creation has been successful, unsuccessful, or no variable has ...
Definition: Compiler.h:91
static VarCreationState NotCreated()
Definition: Compiler.h:95
std::optional< bool > S
Definition: Compiler.h:92