Commit Graph
24 Commits
Author SHA1 Message Date
Richard Smith 7d9340880e Separate ClassType from ClassDeclaration. (#3329)
Retain the `ClassDeclaration` node to represent a syntactic declaration
of a class (including possibly a declaration of a generic class), but
use a separate SemIR node to represent the class type itself. This
allows us to give the two separate treatment.

The `ClassDeclaration` is still entered into the name lookup table for
its enclosing scope, but when it is named in an expression, the class
type is produced instead. When the class declaration is named in a
declaration name, it can be used to define members of the class, but an
expression that resolves to the class type cannot be used to define
members of the class.

In order to distinguish these cases, use `Name` rather than
`NameExpression` for the left-hand side of a `QualifiedName` parse node.
This removes the only use of the `Expression` form of a declaration
name, so that is also removed.

In the future, `ClassType` will also be used to describe types such as
`Vector(T)`, for which there is no corresponding `ClassDeclaration`.
2023-10-24 01:26:44 +00:00
Richard SmithandJon Ross-Perkins b01dfb3f93 Support for class definitions with static member functions. (#3305)
This includes being able to define a class that was previously
forward-declared, and being able to define a member function out-of-line
that was previously declared inside a class.

No support for fields or methods yet, and a class definition doesn't yet
cause the class to be treated as a complete type.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2023-10-18 19:49:45 +00:00
Richard Smith 69353ed271 Basic support for incomplete types. (#3302)
Incomplete types may be nested within other types; for example, a tuple
type might have an incomplete type as an element. Handle such cases by
walking through nested incomplete types when completing a type. This is
done non-recursively in case a very complex type is formed.

Types are generally no longer completed at the point where they're
formed. Instead, we attempt to complete a type when it is used in a
context that requires a complete type, and diagnose if the type cannot
be completed at that point. This will be necessary for classes, which
can become complete after their first use, and helps tease out bugs
where a type completeness check is missing.
2023-10-17 19:30:51 +00:00
Richard SmithandChandler Carruth e4caf7d604 Compute and cache the value representation of a type when it becomes complete. (#3271)
Using the computed value representation, fix lowering of struct and
tuple values to use the value representation rather than the object
representation. Fixes an issue found in the review of #3257.

This currently causes us to compute value representations of all types
as they are created, which generates substantially more SemIR to
represent types. We can get some of that back by deferring computation
of the value representation until the type is required to be complete,
but some of the additional cost here will persist with this approach.

I also considered making the computation of the value representation
type be something that lives entirely within the lowering phase, but I
think that's not the right approach in the longer term, because the
value representation will be semantically visible and relevant once we
start allowing it to be customized.

We should consider moving the nodes that exist to compute canonical
non-local types, including value representations, out into a separate
global block. That will clean up the SemIR representation substantially,
and make the SemIR produced for a function not depend on which types we
happen to have encountered beforehand. But that's not being done in this
PR.

---------

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2023-10-13 22:27:02 +00:00
Richard Smith 1ae5fe0cd5 Provide the callee expression to the Call node. (#3291)
Track the callee expression in full, instead of only tracking the
callee's FunctionId. This results in the `name_reference` denoting the
function actually being used.

Lowering now propagates a `llvm::Function*` as the value associated with
expressions of type `<function>`.

We were not creating `NameReference` node for names produced by member
access into a namespace, such as the second name in
`Namespace.Function`, which caused lowering of calls to such names to
fail. This is now fixed, but the resulting `NameReference` node only
refers to the name and the lookup result, not to the `Namespace.`
qualifier. We'll need to decide how to fit a third operand into that
node (perhaps we can stop storing the `name_id`, since it can be derived
from the lookup result) but for now the qualifier is not tracked.
2023-10-13 22:06:23 +00:00
josh11bandRichard Smith 8d0831f431 Made function and namespace nodes typed to remove a crash (#3285)
Bug found by fuzzing. Problem was untyped SemIR nodes had an invalid
type id, which was retrieved by `HandlePrefixOperator` and then passed
to `context.GetUnqualifiedType`, ultimately performing an invalid access
in `semantics_ir_->GetNode`.

We prefer to make a placeholder type for functions and namespaces to
remove the need for checking for the untyped case everywhere. Eventually
functions will have their own types, but this approach will be needed
for namespaces (and perhaps other non-first-class entities like unbound
methods and interface members) long term.

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2023-10-12 21:14:16 +00:00
Richard Smith 5ccee62918 Add name_reference SemIR node for references to names. (#3260)
Also add `name_reference_untyped` for references to non-first-class
names without types, which currently covers namespaces and functions.

This improves the fidelity of the SemIR representation, and fixes some
issues where we would use the wrong location for nodes and diagnostics
downstream of a name reference.

We're still missing a representation for dotted name expressions, such
as `Namespace.Function`, and we don't use the `untyped` node as an
operand of any other node yet.
2023-10-05 01:15:53 +00:00
Richard Smith e15b24e77d Use the name of a function as the name of its file-scope value. (#3262)
As requested in #3260.
2023-10-04 23:50:22 +00:00
Richard Smith 1ac7002bf6 Don't try to infer where to put loads in lowering. (#3146)
Trust semantics to have put them in the right places.

Many parts of lowering still need to be updated to use the value
representation chosen at the semantics layer, but this is an incremental
step towards that.
2023-10-04 19:57:12 +00:00
Richard Smith fea6cf88fc Unify initialization and conversion logic (#3255)
Combine the initialization, implicit conversion, and value category
conversion functions into a single function.

This substantially reduces the duplication between these steps, and
ensures that we support the same set of conversions in all these
contexts. This also fixes some issues where we would not use the proper
value representation for tuples and structs after performing implicit
conversions.
2023-10-02 21:53:52 +00:00
Geoff Romer 7899154a21 Add "ERROR" to all error diagnostics (#3251)
This makes the difference between errors and lower-level diagnostics
visible to users, and aligns the toolchain's behavior with the
expectations in `driver_fuzzer.cpp`.
2023-09-26 16:52:49 +00:00
Richard Smith 491fa1bdd5 Place the computation of the destination of an initialization into the return slot. (#3252)
Fix a bug where we would perform the computation of the return location
in SemIR after we have already used it in some cases, leading to
assertion failures during lowering. Instead, accumulate a sequence of
instructions to compute the return location in a temporary block, and
overwrite the return slot with those instructions when we perform
initialization.

StubReference is replaced by a more general SpliceBlock node, that takes
a code block and a result value, executes the instructions in the block,
and produces the result. This is used in the uncommon case where more
than one instruction is required to compute the return slot, which can
happen if we need to first emit a temporary and then index into it, or
if we need to perform multiple levels of indexing before we reach an
entity to initialize.
2023-09-26 01:31:24 +00:00
Richard Smith 31d55da403 Stop creating fake integer literals as indexes for tuple initialization. (#3248)
This makes struct and tuple initialization basically identical; unify
them.
2023-09-21 00:41:19 +00:00
Richard Smith f389844893 Remove nearly all uses of StubReferences. (#3249)
The speculative insertion of StubReferences after elements in an
argument list turned out to not be necessary, because we decided we want
to insert per-argument initialization steps after all arguments are
evaluated, rather than interleaving them. The StubReferences we insert
are causing some minor code complexity, so remove them.

We still create StubReferences when performing patch-ups of
already-emitted code, but we no longer ever need to look through them
when determining whether an initializer was a literal or when evaluating
a type expression.
2023-09-21 00:00:36 +00:00
Richard Smith 842b471e67 Perform in-place initialization for tuples and structs (#3246)
This implements initializing expression semantics for structs and
tuples, following #2006 and discussions since.

Tuple and (and analogously, struct) literals are treated as having a
mixed expression category that is later resolved based on how the
literal is used, as either a tuple initializer or a tuple value, at
which point we create a `TupleInit` or `TupleValue` that represents the
formation of the tuple initializer or tuple value from the tuple
literal.

There's quite a lot of TODOs here, and the SemIR representation is still
not quite right, but this seems like a good place to checkpoint some
incremental progress.
2023-09-20 21:20:14 +00:00
Richard Smith e81d171226 Switch to modeling array initialization analogously to a function call. (#3205)
Instead of modeling array initialization as a thin wrapper around tuple
initialization, handle it like a function call, with a return slot as
part of its input. This better matches how initialization via a call to
`ImplicitAs::Convert` will eventually work, and in particular lets us do
in-place initialization of arrays rather than always creating a
temporary.
2023-09-14 00:49:55 +00:00
Jon Ross-Perkins 9ae41b2965 Update SemIR yaml and text for multi-file. (#3217)
Building on #3214 and #3215, updates sem_ir yaml to be:

```
- filename: name
  sem_ir: [ ... ]
```

Also, changes the textual format from `package { ... }` to `file
<filename> { ... }`. My thought on packages there is:

```
file "foo.carbon" {
  package MyPackage

  ...
}
```

The reason for putting the file first is that it's easier if we put what
we're grouping on first, whereas the package is an "annotation" on the
file.
2023-09-13 17:08:18 +00:00
Richard Smith 7d0f7b5e8f Rename {Tuple,Struct}Value -> {Tuple,Struct}Literal. (#3213)
This better reflects the purpose of these semantics nodes, and prepares
for adding TupleValue and TupleInit nodes to represent forming values
and initializers from literals.
2023-09-12 21:48:15 +00:00
Richard Smith 71e9fb7511 Disable raw IR printing in tests by default. (#3204)
Add a couple more tests to check/testdata/basic/ to explicitly test the
raw dump.
2023-09-07 21:11:44 +00:00
Jon Ross-Perkins 87d4c2dfc6 Improve testing and handling of unsigned APInt values (#3202) 2023-09-07 18:53:38 +00:00
Richard Smith 80e307e2ce Fix printing of integer literals with the high bit set. (#3201)
Integer literals are not signed, so don't print them as signed.
2023-09-06 22:33:54 +00:00
Richard Smith 76576c1387 Fix rejection of arrays with eight or nine elements. (#3199)
In general, LLVM's parsing of decimal integers to `APInt`s forms an
`APInt` that is 4n bits wide, where n is the length of the integer, and
the `isNegative` check only checks the high bit.

In this case, we form an `APInt` that is four bits wide, with the high
bit set, which we reject because we think it's "negative". These two
array lengths are the only ones where this happens -- if the decimal
integer value is two characters long, we form an `APInt` that is eight
bits wide but holds a value < 100, so the high bit is never set, and the
same applies for longer integers too.

An `IntegerLiteral` is never negative, so we don't need the `isNegative`
check, and in fact it only detects the n=8 and n=9 cases.
2023-09-06 21:32:17 +00:00
Geoff Romer 6f02efe1d3 Add overflow check for array size (#3194)
Closes #3193
2023-09-06 18:51:54 +00:00
Jon Ross-Perkins 1c748c0f14 Split semantics into check and sem_ir directories (#3176)
Continuing along with #3070. Note this is just a file rename, with BUILD
edits; every file previously in semantics/ should show as moved (except
maybe BUILDs, which split).
2023-08-31 19:54:32 +00:00