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`.
This commit is contained in:
Richard Smith
2023-10-24 01:26:44 +00:00
committed by GitHub
parent ce248239d4
commit 7d9340880e
29 changed files with 186 additions and 105 deletions
@@ -17,10 +17,7 @@ static auto HandleDeclarationNameAndParams(Context& context, State after_name)
context.PushState(state);
if (context.PositionIs(Lex::TokenKind::Period)) {
// Because there's a qualifier, we process the first segment as an
// expression for simplicity. This just means semantics has one less thing
// to handle here.
context.AddLeafNode(NodeKind::NameExpression, *identifier);
context.AddLeafNode(NodeKind::Name, *identifier);
state.state = State::PeriodAsDeclaration;
context.PushState(state);
} else {
@@ -71,6 +68,15 @@ static auto HandleDeclarationNameAndParamsAfterName(Context& context,
return;
}
// TODO: We can have a parameter list after a name qualifier, regardless of
// whether the entity itself permits or requires parameters:
//
// fn Class(T:! type).AnotherClass(U:! type).Function(v: T) {}
//
// We should retain a `DeclarationNameAndParams...` state on the stack in all
// cases below to check for a period after a parameter list, which indicates
// that we've not finished parsing the declaration name.
if (params == Params::None) {
return;
}