parse_node_kind.def 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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)
  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)
  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. // This tree represents the subset relationship between these macros, where if a
  22. // specific x-macro isn't defined, it'll fall back to the parent macro.
  23. //
  24. // Parse nodes are clustered based on language feature. Comments will show their
  25. // relationship in postorder, using indentation for child node relationships.
  26. #if !(defined(CARBON_PARSE_NODE_KIND) || \
  27. (defined(CARBON_PARSE_NODE_KIND_BRACKET) && \
  28. defined(CARBON_PARSE_NODE_KIND_CHILD_COUNT)))
  29. #error "Must define CARBON_PARSE_NODE_KIND family x-macros to use this file."
  30. #endif
  31. // The BRACKET and CHILD_COUNT macros will use CARBON_PARSE_NODE_KIND by default
  32. // when undefined.
  33. #ifndef CARBON_PARSE_NODE_KIND_BRACKET
  34. #define CARBON_PARSE_NODE_KIND_BRACKET(Name, ...) CARBON_PARSE_NODE_KIND(Name)
  35. #endif
  36. #ifndef CARBON_PARSE_NODE_KIND_CHILD_COUNT
  37. #define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, ...) \
  38. CARBON_PARSE_NODE_KIND(Name)
  39. #endif
  40. // The end of the file.
  41. CARBON_PARSE_NODE_KIND_CHILD_COUNT(FileEnd, 0)
  42. // An invalid parse. Used to balance the parse tree. Always has an error.
  43. CARBON_PARSE_NODE_KIND_CHILD_COUNT(InvalidParse, 0)
  44. // An empty declaration, such as `;`.
  45. CARBON_PARSE_NODE_KIND_CHILD_COUNT(EmptyDeclaration, 0)
  46. // A name in a non-expression context, such as a declaration.
  47. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, 0)
  48. // A name in an expression context.
  49. CARBON_PARSE_NODE_KIND_CHILD_COUNT(NameExpression, 0)
  50. // `package`:
  51. // PackageIntroducer
  52. // _external_: Name
  53. // _external_: Literal
  54. // PackageLibrary
  55. // PackageApi or PackageImpl
  56. // PackageDirective
  57. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageIntroducer, 0)
  58. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageApi, 0)
  59. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageImpl, 0)
  60. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageLibrary, 1)
  61. CARBON_PARSE_NODE_KIND_BRACKET(PackageDirective, PackageIntroducer)
  62. // `namespace`:
  63. // NamespaceStart
  64. // _external_: Name or QualifiedDeclaration
  65. // Namespace
  66. CARBON_PARSE_NODE_KIND_CHILD_COUNT(NamespaceStart, 0)
  67. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Namespace, 2)
  68. // A code block:
  69. // CodeBlockStart
  70. // _external_: statements
  71. // CodeBlock
  72. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CodeBlockStart, 0)
  73. CARBON_PARSE_NODE_KIND_BRACKET(CodeBlock, CodeBlockStart)
  74. // `fn`:
  75. // FunctionIntroducer
  76. // _external_: Name or QualifiedDeclaration
  77. // _external_: ParameterList
  78. // _external_: type expression
  79. // ReturnType
  80. // FunctionDefinitionStart
  81. // _external_: statements
  82. // FunctionDefinition
  83. //
  84. // The above is the structure for a definition; for a declaration,
  85. // FunctionDefinitionStart and later nodes are removed and replaced by
  86. // FunctionDeclaration.
  87. CARBON_PARSE_NODE_KIND_CHILD_COUNT(FunctionIntroducer, 0)
  88. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnType, 1)
  89. CARBON_PARSE_NODE_KIND_BRACKET(FunctionDefinitionStart, FunctionIntroducer)
  90. CARBON_PARSE_NODE_KIND_BRACKET(FunctionDefinition, FunctionDefinitionStart)
  91. CARBON_PARSE_NODE_KIND_BRACKET(FunctionDeclaration, FunctionIntroducer)
  92. // A parameter list, possibly deduced:
  93. // [Deduced]ParamertListStart
  94. // _external_: [Generic]PatternBinding
  95. // ParameterListComma
  96. // [Deduced]ParameterList
  97. //
  98. // Expressions and ParameterListComma may repeat with ParameterListComma as a
  99. // separator.
  100. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ParameterListStart, 0)
  101. CARBON_PARSE_NODE_KIND_CHILD_COUNT(DeducedParameterListStart, 0)
  102. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ParameterListComma, 0)
  103. CARBON_PARSE_NODE_KIND_BRACKET(ParameterList, ParameterListStart)
  104. CARBON_PARSE_NODE_KIND_BRACKET(DeducedParameterList, DeducedParameterListStart)
  105. // An array type, such as `[i32; 3]` or `[i32;]`:
  106. // ArrayExpressionStart
  107. // _external_: type expression
  108. // ArrayExpressionSemi
  109. // _external_: expression
  110. // ArrayExpression
  111. //
  112. // The bound expression is optional.
  113. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ArrayExpressionStart, 0)
  114. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ArrayExpressionSemi, 2)
  115. CARBON_PARSE_NODE_KIND_BRACKET(ArrayExpression, ArrayExpressionSemi)
  116. // A pattern binding, such as `name: Type`:
  117. // Name
  118. // _external_: type expression
  119. // [Generic]PatternBinding
  120. //
  121. // Address and Template may be parents to [Generic]PatternBinding, in that
  122. // order.
  123. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PatternBinding, 2)
  124. CARBON_PARSE_NODE_KIND_CHILD_COUNT(GenericPatternBinding, 2)
  125. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Address, 1)
  126. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Template, 1)
  127. // `var`:
  128. // VariableIntroducer
  129. // _external_: PatternBinding
  130. // optional VariableInitializer
  131. // optional _external_: expression
  132. // VariableDeclaration
  133. //
  134. // The VariableInitializer and following expression are paired: either both will
  135. // be present, or neither will.
  136. CARBON_PARSE_NODE_KIND_CHILD_COUNT(VariableIntroducer, 0)
  137. CARBON_PARSE_NODE_KIND_CHILD_COUNT(VariableInitializer, 0)
  138. CARBON_PARSE_NODE_KIND_BRACKET(VariableDeclaration, VariableIntroducer)
  139. // An expression statement:
  140. // _external_: expression
  141. // ExpressionStatement
  142. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ExpressionStatement, 1)
  143. // `break`:
  144. // BreakStatementStart
  145. // BreakStatement
  146. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BreakStatementStart, 0)
  147. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BreakStatement, 1)
  148. // `continue`:
  149. // ContinueStatementStart
  150. // ContinueStatement
  151. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ContinueStatementStart, 0)
  152. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ContinueStatement, 1)
  153. // `return`:
  154. // ReturnStatementStart
  155. // _external_: expression
  156. // ReturnStatement
  157. //
  158. // The child expression is optional.
  159. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnStatementStart, 0)
  160. CARBON_PARSE_NODE_KIND_BRACKET(ReturnStatement, ReturnStatementStart)
  161. // `for`:
  162. // ForHeaderStart
  163. // VariableIntroducer
  164. // _external_: PatternBinding
  165. // _external_: type expression
  166. // ForIn
  167. // _external_: expression
  168. // ForHeader
  169. // _external_: CodeBlock
  170. // ForStatement
  171. //
  172. // Versus a normal `var`, ForIn replaces VariableDeclaration.
  173. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ForHeaderStart, 0)
  174. CARBON_PARSE_NODE_KIND_BRACKET(ForIn, VariableIntroducer)
  175. CARBON_PARSE_NODE_KIND_BRACKET(ForHeader, ForHeaderStart)
  176. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ForStatement, 2)
  177. // `if` statement + `else`:
  178. // IfConditionStart
  179. // _external_: expression
  180. // IfCondition
  181. // _external_: CodeBlock
  182. // IfStatementElse
  183. // _external_: CodeBlock or IfStatement
  184. // IfStatement
  185. //
  186. // IfStatementElse and the following node are optional based on `else` presence.
  187. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfConditionStart, 0)
  188. CARBON_PARSE_NODE_KIND_BRACKET(IfCondition, IfConditionStart)
  189. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfStatementElse, 0)
  190. CARBON_PARSE_NODE_KIND_BRACKET(IfStatement, IfCondition)
  191. // `while`:
  192. // WhileConditionStart
  193. // _external_: expression
  194. // WhileCondition
  195. // _external_: CodeBlock
  196. // WhileStatement
  197. CARBON_PARSE_NODE_KIND_CHILD_COUNT(WhileConditionStart, 0)
  198. CARBON_PARSE_NODE_KIND_BRACKET(WhileCondition, WhileConditionStart)
  199. CARBON_PARSE_NODE_KIND_CHILD_COUNT(WhileStatement, 2)
  200. // Index expressions, such as `a[1]`:
  201. // _external_: expression
  202. // IndexExpressionStart
  203. // _external_: expression
  204. // IndexExpression
  205. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IndexExpressionStart, 1)
  206. CARBON_PARSE_NODE_KIND_BRACKET(IndexExpression, IndexExpressionStart)
  207. // Parenthesized expressions, such as `(2)`:
  208. // ParenExpressionOrTupleLiteralStart
  209. // _external_: expression
  210. // ParenExpression
  211. //
  212. // Tuples, such as `(1, 2)`:
  213. // ParenExpressionOrTupleLiteralStart
  214. // _external_: expression
  215. // TupleLiteralComma
  216. // TupleLiteral
  217. //
  218. // Expressions and TupleLiteralComma may repeat with TupleLiteralComma as a
  219. // separator.
  220. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ParenExpressionOrTupleLiteralStart, 0)
  221. CARBON_PARSE_NODE_KIND_BRACKET(ParenExpression,
  222. ParenExpressionOrTupleLiteralStart)
  223. CARBON_PARSE_NODE_KIND_CHILD_COUNT(TupleLiteralComma, 0)
  224. CARBON_PARSE_NODE_KIND_BRACKET(TupleLiteral, ParenExpressionOrTupleLiteralStart)
  225. // Call expressions, such as `a()`:
  226. // _external_: expression
  227. // CallExpressionStart
  228. // _external_: expression
  229. // CallExpressionComma
  230. // CallExpression
  231. //
  232. // Expressions and CallExpressionComma may repeat with CallExpressionComma as a
  233. // separator.
  234. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CallExpressionStart, 1)
  235. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CallExpressionComma, 0)
  236. CARBON_PARSE_NODE_KIND_BRACKET(CallExpression, CallExpressionStart)
  237. // A qualified declaration, such as `a.b`:
  238. // _external_: NameExpression or QualifiedDeclaration
  239. // _external_: Name
  240. // QualifiedDeclaration
  241. //
  242. // TODO: This will eventually more general expressions, for example with
  243. // `GenericType(type_args).ChildType(child_type_args).Name`.
  244. CARBON_PARSE_NODE_KIND_CHILD_COUNT(QualifiedDeclaration, 2)
  245. // A member access expression, such as `a.b` or
  246. // `GetObject().(Interface.member)`:
  247. // _external_: lhs expression
  248. // _external_: rhs expression
  249. // QualifiedExpression
  250. CARBON_PARSE_NODE_KIND_CHILD_COUNT(MemberAccessExpression, 2)
  251. // A pointer member access expression, such as `a->b` or
  252. // `GetObject()->(Interface.member)`:
  253. // _external_: lhs expression
  254. // _external_: rhs expression
  255. // QualifiedExpression
  256. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PointerMemberAccessExpression, 2)
  257. // A literal.
  258. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Literal, 0)
  259. // A prefix operator:
  260. // _external_: expression
  261. // PrefixOperator
  262. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PrefixOperator, 1)
  263. // An infix operator:
  264. // _external_: lhs expression
  265. // _external_: rhs expression
  266. // InfixOperator
  267. CARBON_PARSE_NODE_KIND_CHILD_COUNT(InfixOperator, 2)
  268. // The first operand of a short-circuiting infix operator:
  269. // _external_: expression
  270. // ShortCircuitOperand
  271. // _external_: expression
  272. // _external_: InfixOperator
  273. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ShortCircuitOperand, 1)
  274. // A postfix operator:
  275. // _external_: expression
  276. // PostfixOperator
  277. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PostfixOperator, 1)
  278. // `if` expression + `then` + `else`:
  279. // _external_: expression
  280. // IfExpressionIf
  281. // _external_: expression
  282. // IfExpressionThen
  283. // _external_: expression
  284. // IfExpressionElse
  285. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfExpressionIf, 1)
  286. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfExpressionThen, 1)
  287. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfExpressionElse, 3)
  288. // Struct literals, such as `{.a = 0}`:
  289. // StructLiteralOrStructTypeLiteralStart
  290. // _external_: Name
  291. // StructFieldDesignator
  292. // _external_: expression
  293. // StructFieldValue
  294. // StructComma
  295. // StructLiteral
  296. //
  297. // Struct type literals, such as `{.a: i32}`:
  298. // _external_: Name
  299. // StructFieldDesignator
  300. // _external_: type expression
  301. // StructFieldType
  302. // StructComma
  303. // StructTypeLiteral
  304. //
  305. // Elements (StructFieldValue and StructFieldType, respectively) and StructComma
  306. // may repeat with StructComma as a separator.
  307. //
  308. // When a valid StructFieldType or StructFieldValue cannot be formed, elements
  309. // may be replaced by StructFieldUnknown, which may have a preceding sibling
  310. // StructFieldDesignator if one was successfully parsed.
  311. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructLiteralOrStructTypeLiteralStart, 0)
  312. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldDesignator, 1)
  313. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldValue, 2)
  314. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldType, 2)
  315. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldUnknown, 0)
  316. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructComma, 0)
  317. CARBON_PARSE_NODE_KIND_BRACKET(StructLiteral,
  318. StructLiteralOrStructTypeLiteralStart)
  319. CARBON_PARSE_NODE_KIND_BRACKET(StructTypeLiteral,
  320. StructLiteralOrStructTypeLiteralStart)
  321. // `class`:
  322. // ClassIntroducer
  323. // _external_: Name or QualifiedDeclaration
  324. // ClassDefinitionStart
  325. // _external_: declarations
  326. // ClassDefinition
  327. //
  328. // The above is the structure for a definition; for a declaration,
  329. // ClassDefinitionStart and later nodes are removed and replaced by
  330. // ClassDeclaration.
  331. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ClassIntroducer, 0)
  332. CARBON_PARSE_NODE_KIND_BRACKET(ClassDefinitionStart, ClassIntroducer)
  333. CARBON_PARSE_NODE_KIND_BRACKET(ClassDefinition, ClassDefinitionStart)
  334. CARBON_PARSE_NODE_KIND_BRACKET(ClassDeclaration, ClassIntroducer)
  335. // `interface`:
  336. // InterfaceIntroducer
  337. // _external_: Name or QualifiedDeclaration
  338. // InterfaceDefinitionStart
  339. // _external_: declarations
  340. // InterfaceDefinition
  341. //
  342. // The above is the structure for a definition; for a declaration,
  343. // InterfaceDefinitionStart and later nodes are removed and replaced by
  344. // InterfaceDeclaration.
  345. CARBON_PARSE_NODE_KIND_CHILD_COUNT(InterfaceIntroducer, 0)
  346. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceDefinitionStart, InterfaceIntroducer)
  347. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceDefinition, InterfaceDefinitionStart)
  348. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceDeclaration, InterfaceIntroducer)
  349. // `constraint`:
  350. // NamedConstraintIntroducer
  351. // _external_: Name or QualifiedDeclaration
  352. // NamedConstraintDefinitionStart
  353. // _external_: declarations
  354. // NamedConstraintDefinition
  355. //
  356. // The above is the structure for a definition; for a declaration,
  357. // NamedConstraintDefinitionStart and later nodes are removed and replaced by
  358. // NamedConstraintDeclaration.
  359. CARBON_PARSE_NODE_KIND_CHILD_COUNT(NamedConstraintIntroducer, 0)
  360. CARBON_PARSE_NODE_KIND_BRACKET(NamedConstraintDefinitionStart,
  361. NamedConstraintIntroducer)
  362. CARBON_PARSE_NODE_KIND_BRACKET(NamedConstraintDefinition,
  363. NamedConstraintDefinitionStart)
  364. CARBON_PARSE_NODE_KIND_BRACKET(NamedConstraintDeclaration,
  365. NamedConstraintIntroducer)
  366. // The `self` value and `Self` type identifier keywords. Typically of the form
  367. // `self: Self`:
  368. // SelfValueName
  369. // SelfTypeNameExpression
  370. // PatternBinding
  371. CARBON_PARSE_NODE_KIND_CHILD_COUNT(SelfValueName, 0)
  372. CARBON_PARSE_NODE_KIND_CHILD_COUNT(SelfTypeNameExpression, 0)
  373. #undef CARBON_PARSE_NODE_KIND
  374. #undef CARBON_PARSE_NODE_KIND_BRACKET
  375. #undef CARBON_PARSE_NODE_KIND_CHILD_COUNT