ast_to_proto.cpp 25 KB

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