-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathSyntaxNodes.cpp.gyb
119 lines (108 loc) · 3.86 KB
/
SyntaxNodes.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
%{
# -*- mode: C++ -*-
from gyb_syntax_support import *
NODE_MAP = create_node_map()
# Ignore the following admonition; it applies to the resulting .cpp file only
}%
//// Automatically Generated From SyntaxNodes.cpp.gyb.
//// Do Not Edit Directly!
//===---------------- SyntaxNodes.cpp - Syntax Node definitions -----------===//
//
// 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/SyntaxNodes.h"
using namespace swift;
using namespace swift::syntax;
% for node in SYNTAX_NODES:
% if not node.is_syntax_collection():
void ${node.name}Ref::validate() const {
% if node.requires_validation():
#ifndef NDEBUG
auto raw = getDataRef()->getRaw();
if (isMissing()) return;
assert(raw->getLayout().size() == ${len(node.children)});
% for child in node.children:
% if child.token_choices:
% choices = ", ".join("tok::" + choice.kind
% for choice in child.token_choices)
syntax_assert_child_token(raw, ${child.name}, ${choices});
% end
% if child.main_token() and child.text_choices:
% token_kind = child.main_token().kind
% choices = ", ".join("\"%s\"" % choice
% for choice in child.text_choices)
syntax_assert_child_token_text(raw, ${child.name},
tok::${token_kind}, ${choices});
% end
% if child.node_choices:
if (auto __Child = raw->getChild(Cursor::${child.name}))
assert(${check_child_condition_raw(child)}(__Child));
% end
% end
#endif
% end
}
void ${node.name}::validate() const {
${node.name}Ref(*this).validate();
}
% end
% for child in node.children:
% if child.is_optional:
llvm::Optional<${child.type_name}> ${node.name}::get${child.name}() const {
auto ChildData = Data->getChild(Cursor::${child.name});
if (!ChildData)
return llvm::None;
return ${child.type_name}(ChildData);
}
% else:
${child.type_name} ${node.name}::get${child.name}() const {
return ${child.type_name}(Data->getChild(Cursor::${child.name}));
}
% end
% child_node = NODE_MAP.get(child.syntax_kind)
% if child_node and child_node.is_syntax_collection():
% child_elt = child.collection_element_name
% child_elt_type = child_node.collection_element_type
% if not child_elt:
% raise Exception("'collection_element_name' should be set for '%s' of '%s'" % (child.name, node.name))
% end
${node.name} ${node.name}::add${child_elt}(${child_elt_type} ${child_elt}) {
const RawSyntax *raw = getRaw()->getChild(Cursor::${child.name});
if (raw)
raw = raw->append(${child_elt}.getRaw());
else
raw = RawSyntax::make(SyntaxKind::${child_node.syntax_kind},
{${child_elt}.getRaw()}, SourcePresence::Present, raw->getArena());
return ${node.name}(Data->replacingChild(raw, Cursor::${child.name}));
}
% end
${node.name} ${node.name}::with${child.name}(
llvm::Optional<${child.type_name}> New${child.type_name}) {
const RawSyntax *raw;
if (New${child.type_name}.hasValue()) {
raw = New${child.type_name}->getRaw();
} else {
% if child.is_optional:
raw = nullptr;
% else:
{
// make_missing_child access the 'Arena' variable. Create it.
RC<SyntaxArena> Arena = getRaw()->getArena();
raw = ${make_missing_child(child)};
}
% end
}
return ${node.name}(Data->replacingChild(raw, Cursor::${child.name}));
}
% end
% end
const char* swift::syntax::getSyntaxStructureVersioningIdentifier() {
return "${calculate_node_hash()}";
}