parse_node_kind.def 13 KB

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