typed_nodes.h 56 KB

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