ast_to_proto.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "explorer/fuzzing/ast_to_proto.h"
  5. #include <optional>
  6. #include "explorer/ast/declaration.h"
  7. #include "explorer/ast/expression.h"
  8. #include "llvm/Support/Casting.h"
  9. namespace Carbon {
  10. using ::llvm::cast;
  11. using ::llvm::isa;
  12. static auto ExpressionToProto(const Expression& expression)
  13. -> Fuzzing::Expression;
  14. static auto PatternToProto(const Pattern& pattern) -> Fuzzing::Pattern;
  15. static auto StatementToProto(const Statement& statement) -> Fuzzing::Statement;
  16. static auto DeclarationToProto(const Declaration& declaration)
  17. -> Fuzzing::Declaration;
  18. static auto LibraryNameToProto(const LibraryName& library_name)
  19. -> Fuzzing::LibraryName {
  20. Fuzzing::LibraryName library_name_proto;
  21. library_name_proto.set_package_name(library_name.package);
  22. if (!library_name.path.empty()) {
  23. library_name_proto.set_path(library_name.path);
  24. }
  25. return library_name_proto;
  26. }
  27. static auto OperatorToProtoEnum(const Operator op)
  28. -> Fuzzing::PrimitiveOperatorExpression::Operator {
  29. switch (op) {
  30. case Operator::AddressOf:
  31. return Fuzzing::PrimitiveOperatorExpression::AddressOf;
  32. case Operator::Deref:
  33. return Fuzzing::PrimitiveOperatorExpression::Deref;
  34. case Operator::Neg:
  35. return Fuzzing::PrimitiveOperatorExpression::Neg;
  36. case Operator::Not:
  37. return Fuzzing::PrimitiveOperatorExpression::Not;
  38. case Operator::Ptr:
  39. return Fuzzing::PrimitiveOperatorExpression::Ptr;
  40. case Operator::Add:
  41. return Fuzzing::PrimitiveOperatorExpression::Add;
  42. case Operator::And:
  43. return Fuzzing::PrimitiveOperatorExpression::And;
  44. case Operator::Eq:
  45. return Fuzzing::PrimitiveOperatorExpression::Eq;
  46. case Operator::Mul:
  47. return Fuzzing::PrimitiveOperatorExpression::Mul;
  48. case Operator::Or:
  49. return Fuzzing::PrimitiveOperatorExpression::Or;
  50. case Operator::Sub:
  51. return Fuzzing::PrimitiveOperatorExpression::Sub;
  52. case Operator::Combine:
  53. return Fuzzing::PrimitiveOperatorExpression::Combine;
  54. }
  55. }
  56. static auto FieldInitializerToProto(const FieldInitializer& field)
  57. -> Fuzzing::FieldInitializer {
  58. Fuzzing::FieldInitializer field_proto;
  59. field_proto.set_name(field.name());
  60. *field_proto.mutable_expression() = ExpressionToProto(field.expression());
  61. return field_proto;
  62. }
  63. static auto TupleLiteralExpressionToProto(const TupleLiteral& tuple_literal)
  64. -> Fuzzing::TupleLiteralExpression {
  65. Fuzzing::TupleLiteralExpression tuple_literal_proto;
  66. for (Nonnull<const Expression*> field : tuple_literal.fields()) {
  67. *tuple_literal_proto.add_fields() = ExpressionToProto(*field);
  68. }
  69. return tuple_literal_proto;
  70. }
  71. static auto ExpressionToProto(const Expression& expression)
  72. -> Fuzzing::Expression {
  73. Fuzzing::Expression expression_proto;
  74. switch (expression.kind()) {
  75. case ExpressionKind::InstantiateImpl:
  76. case ExpressionKind::ValueLiteral: {
  77. // These do not correspond to source syntax.
  78. break;
  79. }
  80. case ExpressionKind::CallExpression: {
  81. const auto& call = cast<CallExpression>(expression);
  82. auto* call_proto = expression_proto.mutable_call();
  83. *call_proto->mutable_function() = ExpressionToProto(call.function());
  84. *call_proto->mutable_argument() = ExpressionToProto(call.argument());
  85. break;
  86. }
  87. case ExpressionKind::FunctionTypeLiteral: {
  88. const auto& fun_type = cast<FunctionTypeLiteral>(expression);
  89. auto* fun_type_proto = expression_proto.mutable_function_type();
  90. *fun_type_proto->mutable_parameter() =
  91. TupleLiteralExpressionToProto(fun_type.parameter());
  92. *fun_type_proto->mutable_return_type() =
  93. ExpressionToProto(fun_type.return_type());
  94. break;
  95. }
  96. case ExpressionKind::SimpleMemberAccessExpression: {
  97. const auto& simple_member_access =
  98. cast<SimpleMemberAccessExpression>(expression);
  99. if (isa<DotSelfExpression>(simple_member_access.object())) {
  100. // The parser rewrites `.Foo` into `.Self.Foo`. Undo this
  101. // transformation.
  102. auto* designator_proto = expression_proto.mutable_designator();
  103. designator_proto->set_name(simple_member_access.member());
  104. break;
  105. }
  106. auto* simple_member_access_proto =
  107. expression_proto.mutable_simple_member_access();
  108. simple_member_access_proto->set_field(simple_member_access.member());
  109. *simple_member_access_proto->mutable_object() =
  110. ExpressionToProto(simple_member_access.object());
  111. break;
  112. }
  113. case ExpressionKind::CompoundMemberAccessExpression: {
  114. const auto& simple_member_access =
  115. cast<CompoundMemberAccessExpression>(expression);
  116. auto* simple_member_access_proto =
  117. expression_proto.mutable_compound_member_access();
  118. *simple_member_access_proto->mutable_object() =
  119. ExpressionToProto(simple_member_access.object());
  120. *simple_member_access_proto->mutable_path() =
  121. ExpressionToProto(simple_member_access.path());
  122. break;
  123. }
  124. case ExpressionKind::IndexExpression: {
  125. const auto& index = cast<IndexExpression>(expression);
  126. auto* index_proto = expression_proto.mutable_index();
  127. *index_proto->mutable_object() = ExpressionToProto(index.object());
  128. *index_proto->mutable_offset() = ExpressionToProto(index.offset());
  129. break;
  130. }
  131. case ExpressionKind::PrimitiveOperatorExpression: {
  132. const auto& primitive_operator =
  133. cast<PrimitiveOperatorExpression>(expression);
  134. auto* operator_proto = expression_proto.mutable_primitive_operator();
  135. operator_proto->set_op(OperatorToProtoEnum(primitive_operator.op()));
  136. for (Nonnull<const Expression*> arg : primitive_operator.arguments()) {
  137. *operator_proto->add_arguments() = ExpressionToProto(*arg);
  138. }
  139. break;
  140. }
  141. case ExpressionKind::TupleLiteral:
  142. *expression_proto.mutable_tuple_literal() =
  143. TupleLiteralExpressionToProto(cast<TupleLiteral>(expression));
  144. break;
  145. case ExpressionKind::StructLiteral: {
  146. const auto& struct_literal = cast<StructLiteral>(expression);
  147. auto* struct_literal_proto = expression_proto.mutable_struct_literal();
  148. for (const FieldInitializer& field : struct_literal.fields()) {
  149. *struct_literal_proto->add_fields() = FieldInitializerToProto(field);
  150. }
  151. break;
  152. }
  153. case ExpressionKind::StructTypeLiteral: {
  154. const auto& struct_type_literal = cast<StructTypeLiteral>(expression);
  155. auto* struct_type_literal_proto =
  156. expression_proto.mutable_struct_type_literal();
  157. for (const FieldInitializer& field : struct_type_literal.fields()) {
  158. *struct_type_literal_proto->add_fields() =
  159. FieldInitializerToProto(field);
  160. }
  161. break;
  162. }
  163. case ExpressionKind::IdentifierExpression: {
  164. const auto& identifier = cast<IdentifierExpression>(expression);
  165. auto* identifier_proto = expression_proto.mutable_identifier();
  166. identifier_proto->set_name(identifier.name());
  167. break;
  168. }
  169. case ExpressionKind::WhereExpression: {
  170. const auto& where = cast<WhereExpression>(expression);
  171. auto* where_proto = expression_proto.mutable_where();
  172. *where_proto->mutable_base() =
  173. ExpressionToProto(where.self_binding().type());
  174. for (const WhereClause* where : where.clauses()) {
  175. Fuzzing::WhereClause clause_proto;
  176. switch (where->kind()) {
  177. case WhereClauseKind::IsWhereClause: {
  178. auto* is_proto = clause_proto.mutable_is();
  179. *is_proto->mutable_type() =
  180. ExpressionToProto(cast<IsWhereClause>(where)->type());
  181. *is_proto->mutable_constraint() =
  182. ExpressionToProto(cast<IsWhereClause>(where)->constraint());
  183. break;
  184. }
  185. case WhereClauseKind::EqualsWhereClause: {
  186. auto* equals_proto = clause_proto.mutable_equals();
  187. *equals_proto->mutable_lhs() =
  188. ExpressionToProto(cast<EqualsWhereClause>(where)->lhs());
  189. *equals_proto->mutable_rhs() =
  190. ExpressionToProto(cast<EqualsWhereClause>(where)->rhs());
  191. break;
  192. }
  193. }
  194. *where_proto->add_clauses() = clause_proto;
  195. }
  196. break;
  197. }
  198. case ExpressionKind::DotSelfExpression: {
  199. auto* designator_proto = expression_proto.mutable_designator();
  200. designator_proto->set_name("Self");
  201. break;
  202. }
  203. case ExpressionKind::IntrinsicExpression: {
  204. const auto& intrinsic = cast<IntrinsicExpression>(expression);
  205. auto* intrinsic_proto = expression_proto.mutable_intrinsic();
  206. switch (intrinsic.intrinsic()) {
  207. case IntrinsicExpression::Intrinsic::Print:
  208. intrinsic_proto->set_intrinsic(Fuzzing::IntrinsicExpression::Print);
  209. break;
  210. case IntrinsicExpression::Intrinsic::Alloc:
  211. intrinsic_proto->set_intrinsic(Fuzzing::IntrinsicExpression::Alloc);
  212. break;
  213. case IntrinsicExpression::Intrinsic::Dealloc:
  214. intrinsic_proto->set_intrinsic(Fuzzing::IntrinsicExpression::Dealloc);
  215. break;
  216. }
  217. *intrinsic_proto->mutable_argument() =
  218. TupleLiteralExpressionToProto(intrinsic.args());
  219. break;
  220. }
  221. case ExpressionKind::IfExpression: {
  222. const auto& if_expression = cast<IfExpression>(expression);
  223. auto* if_proto = expression_proto.mutable_if_expression();
  224. *if_proto->mutable_condition() =
  225. ExpressionToProto(if_expression.condition());
  226. *if_proto->mutable_then_expression() =
  227. ExpressionToProto(if_expression.then_expression());
  228. *if_proto->mutable_else_expression() =
  229. ExpressionToProto(if_expression.else_expression());
  230. break;
  231. }
  232. case ExpressionKind::BoolTypeLiteral:
  233. expression_proto.mutable_bool_type_literal();
  234. break;
  235. case ExpressionKind::BoolLiteral:
  236. expression_proto.mutable_bool_literal()->set_value(
  237. cast<BoolLiteral>(expression).value());
  238. break;
  239. case ExpressionKind::IntTypeLiteral:
  240. expression_proto.mutable_int_type_literal();
  241. break;
  242. case ExpressionKind::IntLiteral:
  243. expression_proto.mutable_int_literal()->set_value(
  244. cast<IntLiteral>(expression).value());
  245. break;
  246. case ExpressionKind::StringLiteral:
  247. expression_proto.mutable_string_literal()->set_value(
  248. cast<StringLiteral>(expression).value());
  249. break;
  250. case ExpressionKind::StringTypeLiteral:
  251. expression_proto.mutable_string_type_literal();
  252. break;
  253. case ExpressionKind::ContinuationTypeLiteral:
  254. expression_proto.mutable_continuation_type_literal();
  255. break;
  256. case ExpressionKind::TypeTypeLiteral:
  257. expression_proto.mutable_type_type_literal();
  258. break;
  259. case ExpressionKind::UnimplementedExpression:
  260. expression_proto.mutable_unimplemented_expression();
  261. break;
  262. case ExpressionKind::ArrayTypeLiteral: {
  263. const auto& array_literal = cast<ArrayTypeLiteral>(expression);
  264. Fuzzing::ArrayTypeLiteral* array_literal_proto =
  265. expression_proto.mutable_array_type_literal();
  266. *array_literal_proto->mutable_element_type() =
  267. ExpressionToProto(array_literal.element_type_expression());
  268. *array_literal_proto->mutable_size() =
  269. ExpressionToProto(array_literal.size_expression());
  270. break;
  271. }
  272. }
  273. return expression_proto;
  274. }
  275. static auto BindingPatternToProto(const BindingPattern& pattern)
  276. -> Fuzzing::BindingPattern {
  277. Fuzzing::BindingPattern pattern_proto;
  278. pattern_proto.set_name(pattern.name());
  279. *pattern_proto.mutable_type() = PatternToProto(pattern.type());
  280. return pattern_proto;
  281. }
  282. static auto GenericBindingToProto(const GenericBinding& binding)
  283. -> Fuzzing::GenericBinding {
  284. Fuzzing::GenericBinding binding_proto;
  285. binding_proto.set_name(binding.name());
  286. *binding_proto.mutable_type() = ExpressionToProto(binding.type());
  287. return binding_proto;
  288. }
  289. static auto TuplePatternToProto(const TuplePattern& tuple_pattern)
  290. -> Fuzzing::TuplePattern {
  291. Fuzzing::TuplePattern tuple_pattern_proto;
  292. for (Nonnull<const Pattern*> field : tuple_pattern.fields()) {
  293. *tuple_pattern_proto.add_fields() = PatternToProto(*field);
  294. }
  295. return tuple_pattern_proto;
  296. }
  297. static auto PatternToProto(const Pattern& pattern) -> Fuzzing::Pattern {
  298. Fuzzing::Pattern pattern_proto;
  299. switch (pattern.kind()) {
  300. case PatternKind::GenericBinding: {
  301. const auto& binding = cast<GenericBinding>(pattern);
  302. *pattern_proto.mutable_generic_binding() = GenericBindingToProto(binding);
  303. break;
  304. }
  305. case PatternKind::BindingPattern: {
  306. const auto& binding = cast<BindingPattern>(pattern);
  307. *pattern_proto.mutable_binding_pattern() = BindingPatternToProto(binding);
  308. break;
  309. }
  310. case PatternKind::TuplePattern:
  311. *pattern_proto.mutable_tuple_pattern() =
  312. TuplePatternToProto(cast<TuplePattern>(pattern));
  313. break;
  314. case PatternKind::AlternativePattern: {
  315. const auto& alternative = cast<AlternativePattern>(pattern);
  316. auto* alternative_proto = pattern_proto.mutable_alternative_pattern();
  317. alternative_proto->set_alternative_name(alternative.alternative_name());
  318. *alternative_proto->mutable_choice_type() =
  319. ExpressionToProto(alternative.choice_type());
  320. *alternative_proto->mutable_arguments() =
  321. TuplePatternToProto(alternative.arguments());
  322. break;
  323. }
  324. case PatternKind::ExpressionPattern:
  325. *pattern_proto.mutable_expression_pattern()->mutable_expression() =
  326. ExpressionToProto(cast<ExpressionPattern>(pattern).expression());
  327. break;
  328. case PatternKind::AutoPattern:
  329. pattern_proto.mutable_auto_pattern();
  330. break;
  331. case PatternKind::VarPattern:
  332. *pattern_proto.mutable_var_pattern()->mutable_pattern() =
  333. PatternToProto(cast<VarPattern>(pattern).pattern());
  334. break;
  335. case PatternKind::AddrPattern:
  336. *pattern_proto.mutable_addr_pattern()->mutable_binding_pattern() =
  337. BindingPatternToProto(cast<AddrPattern>(pattern).binding());
  338. break;
  339. }
  340. return pattern_proto;
  341. }
  342. static auto BlockStatementToProto(const Block& block)
  343. -> Fuzzing::BlockStatement {
  344. Fuzzing::BlockStatement block_proto;
  345. for (Nonnull<const Statement*> statement : block.statements()) {
  346. *block_proto.add_statements() = StatementToProto(*statement);
  347. }
  348. return block_proto;
  349. }
  350. static auto StatementToProto(const Statement& statement) -> Fuzzing::Statement {
  351. Fuzzing::Statement statement_proto;
  352. switch (statement.kind()) {
  353. case StatementKind::ExpressionStatement:
  354. *statement_proto.mutable_expression_statement()->mutable_expression() =
  355. ExpressionToProto(cast<ExpressionStatement>(statement).expression());
  356. break;
  357. case StatementKind::Assign: {
  358. const auto& assign = cast<Assign>(statement);
  359. auto* assign_proto = statement_proto.mutable_assign();
  360. *assign_proto->mutable_lhs() = ExpressionToProto(assign.lhs());
  361. *assign_proto->mutable_rhs() = ExpressionToProto(assign.rhs());
  362. break;
  363. }
  364. case StatementKind::VariableDefinition: {
  365. const auto& def = cast<VariableDefinition>(statement);
  366. auto* def_proto = statement_proto.mutable_variable_definition();
  367. *def_proto->mutable_pattern() = PatternToProto(def.pattern());
  368. *def_proto->mutable_init() = ExpressionToProto(def.init());
  369. break;
  370. }
  371. case StatementKind::If: {
  372. const auto& if_stmt = cast<If>(statement);
  373. auto* if_proto = statement_proto.mutable_if_statement();
  374. *if_proto->mutable_condition() = ExpressionToProto(if_stmt.condition());
  375. *if_proto->mutable_then_block() =
  376. BlockStatementToProto(if_stmt.then_block());
  377. if (if_stmt.else_block().has_value()) {
  378. *if_proto->mutable_else_block() =
  379. BlockStatementToProto(**if_stmt.else_block());
  380. }
  381. break;
  382. }
  383. case StatementKind::Return: {
  384. const auto& ret = cast<Return>(statement);
  385. auto* ret_proto = statement_proto.mutable_return_statement();
  386. if (!ret.is_omitted_expression()) {
  387. *ret_proto->mutable_expression() = ExpressionToProto(ret.expression());
  388. } else {
  389. ret_proto->set_is_omitted_expression(true);
  390. }
  391. break;
  392. }
  393. case StatementKind::Block:
  394. *statement_proto.mutable_block() =
  395. BlockStatementToProto(cast<Block>(statement));
  396. break;
  397. case StatementKind::While: {
  398. const auto& while_stmt = cast<While>(statement);
  399. auto* while_proto = statement_proto.mutable_while_statement();
  400. *while_proto->mutable_condition() =
  401. ExpressionToProto(while_stmt.condition());
  402. *while_proto->mutable_body() = BlockStatementToProto(while_stmt.body());
  403. break;
  404. }
  405. case StatementKind::Match: {
  406. const auto& match = cast<Match>(statement);
  407. auto* match_proto = statement_proto.mutable_match();
  408. *match_proto->mutable_expression() =
  409. ExpressionToProto(match.expression());
  410. for (const Match::Clause& clause : match.clauses()) {
  411. auto* clause_proto = match_proto->add_clauses();
  412. const bool is_default_clause =
  413. clause.pattern().kind() == PatternKind::BindingPattern &&
  414. cast<BindingPattern>(clause.pattern()).name() == AnonymousName;
  415. if (is_default_clause) {
  416. clause_proto->set_is_default(true);
  417. } else {
  418. *clause_proto->mutable_pattern() = PatternToProto(clause.pattern());
  419. }
  420. *clause_proto->mutable_statement() =
  421. StatementToProto(clause.statement());
  422. }
  423. break;
  424. }
  425. case StatementKind::Continuation: {
  426. const auto& continuation = cast<Continuation>(statement);
  427. auto* continuation_proto = statement_proto.mutable_continuation();
  428. continuation_proto->set_name(continuation.name());
  429. *continuation_proto->mutable_body() =
  430. BlockStatementToProto(continuation.body());
  431. break;
  432. }
  433. case StatementKind::Run:
  434. *statement_proto.mutable_run()->mutable_argument() =
  435. ExpressionToProto(cast<Run>(statement).argument());
  436. break;
  437. case StatementKind::Await:
  438. // Initializes with the default value; there's nothing to set.
  439. statement_proto.mutable_await_statement();
  440. break;
  441. case StatementKind::Break:
  442. // Initializes with the default value; there's nothing to set.
  443. statement_proto.mutable_break_statement();
  444. break;
  445. case StatementKind::Continue:
  446. // Initializes with the default value; there's nothing to set.
  447. statement_proto.mutable_continue_statement();
  448. break;
  449. }
  450. return statement_proto;
  451. }
  452. static auto ReturnTermToProto(const ReturnTerm& return_term)
  453. -> Fuzzing::ReturnTerm {
  454. Fuzzing::ReturnTerm return_term_proto;
  455. if (return_term.is_omitted()) {
  456. return_term_proto.set_kind(Fuzzing::ReturnTerm::Omitted);
  457. } else if (return_term.is_auto()) {
  458. return_term_proto.set_kind(Fuzzing::ReturnTerm::Auto);
  459. } else {
  460. return_term_proto.set_kind(Fuzzing::ReturnTerm::Expression);
  461. *return_term_proto.mutable_type() =
  462. ExpressionToProto(**return_term.type_expression());
  463. }
  464. return return_term_proto;
  465. }
  466. static auto DeclarationToProto(const Declaration& declaration)
  467. -> Fuzzing::Declaration {
  468. Fuzzing::Declaration declaration_proto;
  469. switch (declaration.kind()) {
  470. case DeclarationKind::FunctionDeclaration: {
  471. const auto& function = cast<FunctionDeclaration>(declaration);
  472. auto* function_proto = declaration_proto.mutable_function();
  473. function_proto->set_name(function.name());
  474. for (Nonnull<const GenericBinding*> binding :
  475. function.deduced_parameters()) {
  476. *function_proto->add_deduced_parameters() =
  477. GenericBindingToProto(*binding);
  478. }
  479. if (function.is_method()) {
  480. switch (function.me_pattern().kind()) {
  481. case PatternKind::AddrPattern:
  482. *function_proto->mutable_me_pattern() =
  483. PatternToProto(cast<AddrPattern>(function.me_pattern()));
  484. break;
  485. case PatternKind::BindingPattern:
  486. *function_proto->mutable_me_pattern() =
  487. PatternToProto(cast<BindingPattern>(function.me_pattern()));
  488. break;
  489. default:
  490. // Parser shouldn't allow me_pattern to be anything other than
  491. // AddrPattern or BindingPattern
  492. CARBON_FATAL() << "me_pattern in method declaration can be either "
  493. "AddrPattern or BindingPattern. Actual pattern: "
  494. << function.me_pattern();
  495. break;
  496. }
  497. }
  498. *function_proto->mutable_param_pattern() =
  499. TuplePatternToProto(function.param_pattern());
  500. *function_proto->mutable_return_term() =
  501. ReturnTermToProto(function.return_term());
  502. if (function.body().has_value()) {
  503. *function_proto->mutable_body() =
  504. BlockStatementToProto(**function.body());
  505. }
  506. break;
  507. }
  508. case DeclarationKind::ClassDeclaration: {
  509. const auto& class_decl = cast<ClassDeclaration>(declaration);
  510. auto* class_proto = declaration_proto.mutable_class_declaration();
  511. class_proto->set_name(class_decl.name());
  512. if (class_decl.type_params().has_value()) {
  513. *class_proto->mutable_type_params() =
  514. TuplePatternToProto(**class_decl.type_params());
  515. }
  516. for (Nonnull<const Declaration*> member : class_decl.members()) {
  517. *class_proto->add_members() = DeclarationToProto(*member);
  518. }
  519. break;
  520. }
  521. case DeclarationKind::ChoiceDeclaration: {
  522. const auto& choice = cast<ChoiceDeclaration>(declaration);
  523. auto* choice_proto = declaration_proto.mutable_choice();
  524. choice_proto->set_name(choice.name());
  525. for (Nonnull<const AlternativeSignature*> alternative :
  526. choice.alternatives()) {
  527. auto* alternative_proto = choice_proto->add_alternatives();
  528. alternative_proto->set_name(alternative->name());
  529. *alternative_proto->mutable_signature() =
  530. TupleLiteralExpressionToProto(alternative->signature());
  531. }
  532. break;
  533. }
  534. case DeclarationKind::VariableDeclaration: {
  535. const auto& var = cast<VariableDeclaration>(declaration);
  536. auto* var_proto = declaration_proto.mutable_variable();
  537. *var_proto->mutable_binding() = BindingPatternToProto(var.binding());
  538. if (var.has_initializer()) {
  539. *var_proto->mutable_initializer() =
  540. ExpressionToProto(var.initializer());
  541. }
  542. break;
  543. }
  544. case DeclarationKind::InterfaceDeclaration: {
  545. const auto& interface = cast<InterfaceDeclaration>(declaration);
  546. auto* interface_proto = declaration_proto.mutable_interface();
  547. interface_proto->set_name(interface.name());
  548. for (const auto& member : interface.members()) {
  549. *interface_proto->add_members() = DeclarationToProto(*member);
  550. }
  551. *interface_proto->mutable_self() =
  552. GenericBindingToProto(*interface.self());
  553. break;
  554. }
  555. case DeclarationKind::ImplDeclaration: {
  556. const auto& impl = cast<ImplDeclaration>(declaration);
  557. auto* impl_proto = declaration_proto.mutable_impl();
  558. switch (impl.kind()) {
  559. case ImplKind::InternalImpl:
  560. impl_proto->set_kind(Fuzzing::ImplDeclaration::InternalImpl);
  561. break;
  562. case ImplKind::ExternalImpl:
  563. impl_proto->set_kind(Fuzzing::ImplDeclaration::ExternalImpl);
  564. break;
  565. }
  566. *impl_proto->mutable_impl_type() = ExpressionToProto(*impl.impl_type());
  567. *impl_proto->mutable_interface() = ExpressionToProto(impl.interface());
  568. for (const auto& member : impl.members()) {
  569. *impl_proto->add_members() = DeclarationToProto(*member);
  570. }
  571. break;
  572. }
  573. case DeclarationKind::SelfDeclaration: {
  574. CARBON_FATAL() << "Unreachable SelfDeclaration in DeclarationToProto().";
  575. }
  576. case DeclarationKind::AliasDeclaration: {
  577. const auto& alias = cast<AliasDeclaration>(declaration);
  578. auto* alias_proto = declaration_proto.mutable_alias();
  579. alias_proto->set_name(alias.name());
  580. *alias_proto->mutable_target() = ExpressionToProto(alias.target());
  581. break;
  582. }
  583. }
  584. return declaration_proto;
  585. }
  586. Fuzzing::CompilationUnit AstToProto(const AST& ast) {
  587. Fuzzing::CompilationUnit compilation_unit;
  588. *compilation_unit.mutable_package_statement() =
  589. LibraryNameToProto(ast.package);
  590. compilation_unit.set_is_api(ast.is_api);
  591. for (const Declaration* declaration : ast.declarations) {
  592. *compilation_unit.add_declarations() = DeclarationToProto(*declaration);
  593. }
  594. return compilation_unit;
  595. }
  596. } // namespace Carbon