ast_to_proto.cpp 30 KB

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