node_kind.def 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BindingPattern, 2, CARBON_IF_VALID(Colon))
  280. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CompileTimeBindingPattern, 2, ColonExclaim)
  281. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Addr, 1, Addr)
  282. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Template, 1, Template)
  283. // `let`:
  284. // LetIntroducer
  285. // _repeated_ _external_: modifier
  286. // _external_: BindingPattern or TuplePattern
  287. // LetInitializer
  288. // _external_: expression
  289. // LetDecl
  290. //
  291. // Modifier keywords only appear for `let` declarations, not `let` statements.
  292. CARBON_PARSE_NODE_KIND_CHILD_COUNT(LetIntroducer, 0, Let)
  293. CARBON_PARSE_NODE_KIND_CHILD_COUNT(LetInitializer, 0, Equal)
  294. CARBON_PARSE_NODE_KIND_BRACKET(LetDecl, LetIntroducer, CARBON_IF_VALID(Semi))
  295. // `var` and `returned var`:
  296. // VariableIntroducer
  297. // _repeated_ _external_: modifier
  298. // _optional_ ReturnedModifier
  299. // _external_: BindingPattern or TuplePattern
  300. // VariableInitializer
  301. // _external_: expression
  302. // _optional_
  303. // VariableDecl
  304. //
  305. // Access and declaration modifier keywords only appear for `var` declarations,
  306. // whereas the returned modifier only appears on `var` statements.
  307. //
  308. // The VariableInitializer and following expression are paired: either both will
  309. // be present, or neither will.
  310. CARBON_PARSE_NODE_KIND_CHILD_COUNT(VariableIntroducer, 0, CARBON_IF_VALID(Var))
  311. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnedModifier, 0, Returned)
  312. CARBON_PARSE_NODE_KIND_CHILD_COUNT(VariableInitializer, 0, Equal)
  313. CARBON_PARSE_NODE_KIND_BRACKET(VariableDecl, VariableIntroducer,
  314. CARBON_IF_VALID(Semi))
  315. // An expression statement:
  316. // _external_: expression
  317. // ExprStatement
  318. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ExprStatement, 1, CARBON_IF_VALID(Semi))
  319. // `break`:
  320. // BreakStatementStart
  321. // BreakStatement
  322. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BreakStatementStart, 0, Break)
  323. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BreakStatement, 1, CARBON_IF_VALID(Semi))
  324. // `continue`:
  325. // ContinueStatementStart
  326. // ContinueStatement
  327. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ContinueStatementStart, 0, Continue)
  328. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ContinueStatement, 1, CARBON_IF_VALID(Semi))
  329. // `return`:
  330. // ReturnStatementStart
  331. // _optional_ ReturnVarModifier or _external_: expression
  332. // ReturnStatement
  333. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnStatementStart, 0, Return)
  334. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnVarModifier, 0, Var)
  335. CARBON_PARSE_NODE_KIND_BRACKET(ReturnStatement, ReturnStatementStart,
  336. CARBON_IF_VALID(Semi))
  337. // `for`:
  338. // ForHeaderStart
  339. // VariableIntroducer
  340. // _external_: BindingPattern
  341. // ForIn
  342. // _external_: expression
  343. // ForHeader
  344. // _external_: CodeBlock
  345. // ForStatement
  346. //
  347. // Versus a normal `var`, ForIn replaces VariableDecl.
  348. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ForHeaderStart, 0,
  349. CARBON_IF_VALID(OpenParen))
  350. CARBON_PARSE_NODE_KIND_BRACKET(ForIn, VariableIntroducer, CARBON_IF_VALID(In))
  351. CARBON_PARSE_NODE_KIND_BRACKET(ForHeader, ForHeaderStart,
  352. CARBON_IF_VALID(CloseParen))
  353. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ForStatement, 2, 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_IF_VALID(OpenParen))
  366. CARBON_PARSE_NODE_KIND_BRACKET(IfCondition, IfConditionStart,
  367. CARBON_IF_VALID(CloseParen))
  368. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfStatementElse, 0, Else)
  369. CARBON_PARSE_NODE_KIND_BRACKET(IfStatement, IfCondition, If)
  370. // `while`:
  371. // WhileConditionStart
  372. // _external_: expression
  373. // WhileCondition
  374. // _external_: CodeBlock
  375. // WhileStatement
  376. CARBON_PARSE_NODE_KIND_CHILD_COUNT(WhileConditionStart, 0,
  377. CARBON_IF_VALID(OpenParen))
  378. CARBON_PARSE_NODE_KIND_BRACKET(WhileCondition, WhileConditionStart,
  379. CARBON_IF_VALID(CloseParen))
  380. CARBON_PARSE_NODE_KIND_CHILD_COUNT(WhileStatement, 2, While)
  381. // Index expressions, such as `a[1]`:
  382. // _external_: expression
  383. // IndexExprStart
  384. // _external_: expression
  385. // IndexExpr
  386. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IndexExprStart, 1, OpenSquareBracket)
  387. CARBON_PARSE_NODE_KIND_BRACKET(IndexExpr, IndexExprStart, CloseSquareBracket)
  388. // Parenthesized single expressions, such as `(2)`:
  389. // ExprOpenParen
  390. // _external_: expression
  391. // ParenExpr
  392. //
  393. // Tuples, such as `(1, 2)`:
  394. // ExprOpenParen
  395. // _external_: expression
  396. // TupleLiteralComma
  397. // _repeated_
  398. // TupleLiteral
  399. //
  400. // Expressions and TupleLiteralComma may repeat with TupleLiteralComma as a
  401. // separator.
  402. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ExprOpenParen, 0, OpenParen)
  403. CARBON_PARSE_NODE_KIND_BRACKET(ParenExpr, ExprOpenParen, CloseParen)
  404. CARBON_PARSE_NODE_KIND_CHILD_COUNT(TupleLiteralComma, 0, Comma)
  405. CARBON_PARSE_NODE_KIND_BRACKET(TupleLiteral, ExprOpenParen, CloseParen)
  406. // Call expressions, such as `a()`:
  407. // _external_: expression
  408. // CallExprStart
  409. // _external_: expression
  410. // CallExprComma
  411. // _repeated_
  412. // CallExpr
  413. //
  414. // Exprs and CallExprComma may repeat with CallExprComma as a separator.
  415. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CallExprStart, 1, OpenParen)
  416. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CallExprComma, 0, Comma)
  417. CARBON_PARSE_NODE_KIND_BRACKET(CallExpr, CallExprStart, CloseParen)
  418. // A qualified declaration, such as `a.b`:
  419. // _external_: IdentifierName or QualifiedName
  420. // _external_: IdentifierName
  421. // QualifiedName
  422. //
  423. // TODO: This will eventually more general expressions, for example with
  424. // `GenericType(type_args).ChildType(child_type_args).Name`.
  425. CARBON_PARSE_NODE_KIND_CHILD_COUNT(QualifiedName, 2, Period)
  426. // A member access expression, such as `a.b` or
  427. // `GetObject().(Interface.member)`:
  428. // _external_: lhs expression
  429. // _external_: rhs expression
  430. // MemberAccessExpr
  431. CARBON_PARSE_NODE_KIND_CHILD_COUNT(MemberAccessExpr, 2, Period)
  432. // A pointer member access expression, such as `a->b` or
  433. // `GetObject()->(Interface.member)`:
  434. // _external_: lhs expression
  435. // _external_: rhs expression
  436. // PointerMemberAccessExpr
  437. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PointerMemberAccessExpr, 2, MinusGreater)
  438. // A value literal.
  439. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(BoolLiteralFalse, False)
  440. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(BoolLiteralTrue, True)
  441. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(IntLiteral, IntLiteral)
  442. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(RealLiteral, RealLiteral)
  443. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(StringLiteral, StringLiteral)
  444. // A type literal.
  445. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(BoolTypeLiteral, Bool)
  446. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(IntTypeLiteral, IntTypeLiteral)
  447. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(UnsignedIntTypeLiteral,
  448. UnsignedIntTypeLiteral)
  449. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(FloatTypeLiteral, FloatTypeLiteral)
  450. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(StringTypeLiteral, StringTypeLiteral)
  451. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(TypeTypeLiteral, Type)
  452. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(AutoTypeLiteral, Auto)
  453. // A prefix operator, such as `not`:
  454. // _external_: expression
  455. // PrefixOperator<name>
  456. CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Amp)
  457. CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Caret)
  458. CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Const)
  459. CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Not)
  460. CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Minus)
  461. CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(MinusMinus)
  462. CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(PlusPlus)
  463. CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Star)
  464. // An infix operator, such as `+`:
  465. // _external_: lhs expression
  466. // _external_: rhs expression
  467. // InfixOperator<name>
  468. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Amp)
  469. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(AmpEqual)
  470. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(As)
  471. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Caret)
  472. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(CaretEqual)
  473. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Equal)
  474. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(EqualEqual)
  475. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(ExclaimEqual)
  476. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Greater)
  477. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(GreaterEqual)
  478. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(GreaterGreater)
  479. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(GreaterGreaterEqual)
  480. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Less)
  481. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(LessEqual)
  482. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(LessEqualGreater)
  483. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(LessLess)
  484. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(LessLessEqual)
  485. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Minus)
  486. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(MinusEqual)
  487. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Percent)
  488. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(PercentEqual)
  489. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Pipe)
  490. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(PipeEqual)
  491. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Plus)
  492. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(PlusEqual)
  493. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Slash)
  494. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(SlashEqual)
  495. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Star)
  496. CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(StarEqual)
  497. // A postfix operator, currently only `*`:
  498. // _external_: expression
  499. // PostfixOperator<name>
  500. CARBON_PARSE_NODE_KIND_POSTFIX_OPERATOR(Star)
  501. // A short-circuiting infix operator, such as `and`:
  502. // _external_: expression
  503. // ShortCircuitOperand(And|Or)
  504. // _external_: expression
  505. // ShortCircuitOperand(And|Or)
  506. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ShortCircuitOperandAnd, 1, And)
  507. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ShortCircuitOperandOr, 1, Or)
  508. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ShortCircuitOperatorAnd, 2, And)
  509. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ShortCircuitOperatorOr, 2, Or)
  510. // `if` expression + `then` + `else`:
  511. // _external_: expression
  512. // IfExprIf
  513. // _external_: expression
  514. // IfExprThen
  515. // _external_: expression
  516. // IfExprElse
  517. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfExprIf, 1, If)
  518. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfExprThen, 1, Then)
  519. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfExprElse, 3, CARBON_IF_VALID(Else))
  520. // Struct literals, such as `{.a = 0}`:
  521. // StructLiteralOrStructTypeLiteralStart
  522. // _external_: IdentifierName or BaseName
  523. // StructFieldDesignator
  524. // _external_: expression
  525. // StructFieldValue
  526. // StructComma
  527. // _repeated_
  528. // StructLiteral
  529. //
  530. // Struct type literals, such as `{.a: i32}`:
  531. // StructLiteralOrStructTypeLiteralStart
  532. // _external_: IdentifierName or BaseName
  533. // StructFieldDesignator
  534. // _external_: type expression
  535. // StructFieldType
  536. // StructComma
  537. // _repeated_
  538. // StructTypeLiteral
  539. //
  540. // Elements (StructFieldValue and StructFieldType, respectively) and StructComma
  541. // may repeat with StructComma as a separator.
  542. //
  543. // When a valid StructFieldType or StructFieldValue cannot be formed, elements
  544. // may be replaced by InvalidParse, which may have a preceding sibling
  545. // StructFieldDesignator if one was successfully parsed.
  546. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructLiteralOrStructTypeLiteralStart, 0,
  547. OpenCurlyBrace)
  548. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldDesignator, 1, Period)
  549. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldValue, 2, Equal)
  550. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldType, 2, Colon)
  551. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructComma, 0, Comma)
  552. CARBON_PARSE_NODE_KIND_BRACKET(StructLiteral,
  553. StructLiteralOrStructTypeLiteralStart,
  554. CloseCurlyBrace)
  555. CARBON_PARSE_NODE_KIND_BRACKET(StructTypeLiteral,
  556. StructLiteralOrStructTypeLiteralStart,
  557. CloseCurlyBrace)
  558. // Various modifiers. These are all a single token.
  559. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Abstract)
  560. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Base)
  561. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Default)
  562. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Extend)
  563. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Final)
  564. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Impl)
  565. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Private)
  566. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Protected)
  567. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Virtual)
  568. // `class`:
  569. // ClassIntroducer
  570. // _repeated_ _external_: modifier
  571. // _external_: IdentifierName or QualifiedName
  572. // _optional_ _external_: ImplicitParamList
  573. // _optional_ _external_: TuplePattern
  574. // ClassDefinitionStart
  575. // _external_: declarations
  576. // ClassDefinition
  577. //
  578. // The above is the structure for a definition; for a declaration,
  579. // ClassDefinitionStart and later nodes are removed and replaced by
  580. // ClassDecl.
  581. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ClassIntroducer, 0, Class)
  582. CARBON_PARSE_NODE_KIND_BRACKET(ClassDefinitionStart, ClassIntroducer,
  583. OpenCurlyBrace)
  584. CARBON_PARSE_NODE_KIND_BRACKET(ClassDefinition, ClassDefinitionStart,
  585. CloseCurlyBrace)
  586. CARBON_PARSE_NODE_KIND_BRACKET(ClassDecl, ClassIntroducer,
  587. CARBON_IF_VALID(Semi))
  588. // `base`:
  589. // BaseIntroducer
  590. // _repeated_ _external_: modifier
  591. // BaseColon
  592. // _external_: expression
  593. // BaseDecl
  594. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BaseIntroducer, 0, Base)
  595. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BaseColon, 0, Colon)
  596. CARBON_PARSE_NODE_KIND_BRACKET(BaseDecl, BaseIntroducer, CARBON_IF_VALID(Semi))
  597. // `interface`:
  598. // InterfaceIntroducer
  599. // _repeated_ _external_: modifier
  600. // _external_: IdentifierName or QualifiedName
  601. // _optional_ _external_: ImplicitParamList
  602. // _optional_ _external_: TuplePattern
  603. // InterfaceDefinitionStart
  604. // _external_: declarations
  605. // InterfaceDefinition
  606. //
  607. // The above is the structure for a definition; for a declaration,
  608. // InterfaceDefinitionStart and later nodes are removed and replaced by
  609. // InterfaceDecl.
  610. CARBON_PARSE_NODE_KIND_CHILD_COUNT(InterfaceIntroducer, 0, Interface)
  611. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceDefinitionStart, InterfaceIntroducer,
  612. OpenCurlyBrace)
  613. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceDefinition, InterfaceDefinitionStart,
  614. CloseCurlyBrace)
  615. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceDecl, InterfaceIntroducer,
  616. CARBON_IF_VALID(Semi))
  617. // `impl ... as`:
  618. // ImplIntroducer
  619. // _repeated_ _external_: modifier
  620. // _optional_ _external_: ImplForall
  621. // _optional_ _external_: expression
  622. // _external_: DefaultSelfImplAs or TypeImplAs
  623. // _external_: expression
  624. // ImplDefinitionStart
  625. // _external_: declarations
  626. // ImplDefinition
  627. //
  628. // The above is the structure for a definition; for a declaration,
  629. // ImplDefinitionStart and later nodes are removed and replaced by
  630. // ImplDecl.
  631. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ImplIntroducer, 0, Impl)
  632. CARBON_PARSE_NODE_KIND_BRACKET(ImplDefinitionStart, ImplIntroducer,
  633. OpenCurlyBrace)
  634. CARBON_PARSE_NODE_KIND_BRACKET(ImplDefinition, ImplDefinitionStart,
  635. CloseCurlyBrace)
  636. CARBON_PARSE_NODE_KIND_BRACKET(ImplDecl, ImplIntroducer, CARBON_IF_VALID(Semi))
  637. // `forall ...`:
  638. // _external_: ImplicitParamList
  639. // ImplForall
  640. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ImplForall, 1, Forall)
  641. // `... as`:
  642. // _external_: expression
  643. // TypeImplAs
  644. CARBON_PARSE_NODE_KIND_CHILD_COUNT(TypeImplAs, 1, As)
  645. // `as` without a type before it
  646. CARBON_PARSE_NODE_KIND_CHILD_COUNT(DefaultSelfImplAs, 0, As)
  647. // `constraint`:
  648. // NamedConstraintIntroducer
  649. // _repeated_ _external_: modifier
  650. // _external_: IdentifierName or QualifiedName
  651. // _optional_ _external_: ImplicitParamList
  652. // _optional_ _external_: TuplePattern
  653. // NamedConstraintDefinitionStart
  654. // _external_: declarations
  655. // NamedConstraintDefinition
  656. //
  657. // The above is the structure for a definition; for a declaration,
  658. // NamedConstraintDefinitionStart and later nodes are removed and replaced by
  659. // NamedConstraintDecl.
  660. CARBON_PARSE_NODE_KIND_CHILD_COUNT(NamedConstraintIntroducer, 0, Constraint)
  661. CARBON_PARSE_NODE_KIND_BRACKET(NamedConstraintDefinitionStart,
  662. NamedConstraintIntroducer, OpenCurlyBrace)
  663. CARBON_PARSE_NODE_KIND_BRACKET(NamedConstraintDefinition,
  664. NamedConstraintDefinitionStart, CloseCurlyBrace)
  665. CARBON_PARSE_NODE_KIND_BRACKET(NamedConstraintDecl, NamedConstraintIntroducer,
  666. CARBON_IF_VALID(Semi))
  667. // `choice`:
  668. // ChoiceIntroducer
  669. // _external_: IdentifierName or QualifiedDecl
  670. // ChoiceDefinitionStart
  671. // _optional_ _external_: ChoiceAlternativeList
  672. // ChoiceDefinition
  673. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ChoiceIntroducer, 0, Choice)
  674. CARBON_PARSE_NODE_KIND_BRACKET(ChoiceDefinitionStart, ChoiceIntroducer,
  675. CARBON_IF_VALID(OpenCurlyBrace))
  676. CARBON_PARSE_NODE_KIND_BRACKET(ChoiceDefinition, ChoiceDefinitionStart,
  677. CARBON_IF_VALID(CloseCurlyBrace))
  678. // Choice alternative list:
  679. // _external_: IdentifierName
  680. // _optional_ _external_ : TuplePattern
  681. // _optional_: ChoiceAlternativeListComma
  682. // _repeated_
  683. // ChoiceAlternativeList
  684. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ChoiceAlternativeListComma, 0, Comma)
  685. // `match`:
  686. // MatchIntroducer
  687. // MatchConditionStart
  688. // _external_: expression
  689. // MatchCondition
  690. // MatchStatementStart
  691. // _repeated_ _external_: MatchCase
  692. // _optional_ _external_: MatchStatementDefault
  693. // MatchStatement
  694. CARBON_PARSE_NODE_KIND_CHILD_COUNT(MatchIntroducer, 0, Match)
  695. CARBON_PARSE_NODE_KIND_CHILD_COUNT(MatchConditionStart, 0,
  696. CARBON_IF_VALID(OpenParen))
  697. CARBON_PARSE_NODE_KIND_BRACKET(MatchCondition, MatchConditionStart,
  698. CARBON_IF_VALID(CloseParen))
  699. CARBON_PARSE_NODE_KIND_BRACKET(MatchStatementStart, MatchIntroducer,
  700. CARBON_IF_VALID(OpenCurlyBrace))
  701. CARBON_PARSE_NODE_KIND_BRACKET(MatchStatement, MatchStatementStart,
  702. CARBON_IF_VALID(CloseCurlyBrace))
  703. // `case`:
  704. // MatchCaseIntroducer
  705. // _external_: Pattern
  706. // MatchCaseGuardIntroducer
  707. // MatchCaseGuardStart
  708. // _external_: expression
  709. // MatchCaseGuard
  710. // MatchCaseEqualGreater
  711. // MatchCaseStart
  712. // _repeated_ _external_: statement
  713. // MatchCase
  714. CARBON_PARSE_NODE_KIND_CHILD_COUNT(MatchCaseIntroducer, 0, Case)
  715. CARBON_PARSE_NODE_KIND_CHILD_COUNT(MatchCaseGuardIntroducer, 0, If)
  716. CARBON_PARSE_NODE_KIND_CHILD_COUNT(MatchCaseGuardStart, 0,
  717. CARBON_IF_VALID(OpenParen))
  718. CARBON_PARSE_NODE_KIND_BRACKET(MatchCaseGuard,
  719. MatchCaseGuardIntroducer,
  720. CARBON_IF_VALID(CloseParen))
  721. CARBON_PARSE_NODE_KIND_CHILD_COUNT(MatchCaseEqualGreater, 0,
  722. CARBON_IF_VALID(EqualGreater))
  723. CARBON_PARSE_NODE_KIND_BRACKET(MatchCaseStart, MatchCaseIntroducer,
  724. CARBON_IF_VALID(OpenCurlyBrace))
  725. CARBON_PARSE_NODE_KIND_BRACKET(MatchCase, MatchCaseStart,
  726. CARBON_IF_VALID(CloseCurlyBrace))
  727. // `default`:
  728. // MatchDefaultIntroducer
  729. // MatchDefaultEqualGreater
  730. // MatchDefaultStart
  731. // _repeated_ _external_: statement
  732. // MatchDefault
  733. CARBON_PARSE_NODE_KIND_CHILD_COUNT(MatchDefaultIntroducer, 0, Default)
  734. CARBON_PARSE_NODE_KIND_CHILD_COUNT(MatchDefaultEqualGreater, 0,
  735. CARBON_IF_VALID(EqualGreater))
  736. CARBON_PARSE_NODE_KIND_BRACKET(MatchDefaultStart, MatchDefaultIntroducer,
  737. CARBON_IF_VALID(OpenCurlyBrace))
  738. CARBON_PARSE_NODE_KIND_BRACKET(MatchDefault, MatchDefaultStart,
  739. CARBON_IF_VALID(CloseCurlyBrace))
  740. #undef CARBON_PARSE_NODE_KIND
  741. #undef CARBON_PARSE_NODE_KIND_BRACKET
  742. #undef CARBON_PARSE_NODE_KIND_CHILD_COUNT
  743. #undef CARBON_PARSE_NODE_KIND_INFIX_OPERATOR
  744. #undef CARBON_PARSE_NODE_KIND_POSTFIX_OPERATOR
  745. #undef CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR
  746. #undef CARBON_PARSE_NODE_KIND_TOKEN_LITERAL
  747. #undef CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER
  748. #undef CARBON_IF_VALID