parse_node_kind.def 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. // A code block:
  63. // CodeBlockStart
  64. // _external_: statements
  65. // CodeBlock
  66. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CodeBlockStart, 0)
  67. CARBON_PARSE_NODE_KIND_BRACKET(CodeBlock, CodeBlockStart)
  68. // `fn`:
  69. // FunctionIntroducer
  70. // Name
  71. // _external_: ParameterList
  72. // _external_: type expression
  73. // ReturnType
  74. // FunctionDefinitionStart
  75. // _external_: statements
  76. // FunctionDefinition
  77. //
  78. // The above is the structure for a definition; for a declaration,
  79. // FunctionDefinitionStart and later nodes are removed and replaced by
  80. // FunctionDeclaration.
  81. CARBON_PARSE_NODE_KIND_CHILD_COUNT(FunctionIntroducer, 0)
  82. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnType, 1)
  83. CARBON_PARSE_NODE_KIND_BRACKET(FunctionDefinitionStart, FunctionIntroducer)
  84. CARBON_PARSE_NODE_KIND_BRACKET(FunctionDefinition, FunctionDefinitionStart)
  85. CARBON_PARSE_NODE_KIND_BRACKET(FunctionDeclaration, FunctionIntroducer)
  86. // A parameter list, possibly deduced:
  87. // [Deduced]ParamertListStart
  88. // _external_: [Generic]PatternBinding
  89. // ParameterListComma
  90. // [Deduced]ParameterList
  91. //
  92. // Expressions and ParameterListComma may repeat with ParameterListComma as a
  93. // separator.
  94. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ParameterListStart, 0)
  95. CARBON_PARSE_NODE_KIND_CHILD_COUNT(DeducedParameterListStart, 0)
  96. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ParameterListComma, 0)
  97. CARBON_PARSE_NODE_KIND_BRACKET(ParameterList, ParameterListStart)
  98. CARBON_PARSE_NODE_KIND_BRACKET(DeducedParameterList, DeducedParameterListStart)
  99. // A pattern binding, such as `name: Type`:
  100. // Name
  101. // _external_: type expression
  102. // [Generic]PatternBinding
  103. //
  104. // Address and Template may be parents to [Generic]PatternBinding, in that
  105. // order.
  106. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PatternBinding, 2)
  107. CARBON_PARSE_NODE_KIND_CHILD_COUNT(GenericPatternBinding, 2)
  108. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Address, 1)
  109. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Template, 1)
  110. // `var`:
  111. // VariableIntroducer
  112. // _external_: PatternBinding
  113. // optional VariableInitializer
  114. // optional _external_: expression
  115. // VariableDeclaration
  116. //
  117. // The VariableInitializer and following expression are paired: either both will
  118. // be present, or neither will.
  119. CARBON_PARSE_NODE_KIND_CHILD_COUNT(VariableIntroducer, 0)
  120. CARBON_PARSE_NODE_KIND_CHILD_COUNT(VariableInitializer, 0)
  121. CARBON_PARSE_NODE_KIND_BRACKET(VariableDeclaration, VariableIntroducer)
  122. // An expression statement:
  123. // _external_: expression
  124. // ExpressionStatement
  125. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ExpressionStatement, 1)
  126. // `break`:
  127. // BreakStatementStart
  128. // BreakStatement
  129. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BreakStatementStart, 0)
  130. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BreakStatement, 1)
  131. // `continue`:
  132. // ContinueStatementStart
  133. // ContinueStatement
  134. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ContinueStatementStart, 0)
  135. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ContinueStatement, 1)
  136. // `return`:
  137. // ReturnStatementStart
  138. // _external_: expression
  139. // ReturnStatement
  140. //
  141. // The child expression is optional.
  142. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnStatementStart, 0)
  143. CARBON_PARSE_NODE_KIND_BRACKET(ReturnStatement, ReturnStatementStart)
  144. // `for`:
  145. // ForHeaderStart
  146. // VariableIntroducer
  147. // _external_: PatternBinding
  148. // _external_: type expression
  149. // ForIn
  150. // _external_: expression
  151. // ForHeader
  152. // _external_: CodeBlock
  153. // ForStatement
  154. //
  155. // Versus a normal `var`, ForIn replaces VariableDeclaration.
  156. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ForHeaderStart, 0)
  157. CARBON_PARSE_NODE_KIND_BRACKET(ForIn, VariableIntroducer)
  158. CARBON_PARSE_NODE_KIND_BRACKET(ForHeader, ForHeaderStart)
  159. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ForStatement, 2)
  160. // `if` statement + `else`:
  161. // IfConditionStart
  162. // _external_: expression
  163. // IfCondition
  164. // _external_: CodeBlock
  165. // IfStatementElse
  166. // _external_: CodeBlock or IfStatement
  167. // IfStatement
  168. //
  169. // IfStatementElse and the following node are optional based on `else` presence.
  170. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfConditionStart, 0)
  171. CARBON_PARSE_NODE_KIND_BRACKET(IfCondition, IfConditionStart)
  172. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfStatementElse, 0)
  173. CARBON_PARSE_NODE_KIND_BRACKET(IfStatement, IfCondition)
  174. // `while`:
  175. // WhileConditionStart
  176. // _external_: expression
  177. // WhileCondition
  178. // _external_: CodeBlock
  179. // WhileStatement
  180. CARBON_PARSE_NODE_KIND_CHILD_COUNT(WhileConditionStart, 0)
  181. CARBON_PARSE_NODE_KIND_BRACKET(WhileCondition, WhileConditionStart)
  182. CARBON_PARSE_NODE_KIND_CHILD_COUNT(WhileStatement, 2)
  183. // Parenthesized expressions:
  184. // ParenExpressionOrTupleLiteralStart
  185. // _external_: expression
  186. // ParenExpression
  187. // Tuples:
  188. // ParenExpressionOrTupleLiteralStart
  189. // _external_: expression
  190. // TupleLiteralComma
  191. // TupleLiteral
  192. //
  193. // Expressions and TupleLiteralComma may repeat with TupleLiteralComma as a
  194. // separator.
  195. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ParenExpressionOrTupleLiteralStart, 0)
  196. CARBON_PARSE_NODE_KIND_BRACKET(ParenExpression,
  197. ParenExpressionOrTupleLiteralStart)
  198. CARBON_PARSE_NODE_KIND_CHILD_COUNT(TupleLiteralComma, 0)
  199. CARBON_PARSE_NODE_KIND_BRACKET(TupleLiteral, ParenExpressionOrTupleLiteralStart)
  200. // Call expressions, such as `a()`:
  201. // _external_: expression
  202. // CallExpressionStart
  203. // _external_: expression
  204. // CallExpressionComma
  205. // CallExpression
  206. //
  207. // Expressions and CallExpressionComma may repeat with CallExpressionComma as a
  208. // separator.
  209. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CallExpressionStart, 1)
  210. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CallExpressionComma, 0)
  211. CARBON_PARSE_NODE_KIND_BRACKET(CallExpression, CallExpressionStart)
  212. // A designator expression, such as `a.b`:
  213. // _external_: lhs expression
  214. // _external_: Name
  215. // DesignatorExpression
  216. CARBON_PARSE_NODE_KIND_CHILD_COUNT(DesignatorExpression, 2)
  217. // A literal.
  218. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Literal, 0)
  219. // A prefix operator:
  220. // _external_: expression
  221. // PrefixOperator
  222. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PrefixOperator, 1)
  223. // An infix operator:
  224. // _external_: lhs expression
  225. // _external_: rhs expression
  226. // InfixOperator
  227. CARBON_PARSE_NODE_KIND_CHILD_COUNT(InfixOperator, 2)
  228. // The first operand of a short-circuiting infix operator:
  229. // _external_: expression
  230. // ShortCircuitOperand
  231. // _external_: expression
  232. // _external_: InfixOperator
  233. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ShortCircuitOperand, 1)
  234. // A postfix operator:
  235. // _external_: expression
  236. // PostfixOperator
  237. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PostfixOperator, 1)
  238. // `if` expression + `then` + `else`:
  239. // _external_: expression
  240. // IfExpressionIf
  241. // _external_: expression
  242. // IfExpressionThen
  243. // _external_: expression
  244. // IfExpressionElse
  245. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfExpressionIf, 1)
  246. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfExpressionThen, 1)
  247. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfExpressionElse, 3)
  248. // Struct literals, such as `{.a = 0}`:
  249. // StructLiteralOrStructTypeLiteralStart
  250. // _external_: Name
  251. // StructFieldDesignator
  252. // _external_: expression
  253. // StructFieldValue
  254. // StructComma
  255. // StructLiteral
  256. //
  257. // Struct type literals, such as `{.a: i32}`:
  258. // _external_: Name
  259. // StructFieldDesignator
  260. // _external_: type expression
  261. // StructFieldType
  262. // StructComma
  263. // StructTypeLiteral
  264. //
  265. // Elements (StructFieldValue and StructFieldType, respectively) and StructComma
  266. // may repeat with StructComma as a separator.
  267. //
  268. // When a valid StructFieldType or StructFieldValue cannot be formed, elements
  269. // may be replaced by StructFieldUnknown, which may have a preceding sibling
  270. // StructFieldDesignator if one was successfully parsed.
  271. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructLiteralOrStructTypeLiteralStart, 0)
  272. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldDesignator, 1)
  273. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldValue, 2)
  274. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldType, 2)
  275. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldUnknown, 0)
  276. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructComma, 0)
  277. CARBON_PARSE_NODE_KIND_BRACKET(StructLiteral,
  278. StructLiteralOrStructTypeLiteralStart)
  279. CARBON_PARSE_NODE_KIND_BRACKET(StructTypeLiteral,
  280. StructLiteralOrStructTypeLiteralStart)
  281. // `class`:
  282. // ClassIntroducer
  283. // Name
  284. // ClassDefinitionStart
  285. // _external_: declarations
  286. // ClassDefinition
  287. //
  288. // The above is the structure for a definition; for a declaration,
  289. // ClassDefinitionStart and later nodes are removed and replaced by
  290. // ClassDeclaration.
  291. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ClassIntroducer, 0)
  292. CARBON_PARSE_NODE_KIND_BRACKET(ClassDefinitionStart, ClassIntroducer)
  293. CARBON_PARSE_NODE_KIND_BRACKET(ClassDefinition, ClassDefinitionStart)
  294. CARBON_PARSE_NODE_KIND_BRACKET(ClassDeclaration, ClassIntroducer)
  295. // `interface`:
  296. // InterfaceIntroducer
  297. // Name
  298. // InterfaceDefinitionStart
  299. // _external_: declarations
  300. // InterfaceDefinition
  301. //
  302. // The above is the structure for a definition; for a declaration,
  303. // InterfaceDefinitionStart and later nodes are removed and replaced by
  304. // InterfaceDeclaration.
  305. CARBON_PARSE_NODE_KIND_CHILD_COUNT(InterfaceIntroducer, 0)
  306. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceDefinitionStart, InterfaceIntroducer)
  307. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceDefinition, InterfaceDefinitionStart)
  308. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceDeclaration, InterfaceIntroducer)
  309. // `constraint`:
  310. // NamedConstraintIntroducer
  311. // Name
  312. // NamedConstraintDefinitionStart
  313. // _external_: declarations
  314. // NamedConstraintDefinition
  315. //
  316. // The above is the structure for a definition; for a declaration,
  317. // NamedConstraintDefinitionStart and later nodes are removed and replaced by
  318. // NamedConstraintDeclaration.
  319. CARBON_PARSE_NODE_KIND_CHILD_COUNT(NamedConstraintIntroducer, 0)
  320. CARBON_PARSE_NODE_KIND_BRACKET(NamedConstraintDefinitionStart,
  321. NamedConstraintIntroducer)
  322. CARBON_PARSE_NODE_KIND_BRACKET(NamedConstraintDefinition,
  323. NamedConstraintDefinitionStart)
  324. CARBON_PARSE_NODE_KIND_BRACKET(NamedConstraintDeclaration,
  325. NamedConstraintIntroducer)
  326. // The `self` value and `Self` type identifier keywords. Typically of the form
  327. // `self: Self`:
  328. // SelfValueName
  329. // SelfTypeNameExpression
  330. // PatternBinding
  331. CARBON_PARSE_NODE_KIND_CHILD_COUNT(SelfValueName, 0)
  332. CARBON_PARSE_NODE_KIND_CHILD_COUNT(SelfTypeNameExpression, 0)
  333. #undef CARBON_PARSE_NODE_KIND
  334. #undef CARBON_PARSE_NODE_KIND_BRACKET
  335. #undef CARBON_PARSE_NODE_KIND_CHILD_COUNT