1//===- Syntax.td - TableGen metamodel for syntax::Node hierarchy ----------===// 2// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6// 7//===----------------------------------------------------------------------===// 8// 9// The tree representation of the is C++ syntax is quite regular. 10// 11// There are 4 archetypes of nodes in the syntax tree: 12// - Leaves, owning exactly one token. (syntax::Leaf) 13// - Sequences, with a fixed list of children that should appear in order. 14// The concrete node defines a Role sequence which identifies the children. 15// The type of child in each role is also constrained. 16// - Lists, with children in alternating Element/Delimiter roles. (syntax::List) 17// The concrete node defines the element type, delimiters are always leaves. 18// - Alternatives, where several different node types are allowed. 19// These are modeled as abstract types with inheritance (e.g. Declaration). 20// 21// This file defines TableGen classes modelling these archetypes. 22// The concrete nodes are defined in terms of these classes in Nodes.td. 23// 24// The C++ classes for the archetypes themselves are written by hand, and the 25// concrete node classes will be generated. Migration to TableGen is not 26// complete, so currently there is a mix of generated and hand-authored code. 27// 28//===----------------------------------------------------------------------===// 29 30// Syntax is any constraint on constructs that can appear somewhere. 31class Syntax; 32class Optional<Syntax inner_> : Syntax { Syntax inner = inner_; } 33class AnyToken<list<string> kinds_> : Syntax { list<string> kinds = kinds_; } 34class Token<string kind_> : AnyToken<[kind_]>; 35class Keyword<string kw> : Token<!strconcat("kw_", kw)>; 36 37// Defs derived from NodeType correspond to syntax tree node types. 38// NodeType is also a syntax constraint: one node of this type. 39class NodeType : Syntax { 40 // The NodeType that this node is derived from in the Node class hierarchy. 41 NodeType base = ?; 42 // Documentation for this Node subclass. 43 string documentation; 44} 45 46// A node type which is defined in Nodes.h rather than by generated code. 47// We merely specify the inheritance hierarchy here. 48class External<NodeType base_> : NodeType { let base = base_; } 49 50// Special nodes defined here. 51def Node : External<?> {} 52def Leaf : External<Node> {} 53def Tree : External<Node> {} 54 55// An abstract node type which merely serves as a base for more specific types. 56// 57// This corresponds to an alternative rule in the grammar, such as: 58// Statement = IfStatement | ForStatement | ... 59// Statement is modeled using Alternatives, and IfStatement.base is Statement. 60class Alternatives<NodeType base_ = Tree> : NodeType { let base = base_; } 61 62// A node type which may contain anything and has no specific accessors. 63// These are generally placeholders for a more precise implementation. 64class Unconstrained<NodeType base_ = Tree> : NodeType { let base = base_; } 65 66class Role<string role_, Syntax syntax_> { 67 string role = role_; 68 Syntax syntax = syntax_; 69} 70 71// A node which contains a fixed sequence of children in a particular order. 72// 73// Each child is characterized by a role (unique within the sequence), and 74// has an allowed base type for the node. 75// The role sequence and role/type match are enforced invariants of the class. 76// 77// We also record whether the child is required to be present, and which tokens 78// are permitted (for Leaf nodes). These invariants are not enforced. 79class Sequence<NodeType base_ = Tree> : NodeType { 80 let base = base_; 81 // Children must be Role or have a default role derived from the NodeType. 82 list<Role> children; 83} 84 85// FIXME: add list archetype. 86