node_kind.def 28 KB

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