typed_nodes.h 56 KB

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