node_kind.def 31 KB

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