Commit Graph

9863 Commits

Author SHA1 Message Date
Kazu Hirata 35b4fbb559 [clang] Use std::nullopt instead of None in comments (NFC)
This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-04 15:57:24 -08:00
Kazu Hirata e31564afc3 [AST] Use std::nullopt instead of None (NFC)
This patch mechanically replaces None with std::nullopt where the
compiler would warn if None were deprecated.  The intent is to reduce
the amount of manual work required in migrating from Optional to
std::optional.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-03 11:13:41 -08:00
Vassil Vassilev dc4889357a [clang-repl] Support statements on global scope in incremental mode.
This patch teaches clang to parse statements on the global scope to allow:
```
./bin/clang-repl
clang-repl> int i = 12;
clang-repl> ++i;
clang-repl> extern "C" int printf(const char*,...);
clang-repl> printf("%d\n", i);
13
clang-repl> %quit
```

Generally, disambiguating between statements and declarations is a non-trivial
task for a C++ parser. The challenge is to allow both standard C++ to be
translated as if this patch does not exist and in the cases where the user typed
a statement to be executed as if it were in a function body.

Clang's Parser does pretty well in disambiguating between declarations and
expressions. We have added DisambiguatingWithExpression flag which allows us to
preserve the existing and optimized behavior where needed and implement the
extra rules for disambiguating. Only few cases require additional attention:
  * Constructors/destructors -- Parser::isConstructorDeclarator was used in to
    disambiguate between ctor-looking declarations and statements on the global
    scope(eg. `Ns::f()`).
  * The template keyword -- the template keyword can appear in both declarations
    and statements. This patch considers the template keyword to be a declaration
    starter which breaks a few cases in incremental mode which will be tackled
    later.
  * The inline (and similar) keyword -- looking at the first token in many cases
    allows us to classify what is a declaration.
  * Other language keywords and specifiers -- ObjC/ObjC++/OpenCL/OpenMP rely on
    pragmas or special tokens which will be handled in subsequent patches.

The patch conceptually models a "top-level" statement into a TopLevelStmtDecl.
The TopLevelStmtDecl is lowered into a void function with no arguments.
We attach this function to the global initializer list to execute the statement
blocks in the correct order.

Differential revision: https://reviews.llvm.org/D127284
2022-12-03 07:18:07 +00:00
Jan Svoboda abf0c6c0c0 Use CTAD on llvm::SaveAndRestore
Reviewed By: dblaikie

Differential Revision: https://reviews.llvm.org/D139229
2022-12-02 15:36:12 -08:00
Akira Hatanaka 1127e479e8 Don't revisit the subexpressions of PseudoObjectExpr when building a
ParentMap

The assertion that is removed in this patch was failing when ObjC dot
notation expressions appear in both sides of an assignment (see the test
case in arc-repeated-weak.mm). Visit the PseudoObjectExpr once when the
syntactic expression is visited and return without visiting the
subexpressions when it's visited again when the semantic expressions are
visited.

Differential Revision: https://reviews.llvm.org/D139171
2022-12-02 11:41:09 -08:00
Alex Richardson f3a17d0595 [clang] Avoid duplicating ProgramAddressSpace in TargetInfo. NFCI
This value was added to clang/Basic in D111566, but is only used during
codegen, where we can use the LLVM IR DataLayout instead. I noticed this
because the downstream CHERI targets would have to also set this value
for AArch64/RISC-V/MIPS. Instead of duplicating more information between
LLVM IR and Clang, this patch moves getTargetAddressSpace(QualType T) to
CodeGenTypes, where we can consult the DataLayout.

Reviewed By: rjmccall

Differential Revision: https://reviews.llvm.org/D138296
2022-12-01 20:40:58 +00:00
Alex Richardson a602f76a24 [clang][TargetInfo] Use LangAS for getPointer{Width,Align}()
Mixing LLVM and Clang address spaces can result in subtle bugs, and there
is no need for this hook to use the LLVM IR level address spaces.
Most of this change is just replacing zero with LangAS::Default,
but it also allows us to remove a few calls to getTargetAddressSpace().

This also removes a stale comment+workaround in
CGDebugInfo::CreatePointerLikeType(): ASTContext::getTypeSize() does
return the expected size for ReferenceType (and handles address spaces).

Differential Revision: https://reviews.llvm.org/D138295
2022-11-30 20:24:01 +00:00
Timm Bäder 89b009559c Revert "[clang][Interp] Use placement new to construct opcode args into vector"
This reverts commit aaf73ae266.

This breaks sanitized builds because the constructor is called with an
unaligned address.
2022-11-30 16:07:57 +01:00
Timm Bäder ec23e5584d [clang][Interp][NFC] Avoid unnecessary work in compileFunc()
We don't need to create the paramter descriptors etc. if we've already
done that in the past.
2022-11-30 16:07:29 +01:00
Corentin Jabot 9221bedfd7 Revert "Implement CWG2631"
This reverts commit 26fa17ed29.
This reverts commit 4403c4f9e7.

There is still an ODR issue causing linker errors, investigating.
2022-11-30 16:03:05 +01:00
Corentin Jabot 26fa17ed29 Implement CWG2631
Implement https://cplusplus.github.io/CWG/issues/2631.html.

Immediate calls in default arguments and defaults members
are not evaluated.

Instead, we evaluate them when constructing a
`CXXDefaultArgExpr`/`BuildCXXDefaultInitExpr`.

The immediate calls are executed by doing a
transform on the initializing expression.

Note that lambdas are not considering subexpressions so
we do not need to transform them.

As a result of this patch, unused default member
initializers are not considered odr-used, and
errors about members binding to local variables
in an outer scope only surface at the point
where a constructor is defined.

Reviewed By: aaron.ballman, #clang-language-wg

Differential Revision: https://reviews.llvm.org/D136554
2022-11-30 11:25:01 +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 aaf73ae266 [clang][Interp] Use placement new to construct opcode args into vector
This way we're invoking the copy constructor, which might be necessary
if the argument is not trivially constructible.

Differential Revision: https://reviews.llvm.org/D138554
2022-11-30 10:09:52 +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 8095b090db [clang][Interp] Array initialization via CXXConstructExpr
Differential Revision: https://reviews.llvm.org/D136920
2022-11-30 10:09:52 +01:00
Timm Bäder afa60d08a2 [clang][Interp] Fix discarding non-primitive function call return values
Differential Revision: https://reviews.llvm.org/D136457
2022-11-30 09:18:28 +01:00
Corentin Jabot 5607fc002d [Clang] Permit static constexpr variables in constexpr functions
This implement the C++23 paper P2647R1 (adopted in Kona)

Reviewed By: #clang-language-wg, erichkeane

Differential Revision: https://reviews.llvm.org/D138851
2022-11-28 21:38:31 +01:00
Nathan James 15e76eed0c
[clang] Add [is|set]Nested methods to NamespaceDecl
Adds support for NamespaceDecl to inform if its part of a nested namespace.
This flag only corresponds to the inner namespaces in a nested namespace declaration.
In this example:
namespace <X>::<Y>::<Z> {}
Only <Y> and <Z> will be classified as nested.

This flag isn't meant for assisting in building the AST, more for static analysis and refactorings.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D90568
2022-11-24 12:44:35 +00:00
Fangrui Song e690a34645 AST: Internalize functions after D111283 2022-11-23 22:53:23 -08:00
Kazu Hirata 34bcadc38c Use std::nullopt_t instead of NoneType (NFC)
This patch replaces those occurrences of NoneType that would trigger
an error if the definition of NoneType were missing in None.h.

To keep this patch focused, I am deliberately not replacing None with
std::nullopt in this patch or updating comments.  They will be
addressed in subsequent patches.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716

Differential Revision: https://reviews.llvm.org/D138539
2022-11-23 14:16:04 -08:00
Alex Richardson 64f5fedb59 [ASTContext][NFC] Remove getTargetAddressSpace(Qualifiers Q)
This simply calls getTargetAddressSpace(Q.getAddressSpace()) and there
are only two callers, so adjust those caller instead.
2022-11-23 09:04:42 +00:00
Kazu Hirata 6ba4b62af8 Return None instead of Optional<T>() (NFC)
This patch replaces:

  return Optional<T>();

with:

  return None;

to make the migration from llvm::Optional to std::optional easier.
Specifically, I can deprecate None (in my source tree, that is) to
identify all the instances of None that should be replaced with
std::nullopt.

Note that "return None" far outnumbers "return Optional<T>();".  There
are more than 2000 instances of "return None" in our source tree.

All of the instances in this patch come from functions that return
Optional<T> except Archive::findSym and ASTNodeImporter::import, where
we return Expected<Optional<T>>.  Note that we can construct
Expected<Optional<T>> from any parameter convertible to Optional<T>,
which None certainly is.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716

Differential Revision: https://reviews.llvm.org/D138464
2022-11-21 19:06:42 -08:00
Kazu Hirata 30f9eb1eb8 [clang] Remove unused forward declarations (NFC) 2022-11-20 14:32:17 -08:00
Jennifer Yu 9d90cf2fca [OPENMP5.1] Initial support for message clause. 2022-11-18 17:59:23 -08:00
Fazlay Rabbi 56c1660170 [OpenMP] Initial parsing/sema for 'strict' modifier with 'num_tasks' clause
This patch gives basic parsing and semantic analysis support for 'strict'
modifier with 'num_tasks' clause of 'taskloop' construct introduced in
OpenMP 5.1 (section 2.12.2)

Differential Revision: https://reviews.llvm.org/D138328
2022-11-18 16:26:47 -08:00
Fazlay Rabbi ab9eac762c [OpenMP] Initial parsing/sema for 'strict' modifier with 'grainsize' clause
This patch gives basic parsing and semantic analysis support for 'strict'
modifier with 'grainsize' clause of 'taskloop' construct introduced in
OpenMP 5.1 (section 2.12.2)

Differential Revision: https://reviews.llvm.org/D138217
2022-11-17 20:59:07 -08:00
Volodymyr Sapsai a65d5309d5 [ODRHash] Detect duplicate `ObjCProtocolDecl` ODR mismatches during parsing.
When during parsing we encountered a duplicate `ObjCProtocolDecl`, we
were always emitting an error. With this change we accept
* when a previous `ObjCProtocolDecl` is in a hidden [sub]module;
* parsed `ObjCProtocolDecl` is the same as the previous one.

And in case of mismatches we provide more detailed error messages.

rdar://93069080

Differential Revision: https://reviews.llvm.org/D130327
2022-11-17 18:31:32 -08:00
Volodymyr Sapsai dcb71b5e1d [ODRHash] Hash `ObjCPropertyDecl` and diagnose discovered mismatches.
Differential Revision: https://reviews.llvm.org/D130326
2022-11-17 17:22:03 -08:00
Jennifer Yu 1e054e6b52 [OPENMP5.1] Initial support for severity clause
Differential Revision:https://reviews.llvm.org/D138227
2022-11-17 16:05:02 -08:00
Doru Bercea 98bfd7f976 Fix declare target implementation to support enter. 2022-11-17 17:35:53 -06:00
Timm Bäder ec782951d7 [clang] Short-circuit evaluation in ::EvaluateAsConstantExpr
Use FastEvaluateAsRValue() in EvaluateAsConstantExpr() as well, to
short-circuit evaluation of simple integrals.

Differential Revision: https://reviews.llvm.org/D138115
2022-11-17 12:01:37 +01:00
Serge Pavlov 1ddd586308 [clang] Missed rounding mode use in constant evaluation
Integer-to-float conversion was handled in constant evaluator with
default rounding mode. This change fixes the behavior and the conversion
is made using rounding mode stored in ImplicitCastExpr node.

Differential Revision: https://reviews.llvm.org/D137719
2022-11-17 12:05:28 +07:00
Eli Friedman 0fcb26c5b6 [clang] Fix __try/__finally blocks in C++ constructors.
We were crashing trying to convert a GlobalDecl from a
CXXConstructorDecl.  Instead of trying to do that conversion, just pass
down the original GlobalDecl.

I think we could actually compute the correct constructor/destructor
kind from the context, given the way Microsoft mangling works, but it's
simpler to just pass through the correct constructor/destructor kind.

Differential Revision: https://reviews.llvm.org/D136776
2022-11-16 15:13:33 -08:00
Zequan Wu 29c9287917 [AST] Fix class layout when using external layout under MS ABI.
Reviewed By: rnk

Differential Revision: https://reviews.llvm.org/D137806
2022-11-16 11:17:50 -08:00
Timm Bäder d128a03ff3 [clang][Interp][NFC] Use constexpr if in OffsetHelper
Add here is a template parameter, so we can do this.
2022-11-16 09:56:29 +01:00
Akira Hatanaka 063a43b4fd [ObjC] Fix an assertion failure in EvaluateLValue
Look through parentheses when determining whether the expression is a
@selector expression.
2022-11-15 14:41:28 -08:00
Jennifer Yu 628fdc3f57 [OPENMP]Initial support for at clause
Error directive is allowed in both declared and executable contexts.
The function ActOnOpenMPAtClause is called in both places during the
parsers.

Adding a param "bool InExContext" to identify context which is used to
emit error massage.

Differential Revision: https://reviews.llvm.org/D137851
2022-11-15 14:06:50 -08:00
Reed 88eb3c62f2 Add FP8 E4M3 support to APFloat.
NVIDIA, ARM, and Intel recently introduced two new FP8 formats, as described in the paper: https://arxiv.org/abs/2209.05433. The first of the two FP8 dtypes, E5M2, was added in https://reviews.llvm.org/D133823. This change adds the second of the two: E4M3.

There is an RFC for adding the FP8 dtypes here: https://discourse.llvm.org/t/rfc-add-apfloat-and-mlir-type-support-for-fp8-e5m2/65279. I spoke with the RFC's author, Stella, and she gave me the go ahead to implement the E4M3 type. The name of the E4M3 type in APFloat is Float8E4M3FN, as discussed in the RFC. The "FN" means only Finite and NaN values are supported.

Unlike E5M2, E4M3 has different behavior from IEEE types in regards to Inf and NaN values. There are no Inf values, and NaN is represented when the exponent and mantissa bits are all 1s. To represent these differences in APFloat, I added an enum field, fltNonfiniteBehavior, to the fltSemantics struct. The possible enum values are IEEE754 and NanOnly. Only Float8E4M3FN has the NanOnly behavior.

After this change is submitted, I plan on adding the Float8E4M3FN type to MLIR, in the same way as E5M2 was added in https://reviews.llvm.org/D133823.

Reviewed By: bkramer

Differential Revision: https://reviews.llvm.org/D137760
2022-11-15 20:26:42 +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 0dcfd0ce02 [clang][Interp] Support alignof()
Support alignof() and __alignof() expressions.

Fixes #58816

Differential Revision: https://reviews.llvm.org/D137240
2022-11-11 08:38:06 +01:00
Timm Bäder 7863646fd2 [clang][Interp] DerivedToBase casts
Differential Revision: https://reviews.llvm.org/D137545
2022-11-11 08:38:06 +01:00
Rageking8 94738a5ac3 Fix duplicate word typos; NFC
This revision fixes typos where there are 2 consecutive words which are
duplicated. There should be no code changes in this revision (only
changes to comments and docs). Do let me know if there are any
undesirable changes in this revision. Thanks.
2022-11-08 07:21:23 -05:00
Grace Jennings 86674f66cc [HLSL] Added HLSL this as a reference
This change makes `this` a reference instead of a pointer in
HLSL. HLSL does not have the `->` operator, and accesses through `this`
are with the `.` syntax.

Tests were added and altered to make sure
the AST accurately reflects the types.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D135721
2022-11-07 13:50:08 -08:00
Arthur Eubanks cd8c6ab084 [NFC] clang-format part of TypePrinter.cpp
Fixes weird clang-format indentation with an upcoming patch.
2022-11-07 13:20:36 -08:00
Nathan James 108e41d962
[clang][NFC] Use c++17 style variable type traits
This was done as a test for D137302 and it makes sense to push these changes

Reviewed By: shafik

Differential Revision: https://reviews.llvm.org/D137491
2022-11-07 18:25:48 +00:00
Timm Bäder 5dfacb1245 [clang][Interp][NFC] Replace dyn_cast_or_null with _if_present
... in Descriptor.h
2022-11-07 09:42:41 +01:00
Timm Bäder 5bd6bd1227 [clang][Interp][NFC] Simplify visitReturnStmt() 2022-11-07 09:42:41 +01:00
Timm Bäder 6b3e5c595b [clang][Interp][NFC] Remove unused function 2022-11-07 09:42:41 +01:00
Timm Bäder c8341a6615 [clang][Interp][NFC] Avoid a getSource() call in the common case
In the common (successful) case, we don't need the getSource() call, so
move it to the two if statement bodies instead.
2022-11-07 09:42:41 +01:00
Timm Bäder f4707af294 [clang][Interp][NFCI] Cleanup emitConst()
Before, when emitting a regular integer constant, we went:

Int -> APInt -> int -> emit

Fix this by using regular integer constants in emitConst() and instead
converting APInt to those once.
2022-11-07 09:05:29 +01:00