Commit Graph

91 Commits

Author SHA1 Message Date
Timm Bäder
d16cf470ac [clang][Interp] Start implementing binary operators for complex types
Differential Revision: https://reviews.llvm.org/D155572
2023-12-14 15:33:52 +01:00
Timm Bäder
497480b38a [clang][Interp] IntegralComplexToBoolean casts
Differential Revision: https://reviews.llvm.org/D148426
2023-12-14 13:11:00 +01:00
Timm Bäder
4a8b43ba3b [clang][Interp][NFC] Factor array element init into its own function 2023-11-20 13:00:57 +01:00
Timm Baeder
410f130bb9 [clang][Interp] Fix classify for glvalues of function type (#72269)
This can't be tested right now but will show up once we use the new
interpreter in evaluateAsConstantExpression() as well.

Pulled out from https://github.com/llvm/llvm-project/pull/70763
2023-11-14 19:40:59 +01:00
Timm Baeder
216dfd5ff0 [clang][Interp] Fix stack peek offset for This ptr (#70663)
`Function::getArgSize()` include both the instance and the RVO pointer,
so we need to subtract here.
2023-11-14 16:22:22 +01:00
Timm Baeder
190b9179a5 [clang][Interp] Handle SizeOfPackExprs (#71929) 2023-11-12 05:34:16 +01:00
Timm Bäder
311f725d9a [clang][Interp] Create only globals when initializing a global variable
For this code:

struct O {
  int &&j;
};

O o1(0);

The generated AST for the initializer of o1 is:

VarDecl 0x62100006ab08 <array.cpp:119:3, col:9> col:5 o1 'O':'O' parenlistinit
`-ExprWithCleanups 0x62100006b250 <col:7, col:9> 'O':'O'
  `-CXXParenListInitExpr 0x62100006b210 <col:7, col:9> 'O':'O'
    `-MaterializeTemporaryExpr 0x62100006b1f0 <col:8> 'int' xvalue
      `-IntegerLiteral 0x62100006abd0 <col:8> 'int' 0

Before this patch, we create a local temporary variable for the
MaterializeTemporaryExpr and destroy it again when destroying the
EvalEmitter we create to interpret the initializer. However, since
O::j is a reference, this reference now points to a local variable
that doesn't exist anymore.

Differential Revision: https://reviews.llvm.org/D156453
2023-10-19 14:50:17 +02:00
Timm Bäder
80737d2ddf [clang][Interp][NFC] Pass PrimType to visitZeroInitializer()
This fixes an old FIXME comment. Almost all callers already classify()
the type anyway, so just pass the result of that
to visitZeroInitializer().
2023-10-14 18:43:00 +02:00
Timm Baeder
12107386ca [clang][Interp] Handle CXXScalarValueInitExprs (#67147)
Handle those by just zero-initializing the result.
2023-10-10 12:45:09 +02:00
Timm Bäder
87461d6696 [clang][Interp] Implement __builtin_offsetof
Differential Revision: https://reviews.llvm.org/D156400
2023-09-11 12:03:47 +02:00
Timm Bäder
3276ac86aa [clang][Interp][NFC] Remove unused function prototype
This was causing warnings in MSVC.
2023-09-08 16:40:10 +02:00
Timm Bäder
92ac46e703 [clang][Interp] Handle CXXParenListInitExprs
Differential Revision: https://reviews.llvm.org/D156047
2023-09-06 15:26:54 +02:00
Timm Bäder
b93d2d37e7 [clang][Interp] Handle SourceLocExprs
Differential Revision: https://reviews.llvm.org/D155627
2023-09-06 14:45:49 +02:00
Timm Bäder
adb1fb40e8 [clang][Interp] Handle mixed floating/integral compound assign operators
Differential Revision: https://reviews.llvm.org/D157596
2023-09-05 12:10:00 +02:00
Timm Bäder
6d79f985b5 [clang][Interp] Implement zero-init of record types
Differential Revision: https://reviews.llvm.org/D154189
2023-09-05 11:20:35 +02:00
Timm Bäder
12a789710e [clang][Interp] BaseToDerived casts
We can implement these similarly to DerivedToBase casts. We just have to
walk the class hierarchy, sum the base offsets and subtract it from the
current base offset of the pointer.

Differential Revision: https://reviews.llvm.org/D149133
2023-09-05 10:53:54 +02:00
Timm Bäder
6dfe55569d [clang][Interp] Rework initializers
Before this patch, we had visitRecordInitializer() and
visitArrayInitializer(), which were different from the regular visit()
in that they expected a pointer on the top of the stack, which they
initialized. For example, visitArrayInitializer handled InitListExprs by
looping over the members and initializing the elements of that pointer.

However, this had a few corner cases and problems. For example, in
visitLambdaExpr() (a lambda is always of record type), it was not clear
whether we should always create a new local variable to save the lambda
to, or not. This is why https://reviews.llvm.org/D153616 changed
things around.

This patch changes the visiting functions to:

 - visit(): Always leaves a new value on the stack. If the expression
   can be mapped to a primitive type, it's just visited and the value is
   put on the stack. If it's of composite type, this function will
   create a local variable for the expression value and call
   visitInitializer(). The pointer to the local variable will stay on
   the stack.

 - visitInitializer(): Visits the given expression, assuming there is a
   pointer on top of the stack that will be initialized by it.

 - discard(): Visit the expression for side-effects, but don't leave a
   value on the stack.

It also adds an additional Initializing flag to differentiate between the initializing and non-initializing case.

Differential Revision: https://reviews.llvm.org/D156027
2023-08-20 13:33:08 +02:00
Timm Bäder
c4becd50ad [clang][Interp] Support __null
Differential Revision: https://reviews.llvm.org/D155552
2023-08-17 10:28:37 +02:00
Timm Bäder
378fcbf20f [clang][Interp] Handle CXXNoexceptExprs
Differential Revision: https://reviews.llvm.org/D155707
2023-07-26 12:23:54 +02:00
Timm Bäder
8c0246c7f5 [clang][Interp] Reject reinterpret_casts
Add a new InvalidCast op for this purpose and emit a diagnostic.

Differential Revision: https://reviews.llvm.org/D153276
2023-07-26 09:56:29 +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
e45d199768 [clang][Interp] Handle CXXTemporaryObjectExprs
This is a CXXConstructExpr, so create a local temporary variable and
initialize it.

Differential Revision: https://reviews.llvm.org/D147591
2023-07-26 07:36:26 +02:00
Timm Bäder
254ad7dd41 [clang][Interp][NFC] Move isGloballyIndexed() to Context
And use it in Program as well, to make a similar decision.
2023-07-09 16:27:49 +02:00
Takuya Shimizu
dfb85c3ce0 [Clang][Interp] Diagnose uninitialized ctor of global record arrays
This patch adds a check for uninitialized subobjects of global variables that are record arrays.
e.g. `constexpr Foo f[2];`

Reviewed By: tbaeder

Differential Revision: https://reviews.llvm.org/D152548
2023-06-21 19:03:01 +09:00
Timm Bäder
1e9ac71787 [clang][Interp] Handle PredefinedExprs
Differential Revision: https://reviews.llvm.org/D148689
2023-06-16 15:52:12 +02:00
Timm Bäder
461f91b1e4 [clang][Interp] Handle LambdaExprs
Differential Revision: https://reviews.llvm.org/D146030
2023-06-16 10:05:33 +02:00
Timm Bäder
c3f1faf96f [clang][Interp][NFC] Fix allocateLocalPrimitive parameter name
This is passed on to Program::createDescriptor, where it is used as a
value for IsConst.
2023-05-04 13:45:14 +02:00
Timm Bäder
8aea6e004f [clang][Interp][NFC] Take a QualType in visitZeroInitializer()
The given expression is not necessarily usable to obtain a type for,
so we can't use it to get the floating point semantics. Pass a QualType
instead, which we can use--and classify() that here.
2023-04-24 17:58:15 +02:00
Timm Bäder
92417f2d4b [clang][Interp] Record initialization via conditional operator
Differential Revision: https://reviews.llvm.org/D141497
2023-04-06 09:44:02 +02:00
Timm Bäder
626b7e5dd2 [clang][Interp][NFC] Remove Integral.h include from PrimType.h 2023-04-04 10:44:56 +02:00
Timm Bäder
968b4172f6 [clang][Interp] Fix derived-to-base casts for >1 levels
The GetPtrBasePop op we were using only works for direct base classes.

Differential Revision: https://reviews.llvm.org/D143480
2023-04-03 11:35:55 +02:00
Timm Bäder
8d2899acbc [clang][Interp] Handle TypeTraitExprs
Differential Revision: https://reviews.llvm.org/D142448
2023-03-31 15:54:44 +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
Timm Bäder
5e12002c6c Revert "[clang][Interp] Support destructors"
This reverts commit 78e4237460.

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
569222e172 [clang][Interp] Only check constructors for global variables
Local variables may be partially initialized.

Differential Revision: https://reviews.llvm.org/D140723
2023-03-02 09:13:47 +01:00
Timm Bäder
f18b71d14f [clang][Interp] Implement remaining MaterializeTemporaryExpr bits
As well as support for CompoundLiteralExprs.

Differential Revision: https://reviews.llvm.org/D140668
2023-03-02 07:51:54 +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
4a2c4acb98 [clang][Interp] Support pointers in compound assignment operators
Differential Revision: https://reviews.llvm.org/D140874
2023-02-03 16:42:46 +01:00
Timm Bäder
0505c0b8cc [clang][Interp][NFC] Make VariableScope::getParent() const 2023-02-03 15:35:42 +01:00
Timm Bäder
7bb615ea0e [clang][Interp] Materializing primitive temporaries
Implement MaterializeTemporaryExpr for primitive types.

Differential Revision: https://reviews.llvm.org/D136017
2023-02-03 15:11:15 +01:00
Timm Bäder
d2ea8ae5d4 [clang][Interp] Implement logical and/or operators
Differential Revision: https://reviews.llvm.org/D140809
2023-01-26 06:52:35 +01:00
Timm Bäder
af3a171dcf [clang][Interp] Compound assign operators for FP values
Differential Revision: https://reviews.llvm.org/D140377
2023-01-25 16:25:04 +01:00
Timm Bäder
cb7f582211 Re-apply "[clang][Interp] Support floating-point values"
Don't move the Repr struct into Integral this time.

Differential Revision: https://reviews.llvm.org/D134859
2023-01-25 15:13:09 +01:00
Timm Bäder
b3b1d86137 Revert "[clang][Interp] Support floating-point values"
This reverts commit 62f43c3eae.

This breaks a couple of builders, e.g.

https://lab.llvm.org/buildbot/#/builders/139/builds/34925
2023-01-25 14:48:39 +01:00
Timm Bäder
62f43c3eae [clang][Interp] Support floating-point values
Add a new Floating type and a few new opcodes to support floating point
values.

Differential Revision: https://reviews.llvm.org/D134859
2023-01-25 14:41:26 +01:00
Timm Bäder
a7a4463acb [clang][Interp] Start implementing builtin functions
Differential Revision: https://reviews.llvm.org/D137487
2023-01-25 14:08:03 +01:00
Timm Bäder
6469281100 [clang][Interp][NFC] Remove InitFn code
This is unused.
2023-01-23 12:19:36 +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 5b54cf1a28.

This breaks a builder: https://lab.llvm.org/buildbot/#/builders/5/builds/30854
2023-01-19 13:52:47 +01:00