node_kind.def 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. //
  21. // In both cases, LexTokenKinds says which Lex::TokenKind values that this
  22. // parse node can correspond to, and is a sequence of:
  23. // - CARBON_TOKEN(kind): This node can correspond to this kind of token.
  24. // - CARBON_ANY_TOKEN: This node can correspond to any token.
  25. // - CARBON_IF_ERROR(LexTokenKinds): This node can additionally correspond
  26. // to the given kinds of tokens if its `has_error` flag is set.
  27. //
  28. // This tree represents the subset relationship between these macros, where if a
  29. // specific x-macro isn't defined, it'll fall back to the parent macro.
  30. //
  31. // Parse nodes are clustered based on language feature. Comments will show their
  32. // relationship in postorder, using indentation for child node relationships.
  33. #if !(defined(CARBON_PARSE_NODE_KIND) || \
  34. (defined(CARBON_PARSE_NODE_KIND_BRACKET) && \
  35. defined(CARBON_PARSE_NODE_KIND_CHILD_COUNT)))
  36. #error "Must define CARBON_PARSE_NODE_KIND family x-macros to use this file."
  37. #endif
  38. // The BRACKET and CHILD_COUNT macros will use CARBON_PARSE_NODE_KIND by default
  39. // when undefined.
  40. #ifndef CARBON_PARSE_NODE_KIND_BRACKET
  41. #define CARBON_PARSE_NODE_KIND_BRACKET(Name, ...) CARBON_PARSE_NODE_KIND(Name)
  42. #endif
  43. #ifndef CARBON_PARSE_NODE_KIND_CHILD_COUNT
  44. #define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, ...) \
  45. CARBON_PARSE_NODE_KIND(Name)
  46. #endif
  47. // The start of the file.
  48. CARBON_PARSE_NODE_KIND_CHILD_COUNT(FileStart, 0, CARBON_TOKEN(StartOfFile))
  49. // The end of the file.
  50. CARBON_PARSE_NODE_KIND_CHILD_COUNT(FileEnd, 0, CARBON_TOKEN(EndOfFile))
  51. // An invalid parse. Used to balance the parse tree. Always has an error.
  52. CARBON_PARSE_NODE_KIND_CHILD_COUNT(InvalidParse, 0,
  53. CARBON_IF_ERROR(CARBON_ANY_TOKEN))
  54. // An empty declaration, such as `;`.
  55. CARBON_PARSE_NODE_KIND_CHILD_COUNT(EmptyDeclaration, 0,
  56. CARBON_TOKEN(Semi)
  57. CARBON_IF_ERROR(CARBON_ANY_TOKEN))
  58. // A name in a non-expression context, such as a declaration.
  59. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, 0,
  60. CARBON_TOKEN(Identifier)
  61. CARBON_IF_ERROR(CARBON_ANY_TOKEN))
  62. // A name in an expression context.
  63. CARBON_PARSE_NODE_KIND_CHILD_COUNT(NameExpression, 0, CARBON_TOKEN(Identifier))
  64. // ----------------------------------------------------------------------------
  65. // The comments below follow this pattern:
  66. //
  67. // // Descriptive heading:
  68. // // Child1
  69. // // Child2
  70. // // Parent
  71. //
  72. // In this, `Child1`, `Child2`, and `Parent` are all kinds of parse nodes, which
  73. // are then defined using the `CARBON_PARSE_NODE_KIND_*` macros. They are
  74. // written in postorder, with the indentation showing the tree structure. See
  75. // tree.h for more information.
  76. //
  77. // A parse node kind may be preceded by:
  78. // - `_external_:` if this node is the child of multiple kinds of nodes and
  79. // is documented separately.
  80. // - `_optional_` if this node may be present or omitted in valid parses,
  81. // depending on which tokens are in the source code.
  82. // ----------------------------------------------------------------------------
  83. // `package`:
  84. // PackageIntroducer
  85. // _external_: Name
  86. // _external_: Literal
  87. // PackageLibrary
  88. // PackageApi or PackageImpl
  89. // PackageDirective
  90. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageIntroducer, 0, CARBON_TOKEN(Package))
  91. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageApi, 0, CARBON_TOKEN(Api))
  92. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageImpl, 0, CARBON_TOKEN(Impl))
  93. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageLibrary, 1, CARBON_TOKEN(Library))
  94. CARBON_PARSE_NODE_KIND_BRACKET(PackageDirective, PackageIntroducer,
  95. CARBON_TOKEN(Semi)
  96. CARBON_IF_ERROR(CARBON_TOKEN(Package)))
  97. // `namespace`:
  98. // NamespaceStart
  99. // _external_: Name or QualifiedDeclaration
  100. // Namespace
  101. CARBON_PARSE_NODE_KIND_CHILD_COUNT(NamespaceStart, 0, CARBON_TOKEN(Namespace))
  102. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Namespace, 2, CARBON_TOKEN(Semi))
  103. // A code block:
  104. // CodeBlockStart
  105. // _external_: statements
  106. // CodeBlock
  107. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CodeBlockStart, 0,
  108. CARBON_TOKEN(OpenCurlyBrace)
  109. CARBON_IF_ERROR(CARBON_ANY_TOKEN))
  110. CARBON_PARSE_NODE_KIND_BRACKET(CodeBlock, CodeBlockStart,
  111. CARBON_TOKEN(CloseCurlyBrace)
  112. CARBON_IF_ERROR(CARBON_ANY_TOKEN))
  113. // `fn`:
  114. // FunctionIntroducer
  115. // _external_: Name or QualifiedDeclaration
  116. // _external_: ParameterList
  117. // _external_: type expression
  118. // ReturnType
  119. // FunctionDefinitionStart
  120. // _external_: statements
  121. // FunctionDefinition
  122. //
  123. // The above is the structure for a definition; for a declaration,
  124. // FunctionDefinitionStart and later nodes are removed and replaced by
  125. // FunctionDeclaration.
  126. CARBON_PARSE_NODE_KIND_CHILD_COUNT(FunctionIntroducer, 0, CARBON_TOKEN(Fn))
  127. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnType, 1, CARBON_TOKEN(MinusGreater))
  128. CARBON_PARSE_NODE_KIND_BRACKET(FunctionDefinitionStart, FunctionIntroducer,
  129. CARBON_TOKEN(OpenCurlyBrace))
  130. CARBON_PARSE_NODE_KIND_BRACKET(FunctionDefinition, FunctionDefinitionStart,
  131. CARBON_TOKEN(CloseCurlyBrace))
  132. CARBON_PARSE_NODE_KIND_BRACKET(FunctionDeclaration, FunctionIntroducer,
  133. CARBON_TOKEN(Semi)
  134. CARBON_IF_ERROR(CARBON_TOKEN(Fn)))
  135. // A parameter list, possibly deduced:
  136. // [Deduced]ParamertListStart
  137. // _external_: [Generic]PatternBinding
  138. // ParameterListComma
  139. // [Deduced]ParameterList
  140. //
  141. // Expressions and ParameterListComma may repeat with ParameterListComma as a
  142. // separator.
  143. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ParameterListStart, 0,
  144. CARBON_TOKEN(OpenParen))
  145. CARBON_PARSE_NODE_KIND_CHILD_COUNT(DeducedParameterListStart, 0,
  146. CARBON_TOKEN(OpenSquareBracket))
  147. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ParameterListComma, 0, CARBON_TOKEN(Comma))
  148. CARBON_PARSE_NODE_KIND_BRACKET(ParameterList, ParameterListStart,
  149. CARBON_TOKEN(CloseParen))
  150. CARBON_PARSE_NODE_KIND_BRACKET(DeducedParameterList, DeducedParameterListStart,
  151. CARBON_TOKEN(CloseSquareBracket))
  152. // An array type, such as `[i32; 3]` or `[i32;]`:
  153. // ArrayExpressionStart
  154. // _external_: type expression
  155. // ArrayExpressionSemi
  156. // _optional_ _external_: expression
  157. // ArrayExpression
  158. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ArrayExpressionStart, 0,
  159. CARBON_TOKEN(OpenSquareBracket))
  160. CARBON_PARSE_NODE_KIND_CHILD_COUNT(
  161. ArrayExpressionSemi, 2,
  162. CARBON_TOKEN(Semi) CARBON_IF_ERROR(CARBON_TOKEN(CloseSquareBracket)))
  163. CARBON_PARSE_NODE_KIND_BRACKET(ArrayExpression, ArrayExpressionSemi,
  164. CARBON_TOKEN(CloseSquareBracket))
  165. // A pattern binding, such as `name: Type`:
  166. // Name
  167. // _external_: type expression
  168. // [Generic]PatternBinding
  169. // _optional_ Address
  170. // _optional_ Template
  171. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PatternBinding, 2,
  172. CARBON_TOKEN(Colon)
  173. CARBON_IF_ERROR(CARBON_ANY_TOKEN))
  174. CARBON_PARSE_NODE_KIND_CHILD_COUNT(GenericPatternBinding, 2,
  175. CARBON_TOKEN(ColonExclaim))
  176. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Address, 1, CARBON_TOKEN(Addr))
  177. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Template, 1, CARBON_TOKEN(Template))
  178. // `let`:
  179. // LetIntroducer
  180. // _external_: PatternBinding
  181. // LetInitializer
  182. // _external_: expression
  183. // LetDeclaration
  184. CARBON_PARSE_NODE_KIND_CHILD_COUNT(LetIntroducer, 0, CARBON_TOKEN(Let))
  185. CARBON_PARSE_NODE_KIND_CHILD_COUNT(LetInitializer, 0, CARBON_TOKEN(Equal))
  186. CARBON_PARSE_NODE_KIND_BRACKET(LetDeclaration, LetIntroducer,
  187. CARBON_TOKEN(Semi)
  188. CARBON_IF_ERROR(CARBON_TOKEN(Let)))
  189. // `var`:
  190. // VariableIntroducer
  191. // _external_: PatternBinding
  192. // _optional_ VariableInitializer
  193. // _optional_ _external_: expression
  194. // VariableDeclaration
  195. //
  196. // The VariableInitializer and following expression are paired: either both will
  197. // be present, or neither will.
  198. CARBON_PARSE_NODE_KIND_CHILD_COUNT(VariableIntroducer, 0, CARBON_TOKEN(Var))
  199. CARBON_PARSE_NODE_KIND_CHILD_COUNT(VariableInitializer, 0, CARBON_TOKEN(Equal))
  200. CARBON_PARSE_NODE_KIND_BRACKET(VariableDeclaration, VariableIntroducer,
  201. CARBON_TOKEN(Semi)
  202. CARBON_IF_ERROR(CARBON_TOKEN(Var)))
  203. // An expression statement:
  204. // _external_: expression
  205. // ExpressionStatement
  206. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ExpressionStatement, 1, CARBON_TOKEN(Semi))
  207. // `break`:
  208. // BreakStatementStart
  209. // BreakStatement
  210. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BreakStatementStart, 0, CARBON_TOKEN(Break))
  211. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BreakStatement, 1,
  212. CARBON_TOKEN(Semi)
  213. CARBON_IF_ERROR(CARBON_TOKEN(Break)))
  214. // `continue`:
  215. // ContinueStatementStart
  216. // ContinueStatement
  217. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ContinueStatementStart, 0,
  218. CARBON_TOKEN(Continue))
  219. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ContinueStatement, 1,
  220. CARBON_TOKEN(Semi)
  221. CARBON_IF_ERROR(CARBON_TOKEN(Continue)))
  222. // `return`:
  223. // ReturnStatementStart
  224. // _optional_ _external_: expression
  225. // ReturnStatement
  226. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnStatementStart, 0,
  227. CARBON_TOKEN(Return))
  228. CARBON_PARSE_NODE_KIND_BRACKET(ReturnStatement, ReturnStatementStart,
  229. CARBON_TOKEN(Semi)
  230. CARBON_IF_ERROR(CARBON_TOKEN(Return)))
  231. // `for`:
  232. // ForHeaderStart
  233. // VariableIntroducer
  234. // _external_: PatternBinding
  235. // ForIn
  236. // _external_: expression
  237. // ForHeader
  238. // _external_: CodeBlock
  239. // ForStatement
  240. //
  241. // Versus a normal `var`, ForIn replaces VariableDeclaration.
  242. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ForHeaderStart, 0,
  243. CARBON_TOKEN(OpenParen)
  244. CARBON_IF_ERROR(CARBON_TOKEN(For)))
  245. CARBON_PARSE_NODE_KIND_BRACKET(ForIn, VariableIntroducer,
  246. CARBON_TOKEN(In)
  247. CARBON_IF_ERROR(CARBON_ANY_TOKEN))
  248. CARBON_PARSE_NODE_KIND_BRACKET(ForHeader, ForHeaderStart,
  249. CARBON_TOKEN(CloseParen)
  250. CARBON_IF_ERROR(CARBON_TOKEN(For)))
  251. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ForStatement, 2, CARBON_TOKEN(For))
  252. // `if` statement + `else`:
  253. // IfConditionStart
  254. // _external_: expression
  255. // IfCondition
  256. // _external_: CodeBlock
  257. // IfStatementElse
  258. // _external_: CodeBlock or IfStatement
  259. // IfStatement
  260. //
  261. // IfStatementElse and the following node are optional based on `else` presence.
  262. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfConditionStart, 0,
  263. CARBON_TOKEN(OpenParen)
  264. CARBON_IF_ERROR(CARBON_TOKEN(If)))
  265. CARBON_PARSE_NODE_KIND_BRACKET(IfCondition, IfConditionStart,
  266. CARBON_TOKEN(CloseParen)
  267. CARBON_IF_ERROR(CARBON_TOKEN(If)))
  268. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfStatementElse, 0, CARBON_TOKEN(Else))
  269. CARBON_PARSE_NODE_KIND_BRACKET(IfStatement, IfCondition, CARBON_TOKEN(If))
  270. // `while`:
  271. // WhileConditionStart
  272. // _external_: expression
  273. // WhileCondition
  274. // _external_: CodeBlock
  275. // WhileStatement
  276. CARBON_PARSE_NODE_KIND_CHILD_COUNT(WhileConditionStart, 0,
  277. CARBON_TOKEN(OpenParen))
  278. CARBON_PARSE_NODE_KIND_BRACKET(WhileCondition, WhileConditionStart,
  279. CARBON_TOKEN(CloseParen))
  280. CARBON_PARSE_NODE_KIND_CHILD_COUNT(WhileStatement, 2, CARBON_TOKEN(While))
  281. // Index expressions, such as `a[1]`:
  282. // _external_: expression
  283. // IndexExpressionStart
  284. // _external_: expression
  285. // IndexExpression
  286. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IndexExpressionStart, 1,
  287. CARBON_TOKEN(OpenSquareBracket))
  288. CARBON_PARSE_NODE_KIND_BRACKET(IndexExpression, IndexExpressionStart,
  289. CARBON_TOKEN(CloseSquareBracket))
  290. // Parenthesized expressions, such as `(2)`:
  291. // ParenExpressionOrTupleLiteralStart
  292. // _external_: expression
  293. // ParenExpression
  294. //
  295. // Tuples, such as `(1, 2)`:
  296. // ParenExpressionOrTupleLiteralStart
  297. // _external_: expression
  298. // TupleLiteralComma
  299. // TupleLiteral
  300. //
  301. // Expressions and TupleLiteralComma may repeat with TupleLiteralComma as a
  302. // separator.
  303. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ParenExpressionOrTupleLiteralStart, 0,
  304. CARBON_TOKEN(OpenParen))
  305. CARBON_PARSE_NODE_KIND_BRACKET(ParenExpression,
  306. ParenExpressionOrTupleLiteralStart,
  307. CARBON_TOKEN(CloseParen))
  308. CARBON_PARSE_NODE_KIND_CHILD_COUNT(TupleLiteralComma, 0, CARBON_TOKEN(Comma))
  309. CARBON_PARSE_NODE_KIND_BRACKET(TupleLiteral, ParenExpressionOrTupleLiteralStart,
  310. CARBON_TOKEN(CloseParen))
  311. // Call expressions, such as `a()`:
  312. // _external_: expression
  313. // CallExpressionStart
  314. // _external_: expression
  315. // CallExpressionComma
  316. // CallExpression
  317. //
  318. // Expressions and CallExpressionComma may repeat with CallExpressionComma as a
  319. // separator.
  320. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CallExpressionStart, 1,
  321. CARBON_TOKEN(OpenParen))
  322. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CallExpressionComma, 0, CARBON_TOKEN(Comma))
  323. CARBON_PARSE_NODE_KIND_BRACKET(CallExpression, CallExpressionStart,
  324. CARBON_TOKEN(CloseParen))
  325. // A qualified declaration, such as `a.b`:
  326. // _external_: Name or QualifiedDeclaration
  327. // _external_: Name
  328. // QualifiedDeclaration
  329. //
  330. // TODO: This will eventually more general expressions, for example with
  331. // `GenericType(type_args).ChildType(child_type_args).Name`.
  332. CARBON_PARSE_NODE_KIND_CHILD_COUNT(QualifiedDeclaration, 2,
  333. CARBON_TOKEN(Period))
  334. // A member access expression, such as `a.b` or
  335. // `GetObject().(Interface.member)`:
  336. // _external_: lhs expression
  337. // _external_: rhs expression
  338. // QualifiedExpression
  339. CARBON_PARSE_NODE_KIND_CHILD_COUNT(MemberAccessExpression, 2,
  340. CARBON_TOKEN(Period))
  341. // A pointer member access expression, such as `a->b` or
  342. // `GetObject()->(Interface.member)`:
  343. // _external_: lhs expression
  344. // _external_: rhs expression
  345. // QualifiedExpression
  346. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PointerMemberAccessExpression, 2,
  347. CARBON_TOKEN(MinusGreater))
  348. // clang-format off
  349. // A literal.
  350. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Literal, 0,
  351. CARBON_TOKEN(False)
  352. CARBON_TOKEN(True)
  353. CARBON_TOKEN(IntegerLiteral)
  354. CARBON_TOKEN(RealLiteral)
  355. CARBON_TOKEN(StringLiteral)
  356. CARBON_TOKEN(Bool)
  357. CARBON_TOKEN(IntegerTypeLiteral)
  358. CARBON_TOKEN(UnsignedIntegerTypeLiteral)
  359. CARBON_TOKEN(FloatingPointTypeLiteral)
  360. CARBON_TOKEN(StringTypeLiteral)
  361. CARBON_TOKEN(Type))
  362. // A prefix operator:
  363. // _external_: expression
  364. // PrefixOperator
  365. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PrefixOperator, 1,
  366. CARBON_TOKEN(Star)
  367. CARBON_TOKEN(Amp)
  368. CARBON_TOKEN(Not)
  369. CARBON_TOKEN(Minus)
  370. CARBON_TOKEN(MinusMinus)
  371. CARBON_TOKEN(PlusPlus)
  372. CARBON_TOKEN(Caret)
  373. CARBON_TOKEN(Const))
  374. // An infix operator:
  375. // _external_: lhs expression
  376. // _external_: rhs expression
  377. // InfixOperator
  378. CARBON_PARSE_NODE_KIND_CHILD_COUNT(InfixOperator, 2,
  379. CARBON_TOKEN(Amp)
  380. CARBON_TOKEN(AmpEqual)
  381. CARBON_TOKEN(And)
  382. CARBON_TOKEN(As)
  383. CARBON_TOKEN(Caret)
  384. CARBON_TOKEN(CaretEqual)
  385. CARBON_TOKEN(Equal)
  386. CARBON_TOKEN(EqualEqual)
  387. CARBON_TOKEN(ExclaimEqual)
  388. CARBON_TOKEN(Greater)
  389. CARBON_TOKEN(GreaterEqual)
  390. CARBON_TOKEN(GreaterGreater)
  391. CARBON_TOKEN(GreaterGreaterEqual)
  392. CARBON_TOKEN(Less)
  393. CARBON_TOKEN(LessEqual)
  394. CARBON_TOKEN(LessLess)
  395. CARBON_TOKEN(LessLessEqual)
  396. CARBON_TOKEN(Minus)
  397. CARBON_TOKEN(MinusEqual)
  398. CARBON_TOKEN(Or)
  399. CARBON_TOKEN(Percent)
  400. CARBON_TOKEN(PercentEqual)
  401. CARBON_TOKEN(Pipe)
  402. CARBON_TOKEN(PipeEqual)
  403. CARBON_TOKEN(Plus)
  404. CARBON_TOKEN(PlusEqual)
  405. CARBON_TOKEN(Slash)
  406. CARBON_TOKEN(SlashEqual)
  407. CARBON_TOKEN(Star)
  408. CARBON_TOKEN(StarEqual))
  409. // clang-format on
  410. // The first operand of a short-circuiting infix operator:
  411. // _external_: expression
  412. // ShortCircuitOperand
  413. // _external_: expression
  414. // _external_: InfixOperator
  415. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ShortCircuitOperand, 1,
  416. CARBON_TOKEN(And) CARBON_TOKEN(Or))
  417. // A postfix operator:
  418. // _external_: expression
  419. // PostfixOperator
  420. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PostfixOperator, 1, CARBON_TOKEN(Star))
  421. // `if` expression + `then` + `else`:
  422. // _external_: expression
  423. // IfExpressionIf
  424. // _external_: expression
  425. // IfExpressionThen
  426. // _external_: expression
  427. // IfExpressionElse
  428. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfExpressionIf, 1, CARBON_TOKEN(If))
  429. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfExpressionThen, 1, CARBON_TOKEN(Then))
  430. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfExpressionElse, 3,
  431. CARBON_TOKEN(Else)
  432. CARBON_IF_ERROR(CARBON_TOKEN(If)))
  433. // Struct literals, such as `{.a = 0}`:
  434. // StructLiteralOrStructTypeLiteralStart
  435. // _external_: Name
  436. // StructFieldDesignator
  437. // _external_: expression
  438. // StructFieldValue
  439. // StructComma
  440. // StructLiteral
  441. //
  442. // Struct type literals, such as `{.a: i32}`:
  443. // _external_: Name
  444. // StructFieldDesignator
  445. // _external_: type expression
  446. // StructFieldType
  447. // StructComma
  448. // StructTypeLiteral
  449. //
  450. // Elements (StructFieldValue and StructFieldType, respectively) and StructComma
  451. // may repeat with StructComma as a separator.
  452. //
  453. // When a valid StructFieldType or StructFieldValue cannot be formed, elements
  454. // may be replaced by StructFieldUnknown, which may have a preceding sibling
  455. // StructFieldDesignator if one was successfully parsed.
  456. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructLiteralOrStructTypeLiteralStart, 0,
  457. CARBON_TOKEN(OpenCurlyBrace))
  458. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldDesignator, 1,
  459. CARBON_TOKEN(Period))
  460. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldValue, 2, CARBON_TOKEN(Equal))
  461. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldType, 2, CARBON_TOKEN(Colon))
  462. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldUnknown, 0,
  463. CARBON_IF_ERROR(CARBON_ANY_TOKEN))
  464. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructComma, 0, CARBON_TOKEN(Comma))
  465. CARBON_PARSE_NODE_KIND_BRACKET(StructLiteral,
  466. StructLiteralOrStructTypeLiteralStart,
  467. CARBON_TOKEN(CloseCurlyBrace))
  468. CARBON_PARSE_NODE_KIND_BRACKET(StructTypeLiteral,
  469. StructLiteralOrStructTypeLiteralStart,
  470. CARBON_TOKEN(CloseCurlyBrace))
  471. // `class`:
  472. // ClassIntroducer
  473. // _external_: Name or QualifiedDeclaration
  474. // ClassDefinitionStart
  475. // _external_: declarations
  476. // ClassDefinition
  477. //
  478. // The above is the structure for a definition; for a declaration,
  479. // ClassDefinitionStart and later nodes are removed and replaced by
  480. // ClassDeclaration.
  481. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ClassIntroducer, 0, CARBON_TOKEN(Class))
  482. CARBON_PARSE_NODE_KIND_BRACKET(ClassDefinitionStart, ClassIntroducer,
  483. CARBON_TOKEN(OpenCurlyBrace))
  484. CARBON_PARSE_NODE_KIND_BRACKET(ClassDefinition, ClassDefinitionStart,
  485. CARBON_TOKEN(CloseCurlyBrace))
  486. CARBON_PARSE_NODE_KIND_BRACKET(ClassDeclaration, ClassIntroducer,
  487. CARBON_TOKEN(Semi)
  488. CARBON_IF_ERROR(CARBON_TOKEN(Class)))
  489. // `interface`:
  490. // InterfaceIntroducer
  491. // _external_: Name or QualifiedDeclaration
  492. // InterfaceDefinitionStart
  493. // _external_: declarations
  494. // InterfaceDefinition
  495. //
  496. // The above is the structure for a definition; for a declaration,
  497. // InterfaceDefinitionStart and later nodes are removed and replaced by
  498. // InterfaceDeclaration.
  499. CARBON_PARSE_NODE_KIND_CHILD_COUNT(InterfaceIntroducer, 0,
  500. CARBON_TOKEN(Interface))
  501. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceDefinitionStart, InterfaceIntroducer,
  502. CARBON_TOKEN(OpenCurlyBrace))
  503. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceDefinition, InterfaceDefinitionStart,
  504. CARBON_TOKEN(CloseCurlyBrace))
  505. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceDeclaration, InterfaceIntroducer,
  506. CARBON_TOKEN(Semi)
  507. CARBON_IF_ERROR(CARBON_TOKEN(Interface)))
  508. // `constraint`:
  509. // NamedConstraintIntroducer
  510. // _external_: Name or QualifiedDeclaration
  511. // NamedConstraintDefinitionStart
  512. // _external_: declarations
  513. // NamedConstraintDefinition
  514. //
  515. // The above is the structure for a definition; for a declaration,
  516. // NamedConstraintDefinitionStart and later nodes are removed and replaced by
  517. // NamedConstraintDeclaration.
  518. CARBON_PARSE_NODE_KIND_CHILD_COUNT(NamedConstraintIntroducer, 0,
  519. CARBON_TOKEN(Constraint))
  520. CARBON_PARSE_NODE_KIND_BRACKET(NamedConstraintDefinitionStart,
  521. NamedConstraintIntroducer,
  522. CARBON_TOKEN(OpenCurlyBrace))
  523. CARBON_PARSE_NODE_KIND_BRACKET(NamedConstraintDefinition,
  524. NamedConstraintDefinitionStart,
  525. CARBON_TOKEN(CloseCurlyBrace))
  526. CARBON_PARSE_NODE_KIND_BRACKET(NamedConstraintDeclaration,
  527. NamedConstraintIntroducer, CARBON_TOKEN(Semi))
  528. // The `self` value and `Self` type identifier keywords. Typically of the form
  529. // `self: Self`:
  530. // SelfValueName
  531. // SelfTypeNameExpression
  532. // PatternBinding
  533. CARBON_PARSE_NODE_KIND_CHILD_COUNT(SelfValueName, 0,
  534. CARBON_TOKEN(SelfValueIdentifier))
  535. CARBON_PARSE_NODE_KIND_CHILD_COUNT(SelfTypeNameExpression, 0,
  536. CARBON_TOKEN(SelfTypeIdentifier))
  537. #undef CARBON_PARSE_NODE_KIND
  538. #undef CARBON_PARSE_NODE_KIND_BRACKET
  539. #undef CARBON_PARSE_NODE_KIND_CHILD_COUNT
  540. #undef CARBON_TOKEN
  541. #undef CARBON_ANY_TOKEN
  542. #undef CARBON_IF_ERROR