node_kind.def 28 KB

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