node_kind.def 31 KB

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