36 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
cec2073f8e [clang][Interp] Diagnose comparisions against weak function pointers 2024-03-05 05:44:09 +01:00
Timm Bäder
f15d799f16 [clang][Interp] Fix variadic operator calls
Operator calls pass their instance member explicitly, so remove
it from NumParams when calling a variadic function
2024-03-01 15:46:00 +01:00
Timm Bäder
411c5dde59 [clang][Interp] Handle null function pointers
We were instead asserting that they are non-null before.
2024-02-25 17:26:36 +01:00
Timm Bäder
21431e0f94 [clang][Interp] Remove questionable initializer special case
I'm not sure where this would be needed, but for the time being,
removing it fixes a problem.
2024-02-19 12:18:49 +01:00
Timm Bäder
54826d4980 [clang][Interp] Emit dummy pointers for unknown static locals
We used to emit dummy pointers for unknown declarations in certain
cases in C, but this is also necessary in C++.

I'm limiting this to static local variables for now.
2024-02-15 18:12:47 +01:00
Timm Bäder
b93916c979 [clang][Interp][NFC] Convert test to verify=expected,both style 2024-02-15 07:23:35 +01:00
Timm Bäder
b200dfc159 [clang][Interp] Fix calling invalid function pointers
Checking for isConstexpr() is wrong; we need to (try to) call
the function and let later code diagnose the failure accordingly.
2024-02-15 07:23:35 +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 Baeder
47df391296
[clang][Interp] Handle std::move etc. builtins (#70772) 2024-01-31 15:57:23 +01:00
Timm Baeder
e3993e044e
[clang][Interp] Implement __builtin_addressof (#77303)
We don't need to do anything here, since the input is already a Pointer.
The only complexity is that we pre-classify the parameters as PT_Ptr,
but they might end up being of a different pointer type, e.g. PT_FnPtr.
2024-01-11 09:02:24 +01:00
Timm Baeder
a8977005b6
[clang][Interp] Don't diagnose undefined functions when checking... (#75051)
... for a potential constant expression. They are not defined now, but
might be defined later when the function is actually called.
2023-12-13 12:14:53 +01:00
Timm Baeder
190b9179a5
[clang][Interp] Handle SizeOfPackExprs (#71929) 2023-11-12 05:34:16 +01:00
Timm Baeder
c8b267e98f
[clang][Interp] Handle variadic functions (#67814)
Similarly to the code we already had for builtin functions, we need to
check the call expression for the arguments passed.
2023-10-24 12:33:29 +02:00
Timm Baeder
17414eae24
[clang][Interp] Fix returning nullptr from functions (#67229)
isLive() is false for null pointers, so we need to special-case this
here.
2023-10-10 11:19:14 +02:00
Timm Baeder
364757d2d5
[clang][Interp] Fix compiling undefined templated functions (#67232) 2023-09-25 17:28:44 +02:00
Timm Bäder
23c39f9a9e [clang][Interp] Diagnose unknown parameter values
Differential Revision: https://reviews.llvm.org/D156509
2023-09-15 13:10:19 +02:00
Timm Bäder
015ffba811 [clang][Interp] Fix converting function pointers to bool
Differential Revision: https://reviews.llvm.org/D156786
2023-08-01 15:36:34 +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
c7251385c8 [clang][Interp] Check pointers for live-ness when returning them
We might be trying to return a pointer or reference to a local variable,
which doesn't work.

Differential Revision: https://reviews.llvm.org/D154795
2023-07-26 09:52:09 +02:00
Timm Bäder
8a4bbeb916 [clang][Interp] Remove args from called functions in more cases
When calling functions in the checkingPotentialConstantExpression mode,
we cannot have arguments (including This + RVO pointers) for the
toplevel callee, but the functions called from within can work just
fine, or at least we succeed in pushing their arguments on the stack, so
we must also succeed in removing them again.

Differential Revision: https://reviews.llvm.org/D150358
2023-07-26 08:47:54 +02:00
Timm Bäder
f8a9c55bef [clang][Interp] Emit diagnostic when comparing function pointers
Function pointers can be compared for (in)equality but, but LE, GE, LT,
and GT opcodes should emit an error and abort.

Differential Revision: https://reviews.llvm.org/D149154
2023-04-27 12:33:28 +02:00
Timm Bäder
f43adc4980 [clang][Interp] Add missing static_assert message 2023-04-08 15:44:11 +02:00
Timm Bäder
0260ea3a5b [clang][Interp][NFC] Add test for e7f55bb 2023-04-08 15:28:47 +02:00
Timm Bäder
1c818b0a4f [clang][Interp] Fix a crash when calling invalid constexpr functions
Differential Revision: https://reviews.llvm.org/D147845
2023-04-08 15:28:47 +02:00
Timm Bäder
82facc2b24 [clang][Interp] Add missing static_assert message
This broke builders, e.g:
https://lab.llvm.org/buildbot/#builders/139/builds/38457
2023-04-03 17:12:05 +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
e9b150c2d9 [clang][Interp][NFC] Add missing static_assert message
This broke builders, e.g.
https://lab.llvm.org/buildbot#builders/139/builds/38298
2023-03-31 08:22:34 +02:00
Timm Bäder
fce093ccb1 [clang][Interp] Fix parameter map when re-visiting function
'Params' is a member of the ByteCodeEmitter. We only added the
parameters the first time  we saw the function, so subsequent visits
didn't work if they had (and used) parameters.

Just do the work everytime we see a function.

Differential Revision: https://reviews.llvm.org/D141681
2023-03-31 08:06:06 +02:00
Timm Bäder
30f96a8fb4 [clang][Interp] Properly identify not-yet-defined functions
Since we now handle functions without a body as well, we can't just use
getHasBody() anymore. Funtions that haven't been defined yet are those
that don't have a body *and* aren't valid.

Also, just pass the information about whether the Function has a body or
not along from the FunctionDecl.

Differential Revision: https://reviews.llvm.org/D141591
2023-03-31 07:27:04 +02:00
Timm Bäder
3930e3331e [clang][Interp] Add missing static_assert messages
This broke builders, e.g.
https://lab.llvm.org/buildbot/#builders/139/builds/38250
2023-03-30 15:43:43 +02:00
Timm Bäder
3ad167329a [clang][Interp] Implement function pointers
Differential Revision: https://reviews.llvm.org/D141472
2023-03-30 15:37:49 +02:00
Timm Bäder
f4a6842c5a [clang][Interp] Reject invalid declarations and expressions
Reject them early, since we will run into problems and/or assertions
later on anyway.

Differential Revision: https://reviews.llvm.org/D137386
2023-01-25 12:52:44 +01:00
Timm Bäder
c3380c32f8 [clang][Interp] Handle undefined functions better
Differential Revision: https://reviews.llvm.org/D136936
2022-11-30 10:09:52 +01:00
Timm Bäder
5c4dbff0b6 [clang][Interp] Handle SubstNonTypeTemplateParmExprs
Differential Revision: https://reviews.llvm.org/D132831
2022-09-08 07:31:07 +02:00
Timm Bäder
8e41e6a4ea [clang][Interp] Implement function calls
Add Call() and CallVoid() ops and use them to call functions. Only
FunctionDecls are supported for now.

Differential Revision: https://reviews.llvm.org/D132286
2022-09-08 07:31:07 +02:00