node_kind.def 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. //
  5. // This is an X-macro header. It does not use `#include` guards, and instead is
  6. // designed to be `#include`ed after the x-macro is defined in order for its
  7. // inclusion to expand to the desired output. Macro definitions are cleaned up
  8. // at the end of this file.
  9. //
  10. // Supported x-macros are:
  11. // - CARBON_PARSE_NODE_KIND(Name)
  12. // Used as a fallback if other macros are missing.
  13. // - CARBON_PARSE_NODE_KIND_BRACKET(Name, BracketName, LexTokenKind)
  14. // Defines a bracketed node kind. BracketName should refer to the node
  15. // kind that is the _start_ of the bracketed range.
  16. // - CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, ChildCount, LexTokenKind)
  17. // Defines a parse node with a set number of children, often 0. This count
  18. // must be correct even when the node contains errors.
  19. // - CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Name)
  20. // Defines a parse node for a prefix operator, with the Name as token.
  21. // - CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Name)
  22. // Defines a parse node for an infix operator, with the Name as token.
  23. // - CARBON_PARSE_NODE_KIND_POSTFIX_OPERATOR(Name)
  24. // Defines a parse node for a postfix operator, with the Name as token.
  25. // - CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(Name, LexTokenKind)
  26. // Defines a parse node that corresponds to a token that is a single-token
  27. // literal. The token is wrapped for LexTokenKinds.
  28. // - CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Name)
  29. // A token-based modifier. The Name is the TokenKind, and will be appended
  30. // with "Modifier" for the parse kind.
  31. //
  32. // LexTokenKind indicates the token kind required on a valid node.
  33. // It will be either `kind_name` to indicate it's required even if the node is
  34. // an error, or `CARBON_IF_VALID(kind_name)` to indicate that the kind is
  35. // not enforced on error. A token kind of `Error` may be used if the kind should
  36. // never be enforced; this should only be used when the node is either not in
  37. // trees (`Placeholder`) or is always invalid (such as `InvalidParse`).
  38. //
  39. // This tree represents the subset relationship between these macros, where if a
  40. // specific x-macro isn't defined, it'll fall back to the parent macro.
  41. //
  42. // Parse nodes are clustered based on language feature. Comments will show their
  43. // relationship in postorder, using indentation for child node relationships.
  44. #if !(defined(CARBON_PARSE_NODE_KIND) || \
  45. (defined(CARBON_PARSE_NODE_KIND_BRACKET) && \
  46. defined(CARBON_PARSE_NODE_KIND_CHILD_COUNT)))
  47. #error "Must define CARBON_PARSE_NODE_KIND family x-macros to use this file."
  48. #endif
  49. // The BRACKET and CHILD_COUNT macros will use CARBON_PARSE_NODE_KIND by default
  50. // when undefined.
  51. #ifndef CARBON_PARSE_NODE_KIND_BRACKET
  52. #define CARBON_PARSE_NODE_KIND_BRACKET(Name, ...) CARBON_PARSE_NODE_KIND(Name)
  53. #endif
  54. #ifndef CARBON_PARSE_NODE_KIND_CHILD_COUNT
  55. #define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, ...) \
  56. CARBON_PARSE_NODE_KIND(Name)
  57. #endif
  58. // This is expected to be used with something like:
  59. //
  60. // // Use x-macros to handle modifier cases.
  61. // #define CARBON_PARSE_NODE_KIND(...)
  62. // #define CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Name, ...) <code>
  63. #ifndef CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR
  64. #define CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Name) \
  65. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PrefixOperator##Name, 1, Name)
  66. #endif
  67. // This is expected to be used with something like:
  68. //
  69. // // Use x-macros to handle modifier cases.
  70. // #define CARBON_PARSE_NODE_KIND(...)
  71. // #define CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Name, ...) <code>
  72. #ifndef CARBON_PARSE_NODE_KIND_INFIX_OPERATOR
  73. #define CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Name) \
  74. CARBON_PARSE_NODE_KIND_CHILD_COUNT(InfixOperator##Name, 2, Name)
  75. #endif
  76. // This is expected to be used with something like:
  77. //
  78. // // Use x-macros to handle modifier cases.
  79. // #define CARBON_PARSE_NODE_KIND(...)
  80. // #define CARBON_PARSE_NODE_KIND_POSTFIX_OPERATOR(Name, ...) <code>
  81. #ifndef CARBON_PARSE_NODE_KIND_POSTFIX_OPERATOR
  82. #define CARBON_PARSE_NODE_KIND_POSTFIX_OPERATOR(Name) \
  83. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PostfixOperator##Name, 1, Name)
  84. #endif
  85. // This is expected to be used with something like:
  86. //
  87. // // Use x-macros to handle literal cases.
  88. // #define CARBON_PARSE_NODE_KIND(...)
  89. // #define CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(Name, ...) <code>
  90. #ifndef CARBON_PARSE_NODE_KIND_TOKEN_LITERAL
  91. #define CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(Name, LexTokenKinds) \
  92. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, 0, LexTokenKinds)
  93. #endif
  94. // This is expected to be used with something like:
  95. //
  96. // // Use x-macros to handle modifier cases.
  97. // #define CARBON_PARSE_NODE_KIND(...)
  98. // #define CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Name, ...) <code>
  99. #ifndef CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER
  100. #define CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Name) \
  101. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name##Modifier, 0, Name)
  102. #endif
  103. // The start of the file.
  104. CARBON_PARSE_NODE_KIND_CHILD_COUNT(FileStart, 0, FileStart)
  105. // The end of the file.
  106. CARBON_PARSE_NODE_KIND_CHILD_COUNT(FileEnd, 0, FileEnd)
  107. // An invalid parse. Used to balance the parse tree. Always has an error.
  108. CARBON_PARSE_NODE_KIND_CHILD_COUNT(InvalidParse, 0, Error)
  109. // An invalid subtree. Always has an error.
  110. CARBON_PARSE_NODE_KIND_CHILD_COUNT(InvalidParseStart, 0, Error)
  111. CARBON_PARSE_NODE_KIND_BRACKET(InvalidParseSubtree, InvalidParseStart, Error)
  112. // A placeholder node to be replaced; it will never exist in a valid parse tree.
  113. // Its token kind is not enforced even when valid.
  114. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Placeholder, 0, Error)
  115. // An empty declaration, such as `;`.
  116. CARBON_PARSE_NODE_KIND_CHILD_COUNT(EmptyDecl, 0, CARBON_IF_VALID(Semi))
  117. // An identifier name in a non-expression context, such as a declaration.
  118. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IdentifierName, 0,
  119. CARBON_IF_VALID(Identifier))
  120. // An identifier name in an expression context.
  121. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IdentifierNameExpr, 0, Identifier)
  122. // The `self` value and `Self` type identifier keywords. Typically of the form
  123. // `self: Self`.
  124. CARBON_PARSE_NODE_KIND_CHILD_COUNT(SelfValueName, 0, SelfValueIdentifier)
  125. CARBON_PARSE_NODE_KIND_CHILD_COUNT(SelfValueNameExpr, 0, SelfValueIdentifier)
  126. CARBON_PARSE_NODE_KIND_CHILD_COUNT(SelfTypeNameExpr, 0, SelfTypeIdentifier)
  127. // The `base` value keyword, introduced by `base: B`. Typically referenced in
  128. // an expression, as in `x.base` or `{.base = ...}`, but can also be used as a
  129. // declared name, as in `{.base: partial B}`.
  130. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BaseName, 0, Base)
  131. // The `package` keyword in an expression.
  132. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageExpr, 0, Package)
  133. // ----------------------------------------------------------------------------
  134. // The comments below follow this pattern:
  135. //
  136. // // Descriptive heading:
  137. // // Child1
  138. // // Child2
  139. // // Parent
  140. //
  141. // In this, `Child1`, `Child2`, and `Parent` are all kinds of parse nodes, which
  142. // are then defined using the `CARBON_PARSE_NODE_KIND_*` macros. They are
  143. // written in postorder, with the indentation showing the tree structure. See
  144. // tree.h for more information.
  145. //
  146. // A parse node kind may be preceded by:
  147. // - `_optional_` if this node (or nodes) may be present or omitted in valid
  148. // parses, depending on which tokens are in the source code.
  149. // - `_repeated_` if this node (or nodes) may be repeated or omitted in valid
  150. // parses, depending on which tokens are in the source code.
  151. // - `_external_:` if this node is the child of multiple kinds of nodes and
  152. // is documented separately.
  153. //
  154. // There is generally a close correspondence between handling of tokens and the
  155. // creation of non-external nodes in a given block.
  156. // ----------------------------------------------------------------------------
  157. // The name of a package or library for `package`, `import`, and `library`.
  158. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageName, 0, Identifier)
  159. CARBON_PARSE_NODE_KIND_CHILD_COUNT(LibraryName, 0, StringLiteral)
  160. // `package`:
  161. // PackageIntroducer
  162. // _optional_ _external_: PackageName
  163. // _optional_ _external_: LibrarySpecifier
  164. // PackageApi or PackageImpl
  165. // PackageDirective
  166. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageIntroducer, 0, Package)
  167. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageApi, 0, Api)
  168. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageImpl, 0, Impl)
  169. CARBON_PARSE_NODE_KIND_BRACKET(PackageDirective, PackageIntroducer,
  170. CARBON_IF_VALID(Semi))
  171. // `import`:
  172. // ImportIntroducer
  173. // _optional_ _external_: PackageName
  174. // _optional_ _external_: LibrarySpecifier
  175. // ImportDirective
  176. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ImportIntroducer, 0, Import)
  177. CARBON_PARSE_NODE_KIND_BRACKET(ImportDirective, ImportIntroducer,
  178. CARBON_IF_VALID(Semi))
  179. // `library` as directive:
  180. // LibraryIntroducer
  181. // DefaultLibrary or _external_: LibraryName
  182. // PackageApi or PackageImpl
  183. // LibraryDirective
  184. CARBON_PARSE_NODE_KIND_CHILD_COUNT(DefaultLibrary, 0, Default)
  185. CARBON_PARSE_NODE_KIND_CHILD_COUNT(LibraryIntroducer, 0, Library)
  186. CARBON_PARSE_NODE_KIND_BRACKET(LibraryDirective, LibraryIntroducer,
  187. CARBON_IF_VALID(Semi))
  188. // `library` in `package` or `import`:
  189. // _external_: LibraryName or DefaultLibrary
  190. // LibrarySpecifier
  191. CARBON_PARSE_NODE_KIND_CHILD_COUNT(LibrarySpecifier, 1, Library)
  192. // `namespace`:
  193. // NamespaceStart
  194. // _repeated_ _external_: modifier
  195. // _external_: IdentifierName or QualifiedName
  196. // Namespace
  197. CARBON_PARSE_NODE_KIND_CHILD_COUNT(NamespaceStart, 0, Namespace)
  198. CARBON_PARSE_NODE_KIND_BRACKET(Namespace, NamespaceStart, CARBON_IF_VALID(Semi))
  199. // A code block:
  200. // CodeBlockStart
  201. // _repeated_ _external_: statement
  202. // CodeBlock
  203. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CodeBlockStart, 0,
  204. CARBON_IF_VALID(OpenCurlyBrace))
  205. CARBON_PARSE_NODE_KIND_BRACKET(CodeBlock, CodeBlockStart,
  206. CARBON_IF_VALID(CloseCurlyBrace))
  207. // `fn`:
  208. // FunctionIntroducer
  209. // _repeated_ _external_: modifier
  210. // _external_: IdentifierName or QualifiedName
  211. // _optional_ _external_: ImplicitParamList
  212. // _external_: TuplePattern
  213. // _external_: type expression
  214. // ReturnType
  215. // FunctionDefinitionStart
  216. // _repeated_ _external_: statement
  217. // FunctionDefinition
  218. //
  219. // The above is the structure for a definition; for a declaration,
  220. // FunctionDefinitionStart and later nodes are removed and replaced by
  221. // FunctionDecl.
  222. CARBON_PARSE_NODE_KIND_CHILD_COUNT(FunctionIntroducer, 0, Fn)
  223. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnType, 1, MinusGreater)
  224. CARBON_PARSE_NODE_KIND_BRACKET(FunctionDefinitionStart, FunctionIntroducer,
  225. OpenCurlyBrace)
  226. CARBON_PARSE_NODE_KIND_BRACKET(FunctionDefinition, FunctionDefinitionStart,
  227. CloseCurlyBrace)
  228. CARBON_PARSE_NODE_KIND_BRACKET(FunctionDecl, FunctionIntroducer,
  229. CARBON_IF_VALID(Semi))
  230. // `alias`:
  231. // AliasIntroducer
  232. // _repeated_ _external_: modifier
  233. // _external_: IdentifierName or QualifiedName
  234. // AliasInitializer
  235. // _external_: expression
  236. // Alias
  237. CARBON_PARSE_NODE_KIND_CHILD_COUNT(AliasIntroducer, 0, Alias)
  238. CARBON_PARSE_NODE_KIND_CHILD_COUNT(AliasInitializer, 0, Equal)
  239. CARBON_PARSE_NODE_KIND_BRACKET(Alias, AliasIntroducer, CARBON_IF_VALID(Semi))
  240. // A tuple pattern:
  241. // TuplePatternStart
  242. // _external_: [Generic]BindingPattern
  243. // PatternListComma
  244. // _repeated_
  245. // TuplePattern
  246. //
  247. // Patterns and PatternListComma may repeat with PatternListComma as a
  248. // separator.
  249. CARBON_PARSE_NODE_KIND_CHILD_COUNT(TuplePatternStart, 0, OpenParen)
  250. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PatternListComma, 0, Comma)
  251. CARBON_PARSE_NODE_KIND_BRACKET(TuplePattern, TuplePatternStart, CloseParen)
  252. // An implicit parameter list:
  253. // ImplicitParamListStart
  254. // _external_: [Generic]BindingPattern
  255. // PatternListComma
  256. // _repeated_
  257. // ImplicitParamList
  258. //
  259. // Patterns and PatternListComma may repeat with PatternListComma as a
  260. // separator.
  261. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ImplicitParamListStart, 0, OpenSquareBracket)
  262. CARBON_PARSE_NODE_KIND_BRACKET(ImplicitParamList, ImplicitParamListStart,
  263. CloseSquareBracket)
  264. // An array type, such as `[i32; 3]` or `[i32;]`:
  265. // ArrayExprStart
  266. // _external_: type expression
  267. // ArrayExprSemi
  268. // _optional_ _external_: expression
  269. // ArrayExpr
  270. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ArrayExprStart, 0, OpenSquareBracket)
  271. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ArrayExprSemi, 2, CARBON_IF_VALID(Semi))
  272. CARBON_PARSE_NODE_KIND_BRACKET(ArrayExpr, ArrayExprSemi, CloseSquareBracket)
  273. // A binding pattern, such as `name: Type`:
  274. // IdentifierName or SelfValueName
  275. // _external_: type expression
  276. // [Generic]BindingPattern
  277. // _optional_ Addr
  278. // _optional_ Template
  279. //
  280. // TODO: Rename GenericBindingPattern to CompileTimeBindingPattern.
  281. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BindingPattern, 2, CARBON_IF_VALID(Colon))
  282. CARBON_PARSE_NODE_KIND_CHILD_COUNT(GenericBindingPattern, 2, ColonExclaim)
  283. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Addr, 1, Addr)
  284. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Template, 1, Template)
  285. // `let`:
  286. // LetIntroducer
  287. // _repeated_ _external_: modifier
  288. // _external_: BindingPattern or TuplePattern
  289. // LetInitializer
  290. // _external_: expression
  291. // LetDecl
  292. //
  293. // Modifier keywords only appear for `let` declarations, not `let` statements.
  294. CARBON_PARSE_NODE_KIND_CHILD_COUNT(LetIntroducer, 0, Let)
  295. CARBON_PARSE_NODE_KIND_CHILD_COUNT(LetInitializer, 0, Equal)
  296. CARBON_PARSE_NODE_KIND_BRACKET(LetDecl, LetIntroducer, CARBON_IF_VALID(Semi))
  297. // `var` and `returned var`:
  298. // VariableIntroducer
  299. // _repeated_ _external_: modifier
  300. // _optional_ ReturnedModifier
  301. // _external_: BindingPattern or TuplePattern
  302. // VariableInitializer
  303. // _external_: expression
  304. // _optional_
  305. // VariableDecl
  306. //
  307. // Access and declaration modifier keywords only appear for `var` declarations,
  308. // whereas the returned modifier only appears on `var` statements.
  309. //
  310. // The VariableInitializer and following expression are paired: either both will
  311. // be present, or neither will.
  312. CARBON_PARSE_NODE_KIND_CHILD_COUNT(VariableIntroducer, 0, CARBON_IF_VALID(Var))
  313. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnedModifier, 0, Returned)
  314. CARBON_PARSE_NODE_KIND_CHILD_COUNT(VariableInitializer, 0, Equal)
  315. CARBON_PARSE_NODE_KIND_BRACKET(VariableDecl, VariableIntroducer,
  316. CARBON_IF_VALID(Semi))
  317. // An expression statement:
  318. // _external_: expression
  319. // ExprStatement
  320. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ExprStatement, 1, CARBON_IF_VALID(Semi))
  321. // `break`:
  322. // BreakStatementStart
  323. // BreakStatement
  324. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BreakStatementStart, 0, Break)
  325. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BreakStatement, 1, CARBON_IF_VALID(Semi))
  326. // `continue`:
  327. // ContinueStatementStart
  328. // ContinueStatement
  329. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ContinueStatementStart, 0, Continue)
  330. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ContinueStatement, 1, CARBON_IF_VALID(Semi))
  331. // `return`:
  332. // ReturnStatementStart
  333. // _optional_ ReturnVarModifier or _external_: expression
  334. // ReturnStatement
  335. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnStatementStart, 0, Return)
  336. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnVarModifier, 0, Var)
  337. CARBON_PARSE_NODE_KIND_BRACKET(ReturnStatement, ReturnStatementStart,
  338. CARBON_IF_VALID(Semi))
  339. // `for`:
  340. // ForHeaderStart
  341. // VariableIntroducer
  342. // _external_: BindingPattern
  343. // ForIn
  344. // _external_: expression
  345. // ForHeader
  346. // _external_: CodeBlock
  347. // ForStatement
  348. //
  349. // Versus a normal `var`, ForIn replaces VariableDecl.
  350. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ForHeaderStart, 0,
  351. CARBON_IF_VALID(OpenParen))
  352. CARBON_PARSE_NODE_KIND_BRACKET(ForIn, VariableIntroducer, CARBON_IF_VALID(In))
  353. CARBON_PARSE_NODE_KIND_BRACKET(ForHeader, ForHeaderStart,
  354. CARBON_IF_VALID(CloseParen))
  355. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ForStatement, 2, For)
  356. // `if` statement + `else`:
  357. // IfConditionStart
  358. // _external_: expression
  359. // IfCondition
  360. // _external_: CodeBlock
  361. // IfStatementElse
  362. // _external_: CodeBlock or IfStatement
  363. // IfStatement
  364. //
  365. // IfStatementElse and the following node are optional based on `else` presence.
  366. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfConditionStart, 0,
  367. CARBON_IF_VALID(OpenParen))
  368. CARBON_PARSE_NODE_KIND_BRACKET(IfCondition, IfConditionStart,
  369. CARBON_IF_VALID(CloseParen))
  370. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfStatementElse, 0, Else)
  371. CARBON_PARSE_NODE_KIND_BRACKET(IfStatement, IfCondition, If)
  372. // `while`:
  373. // WhileConditionStart
  374. // _external_: expression
  375. // WhileCondition
  376. // _external_: CodeBlock
  377. // WhileStatement
  378. CARBON_PARSE_NODE_KIND_CHILD_COUNT(WhileConditionStart, 0,
  379. CARBON_IF_VALID(OpenParen))
  380. CARBON_PARSE_NODE_KIND_BRACKET(WhileCondition, WhileConditionStart,
  381. CARBON_IF_VALID(CloseParen))
  382. CARBON_PARSE_NODE_KIND_CHILD_COUNT(WhileStatement, 2, While)
  383. // Index expressions, such as `a[1]`:
  384. // _external_: expression
  385. // IndexExprStart
  386. // _external_: expression
  387. // IndexExpr
  388. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IndexExprStart, 1, OpenSquareBracket)
  389. CARBON_PARSE_NODE_KIND_BRACKET(IndexExpr, IndexExprStart, CloseSquareBracket)
  390. // Parenthesized single expressions, such as `(2)`:
  391. // ExprOpenParen
  392. // _external_: expression
  393. // ParenExpr
  394. //
  395. // Tuples, such as `(1, 2)`:
  396. // ExprOpenParen
  397. // _external_: expression
  398. // TupleLiteralComma
  399. // _repeated_
  400. // TupleLiteral
  401. //
  402. // Expressions and TupleLiteralComma may repeat with TupleLiteralComma as a
  403. // separator.
  404. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ExprOpenParen, 0, OpenParen)
  405. CARBON_PARSE_NODE_KIND_BRACKET(ParenExpr, ExprOpenParen, CloseParen)
  406. CARBON_PARSE_NODE_KIND_CHILD_COUNT(TupleLiteralComma, 0, Comma)
  407. CARBON_PARSE_NODE_KIND_BRACKET(TupleLiteral, ExprOpenParen, CloseParen)
  408. // Call expressions, such as `a()`:
  409. // _external_: expression
  410. // CallExprStart
  411. // _external_: expression
  412. // CallExprComma
  413. // _repeated_
  414. // CallExpr
  415. //
  416. // Exprs and CallExprComma may repeat with CallExprComma as a separator.
  417. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CallExprStart, 1, OpenParen)
  418. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CallExprComma, 0, Comma)
  419. CARBON_PARSE_NODE_KIND_BRACKET(CallExpr, CallExprStart, CloseParen)
  420. // A qualified declaration, such as `a.b`:
  421. // _external_: IdentifierName or QualifiedName
  422. // _external_: IdentifierName
  423. // QualifiedName
  424. //
  425. // TODO: This will eventually more general expressions, for example with
  426. // `GenericType(type_args).ChildType(child_type_args).Name`.
  427. CARBON_PARSE_NODE_KIND_CHILD_COUNT(QualifiedName, 2, Period)
  428. // A member access expression, such as `a.b` or
  429. // `GetObject().(Interface.member)`:
  430. // _external_: lhs expression
  431. // _external_: rhs expression
  432. // MemberAccessExpr
  433. CARBON_PARSE_NODE_KIND_CHILD_COUNT(MemberAccessExpr, 2, Period)
  434. // A pointer member access expression, such as `a->b` or
  435. // `GetObject()->(Interface.member)`:
  436. // _external_: lhs expression
  437. // _external_: rhs expression
  438. // PointerMemberAccessExpr
  439. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PointerMemberAccessExpr, 2, MinusGreater)
  440. // A value literal.
  441. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(BoolLiteralFalse, False)
  442. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(BoolLiteralTrue, True)
  443. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(IntLiteral, IntLiteral)
  444. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(RealLiteral, RealLiteral)
  445. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(StringLiteral, StringLiteral)
  446. // A type literal.
  447. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(BoolTypeLiteral, Bool)
  448. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(IntTypeLiteral, IntTypeLiteral)
  449. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(UnsignedIntTypeLiteral,
  450. UnsignedIntTypeLiteral)
  451. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(FloatTypeLiteral, FloatTypeLiteral)
  452. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(StringTypeLiteral, StringTypeLiteral)
  453. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(TypeTypeLiteral, Type)
  454. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(AutoTypeLiteral, Auto)
  455. // A prefix operator, such as `not`:
  456. // _external_: expression
  457. // PrefixOperator<name>
  458. CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Amp)
  459. CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Caret)
  460. CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Const)
  461. CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Not)
  462. CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Minus)
  463. CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(MinusMinus)
  464. CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(PlusPlus)
  465. CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Star)
  466. // An infix operator, such as `+`:
  467. // _external_: lhs expression
  468. // _external_: rhs expression
  469. // InfixOperator<name>
  470. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Amp)
  471. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(AmpEqual)
  472. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(As)
  473. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Caret)
  474. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(CaretEqual)
  475. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Equal)
  476. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(EqualEqual)
  477. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(ExclaimEqual)
  478. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Greater)
  479. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(GreaterEqual)
  480. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(GreaterGreater)
  481. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(GreaterGreaterEqual)
  482. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Less)
  483. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(LessEqual)
  484. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(LessEqualGreater)
  485. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(LessLess)
  486. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(LessLessEqual)
  487. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Minus)
  488. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(MinusEqual)
  489. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Percent)
  490. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(PercentEqual)
  491. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Pipe)
  492. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(PipeEqual)
  493. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Plus)
  494. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(PlusEqual)
  495. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Slash)
  496. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(SlashEqual)
  497. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Star)
  498. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(StarEqual)
  499. // A postfix operator, currently only `*`:
  500. // _external_: expression
  501. // PostfixOperator<name>
  502. CARBON_PARSE_NODE_KIND_POSTFIX_OPERATOR(Star)
  503. // A short-circuiting infix operator, such as `and`:
  504. // _external_: expression
  505. // ShortCircuitOperand(And|Or)
  506. // _external_: expression
  507. // ShortCircuitOperand(And|Or)
  508. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ShortCircuitOperandAnd, 1, And)
  509. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ShortCircuitOperandOr, 1, Or)
  510. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ShortCircuitOperatorAnd, 2, And)
  511. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ShortCircuitOperatorOr, 2, Or)
  512. // `if` expression + `then` + `else`:
  513. // _external_: expression
  514. // IfExprIf
  515. // _external_: expression
  516. // IfExprThen
  517. // _external_: expression
  518. // IfExprElse
  519. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfExprIf, 1, If)
  520. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfExprThen, 1, Then)
  521. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfExprElse, 3, CARBON_IF_VALID(Else))
  522. // Struct literals, such as `{.a = 0}`:
  523. // StructLiteralOrStructTypeLiteralStart
  524. // _external_: IdentifierName or BaseName
  525. // StructFieldDesignator
  526. // _external_: expression
  527. // StructFieldValue
  528. // StructComma
  529. // _repeated_
  530. // StructLiteral
  531. //
  532. // Struct type literals, such as `{.a: i32}`:
  533. // StructLiteralOrStructTypeLiteralStart
  534. // _external_: IdentifierName or BaseName
  535. // StructFieldDesignator
  536. // _external_: type expression
  537. // StructFieldType
  538. // StructComma
  539. // _repeated_
  540. // StructTypeLiteral
  541. //
  542. // Elements (StructFieldValue and StructFieldType, respectively) and StructComma
  543. // may repeat with StructComma as a separator.
  544. //
  545. // When a valid StructFieldType or StructFieldValue cannot be formed, elements
  546. // may be replaced by InvalidParse, which may have a preceding sibling
  547. // StructFieldDesignator if one was successfully parsed.
  548. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructLiteralOrStructTypeLiteralStart, 0,
  549. OpenCurlyBrace)
  550. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldDesignator, 1, Period)
  551. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldValue, 2, Equal)
  552. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldType, 2, Colon)
  553. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructComma, 0, Comma)
  554. CARBON_PARSE_NODE_KIND_BRACKET(StructLiteral,
  555. StructLiteralOrStructTypeLiteralStart,
  556. CloseCurlyBrace)
  557. CARBON_PARSE_NODE_KIND_BRACKET(StructTypeLiteral,
  558. StructLiteralOrStructTypeLiteralStart,
  559. CloseCurlyBrace)
  560. // Various modifiers. These are all a single token.
  561. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Abstract)
  562. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Base)
  563. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Default)
  564. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Extend)
  565. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Final)
  566. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Impl)
  567. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Private)
  568. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Protected)
  569. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Virtual)
  570. // `class`:
  571. // ClassIntroducer
  572. // _repeated_ _external_: modifier
  573. // _external_: IdentifierName or QualifiedName
  574. // _optional_ _external_: ImplicitParamList
  575. // _optional_ _external_: TuplePattern
  576. // ClassDefinitionStart
  577. // _external_: declarations
  578. // ClassDefinition
  579. //
  580. // The above is the structure for a definition; for a declaration,
  581. // ClassDefinitionStart and later nodes are removed and replaced by
  582. // ClassDecl.
  583. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ClassIntroducer, 0, Class)
  584. CARBON_PARSE_NODE_KIND_BRACKET(ClassDefinitionStart, ClassIntroducer,
  585. OpenCurlyBrace)
  586. CARBON_PARSE_NODE_KIND_BRACKET(ClassDefinition, ClassDefinitionStart,
  587. CloseCurlyBrace)
  588. CARBON_PARSE_NODE_KIND_BRACKET(ClassDecl, ClassIntroducer,
  589. CARBON_IF_VALID(Semi))
  590. // `base`:
  591. // BaseIntroducer
  592. // _repeated_ _external_: modifier
  593. // BaseColon
  594. // _external_: expression
  595. // BaseDecl
  596. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BaseIntroducer, 0, Base)
  597. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BaseColon, 0, Colon)
  598. CARBON_PARSE_NODE_KIND_BRACKET(BaseDecl, BaseIntroducer, CARBON_IF_VALID(Semi))
  599. // `interface`:
  600. // InterfaceIntroducer
  601. // _repeated_ _external_: modifier
  602. // _external_: IdentifierName or QualifiedName
  603. // _optional_ _external_: ImplicitParamList
  604. // _optional_ _external_: TuplePattern
  605. // InterfaceDefinitionStart
  606. // _external_: declarations
  607. // InterfaceDefinition
  608. //
  609. // The above is the structure for a definition; for a declaration,
  610. // InterfaceDefinitionStart and later nodes are removed and replaced by
  611. // InterfaceDecl.
  612. CARBON_PARSE_NODE_KIND_CHILD_COUNT(InterfaceIntroducer, 0, Interface)
  613. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceDefinitionStart, InterfaceIntroducer,
  614. OpenCurlyBrace)
  615. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceDefinition, InterfaceDefinitionStart,
  616. CloseCurlyBrace)
  617. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceDecl, InterfaceIntroducer,
  618. CARBON_IF_VALID(Semi))
  619. // `impl ... as`:
  620. // ImplIntroducer
  621. // _repeated_ _external_: modifier
  622. // _optional_ _external_: ImplForall
  623. // _optional_ _external_: expression
  624. // _external_: DefaultSelfImplAs or TypeImplAs
  625. // _external_: expression
  626. // ImplDefinitionStart
  627. // _external_: declarations
  628. // ImplDefinition
  629. //
  630. // The above is the structure for a definition; for a declaration,
  631. // ImplDefinitionStart and later nodes are removed and replaced by
  632. // ImplDecl.
  633. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ImplIntroducer, 0, Impl)
  634. CARBON_PARSE_NODE_KIND_BRACKET(ImplDefinitionStart, ImplIntroducer,
  635. OpenCurlyBrace)
  636. CARBON_PARSE_NODE_KIND_BRACKET(ImplDefinition, ImplDefinitionStart,
  637. CloseCurlyBrace)
  638. CARBON_PARSE_NODE_KIND_BRACKET(ImplDecl, ImplIntroducer, CARBON_IF_VALID(Semi))
  639. // `forall ...`:
  640. // _external_: ImplicitParamList
  641. // ImplForall
  642. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ImplForall, 1, Forall)
  643. // `... as`:
  644. // _external_: expression
  645. // TypeImplAs
  646. CARBON_PARSE_NODE_KIND_CHILD_COUNT(TypeImplAs, 1, As)
  647. // `as` without a type before it
  648. CARBON_PARSE_NODE_KIND_CHILD_COUNT(DefaultSelfImplAs, 0, As)
  649. // `constraint`:
  650. // NamedConstraintIntroducer
  651. // _repeated_ _external_: modifier
  652. // _external_: IdentifierName or QualifiedName
  653. // _optional_ _external_: ImplicitParamList
  654. // _optional_ _external_: TuplePattern
  655. // NamedConstraintDefinitionStart
  656. // _external_: declarations
  657. // NamedConstraintDefinition
  658. //
  659. // The above is the structure for a definition; for a declaration,
  660. // NamedConstraintDefinitionStart and later nodes are removed and replaced by
  661. // NamedConstraintDecl.
  662. CARBON_PARSE_NODE_KIND_CHILD_COUNT(NamedConstraintIntroducer, 0, Constraint)
  663. CARBON_PARSE_NODE_KIND_BRACKET(NamedConstraintDefinitionStart,
  664. NamedConstraintIntroducer, OpenCurlyBrace)
  665. CARBON_PARSE_NODE_KIND_BRACKET(NamedConstraintDefinition,
  666. NamedConstraintDefinitionStart, CloseCurlyBrace)
  667. CARBON_PARSE_NODE_KIND_BRACKET(NamedConstraintDecl, NamedConstraintIntroducer,
  668. CARBON_IF_VALID(Semi))
  669. // `choice`:
  670. // ChoiceIntroducer
  671. // _external_: IdentifierName or QualifiedDecl
  672. // ChoiceDefinitionStart
  673. // _optional_ _external_: ChoiceAlternativeList
  674. // ChoiceDefinition
  675. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ChoiceIntroducer, 0, Choice)
  676. CARBON_PARSE_NODE_KIND_BRACKET(ChoiceDefinitionStart, ChoiceIntroducer,
  677. CARBON_IF_VALID(OpenCurlyBrace))
  678. CARBON_PARSE_NODE_KIND_BRACKET(ChoiceDefinition, ChoiceDefinitionStart,
  679. CARBON_IF_VALID(CloseCurlyBrace))
  680. // Choice alternative list:
  681. // _external_: IdentifierName
  682. // _optional_ _external_ : TuplePattern
  683. // _optional_: ChoiceAlternativeListComma
  684. // _repeated_
  685. // ChoiceAlternativeList
  686. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ChoiceAlternativeListComma, 0, Comma)
  687. // `match`:
  688. // MatchIntroducer
  689. // MatchConditionStart
  690. // _external_: expression
  691. // MatchCondition
  692. // MatchStatementStart
  693. // _repeated_ _external_: MatchCase
  694. // _optional_ _external_: MatchStatementDefault
  695. // MatchStatement
  696. CARBON_PARSE_NODE_KIND_CHILD_COUNT(MatchIntroducer, 0, Match)
  697. CARBON_PARSE_NODE_KIND_CHILD_COUNT(MatchConditionStart, 0,
  698. CARBON_IF_VALID(OpenParen))
  699. CARBON_PARSE_NODE_KIND_BRACKET(MatchCondition, MatchConditionStart,
  700. CARBON_IF_VALID(CloseParen))
  701. CARBON_PARSE_NODE_KIND_BRACKET(MatchStatementStart, MatchIntroducer,
  702. CARBON_IF_VALID(OpenCurlyBrace))
  703. CARBON_PARSE_NODE_KIND_BRACKET(MatchStatement, MatchStatementStart,
  704. CARBON_IF_VALID(CloseCurlyBrace))
  705. // `case`:
  706. // MatchCaseIntroducer
  707. // _external_: Pattern
  708. // MatchCaseGuardIntroducer
  709. // MatchCaseGuardStart
  710. // _external_: expression
  711. // MatchCaseGuard
  712. // MatchCaseEqualGreater
  713. // MatchCaseStart
  714. // _repeated_ _external_: statement
  715. // MatchCase
  716. CARBON_PARSE_NODE_KIND_CHILD_COUNT(MatchCaseIntroducer, 0, Case)
  717. CARBON_PARSE_NODE_KIND_CHILD_COUNT(MatchCaseGuardIntroducer, 0, If)
  718. CARBON_PARSE_NODE_KIND_CHILD_COUNT(MatchCaseGuardStart, 0,
  719. CARBON_IF_VALID(OpenParen))
  720. CARBON_PARSE_NODE_KIND_BRACKET(MatchCaseGuard,
  721. MatchCaseGuardIntroducer,
  722. CARBON_IF_VALID(CloseParen))
  723. CARBON_PARSE_NODE_KIND_CHILD_COUNT(MatchCaseEqualGreater, 0,
  724. CARBON_IF_VALID(EqualGreater))
  725. CARBON_PARSE_NODE_KIND_BRACKET(MatchCaseStart, MatchCaseIntroducer,
  726. CARBON_IF_VALID(OpenCurlyBrace))
  727. CARBON_PARSE_NODE_KIND_BRACKET(MatchCase, MatchCaseStart,
  728. CARBON_IF_VALID(CloseCurlyBrace))
  729. // `default`:
  730. // MatchDefaultIntroducer
  731. // MatchDefaultEqualGreater
  732. // MatchDefaultStart
  733. // _repeated_ _external_: statement
  734. // MatchDefault
  735. CARBON_PARSE_NODE_KIND_CHILD_COUNT(MatchDefaultIntroducer, 0, Default)
  736. CARBON_PARSE_NODE_KIND_CHILD_COUNT(MatchDefaultEqualGreater, 0,
  737. CARBON_IF_VALID(EqualGreater))
  738. CARBON_PARSE_NODE_KIND_BRACKET(MatchDefaultStart, MatchDefaultIntroducer,
  739. CARBON_IF_VALID(OpenCurlyBrace))
  740. CARBON_PARSE_NODE_KIND_BRACKET(MatchDefault, MatchDefaultStart,
  741. CARBON_IF_VALID(CloseCurlyBrace))
  742. #undef CARBON_PARSE_NODE_KIND
  743. #undef CARBON_PARSE_NODE_KIND_BRACKET
  744. #undef CARBON_PARSE_NODE_KIND_CHILD_COUNT
  745. #undef CARBON_PARSE_NODE_KIND_INFIX_OPERATOR
  746. #undef CARBON_PARSE_NODE_KIND_POSTFIX_OPERATOR
  747. #undef CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR
  748. #undef CARBON_PARSE_NODE_KIND_TOKEN_LITERAL
  749. #undef CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER
  750. #undef CARBON_IF_VALID