node_kind.def 26 KB

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