ast_to_proto.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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::Testing {
  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::ImplsWhereClause: {
  236. auto* impls_proto = clause_proto.mutable_impls();
  237. *impls_proto->mutable_type() =
  238. ExpressionToProto(cast<ImplsWhereClause>(where)->type());
  239. *impls_proto->mutable_constraint() =
  240. ExpressionToProto(cast<ImplsWhereClause>(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::TypeTypeLiteral:
  310. expression_proto.mutable_type_type_literal();
  311. break;
  312. case ExpressionKind::UnimplementedExpression:
  313. expression_proto.mutable_unimplemented_expression();
  314. break;
  315. case ExpressionKind::ArrayTypeLiteral: {
  316. const auto& array_literal = cast<ArrayTypeLiteral>(expression);
  317. Fuzzing::ArrayTypeLiteral* array_literal_proto =
  318. expression_proto.mutable_array_type_literal();
  319. *array_literal_proto->mutable_element_type() =
  320. ExpressionToProto(array_literal.element_type_expression());
  321. *array_literal_proto->mutable_size() =
  322. ExpressionToProto(array_literal.size_expression());
  323. break;
  324. }
  325. }
  326. return expression_proto;
  327. }
  328. static auto BindingPatternToProto(const BindingPattern& pattern)
  329. -> Fuzzing::BindingPattern {
  330. Fuzzing::BindingPattern pattern_proto;
  331. pattern_proto.set_name(pattern.name());
  332. *pattern_proto.mutable_type() = PatternToProto(pattern.type());
  333. return pattern_proto;
  334. }
  335. static auto GenericBindingToProto(const GenericBinding& binding)
  336. -> Fuzzing::GenericBinding {
  337. Fuzzing::GenericBinding binding_proto;
  338. binding_proto.set_name(binding.name());
  339. *binding_proto.mutable_type() = ExpressionToProto(binding.type());
  340. switch (binding.binding_kind()) {
  341. case GenericBinding::BindingKind::Checked:
  342. binding_proto.set_kind(Fuzzing::GenericBinding::Checked);
  343. break;
  344. case GenericBinding::BindingKind::Template:
  345. binding_proto.set_kind(Fuzzing::GenericBinding::Template);
  346. break;
  347. }
  348. return binding_proto;
  349. }
  350. static auto TuplePatternToProto(const TuplePattern& tuple_pattern)
  351. -> Fuzzing::TuplePattern {
  352. Fuzzing::TuplePattern tuple_pattern_proto;
  353. for (Nonnull<const Pattern*> field : tuple_pattern.fields()) {
  354. *tuple_pattern_proto.add_fields() = PatternToProto(*field);
  355. }
  356. return tuple_pattern_proto;
  357. }
  358. static auto PatternToProto(const Pattern& pattern) -> Fuzzing::Pattern {
  359. Fuzzing::Pattern pattern_proto;
  360. switch (pattern.kind()) {
  361. case PatternKind::GenericBinding: {
  362. const auto& binding = cast<GenericBinding>(pattern);
  363. *pattern_proto.mutable_generic_binding() = GenericBindingToProto(binding);
  364. break;
  365. }
  366. case PatternKind::BindingPattern: {
  367. const auto& binding = cast<BindingPattern>(pattern);
  368. *pattern_proto.mutable_binding_pattern() = BindingPatternToProto(binding);
  369. break;
  370. }
  371. case PatternKind::TuplePattern:
  372. *pattern_proto.mutable_tuple_pattern() =
  373. TuplePatternToProto(cast<TuplePattern>(pattern));
  374. break;
  375. case PatternKind::AlternativePattern: {
  376. const auto& alternative = cast<AlternativePattern>(pattern);
  377. auto* alternative_proto = pattern_proto.mutable_alternative_pattern();
  378. alternative_proto->set_alternative_name(alternative.alternative_name());
  379. *alternative_proto->mutable_choice_type() =
  380. ExpressionToProto(alternative.choice_type());
  381. *alternative_proto->mutable_arguments() =
  382. TuplePatternToProto(alternative.arguments());
  383. break;
  384. }
  385. case PatternKind::ExpressionPattern:
  386. *pattern_proto.mutable_expression_pattern()->mutable_expression() =
  387. ExpressionToProto(cast<ExpressionPattern>(pattern).expression());
  388. break;
  389. case PatternKind::AutoPattern:
  390. pattern_proto.mutable_auto_pattern();
  391. break;
  392. case PatternKind::VarPattern:
  393. *pattern_proto.mutable_var_pattern()->mutable_pattern() =
  394. PatternToProto(cast<VarPattern>(pattern).pattern());
  395. break;
  396. case PatternKind::AddrPattern:
  397. *pattern_proto.mutable_addr_pattern()->mutable_binding_pattern() =
  398. BindingPatternToProto(cast<AddrPattern>(pattern).binding());
  399. break;
  400. }
  401. return pattern_proto;
  402. }
  403. static auto BlockStatementToProto(const Block& block)
  404. -> Fuzzing::BlockStatement {
  405. Fuzzing::BlockStatement block_proto;
  406. for (Nonnull<const Statement*> statement : block.statements()) {
  407. *block_proto.add_statements() = StatementToProto(*statement);
  408. }
  409. return block_proto;
  410. }
  411. static auto StatementToProto(const Statement& statement) -> Fuzzing::Statement {
  412. Fuzzing::Statement statement_proto;
  413. switch (statement.kind()) {
  414. case StatementKind::ExpressionStatement:
  415. *statement_proto.mutable_expression_statement()->mutable_expression() =
  416. ExpressionToProto(cast<ExpressionStatement>(statement).expression());
  417. break;
  418. case StatementKind::Assign: {
  419. const auto& assign = cast<Assign>(statement);
  420. auto* assign_proto = statement_proto.mutable_assign();
  421. *assign_proto->mutable_lhs() = ExpressionToProto(assign.lhs());
  422. *assign_proto->mutable_rhs() = ExpressionToProto(assign.rhs());
  423. assign_proto->set_op(AssignOperatorToProtoEnum(assign.op()));
  424. break;
  425. }
  426. case StatementKind::IncrementDecrement: {
  427. const auto& inc_dec = cast<IncrementDecrement>(statement);
  428. auto* inc_dec_proto = statement_proto.mutable_inc_dec();
  429. *inc_dec_proto->mutable_operand() = ExpressionToProto(inc_dec.argument());
  430. inc_dec_proto->set_is_increment(inc_dec.is_increment());
  431. break;
  432. }
  433. case StatementKind::VariableDefinition: {
  434. const auto& def = cast<VariableDefinition>(statement);
  435. auto* def_proto = statement_proto.mutable_variable_definition();
  436. *def_proto->mutable_pattern() = PatternToProto(def.pattern());
  437. if (def.has_init()) {
  438. *def_proto->mutable_init() = ExpressionToProto(def.init());
  439. }
  440. def_proto->set_is_returned(def.is_returned());
  441. break;
  442. }
  443. case StatementKind::If: {
  444. const auto& if_stmt = cast<If>(statement);
  445. auto* if_proto = statement_proto.mutable_if_statement();
  446. *if_proto->mutable_condition() = ExpressionToProto(if_stmt.condition());
  447. *if_proto->mutable_then_block() =
  448. BlockStatementToProto(if_stmt.then_block());
  449. if (if_stmt.else_block().has_value()) {
  450. *if_proto->mutable_else_block() =
  451. BlockStatementToProto(**if_stmt.else_block());
  452. }
  453. break;
  454. }
  455. case StatementKind::ReturnVar: {
  456. statement_proto.mutable_return_var_statement();
  457. break;
  458. }
  459. case StatementKind::ReturnExpression: {
  460. const auto& ret = cast<ReturnExpression>(statement);
  461. auto* ret_proto = statement_proto.mutable_return_expression_statement();
  462. if (!ret.is_omitted_expression()) {
  463. *ret_proto->mutable_expression() = ExpressionToProto(ret.expression());
  464. } else {
  465. ret_proto->set_is_omitted_expression(true);
  466. }
  467. break;
  468. }
  469. case StatementKind::Block:
  470. *statement_proto.mutable_block() =
  471. BlockStatementToProto(cast<Block>(statement));
  472. break;
  473. case StatementKind::While: {
  474. const auto& while_stmt = cast<While>(statement);
  475. auto* while_proto = statement_proto.mutable_while_statement();
  476. *while_proto->mutable_condition() =
  477. ExpressionToProto(while_stmt.condition());
  478. *while_proto->mutable_body() = BlockStatementToProto(while_stmt.body());
  479. break;
  480. }
  481. case StatementKind::Match: {
  482. const auto& match = cast<Match>(statement);
  483. auto* match_proto = statement_proto.mutable_match();
  484. *match_proto->mutable_expression() =
  485. ExpressionToProto(match.expression());
  486. for (const Match::Clause& clause : match.clauses()) {
  487. auto* clause_proto = match_proto->add_clauses();
  488. // TODO: Working out whether we have a default clause after the fact
  489. // like this is fragile.
  490. bool is_default_clause = false;
  491. if (const auto* binding = dyn_cast<BindingPattern>(&clause.pattern())) {
  492. if (binding->name() == AnonymousName &&
  493. isa<AutoPattern>(binding->type()) &&
  494. binding->source_loc() == binding->type().source_loc()) {
  495. is_default_clause = true;
  496. }
  497. }
  498. if (is_default_clause) {
  499. clause_proto->set_is_default(true);
  500. } else {
  501. *clause_proto->mutable_pattern() = PatternToProto(clause.pattern());
  502. }
  503. *clause_proto->mutable_statement() =
  504. StatementToProto(clause.statement());
  505. }
  506. break;
  507. }
  508. case StatementKind::Break:
  509. // Initializes with the default value; there's nothing to set.
  510. statement_proto.mutable_break_statement();
  511. break;
  512. case StatementKind::Continue:
  513. // Initializes with the default value; there's nothing to set.
  514. statement_proto.mutable_continue_statement();
  515. break;
  516. case StatementKind::For: {
  517. const auto& for_stmt = cast<For>(statement);
  518. auto* for_proto = statement_proto.mutable_for_statement();
  519. *for_proto->mutable_var_decl() =
  520. BindingPatternToProto(for_stmt.variable_declaration());
  521. *for_proto->mutable_target() = ExpressionToProto(for_stmt.loop_target());
  522. *for_proto->mutable_body() = BlockStatementToProto(for_stmt.body());
  523. break;
  524. }
  525. }
  526. return statement_proto;
  527. }
  528. static auto ReturnTermToProto(const ReturnTerm& return_term)
  529. -> Fuzzing::ReturnTerm {
  530. Fuzzing::ReturnTerm return_term_proto;
  531. if (return_term.is_omitted()) {
  532. return_term_proto.set_kind(Fuzzing::ReturnTerm::Omitted);
  533. } else if (return_term.is_auto()) {
  534. return_term_proto.set_kind(Fuzzing::ReturnTerm::Auto);
  535. } else {
  536. return_term_proto.set_kind(Fuzzing::ReturnTerm::Expression);
  537. *return_term_proto.mutable_type() =
  538. ExpressionToProto(**return_term.type_expression());
  539. }
  540. return return_term_proto;
  541. }
  542. static auto DeclaredNameToProto(const DeclaredName& name)
  543. -> Fuzzing::DeclaredName {
  544. Fuzzing::DeclaredName name_proto;
  545. name_proto.set_name(std::string(name.inner_name()));
  546. for (const auto& [loc, qual] : name.qualifiers()) {
  547. name_proto.add_qualifiers(qual);
  548. }
  549. return name_proto;
  550. }
  551. static auto DeclarationToProto(const Declaration& declaration)
  552. -> Fuzzing::Declaration {
  553. Fuzzing::Declaration declaration_proto;
  554. switch (declaration.kind()) {
  555. case DeclarationKind::NamespaceDeclaration: {
  556. const auto& namespace_decl = cast<NamespaceDeclaration>(declaration);
  557. auto* namespace_proto = declaration_proto.mutable_namespace_();
  558. *namespace_proto->mutable_name() =
  559. DeclaredNameToProto(namespace_decl.name());
  560. break;
  561. }
  562. case DeclarationKind::DestructorDeclaration: {
  563. const auto& function = cast<DestructorDeclaration>(declaration);
  564. auto* function_proto = declaration_proto.mutable_destructor();
  565. if (function.is_method()) {
  566. switch (function.self_pattern().kind()) {
  567. case PatternKind::AddrPattern:
  568. *function_proto->mutable_self_pattern() =
  569. PatternToProto(cast<AddrPattern>(function.self_pattern()));
  570. break;
  571. case PatternKind::BindingPattern:
  572. *function_proto->mutable_self_pattern() =
  573. PatternToProto(cast<BindingPattern>(function.self_pattern()));
  574. break;
  575. default:
  576. // Parser shouldn't allow self_pattern to be anything other than
  577. // AddrPattern or BindingPattern
  578. CARBON_FATAL()
  579. << "self_pattern in method declaration can be either "
  580. "AddrPattern or BindingPattern. Actual pattern: "
  581. << function.self_pattern();
  582. break;
  583. }
  584. }
  585. if (function.body().has_value()) {
  586. *function_proto->mutable_body() =
  587. BlockStatementToProto(**function.body());
  588. }
  589. break;
  590. }
  591. case DeclarationKind::FunctionDeclaration: {
  592. const auto& function = cast<FunctionDeclaration>(declaration);
  593. auto* function_proto = declaration_proto.mutable_function();
  594. *function_proto->mutable_name() = DeclaredNameToProto(function.name());
  595. for (Nonnull<const GenericBinding*> binding :
  596. function.deduced_parameters()) {
  597. *function_proto->add_deduced_parameters() =
  598. GenericBindingToProto(*binding);
  599. }
  600. if (function.is_method()) {
  601. switch (function.self_pattern().kind()) {
  602. case PatternKind::AddrPattern:
  603. *function_proto->mutable_self_pattern() =
  604. PatternToProto(cast<AddrPattern>(function.self_pattern()));
  605. break;
  606. case PatternKind::BindingPattern:
  607. *function_proto->mutable_self_pattern() =
  608. PatternToProto(cast<BindingPattern>(function.self_pattern()));
  609. break;
  610. default:
  611. // Parser shouldn't allow self_pattern to be anything other than
  612. // AddrPattern or BindingPattern
  613. CARBON_FATAL()
  614. << "self_pattern in method declaration can be either "
  615. "AddrPattern or BindingPattern. Actual pattern: "
  616. << function.self_pattern();
  617. break;
  618. }
  619. }
  620. *function_proto->mutable_param_pattern() =
  621. TuplePatternToProto(function.param_pattern());
  622. *function_proto->mutable_return_term() =
  623. ReturnTermToProto(function.return_term());
  624. if (function.body().has_value()) {
  625. *function_proto->mutable_body() =
  626. BlockStatementToProto(**function.body());
  627. }
  628. break;
  629. }
  630. case DeclarationKind::ClassDeclaration: {
  631. const auto& class_decl = cast<ClassDeclaration>(declaration);
  632. auto* class_proto = declaration_proto.mutable_class_declaration();
  633. *class_proto->mutable_name() = DeclaredNameToProto(class_decl.name());
  634. if (class_decl.type_params().has_value()) {
  635. *class_proto->mutable_type_params() =
  636. TuplePatternToProto(**class_decl.type_params());
  637. }
  638. for (Nonnull<const Declaration*> member : class_decl.members()) {
  639. *class_proto->add_members() = DeclarationToProto(*member);
  640. }
  641. break;
  642. }
  643. case DeclarationKind::MixinDeclaration: {
  644. const auto& mixin = cast<MixinDeclaration>(declaration);
  645. auto* mixin_proto = declaration_proto.mutable_mixin();
  646. *mixin_proto->mutable_name() = DeclaredNameToProto(mixin.name());
  647. for (const auto& member : mixin.members()) {
  648. *mixin_proto->add_members() = DeclarationToProto(*member);
  649. }
  650. // Type params not implemented yet
  651. // if (mixin.params().has_value()) {
  652. // *mixin_proto->mutable_params() =
  653. // TuplePatternToProto(**mixin.params());
  654. //}
  655. *mixin_proto->mutable_self() = GenericBindingToProto(*mixin.self());
  656. break;
  657. }
  658. case DeclarationKind::MixDeclaration: {
  659. const auto& mix = cast<MixDeclaration>(declaration);
  660. auto* mix_proto = declaration_proto.mutable_mix();
  661. *mix_proto->mutable_mixin() = ExpressionToProto(mix.mixin());
  662. break;
  663. }
  664. case DeclarationKind::ChoiceDeclaration: {
  665. const auto& choice = cast<ChoiceDeclaration>(declaration);
  666. auto* choice_proto = declaration_proto.mutable_choice();
  667. *choice_proto->mutable_name() = DeclaredNameToProto(choice.name());
  668. for (Nonnull<const AlternativeSignature*> alternative :
  669. choice.alternatives()) {
  670. auto* alternative_proto = choice_proto->add_alternatives();
  671. alternative_proto->set_name(alternative->name());
  672. if (auto params = alternative->parameters()) {
  673. *alternative_proto->mutable_signature() =
  674. TupleLiteralExpressionToProto(**params);
  675. }
  676. }
  677. break;
  678. }
  679. case DeclarationKind::VariableDeclaration: {
  680. const auto& var = cast<VariableDeclaration>(declaration);
  681. auto* var_proto = declaration_proto.mutable_variable();
  682. *var_proto->mutable_binding() = BindingPatternToProto(var.binding());
  683. if (var.has_initializer()) {
  684. *var_proto->mutable_initializer() =
  685. ExpressionToProto(var.initializer());
  686. }
  687. break;
  688. }
  689. case DeclarationKind::InterfaceExtendsDeclaration: {
  690. const auto& extends = cast<InterfaceExtendsDeclaration>(declaration);
  691. auto* extends_proto = declaration_proto.mutable_interface_extends();
  692. *extends_proto->mutable_base() = ExpressionToProto(*extends.base());
  693. break;
  694. }
  695. case DeclarationKind::InterfaceImplDeclaration: {
  696. const auto& impl = cast<InterfaceImplDeclaration>(declaration);
  697. auto* impl_proto = declaration_proto.mutable_interface_impl();
  698. *impl_proto->mutable_impl_type() = ExpressionToProto(*impl.impl_type());
  699. *impl_proto->mutable_constraint() = ExpressionToProto(*impl.constraint());
  700. break;
  701. }
  702. case DeclarationKind::AssociatedConstantDeclaration: {
  703. const auto& assoc = cast<AssociatedConstantDeclaration>(declaration);
  704. auto* let_proto = declaration_proto.mutable_let();
  705. *let_proto->mutable_pattern() = PatternToProto(assoc.binding());
  706. break;
  707. }
  708. case DeclarationKind::InterfaceDeclaration: {
  709. const auto& interface = cast<InterfaceDeclaration>(declaration);
  710. auto* interface_proto = declaration_proto.mutable_interface();
  711. *interface_proto->mutable_name() = DeclaredNameToProto(interface.name());
  712. for (const auto& member : interface.members()) {
  713. *interface_proto->add_members() = DeclarationToProto(*member);
  714. }
  715. break;
  716. }
  717. case DeclarationKind::ConstraintDeclaration: {
  718. const auto& constraint = cast<ConstraintDeclaration>(declaration);
  719. auto* constraint_proto = declaration_proto.mutable_constraint();
  720. *constraint_proto->mutable_name() =
  721. DeclaredNameToProto(constraint.name());
  722. for (const auto& member : constraint.members()) {
  723. *constraint_proto->add_members() = DeclarationToProto(*member);
  724. }
  725. break;
  726. }
  727. case DeclarationKind::ImplDeclaration: {
  728. const auto& impl = cast<ImplDeclaration>(declaration);
  729. auto* impl_proto = declaration_proto.mutable_impl();
  730. switch (impl.kind()) {
  731. case ImplKind::InternalImpl:
  732. impl_proto->set_kind(Fuzzing::ImplDeclaration::InternalImpl);
  733. break;
  734. case ImplKind::ExternalImpl:
  735. impl_proto->set_kind(Fuzzing::ImplDeclaration::ExternalImpl);
  736. break;
  737. }
  738. for (Nonnull<const GenericBinding*> binding : impl.deduced_parameters()) {
  739. *impl_proto->add_deduced_parameters() = GenericBindingToProto(*binding);
  740. }
  741. *impl_proto->mutable_impl_type() = ExpressionToProto(*impl.impl_type());
  742. *impl_proto->mutable_interface() = ExpressionToProto(impl.interface());
  743. for (const auto& member : impl.members()) {
  744. *impl_proto->add_members() = DeclarationToProto(*member);
  745. }
  746. break;
  747. }
  748. case DeclarationKind::MatchFirstDeclaration: {
  749. const auto& match_first = cast<MatchFirstDeclaration>(declaration);
  750. auto* match_first_proto = declaration_proto.mutable_match_first();
  751. for (const auto* impl : match_first.impl_declarations()) {
  752. *match_first_proto->add_impl_declarations() = DeclarationToProto(*impl);
  753. }
  754. break;
  755. }
  756. case DeclarationKind::SelfDeclaration: {
  757. CARBON_FATAL() << "Unreachable SelfDeclaration in DeclarationToProto().";
  758. }
  759. case DeclarationKind::AliasDeclaration: {
  760. const auto& alias = cast<AliasDeclaration>(declaration);
  761. auto* alias_proto = declaration_proto.mutable_alias();
  762. *alias_proto->mutable_name() = DeclaredNameToProto(alias.name());
  763. *alias_proto->mutable_target() = ExpressionToProto(alias.target());
  764. break;
  765. }
  766. }
  767. return declaration_proto;
  768. }
  769. auto AstToProto(const AST& ast) -> Fuzzing::Carbon {
  770. Fuzzing::Carbon carbon;
  771. auto* unit = carbon.mutable_compilation_unit();
  772. *unit->mutable_package_statement() = LibraryNameToProto(ast.package);
  773. unit->set_is_api(ast.is_api);
  774. for (const Declaration* declaration : ast.declarations) {
  775. *unit->add_declarations() = DeclarationToProto(*declaration);
  776. }
  777. return carbon;
  778. }
  779. } // namespace Carbon::Testing