Skip to content

Commit

Permalink
Addressing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jonmeow committed Mar 1, 2025
1 parent 67dc09f commit 6c84ea3
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 25 deletions.
9 changes: 8 additions & 1 deletion toolchain/check/handle_name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ auto HandleParseNode(Context& context,
}

auto HandleParseNode(Context& context,
Parse::NameQualifierWithoutParamsId /*node_id*/) -> bool {
Parse::IdentifierNameQualifierWithoutParamsId /*node_id*/)
-> bool {
context.decl_name_stack().ApplyNameQualifier(PopNameComponent(context));
return true;
}
Expand Down Expand Up @@ -239,6 +240,12 @@ auto HandleParseNode(Context& context, Parse::DesignatorExprId node_id)
return true;
}

auto HandleParseNode(Context& context,
Parse::KeywordNameQualifierWithoutParamsId node_id)
-> bool {
return context.TODO(node_id, "KeywordNameQualifierWithoutParamsId");
}

auto HandleParseNode(Context& context, Parse::PackageExprId node_id) -> bool {
AddInstAndPush<SemIR::NameRef>(
context, node_id,
Expand Down
3 changes: 2 additions & 1 deletion toolchain/check/node_stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -469,11 +469,13 @@ class NodeStack {
case Parse::NodeKind::ForHeader:
case Parse::NodeKind::ForHeaderStart:
case Parse::NodeKind::ForIn:
case Parse::NodeKind::IdentifierNameQualifierWithoutParams:
case Parse::NodeKind::IdentifierPackageName:
case Parse::NodeKind::IfConditionStart:
case Parse::NodeKind::ImportIntroducer:
case Parse::NodeKind::IndexExprStart:
case Parse::NodeKind::InvalidParseStart:
case Parse::NodeKind::KeywordNameQualifierWithoutParams:
case Parse::NodeKind::LibraryIntroducer:
case Parse::NodeKind::LibrarySpecifier:
case Parse::NodeKind::MatchCase:
Expand All @@ -494,7 +496,6 @@ class NodeStack {
case Parse::NodeKind::NamedConstraintDefinitionStart:
case Parse::NodeKind::NamedConstraintIntroducer:
case Parse::NodeKind::NameQualifierWithParams:
case Parse::NodeKind::NameQualifierWithoutParams:
case Parse::NodeKind::NamespaceStart:
case Parse::NodeKind::PackageIntroducer:
case Parse::NodeKind::ParenExprStart:
Expand Down
89 changes: 83 additions & 6 deletions toolchain/check/testdata/class/no_prelude/destroy.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

// --- fail_todo_basic.carbon

library "[[@TEST_NAME]]";

class C {
// CHECK:STDERR: fail_todo_basic.carbon:[[@LINE+4]]:6: error: semantics TODO: `KeywordNameBeforeParamsId` [SemanticsTodo]
// CHECK:STDERR: fn destroy[self: Self]();
Expand All @@ -19,8 +21,8 @@ class C {
}

// --- fail_todo_addr.carbon
// CHECK:STDERR: fail_todo_addr.carbon: error: `Main//default` previously provided by `fail_todo_basic.carbon` [DuplicateMainApi]
// CHECK:STDERR:

library "[[@TEST_NAME]]";

class C {
// CHECK:STDERR: fail_todo_addr.carbon:[[@LINE+4]]:6: error: semantics TODO: `KeywordNameBeforeParamsId` [SemanticsTodo]
Expand All @@ -31,8 +33,9 @@ class C {
}

// --- fail_todo_class_function.carbon
// CHECK:STDERR: fail_todo_class_function.carbon: error: `Main//default` previously provided by `fail_todo_basic.carbon` [DuplicateMainApi]
// CHECK:STDERR:

library "[[@TEST_NAME]]";


class C {
// CHECK:STDERR: fail_todo_class_function.carbon:[[@LINE+4]]:6: error: semantics TODO: `KeywordNameBeforeParamsId` [SemanticsTodo]
Expand All @@ -42,16 +45,56 @@ class C {
fn destroy();
}

// --- fail_todo_wrong_scope.carbon
// CHECK:STDERR: fail_todo_wrong_scope.carbon: error: `Main//default` previously provided by `fail_todo_basic.carbon` [DuplicateMainApi]
// --- fail_todo_destroy_in_namespace.carbon

library "[[@TEST_NAME]]";

namespace NS;

// CHECK:STDERR: fail_todo_destroy_in_namespace.carbon:[[@LINE+4]]:7: error: semantics TODO: `KeywordNameBeforeParamsId` [SemanticsTodo]
// CHECK:STDERR: fn NS.destroy();
// CHECK:STDERR: ^~~~~~~
// CHECK:STDERR:
fn NS.destroy();

// --- fail_todo_missing_params.carbon

library "[[@TEST_NAME]]";


class C {
// CHECK:STDERR: fail_todo_missing_params.carbon:[[@LINE+4]]:6: error: semantics TODO: `KeywordNameNotBeforeParamsId` [SemanticsTodo]
// CHECK:STDERR: fn destroy;
// CHECK:STDERR: ^~~~~~~
// CHECK:STDERR:
fn destroy;
}

fn C.destroy {}

// --- fail_todo_wrong_scope.carbon

library "[[@TEST_NAME]]";


// CHECK:STDERR: fail_todo_wrong_scope.carbon:[[@LINE+4]]:4: error: semantics TODO: `KeywordNameBeforeParamsId` [SemanticsTodo]
// CHECK:STDERR: fn destroy();
// CHECK:STDERR: ^~~~~~~
// CHECK:STDERR:
fn destroy();

// --- fail_invalid_qualifier.carbon

class C {
// CHECK:STDERR: fail_invalid_qualifier.carbon:[[@LINE+4]]:6: error: semantics TODO: `KeywordNameBeforeParamsId` [SemanticsTodo]
// CHECK:STDERR: fn destroy();
// CHECK:STDERR: ^~~~~~~
// CHECK:STDERR:
fn destroy();
}

fn C.destroy.destroy() {}

// CHECK:STDOUT: --- fail_todo_basic.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
Expand Down Expand Up @@ -97,7 +140,41 @@ fn destroy();
// CHECK:STDOUT: .Self = constants.%C
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: --- fail_todo_destroy_in_namespace.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: file {}
// CHECK:STDOUT:
// CHECK:STDOUT: --- fail_todo_missing_params.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %C: type = class_type @C [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {}
// CHECK:STDOUT:
// CHECK:STDOUT: class @C {
// CHECK:STDOUT: complete_type_witness = invalid
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = constants.%C
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: --- fail_todo_wrong_scope.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: file {}
// CHECK:STDOUT:
// CHECK:STDOUT: --- fail_invalid_qualifier.carbon
// CHECK:STDOUT:
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %C: type = class_type @C [concrete]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {}
// CHECK:STDOUT:
// CHECK:STDOUT: class @C {
// CHECK:STDOUT: complete_type_witness = invalid
// CHECK:STDOUT:
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = constants.%C
// CHECK:STDOUT: }
// CHECK:STDOUT:
5 changes: 4 additions & 1 deletion toolchain/parse/handle_decl_name_and_params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ namespace Carbon::Parse {
static auto HandleName(Context& context, Context::StateStackEntry state,
Lex::TokenIndex name_token,
NodeKind not_before_params_kind,
NodeKind not_before_params_qualifier_kind,
NodeKind before_params_kind) -> void {
switch (context.PositionKind()) {
case Lex::TokenKind::Period:
context.AddLeafNode(not_before_params_kind, name_token);
context.AddNode(NodeKind::NameQualifierWithoutParams,
context.AddNode(not_before_params_qualifier_kind,
context.ConsumeChecked(Lex::TokenKind::Period),
state.has_error);
context.PushState(State::DeclNameAndParams);
Expand Down Expand Up @@ -49,12 +50,14 @@ auto HandleDeclNameAndParams(Context& context) -> void {
if (auto identifier = context.ConsumeIf(Lex::TokenKind::Identifier)) {
HandleName(context, state, *identifier,
NodeKind::IdentifierNameNotBeforeParams,
NodeKind::IdentifierNameQualifierWithoutParams,
NodeKind::IdentifierNameBeforeParams);
return;
}

if (auto keyword = context.ConsumeIf(Lex::TokenKind::Destroy)) {
HandleName(context, state, *keyword, NodeKind::KeywordNameNotBeforeParams,
NodeKind::KeywordNameQualifierWithoutParams,
NodeKind::KeywordNameBeforeParams);
return;
}
Expand Down
3 changes: 2 additions & 1 deletion toolchain/parse/node_kind.def
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ CARBON_PARSE_NODE_KIND(LibraryDecl)
CARBON_PARSE_NODE_KIND(LibrarySpecifier)

CARBON_PARSE_NODE_KIND(NameQualifierWithParams)
CARBON_PARSE_NODE_KIND(NameQualifierWithoutParams)
CARBON_PARSE_NODE_KIND(IdentifierNameQualifierWithoutParams)
CARBON_PARSE_NODE_KIND(KeywordNameQualifierWithoutParams)

CARBON_PARSE_NODE_KIND(ExportIntroducer)
CARBON_PARSE_NODE_KIND(ExportDecl)
Expand Down
2 changes: 1 addition & 1 deletion toolchain/parse/testdata/alias/basic.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn F() {
// CHECK:STDOUT: {kind: 'Alias', text: ';', subtree_size: 7},
// CHECK:STDOUT: {kind: 'AliasIntroducer', text: 'alias'},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeParams', text: 'NS'},
// CHECK:STDOUT: {kind: 'NameQualifierWithoutParams', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'IdentifierNameQualifierWithoutParams', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeParams', text: 'ns'},
// CHECK:STDOUT: {kind: 'AliasInitializer', text: '='},
// CHECK:STDOUT: {kind: 'IdentifierNameExpr', text: 'foo'},
Expand Down
2 changes: 1 addition & 1 deletion toolchain/parse/testdata/class/local.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn F() {
// CHECK:STDOUT: {kind: 'ClassDefinition', text: '}', subtree_size: 15},
// CHECK:STDOUT: {kind: 'FunctionIntroducer', text: 'fn'},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeParams', text: 'C'},
// CHECK:STDOUT: {kind: 'NameQualifierWithoutParams', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'IdentifierNameQualifierWithoutParams', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'IdentifierNameBeforeParams', text: 'H'},
// CHECK:STDOUT: {kind: 'ExplicitParamListStart', text: '('},
// CHECK:STDOUT: {kind: 'ExplicitParamList', text: ')', subtree_size: 2},
Expand Down
34 changes: 34 additions & 0 deletions toolchain/parse/testdata/function/declaration.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ fn destroy() {}

fn destroy {}

// --- keyword_in_qualified.carbon

fn MyClass.destroy() {}

// --- keyword_decl.carbon

fn destroy.destroy() {}

// --- impl_fn.carbon

impl fn F();
Expand Down Expand Up @@ -284,6 +292,32 @@ fn (a tokens c d e f g h i j k l m n o p q r s t u v w x y z);
// CHECK:STDOUT: {kind: 'FunctionDefinition', text: '}', subtree_size: 4},
// CHECK:STDOUT: {kind: 'FileEnd', text: ''},
// CHECK:STDOUT: ]
// CHECK:STDOUT: - filename: keyword_in_qualified.carbon
// CHECK:STDOUT: parse_tree: [
// CHECK:STDOUT: {kind: 'FileStart', text: ''},
// CHECK:STDOUT: {kind: 'FunctionIntroducer', text: 'fn'},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeParams', text: 'MyClass'},
// CHECK:STDOUT: {kind: 'IdentifierNameQualifierWithoutParams', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'KeywordNameBeforeParams', text: 'destroy'},
// CHECK:STDOUT: {kind: 'ExplicitParamListStart', text: '('},
// CHECK:STDOUT: {kind: 'ExplicitParamList', text: ')', subtree_size: 2},
// CHECK:STDOUT: {kind: 'FunctionDefinitionStart', text: '{', subtree_size: 7},
// CHECK:STDOUT: {kind: 'FunctionDefinition', text: '}', subtree_size: 8},
// CHECK:STDOUT: {kind: 'FileEnd', text: ''},
// CHECK:STDOUT: ]
// CHECK:STDOUT: - filename: keyword_decl.carbon
// CHECK:STDOUT: parse_tree: [
// CHECK:STDOUT: {kind: 'FileStart', text: ''},
// CHECK:STDOUT: {kind: 'FunctionIntroducer', text: 'fn'},
// CHECK:STDOUT: {kind: 'KeywordNameNotBeforeParams', text: 'destroy'},
// CHECK:STDOUT: {kind: 'KeywordNameQualifierWithoutParams', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'KeywordNameBeforeParams', text: 'destroy'},
// CHECK:STDOUT: {kind: 'ExplicitParamListStart', text: '('},
// CHECK:STDOUT: {kind: 'ExplicitParamList', text: ')', subtree_size: 2},
// CHECK:STDOUT: {kind: 'FunctionDefinitionStart', text: '{', subtree_size: 7},
// CHECK:STDOUT: {kind: 'FunctionDefinition', text: '}', subtree_size: 8},
// CHECK:STDOUT: {kind: 'FileEnd', text: ''},
// CHECK:STDOUT: ]
// CHECK:STDOUT: - filename: impl_fn.carbon
// CHECK:STDOUT: parse_tree: [
// CHECK:STDOUT: {kind: 'FileStart', text: ''},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn C.(Self as Interface).F() {}
// CHECK:STDOUT: {kind: 'ClassDefinition', text: '}', subtree_size: 15},
// CHECK:STDOUT: {kind: 'FunctionIntroducer', text: 'fn'},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeParams', text: 'C'},
// CHECK:STDOUT: {kind: 'NameQualifierWithoutParams', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'IdentifierNameQualifierWithoutParams', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'InvalidParse', text: '(', has_error: yes},
// CHECK:STDOUT: {kind: 'FunctionDecl', text: '}', has_error: yes, subtree_size: 5},
// CHECK:STDOUT: {kind: 'FileEnd', text: ''},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Foo.;
// CHECK:STDOUT: {kind: 'FileStart', text: ''},
// CHECK:STDOUT: {kind: 'NamespaceStart', text: 'namespace'},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeParams', text: 'Foo'},
// CHECK:STDOUT: {kind: 'NameQualifierWithoutParams', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'IdentifierNameQualifierWithoutParams', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'InvalidParse', text: ';', has_error: yes},
// CHECK:STDOUT: {kind: 'Namespace', text: ';', has_error: yes, subtree_size: 5},
// CHECK:STDOUT: {kind: 'FileEnd', text: ''},
Expand Down
6 changes: 3 additions & 3 deletions toolchain/parse/testdata/namespace/nested.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ fn Foo.Bar.Baz() {
// CHECK:STDOUT: {kind: 'Namespace', text: ';', subtree_size: 3},
// CHECK:STDOUT: {kind: 'NamespaceStart', text: 'namespace'},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeParams', text: 'Foo'},
// CHECK:STDOUT: {kind: 'NameQualifierWithoutParams', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'IdentifierNameQualifierWithoutParams', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeParams', text: 'Bar'},
// CHECK:STDOUT: {kind: 'Namespace', text: ';', subtree_size: 5},
// CHECK:STDOUT: {kind: 'FunctionIntroducer', text: 'fn'},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeParams', text: 'Foo'},
// CHECK:STDOUT: {kind: 'NameQualifierWithoutParams', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'IdentifierNameQualifierWithoutParams', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeParams', text: 'Bar'},
// CHECK:STDOUT: {kind: 'NameQualifierWithoutParams', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'IdentifierNameQualifierWithoutParams', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'IdentifierNameBeforeParams', text: 'Baz'},
// CHECK:STDOUT: {kind: 'ExplicitParamListStart', text: '('},
// CHECK:STDOUT: {kind: 'ExplicitParamList', text: ')', subtree_size: 2},
Expand Down
4 changes: 2 additions & 2 deletions toolchain/parse/testdata/packages/export.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export Foo;
// CHECK:STDOUT: {kind: 'PackageDecl', text: ';', subtree_size: 3},
// CHECK:STDOUT: {kind: 'ExportIntroducer', text: 'export'},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeParams', text: 'Foo'},
// CHECK:STDOUT: {kind: 'NameQualifierWithoutParams', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'IdentifierNameQualifierWithoutParams', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeParams', text: 'Bar'},
// CHECK:STDOUT: {kind: 'ExportDecl', text: ';', subtree_size: 5},
// CHECK:STDOUT: {kind: 'FileEnd', text: ''},
Expand Down Expand Up @@ -230,7 +230,7 @@ export Foo;
// CHECK:STDOUT: {kind: 'PackageDecl', text: ';', subtree_size: 3},
// CHECK:STDOUT: {kind: 'ExportIntroducer', text: 'export'},
// CHECK:STDOUT: {kind: 'IdentifierNameNotBeforeParams', text: 'Foo'},
// CHECK:STDOUT: {kind: 'NameQualifierWithoutParams', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'IdentifierNameQualifierWithoutParams', text: '.', subtree_size: 2},
// CHECK:STDOUT: {kind: 'InvalidParse', text: ';', has_error: yes},
// CHECK:STDOUT: {kind: 'ExportDecl', text: ';', has_error: yes, subtree_size: 5},
// CHECK:STDOUT: {kind: 'FileEnd', text: ''},
Expand Down
18 changes: 14 additions & 4 deletions toolchain/parse/typed_nodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,19 +180,29 @@ struct NameQualifierWithParams {
};

// A name qualifier without parameters, such as `A.`.
struct NameQualifierWithoutParams {
static constexpr auto Kind = NodeKind::NameQualifierWithoutParams.Define(
{.bracketed_by = IdentifierNameNotBeforeParams::Kind});
struct IdentifierNameQualifierWithoutParams {
static constexpr auto Kind =
NodeKind::IdentifierNameQualifierWithoutParams.Define(
{.bracketed_by = IdentifierNameNotBeforeParams::Kind});

IdentifierNameNotBeforeParamsId name;
Lex::PeriodTokenIndex token;
};
struct KeywordNameQualifierWithoutParams {
static constexpr auto Kind =
NodeKind::KeywordNameQualifierWithoutParams.Define(
{.bracketed_by = KeywordNameNotBeforeParams::Kind});

KeywordNameNotBeforeParamsId name;
Lex::PeriodTokenIndex token;
};

// A complete name in a declaration: `A.C(T:! type).F(n: i32)`.
// Note that this includes the parameters of the entity itself.
struct DeclName {
llvm::SmallVector<
NodeIdOneOf<NameQualifierWithParams, NameQualifierWithoutParams>>
NodeIdOneOf<NameQualifierWithParams, IdentifierNameQualifierWithoutParams,
KeywordNameQualifierWithoutParams>>
qualifiers;
AnyNonExprNameId name;
std::optional<ImplicitParamListId> implicit_params;
Expand Down
4 changes: 2 additions & 2 deletions toolchain/parse/typed_nodes_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ NodeIdForKind error: wrong kind IdentifierNameBeforeParams, expected ImplicitPar
Optional [^:]*: missing
NodeIdInCategory NonExprName: kind IdentifierNameBeforeParams consumed
Vector: begin
NodeIdOneOf NameQualifierWithParams or NameQualifierWithoutParams: NameQualifierWithoutParams consumed
NodeIdOneOf error: wrong kind AbstractModifier, expected NameQualifierWithParams or NameQualifierWithoutParams
NodeIdOneOf NameQualifierWithParams or IdentifierNameQualifierWithoutParams or KeywordNameQualifierWithoutParams: IdentifierNameQualifierWithoutParams consumed
NodeIdOneOf error: wrong kind AbstractModifier, expected NameQualifierWithParams or IdentifierNameQualifierWithoutParams or KeywordNameQualifierWithoutParams
Vector: end
Aggregate [^:]*: success
Vector: begin
Expand Down

0 comments on commit 6c84ea3

Please sign in to comment.