typed_nodes.h 52 KB

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