node_kind.def 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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, Token)
  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(StartOfFile))
  61. // The end of the file.
  62. CARBON_PARSE_NODE_KIND_CHILD_COUNT(FileEnd, 0, CARBON_TOKEN(EndOfFile))
  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 empty declaration, such as `;`.
  67. CARBON_PARSE_NODE_KIND_CHILD_COUNT(EmptyDecl, 0,
  68. CARBON_TOKEN(Semi)
  69. CARBON_IF_ERROR(CARBON_ANY_TOKEN))
  70. // A name in a non-expression context, such as a declaration.
  71. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, 0,
  72. CARBON_TOKEN(Identifier)
  73. CARBON_IF_ERROR(CARBON_ANY_TOKEN))
  74. // A name in an expression context.
  75. CARBON_PARSE_NODE_KIND_CHILD_COUNT(NameExpr, 0, CARBON_TOKEN(Identifier))
  76. // ----------------------------------------------------------------------------
  77. // The comments below follow this pattern:
  78. //
  79. // // Descriptive heading:
  80. // // Child1
  81. // // Child2
  82. // // Parent
  83. //
  84. // In this, `Child1`, `Child2`, and `Parent` are all kinds of parse nodes, which
  85. // are then defined using the `CARBON_PARSE_NODE_KIND_*` macros. They are
  86. // written in postorder, with the indentation showing the tree structure. See
  87. // tree.h for more information.
  88. //
  89. // A parse node kind may be preceded by:
  90. // - `_external_:` if this node is the child of multiple kinds of nodes and
  91. // is documented separately.
  92. // - `_optional_` if this node may be present or omitted in valid parses,
  93. // depending on which tokens are in the source code.
  94. //
  95. // There is generally a close correspondence between handling of tokens and the
  96. // creation of non-external nodes in a given block.
  97. // ----------------------------------------------------------------------------
  98. // The name of a package or library for `package`, `import`, and `library`.
  99. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageName, 0, CARBON_TOKEN(Identifier))
  100. CARBON_PARSE_NODE_KIND_CHILD_COUNT(LibraryName, 0, CARBON_TOKEN(StringLiteral))
  101. // `package`:
  102. // PackageIntroducer
  103. // _optional_ _external_: PackageName
  104. // _optional_ _external_: LibrarySpecifier
  105. // PackageApi or PackageImpl
  106. // PackageDirective
  107. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageIntroducer, 0, CARBON_TOKEN(Package))
  108. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageApi, 0, CARBON_TOKEN(Api))
  109. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageImpl, 0, CARBON_TOKEN(Impl))
  110. CARBON_PARSE_NODE_KIND_BRACKET(PackageDirective, PackageIntroducer,
  111. CARBON_TOKEN(Semi)
  112. CARBON_IF_ERROR(CARBON_TOKEN(Package)))
  113. // `import`:
  114. // ImportIntroducer
  115. // _optional_ _external_: PackageName
  116. // _optional_ _external_: LibrarySpecifier
  117. // ImportDirective
  118. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ImportIntroducer, 0, CARBON_TOKEN(Import))
  119. CARBON_PARSE_NODE_KIND_BRACKET(ImportDirective, ImportIntroducer,
  120. CARBON_TOKEN(Semi)
  121. CARBON_IF_ERROR(CARBON_TOKEN(Import)))
  122. // `library` as directive:
  123. // LibraryIntroducer
  124. // DefaultLibrary or _external_: LibraryName
  125. // LibraryDirective
  126. CARBON_PARSE_NODE_KIND_CHILD_COUNT(DefaultLibrary, 0, CARBON_TOKEN(Default))
  127. CARBON_PARSE_NODE_KIND_CHILD_COUNT(LibraryIntroducer, 0, CARBON_TOKEN(Library))
  128. CARBON_PARSE_NODE_KIND_BRACKET(LibraryDirective, LibraryIntroducer,
  129. CARBON_TOKEN(Semi)
  130. CARBON_IF_ERROR(CARBON_TOKEN(Library)))
  131. // `library` in `package` or `import`:
  132. // _external_: LibraryName
  133. // LibrarySpecifier
  134. CARBON_PARSE_NODE_KIND_CHILD_COUNT(LibrarySpecifier, 1, CARBON_TOKEN(Library))
  135. // `namespace`:
  136. // NamespaceStart
  137. // _external_: Name or QualifiedDecl
  138. // Namespace
  139. CARBON_PARSE_NODE_KIND_CHILD_COUNT(NamespaceStart, 0, CARBON_TOKEN(Namespace))
  140. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Namespace, 2,
  141. CARBON_TOKEN(Semi)
  142. CARBON_IF_ERROR(CARBON_TOKEN(Namespace)))
  143. // A code block:
  144. // CodeBlockStart
  145. // _external_: statements
  146. // CodeBlock
  147. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CodeBlockStart, 0,
  148. CARBON_TOKEN(OpenCurlyBrace)
  149. CARBON_IF_ERROR(CARBON_ANY_TOKEN))
  150. CARBON_PARSE_NODE_KIND_BRACKET(CodeBlock, CodeBlockStart,
  151. CARBON_TOKEN(CloseCurlyBrace)
  152. CARBON_IF_ERROR(CARBON_ANY_TOKEN))
  153. // `fn`:
  154. // FunctionIntroducer
  155. // _external_: Name or QualifiedDecl
  156. // _external_: ParamList
  157. // _external_: type expression
  158. // ReturnType
  159. // FunctionDefinitionStart
  160. // _external_: statements
  161. // FunctionDefinition
  162. //
  163. // The above is the structure for a definition; for a declaration,
  164. // FunctionDefinitionStart and later nodes are removed and replaced by
  165. // FunctionDecl.
  166. CARBON_PARSE_NODE_KIND_CHILD_COUNT(FunctionIntroducer, 0, CARBON_TOKEN(Fn))
  167. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnType, 1, CARBON_TOKEN(MinusGreater))
  168. CARBON_PARSE_NODE_KIND_BRACKET(FunctionDefinitionStart, FunctionIntroducer,
  169. CARBON_TOKEN(OpenCurlyBrace))
  170. CARBON_PARSE_NODE_KIND_BRACKET(FunctionDefinition, FunctionDefinitionStart,
  171. CARBON_TOKEN(CloseCurlyBrace))
  172. CARBON_PARSE_NODE_KIND_BRACKET(FunctionDecl, FunctionIntroducer,
  173. CARBON_TOKEN(Semi)
  174. CARBON_IF_ERROR(CARBON_TOKEN(Fn)))
  175. // A parameter list, possibly implicit:
  176. // [Implicit]ParamertListStart
  177. // _external_: [Generic]PatternBinding
  178. // ParamListComma
  179. // [Implicit]ParamList
  180. //
  181. // Exprs and ParamListComma may repeat with ParamListComma as a
  182. // separator.
  183. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ParamListStart, 0, CARBON_TOKEN(OpenParen))
  184. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ImplicitParamListStart, 0,
  185. CARBON_TOKEN(OpenSquareBracket))
  186. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ParamListComma, 0, CARBON_TOKEN(Comma))
  187. CARBON_PARSE_NODE_KIND_BRACKET(ParamList, ParamListStart,
  188. CARBON_TOKEN(CloseParen))
  189. CARBON_PARSE_NODE_KIND_BRACKET(ImplicitParamList, ImplicitParamListStart,
  190. CARBON_TOKEN(CloseSquareBracket))
  191. // An array type, such as `[i32; 3]` or `[i32;]`:
  192. // ArrayExprStart
  193. // _external_: type expression
  194. // ArrayExprSemi
  195. // _optional_ _external_: expression
  196. // ArrayExpr
  197. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ArrayExprStart, 0,
  198. CARBON_TOKEN(OpenSquareBracket))
  199. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ArrayExprSemi, 2,
  200. CARBON_TOKEN(Semi)
  201. CARBON_IF_ERROR(CARBON_ANY_TOKEN))
  202. CARBON_PARSE_NODE_KIND_BRACKET(ArrayExpr, ArrayExprSemi,
  203. CARBON_TOKEN(CloseSquareBracket))
  204. // A pattern binding, such as `name: Type`:
  205. // Name or SelfValueName
  206. // _external_: type expression
  207. // [Generic]PatternBinding
  208. // _optional_ Address
  209. // _optional_ Template
  210. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PatternBinding, 2,
  211. CARBON_TOKEN(Colon)
  212. CARBON_IF_ERROR(CARBON_ANY_TOKEN))
  213. CARBON_PARSE_NODE_KIND_CHILD_COUNT(GenericPatternBinding, 2,
  214. CARBON_TOKEN(ColonExclaim))
  215. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Address, 1, CARBON_TOKEN(Addr))
  216. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Template, 1, CARBON_TOKEN(Template))
  217. // `let`:
  218. // LetIntroducer
  219. // _external_: PatternBinding
  220. // LetInitializer
  221. // _external_: expression
  222. // LetDecl
  223. CARBON_PARSE_NODE_KIND_CHILD_COUNT(LetIntroducer, 0, CARBON_TOKEN(Let))
  224. CARBON_PARSE_NODE_KIND_CHILD_COUNT(LetInitializer, 0, CARBON_TOKEN(Equal))
  225. CARBON_PARSE_NODE_KIND_BRACKET(LetDecl, LetIntroducer,
  226. CARBON_TOKEN(Semi)
  227. CARBON_IF_ERROR(CARBON_TOKEN(Let)))
  228. // `var` and `returned var`:
  229. // VariableIntroducer
  230. // _optional_ ReturnedModifier
  231. // _external_: PatternBinding
  232. // _optional_ VariableInitializer
  233. // _optional_ _external_: expression
  234. // VariableDecl
  235. //
  236. // The VariableInitializer and following expression are paired: either both will
  237. // be present, or neither will.
  238. CARBON_PARSE_NODE_KIND_CHILD_COUNT(VariableIntroducer, 0,
  239. CARBON_TOKEN(Var)
  240. CARBON_IF_ERROR(CARBON_TOKEN(Returned)))
  241. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnedModifier, 0, CARBON_TOKEN(Returned))
  242. CARBON_PARSE_NODE_KIND_CHILD_COUNT(VariableInitializer, 0, CARBON_TOKEN(Equal))
  243. CARBON_PARSE_NODE_KIND_BRACKET(VariableDecl, VariableIntroducer,
  244. CARBON_TOKEN(Semi)
  245. CARBON_IF_ERROR(CARBON_TOKEN(Var)
  246. CARBON_TOKEN(Returned)))
  247. // An expression statement:
  248. // _external_: expression
  249. // ExprStatement
  250. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ExprStatement, 1, CARBON_TOKEN(Semi))
  251. // `break`:
  252. // BreakStatementStart
  253. // BreakStatement
  254. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BreakStatementStart, 0, CARBON_TOKEN(Break))
  255. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BreakStatement, 1,
  256. CARBON_TOKEN(Semi)
  257. CARBON_IF_ERROR(CARBON_TOKEN(Break)))
  258. // `continue`:
  259. // ContinueStatementStart
  260. // ContinueStatement
  261. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ContinueStatementStart, 0,
  262. CARBON_TOKEN(Continue))
  263. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ContinueStatement, 1,
  264. CARBON_TOKEN(Semi)
  265. CARBON_IF_ERROR(CARBON_TOKEN(Continue)))
  266. // `return`:
  267. // ReturnStatementStart
  268. // _optional_ ReturnVarModifier or _external_: expression
  269. // ReturnStatement
  270. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnStatementStart, 0,
  271. CARBON_TOKEN(Return))
  272. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnVarModifier, 0, CARBON_TOKEN(Var))
  273. CARBON_PARSE_NODE_KIND_BRACKET(ReturnStatement, ReturnStatementStart,
  274. CARBON_TOKEN(Semi)
  275. CARBON_IF_ERROR(CARBON_TOKEN(Return)))
  276. // `for`:
  277. // ForHeaderStart
  278. // VariableIntroducer
  279. // _external_: PatternBinding
  280. // ForIn
  281. // _external_: expression
  282. // ForHeader
  283. // _external_: CodeBlock
  284. // ForStatement
  285. //
  286. // Versus a normal `var`, ForIn replaces VariableDecl.
  287. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ForHeaderStart, 0,
  288. CARBON_TOKEN(OpenParen)
  289. CARBON_IF_ERROR(CARBON_TOKEN(For)))
  290. CARBON_PARSE_NODE_KIND_BRACKET(ForIn, VariableIntroducer,
  291. CARBON_TOKEN(In)
  292. CARBON_IF_ERROR(CARBON_ANY_TOKEN))
  293. CARBON_PARSE_NODE_KIND_BRACKET(ForHeader, ForHeaderStart,
  294. CARBON_TOKEN(CloseParen)
  295. CARBON_IF_ERROR(CARBON_TOKEN(For)))
  296. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ForStatement, 2, CARBON_TOKEN(For))
  297. // `if` statement + `else`:
  298. // IfConditionStart
  299. // _external_: expression
  300. // IfCondition
  301. // _external_: CodeBlock
  302. // IfStatementElse
  303. // _external_: CodeBlock or IfStatement
  304. // IfStatement
  305. //
  306. // IfStatementElse and the following node are optional based on `else` presence.
  307. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfConditionStart, 0,
  308. CARBON_TOKEN(OpenParen)
  309. CARBON_IF_ERROR(CARBON_TOKEN(If)))
  310. CARBON_PARSE_NODE_KIND_BRACKET(IfCondition, IfConditionStart,
  311. CARBON_TOKEN(CloseParen)
  312. CARBON_IF_ERROR(CARBON_TOKEN(If)))
  313. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfStatementElse, 0, CARBON_TOKEN(Else))
  314. CARBON_PARSE_NODE_KIND_BRACKET(IfStatement, IfCondition, CARBON_TOKEN(If))
  315. // `while`:
  316. // WhileConditionStart
  317. // _external_: expression
  318. // WhileCondition
  319. // _external_: CodeBlock
  320. // WhileStatement
  321. CARBON_PARSE_NODE_KIND_CHILD_COUNT(WhileConditionStart, 0,
  322. CARBON_TOKEN(OpenParen))
  323. CARBON_PARSE_NODE_KIND_BRACKET(WhileCondition, WhileConditionStart,
  324. CARBON_TOKEN(CloseParen))
  325. CARBON_PARSE_NODE_KIND_CHILD_COUNT(WhileStatement, 2, CARBON_TOKEN(While))
  326. // Index expressions, such as `a[1]`:
  327. // _external_: expression
  328. // IndexExprStart
  329. // _external_: expression
  330. // IndexExpr
  331. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IndexExprStart, 1,
  332. CARBON_TOKEN(OpenSquareBracket))
  333. CARBON_PARSE_NODE_KIND_BRACKET(IndexExpr, IndexExprStart,
  334. CARBON_TOKEN(CloseSquareBracket))
  335. // Parenthesized expressions, such as `(2)`:
  336. // ParenExprOrTupleLiteralStart
  337. // _external_: expression
  338. // ParenExpr
  339. //
  340. // Tuples, such as `(1, 2)`:
  341. // ParenExprOrTupleLiteralStart
  342. // _external_: expression
  343. // TupleLiteralComma
  344. // TupleLiteral
  345. //
  346. // Exprs and TupleLiteralComma may repeat with TupleLiteralComma as a
  347. // separator.
  348. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ParenExprOrTupleLiteralStart, 0,
  349. CARBON_TOKEN(OpenParen))
  350. CARBON_PARSE_NODE_KIND_BRACKET(ParenExpr, ParenExprOrTupleLiteralStart,
  351. CARBON_TOKEN(CloseParen))
  352. CARBON_PARSE_NODE_KIND_CHILD_COUNT(TupleLiteralComma, 0, CARBON_TOKEN(Comma))
  353. CARBON_PARSE_NODE_KIND_BRACKET(TupleLiteral, ParenExprOrTupleLiteralStart,
  354. CARBON_TOKEN(CloseParen))
  355. // Call expressions, such as `a()`:
  356. // _external_: expression
  357. // CallExprStart
  358. // _external_: expression
  359. // CallExprComma
  360. // CallExpr
  361. //
  362. // Exprs and CallExprComma may repeat with CallExprComma as a
  363. // separator.
  364. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CallExprStart, 1, CARBON_TOKEN(OpenParen))
  365. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CallExprComma, 0, CARBON_TOKEN(Comma))
  366. CARBON_PARSE_NODE_KIND_BRACKET(CallExpr, CallExprStart,
  367. CARBON_TOKEN(CloseParen))
  368. // A qualified declaration, such as `a.b`:
  369. // _external_: Name or QualifiedDecl
  370. // _external_: Name
  371. // QualifiedDecl
  372. //
  373. // TODO: This will eventually more general expressions, for example with
  374. // `GenericType(type_args).ChildType(child_type_args).Name`.
  375. CARBON_PARSE_NODE_KIND_CHILD_COUNT(QualifiedDecl, 2, CARBON_TOKEN(Period))
  376. // A member access expression, such as `a.b` or
  377. // `GetObject().(Interface.member)`:
  378. // _external_: lhs expression
  379. // _external_: rhs expression
  380. // QualifiedExpr
  381. CARBON_PARSE_NODE_KIND_CHILD_COUNT(MemberAccessExpr, 2, CARBON_TOKEN(Period))
  382. // A pointer member access expression, such as `a->b` or
  383. // `GetObject()->(Interface.member)`:
  384. // _external_: lhs expression
  385. // _external_: rhs expression
  386. // QualifiedExpr
  387. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PointerMemberAccessExpr, 2,
  388. CARBON_TOKEN(MinusGreater))
  389. // A value literal.
  390. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(BoolLiteralFalse, CARBON_TOKEN(False))
  391. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(BoolLiteralTrue, CARBON_TOKEN(True))
  392. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(IntegerLiteral,
  393. CARBON_TOKEN(IntegerLiteral))
  394. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(FloatingPointLiteral,
  395. CARBON_TOKEN(RealLiteral))
  396. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(StringLiteral, CARBON_TOKEN(StringLiteral))
  397. // A type literal.
  398. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(BoolTypeLiteral, CARBON_TOKEN(Bool))
  399. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(IntegerTypeLiteral,
  400. CARBON_TOKEN(IntegerTypeLiteral))
  401. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(UnsignedIntegerTypeLiteral,
  402. CARBON_TOKEN(UnsignedIntegerTypeLiteral))
  403. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(FloatingPointTypeLiteral,
  404. CARBON_TOKEN(FloatingPointTypeLiteral))
  405. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(StringTypeLiteral,
  406. CARBON_TOKEN(StringTypeLiteral))
  407. CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(TypeTypeLiteral, CARBON_TOKEN(Type))
  408. // clang-format off
  409. // A prefix operator:
  410. // _external_: expression
  411. // PrefixOperator
  412. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PrefixOperator, 1,
  413. CARBON_TOKEN(Star)
  414. CARBON_TOKEN(Amp)
  415. CARBON_TOKEN(Not)
  416. CARBON_TOKEN(Minus)
  417. CARBON_TOKEN(MinusMinus)
  418. CARBON_TOKEN(PlusPlus)
  419. CARBON_TOKEN(Caret)
  420. CARBON_TOKEN(Const))
  421. // An infix operator:
  422. // _external_: lhs expression
  423. // _external_: rhs expression
  424. // InfixOperator
  425. CARBON_PARSE_NODE_KIND_CHILD_COUNT(InfixOperator, 2,
  426. CARBON_TOKEN(Amp)
  427. CARBON_TOKEN(AmpEqual)
  428. CARBON_TOKEN(And)
  429. CARBON_TOKEN(As)
  430. CARBON_TOKEN(Caret)
  431. CARBON_TOKEN(CaretEqual)
  432. CARBON_TOKEN(Equal)
  433. CARBON_TOKEN(EqualEqual)
  434. CARBON_TOKEN(ExclaimEqual)
  435. CARBON_TOKEN(Greater)
  436. CARBON_TOKEN(GreaterEqual)
  437. CARBON_TOKEN(GreaterGreater)
  438. CARBON_TOKEN(GreaterGreaterEqual)
  439. CARBON_TOKEN(Less)
  440. CARBON_TOKEN(LessEqual)
  441. CARBON_TOKEN(LessLess)
  442. CARBON_TOKEN(LessLessEqual)
  443. CARBON_TOKEN(Minus)
  444. CARBON_TOKEN(MinusEqual)
  445. CARBON_TOKEN(Or)
  446. CARBON_TOKEN(Percent)
  447. CARBON_TOKEN(PercentEqual)
  448. CARBON_TOKEN(Pipe)
  449. CARBON_TOKEN(PipeEqual)
  450. CARBON_TOKEN(Plus)
  451. CARBON_TOKEN(PlusEqual)
  452. CARBON_TOKEN(Slash)
  453. CARBON_TOKEN(SlashEqual)
  454. CARBON_TOKEN(Star)
  455. CARBON_TOKEN(StarEqual))
  456. // clang-format on
  457. // The first operand of a short-circuiting infix operator:
  458. // _external_: expression
  459. // ShortCircuitOperand
  460. // _external_: expression
  461. // _external_: InfixOperator
  462. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ShortCircuitOperand, 1,
  463. CARBON_TOKEN(And) CARBON_TOKEN(Or))
  464. // A postfix operator:
  465. // _external_: expression
  466. // PostfixOperator
  467. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PostfixOperator, 1, CARBON_TOKEN(Star))
  468. // `if` expression + `then` + `else`:
  469. // _external_: expression
  470. // IfExprIf
  471. // _external_: expression
  472. // IfExprThen
  473. // _external_: expression
  474. // IfExprElse
  475. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfExprIf, 1, CARBON_TOKEN(If))
  476. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfExprThen, 1, CARBON_TOKEN(Then))
  477. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfExprElse, 3,
  478. CARBON_TOKEN(Else)
  479. CARBON_IF_ERROR(CARBON_TOKEN(If)))
  480. // Struct literals, such as `{.a = 0}`:
  481. // StructLiteralOrStructTypeLiteralStart
  482. // _external_: Name
  483. // StructFieldDesignator
  484. // _external_: expression
  485. // StructFieldValue
  486. // StructComma
  487. // StructLiteral
  488. //
  489. // Struct type literals, such as `{.a: i32}`:
  490. // _external_: Name
  491. // StructFieldDesignator
  492. // _external_: type expression
  493. // StructFieldType
  494. // StructComma
  495. // StructTypeLiteral
  496. //
  497. // Elements (StructFieldValue and StructFieldType, respectively) and StructComma
  498. // may repeat with StructComma as a separator.
  499. //
  500. // When a valid StructFieldType or StructFieldValue cannot be formed, elements
  501. // may be replaced by StructFieldUnknown, which may have a preceding sibling
  502. // StructFieldDesignator if one was successfully parsed.
  503. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructLiteralOrStructTypeLiteralStart, 0,
  504. CARBON_TOKEN(OpenCurlyBrace))
  505. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldDesignator, 1,
  506. CARBON_TOKEN(Period))
  507. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldValue, 2, CARBON_TOKEN(Equal))
  508. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldType, 2, CARBON_TOKEN(Colon))
  509. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldUnknown, 0,
  510. CARBON_IF_ERROR(CARBON_ANY_TOKEN))
  511. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructComma, 0, CARBON_TOKEN(Comma))
  512. CARBON_PARSE_NODE_KIND_BRACKET(StructLiteral,
  513. StructLiteralOrStructTypeLiteralStart,
  514. CARBON_TOKEN(CloseCurlyBrace))
  515. CARBON_PARSE_NODE_KIND_BRACKET(StructTypeLiteral,
  516. StructLiteralOrStructTypeLiteralStart,
  517. CARBON_TOKEN(CloseCurlyBrace))
  518. // `class`:
  519. // ClassIntroducer
  520. // _optional_ AbstractModifier or BaseModifier
  521. // _external_: Name or QualifiedDecl
  522. // ClassDefinitionStart
  523. // _external_: declarations
  524. // ClassDefinition
  525. //
  526. // The above is the structure for a definition; for a declaration,
  527. // ClassDefinitionStart and later nodes are removed and replaced by
  528. // ClassDecl.
  529. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ClassIntroducer, 0, CARBON_TOKEN(Class))
  530. CARBON_PARSE_NODE_KIND_CHILD_COUNT(AbstractModifier, 0, CARBON_TOKEN(Abstract))
  531. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BaseModifier, 0, CARBON_TOKEN(Base))
  532. CARBON_PARSE_NODE_KIND_BRACKET(ClassDefinitionStart, ClassIntroducer,
  533. CARBON_TOKEN(OpenCurlyBrace))
  534. CARBON_PARSE_NODE_KIND_BRACKET(ClassDefinition, ClassDefinitionStart,
  535. CARBON_TOKEN(CloseCurlyBrace))
  536. CARBON_PARSE_NODE_KIND_BRACKET(ClassDecl, ClassIntroducer,
  537. CARBON_TOKEN(Semi)
  538. CARBON_IF_ERROR(CARBON_TOKEN(Class)))
  539. // `interface`:
  540. // InterfaceIntroducer
  541. // _external_: Name or QualifiedDecl
  542. // InterfaceDefinitionStart
  543. // _external_: declarations
  544. // InterfaceDefinition
  545. //
  546. // The above is the structure for a definition; for a declaration,
  547. // InterfaceDefinitionStart and later nodes are removed and replaced by
  548. // InterfaceDecl.
  549. CARBON_PARSE_NODE_KIND_CHILD_COUNT(InterfaceIntroducer, 0,
  550. CARBON_TOKEN(Interface))
  551. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceDefinitionStart, InterfaceIntroducer,
  552. CARBON_TOKEN(OpenCurlyBrace))
  553. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceDefinition, InterfaceDefinitionStart,
  554. CARBON_TOKEN(CloseCurlyBrace))
  555. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceDecl, InterfaceIntroducer,
  556. CARBON_TOKEN(Semi)
  557. CARBON_IF_ERROR(CARBON_TOKEN(Interface)))
  558. // `constraint`:
  559. // NamedConstraintIntroducer
  560. // _external_: Name or QualifiedDecl
  561. // NamedConstraintDefinitionStart
  562. // _external_: declarations
  563. // NamedConstraintDefinition
  564. //
  565. // The above is the structure for a definition; for a declaration,
  566. // NamedConstraintDefinitionStart and later nodes are removed and replaced by
  567. // NamedConstraintDecl.
  568. CARBON_PARSE_NODE_KIND_CHILD_COUNT(NamedConstraintIntroducer, 0,
  569. CARBON_TOKEN(Constraint))
  570. CARBON_PARSE_NODE_KIND_BRACKET(NamedConstraintDefinitionStart,
  571. NamedConstraintIntroducer,
  572. CARBON_TOKEN(OpenCurlyBrace))
  573. CARBON_PARSE_NODE_KIND_BRACKET(NamedConstraintDefinition,
  574. NamedConstraintDefinitionStart,
  575. CARBON_TOKEN(CloseCurlyBrace))
  576. CARBON_PARSE_NODE_KIND_BRACKET(NamedConstraintDecl, NamedConstraintIntroducer,
  577. CARBON_TOKEN(Semi))
  578. // The `self` value and `Self` type identifier keywords. Typically of the form
  579. // `self: Self`:
  580. // SelfValueName
  581. // SelfValueNameExpr
  582. // SelfTypeNameExpr
  583. // PatternBinding
  584. CARBON_PARSE_NODE_KIND_CHILD_COUNT(SelfValueName, 0,
  585. CARBON_TOKEN(SelfValueIdentifier))
  586. CARBON_PARSE_NODE_KIND_CHILD_COUNT(SelfValueNameExpr, 0,
  587. CARBON_TOKEN(SelfValueIdentifier))
  588. CARBON_PARSE_NODE_KIND_CHILD_COUNT(SelfTypeNameExpr, 0,
  589. CARBON_TOKEN(SelfTypeIdentifier))
  590. #undef CARBON_PARSE_NODE_KIND
  591. #undef CARBON_PARSE_NODE_KIND_BRACKET
  592. #undef CARBON_PARSE_NODE_KIND_CHILD_COUNT
  593. #undef CARBON_PARSE_NODE_KIND_TOKEN_LITERAL
  594. #undef CARBON_TOKEN
  595. #undef CARBON_ANY_TOKEN
  596. #undef CARBON_IF_ERROR