Skip to content

Commit

Permalink
Cleanup semantic analysis
Browse files Browse the repository at this point in the history
Summary: AST mutators are an implementation detail of semantic analysis and are not supposed to be used directly. Remove them from the public API. This reduces the public API surface and prevents misuse.

Differential Revision: D63795762

fbshipit-source-id: e059217f1b1f10939766d2822ba9881b241881e3
  • Loading branch information
vitaut authored and facebook-github-bot committed Oct 3, 2024
1 parent adf6016 commit e53d039
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 182 deletions.
13 changes: 5 additions & 8 deletions thrift/compiler/compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
#include <thrift/compiler/diagnostic.h>
#include <thrift/compiler/generate/t_generator.h>
#include <thrift/compiler/parse/parse_ast.h>
#include <thrift/compiler/sema/ast_mutator.h>
#include <thrift/compiler/sema/ast_validator.h>
#include <thrift/compiler/sema/sema.h>
#include <thrift/compiler/sema/sema_context.h>
Expand Down Expand Up @@ -773,9 +772,7 @@ std::unique_ptr<t_program_bundle> parse_and_mutate(
// C++ codegen inserts an empty const if this is false. Other languages may
// dynamically determine whether the schema const exists.
if (gparams.inject_schema_const) {
mutator_context mctx;
mctx.bundle = program_bundle.get();
schema_mutator()(ctx, mctx, *program_bundle->root_program());
sema::add_schema(ctx, *program_bundle);
}

program_bundle->root_program()->set_include_prefix(
Expand All @@ -793,8 +790,8 @@ parse_and_mutate_program(
parsing_params params,
diagnostic_params dparams) {
diagnostic_results results;
sema_context ctx(sm, results, std::move(dparams));
return {parse_ast(sm, ctx, filename, std::move(params)), results};
diagnostics_engine diags(sm, results, std::move(dparams));
return {parse_ast(sm, diags, filename, std::move(params)), results};
}

std::unique_ptr<t_program_bundle> parse_and_dump_diagnostics(
Expand All @@ -803,8 +800,8 @@ std::unique_ptr<t_program_bundle> parse_and_dump_diagnostics(
parsing_params pparams,
diagnostic_params dparams) {
diagnostic_results results;
sema_context ctx(sm, results, std::move(dparams));
auto programs = parse_ast(sm, ctx, filename, std::move(pparams));
diagnostics_engine diags(sm, results, std::move(dparams));
auto programs = parse_ast(sm, diags, filename, std::move(pparams));
for (const auto& diag : results.diagnostics()) {
fmt::print(stderr, "{}\n", diag);
}
Expand Down
20 changes: 4 additions & 16 deletions thrift/compiler/parse/parse_ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include <thrift/compiler/diagnostic.h>
#include <thrift/compiler/parse/lexer.h>
#include <thrift/compiler/parse/parser.h>
#include <thrift/compiler/sema/ast_mutator.h>
#include <thrift/compiler/sema/sema.h>
#include <thrift/compiler/source_location.h>

Expand Down Expand Up @@ -1095,6 +1094,9 @@ std::unique_ptr<t_program_bundle> parse_ast(
} catch (const parsing_terminator&) {
return {}; // Return a null program bundle if parsing failed.
}
if (diags.has_errors()) {
return programs;
}

sema_context ctx(
diags.source_mgr(),
Expand All @@ -1103,21 +1105,7 @@ std::unique_ptr<t_program_bundle> parse_ast(
if (sparams) {
ctx.sema_parameters() = *sparams;
}

// Resolve types in the root program.
if (!params.use_legacy_type_ref_resolution) {
type_ref_resolver().run(ctx, *programs);
}
std::string program_prefix = root_program.name() + ".";
for (t_placeholder_typedef& t :
root_program.scope()->placeholder_typedefs()) {
if (!t.resolve() && t.name().find(program_prefix) == 0) {
diags.error(t, "Type `{}` not defined.", t.name());
}
}
if (!diags.has_errors()) {
sema(params.use_legacy_type_ref_resolution).run(ctx, *programs);
}
sema(params.use_legacy_type_ref_resolution).run(ctx, *programs);
return programs;
}

Expand Down
144 changes: 0 additions & 144 deletions thrift/compiler/sema/ast_mutator.h

This file was deleted.

Loading

0 comments on commit e53d039

Please sign in to comment.