parse_node_kind.def 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. // _external_: expression
  97. // optional VariableInitializer
  98. // VariableDeclaration
  99. CARBON_PARSE_NODE_KIND_CHILD_COUNT(VariableIntroducer, 0)
  100. CARBON_PARSE_NODE_KIND_CHILD_COUNT(VariableInitializer, 1)
  101. CARBON_PARSE_NODE_KIND_BRACKET(VariableDeclaration, VariableIntroducer)
  102. // An expression statement:
  103. // _external_: expression
  104. // ExpressionStatement
  105. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ExpressionStatement, 1)
  106. // `break`:
  107. // BreakStatementStart
  108. // BreakStatement
  109. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BreakStatementStart, 0)
  110. CARBON_PARSE_NODE_KIND_CHILD_COUNT(BreakStatement, 1)
  111. // `continue`:
  112. // ContinueStatementStart
  113. // ContinueStatement
  114. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ContinueStatementStart, 0)
  115. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ContinueStatement, 1)
  116. // `return`:
  117. // ReturnStatementStart
  118. // _external_: expression
  119. // ReturnStatement
  120. //
  121. // The child expression is optional.
  122. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnStatementStart, 0)
  123. CARBON_PARSE_NODE_KIND_BRACKET(ReturnStatement, ReturnStatementStart)
  124. // `for`:
  125. // ForHeaderStart
  126. // VariableIntroducer
  127. // _external_: PatternBinding
  128. // _external_: type expression
  129. // ForIn
  130. // _external_: expression
  131. // ForHeader
  132. // _external_: CodeBlock
  133. // ForStatement
  134. //
  135. // Versus a normal `var`, ForIn replaces VariableDeclaration.
  136. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ForHeaderStart, 0)
  137. CARBON_PARSE_NODE_KIND_BRACKET(ForIn, VariableIntroducer)
  138. CARBON_PARSE_NODE_KIND_BRACKET(ForHeader, ForHeaderStart)
  139. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ForStatement, 2)
  140. // `if` + `else`:
  141. // IfConditionStart
  142. // _external_: expression
  143. // IfCondition
  144. // _external_: CodeBlock
  145. // IfStatementElse
  146. // _external_: CodeBlock or IfStatement
  147. // IfStatement
  148. //
  149. // IfStatementElse and the following node are optional based on `else` presence.
  150. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfConditionStart, 0)
  151. CARBON_PARSE_NODE_KIND_BRACKET(IfCondition, IfConditionStart)
  152. CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfStatementElse, 0)
  153. CARBON_PARSE_NODE_KIND_BRACKET(IfStatement, IfCondition)
  154. // `while`:
  155. // WhileConditionStart
  156. // _external_: expression
  157. // WhileCondition
  158. // _external_: CodeBlock
  159. // WhileStatement
  160. CARBON_PARSE_NODE_KIND_CHILD_COUNT(WhileConditionStart, 0)
  161. CARBON_PARSE_NODE_KIND_BRACKET(WhileCondition, WhileConditionStart)
  162. CARBON_PARSE_NODE_KIND_CHILD_COUNT(WhileStatement, 2)
  163. // Parenthesized expressions:
  164. // ParenExpressionOrTupleLiteralStart
  165. // _external_: expression
  166. // ParenExpression
  167. // Tuples:
  168. // ParenExpressionOrTupleLiteralStart
  169. // _external_: expression
  170. // TupleLiteralComma
  171. // TupleLiteral
  172. //
  173. // Expressions and TupleLiteralComma may repeat with TupleLiteralComma as a
  174. // separator.
  175. CARBON_PARSE_NODE_KIND_CHILD_COUNT(ParenExpressionOrTupleLiteralStart, 0)
  176. CARBON_PARSE_NODE_KIND_BRACKET(ParenExpression,
  177. ParenExpressionOrTupleLiteralStart)
  178. CARBON_PARSE_NODE_KIND_CHILD_COUNT(TupleLiteralComma, 0)
  179. CARBON_PARSE_NODE_KIND_BRACKET(TupleLiteral, ParenExpressionOrTupleLiteralStart)
  180. // Call expressions, such as `a()`:
  181. // _external_: expression
  182. // CallExpressionStart
  183. // _external_: expression
  184. // CallExpressionComma
  185. // CallExpression
  186. //
  187. // Expressions and CallExpressionComma may repeat with CallExpressionComma as a
  188. // separator.
  189. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CallExpressionStart, 1)
  190. CARBON_PARSE_NODE_KIND_CHILD_COUNT(CallExpressionComma, 0)
  191. CARBON_PARSE_NODE_KIND_BRACKET(CallExpression, CallExpressionStart)
  192. // A designator expression, such as `a.b`:
  193. // _external_: DesignatedName or lhs expression
  194. // _external_: DesignatedName
  195. // DesignatorExpression
  196. CARBON_PARSE_NODE_KIND_CHILD_COUNT(DesignatedName, 0)
  197. CARBON_PARSE_NODE_KIND_CHILD_COUNT(DesignatorExpression, 2)
  198. // A literal.
  199. CARBON_PARSE_NODE_KIND_CHILD_COUNT(Literal, 0)
  200. // A reference to an identifier.
  201. CARBON_PARSE_NODE_KIND_CHILD_COUNT(NameReference, 0)
  202. // A prefix operator:
  203. // _external_: expression
  204. // PrefixOperator
  205. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PrefixOperator, 1)
  206. // An infix operator:
  207. // _external_: lhs expression
  208. // _external_: rhs expression
  209. // InfixOperator
  210. CARBON_PARSE_NODE_KIND_CHILD_COUNT(InfixOperator, 2)
  211. // A postfix operator:
  212. // _external_: expression
  213. // PostfixOperator
  214. CARBON_PARSE_NODE_KIND_CHILD_COUNT(PostfixOperator, 1)
  215. // Struct literals, such as `{.a = 0}`:
  216. // StructLiteralOrStructTypeLiteralStart
  217. // _external_: DesignatedName
  218. // StructFieldDesignator
  219. // _external_: expression
  220. // StructFieldValue
  221. // StructComma
  222. // StructLiteral
  223. //
  224. //
  225. // Struct type literals, such as `{.a: i32}`:
  226. // _external_: DesignatedName
  227. // StructFieldDesignator
  228. // _external_: type expression
  229. // StructFieldType
  230. // StructComma
  231. // StructTypeLiteral
  232. //
  233. // Elements (StructFieldValue and StructFieldType, respectively) and StructComma
  234. // may repeat with StructComma as a separator.
  235. //
  236. // When a valid StructFieldType or StructFieldValue cannot be formed, elements
  237. // may be replaced by StructFieldUnknown, which may have a preceding sibling
  238. // StructFieldDesignator if one was successfully parsed.
  239. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructLiteralOrStructTypeLiteralStart, 0)
  240. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldDesignator, 1)
  241. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldValue, 2)
  242. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldType, 2)
  243. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldUnknown, 0)
  244. CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructComma, 0)
  245. CARBON_PARSE_NODE_KIND_BRACKET(StructLiteral,
  246. StructLiteralOrStructTypeLiteralStart)
  247. CARBON_PARSE_NODE_KIND_BRACKET(StructTypeLiteral,
  248. StructLiteralOrStructTypeLiteralStart)
  249. // `interface`:
  250. // _external_: DeclaredName
  251. // InterfaceBodyStart
  252. // _external_: statements
  253. // InterfaceBodyEnd
  254. // InterfaceDefinition
  255. CARBON_PARSE_NODE_KIND_CHILD_COUNT(InterfaceBodyStart, 0)
  256. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceBody, InterfaceBodyStart)
  257. CARBON_PARSE_NODE_KIND_BRACKET(InterfaceDefinition, DeclaredName)
  258. #undef CARBON_PARSE_NODE_KIND
  259. #undef CARBON_PARSE_NODE_KIND_BRACKET
  260. #undef CARBON_PARSE_NODE_KIND_CHILD_COUNT