forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyntaxFactory.cpp.gyb
305 lines (279 loc) · 10.1 KB
/
SyntaxFactory.cpp.gyb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
%{
from gyb_syntax_support import *
# -*- mode: C++ -*-
# Ignore the following admonition; it applies to the resulting .cpp file only
}%
//// Automatically Generated From SyntaxFactory.cpp.gyb.
//// Do Not Edit Directly!
//===--------- SyntaxFactory.cpp - Syntax Factory implementations ---------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/Syntax/SyntaxFactory.h"
#include "swift/Syntax/SyntaxNodes.h"
#include "swift/Syntax/Trivia.h"
#include "llvm/ADT/ArrayRef.h"
#include <vector>
using namespace swift;
using namespace swift::syntax;
TokenSyntax SyntaxFactory::makeToken(tok Kind, StringRef Text,
StringRef LeadingTrivia,
StringRef TrailingTrivia,
SourcePresence Presence) {
return makeRoot<TokenSyntax>(RawSyntax::makeAndCalcLength(Kind, Text,
LeadingTrivia, TrailingTrivia, Presence, Arena));
}
UnknownSyntax
SyntaxFactory::makeUnknownSyntax(llvm::ArrayRef<TokenSyntax> Tokens) {
auto RawTokens = llvm::map_iterator(Tokens.begin(),
[](const TokenSyntax &Token) -> const RawSyntax * {
return Token.getRaw();
});
auto Raw = RawSyntax::make(SyntaxKind::Unknown, RawTokens, Tokens.size(),
SourcePresence::Present, Arena);
return makeRoot<UnknownSyntax>(Raw);
}
Syntax SyntaxFactory::makeBlankCollectionSyntax(SyntaxKind Kind) {
switch(Kind) {
% for node in SYNTAX_NODES:
% if node.is_syntax_collection():
case SyntaxKind::${node.syntax_kind}:
return makeBlank${node.syntax_kind}();
% end
% end
default: break;
}
llvm_unreachable("not collection kind.");
}
std::pair<unsigned, unsigned>
SyntaxFactory::countChildren(SyntaxKind Kind){
switch(Kind) {
% for node in SYNTAX_NODES:
% if not node.is_syntax_collection():
case SyntaxKind::${node.syntax_kind}:
% child_count = len(node.children)
% non_optional_child_count = sum(0 if child.is_optional else 1 for child in node.children)
return {${non_optional_child_count}, ${child_count}};
% end
% end
default:
llvm_unreachable("bad syntax kind.");
}
}
bool SyntaxFactory::canServeAsCollectionMemberRaw(SyntaxKind CollectionKind,
SyntaxKind MemberKind) {
switch (CollectionKind) {
% for node in SYNTAX_NODES:
% if node.is_syntax_collection():
case SyntaxKind::${node.syntax_kind}:
% if node.collection_element_choices:
% element_checks = []
% for choice in node.collection_element_choices:
% element_checks.append("" + choice + "Syntax::kindof(MemberKind)")
% end
return ${' || '.join(element_checks)};
% else:
return ${node.collection_element_type}::kindof(MemberKind);
% end
% end
% end
default:
llvm_unreachable("Not collection kind.");
}
}
bool SyntaxFactory::canServeAsCollectionMemberRaw(SyntaxKind CollectionKind,
const RawSyntax *Member) {
return canServeAsCollectionMemberRaw(CollectionKind, Member->getKind());
}
bool SyntaxFactory::
canServeAsCollectionMember(SyntaxKind CollectionKind, Syntax Member) {
return canServeAsCollectionMemberRaw(CollectionKind, Member.getRaw());
}
const RawSyntax *SyntaxFactory::createRaw(
SyntaxKind Kind,
llvm::ArrayRef<const RawSyntax *> Elements
) {
switch (Kind) {
% for node in SYNTAX_NODES:
case SyntaxKind::${node.syntax_kind}: {
% if node.children:
% child_count = len(node.children)
const RawSyntax *Layout[${child_count}];
unsigned I = 0;
% for (child_idx, child) in enumerate(node.children):
// child[${child_idx}] ${child.name}
if (I == Elements.size() ||
!${check_child_condition_raw(child)}(Elements[I])) {
% if child.is_optional:
Layout[${child_idx}] = nullptr;
% else:
return nullptr;
% end
} else {
Layout[${child_idx}] = Elements[I];
++I;
}
% end
if (I != Elements.size())
return nullptr;
return RawSyntax::make(Kind, Layout, SourcePresence::Present, Arena);
% elif node.is_syntax_collection():
for (auto &E : Elements) {
if (!canServeAsCollectionMemberRaw(SyntaxKind::${node.syntax_kind}, E))
return nullptr;
}
return RawSyntax::make(Kind, Elements, SourcePresence::Present, Arena);
% else:
return nullptr;
% end
}
% end
default:
return nullptr;
}
}
Optional<Syntax> SyntaxFactory::createSyntax(SyntaxKind Kind,
llvm::ArrayRef<Syntax> Elements) {
std::vector<const RawSyntax *> Layout;
Layout.reserve(Elements.size());
for (auto &E : Elements)
Layout.emplace_back(E.getRaw());
if (auto Raw = createRaw(Kind, Layout))
return makeRoot<Syntax>(Raw);
else
return None;
}
% for node in SYNTAX_NODES:
% if node.children:
% child_params = []
% for child in node.children:
% param_type = child.type_name
% if child.is_optional:
% param_type = "llvm::Optional<%s>" % param_type
% child_params.append("%s %s" % (param_type, child.name))
% child_params = ', '.join(child_params)
${node.name}
SyntaxFactory::make${node.syntax_kind}(${child_params}) {
auto Raw = RawSyntax::make(SyntaxKind::${node.syntax_kind}, {
% for child in node.children:
% if child.is_optional:
${child.name}.hasValue() ? ${child.name}->getRaw() : nullptr,
% else:
${child.name}.getRaw(),
% end
% end
}, SourcePresence::Present, Arena);
return makeRoot<${node.name}>(Raw);
}
% elif node.is_syntax_collection():
${node.name}
SyntaxFactory::make${node.syntax_kind}(
const std::vector<${node.collection_element_type}> &elements) {
std::vector<const RawSyntax *> layout;
layout.reserve(elements.size());
for (auto &element : elements) {
layout.push_back(element.getRaw());
}
auto raw = RawSyntax::make(SyntaxKind::${node.syntax_kind}, layout,
SourcePresence::Present, Arena);
return makeRoot<${node.name}>(raw);
}
% end
${node.name}
SyntaxFactory::makeBlank${node.syntax_kind}() {
auto raw = RawSyntax::make(SyntaxKind::${node.syntax_kind}, {
% for child in node.children:
% if child.is_optional:
nullptr,
% else:
${make_missing_child(child)},
% end
% end
}, SourcePresence::Present, Arena);
return makeRoot<${node.name}>(raw);
}
% end
% for token in SYNTAX_TOKENS:
% if token.is_keyword:
TokenSyntax
SyntaxFactory::make${token.name}Keyword(StringRef LeadingTrivia,
StringRef TrailingTrivia) {
return makeToken(tok::${token.kind}, "${token.text}", LeadingTrivia,
TrailingTrivia, SourcePresence::Present);
}
% elif token.text:
TokenSyntax
SyntaxFactory::make${token.name}Token(StringRef LeadingTrivia,
StringRef TrailingTrivia) {
return makeToken(tok::${token.kind}, "${token.text}", LeadingTrivia,
TrailingTrivia, SourcePresence::Present);
}
% else:
TokenSyntax
SyntaxFactory::make${token.name}(StringRef Text,
StringRef LeadingTrivia,
StringRef TrailingTrivia) {
return makeToken(tok::${token.kind}, Text, LeadingTrivia, TrailingTrivia,
SourcePresence::Present);
}
% end
% end
TupleTypeSyntax SyntaxFactory::makeVoidTupleType() {
return makeTupleType(makeLeftParenToken({}, {}),
makeBlankTupleTypeElementList(),
makeRightParenToken({}, {}));
}
TupleTypeElementSyntax
SyntaxFactory::makeTupleTypeElement(llvm::Optional<TokenSyntax> Label,
llvm::Optional<TokenSyntax> Colon,
TypeSyntax Type,
llvm::Optional<TokenSyntax> TrailingComma) {
return makeTupleTypeElement(None, Label, None, Colon, Type, None, None,
TrailingComma);
}
TupleTypeElementSyntax
SyntaxFactory::makeTupleTypeElement(TypeSyntax Type,
llvm::Optional<TokenSyntax> TrailingComma) {
return makeTupleTypeElement(None, None, None, None, Type, None, None,
TrailingComma);
}
GenericParameterSyntax
SyntaxFactory::makeGenericParameter(TokenSyntax Name,
llvm::Optional<TokenSyntax> TrailingComma) {
return makeGenericParameter(None, Name, None, None, TrailingComma);
}
TypeSyntax SyntaxFactory::makeTypeIdentifier(StringRef TypeName,
StringRef LeadingTrivia,
StringRef TrailingTrivia) {
auto identifier =
makeIdentifier(TypeName, LeadingTrivia, TrailingTrivia);
return makeSimpleTypeIdentifier(identifier, None);
}
TypeSyntax SyntaxFactory::makeAnyTypeIdentifier(StringRef LeadingTrivia,
StringRef TrailingTrivia) {
return makeTypeIdentifier("Any", LeadingTrivia, TrailingTrivia);
}
TypeSyntax SyntaxFactory::makeSelfTypeIdentifier(StringRef LeadingTrivia,
StringRef TrailingTrivia) {
return makeTypeIdentifier("Self", LeadingTrivia, TrailingTrivia);
}
TokenSyntax SyntaxFactory::makeTypeToken(StringRef LeadingTrivia,
StringRef TrailingTrivia) {
return makeIdentifier("Type", LeadingTrivia, TrailingTrivia);
}
TokenSyntax SyntaxFactory::makeProtocolToken(StringRef LeadingTrivia,
StringRef TrailingTrivia) {
return makeIdentifier("Protocol", LeadingTrivia, TrailingTrivia);
}
TokenSyntax SyntaxFactory::makeEqualityOperator(StringRef LeadingTrivia,
StringRef TrailingTrivia) {
return makeToken(tok::oper_binary_spaced, "==", LeadingTrivia, TrailingTrivia,
SourcePresence::Present);
}