node_kind.def 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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_: ParamList
  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 parameter list, possibly implicit:
  214. // [Implicit]ParamListStart
  215. // _external_: [Generic]BindingPattern
  216. // ParamListComma
  217. // _repeated_
  218. // [Implicit]ParamList
  219. //
  220. // [Generic]PatternBinding and ParamListComma may repeat with ParamListComma
  221. // as a separator.
  222. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ParamListStart, 0, CARBON_TOKEN(OpenParen))
  223. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ImplicitParamListStart, 0,
  224. CARBON_TOKEN(OpenSquareBracket))
  225. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ParamListComma, 0, CARBON_TOKEN(Comma))
  226. CARBON_PARSE_NODE_KIND_BRACKET(ParamList, ParamListStart,
  227. CARBON_TOKEN(CloseParen))
  228. CARBON_PARSE_NODE_KIND_BRACKET(ImplicitParamList, ImplicitParamListStart,
  229. CARBON_TOKEN(CloseSquareBracket))
  230. // An array type, such as `[i32; 3]` or `[i32;]`:
  231. // ArrayExprStart
  232. // _external_: type expression
  233. // ArrayExprSemi
  234. // _optional_ _external_: expression
  235. // ArrayExpr
  236. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ArrayExprStart, 0,
  237. CARBON_TOKEN(OpenSquareBracket))
  238. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ArrayExprSemi, 2,
  239. CARBON_TOKEN(Semi)
  240. CARBON_IF_ERROR(CARBON_ANY_TOKEN))
  241. CARBON_PARSE_NODE_KIND_BRACKET(ArrayExpr, ArrayExprSemi,
  242. CARBON_TOKEN(CloseSquareBracket))
  243. // A binding pattern, such as `name: Type`:
  244. // Name or SelfValueName
  245. // _external_: type expression
  246. // [Generic]BindingPattern
  247. // _optional_ Address
  248. // _optional_ Template
  249. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BindingPattern, 2,
  250. CARBON_TOKEN(Colon)
  251. CARBON_IF_ERROR(CARBON_ANY_TOKEN))
  252. CARBON_PARSE_NODE_KIND_CHILD_COUNT(GenericBindingPattern, 2,
  253. CARBON_TOKEN(ColonExclaim))
  254. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Address, 1, CARBON_TOKEN(Addr))
  255. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Template, 1, CARBON_TOKEN(Template))
  256. // `let`:
  257. // LetIntroducer
  258. // _repeated_ _external_: modifier
  259. // _external_: BindingPattern
  260. // LetInitializer
  261. // _external_: expression
  262. // LetDecl
  263. //
  264. // Modifier keywords only appear for `let` declarations, not `let` statements.
  265. CARBON_PARSE_NODE_KIND_CHILD_COUNT(LetIntroducer, 0, CARBON_TOKEN(Let))
  266. CARBON_PARSE_NODE_KIND_CHILD_COUNT(LetInitializer, 0, CARBON_TOKEN(Equal))
  267. CARBON_PARSE_NODE_KIND_BRACKET(LetDecl, LetIntroducer,
  268. CARBON_TOKEN(Semi)
  269. CARBON_IF_ERROR(CARBON_TOKEN(Let)))
  270. // `var` and `returned var`:
  271. // VariableIntroducer
  272. // _repeated_ _external_: modifier
  273. // _optional_ ReturnedModifier
  274. // _external_: BindingPattern
  275. // VariableInitializer
  276. // _external_: expression
  277. // _optional_
  278. // VariableDecl
  279. //
  280. // Access and declaration modifier keywords only appear for `var` declarations,
  281. // whereas the returned modifier only appears on `var` statements.
  282. // The VariableInitializer and following expression are paired: either both will
  283. // be present, or neither will.
  284. CARBON_PARSE_NODE_KIND_CHILD_COUNT(VariableIntroducer, 0,
  285. CARBON_TOKEN(Var)
  286. CARBON_IF_ERROR(CARBON_TOKEN(Returned)))
  287. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnedModifier, 0, CARBON_TOKEN(Returned))
  288. CARBON_PARSE_NODE_KIND_CHILD_COUNT(VariableInitializer, 0, CARBON_TOKEN(Equal))
  289. CARBON_PARSE_NODE_KIND_BRACKET(VariableDecl, VariableIntroducer,
  290. CARBON_TOKEN(Semi)
  291. CARBON_IF_ERROR(CARBON_TOKEN(Var)
  292. CARBON_TOKEN(Returned)))
  293. // An expression statement:
  294. // _external_: expression
  295. // ExprStatement
  296. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ExprStatement, 1, CARBON_TOKEN(Semi))
  297. // `break`:
  298. // BreakStatementStart
  299. // BreakStatement
  300. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BreakStatementStart, 0, CARBON_TOKEN(Break))
  301. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BreakStatement, 1,
  302. CARBON_TOKEN(Semi)
  303. CARBON_IF_ERROR(CARBON_TOKEN(Break)))
  304. // `continue`:
  305. // ContinueStatementStart
  306. // ContinueStatement
  307. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ContinueStatementStart, 0,
  308. CARBON_TOKEN(Continue))
  309. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ContinueStatement, 1,
  310. CARBON_TOKEN(Semi)
  311. CARBON_IF_ERROR(CARBON_TOKEN(Continue)))
  312. // `return`:
  313. // ReturnStatementStart
  314. // _optional_ ReturnVarModifier or _external_: expression
  315. // ReturnStatement
  316. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnStatementStart, 0,
  317. CARBON_TOKEN(Return))
  318. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnVarModifier, 0, CARBON_TOKEN(Var))
  319. CARBON_PARSE_NODE_KIND_BRACKET(ReturnStatement, ReturnStatementStart,
  320. CARBON_TOKEN(Semi)
  321. CARBON_IF_ERROR(CARBON_TOKEN(Return)))
  322. // `for`:
  323. // ForHeaderStart
  324. // VariableIntroducer
  325. // _external_: BindingPattern
  326. // ForIn
  327. // _external_: expression
  328. // ForHeader
  329. // _external_: CodeBlock
  330. // ForStatement
  331. //
  332. // Versus a normal `var`, ForIn replaces VariableDecl.
  333. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ForHeaderStart, 0,
  334. CARBON_TOKEN(OpenParen)
  335. CARBON_IF_ERROR(CARBON_TOKEN(For)))
  336. CARBON_PARSE_NODE_KIND_BRACKET(ForIn, VariableIntroducer,
  337. CARBON_TOKEN(In)
  338. CARBON_IF_ERROR(CARBON_ANY_TOKEN))
  339. CARBON_PARSE_NODE_KIND_BRACKET(ForHeader, ForHeaderStart,
  340. CARBON_TOKEN(CloseParen)
  341. CARBON_IF_ERROR(CARBON_TOKEN(For)))
  342. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ForStatement, 2, CARBON_TOKEN(For))
  343. // `if` statement + `else`:
  344. // IfConditionStart
  345. // _external_: expression
  346. // IfCondition
  347. // _external_: CodeBlock
  348. // IfStatementElse
  349. // _external_: CodeBlock or IfStatement
  350. // IfStatement
  351. //
  352. // IfStatementElse and the following node are optional based on `else` presence.
  353. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfConditionStart, 0,
  354. CARBON_TOKEN(OpenParen)
  355. CARBON_IF_ERROR(CARBON_TOKEN(If)))
  356. CARBON_PARSE_NODE_KIND_BRACKET(IfCondition, IfConditionStart,
  357. CARBON_TOKEN(CloseParen)
  358. CARBON_IF_ERROR(CARBON_TOKEN(If)))
  359. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfStatementElse, 0, CARBON_TOKEN(Else))
  360. CARBON_PARSE_NODE_KIND_BRACKET(IfStatement, IfCondition, CARBON_TOKEN(If))
  361. // `while`:
  362. // WhileConditionStart
  363. // _external_: expression
  364. // WhileCondition
  365. // _external_: CodeBlock
  366. // WhileStatement
  367. CARBON_PARSE_NODE_KIND_CHILD_COUNT(WhileConditionStart, 0,
  368. CARBON_TOKEN(OpenParen))
  369. CARBON_PARSE_NODE_KIND_BRACKET(WhileCondition, WhileConditionStart,
  370. CARBON_TOKEN(CloseParen))
  371. CARBON_PARSE_NODE_KIND_CHILD_COUNT(WhileStatement, 2, CARBON_TOKEN(While))
  372. // Index expressions, such as `a[1]`:
  373. // _external_: expression
  374. // IndexExprStart
  375. // _external_: expression
  376. // IndexExpr
  377. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IndexExprStart, 1,
  378. CARBON_TOKEN(OpenSquareBracket))
  379. CARBON_PARSE_NODE_KIND_BRACKET(IndexExpr, IndexExprStart,
  380. CARBON_TOKEN(CloseSquareBracket))
  381. // Parenthesized expressions, such as `(2)`:
  382. // ParenExprOrTupleLiteralStart
  383. // _external_: expression
  384. // ParenExpr
  385. //
  386. // Tuples, such as `(1, 2)`:
  387. // ParenExprOrTupleLiteralStart
  388. // _external_: expression
  389. // TupleLiteralComma
  390. // _repeated_
  391. // TupleLiteral
  392. //
  393. // Expressions and TupleLiteralComma may repeat with TupleLiteralComma as a
  394. // separator.
  395. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ParenExprOrTupleLiteralStart, 0,
  396. CARBON_TOKEN(OpenParen))
  397. CARBON_PARSE_NODE_KIND_BRACKET(ParenExpr, ParenExprOrTupleLiteralStart,
  398. CARBON_TOKEN(CloseParen))
  399. CARBON_PARSE_NODE_KIND_CHILD_COUNT(TupleLiteralComma, 0, CARBON_TOKEN(Comma))
  400. CARBON_PARSE_NODE_KIND_BRACKET(TupleLiteral, ParenExprOrTupleLiteralStart,
  401. CARBON_TOKEN(CloseParen))
  402. // Call expressions, such as `a()`:
  403. // _external_: expression
  404. // CallExprStart
  405. // _external_: expression
  406. // CallExprComma
  407. // _repeated_
  408. // CallExpr
  409. //
  410. // Exprs and CallExprComma may repeat with CallExprComma as a separator.
  411. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CallExprStart, 1, CARBON_TOKEN(OpenParen))
  412. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CallExprComma, 0, CARBON_TOKEN(Comma))
  413. CARBON_PARSE_NODE_KIND_BRACKET(CallExpr, CallExprStart,
  414. CARBON_TOKEN(CloseParen))
  415. // A qualified declaration, such as `a.b`:
  416. // _external_: Name or QualifiedDecl
  417. // _external_: Name
  418. // QualifiedDecl
  419. //
  420. // TODO: This will eventually more general expressions, for example with
  421. // `GenericType(type_args).ChildType(child_type_args).Name`.
  422. CARBON_PARSE_NODE_KIND_CHILD_COUNT(QualifiedDecl, 2, CARBON_TOKEN(Period))
  423. // A member access expression, such as `a.b` or
  424. // `GetObject().(Interface.member)`:
  425. // _external_: lhs expression
  426. // _external_: rhs expression
  427. // QualifiedExpr
  428. CARBON_PARSE_NODE_KIND_CHILD_COUNT(MemberAccessExpr, 2, CARBON_TOKEN(Period))
  429. // A pointer member access expression, such as `a->b` or
  430. // `GetObject()->(Interface.member)`:
  431. // _external_: lhs expression
  432. // _external_: rhs expression
  433. // QualifiedExpr
  434. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PointerMemberAccessExpr, 2,
  435. CARBON_TOKEN(MinusGreater))
  436. // A value literal.
  437. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(BoolLiteralFalse, CARBON_TOKEN(False))
  438. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(BoolLiteralTrue, CARBON_TOKEN(True))
  439. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(IntLiteral, CARBON_TOKEN(IntLiteral))
  440. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(RealLiteral, CARBON_TOKEN(RealLiteral))
  441. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(StringLiteral, CARBON_TOKEN(StringLiteral))
  442. // A type literal.
  443. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(BoolTypeLiteral, CARBON_TOKEN(Bool))
  444. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(IntTypeLiteral,
  445. CARBON_TOKEN(IntTypeLiteral))
  446. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(UnsignedIntTypeLiteral,
  447. CARBON_TOKEN(UnsignedIntTypeLiteral))
  448. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(FloatTypeLiteral,
  449. CARBON_TOKEN(FloatTypeLiteral))
  450. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(StringTypeLiteral,
  451. CARBON_TOKEN(StringTypeLiteral))
  452. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(TypeTypeLiteral, CARBON_TOKEN(Type))
  453. // clang-format off
  454. // A prefix operator:
  455. // _external_: expression
  456. // PrefixOperator
  457. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PrefixOperator, 1,
  458. CARBON_TOKEN(Star)
  459. CARBON_TOKEN(Amp)
  460. CARBON_TOKEN(Not)
  461. CARBON_TOKEN(Minus)
  462. CARBON_TOKEN(MinusMinus)
  463. CARBON_TOKEN(PlusPlus)
  464. CARBON_TOKEN(Caret)
  465. CARBON_TOKEN(Const))
  466. // An infix operator:
  467. // _external_: lhs expression
  468. // _external_: rhs expression
  469. // InfixOperator
  470. CARBON_PARSE_NODE_KIND_CHILD_COUNT(InfixOperator, 2,
  471. CARBON_TOKEN(Amp)
  472. CARBON_TOKEN(AmpEqual)
  473. CARBON_TOKEN(And)
  474. CARBON_TOKEN(As)
  475. CARBON_TOKEN(Caret)
  476. CARBON_TOKEN(CaretEqual)
  477. CARBON_TOKEN(Equal)
  478. CARBON_TOKEN(EqualEqual)
  479. CARBON_TOKEN(ExclaimEqual)
  480. CARBON_TOKEN(Greater)
  481. CARBON_TOKEN(GreaterEqual)
  482. CARBON_TOKEN(GreaterGreater)
  483. CARBON_TOKEN(GreaterGreaterEqual)
  484. CARBON_TOKEN(Less)
  485. CARBON_TOKEN(LessEqual)
  486. CARBON_TOKEN(LessLess)
  487. CARBON_TOKEN(LessLessEqual)
  488. CARBON_TOKEN(Minus)
  489. CARBON_TOKEN(MinusEqual)
  490. CARBON_TOKEN(Or)
  491. CARBON_TOKEN(Percent)
  492. CARBON_TOKEN(PercentEqual)
  493. CARBON_TOKEN(Pipe)
  494. CARBON_TOKEN(PipeEqual)
  495. CARBON_TOKEN(Plus)
  496. CARBON_TOKEN(PlusEqual)
  497. CARBON_TOKEN(Slash)
  498. CARBON_TOKEN(SlashEqual)
  499. CARBON_TOKEN(Star)
  500. CARBON_TOKEN(StarEqual))
  501. // clang-format on
  502. // The first operand of a short-circuiting infix operator:
  503. // _external_: expression
  504. // ShortCircuitOperand
  505. // _external_: expression
  506. // _external_: InfixOperator
  507. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ShortCircuitOperand, 1,
  508. CARBON_TOKEN(And) CARBON_TOKEN(Or))
  509. // A postfix operator:
  510. // _external_: expression
  511. // PostfixOperator
  512. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PostfixOperator, 1, CARBON_TOKEN(Star))
  513. // `if` expression + `then` + `else`:
  514. // _external_: expression
  515. // IfExprIf
  516. // _external_: expression
  517. // IfExprThen
  518. // _external_: expression
  519. // IfExprElse
  520. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfExprIf, 1, CARBON_TOKEN(If))
  521. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfExprThen, 1, CARBON_TOKEN(Then))
  522. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfExprElse, 3,
  523. CARBON_TOKEN(Else)
  524. CARBON_IF_ERROR(CARBON_TOKEN(If)))
  525. // Struct literals, such as `{.a = 0}`:
  526. // StructLiteralOrStructTypeLiteralStart
  527. // _external_: Name
  528. // StructFieldDesignator
  529. // _external_: expression
  530. // StructFieldValue
  531. // StructComma
  532. // _repeated_
  533. // StructLiteral
  534. //
  535. // Struct type literals, such as `{.a: i32}`:
  536. // StructLiteralOrStructTypeLiteralStart
  537. // _external_: Name
  538. // StructFieldDesignator
  539. // _external_: type expression
  540. // StructFieldType
  541. // StructComma
  542. // _repeated_
  543. // StructTypeLiteral
  544. //
  545. // Elements (StructFieldValue and StructFieldType, respectively) and StructComma
  546. // may repeat with StructComma as a separator.
  547. //
  548. // When a valid StructFieldType or StructFieldValue cannot be formed, elements
  549. // may be replaced by StructFieldUnknown, which may have a preceding sibling
  550. // StructFieldDesignator if one was successfully parsed.
  551. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructLiteralOrStructTypeLiteralStart, 0,
  552. CARBON_TOKEN(OpenCurlyBrace))
  553. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldDesignator, 1,
  554. CARBON_TOKEN(Period))
  555. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldValue, 2, CARBON_TOKEN(Equal))
  556. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldType, 2, CARBON_TOKEN(Colon))
  557. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldUnknown, 0,
  558. CARBON_IF_ERROR(CARBON_ANY_TOKEN))
  559. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructComma, 0, CARBON_TOKEN(Comma))
  560. CARBON_PARSE_NODE_KIND_BRACKET(StructLiteral,
  561. StructLiteralOrStructTypeLiteralStart,
  562. CARBON_TOKEN(CloseCurlyBrace))
  563. CARBON_PARSE_NODE_KIND_BRACKET(StructTypeLiteral,
  564. StructLiteralOrStructTypeLiteralStart,
  565. CARBON_TOKEN(CloseCurlyBrace))
  566. // Various modifiers. These are all a single token.
  567. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Abstract)
  568. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Base)
  569. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Default)
  570. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Extend)
  571. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Final)
  572. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Impl)
  573. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Private)
  574. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Protected)
  575. CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Virtual)
  576. // `class`:
  577. // ClassIntroducer
  578. // _repeated_ _external_: modifier
  579. // _external_: Name or QualifiedDecl
  580. // ClassDefinitionStart
  581. // _external_: declarations
  582. // ClassDefinition
  583. //
  584. // The above is the structure for a definition; for a declaration,
  585. // ClassDefinitionStart and later nodes are removed and replaced by
  586. // ClassDecl.
  587. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ClassIntroducer, 0, CARBON_TOKEN(Class))
  588. CARBON_PARSE_NODE_KIND_BRACKET(ClassDefinitionStart, ClassIntroducer,
  589. CARBON_TOKEN(OpenCurlyBrace))
  590. CARBON_PARSE_NODE_KIND_BRACKET(ClassDefinition, ClassDefinitionStart,
  591. CARBON_TOKEN(CloseCurlyBrace))
  592. CARBON_PARSE_NODE_KIND_BRACKET(ClassDecl, ClassIntroducer,
  593. CARBON_TOKEN(Semi)
  594. CARBON_IF_ERROR(CARBON_TOKEN(Class)))
  595. // `base`:
  596. // BaseIntroducer
  597. // _repeated_ _external_: modifier
  598. // BaseColon
  599. // _external_: expression
  600. // BaseDecl
  601. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BaseIntroducer, 0, CARBON_TOKEN(Base))
  602. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BaseColon, 0, CARBON_TOKEN(Colon))
  603. CARBON_PARSE_NODE_KIND_BRACKET(BaseDecl, BaseIntroducer,
  604. CARBON_TOKEN(Semi)
  605. CARBON_IF_ERROR(CARBON_TOKEN(Base)))
  606. // `interface`:
  607. // InterfaceIntroducer
  608. // _repeated_ _external_: modifier
  609. // _external_: Name or QualifiedDecl
  610. // InterfaceDefinitionStart
  611. // _external_: declarations
  612. // InterfaceDefinition
  613. //
  614. // The above is the structure for a definition; for a declaration,
  615. // InterfaceDefinitionStart and later nodes are removed and replaced by
  616. // InterfaceDecl.
  617. CARBON_PARSE_NODE_KIND_CHILD_COUNT(InterfaceIntroducer, 0,
  618. CARBON_TOKEN(Interface))
  619. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceDefinitionStart, InterfaceIntroducer,
  620. CARBON_TOKEN(OpenCurlyBrace))
  621. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceDefinition, InterfaceDefinitionStart,
  622. CARBON_TOKEN(CloseCurlyBrace))
  623. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceDecl, InterfaceIntroducer,
  624. CARBON_TOKEN(Semi)
  625. CARBON_IF_ERROR(CARBON_TOKEN(Interface)))
  626. // `constraint`:
  627. // NamedConstraintIntroducer
  628. // _repeated_ _external_: modifier
  629. // _external_: Name or QualifiedDecl
  630. // NamedConstraintDefinitionStart
  631. // _external_: declarations
  632. // NamedConstraintDefinition
  633. //
  634. // The above is the structure for a definition; for a declaration,
  635. // NamedConstraintDefinitionStart and later nodes are removed and replaced by
  636. // NamedConstraintDecl.
  637. CARBON_PARSE_NODE_KIND_CHILD_COUNT(NamedConstraintIntroducer, 0,
  638. CARBON_TOKEN(Constraint))
  639. CARBON_PARSE_NODE_KIND_BRACKET(NamedConstraintDefinitionStart,
  640. NamedConstraintIntroducer,
  641. CARBON_TOKEN(OpenCurlyBrace))
  642. CARBON_PARSE_NODE_KIND_BRACKET(NamedConstraintDefinition,
  643. NamedConstraintDefinitionStart,
  644. CARBON_TOKEN(CloseCurlyBrace))
  645. CARBON_PARSE_NODE_KIND_BRACKET(NamedConstraintDecl, NamedConstraintIntroducer,
  646. CARBON_TOKEN(Semi))
  647. #undef CARBON_PARSE_NODE_KIND
  648. #undef CARBON_PARSE_NODE_KIND_BRACKET
  649. #undef CARBON_PARSE_NODE_KIND_CHILD_COUNT
  650. #undef CARBON_PARSE_NODE_KIND_TOKEN_LITERAL
  651. #undef CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER
  652. #undef CARBON_TOKEN
  653. #undef CARBON_ANY_TOKEN
  654. #undef CARBON_IF_ERROR