ast_to_proto.cpp 29 KB

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