typed_nodes.h 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746
  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(
  383. {.category = NodeCategory::ReturnDecl, .child_count = 1});
  384. Lex::MinusGreaterTokenIndex token;
  385. AnyExprId type;
  386. };
  387. // A return form: `->? form(var i32)`
  388. struct ReturnForm {
  389. static constexpr auto Kind = NodeKind::ReturnForm.Define(
  390. {.category = NodeCategory::ReturnDecl, .child_count = 1});
  391. Lex::MinusGreaterQuestionTokenIndex token;
  392. AnyExprId type;
  393. };
  394. // A function signature: `fn F() -> i32`.
  395. template <const NodeKind& KindT, typename TokenKind,
  396. NodeCategory::RawEnumType Category>
  397. struct FunctionSignature {
  398. static constexpr auto Kind = KindT.Define(
  399. {.category = Category, .bracketed_by = FunctionIntroducer::Kind});
  400. FunctionIntroducerId introducer;
  401. llvm::SmallVector<AnyModifierId> modifiers;
  402. DeclName name;
  403. std::optional<AnyReturnDeclId> return_type;
  404. TokenKind token;
  405. };
  406. using FunctionDecl = FunctionSignature<NodeKind::FunctionDecl,
  407. Lex::SemiTokenIndex, NodeCategory::Decl>;
  408. using FunctionDefinitionStart =
  409. FunctionSignature<NodeKind::FunctionDefinitionStart, Lex::TokenIndex,
  410. NodeCategory::None>;
  411. // A function definition: `fn F() -> i32 { ... }`.
  412. struct FunctionDefinition {
  413. static constexpr auto Kind = NodeKind::FunctionDefinition.Define(
  414. {.category = NodeCategory::Decl,
  415. .bracketed_by = FunctionDefinitionStart::Kind});
  416. FunctionDefinitionStartId signature;
  417. llvm::SmallVector<AnyStatementId> body;
  418. Lex::CloseCurlyBraceTokenIndex token;
  419. };
  420. // A terse function definition: `fn F() -> i32 => expr;`.
  421. struct FunctionTerseDefinition {
  422. static constexpr auto Kind = NodeKind::FunctionTerseDefinition.Define(
  423. {.category = NodeCategory::Decl,
  424. .bracketed_by = FunctionDefinitionStart::Kind});
  425. FunctionDefinitionStartId signature;
  426. TerseBodyArrowId arrow;
  427. AnyExprId body;
  428. Lex::SemiTokenIndex token;
  429. };
  430. using BuiltinFunctionDefinitionStart =
  431. FunctionSignature<NodeKind::BuiltinFunctionDefinitionStart,
  432. Lex::EqualTokenIndex, NodeCategory::None>;
  433. using BuiltinName =
  434. LeafNode<NodeKind::BuiltinName, Lex::StringLiteralTokenIndex>;
  435. // A builtin function definition: `fn F() -> i32 = "builtin name";`
  436. struct BuiltinFunctionDefinition {
  437. static constexpr auto Kind = NodeKind::BuiltinFunctionDefinition.Define(
  438. {.category = NodeCategory::Decl,
  439. .bracketed_by = BuiltinFunctionDefinitionStart::Kind});
  440. BuiltinFunctionDefinitionStartId signature;
  441. BuiltinNameId builtin_name;
  442. Lex::SemiTokenIndex token;
  443. };
  444. // `alias` nodes
  445. // -------------
  446. using AliasIntroducer =
  447. LeafNode<NodeKind::AliasIntroducer, Lex::AliasTokenIndex>;
  448. using AliasInitializer =
  449. LeafNode<NodeKind::AliasInitializer, Lex::EqualTokenIndex>;
  450. // An `alias` declaration: `alias a = b;`.
  451. struct Alias {
  452. static constexpr auto Kind = NodeKind::Alias.Define(
  453. {.category = NodeCategory::Decl, .bracketed_by = AliasIntroducer::Kind});
  454. AliasIntroducerId introducer;
  455. llvm::SmallVector<AnyModifierId> modifiers;
  456. DeclName name;
  457. AliasInitializerId equals;
  458. AnyExprId initializer;
  459. Lex::SemiTokenIndex token;
  460. };
  461. // `let` nodes
  462. // -----------
  463. using LetIntroducer = LeafNode<NodeKind::LetIntroducer, Lex::LetTokenIndex>;
  464. using LetInitializer = LeafNode<NodeKind::LetInitializer, Lex::EqualTokenIndex>;
  465. // A `let` declaration: `let a: i32 = 5;`.
  466. struct LetDecl {
  467. static constexpr auto Kind = NodeKind::LetDecl.Define(
  468. {.category = NodeCategory::Decl, .bracketed_by = LetIntroducer::Kind});
  469. LetIntroducerId introducer;
  470. llvm::SmallVector<AnyModifierId> modifiers;
  471. AnyPatternId pattern;
  472. struct Initializer {
  473. LetInitializerId equals;
  474. AnyExprId initializer;
  475. };
  476. std::optional<Initializer> initializer;
  477. Lex::SemiTokenIndex token;
  478. };
  479. // Associated constant nodes
  480. using AssociatedConstantIntroducer =
  481. LeafNode<NodeKind::AssociatedConstantIntroducer, Lex::LetTokenIndex>;
  482. using AssociatedConstantInitializer =
  483. LeafNode<NodeKind::AssociatedConstantInitializer, Lex::EqualTokenIndex>;
  484. struct AssociatedConstantNameAndType {
  485. static constexpr auto Kind = NodeKind::AssociatedConstantNameAndType.Define(
  486. {.category = NodeCategory::Pattern, .child_count = 2});
  487. AnyRuntimeBindingPatternName name;
  488. Lex::ColonExclaimTokenIndex token;
  489. AnyExprId type;
  490. };
  491. // An associated constant declaration: `let a:! i32;`.
  492. struct AssociatedConstantDecl {
  493. static constexpr auto Kind = NodeKind::AssociatedConstantDecl.Define(
  494. {.category = NodeCategory::Decl,
  495. .bracketed_by = AssociatedConstantIntroducer::Kind});
  496. AssociatedConstantIntroducerId introducer;
  497. llvm::SmallVector<AnyModifierId> modifiers;
  498. AssociatedConstantNameAndTypeId pattern;
  499. struct Initializer {
  500. AssociatedConstantInitializerId equals;
  501. AnyExprId initializer;
  502. };
  503. std::optional<Initializer> initializer;
  504. Lex::SemiTokenIndex token;
  505. };
  506. // `var` nodes
  507. // -----------
  508. using VariableIntroducer =
  509. LeafNode<NodeKind::VariableIntroducer, Lex::VarTokenIndex>;
  510. using ReturnedModifier =
  511. LeafNode<NodeKind::ReturnedModifier, Lex::ReturnedTokenIndex,
  512. NodeCategory::Modifier>;
  513. using VariableInitializer =
  514. LeafNode<NodeKind::VariableInitializer, Lex::EqualTokenIndex>;
  515. // A `var` declaration: `var a: i32;` or `var a: i32 = 5;`.
  516. struct VariableDecl {
  517. static constexpr auto Kind =
  518. NodeKind::VariableDecl.Define({.category = NodeCategory::Decl,
  519. .bracketed_by = VariableIntroducer::Kind});
  520. VariableIntroducerId introducer;
  521. llvm::SmallVector<AnyModifierId> modifiers;
  522. std::optional<ReturnedModifierId> returned;
  523. VariablePatternId pattern;
  524. struct Initializer {
  525. VariableInitializerId equals;
  526. AnyExprId value;
  527. };
  528. std::optional<Initializer> initializer;
  529. Lex::SemiTokenIndex token;
  530. };
  531. using FieldIntroducer = LeafNode<NodeKind::FieldIntroducer, Lex::VarTokenIndex>;
  532. using FieldInitializer =
  533. LeafNode<NodeKind::FieldInitializer, Lex::EqualTokenIndex>;
  534. struct FieldNameAndType {
  535. static constexpr auto Kind =
  536. NodeKind::FieldNameAndType.Define({.child_count = 2});
  537. IdentifierNameNotBeforeSignatureId name;
  538. Lex::ColonTokenIndex token;
  539. AnyExprId type;
  540. };
  541. struct FieldDecl {
  542. static constexpr auto Kind = NodeKind::FieldDecl.Define(
  543. {.category = NodeCategory::Decl, .bracketed_by = FieldIntroducer::Kind});
  544. FieldIntroducerId introducer;
  545. llvm::SmallVector<AnyModifierId> modifiers;
  546. FieldNameAndTypeId name_and_type;
  547. struct Initializer {
  548. FieldInitializerId equals;
  549. AnyExprId value;
  550. };
  551. std::optional<Initializer> initializer;
  552. Lex::SemiTokenIndex token;
  553. };
  554. // A `var` pattern.
  555. struct VariablePattern {
  556. static constexpr auto Kind = NodeKind::VariablePattern.Define(
  557. {.category = NodeCategory::Pattern, .child_count = 1});
  558. Lex::VarTokenIndex token;
  559. AnyPatternId inner;
  560. };
  561. // Statement nodes
  562. // ---------------
  563. using CodeBlockStart =
  564. LeafNode<NodeKind::CodeBlockStart, Lex::OpenCurlyBraceTokenIndex>;
  565. using TerseBodyArrow =
  566. LeafNode<NodeKind::TerseBodyArrow, Lex::EqualGreaterTokenIndex>;
  567. // A code block: `{ statement; statement; ... }`.
  568. struct CodeBlock {
  569. static constexpr auto Kind =
  570. NodeKind::CodeBlock.Define({.bracketed_by = CodeBlockStart::Kind});
  571. CodeBlockStartId left_brace;
  572. llvm::SmallVector<AnyStatementId> statements;
  573. Lex::CloseCurlyBraceTokenIndex token;
  574. };
  575. using LambdaIntroducer =
  576. LeafNode<NodeKind::LambdaIntroducer, Lex::FnTokenIndex>;
  577. struct Lambda {
  578. static constexpr auto Kind = NodeKind::Lambda.Define(
  579. {.category = NodeCategory::Expr, .bracketed_by = LambdaIntroducer::Kind});
  580. LambdaIntroducerId introducer;
  581. std::optional<ImplicitParamListId> implicit_params;
  582. std::optional<ExplicitParamListId> explicit_params;
  583. std::optional<ReturnTypeId> return_type;
  584. std::optional<TerseBodyArrowId> arrow;
  585. NodeId body;
  586. // Use a generic token index because the token might be `}` or part of an
  587. // expression.
  588. Lex::TokenIndex token;
  589. };
  590. // An expression statement: `F(x);`.
  591. struct ExprStatement {
  592. static constexpr auto Kind = NodeKind::ExprStatement.Define(
  593. {.category = NodeCategory::Statement, .child_count = 1});
  594. AnyExprId expr;
  595. Lex::SemiTokenIndex token;
  596. };
  597. using BreakStatementStart =
  598. LeafNode<NodeKind::BreakStatementStart, Lex::BreakTokenIndex>;
  599. // A break statement: `break;`.
  600. struct BreakStatement {
  601. static constexpr auto Kind = NodeKind::BreakStatement.Define(
  602. {.category = NodeCategory::Statement,
  603. .bracketed_by = BreakStatementStart::Kind,
  604. .child_count = 1});
  605. BreakStatementStartId introducer;
  606. Lex::SemiTokenIndex token;
  607. };
  608. using ContinueStatementStart =
  609. LeafNode<NodeKind::ContinueStatementStart, Lex::ContinueTokenIndex>;
  610. // A continue statement: `continue;`.
  611. struct ContinueStatement {
  612. static constexpr auto Kind = NodeKind::ContinueStatement.Define(
  613. {.category = NodeCategory::Statement,
  614. .bracketed_by = ContinueStatementStart::Kind,
  615. .child_count = 1});
  616. ContinueStatementStartId introducer;
  617. Lex::SemiTokenIndex token;
  618. };
  619. using ReturnStatementStart =
  620. LeafNode<NodeKind::ReturnStatementStart, Lex::ReturnTokenIndex>;
  621. using ReturnVarModifier = LeafNode<NodeKind::ReturnVarModifier,
  622. Lex::VarTokenIndex, NodeCategory::Modifier>;
  623. // A return statement: `return;` or `return expr;` or `return var;`.
  624. struct ReturnStatement {
  625. static constexpr auto Kind = NodeKind::ReturnStatement.Define(
  626. {.category = NodeCategory::Statement,
  627. .bracketed_by = ReturnStatementStart::Kind});
  628. ReturnStatementStartId introducer;
  629. // TODO: This should be optional<OneOf<AnyExprId, ReturnVarModifierId>>,
  630. // but we don't have support for OneOf between a node kind and a category.
  631. std::optional<AnyExprId> expr;
  632. std::optional<ReturnVarModifierId> var;
  633. Lex::SemiTokenIndex token;
  634. };
  635. using ForHeaderStart =
  636. LeafNode<NodeKind::ForHeaderStart, Lex::OpenParenTokenIndex>;
  637. // The `... in` portion of a `for` statement.
  638. struct ForIn {
  639. static constexpr auto Kind = NodeKind::ForIn.Define({.child_count = 1});
  640. AnyPatternId pattern;
  641. Lex::InTokenIndex token;
  642. };
  643. // The `(... in ...)` portion of a `for` statement.
  644. struct ForHeader {
  645. static constexpr auto Kind =
  646. NodeKind::ForHeader.Define({.bracketed_by = ForHeaderStart::Kind});
  647. ForHeaderStartId introducer;
  648. ForInId var;
  649. AnyExprId range;
  650. Lex::CloseParenTokenIndex token;
  651. };
  652. // A complete `for (...) { ... }` statement.
  653. struct ForStatement {
  654. static constexpr auto Kind =
  655. NodeKind::ForStatement.Define({.category = NodeCategory::Statement,
  656. .bracketed_by = ForHeader::Kind,
  657. .child_count = 2});
  658. Lex::ForTokenIndex token;
  659. ForHeaderId header;
  660. CodeBlockId body;
  661. };
  662. using IfConditionStart =
  663. LeafNode<NodeKind::IfConditionStart, Lex::OpenParenTokenIndex>;
  664. // The condition portion of an `if` statement: `(expr)`.
  665. struct IfCondition {
  666. static constexpr auto Kind = NodeKind::IfCondition.Define(
  667. {.bracketed_by = IfConditionStart::Kind, .child_count = 2});
  668. IfConditionStartId left_paren;
  669. AnyExprId condition;
  670. Lex::CloseParenTokenIndex token;
  671. };
  672. using IfStatementElse =
  673. LeafNode<NodeKind::IfStatementElse, Lex::ElseTokenIndex>;
  674. // An `if` statement: `if (expr) { ... } else { ... }`.
  675. struct IfStatement {
  676. static constexpr auto Kind = NodeKind::IfStatement.Define(
  677. {.category = NodeCategory::Statement, .bracketed_by = IfCondition::Kind});
  678. Lex::IfTokenIndex token;
  679. IfConditionId head;
  680. CodeBlockId then;
  681. struct Else {
  682. IfStatementElseId else_token;
  683. NodeIdOneOf<CodeBlock, IfStatement> body;
  684. };
  685. std::optional<Else> else_clause;
  686. };
  687. using WhileConditionStart =
  688. LeafNode<NodeKind::WhileConditionStart, Lex::OpenParenTokenIndex>;
  689. // The condition portion of a `while` statement: `(expr)`.
  690. struct WhileCondition {
  691. static constexpr auto Kind = NodeKind::WhileCondition.Define(
  692. {.bracketed_by = WhileConditionStart::Kind, .child_count = 2});
  693. WhileConditionStartId left_paren;
  694. AnyExprId condition;
  695. Lex::CloseParenTokenIndex token;
  696. };
  697. // A `while` statement: `while (expr) { ... }`.
  698. struct WhileStatement {
  699. static constexpr auto Kind =
  700. NodeKind::WhileStatement.Define({.category = NodeCategory::Statement,
  701. .bracketed_by = WhileCondition::Kind,
  702. .child_count = 2});
  703. Lex::WhileTokenIndex token;
  704. WhileConditionId head;
  705. CodeBlockId body;
  706. };
  707. using MatchConditionStart =
  708. LeafNode<NodeKind::MatchConditionStart, Lex::OpenParenTokenIndex>;
  709. struct MatchCondition {
  710. static constexpr auto Kind = NodeKind::MatchCondition.Define(
  711. {.bracketed_by = MatchConditionStart::Kind, .child_count = 2});
  712. MatchConditionStartId left_paren;
  713. AnyExprId condition;
  714. Lex::CloseParenTokenIndex token;
  715. };
  716. using MatchIntroducer =
  717. LeafNode<NodeKind::MatchIntroducer, Lex::MatchTokenIndex>;
  718. struct MatchStatementStart {
  719. static constexpr auto Kind = NodeKind::MatchStatementStart.Define(
  720. {.bracketed_by = MatchIntroducer::Kind, .child_count = 2});
  721. MatchIntroducerId introducer;
  722. MatchConditionId condition;
  723. Lex::OpenCurlyBraceTokenIndex token;
  724. };
  725. using MatchCaseIntroducer =
  726. LeafNode<NodeKind::MatchCaseIntroducer, Lex::CaseTokenIndex>;
  727. using MatchCaseGuardIntroducer =
  728. LeafNode<NodeKind::MatchCaseGuardIntroducer, Lex::IfTokenIndex>;
  729. using MatchCaseGuardStart =
  730. LeafNode<NodeKind::MatchCaseGuardStart, Lex::OpenParenTokenIndex>;
  731. struct MatchCaseGuard {
  732. static constexpr auto Kind = NodeKind::MatchCaseGuard.Define(
  733. {.bracketed_by = MatchCaseGuardIntroducer::Kind, .child_count = 3});
  734. MatchCaseGuardIntroducerId introducer;
  735. MatchCaseGuardStartId left_paren;
  736. AnyExprId condition;
  737. Lex::CloseParenTokenIndex token;
  738. };
  739. struct MatchCase {
  740. static constexpr auto Kind =
  741. NodeKind::MatchCase.Define({.bracketed_by = MatchCaseIntroducer::Kind});
  742. MatchCaseIntroducerId introducer;
  743. AnyPatternId pattern;
  744. std::optional<MatchCaseGuardId> guard;
  745. Lex::EqualGreaterTokenIndex token;
  746. };
  747. using MatchDefaultIntroducer =
  748. LeafNode<NodeKind::MatchDefaultIntroducer, Lex::DefaultTokenIndex>;
  749. struct MatchDefault {
  750. static constexpr auto Kind = NodeKind::MatchDefault.Define(
  751. {.bracketed_by = MatchDefaultIntroducer::Kind, .child_count = 1});
  752. MatchDefaultIntroducerId introducer;
  753. Lex::EqualGreaterTokenIndex token;
  754. };
  755. struct MatchHandlerStart {
  756. static constexpr auto Kind =
  757. NodeKind::MatchHandlerStart.Define({.child_count = 1});
  758. NodeIdOneOf<MatchCase, MatchDefault> label;
  759. Lex::OpenCurlyBraceTokenIndex token;
  760. };
  761. struct MatchHandler {
  762. static constexpr auto Kind =
  763. NodeKind::MatchHandler.Define({.bracketed_by = MatchHandlerStart::Kind});
  764. MatchHandlerStartId head;
  765. llvm::SmallVector<AnyStatementId> statements;
  766. Lex::CloseCurlyBraceTokenIndex token;
  767. };
  768. // A `match` statement: `match (expr) { case (...) => {...} default => {...}}`.
  769. struct MatchStatement {
  770. static constexpr auto Kind = NodeKind::MatchStatement.Define(
  771. {.category = NodeCategory::Statement,
  772. .bracketed_by = MatchStatementStart::Kind});
  773. MatchStatementStartId head;
  774. llvm::SmallVector<MatchHandlerId> handlers;
  775. Lex::CloseCurlyBraceTokenIndex token;
  776. };
  777. // Expression nodes
  778. // ----------------
  779. using ArrayExprKeyword =
  780. LeafNode<NodeKind::ArrayExprKeyword, Lex::ArrayTokenIndex>;
  781. using ArrayExprOpenParen =
  782. LeafNode<NodeKind::ArrayExprOpenParen, Lex::OpenParenTokenIndex>;
  783. using ArrayExprComma = LeafNode<NodeKind::ArrayExprComma, Lex::CommaTokenIndex>;
  784. // An array type, `array(T, N)`.
  785. struct ArrayExpr {
  786. static constexpr auto Kind = NodeKind::ArrayExpr.Define(
  787. {.category = NodeCategory::Expr, .child_count = 5});
  788. ArrayExprKeywordId keyword;
  789. ArrayExprOpenParenId start;
  790. AnyExprId type;
  791. ArrayExprCommaId comma;
  792. AnyExprId bound;
  793. Lex::CloseParenTokenIndex token;
  794. };
  795. struct RefPrimitiveForm {
  796. static constexpr auto Kind = NodeKind::RefPrimitiveForm.Define(
  797. {.category = NodeCategory::Expr, .child_count = 1});
  798. Lex::RefTokenIndex token;
  799. AnyExprId type;
  800. };
  801. struct VarPrimitiveForm {
  802. static constexpr auto Kind = NodeKind::VarPrimitiveForm.Define(
  803. {.category = NodeCategory::Expr, .child_count = 1});
  804. Lex::VarTokenIndex token;
  805. AnyExprId type;
  806. };
  807. struct ValPrimitiveForm {
  808. static constexpr auto Kind = NodeKind::ValPrimitiveForm.Define(
  809. {.category = NodeCategory::Expr, .child_count = 1});
  810. Lex::ValTokenIndex token;
  811. AnyExprId type;
  812. };
  813. using FormLiteralKeyword =
  814. LeafNode<NodeKind::FormLiteralKeyword, Lex::FormTokenIndex>;
  815. using FormLiteralOpenParen =
  816. LeafNode<NodeKind::FormLiteralOpenParen, Lex::OpenParenTokenIndex>;
  817. // A `form` literal: `form(ref i32)`
  818. struct FormLiteral {
  819. static constexpr auto Kind = NodeKind::FormLiteral.Define(
  820. {.category = NodeCategory::Expr,
  821. .bracketed_by = NodeKind::FormLiteralKeyword,
  822. .child_count = 3});
  823. FormLiteralKeywordId keyword;
  824. FormLiteralOpenParenId start;
  825. AnyPrimitiveFormIdId category;
  826. Lex::CloseParenTokenIndex token;
  827. };
  828. // The opening portion of an indexing expression: `a[`.
  829. //
  830. // TODO: Consider flattening this into `IndexExpr`.
  831. struct IndexExprStart {
  832. static constexpr auto Kind =
  833. NodeKind::IndexExprStart.Define({.child_count = 1});
  834. AnyExprId sequence;
  835. Lex::OpenSquareBracketTokenIndex token;
  836. };
  837. // An indexing expression, such as `a[1]`.
  838. struct IndexExpr {
  839. static constexpr auto Kind =
  840. NodeKind::IndexExpr.Define({.category = NodeCategory::Expr,
  841. .bracketed_by = IndexExprStart::Kind,
  842. .child_count = 2});
  843. IndexExprStartId start;
  844. AnyExprId index;
  845. Lex::CloseSquareBracketTokenIndex token;
  846. };
  847. using ParenExprStart =
  848. LeafNode<NodeKind::ParenExprStart, Lex::OpenParenTokenIndex>;
  849. // A parenthesized expression: `(a)`.
  850. struct ParenExpr {
  851. static constexpr auto Kind = NodeKind::ParenExpr.Define(
  852. {.category = NodeCategory::Expr | NodeCategory::MemberExpr,
  853. .bracketed_by = ParenExprStart::Kind,
  854. .child_count = 2});
  855. ParenExprStartId start;
  856. AnyExprId expr;
  857. Lex::CloseParenTokenIndex token;
  858. };
  859. using TupleLiteralStart =
  860. LeafNode<NodeKind::TupleLiteralStart, Lex::OpenParenTokenIndex>;
  861. using TupleLiteralComma =
  862. LeafNode<NodeKind::TupleLiteralComma, Lex::CommaTokenIndex>;
  863. // A tuple literal: `()`, `(a, b, c)`, or `(a,)`.
  864. struct TupleLiteral {
  865. static constexpr auto Kind =
  866. NodeKind::TupleLiteral.Define({.category = NodeCategory::Expr,
  867. .bracketed_by = TupleLiteralStart::Kind});
  868. TupleLiteralStartId start;
  869. CommaSeparatedList<AnyExprId, TupleLiteralCommaId> elements;
  870. Lex::CloseParenTokenIndex token;
  871. };
  872. // The opening portion of a call expression: `F(`.
  873. //
  874. // TODO: Consider flattening this into `CallExpr`.
  875. struct CallExprStart {
  876. static constexpr auto Kind =
  877. NodeKind::CallExprStart.Define({.child_count = 1});
  878. AnyExprId callee;
  879. Lex::OpenParenTokenIndex token;
  880. };
  881. // A call expression: `F(a, b, c)`.
  882. struct CallExpr {
  883. static constexpr auto Kind = NodeKind::CallExpr.Define(
  884. {.category = NodeCategory::Expr, .bracketed_by = CallExprStart::Kind});
  885. CallExprStartId start;
  886. CommaSeparatedList<AnyExprId, TupleLiteralCommaId> arguments;
  887. Lex::CloseParenTokenIndex token;
  888. };
  889. // A member access expression: `a.b` or `a.(b)`.
  890. struct MemberAccessExpr {
  891. static constexpr auto Kind = NodeKind::MemberAccessExpr.Define(
  892. {.category = NodeCategory::Expr, .child_count = 2});
  893. AnyExprId lhs;
  894. Lex::PeriodTokenIndex token;
  895. AnyMemberAccessId rhs;
  896. };
  897. // An indirect member access expression: `a->b` or `a->(b)`.
  898. struct PointerMemberAccessExpr {
  899. static constexpr auto Kind = NodeKind::PointerMemberAccessExpr.Define(
  900. {.category = NodeCategory::Expr, .child_count = 2});
  901. AnyExprId lhs;
  902. Lex::MinusGreaterTokenIndex token;
  903. AnyMemberAccessId rhs;
  904. };
  905. // A prefix operator expression.
  906. template <const NodeKind& KindT, typename TokenKind>
  907. struct PrefixOperator {
  908. static constexpr auto Kind =
  909. KindT.Define({.category = NodeCategory::Expr, .child_count = 1});
  910. TokenKind token;
  911. AnyExprId operand;
  912. };
  913. // An infix operator expression.
  914. template <const NodeKind& KindT, typename TokenKind>
  915. struct InfixOperator {
  916. static constexpr auto Kind =
  917. KindT.Define({.category = NodeCategory::Expr, .child_count = 2});
  918. AnyExprId lhs;
  919. TokenKind token;
  920. AnyExprId rhs;
  921. };
  922. // A postfix operator expression.
  923. template <const NodeKind& KindT, typename TokenKind>
  924. struct PostfixOperator {
  925. static constexpr auto Kind =
  926. KindT.Define({.category = NodeCategory::Expr, .child_count = 1});
  927. AnyExprId operand;
  928. TokenKind token;
  929. };
  930. // An `unsafe` modifier: `a unsafe <operator> b`. This is modeled in the parse
  931. // tree as a postfix operator applied to `a`.
  932. struct UnsafeModifier {
  933. static constexpr auto Kind = NodeKind::UnsafeModifier.Define(
  934. {.category = NodeCategory::Expr, .child_count = 1});
  935. AnyExprId operand;
  936. Lex::UnsafeTokenIndex token;
  937. };
  938. // Literals, operators, and modifiers
  939. #define CARBON_PARSE_NODE_KIND(Name)
  940. #define CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(Name, LexTokenKind) \
  941. using Name = LeafNode<NodeKind::Name, Lex::LexTokenKind##TokenIndex, \
  942. NodeCategory::Expr>;
  943. #define CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Name) \
  944. using Name##Modifier = \
  945. LeafNode<NodeKind::Name##Modifier, Lex::Name##TokenIndex, \
  946. NodeCategory::Modifier>;
  947. #define CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Name) \
  948. using PrefixOperator##Name = \
  949. PrefixOperator<NodeKind::PrefixOperator##Name, Lex::Name##TokenIndex>;
  950. #define CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Name) \
  951. using InfixOperator##Name = \
  952. InfixOperator<NodeKind::InfixOperator##Name, Lex::Name##TokenIndex>;
  953. #define CARBON_PARSE_NODE_KIND_POSTFIX_OPERATOR(Name) \
  954. using PostfixOperator##Name = \
  955. PostfixOperator<NodeKind::PostfixOperator##Name, Lex::Name##TokenIndex>;
  956. #include "toolchain/parse/node_kind.def"
  957. using IntLiteral = LeafNode<NodeKind::IntLiteral, Lex::IntLiteralTokenIndex,
  958. NodeCategory::Expr | NodeCategory::IntConst>;
  959. // `extern` as a standalone modifier.
  960. using ExternModifier = LeafNode<NodeKind::ExternModifier, Lex::ExternTokenIndex,
  961. NodeCategory::Modifier>;
  962. // `extern library <owning_library>` modifiers.
  963. struct ExternModifierWithLibrary {
  964. static constexpr auto Kind = NodeKind::ExternModifierWithLibrary.Define(
  965. {.category = NodeCategory::Modifier, .child_count = 1});
  966. Lex::ExternTokenIndex token;
  967. LibrarySpecifierId library;
  968. };
  969. // The first operand of a short-circuiting infix operator: `a and` or `a or`.
  970. // The complete operator expression will be an InfixOperator with this as the
  971. // `lhs`.
  972. // TODO: Make this be a template if we ever need to write generic code to cover
  973. // both cases at once, say in check.
  974. struct ShortCircuitOperandAnd {
  975. static constexpr auto Kind =
  976. NodeKind::ShortCircuitOperandAnd.Define({.child_count = 1});
  977. AnyExprId operand;
  978. // This is a virtual token. The `and` token is owned by the
  979. // ShortCircuitOperatorAnd node.
  980. Lex::AndTokenIndex token;
  981. };
  982. struct ShortCircuitOperandOr {
  983. static constexpr auto Kind =
  984. NodeKind::ShortCircuitOperandOr.Define({.child_count = 1});
  985. AnyExprId operand;
  986. // This is a virtual token. The `or` token is owned by the
  987. // ShortCircuitOperatorOr node.
  988. Lex::OrTokenIndex token;
  989. };
  990. struct ShortCircuitOperatorAnd {
  991. static constexpr auto Kind = NodeKind::ShortCircuitOperatorAnd.Define(
  992. {.category = NodeCategory::Expr,
  993. .bracketed_by = ShortCircuitOperandAnd::Kind,
  994. .child_count = 2});
  995. ShortCircuitOperandAndId lhs;
  996. Lex::AndTokenIndex token;
  997. AnyExprId rhs;
  998. };
  999. struct ShortCircuitOperatorOr {
  1000. static constexpr auto Kind = NodeKind::ShortCircuitOperatorOr.Define(
  1001. {.category = NodeCategory::Expr,
  1002. .bracketed_by = ShortCircuitOperandOr::Kind,
  1003. .child_count = 2});
  1004. ShortCircuitOperandOrId lhs;
  1005. Lex::OrTokenIndex token;
  1006. AnyExprId rhs;
  1007. };
  1008. // The `if` portion of an `if` expression: `if expr`.
  1009. struct IfExprIf {
  1010. static constexpr auto Kind = NodeKind::IfExprIf.Define({.child_count = 1});
  1011. Lex::IfTokenIndex token;
  1012. AnyExprId condition;
  1013. };
  1014. // The `then` portion of an `if` expression: `then expr`.
  1015. struct IfExprThen {
  1016. static constexpr auto Kind = NodeKind::IfExprThen.Define({.child_count = 1});
  1017. Lex::ThenTokenIndex token;
  1018. AnyExprId result;
  1019. };
  1020. // A full `if` expression: `if expr then expr else expr`.
  1021. struct IfExprElse {
  1022. static constexpr auto Kind =
  1023. NodeKind::IfExprElse.Define({.category = NodeCategory::Expr,
  1024. .bracketed_by = IfExprIf::Kind,
  1025. .child_count = 3});
  1026. IfExprIfId start;
  1027. IfExprThenId then;
  1028. Lex::ElseTokenIndex token;
  1029. AnyExprId else_result;
  1030. };
  1031. // A `where` expression (TODO: `require` and `observe` declarations)
  1032. // The `Self` in a context where it is treated as a name rather than an
  1033. // expression, such as `.Self`.
  1034. using SelfTypeName =
  1035. LeafNode<NodeKind::SelfTypeName, Lex::SelfTypeIdentifierTokenIndex>;
  1036. // `.Member` or `.Self` in an expression context, used in `where` and `require`
  1037. // clauses.
  1038. // TODO: Do we want to support `.1`, a designator for accessing a tuple member?
  1039. struct DesignatorExpr {
  1040. static constexpr auto Kind = NodeKind::DesignatorExpr.Define(
  1041. {.category = NodeCategory::Expr, .child_count = 1});
  1042. Lex::PeriodTokenIndex token;
  1043. NodeIdOneOf<IdentifierNameNotBeforeSignature, SelfTypeName> name;
  1044. };
  1045. struct RequirementEqual {
  1046. static constexpr auto Kind = NodeKind::RequirementEqual.Define(
  1047. {.category = NodeCategory::Requirement, .child_count = 2});
  1048. DesignatorExprId lhs;
  1049. Lex::EqualTokenIndex token;
  1050. AnyExprId rhs;
  1051. };
  1052. struct RequirementEqualEqual {
  1053. static constexpr auto Kind = NodeKind::RequirementEqualEqual.Define(
  1054. {.category = NodeCategory::Requirement, .child_count = 2});
  1055. AnyExprId lhs;
  1056. Lex::EqualEqualTokenIndex token;
  1057. AnyExprId rhs;
  1058. };
  1059. struct RequirementImpls {
  1060. static constexpr auto Kind = NodeKind::RequirementImpls.Define(
  1061. {.category = NodeCategory::Requirement, .child_count = 2});
  1062. AnyExprId lhs;
  1063. Lex::ImplsTokenIndex token;
  1064. AnyExprId rhs;
  1065. };
  1066. // An `and` token separating requirements in a `where` expression.
  1067. using RequirementAnd = LeafNode<NodeKind::RequirementAnd, Lex::AndTokenIndex>;
  1068. struct WhereOperand {
  1069. static constexpr auto Kind =
  1070. NodeKind::WhereOperand.Define({.child_count = 1});
  1071. AnyExprId type;
  1072. // This is a virtual token. The `where` token is owned by the
  1073. // WhereExpr node.
  1074. Lex::WhereTokenIndex token;
  1075. };
  1076. struct WhereExpr {
  1077. static constexpr auto Kind = NodeKind::WhereExpr.Define(
  1078. {.category = NodeCategory::Expr, .bracketed_by = WhereOperand::Kind});
  1079. WhereOperandId introducer;
  1080. Lex::WhereTokenIndex token;
  1081. CommaSeparatedList<AnyRequirementId, RequirementAndId> requirements;
  1082. };
  1083. // Choice nodes
  1084. // ------------
  1085. using ChoiceIntroducer =
  1086. LeafNode<NodeKind::ChoiceIntroducer, Lex::ChoiceTokenIndex>;
  1087. struct ChoiceSignature {
  1088. static constexpr auto Kind = NodeKind::ChoiceDefinitionStart.Define(
  1089. {.category = NodeCategory::None, .bracketed_by = ChoiceIntroducer::Kind});
  1090. ChoiceIntroducerId introducer;
  1091. llvm::SmallVector<AnyModifierId> modifiers;
  1092. DeclName name;
  1093. Lex::OpenCurlyBraceTokenIndex token;
  1094. };
  1095. using ChoiceDefinitionStart = ChoiceSignature;
  1096. using ChoiceAlternativeListComma =
  1097. LeafNode<NodeKind::ChoiceAlternativeListComma, Lex::CommaTokenIndex>;
  1098. struct ChoiceDefinition {
  1099. static constexpr auto Kind = NodeKind::ChoiceDefinition.Define(
  1100. {.category = NodeCategory::Decl,
  1101. .bracketed_by = ChoiceDefinitionStart::Kind});
  1102. ChoiceDefinitionStartId signature;
  1103. struct Alternative {
  1104. AnyNonExprNameId name;
  1105. std::optional<ExplicitParamListId> parameters;
  1106. };
  1107. CommaSeparatedList<Alternative, ChoiceAlternativeListCommaId> alternatives;
  1108. Lex::CloseCurlyBraceTokenIndex token;
  1109. };
  1110. // Struct type and value literals
  1111. // ----------------------------------------
  1112. // `{`
  1113. using StructLiteralStart =
  1114. LeafNode<NodeKind::StructLiteralStart, Lex::OpenCurlyBraceTokenIndex>;
  1115. using StructTypeLiteralStart =
  1116. LeafNode<NodeKind::StructTypeLiteralStart, Lex::OpenCurlyBraceTokenIndex>;
  1117. // `,`
  1118. using StructLiteralComma =
  1119. LeafNode<NodeKind::StructLiteralComma, Lex::CommaTokenIndex>;
  1120. using StructTypeLiteralComma =
  1121. LeafNode<NodeKind::StructTypeLiteralComma, Lex::CommaTokenIndex>;
  1122. // `.a`
  1123. // This is shared for struct literals and type literals in order to reduce
  1124. // lookahead for parse (the `=` versus `:` would require lookahead of 2).
  1125. struct StructFieldDesignator {
  1126. static constexpr auto Kind =
  1127. NodeKind::StructFieldDesignator.Define({.child_count = 1});
  1128. Lex::PeriodTokenIndex token;
  1129. NodeIdOneOf<IdentifierNameNotBeforeSignature, BaseName> name;
  1130. };
  1131. // `.a = 0`
  1132. struct StructLiteralField {
  1133. static constexpr auto Kind = NodeKind::StructLiteralField.Define(
  1134. {.bracketed_by = StructFieldDesignator::Kind, .child_count = 2});
  1135. StructFieldDesignatorId designator;
  1136. Lex::EqualTokenIndex token;
  1137. AnyExprId expr;
  1138. };
  1139. // `.a: i32`
  1140. struct StructTypeLiteralField {
  1141. static constexpr auto Kind = NodeKind::StructTypeLiteralField.Define(
  1142. {.bracketed_by = StructFieldDesignator::Kind, .child_count = 2});
  1143. StructFieldDesignatorId designator;
  1144. Lex::ColonTokenIndex token;
  1145. AnyExprId type_expr;
  1146. };
  1147. // Struct literals, such as `{.a = 0}`.
  1148. struct StructLiteral {
  1149. static constexpr auto Kind = NodeKind::StructLiteral.Define(
  1150. {.category = NodeCategory::Expr,
  1151. .bracketed_by = StructLiteralStart::Kind});
  1152. StructLiteralStartId start;
  1153. CommaSeparatedList<StructLiteralFieldId, StructLiteralCommaId> fields;
  1154. Lex::CloseCurlyBraceTokenIndex token;
  1155. };
  1156. // Struct type literals, such as `{.a: i32}`.
  1157. struct StructTypeLiteral {
  1158. static constexpr auto Kind = NodeKind::StructTypeLiteral.Define(
  1159. {.category = NodeCategory::Expr,
  1160. .bracketed_by = StructTypeLiteralStart::Kind});
  1161. StructTypeLiteralStartId start;
  1162. CommaSeparatedList<StructTypeLiteralFieldId, StructTypeLiteralCommaId> fields;
  1163. Lex::CloseCurlyBraceTokenIndex token;
  1164. };
  1165. // `class` declarations and definitions
  1166. // ------------------------------------
  1167. // `class`
  1168. using ClassIntroducer =
  1169. LeafNode<NodeKind::ClassIntroducer, Lex::ClassTokenIndex>;
  1170. // A class signature `class C`
  1171. template <const NodeKind& KindT, typename TokenKind,
  1172. NodeCategory::RawEnumType Category>
  1173. struct ClassSignature {
  1174. static constexpr auto Kind = KindT.Define(
  1175. {.category = Category, .bracketed_by = ClassIntroducer::Kind});
  1176. ClassIntroducerId introducer;
  1177. llvm::SmallVector<AnyModifierId> modifiers;
  1178. DeclName name;
  1179. TokenKind token;
  1180. };
  1181. // `class C;`
  1182. using ClassDecl = ClassSignature<NodeKind::ClassDecl, Lex::SemiTokenIndex,
  1183. NodeCategory::Decl>;
  1184. // `class C {`
  1185. using ClassDefinitionStart =
  1186. ClassSignature<NodeKind::ClassDefinitionStart,
  1187. Lex::OpenCurlyBraceTokenIndex, NodeCategory::None>;
  1188. // `class C { ... }`
  1189. struct ClassDefinition {
  1190. static constexpr auto Kind = NodeKind::ClassDefinition.Define(
  1191. {.category = NodeCategory::Decl,
  1192. .bracketed_by = ClassDefinitionStart::Kind});
  1193. ClassDefinitionStartId signature;
  1194. llvm::SmallVector<AnyDeclId> members;
  1195. Lex::CloseCurlyBraceTokenIndex token;
  1196. };
  1197. // Adapter declaration
  1198. // -------------------
  1199. // `adapt`
  1200. using AdaptIntroducer =
  1201. LeafNode<NodeKind::AdaptIntroducer, Lex::AdaptTokenIndex>;
  1202. // `adapt SomeType;`
  1203. struct AdaptDecl {
  1204. static constexpr auto Kind = NodeKind::AdaptDecl.Define(
  1205. {.category = NodeCategory::Decl, .bracketed_by = AdaptIntroducer::Kind});
  1206. AdaptIntroducerId introducer;
  1207. llvm::SmallVector<AnyModifierId> modifiers;
  1208. AnyExprId adapted_type;
  1209. Lex::SemiTokenIndex token;
  1210. };
  1211. // Base class declaration
  1212. // ----------------------
  1213. // `base`
  1214. using BaseIntroducer = LeafNode<NodeKind::BaseIntroducer, Lex::BaseTokenIndex>;
  1215. using BaseColon = LeafNode<NodeKind::BaseColon, Lex::ColonTokenIndex>;
  1216. // `extend base: BaseClass;`
  1217. struct BaseDecl {
  1218. static constexpr auto Kind = NodeKind::BaseDecl.Define(
  1219. {.category = NodeCategory::Decl, .bracketed_by = BaseIntroducer::Kind});
  1220. BaseIntroducerId introducer;
  1221. llvm::SmallVector<AnyModifierId> modifiers;
  1222. BaseColonId colon;
  1223. AnyExprId base_class;
  1224. Lex::SemiTokenIndex token;
  1225. };
  1226. // Interface declarations and definitions
  1227. // --------------------------------------
  1228. // `interface`
  1229. using InterfaceIntroducer =
  1230. LeafNode<NodeKind::InterfaceIntroducer, Lex::InterfaceTokenIndex>;
  1231. // `interface I`
  1232. template <const NodeKind& KindT, typename TokenKind,
  1233. NodeCategory::RawEnumType Category>
  1234. struct InterfaceSignature {
  1235. static constexpr auto Kind = KindT.Define(
  1236. {.category = Category, .bracketed_by = InterfaceIntroducer::Kind});
  1237. InterfaceIntroducerId introducer;
  1238. llvm::SmallVector<AnyModifierId> modifiers;
  1239. DeclName name;
  1240. TokenKind token;
  1241. };
  1242. // `interface I;`
  1243. using InterfaceDecl =
  1244. InterfaceSignature<NodeKind::InterfaceDecl, Lex::SemiTokenIndex,
  1245. NodeCategory::Decl>;
  1246. // `interface I {`
  1247. using InterfaceDefinitionStart =
  1248. InterfaceSignature<NodeKind::InterfaceDefinitionStart,
  1249. Lex::OpenCurlyBraceTokenIndex, NodeCategory::None>;
  1250. // `interface I { ... }`
  1251. struct InterfaceDefinition {
  1252. static constexpr auto Kind = NodeKind::InterfaceDefinition.Define(
  1253. {.category = NodeCategory::Decl,
  1254. .bracketed_by = InterfaceDefinitionStart::Kind});
  1255. InterfaceDefinitionStartId signature;
  1256. llvm::SmallVector<AnyDeclId> members;
  1257. Lex::CloseCurlyBraceTokenIndex token;
  1258. };
  1259. // `require`...`impls` statements
  1260. // ------------------------------
  1261. // `require`
  1262. using RequireIntroducer =
  1263. LeafNode<NodeKind::RequireIntroducer, Lex::RequireTokenIndex>;
  1264. // `impls` with no type before it
  1265. using RequireDefaultSelfImpls =
  1266. LeafNode<NodeKind::RequireDefaultSelfImpls, Lex::ImplsTokenIndex,
  1267. NodeCategory::RequireImpls>;
  1268. // `<type> impls`.
  1269. struct RequireTypeImpls {
  1270. static constexpr auto Kind = NodeKind::RequireTypeImpls.Define(
  1271. {.category = NodeCategory::RequireImpls, .child_count = 1});
  1272. AnyExprId type_expr;
  1273. Lex::ImplsTokenIndex token;
  1274. };
  1275. // `require T impls I where...`
  1276. struct RequireDecl {
  1277. static constexpr auto Kind =
  1278. NodeKind::RequireDecl.Define({.category = NodeCategory::Decl,
  1279. .bracketed_by = RequireIntroducer::Kind});
  1280. RequireIntroducerId introducer;
  1281. llvm::SmallVector<AnyModifierId> modifiers;
  1282. AnyRequireImplsId impls;
  1283. AnyExprId facet_type;
  1284. Lex::SemiTokenIndex token;
  1285. };
  1286. // `observe` declarations
  1287. // --------------------------------
  1288. // `observe`
  1289. using ObserveIntroducer =
  1290. LeafNode<NodeKind::ObserveIntroducer, Lex::ObserveTokenIndex>;
  1291. // `==`
  1292. struct ObserveEqualEqual {
  1293. static constexpr auto Kind = NodeKind::ObserveEqualEqual.Define(
  1294. {.category = NodeCategory::ObserveOperator, .child_count = 2});
  1295. AnyObserveOperandId lhs;
  1296. Lex::EqualEqualTokenIndex token;
  1297. AnyExprId rhs;
  1298. };
  1299. // `impls`
  1300. struct ObserveImpls {
  1301. static constexpr auto Kind = NodeKind::ObserveImpls.Define(
  1302. {.category = NodeCategory::ObserveOperator, .child_count = 2});
  1303. AnyObserveOperandId lhs;
  1304. Lex::ImplsTokenIndex token;
  1305. AnyExprId rhs;
  1306. };
  1307. // `observe T == U impls I`
  1308. struct ObserveDecl {
  1309. static constexpr auto Kind =
  1310. NodeKind::ObserveDecl.Define({.category = NodeCategory::Decl,
  1311. .bracketed_by = ObserveIntroducer::Kind});
  1312. ObserveIntroducerId introducer;
  1313. llvm::SmallVector<AnyModifierId> modifiers;
  1314. AnyObserveOperatorId op;
  1315. Lex::SemiTokenIndex token;
  1316. };
  1317. // `impl`...`as` declarations and definitions
  1318. // ------------------------------------------
  1319. // `impl`
  1320. using ImplIntroducer = LeafNode<NodeKind::ImplIntroducer, Lex::ImplTokenIndex>;
  1321. // `forall`
  1322. using Forall = LeafNode<NodeKind::Forall, Lex::ForallTokenIndex>;
  1323. // `forall [...]`
  1324. struct ImplForall {
  1325. ForallId forall;
  1326. ImplicitParamListId params;
  1327. };
  1328. // `as` with no type before it
  1329. using ImplDefaultSelfAs = LeafNode<NodeKind::ImplDefaultSelfAs,
  1330. Lex::AsTokenIndex, NodeCategory::ImplAs>;
  1331. // `<type> as`
  1332. struct ImplTypeAs {
  1333. static constexpr auto Kind = NodeKind::ImplTypeAs.Define(
  1334. {.category = NodeCategory::ImplAs, .child_count = 1});
  1335. AnyExprId type_expr;
  1336. Lex::AsTokenIndex token;
  1337. };
  1338. // `impl T as I`
  1339. template <const NodeKind& KindT, typename TokenKind,
  1340. NodeCategory::RawEnumType Category>
  1341. struct ImplSignature {
  1342. static constexpr auto Kind = KindT.Define(
  1343. {.category = Category, .bracketed_by = ImplIntroducer::Kind});
  1344. ImplIntroducerId introducer;
  1345. llvm::SmallVector<AnyModifierId> modifiers;
  1346. std::optional<ImplForall> forall;
  1347. AnyImplAsId as;
  1348. AnyExprId interface;
  1349. TokenKind token;
  1350. };
  1351. // `impl T as I;`
  1352. using ImplDecl =
  1353. ImplSignature<NodeKind::ImplDecl, Lex::SemiTokenIndex, NodeCategory::Decl>;
  1354. // `impl T as I {`
  1355. using ImplDefinitionStart =
  1356. ImplSignature<NodeKind::ImplDefinitionStart, Lex::OpenCurlyBraceTokenIndex,
  1357. NodeCategory::None>;
  1358. // `impl T as I { ... }`
  1359. struct ImplDefinition {
  1360. static constexpr auto Kind = NodeKind::ImplDefinition.Define(
  1361. {.category = NodeCategory::Decl,
  1362. .bracketed_by = ImplDefinitionStart::Kind});
  1363. ImplDefinitionStartId signature;
  1364. llvm::SmallVector<AnyDeclId> members;
  1365. Lex::CloseCurlyBraceTokenIndex token;
  1366. };
  1367. // Named constraint declarations and definitions
  1368. // ---------------------------------------------
  1369. // `constraint`
  1370. using NamedConstraintIntroducer =
  1371. LeafNode<NodeKind::NamedConstraintIntroducer, Lex::ConstraintTokenIndex>;
  1372. // `constraint NC`
  1373. template <const NodeKind& KindT, typename TokenKind,
  1374. NodeCategory::RawEnumType Category>
  1375. struct NamedConstraintSignature {
  1376. static constexpr auto Kind = KindT.Define(
  1377. {.category = Category, .bracketed_by = NamedConstraintIntroducer::Kind});
  1378. NamedConstraintIntroducerId introducer;
  1379. llvm::SmallVector<AnyModifierId> modifiers;
  1380. DeclName name;
  1381. TokenKind token;
  1382. };
  1383. // `constraint NC;`
  1384. using NamedConstraintDecl =
  1385. NamedConstraintSignature<NodeKind::NamedConstraintDecl, Lex::SemiTokenIndex,
  1386. NodeCategory::Decl>;
  1387. // `constraint NC {`
  1388. using NamedConstraintDefinitionStart =
  1389. NamedConstraintSignature<NodeKind::NamedConstraintDefinitionStart,
  1390. Lex::OpenCurlyBraceTokenIndex, NodeCategory::None>;
  1391. // `constraint NC { ... }`
  1392. struct NamedConstraintDefinition {
  1393. static constexpr auto Kind = NodeKind::NamedConstraintDefinition.Define(
  1394. {.category = NodeCategory::Decl,
  1395. .bracketed_by = NamedConstraintDefinitionStart::Kind});
  1396. NamedConstraintDefinitionStartId signature;
  1397. llvm::SmallVector<AnyDeclId> members;
  1398. Lex::CloseCurlyBraceTokenIndex token;
  1399. };
  1400. // ---------------------------------------------------------------------------
  1401. // A complete source file. Note that there is no corresponding parse node for
  1402. // the file. The file is instead the complete contents of the parse tree.
  1403. struct File {
  1404. FileStartId start;
  1405. llvm::SmallVector<AnyDeclId> decls;
  1406. FileEndId end;
  1407. };
  1408. // Define `Foo` as the node type for the ID type `FooId`.
  1409. #define CARBON_PARSE_NODE_KIND(KindName) \
  1410. template <> \
  1411. struct NodeForId<KindName##Id> { \
  1412. using TypedNode = KindName; \
  1413. };
  1414. #include "toolchain/parse/node_kind.def"
  1415. } // namespace Carbon::Parse
  1416. #endif // CARBON_TOOLCHAIN_PARSE_TYPED_NODES_H_