ast_to_proto.cpp 30 KB

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