clang: include/clang/Tooling/Syntax/Tree.h Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21#ifndef LLVM_CLANG_TOOLING_SYNTAX_TREE_H

22#define LLVM_CLANG_TOOLING_SYNTAX_TREE_H

23

26#include "llvm/ADT/iterator.h"

27#include "llvm/Support/Allocator.h"

28#include

29#include

30

33

34

35

37public:

38 llvm::BumpPtrAllocator &getAllocator() { return Allocator; }

39private:

40

41 llvm::BumpPtrAllocator Allocator;

42};

43

44class Tree;

45class TreeBuilder;

46class FactoryImpl;

47class MutationsImpl;

48

49enum class NodeKind : uint16_t;

51

52

53

55protected:

56

57

59

61

62public:

63

66

69

72

73

75

76

77

78

79

80

82

83

84

85

86

87

88 bool canModify() const { return CanModify; }

89

92

97

98

100

102

103

104

106

108

109private:

110

112

114

116

118

120

121 Tree *Parent;

122 Node *NextSibling;

123 Node *PreviousSibling;

124 unsigned Kind : 16;

125 unsigned Role : 8;

126 unsigned Original : 1;

127 unsigned CanModify : 1;

128};

129

130

131

133public:

136

138

139private:

141};

142

143

145

146

147 template <typename DerivedT, typename NodeT>

148 class ChildIteratorBase

149 : public llvm::iterator_facade_base<DerivedT, std::forward_iterator_tag,

150 NodeT> {

151 protected:

152 NodeT *N = nullptr;

153 using Base = ChildIteratorBase;

154

155 public:

156 ChildIteratorBase() = default;

157 explicit ChildIteratorBase(NodeT *N) : N(N) {}

158

159 friend bool operator==(const DerivedT &LHS, const DerivedT &RHS) {

160 return LHS.N == RHS.N;

161 }

162

163 NodeT &operator*() const { return *N; }

164 DerivedT &operator++() {

165 N = N->getNextSibling();

166 return *static_cast<DerivedT *>(this);

167 }

168

169

170

171 explicit operator bool() const { return N != nullptr; }

172

173 NodeT *asPointer() const { return N; }

174 };

175

176public:

178

183

186 return const_cast<Leaf *>(const_cast<const Tree *>(this)->findFirstLeaf());

187 }

188

191 return const_cast<Leaf *>(const_cast<const Tree *>(this)->findLastLeaf());

192 }

193

194

195 struct ChildIterator : ChildIteratorBase<ChildIterator, Node> {

196 using Base::ChildIteratorBase;

197 };

199 : ChildIteratorBase<ConstChildIterator, const Node> {

200 using Base::ChildIteratorBase;

203 };

204

208 llvm::iterator_range getChildren() const {

210 }

211

212

215 return const_cast<Node *>(const_cast<const Tree *>(this)->findChild(R));

216 }

217

218protected:

220

221private:

222

223

224

225

227

229

230

231

232 void appendChildLowLevel(Node *Child);

233 void prependChildLowLevel(Node *Child);

236

237

238

239

240

241

242

243 void replaceChildRangeLowLevel(Node *Begin, Node *End, Node *New);

245

246 Node *FirstChild = nullptr;

247 Node *LastChild = nullptr;

248};

249

250

251

252

253

255public:

260

266

269

270

271

272

273

274

275

276

277

278

279

280

281

282

284

285

286

287

289

290

291

292

293

294

295

297

299

300

301

302

303

304

306};

307

308}

309}

310

311#endif

clang::CharUnits operator*(clang::CharUnits::QuantityType Scale, const clang::CharUnits &CU)

std::shared_ptr< TokenRole > Role

A token can have a special role that can carry extra information about the token's formatting.

Defines the clang::TokenKind enum and support functions.

A memory arena for syntax trees.

Definition Tree.h:36

llvm::BumpPtrAllocator & getAllocator()

Definition Tree.h:38

A leaf node points to a single token.

Definition Tree.h:132

TokenManager::Key getTokenKey() const

Definition Tree.h:137

static bool classof(const Node *N)

Leaf(TokenManager::Key K)

A list of Elements separated or terminated by a fixed token.

Definition Tree.h:254

bool canBeEmpty() const

Whether this list can be empty in syntactically and semantically correct code.

static bool classof(const Node *N)

std::vector< Node * > getElementsAsNodes()

Returns the elements of the list.

std::vector< ElementAndDelimiter< Node > > getElementsAsNodesAndDelimiters()

Returns the elements and corresponding delimiters.

TerminationKind

Definition Tree.h:261

@ Separated

Definition Tree.h:264

@ MaybeTerminated

Definition Tree.h:263

@ Terminated

Definition Tree.h:262

TerminationKind getTerminationKind() const

clang::tok::TokenKind getDelimiterTokenKind() const

Returns the appropriate delimiter for this list.

A node in a syntax tree.

Definition Tree.h:54

void assertInvariants() const

Asserts invariants on this node of the tree and its immediate children.

friend class MutationsImpl

Definition Tree.h:115

friend class TreeBuilder

Definition Tree.h:113

friend class Tree

Definition Tree.h:111

Node * getPreviousSibling()

Definition Tree.h:96

bool canModify() const

If this function return false, the tree cannot be modified because there is no reasonable way to prod...

Definition Tree.h:88

void assertInvariantsRecursive() const

Runs checkInvariants on all nodes in the subtree. No-op if NDEBUG is set.

const Node * getPreviousSibling() const

Definition Tree.h:95

NodeRole getRole() const

Definition Tree.h:71

Node(NodeKind Kind)

Newly created nodes are detached from a tree, parent and sibling links are set when the node is added...

std::string dump(const TokenManager &SM) const

Dumps the structure of a subtree. For debugging and testing purposes.

Node * getNextSibling()

Definition Tree.h:94

Node(const Node &)=delete

Nodes cannot simply be copied without violating tree invariants.

NodeKind getKind() const

Definition Tree.h:70

Node & operator=(Node &&)=delete

const Node * getNextSibling() const

Definition Tree.h:93

Tree * getParent()

Definition Tree.h:91

friend class FactoryImpl

Definition Tree.h:117

~Node()=default

Nodes are allocated on Arenas; the destructor is never called.

std::string dumpTokens(const TokenManager &SM) const

Dumps the tokens forming this subtree.

Node(Node &&)=delete

Idiomatically, nodes are allocated on an Arena and never moved.

Node & operator=(const Node &)=delete

bool isOriginal() const

Whether the node was created from the AST backed by the source code rather than added later through m...

Definition Tree.h:81

const Tree * getParent() const

Definition Tree.h:90

bool isDetached() const

Whether the node is detached from a tree, i.e. does not have a parent.

Defines interfaces for operating "Token" in the clang syntax-tree.

uintptr_t Key

A key to identify a specific token.

A node that has children and represents a syntactic language construct.

Definition Tree.h:144

const Node * getLastChild() const

Definition Tree.h:182

static bool classof(const Node *N)

friend class MutationsImpl

Definition Tree.h:244

const Leaf * findLastLeaf() const

friend class TreeBuilder

Definition Tree.h:234

const Leaf * findFirstLeaf() const

Node * findChild(NodeRole R)

Definition Tree.h:214

Node * getLastChild()

Definition Tree.h:181

Node(NodeKind Kind)

Newly created nodes are detached from a tree, parent and sibling links are set when the node is added...

Node * getFirstChild()

Definition Tree.h:179

Leaf * findLastLeaf()

Definition Tree.h:190

const Node * findChild(NodeRole R) const

Find the first node with a corresponding role.

Leaf * findFirstLeaf()

Definition Tree.h:185

llvm::iterator_range< ChildIterator > getChildren()

Definition Tree.h:205

friend class FactoryImpl

Definition Tree.h:235

const Node * getFirstChild() const

Definition Tree.h:180

llvm::iterator_range< ConstChildIterator > getChildren() const

Definition Tree.h:208

NodeRole

A relation between a parent and child node, e.g.

NodeKind

A kind of a syntax node, used for implementing casts.

TokenKind

Provides a simple uniform namespace for tokens from all C languages.

The JSON file list parser is used to communicate input to InstallAPI.

bool operator==(const CallGraphNode::CallRecord &LHS, const CallGraphNode::CallRecord &RHS)

Element * element

Definition Tree.h:257

Leaf * delimiter

Definition Tree.h:258

child_iterator is not invalidated by mutations.

Definition Tree.h:195

ConstChildIterator()=default

ConstChildIterator(const ChildIterator &I)

Definition Tree.h:202