| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619 |
- // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
- // Exceptions. See /LICENSE for license information.
- // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- #ifndef CARBON_TOOLCHAIN_PARSE_TYPED_NODES_H_
- #define CARBON_TOOLCHAIN_PARSE_TYPED_NODES_H_
- #include <optional>
- #include "toolchain/lex/token_index.h"
- #include "toolchain/parse/node_ids.h"
- #include "toolchain/parse/node_kind.h"
- namespace Carbon::Parse {
- // Helpers for defining different kinds of parse nodes.
- // ----------------------------------------------------
- // A pair of a list item and its optional following comma.
- template <typename Element, typename Comma>
- struct ListItem {
- Element value;
- std::optional<Comma> comma;
- };
- // A list of items, parameterized by the kind of the elements and comma.
- template <typename Element, typename Comma>
- using CommaSeparatedList = llvm::SmallVector<ListItem<Element, Comma>>;
- // This class provides a shorthand for defining parse node kinds for leaf nodes.
- template <const NodeKind& KindT, typename TokenKind,
- NodeCategory::RawEnumType Category = NodeCategory::None>
- struct LeafNode {
- static constexpr auto Kind =
- KindT.Define({.category = Category, .child_count = 0});
- TokenKind token;
- };
- // ----------------------------------------------------------------------------
- // Each node kind (in node_kind.def) should have a corresponding type defined
- // here which describes the expected child structure of that parse node.
- //
- // Each of these types should start with a `static constexpr Kind` member
- // initialized by calling `Define` on the corresponding `NodeKind`, and passing
- // in the `NodeCategory` of that kind. This will both associate the category
- // with the node kind and create the necessary kind object for the typed node.
- //
- // This should be followed by field declarations that describe the child nodes,
- // in order, that occur in the parse tree. The `Extract...` functions on the
- // parse tree use struct reflection on these fields to guide the extraction of
- // the child nodes from the tree into an object of this type with these fields
- // for convenient access.
- //
- // The types of these fields are special and describe the specific child node
- // structure of the parse node. Many of these types are defined in `node_ids.h`.
- //
- // Valid primitive types here are:
- // - `NodeId` to match any single child node
- // - `FooId` to require that child to have kind `NodeKind::Foo`
- // - `AnyCatId` to require that child to have a kind in category `Cat`
- // - `NodeIdOneOf<A, B>` to require the child to have kind `NodeKind::A` or
- // `NodeKind::B`
- // - `NodeIdNot<A>` to match any single child whose kind is not `NodeKind::A`
- //
- // There a few, restricted composite field types allowed that compose types in
- // various ways, where all of the `T`s and `U`s below are themselves valid field
- // types:
- // - `llvm::SmallVector<T>` to match any number of children matching `T`
- // - `std::optional<T>` to match 0 or 1 children matching `T`
- // - `std::tuple<T...>` to match children matching `T...`
- // - Any provided `Aggregate` type that is a simple aggregate type such as
- // `struct Aggregate { T x; U y; }`,
- // to match children with types `T` and `U`.
- //
- // In addition to the fields describing the child nodes, each parse node should
- // also have exactly one field that describes the token corresponding to the
- // parse node itself. This field should have the name `token`. The type of the
- // field should be `Lex::*TokenIndex`, describing the kind of the token, such as
- // `Lex::SemiTokenIndex` for a `;` token. If the parse node can correspond to
- // any kind of token, `Lex::TokenIndex` can be used instead, but should only be
- // used when the node kind is either not used in a finished tree, such as
- // `Placeholder`, or is always invalid, such as `InvalidParse`. The location of
- // the field relative to the child nodes indicates the location within the
- // corresponding grammar production where the token appears.
- // ----------------------------------------------------------------------------
- // Error nodes
- // -----------
- // An invalid parse. Used to balance the parse tree. This type is here only to
- // ensure we have a type for each parse node kind. This node kind always has an
- // error, so can never be extracted.
- using InvalidParse = LeafNode<NodeKind::InvalidParse, Lex::TokenIndex,
- NodeCategory::Decl | NodeCategory::Expr>;
- // An invalid subtree. Always has an error so can never be extracted.
- using InvalidParseStart =
- LeafNode<NodeKind::InvalidParseStart, Lex::TokenIndex>;
- struct InvalidParseSubtree {
- static constexpr auto Kind = NodeKind::InvalidParseSubtree.Define(
- {.category = NodeCategory::Decl,
- .bracketed_by = InvalidParseStart::Kind});
- InvalidParseStartId start;
- llvm::SmallVector<NodeIdNot<InvalidParseStart>> extra;
- Lex::TokenIndex token;
- };
- // A placeholder node to be replaced; it will never exist in a valid parse tree.
- // Its token kind is not enforced even when valid.
- using Placeholder = LeafNode<NodeKind::Placeholder, Lex::TokenIndex>;
- // File nodes
- // ----------
- // The start of the file.
- using FileStart = LeafNode<NodeKind::FileStart, Lex::FileStartTokenIndex>;
- // The end of the file.
- using FileEnd = LeafNode<NodeKind::FileEnd, Lex::FileEndTokenIndex>;
- // General-purpose nodes
- // ---------------------
- // An empty declaration, such as `;`.
- using EmptyDecl =
- LeafNode<NodeKind::EmptyDecl, Lex::SemiTokenIndex, NodeCategory::Decl>;
- // A name in a non-expression context, such as a declaration, that is known
- // to be followed by parameters.
- using IdentifierNameBeforeParams =
- LeafNode<NodeKind::IdentifierNameBeforeParams, Lex::IdentifierTokenIndex,
- NodeCategory::MemberName | NodeCategory::NonExprName>;
- // A name in a non-expression context, such as a declaration, that is known
- // to not be followed by parameters.
- using IdentifierNameNotBeforeParams =
- LeafNode<NodeKind::IdentifierNameNotBeforeParams, Lex::IdentifierTokenIndex,
- NodeCategory::MemberName | NodeCategory::NonExprName>;
- // A name in an expression context.
- using IdentifierNameExpr =
- LeafNode<NodeKind::IdentifierNameExpr, Lex::IdentifierTokenIndex,
- NodeCategory::Expr>;
- // The `self` value and `Self` type identifier keywords. Typically of the form
- // `self: Self`.
- using SelfValueName =
- LeafNode<NodeKind::SelfValueName, Lex::SelfValueIdentifierTokenIndex>;
- using SelfValueNameExpr =
- LeafNode<NodeKind::SelfValueNameExpr, Lex::SelfValueIdentifierTokenIndex,
- NodeCategory::Expr>;
- using SelfTypeNameExpr =
- LeafNode<NodeKind::SelfTypeNameExpr, Lex::SelfTypeIdentifierTokenIndex,
- NodeCategory::Expr>;
- // The `base` value keyword, introduced by `base: B`. Typically referenced in
- // an expression, as in `x.base` or `{.base = ...}`, but can also be used as a
- // declared name, as in `{.base: partial B}`.
- using BaseName =
- LeafNode<NodeKind::BaseName, Lex::BaseTokenIndex, NodeCategory::MemberName>;
- // The `_` token, when used in the name position of a binding pattern.
- using UnderscoreName =
- LeafNode<NodeKind::UnderscoreName, Lex::UnderscoreTokenIndex,
- NodeCategory::NonExprName>;
- // A name qualifier with parameters, such as `A(T:! type).` or `A[T:! type](N:!
- // T).`.
- struct IdentifierNameQualifierWithParams {
- static constexpr auto Kind =
- NodeKind::IdentifierNameQualifierWithParams.Define(
- {.bracketed_by = IdentifierNameBeforeParams::Kind});
- IdentifierNameBeforeParamsId name;
- std::optional<ImplicitParamListId> implicit_params;
- std::optional<ExplicitParamListId> params;
- Lex::PeriodTokenIndex token;
- };
- // A name qualifier without parameters, such as `A.`.
- struct IdentifierNameQualifierWithoutParams {
- static constexpr auto Kind =
- NodeKind::IdentifierNameQualifierWithoutParams.Define(
- {.bracketed_by = IdentifierNameNotBeforeParams::Kind});
- IdentifierNameNotBeforeParamsId name;
- Lex::PeriodTokenIndex token;
- };
- // A complete name in a declaration: `A.C(T:! type).F(n: i32)`.
- // Note that this includes the parameters of the entity itself.
- struct DeclName {
- llvm::SmallVector<NodeIdOneOf<IdentifierNameQualifierWithParams,
- IdentifierNameQualifierWithoutParams>>
- qualifiers;
- AnyNonExprNameId name;
- std::optional<ImplicitParamListId> implicit_params;
- std::optional<ExplicitParamListId> params;
- };
- // Library, package, import, export
- // --------------------------------
- // Various keywords in an expression.
- using CoreNameExpr =
- LeafNode<NodeKind::CoreNameExpr, Lex::CoreTokenIndex, NodeCategory::Expr>;
- using CppNameExpr =
- LeafNode<NodeKind::CppNameExpr, Lex::CppTokenIndex, NodeCategory::Expr>;
- using PackageExpr =
- LeafNode<NodeKind::PackageExpr, Lex::PackageTokenIndex, NodeCategory::Expr>;
- // The name of a package or library for `package`, `import`, and `library`.
- using IdentifierPackageName =
- LeafNode<NodeKind::IdentifierPackageName, Lex::IdentifierTokenIndex,
- NodeCategory::PackageName>;
- using CorePackageName = LeafNode<NodeKind::CorePackageName, Lex::CoreTokenIndex,
- NodeCategory::PackageName>;
- using CppPackageName = LeafNode<NodeKind::CppPackageName, Lex::CppTokenIndex,
- NodeCategory::PackageName>;
- using LibraryName =
- LeafNode<NodeKind::LibraryName, Lex::StringLiteralTokenIndex>;
- using DefaultLibrary =
- LeafNode<NodeKind::DefaultLibrary, Lex::DefaultTokenIndex>;
- using PackageIntroducer =
- LeafNode<NodeKind::PackageIntroducer, Lex::PackageTokenIndex>;
- // `library` in `package` or `import`.
- struct LibrarySpecifier {
- static constexpr auto Kind =
- NodeKind::LibrarySpecifier.Define({.child_count = 1});
- Lex::LibraryTokenIndex token;
- NodeIdOneOf<LibraryName, DefaultLibrary> name;
- };
- using InlineImportBody =
- LeafNode<NodeKind::InlineImportBody, Lex::StringLiteralTokenIndex>;
- // `inline` in `import`.
- struct InlineImportSpecifier {
- static constexpr auto Kind =
- NodeKind::InlineImportSpecifier.Define({.child_count = 1});
- Lex::InlineTokenIndex token;
- InlineImportBodyId body;
- };
- // First line of the file, such as:
- // `impl package MyPackage library "MyLibrary";`
- struct PackageDecl {
- static constexpr auto Kind =
- NodeKind::PackageDecl.Define({.category = NodeCategory::Decl,
- .bracketed_by = PackageIntroducer::Kind});
- PackageIntroducerId introducer;
- llvm::SmallVector<AnyModifierId> modifiers;
- AnyPackageNameId name;
- std::optional<LibrarySpecifierId> library;
- Lex::SemiTokenIndex token;
- };
- // `import [TheirPackage] [library "TheirLibrary" | inline "code"];`
- using ImportIntroducer =
- LeafNode<NodeKind::ImportIntroducer, Lex::ImportTokenIndex>;
- struct ImportDecl {
- static constexpr auto Kind = NodeKind::ImportDecl.Define(
- {.category = NodeCategory::Decl, .bracketed_by = ImportIntroducer::Kind});
- ImportIntroducerId introducer;
- llvm::SmallVector<AnyModifierId> modifiers;
- std::optional<AnyPackageNameId> name;
- std::optional<LibrarySpecifierId> library;
- std::optional<InlineImportSpecifierId> inline_specifier;
- Lex::SemiTokenIndex token;
- };
- // `library` as declaration.
- using LibraryIntroducer =
- LeafNode<NodeKind::LibraryIntroducer, Lex::LibraryTokenIndex>;
- struct LibraryDecl {
- static constexpr auto Kind =
- NodeKind::LibraryDecl.Define({.category = NodeCategory::Decl,
- .bracketed_by = LibraryIntroducer::Kind});
- LibraryIntroducerId introducer;
- llvm::SmallVector<AnyModifierId> modifiers;
- NodeIdOneOf<LibraryName, DefaultLibrary> library_name;
- Lex::SemiTokenIndex token;
- };
- // `export` as a declaration.
- using ExportIntroducer =
- LeafNode<NodeKind::ExportIntroducer, Lex::ExportTokenIndex>;
- struct ExportDecl {
- static constexpr auto Kind = NodeKind::ExportDecl.Define(
- {.category = NodeCategory::Decl, .bracketed_by = ExportIntroducer::Kind});
- ExportIntroducerId introducer;
- llvm::SmallVector<AnyModifierId> modifiers;
- DeclName name;
- Lex::SemiTokenIndex token;
- };
- // Namespace nodes
- // ---------------
- using NamespaceStart =
- LeafNode<NodeKind::NamespaceStart, Lex::NamespaceTokenIndex>;
- // A namespace: `namespace N;`.
- struct Namespace {
- static constexpr auto Kind = NodeKind::Namespace.Define(
- {.category = NodeCategory::Decl, .bracketed_by = NamespaceStart::Kind});
- NamespaceStartId introducer;
- llvm::SmallVector<AnyModifierId> modifiers;
- DeclName name;
- Lex::SemiTokenIndex token;
- };
- // Pattern nodes
- // -------------
- // A ref binding name: `ref name`.
- struct RefBindingName {
- static constexpr auto Kind =
- NodeKind::RefBindingName.Define({.child_count = 1});
- Lex::RefTokenIndex token;
- AnyRuntimeBindingPatternName name;
- };
- // A binding pattern, such as `name: Type`, that isn't inside a `var` pattern.
- struct LetBindingPattern {
- static constexpr auto Kind = NodeKind::LetBindingPattern.Define(
- {.category = NodeCategory::Pattern, .child_count = 2});
- // TODO: is there some way to reuse AnyRuntimeBindingPatternName here?
- NodeIdOneOf<IdentifierNameNotBeforeParams, SelfValueName, UnderscoreName,
- RefBindingName>
- name;
- Lex::ColonTokenIndex token;
- AnyExprId type;
- };
- // A binding pattern, such as `name: Type`, that is inside a `var` pattern.
- struct VarBindingPattern {
- static constexpr auto Kind = NodeKind::VarBindingPattern.Define(
- {.category = NodeCategory::Pattern, .child_count = 2});
- AnyRuntimeBindingPatternName name;
- Lex::ColonTokenIndex token;
- AnyExprId type;
- };
- // A template binding name: `template T`.
- struct TemplateBindingName {
- static constexpr auto Kind =
- NodeKind::TemplateBindingName.Define({.child_count = 1});
- Lex::TemplateTokenIndex token;
- AnyRuntimeBindingPatternName name;
- };
- struct CompileTimeBindingPatternStart {
- static constexpr auto Kind =
- NodeKind::CompileTimeBindingPatternStart.Define({.child_count = 1});
- // TODO: is there some way to reuse AnyRuntimeBindingPatternName here?
- NodeIdOneOf<IdentifierNameNotBeforeParams, SelfValueName, UnderscoreName,
- TemplateBindingName>
- name;
- // This is a virtual token. The `:!` token is owned by the
- // CompileTimeBindingPattern node.
- Lex::ColonExclaimTokenIndex token;
- };
- // `name:! Type`
- struct CompileTimeBindingPattern {
- static constexpr auto Kind = NodeKind::CompileTimeBindingPattern.Define(
- {.category = NodeCategory::Pattern, .child_count = 2});
- CompileTimeBindingPatternStartId introducer;
- Lex::ColonExclaimTokenIndex token;
- AnyExprId type;
- };
- // An address-of binding: `addr self: Self*`.
- struct Addr {
- static constexpr auto Kind = NodeKind::Addr.Define(
- {.category = NodeCategory::Pattern, .child_count = 1});
- Lex::AddrTokenIndex token;
- AnyPatternId inner;
- };
- using TuplePatternStart =
- LeafNode<NodeKind::TuplePatternStart, Lex::OpenParenTokenIndex>;
- using PatternListComma =
- LeafNode<NodeKind::PatternListComma, Lex::CommaTokenIndex>;
- // A tuple pattern that isn't an explicit parameter list: `(a: i32, b: i32)`.
- struct TuplePattern {
- static constexpr auto Kind =
- NodeKind::TuplePattern.Define({.category = NodeCategory::Pattern,
- .bracketed_by = TuplePatternStart::Kind});
- TuplePatternStartId left_paren;
- CommaSeparatedList<AnyPatternId, PatternListCommaId> params;
- Lex::CloseParenTokenIndex token;
- };
- using ExplicitParamListStart =
- LeafNode<NodeKind::ExplicitParamListStart, Lex::OpenParenTokenIndex>;
- // An explicit parameter list: `(a: i32, b: i32)`.
- struct ExplicitParamList {
- static constexpr auto Kind = NodeKind::ExplicitParamList.Define(
- {.bracketed_by = ExplicitParamListStart::Kind});
- ExplicitParamListStartId left_paren;
- CommaSeparatedList<AnyPatternId, PatternListCommaId> params;
- Lex::CloseParenTokenIndex token;
- };
- using ImplicitParamListStart = LeafNode<NodeKind::ImplicitParamListStart,
- Lex::OpenSquareBracketTokenIndex>;
- // An implicit parameter list: `[T:! type, self: Self]`.
- struct ImplicitParamList {
- static constexpr auto Kind = NodeKind::ImplicitParamList.Define(
- {.bracketed_by = ImplicitParamListStart::Kind});
- ImplicitParamListStartId left_square;
- CommaSeparatedList<AnyPatternId, PatternListCommaId> params;
- Lex::CloseSquareBracketTokenIndex token;
- };
- // Function nodes
- // --------------
- using FunctionIntroducer =
- LeafNode<NodeKind::FunctionIntroducer, Lex::FnTokenIndex>;
- // A return type: `-> i32`.
- struct ReturnType {
- static constexpr auto Kind = NodeKind::ReturnType.Define({.child_count = 1});
- Lex::MinusGreaterTokenIndex token;
- AnyExprId type;
- };
- // A function signature: `fn F() -> i32`.
- template <const NodeKind& KindT, typename TokenKind,
- NodeCategory::RawEnumType Category>
- struct FunctionSignature {
- static constexpr auto Kind = KindT.Define(
- {.category = Category, .bracketed_by = FunctionIntroducer::Kind});
- FunctionIntroducerId introducer;
- llvm::SmallVector<AnyModifierId> modifiers;
- DeclName name;
- std::optional<ReturnTypeId> return_type;
- TokenKind token;
- };
- using FunctionDecl = FunctionSignature<NodeKind::FunctionDecl,
- Lex::SemiTokenIndex, NodeCategory::Decl>;
- using FunctionDefinitionStart =
- FunctionSignature<NodeKind::FunctionDefinitionStart,
- Lex::OpenCurlyBraceTokenIndex, NodeCategory::None>;
- // A function definition: `fn F() -> i32 { ... }`.
- struct FunctionDefinition {
- static constexpr auto Kind = NodeKind::FunctionDefinition.Define(
- {.category = NodeCategory::Decl,
- .bracketed_by = FunctionDefinitionStart::Kind});
- FunctionDefinitionStartId signature;
- llvm::SmallVector<AnyStatementId> body;
- Lex::CloseCurlyBraceTokenIndex token;
- };
- using BuiltinFunctionDefinitionStart =
- FunctionSignature<NodeKind::BuiltinFunctionDefinitionStart,
- Lex::EqualTokenIndex, NodeCategory::None>;
- using BuiltinName =
- LeafNode<NodeKind::BuiltinName, Lex::StringLiteralTokenIndex>;
- // A builtin function definition: `fn F() -> i32 = "builtin name";`
- struct BuiltinFunctionDefinition {
- static constexpr auto Kind = NodeKind::BuiltinFunctionDefinition.Define(
- {.category = NodeCategory::Decl,
- .bracketed_by = BuiltinFunctionDefinitionStart::Kind});
- BuiltinFunctionDefinitionStartId signature;
- BuiltinNameId builtin_name;
- Lex::SemiTokenIndex token;
- };
- // `alias` nodes
- // -------------
- using AliasIntroducer =
- LeafNode<NodeKind::AliasIntroducer, Lex::AliasTokenIndex>;
- using AliasInitializer =
- LeafNode<NodeKind::AliasInitializer, Lex::EqualTokenIndex>;
- // An `alias` declaration: `alias a = b;`.
- struct Alias {
- static constexpr auto Kind = NodeKind::Alias.Define(
- {.category = NodeCategory::Decl, .bracketed_by = AliasIntroducer::Kind});
- AliasIntroducerId introducer;
- llvm::SmallVector<AnyModifierId> modifiers;
- DeclName name;
- AliasInitializerId equals;
- AnyExprId initializer;
- Lex::SemiTokenIndex token;
- };
- // `let` nodes
- // -----------
- using LetIntroducer = LeafNode<NodeKind::LetIntroducer, Lex::LetTokenIndex>;
- using LetInitializer = LeafNode<NodeKind::LetInitializer, Lex::EqualTokenIndex>;
- // A `let` declaration: `let a: i32 = 5;`.
- struct LetDecl {
- static constexpr auto Kind = NodeKind::LetDecl.Define(
- {.category = NodeCategory::Decl, .bracketed_by = LetIntroducer::Kind});
- LetIntroducerId introducer;
- llvm::SmallVector<AnyModifierId> modifiers;
- AnyPatternId pattern;
- struct Initializer {
- LetInitializerId equals;
- AnyExprId initializer;
- };
- std::optional<Initializer> initializer;
- Lex::SemiTokenIndex token;
- };
- // Associated constant nodes
- using AssociatedConstantIntroducer =
- LeafNode<NodeKind::AssociatedConstantIntroducer, Lex::LetTokenIndex>;
- using AssociatedConstantInitializer =
- LeafNode<NodeKind::AssociatedConstantInitializer, Lex::EqualTokenIndex>;
- struct AssociatedConstantNameAndType {
- static constexpr auto Kind = NodeKind::AssociatedConstantNameAndType.Define(
- {.category = NodeCategory::Pattern, .child_count = 2});
- AnyRuntimeBindingPatternName name;
- Lex::ColonExclaimTokenIndex token;
- AnyExprId type;
- };
- // An associated constant declaration: `let a:! i32;`.
- struct AssociatedConstantDecl {
- static constexpr auto Kind = NodeKind::AssociatedConstantDecl.Define(
- {.category = NodeCategory::Decl,
- .bracketed_by = AssociatedConstantIntroducer::Kind});
- AssociatedConstantIntroducerId introducer;
- llvm::SmallVector<AnyModifierId> modifiers;
- AssociatedConstantNameAndTypeId pattern;
- struct Initializer {
- AssociatedConstantInitializerId equals;
- AnyExprId initializer;
- };
- std::optional<Initializer> initializer;
- Lex::SemiTokenIndex token;
- };
- // `var` nodes
- // -----------
- using VariableIntroducer =
- LeafNode<NodeKind::VariableIntroducer, Lex::VarTokenIndex>;
- using ReturnedModifier =
- LeafNode<NodeKind::ReturnedModifier, Lex::ReturnedTokenIndex,
- NodeCategory::Modifier>;
- using VariableInitializer =
- LeafNode<NodeKind::VariableInitializer, Lex::EqualTokenIndex>;
- // A `var` declaration: `var a: i32;` or `var a: i32 = 5;`.
- struct VariableDecl {
- static constexpr auto Kind =
- NodeKind::VariableDecl.Define({.category = NodeCategory::Decl,
- .bracketed_by = VariableIntroducer::Kind});
- VariableIntroducerId introducer;
- llvm::SmallVector<AnyModifierId> modifiers;
- std::optional<ReturnedModifierId> returned;
- VariablePatternId pattern;
- struct Initializer {
- VariableInitializerId equals;
- AnyExprId value;
- };
- std::optional<Initializer> initializer;
- Lex::SemiTokenIndex token;
- };
- using FieldIntroducer = LeafNode<NodeKind::FieldIntroducer, Lex::VarTokenIndex>;
- using FieldInitializer =
- LeafNode<NodeKind::FieldInitializer, Lex::EqualTokenIndex>;
- struct FieldNameAndType {
- static constexpr auto Kind =
- NodeKind::FieldNameAndType.Define({.child_count = 2});
- IdentifierNameNotBeforeParamsId name;
- Lex::ColonTokenIndex token;
- AnyExprId type;
- };
- struct FieldDecl {
- static constexpr auto Kind = NodeKind::FieldDecl.Define(
- {.category = NodeCategory::Decl, .bracketed_by = FieldIntroducer::Kind});
- FieldIntroducerId introducer;
- llvm::SmallVector<AnyModifierId> modifiers;
- FieldNameAndTypeId name_and_type;
- struct Initializer {
- FieldInitializerId equals;
- AnyExprId value;
- };
- std::optional<Initializer> initializer;
- Lex::SemiTokenIndex token;
- };
- // A `var` pattern.
- struct VariablePattern {
- static constexpr auto Kind = NodeKind::VariablePattern.Define(
- {.category = NodeCategory::Pattern, .child_count = 1});
- Lex::VarTokenIndex token;
- AnyPatternId inner;
- };
- // Statement nodes
- // ---------------
- using CodeBlockStart =
- LeafNode<NodeKind::CodeBlockStart, Lex::OpenCurlyBraceTokenIndex>;
- // A code block: `{ statement; statement; ... }`.
- struct CodeBlock {
- static constexpr auto Kind =
- NodeKind::CodeBlock.Define({.bracketed_by = CodeBlockStart::Kind});
- CodeBlockStartId left_brace;
- llvm::SmallVector<AnyStatementId> statements;
- Lex::CloseCurlyBraceTokenIndex token;
- };
- // An expression statement: `F(x);`.
- struct ExprStatement {
- static constexpr auto Kind = NodeKind::ExprStatement.Define(
- {.category = NodeCategory::Statement, .child_count = 1});
- AnyExprId expr;
- Lex::SemiTokenIndex token;
- };
- using BreakStatementStart =
- LeafNode<NodeKind::BreakStatementStart, Lex::BreakTokenIndex>;
- // A break statement: `break;`.
- struct BreakStatement {
- static constexpr auto Kind = NodeKind::BreakStatement.Define(
- {.category = NodeCategory::Statement,
- .bracketed_by = BreakStatementStart::Kind,
- .child_count = 1});
- BreakStatementStartId introducer;
- Lex::SemiTokenIndex token;
- };
- using ContinueStatementStart =
- LeafNode<NodeKind::ContinueStatementStart, Lex::ContinueTokenIndex>;
- // A continue statement: `continue;`.
- struct ContinueStatement {
- static constexpr auto Kind = NodeKind::ContinueStatement.Define(
- {.category = NodeCategory::Statement,
- .bracketed_by = ContinueStatementStart::Kind,
- .child_count = 1});
- ContinueStatementStartId introducer;
- Lex::SemiTokenIndex token;
- };
- using ReturnStatementStart =
- LeafNode<NodeKind::ReturnStatementStart, Lex::ReturnTokenIndex>;
- using ReturnVarModifier = LeafNode<NodeKind::ReturnVarModifier,
- Lex::VarTokenIndex, NodeCategory::Modifier>;
- // A return statement: `return;` or `return expr;` or `return var;`.
- struct ReturnStatement {
- static constexpr auto Kind = NodeKind::ReturnStatement.Define(
- {.category = NodeCategory::Statement,
- .bracketed_by = ReturnStatementStart::Kind});
- ReturnStatementStartId introducer;
- // TODO: This should be optional<OneOf<AnyExprId, ReturnVarModifierId>>,
- // but we don't have support for OneOf between a node kind and a category.
- std::optional<AnyExprId> expr;
- std::optional<ReturnVarModifierId> var;
- Lex::SemiTokenIndex token;
- };
- using ForHeaderStart =
- LeafNode<NodeKind::ForHeaderStart, Lex::OpenParenTokenIndex>;
- // The `... in` portion of a `for` statement.
- struct ForIn {
- static constexpr auto Kind = NodeKind::ForIn.Define({.child_count = 1});
- AnyPatternId pattern;
- Lex::InTokenIndex token;
- };
- // The `(... in ...)` portion of a `for` statement.
- struct ForHeader {
- static constexpr auto Kind =
- NodeKind::ForHeader.Define({.bracketed_by = ForHeaderStart::Kind});
- ForHeaderStartId introducer;
- ForInId var;
- AnyExprId range;
- Lex::CloseParenTokenIndex token;
- };
- // A complete `for (...) { ... }` statement.
- struct ForStatement {
- static constexpr auto Kind =
- NodeKind::ForStatement.Define({.category = NodeCategory::Statement,
- .bracketed_by = ForHeader::Kind,
- .child_count = 2});
- Lex::ForTokenIndex token;
- ForHeaderId header;
- CodeBlockId body;
- };
- using IfConditionStart =
- LeafNode<NodeKind::IfConditionStart, Lex::OpenParenTokenIndex>;
- // The condition portion of an `if` statement: `(expr)`.
- struct IfCondition {
- static constexpr auto Kind = NodeKind::IfCondition.Define(
- {.bracketed_by = IfConditionStart::Kind, .child_count = 2});
- IfConditionStartId left_paren;
- AnyExprId condition;
- Lex::CloseParenTokenIndex token;
- };
- using IfStatementElse =
- LeafNode<NodeKind::IfStatementElse, Lex::ElseTokenIndex>;
- // An `if` statement: `if (expr) { ... } else { ... }`.
- struct IfStatement {
- static constexpr auto Kind = NodeKind::IfStatement.Define(
- {.category = NodeCategory::Statement, .bracketed_by = IfCondition::Kind});
- Lex::IfTokenIndex token;
- IfConditionId head;
- CodeBlockId then;
- struct Else {
- IfStatementElseId else_token;
- NodeIdOneOf<CodeBlock, IfStatement> body;
- };
- std::optional<Else> else_clause;
- };
- using WhileConditionStart =
- LeafNode<NodeKind::WhileConditionStart, Lex::OpenParenTokenIndex>;
- // The condition portion of a `while` statement: `(expr)`.
- struct WhileCondition {
- static constexpr auto Kind = NodeKind::WhileCondition.Define(
- {.bracketed_by = WhileConditionStart::Kind, .child_count = 2});
- WhileConditionStartId left_paren;
- AnyExprId condition;
- Lex::CloseParenTokenIndex token;
- };
- // A `while` statement: `while (expr) { ... }`.
- struct WhileStatement {
- static constexpr auto Kind =
- NodeKind::WhileStatement.Define({.category = NodeCategory::Statement,
- .bracketed_by = WhileCondition::Kind,
- .child_count = 2});
- Lex::WhileTokenIndex token;
- WhileConditionId head;
- CodeBlockId body;
- };
- using MatchConditionStart =
- LeafNode<NodeKind::MatchConditionStart, Lex::OpenParenTokenIndex>;
- struct MatchCondition {
- static constexpr auto Kind = NodeKind::MatchCondition.Define(
- {.bracketed_by = MatchConditionStart::Kind, .child_count = 2});
- MatchConditionStartId left_paren;
- AnyExprId condition;
- Lex::CloseParenTokenIndex token;
- };
- using MatchIntroducer =
- LeafNode<NodeKind::MatchIntroducer, Lex::MatchTokenIndex>;
- struct MatchStatementStart {
- static constexpr auto Kind = NodeKind::MatchStatementStart.Define(
- {.bracketed_by = MatchIntroducer::Kind, .child_count = 2});
- MatchIntroducerId introducer;
- MatchConditionId condition;
- Lex::OpenCurlyBraceTokenIndex token;
- };
- using MatchCaseIntroducer =
- LeafNode<NodeKind::MatchCaseIntroducer, Lex::CaseTokenIndex>;
- using MatchCaseGuardIntroducer =
- LeafNode<NodeKind::MatchCaseGuardIntroducer, Lex::IfTokenIndex>;
- using MatchCaseGuardStart =
- LeafNode<NodeKind::MatchCaseGuardStart, Lex::OpenParenTokenIndex>;
- struct MatchCaseGuard {
- static constexpr auto Kind = NodeKind::MatchCaseGuard.Define(
- {.bracketed_by = MatchCaseGuardIntroducer::Kind, .child_count = 3});
- MatchCaseGuardIntroducerId introducer;
- MatchCaseGuardStartId left_paren;
- AnyExprId condition;
- Lex::CloseParenTokenIndex token;
- };
- using MatchCaseEqualGreater =
- LeafNode<NodeKind::MatchCaseEqualGreater, Lex::EqualGreaterTokenIndex>;
- struct MatchCaseStart {
- static constexpr auto Kind = NodeKind::MatchCaseStart.Define(
- {.bracketed_by = MatchCaseIntroducer::Kind});
- MatchCaseIntroducerId introducer;
- AnyPatternId pattern;
- std::optional<MatchCaseGuardId> guard;
- MatchCaseEqualGreaterId equal_greater_token;
- Lex::OpenCurlyBraceTokenIndex token;
- };
- struct MatchCase {
- static constexpr auto Kind =
- NodeKind::MatchCase.Define({.bracketed_by = MatchCaseStart::Kind});
- MatchCaseStartId head;
- llvm::SmallVector<AnyStatementId> statements;
- Lex::CloseCurlyBraceTokenIndex token;
- };
- using MatchDefaultIntroducer =
- LeafNode<NodeKind::MatchDefaultIntroducer, Lex::DefaultTokenIndex>;
- using MatchDefaultEqualGreater =
- LeafNode<NodeKind::MatchDefaultEqualGreater, Lex::EqualGreaterTokenIndex>;
- struct MatchDefaultStart {
- static constexpr auto Kind = NodeKind::MatchDefaultStart.Define(
- {.bracketed_by = MatchDefaultIntroducer::Kind, .child_count = 2});
- MatchDefaultIntroducerId introducer;
- MatchDefaultEqualGreaterId equal_greater_token;
- Lex::OpenCurlyBraceTokenIndex token;
- };
- struct MatchDefault {
- static constexpr auto Kind =
- NodeKind::MatchDefault.Define({.bracketed_by = MatchDefaultStart::Kind});
- MatchDefaultStartId introducer;
- llvm::SmallVector<AnyStatementId> statements;
- Lex::CloseCurlyBraceTokenIndex token;
- };
- // A `match` statement: `match (expr) { case (...) => {...} default => {...}}`.
- struct MatchStatement {
- static constexpr auto Kind = NodeKind::MatchStatement.Define(
- {.category = NodeCategory::Statement,
- .bracketed_by = MatchStatementStart::Kind});
- MatchStatementStartId head;
- llvm::SmallVector<MatchCaseId> cases;
- std::optional<MatchDefaultId> default_case;
- Lex::CloseCurlyBraceTokenIndex token;
- };
- // Expression nodes
- // ----------------
- using ArrayExprKeyword =
- LeafNode<NodeKind::ArrayExprKeyword, Lex::ArrayTokenIndex>;
- using ArrayExprOpenParen =
- LeafNode<NodeKind::ArrayExprOpenParen, Lex::OpenParenTokenIndex>;
- using ArrayExprComma = LeafNode<NodeKind::ArrayExprComma, Lex::CommaTokenIndex>;
- // An array type, `array(T, N)`.
- struct ArrayExpr {
- static constexpr auto Kind = NodeKind::ArrayExpr.Define(
- {.category = NodeCategory::Expr, .child_count = 5});
- ArrayExprKeywordId keyword;
- ArrayExprOpenParenId start;
- AnyExprId type;
- ArrayExprCommaId comma;
- AnyExprId bound;
- Lex::CloseParenTokenIndex token;
- };
- // The opening portion of an indexing expression: `a[`.
- //
- // TODO: Consider flattening this into `IndexExpr`.
- struct IndexExprStart {
- static constexpr auto Kind =
- NodeKind::IndexExprStart.Define({.child_count = 1});
- AnyExprId sequence;
- Lex::OpenSquareBracketTokenIndex token;
- };
- // An indexing expression, such as `a[1]`.
- struct IndexExpr {
- static constexpr auto Kind =
- NodeKind::IndexExpr.Define({.category = NodeCategory::Expr,
- .bracketed_by = IndexExprStart::Kind,
- .child_count = 2});
- IndexExprStartId start;
- AnyExprId index;
- Lex::CloseSquareBracketTokenIndex token;
- };
- using ParenExprStart =
- LeafNode<NodeKind::ParenExprStart, Lex::OpenParenTokenIndex>;
- // A parenthesized expression: `(a)`.
- struct ParenExpr {
- static constexpr auto Kind = NodeKind::ParenExpr.Define(
- {.category = NodeCategory::Expr | NodeCategory::MemberExpr,
- .bracketed_by = ParenExprStart::Kind,
- .child_count = 2});
- ParenExprStartId start;
- AnyExprId expr;
- Lex::CloseParenTokenIndex token;
- };
- using TupleLiteralStart =
- LeafNode<NodeKind::TupleLiteralStart, Lex::OpenParenTokenIndex>;
- using TupleLiteralComma =
- LeafNode<NodeKind::TupleLiteralComma, Lex::CommaTokenIndex>;
- // A tuple literal: `()`, `(a, b, c)`, or `(a,)`.
- struct TupleLiteral {
- static constexpr auto Kind =
- NodeKind::TupleLiteral.Define({.category = NodeCategory::Expr,
- .bracketed_by = TupleLiteralStart::Kind});
- TupleLiteralStartId start;
- CommaSeparatedList<AnyExprId, TupleLiteralCommaId> elements;
- Lex::CloseParenTokenIndex token;
- };
- // The opening portion of a call expression: `F(`.
- //
- // TODO: Consider flattening this into `CallExpr`.
- struct CallExprStart {
- static constexpr auto Kind =
- NodeKind::CallExprStart.Define({.child_count = 1});
- AnyExprId callee;
- Lex::OpenParenTokenIndex token;
- };
- using CallExprComma = LeafNode<NodeKind::CallExprComma, Lex::CommaTokenIndex>;
- // A call expression: `F(a, b, c)`.
- struct CallExpr {
- static constexpr auto Kind = NodeKind::CallExpr.Define(
- {.category = NodeCategory::Expr, .bracketed_by = CallExprStart::Kind});
- CallExprStartId start;
- CommaSeparatedList<AnyExprId, CallExprCommaId> arguments;
- Lex::CloseParenTokenIndex token;
- };
- // A member access expression: `a.b` or `a.(b)`.
- struct MemberAccessExpr {
- static constexpr auto Kind = NodeKind::MemberAccessExpr.Define(
- {.category = NodeCategory::Expr, .child_count = 2});
- AnyExprId lhs;
- Lex::PeriodTokenIndex token;
- AnyMemberAccessId rhs;
- };
- // An indirect member access expression: `a->b` or `a->(b)`.
- struct PointerMemberAccessExpr {
- static constexpr auto Kind = NodeKind::PointerMemberAccessExpr.Define(
- {.category = NodeCategory::Expr, .child_count = 2});
- AnyExprId lhs;
- Lex::MinusGreaterTokenIndex token;
- AnyMemberAccessId rhs;
- };
- // A prefix operator expression.
- template <const NodeKind& KindT, typename TokenKind>
- struct PrefixOperator {
- static constexpr auto Kind =
- KindT.Define({.category = NodeCategory::Expr, .child_count = 1});
- TokenKind token;
- AnyExprId operand;
- };
- // An infix operator expression.
- template <const NodeKind& KindT, typename TokenKind>
- struct InfixOperator {
- static constexpr auto Kind =
- KindT.Define({.category = NodeCategory::Expr, .child_count = 2});
- AnyExprId lhs;
- TokenKind token;
- AnyExprId rhs;
- };
- // A postfix operator expression.
- template <const NodeKind& KindT, typename TokenKind>
- struct PostfixOperator {
- static constexpr auto Kind =
- KindT.Define({.category = NodeCategory::Expr, .child_count = 1});
- AnyExprId operand;
- TokenKind token;
- };
- // An `unsafe` modifier: `a unsafe <operator> b`. This is modeled in the parse
- // tree as a postfix operator applied to `a`.
- struct UnsafeModifier {
- static constexpr auto Kind = NodeKind::UnsafeModifier.Define(
- {.category = NodeCategory::Expr, .child_count = 1});
- AnyExprId operand;
- Lex::UnsafeTokenIndex token;
- };
- // Literals, operators, and modifiers
- #define CARBON_PARSE_NODE_KIND(Name)
- #define CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(Name, LexTokenKind) \
- using Name = LeafNode<NodeKind::Name, Lex::LexTokenKind##TokenIndex, \
- NodeCategory::Expr>;
- #define CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER(Name) \
- using Name##Modifier = \
- LeafNode<NodeKind::Name##Modifier, Lex::Name##TokenIndex, \
- NodeCategory::Modifier>;
- #define CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Name) \
- using PrefixOperator##Name = \
- PrefixOperator<NodeKind::PrefixOperator##Name, Lex::Name##TokenIndex>;
- #define CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Name) \
- using InfixOperator##Name = \
- InfixOperator<NodeKind::InfixOperator##Name, Lex::Name##TokenIndex>;
- #define CARBON_PARSE_NODE_KIND_POSTFIX_OPERATOR(Name) \
- using PostfixOperator##Name = \
- PostfixOperator<NodeKind::PostfixOperator##Name, Lex::Name##TokenIndex>;
- #include "toolchain/parse/node_kind.def"
- using IntLiteral = LeafNode<NodeKind::IntLiteral, Lex::IntLiteralTokenIndex,
- NodeCategory::Expr | NodeCategory::IntConst>;
- // `extern` as a standalone modifier.
- using ExternModifier = LeafNode<NodeKind::ExternModifier, Lex::ExternTokenIndex,
- NodeCategory::Modifier>;
- // `extern library <owning_library>` modifiers.
- struct ExternModifierWithLibrary {
- static constexpr auto Kind = NodeKind::ExternModifierWithLibrary.Define(
- {.category = NodeCategory::Modifier, .child_count = 1});
- Lex::ExternTokenIndex token;
- LibrarySpecifierId library;
- };
- // The first operand of a short-circuiting infix operator: `a and` or `a or`.
- // The complete operator expression will be an InfixOperator with this as the
- // `lhs`.
- // TODO: Make this be a template if we ever need to write generic code to cover
- // both cases at once, say in check.
- struct ShortCircuitOperandAnd {
- static constexpr auto Kind =
- NodeKind::ShortCircuitOperandAnd.Define({.child_count = 1});
- AnyExprId operand;
- // This is a virtual token. The `and` token is owned by the
- // ShortCircuitOperatorAnd node.
- Lex::AndTokenIndex token;
- };
- struct ShortCircuitOperandOr {
- static constexpr auto Kind =
- NodeKind::ShortCircuitOperandOr.Define({.child_count = 1});
- AnyExprId operand;
- // This is a virtual token. The `or` token is owned by the
- // ShortCircuitOperatorOr node.
- Lex::OrTokenIndex token;
- };
- struct ShortCircuitOperatorAnd {
- static constexpr auto Kind = NodeKind::ShortCircuitOperatorAnd.Define(
- {.category = NodeCategory::Expr,
- .bracketed_by = ShortCircuitOperandAnd::Kind,
- .child_count = 2});
- ShortCircuitOperandAndId lhs;
- Lex::AndTokenIndex token;
- AnyExprId rhs;
- };
- struct ShortCircuitOperatorOr {
- static constexpr auto Kind = NodeKind::ShortCircuitOperatorOr.Define(
- {.category = NodeCategory::Expr,
- .bracketed_by = ShortCircuitOperandOr::Kind,
- .child_count = 2});
- ShortCircuitOperandOrId lhs;
- Lex::OrTokenIndex token;
- AnyExprId rhs;
- };
- // The `if` portion of an `if` expression: `if expr`.
- struct IfExprIf {
- static constexpr auto Kind = NodeKind::IfExprIf.Define({.child_count = 1});
- Lex::IfTokenIndex token;
- AnyExprId condition;
- };
- // The `then` portion of an `if` expression: `then expr`.
- struct IfExprThen {
- static constexpr auto Kind = NodeKind::IfExprThen.Define({.child_count = 1});
- Lex::ThenTokenIndex token;
- AnyExprId result;
- };
- // A full `if` expression: `if expr then expr else expr`.
- struct IfExprElse {
- static constexpr auto Kind =
- NodeKind::IfExprElse.Define({.category = NodeCategory::Expr,
- .bracketed_by = IfExprIf::Kind,
- .child_count = 3});
- IfExprIfId start;
- IfExprThenId then;
- Lex::ElseTokenIndex token;
- AnyExprId else_result;
- };
- // A `where` expression (TODO: `require` and `observe` declarations)
- // The `Self` in a context where it is treated as a name rather than an
- // expression, such as `.Self`.
- using SelfTypeName =
- LeafNode<NodeKind::SelfTypeName, Lex::SelfTypeIdentifierTokenIndex>;
- // `.Member` or `.Self` in an expression context, used in `where` and `require`
- // clauses.
- // TODO: Do we want to support `.1`, a designator for accessing a tuple member?
- struct DesignatorExpr {
- static constexpr auto Kind = NodeKind::DesignatorExpr.Define(
- {.category = NodeCategory::Expr, .child_count = 1});
- Lex::PeriodTokenIndex token;
- NodeIdOneOf<IdentifierNameNotBeforeParams, SelfTypeName> name;
- };
- struct RequirementEqual {
- static constexpr auto Kind = NodeKind::RequirementEqual.Define(
- {.category = NodeCategory::Requirement, .child_count = 2});
- DesignatorExprId lhs;
- Lex::EqualTokenIndex token;
- AnyExprId rhs;
- };
- struct RequirementEqualEqual {
- static constexpr auto Kind = NodeKind::RequirementEqualEqual.Define(
- {.category = NodeCategory::Requirement, .child_count = 2});
- AnyExprId lhs;
- Lex::EqualEqualTokenIndex token;
- AnyExprId rhs;
- };
- struct RequirementImpls {
- static constexpr auto Kind = NodeKind::RequirementImpls.Define(
- {.category = NodeCategory::Requirement, .child_count = 2});
- AnyExprId lhs;
- Lex::ImplsTokenIndex token;
- AnyExprId rhs;
- };
- // An `and` token separating requirements in a `where` expression.
- using RequirementAnd = LeafNode<NodeKind::RequirementAnd, Lex::AndTokenIndex>;
- struct WhereOperand {
- static constexpr auto Kind =
- NodeKind::WhereOperand.Define({.child_count = 1});
- AnyExprId type;
- // This is a virtual token. The `where` token is owned by the
- // WhereExpr node.
- Lex::WhereTokenIndex token;
- };
- struct WhereExpr {
- static constexpr auto Kind = NodeKind::WhereExpr.Define(
- {.category = NodeCategory::Expr, .bracketed_by = WhereOperand::Kind});
- WhereOperandId introducer;
- Lex::WhereTokenIndex token;
- CommaSeparatedList<AnyRequirementId, RequirementAndId> requirements;
- };
- // Choice nodes
- // ------------
- using ChoiceIntroducer =
- LeafNode<NodeKind::ChoiceIntroducer, Lex::ChoiceTokenIndex>;
- struct ChoiceSignature {
- static constexpr auto Kind = NodeKind::ChoiceDefinitionStart.Define(
- {.category = NodeCategory::None, .bracketed_by = ChoiceIntroducer::Kind});
- ChoiceIntroducerId introducer;
- llvm::SmallVector<AnyModifierId> modifiers;
- DeclName name;
- Lex::OpenCurlyBraceTokenIndex token;
- };
- using ChoiceDefinitionStart = ChoiceSignature;
- using ChoiceAlternativeListComma =
- LeafNode<NodeKind::ChoiceAlternativeListComma, Lex::CommaTokenIndex>;
- struct ChoiceDefinition {
- static constexpr auto Kind = NodeKind::ChoiceDefinition.Define(
- {.category = NodeCategory::Decl,
- .bracketed_by = ChoiceDefinitionStart::Kind});
- ChoiceDefinitionStartId signature;
- struct Alternative {
- AnyNonExprNameId name;
- std::optional<ExplicitParamListId> parameters;
- };
- CommaSeparatedList<Alternative, ChoiceAlternativeListCommaId> alternatives;
- Lex::CloseCurlyBraceTokenIndex token;
- };
- // Struct type and value literals
- // ----------------------------------------
- // `{`
- using StructLiteralStart =
- LeafNode<NodeKind::StructLiteralStart, Lex::OpenCurlyBraceTokenIndex>;
- using StructTypeLiteralStart =
- LeafNode<NodeKind::StructTypeLiteralStart, Lex::OpenCurlyBraceTokenIndex>;
- // `,`
- using StructLiteralComma =
- LeafNode<NodeKind::StructLiteralComma, Lex::CommaTokenIndex>;
- using StructTypeLiteralComma =
- LeafNode<NodeKind::StructTypeLiteralComma, Lex::CommaTokenIndex>;
- // `.a`
- // This is shared for struct literals and type literals in order to reduce
- // lookahead for parse (the `=` versus `:` would require lookahead of 2).
- struct StructFieldDesignator {
- static constexpr auto Kind =
- NodeKind::StructFieldDesignator.Define({.child_count = 1});
- Lex::PeriodTokenIndex token;
- NodeIdOneOf<IdentifierNameNotBeforeParams, BaseName> name;
- };
- // `.a = 0`
- struct StructLiteralField {
- static constexpr auto Kind = NodeKind::StructLiteralField.Define(
- {.bracketed_by = StructFieldDesignator::Kind, .child_count = 2});
- StructFieldDesignatorId designator;
- Lex::EqualTokenIndex token;
- AnyExprId expr;
- };
- // `.a: i32`
- struct StructTypeLiteralField {
- static constexpr auto Kind = NodeKind::StructTypeLiteralField.Define(
- {.bracketed_by = StructFieldDesignator::Kind, .child_count = 2});
- StructFieldDesignatorId designator;
- Lex::ColonTokenIndex token;
- AnyExprId type_expr;
- };
- // Struct literals, such as `{.a = 0}`.
- struct StructLiteral {
- static constexpr auto Kind = NodeKind::StructLiteral.Define(
- {.category = NodeCategory::Expr,
- .bracketed_by = StructLiteralStart::Kind});
- StructLiteralStartId start;
- CommaSeparatedList<StructLiteralFieldId, StructLiteralCommaId> fields;
- Lex::CloseCurlyBraceTokenIndex token;
- };
- // Struct type literals, such as `{.a: i32}`.
- struct StructTypeLiteral {
- static constexpr auto Kind = NodeKind::StructTypeLiteral.Define(
- {.category = NodeCategory::Expr,
- .bracketed_by = StructTypeLiteralStart::Kind});
- StructTypeLiteralStartId start;
- CommaSeparatedList<StructTypeLiteralFieldId, StructTypeLiteralCommaId> fields;
- Lex::CloseCurlyBraceTokenIndex token;
- };
- // `class` declarations and definitions
- // ------------------------------------
- // `class`
- using ClassIntroducer =
- LeafNode<NodeKind::ClassIntroducer, Lex::ClassTokenIndex>;
- // A class signature `class C`
- template <const NodeKind& KindT, typename TokenKind,
- NodeCategory::RawEnumType Category>
- struct ClassSignature {
- static constexpr auto Kind = KindT.Define(
- {.category = Category, .bracketed_by = ClassIntroducer::Kind});
- ClassIntroducerId introducer;
- llvm::SmallVector<AnyModifierId> modifiers;
- DeclName name;
- TokenKind token;
- };
- // `class C;`
- using ClassDecl = ClassSignature<NodeKind::ClassDecl, Lex::SemiTokenIndex,
- NodeCategory::Decl>;
- // `class C {`
- using ClassDefinitionStart =
- ClassSignature<NodeKind::ClassDefinitionStart,
- Lex::OpenCurlyBraceTokenIndex, NodeCategory::None>;
- // `class C { ... }`
- struct ClassDefinition {
- static constexpr auto Kind = NodeKind::ClassDefinition.Define(
- {.category = NodeCategory::Decl,
- .bracketed_by = ClassDefinitionStart::Kind});
- ClassDefinitionStartId signature;
- llvm::SmallVector<AnyDeclId> members;
- Lex::CloseCurlyBraceTokenIndex token;
- };
- // Adapter declaration
- // -------------------
- // `adapt`
- using AdaptIntroducer =
- LeafNode<NodeKind::AdaptIntroducer, Lex::AdaptTokenIndex>;
- // `adapt SomeType;`
- struct AdaptDecl {
- static constexpr auto Kind = NodeKind::AdaptDecl.Define(
- {.category = NodeCategory::Decl, .bracketed_by = AdaptIntroducer::Kind});
- AdaptIntroducerId introducer;
- llvm::SmallVector<AnyModifierId> modifiers;
- AnyExprId adapted_type;
- Lex::SemiTokenIndex token;
- };
- // Base class declaration
- // ----------------------
- // `base`
- using BaseIntroducer = LeafNode<NodeKind::BaseIntroducer, Lex::BaseTokenIndex>;
- using BaseColon = LeafNode<NodeKind::BaseColon, Lex::ColonTokenIndex>;
- // `extend base: BaseClass;`
- struct BaseDecl {
- static constexpr auto Kind = NodeKind::BaseDecl.Define(
- {.category = NodeCategory::Decl, .bracketed_by = BaseIntroducer::Kind});
- BaseIntroducerId introducer;
- llvm::SmallVector<AnyModifierId> modifiers;
- BaseColonId colon;
- AnyExprId base_class;
- Lex::SemiTokenIndex token;
- };
- // Interface declarations and definitions
- // --------------------------------------
- // `interface`
- using InterfaceIntroducer =
- LeafNode<NodeKind::InterfaceIntroducer, Lex::InterfaceTokenIndex>;
- // `interface I`
- template <const NodeKind& KindT, typename TokenKind,
- NodeCategory::RawEnumType Category>
- struct InterfaceSignature {
- static constexpr auto Kind = KindT.Define(
- {.category = Category, .bracketed_by = InterfaceIntroducer::Kind});
- InterfaceIntroducerId introducer;
- llvm::SmallVector<AnyModifierId> modifiers;
- DeclName name;
- TokenKind token;
- };
- // `interface I;`
- using InterfaceDecl =
- InterfaceSignature<NodeKind::InterfaceDecl, Lex::SemiTokenIndex,
- NodeCategory::Decl>;
- // `interface I {`
- using InterfaceDefinitionStart =
- InterfaceSignature<NodeKind::InterfaceDefinitionStart,
- Lex::OpenCurlyBraceTokenIndex, NodeCategory::None>;
- // `interface I { ... }`
- struct InterfaceDefinition {
- static constexpr auto Kind = NodeKind::InterfaceDefinition.Define(
- {.category = NodeCategory::Decl,
- .bracketed_by = InterfaceDefinitionStart::Kind});
- InterfaceDefinitionStartId signature;
- llvm::SmallVector<AnyDeclId> members;
- Lex::CloseCurlyBraceTokenIndex token;
- };
- // `require`...`impls` statements
- // ------------------------------
- // `require`
- using RequireIntroducer =
- LeafNode<NodeKind::RequireIntroducer, Lex::RequireTokenIndex>;
- // `impls` with no type before it
- using RequireDefaultSelfImpls =
- LeafNode<NodeKind::RequireDefaultSelfImpls, Lex::ImplsTokenIndex,
- NodeCategory::RequireImpls>;
- // `<type> impls`.
- struct RequireTypeImpls {
- static constexpr auto Kind = NodeKind::RequireTypeImpls.Define(
- {.category = NodeCategory::RequireImpls, .child_count = 1});
- AnyExprId type_expr;
- Lex::ImplsTokenIndex token;
- };
- // `require T impls I where...`
- struct RequireDecl {
- static constexpr auto Kind =
- NodeKind::RequireDecl.Define({.category = NodeCategory::Decl,
- .bracketed_by = RequireIntroducer::Kind});
- RequireIntroducerId introducer;
- llvm::SmallVector<AnyModifierId> modifiers;
- AnyRequireImplsId impls;
- AnyExprId facet_type;
- Lex::SemiTokenIndex token;
- };
- // `impl`...`as` declarations and definitions
- // ------------------------------------------
- // `impl`
- using ImplIntroducer = LeafNode<NodeKind::ImplIntroducer, Lex::ImplTokenIndex>;
- // `forall`
- using Forall = LeafNode<NodeKind::Forall, Lex::ForallTokenIndex>;
- // `forall [...]`
- struct ImplForall {
- ForallId forall;
- ImplicitParamListId params;
- };
- // `as` with no type before it
- using ImplDefaultSelfAs = LeafNode<NodeKind::ImplDefaultSelfAs,
- Lex::AsTokenIndex, NodeCategory::ImplAs>;
- // `<type> as`
- struct ImplTypeAs {
- static constexpr auto Kind = NodeKind::ImplTypeAs.Define(
- {.category = NodeCategory::ImplAs, .child_count = 1});
- AnyExprId type_expr;
- Lex::AsTokenIndex token;
- };
- // `impl T as I`
- template <const NodeKind& KindT, typename TokenKind,
- NodeCategory::RawEnumType Category>
- struct ImplSignature {
- static constexpr auto Kind = KindT.Define(
- {.category = Category, .bracketed_by = ImplIntroducer::Kind});
- ImplIntroducerId introducer;
- llvm::SmallVector<AnyModifierId> modifiers;
- std::optional<ImplForall> forall;
- AnyImplAsId as;
- AnyExprId interface;
- TokenKind token;
- };
- // `impl T as I;`
- using ImplDecl =
- ImplSignature<NodeKind::ImplDecl, Lex::SemiTokenIndex, NodeCategory::Decl>;
- // `impl T as I {`
- using ImplDefinitionStart =
- ImplSignature<NodeKind::ImplDefinitionStart, Lex::OpenCurlyBraceTokenIndex,
- NodeCategory::None>;
- // `impl T as I { ... }`
- struct ImplDefinition {
- static constexpr auto Kind = NodeKind::ImplDefinition.Define(
- {.category = NodeCategory::Decl,
- .bracketed_by = ImplDefinitionStart::Kind});
- ImplDefinitionStartId signature;
- llvm::SmallVector<AnyDeclId> members;
- Lex::CloseCurlyBraceTokenIndex token;
- };
- // Named constraint declarations and definitions
- // ---------------------------------------------
- // `constraint`
- using NamedConstraintIntroducer =
- LeafNode<NodeKind::NamedConstraintIntroducer, Lex::ConstraintTokenIndex>;
- // `constraint NC`
- template <const NodeKind& KindT, typename TokenKind,
- NodeCategory::RawEnumType Category>
- struct NamedConstraintSignature {
- static constexpr auto Kind = KindT.Define(
- {.category = Category, .bracketed_by = NamedConstraintIntroducer::Kind});
- NamedConstraintIntroducerId introducer;
- llvm::SmallVector<AnyModifierId> modifiers;
- DeclName name;
- TokenKind token;
- };
- // `constraint NC;`
- using NamedConstraintDecl =
- NamedConstraintSignature<NodeKind::NamedConstraintDecl, Lex::SemiTokenIndex,
- NodeCategory::Decl>;
- // `constraint NC {`
- using NamedConstraintDefinitionStart =
- NamedConstraintSignature<NodeKind::NamedConstraintDefinitionStart,
- Lex::OpenCurlyBraceTokenIndex, NodeCategory::None>;
- // `constraint NC { ... }`
- struct NamedConstraintDefinition {
- static constexpr auto Kind = NodeKind::NamedConstraintDefinition.Define(
- {.category = NodeCategory::Decl,
- .bracketed_by = NamedConstraintDefinitionStart::Kind});
- NamedConstraintDefinitionStartId signature;
- llvm::SmallVector<AnyDeclId> members;
- Lex::CloseCurlyBraceTokenIndex token;
- };
- // ---------------------------------------------------------------------------
- // A complete source file. Note that there is no corresponding parse node for
- // the file. The file is instead the complete contents of the parse tree.
- struct File {
- FileStartId start;
- llvm::SmallVector<AnyDeclId> decls;
- FileEndId end;
- };
- // Define `Foo` as the node type for the ID type `FooId`.
- #define CARBON_PARSE_NODE_KIND(KindName) \
- template <> \
- struct NodeForId<KindName##Id> { \
- using TypedNode = KindName; \
- };
- #include "toolchain/parse/node_kind.def"
- } // namespace Carbon::Parse
- #endif // CARBON_TOOLCHAIN_PARSE_TYPED_NODES_H_
|