ast_to_proto.cpp 28 KB

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