ast_to_proto.cpp 25 KB

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