parse_node_kind.def 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. // Note that this is an X-macro header.
  6. //
  7. // It does not use `#include` guards, and instead is designed to be `#include`ed
  8. // after the x-macro is defined in order for its inclusion to expand to the
  9. // desired output.
  10. //
  11. // x-macros come in three forms:
  12. // CARBON_PARSE_NODE_KIND(Name)
  13. // Used as a fallback if other macros are missing.
  14. // CARBON_PARSE_NODE_KIND_BRACKET(Name, BracketName)
  15. // Defines a bracketed node kind. BracketName should refer to the node kind
  16. // 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. // Macro definitions will be removed at the end of this file to clean up.
  22. #if !(defined(CARBON_PARSE_NODE_KIND) || \
  23. (defined(CARBON_PARSE_NODE_KIND_BRACKET) && \
  24. defined(CARBON_PARSE_NODE_KIND_CHILD_COUNT)))
  25. #error "Must define CARBON_PARSE_NODE_KIND family x-macros to use this file."
  26. #endif
  27. // The BRACKET and CHILD_COUNT macros will use CARBON_PARSE_NODE_KIND by default
  28. // when undefined.
  29. #ifndef CARBON_PARSE_NODE_KIND_BRACKET
  30. #define CARBON_PARSE_NODE_KIND_BRACKET(Name, ...) CARBON_PARSE_NODE_KIND(Name)
  31. #endif
  32. #ifndef CARBON_PARSE_NODE_KIND_CHILD_COUNT
  33. #define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, ...) \
  34. CARBON_PARSE_NODE_KIND(Name)
  35. #endif
  36. // The end of the file.
  37. CARBON_PARSE_NODE_KIND_CHILD_COUNT(FileEnd, 0)
  38. // An empty declaration, such as `;`.
  39. CARBON_PARSE_NODE_KIND_CHILD_COUNT(EmptyDeclaration, 0)
  40. // A name.
  41. CARBON_PARSE_NODE_KIND_CHILD_COUNT(DeclaredName, 0)
  42. // `package`:
  43. // PackageIntroducer
  44. // _external_: DeclaredName
  45. // _external_: Literal
  46. // PackageLibrary
  47. // PackageApi or PackageImpl
  48. // PackageDirective
  49. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageIntroducer, 0)
  50. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageApi, 0)
  51. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageImpl, 0)
  52. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageLibrary, 1)
  53. CARBON_PARSE_NODE_KIND_BRACKET(PackageDirective, PackageIntroducer)
  54. // A code block:
  55. // CodeBlockStart
  56. // _external_: statements
  57. // CodeBlock
  58. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CodeBlockStart, 0)
  59. CARBON_PARSE_NODE_KIND_BRACKET(CodeBlock, CodeBlockStart)
  60. // `fn`:
  61. // FunctionIntroducer
  62. // DeclaredName
  63. // _external_: ParameterList
  64. // _external_: type expression
  65. // ReturnType
  66. // FunctionDefinitionStart
  67. // _external_: statements
  68. // FunctionDefinition
  69. //
  70. // The above is a definition; for a declaration, FunctionDeclaration will end it
  71. // where FunctionDefinitionStart is for a definition.
  72. CARBON_PARSE_NODE_KIND_CHILD_COUNT(FunctionIntroducer, 0)
  73. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnType, 1)
  74. CARBON_PARSE_NODE_KIND_BRACKET(FunctionDefinitionStart, FunctionIntroducer)
  75. CARBON_PARSE_NODE_KIND_BRACKET(FunctionDefinition, FunctionDefinitionStart)
  76. CARBON_PARSE_NODE_KIND_BRACKET(FunctionDeclaration, FunctionIntroducer)
  77. // A parameter list:
  78. // ParamertListStart
  79. // _external_: expression
  80. // ParameterListComma
  81. // ParameterList
  82. //
  83. // Expressions and ParameterListComma may repeat with ParameterListComma as a
  84. // separator.
  85. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ParameterListStart, 0)
  86. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ParameterListComma, 0)
  87. CARBON_PARSE_NODE_KIND_BRACKET(ParameterList, ParameterListStart)
  88. // A pattern binding, such as `name: Type`:
  89. // DeclaredName
  90. // _external_: type expression
  91. // PatternBinding
  92. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PatternBinding, 2)
  93. // `var`:
  94. // VariableIntroducer
  95. // _external_: PatternBinding
  96. // optional VariableInitializer
  97. // optional _external_: expression
  98. // VariableDeclaration
  99. //
  100. // The VariableInitializer and following expression are paired: either both will
  101. // be present, or neither will.
  102. CARBON_PARSE_NODE_KIND_CHILD_COUNT(VariableIntroducer, 0)
  103. CARBON_PARSE_NODE_KIND_CHILD_COUNT(VariableInitializer, 0)
  104. CARBON_PARSE_NODE_KIND_BRACKET(VariableDeclaration, VariableIntroducer)
  105. // An expression statement:
  106. // _external_: expression
  107. // ExpressionStatement
  108. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ExpressionStatement, 1)
  109. // `break`:
  110. // BreakStatementStart
  111. // BreakStatement
  112. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BreakStatementStart, 0)
  113. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BreakStatement, 1)
  114. // `continue`:
  115. // ContinueStatementStart
  116. // ContinueStatement
  117. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ContinueStatementStart, 0)
  118. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ContinueStatement, 1)
  119. // `return`:
  120. // ReturnStatementStart
  121. // _external_: expression
  122. // ReturnStatement
  123. //
  124. // The child expression is optional.
  125. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnStatementStart, 0)
  126. CARBON_PARSE_NODE_KIND_BRACKET(ReturnStatement, ReturnStatementStart)
  127. // `for`:
  128. // ForHeaderStart
  129. // VariableIntroducer
  130. // _external_: PatternBinding
  131. // _external_: type expression
  132. // ForIn
  133. // _external_: expression
  134. // ForHeader
  135. // _external_: CodeBlock
  136. // ForStatement
  137. //
  138. // Versus a normal `var`, ForIn replaces VariableDeclaration.
  139. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ForHeaderStart, 0)
  140. CARBON_PARSE_NODE_KIND_BRACKET(ForIn, VariableIntroducer)
  141. CARBON_PARSE_NODE_KIND_BRACKET(ForHeader, ForHeaderStart)
  142. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ForStatement, 2)
  143. // `if` + `else`:
  144. // IfConditionStart
  145. // _external_: expression
  146. // IfCondition
  147. // _external_: CodeBlock
  148. // IfStatementElse
  149. // _external_: CodeBlock or IfStatement
  150. // IfStatement
  151. //
  152. // IfStatementElse and the following node are optional based on `else` presence.
  153. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfConditionStart, 0)
  154. CARBON_PARSE_NODE_KIND_BRACKET(IfCondition, IfConditionStart)
  155. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfStatementElse, 0)
  156. CARBON_PARSE_NODE_KIND_BRACKET(IfStatement, IfCondition)
  157. // `while`:
  158. // WhileConditionStart
  159. // _external_: expression
  160. // WhileCondition
  161. // _external_: CodeBlock
  162. // WhileStatement
  163. CARBON_PARSE_NODE_KIND_CHILD_COUNT(WhileConditionStart, 0)
  164. CARBON_PARSE_NODE_KIND_BRACKET(WhileCondition, WhileConditionStart)
  165. CARBON_PARSE_NODE_KIND_CHILD_COUNT(WhileStatement, 2)
  166. // Parenthesized expressions:
  167. // ParenExpressionOrTupleLiteralStart
  168. // _external_: expression
  169. // ParenExpression
  170. // Tuples:
  171. // ParenExpressionOrTupleLiteralStart
  172. // _external_: expression
  173. // TupleLiteralComma
  174. // TupleLiteral
  175. //
  176. // Expressions and TupleLiteralComma may repeat with TupleLiteralComma as a
  177. // separator.
  178. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ParenExpressionOrTupleLiteralStart, 0)
  179. CARBON_PARSE_NODE_KIND_BRACKET(ParenExpression,
  180. ParenExpressionOrTupleLiteralStart)
  181. CARBON_PARSE_NODE_KIND_CHILD_COUNT(TupleLiteralComma, 0)
  182. CARBON_PARSE_NODE_KIND_BRACKET(TupleLiteral, ParenExpressionOrTupleLiteralStart)
  183. // Call expressions, such as `a()`:
  184. // _external_: expression
  185. // CallExpressionStart
  186. // _external_: expression
  187. // CallExpressionComma
  188. // CallExpression
  189. //
  190. // Expressions and CallExpressionComma may repeat with CallExpressionComma as a
  191. // separator.
  192. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CallExpressionStart, 1)
  193. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CallExpressionComma, 0)
  194. CARBON_PARSE_NODE_KIND_BRACKET(CallExpression, CallExpressionStart)
  195. // A designator expression, such as `a.b`:
  196. // _external_: DesignatedName or lhs expression
  197. // _external_: DesignatedName
  198. // DesignatorExpression
  199. CARBON_PARSE_NODE_KIND_CHILD_COUNT(DesignatedName, 0)
  200. CARBON_PARSE_NODE_KIND_CHILD_COUNT(DesignatorExpression, 2)
  201. // A literal.
  202. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Literal, 0)
  203. // A reference to an identifier.
  204. CARBON_PARSE_NODE_KIND_CHILD_COUNT(NameReference, 0)
  205. // A prefix operator:
  206. // _external_: expression
  207. // PrefixOperator
  208. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PrefixOperator, 1)
  209. // An infix operator:
  210. // _external_: lhs expression
  211. // _external_: rhs expression
  212. // InfixOperator
  213. CARBON_PARSE_NODE_KIND_CHILD_COUNT(InfixOperator, 2)
  214. // A postfix operator:
  215. // _external_: expression
  216. // PostfixOperator
  217. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PostfixOperator, 1)
  218. // Struct literals, such as `{.a = 0}`:
  219. // StructLiteralOrStructTypeLiteralStart
  220. // _external_: DesignatedName
  221. // StructFieldDesignator
  222. // _external_: expression
  223. // StructFieldValue
  224. // StructComma
  225. // StructLiteral
  226. //
  227. //
  228. // Struct type literals, such as `{.a: i32}`:
  229. // _external_: DesignatedName
  230. // StructFieldDesignator
  231. // _external_: type expression
  232. // StructFieldType
  233. // StructComma
  234. // StructTypeLiteral
  235. //
  236. // Elements (StructFieldValue and StructFieldType, respectively) and StructComma
  237. // may repeat with StructComma as a separator.
  238. //
  239. // When a valid StructFieldType or StructFieldValue cannot be formed, elements
  240. // may be replaced by StructFieldUnknown, which may have a preceding sibling
  241. // StructFieldDesignator if one was successfully parsed.
  242. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructLiteralOrStructTypeLiteralStart, 0)
  243. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldDesignator, 1)
  244. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldValue, 2)
  245. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldType, 2)
  246. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldUnknown, 0)
  247. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructComma, 0)
  248. CARBON_PARSE_NODE_KIND_BRACKET(StructLiteral,
  249. StructLiteralOrStructTypeLiteralStart)
  250. CARBON_PARSE_NODE_KIND_BRACKET(StructTypeLiteral,
  251. StructLiteralOrStructTypeLiteralStart)
  252. // `interface`:
  253. // _external_: DeclaredName
  254. // InterfaceBodyStart
  255. // _external_: statements
  256. // InterfaceBodyEnd
  257. // InterfaceDefinition
  258. CARBON_PARSE_NODE_KIND_CHILD_COUNT(InterfaceBodyStart, 0)
  259. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceBody, InterfaceBodyStart)
  260. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceDefinition, DeclaredName)
  261. // A pattern binding for `self` deduced parameter:
  262. // `self`
  263. // _external_: type expression
  264. // SelfDeducedParameter
  265. CARBON_PARSE_NODE_KIND_CHILD_COUNT(SelfDeducedParameter, 0)
  266. // A pattern binding for `addr self` deduced parameter:
  267. // `addr self`
  268. // _external_: type expression
  269. // SelfDeducedParameter
  270. // CARBON_PARSE_NODE_KIND_CHILD_COUNT(SelfDeducedParameterAddress, 1)
  271. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Address, 1)
  272. CARBON_PARSE_NODE_KIND_CHILD_COUNT(SelfType, 0)
  273. // Deduced parameters, such as `[self: Self]`:
  274. // DeducedParameterListStart
  275. // _external_: Address or PatternBinding
  276. // DeducedParameterList
  277. CARBON_PARSE_NODE_KIND_CHILD_COUNT(DeducedParameterListStart, 0)
  278. CARBON_PARSE_NODE_KIND_BRACKET(DeducedParameterList, DeducedParameterListStart)
  279. #undef CARBON_PARSE_NODE_KIND
  280. #undef CARBON_PARSE_NODE_KIND_BRACKET
  281. #undef CARBON_PARSE_NODE_KIND_CHILD_COUNT