typed_nodes.h 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  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. #ifndef CARBON_TOOLCHAIN_PARSE_TYPED_NODES_H_
  5. #define CARBON_TOOLCHAIN_PARSE_TYPED_NODES_H_
  6. #include <optional>
  7. #include "toolchain/lex/token_index.h"
  8. #include "toolchain/parse/node_ids.h"
  9. #include "toolchain/parse/node_kind.h"
  10. namespace Carbon::Parse {
  11. // Helpers for defining different kinds of parse nodes.
  12. // ----------------------------------------------------
  13. // A pair of a list item and its optional following comma.
  14. template <typename Element, typename Comma>
  15. struct ListItem {
  16. Element value;
  17. std::optional<Comma> comma;
  18. };
  19. // A list of items, parameterized by the kind of the elements and comma.
  20. template <typename Element, typename Comma>
  21. using CommaSeparatedList = llvm::SmallVector<ListItem<Element, Comma>>;
  22. // This class provides a shorthand for defining parse node kinds for leaf nodes.
  23. template <const NodeKind& KindT, typename TokenKind,
  24. NodeCategory::RawEnumType Category = NodeCategory::None>
  25. struct LeafNode {
  26. static constexpr auto Kind =
  27. KindT.Define({.category = Category, .child_count = 0});
  28. TokenKind token;
  29. };
  30. // ----------------------------------------------------------------------------
  31. // Each node kind (in node_kind.def) should have a corresponding type defined
  32. // here which describes the expected child structure of that parse node.
  33. //
  34. // Each of these types should start with a `static constexpr Kind` member
  35. // initialized by calling `Define` on the corresponding `NodeKind`, and passing
  36. // in the `NodeCategory` of that kind. This will both associate the category
  37. // with the node kind and create the necessary kind object for the typed node.
  38. //
  39. // This should be followed by field declarations that describe the child nodes,
  40. // in order, that occur in the parse tree. The `Extract...` functions on the
  41. // parse tree use struct reflection on these fields to guide the extraction of
  42. // the child nodes from the tree into an object of this type with these fields
  43. // for convenient access.
  44. //
  45. // The types of these fields are special and describe the specific child node
  46. // structure of the parse node. Many of these types are defined in `node_ids.h`.
  47. //
  48. // Valid primitive types here are:
  49. // - `NodeId` to match any single child node
  50. // - `FooId` to require that child to have kind `NodeKind::Foo`
  51. // - `AnyCatId` to require that child to have a kind in category `Cat`
  52. // - `NodeIdOneOf<A, B>` to require the child to have kind `NodeKind::A` or
  53. // `NodeKind::B`
  54. // - `NodeIdNot<A>` to match any single child whose kind is not `NodeKind::A`
  55. //
  56. // There a few, restricted composite field types allowed that compose types in
  57. // various ways, where all of the `T`s and `U`s below are themselves valid field
  58. // types:
  59. // - `llvm::SmallVector<T>` to match any number of children matching `T`
  60. // - `std::optional<T>` to match 0 or 1 children matching `T`
  61. // - `std::tuple<T...>` to match children matching `T...`
  62. // - Any provided `Aggregate` type that is a simple aggregate type such as
  63. // `struct Aggregate { T x; U y; }`,
  64. // to match children with types `T` and `U`.
  65. //
  66. // In addition to the fields describing the child nodes, each parse node should
  67. // also have exactly one field that describes the token corresponding to the
  68. // parse node itself. This field should have the name `token`. The type of the
  69. // field should be `Lex::*TokenIndex`, describing the kind of the token, such as
  70. // `Lex::SemiTokenIndex` for a `;` token. If the parse node can correspond to
  71. // any kind of token, `Lex::TokenIndex` can be used instead, but should only be
  72. // used when the node kind is either not used in a finished tree, such as
  73. // `Placeholder`, or is always invalid, such as `InvalidParse`. The location of
  74. // the field relative to the child nodes indicates the location within the
  75. // corresponding grammar production where the token appears.
  76. // ----------------------------------------------------------------------------
  77. // Error nodes
  78. // -----------
  79. // An invalid parse. Used to balance the parse tree. This type is here only to
  80. // ensure we have a type for each parse node kind. This node kind always has an
  81. // error, so can never be extracted.
  82. using InvalidParse = LeafNode<NodeKind::InvalidParse, Lex::TokenIndex,
  83. NodeCategory::Decl | NodeCategory::Expr>;
  84. // An invalid subtree. Always has an error so can never be extracted.
  85. using InvalidParseStart =
  86. LeafNode<NodeKind::InvalidParseStart, Lex::TokenIndex>;
  87. struct InvalidParseSubtree {
  88. static constexpr auto Kind = NodeKind::InvalidParseSubtree.Define(
  89. {.category = NodeCategory::Decl,
  90. .bracketed_by = InvalidParseStart::Kind});
  91. InvalidParseStartId start;
  92. llvm::SmallVector<NodeIdNot<InvalidParseStart>> extra;
  93. Lex::TokenIndex token;
  94. };
  95. // A placeholder node to be replaced; it will never exist in a valid parse tree.
  96. // Its token kind is not enforced even when valid.
  97. using Placeholder = LeafNode<NodeKind::Placeholder, Lex::TokenIndex>;
  98. // File nodes
  99. // ----------
  100. // The start of the file.
  101. using FileStart = LeafNode<NodeKind::FileStart, Lex::FileStartTokenIndex>;
  102. // The end of the file.
  103. using FileEnd = LeafNode<NodeKind::FileEnd, Lex::FileEndTokenIndex>;
  104. // General-purpose nodes
  105. // ---------------------
  106. // An empty declaration, such as `;`.
  107. using EmptyDecl =
  108. LeafNode<NodeKind::EmptyDecl, Lex::SemiTokenIndex, NodeCategory::Decl>;
  109. // A name in a non-expression context, such as a declaration, that is known
  110. // to be followed by parameters.
  111. using IdentifierNameBeforeParams =
  112. LeafNode<NodeKind::IdentifierNameBeforeParams, Lex::IdentifierTokenIndex,
  113. NodeCategory::MemberName | NodeCategory::NonExprName>;
  114. // A name in a non-expression context, such as a declaration, that is known
  115. // to not be followed by parameters.
  116. using IdentifierNameNotBeforeParams =
  117. LeafNode<NodeKind::IdentifierNameNotBeforeParams, Lex::IdentifierTokenIndex,
  118. NodeCategory::MemberName | NodeCategory::NonExprName>;
  119. // A name in an expression context.
  120. using IdentifierNameExpr =
  121. LeafNode<NodeKind::IdentifierNameExpr, Lex::IdentifierTokenIndex,
  122. NodeCategory::Expr>;
  123. // The `self` value and `Self` type identifier keywords. Typically of the form
  124. // `self: Self`.
  125. using SelfValueName =
  126. LeafNode<NodeKind::SelfValueName, Lex::SelfValueIdentifierTokenIndex>;
  127. using SelfValueNameExpr =
  128. LeafNode<NodeKind::SelfValueNameExpr, Lex::SelfValueIdentifierTokenIndex,
  129. NodeCategory::Expr>;
  130. using SelfTypeNameExpr =
  131. LeafNode<NodeKind::SelfTypeNameExpr, Lex::SelfTypeIdentifierTokenIndex,
  132. NodeCategory::Expr>;
  133. // The `base` value keyword, introduced by `base: B`. Typically referenced in
  134. // an expression, as in `x.base` or `{.base = ...}`, but can also be used as a
  135. // declared name, as in `{.base: partial B}`.
  136. using BaseName =
  137. LeafNode<NodeKind::BaseName, Lex::BaseTokenIndex, NodeCategory::MemberName>;
  138. // The `_` token, when used in the name position of a binding pattern.
  139. using UnderscoreName =
  140. LeafNode<NodeKind::UnderscoreName, Lex::UnderscoreTokenIndex,
  141. NodeCategory::NonExprName>;
  142. // A name qualifier with parameters, such as `A(T:! type).` or `A[T:! type](N:!
  143. // T).`.
  144. struct IdentifierNameQualifierWithParams {
  145. static constexpr auto Kind =
  146. NodeKind::IdentifierNameQualifierWithParams.Define(
  147. {.bracketed_by = IdentifierNameBeforeParams::Kind});
  148. IdentifierNameBeforeParamsId name;
  149. std::optional<ImplicitParamListId> implicit_params;
  150. std::optional<ExplicitParamListId> params;
  151. Lex::PeriodTokenIndex token;
  152. };
  153. // A name qualifier without parameters, such as `A.`.
  154. struct IdentifierNameQualifierWithoutParams {
  155. static constexpr auto Kind =
  156. NodeKind::IdentifierNameQualifierWithoutParams.Define(
  157. {.bracketed_by = IdentifierNameNotBeforeParams::Kind});
  158. IdentifierNameNotBeforeParamsId name;
  159. Lex::PeriodTokenIndex token;
  160. };
  161. // A complete name in a declaration: `A.C(T:! type).F(n: i32)`.
  162. // Note that this includes the parameters of the entity itself.
  163. struct DeclName {
  164. llvm::SmallVector<NodeIdOneOf<IdentifierNameQualifierWithParams,
  165. IdentifierNameQualifierWithoutParams>>
  166. qualifiers;
  167. AnyNonExprNameId name;
  168. std::optional<ImplicitParamListId> implicit_params;
  169. std::optional<ExplicitParamListId> params;
  170. };
  171. // Library, package, import, export
  172. // --------------------------------
  173. // Various keywords in an expression.
  174. using CoreNameExpr =
  175. LeafNode<NodeKind::CoreNameExpr, Lex::CoreTokenIndex, NodeCategory::Expr>;
  176. using CppNameExpr =
  177. LeafNode<NodeKind::CppNameExpr, Lex::CppTokenIndex, NodeCategory::Expr>;
  178. using PackageExpr =
  179. LeafNode<NodeKind::PackageExpr, Lex::PackageTokenIndex, NodeCategory::Expr>;
  180. // The name of a package or library for `package`, `import`, and `library`.
  181. using IdentifierPackageName =
  182. LeafNode<NodeKind::IdentifierPackageName, Lex::IdentifierTokenIndex,
  183. NodeCategory::PackageName>;
  184. using CorePackageName = LeafNode<NodeKind::CorePackageName, Lex::CoreTokenIndex,
  185. NodeCategory::PackageName>;
  186. using CppPackageName = LeafNode<NodeKind::CppPackageName, Lex::CppTokenIndex,
  187. NodeCategory::PackageName>;
  188. using LibraryName =
  189. LeafNode<NodeKind::LibraryName, Lex::StringLiteralTokenIndex>;
  190. using DefaultLibrary =
  191. LeafNode<NodeKind::DefaultLibrary, Lex::DefaultTokenIndex>;
  192. using PackageIntroducer =
  193. LeafNode<NodeKind::PackageIntroducer, Lex::PackageTokenIndex>;
  194. // `library` in `package` or `import`.
  195. struct LibrarySpecifier {
  196. static constexpr auto Kind =
  197. NodeKind::LibrarySpecifier.Define({.child_count = 1});
  198. Lex::LibraryTokenIndex token;
  199. NodeIdOneOf<LibraryName, DefaultLibrary> name;
  200. };
  201. using InlineImportBody =
  202. LeafNode<NodeKind::InlineImportBody, Lex::StringLiteralTokenIndex>;
  203. // `inline` in `import`.
  204. struct InlineImportSpecifier {
  205. static constexpr auto Kind =
  206. NodeKind::InlineImportSpecifier.Define({.child_count = 1});
  207. Lex::InlineTokenIndex token;
  208. InlineImportBodyId body;
  209. };
  210. // First line of the file, such as:
  211. // `impl package MyPackage library "MyLibrary";`
  212. struct PackageDecl {
  213. static constexpr auto Kind =
  214. NodeKind::PackageDecl.Define({.category = NodeCategory::Decl,
  215. .bracketed_by = PackageIntroducer::Kind});
  216. PackageIntroducerId introducer;
  217. llvm::SmallVector<AnyModifierId> modifiers;
  218. AnyPackageNameId name;
  219. std::optional<LibrarySpecifierId> library;
  220. Lex::SemiTokenIndex token;
  221. };
  222. // `import [TheirPackage] [library "TheirLibrary" | inline "code"];`
  223. using ImportIntroducer =
  224. LeafNode<NodeKind::ImportIntroducer, Lex::ImportTokenIndex>;
  225. struct ImportDecl {
  226. static constexpr auto Kind = NodeKind::ImportDecl.Define(
  227. {.category = NodeCategory::Decl, .bracketed_by = ImportIntroducer::Kind});
  228. ImportIntroducerId introducer;
  229. llvm::SmallVector<AnyModifierId> modifiers;
  230. std::optional<AnyPackageNameId> name;
  231. std::optional<LibrarySpecifierId> library;
  232. std::optional<InlineImportSpecifierId> inline_specifier;
  233. Lex::SemiTokenIndex token;
  234. };
  235. // `library` as declaration.
  236. using LibraryIntroducer =
  237. LeafNode<NodeKind::LibraryIntroducer, Lex::LibraryTokenIndex>;
  238. struct LibraryDecl {
  239. static constexpr auto Kind =
  240. NodeKind::LibraryDecl.Define({.category = NodeCategory::Decl,
  241. .bracketed_by = LibraryIntroducer::Kind});
  242. LibraryIntroducerId introducer;
  243. llvm::SmallVector<AnyModifierId> modifiers;
  244. NodeIdOneOf<LibraryName, DefaultLibrary> library_name;
  245. Lex::SemiTokenIndex token;
  246. };
  247. // `export` as a declaration.
  248. using ExportIntroducer =
  249. LeafNode<NodeKind::ExportIntroducer, Lex::ExportTokenIndex>;
  250. struct ExportDecl {
  251. static constexpr auto Kind = NodeKind::ExportDecl.Define(
  252. {.category = NodeCategory::Decl, .bracketed_by = ExportIntroducer::Kind});
  253. ExportIntroducerId introducer;
  254. llvm::SmallVector<AnyModifierId> modifiers;
  255. DeclName name;
  256. Lex::SemiTokenIndex token;
  257. };
  258. // Namespace nodes
  259. // ---------------
  260. using NamespaceStart =
  261. LeafNode<NodeKind::NamespaceStart, Lex::NamespaceTokenIndex>;
  262. // A namespace: `namespace N;`.
  263. struct Namespace {
  264. static constexpr auto Kind = NodeKind::Namespace.Define(
  265. {.category = NodeCategory::Decl, .bracketed_by = NamespaceStart::Kind});
  266. NamespaceStartId introducer;
  267. llvm::SmallVector<AnyModifierId> modifiers;
  268. DeclName name;
  269. Lex::SemiTokenIndex token;
  270. };
  271. // Pattern nodes
  272. // -------------
  273. // An unused pattern: `unused pattern`.
  274. struct UnusedPattern {
  275. static constexpr auto Kind = NodeKind::UnusedPattern.Define(
  276. {.category = NodeCategory::Pattern, .child_count = 1});
  277. Lex::UnusedTokenIndex token;
  278. AnyPatternId inner;
  279. };
  280. // A ref binding name: `ref name`.
  281. struct RefBindingName {
  282. static constexpr auto Kind =
  283. NodeKind::RefBindingName.Define({.child_count = 1});
  284. Lex::RefTokenIndex token;
  285. AnyRuntimeBindingPatternName name;
  286. };
  287. // A binding pattern, such as `name: Type`, that isn't inside a `var` pattern.
  288. struct LetBindingPattern {
  289. static constexpr auto Kind = NodeKind::LetBindingPattern.Define(
  290. {.category = NodeCategory::Pattern, .child_count = 2});
  291. // TODO: is there some way to reuse AnyRuntimeBindingPatternName here?
  292. NodeIdOneOf<IdentifierNameNotBeforeParams, SelfValueName, UnderscoreName,
  293. RefBindingName>
  294. name;
  295. Lex::ColonTokenIndex token;
  296. AnyExprId type;
  297. };
  298. // A binding pattern, such as `name: Type`, that is inside a `var` pattern.
  299. struct VarBindingPattern {
  300. static constexpr auto Kind = NodeKind::VarBindingPattern.Define(
  301. {.category = NodeCategory::Pattern, .child_count = 2});
  302. AnyRuntimeBindingPatternName name;
  303. Lex::ColonTokenIndex token;
  304. AnyExprId type;
  305. };
  306. // A template binding name: `template T`.
  307. struct TemplateBindingName {
  308. static constexpr auto Kind =
  309. NodeKind::TemplateBindingName.Define({.child_count = 1});
  310. Lex::TemplateTokenIndex token;
  311. AnyRuntimeBindingPatternName name;
  312. };
  313. struct CompileTimeBindingPatternStart {
  314. static constexpr auto Kind =
  315. NodeKind::CompileTimeBindingPatternStart.Define({.child_count = 1});
  316. // TODO: is there some way to reuse AnyRuntimeBindingPatternName here?
  317. NodeIdOneOf<IdentifierNameNotBeforeParams, SelfValueName, UnderscoreName,
  318. TemplateBindingName>
  319. name;
  320. // This is a virtual token. The `:!` token is owned by the
  321. // CompileTimeBindingPattern node.
  322. Lex::ColonExclaimTokenIndex token;
  323. };
  324. // `name:! Type`
  325. struct CompileTimeBindingPattern {
  326. static constexpr auto Kind = NodeKind::CompileTimeBindingPattern.Define(
  327. {.category = NodeCategory::Pattern, .child_count = 2});
  328. CompileTimeBindingPatternStartId introducer;
  329. Lex::ColonExclaimTokenIndex token;
  330. AnyExprId type;
  331. };
  332. using TuplePatternStart =
  333. LeafNode<NodeKind::TuplePatternStart, Lex::OpenParenTokenIndex>;
  334. using PatternListComma =
  335. LeafNode<NodeKind::PatternListComma, Lex::CommaTokenIndex>;
  336. // A tuple pattern that isn't an explicit parameter list: `(a: i32, b: i32)`.
  337. struct TuplePattern {
  338. static constexpr auto Kind =
  339. NodeKind::TuplePattern.Define({.category = NodeCategory::Pattern,
  340. .bracketed_by = TuplePatternStart::Kind});
  341. TuplePatternStartId left_paren;
  342. CommaSeparatedList<AnyPatternId, PatternListCommaId> params;
  343. Lex::CloseParenTokenIndex token;
  344. };
  345. using ExplicitParamListStart =
  346. LeafNode<NodeKind::ExplicitParamListStart, Lex::OpenParenTokenIndex>;
  347. // An explicit parameter list: `(a: i32, b: i32)`.
  348. struct ExplicitParamList {
  349. static constexpr auto Kind = NodeKind::ExplicitParamList.Define(
  350. {.bracketed_by = ExplicitParamListStart::Kind});
  351. ExplicitParamListStartId left_paren;
  352. CommaSeparatedList<AnyPatternId, PatternListCommaId> params;
  353. Lex::CloseParenTokenIndex token;
  354. };
  355. using ImplicitParamListStart = LeafNode<NodeKind::ImplicitParamListStart,
  356. Lex::OpenSquareBracketTokenIndex>;
  357. // An implicit parameter list: `[T:! type, self: Self]`.
  358. struct ImplicitParamList {
  359. static constexpr auto Kind = NodeKind::ImplicitParamList.Define(
  360. {.bracketed_by = ImplicitParamListStart::Kind});
  361. ImplicitParamListStartId left_square;
  362. CommaSeparatedList<AnyPatternId, PatternListCommaId> params;
  363. Lex::CloseSquareBracketTokenIndex token;
  364. };
  365. // Function nodes
  366. // --------------
  367. using FunctionIntroducer =
  368. LeafNode<NodeKind::FunctionIntroducer, Lex::FnTokenIndex>;
  369. // A return type: `-> i32`.
  370. struct ReturnType {
  371. static constexpr auto Kind = NodeKind::ReturnType.Define({.child_count = 1});
  372. Lex::MinusGreaterTokenIndex token;
  373. AnyExprId type;
  374. };
  375. // A function signature: `fn F() -> i32`.
  376. template <const NodeKind& KindT, typename TokenKind,
  377. NodeCategory::RawEnumType Category>
  378. struct FunctionSignature {
  379. static constexpr auto Kind = KindT.Define(
  380. {.category = Category, .bracketed_by = FunctionIntroducer::Kind});
  381. FunctionIntroducerId introducer;
  382. llvm::SmallVector<AnyModifierId> modifiers;
  383. DeclName name;
  384. std::optional<ReturnTypeId> return_type;
  385. TokenKind token;
  386. };
  387. using FunctionDecl = FunctionSignature<NodeKind::FunctionDecl,
  388. Lex::SemiTokenIndex, NodeCategory::Decl>;
  389. using FunctionDefinitionStart =
  390. FunctionSignature<NodeKind::FunctionDefinitionStart, Lex::TokenIndex,
  391. NodeCategory::None>;
  392. // A function definition: `fn F() -> i32 { ... }`.
  393. struct FunctionDefinition {
  394. static constexpr auto Kind = NodeKind::FunctionDefinition.Define(
  395. {.category = NodeCategory::Decl,
  396. .bracketed_by = FunctionDefinitionStart::Kind});
  397. FunctionDefinitionStartId signature;
  398. llvm::SmallVector<AnyStatementId> body;
  399. Lex::CloseCurlyBraceTokenIndex token;
  400. };
  401. // A terse function definition: `fn F() -> i32 => expr;`.
  402. struct FunctionTerseDefinition {
  403. static constexpr auto Kind = NodeKind::FunctionTerseDefinition.Define(
  404. {.category = NodeCategory::Decl,
  405. .bracketed_by = FunctionDefinitionStart::Kind});
  406. FunctionDefinitionStartId signature;
  407. TerseBodyArrowId arrow;
  408. AnyExprId body;
  409. Lex::SemiTokenIndex token;
  410. };
  411. using BuiltinFunctionDefinitionStart =
  412. FunctionSignature<NodeKind::BuiltinFunctionDefinitionStart,
  413. Lex::EqualTokenIndex, NodeCategory::None>;
  414. using BuiltinName =
  415. LeafNode<NodeKind::BuiltinName, Lex::StringLiteralTokenIndex>;
  416. // A builtin function definition: `fn F() -> i32 = "builtin name";`
  417. struct BuiltinFunctionDefinition {
  418. static constexpr auto Kind = NodeKind::BuiltinFunctionDefinition.Define(
  419. {.category = NodeCategory::Decl,
  420. .bracketed_by = BuiltinFunctionDefinitionStart::Kind});
  421. BuiltinFunctionDefinitionStartId signature;
  422. BuiltinNameId builtin_name;
  423. Lex::SemiTokenIndex token;
  424. };
  425. // `alias` nodes
  426. // -------------
  427. using AliasIntroducer =
  428. LeafNode<NodeKind::AliasIntroducer, Lex::AliasTokenIndex>;
  429. using AliasInitializer =
  430. LeafNode<NodeKind::AliasInitializer, Lex::EqualTokenIndex>;
  431. // An `alias` declaration: `alias a = b;`.
  432. struct Alias {
  433. static constexpr auto Kind = NodeKind::Alias.Define(
  434. {.category = NodeCategory::Decl, .bracketed_by = AliasIntroducer::Kind});
  435. AliasIntroducerId introducer;
  436. llvm::SmallVector<AnyModifierId> modifiers;
  437. DeclName name;
  438. AliasInitializerId equals;
  439. AnyExprId initializer;
  440. Lex::SemiTokenIndex token;
  441. };
  442. // `let` nodes
  443. // -----------
  444. using LetIntroducer = LeafNode<NodeKind::LetIntroducer, Lex::LetTokenIndex>;
  445. using LetInitializer = LeafNode<NodeKind::LetInitializer, Lex::EqualTokenIndex>;
  446. // A `let` declaration: `let a: i32 = 5;`.
  447. struct LetDecl {
  448. static constexpr auto Kind = NodeKind::LetDecl.Define(
  449. {.category = NodeCategory::Decl, .bracketed_by = LetIntroducer::Kind});
  450. LetIntroducerId introducer;
  451. llvm::SmallVector<AnyModifierId> modifiers;
  452. AnyPatternId pattern;
  453. struct Initializer {
  454. LetInitializerId equals;
  455. AnyExprId initializer;
  456. };
  457. std::optional<Initializer> initializer;
  458. Lex::SemiTokenIndex token;
  459. };
  460. // Associated constant nodes
  461. using AssociatedConstantIntroducer =
  462. LeafNode<NodeKind::AssociatedConstantIntroducer, Lex::LetTokenIndex>;
  463. using AssociatedConstantInitializer =
  464. LeafNode<NodeKind::AssociatedConstantInitializer, Lex::EqualTokenIndex>;
  465. struct AssociatedConstantNameAndType {
  466. static constexpr auto Kind = NodeKind::AssociatedConstantNameAndType.Define(
  467. {.category = NodeCategory::Pattern, .child_count = 2});
  468. AnyRuntimeBindingPatternName name;
  469. Lex::ColonExclaimTokenIndex token;
  470. AnyExprId type;
  471. };
  472. // An associated constant declaration: `let a:! i32;`.
  473. struct AssociatedConstantDecl {
  474. static constexpr auto Kind = NodeKind::AssociatedConstantDecl.Define(
  475. {.category = NodeCategory::Decl,
  476. .bracketed_by = AssociatedConstantIntroducer::Kind});
  477. AssociatedConstantIntroducerId introducer;
  478. llvm::SmallVector<AnyModifierId> modifiers;
  479. AssociatedConstantNameAndTypeId pattern;
  480. struct Initializer {
  481. AssociatedConstantInitializerId equals;
  482. AnyExprId initializer;
  483. };
  484. std::optional<Initializer> initializer;
  485. Lex::SemiTokenIndex token;
  486. };
  487. // `var` nodes
  488. // -----------
  489. using VariableIntroducer =
  490. LeafNode<NodeKind::VariableIntroducer, Lex::VarTokenIndex>;
  491. using ReturnedModifier =
  492. LeafNode<NodeKind::ReturnedModifier, Lex::ReturnedTokenIndex,
  493. NodeCategory::Modifier>;
  494. using VariableInitializer =
  495. LeafNode<NodeKind::VariableInitializer, Lex::EqualTokenIndex>;
  496. // A `var` declaration: `var a: i32;` or `var a: i32 = 5;`.
  497. struct VariableDecl {
  498. static constexpr auto Kind =
  499. NodeKind::VariableDecl.Define({.category = NodeCategory::Decl,
  500. .bracketed_by = VariableIntroducer::Kind});
  501. VariableIntroducerId introducer;
  502. llvm::SmallVector<AnyModifierId> modifiers;
  503. std::optional<ReturnedModifierId> returned;
  504. VariablePatternId pattern;
  505. struct Initializer {
  506. VariableInitializerId equals;
  507. AnyExprId value;
  508. };
  509. std::optional<Initializer> initializer;
  510. Lex::SemiTokenIndex token;
  511. };
  512. using FieldIntroducer = LeafNode<NodeKind::FieldIntroducer, Lex::VarTokenIndex>;
  513. using FieldInitializer =
  514. LeafNode<NodeKind::FieldInitializer, Lex::EqualTokenIndex>;
  515. struct FieldNameAndType {
  516. static constexpr auto Kind =
  517. NodeKind::FieldNameAndType.Define({.child_count = 2});
  518. IdentifierNameNotBeforeParamsId name;
  519. Lex::ColonTokenIndex token;
  520. AnyExprId type;
  521. };
  522. struct FieldDecl {
  523. static constexpr auto Kind = NodeKind::FieldDecl.Define(
  524. {.category = NodeCategory::Decl, .bracketed_by = FieldIntroducer::Kind});
  525. FieldIntroducerId introducer;
  526. llvm::SmallVector<AnyModifierId> modifiers;
  527. FieldNameAndTypeId name_and_type;
  528. struct Initializer {
  529. FieldInitializerId equals;
  530. AnyExprId value;
  531. };
  532. std::optional<Initializer> initializer;
  533. Lex::SemiTokenIndex token;
  534. };
  535. // A `var` pattern.
  536. struct VariablePattern {
  537. static constexpr auto Kind = NodeKind::VariablePattern.Define(
  538. {.category = NodeCategory::Pattern, .child_count = 1});
  539. Lex::VarTokenIndex token;
  540. AnyPatternId inner;
  541. };
  542. // Statement nodes
  543. // ---------------
  544. using CodeBlockStart =
  545. LeafNode<NodeKind::CodeBlockStart, Lex::OpenCurlyBraceTokenIndex>;
  546. using TerseBodyArrow =
  547. LeafNode<NodeKind::TerseBodyArrow, Lex::EqualGreaterTokenIndex>;
  548. // A code block: `{ statement; statement; ... }`.
  549. struct CodeBlock {
  550. static constexpr auto Kind =
  551. NodeKind::CodeBlock.Define({.bracketed_by = CodeBlockStart::Kind});
  552. CodeBlockStartId left_brace;
  553. llvm::SmallVector<AnyStatementId> statements;
  554. Lex::CloseCurlyBraceTokenIndex token;
  555. };
  556. using LambdaIntroducer =
  557. LeafNode<NodeKind::LambdaIntroducer, Lex::FnTokenIndex>;
  558. struct Lambda {
  559. static constexpr auto Kind = NodeKind::Lambda.Define(
  560. {.category = NodeCategory::Expr, .bracketed_by = LambdaIntroducer::Kind});
  561. LambdaIntroducerId introducer;
  562. std::optional<ImplicitParamListId> implicit_params;
  563. std::optional<ExplicitParamListId> explicit_params;
  564. std::optional<ReturnTypeId> return_type;
  565. std::optional<TerseBodyArrowId> arrow;
  566. NodeId body;
  567. // Use a generic token index because the token might be `}` or part of an
  568. // expression.
  569. Lex::TokenIndex token;
  570. };
  571. // An expression statement: `F(x);`.
  572. struct ExprStatement {
  573. static constexpr auto Kind = NodeKind::ExprStatement.Define(
  574. {.category = NodeCategory::Statement, .child_count = 1});
  575. AnyExprId expr;
  576. Lex::SemiTokenIndex token;
  577. };
  578. using BreakStatementStart =
  579. LeafNode<NodeKind::BreakStatementStart, Lex::BreakTokenIndex>;
  580. // A break statement: `break;`.
  581. struct BreakStatement {
  582. static constexpr auto Kind = NodeKind::BreakStatement.Define(
  583. {.category = NodeCategory::Statement,
  584. .bracketed_by = BreakStatementStart::Kind,
  585. .child_count = 1});
  586. BreakStatementStartId introducer;
  587. Lex::SemiTokenIndex token;
  588. };
  589. using ContinueStatementStart =
  590. LeafNode<NodeKind::ContinueStatementStart, Lex::ContinueTokenIndex>;
  591. // A continue statement: `continue;`.
  592. struct ContinueStatement {
  593. static constexpr auto Kind = NodeKind::ContinueStatement.Define(
  594. {.category = NodeCategory::Statement,
  595. .bracketed_by = ContinueStatementStart::Kind,
  596. .child_count = 1});
  597. ContinueStatementStartId introducer;
  598. Lex::SemiTokenIndex token;
  599. };
  600. using ReturnStatementStart =
  601. LeafNode<NodeKind::ReturnStatementStart, Lex::ReturnTokenIndex>;
  602. using ReturnVarModifier = LeafNode<NodeKind::ReturnVarModifier,
  603. Lex::VarTokenIndex, NodeCategory::Modifier>;
  604. // A return statement: `return;` or `return expr;` or `return var;`.
  605. struct ReturnStatement {
  606. static constexpr auto Kind = NodeKind::ReturnStatement.Define(
  607. {.category = NodeCategory::Statement,
  608. .bracketed_by = ReturnStatementStart::Kind});
  609. ReturnStatementStartId introducer;
  610. // TODO: This should be optional<OneOf<AnyExprId, ReturnVarModifierId>>,
  611. // but we don't have support for OneOf between a node kind and a category.
  612. std::optional<AnyExprId> expr;
  613. std::optional<ReturnVarModifierId> var;
  614. Lex::SemiTokenIndex token;
  615. };
  616. using ForHeaderStart =
  617. LeafNode<NodeKind::ForHeaderStart, Lex::OpenParenTokenIndex>;
  618. // The `... in` portion of a `for` statement.
  619. struct ForIn {
  620. static constexpr auto Kind = NodeKind::ForIn.Define({.child_count = 1});
  621. AnyPatternId pattern;
  622. Lex::InTokenIndex token;
  623. };
  624. // The `(... in ...)` portion of a `for` statement.
  625. struct ForHeader {
  626. static constexpr auto Kind =
  627. NodeKind::ForHeader.Define({.bracketed_by = ForHeaderStart::Kind});
  628. ForHeaderStartId introducer;
  629. ForInId var;
  630. AnyExprId range;
  631. Lex::CloseParenTokenIndex token;
  632. };
  633. // A complete `for (...) { ... }` statement.
  634. struct ForStatement {
  635. static constexpr auto Kind =
  636. NodeKind::ForStatement.Define({.category = NodeCategory::Statement,
  637. .bracketed_by = ForHeader::Kind,
  638. .child_count = 2});
  639. Lex::ForTokenIndex token;
  640. ForHeaderId header;
  641. CodeBlockId body;
  642. };
  643. using IfConditionStart =
  644. LeafNode<NodeKind::IfConditionStart, Lex::OpenParenTokenIndex>;
  645. // The condition portion of an `if` statement: `(expr)`.
  646. struct IfCondition {
  647. static constexpr auto Kind = NodeKind::IfCondition.Define(
  648. {.bracketed_by = IfConditionStart::Kind, .child_count = 2});
  649. IfConditionStartId left_paren;
  650. AnyExprId condition;
  651. Lex::CloseParenTokenIndex token;
  652. };
  653. using IfStatementElse =
  654. LeafNode<NodeKind::IfStatementElse, Lex::ElseTokenIndex>;
  655. // An `if` statement: `if (expr) { ... } else { ... }`.
  656. struct IfStatement {
  657. static constexpr auto Kind = NodeKind::IfStatement.Define(
  658. {.category = NodeCategory::Statement, .bracketed_by = IfCondition::Kind});
  659. Lex::IfTokenIndex token;
  660. IfConditionId head;
  661. CodeBlockId then;
  662. struct Else {
  663. IfStatementElseId else_token;
  664. NodeIdOneOf<CodeBlock, IfStatement> body;
  665. };
  666. std::optional<Else> else_clause;
  667. };
  668. using WhileConditionStart =
  669. LeafNode<NodeKind::WhileConditionStart, Lex::OpenParenTokenIndex>;
  670. // The condition portion of a `while` statement: `(expr)`.
  671. struct WhileCondition {
  672. static constexpr auto Kind = NodeKind::WhileCondition.Define(
  673. {.bracketed_by = WhileConditionStart::Kind, .child_count = 2});
  674. WhileConditionStartId left_paren;
  675. AnyExprId condition;
  676. Lex::CloseParenTokenIndex token;
  677. };
  678. // A `while` statement: `while (expr) { ... }`.
  679. struct WhileStatement {
  680. static constexpr auto Kind =
  681. NodeKind::WhileStatement.Define({.category = NodeCategory::Statement,
  682. .bracketed_by = WhileCondition::Kind,
  683. .child_count = 2});
  684. Lex::WhileTokenIndex token;
  685. WhileConditionId head;
  686. CodeBlockId body;
  687. };
  688. using MatchConditionStart =
  689. LeafNode<NodeKind::MatchConditionStart, Lex::OpenParenTokenIndex>;
  690. struct MatchCondition {
  691. static constexpr auto Kind = NodeKind::MatchCondition.Define(
  692. {.bracketed_by = MatchConditionStart::Kind, .child_count = 2});
  693. MatchConditionStartId left_paren;
  694. AnyExprId condition;
  695. Lex::CloseParenTokenIndex token;
  696. };
  697. using MatchIntroducer =
  698. LeafNode<NodeKind::MatchIntroducer, Lex::MatchTokenIndex>;
  699. struct MatchStatementStart {
  700. static constexpr auto Kind = NodeKind::MatchStatementStart.Define(
  701. {.bracketed_by = MatchIntroducer::Kind, .child_count = 2});
  702. MatchIntroducerId introducer;
  703. MatchConditionId condition;
  704. Lex::OpenCurlyBraceTokenIndex token;
  705. };
  706. using MatchCaseIntroducer =
  707. LeafNode<NodeKind::MatchCaseIntroducer, Lex::CaseTokenIndex>;
  708. using MatchCaseGuardIntroducer =
  709. LeafNode<NodeKind::MatchCaseGuardIntroducer, Lex::IfTokenIndex>;
  710. using MatchCaseGuardStart =
  711. LeafNode<NodeKind::MatchCaseGuardStart, Lex::OpenParenTokenIndex>;
  712. struct MatchCaseGuard {
  713. static constexpr auto Kind = NodeKind::MatchCaseGuard.Define(
  714. {.bracketed_by = MatchCaseGuardIntroducer::Kind, .child_count = 3});
  715. MatchCaseGuardIntroducerId introducer;
  716. MatchCaseGuardStartId left_paren;
  717. AnyExprId condition;
  718. Lex::CloseParenTokenIndex token;
  719. };
  720. using MatchCaseEqualGreater =
  721. LeafNode<NodeKind::MatchCaseEqualGreater, Lex::EqualGreaterTokenIndex>;
  722. struct MatchCaseStart {
  723. static constexpr auto Kind = NodeKind::MatchCaseStart.Define(
  724. {.bracketed_by = MatchCaseIntroducer::Kind});
  725. MatchCaseIntroducerId introducer;
  726. AnyPatternId pattern;
  727. std::optional<MatchCaseGuardId> guard;
  728. MatchCaseEqualGreaterId equal_greater_token;
  729. Lex::OpenCurlyBraceTokenIndex token;
  730. };
  731. struct MatchCase {
  732. static constexpr auto Kind =
  733. NodeKind::MatchCase.Define({.bracketed_by = MatchCaseStart::Kind});
  734. MatchCaseStartId head;
  735. llvm::SmallVector<AnyStatementId> statements;
  736. Lex::CloseCurlyBraceTokenIndex token;
  737. };
  738. using MatchDefaultIntroducer =
  739. LeafNode<NodeKind::MatchDefaultIntroducer, Lex::DefaultTokenIndex>;
  740. using MatchDefaultEqualGreater =
  741. LeafNode<NodeKind::MatchDefaultEqualGreater, Lex::EqualGreaterTokenIndex>;
  742. struct MatchDefaultStart {
  743. static constexpr auto Kind = NodeKind::MatchDefaultStart.Define(
  744. {.bracketed_by = MatchDefaultIntroducer::Kind, .child_count = 2});
  745. MatchDefaultIntroducerId introducer;
  746. MatchDefaultEqualGreaterId equal_greater_token;
  747. Lex::OpenCurlyBraceTokenIndex token;
  748. };
  749. struct MatchDefault {
  750. static constexpr auto Kind =
  751. NodeKind::MatchDefault.Define({.bracketed_by = MatchDefaultStart::Kind});
  752. MatchDefaultStartId introducer;
  753. llvm::SmallVector<AnyStatementId> statements;
  754. Lex::CloseCurlyBraceTokenIndex token;
  755. };
  756. // A `match` statement: `match (expr) { case (...) => {...} default => {...}}`.
  757. struct MatchStatement {
  758. static constexpr auto Kind = NodeKind::MatchStatement.Define(
  759. {.category = NodeCategory::Statement,
  760. .bracketed_by = MatchStatementStart::Kind});
  761. MatchStatementStartId head;
  762. llvm::SmallVector<MatchCaseId> cases;
  763. std::optional<MatchDefaultId> default_case;
  764. Lex::CloseCurlyBraceTokenIndex token;
  765. };
  766. // Expression nodes
  767. // ----------------
  768. using ArrayExprKeyword =
  769. LeafNode<NodeKind::ArrayExprKeyword, Lex::ArrayTokenIndex>;
  770. using ArrayExprOpenParen =
  771. LeafNode<NodeKind::ArrayExprOpenParen, Lex::OpenParenTokenIndex>;
  772. using ArrayExprComma = LeafNode<NodeKind::ArrayExprComma, Lex::CommaTokenIndex>;
  773. // An array type, `array(T, N)`.
  774. struct ArrayExpr {
  775. static constexpr auto Kind = NodeKind::ArrayExpr.Define(
  776. {.category = NodeCategory::Expr, .child_count = 5});
  777. ArrayExprKeywordId keyword;
  778. ArrayExprOpenParenId start;
  779. AnyExprId type;
  780. ArrayExprCommaId comma;
  781. AnyExprId bound;
  782. Lex::CloseParenTokenIndex token;
  783. };
  784. // The opening portion of an indexing expression: `a[`.
  785. //
  786. // TODO: Consider flattening this into `IndexExpr`.
  787. struct IndexExprStart {
  788. static constexpr auto Kind =
  789. NodeKind::IndexExprStart.Define({.child_count = 1});
  790. AnyExprId sequence;
  791. Lex::OpenSquareBracketTokenIndex token;
  792. };
  793. // An indexing expression, such as `a[1]`.
  794. struct IndexExpr {
  795. static constexpr auto Kind =
  796. NodeKind::IndexExpr.Define({.category = NodeCategory::Expr,
  797. .bracketed_by = IndexExprStart::Kind,
  798. .child_count = 2});
  799. IndexExprStartId start;
  800. AnyExprId index;
  801. Lex::CloseSquareBracketTokenIndex token;
  802. };
  803. using ParenExprStart =
  804. LeafNode<NodeKind::ParenExprStart, Lex::OpenParenTokenIndex>;
  805. // A parenthesized expression: `(a)`.
  806. struct ParenExpr {
  807. static constexpr auto Kind = NodeKind::ParenExpr.Define(
  808. {.category = NodeCategory::Expr | NodeCategory::MemberExpr,
  809. .bracketed_by = ParenExprStart::Kind,
  810. .child_count = 2});
  811. ParenExprStartId start;
  812. AnyExprId expr;
  813. Lex::CloseParenTokenIndex token;
  814. };
  815. using TupleLiteralStart =
  816. LeafNode<NodeKind::TupleLiteralStart, Lex::OpenParenTokenIndex>;
  817. using TupleLiteralComma =
  818. LeafNode<NodeKind::TupleLiteralComma, Lex::CommaTokenIndex>;
  819. // A tuple literal: `()`, `(a, b, c)`, or `(a,)`.
  820. struct TupleLiteral {
  821. static constexpr auto Kind =
  822. NodeKind::TupleLiteral.Define({.category = NodeCategory::Expr,
  823. .bracketed_by = TupleLiteralStart::Kind});
  824. TupleLiteralStartId start;
  825. CommaSeparatedList<AnyExprId, TupleLiteralCommaId> elements;
  826. Lex::CloseParenTokenIndex token;
  827. };
  828. // The opening portion of a call expression: `F(`.
  829. //
  830. // TODO: Consider flattening this into `CallExpr`.
  831. struct CallExprStart {
  832. static constexpr auto Kind =
  833. NodeKind::CallExprStart.Define({.child_count = 1});
  834. AnyExprId callee;
  835. Lex::OpenParenTokenIndex token;
  836. };
  837. // A call expression: `F(a, b, c)`.
  838. struct CallExpr {
  839. static constexpr auto Kind = NodeKind::CallExpr.Define(
  840. {.category = NodeCategory::Expr, .bracketed_by = CallExprStart::Kind});
  841. CallExprStartId start;
  842. CommaSeparatedList<AnyExprId, TupleLiteralCommaId> arguments;
  843. Lex::CloseParenTokenIndex token;
  844. };
  845. // A member access expression: `a.b` or `a.(b)`.
  846. struct MemberAccessExpr {
  847. static constexpr auto Kind = NodeKind::MemberAccessExpr.Define(
  848. {.category = NodeCategory::Expr, .child_count = 2});
  849. AnyExprId lhs;
  850. Lex::PeriodTokenIndex token;
  851. AnyMemberAccessId rhs;
  852. };
  853. // An indirect member access expression: `a->b` or `a->(b)`.
  854. struct PointerMemberAccessExpr {
  855. static constexpr auto Kind = NodeKind::PointerMemberAccessExpr.Define(
  856. {.category = NodeCategory::Expr, .child_count = 2});
  857. AnyExprId lhs;
  858. Lex::MinusGreaterTokenIndex token;
  859. AnyMemberAccessId rhs;
  860. };
  861. // A prefix operator expression.
  862. template <const NodeKind& KindT, typename TokenKind>
  863. struct PrefixOperator {
  864. static constexpr auto Kind =
  865. KindT.Define({.category = NodeCategory::Expr, .child_count = 1});
  866. TokenKind token;
  867. AnyExprId operand;
  868. };
  869. // An infix operator expression.
  870. template <const NodeKind& KindT, typename TokenKind>
  871. struct InfixOperator {
  872. static constexpr auto Kind =
  873. KindT.Define({.category = NodeCategory::Expr, .child_count = 2});
  874. AnyExprId lhs;
  875. TokenKind token;
  876. AnyExprId rhs;
  877. };
  878. // A postfix operator expression.
  879. template <const NodeKind& KindT, typename TokenKind>
  880. struct PostfixOperator {
  881. static constexpr auto Kind =
  882. KindT.Define({.category = NodeCategory::Expr, .child_count = 1});
  883. AnyExprId operand;
  884. TokenKind token;
  885. };
  886. // An `unsafe` modifier: `a unsafe <operator> b`. This is modeled in the parse
  887. // tree as a postfix operator applied to `a`.
  888. struct UnsafeModifier {
  889. static constexpr auto Kind = NodeKind::UnsafeModifier.Define(
  890. {.category = NodeCategory::Expr, .child_count = 1});
  891. AnyExprId operand;
  892. Lex::UnsafeTokenIndex token;
  893. };
  894. // Literals, operators, and modifiers
  895. #define CARBON_PARSE_NODE_KIND(Name)
  896. #define CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(Name, LexTokenKind) \
  897. using Name = LeafNode<NodeKind::Name, Lex::LexTokenKind##TokenIndex, \
  898. NodeCategory::Expr>;
  899. #define CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Name) \
  900. using Name##Modifier = \
  901. LeafNode<NodeKind::Name##Modifier, Lex::Name##TokenIndex, \
  902. NodeCategory::Modifier>;
  903. #define CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Name) \
  904. using PrefixOperator##Name = \
  905. PrefixOperator<NodeKind::PrefixOperator##Name, Lex::Name##TokenIndex>;
  906. #define CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Name) \
  907. using InfixOperator##Name = \
  908. InfixOperator<NodeKind::InfixOperator##Name, Lex::Name##TokenIndex>;
  909. #define CARBON_PARSE_NODE_KIND_POSTFIX_OPERATOR(Name) \
  910. using PostfixOperator##Name = \
  911. PostfixOperator<NodeKind::PostfixOperator##Name, Lex::Name##TokenIndex>;
  912. #include "toolchain/parse/node_kind.def"
  913. using IntLiteral = LeafNode<NodeKind::IntLiteral, Lex::IntLiteralTokenIndex,
  914. NodeCategory::Expr | NodeCategory::IntConst>;
  915. // `extern` as a standalone modifier.
  916. using ExternModifier = LeafNode<NodeKind::ExternModifier, Lex::ExternTokenIndex,
  917. NodeCategory::Modifier>;
  918. // `extern library <owning_library>` modifiers.
  919. struct ExternModifierWithLibrary {
  920. static constexpr auto Kind = NodeKind::ExternModifierWithLibrary.Define(
  921. {.category = NodeCategory::Modifier, .child_count = 1});
  922. Lex::ExternTokenIndex token;
  923. LibrarySpecifierId library;
  924. };
  925. // The first operand of a short-circuiting infix operator: `a and` or `a or`.
  926. // The complete operator expression will be an InfixOperator with this as the
  927. // `lhs`.
  928. // TODO: Make this be a template if we ever need to write generic code to cover
  929. // both cases at once, say in check.
  930. struct ShortCircuitOperandAnd {
  931. static constexpr auto Kind =
  932. NodeKind::ShortCircuitOperandAnd.Define({.child_count = 1});
  933. AnyExprId operand;
  934. // This is a virtual token. The `and` token is owned by the
  935. // ShortCircuitOperatorAnd node.
  936. Lex::AndTokenIndex token;
  937. };
  938. struct ShortCircuitOperandOr {
  939. static constexpr auto Kind =
  940. NodeKind::ShortCircuitOperandOr.Define({.child_count = 1});
  941. AnyExprId operand;
  942. // This is a virtual token. The `or` token is owned by the
  943. // ShortCircuitOperatorOr node.
  944. Lex::OrTokenIndex token;
  945. };
  946. struct ShortCircuitOperatorAnd {
  947. static constexpr auto Kind = NodeKind::ShortCircuitOperatorAnd.Define(
  948. {.category = NodeCategory::Expr,
  949. .bracketed_by = ShortCircuitOperandAnd::Kind,
  950. .child_count = 2});
  951. ShortCircuitOperandAndId lhs;
  952. Lex::AndTokenIndex token;
  953. AnyExprId rhs;
  954. };
  955. struct ShortCircuitOperatorOr {
  956. static constexpr auto Kind = NodeKind::ShortCircuitOperatorOr.Define(
  957. {.category = NodeCategory::Expr,
  958. .bracketed_by = ShortCircuitOperandOr::Kind,
  959. .child_count = 2});
  960. ShortCircuitOperandOrId lhs;
  961. Lex::OrTokenIndex token;
  962. AnyExprId rhs;
  963. };
  964. // The `if` portion of an `if` expression: `if expr`.
  965. struct IfExprIf {
  966. static constexpr auto Kind = NodeKind::IfExprIf.Define({.child_count = 1});
  967. Lex::IfTokenIndex token;
  968. AnyExprId condition;
  969. };
  970. // The `then` portion of an `if` expression: `then expr`.
  971. struct IfExprThen {
  972. static constexpr auto Kind = NodeKind::IfExprThen.Define({.child_count = 1});
  973. Lex::ThenTokenIndex token;
  974. AnyExprId result;
  975. };
  976. // A full `if` expression: `if expr then expr else expr`.
  977. struct IfExprElse {
  978. static constexpr auto Kind =
  979. NodeKind::IfExprElse.Define({.category = NodeCategory::Expr,
  980. .bracketed_by = IfExprIf::Kind,
  981. .child_count = 3});
  982. IfExprIfId start;
  983. IfExprThenId then;
  984. Lex::ElseTokenIndex token;
  985. AnyExprId else_result;
  986. };
  987. // A `where` expression (TODO: `require` and `observe` declarations)
  988. // The `Self` in a context where it is treated as a name rather than an
  989. // expression, such as `.Self`.
  990. using SelfTypeName =
  991. LeafNode<NodeKind::SelfTypeName, Lex::SelfTypeIdentifierTokenIndex>;
  992. // `.Member` or `.Self` in an expression context, used in `where` and `require`
  993. // clauses.
  994. // TODO: Do we want to support `.1`, a designator for accessing a tuple member?
  995. struct DesignatorExpr {
  996. static constexpr auto Kind = NodeKind::DesignatorExpr.Define(
  997. {.category = NodeCategory::Expr, .child_count = 1});
  998. Lex::PeriodTokenIndex token;
  999. NodeIdOneOf<IdentifierNameNotBeforeParams, SelfTypeName> name;
  1000. };
  1001. struct RequirementEqual {
  1002. static constexpr auto Kind = NodeKind::RequirementEqual.Define(
  1003. {.category = NodeCategory::Requirement, .child_count = 2});
  1004. DesignatorExprId lhs;
  1005. Lex::EqualTokenIndex token;
  1006. AnyExprId rhs;
  1007. };
  1008. struct RequirementEqualEqual {
  1009. static constexpr auto Kind = NodeKind::RequirementEqualEqual.Define(
  1010. {.category = NodeCategory::Requirement, .child_count = 2});
  1011. AnyExprId lhs;
  1012. Lex::EqualEqualTokenIndex token;
  1013. AnyExprId rhs;
  1014. };
  1015. struct RequirementImpls {
  1016. static constexpr auto Kind = NodeKind::RequirementImpls.Define(
  1017. {.category = NodeCategory::Requirement, .child_count = 2});
  1018. AnyExprId lhs;
  1019. Lex::ImplsTokenIndex token;
  1020. AnyExprId rhs;
  1021. };
  1022. // An `and` token separating requirements in a `where` expression.
  1023. using RequirementAnd = LeafNode<NodeKind::RequirementAnd, Lex::AndTokenIndex>;
  1024. struct WhereOperand {
  1025. static constexpr auto Kind =
  1026. NodeKind::WhereOperand.Define({.child_count = 1});
  1027. AnyExprId type;
  1028. // This is a virtual token. The `where` token is owned by the
  1029. // WhereExpr node.
  1030. Lex::WhereTokenIndex token;
  1031. };
  1032. struct WhereExpr {
  1033. static constexpr auto Kind = NodeKind::WhereExpr.Define(
  1034. {.category = NodeCategory::Expr, .bracketed_by = WhereOperand::Kind});
  1035. WhereOperandId introducer;
  1036. Lex::WhereTokenIndex token;
  1037. CommaSeparatedList<AnyRequirementId, RequirementAndId> requirements;
  1038. };
  1039. // Choice nodes
  1040. // ------------
  1041. using ChoiceIntroducer =
  1042. LeafNode<NodeKind::ChoiceIntroducer, Lex::ChoiceTokenIndex>;
  1043. struct ChoiceSignature {
  1044. static constexpr auto Kind = NodeKind::ChoiceDefinitionStart.Define(
  1045. {.category = NodeCategory::None, .bracketed_by = ChoiceIntroducer::Kind});
  1046. ChoiceIntroducerId introducer;
  1047. llvm::SmallVector<AnyModifierId> modifiers;
  1048. DeclName name;
  1049. Lex::OpenCurlyBraceTokenIndex token;
  1050. };
  1051. using ChoiceDefinitionStart = ChoiceSignature;
  1052. using ChoiceAlternativeListComma =
  1053. LeafNode<NodeKind::ChoiceAlternativeListComma, Lex::CommaTokenIndex>;
  1054. struct ChoiceDefinition {
  1055. static constexpr auto Kind = NodeKind::ChoiceDefinition.Define(
  1056. {.category = NodeCategory::Decl,
  1057. .bracketed_by = ChoiceDefinitionStart::Kind});
  1058. ChoiceDefinitionStartId signature;
  1059. struct Alternative {
  1060. AnyNonExprNameId name;
  1061. std::optional<ExplicitParamListId> parameters;
  1062. };
  1063. CommaSeparatedList<Alternative, ChoiceAlternativeListCommaId> alternatives;
  1064. Lex::CloseCurlyBraceTokenIndex token;
  1065. };
  1066. // Struct type and value literals
  1067. // ----------------------------------------
  1068. // `{`
  1069. using StructLiteralStart =
  1070. LeafNode<NodeKind::StructLiteralStart, Lex::OpenCurlyBraceTokenIndex>;
  1071. using StructTypeLiteralStart =
  1072. LeafNode<NodeKind::StructTypeLiteralStart, Lex::OpenCurlyBraceTokenIndex>;
  1073. // `,`
  1074. using StructLiteralComma =
  1075. LeafNode<NodeKind::StructLiteralComma, Lex::CommaTokenIndex>;
  1076. using StructTypeLiteralComma =
  1077. LeafNode<NodeKind::StructTypeLiteralComma, Lex::CommaTokenIndex>;
  1078. // `.a`
  1079. // This is shared for struct literals and type literals in order to reduce
  1080. // lookahead for parse (the `=` versus `:` would require lookahead of 2).
  1081. struct StructFieldDesignator {
  1082. static constexpr auto Kind =
  1083. NodeKind::StructFieldDesignator.Define({.child_count = 1});
  1084. Lex::PeriodTokenIndex token;
  1085. NodeIdOneOf<IdentifierNameNotBeforeParams, BaseName> name;
  1086. };
  1087. // `.a = 0`
  1088. struct StructLiteralField {
  1089. static constexpr auto Kind = NodeKind::StructLiteralField.Define(
  1090. {.bracketed_by = StructFieldDesignator::Kind, .child_count = 2});
  1091. StructFieldDesignatorId designator;
  1092. Lex::EqualTokenIndex token;
  1093. AnyExprId expr;
  1094. };
  1095. // `.a: i32`
  1096. struct StructTypeLiteralField {
  1097. static constexpr auto Kind = NodeKind::StructTypeLiteralField.Define(
  1098. {.bracketed_by = StructFieldDesignator::Kind, .child_count = 2});
  1099. StructFieldDesignatorId designator;
  1100. Lex::ColonTokenIndex token;
  1101. AnyExprId type_expr;
  1102. };
  1103. // Struct literals, such as `{.a = 0}`.
  1104. struct StructLiteral {
  1105. static constexpr auto Kind = NodeKind::StructLiteral.Define(
  1106. {.category = NodeCategory::Expr,
  1107. .bracketed_by = StructLiteralStart::Kind});
  1108. StructLiteralStartId start;
  1109. CommaSeparatedList<StructLiteralFieldId, StructLiteralCommaId> fields;
  1110. Lex::CloseCurlyBraceTokenIndex token;
  1111. };
  1112. // Struct type literals, such as `{.a: i32}`.
  1113. struct StructTypeLiteral {
  1114. static constexpr auto Kind = NodeKind::StructTypeLiteral.Define(
  1115. {.category = NodeCategory::Expr,
  1116. .bracketed_by = StructTypeLiteralStart::Kind});
  1117. StructTypeLiteralStartId start;
  1118. CommaSeparatedList<StructTypeLiteralFieldId, StructTypeLiteralCommaId> fields;
  1119. Lex::CloseCurlyBraceTokenIndex token;
  1120. };
  1121. // `class` declarations and definitions
  1122. // ------------------------------------
  1123. // `class`
  1124. using ClassIntroducer =
  1125. LeafNode<NodeKind::ClassIntroducer, Lex::ClassTokenIndex>;
  1126. // A class signature `class C`
  1127. template <const NodeKind& KindT, typename TokenKind,
  1128. NodeCategory::RawEnumType Category>
  1129. struct ClassSignature {
  1130. static constexpr auto Kind = KindT.Define(
  1131. {.category = Category, .bracketed_by = ClassIntroducer::Kind});
  1132. ClassIntroducerId introducer;
  1133. llvm::SmallVector<AnyModifierId> modifiers;
  1134. DeclName name;
  1135. TokenKind token;
  1136. };
  1137. // `class C;`
  1138. using ClassDecl = ClassSignature<NodeKind::ClassDecl, Lex::SemiTokenIndex,
  1139. NodeCategory::Decl>;
  1140. // `class C {`
  1141. using ClassDefinitionStart =
  1142. ClassSignature<NodeKind::ClassDefinitionStart,
  1143. Lex::OpenCurlyBraceTokenIndex, NodeCategory::None>;
  1144. // `class C { ... }`
  1145. struct ClassDefinition {
  1146. static constexpr auto Kind = NodeKind::ClassDefinition.Define(
  1147. {.category = NodeCategory::Decl,
  1148. .bracketed_by = ClassDefinitionStart::Kind});
  1149. ClassDefinitionStartId signature;
  1150. llvm::SmallVector<AnyDeclId> members;
  1151. Lex::CloseCurlyBraceTokenIndex token;
  1152. };
  1153. // Adapter declaration
  1154. // -------------------
  1155. // `adapt`
  1156. using AdaptIntroducer =
  1157. LeafNode<NodeKind::AdaptIntroducer, Lex::AdaptTokenIndex>;
  1158. // `adapt SomeType;`
  1159. struct AdaptDecl {
  1160. static constexpr auto Kind = NodeKind::AdaptDecl.Define(
  1161. {.category = NodeCategory::Decl, .bracketed_by = AdaptIntroducer::Kind});
  1162. AdaptIntroducerId introducer;
  1163. llvm::SmallVector<AnyModifierId> modifiers;
  1164. AnyExprId adapted_type;
  1165. Lex::SemiTokenIndex token;
  1166. };
  1167. // Base class declaration
  1168. // ----------------------
  1169. // `base`
  1170. using BaseIntroducer = LeafNode<NodeKind::BaseIntroducer, Lex::BaseTokenIndex>;
  1171. using BaseColon = LeafNode<NodeKind::BaseColon, Lex::ColonTokenIndex>;
  1172. // `extend base: BaseClass;`
  1173. struct BaseDecl {
  1174. static constexpr auto Kind = NodeKind::BaseDecl.Define(
  1175. {.category = NodeCategory::Decl, .bracketed_by = BaseIntroducer::Kind});
  1176. BaseIntroducerId introducer;
  1177. llvm::SmallVector<AnyModifierId> modifiers;
  1178. BaseColonId colon;
  1179. AnyExprId base_class;
  1180. Lex::SemiTokenIndex token;
  1181. };
  1182. // Interface declarations and definitions
  1183. // --------------------------------------
  1184. // `interface`
  1185. using InterfaceIntroducer =
  1186. LeafNode<NodeKind::InterfaceIntroducer, Lex::InterfaceTokenIndex>;
  1187. // `interface I`
  1188. template <const NodeKind& KindT, typename TokenKind,
  1189. NodeCategory::RawEnumType Category>
  1190. struct InterfaceSignature {
  1191. static constexpr auto Kind = KindT.Define(
  1192. {.category = Category, .bracketed_by = InterfaceIntroducer::Kind});
  1193. InterfaceIntroducerId introducer;
  1194. llvm::SmallVector<AnyModifierId> modifiers;
  1195. DeclName name;
  1196. TokenKind token;
  1197. };
  1198. // `interface I;`
  1199. using InterfaceDecl =
  1200. InterfaceSignature<NodeKind::InterfaceDecl, Lex::SemiTokenIndex,
  1201. NodeCategory::Decl>;
  1202. // `interface I {`
  1203. using InterfaceDefinitionStart =
  1204. InterfaceSignature<NodeKind::InterfaceDefinitionStart,
  1205. Lex::OpenCurlyBraceTokenIndex, NodeCategory::None>;
  1206. // `interface I { ... }`
  1207. struct InterfaceDefinition {
  1208. static constexpr auto Kind = NodeKind::InterfaceDefinition.Define(
  1209. {.category = NodeCategory::Decl,
  1210. .bracketed_by = InterfaceDefinitionStart::Kind});
  1211. InterfaceDefinitionStartId signature;
  1212. llvm::SmallVector<AnyDeclId> members;
  1213. Lex::CloseCurlyBraceTokenIndex token;
  1214. };
  1215. // `require`...`impls` statements
  1216. // ------------------------------
  1217. // `require`
  1218. using RequireIntroducer =
  1219. LeafNode<NodeKind::RequireIntroducer, Lex::RequireTokenIndex>;
  1220. // `impls` with no type before it
  1221. using RequireDefaultSelfImpls =
  1222. LeafNode<NodeKind::RequireDefaultSelfImpls, Lex::ImplsTokenIndex,
  1223. NodeCategory::RequireImpls>;
  1224. // `<type> impls`.
  1225. struct RequireTypeImpls {
  1226. static constexpr auto Kind = NodeKind::RequireTypeImpls.Define(
  1227. {.category = NodeCategory::RequireImpls, .child_count = 1});
  1228. AnyExprId type_expr;
  1229. Lex::ImplsTokenIndex token;
  1230. };
  1231. // `require T impls I where...`
  1232. struct RequireDecl {
  1233. static constexpr auto Kind =
  1234. NodeKind::RequireDecl.Define({.category = NodeCategory::Decl,
  1235. .bracketed_by = RequireIntroducer::Kind});
  1236. RequireIntroducerId introducer;
  1237. llvm::SmallVector<AnyModifierId> modifiers;
  1238. AnyRequireImplsId impls;
  1239. AnyExprId facet_type;
  1240. Lex::SemiTokenIndex token;
  1241. };
  1242. // `impl`...`as` declarations and definitions
  1243. // ------------------------------------------
  1244. // `impl`
  1245. using ImplIntroducer = LeafNode<NodeKind::ImplIntroducer, Lex::ImplTokenIndex>;
  1246. // `forall`
  1247. using Forall = LeafNode<NodeKind::Forall, Lex::ForallTokenIndex>;
  1248. // `forall [...]`
  1249. struct ImplForall {
  1250. ForallId forall;
  1251. ImplicitParamListId params;
  1252. };
  1253. // `as` with no type before it
  1254. using ImplDefaultSelfAs = LeafNode<NodeKind::ImplDefaultSelfAs,
  1255. Lex::AsTokenIndex, NodeCategory::ImplAs>;
  1256. // `<type> as`
  1257. struct ImplTypeAs {
  1258. static constexpr auto Kind = NodeKind::ImplTypeAs.Define(
  1259. {.category = NodeCategory::ImplAs, .child_count = 1});
  1260. AnyExprId type_expr;
  1261. Lex::AsTokenIndex token;
  1262. };
  1263. // `impl T as I`
  1264. template <const NodeKind& KindT, typename TokenKind,
  1265. NodeCategory::RawEnumType Category>
  1266. struct ImplSignature {
  1267. static constexpr auto Kind = KindT.Define(
  1268. {.category = Category, .bracketed_by = ImplIntroducer::Kind});
  1269. ImplIntroducerId introducer;
  1270. llvm::SmallVector<AnyModifierId> modifiers;
  1271. std::optional<ImplForall> forall;
  1272. AnyImplAsId as;
  1273. AnyExprId interface;
  1274. TokenKind token;
  1275. };
  1276. // `impl T as I;`
  1277. using ImplDecl =
  1278. ImplSignature<NodeKind::ImplDecl, Lex::SemiTokenIndex, NodeCategory::Decl>;
  1279. // `impl T as I {`
  1280. using ImplDefinitionStart =
  1281. ImplSignature<NodeKind::ImplDefinitionStart, Lex::OpenCurlyBraceTokenIndex,
  1282. NodeCategory::None>;
  1283. // `impl T as I { ... }`
  1284. struct ImplDefinition {
  1285. static constexpr auto Kind = NodeKind::ImplDefinition.Define(
  1286. {.category = NodeCategory::Decl,
  1287. .bracketed_by = ImplDefinitionStart::Kind});
  1288. ImplDefinitionStartId signature;
  1289. llvm::SmallVector<AnyDeclId> members;
  1290. Lex::CloseCurlyBraceTokenIndex token;
  1291. };
  1292. // Named constraint declarations and definitions
  1293. // ---------------------------------------------
  1294. // `constraint`
  1295. using NamedConstraintIntroducer =
  1296. LeafNode<NodeKind::NamedConstraintIntroducer, Lex::ConstraintTokenIndex>;
  1297. // `constraint NC`
  1298. template <const NodeKind& KindT, typename TokenKind,
  1299. NodeCategory::RawEnumType Category>
  1300. struct NamedConstraintSignature {
  1301. static constexpr auto Kind = KindT.Define(
  1302. {.category = Category, .bracketed_by = NamedConstraintIntroducer::Kind});
  1303. NamedConstraintIntroducerId introducer;
  1304. llvm::SmallVector<AnyModifierId> modifiers;
  1305. DeclName name;
  1306. TokenKind token;
  1307. };
  1308. // `constraint NC;`
  1309. using NamedConstraintDecl =
  1310. NamedConstraintSignature<NodeKind::NamedConstraintDecl, Lex::SemiTokenIndex,
  1311. NodeCategory::Decl>;
  1312. // `constraint NC {`
  1313. using NamedConstraintDefinitionStart =
  1314. NamedConstraintSignature<NodeKind::NamedConstraintDefinitionStart,
  1315. Lex::OpenCurlyBraceTokenIndex, NodeCategory::None>;
  1316. // `constraint NC { ... }`
  1317. struct NamedConstraintDefinition {
  1318. static constexpr auto Kind = NodeKind::NamedConstraintDefinition.Define(
  1319. {.category = NodeCategory::Decl,
  1320. .bracketed_by = NamedConstraintDefinitionStart::Kind});
  1321. NamedConstraintDefinitionStartId signature;
  1322. llvm::SmallVector<AnyDeclId> members;
  1323. Lex::CloseCurlyBraceTokenIndex token;
  1324. };
  1325. // ---------------------------------------------------------------------------
  1326. // A complete source file. Note that there is no corresponding parse node for
  1327. // the file. The file is instead the complete contents of the parse tree.
  1328. struct File {
  1329. FileStartId start;
  1330. llvm::SmallVector<AnyDeclId> decls;
  1331. FileEndId end;
  1332. };
  1333. // Define `Foo` as the node type for the ID type `FooId`.
  1334. #define CARBON_PARSE_NODE_KIND(KindName) \
  1335. template <> \
  1336. struct NodeForId<KindName##Id> { \
  1337. using TypedNode = KindName; \
  1338. };
  1339. #include "toolchain/parse/node_kind.def"
  1340. } // namespace Carbon::Parse
  1341. #endif // CARBON_TOOLCHAIN_PARSE_TYPED_NODES_H_