clang  19.0.0git
ByteCodeExprGen.h
Go to the documentation of this file.
1 //===--- ByteCodeExprGen.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"
23 #include "clang/AST/StmtVisitor.h"
24 #include "clang/Basic/TargetInfo.h"
25 
26 namespace clang {
27 class QualType;
28 
29 namespace interp {
30 
31 template <class Emitter> class LocalScope;
32 template <class Emitter> class DestructorScope;
33 template <class Emitter> class VariableScope;
34 template <class Emitter> class DeclScope;
35 template <class Emitter> class OptionScope;
36 template <class Emitter> class ArrayIndexScope;
37 template <class Emitter> class SourceLocScope;
38 
39 /// Compilation context for expressions.
40 template <class Emitter>
41 class ByteCodeExprGen : public ConstStmtVisitor<ByteCodeExprGen<Emitter>, bool>,
42  public Emitter {
43 protected:
44  // Aliases for types defined in the emitter.
45  using LabelTy = typename Emitter::LabelTy;
46  using AddrTy = typename Emitter::AddrTy;
47 
48  /// Current compilation context.
50  /// Program to link to.
52 
53 public:
54  /// Initializes the compiler and the backend emitter.
55  template <typename... Tys>
56  ByteCodeExprGen(Context &Ctx, Program &P, Tys &&... Args)
57  : Emitter(Ctx, P, Args...), Ctx(Ctx), P(P) {}
58 
59  // Expression visitors - result returned on interp stack.
60  bool VisitCastExpr(const CastExpr *E);
61  bool VisitIntegerLiteral(const IntegerLiteral *E);
64  bool VisitParenExpr(const ParenExpr *E);
65  bool VisitBinaryOperator(const BinaryOperator *E);
66  bool VisitLogicalBinOp(const BinaryOperator *E);
68  bool VisitComplexBinOp(const BinaryOperator *E);
70  bool VisitCallExpr(const CallExpr *E);
71  bool VisitBuiltinCallExpr(const CallExpr *E);
75  bool VisitGNUNullExpr(const GNUNullExpr *E);
76  bool VisitCXXThisExpr(const CXXThisExpr *E);
77  bool VisitUnaryOperator(const UnaryOperator *E);
79  bool VisitDeclRefExpr(const DeclRefExpr *E);
83  bool VisitInitListExpr(const InitListExpr *E);
85  bool VisitConstantExpr(const ConstantExpr *E);
87  bool VisitMemberExpr(const MemberExpr *E);
92  bool VisitStringLiteral(const StringLiteral *E);
103  bool VisitTypeTraitExpr(const TypeTraitExpr *E);
105  bool VisitLambdaExpr(const LambdaExpr *E);
106  bool VisitPredefinedExpr(const PredefinedExpr *E);
107  bool VisitCXXThrowExpr(const CXXThrowExpr *E);
109  bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E);
111  bool VisitSourceLocExpr(const SourceLocExpr *E);
112  bool VisitOffsetOfExpr(const OffsetOfExpr *E);
114  bool VisitSizeOfPackExpr(const SizeOfPackExpr *E);
116  bool VisitChooseExpr(const ChooseExpr *E);
120  bool VisitCXXUuidofExpr(const CXXUuidofExpr *E);
121  bool VisitRequiresExpr(const RequiresExpr *E);
126  bool VisitRecoveryExpr(const RecoveryExpr *E);
127  bool VisitAddrLabelExpr(const AddrLabelExpr *E);
130  bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E);
131 
132 protected:
133  bool visitExpr(const Expr *E) override;
134  bool visitDecl(const VarDecl *VD) override;
135 
136 protected:
137  /// Emits scope cleanup instructions.
138  void emitCleanup();
139 
140  /// Returns a record type from a record or pointer type.
141  const RecordType *getRecordTy(QualType Ty);
142 
143  /// Returns a record from a record or pointer type.
145  Record *getRecord(const RecordDecl *RD);
146 
147  // Returns a function for the given FunctionDecl.
148  // If the function does not exist yet, it is compiled.
149  const Function *getFunction(const FunctionDecl *FD);
150 
151  std::optional<PrimType> classify(const Expr *E) const {
152  return Ctx.classify(E);
153  }
154  std::optional<PrimType> classify(QualType Ty) const {
155  return Ctx.classify(Ty);
156  }
157 
158  /// Classifies a known primitive type.
160  if (auto T = classify(Ty)) {
161  return *T;
162  }
163  llvm_unreachable("not a primitive type");
164  }
165  /// Classifies a known primitive expression.
166  PrimType classifyPrim(const Expr *E) const {
167  if (auto T = classify(E))
168  return *T;
169  llvm_unreachable("not a primitive type");
170  }
171 
172  /// Evaluates an expression and places the result on the stack. If the
173  /// expression is of composite type, a local variable will be created
174  /// and a pointer to said variable will be placed on the stack.
175  bool visit(const Expr *E);
176  /// Compiles an initializer. This is like visit() but it will never
177  /// create a variable and instead rely on a variable already having
178  /// been created. visitInitializer() then relies on a pointer to this
179  /// variable being on top of the stack.
180  bool visitInitializer(const Expr *E);
181  /// Evaluates an expression for side effects and discards the result.
182  bool discard(const Expr *E);
183  /// Just pass evaluation on to \p E. This leaves all the parsing flags
184  /// intact.
185  bool delegate(const Expr *E);
186 
187  /// Creates and initializes a variable from the given decl.
188  bool visitVarDecl(const VarDecl *VD);
189  /// Visit an APValue.
190  bool visitAPValue(const APValue &Val, PrimType ValType, const Expr *E);
191  bool visitAPValueInitializer(const APValue &Val, const Expr *E);
192 
193  /// Visits an expression and converts it to a boolean.
194  bool visitBool(const Expr *E);
195 
196  /// Visits an initializer for a local.
197  bool visitLocalInitializer(const Expr *Init, unsigned I) {
198  if (!this->emitGetPtrLocal(I, Init))
199  return false;
200 
201  if (!visitInitializer(Init))
202  return false;
203 
204  if (!this->emitFinishInit(Init))
205  return false;
206 
207  return this->emitPopPtr(Init);
208  }
209 
210  /// Visits an initializer for a global.
211  bool visitGlobalInitializer(const Expr *Init, unsigned I) {
212  if (!this->emitGetPtrGlobal(I, Init))
213  return false;
214 
215  if (!visitInitializer(Init))
216  return false;
217 
218  if (!this->emitFinishInit(Init))
219  return false;
220 
221  return this->emitPopPtr(Init);
222  }
223 
224  /// Visits a delegated initializer.
225  bool visitThisInitializer(const Expr *I) {
226  if (!this->emitThis(I))
227  return false;
228 
229  if (!visitInitializer(I))
230  return false;
231 
232  return this->emitFinishInitPop(I);
233  }
234 
235  bool visitInitList(ArrayRef<const Expr *> Inits, const Expr *ArrayFiller,
236  const Expr *E);
237  bool visitArrayElemInit(unsigned ElemIndex, const Expr *Init);
238 
239  /// Creates a local primitive value.
240  unsigned allocateLocalPrimitive(DeclTy &&Decl, PrimType Ty, bool IsConst,
241  bool IsExtended = false);
242 
243  /// Allocates a space storing a local given its type.
244  std::optional<unsigned>
245  allocateLocal(DeclTy &&Decl, const ValueDecl *ExtendingDecl = nullptr);
246 
247 private:
248  friend class VariableScope<Emitter>;
249  friend class LocalScope<Emitter>;
250  friend class DestructorScope<Emitter>;
251  friend class DeclScope<Emitter>;
252  friend class OptionScope<Emitter>;
253  friend class ArrayIndexScope<Emitter>;
254  friend class SourceLocScope<Emitter>;
255 
256  /// Emits a zero initializer.
257  bool visitZeroInitializer(PrimType T, QualType QT, const Expr *E);
258  bool visitZeroRecordInitializer(const Record *R, const Expr *E);
259 
260  /// Emits an APSInt constant.
261  bool emitConst(const llvm::APSInt &Value, PrimType Ty, const Expr *E);
262  bool emitConst(const llvm::APSInt &Value, const Expr *E);
263  bool emitConst(const llvm::APInt &Value, const Expr *E) {
264  return emitConst(static_cast<llvm::APSInt>(Value), E);
265  }
266 
267  /// Emits an integer constant.
268  template <typename T> bool emitConst(T Value, PrimType Ty, const Expr *E);
269  template <typename T> bool emitConst(T Value, const Expr *E);
270 
271  llvm::RoundingMode getRoundingMode(const Expr *E) const {
273 
274  if (FPO.getRoundingMode() == llvm::RoundingMode::Dynamic)
275  return llvm::RoundingMode::NearestTiesToEven;
276 
277  return FPO.getRoundingMode();
278  }
279 
280  bool emitPrimCast(PrimType FromT, PrimType ToT, QualType ToQT, const Expr *E);
281  PrimType classifyComplexElementType(QualType T) const {
282  assert(T->isAnyComplexType());
283 
284  QualType ElemType = T->getAs<ComplexType>()->getElementType();
285 
286  return *this->classify(ElemType);
287  }
288 
289  bool emitComplexReal(const Expr *SubExpr);
290  bool emitComplexBoolCast(const Expr *E);
291  bool emitComplexComparison(const Expr *LHS, const Expr *RHS,
292  const BinaryOperator *E);
293 
294  bool emitRecordDestruction(const Record *R);
295  bool emitDestruction(const Descriptor *Desc);
296  unsigned collectBaseOffset(const QualType BaseType,
297  const QualType DerivedType);
298 
299 protected:
300  /// Variable to storage mapping.
301  llvm::DenseMap<const ValueDecl *, Scope::Local> Locals;
302 
303  /// OpaqueValueExpr to location mapping.
304  llvm::DenseMap<const OpaqueValueExpr *, unsigned> OpaqueExprs;
305 
306  /// Current scope.
308 
309  /// Current argument index. Needed to emit ArrayInitIndexExpr.
310  std::optional<uint64_t> ArrayIndex;
311 
312  /// DefaultInit- or DefaultArgExpr, needed for SourceLocExpr.
313  const Expr *SourceLocDefaultExpr = nullptr;
314 
315  /// Flag indicating if return value is to be discarded.
316  bool DiscardResult = false;
317 
318  /// Flag inidicating if we're initializing an already created
319  /// variable. This is set in visitInitializer().
320  bool Initializing = false;
321 
322  /// Flag indicating if we're initializing a global variable.
323  bool GlobalDecl = false;
324 };
325 
326 extern template class ByteCodeExprGen<ByteCodeEmitter>;
327 extern template class ByteCodeExprGen<EvalEmitter>;
328 
329 /// Scope chain managing the variable lifetimes.
330 template <class Emitter> class VariableScope {
331 public:
333  : Ctx(Ctx), Parent(Ctx->VarScope), ValDecl(VD) {
334  Ctx->VarScope = this;
335  }
336 
337  virtual ~VariableScope() { Ctx->VarScope = this->Parent; }
338 
339  void add(const Scope::Local &Local, bool IsExtended) {
340  if (IsExtended)
341  this->addExtended(Local);
342  else
343  this->addLocal(Local);
344  }
345 
346  virtual void addLocal(const Scope::Local &Local) {
347  if (this->Parent)
348  this->Parent->addLocal(Local);
349  }
350 
351  virtual void addExtended(const Scope::Local &Local) {
352  if (this->Parent)
353  this->Parent->addExtended(Local);
354  }
355 
356  void addExtended(const Scope::Local &Local, const ValueDecl *ExtendingDecl) {
357  // Walk up the chain of scopes until we find the one for ExtendingDecl.
358  // If there is no such scope, attach it to the parent one.
359  VariableScope *P = this;
360  while (P) {
361  if (P->ValDecl == ExtendingDecl) {
362  P->addLocal(Local);
363  return;
364  }
365  P = P->Parent;
366  if (!P)
367  break;
368  }
369 
370  // Use the parent scope.
371  addExtended(Local);
372  }
373 
374  virtual void emitDestruction() {}
375  virtual bool emitDestructors() { return true; }
376  VariableScope *getParent() const { return Parent; }
377 
378 protected:
379  /// ByteCodeExprGen instance.
381  /// Link to the parent scope.
383  const ValueDecl *ValDecl = nullptr;
384 };
385 
386 /// Generic scope for local variables.
387 template <class Emitter> class LocalScope : public VariableScope<Emitter> {
388 public:
390  : VariableScope<Emitter>(Ctx, nullptr) {}
391 
392  /// Emit a Destroy op for this scope.
393  ~LocalScope() override {
394  if (!Idx)
395  return;
396  this->Ctx->emitDestroy(*Idx, SourceInfo{});
397  removeStoredOpaqueValues();
398  }
399 
400  /// Overriden to support explicit destruction.
401  void emitDestruction() override { destroyLocals(); }
402 
403  /// Explicit destruction of local variables.
404  bool destroyLocals() {
405  if (!Idx)
406  return true;
407 
408  bool Success = this->emitDestructors();
409  this->Ctx->emitDestroy(*Idx, SourceInfo{});
410  removeStoredOpaqueValues();
411  this->Idx = std::nullopt;
412  return Success;
413  }
414 
415  void addLocal(const Scope::Local &Local) override {
416  if (!Idx) {
417  Idx = this->Ctx->Descriptors.size();
418  this->Ctx->Descriptors.emplace_back();
419  }
420 
421  this->Ctx->Descriptors[*Idx].emplace_back(Local);
422  }
423 
424  bool emitDestructors() override {
425  if (!Idx)
426  return true;
427  // Emit destructor calls for local variables of record
428  // type with a destructor.
429  for (Scope::Local &Local : this->Ctx->Descriptors[*Idx]) {
430  if (!Local.Desc->isPrimitive() && !Local.Desc->isPrimitiveArray()) {
431  if (!this->Ctx->emitGetPtrLocal(Local.Offset, SourceInfo{}))
432  return false;
433 
434  if (!this->Ctx->emitDestruction(Local.Desc))
435  return false;
436 
437  if (!this->Ctx->emitPopPtr(SourceInfo{}))
438  return false;
439  removeIfStoredOpaqueValue(Local);
440  }
441  }
442  return true;
443  }
444 
446  if (!Idx)
447  return;
448 
449  for (const Scope::Local &Local : this->Ctx->Descriptors[*Idx]) {
450  removeIfStoredOpaqueValue(Local);
451  }
452  }
453 
455  if (const auto *OVE =
456  llvm::dyn_cast_if_present<OpaqueValueExpr>(Local.Desc->asExpr())) {
457  if (auto It = this->Ctx->OpaqueExprs.find(OVE);
458  It != this->Ctx->OpaqueExprs.end())
459  this->Ctx->OpaqueExprs.erase(It);
460  };
461  }
462 
463  /// Index of the scope in the chain.
464  std::optional<unsigned> Idx;
465 };
466 
467 /// Emits the destructors of the variables of \param OtherScope
468 /// when this scope is destroyed. Does not create a Scope in the bytecode at
469 /// all, this is just a RAII object to emit destructors.
470 template <class Emitter> class DestructorScope final {
471 public:
472  DestructorScope(LocalScope<Emitter> &OtherScope) : OtherScope(OtherScope) {}
473 
474  ~DestructorScope() { OtherScope.emitDestructors(); }
475 
476 private:
477  LocalScope<Emitter> &OtherScope;
478 };
479 
480 /// Like a regular LocalScope, except that the destructors of all local
481 /// variables are automatically emitted when the AutoScope is destroyed.
482 template <class Emitter> class AutoScope : public LocalScope<Emitter> {
483 public:
485  : LocalScope<Emitter>(Ctx), DS(*this) {}
486 
487 private:
489 };
490 
491 /// Scope for storage declared in a compound statement.
492 template <class Emitter> class BlockScope final : public AutoScope<Emitter> {
493 public:
495 
496  void addExtended(const Scope::Local &Local) override {
497  // If we to this point, just add the variable as a normal local
498  // variable. It will be destroyed at the end of the block just
499  // like all others.
500  this->addLocal(Local);
501  }
502 };
503 
504 template <class Emitter> class ExprScope final : public AutoScope<Emitter> {
505 public:
507 };
508 
509 template <class Emitter> class ArrayIndexScope final {
510 public:
512  OldArrayIndex = Ctx->ArrayIndex;
513  Ctx->ArrayIndex = Index;
514  }
515 
516  ~ArrayIndexScope() { Ctx->ArrayIndex = OldArrayIndex; }
517 
518 private:
520  std::optional<uint64_t> OldArrayIndex;
521 };
522 
523 template <class Emitter> class SourceLocScope final {
524 public:
525  SourceLocScope(ByteCodeExprGen<Emitter> *Ctx, const Expr *DefaultExpr)
526  : Ctx(Ctx) {
527  assert(DefaultExpr);
528  // We only switch if the current SourceLocDefaultExpr is null.
529  if (!Ctx->SourceLocDefaultExpr) {
530  Enabled = true;
531  Ctx->SourceLocDefaultExpr = DefaultExpr;
532  }
533  }
534 
536  if (Enabled)
537  Ctx->SourceLocDefaultExpr = nullptr;
538  }
539 
540 private:
542  bool Enabled = false;
543 };
544 
545 } // namespace interp
546 } // namespace clang
547 
548 #endif
NodeId Parent
Definition: ASTDiff.cpp:191
StringRef P
llvm::APSInt APSInt
llvm::MachO::Record Record
Definition: MachO.h:31
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:4193
AddrLabelExpr - The GNU address of label extension, representing &&label.
Definition: Expr.h:4390
Represents the index of the current element of an array being initialized by an ArrayInitLoopExpr.
Definition: Expr.h:5605
Represents a loop initializing the elements of an array.
Definition: Expr.h:5552
ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
Definition: Expr.h:2716
An Embarcadero array type trait, as used in the implementation of __array_rank and __array_extent.
Definition: ExprCXX.h:2848
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3892
Represents binding an expression to a temporary.
Definition: ExprCXX.h:1487
A boolean literal, per ([C++ lex.bool] Boolean literals).
Definition: ExprCXX.h:720
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1542
A default argument (C++ [dcl.fct.default]).
Definition: ExprCXX.h:1264
A use of a default initializer in a constructor or in aggregate initialization.
Definition: ExprCXX.h:1371
Represents a call to an inherited base class constructor from an inheriting constructor.
Definition: ExprCXX.h:1733
Represents a C++11 noexcept expression (C++ [expr.unary.noexcept]).
Definition: ExprCXX.h:4119
The null pointer literal (C++11 [lex.nullptr])
Definition: ExprCXX.h:765
Represents a list-initialization with parenthesis.
Definition: ExprCXX.h:4955
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 a value-initialized rvalue of type T, which is a non-class type.
Definition: ExprCXX.h:2177
Represents the this expression in C++.
Definition: ExprCXX.h:1148
A C++ throw-expression (C++ [except.throw]).
Definition: ExprCXX.h:1202
A Microsoft C++ __uuidof expression, which gets the _GUID that corresponds to the supplied type or ex...
Definition: ExprCXX.h:1062
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2872
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
Definition: Expr.h:3535
ChooseExpr - GNU builtin-in function __builtin_choose_expr.
Definition: Expr.h:4610
Complex values, per C99 6.2.5p11.
Definition: Type.h:3098
CompoundAssignOperator - For compound assignments (e.g.
Definition: Expr.h:4140
CompoundLiteralExpr - [C99 6.5.2.5].
Definition: Expr.h:3465
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:1072
ConvertVectorExpr - Clang builtin function __builtin_convertvector This AST node provides support for...
Definition: Expr.h:4551
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1260
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
Represents an expression – generally a full-expression – that introduces cleanups to be run at the en...
Definition: ExprCXX.h:3467
This represents one expression.
Definition: Expr.h:110
FPOptions getFPFeaturesInEffect(const LangOptions &LO) const
Returns the set of floating point options that apply to this expression.
Definition: Expr.cpp:3903
An expression trait intrinsic.
Definition: ExprCXX.h:2919
RoundingMode getRoundingMode() const
Definition: LangOptions.h:890
Represents a function declaration or definition.
Definition: Decl.h:1972
GNUNullExpr - Implements the GNU __null extension, which is a name for a null pointer constant that h...
Definition: Expr.h:4685
Represents a C11 generic selection.
Definition: Expr.h:5766
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:56
ImaginaryLiteral - We support imaginary integer and floating point literals, like "1....
Definition: Expr.h:1712
Represents an implicitly-generated value initialization of an object of a given type.
Definition: Expr.h:5641
Describes an C or C++ initializer list.
Definition: Expr.h:4888
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
Definition: ExprCXX.h:1950
Represents a prvalue temporary that is written into memory so that a reference can bind to it.
Definition: ExprCXX.h:4721
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition: Expr.h:3224
ObjCBoolLiteralExpr - Objective-C Boolean Literal.
Definition: ExprObjC.h:87
ObjCBoxedExpr - used for generalized expression boxing.
Definition: ExprObjC.h:127
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:2517
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class.
Definition: Expr.h:1168
ParenExpr - This represents a parethesized expression, e.g.
Definition: Expr.h:2182
[C99 6.4.2.2] - A predefined identifier such as func.
Definition: Expr.h:1986
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Definition: Expr.h:6346
A (possibly-)qualified type.
Definition: Type.h:940
Represents a struct/union/class.
Definition: Decl.h:4171
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:5561
Frontend produces RecoveryExprs on semantic errors that prevent creating other well-formed expression...
Definition: Expr.h:6950
C++2a [expr.prim.req]: A requires-expression provides a concise way to express requirements on templa...
Definition: ExprConcepts.h:510
ShuffleVectorExpr - clang-specific builtin-in function __builtin_shufflevector.
Definition: Expr.h:4483
Represents an expression that computes the length of a parameter pack.
Definition: ExprCXX.h:4251
Represents a function call to one of __builtin_LINE(), __builtin_COLUMN(), __builtin_FUNCTION(),...
Definition: Expr.h:4779
StringLiteral - This represents a string literal expression, e.g.
Definition: Expr.h:1773
Represents a reference to a non-type template parameter that has been substituted with a template arg...
Definition: ExprCXX.h:4477
A type trait used in the implementation of various C++11 and Library TR1 trait templates.
Definition: ExprCXX.h:2763
bool isAnyComplexType() const
Definition: Type.h:7726
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:8160
UnaryExprOrTypeTraitExpr - expression with either a type or (unevaluated) expression operand.
Definition: Expr.h:2620
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition: Expr.h:2235
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:707
Represents a variable declaration or definition.
Definition: Decl.h:919
ArrayIndexScope(ByteCodeExprGen< Emitter > *Ctx, uint64_t Index)
Like a regular LocalScope, except that the destructors of all local variables are automatically emitt...
AutoScope(ByteCodeExprGen< Emitter > *Ctx)
Scope for storage declared in a compound statement.
BlockScope(ByteCodeExprGen< Emitter > *Ctx)
void addExtended(const Scope::Local &Local) override
Compilation context for expressions.
bool visitExpr(const Expr *E) override
bool VisitAddrLabelExpr(const AddrLabelExpr *E)
std::optional< unsigned > allocateLocal(DeclTy &&Decl, const ValueDecl *ExtendingDecl=nullptr)
Allocates a space storing a local given its type.
unsigned allocateLocalPrimitive(DeclTy &&Decl, PrimType Ty, bool IsConst, bool IsExtended=false)
Creates a local primitive value.
bool VisitIntegerLiteral(const IntegerLiteral *E)
bool VisitSizeOfPackExpr(const SizeOfPackExpr *E)
bool VisitCharacterLiteral(const CharacterLiteral *E)
std::optional< PrimType > classify(QualType Ty) const
bool VisitDeclRefExpr(const DeclRefExpr *E)
bool VisitExprWithCleanups(const ExprWithCleanups *E)
bool VisitPseudoObjectExpr(const PseudoObjectExpr *E)
bool VisitObjCStringLiteral(const ObjCStringLiteral *E)
bool VisitComplexBinOp(const BinaryOperator *E)
bool VisitCXXThrowExpr(const CXXThrowExpr *E)
bool VisitConvertVectorExpr(const ConvertVectorExpr *E)
bool VisitMemberExpr(const MemberExpr *E)
bool VisitOffsetOfExpr(const OffsetOfExpr *E)
bool VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E)
bool discard(const Expr *E)
Evaluates an expression for side effects and discards the result.
bool visitThisInitializer(const Expr *I)
Visits a delegated initializer.
bool visitDecl(const VarDecl *VD) override
Toplevel visitDecl().
bool VisitCXXUuidofExpr(const CXXUuidofExpr *E)
bool visitInitializer(const Expr *E)
Compiles an initializer.
bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E)
std::optional< PrimType > classify(const Expr *E) const
bool VisitParenExpr(const ParenExpr *E)
bool Initializing
Flag inidicating if we're initializing an already created variable.
bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E)
bool VisitShuffleVectorExpr(const ShuffleVectorExpr *E)
bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E)
bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E)
bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E)
bool VisitImaginaryLiteral(const ImaginaryLiteral *E)
bool VisitLambdaExpr(const LambdaExpr *E)
bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E)
bool visitAPValueInitializer(const APValue &Val, const Expr *E)
bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E)
bool VisitComplexUnaryOperator(const UnaryOperator *E)
bool VisitRequiresExpr(const RequiresExpr *E)
Program & P
Program to link to.
bool VisitBuiltinCallExpr(const CallExpr *E)
PrimType classifyPrim(const Expr *E) const
Classifies a known primitive expression.
PrimType classifyPrim(QualType Ty) const
Classifies a known primitive type.
bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E)
bool visitArrayElemInit(unsigned ElemIndex, const Expr *Init)
Pointer to the array(not the element!) must be on the stack when calling this.
llvm::DenseMap< const ValueDecl *, Scope::Local > Locals
Variable to storage mapping.
void emitCleanup()
Emits scope cleanup instructions.
bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E)
bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
Context & Ctx
Current compilation context.
bool visitGlobalInitializer(const Expr *Init, unsigned I)
Visits an initializer for a global.
bool visitBool(const Expr *E)
Visits an expression and converts it to a boolean.
bool VisitPredefinedExpr(const PredefinedExpr *E)
VariableScope< Emitter > * VarScope
Current scope.
bool VisitBinaryOperator(const BinaryOperator *E)
bool VisitTypeTraitExpr(const TypeTraitExpr *E)
bool visit(const Expr *E)
Evaluates an expression and places the result on the stack.
bool VisitInitListExpr(const InitListExpr *E)
bool VisitFloatingLiteral(const FloatingLiteral *E)
bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E)
bool VisitPackIndexingExpr(const PackIndexingExpr *E)
bool VisitAbstractConditionalOperator(const AbstractConditionalOperator *E)
bool VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *E)
bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E)
bool VisitCXXRewrittenBinaryOperator(const CXXRewrittenBinaryOperator *E)
bool VisitGenericSelectionExpr(const GenericSelectionExpr *E)
bool VisitUnaryOperator(const UnaryOperator *E)
bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E)
std::optional< uint64_t > ArrayIndex
Current argument index. Needed to emit ArrayInitIndexExpr.
bool VisitPointerCompoundAssignOperator(const CompoundAssignOperator *E)
bool VisitFloatCompoundAssignOperator(const CompoundAssignOperator *E)
bool DiscardResult
Flag indicating if return value is to be discarded.
bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E)
bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E)
const Function * getFunction(const FunctionDecl *FD)
bool visitLocalInitializer(const Expr *Init, unsigned I)
Visits an initializer for a local.
bool VisitLogicalBinOp(const BinaryOperator *E)
bool visitVarDecl(const VarDecl *VD)
Creates and initializes a variable from the given decl.
bool VisitStringLiteral(const StringLiteral *E)
bool VisitCXXConstructExpr(const CXXConstructExpr *E)
bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E)
bool VisitSYCLUniqueStableNameExpr(const SYCLUniqueStableNameExpr *E)
ByteCodeExprGen(Context &Ctx, Program &P, Tys &&... Args)
Initializes the compiler and the backend emitter.
const Expr * SourceLocDefaultExpr
DefaultInit- or DefaultArgExpr, needed for SourceLocExpr.
bool VisitGNUNullExpr(const GNUNullExpr *E)
Record * getRecord(QualType Ty)
Returns a record from a record or pointer type.
llvm::DenseMap< const OpaqueValueExpr *, unsigned > OpaqueExprs
OpaqueValueExpr to location mapping.
bool VisitSourceLocExpr(const SourceLocExpr *E)
bool VisitOpaqueValueExpr(const OpaqueValueExpr *E)
bool VisitCXXThisExpr(const CXXThisExpr *E)
bool visitInitList(ArrayRef< const Expr * > Inits, const Expr *ArrayFiller, const Expr *E)
bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E)
typename Emitter::LabelTy LabelTy
bool visitAPValue(const APValue &Val, PrimType ValType, const Expr *E)
Visit an APValue.
bool VisitPointerArithBinOp(const BinaryOperator *E)
Perform addition/subtraction of a pointer and an integer or subtraction of two pointers.
bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E)
bool VisitCompoundAssignOperator(const CompoundAssignOperator *E)
bool VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E)
bool VisitCallExpr(const CallExpr *E)
const RecordType * getRecordTy(QualType Ty)
Returns a record type from a record or pointer type.
bool delegate(const Expr *E)
Just pass evaluation on to E.
bool VisitConstantExpr(const ConstantExpr *E)
bool VisitChooseExpr(const ChooseExpr *E)
bool VisitCastExpr(const CastExpr *E)
bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E)
bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E)
bool VisitRecoveryExpr(const RecoveryExpr *E)
typename Emitter::AddrTy AddrTy
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:117
std::optional< PrimType > classify(QualType T) const
Classifies a type.
Definition: Context.cpp:119
Scope used to handle temporaries in toplevel variable declarations.
Emits the destructors of the variables of.
DestructorScope(LocalScope< Emitter > &OtherScope)
ExprScope(ByteCodeExprGen< Emitter > *Ctx)
Bytecode function.
Definition: Function.h:77
Generic scope for local variables.
bool destroyLocals()
Explicit destruction of local variables.
~LocalScope() override
Emit a Destroy op for this scope.
void emitDestruction() override
Overriden to support explicit destruction.
void removeIfStoredOpaqueValue(const Scope::Local &Local)
bool emitDestructors() override
void addLocal(const Scope::Local &Local) override
std::optional< unsigned > Idx
Index of the scope in the chain.
LocalScope(ByteCodeExprGen< Emitter > *Ctx)
Scope used to handle initialization methods.
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:72
SourceLocScope(ByteCodeExprGen< Emitter > *Ctx, const Expr *DefaultExpr)
Scope chain managing the variable lifetimes.
virtual void addExtended(const Scope::Local &Local)
void addExtended(const Scope::Local &Local, const ValueDecl *ExtendingDecl)
void add(const Scope::Local &Local, bool IsExtended)
VariableScope * getParent() const
VariableScope(ByteCodeExprGen< Emitter > *Ctx, const ValueDecl *VD)
VariableScope * Parent
Link to the parent scope.
virtual void addLocal(const Scope::Local &Local)
ByteCodeExprGen< Emitter > * Ctx
ByteCodeExprGen instance.
Defines the clang::TargetInfo interface.
llvm::APInt APInt
Definition: Integral.h:29
unsigned llvm::PointerUnion< const Decl *, const Expr * > DeclTy
Definition: Descriptor.h:27
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:32
bool Init(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1472
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
unsigned long uint64_t
Information about a local's storage.
Definition: Function.h:38