-
Notifications
You must be signed in to change notification settings - Fork 644
Expand file tree
/
Copy patht_named.cc
More file actions
90 lines (77 loc) · 2.73 KB
/
Copy patht_named.cc
File metadata and controls
90 lines (77 loc) · 2.73 KB
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <thrift/compiler/ast/t_named.h>
#include <thrift/compiler/ast/t_const.h>
#include <thrift/compiler/ast/t_program.h>
#include <thrift/compiler/ast/t_type.h>
#include <thrift/compiler/ast/uri.h>
#include <fmt/format.h>
namespace apache::thrift::compiler {
t_named::t_named(const t_program* program, std::string name)
: name_(std::move(name)), program_(program) {}
t_named::t_named(const t_named& named)
: t_node::t_node(named),
name_(named.name_),
program_(named.program_),
generated_(named.generated_),
uri_(named.uri_),
explicit_uri_(named.explicit_uri_) {
for (const auto& annotation : named.structured_annotations_) {
add_structured_annotation(annotation->clone());
}
}
// NOTE: Must be defined here for t_const's destructor definition.
t_named::~t_named() = default;
std::string t_named::get_scoped_name() const {
return program_ ? fmt::format("{}.{}", program_->name(), name_) : name_;
}
void t_named::add_structured_annotation(std::unique_ptr<t_const> annot) {
assert(!annot->type_ref().empty());
structured_annotations_.emplace_back(std::move(annot));
}
bool t_named::has_structured_annotation(const char* uri) const {
return find_structured_annotation_or_null(uri) != nullptr;
}
const t_const* t_named::find_structured_annotation_or_null(
const char* uri) const {
for (const auto& annotation : structured_annotations_) {
if (annotation->type() == nullptr) {
continue;
}
const t_type& annotation_type = *annotation->type();
if (annotation_type.uri() == uri) {
return annotation.get();
}
if (is_transitive_annotation(annotation_type)) {
if (const t_const* annot =
annotation_type.find_structured_annotation_or_null(uri)) {
return annot;
}
}
}
return nullptr;
}
bool is_transitive_annotation(const t_named& node) {
for (const auto& annotation : node.structured_annotations()) {
if (const t_type* annotation_type = annotation.type();
annotation_type != nullptr &&
annotation_type->uri() == kTransitiveUri) {
return true;
}
}
return false;
}
} // namespace apache::thrift::compiler