node_kind.def 30 KB

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