parse_node_kind.def 14 KB

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