Boaz Brickner
a269c72e48
Fix variadic arguments test to use the format that is not deprecated in C++26 and fix the call site to be valid ( #5842 )
...
See https://en.cppreference.com/w/cpp/language/variadic_arguments.html .
Part of #5436 .
2025-07-24 19:40:19 +00:00
Boaz Brickner
68ee3d5021
Use llvm::reverse() instead of pop_back_val() in ImportDeclAndDependencies() ( #5831 )
...
This is more explicit and similar to what we do in `MapType()`.
2025-07-18 21:11:12 +00:00
Boaz Brickner
977875ec20
Add C++ inline namespace tests ( #5826 )
...
Part of #5436 .
2025-07-18 21:04:48 +00:00
Boaz Brickner
8cb01b54bd
Avoid passing name scope id and name id through ImportCXXRecordDecl() and BuildClassDefinition() ( #5829 )
...
All this information is calculated based on the Clang declaration.
2025-07-18 20:58:59 +00:00
Boaz Brickner
52976c55fb
When importing a declaration, first collect all dependent unimported declarations and import them first ( #5821 )
...
This fixes some tests since we now handle record name scopes correctly.
Part of #5533 .
2025-07-18 18:01:27 +00:00
Boaz Brickner
f4f521de7f
Explicitly mark as unsupported instead of crashing mapping a parameter record type defined not in a namespace ( #5777 )
...
Before this change we crash in this case when trying to use the outer
type.
After this change we diagnose a TODO.
Will add the missing support for this in a separate PR.
Part of #5533 .
2025-07-18 02:42:45 +00:00
Boaz Brickner
dd76c9bd13
Merge i16 and i32 param and return test files ( #5760 )
...
This is instead of having one test file that has different return types
and two test files for `i16` and `i32` param tests, which doesn't seem
consistent.
First commit does file renames for easier review.
Part of #5063 .
2025-07-16 12:53:05 +00:00
Boaz Brickner
4f8d0649d5
When importing a looked up name, reuse previously imported declaration instead of importing the same declaration again ( #5789 )
...
Added test coverage to demonstrate name lookup following import of a
type of a function parameter.
SemIR changes show that the same decl isn't imported multiple times.
Part of #5533 .
2025-07-16 11:21:49 +00:00
Boaz Brickner
a5ddc3e3cd
Support importing C++ _Nonnull pointers as function parameters or return values ( #5773 )
...
We avoid using canonical type before knowing it's not a pointer because
we need the nullability attribute.
No support for pointers to pointers, yet.
C++ Interop Demo:
```c++
// hello_world.h
auto hello_world_param(int* _Nonnull i) -> void;
auto hello_world_return() -> int* _Nonnull;
```
```c++
// hello_world.cpp
#include "hello_world.h"
#include <cstdio>
auto hello_world_param(int* _Nonnull i) -> void {
printf("hello_world: %d\n", *i);
}
static int x = 5;
auto hello_world_return() -> int* _Nonnull { return &x; }
```
```carbon
// main.carbon
library "Main";
import Core library "io";
import Cpp library "hello_world.h";
fn Run() -> i32 {
var i: i32 = 10;
Cpp.hello_world_param(&i);
let p: i32* = Cpp.hello_world_return();
Core.Print(*p);
return 0;
}
```
```shell
$ clang -c hello_world.cpp
$ ./bazel-bin/toolchain/install/prefix_root/bin/carbon compile main.carbon
$ ./bazel-bin/toolchain/install/prefix_root/bin/carbon link hello_world.o main.o --output=demo
$ ./demo
hello_world: 10
5
```
Part of #5772 .
2025-07-10 08:00:37 +00:00
Boaz Brickner
9d0aaa740b
When adding an imported C++ name, make sure that its clang::Decl is mapped if import failed ( #5769 )
...
When mapping parameter types, we assume that if the `clang::Decl` isn't
mapped, the name wasn't added, so this fixes a bug that triggers a crash
otherwise.
Part of #5533 .
2025-07-08 13:12:32 +00:00
Boaz Brickner
ff9154b978
Push a decl name scope before calling CalleePatternMatch() ( #5771 )
...
Otherwise the return values of different functions collide.
Part of #5063 .
2025-07-07 15:02:47 +00:00
Boaz Brickner
3f5b04f777
Use Core.Print instead of Carbon.Print in documentation ( #5770 )
2025-07-04 08:05:11 +00:00
Boaz Brickner
12d66be1cd
Delete files that were moved in #5716 but got undeleted in #5678 ( #5768 )
2025-07-03 10:33:25 +00:00
Boaz Brickner
b90d3b7751
Use Decl::getAsFunction() to cast clang_decl to FunctionDecl ( #5737 )
...
This seems like a better practice though has no effect since we don't
support templates yet.
Part of #5436 .
2025-06-26 17:14:02 +00:00
Boaz Brickner
11a75d1de1
Move function_ test files to be in function dir and remove the function_ prefix ( #5716 )
...
Follow up of
https://github.com/carbon-language/carbon-lang/pull/5645#discussion_r2153064236
and #5607 .
2025-06-24 12:53:33 +00:00
Boaz Brickner
4b8ac429a7
Replace isStruct() || isClass() with !isUnion() ( #5715 )
...
Follow up of
https://github.com/carbon-language/carbon-lang/pull/5709/files#r2162385549
Part of https://github.com/carbon-language/carbon-lang/issues/5533 .
2025-06-24 07:42:51 +00:00
Boaz Brickner
20f44e0a92
Support a C++ class as a parameter or return by value, similar to a C++ struct ( #5709 )
...
Add tests for C++ `union`.
Also fix some typos in `class` tests and remove SemIR ranges from tests
that should diagnose an error.
Part of #5533 .
2025-06-23 19:30:56 +00:00
Boaz Brickner
8aedcbcabd
Remove TODOs from the test of a declared but not defined struct as a by value parameter/return value ( #5708 )
...
These TODOs should have been removed in
https://github.com/carbon-language/carbon-lang/pull/5538 which adds
support for struct by value parameters and return values.
Part of #5533 .
2025-06-23 14:45:06 +00:00
Boaz Brickner
c025f96894
Replace EXTRA-ARGS: --no-prelude-import with INCLUDE-FILE: toolchain/testing/testdata/min_prelude/none.carbon ( #5707 )
...
Follow up https://github.com/carbon-language/carbon-lang/pull/5538 to be
consistent with the practice introduced in
https://github.com/carbon-language/carbon-lang/pull/5694 .
2025-06-23 13:57:47 +00:00
Boaz Brickner
cc698d78f5
When using a C++ struct as a parameter, map its type to a Carbon class type ( #5538 )
...
This doesn't support actually passing the value of the struct, which is
planned to be implemented using thunks.
`ClangDeclId` value is now `ClangDecl` which includes the mapped Carbon
instruction in addition to the Clang declaration. This allows finding
the Carbon instruction for a given Clang declaration, which is necessary
for mapping a Clang struct parameter type to the Carbon class without
doing name lookup. We don't take the instruction as part of the hash
key, as discussed in
[Discord](https://discord.com/channels/655572317891461132/768530752592805919/1380575881050718469 ).
To map the type, we also need to map namespaces. To avoid recursion for
inner namespaces, we use a vector.
Note that the first commit just changes the order of functions in the
file to make review easier.
C++ Interop Demo (that shows missing behavior):
```c++
// hello_world.h
struct S {
S(const S&) { x = 1; }
int x;
};
void hello_world(S s);
```
```c++
// hello_world.cpp
#include "hello_world.h"
#include <cstdio>
void hello_world2(S s) { printf("hello_world2: %d\n", s.x); }
void hello_world(S s) {
printf("hello_world: %d\n", s.x);
hello_world2(s);
}
```
```carbon
// main.carbon
library "Main";
import Cpp library "hello_world.h";
fn Run() -> i32 {
var s : Cpp.S;
Cpp.hello_world(s);
return 0;
}
```
```shell
$ clang -c hello_world.cpp
$ bazel-bin/toolchain/carbon compile main.carbon
$ bazel-bin/toolchain/carbon link hello_world.o main.o --output=demo
$ ./demo
hello_world: -1108224096
hello_world2: 1
```
Part of #5533 .
2025-06-20 16:51:58 +00:00
Boaz Brickner
8f93bb1045
Move EntityName's CarbonHashValue() to be a "hidden friend", like CarbonHashtableEq() ( #5701 )
...
Context:
https://github.com/carbon-language/carbon-lang/pull/5538/files/dfc50609a5b1184cb7970444c1edd524bf17184d#r2152818374
2025-06-20 14:00:38 +00:00
Boaz Brickner
64ad57adef
Update toolchain/check/testdata/interop/cpp/function_param_int*.carbon and toolchain/check/testdata/interop/cpp/function_return.carbon tests to use sem ir ranges ( #5645 )
...
Follow up of #5594 .
Trying to compromise SemIR size, having enough information and
complexity of tests, I've duplicated representative tests to a separate
test file with `--dump-sem-ir-ranges=if-present`.
2025-06-18 07:50:44 +00:00
Boaz Brickner
29d4aae722
Update most toolchain/check/testdata/interop/cpp tests to use sem ir ranges ( #5594 )
...
All except
`toolchain/check/testdata/interop/cpp/function_param_int*.carbon` and
`toolchain/check/testdata/interop/cpp/function_return.carbon`.
Don't output SemIR for cases that are intended to fail.
2025-06-11 12:35:55 +00:00
Boaz Brickner
e4c8150f2c
Store Clang Decls in a CanonicalValueStore ( #5638 )
...
Saves space since in most cases, we only need a `None` 32 bits index
instead of a null 64 bits pointer.
Based on discussions in [Carbon C++ Interop
weekly](https://docs.google.com/document/d/1YlxEOJ0r-o19o19TCJbFl4Ln1U88yn_Vj23y1Hr5vTk/edit?tab=t.0#heading=h.23l56bt5xwlu )
and Discord
([1](https://discord.com/channels/655572317891461132/768530752592805919/1375121180306047106 ),
[2](https://discord.com/channels/655572317891461132/768530752592805919/1380575881050718469 )).
Note: Any `clang::DeclContext` is also a `clang::Decl`.
Part of #4666 .
2025-06-10 16:55:29 +00:00
Boaz Brickner
ec97ec9664
Use InImport pointing to C++ imports in ConvertLocInFile() instead of adding a separate InCppImport when emitting ( #5614 )
...
I believe we will need to eventually add the specific C++ import
information in `LocId`.
This also seems to fix the `LanguageServerDiagnosticInWrongFile` issue
(#5604 ).
We're still not in the state we want to be according to
https://github.com/carbon-language/carbon-lang/pull/5246#issuecomment-2784301206 .
Will look into removing the location part from the `"In file included
from ..."` line.
Part of #5245 .
2025-06-10 09:02:45 +00:00
Boaz Brickner
fb33f7c481
In test_clang_cpp, if CalledProcessError is raised, log the stderr for easier debugging ( #5595 )
...
Found this is very useful for debugging since otherwise it's hard to
tell what happened in the process.
Based on similar logic in `scripts/target_determinator.py`.
2025-06-04 06:39:02 +00:00
Boaz Brickner
5435877745
Simplify the test that accesses LangOptions due to handling diagnostic with FixItHints and clarify the test if for diagnostic with FixItHints ( #5596 )
...
Followup of #5586 .
Part of #5176 .
2025-06-04 06:32:12 +00:00
Boaz Brickner
1899a6b285
Add C++ struct parameter tests with data members ( #5540 )
...
Also, add ranges.
Part of #5533 .
2025-06-03 16:53:36 +00:00
Boaz Brickner
464ee76b9b
Fix stack-use-after-scope issue by making LangOptions parameter non temporary ( #5586 )
...
The parameter is kept by reference.
Added a test that accesses `LangOptions` and crashes without this fix.
Part of #5176 .
2025-06-02 14:28:36 +00:00
Boaz Brickner
ae454bb48c
Test C++ structs as parameters and return types as declarations and definitions ( #5537 )
...
Move all function tests to a dedicated directory and all `struct`
function tests to a dedicated file in this directory.
Part of #5533 .
2025-05-30 21:52:26 +00:00
Boaz Brickner
5095af991f
Make MatchContext::WorkItem, CalleeFunction, InitRepr, ReturnTypeInfo Printable ( #5535 )
...
Found these to be useful for debugging.
2025-05-27 16:50:13 +00:00
Boaz Brickner
0a5dc9a9cc
Use C++ trailing return type in toolchain/check/testdata/interop/cpp/no_prelude/function.carbon ( #5541 )
2025-05-27 16:20:00 +00:00
Boaz Brickner
4901db832c
Deduplicate getting the function in HandleInst() for Call ( #5515 )
...
Part of #5514 .
2025-05-22 15:45:10 +00:00
Boaz Brickner
852d0191a9
Add support for importing C++ inline functions ( #5427 )
...
This requires:
* Making `FunctionDecl` mutable since generating code
(`HandleTopLevelDecl()`) requires a mutable declaration and since we
manually add `used` attribute to force code generation.
* Passing the file system to `Lower` since it's needed by Clang code
generation.
* Creating an internal Clang LLVM module and link it against the Carbon
LLVM module.
Demo:
```c++
// hello_world.h
extern int puts;
inline void hello_world() {
((int (*)(const char*))&puts)("hello world");
}
```
```carbon
// main.carbon
library "Main";
import Cpp library "hello_world.h";
fn Run() -> i32 {
Cpp.hello_world();
return 0;
}
```
```shell
$ bazel-bin/toolchain/carbon compile main.carbon
$ bazel-bin/toolchain/carbon link main.o --output=demo
$ ./demo
hello world
```
Based on https://github.com/carbon-language/carbon-lang/pull/5406 .
Part of #5405 .
2025-05-21 07:02:05 +00:00
Boaz Brickner
c39efe321e
Refactor toolchain/check/testdata/interop/cpp/no_prelude/function_decl.carbon ( #5477 )
...
Rename file and shard files to shorter name given context.
Add `todo_` to file shards where appropriate.
Add shard files section comments.
2025-05-20 16:15:49 +00:00
Boaz Brickner
8e666cea3e
Add section descriptions in toolchain/check/testdata/interop/cpp/no_prelude/cpp_diagnostics.carbon as agreed in #5467 ( #5497 )
...
Part of #4666 .
2025-05-19 21:39:57 +00:00
Boaz Brickner
04d2643fd7
Reformat banner comments in toolchain/check/testdata/interop/cpp/function_param_*.carbon , following the agreed format in #5467 ( #5498 )
...
I've decided to keep `int` and similar as low case, as it refers to an
actual type.
Part of #5436
2025-05-19 21:17:23 +00:00
Boaz Brickner
804613f6b7
Fix speicifc typo ( #5499 )
2025-05-19 14:57:13 +00:00
Boaz Brickner
6842c51fd1
Rename toolchain/check/testdata/interop/cpp/no_prelude/function_decl_inline.carbon and its shard files to simpler names ( #5476 )
...
Part of #5405 .
2025-05-18 23:30:47 +00:00
Boaz Brickner
42f4d20e36
Improve toolchain/check/testdata/interop/cpp/no_prelude/namespace.carbon ( #5478 )
...
* Add // === headlines on group of file shards.
* Rename shard files to shorten given the file context them and add
`todo` where appropriate and group them together..
2025-05-17 15:17:41 +00:00
Boaz Brickner
4071372677
Improve toolchain/check/testdata/interop/cpp/no_prelude/{class,struct,union}.carbon ( #5467 )
...
* Add // === headlines on group of file shards.
* Rename shard files to shorten given the file context them and add
`todo` where appropriate.
Not splitting this file for now.
Part of #5150 .
2025-05-15 09:01:18 +00:00
Boaz Brickner
2b48033da7
Refactor toolchain/check/testdata/interop/cpp/function_decl.carbon ( #5457 )
...
* Split to 4 files: `function_param_int16`, `function_param_int32`,
`function_param_unsupported.carbon`, `function_return`.
* Add // === headlines on group of file shards.
* Rename shard files to shorten them and make them more consistent given
the file context.
* Deduplicate identical .h files and group their tests.
Potential future improvements:
* Split further. For example, pointers and references might be somewhat
separate from int primitives.
* Remove `import_` shard file prefix, as it repeats itself, but leaving
for now as it makes it more explicit.
Part of #5263
2025-05-14 16:00:27 +00:00
Boaz Brickner
2cb0df42e0
Add C++ interop inline function tests ( #5406 )
...
This shows that `inline` is ignored and the function definition is not
generated.
Demo:
```c++
// hello_world.h
inline void hello_world() {}
```
```carbon
// main.carbon
library "Main";
import Cpp library "hello_world.h";
fn Run() -> i32 {
Cpp.hello_world();
return 0;
}
```
```shell
$ bazel-bin/toolchain/carbon compile main.carbon
$ bazel-bin/toolchain/carbon link main.o --output=demo
ld.lld: error: undefined symbol: hello_world()
>>> referenced by main.carbon:8
>>> main.o:(main)
error: linker command failed with exit code 1 (use -v to see invocation)
```
Part of #5405 .
2025-05-09 16:33:16 +00:00
Boaz Brickner
2aa5fbfa4a
Move the logic in TryConvertClangDiagnosticLoc() to ConvertLocInFile() ( #5391 )
...
Make `AbsoluteNodeId` support Clang source locations.
Part of #5245 .
2025-05-02 08:08:11 +00:00
Boaz Brickner
8ba3da9730
Fix "is is" typo ( #5382 )
2025-04-29 15:32:17 +00:00
Boaz Brickner
84384cf126
Remove exceptions for performance-enum-size ( #5370 )
...
This is a followup of `performance-enum-size` disablement in
https://github.com/carbon-language/carbon-lang/pull/5368 .
2025-04-28 15:45:54 +00:00
Boaz Brickner
0647d0e045
Properly link to the discussion in the TODO ( #5369 )
...
Followup of https://github.com/carbon-language/carbon-lang/pull/5262 .
Part of #5245 .
2025-04-28 14:25:45 +00:00
Boaz Brickner
63b14ee245
Disable clang-tidy performance-enum-size ( #5368 )
...
See discussion in
https://github.com/carbon-language/carbon-lang/pull/5352 and
https://discord.com/channels/655572317891461132/655578254970716160/1365319438198378577
2025-04-28 12:16:19 +00:00
Boaz Brickner
d2826ae841
Disable clang-tidy modernize-use-ranges ( #5359 )
...
See discussion in
https://github.com/carbon-language/carbon-lang/pull/5353
2025-04-25 13:31:08 +00:00
Boaz Brickner
609ccefd18
Introduce a Clang diagnostic instruction and use it to point to C++ source locations on Clang errors and warnings ( #5262 )
...
Introduce `ImportIRId::Cpp` and refer to clang source location in its
`ImportIRInst`.
Part of #5245 .
2025-04-25 13:05:45 +00:00
Boaz Brickner
23d92c05ca
Fix clang-tidy: move assignment operators should be marked noexcept [performance-noexcept-move-constructor,-warnings-as-errors] ( #5266 )
2025-04-24 15:37:24 +00:00
Boaz Brickner
68111a994c
C++ Interop: Basic C++ record (class/struct/union) import support ( #5156 )
...
Focus: Calling static C++ function defined in C++ classes.
Limitations:
* Ignores visibility (public / protected / private).
* No support for: dynamic classes, member methods, data members,
declarations without definitions, importing inheritance.
Based on #5142 .
C++ Interop Demo with a class:
```c++
// hello_world.h
namespace some_namespace {
class MyClass {
public:
static void hello_world();
};
} // namespace some_namespace
```
```c++
// hello_world.cpp
#include "hello_world.h"
#include <cstdio>
namespace some_namespace {
void MyClass::hello_world() { printf("Hello World!\n"); }
} // namespace some_namespace
```
```carbon
// main.carbon
library "Main";
import Cpp library "hello_world.h";
fn Run() -> i32 {
Cpp.some_namespace.MyClass.hello_world();
return 0;
}
```
```shell
$ clang -c hello_world.cpp
$ bazel-bin/toolchain/carbon compile main.carbon
$ bazel-bin/toolchain/carbon link hello_world.o main.o --output=demo
$ ./demo
Hello World!
```
Part of #5150 .
2025-04-10 08:15:12 +00:00
Boaz Brickner
817dacfc77
Fix clang-tidy: function 'rewind' has no error detection; 'fseek' should be used instead [bugprone-unsafe-functions,-warnings-as-errors] ( #5265 )
2025-04-09 13:17:26 +00:00
Boaz Brickner
7d2029e8ca
Fix clang-tidy: result of a data() call may not be null terminated, provide size information to the callee to prevent potential issues [bugprone-suspicious-stringview-data-usage,-warnings-as-errors] ( #5267 )
...
Replaced using `llvm::StringRef` with `const std::string&` so we can
have `c_str()`.
2025-04-08 15:39:55 +00:00
Boaz Brickner
afa29d5e66
Fix clang-tidy: use a ranges version of this algorithm [modernize-use-ranges,-warnings-as-errors] ( #5268 )
2025-04-08 15:37:52 +00:00
Boaz Brickner
61c705311f
Remove unused variable diagnostics_stream ( #5239 )
2025-04-04 00:33:14 +00:00
Boaz Brickner
cfdd5fbdb5
Simplify GetAbsoluteNodeIdImpl() by merging while (true) with the first if ( #5240 )
2025-04-03 22:44:34 +00:00
Boaz Brickner
feb78e778d
Change OutputMapping::Map::io_ from reference to pointer ( #5227 )
...
Per [the style
guide](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/cpp_style_guide.md#syntax-and-formatting ):
* If it is captured and must outlive the call expression itself, use a
pointer and document that it must not be null (unless it is also
optional).
* When storing an object's address as a non-owned member, prefer storing
a pointer.
2025-04-02 17:54:13 +00:00
Boaz Brickner
50833a9c3b
Change Lexer::ErrorRecoveryBuffer::buffer_ from reference to pointer ( #5228 )
...
Per [the style
guide](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/cpp_style_guide.md#syntax-and-formatting ):
* If it is captured and must outlive the call expression itself, use a
pointer and document that it must not be null (unless it is also
optional).
* When storing an object's address as a non-owned member, prefer storing
a pointer.
2025-04-01 16:04:19 +00:00
Boaz Brickner
ccd2cb346a
Change CodeGen::Make() to take module and errors as pointers and not references ( #5229 )
...
Per [the style
guide](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/cpp_style_guide.md#syntax-and-formatting ):
* If it is captured and must outlive the call expression itself, use a
pointer and document that it must not be null (unless it is also
optional).
* When storing an object's address as a non-owned member, prefer storing
a pointer.
2025-04-01 14:55:04 +00:00
Boaz Brickner
6e2dbb5b61
Change CopyOnWriteBlock::file_ from reference to pointer ( #5230 )
...
Per [the style
guide](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/cpp_style_guide.md#syntax-and-formatting ):
* If it is captured and must outlive the call expression itself, use a
pointer and document that it must not be null (unless it is also
optional).
* When storing an object's address as a non-owned member, prefer storing
a pointer.
2025-04-01 14:40:28 +00:00
Boaz Brickner
97b234358e
Change ImportContext.context_ from reference to pointer ( #5207 )
...
Per [the style
guide](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/cpp_style_guide.md#syntax-and-formatting ):
* If it is captured and must outlive the call expression itself, use a
pointer and document that it must not be null (unless it is also
optional).
* When storing an object's address as a non-owned member, prefer storing
a pointer.
2025-03-31 07:11:08 +00:00
Boaz Brickner
ac3bf0d3fa
Change TypeStructureBuilder.context_ from reference to pointer ( #5211 )
...
Per [the style
guide](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/cpp_style_guide.md#syntax-and-formatting ):
* If it is captured and must outlive the call expression itself, use a
pointer and document that it must not be null (unless it is also
optional).
* When storing an object's address as a non-owned member, prefer storing
a pointer.
2025-03-28 16:24:23 +00:00
Boaz Brickner
3acca8402f
Change NodeIdTraversal.context_ from reference to pointer ( #5210 )
...
Per [the style
guide](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/cpp_style_guide.md#syntax-and-formatting ):
* If it is captured and must outlive the call expression itself, use a
pointer and document that it must not be null (unless it is also
optional).
* When storing an object's address as a non-owned member, prefer storing
a pointer.
2025-03-28 16:24:17 +00:00
Boaz Brickner
9d3664baa9
Change PendingBlock.context_ from reference to pointer ( #5209 )
...
Per [the style
guide](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/cpp_style_guide.md#syntax-and-formatting ):
* If it is captured and must outlive the call expression itself, use a
pointer and document that it must not be null (unless it is also
optional).
* When storing an object's address as a non-owned member, prefer storing
a pointer.
2025-03-28 16:24:11 +00:00
Boaz Brickner
bd24d74975
Change SubstConstantCallbacks.context_ from reference to pointer ( #5208 )
...
Per [the style
guide](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/cpp_style_guide.md#syntax-and-formatting ):
* If it is captured and must outlive the call expression itself, use a
pointer and document that it must not be null (unless it is also
optional).
* When storing an object's address as a non-owned member, prefer storing
a pointer.
2025-03-28 16:24:03 +00:00
Boaz Brickner
15bb7d5ac6
Change DeductionWorklist.context_ from reference to pointer ( #5204 )
...
Per [the style
guide](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/cpp_style_guide.md#syntax-and-formatting ):
* If it is captured and must outlive the call expression itself, use a
pointer and document that it must not be null (unless it is also
optional).
* When storing an object's address as a non-owned member, prefer storing
a pointer.
2025-03-28 15:29:09 +00:00
Boaz Brickner
624ebbd805
Change TypeCompleter.context_ from reference to pointer ( #5206 )
...
Per [the style
guide](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/cpp_style_guide.md#syntax-and-formatting ):
* If it is captured and must outlive the call expression itself, use a
pointer and document that it must not be null (unless it is also
optional).
* When storing an object's address as a non-owned member, prefer storing
a pointer.
2025-03-28 15:29:02 +00:00
Boaz Brickner
afe034f9f4
Change RebuildGenericConstantInEvalBlockCallbacks.context_ from reference to pointer ( #5205 )
...
Per [the style
guide](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/cpp_style_guide.md#syntax-and-formatting ):
* If it is captured and must outlive the call expression itself, use a
pointer and document that it must not be null (unless it is also
optional).
* When storing an object's address as a non-owned member, prefer storing
a pointer.
2025-03-28 15:14:02 +00:00
Boaz Brickner
181c7b9290
Change EvalContext.context_ from reference to pointer ( #5203 )
...
Per [the style
guide](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/cpp_style_guide.md#syntax-and-formatting ):
* If it is captured and must outlive the call expression itself, use a
pointer and document that it must not be null (unless it is also
optional).
* When storing an object's address as a non-owned member, prefer storing
a pointer.
2025-03-28 14:48:18 +00:00
Boaz Brickner
e7e52a14ba
When compiling C++, output a diagnostic per C++ diagnostic ( #5177 )
...
Part of #5176 .
2025-03-28 08:46:17 +00:00
Boaz Brickner
44bcceba86
When import C++ names, add the note on the ClangLookup() as well ( #5142 )
...
Currently has no effect because we can't do lookup in classes, yet.
Part of #4666 .
2025-03-24 17:27:20 +00:00
Boaz Brickner
3a5e18b1a1
In C++ interop, stop referring to the diagnostics consumer before deleting it ( #5149 )
...
Part of #4666
2025-03-20 18:37:25 +00:00
Boaz Brickner
caaeabce09
Update comment following a rename ( #5139 )
...
Rename:
https://github.com/carbon-language/carbon-lang/pull/4100/commits/6da9a9ee19eec002878a0d59a1af00d24f515d9a
2025-03-18 15:19:22 +00:00
Boaz Brickner
fcd38a4d7f
Add support for importing C++ namespaces ( #5103 )
...
This adds support for having different C++ `NameScope`s (and not just
the main `Cpp` scope), and we keep a pointer to `clang::DeclContext`
these scopes so we can look up C++ names in the right part of the AST.
C++ Interop Demo with a namespace:
```c++
// hello_world.h
namespace some_namespace {
void hello_world();
} // namespace some_namespace
```
```c++
// hello_world.cpp
#include <cstdio>
namespace some_namespace {
void hello_world() { printf("Hello World!\n"); }
} // namespace some_namespace
```
```carbon
// main.carbon
library "Main";
import Cpp library "hello_world.h";
fn Run() -> i32 {
Cpp.some_namespace.hello_world();
return 0;
}
```
```shell
$ clang -c hello_world.cpp
$ bazel-bin/toolchain/carbon compile main.carbon
$ bazel-bin/toolchain/carbon link hello_world.o main.o --output=demo
$ ./demo
Hello World!
```
Closes #5102
2025-03-15 20:41:48 +00:00
Boaz Brickner
a4a229b637
Initialize cpp_mangle_context_ in Mangler's constructor ( #5095 )
...
This is a followup of [a
comment](https://github.com/carbon-language/carbon-lang/pull/5062/files/89e56d51858bcc18d4242d4e5c9ee0e7496d887e#r1979993815 )
in #5062 .
Add a mutable AST pointer to `FileContext`.
This is necessary since we use [Clang with lack of const
correctness](https://github.com/llvm/llvm-project/pull/130096#issuecomment-2704413782 ).
Alternatives in Clang:
* Change `ASTUnit::getASTContext() const` to return a non-const
`ASTContext`. [Tried and was rejected upstream due to weakening const
correctness](https://github.com/llvm/llvm-project/pull/130096 ).
* Change `createMangleContext()` to be `const`. Tried that and it seems
like it relies heavily on non const API.
* Change `MangleContext::mangleName()` to `const`. Tried that but there
are several lazy initialization and id creations happening that modify
the context. See details in
https://github.com/llvm/llvm-project/pull/130613 .
Alternatives in Carbon:
* Use `const_cast` on `ASTContext` when calling `createMangleContext()`.
* Make `FileContext::sem_ir_` point to a mutable `SemIR::File`.
* Change `File::cpp_ast()` to be const while keeping it return a mutable
pointer.
Part of #4666 .
2025-03-12 18:49:43 +00:00
Boaz Brickner
201a4dc1c6
Move the test for unsupported C++ interop decl type out of function_decl.carbon ( #5104 )
...
It's not specific for functions
Part of #4666
2025-03-11 17:47:41 +00:00
Boaz Brickner
156ab889f8
Support mangling imported C++ functions using Clang's MangleContext ( #5062 )
...
Keep a pointer to the Clang declaration in Carbon's function declaration
and use it in Carbon mangling by calling Clang mangling.
Create Clang's `MangleContext` once on demand.
Part of #4666 .
C++ Interop Demo:
```c++
// hello_world.h
void hello_world();
```
```c++
// hello_world.cpp
#include <cstdio>
void hello_world() { printf("Hello World!\n"); }
```
```
// main.carbon
library "Main";
import Cpp library "hello_world.h";
fn Run() -> i32 {
Cpp.hello_world();
return 0;
}
```
```shell
$ clang -c hello_world.cpp
$ bazel-bin/toolchain/carbon compile main.carbon
$ bazel-bin/toolchain/carbon link hello_world.o main.o --output=demo
$ ./demo
Hello World!
```
2025-03-06 09:51:01 +00:00
Boaz Brickner
d25140e8b9
Add a test that should catch checking poisoned names when extending a class ( #5071 )
...
See
https://discord.com/channels/655572317891461132/655578254970716160/1346727881912619081
Part of #4622 .
2025-03-06 08:45:23 +00:00
Boaz Brickner
28de6c9b7d
Add --no-dump-sem-ir to all name_poisoning tests ( #5053 )
...
Follow up of previous PR discussions
([#4950 ](https://github.com/carbon-language/carbon-lang/pull/4950/files/89c2e66dc3190159e2f8d94c31bff31bdd0d81a1..a1650a7d73c8f4013b4b77e4d7d60933f1f6d676#r1972257889 ),
[#4987 ](https://github.com/carbon-language/carbon-lang/pull/4987/files#r1964105413 )).
Part of #4622 .
2025-03-03 19:25:55 +00:00
Boaz Brickner
87b9cab7b1
Add support for importing a trivial global C++ function ( #5033 )
...
ASTUnit is owned by `CompileSubcommand`, passed through `Unit` to be
populated in `ImportCppFiles()` and used via `SemIR::File`.
When generating the AST, pass `-x c++` args to compile C++ (temporary
until we pass args properly).
`Cpp` namespace is marked as a special namespace and has dedicated logic
in `LookupNameInExactScope()`.
The logic for importing declarations from C++ to Carbon is in
`import_cpp.cpp`, but we're likely to want to refactor this
signfiicantly over time as it grows (perhaps a dedicated directory?).
Part of #4666 .
2025-03-03 10:38:19 +00:00
Boaz Brickner
fc5dcfe957
Add a test for the case that impl function is poisoned ( #4950 )
...
This adds missing coverage.
Part of #4622 .
2025-02-28 21:10:37 +00:00
Boaz Brickner
43b9969058
Split impl/no_prelude/name_poisoning.carbon to interface/no_prelude/name_poisoning.carbon and move interface tests there ( #5031 )
...
See
https://github.com/carbon-language/carbon-lang/pull/4950#discussion_r1972252460 .
Part of #4622 .
2025-02-28 08:06:13 +00:00
Boaz Brickner
80e1a6ef61
Avoid copying NameScope and only allow moving it ( #5032 )
...
This class is not intended to be copied.
Part of #4622 .
2025-02-27 22:46:46 +00:00
Boaz Brickner
3573763def
Use Generics in no_poison test instead of pointers. ( #5011 )
...
Followup of [#4987
comment](https://github.com/carbon-language/carbon-lang/pull/4987/files/b015f99d0e86f5dfe3b1709bec8a426a584f7804#r1964119857 ).
Part of #4622 .
2025-02-26 17:41:11 +00:00
Boaz Brickner
5b67bb8981
Refactor name poisoning tests to be more organized, complete and consistent ( #4987 )
...
This is also following
https://github.com/carbon-language/carbon-lang/pull/4900#discussion_r1945606053 ,
which points that name poisoning tests are not in the correct place.
Part of #4622 .
2025-02-24 08:59:36 +00:00
Boaz Brickner
6a99c4e970
When diagnosing a duplicated name, add the name to the diagnosis ( #4902 )
...
In order to have the name available for diagnostics, we now always set
`NameId` in `NameContext` and put `poisoning_loc_id` as part of the
union with `resolved_inst_id` instead (since we never need both).
2025-02-19 07:19:23 +00:00
Boaz Brickner
4a93b6667e
Add the used name to the NameUseBeforeDecl diagnostic ( #4901 )
...
Part of #4622 .
2025-02-14 21:54:35 +00:00
Boaz Brickner
d65f0d959d
Add a test for the case that extend poisons a class member ( #4960 )
...
Part of #4622 .
2025-02-14 20:31:58 +00:00
Boaz Brickner
dd7c64bad0
When diagnosing a duplicate name, point to the name instead of the instruction ( #4953 )
...
Left TODOs where more work is necessary before this change can be
applied or its impact can be verified.
Includes #4952 , to avoid regression in some cases.
Similar to #4938 . See
https://discord.com/channels/655572317891461132/655578254970716160/1339007384361762857 .
2025-02-14 19:21:50 +00:00
Boaz Brickner
986a2a064c
Add poisoned names to the format ( #4961 )
...
Part of #4622 .
2025-02-14 19:18:51 +00:00
Boaz Brickner
809bcf10ed
Fix bad merge of comments introduced in #4884 ( #4955 )
...
Part of #4622 .
2025-02-14 00:34:48 +00:00
Boaz Brickner
23e5677c8e
Avoid poisoning non identifier names ( #4884 )
...
There are different use cases where we call
`Context::LookupQualifiedName()` on non identifiers, like
`NameId::SelfType`, which implicitly triggers poisoning these names. I
don't think poisoning non identifiers like`Self` is ever useful.
Use cases where `Self` is being poisoned:
* Checking allowed access:
https://github.com/carbon-language/carbon-lang/blob/e257051612e4217295e206fd3274fc75e22d206a/toolchain/check/member_access.cpp#L62 ,
for example, in
https://github.com/carbon-language/carbon-lang/blob/e257051612e4217295e206fd3274fc75e22d206a/toolchain/check/testdata/alias/no_prelude/fail_aliased_name_in_diag.carbon#L21
* Using the type `Self` as a parameter type:
https://github.com/carbon-language/carbon-lang/blob/e257051612e4217295e206fd3274fc75e22d206a/core/prelude/operators/arithmetic.carbon#L78
Part of #4622 .
2025-02-13 20:00:54 +00:00
Boaz Brickner
1aa6573d4e
When diagnosing poisoned name, point to the declared name instead of the entire declaration ( #4938 )
...
See
https://discord.com/channels/655572317891461132/655578254970716160/1339007384361762857 .
Part of #4622 .
2025-02-12 20:59:55 +00:00
Boaz Brickner
3f599c250b
Generate Cpp namespace when import Cpp is used ( #4873 )
...
Also defined a dedicated `ImportCppDecl` `InstKind`.
Part of #4666
2025-02-12 01:44:37 +00:00
Boaz Brickner
e1c9cef7ec
Add a test showing how Core can be implicitly poisoned ( #4900 )
...
Alternatively, if poisoning `Core` is considered a bug, we could avoid
poisoning it (see #4903 ).
Part of #4622 .
2025-02-10 18:28:40 +00:00
Boaz Brickner
c67920e631
When diagnosing name used before declared, set the location of the usage ( #4860 )
...
Done by adding a poisoning location for each poisoned name.
Part of #4622 .
2025-02-03 20:01:09 +00:00
Boaz Brickner
65180a77c3
Use designated initializers in import.cpp ( #4883 )
...
From
https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/cpp_style_guide.md#syntax-and-formatting :
"Use designated initializers (`{.a = 1}`) when possible for structs".
2025-02-03 15:41:28 +00:00
Boaz Brickner
7c01bb4e5c
Return the NameId for a new instruction regardless if it's unresolved because it's poisoned or not ( #4861 )
...
i.e. Support `Poisoned` in `NameContext::name_id_for_new_inst()`.
Part of #4622 .
2025-01-29 14:20:05 +00:00
Boaz Brickner
f4e19f4390
Change DeclNameStack::LookupOrAddName() to return SemIR::ScopeLookupResult instead of a pair ( #4852 )
...
This makes the lookup API more consistent and would make it easier to
add poisoning location.
Part of #4622 .
2025-01-28 21:36:20 +00:00