71 Commits

Author SHA1 Message Date
Timm Baeder
1709eac58f
[clang][Interp] Integral pointers (#84159)
This turns the current `Pointer` class into a discriminated union of
`BlockPointer` and `IntPointer`. The former is what `Pointer` currently
is while the latter is just an integer value and an optional
`Descriptor*`.

The `Pointer` then has type check functions like
`isBlockPointer()`/`isIntegralPointer()`/`asBlockPointer()`/`asIntPointer()`,
which can be used to access its data.

Right now, the `IntPointer` and `BlockPointer` structs do not have any
methods of their own and everything is instead implemented in Pointer
(like it was before) and the functions now just either assert for the
right type or decide what to do based on it.

This also implements bitcasts by decaying the pointer to an integral
pointer.

`test/AST/Interp/const-eval.c` is a new test testing all kinds of stuff
related to this. It still has a few tests `#ifdef`-ed out but that
mostly depends on other unimplemented things like
`__builtin_constant_p`.
2024-04-10 12:53:54 +02:00
Timm Bäder
5334afcad8 [clang][Interp] Don't forget to visit condition variable decls
We did this for if statements, but switch and loop constructs
need to do it as well.
2024-03-15 12:30:37 +01:00
Timm Bäder
8ab0632735 [clang][Interp] Handle goto and label statements 2024-03-15 09:57:28 +01:00
Timm Bäder
bc9c6c0c65 [clang][Interp][NFC] Remove ByteCodeGenError
Unused.
2024-03-04 17:46:08 +01:00
Timm Bäder
70e61f5bbb [clang][Interp][NFC] Rename InitPtr{,Pop} to FinishInit{,Pop}
The old name clashes with the Init opcode.
2024-02-27 16:14:37 +01:00
Timm Bäder
d53515afef [clang][Interp] Fix variadic member functions
For variadic member functions, the way we calculated the instance
pointer and RVO pointer offsts on the stack was incorrect, due
to Func->getArgSize() not returning the full size of all the
passed arguments. When calling variadic functions, we need
to pass the size of the passed (variadic) arguments to the Call*
ops, so they can use that information to properly check the
instance pointer, etc.

This patch adds a bit of code duplication in Interp.h, which I
will get rid of in later cleanup NFC patches.
2024-02-15 05:59:53 +01:00
Timm Bäder
0d9decc669 [clang][Interp] Handle invalid CXXCtorInitializer expressions
Their type might be a null type, in which case we need to abort here.
2024-02-09 09:20:41 +01:00
Timm Bäder
b0c6fc81fe [clang][Interp] Ignore UsingEnumDecls
We previously aborted compilation when seeing one of them. Ignore
them instead, they have no effect on the generated bytecode.
2024-02-07 14:24:37 +01:00
Timm Baeder
4e7cf1b1ed
[clang][Interp] Add an EvaluationResult class (#71315)
Add an `EvaluationResult` class. This contains the result either as a
`Pointer` or as a `APValue`.

This way, we can inspect the result of the evaluation and diagnose
problems with it (e.g. uninitialized fields in global initializers or
pointers pointing to things they shouldn't point to).
2024-01-19 10:08:03 +01:00
Timm Baeder
819bd9e39b
[clang][Interp] IndirectMember initializers (#69900)
We need to look at the chain of declarations to initialize the right
field.
2024-01-18 16:25:05 +01:00
Timm Baeder
b72732c37f
[clang][Interp] Handle CXXTryStmts (#70584)
Just do the same thing the current interpreter does: Ignore all handlers
and visit the try block like normal.
2023-10-30 09:38:02 +01:00
Timm Baeder
de9b3c5eba
[clang][Interp] Handle delegating constructors (#67823) 2023-10-16 14:11:25 +02:00
Timm Bäder
6172960cea [clang][Interp] Basic support for bit fields
Differential Revision: https://reviews.llvm.org/D155548
2023-10-10 13:38:26 +02:00
Timm Baeder
f58d54ab96
[clang][Interp] Diagnose uninitialized bases (#67131) 2023-10-03 06:46:31 +02:00
Timm Bäder
3935a18a5c [clang][Interp][NFC] Use GetPtrThisField intead of two ops 2023-09-24 09:56:48 +02:00
Timm Baeder
d462bd527a
[clang][Interp] Handle AttributedStmts (#66495)
Just ignore the attributes.
2023-09-15 21:03:18 +02:00
Timm Bäder
73371faf05 [clang][Interp][NFC] Remove unused includes 2023-09-15 13:37:35 +02:00
Takuya Shimizu
01b88dd66d [NFC] Remove unused variables declared in conditions
D152495 makes clang warn on unused variables that are declared in conditions like `if (int var = init) {}`
This patch is an NFC fix to suppress the new warning in llvm,clang,lld builds to pass CI in the above patch.

Differential Revision: https://reviews.llvm.org/D158016
2023-08-30 10:05:06 +09:00
Timm Bäder
89361e2b98 [clang][Interp] Fix passing parameters of composite type
We pass these as pointers, so we need to be careful not to emit pointers
to pointers when we emit visit DeclRefExprs pointing to parameters.

Differential Revision: https://reviews.llvm.org/D153695
2023-08-17 12:08:10 +02:00
Timm Bäder
b56ab41d54 Revert "[clang][Interp] Basic support for bit fields"
This reverts commit 8065b1cc133c9e4b6900d5c11220617180706b94.

This breaks builders. I forgot this depends
on https://reviews.llvm.org/D155548
2023-08-10 09:17:07 +02:00
Timm Bäder
8065b1cc13 [clang][Interp] Basic support for bit fields
Differential Revision: https://reviews.llvm.org/D155270
2023-08-10 08:50:10 +02:00
Timm Bäder
744a968f91 [clang][Interp] Fix return statements with expresssion in void functions
If the return type of a function is void, ReturnType is not set, but we
used to emit a RVOPtr instruction, which doesn't make sense for a
function returning void.

Differential Revision: https://reviews.llvm.org/D153649
2023-07-26 11:50:07 +02:00
Timm Bäder
7d876c62a3 [clang][Interp] Add 'Invalid' opcode and use it for throw stmts
We will use this opcode for conditionally executed statements that are
invalid in a constant expression.

Differential Revision: https://reviews.llvm.org/D150364
2023-07-26 08:59:53 +02:00
Timm Bäder
6d2e141e5c [clang][Interp] Handle lambda static invokers
Differential Revision: https://reviews.llvm.org/D150111
2023-07-26 08:42:16 +02:00
Timm Bäder
142bfaff7e [clang][Interp][NFC] Fix two small code style problems 2023-07-23 10:31:22 +02:00
Timm Bäder
25d6123854 [clang][Interp] Make sure we have a variable scope for initializers
Otherwise, we run into an assertion when trying to use the current
variable scope while creating temporaries for constructor initializers.

Differential Revision: https://reviews.llvm.org/D147534
2023-04-13 15:35:30 +02:00
Timm Bäder
ee71cbddb7 [clang][Interp] Ignore more non-VarDecl declarations
They are harmless and handled by other means, but we used to return
false from visitDeclStmt.

Differential Revision: https://reviews.llvm.org/D145861
2023-04-04 10:41:46 +02:00
Timm Bäder
192c2c5c89 [clang][Interp] Ignore StaticAssertDecls
They have already been handled before, but we can't just return false
when we encounter one.

Differential Revision: https://reviews.llvm.org/D144272
2023-04-03 16:56:58 +02:00
Timm Bäder
243b355ee0 [clang][Interp] Support destructors
Emit destructors for non-primitive (array) variables on scope ends.

Differential Revision: https://reviews.llvm.org/D145545
2023-03-30 13:17:04 +02:00
HerrCai0907
73f9a0c923 Fix typo: statment to statement; NFC
Differential Revision: https://reviews.llvm.org/D145834
2023-03-13 11:51:28 -04:00
Timm Bäder
5e12002c6c Revert "[clang][Interp] Support destructors"
This reverts commit 78e4237460bf58f3d6b75f275e0424f38e3b1d04.

This breaks the memory sanitizer builder:
https://lab.llvm.org/buildbot/#/builders/5/builds/31959
2023-03-05 13:18:13 +01:00
Timm Bäder
78e4237460 [clang][Interp] Support destructors
Use the existing local variable cleanup infrastructure to implement
destruction.

Differential Revision: https://reviews.llvm.org/D137070
2023-03-05 10:02:42 +01:00
Timm Bäder
2708869801 Re-land "[clang][Interp] Implement C++ Range-for loops"
Differential Revision: https://reviews.llvm.org/D140803
2023-03-04 15:17:44 +01:00
Timm Bäder
43e67707f9 Revert "[clang][Interp] Implement C++ Range-for loops"
This reverts commit bce8b3c1830434c10b8a30380db522d7c6a8658d.

This commit breaks memory-sanitizer builds:
https://lab.llvm.org/buildbot/#/builders/5/builds/31899
2023-03-02 11:04:09 +01:00
Timm Bäder
bce8b3c183 [clang][Interp] Implement C++ Range-for loops
Differential Revision: https://reviews.llvm.org/D140803
2023-03-02 08:59:07 +01:00
Timm Bäder
730eca6a3d [clang][Interp] Handle DecompositionDecls
Differential Revision: https://reviews.llvm.org/D138802
2023-03-01 17:11:57 +01:00
Timm Bäder
8b70d97c39 [clang][Interp][NFC] Use InitThisField in initializers
This creates fewer instructions, makes the bytecode easier to read and
InitThisField also cares about checkingPotentialConstantExpression()
cases.
2023-01-25 16:59:19 +01:00
Timm Bäder
024e4f16ca [clang][Interp] Implement switch statements
Differential Revision: https://reviews.llvm.org/D137415
2023-01-25 13:21:24 +01:00
Timm Bäder
3665da3d00 Re-commit "[clang][Interp] Unify visiting variable declarations"
We often visit the same variable multiple times, e.g. once when checking
its initializer and later when compiling the function. Unify both of
those in visitVarDecl() and do the returning of the value in
visitDecl().

This time, use a VariableScope instead of a DeclScope for local
variables. This way, we don't emit Destroy ops for the local variables
immediately after creating them.

Differential Revision: https://reviews.llvm.org/D136815
2023-01-21 10:23:53 +01:00
Timm Bäder
8df7e818de Revert "[clang][Interp] Unify visiting variable declarations"
This reverts commit 5b54cf1a2892767fe949826a32d7820732028a38.

This breaks a builder: https://lab.llvm.org/buildbot/#/builders/5/builds/30854
2023-01-19 13:52:47 +01:00
Timm Bäder
5b54cf1a28 [clang][Interp] Unify visiting variable declarations
We often visit the same variable multiple times, e.g. once when checking
its initializer and later when compiling the function. Unify both of
those in visitVarDecl() and do the returning of the value in
visitDecl().

Differential Revision: https://reviews.llvm.org/D136815
2023-01-19 10:46:16 +01:00
Timm Bäder
865094746e [clang][Interp] Track initialization state of local variables
Use an InlineDescriptor per local variable to track whether locals
have been initialized or not. This way we can support uninitialized
local variables in constexpr functions.

Differential Revision: https://reviews.llvm.org/D135750
2023-01-18 16:55:02 +01:00
Timm Bäder
d4f1f35978 [clang][Interp][NFC] Move to std::optional 2022-12-05 17:31:49 +01:00
Timm Bäder
7c215a4571 [clang][Interp] Explicitly handle RVO Pointer
The calling convention is:

[RVO pointer]
[instance pointer]
[... args ...]

We handle the instance pointer ourselves, BUT for the RVO pointer, we
just assumed in visitReturnStmt() that it is on top of the stack. Which
isn't true if there are other args present (and a this pointer, maybe).

Fix this by recording the RVO pointer explicitly when creating an
InterpFrame, just like we do with the instance/This pointer.

There is already a "RVOAndParams()" test in test/AST/Inter/records.cpp,
that was supposed to test this, however, it didn't trigger any
problematic behavior because the parameter and the return value have the
same type.

Differential Revision: https://reviews.llvm.org/D137392
2022-11-30 10:37:57 +01:00
Timm Bäder
99d3ead44c [clang][Interp] Protect Record creation against infinite recursion
This happens only in error cases, but we need to handle it anyway.

Differential Revision: https://reviews.llvm.org/D136831
2022-11-11 08:38:06 +01:00
Timm Bäder
5bd6bd1227 [clang][Interp][NFC] Simplify visitReturnStmt() 2022-11-07 09:42:41 +01:00
Timm Bäder
9a3b969d1f [clang][Interp][NFC] Make InitField() not pop the pointer
This was confusing. InitElem peeks a pointer, while InitElemPop will
pop the pointer. However, for fields, InitField would pop the pointer
and no InitFieldPop exists. At least make InitField and InitElem behave
the same.
2022-11-07 08:30:43 +01:00
Timm Bäder
0ccff030f3 [clang][Interp] Fix record members of reference type
When assigning to them, we can't classify the expression type, because
that doesn't contain the right information.

And when reading from them, we need to do the extra deref, just like we
do when reading from a DeclRefExpr.

Differential Revision: https://reviews.llvm.org/D136012
2022-10-28 16:34:45 +02:00
Timm Bäder
751f1bfb5f [clang][Interp][NFC] Assert we have a valid Record instance 2022-10-26 08:49:31 +02:00
Timm Bäder
f6f02e0900 [clang][Interp] Implement for loops
Differential Revision: https://reviews.llvm.org/D135764
2022-10-26 08:48:36 +02:00