ast_to_proto.cpp 32 KB

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