node_kind.def 29 KB

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