clang  10.0.0svn
InterpState.cpp
Go to the documentation of this file.
1 //===--- InterpState.cpp - Interpreter for the constexpr VM -----*- 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 #include "InterpState.h"
10 #include <limits>
11 #include "Function.h"
12 #include "InterpFrame.h"
13 #include "InterpStack.h"
14 #include "Opcode.h"
15 #include "PrimType.h"
16 #include "Program.h"
17 #include "State.h"
18 
19 using namespace clang;
20 using namespace clang::interp;
21 
23 
25  Context &Ctx, SourceMapper *M)
26  : Parent(Parent), M(M), P(P), Stk(Stk), Ctx(Ctx), Current(nullptr),
27  CallStackDepth(Parent.getCallStackDepth() + 1) {}
28 
30  while (Current) {
31  InterpFrame *Next = Current->Caller;
32  delete Current;
33  Current = Next;
34  }
35 
36  while (DeadBlocks) {
37  DeadBlock *Next = DeadBlocks->Next;
38  free(DeadBlocks);
39  DeadBlocks = Next;
40  }
41 }
42 
44  if (Current && Current->Caller) {
45  return Current;
46  } else {
47  return Parent.getCurrentFrame();
48  }
49 }
50 
52  QualType Type = E->getType();
53  CCEDiag(E, diag::note_constexpr_overflow) << Value << Type;
54  return noteUndefinedBehavior();
55 }
56 
58  Descriptor *Desc = B->getDescriptor();
59  if (B->hasPointers()) {
60  size_t Size = B->getSize();
61 
62  // Allocate a new block, transferring over pointers.
63  char *Memory = reinterpret_cast<char *>(malloc(sizeof(DeadBlock) + Size));
64  auto *D = new (Memory) DeadBlock(DeadBlocks, B);
65 
66  // Move data from one block to another.
67  if (Desc->MoveFn)
68  Desc->MoveFn(B, B->data(), D->data(), Desc);
69  } else {
70  // Free storage, if necessary.
71  if (Desc->DtorFn)
72  Desc->DtorFn(B, B->data(), Desc);
73  }
74 }
Descriptor for a dead block.
Definition: Block.h:112
A (possibly-)qualified type.
Definition: Type.h:643
InterpFrame * Current
The current frame.
Definition: InterpState.h:104
bool hasPointers() const
Checks if the block has any live pointers.
Definition: Block.h:51
Interface for classes which map locations to sources.
Definition: Source.h:102
Frame storing local variables.
Definition: InterpFrame.h:29
StringRef P
The base class of the type hierarchy.
Definition: Type.h:1436
Descriptor * getDescriptor() const
Returns the block&#39;s descriptor.
Definition: Block.h:49
const BlockMoveFn MoveFn
Definition: Descriptor.h:85
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:48
InterpFrame * Caller
The frame of the previous function.
Definition: InterpFrame.h:32
bool reportOverflow(const Expr *E, const llvm::APSInt &Value)
Reports overflow and return true if evaluation should continue.
Definition: InterpState.cpp:51
A memory block, either on the stack or in the heap.
Definition: Block.h:37
bool noteUndefinedBehavior() override
Definition: InterpState.h:65
InterpSize getSize() const
Returns the size of the block.
Definition: Block.h:59
Describes a memory block created by an allocation site.
Definition: Descriptor.h:51
Stack frame storing temporaries and parameters.
Definition: InterpStack.h:22
NodeId Parent
Definition: ASTDiff.cpp:191
This represents one expression.
Definition: Expr.h:108
llvm::APSInt APSInt
Definition: InterpState.cpp:22
QualType getType() const
Definition: Expr.h:137
The program contains and links the bytecode for all functions.
Definition: Program.h:43
Base class for stack frames, shared between VM and walker.
Definition: Frame.h:25
virtual Frame * getCurrentFrame()=0
char * data()
Returns a pointer to the stored data.
Definition: Block.h:64
llvm::APSInt APSInt
Frame * getCurrentFrame() override
Definition: InterpState.cpp:43
Dataflow Directional Tag Classes.
InterpState(State &Parent, Program &P, InterpStack &Stk, Context &Ctx, SourceMapper *M=nullptr)
Definition: InterpState.cpp:24
const BlockDtorFn DtorFn
Definition: Descriptor.h:84
void deallocate(Block *B)
Deallocates a pointer.
Definition: InterpState.cpp:57
OptionalDiagnostic CCEDiag(SourceLocation Loc, diag::kind DiagId=diag::note_invalid_subexpr_in_const_expr, unsigned ExtraNotes=0)
Diagnose that the evaluation does not produce a C++11 core constant expression.
Definition: State.cpp:41
Interface for the VM to interact with the AST walker&#39;s context.
Definition: State.h:55