mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Move 'export name' out of the import section. (#3950)
This implements recent updates to #3938
This commit is contained in:
@@ -183,8 +183,8 @@ static auto VerifyInImports(Context& context, Lex::TokenIndex intro_token)
|
||||
context.set_packaging_state(
|
||||
Context::PackagingState::InImportsAfterNonPackagingDecl);
|
||||
CARBON_DIAGNOSTIC(ImportTooLate, Error,
|
||||
"`import` and `export` directives must come after the "
|
||||
"`package` directive (if present) and before any other "
|
||||
"`import` directives must come after the `package` "
|
||||
"directive (if present) and before any other "
|
||||
"entities in the file.");
|
||||
CARBON_DIAGNOSTIC(FirstDecl, Note, "First declaration is here.");
|
||||
context.emitter()
|
||||
@@ -225,11 +225,15 @@ static auto HandleImportHelper(Context& context,
|
||||
}
|
||||
}
|
||||
|
||||
auto HandleExport(Context& context) -> void {
|
||||
auto HandleImportAsRegular(Context& context) -> void {
|
||||
auto state = context.PopState();
|
||||
|
||||
context.ConsumeChecked(Lex::TokenKind::Export);
|
||||
HandleImportHelper(context, state, /*export_token=*/Lex::TokenIndex::Invalid);
|
||||
}
|
||||
|
||||
// Diagnoses if `export` is used in an `impl` file.
|
||||
static auto RestrictExportToApi(Context& context,
|
||||
Context::StateStackEntry& state) -> void {
|
||||
// Error for both Main//default and every implementation file.
|
||||
auto packaging = context.tree().packaging_directive();
|
||||
if (!packaging || packaging->api_or_impl == Tree::ApiOrImpl::Impl) {
|
||||
@@ -238,21 +242,27 @@ auto HandleExport(Context& context) -> void {
|
||||
context.emitter().Emit(state.token, ExportFromImpl);
|
||||
state.has_error = true;
|
||||
}
|
||||
|
||||
if (context.PositionIs(Lex::TokenKind::Import)) {
|
||||
HandleImportHelper(context, state, state.token);
|
||||
} else {
|
||||
if (!VerifyInImports(context, state.token)) {
|
||||
state.has_error = true;
|
||||
}
|
||||
context.AddLeafNode(NodeKind::ExportIntroducer, state.token,
|
||||
state.has_error);
|
||||
context.PushState(state, State::ExportFinish);
|
||||
context.PushState(State::DeclNameAndParamsAsNone, state.token);
|
||||
}
|
||||
}
|
||||
|
||||
auto HandleExportFinish(Context& context) -> void {
|
||||
auto HandleImportAsExport(Context& context) -> void {
|
||||
auto state = context.PopState();
|
||||
|
||||
context.ConsumeChecked(Lex::TokenKind::Export);
|
||||
RestrictExportToApi(context, state);
|
||||
|
||||
HandleImportHelper(context, state, state.token);
|
||||
}
|
||||
|
||||
auto HandleExportName(Context& context) -> void {
|
||||
auto state = context.PopState();
|
||||
|
||||
RestrictExportToApi(context, state);
|
||||
|
||||
context.PushState(state, State::ExportNameFinish);
|
||||
context.PushState(State::DeclNameAndParamsAsNone, state.token);
|
||||
}
|
||||
|
||||
auto HandleExportNameFinish(Context& context) -> void {
|
||||
auto state = context.PopState();
|
||||
|
||||
context.AddNodeExpectingDeclSemi(state, NodeKind::ExportDirective,
|
||||
@@ -260,12 +270,6 @@ auto HandleExportFinish(Context& context) -> void {
|
||||
/*is_def_allowed=*/false);
|
||||
}
|
||||
|
||||
auto HandleImport(Context& context) -> void {
|
||||
auto state = context.PopState();
|
||||
|
||||
HandleImportHelper(context, state, /*export_token=*/Lex::TokenIndex::Invalid);
|
||||
}
|
||||
|
||||
// Handles common logic for `package` and `library`.
|
||||
static auto HandlePackageAndLibraryDirectives(Context& context,
|
||||
Lex::TokenKind intro_token_kind,
|
||||
|
||||
Reference in New Issue
Block a user